diff --git a/app/Actions/Notifications/SendHealthCheckTestNotification.php b/app/Actions/Notifications/SendHealthCheckTestNotification.php new file mode 100644 index 000000000..f37e20fad --- /dev/null +++ b/app/Actions/Notifications/SendHealthCheckTestNotification.php @@ -0,0 +1,37 @@ +title('You need to add HealthCheck.io urls!') + ->warning() + ->send(); + + return; + } + + foreach ($webhooks as $webhook) { + WebhookCall::create() + ->url($webhook['url']) + ->payload(['message' => '👋 Testing the HealthCheck.io notification channel.']) + ->doNotSign() + ->dispatch(); + } + + Notification::make() + ->title('Test HealthCheck.io notification sent.') + ->success() + ->send(); + } +} diff --git a/app/Filament/Pages/Settings/NotificationPage.php b/app/Filament/Pages/Settings/NotificationPage.php index c11e56d54..652ec4a9f 100755 --- a/app/Filament/Pages/Settings/NotificationPage.php +++ b/app/Filament/Pages/Settings/NotificationPage.php @@ -5,6 +5,7 @@ use App\Actions\Notifications\SendDatabaseTestNotification; use App\Actions\Notifications\SendDiscordTestNotification; use App\Actions\Notifications\SendGotifyTestNotification; +use App\Actions\Notifications\SendHealthCheckTestNotification; use App\Actions\Notifications\SendMailTestNotification; use App\Actions\Notifications\SendSlackTestNotification; use App\Actions\Notifications\SendTelegramTestNotification; @@ -261,6 +262,51 @@ public function form(Form $form): Form 'md' => 2, ]), + Forms\Components\Section::make('Healthcheck.io') + ->schema([ + Forms\Components\Toggle::make('healthcheck_enabled') + ->label('Enable healthcheck.io webhook notifications') + ->reactive() + ->columnSpanFull(), + Forms\Components\Grid::make([ + 'default' => 1, + ]) + ->hidden(fn (Forms\Get $get) => $get('healthcheck_enabled') !== true) + ->schema([ + Forms\Components\Fieldset::make('Triggers') + ->schema([ + Forms\Components\Toggle::make('healthcheck_on_speedtest_run') + ->label('Notify on every speedtest run') + ->columnSpanFull(), + Forms\Components\Toggle::make('healthcheck_on_threshold_failure') + ->label('Notify on threshold failures') + ->helperText('Threshold notifications will be sent to the /fail path of the URL.') + ->columnSpanFull(), + ]), + Forms\Components\Repeater::make('healthcheck_webhooks') + ->label('webhooks') + ->schema([ + Forms\Components\TextInput::make('url') + ->placeholder('https://hc-ping.com/your-uuid-here') + ->maxLength(2000) + ->required() + ->url(), + ]) + ->columnSpanFull(), + Forms\Components\Actions::make([ + Forms\Components\Actions\Action::make('test healthcheck') + ->label('Test healthcheck.io webhook') + ->action(fn (Forms\Get $get) => SendHealthCheckTestNotification::run(webhooks: $get('healthcheck_webhooks'))) + ->hidden(fn (Forms\Get $get) => ! count($get('healthcheck_webhooks'))), + ]), + ]), + ]) + ->compact() + ->columns([ + 'default' => 1, + 'md' => 2, + ]), + Forms\Components\Section::make('Telegram') ->schema([ Forms\Components\Toggle::make('telegram_enabled') diff --git a/app/Listeners/HealthCheck/SendSpeedtestCompletedNotification.php b/app/Listeners/HealthCheck/SendSpeedtestCompletedNotification.php new file mode 100644 index 000000000..a7936c815 --- /dev/null +++ b/app/Listeners/HealthCheck/SendSpeedtestCompletedNotification.php @@ -0,0 +1,50 @@ +healthcheck_enabled) { + return; + } + + if (! $notificationSettings->healthcheck_on_speedtest_run) { + return; + } + + if (! count($notificationSettings->healthcheck_webhooks)) { + Log::warning('healthcheck urls not found, check healthcheck notification channel settings.'); + + return; + } + + foreach ($notificationSettings->healthcheck_webhooks as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload([ + 'result_id' => $event->result->id, + 'site_name' => config('app.name'), + 'isp' => $event->result->isp, + 'ping' => $event->result->ping, + 'download' => $event->result->downloadBits, + 'upload' => $event->result->uploadBits, + 'packetLoss' => $event->result->packet_loss, + 'url' => url('/admin/results'), + ]) + ->doNotSign() + ->dispatch(); + } + } +} diff --git a/app/Listeners/HealthCheck/SendSpeedtestThresholdNotification.php b/app/Listeners/HealthCheck/SendSpeedtestThresholdNotification.php new file mode 100644 index 000000000..76bb3833d --- /dev/null +++ b/app/Listeners/HealthCheck/SendSpeedtestThresholdNotification.php @@ -0,0 +1,125 @@ +healthcheck_enabled) { + return; + } + + if (! $notificationSettings->healthcheck_on_threshold_failure) { + return; + } + + if (! count($notificationSettings->healthcheck_webhooks)) { + Log::warning('HealthCheck urls not found, check healthcheck 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 healthcheck thresholds not found, won\'t send notification.'); + + return; + } + + foreach ($notificationSettings->healthcheck_webhooks as $url) { + WebhookCall::create() + ->url($url['url'].'/fail') + ->payload([ + 'result_id' => $event->result->id, + 'site_name' => config('app.name'), + 'isp' => $event->result->isp, + 'metrics' => $failed, + 'url' => url('/admin/results'), + ]) + ->doNotSign() + ->dispatch(); + } + } + + /** + * Build HealthCheck 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 Healthcheck 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 Healthcheck 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 4c8063799..bf88f72a4 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 $healthcheck_enabled; + + public bool $healthcheck_on_speedtest_run; + + public bool $healthcheck_on_threshold_failure; + + public ?array $healthcheck_webhooks; + public bool $slack_enabled; public bool $slack_on_speedtest_run; diff --git a/database/settings/2024_02_22_144620_create_healthcheck_notification_settings.php b/database/settings/2024_02_22_144620_create_healthcheck_notification_settings.php new file mode 100644 index 000000000..b5dd742bb --- /dev/null +++ b/database/settings/2024_02_22_144620_create_healthcheck_notification_settings.php @@ -0,0 +1,14 @@ +migrator->add('notification.healthcheck_enabled', false); + $this->migrator->add('notification.healthcheck_on_speedtest_run', false); + $this->migrator->add('notification.healthcheck_on_threshold_failure', false); + $this->migrator->add('notification.healthcheck_webhooks', null); + } +};