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
* Make CRUD possible from the backend for adding tasks and contacts
* Multithreading
* SMS Notifications
* Protected backend with authentication
* Create an installation script
* 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
* Add a notification history log
* Keep track of tasks response time
* Daemonize the script (instead of CRONs)
[ ] Make CRUD possible from the backend for adding tasks and contacts
[ ] Multithreading
[ ] SMS Notifications
[ ] Protected backend with authentication
[ ] Create an installation script
[ ] Raise alert when tasks are not run at the correct frequency (CRON down or other reason)
[x] Set a notification capping limit to prevent many notifications to be sent in case of an up-and-down host
[x] Add a notification history log
[x] Keep track of tasks response time
[ ] Daemonize the script (instead of CRONs)

View file

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

View file

@ -73,7 +73,7 @@ class ApiController extends Controller
->history()
->orderBy('created_at', 'desc')
->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()
;

File diff suppressed because one or more lines are too long

View file

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