forked from totoCZ/Runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.php
More file actions
98 lines (75 loc) · 2.43 KB
/
cron.php
File metadata and controls
98 lines (75 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
if(!isset($dibi)){
require './db.php';
}
require './func.cron.php';
// main schedule
$date = date('c', strtotime('5 minutes ago'));
$to_check = dibi::query('select * from services where last_checked <= %t', $date)->fetchAll();
foreach($to_check as $service) {
usleep(200000);
$status1 = getStatus($service->type, $service->hostname, $service->port, $service->text_match);
// twice for safety
if($status1 === 9999) {
usleep(200000);
$status2 = getStatus($service->type, $service->hostname, $service->port, $service->text_match);
if ($status1 === 9999 && $status2 !== 9999) {
// #1 fail, #2 ok
$status = $status2;
} else {
// #1 fail, #2 fail
$status = 9999;
}
} else {
// #1 ok
$status = $status1;
}
$history = array(
'service_id' => $service->id,
'time' => new Datetime,
'response_ms' => $status
);
dibi::query('insert into history', $history);
if($status === 9999 && $service->response_ms !== 9999) {
// online -> OFFLINE
alertService($service);
$incident = array(
'user_id' => $service->user_id,
'service_id' => $service->id,
'name' => $service->hostname. ' ('.$service->type .':'. $service->port . ')',
'outage_start' => new DateTime,
'outage_end' => '0000-00-00 00:00:00'
);
dibi::insert('outages', $incident)->execute();
}
if($status === 9999) {
// OFFLINE -> OFFLINE
}
if($status !== 9999 && $service->response_ms === 9999) {
// OFFLINE -> online
$incident = array(
'outage_end' => new DateTime
);
dibi::update('outages', $incident)->where('service_id = %i', $service->id, 'and outage_end="00-00-0000 00:00:00"')->execute();
}
//if($status >= $service->alert_threshold) {
// Alert threshold reached
//}
$arr = array();
$arr['last_checked'] = new DateTime;
$arr['response_ms'] = $status;
// ignore 100% servers
if($service->availability != 100 || $status === 9999) {
$all = dibi::select('count(*)')->from('history')->where('service_id = %i', $service->id);
$off = dibi::select('count(*)')->from('history')->where('service_id = %i', $service->id)->where('response_ms = "9999"');
$all = $all->fetchSingle();
$offline = $off->fetchSingle();
$online = $all - $offline;
$arr['availability'] = round((($online / $offline) / $all * 100), 2);
}
dibi::query('update services set', $arr, 'where id = %i', $service->id);
}
// purge old data
$date = date('c', strtotime('30 days ago'));
dibi::query('delete from history where time <= %t', $date);
?>