1
|
|
|
<?php |
|
|
|
|
2
|
|
|
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; |
3
|
|
|
|
4
|
|
|
class Monitoring extends Base { |
|
|
|
|
5
|
|
|
protected $table = 'monitoring'; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Store Uptime Robot status information as JSON in settings table |
9
|
|
|
* @param none |
10
|
|
|
* @return bool true on success, false on error |
11
|
|
|
**/ |
12
|
|
|
public function storeUptimeRobotStatus() { |
13
|
|
|
if ($api_keys = $this->setting->getValue('monitoring_uptimerobot_api_keys')) { |
14
|
|
|
$aJSONData = array(); |
|
|
|
|
15
|
|
|
$url = 'https://api.uptimerobot.com'; |
16
|
|
|
$aMonitors = explode(',', $api_keys); |
17
|
|
|
foreach ($aMonitors as $aData) { |
18
|
|
|
$temp = explode('|', $aData); |
19
|
|
|
$aMonitor['api_key'] = trim($temp[0]); |
|
|
|
|
20
|
|
|
$aMonitor['monitor_id'] = trim($temp[1]); |
|
|
|
|
21
|
|
|
$target = '/getMonitors?apiKey=' . $aMonitor['api_key'] . '&monitors=' . $aMonitor['monitor_id'] . '&format=json&noJsonCallback=1&customUptimeRatio=1-7-30&logs=1'; |
22
|
|
|
$aMonitorStatus = $this->tools->getApi($url, $target); |
|
|
|
|
23
|
|
|
if (!$aMonitorStatus || @$aMonitorStatus['stat'] == 'fail') { |
24
|
|
|
if (is_array($aMonitorStatus) && array_key_exists('message', @$aMonitorStatus)) { |
25
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0032', $aMonitorStatus['message'])); |
26
|
|
|
} else { |
27
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0032', $this->tools->getError())); |
28
|
|
|
} |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
$aMonitorStatus['monitors']['monitor'][0]['customuptimeratio'] = explode('-', $aMonitorStatus['monitors']['monitor'][0]['customuptimeratio']); |
32
|
|
|
$aAllMonitorsStatus[] = $aMonitorStatus['monitors']['monitor'][0]; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
if (!$this->setting->setValue('monitoring_uptimerobot_status', json_encode($aAllMonitorsStatus)) || !$this->setting->setValue('monitoring_uptimerobot_lastcheck', time())) { |
|
|
|
|
35
|
|
|
$this->setErrorMessage($this->getErrorMsg('E0033'), $setting->getError()); |
|
|
|
|
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Fetch Uptime Robot Status from settings table |
44
|
|
|
* @param none |
45
|
|
|
* @return array Data on success, false on failure |
46
|
|
|
**/ |
47
|
|
|
public function getUptimeRobotStatus() { |
48
|
|
|
if ($json = $this->setting->getValue('monitoring_uptimerobot_status')) |
49
|
|
|
return json_decode($json, true); |
50
|
|
|
return false; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Check that our cron is currently activated |
55
|
|
|
* @param name string Cronjob name |
56
|
|
|
* @return bool true or false |
57
|
|
|
**/ |
58
|
|
|
public function isDisabled($name) { |
59
|
|
|
$aStatus = $this->getStatus($name . '_disabled'); |
60
|
|
|
return $aStatus['value']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the timestamp that last time a cronjob started |
65
|
|
|
* @param name string Cronjob name |
66
|
|
|
* @return int unix timestamp of last time the cronjob started |
67
|
|
|
**/ |
68
|
|
|
public function getLastCronStarted($name) { |
69
|
|
|
$aStatus = $this->getStatus($name . '_starttime'); |
70
|
|
|
return $aStatus['value']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Fetch a value from our table |
75
|
|
|
* @param name string Setting name |
76
|
|
|
* @return value string Value |
77
|
|
|
**/ |
78
|
|
|
public function getStatus($name) { |
79
|
|
|
$query = $this->mysqli->prepare("SELECT * FROM $this->table WHERE name = ? LIMIT 1"); |
80
|
|
|
if ($query && $query->bind_param('s', $name) && $query->execute() && $result = $query->get_result()) { |
81
|
|
|
return $result->fetch_assoc(); |
82
|
|
|
} else { |
83
|
|
|
return $this->sqlError(); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Insert or update a setting |
89
|
|
|
* @param name string Name of the variable |
90
|
|
|
* @param value string Variable value |
91
|
|
|
* @return bool |
92
|
|
|
**/ |
93
|
|
View Code Duplication |
public function setStatus($name, $type, $value) { |
|
|
|
|
94
|
|
|
$stmt = $this->mysqli->prepare(" |
95
|
|
|
INSERT INTO $this->table (name, type, value) |
96
|
|
|
VALUES (?, ?, ?) |
97
|
|
|
ON DUPLICATE KEY UPDATE value = ? |
98
|
|
|
"); |
99
|
|
|
if ($stmt && $stmt->bind_param('ssss', $name, $type, $value, $value) && $stmt->execute()) |
100
|
|
|
return true; |
101
|
|
|
$this->debug->append("Failed to set $name to $value"); |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Start a cronjob, mark various fields properly |
107
|
|
|
* @param cron_name string Cronjob name |
108
|
|
|
**/ |
109
|
|
|
public function startCronjob($cron_name) { |
110
|
|
|
$aStatus = $this->getStatus($cron_name . '_active'); |
111
|
|
|
if ($aStatus['value'] == 1) { |
112
|
|
|
$this->setErrorMessage('Cron is already active in database: ' . $cron_name . '_active is 1, please force run with -f once ensured it\' not running'); |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
$this->setStatus($cron_name . "_active", "yesno", 1); |
116
|
|
|
$this->setStatus($cron_name . '_starttime', 'date', time()); |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* End cronjob with an error message |
122
|
|
|
* @param cron_name string Cronjob Name |
123
|
|
|
* @param msgCode string Message code as stored in error_codes array |
124
|
|
|
* @param exitCode int Exit code to pass on to exit function and monitor report |
125
|
|
|
* @param fatal boolean Should we exit out entirely |
126
|
|
|
* @return none |
127
|
|
|
**/ |
128
|
|
|
public function endCronjob($cron_name, $msgCode, $exitCode=0, $fatal=false, $mail=true) { |
129
|
|
|
$this->setStatus($cron_name . "_active", "yesno", 0); |
130
|
|
|
$this->setStatus($cron_name . "_message", "message", $this->getErrorMsg($msgCode)); |
131
|
|
|
$this->setStatus($cron_name . "_status", "okerror", $exitCode); |
132
|
|
|
$this->setStatus($cron_name . "_endtime", "date", time()); |
133
|
|
|
if ($mail) { |
134
|
|
|
$aMailData = array( |
135
|
|
|
'email' => $this->setting->getValue('system_error_email'), |
136
|
|
|
'subject' => 'Cronjob Failure', |
137
|
|
|
'Error Code' => $msgCode, |
138
|
|
|
'Error Message' => $this->getErrorMsg($msgCode) |
139
|
|
|
); |
140
|
|
|
if (!$this->mail->sendMail('notifications/error', $aMailData)) |
|
|
|
|
141
|
|
|
$this->setErrorMessage('Failed to send mail notification'); |
142
|
|
|
} |
143
|
|
|
if ($fatal) { |
144
|
|
|
if ($exitCode == 1) $this->setStatus($cron_name . "_disabled", "yesno", 1); |
145
|
|
|
exit($exitCode); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$monitoring = new Monitoring(); |
151
|
|
|
$monitoring->setErrorCodes($aErrorCodes); |
152
|
|
|
$monitoring->setConfig($config); |
153
|
|
|
$monitoring->setDebug($debug); |
154
|
|
|
$monitoring->setMail($mail); |
155
|
|
|
$monitoring->setMysql($mysqli); |
156
|
|
|
$monitoring->setSetting($setting); |
157
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.