Repository navigation
Expand file tree
/
Copy pathpoller_webseer.php
More file actions
318 lines (259 loc) · 8.64 KB
/
Copy pathpoller_webseer.php
File metadata and controls
318 lines (259 loc) · 8.64 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
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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 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/ |
+-------------------------------------------------------------------------+
*/
// let PHP run just as long as it has to
ini_set('max_execution_time', '55');
$dir = __DIR__;
chdir($dir);
if (strpos($dir, 'plugins') !== false) {
chdir('../../');
}
require('./include/cli_check.php');
require_once($config['base_path'] . '/plugins/webseer/includes/functions.php');
require_once($config['base_path'] . '/lib/poller.php');
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
$debug = false;
$force = false;
$start = microtime(true);
$poller_id = $config['poller_id'];
if (cacti_sizeof($parms)) {
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
[$arg, $value] = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-f':
case '--force':
$force = true;
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:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
exit;
}
}
}
if (!function_exists('curl_init')) {
print 'FATAL: You must install php-curl to use this Plugin' . PHP_EOL;
}
plugin_webseer_check_debug();
print "Running Service Checks\n";
plugin_webseer_register_server();
// Remove old Logs (ADD A SETTING!!!!!!)
$t = time() - (86400 * 30);
if ($poller_id == 1) {
db_execute_prepared('DELETE FROM plugin_webseer_urls_log
WHERE lastcheck < FROM_UNIXTIME(?)',
[$t]);
db_execute_prepared('DELETE FROM plugin_webseer_processes
WHERE time < FROM_UNIXTIME(?)',
[time() - 15]);
}
$urls = db_fetch_assoc_prepared('SELECT *
FROM plugin_webseer_urls
WHERE enabled = "on"
AND poller_id = ?',
[$poller_id]);
$max = 12;
if (cacti_sizeof($urls)) {
foreach ($urls as $url) {
$total = db_fetch_cell_prepared('SELECT COUNT(id)
FROM plugin_webseer_processes
WHERE poller_id = ?',
[$poller_id]);
if ($max - $total > 0) {
$url['debug_type'] = 'Url';
plugin_webseer_debug('Launching Service Check ' . $url['url'], $url);
$command_string = read_config_option('path_php_binary');
$extra_args = '-q "' . $config['base_path'] . '/plugins/webseer/webseer_process.php" --id=' . $url['id'] . ($debug ? ' --debug' : '');
exec_background($command_string, $extra_args);
usleep(10000);
} else {
usleep(10000);
db_execute_prepared('DELETE FROM plugin_webseer_processes
WHERE time < FROM_UNIXTIME(?)
AND poller_id = ?',
[time() - 15, $poller_id]);
}
}
}
while (true) {
db_execute_prepared('DELETE FROM plugin_webseer_processes
WHERE time < FROM_UNIXTIME(?)
AND poller_id = ?',
[time() - 15, $poller_id]);
$running = db_fetch_cell_prepared('SELECT COUNT(*)
FROM plugin_webseer_processes
WHERE poller_id = ?',
[$poller_id]);
if ($running == 0) {
break;
} else {
sleep(1);
}
}
$servers = plugin_webseer_update_servers();
$end = microtime(true);
$ttime = round($end - $start, 2);
$stats = 'Time:' . $ttime . ' Checks:' . sizeof($urls) . ' Servers:' . $servers;
cacti_log("WEBSEER STATS: $stats", false, 'SYSTEM');
if ($poller_id == 1) {
set_config_option('stats_webseer', $stats);
}
set_config_option('stats_webseer_' . $poller_id, $stats);
/**
* Registers this Cacti poller (master or remote) as a webseer server in
* plugin_webseer_servers, if it is not already registered, recording its
* detected hostname/IP, master/remote role, and callback URL. Called
* from this script's main flow at the start of every poller run.
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* determine this poller's id/role and to build
* its callback URL.
*/
function plugin_webseer_register_server() {
global $config;
$lastcheck = date('Y-m-d H:i:s');
if (function_exists('gethostname')) {
$hostname = gethostname();
} else {
$hostname = false;
}
if ($hostname === false) {
$hostname = php_uname('n');
}
$ipaddress = gethostbyname($hostname);
$found = db_fetch_cell_prepared('SELECT id
FROM plugin_webseer_servers
WHERE ip = ?',
[$ipaddress]);
if (!$found) {
$found = [];
$found['debug_type'] = 'Server';
plugin_webseer_debug('Registering Server ' . $ipaddress, $found);
$save = [];
$save['enabled'] = 'on';
$save['isme'] = 1;
$save['lastcheck'] = $lastcheck;
$save['ip'] = $ipaddress;
if (isset($config['poller_id']) && $config['poller_id'] == 1) {
$save['master'] = 1;
$save['name'] = __('Cacti Master');
} else {
$save['master'] = 0;
$save['name'] = __('Cacti Remote Server');
}
if (substr_count($hostname, '.') > 0) {
$urlhost = $hostname;
} else {
$urlhost = $ipaddress;
}
if (isset($config['url_path'])) {
$save['url'] = (read_config_option('force_https') == 'on' ? 'https://' : 'http://') . $urlhost . $config['url_path'] . 'index.php';
} else {
$save['url'] = 'http://' . $urlhost;
}
$id = sql_save($save, 'plugin_webseer_servers');
}
}
/**
* Sends a HEARTBEAT request to every other registered, enabled webseer
* server so they can record this server as still alive. Called from
* this script's main flow after all service checks have run.
*
* @return int The number of remote servers a heartbeat was sent to.
*/
function plugin_webseer_update_servers() {
$servers = db_fetch_assoc('SELECT *
FROM plugin_webseer_servers
WHERE isme = 0
AND enabled = 1');
if ($servers !== false && cacti_sizeof($servers)) {
foreach ($servers as $server) {
$server['debug_type'] = 'Server';
$cc = new cURL(true, 'cookies.txt', $server['compression'], '', $server);
$data = [];
$data['action'] = 'HEARTBEAT';
$results = $cc->post($server['url'], $data);
}
}
return cacti_sizeof($servers);
}
/**
* display_version - displays version information
*
* Prints this poller script's name/plugin version/copyright. Called
* from the CLI argument parser for the '--version' flag, and from
* display_help() to prefix the usage text.
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* locate and load setup.php for the version
* lookup.
*/
function display_version() {
global $config;
if (!function_exists('plugin_webseer_version')) {
require_once($config['base_path'] . '/plugins/webseer/setup.php');
}
$info = plugin_webseer_version();
print 'Cacti Service Check Master Process, Version ' . (isset($info['version']) ? $info['version'] : 'unknown') . ', ' . COPYRIGHT_YEARS . "\n";
}
/**
* display_help - displays the usage of the function
*
* Prints this script's version banner followed by its command-line
* usage/argument summary. Called from the CLI argument parser for the
* '--help' flag, and whenever an invalid argument is supplied.
*
* @return void
*/
function display_help() {
display_version();
print "\nusage: poller_webseer.php [--debug] [--force]\n\n";
print "This binary will exec all the Web Service check child processes.\n\n";
print "--force - Force all the service checks to run now\n";
print "--debug - Display verbose output during execution\n\n";
}