Repository navigation
Expand file tree
/
Copy pathpoller_graphs.php
More file actions
executable file
·764 lines (649 loc) · 25.2 KB
/
Copy pathpoller_graphs.php
File metadata and controls
executable file
·764 lines (649 loc) · 25.2 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 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/ |
+-------------------------------------------------------------------------+
*/
$no_http_headers = true;
chdir(__DIR__);
chdir('../..');
include('./include/global.php');
include_once('./lib/poller.php');
include_once('./lib/utility.php');
include_once('./lib/data_query.php');
include_once('./lib/api_graph.php');
include_once('./lib/api_tree.php');
include_once('./lib/api_data_source.php');
include_once('./lib/api_aggregate.php');
ini_set('memory_limit', '256M');
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
global $debug, $start, $seed, $forcerun;
$debug = false;
$forcerun = false;
$start = time();
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
[$arg, $value] = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--debug':
case '-d':
$debug = true;
break;
case '--force':
case '-f':
$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('mikrotik_enabled') == '' || db_fetch_cell("SELECT status FROM plugin_config WHERE directory='mikrotik'") != 1) {
print "WARNING: The MiktroTik Collection is Down! Exiting\n";
exit(0);
}
// see if its time to run
$last_run = (int) read_config_option('mikrotik_automation_lastrun');
$frequency = (int) read_config_option('mikrotik_automation_frequency') * 60;
debug("Last Run Was '" . date('Y-m-d H:i:s', $last_run) . "', Frequency is '" . round($frequency / 60, 1) . "' Minutes");
if ($frequency == 0) {
print "NOTE: Graph Automation is Disabled\n";
} elseif (($frequency > 0 && ($start - $last_run) > $frequency) || ($frequency > 0 && $forcerun)) {
$start = microtime(true);
print "NOTE: Starting Automation Process\n";
set_config_option('mikrotik_automation_lastrun', $start);
debug('Removing invalid stations graphs');
remove_invalid_station_graphs();
debug('Adding Graphs');
add_graphs();
$end = microtime(true);
$cacti_stats = sprintf('Time:%0.2f ', $end - $start);
// log to the database
set_config_option('stats_mikrotik_graphs', $cacti_stats);
// log to the logfile
cacti_log('MIKROTIK GRAPH STATS: ' . $cacti_stats , true, 'SYSTEM');
} else {
print "NOTE: Its Not Time to Run Automation\n";
}
exit(0);
/**
* Entry point for the graph-automation cycle: delegates to
* add_host_based_graphs() to create any missing per-host graphs.
* Called from this script's main flow on every poller_graphs.php run.
*
* @return void
*
* @global array $config Reserved/declared for parity with other
* functions in this file; not used directly
* here.
*/
function add_graphs(): void {
global $config;
// /* check for summary changes first */
// $host_template = read_config_option('mikrotik_summary_host_template');
// $host_type_dq = read_config_option('mikrotik_dq_host_type');
// if (!empty($host_template)) {
// /* check to see if the template exists */
// debug('Host Template Set');
//
// if (db_fetch_cell_prepared('SELECT count(*) FROM host_template WHERE id = ?', array($host_template))) {
// debug('Host Template Exists');
//
// $host_id = db_fetch_cell_prepared('SELECT id FROM host WHERE host_template_id = ?', array($host_template));
// if (empty($host_id)) {
// debug('MikroTik Summary Device Not Found, Adding');
// } else {
// debug("Host Exists Hostname is '" . db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array($host_id)). "'");
// }
//
//
// add_summary_graphs($host_id, $host_template);
// } else {
// cacti_log('WARNING: Unable to find MikroTik Summary Host Template', true, 'MIKROTIK');
// }
// } else {
// cacti_log('NOTE: MikroTik Summary Host Template Not Specified', true, 'MIKROTIK');
// }
add_host_based_graphs();
}
/**
* Scans every active MikroTik host and, for each, adds any missing
* standard device graphs (via mikrotik_gt_graph()), missing data-query
* graphs (via add_host_dq_graphs(), excluding entries matching the
* configured 'mikrotik_user_exclusion' pattern for the users data
* query only; no exclusion pattern is applied to other data queries),
* and missing health-metric graphs based on which health columns the
* device has data for. Called from add_graphs().
*
* @return void
*
* @global array $config Cacti global configuration
* array (declared but not
* directly used here).
* @global array $device_hashes The list of standard
* device-level graph template
* hashes to ensure exist for every
* host.
* @global array $device_query_hashes The list of standard data-query
* hashes to ensure are attached to
* every host.
* @global array $device_health_hashes Map of health column name =>
* graph template hash, used to
* conditionally add health graphs
* only for populated metrics.
*/
function add_host_based_graphs(): void {
global $config, $device_hashes, $device_query_hashes, $device_health_hashes;
debug('Adding Host Based Graphs');
// check for host level graphs next data queries
$host_cpu_dq = read_config_option('mikrotik_dq_host_cpu');
$host_users_dq = mikrotik_data_query_by_hash('ce63249e6cc3d52bc69659a3f32194fe');
$hosts = db_fetch_assoc("SELECT host_id, host.description, host.hostname
FROM plugin_mikrotik_system
INNER JOIN host
ON host.id=plugin_mikrotik_system.host_id
WHERE host_status IN(0,3) AND host.disabled=''");
if (cacti_sizeof($hosts)) {
foreach ($hosts as $h) {
debug('Processing Host: ' . $h['description'] . ' [' . $h['hostname'] . ']');
foreach ($device_hashes as $hash) {
$template = mikrotik_template_by_hash($hash);
if (!empty($template)) {
debug('Processing ' . db_fetch_cell_prepared('SELECT name FROM graph_templates WHERE hash = ?', [$hash]));
mikrotik_gt_graph($h['host_id'], $template);
}
}
foreach ($device_query_hashes as $hash) {
$query = mikrotik_data_query_by_hash($hash);
if (!empty($query)) {
debug('Processing ' . db_fetch_cell_prepared('SELECT name FROM snmp_query WHERE hash = ?', [$hash]));
if ($hash == '7dd90372956af1dc8ec7b859a678f227') {
$exclusion = read_config_option('mikrotik_user_exclusion');
add_host_dq_graphs($h['host_id'], $query, 'userName', $exclusion, false);
} else {
add_host_dq_graphs($h['host_id'], $query);
}
}
}
$health = db_fetch_row_prepared('SELECT * FROM plugin_mikrotik_system_health WHERE host_id = ?', [$h['host_id']]);
debug('Processing Health');
if (cacti_sizeof($health)) {
foreach ($device_health_hashes as $column => $hash) {
if (!empty($health[$column]) && $health[$column] != 'NULL') {
$template = mikrotik_template_by_hash($hash);
if (!empty($template)) {
debug('Processing ' . db_fetch_cell_prepared('SELECT name FROM graph_templates WHERE hash = ?', [$hash]));
mikrotik_gt_graph($h['host_id'], $template);
}
}
}
}
}
} else {
debug('No Hosts Found');
}
}
/**
* Ensures a data query is attached to a host (adding it if missing),
* reindexes it, then adds a graph for every graph template associated
* with that data query (via mikrotik_dq_graphs()). Called from
* add_host_based_graphs() for each of the standard MikroTik data
* queries.
*
* @param int $host_id The host id to add the data query/graphs for.
* @param int $dq The snmp_query id to attach and graph.
* @param string $field Optional sort/match field name used to filter
* which data query rows get graphed (passed
* through to mikrotik_dq_graphs()).
* @param string $regex Optional regex pattern used to include/exclude
* matching rows (passed through to
* mikrotik_dq_graphs()).
* @param bool $include Whether $regex matches should be included
* (true) or excluded (false) (passed through to
* mikrotik_dq_graphs()).
*
* @return void
*
* @global array $config Reserved/declared for parity with other
* functions in this file; not used directly
* here.
*/
function add_host_dq_graphs(int $host_id, int $dq, string $field = '', string $regex = '', bool $include = true): void {
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 = ?', [$host_id, $dq]);
if (!$exists) {
db_execute_prepared('REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) VALUES (?, ?, 1)', [$host_id, $dq]);
}
// 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 = ?', [$dq]);
debug('Adding Graphs');
if (cacti_sizeof($graph_templates)) {
foreach ($graph_templates as $gt) {
mikrotik_dq_graphs($host_id, $dq, $gt['graph_template_id'], $gt['id'], $field, $regex, $include);
}
}
}
/**
* Ensures a host-level graph template is associated with a host (adding
* the host_graph mapping if missing) and, if no matching graph exists
* yet, invokes Cacti's add_graphs.php CLI tool to create it. Called
* from add_host_based_graphs() for each standard device-level and
* health graph template.
*
* @param int $host_id The host id to add the graph for.
* @param int $graph_template_id The graph_templates id to add.
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* resolve the PHP binary and add_graphs.php
* path.
*/
function mikrotik_gt_graph(int $host_id, int $graph_template_id): void {
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 = ?',
[$graph_template_id]);
$assoc = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host_graph
WHERE graph_template_id = ?
AND host_id = ?',
[$graph_template_id, $host_id]);
if (!$assoc) {
db_execute_prepared('INSERT INTO host_graph
(host_id, graph_template_id)
VALUES (?, ?)',
[$host_id, $graph_template_id]);
}
$exists = db_fetch_cell_prepared('SELECT count(*)
FROM graph_local
WHERE host_id = ?
AND graph_template_id = ?',
[$host_id, $graph_template_id]);
if (!$exists) {
print "NOTE: Adding Graph: '$name' for Host: " . $host_id . PHP_EOL;
$command = "$php_bin -q $base/cli/add_graphs.php" .
" --graph-template-id=$graph_template_id" .
' --graph-type=cg' .
' --host-id=' . $host_id;
execute_automation($command, 'Template Graph', $name);
}
}
/**
* Adds summary graphs for a designated MikroTik summary host template,
* by invoking Cacti's add_graphs.php CLI tool for the given host and
* template. Currently unused by add_graphs() (its calling code is
* commented out) but retained for the summary-graph feature.
*
* @param int $host_id The summary host id to add graphs for.
* @param int $host_template The host_template id whose graph templates
* should be added.
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* resolve the PHP binary and add_graphs.php
* path.
*/
function add_summary_graphs(int $host_id, int $host_template): void {
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');
$command = "$php_bin -q $base/cli/add_device.php --description='Summary Device' --ip=summary --template=$host_template --version=0 --avail=none";
execute_automation($command, 'Device Creation', 'Summary Device');
} else {
debug('Reindexing Host');
$command = "$php_bin -q $base/cli/poller_reindex_hosts.php -id=$host_id -qid=All";
execute_automation($command, 'Device Re-Index', 'Summary Device');
}
// data query graphs first
debug('Processing Data Queries');
$data_queries = db_fetch_assoc_prepared('SELECT *
FROM host_snmp_query
WHERE host_id = ?',
[$host_id]);
if (cacti_sizeof($data_queries)) {
foreach ($data_queries as $dq) {
$graph_templates = db_fetch_assoc('SELECT *
FROM snmp_query_graph
WHERE snmp_query_id=' . $dq['snmp_query_id']);
if (cacti_sizeof($graph_templates)) {
foreach ($graph_templates as $gt) {
mikrotik_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 = ?',
[$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= ?',
[$host_id, $gt['graph_template_id']]);
if (!$exists) {
$name = db_fetch_cell_prepared('SELECT name
FROM graph_templates
WHERE id = ?',
[$gt['graph_template_id']]);
print "NOTE: Adding item for Graph Template: '$name' for Host: '$host_id'" . PHP_EOL;
$command = "$php_bin -q $base/cli/add_graphs.php" .
' --graph-template-id=' . $gt['graph_template_id'] .
' --graph-type=cg' .
' --host-id=' . $host_id;
execute_automation($command, 'Template Graphs', $name);
}
}
}
}
/**
* Adds a data-query-driven graph for every host_snmp_cache row matching
* a data query's sort field (optionally filtered by a regex
* include/exclude rule on the field value), for any row that doesn't
* already have a matching graph, by invoking Cacti's add_graphs.php CLI
* tool. Called from add_host_dq_graphs() for each graph template
* associated with a data query.
*
* @param int $host_id The host id to add graphs for.
* @param int $query_id The snmp_query id being graphed.
* @param int $graph_template_id The graph_templates id to add for
* each matching row.
* @param int $query_type_id The snmp_query_graph id identifying
* this specific graph type within the
* data query.
* @param string $field The host_snmp_cache field name to
* match rows on; defaults to the data
* query's configured sort field.
* @param string $regex Optional regex pattern applied to
* each row's field value to
* include/exclude it from graphing.
* @param bool $include Whether $regex matches should be
* included (true) or excluded (false).
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* resolve the PHP binary and add_graphs.php
* path.
* @global string $php_bin Populated here with the resolved PHP
* binary path.
* @global string $path_grid Reserved/declared for parity with other
* functions in this file; not used directly
* here.
*/
function mikrotik_dq_graphs(int $host_id, int $query_id, int $graph_template_id, int $query_type_id, string $field = '', string $regex = '', bool $include = true): void {
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 = ?',
[$host_id, $query_id]);
}
$items = db_fetch_assoc_prepared('SELECT *
FROM host_snmp_cache
WHERE field_name = ?
AND host_id = ?
AND snmp_query_id = ?',
[$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 == false && preg_match("/$regex/", $field_value)) {
print "NOTE: Bypassing item due to Regex rule: '$regex', Field Value: '" . $field_value . "' for Host: '" . $host_id . "'\n";
continue;
} elseif ($include == true && preg_match("/$regex/", $field_value)) {
// add graph below, we should never be here
} else {
print "NOTE: Not Bypassing item due to Regex rule: '$regex', Field Value: '" . $field_value . "' for Host: '" . $host_id . "'\n";
}
// 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 = ?',
[$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);
execute_automation($command, 'Data Query Graph', $field_value);
}
}
}
}
/**
* Runs a shell command (typically Cacti's add_graphs.php CLI tool) and
* prints a success/warning summary of its output. Called from
* mikrotik_gt_graph(), add_summary_graphs(), and mikrotik_dq_graphs()
* after building each graph-creation command.
*
* @param string $command The shell command to execute.
* @param string $type A short label describing the command's
* purpose, used in the printed summary.
* @param string $field_value An identifying value (e.g. the graphed
* item's field value) included in the
* printed summary.
*
* @return void
*/
function execute_automation(string $command, string $type, string $field_value = ''): void {
$return = 0;
$output = [];
$lline = exec($command, $output, $return);
if ($return > 0) {
print "WARNING: Error for $type command and item '$field_value' Error Code:'$return'. Results below." . PHP_EOL;
if (sizeof($output)) {
foreach ($output as $l) {
print trim('WARNING Response: ' . $l) . PHP_EOL;
}
}
} else {
print trim("NOTE: $type command for item: '$field_value' succeeded. Results relow") . PHP_EOL;
if (sizeof($output)) {
foreach ($output as $l) {
print trim('Response: ' . $l) . PHP_EOL;
}
}
}
}
/**
* Removes graphs created from now-obsolete wireless station graph
* templates (identified by a fixed list of legacy template hashes),
* cleaning up after a template consolidation/rename. Called from this
* script's main flow as a one-time/periodic cleanup step.
*
* @return void
*/
function remove_invalid_station_graphs(): void {
$old_wireless_station_hashes = [
'0e88ad681dda36417a537c2e06a2add3',
'8cea2d49a035d5424ff28b9856d78053',
'0a0e496b94667220dce953cb374cee7c',
'98ee665dc39e0404a272c87cc4efea2e'
];
// Remove incorrect graphs
foreach ($old_wireless_station_hashes as $hash) {
$graph_template_id = db_fetch_cell_prepared('SELECT id
FROM graph_templates
WHERE hash = ?',
[$hash]);
$snmp_query_ids[] = db_fetch_cell_prepared('SELECT sqg.snmp_query_id
FROM snmp_query_graph AS sqg
INNER JOIN graph_templates AS gt
ON sqg.graph_template_id=gt.id
WHERE gt.hash = ?', [$hash]);
if ($graph_template_id > 0) {
mikrotik_delete_graphs_and_data_sources_from_hash($graph_template_id);
// Remove graph templates
db_execute_prepared('DELETE FROM graph_templates
WHERE id = ?',
[$graph_template_id]);
$graph_template_input = db_fetch_assoc_prepared('SELECT id
FROM graph_template_input
WHERE graph_template_id = ?',
[$graph_template_id]);
if (cacti_sizeof($graph_template_input)) {
foreach ($graph_template_input as $item) {
db_execute_prepared('DELETE FROM graph_template_input_defs
WHERE graph_template_input_id = ?', [$item['id']]);
}
}
db_execute_prepared('DELETE FROM graph_template_input
WHERE graph_template_id = ?',
[$graph_template_id]);
db_execute_prepared('DELETE FROM graph_templates_graph
WHERE graph_template_id = ?',
[$graph_template_id]);
db_execute_prepared('DELETE FROM graph_templates_item
WHERE graph_template_id = ?',
[$graph_template_id]);
db_execute_prepared('DELETE FROM host_template_graph
WHERE graph_template_id = ?',
[$graph_template_id]);
}
}
if (!empty($snmp_query_ids)) {
foreach ($snmp_query_ids as $snmp_query_id) {
db_execute_prepared('DELETE FROM host_template_snmp_query WHERE snmp_query_id = ?', [$snmp_query_id]);
db_execute_prepared('DELETE FROM host_snmp_query WHERE snmp_query_id = ?', [$snmp_query_id]);
db_execute_prepared('DELETE FROM snmp_query_graph WHERE snmp_query_id = ?', [$snmp_query_id]);
}
}
$old_data_template_hashes = [
'2e88a62f3d3bd3756ab48a9613e86439',
'852ab786ca385b1bd87d1308d7e3ae75',
'2828f43f6d8e477ee5616da510ccc314',
'ca928def30203cc6d7daed75d826f91c'
];
foreach ($old_data_template_hashes as $hash) {
$data_template_id = db_fetch_cell_prepared('SELECT id
FROM data_template
WHERE hash = ?',
[$hash]);
if (!empty($data_template_id)) {
db_execute_prepared('DELETE FROM data_template_data
WHERE data_template_id = ?', [$data_template_id]);
db_execute_prepared('DELETE FROM data_template_rrd
WHERE data_template_id = ?', [$data_template_id]);
db_execute_prepared('DELETE FROM snmp_query_graph_rrd
WHERE data_template_id = ?', [$data_template_id]);
db_execute_prepared('DELETE FROM snmp_query_graph_rrd_sv
WHERE data_template_id = ?', [$data_template_id]);
db_execute_prepared('DELETE FROM data_template
WHERE id = ?' , [$data_template_id]);
db_execute_prepared('DELETE FROM data_local
WHERE data_template_id = ?' , [$data_template_id]);
}
}
}
/**
* Prints a debug message to stdout when CLI debug output is enabled.
* Called throughout this script to report graph-automation progress.
*
* @param string $message The debug message to print.
*
* @return void
*
* @global bool $debug Whether debug output ('--debug' CLI flag) is
* enabled; when false, this function is a no-op.
*/
function debug(string $message): void {
global $debug;
if ($debug) {
print 'DEBUG: ' . trim($message) . "\n";
}
}
/**
* Prints this script's version and copyright banner, loading the
* plugin's version info from setup.php if not already available.
* Called from display_help() and when invoked with '--version'/'-v'.
*
* @return void
*
* @global array $config Cacti global configuration array; used to
* locate and include setup.php.
*/
function display_version(): void {
global $config;
if (!function_exists('plugin_mikrotik_version')) {
include_once($config['base_path'] . '/plugins/mikrotik/setup.php');
}
$info = plugin_mikrotik_version();
print 'MikroTik Graph Automator, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . "\n";
}
/**
* Prints the version banner followed by this script's command-line
* usage summary. Called when invoked with '--help'/'-h' or with
* invalid/missing arguments.
*
* @return void
*/
function display_help(): void {
display_version();
print "\nThe MikroTik process that creates graphs for Cacti.\n\n";
print "usage: poller_graphs.php [-f] [-d]\n";
}