forked from Cacti/plugin_thold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththold_daemon.php
404 lines (327 loc) · 11 KB
/
thold_daemon.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
#!/usr/bin/php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2006-2017 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 snmpagent 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/ |
+-------------------------------------------------------------------------+
*/
/* tick use required as of PHP 4.3.0 to accomodate signal handling */
declare(ticks = 1);
/* sig_handler - provides a generic means to catch exceptions to the Cacti log.
@arg $signo - (int) the signal that was thrown by the interface.
@returns - null */
function sig_handler($signo) {
global $config;
switch ($signo) {
case SIGTERM:
case SIGINT:
if (read_config_option('remote_storage_method') == 1) {
db_execute_prepared('DELETE FROM plugin_thold_daemon_processes
WHERE poller_id = ?',
array($config['poller_id']));
db_execute_prepared('DELETE FROM plugin_thold_daemon_data
WHERE poller_id = ?',
array($config['poller_id']));
if ($config['poller_id'] == 1) {
db_execute('UPDATE thold_data AS td
LEFT JOIN host AS h
ON td.host_id = h.id
SET td.thold_daemon_pid = ""
WHERE (h.poller_id = 1 OR h.poller_id IS NULL)
AND td.thold_daemon_pid != ""');
} else {
db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN host AS h
ON td.host_id = h.id
SET td.thold_daemon_pid = ""
WHERE poller_id = ?
AND td.thold_daemon_pid != ""',
array($config['poller_id']));
}
} else {
db_execute('TRUNCATE plugin_thold_daemon_processes');
db_execute('TRUNCATE plugin_thold_daemon_data');
db_execute('UPDATE thold_data SET thold_daemon_pid = "" WHERE thold_daemon_pid != ""');
}
cacti_log('WARNING: Thold Daemon Process (' . getmypid() . ') terminated by user', false, 'THOLD');
exit;
break;
default:
/* ignore all other signals */
}
}
/* do NOT run this script through a web browser */
if (!isset($_SERVER['argv'][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
die('<br><strong>This script is only meant to run at the command line.</strong>');
}
/* we are not talking to the browser */
$no_http_headers = true;
/* let's report all errors */
error_reporting(E_ALL);
/* allow the script to hang around waiting for connections. */
set_time_limit(0);
/* we do not need so much memory */
ini_set('memory_limit', '256M');
$no_http_headers = true;
chdir(dirname(__FILE__));
chdir('../../');
include_once('./include/global.php');
include_once($config['base_path'] . '/lib/poller.php');
/* install signal handlers for Linux/UNIX only */
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
}
global $cnn_id, $config;
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
$debug = false;
$foreground = false;
if (sizeof($parms)) {
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
list ($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-d':
case '--debug':
$debug = true;
break;
case '-f':
case '--foreground':
$foreground = true;
break;
case '-v':
case '--version':
case '-V':
display_version();
exit;
case '--help':
case '-h':
case '-H':
display_help();
exit;
exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
}
}
}
/* redirect standard error to dev/null */
if ($config['cacti_server_os'] == 'unix') {
fclose(STDERR);
$STDERR = fopen('/dev/null', 'wb');
} else {
fclose(STDERR);
$STDERR = fopen('null', 'wb');
}
/* check if poller daemon is already running */
exec('pgrep -a php | grep thold_daemon.php', $output);
if (sizeof($output) >= 2) {
print 'The Thold Daemon is still running' . PHP_EOL;
return;
}
/* do not run the thold daemon on the remote server in central storage mode */
if (read_config_option('remote_storage_method') != 1 && $config['poller_id'] > 1) {
print 'In Central Storage Mode, the thold_daemon only runs on the main data collector.' . PHP_EOL;
exit(1);
}
print 'Starting Thold Daemon ... ';
if (!$foreground) {
if (function_exists('pcntl_fork')) {
// Close the database connection
db_close($cnn_id);
// Fork the current process to daemonize
$pid = pcntl_fork();
if ($pid == -1) {
/* oha ... something went wrong :( */
print '[FAILED]' . PHP_EOL;
return false;
} elseif ($pid == 0) {
// We are the child
} else {
cacti_log('NOTE: Thold Daemon Started on ' . gethostname(), false, 'THOLD');;
// We are the parent, output and exit
print '[OK]' . PHP_EOL;
exit;
}
} else {
// Windows.... awesome! But no worries
print '[WARNING] This system does not support forking.' . PHP_EOL;
}
} else {
print '[NOTE] The Thold Daemon is running in foreground mode.' . PHP_EOL;
}
sleep(2);
// The database connection looses state as parent, so reconnect regardless
$cnn_id = thold_db_reconnect($cnn_id);
if (read_config_option('remote_storage_method') == 1) {
db_execute_prepared('DELETE FROM plugin_thold_daemon_processes
WHERE poller_id = ?',
array($config['poller_id']));
db_execute_prepared('DELETE FROM plugin_thold_daemon_data
WHERE poller_id = ?',
array($config['poller_id']));
// Poller 1 handles external the special case of external data sources
if ($config['poller_id'] == 1) {
db_execute('UPDATE thold_data AS td
LEFT JOIN host AS h
ON td.host_id = h.id
SET td.thold_daemon_pid = ""
WHERE (h.poller_id = 1 OR h.poller_id IS NULL)');
} else {
db_execute_prepared('UPDATE thold_data AS td
LEFT JOIN host AS h
ON td.host_id = h.id
SET td.thold_daemon_pid = ""
WHERE poller_id = ?',
array($config['poller_id']));
}
} elseif ($config['poller_id'] == 1) {
db_execute('TRUNCATE plugin_thold_daemon_processes');
db_execute('TRUNCATE plugin_thold_daemon_data');
db_execute('UPDATE thold_data AS td
SET thold_daemon_pid = ""
WHERE thold_daemon_pid != ""');
}
$path_php_binary = read_config_option('path_php_binary');
$queued_processes = 0;
while (true) {
if (thold_db_connection()) {
// Initiate concurrent background processes as long as we do not hit the limits
if (read_config_option('remote_storage_method') == 1) {
$queue = db_fetch_assoc_prepared('SELECT *
FROM plugin_thold_daemon_processes
WHERE start = 0
AND poller_id = ?
ORDER BY pid',
array($config['poller_id']));
} else {
$queue = db_fetch_assoc('SELECT *
FROM plugin_thold_daemon_processes
WHERE start = 0
ORDER BY pid');
}
$queued_processes = sizeof($queue);
thold_debug('Processes Queued: ' . $queued_processes);
if ($queued_processes) {
$thold_max_concurrent_processes = read_config_option('thold_max_concurrent_processes');
if (read_config_option('remote_storage_method') == 1) {
$running_processes = db_fetch_cell_prepared('SELECT COUNT(*)
FROM plugin_thold_daemon_processes
WHERE start > 0
AND end = 0
AND poller_id = ?',
array($config['poller_id']));
} else {
$running_processes = db_fetch_cell('SELECT COUNT(*)
FROM plugin_thold_daemon_processes
WHERE start > 0
AND end = 0');
}
thold_debug('Processes Running: ' . $running_processes);
$free_processes = $thold_max_concurrent_processes - $running_processes;
thold_debug('Processes Free: ' . $free_processes);
$launched = 0;
if ($free_processes > 0) {
foreach ($queue as $proc) {
$pid = $proc['pid'];
/* mark the pid as started from here */
db_execute_prepared('UPDATE plugin_thold_daemon_processes
SET start = ?
WHERE pid = ?',
array(microtime(true), $pid));
$process = '-q ' . $config['base_path'] . '/plugins/thold/thold_process.php --pid=' . $pid . ' > /dev/null';
thold_debug('Starting process: ' . $path_php_binary . ' -q ' . $process, false, 'THOLD');
exec_background($path_php_binary, $process);
$launched++;
if ($launched >= $free_processes) {
break;
}
}
}
} else {
thold_debug('Idle Sleeping');
sleep(2);
}
} else {
// try to reconnect if the test was no good
$cnn_id = thold_db_reconnect($cnn_id);
}
}
function thold_db_connection(){
global $cnn_id;
if (is_object($cnn_id)) {
// Avoid showing errors
restore_error_handler();
set_error_handler('thold_db_error_handler');
$cacti_version = db_fetch_cell('SELECT cacti FROM version');
// Restore Cactis Error handler
restore_error_handler();
set_error_handler('CactiErrorHandler');
return is_null($cacti_version) ? false : true;
}
return false;
}
function thold_db_reconnect($cnn_id = null) {
chdir(dirname(__FILE__));
include('../../include/config.php');
if (is_object($cnn_id)) {
db_close($cnn_id);
}
// Avoid showing errors
restore_error_handler();
set_error_handler('thold_db_error_handler');
// Connect to the database server
$cnn_id = db_connect_real($database_hostname, $database_username, $database_password, $database_default, $database_type, $database_port, $database_ssl);
// Restore Cactis Error handler
restore_error_handler();
set_error_handler('CactiErrorHandler');
return $cnn_id;
}
function thold_db_error_handler() {
return true;
}
function thold_debug($string) {
global $debug;
if ($debug) {
$output = 'DEBUG: ' . trim($string);
print $output . PHP_EOL;
}
}
function display_version() {
global $config;
if (!function_exists('plugin_thold_version')) {
include_once($config['base_path'] . '/plugins/thold/setup.php');
}
$info = plugin_thold_version();
echo 'Threshold Daemon, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . PHP_EOL;
}
/* display_help - displays the usage of the function */
function display_help () {
display_version();
print PHP_EOL . 'usage: thold_daemon.php [ --foreground | -f ] [ --debug ]' . PHP_EOL . PHP_EOL;
print 'The Threshold Daemon processor for the Thold Plugin.' . PHP_EOL;
}