-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.php
314 lines (266 loc) · 8.94 KB
/
index.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
date_default_timezone_set('UTC');
// extensions check
if (!extension_loaded('curl'))
die('"curl" extension not loaded or installed :( ');
// function for email notification
function sendEmailAlert($to, $subject, $message, $headers) {
mail(
$to,
$subject,
$message,
$headers
);
}
// function for simple TCP connection test
function tcp_check($host, $port, $connect_timeout=1) {
$start = microtime(TRUE);
if (preg_replace("/[^0-9a-z.-]/",'', $host) !== $host) {
return "0,InvalidHost,0";
}
try {
$fp = fsockopen($host, $port, $errno, $errstr, $connect_timeout);
fclose($fp);
} catch (Exception $e){
}
if(!empty($errstr))
$conresult = "ConnectFail";
else
$conresult = "ConnectOK";
$end = microtime(TRUE);
$duration = intval( ($end - $start)*1000 );
return "$duration,$conresult,$errstr";
}
// function for URL check using cURL
function url_check($url, $string='<html', $connect_timeout=5) {
$start = microtime(TRUE);
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return "0,InvalidURL,0";
}
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0');
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
curl_setopt($c, CURLOPT_TIMEOUT, $connect_timeout);
curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($c, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($c, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($c, CURLOPT_HEADER, FALSE);
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP | CURLPROTO_SFTP);
$data= curl_exec($c);
curl_close($c);
if(!$data)
$conresult = "ConnectFail";
else
$conresult = "ConnectOK";
if (strpos($data, $string))
$result="StringOK";
else
$result="StringNotFound";
$end = microtime(TRUE);
$duration = intval( ($end - $start)*1000 );
return "$duration,$conresult,$result";
}
// ---- begin output processing ----
// initialize and fetch DB
$db = 'db.json';
if (!file_exists('db.json'))
touch('db.json');
$json = json_decode(file_get_contents($db));
// let's loop in the checks
$checkExecuted = FALSE;
foreach($json as $address) {
if(( time() - $address->lastCheck > $address->checkInterval) or $address->lastResultConnection != 'ConnectOK' or $address->lastResultString == 'StringNotFound') { // execute if check interval expired OR if previous check found error
if($address->type == 'url') {
$result = url_check($address->address, $address->findString, $address->timeout);
}
else {
$result = tcp_check($address->address, $address->findString, $address->timeout);
}
$result = explode(',', $result);
$address->lastResultDuration = $result[0];
$address->lastResultConnection = $result[1];
$address->lastResultString = $result[2];
$address->lastCheck = time();
$checkExecuted = TRUE;
}
}
// write out JSON data only if any checks have been executed
if($checkExecuted == TRUE)
file_put_contents($db, json_encode($json, JSON_PRETTY_PRINT), LOCK_EX);
// DEBUG: output DB array and die
//header('Content-type: text/plain');
//print_r($json); die;
// ============
// HTML OUTPUT
$checkOutput='';
$refreshCookie='';
$stylefile='frontend.css';
$downTime = FALSE;
$textNotFound = FALSE;
// let's populate the table with the checks
foreach($json as $check) {
if($check->lastResultConnection == 'ConnectOK')
$lastResultConnection = '<b class="ok">✔</b>';
else {
$downTime = TRUE;
sendEmailAlert(
$check->emailNotification,
'connection failed: '.$check->address,
"Hello, I am ".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']."\r\nSorry to bother you, but I couldn't connect to ".$check->address.", it told me: ".$check->lastResultConnection.' with string '.$check->lastResultString,
"From: php-uptime-monitor@".$_SERVER['SERVER_NAME']."\r\n"
);
$lastResultConnection = '<b class="err">✖ '.$check->lastResultConnection.'</b>';
}
if($check->lastResultString == 'StringOK')
$lastResultString = '<b class="ok">✔</b>';
else if (empty($check->lastResultString))
$lastResultString = '·';
else {
$textNotFound = TRUE;
sendEmailAlert(
$check->emailNotification,
'text not found: '.$check->address,
"Hello, I am ".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']."\r\nSorry to bother you, but I couldn't find the text string \"".$check->findString."\" on page ".$check->address." :(",
"From: php-uptime-monitor@".$_SERVER['SERVER_NAME']."\r\n"
);
$lastResultString = '<b class="err">✖'.'</b>';
}
$checkOutput.='
<tr>
<td>
<b>·'.$check->type.'·</b> '.$check->address.' : '.$check->findString.'
</td>
<td>
<progress value="'.((time() - $check->lastCheck)).'" max="'.$check->checkInterval.'"></progress> <abbr title="'.$check->lastCheck.' ('.date('r', $check->lastCheck).')">'.((time() - $check->lastCheck)).'s ago (interval: '.$check->checkInterval.'s)</abbr>
</td>
<td>
<b>'.$check->lastResultDuration.' ms</b>
</td>
<td>
'.$lastResultConnection.'
</td>
<td>
'.$lastResultString.'
</td>
</tr>
';
}
// if there is a visitor cookie AND there is no downtime to notify, then let's refresh the page
if(isset($_COOKIE['refresh']) and $downTime== FALSE) {
$refreshCookie='<meta http-equiv="refresh" content="'.(int)$_COOKIE['refresh'].'">';
}
// now for the actual HTML
print <<<EOT
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>php-uptime-monitor</title>
<link rel='stylesheet' href='{$stylefile}' type='text/css' media='screen' />
<meta name="viewport" content="initial-scale=1.0, user-scalable=yes">
{$refreshCookie}
<meta name="robots" content="noindex" />
</head>
<body>
<div class="box" id="main-container">
EOT;
// downtime ? Let's play some fancy music !
if ($downTime == TRUE) {
print <<<EOT
<audio controls autoplay loop><source src="alarm.ogg" type="audio/ogg"></audio><br>
EOT;
}
// a text not found ? Some calmer tunes
if ($textNotFound == TRUE) {
print <<<EOT
<audio controls autoplay><source src="textnotfound.ogg" type="audio/ogg"></audio><br>
EOT;
}
// proceed with the rest of the page
$pageCall=date(DATE_ATOM, time());
print <<<EOT
Page called: {$pageCall}
<button type="button" onclick="document.location.reload(true)">reload</button>
<button type="button" onclick="document.cookie = 'refresh=10;expires=0;';location.reload(true);">reload every 10s</button>
<div class="box-content">
<table>
<thead>
<tr>
<th>address + port/string</th>
<th>last check</th>
<th>last response time</th>
<th>last connect</th>
<th>last text search</th>
</tr>
</thead>
<tbody>{$checkOutput}</tbody>
</table>
</div>
</div>
</body>
</html>
EOT;
// ======= THAT'S ALL FOLKS ! Some data specs below:
/*
====== DATA FORMAT ======
[address] => ftp://ftp.mozilla.org/
→ host name, or URL in HTTP, HTTPS, FTP or SFTP schema
[type] => url
→ address type url | host
[findString] => pub
→ port number (host) or text string in web page (HTTP/HTTPS) or file/folder name (FTP & SFTP)
[timeout] => 3
→ time out delay in seconds
[checkInterval] => 600
→ check interval in seconds
[lastCheck] => 1437670605
→ timestamp of the last check
[lastResultDuration] => 1650
→ last check duration (response time) in milliseconds
[lastResultConnection] => ConnectOK
→ connection status of the last check {ConnectOK | ConnectFail} or {InvalidHost | InvalidURL} if input rejected by filter
[lastResultString] => StringOK
→ finding of the text string in last check {StringOK | StringNotFound | -empty-}
====== EXAMPLES to put in "db.json" =======
[
{
"address": "https://www.wikipedia.org/",
"type": "url",
"findString": "Wikibooks",
"timeout": 3,
"checkInterval": 600,
"lastCheck": 1438185116,
"lastResultDuration": "235",
"lastResultConnection": "ConnectOK",
"lastResultString": "StringOK",
"emailNotification": "[email protected]"
},
{
"address": "www.google.com",
"type": "host",
"findString": "80",
"timeout": 2,
"checkInterval": 120,
"lastCheck": 1438185126,
"lastResultDuration": "286",
"lastResultConnection": "ConnectOK",
"lastResultString": "",
"emailNotification": "[email protected]"
},
{
"address": "ftp://ftp.mozilla.org/",
"type": "url",
"findString": "pub",
"timeout": 3,
"checkInterval": 600,
"lastCheck": 1438185064,
"lastResultDuration": "1684",
"lastResultConnection": "ConnectOK",
"lastResultString": "StringOK",
"emailNotification": "[email protected]"
}
]
*/
?>