-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
cactid.php
executable file
·299 lines (229 loc) · 7.27 KB
/
cactid.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
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 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/ |
+-------------------------------------------------------------------------+
*/
if (function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
} else {
declare(ticks = 100);
}
ini_set('output_buffering', 'Off');
/* 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, $hostname;
switch ($signo) {
case SIGTERM:
case SIGINT:
cacti_log('WARNING: Cacti Daemon PID[' . getmypid() . '] Terminated on Device[' . gethostname() . ']', true, 'CACTID');
admin_email(__('Cacti System Warning'), __('WARNING: Cacti Daemon PID[' . getmypid() . '] Terminated on Device[' . gethostname() . ']', true, 'CACTID'));
exit(1);
break;
default:
/* ignore all other signals */
}
}
/* 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', '-1');
ini_set('max_execution_time', '0');
// default values
$hostname = gethostname();
$debug = false;
$foreground = false;
$logrecon = false;
chdir(__DIR__);
include_once('./include/cli_check.php');
/* install signal handlers for Linux/UNIX only */
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
}
global $config, $hostname, $debug;
// process calling arguments
$options = get_options();
if (isset($options['debug'])) {
$debug = true;
}
if (isset($options['foreground'])) {
$foreground = true;
}
// Set the frequency of data collection dynamically
// before loosing our database connection after fork.
// We would not have to do this is PDO supported
// connection cloning as does Perl.
$frequency = read_config_option('cron_interval', true);
// redirect standard error to dev/null
if (DIRECTORY_SEPARATOR != '\\') {
fclose(STDERR);
$STDERR = fopen('/dev/null', 'wb');
// check if cactid daemon is already running
exec('pgrep -a php | grep cactid.php', $output);
if (sizeof($output) >= 2) {
print 'The Cacti Daemon is still running' . PHP_EOL;
return;
}
} else {
fclose(STDERR);
$STDERR = fopen('null', 'wb');
}
print 'Starting Cacti Daemon ... ';
if (!$foreground) {
if (function_exists('pcntl_fork')) {
// Close the database connection
db_close();
// Fork the current process to daemonize
$pid = pcntl_fork();
if ($pid == -1) {
// Something went wrong
print '[FAILED]' . PHP_EOL;
exit(1);
}
if ($pid == 0) {
// We are the child
} else {
cacti_log('NOTE: Cacti Daemon PID[' . getmypid() . '] Started on Device[' . gethostname() . ']');
admin_email(__('Cacti System Notice'), __('Notice: Cacti Daemon PID[' . getmypid() . '] Started on Device[' . gethostname() . ']', true, 'CACTID'));
print '[OK]' . PHP_EOL;
exit(0);
}
} else {
// On Windows we run in foreground mode
print '[OK]' . PHP_EOL . '[NOTE] This system does not support forking.' . PHP_EOL;
}
} else {
print '[OK]' . PHP_EOL . '[NOTE] The Cacti Daemon is running in foreground mode.' . PHP_EOL;
}
sleep(2);
while (true) {
wait_for_start($frequency);
db_check_reconnect(false, $logrecon);
run_poller();
// Force Cacti to check the service start frequency dynamically
$frequency = -1;
$logrecon = true;
}
function wait_for_start($frequency = -1) {
$prev_time = -1;
$i = 0;
while (true) {
if ($frequency <= 0) {
$frequency = read_config_option('cron_interval', true);
if (empty($frequency)) {
$frequency = 300;
}
}
$now = time();
$offset = $now % $frequency;
if ($i % 5 == 0) {
debug('PrevOS: ' . $prev_time . ', CurrOS: ' . $offset . ', Freq: ' . $frequency);
}
if ($prev_time > 0) {
if ($offset < $prev_time) {
debug('Time to Run Poller');
break;
}
}
$prev_time = $offset;
$i++;
sleep(1);
}
return $frequency;
}
function run_poller() {
global $config, $debug;
debug('Cacti Data Collector');
$command = ' -q ' . CACTI_PATH_BASE . '/poller.php --force' . ($debug ? ' --debug':'');
$php_binary = read_config_option('path_php_binary');
if (empty($php_binary)) {
if ($config['cacti_server_os'] == 'win32') {
$php_binary = 'php';
} else {
$php_binary = '/usr/bin/php';
}
}
debug('Command Line is: ' . $command);
exec_background($php_binary, $command);
}
function get_options() {
$parms = $_SERVER['argv'];
array_shift($parms);
$options = array();
if (sizeof($parms)) {
$shortopts = 'VvHh';
$longopts = array(
'foreground',
'debug',
'version',
'help'
);
$options = getopt($shortopts, $longopts);
foreach ($options as $arg => $value) {
switch($arg) {
case 'foreground':
case 'debug':
break;
case 'version':
case 'V':
case 'v':
display_version();
exit(0);
case 'help':
case 'H':
case 'h':
display_help();
exit(0);
default:
print "ERROR: Invalid Argument: ($arg)" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
}
return $options;
}
function debug($string) {
global $debug;
if ($debug) {
$output = 'DEBUG: ' . trim($string);
print $output . PHP_EOL;
}
}
function display_version() {
global $config;
$version = get_cacti_cli_version();
print 'The Cacti Daemon (cactid), Version ' . $version . ', ' . COPYRIGHT_YEARS . PHP_EOL;
}
/* display_help - displays the usage of the function */
function display_help() {
display_version();
print PHP_EOL . 'usage: cactid.php [ --foreground ] [ --debug ]' . PHP_EOL . PHP_EOL;
print 'Daemon for Cacti data collection, otherwise known as cactid.' . PHP_EOL;
print 'optional:' . PHP_EOL;
print ' --foreground Run cactid in foreground mode, otherwise this is a forking daemon.' . PHP_EOL;
print ' --debug Used for debugging in --foreground mode.' . PHP_EOL;
}