Keeping track of duration

This commit is contained in:
Axel 2021-12-24 10:23:04 +01:00
parent 1e0d78f67b
commit 9ead771d6b
5 changed files with 36 additions and 21 deletions

View file

@ -80,13 +80,13 @@ It comes with a very straightforward dashboard written in PHP. This is **optiona
## TODO ## TODO
* Make CRUD possible from the backend for adding tasks and contacts [ ] Make CRUD possible from the backend for adding tasks and contacts
* Multithreading [ ] Multithreading
* SMS Notifications [ ] SMS Notifications
* Protected backend with authentication [ ] Protected backend with authentication
* Create an installation script [ ] Create an installation script
* Raise alert when tasks are not run at the correct frequency (CRON down or other reason) [ ] Raise alert when tasks are not run at the correct frequency (CRON down or other reason)
* Set a notification capping limit to prevent many notifications to be sent in case of an up-and-down host [x] Set a notification capping limit to prevent many notifications to be sent in case of an up-and-down host
* Add a notification history log [x] Add a notification history log
* Keep track of tasks response time [x] Keep track of tasks response time
* Daemonize the script (instead of CRONs) [ ] Daemonize the script (instead of CRONs)

View file

@ -115,11 +115,11 @@ class RunMonitoring extends Command
try { try {
switch ($task->type) { switch ($task->type) {
case 'ping': case 'ping':
$new_status = $this->checkPing($task); $result = $this->checkPing($task);
break; break;
case 'http': case 'http':
$new_status = $this->checkHttp($task); $result = $this->checkHttp($task);
break; break;
default: default:
@ -127,12 +127,14 @@ class RunMonitoring extends Command
continue 2; continue 2;
} }
$history = $this->saveHistory($task, true); $new_status = 1;
$history = $this->saveHistory($task, true, 'success', $result['duration'] ?? null);
} }
catch(MonitoringException $e) { catch(MonitoringException $e) {
$history = $this->saveHistory($task, false, $e->getMessage()); $history = $this->saveHistory($task, false, $e->getMessage());
} }
catch(Exception $e) { catch(Exception $e) {
//TODO: handle system exception differently
$history = $this->saveHistory($task, false, $e->getMessage()); $history = $this->saveHistory($task, false, $e->getMessage());
} }
finally { finally {
@ -177,7 +179,7 @@ class RunMonitoring extends Command
} }
} }
final private function saveHistory(Task $task, $status, $output = null) { final private function saveHistory(Task $task, $status, $output = null, $duration = null) {
$date = date('Y-m-d H:i:s'); $date = date('Y-m-d H:i:s');
// Inserting new history // Inserting new history
@ -185,6 +187,7 @@ class RunMonitoring extends Command
$insert->status = $status === true ? 1 : 0; $insert->status = $status === true ? 1 : 0;
$insert->created_at = $date; $insert->created_at = $date;
$insert->output = $output ?? ''; $insert->output = $output ?? '';
$insert->duration = $duration;
$insert->task_id = $task->id; $insert->task_id = $task->id;
if (! $insert->save()) { if (! $insert->save()) {
throw new Exception('Cannot insert history for task #'.$task->id); throw new Exception('Cannot insert history for task #'.$task->id);
@ -254,6 +257,7 @@ class RunMonitoring extends Command
// Preparing cURL // Preparing cURL
$opts = [ $opts = [
CURLOPT_HEADER => true,
CURLOPT_HTTPGET => true, CURLOPT_HTTPGET => true,
CURLOPT_FRESH_CONNECT => true, CURLOPT_FRESH_CONNECT => true,
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
@ -262,6 +266,7 @@ class RunMonitoring extends Command
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 3, CURLOPT_MAXREDIRS => 3,
CURLOPT_FAILONERROR => true, CURLOPT_FAILONERROR => true,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_URL => trim($task->host) CURLOPT_URL => trim($task->host)
]; ];
@ -269,16 +274,24 @@ class RunMonitoring extends Command
$ch = curl_init(); $ch = curl_init();
curl_setopt_array($ch, $opts); curl_setopt_array($ch, $opts);
if ($result = curl_exec($ch)) { if ($result = curl_exec($ch)) {
$duration = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
// We have nothing to check into the page // We have nothing to check into the page
// So for me, this is a big YES // So for me, this is a big YES
if (empty($task->params)) { if (empty($task->params)) {
return true; return [
'result' => true,
'duration' => $duration
];
} }
// We are looking for a string in the page // We are looking for a string in the page
else { else {
if (strpos($result, $task->params) !== false) { if (strpos($result, $task->params) !== false) {
return true; return [
'result' => true,
'output' => 'String was found in the page',
'duration' => $duration
];
} }
else { else {
throw new MonitoringException('Cannot find the required string into the page'); throw new MonitoringException('Cannot find the required string into the page');

View file

@ -73,7 +73,7 @@ class ApiController extends Controller
->history() ->history()
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->where('created_at', '>', $last_days->toDateString()) ->where('created_at', '>', $last_days->toDateString())
->selectRaw('id, date(created_at) as date, created_at, status') ->selectRaw('id, date(created_at) as date, created_at, status, duration')
->get() ->get()
; ;

File diff suppressed because one or more lines are too long

View file

@ -34,9 +34,10 @@
<table id="tasks_tbl"> <table id="tasks_tbl">
<thead> <thead>
<tr> <tr>
<th width="20%">Date</th> <th width="10%">Date</th>
<th width="20%">Time</th> <th width="10%">Time</th>
<th width="*">Output</th> <th width="*">Output</th>
<th width="10%">Duration</th>
<th width="10%">Status</th> <th width="10%">Status</th>
</tr> </tr>
</thead> </thead>
@ -55,6 +56,7 @@
<i>No output</i> <i>No output</i>
</span> </span>
</td> </td>
<td>{{ h.duration }}s</td>
<td :class="statusText(h.status)"> <td :class="statusText(h.status)">
<img :src="'/img/'+statusText(h.status)+'.svg'" width="16" alt="Status" /> <img :src="'/img/'+statusText(h.status)+'.svg'" width="16" alt="Status" />
</td> </td>