forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller_commands.php
200 lines (168 loc) · 6.67 KB
/
poller_commands.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
#!/usr/bin/php -q
<?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 */
define('MAX_RECACHE_RUNTIME', 1800);
ini_set('max_runtime', '-1');
ini_set('memory_limit', '800M');
require(__DIR__ . '/include/cli_check.php');
require_once($config['base_path'] . '/lib/api_device.php');
require_once($config['base_path'] . '/lib/api_data_source.php');
require_once($config['base_path'] . '/lib/api_graph.php');
require_once($config['base_path'] . '/lib/api_tree.php');
require_once($config['base_path'] . '/lib/data_query.php');
require_once($config['base_path'] . '/lib/html_form_template.php');
require_once($config['base_path'] . '/lib/ping.php');
require_once($config['base_path'] . '/lib/poller.php');
require_once($config['base_path'] . '/lib/rrd.php');
require_once($config['base_path'] . '/lib/snmp.php');
require_once($config['base_path'] . '/lib/sort.php');
require_once($config['base_path'] . '/lib/template.php');
require_once($config['base_path'] . '/lib/utility.php');
$poller_id = $config['poller_id'];
$debug = false;
global $poller_db_cnn_id, $remote_db_cnn_id;
if ($config['poller_id'] > 1 && $config['connection'] == 'online') {
$poller_db_cnn_id = $remote_db_cnn_id;
} else {
$poller_db_cnn_id = false;
}
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--version':
case '-V':
display_version();
exit;
case '-H':
case '--help':
display_help();
exit(0);
case '--poller':
case '-p':
$poller_id = $value;
break;
case '--debug':
case '-d':
$debug = true;
break;
default:
print "ERROR: Invalid Argument: ($arg)\n\n";
display_help();
exit(1);
}
}
}
/* Record Start Time */
$start = microtime(true);
$max_updated = db_fetch_cell_prepared('SELECT MAX(UNIX_TIMESTAMP(last_updated))
FROM poller_command
WHERE poller_id = ?',
array($poller_id), '', true, $poller_db_cnn_id);
$poller_commands = db_fetch_assoc_prepared('SELECT action, command
FROM poller_command
WHERE poller_id = ?',
array($poller_id), true, $poller_db_cnn_id);
$last_host_id = 0;
$first_host = true;
$recached_hosts = 0;
if ($debug) {
$verbosity = POLLER_VERBOSITY_LOW;
} else {
$verbosity = POLLER_VERBOSITY_MEDIUM;
}
if (cacti_sizeof($poller_commands)) {
foreach ($poller_commands as $command) {
switch ($command['action']) {
case POLLER_COMMAND_REINDEX:
list($device_id, $data_query_id) = explode(':', $command['command']);
if ($last_host_id != $device_id) {
$last_host_id = $device_id;
$first_host = true;
$recached_hosts++;
} else {
$first_host = false;
}
if ($first_host) {
cacti_log("Device[$device_id] WARNING: Recache Event Detected for Device", true, 'PCOMMAND');
}
cacti_log("Device[$device_id] DQ[$data_query_id] RECACHE: Recache for Device started.", true, 'PCOMMAND', $verbosity);
run_data_query($device_id, $data_query_id);
cacti_log("Device[$device_id] DQ[$data_query_id] RECACHE: Recached successfully.", true, 'PCOMMAND', $verbosity);
break;
case POLLER_COMMAND_PURGE:
$device_id = $command['command'];
api_device_purge_from_remote($device_id, $poller_id);
cacti_log("Device[$device_id] PURGE: Purged successfully.", true, 'PCOMMAND', $verbosity);
break;
default:
cacti_log('ERROR: Unknown poller command issued', true, 'PCOMMAND');
}
/* record current_time */
$current = microtime(true);
/* end if runtime has been exceeded */
if (($current-$start) > MAX_RECACHE_RUNTIME) {
cacti_log("ERROR: Poller Command processing timed out after processing '$command'", true, 'PCOMMAND');
break;
}
}
db_execute_prepared('DELETE FROM poller_command
WHERE poller_id = ?
AND last_updated <= FROM_UNIXTIME(?)',
array($poller_id, $max_updated), true, $poller_db_cnn_id);
} else {
cacti_log('NOTE: No Poller Commands found for processing', true, 'PCOMMAND', $verbosity);
}
/* take time to log performance data */
$recache = microtime(true);
$recache_stats = sprintf('Poller:%s RecacheTime:%01.4f DevicesRecached:%s', $poller_id, round($recache - $start, 4), $recached_hosts);
if ($recached_hosts > 0) {
cacti_log('STATS: ' . $recache_stats, true, 'RECACHE');
}
/* insert poller stats into the settings table */
db_execute_prepared('REPLACE INTO settings (name, value) VALUES (?, ?)',
array('stats_recache_' . $poller_id, $recache_stats), true, $poller_db_cnn_id);
/* display_version - displays version information */
function display_version() {
$version = get_cacti_version();
print "Cacti Poller Commands Poller, Version $version " . COPYRIGHT_YEARS . "\n";
}
function display_help () {
display_version();
print "\nusage: poller_commands.php [--poller=ID] [--debug]\n\n";
print "Cacti's commands poller. This poller can receive specifically crafted commands from\n";
print "either the Cacti UI, or from the main poller, and then run them in the background.\n\n";
print "Optional:\n";
print " --poller=ID - The poller to run as. Defaults to the system poller\n";
print " --debug - Display verbose output during execution\n\n";
}