From 73a474b8f5bfe6395407ae18577547a0354e895b Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Tue, 14 Dec 2021 01:07:51 +0200 Subject: [PATCH] logging --- app/Commands/Monitor.php | 4 +++- app/Concerns/SendsWebhooks.php | 3 +++ app/Monitor.php | 22 ++++++++++++++++++++-- tests/Feature/MonitoringTest.php | 3 +++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/Commands/Monitor.php b/app/Commands/Monitor.php index e420eda..f532156 100644 --- a/app/Commands/Monitor.php +++ b/app/Commands/Monitor.php @@ -55,12 +55,14 @@ class Monitor extends Command */ public function handle() { - $this->monitor = AppMonitor::website($this->argument('url')); + $this->monitor = AppMonitor::website($this->argument('url'), $this); $this->configureHttp(); $this->configureWebhooks(); $this->configureDns(); + $this->line('Running the operator...', verbosity: 'v'); + $this->monitor->run(); } diff --git a/app/Concerns/SendsWebhooks.php b/app/Concerns/SendsWebhooks.php index 9aa9130..e9cdd6f 100644 --- a/app/Concerns/SendsWebhooks.php +++ b/app/Concerns/SendsWebhooks.php @@ -37,6 +37,7 @@ public function webhooks(array $webhooks) */ protected function deliverPayload(array $payload): void { + /** @var \App\Monitor $this */ foreach ($this->webhooks as $webhook) { WebhookCall::create() ->url($webhook['url']) @@ -46,6 +47,8 @@ protected function deliverPayload(array $payload): void ->timeoutInSeconds($this->timeout) ->withHeaders(['User-Agent' => 'Opsiebot/1.0']) ->dispatch(); + + $this->cli->line('['.now()->toIso8601String().'] Sent webhook to '.$webhook['url'], verbosity: 'v'); } } } diff --git a/app/Monitor.php b/app/Monitor.php index cde5213..3b8e911 100644 --- a/app/Monitor.php +++ b/app/Monitor.php @@ -7,6 +7,7 @@ use App\Concerns\MonitorsSsl; use App\Concerns\SendsWebhooks; use Illuminate\Support\Str; +use LaravelZero\Framework\Commands\Command; class Monitor { @@ -40,21 +41,24 @@ class Monitor * Create a new Monitor instance. * * @param string $url + * @param Command $cli * @return static */ - public static function website(string $url) + public static function website(string $url, Command $cli) { - return new static($url); + return new static($url, $cli); } /** * Create a new Monitor instance. * * @param string $url + * @param Command $cli * @return void */ public function __construct( protected string $url, + protected Command $cli, ) { $this->shouldCheckSsl = Str::startsWith($url, 'https://'); } @@ -66,7 +70,19 @@ public function __construct( */ public function run(): void { + if ($this->shouldCheckSsl) { + $this->cli->line('The SSL checks are enabled.'); + } + + if ($this->shouldCheckDns) { + $this->cli->line('The DNS checks and records retrievals are enabled.'); + } + + $this->cli->line("The checks will run at an interval of {$this->interval} seconds."); + while (true) { + $this->cli->line('['.now()->toIso8601String().'] Performing a check...', verbosity: 'v'); + $httpPayload = $this->checkHttp($this->url); $sslPayload = $this->checkSsl($this->url); $dnsPayload = $this->checkDns($this->url); @@ -78,6 +94,8 @@ public function run(): void 'metadata' => $this->metadata, ]); + $this->cli->line('Check done.', verbosity: 'v'); + if ($this->once) { break; } diff --git a/tests/Feature/MonitoringTest.php b/tests/Feature/MonitoringTest.php index e4b0a60..f179269 100644 --- a/tests/Feature/MonitoringTest.php +++ b/tests/Feature/MonitoringTest.php @@ -19,6 +19,7 @@ public function test_healthy_website() '--webhook-secret' => ['secret'], '--dns-checking' => true, '--once' => true, + '--verbose' => true, ]); Queue::assertPushed(CallWebhookJob::class, function ($job) { @@ -65,6 +66,7 @@ public function test_down_website() '--webhook-secret' => ['secret'], '--dns-checking' => true, '--once' => true, + '--verbose' => true, ]); Queue::assertPushed(CallWebhookJob::class, function ($job) { @@ -113,6 +115,7 @@ public function test_ssl_broken_website() '--webhook-secret' => ['secret'], '--dns-checking' => true, '--once' => true, + '--verbose' => true, ]); Queue::assertPushed(CallWebhookJob::class, function ($job) {