-
Notifications
You must be signed in to change notification settings - Fork 6
/
poller_graphs.php
485 lines (401 loc) · 13.9 KB
/
poller_graphs.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/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('../..');
require('./include/cli_check.php');
require_once($config['base_path'] . '/lib/api_automation_tools.php');
require_once($config['base_path'] . '/lib/api_automation.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_device.php');
require_once($config['base_path'] . '/lib/data_query.php');
require_once($config['base_path'] . '/lib/poller.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');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
global $debug, $start, $seed, $forcerun;
$debug = false;
$forcerun = false;
$start = time();
if (cacti_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 '--force':
$forcerun = 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;
}
}
}
/* Do not process if not enabled */
if (read_config_option('hmib_enabled') == '' || !api_plugin_is_enabled('hmib')) {
print 'WARNING: The Host Mib Collection is Down! Exiting' . PHP_EOL;
exit(0);
}
/* see if its time to run */
$last_run = read_config_option('hmib_automation_lastrun');
$frequency = read_config_option('hmib_automation_frequency') * 86400;
debug("Last Run Was '" . date('Y-m-d H:i:s', $last_run) . "', Frequency is '" . ($frequency/86400) . "' Hours");
if ($frequency == 0 && !$forcerun) {
print "NOTE: Graph Automation is Disabled\n";
} elseif (($frequency > 0 && ($start - $last_run) > $frequency) || $forcerun) {
print "NOTE: Starting Automation Process\n";
db_execute("REPLACE INTO settings (name,value) VALUES ('hmib_automation_lastrun', '$start')");
add_graphs();
} else {
print "NOTE: Its Not Time to Run Automation\n";
}
exit(0);
function add_graphs() {
global $config;
/* check for summary changes first */
$host_template = db_fetch_cell("SELECT id
FROM host_template
WHERE hash='7c13344910097cc599f0d0485305361d'");
$host_app_dq = db_fetch_cell("SELECT id
FROM snmp_query
WHERE hash='6b0ef0fe7f1d85bbb6812801ca15a7c5'");
$host_type_dq = db_fetch_cell("SELECT id
FROM snmp_query
WHERE hash='137aeab842986a76cf5bdef41b96c9a3'");
if (!empty($host_template)) {
/* check to see if the template exists */
debug('Host Template Set');
if (db_fetch_cell("SELECT count(*) FROM host_template WHERE id=$host_template")) {
debug('Host Template Exists');
$host_id = db_fetch_cell("SELECT id FROM host WHERE host_template_id=$host_template");
if (empty($host_id)) {
debug('Host MIB Summary Device Not Found, Adding');
} else {
debug("Host Exists Hostname is '" . db_fetch_cell("SELECT description FROM host WHERE id=$host_id"). "'");
}
add_summary_graphs($host_id, $host_template);
} else {
cacti_log('WARNING: Unable to find Host MIB Summary Host Template', true, 'HMIB');
}
} else {
cacti_log('NOTE: Host MIB Summary Host Template Not Specified', true, 'HMIB');
}
add_host_based_graphs();
}
function add_host_based_graphs() {
global $config;
debug('Adding Host Based Graphs');
/* check for host level graphs next data queries */
$host_cpu_dq = db_fetch_cell("SELECT id
FROM snmp_query
WHERE hash='0d1ab53fe37487a5d0b9e1d3ee8c1d0d'");
$host_disk_dq = db_fetch_cell("SELECT id
FROM snmp_query
WHERE hash='9343eab1f4d88b0e61ffc9d020f35414'");
$host_users_gt = db_fetch_cell("SELECT id
FROM graph_templates
WHERE hash='e8462bbe094e4e9e814d4e681671ea82'");
$host_procs_gt = db_fetch_cell("SELECT id
FROM graph_templates
WHERE hash='62205afbd4066e5c4700338841e3901e'");
$hosts = db_fetch_assoc("SELECT host_id, host.description FROM plugin_hmib_hrSystem
INNER JOIN host
ON host.id=plugin_hmib_hrSystem.host_id
WHERE host_status=3 AND host.disabled=''");
if (cacti_sizeof($hosts)) {
foreach($hosts as $h) {
debug("Processing Host '" . $h['description'] . '[' . $h['host_id'] . "]'");
if ($host_users_gt) {
debug('Processing Users');
hmib_gt_graph($h['host_id'], $host_users_gt);
} else {
debug('Users Graph Template Not Set');
}
if ($host_users_gt) {
debug('Processing Processes');
hmib_gt_graph($h['host_id'], $host_procs_gt);
} else {
debug('Processes Graph Template Not Set');
}
debug('Processing Disks');
if ($host_disk_dq) {
/* only numeric > 0 */
$regex = '^[1-9][0-9]*';
$field = 'hrStorageSizeInput';
add_host_dq_graphs($h['host_id'], $host_disk_dq, $field, $regex);
}
if ($host_cpu_dq) {
add_host_dq_graphs($h['host_id'], $host_cpu_dq);
}
debug('Processing CPU');
}
} else {
debug('No Hosts Found');
}
}
function add_host_dq_graphs($host_id, $dq, $field = '', $regex = '', $include = true) {
global $config;
/* add entry if it does not exist */
$exists = db_fetch_cell_prepared("SELECT COUNT(*)
FROM host_snmp_query
WHERE host_id = ?
AND snmp_query_id = ?",
array($host_id, $dq));
if (!$exists) {
db_execute_prepared("REPLACE INTO host_snmp_query
(host_id, snmp_query_id, reindex_method) VALUES (?, ?, ?)",
array($host_id, $dq, 1));
}
/* recache snmp data */
debug('Reindexing Host');
run_data_query($host_id, $dq);
$graph_templates = db_fetch_assoc_prepared('SELECT *
FROM snmp_query_graph
WHERE snmp_query_id = ?',
array($dq));
debug('Adding Graphs');
if (cacti_sizeof($graph_templates)) {
foreach($graph_templates as $gt) {
hmib_dq_graphs($host_id, $dq, $gt['graph_template_id'], $gt['id'], $field, $regex, $include);
}
}
}
function hmib_gt_graph($host_id, $graph_template_id) {
global $config;
$php_bin = read_config_option('path_php_binary');
$base = $config['base_path'];
$name = db_fetch_cell_prepared("SELECT name
FROM graph_templates
WHERE id = ?",
array($graph_template_id));
$assoc = db_fetch_cell_prepared("SELECT count(*)
FROM host_graph
WHERE graph_template_id = ?
AND host_id = ?",
array($graph_template_id, $host_id));
if (!$assoc) {
db_execute_prepared("INSERT INTO host_graph
(host_id, graph_template_id) VALUES (?, ?)",
array($host_id, $graph_template_id));
}
$exists = db_fetch_cell_prepared("SELECT count(*)
FROM graph_local
WHERE host_id = ?
AND graph_template_id = ?",
array($host_id, $graph_template_id));
if (!$exists) {
print "NOTE: Adding Graph: '$name' for Host: " . $host_id;
$command = "$php_bin -q $base/cli/add_graphs.php" .
' --graph-template-id=' . $graph_template_id .
' --graph-type=cg' .
' --host-id=' . $host_id;
$return_code = 0;
$output = array();
$timeout = 20;
exec_with_timeout($command, $output, $return_code, $timeout);
if (cacti_sizeof($output)) {
if ($return_code != 0) {
print "WARNING: add_graphs.php CLI returned a non-zero return code of $return_code" . PHP_EOL;
}
foreach($output as $l) {
print trim($l) . PHP_EOL;
}
} else {
print "WARNING: add_graphs.php CLI returned no data with a return code of $return_code" . PHP_EOL;
}
}
}
function add_summary_graphs($host_id, $host_template) {
global $config;
$php_bin = read_config_option('path_php_binary');
$base = $config['base_path'];
$return_code = 0;
if (empty($host_id)) {
/* add the host */
debug('Adding Host');
$result = exec("$php_bin -q $base/cli/add_device.php --description='Summary Device' --ip=summary --template=$host_template --version=0 --avail=none", $return_code);
} else {
debug('Reindexing Host');
$result = exec("$php_bin -q $base/cli/poller_reindex_hosts.php -id=$host_id -qid=All", $return_code);
}
/* data query graphs first */
debug('Processing Data Queries');
$data_queries = db_fetch_assoc_prepared("SELECT *
FROM host_snmp_query
WHERE host_id = ?",
array($host_id));
if (cacti_sizeof($data_queries)) {
foreach($data_queries as $dq) {
$graph_templates = db_fetch_assoc_prepared('SELECT *
FROM snmp_query_graph
WHERE snmp_query_id = ?',
array($dq['snmp_query_id']));
if (cacti_sizeof($graph_templates)) {
foreach($graph_templates as $gt) {
hmib_dq_graphs($host_id, $dq['snmp_query_id'], $gt['graph_template_id'], $gt['id']);
}
}
}
}
debug('Processing Graph Templates');
$graph_templates = db_fetch_assoc_prepared("SELECT *
FROM host_graph
WHERE host_id = ?",
array($host_id));
if (cacti_sizeof($graph_templates)) {
foreach($graph_templates as $gt) {
/* see if the graph exists already */
$exists = db_fetch_cell_prepared("SELECT COUNT(*)
FROM graph_local
WHERE host_id = ?
AND graph_template_id = ?",
array($host_id, $gt['graph_template_id']));
if (!$exists) {
print "NOTE: Adding item: '" . $gt['graph_template_id'] . "' for Host: " . $host_id;
$command = "$php_bin -q $base/cli/add_graphs.php" .
' --graph-template-id=' . $gt['graph_template_id'] .
' --graph-type=cg' .
' --host-id=' . $host_id;
$output = array();
$return_code = 0;
$timeout = 20;
exec_with_timeout($command, $output, $return_code, $timeout);
if ($return_code == 0) {
if (cacti_sizeof($output)) {
print implode(PHP_EOL, $output);
} else {
print 'Graph command completed successfully without returning any data.' . PHP_EOL;
}
} else {
print 'Graph command completed with errors.' . PHP_EOL;
if (cacti_sizeof($output)) {
print implode(PHP_EOL, $output);
}
}
}
}
}
}
function hmib_dq_graphs($host_id, $query_id, $graph_template_id, $query_type_id,
$field = '', $regex = '', $include = true) {
global $config, $php_bin, $path_grid;
$php_bin = read_config_option('path_php_binary');
$base = $config['base_path'];
if ($field == '') {
$field = db_fetch_cell_prepared("SELECT sort_field
FROM host_snmp_query
WHERE host_id = ?
AND snmp_query_id= ?",
array($host_id, $query_id));
}
$items = db_fetch_assoc_prepared("SELECT *
FROM host_snmp_cache
WHERE field_name = ?
AND host_id = ?
AND snmp_query_id = ?",
array($field, $host_id, $query_id));
if (cacti_sizeof($items)) {
foreach($items as $item) {
$field_value = $item['field_value'];
$index = $item['snmp_index'];
if ($regex == '') {
/* add graph below */
} elseif ((($include == true) && (preg_match('/' . $regex . '/', $field_value))) ||
(($include != true) && (!preg_match('/' . $regex . '/', $field_value)))) {
/* add graph below */
} else {
print "NOTE: Bypassig item due to Regex rule: '" . $field_value . "' for Host: " . $host_id . "\n";
continue;
}
/* check to see if the graph exists or not */
$exists = db_fetch_cell_prepared("SELECT id
FROM graph_local
WHERE host_id = ?
AND snmp_query_id = ?
AND graph_template_id = ?
AND snmp_index = ?",
array($host_id, $query_id, $graph_template_id, $index));
if (!$exists) {
$command = "$php_bin -q $base/cli/add_graphs.php" .
' --graph-template-id=' . $graph_template_id .
' --graph-type=ds' .
' --snmp-query-type-id=' . $query_type_id .
' --host-id=' . $host_id .
' --snmp-query-id=' . $query_id .
' --snmp-field=' . $field .
' --snmp-value=' . cacti_escapeshellarg($field_value);
$results = shell_exec($command);
if ($results != '') {
print "NOTE: Adding item: '$field_value' " . str_replace("\n", ' ', $results) . PHP_EOL;
} else {
print "ERROR: Problem Adding item '$field_value'" . PHP_EOL;
}
}
}
}
}
function debug($message) {
global $debug;
if ($debug) {
print 'DEBUG: ' . trim($message) . "\n";
}
}
function display_version() {
global $config;
if (!function_exists('plugin_hmib_version')) {
include_once($config['base_path'] . '/plugins/hmib/setup.php');
}
$info = plugin_hmib_version();
print "Host MIB Graph Automator, Version " . $info['version'] . ", " . COPYRIGHT_YEARS . "\n";
}
function display_help() {
display_version();
print "\nThe Host MIB process that creates graphs for Cacti.\n\n";
print "usage: poller_graphs.php [--force] [--debug]\n";
}