-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create and configure custom web status checker [API-417] #90
Open
web-dev-trev
wants to merge
1
commit into
develop
Choose a base branch
from
feature/pingdom
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Facades\Log; | ||
use Carbon\Carbon; | ||
use Carbon\CarbonInterface; | ||
|
||
class WebStatusCheck extends Command | ||
{ | ||
protected $signature = 'monitor:website {domains*}'; | ||
protected $description = 'Monitor specified domains for non-200 responses'; | ||
|
||
private const INITIAL_WAIT = 60; | ||
private const ALERT_INTERVALS = [300, 120, 60]; | ||
private const MAX_ALERTS = 7; | ||
|
||
private $domainStates = []; | ||
|
||
public function handle() | ||
{ | ||
$domains = $this->argument('domains'); | ||
|
||
// Setup states for domains | ||
foreach ($domains as $domain) { | ||
$this->domainStates[$domain] = [ | ||
'firstAlertTime' => null, | ||
'alertCount' => 0, | ||
'lastAlertTime' => null, | ||
'currentWaitIndex' => 0, | ||
]; | ||
} | ||
|
||
$this->info('Starting monitoring of domains: ' . implode(', ', $domains)); | ||
|
||
while (true) { | ||
foreach ($domains as $domain) { | ||
try { | ||
$response = Http::get("https://{$domain}"); | ||
|
||
if ($response->status() !== 200) { | ||
$this->handleNon200Response($domain, $response->status()); | ||
} elseif ($this->domainStates[$domain]['alertCount'] > 0) { | ||
// Site is back up after previous alerts | ||
$this->sendSlackMessage("{$domain} is back up :blob_excited:"); | ||
$this->resetMonitoring($domain); | ||
} | ||
|
||
// If we haven't hit max alerts wait for next check | ||
if ( | ||
$this->domainStates[$domain]['alertCount'] < self::MAX_ALERTS || | ||
$this->domainStates[$domain]['alertCount'] === self::MAX_ALERTS | ||
) { | ||
$waitTime = $this->getWaitTime($domain); | ||
sleep($waitTime); | ||
} | ||
} catch (\Exception $e) { | ||
$this->info("Error monitoring {$domain}: " . $e->getMessage()); | ||
Log::error("Monitoring error for {$domain}: " . $e->getMessage()); | ||
Comment on lines
+60
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you just use |
||
sleep(60); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private function handleNon200Response(string $domain, int $responseCode) | ||
{ | ||
$currentTime = Carbon::now(); | ||
$state = &$this->domainStates[$domain]; | ||
|
||
if ($state['alertCount'] === 0) { | ||
// First alert | ||
$state['firstAlertTime'] = $currentTime; | ||
$state['lastAlertTime'] = $currentTime; | ||
$state['alertCount']++; | ||
$this->sendSlackMessage( | ||
"{$domain} responded {$responseCode} :eyes:" | ||
); | ||
sleep(self::INITIAL_WAIT); | ||
return; | ||
} | ||
|
||
// Only send alerts if we haven't reached the maximum | ||
if ($state['alertCount'] < self::MAX_ALERTS) { | ||
$downtime = $currentTime->diffForHumans($state['firstAlertTime'], [ | ||
'syntax' => CarbonInterface::DIFF_ABSOLUTE, | ||
]); | ||
$this->sendSlackMessage( | ||
"{$domain} responded {$responseCode} for {$downtime} :ahhhhhhhhh:" | ||
); | ||
$state['alertCount']++; | ||
$state['lastAlertTime'] = $currentTime; | ||
|
||
// Move to next wait interval | ||
if ($state['currentWaitIndex'] < count(self::ALERT_INTERVALS) - 1) { | ||
$state['currentWaitIndex']++; | ||
} | ||
} | ||
} | ||
|
||
private function getWaitTime(string $domain): int | ||
{ | ||
$state = $this->domainStates[$domain]; | ||
|
||
if ($state['alertCount'] === 0) { | ||
return 60; // Default check interval when everything is normal | ||
} | ||
|
||
if ($state['alertCount'] === self::MAX_ALERTS) { | ||
return 60; // After max alerts just keep checking every minute | ||
} | ||
|
||
return self::ALERT_INTERVALS[$state['currentWaitIndex']]; | ||
} | ||
|
||
private function sendSlackMessage(string $message) | ||
{ | ||
$webhookUrl = config('aic.monitoring.slack.webhook_url'); | ||
try { | ||
Http::post($webhookUrl, [ | ||
'text' => $message | ||
]); | ||
} catch (\Exception $e) { | ||
Log::error('Failed to send Slack message: ' . $e->getMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider |
||
} | ||
} | ||
|
||
private function resetMonitoring(string $domain) | ||
{ | ||
$this->domainStates[$domain] = [ | ||
'firstAlertTime' => null, | ||
'alertCount' => 0, | ||
'lastAlertTime' => null, | ||
'currentWaitIndex' => 0, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we expect this command always to be running?
while (true)
worries me. I wonder if, instead, we can think of this command as:Then we can think of the code in this script as a single iteration, and have the Kernel below set to run the command every minute.