diff --git a/app/Actions/Notifications/SendNtfyTestNotification.php b/app/Actions/Notifications/SendNtfyTestNotification.php new file mode 100644 index 000000000..8975febaa --- /dev/null +++ b/app/Actions/Notifications/SendNtfyTestNotification.php @@ -0,0 +1,49 @@ +title('You need to add ntfy urls!') + ->warning() + ->send(); + + return; + } + + foreach ($webhooks as $webhook) { + $webhookCall = WebhookCall::create() + ->url($webhook['url']) + ->payload([ + 'topic' => $webhook['topic'], + 'message' => '👋 Testing the ntfy notification channel.', + ]) + ->doNotSign(); + + // Only add authentication if username and password are provided + if (! empty($webhook['username']) && ! empty($webhook['password'])) { + $authHeader = 'Basic '.base64_encode($webhook['username'].':'.$webhook['password']); + $webhookCall->withHeaders([ + 'Authorization' => $authHeader, + ]); + } + + $webhookCall->dispatch(); + } + + Notification::make() + ->title('Test ntfy notification sent.') + ->success() + ->send(); + } +} diff --git a/app/Filament/Pages/Settings/NotificationPage.php b/app/Filament/Pages/Settings/NotificationPage.php index f3faa9687..bd7df5902 100755 --- a/app/Filament/Pages/Settings/NotificationPage.php +++ b/app/Filament/Pages/Settings/NotificationPage.php @@ -7,6 +7,7 @@ use App\Actions\Notifications\SendGotifyTestNotification; use App\Actions\Notifications\SendHealthCheckTestNotification; use App\Actions\Notifications\SendMailTestNotification; +use App\Actions\Notifications\SendNtfyTestNotification; use App\Actions\Notifications\SendPushoverTestNotification; use App\Actions\Notifications\SendSlackTestNotification; use App\Actions\Notifications\SendTelegramTestNotification; @@ -277,6 +278,64 @@ public function form(Form $form): Form 'md' => 2, ]), + Forms\Components\Section::make('Ntfy') + ->schema([ + Forms\Components\Toggle::make('ntfy_enabled') + ->label('Enable Ntfy webhook notifications') + ->reactive() + ->columnSpanFull(), + Forms\Components\Grid::make([ + 'default' => 1, + ]) + ->hidden(fn (Forms\Get $get) => $get('ntfy_enabled') !== true) + ->schema([ + Forms\Components\Fieldset::make('Triggers') + ->schema([ + Forms\Components\Toggle::make('ntfy_on_speedtest_run') + ->label('Notify on every speedtest run') + ->columnSpanFull(), + Forms\Components\Toggle::make('ntfy_on_threshold_failure') + ->label('Notify on threshold failures') + ->columnSpanFull(), + ]), + Forms\Components\Repeater::make('ntfy_webhooks') + ->label('Webhooks') + ->schema([ + Forms\Components\TextInput::make('url') + ->maxLength(2000) + ->placeholder('Your ntfy server url') + ->required() + ->url(), + Forms\Components\TextInput::make('topic') + ->label('Topic') + ->placeholder('Your ntfy Topic') + ->maxLength(200) + ->required(), + Forms\Components\TextInput::make('username') + ->label('Username') + ->placeholder('Username for Basic Auth (optional)') + ->maxLength(200), + Forms\Components\TextInput::make('password') + ->label('Password') + ->placeholder('Password for Basic Auth (optional)') + ->password() + ->maxLength(200), + ]) + ->columnSpanFull(), + Forms\Components\Actions::make([ + Forms\Components\Actions\Action::make('test ntfy') + ->label('Test Ntfy webhook') + ->action(fn (Forms\Get $get) => SendNtfyTestNotification::run(webhooks: $get('ntfy_webhooks'))) + ->hidden(fn (Forms\Get $get) => ! count($get('ntfy_webhooks'))), + ]), + ]), + ]) + ->compact() + ->columns([ + 'default' => 1, + 'md' => 2, + ]), + Forms\Components\Section::make('Mail') ->schema([ Forms\Components\Toggle::make('mail_enabled') diff --git a/app/Listeners/Ntfy/SendSpeedtestCompletedNotification.php b/app/Listeners/Ntfy/SendSpeedtestCompletedNotification.php new file mode 100644 index 000000000..585db1654 --- /dev/null +++ b/app/Listeners/Ntfy/SendSpeedtestCompletedNotification.php @@ -0,0 +1,68 @@ +ntfy_enabled) { + return; + } + + if (! $notificationSettings->ntfy_on_speedtest_run) { + return; + } + + if (! count($notificationSettings->ntfy_webhooks)) { + Log::warning('Ntfy urls not found, check Ntfy notification channel settings.'); + + return; + } + + $payload = + view('ntfy.speedtest-completed', [ + 'id' => $event->result->id, + 'service' => Str::title($event->result->service), + 'serverName' => $event->result->server_name, + 'serverId' => $event->result->server_id, + 'isp' => $event->result->isp, + 'ping' => round($event->result->ping).' ms', + 'download' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), + 'upload' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), + 'packetLoss' => $event->result->packet_loss, + 'url' => url('/admin/results'), + ])->render(); + + foreach ($notificationSettings->ntfy_webhooks as $url) { + $webhookCall = WebhookCall::create() + ->url($url['url']) + ->payload([ + 'topic' => $url['topic'], + 'message' => $payload, + ]) + ->doNotSign(); + + // Only add authentication if username and password are provided + if (! empty($url['username']) && ! empty($url['password'])) { + $authHeader = 'Basic '.base64_encode($url['username'].':'.$url['password']); + $webhookCall->withHeaders([ + 'Authorization' => $authHeader, + ]); + } + $webhookCall->dispatch(); + } + } +} diff --git a/app/Listeners/Ntfy/SendSpeedtestThresholdNotification.php b/app/Listeners/Ntfy/SendSpeedtestThresholdNotification.php new file mode 100644 index 000000000..62263f1b6 --- /dev/null +++ b/app/Listeners/Ntfy/SendSpeedtestThresholdNotification.php @@ -0,0 +1,143 @@ +ntfy_enabled) { + return; + } + + if (! $notificationSettings->ntfy_on_threshold_failure) { + return; + } + + if (! count($notificationSettings->ntfy_webhooks)) { + Log::warning('Ntfy urls not found, check Ntfy notification channel settings.'); + + return; + } + + $thresholdSettings = new ThresholdSettings(); + + if (! $thresholdSettings->absolute_enabled) { + return; + } + + $failed = []; + + if ($thresholdSettings->absolute_download > 0) { + array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + if ($thresholdSettings->absolute_upload > 0) { + array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + if ($thresholdSettings->absolute_ping > 0) { + array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + $failed = array_filter($failed); + + if (! count($failed)) { + Log::warning('Failed ntfy thresholds not found, won\'t send notification.'); + + return; + } + + $payload = + view('ntfy.speedtest-threshold', [ + 'id' => $event->result->id, + 'service' => Str::title($event->result->service), + 'serverName' => $event->result->server_name, + 'serverId' => $event->result->server_id, + 'isp' => $event->result->isp, + 'metrics' => $failed, + 'url' => url('/admin/results'), + ])->render(); + + foreach ($notificationSettings->ntfy_webhooks as $url) { + $webhookCall = WebhookCall::create() + ->url($url['url']) + ->payload([ + 'topic' => $url['topic'], + 'message' => $payload, + ]) + ->doNotSign(); + + // Only add authentication if username and password are provided + if (! empty($url['username']) && ! empty($url['password'])) { + $authHeader = 'Basic '.base64_encode($url['username'].':'.$url['password']); + $webhookCall->withHeaders([ + 'Authorization' => $authHeader, + ]); + } + + $webhookCall->dispatch(); + } + } + + /** + * Build Ntfy notification if absolute download threshold is breached. + */ + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array + { + if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { + return false; + } + + return [ + 'name' => 'Download', + 'threshold' => $thresholdSettings->absolute_download.' Mbps', + 'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), + ]; + } + + /** + * Build Ntfy notification if absolute upload threshold is breached. + */ + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array + { + if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { + return false; + } + + return [ + 'name' => 'Upload', + 'threshold' => $thresholdSettings->absolute_upload.' Mbps', + 'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), + ]; + } + + /** + * Build Ntfy notification if absolute ping threshold is breached. + */ + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array + { + if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { + return false; + } + + return [ + 'name' => 'Ping', + 'threshold' => $thresholdSettings->absolute_ping.' ms', + 'value' => round($event->result->ping, 2).' ms', + ]; + } +} diff --git a/app/Settings/NotificationSettings.php b/app/Settings/NotificationSettings.php index f41f962e6..0796be61a 100644 --- a/app/Settings/NotificationSettings.php +++ b/app/Settings/NotificationSettings.php @@ -46,6 +46,14 @@ class NotificationSettings extends Settings public ?array $discord_webhooks; + public bool $ntfy_enabled; + + public bool $ntfy_on_speedtest_run; + + public bool $ntfy_on_threshold_failure; + + public ?array $ntfy_webhooks; + public bool $pushover_enabled; public bool $pushover_on_speedtest_run; diff --git a/database/settings/2024_02_22_144650_create_ntfy_notification_settings.php b/database/settings/2024_02_22_144650_create_ntfy_notification_settings.php new file mode 100644 index 000000000..aca36e39f --- /dev/null +++ b/database/settings/2024_02_22_144650_create_ntfy_notification_settings.php @@ -0,0 +1,14 @@ +migrator->add('notification.ntfy_enabled', false); + $this->migrator->add('notification.ntfy_on_speedtest_run', false); + $this->migrator->add('notification.ntfy_on_threshold_failure', false); + $this->migrator->add('notification.ntfy_webhooks', null); + } +}; diff --git a/resources/views/ntfy/speedtest-completed.blade.php b/resources/views/ntfy/speedtest-completed.blade.php new file mode 100644 index 000000000..d51c75ce0 --- /dev/null +++ b/resources/views/ntfy/speedtest-completed.blade.php @@ -0,0 +1,12 @@ +Speedtest Completed - #{{ $id }} + +A new speedtest was completed using {{ $service }}. + +Server name: {{ $serverName }} +Server ID: {{ $serverId }} +ISP: {{ $isp }} +Ping: {{ $ping }} +Download: {{ $download }} +Upload: {{ $upload }} +Packet Loss: {{ $packetLoss }} % +URL: {{ $url }} diff --git a/resources/views/ntfy/speedtest-threshold.blade.php b/resources/views/ntfy/speedtest-threshold.blade.php new file mode 100644 index 000000000..a84ee245e --- /dev/null +++ b/resources/views/ntfy/speedtest-threshold.blade.php @@ -0,0 +1,8 @@ +Speedtest Threshold Breached - #{{ $id }} + +A new speedtest was completed using {{ $service }} on {{ $isp }}** but a threshold was breached. + +@foreach ($metrics as $item) +- {{ $item['name'] }} {{ $item['threshold'] }}: {{ $item['value'] }} +@endforeach +- URL: {{ $url }}