Skip to content

Commit

Permalink
Merge pull request #4 from opsie-app/feature/logging
Browse files Browse the repository at this point in the history
[feature] Logging
  • Loading branch information
rennokki authored Dec 13, 2021
2 parents 6885afb + 73a474b commit 0bd94d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/Commands/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
3 changes: 3 additions & 0 deletions app/Concerns/SendsWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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');
}
}
}
22 changes: 20 additions & 2 deletions app/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Concerns\MonitorsSsl;
use App\Concerns\SendsWebhooks;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;

class Monitor
{
Expand Down Expand Up @@ -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://');
}
Expand All @@ -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);
Expand All @@ -78,6 +94,8 @@ public function run(): void
'metadata' => $this->metadata,
]);

$this->cli->line('Check done.', verbosity: 'v');

if ($this->once) {
break;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/MonitoringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0bd94d4

Please sign in to comment.