forked from Cacti/plugin_webseer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webseer_process.php
471 lines (391 loc) · 14.9 KB
/
webseer_process.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2019 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
/* we are not talking to the browser */
$dir = dirname(__FILE__);
chdir($dir);
if (strpos($dir, 'plugins') !== false) {
chdir('../../');
}
require('./include/cli_check.php');
include_once($config['base_path'] . '/plugins/webseer/includes/functions.php');
ini_set('max_execution_time', '21');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
global $debug;
$debug = false;
$url_id = 0;
$poller_interval = read_config_option('poller_interval');
if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--id':
$url_id = $value;
break;
case '-d':
case '--debug':
$debug = TRUE;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
default:
echo 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
exit;
}
}
}
if (empty($url_id)) {
echo "ERROR: You must specify a URL id\n";
exit(1);
}
plugin_webseer_check_debug();
$url = db_fetch_row_prepared('SELECT *
FROM plugin_webseer_urls
WHERE enabled = "on"
AND id = ?',
array($url_id));
if (!cacti_sizeof($url)) {
echo "ERROR: URL is not Found\n";
exit(1);
}
if (api_plugin_is_enabled('maint')) {
include_once($config['base_path'] . '/plugins/maint/functions.php');
}
if (function_exists('plugin_maint_check_webseer_url')) {
if (plugin_maint_check_webseer_url($url_id)) {
plugin_webseer_debug('Maintenance schedule active, skipped ' , $url);
exit(0);
}
}
$url['debug_type'] = 'Url';
register_startup($url_id);
if ($url['url'] != '') {
/* attempt to get results 3 times before exiting */
$x = 0;
while ($x < 3) {
plugin_webseer_debug('Service Check Number ' . $x, $url);
switch ($url['type']) {
case 'http':
case 'https':
$cc = new cURL(true, 'cookies.txt', $url['compression'], '', $url);
if ($url['proxy_server'] > 0) {
$proxy = db_fetch_row_prepared('SELECT *
FROM plugin_webseer_proxies
WHERE id = ?',
array($url['proxy_server']));
if (cacti_sizeof($proxy)) {
$cc->proxy_hostname = $proxy['hostname'];
if ($url['type'] == 'http') {
$cc->proxy_port = $proxy['http_port'];
} else {
$cc->proxy_port = $proxy['https_port'];
}
if ($proxy['username'] != '') {
$cc->proxy_username = $proxy['username'];
}
if ($proxy['password'] != '') {
$cc->proxy_password = $proxy['password'];
}
} else {
cacti_log('ERROR: Unable to obtain Proxy settings');
}
}
$results = $cc->get($url['url']);
$results['data'] = $cc->data;
break;
case 'dns':
$results = plugin_webseer_check_dns($url);
break;
}
if ($results['result']) {
break;
}
$x++;
usleep(10000);
}
// Do calculations for triggering
$pi = read_config_option('poller_interval');
$t = time() - ($url['downtrigger'] * 60);
$lc = time() - ($pi*2);
$ts = db_fetch_cell_prepared('SELECT count(id)
FROM plugin_webseer_servers
WHERE isme = 1
OR (isme = 0 AND UNIX_TIMESTAMP(lastcheck) > ?)',
array($lc));
$tf = ($ts * ($url['downtrigger'] - 1)) + 1;
$url['failures'] = db_fetch_cell_prepared('SELECT COUNT(url_id)
FROM plugin_webseer_servers_log
WHERE UNIX_TIMESTAMP(lastcheck) > ?
AND url_id = ?',
array($t, $url['id']));
plugin_webseer_debug('pi:' . $pi . ', t:' . $t . ' (' . date('Y-m-d H:i:s', $t) . '), lc:' . $lc . ' (' . date('Y-m-d H:i:s', $lc) . '), ts:' . $ts . ', tf:' . $tf, $url);
plugin_webseer_debug('failures:'. $url['failures'] . ', triggered:' . $url['triggered'], $url);
if (strtotime($url['lastcheck']) > 0 && (($url['result'] != '' && $url['result'] != $results['result']) || $url['failures'] > 0 || $url['triggered'] == 1)) {
plugin_webseer_debug('Checking for trigger', $url);
$sendemail = false;
if ($results['result'] == 0) {
$url['failures'] = $url['failures'] + 1;
// //////////if ($url['failures'] > $url['downtrigger'] && $url['triggered'] == 0) {
if ($url['failures'] >= ($url['downtrigger'] * 60)/$poller_interval && $url['triggered'] == 0) {
$sendemail = true;
$url['triggered'] = 1;
}
}
if ($results['result'] == 1) {
if ($url['failures'] == 0 && $url['triggered'] == 1) {
$sendemail = true;
$url['triggered'] = 0;
}
}
db_execute_prepared("INSERT INTO plugin_webseer_urls_log
(url_id, lastcheck, compression, result, http_code, error,
total_time, namelookup_time, connect_time, redirect_time,
redirect_count, size_download, speed_download)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
array($url['id'], date('Y-m-d H:i:s', $results['time']),
$results['options']['compression'], $results['result'],
$results['options']['http_code'], $results['error'],
$results['options']['total_time'], $results['options']['namelookup_time'],
$results['options']['connect_time'], $results['options']['redirect_time'],
$results['options']['redirect_count'], $results['options']['size_download'],
$results['options']['speed_download']
)
);
if ($sendemail) {
plugin_webseer_debug('Time to send email to admins', $url);
if (plugin_webseer_amimaster ()) {
if ($url['notify_format'] == WEBSEER_FORMAT_PLAIN) {
plugin_webseer_get_users($results, $url, 'text');
} else {
plugin_webseer_get_users($results, $url, '');
}
}
}
}else{
plugin_webseer_debug('Not checking for trigger', $url);
}
plugin_webseer_debug('Updating Statistics', $url);
db_execute_prepared('UPDATE plugin_webseer_urls SET result = ?, triggered = ?, failures = ?,
lastcheck = ?, error = ?, http_code = ?, total_time = ?, namelookup_time = ?,
connect_time = ?, redirect_time = ?, redirect_count = ?, speed_download = ?,
size_download = ?, debug = ?
WHERE id = ?',
array($results['result'], $url['triggered'], $url['failures'], date('Y-m-d H:i:s', $results['time']),
$results['error'], $results['options']['http_code'], $results['options']['total_time'],
$results['options']['namelookup_time'], $results['options']['connect_time'],
$results['options']['redirect_time'], $results['options']['redirect_count'],
$results['options']['speed_download'], $results['options']['size_download'],
$results['data'], $url['id']
)
);
if ($results['result'] == 0) {
$save = array();
$save['url_id'] = $url['id'];
$save['server'] = plugin_webseer_whoami();
$save['lastcheck'] = date('Y-m-d H:i:s', $results['time']);
$save['result'] = $results['result'];
$save['http_code'] = $results['options']['http_code'];
$save['error'] = $results['error'];
$save['total_time'] = $results['options']['total_time'];
$save['namelookup_time'] = $results['options']['namelookup_time'];
$save['connect_time'] = $results['options']['connect_time'];
$save['redirect_time'] = $results['options']['redirect_time'];
$save['redirect_count'] = $results['options']['redirect_count'];
$save['size_download'] = $results['options']['size_download'];
$save['speed_download'] = $results['options']['speed_download'];
plugin_webseer_down_remote_hosts($save);
db_execute_prepared('INSERT INTO plugin_webseer_servers_log
(url_id, server, lastcheck, result, http_code, error, total_time,
namelookup_time, connect_time, redirect_time, redirect_count,
size_download, speed_download)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)',
array($url['id'], plugin_webseer_whoami(), date('Y-m-d H:i:s', $results['time']),
$results['result'], $results['options']['http_code'], $results['error'],
$results['options']['total_time'], $results['options']['namelookup_time'],
$results['options']['connect_time'], $results['options']['redirect_time'],
$results['options']['redirect_count'], $results['options']['size_download'],
$results['options']['speed_download'])
);
}
}
/* register process end */
register_shutdown($url_id);
/* purge old entries from the log */
db_execute_prepared('DELETE FROM plugin_webseer_servers_log
WHERE lastcheck < FROM_UNIXTIME(?)',
array(time() - (86400 * 90)));
/* exit */
function register_startup($url_id) {
db_execute_prepared('INSERT INTO plugin_webseer_processes
(url_id, pid, time)
VALUES(?, ?, NOW())',
array($url_id, getmypid()));
}
function register_shutdown($url_id) {
db_execute_prepared('DELETE FROM plugin_webseer_processes
WHERE url = ?
AND pid = ?',
array($url_id, getmypid()), FALSE);
}
function plugin_webseer_get_users($results, $url, $type) {
global $httperrors;
$users = '';
if ($url['notify_accounts'] != '') {
$users = db_fetch_cell("SELECT GROUP_CONCAT(DISTINCT data) AS emails
FROM plugin_webseer_contacts
WHERE id IN (" . $url['notify_accounts'] . ")");
}
if ($users == '' && isset($url['notify_extra']) && $url['notify_extra'] == '') {
cacti_log('ERROR: No users to send WEBSEER Notification for ' . $url['display_name'], false, 'WEBSEER');
return;
}
$to = $users;
if ($url['notify_extra'] != '') {
$to .= ($to != '' ? ', ':'') . $url['notify_extra'];
}
if ($type == 'text') {
if ($results['result'] == 0) {
$subject = "Site Down: " . ($url['display_name'] != '' ? $url['display_name'] : $url['url']);
} else {
$subject = "Site Recovered: " . ($url['display_name'] != '' ? $url['display_name'] : $url['url']);
}
$message = "Site " . ($results['result'] == 0 ? 'Down: ' : 'Recovering: ') . ($url['display_name'] != '' ? $url['display_name']:'') . "\n";
$message .= "URL: " . $url['url'] . "\n";
$message .= "Error: " . $results['error'] . "\n";
$message .= "Total Time: " . $results['options']['total_time'] . "\n";
} else {
if ($results['result'] == 0) {
$subject = "Site Down: " . ($url['display_name'] != '' ? $url['display_name'] : $url['url']);
} else {
$subject = "Site Recovered: " . ($url['display_name'] != '' ? $url['display_name'] : $url['url']);
}
$message = "<h3>" . $subject . "</h3>\n";
$message .= "<hr>";
$message .= "<table>\n";
$message .= "<tr><td>URL:</td><td>" . $url['url'] . "</td></tr>\n";
$message .= "<tr><td>Status:</td><td>" . ($results['result'] == 0 ? 'Down' : 'Recovering') . "</td></tr>\n";
$message .= "<tr><td>Date:</td><td>" . date('F j, Y - h:i:s', $results['time']) . "</td></tr>\n";
$message .= "<tr><td>HTTP Code:</td><td>" . $httperrors[$results['options']['http_code']] . "</td></tr>\n";
if ($results['error'] != '') {
$message .= "<tr><td>Error:</td><td>" . $results['error'] . "</td></tr>\n";
}
$message .= "</table>\n";
$message .= "<hr>";
if ($results['error'] > 0) {
$message .= "<table>\n";
$message .= "<tr><td>Total Time:</td><td> " . round($results['options']['total_time'],4) . "</td></tr>\n";
$message .= "<tr><td>Connect Time:</td><td> " . round($results['options']['connect_time'],4) . "</td></tr>\n";
$message .= "<tr><td>DNS Time:</td><td> " . round($results['options']['namelookup_time'],4) . "</td></tr>\n";
$message .= "<tr><td>Redirect Time:</td><td> " . round($results['options']['redirect_time'],4) . "</td></tr>\n";
$message .= "<tr><td>Redirect Count:</td><td> " . round($results['options']['redirect_count'],4) . "</td></tr>\n";
$message .= "<tr><td>Download Size:</td><td> " . round($results['options']['size_download'],4) . " Bytes" . "</td></tr>\n";
$message .= "<tr><td>Download Speed:</td><td> " . round($results['options']['speed_download'],4) . " Bps" . "</td></tr>\n";
$message .= "</table>\n";
$message .= "<hr>";
}
}
$users = explode(',', $to);
foreach ($users as $u) {
plugin_webseer_send_email($u, $subject, $message);
}
}
function plugin_webseer_amimaster() {
if (function_exists('gethostname')) {
$hostname = gethostname();
}else{
$hostname = php_uname('n');
}
$ipaddress = gethostbyname($hostname);
$server = db_fetch_cell_prepared('SELECT id
FROM plugin_webseer_servers
WHERE ip = ?
AND master = 1',
array($ipaddress));
if ($server) {
return true;
}
return false;
}
function plugin_webseer_whoami() {
if (function_exists('gethostname')) {
$hostname = gethostname();
}else{
$hostname = php_uname('n');
}
$ipaddress = gethostbyname($hostname);
$server = db_fetch_cell_prepared('SELECT id
FROM plugin_webseer_servers
WHERE ip = ?',
array($ipaddress));
if ($server) {
return $server;
}
return 0;
}
function plugin_webseer_send_email($to, $subject, $message) {
$from_name = read_config_option('settings_from_name');
$from_email = read_config_option('settings_from_email');
if ($from_name != '') {
$from[0] = $from_email;
$from[1] = $from_name;
}else{
$from = $from_email;
}
if (defined('CACTI_VERSION')) {
$v = CACTI_VERSION;
} else {
$v = get_cacti_version();
}
$headers['User-Agent'] = 'Cacti-WebSeer-v' . $v;
$message_text = strip_tags($message);
mailer($from, $to, '', '', '', $subject, $message, $message_text, '', $headers);
}
/* display_version - displays version information */
function display_version() {
$version = get_cacti_version();
echo "Cacti Web Service Check Processor, Version $version, " . COPYRIGHT_YEARS . "\n";
}
/* display_help - displays the usage of the function */
function display_help() {
display_version();
echo "\nusage: webseer_process.php --id=N [--debug]\n\n";
echo "This binary will run the Web Service check for the WebSeer plugin.\n\n";
echo "--id=N - The url ID from the WebSeer database.\n";
echo "--debug - Display verbose output during execution\n\n";
}