-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpoller_wmi.php
385 lines (317 loc) · 11.1 KB
/
poller_wmi.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
#!/usr/bin/php -q
<?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/ |
+-------------------------------------------------------------------------+
*/
chdir(dirname(__FILE__));
chdir('../..');
include('./include/cli_check.php');
include_once('./lib/poller.php');
include_once('./lib/ping.php');
include_once('./plugins/wmi/functions.php');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
global $debug, $start, $seed, $forcerun;
$debug = FALSE;
$forcerun = FALSE;
$mainrun = FALSE;
$host_id = '';
$start = '';
$seed = '';
$key = '';
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 '--host-id':
$host_id = $value;
break;
case '--seed':
$seed = $value;
break;
case '--key':
$key = $value;
break;
case '-f':
case '--force':
$forcerun = TRUE;
break;
case '-M':
$mainrun = TRUE;
break;
case '-s':
case '--start':
$start = $value;
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 . PHP_EOL . PHP_EOL;
display_help();
exit;
}
}
}
/* Check for mandatory parameters */
if (!$mainrun && $host_id == '') {
print "FATAL: You must specify a Cacti host-id run" . PHP_EOL;
exit;
}
/* Do not process if not enabled */
if (!api_plugin_is_enabled('wmi')) {
print 'WARNING: The Host WMI Collection is Down! Exiting' . PHP_EOL;
exit(0);
}
if ($seed == '') {
$seed = rand();
}
if ($start == '') {
$start = microtime(true);
}
if ($mainrun) {
process_all_devices();
} else {
process_device($host_id);
}
exit(0);
function debug($message) {
global $debug;
if ($debug) {
print 'DEBUG: ' . trim($message) . PHP_EOL;
}
}
function process_all_devices() {
global $start, $seed;
print "NOTE: Processing Hosts Begins" . PHP_EOL;
/* Do not process collectors are still running */
if (db_fetch_cell('SELECT COUNT(*) FROM wmi_processes') > 0) {
print "WARNING: Another WMI Collector is still running! Exiting" . PHP_EOL;
exit(0);
}
/* The devices to scan will
* 1) Not be disabled,
* 2) Be linked to the host table
* 3) Be up and operational
*/
$devices = db_fetch_assoc("SELECT DISTINCT h.id AS host_id, h.description, h.hostname
FROM host_template_wmi_query AS htwq
LEFT JOIN host AS h
ON htwq.host_template_id = h.host_template_id
LEFT JOIN host_wmi_query AS hwq
ON hwq.host_id = h.id
AND hwq.wmi_query_id = htwq.wmi_query_id
INNER JOIN wmi_wql_queries AS wwq
ON wwq.id = htwq.wmi_query_id
WHERE (UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(last_started)+frequency OR last_started IS NULL)
AND h.status != 1
AND h.disabled != 'on'
AND wwq.enabled = 'on'
AND wmi_account > 0");
/* Remove entries from down and disabled devices */
db_execute("DELETE FROM host_wmi_cache
WHERE host_id IN(
SELECT id
FROM host
WHERE disabled='on'
OR host.status=1
)");
$concurrent_processes = read_config_option('wmi_processes');
if (empty($concurrent_processes)) {
set_config_option('wmi_processes', '10');
}
print "NOTE: Launching Collectors Starting" . PHP_EOL;
$i = 0;
if (sizeof($devices)) {
foreach ($devices as $device) {
while (true) {
$processes = db_fetch_cell('SELECT COUNT(*) FROM wmi_processes');
if ($processes < $concurrent_processes) {
/* put a placeholder in place to prevent overloads on slow systems */
$key = rand();
db_execute("INSERT INTO wmi_processes (pid, taskid, started) VALUES ($key, $seed, NOW())");
print "NOTE: Launching WMI Collector For: '" . $device['description'] . '[' . $device['hostname'] . "]'" . PHP_EOL;
process_background_device($device['host_id'], $seed, $key);
usleep(10000);
break;
} else {
sleep(1);
}
}
}
print "NOTE: All WMI Devices Launched, proceeding to wait for completion" . PHP_EOL;
/* wait for all processes to end or max run time */
while (true) {
$processes_left = db_fetch_cell("SELECT COUNT(*) FROM wmi_processes WHERE taskid = $seed");
$pl = db_fetch_cell('SELECT COUNT(*) FROM wmi_processes');
if ($processes_left == 0) {
print "NOTE: All Processes Complete, Exiting" . PHP_EOL;
break;
} else {
print "NOTE: Waiting on '$processes_left' Processes" . PHP_EOL;
sleep(2);
}
}
} else {
print "NOTE: No Devices found this pass to launch" . PHP_EOL;
}
if (read_config_option('wmi_autopurge') == 'on') {
print "NOTE: Auto Purging Devices" . PHP_EOL;
$dead_devices = db_fetch_assoc('SELECT host_id
FROM host_wmi_cache AS hwc
LEFT JOIN host
ON host.id = hwc.host_id
WHERE host.id IS NULL');
if (sizeof($dead_devices)) {
foreach($dead_devices as $device) {
db_execute('DELETE FROM host_wmi_cache WHERE host_id='. $device['host_id']);
db_execute('DELETE FROM host_wmi_query WHERE host_id='. $device['host_id']);
print "Purged WMI Device with ID '" . $device['host_id'] . "'" . PHP_EOL;
}
}
}
/* take time and log performance data */
$end = microtime(true);
$cacti_stats = sprintf(
'Time:%01.2f ' .
'Processes:%s ' .
'Devices:%s',
$end - $start,
$concurrent_processes,
cacti_sizeof($devices));
/* log to the database */
set_config_option('stats_wmi', $cacti_stats);
/* log to the logfile */
cacti_log('WMI STATS: ' . $cacti_stats , TRUE, 'SYSTEM');
print "NOTE: Device WMI Polling Completed, $cacti_stats" . PHP_EOL;
}
function process_background_device($host_id, $seed, $key) {
global $config, $debug, $start, $forcerun;
exec_background(read_config_option('path_php_binary'),' -q ' .
$config['base_path'] . '/plugins/wmi/poller_wmi.php' .
' --host-id=' . $host_id .
' --start=' . $start .
' --seed=' . $seed .
' --key=' . $key .
($forcerun ? ' --force':'') .
($debug ? ' --debug':''));
}
function process_device($host_id) {
global $config, $start, $seed, $key, $snmp_errors;
$wmi_errors = 0;
$queries_to_run = db_fetch_assoc_prepared('SELECT h.id, h.wmi_account, htwq.wmi_query_id, wwq.*
FROM host_template_wmi_query AS htwq
LEFT JOIN host AS h
ON htwq.host_template_id = h.host_template_id
LEFT JOIN host_wmi_query AS hwq
ON hwq.host_id = h.id
AND hwq.wmi_query_id = htwq.wmi_query_id
INNER JOIN wmi_wql_queries AS wwq
ON wwq.id = htwq.wmi_query_id
WHERE (UNIX_TIMESTAMP(NOW()) >= UNIX_TIMESTAMP(last_started)+frequency OR last_started IS NULL)
AND h.id = ?
AND wmi_account > 0',
array($host_id));
/* remove the key process and insert the set a process lock */
db_execute('REPLACE INTO wmi_processes (pid, taskid) VALUES (' . getmypid() . ", $seed)");
db_execute("DELETE FROM wmi_processes WHERE pid = $key AND taskid = $seed");
$qstart = date('Y-m-d H:i:s');
if (cacti_sizeof($queries_to_run)) {
foreach($queries_to_run AS $q) {
$qmstart = microtime(true);
cacti_log("NOTE: Executing WMI Query[" . $q['wmi_query_id'] . "] for Device [$host_id].", false, 'WMI', POLLER_VERBOSITY_MEDIUM);
$account = db_fetch_row_prepared('SELECT *
FROM wmi_user_accounts
WHERE id = ?',
array($q['wmi_account']));
if (!cacti_sizeof($account)) {
cacti_log("WARNING: WMI Account ID " . $q['wmi_account'] . " not found for WMI Device[$host_id].", false, 'WMI');
break;
}
$run_before = db_fetch_row_prepared('SELECT *
FROM host_wmi_query
WHERE host_id = ?
AND wmi_query_id = ?',
array($host_id, $q['wmi_query_id']));
if (!cacti_sizeof($run_before)) {
$last_failed = '0000-00-00 00:00:00';
} else {
$last_failed = $run_before['last_failed'];
}
// Run the query and store the data
run_store_wmi_query($host_id, $q['wmi_query_id']);
$status = 0;
$qmend = microtime(true);
if ($status != 0) {
$last_failed = date('Y-m-d H:i:s');
cacti_log("WARNING: Errored WMI Query[" . $q['wmi_query_id'] . "] for Device [$host_id] in " . round($qmend - $qmstart, 2) . " seconds.", false, 'WMI');
} else {
cacti_log("NOTE: Finished WMI Query[" . $q['wmi_query_id'] . "] for Device [$host_id] in " . round($qmend -$qmstart, 2) . " seconds.", false, 'WMI', POLLER_VERBOSITY_MEDIUM);
}
db_execute_prepared('REPLACE INTO host_wmi_query
(host_id, wmi_query_id, sort_field, title_format, last_started, last_runtime, last_failed)
VALUES (?, ?, ?, ?, ?, ?, ?)',
array($host_id, $q['wmi_query_id'], '', '', $qstart, ($qmend - $qmstart), $last_failed)
);
}
} else {
cacti_log("NOTE: WMI Device[$host_id] had no WMI Queries to run this cycle.", false, 'WMI', POLLER_VERBOSITY_MEDIUM);
}
/* remove the process lock */
db_execute('DELETE FROM wmi_processes WHERE pid=' . getmypid());
if ($wmi_errors > 0) {
cacti_log("WARNING: WMI Device[$host_id] experienced $wmi_errors WMI Errors while performing data collection. Increase logging to HIGH for this device to see the errors.", false, 'WMI');
}
}
function display_version() {
global $config;
if (!function_exists('plugin_wmi_version')) {
include_once($config['base_path'] . '/plugins/wmi/setup.php');
}
$info = plugin_wmi_version();
print "Device WMI Poller Process, Version " . $info['version'] . ", " . COPYRIGHT_YEARS . PHP_EOL;
}
function display_help() {
display_version();
print PHP_EOL;
print "The Device WMI poller process script for Cacti." . PHP_EOL . PHP_EOL;
print "usage:" . PHP_EOL;
print "master process: poller_wmi.php [-M] [-f] [-d]" . PHP_EOL;
print "child process: poller_wmi.php --host-id=N [--seed=N] [-f] [-d]" . PHP_EOL . PHP_EOL;
}