-
-
Notifications
You must be signed in to change notification settings - Fork 405
/
cmd.php
executable file
·1125 lines (911 loc) · 36.1 KB
/
cmd.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
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/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/ |
+-------------------------------------------------------------------------+
*/
require_once(__DIR__ . '/include/cli_check.php');
require_once(CACTI_PATH_LIBRARY . '/snmp.php');
require_once(CACTI_PATH_LIBRARY . '/poller.php');
require_once(CACTI_PATH_LIBRARY . '/rrd.php');
require_once(CACTI_PATH_LIBRARY . '/ping.php');
if (function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
} else {
declare(ticks = 100);
}
ini_set('max_execution_time', '0');
ini_set('memory_limit', '-1');
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
$first = null;
$last = null;
$allhost = true;
$debug = false;
$mibs = false;
$mode = 'online';
$sessions = array();
$downhosts = array();
$host_count = 0;
$tot_errors = 0;
$poller_id = $config['poller_id'];
$pmessage = false;
$help = false;
$version = false;
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':
case '-v':
$version = true;
break;
case '--help':
case '-H':
case '-h':
$help = true;
break;
case '--poller':
case '-p':
$pmessage = true;
$poller_id = $value;
break;
case '--first':
case '-f':
$first = $value;
$allhost = false;
break;
case '--last':
case '-l':
$last = $value;
$allhost = false;
break;
case '--mibs':
case '-m':
$mibs = true;
break;
case '--mode':
case '-N':
$mode = $value;
break;
case '--debug':
case '-d':
$debug = true;
break;
default:
print "ERROR: Invalid Argument: ($arg)" . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
}
}
if ($version) {
display_version();
exit;
}
if ($help) {
display_help();
exit;
}
global $poller_db_cnn_id, $remote_db_cnn_id, $cactiphp, $using_proc_function;
global $poller_id, $sessions, $downhosts, $print_data_to_stdout;
$maxwidth = get_max_column_width();
if ($pmessage) {
cacti_log('Forcing poller to ' . $value, true, 'POLLER', POLLER_VERBOSITY_HIGH);
}
if ($config['poller_id'] > 1 && $config['connection'] == 'online') {
$poller_db_cnn_id = $remote_db_cnn_id;
} else {
$poller_db_cnn_id = false;
}
if ($first == null && $last == null) {
// This is valid
} elseif (!is_numeric($first) || $first < 0) {
cacti_log('FATAL: The first host in the host range is invalid!', true, 'POLLER');
exit(-1);
} elseif (!is_numeric($last) || $last < 0) {
cacti_log('FATAL: The last host in the host range is invalid!', true, 'POLLER');
exit(-1);
} elseif ($last < $first) {
cacti_log('FATAL: The first host must always be less or equal to the last host!', true, 'POLLER');
exit(-1);
}
// verify the poller_id
if (!is_numeric($poller_id) || $poller_id < 1) {
cacti_log('FATAL: The poller needs to be a positive numeric value', true, 'POLLER');
exit(-1);
}
// notify cacti processes that a poller is running
record_cmdphp_started();
$exists = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host
WHERE poller_id = ?',
array($poller_id)
);
if ($exists == 0 && $poller_id > 1) {
record_cmdphp_done();
db_close();
exit(-1);
}
// install signal handlers for UNIX only
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
}
// record the start time
$start = microtime(true);
$poller_interval = read_config_option('poller_interval');
$script_timeout = read_config_option('script_timeout');
$cron_interval = read_config_option('cron_interval');
$active_profiles = read_config_option('active_profiles');
// check arguments
if ($allhost) {
$sql_where1 = '';
$sql_where2 = '';
$sql_where3 = '';
$params1 = array($poller_id);
$params2 = array($poller_id, POLLER_ACTION_SCRIPT_PHP, POLLER_ACTION_SCRIPT_PHP_COUNT);
$params3 = array($poller_interval, $poller_interval, 0, $poller_interval, $poller_id);
} else {
$sql_where0 = 'WHERE poller_id > ?';
$sql_where1 = ' AND ((h.id >= ? AND h.id <= ?) OR h.id IS NULL)';
$sql_where2 = ' AND pi.host_id >= ? AND pi.host_id <= ?';
$sql_where3 = ' AND pi.host_id >= ? AND pi.host_id <= ?';
$params1 = array($poller_id, $first, $last);
$params2 = array($poller_id, POLLER_ACTION_SCRIPT_PHP, POLLER_ACTION_SCRIPT_PHP_COUNT, $first, $last);
if ($cron_interval == $poller_interval) {
$params3 = array($poller_interval, $poller_interval, 0, $poller_interval, $poller_id, $first, $last);
} else {
$params3 = array($poller_interval, $poller_interval, $poller_interval, $poller_interval, $poller_id, $first, $last);
}
}
if ($debug) {
$print_data_to_stdout = true;
$medium = POLLER_VERBOSITY_LOW;
$hmedium = POLLER_VERBOSITY_LOW;
} elseif ($allhost) {
$print_data_to_stdout = true;
$medium = POLLER_VERBOSITY_LOW;
$hmedium = POLLER_VERBOSITY_MEDIUM;
} else {
$print_data_to_stdout = false;
$medium = POLLER_VERBOSITY_MEDIUM;
$hmedium = POLLER_VERBOSITY_MEDIUM;
}
// address potential exploits
input_validate_input_number($first, 'first');
input_validate_input_number($last, 'last');
if (db_column_exists('sites', 'disabled')) {
$sql_where = 'AND IFNULL(s.disabled, "") != "on"';
} else {
$sql_where = '';
}
if ($active_profiles != 1) {
$poller_items = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . " *
FROM poller_item AS pi
LEFT JOIN host AS h
ON h.id = pi.host_id
LEFT JOIN sites AS s
ON s.id = h.site_id
WHERE pi.poller_id = ?
$sql_where
AND IFNULL(TRIM(h.disabled),'') != 'on'
$sql_where1
AND pi.rrd_next_step <= 0
ORDER by pi.host_id",
$params1
);
$script_server_calls = db_fetch_cell_prepared('SELECT ' . SQL_NO_CACHE . " COUNT(*)
FROM poller_item AS pi
LEFT JOIN host AS h
ON h.id = pi.host_id
LEFT JOIN sites AS s
ON s.id = h.site_id
WHERE pi.poller_id = ?
$sql_where
AND IFNULL(TRIM(h.disabled),'') != 'on'
AND pi.action IN(?, ?)
$sql_where2
AND pi.rrd_next_step <= 0",
$params2
);
// setup next polling interval
db_execute_prepared("UPDATE poller_item AS pi
SET rrd_next_step = IF(rrd_step = ?, 0, IF(rrd_next_step - ? < 0, rrd_step - ?, rrd_next_step - ?))
WHERE poller_id = ?
$sql_where3",
$params3);
} else {
$poller_items = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . " *
FROM poller_item AS pi
LEFT JOIN host AS h
ON h.id = pi.host_id
LEFT JOIN sites AS s
ON s.id = h.site_id
WHERE pi.poller_id = ?
$sql_where
AND IFNULL(TRIM(h.disabled),'') != 'on'
$sql_where1
ORDER by pi.host_id",
$params1
);
$script_server_calls = db_fetch_cell_prepared('SELECT ' . SQL_NO_CACHE . " COUNT(*)
FROM poller_item AS pi
LEFT JOIN host AS h
ON h.id = pi.host_id
LEFT JOIN sites AS s
ON s.id = h.site_id
WHERE pi.poller_id = ?
$sql_where
AND IFNULL(TRIM(h.disabled),'') != 'on'
AND pi.action IN(?, ?)
$sql_where2",
$params2
);
}
if (cacti_sizeof($poller_items) && read_config_option('poller_enabled') == 'on') {
$host_down = false;
$new_host = true;
$last_host = '';
$output_array = array();
$output_count = 0;
$error_ds = array();
$width_dses = array();
/* startup Cacti php polling server and include the
* include file for script processing
*/
if ($script_server_calls > 0) {
$cactides = array(
0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
2 => array('pipe', 'w') // stderr is a pipe to write to
);
$php_path = read_config_option('path_php_binary');
$command = $php_path . ' -q ' .
CACTI_PATH_BASE . '/script_server.php ' .
' --environ=cmd ' .
' --poller=' . $poller_id .
' --mode=' . $config['connection'];
$cactiphp = proc_open($command, $cactides, $pipes);
$output = fgets($pipes[1], 1024);
if (substr_count($output, 'Started') != 0) {
cacti_log('PHP Script Server Started Properly', $print_data_to_stdout, 'POLLER', POLLER_VERBOSITY_HIGH);
}
$using_proc_function = true;
} else {
$using_proc_function = false;
$cactiphp = false;
}
foreach ($poller_items as $item) {
$ds = $item['local_data_id'];
$host_id = $item['host_id'];
// check for host change
if ($host_id != $last_host) {
$new_host = true;
$host_down = false;
$set_spike_kill = false;
if ($last_host != '') {
$host_end = microtime(true);
if ($output_count > 0) {
cacti_log("Device[$last_host] Writing $output_count items to Poller Output Table", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
db_execute('INSERT IGNORE INTO poller_output
(local_data_id, rrd_name, time, output)
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
if (read_config_option('boost_redirect') == 'on' && read_config_option('boost_rrd_update_enable') == 'on') {
db_execute('INSERT IGNORE INTO poller_output_boost
(local_data_id, rrd_name, time, output)
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
}
$output_array = array();
$output_count = 0;
}
db_execute_prepared(
'UPDATE host
SET polling_time = ?
WHERE id = ?
AND deleted = ""',
array(($host_end - $host_start), $last_host)
);
$errors = cacti_sizeof($error_ds);
cacti_log(sprintf('Device[%d] Time[%3.2f] Items[%d] Errors[%d]', $last_host, $host_end - $host_start, $itemcnt, $errors), $print_data_to_stdout, 'POLLER', $hmedium);
if ($errors > 0) {
if (read_config_option('spine_log_level') == 1) {
cacti_log('WARNING: Invalid Response(s), Errors[' . $errors . '] Device[' . $last_host . '] Thread[1] DS[' . implode(', ', $error_ds) . ']', false, 'POLLER');
}
$tot_errors += $errors;
}
}
$error_ds = array();
$itemcnt = 0;
$host_start = microtime(true);
$host_count++;
}
// ping host, find out if there are re-index checks required
if ($new_host && !empty($host_id)) {
$host_down = ping_and_reindex_check($item, $mibs, $script_timeout);
}
$new_host = false;
$last_host = $host_id;
if (!$host_down) {
$output = collect_device_data($item, $error_ds, $script_timeout);
$itemcnt++;
if (read_config_option('poller_debug') == 'on' && strlen($output) > $maxwidth) {
$width_dses[] = $ds;
}
if ($set_spike_kill && !substr_count($output, ':')) {
// insert a U in place of the actual value if the snmp agent restarts
$output_array[] = sprintf('(%d, %s, CURRENT_TIMESTAMP(), "U")', $item['local_data_id'], db_qstr($item['rrd_name']));
} else {
// otherwise, just insert the value received from the poller
$output_array[] = sprintf('(%d, %s, CURRENT_TIMESTAMP(), %s)', $item['local_data_id'], db_qstr($item['rrd_name']), db_qstr($output));
}
if ($output_count > 2000) {
cacti_log("Device[$host_id] Writing $output_count items to Poller Output Table", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
db_execute('INSERT IGNORE INTO poller_output
(local_data_id, rrd_name, time, output)
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
if (read_config_option('boost_redirect') == 'on' && read_config_option('boost_rrd_update_enable') == 'on') {
db_execute('INSERT IGNORE INTO poller_output_boost
(local_data_id, rrd_name, time, output)
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
}
$output_array = array();
$output_count = 0;
} else {
$output_count++;
}
}
// check for an over running poller
$now = microtime(true);
if ($now - $start > $poller_interval) {
cacti_log('WARNING: cmd.php poller has run over its polling interval and therefore is ending');
break;
}
}
// Record the last hosts polling time
$host_end = microtime(true);
// Flush the items to the output table
if ($output_count > 0) {
cacti_log("Device[$host_id] Writing $output_count items to Poller Output Table", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
db_execute('INSERT IGNORE INTO poller_output
(local_data_id, rrd_name, time, output)
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
if (read_config_option('boost_redirect') == 'on' && read_config_option('boost_rrd_update_enable') == 'on') {
db_execute('INSERT IGNORE INTO poller_output_boost
(local_data_id, rrd_name, time, output)
VALUES ' . implode(', ', $output_array), true, $poller_db_cnn_id);
}
}
db_execute_prepared(
'UPDATE host
SET polling_time = ?
WHERE id = ?
AND deleted = ""',
array(($host_end - $host_start), $host_id)
);
$errors = cacti_sizeof($error_ds);
cacti_log(sprintf('Device[%d] Time[%3.2f] Items[%d] Errors[%d]', $last_host, $host_end - $host_start, $itemcnt, $errors), $print_data_to_stdout, 'POLLER', $hmedium);
if ($errors > 0) {
if (read_config_option('spine_log_level') == 1) {
cacti_log('WARNING: Invalid Response(s), Errors[' . $errors . '] Device[' . $host_id . '] Thread[1] DS[' . implode(', ', $error_ds) . ']', false, 'POLLER');
}
$tot_errors += $errors;
}
if (cacti_sizeof($width_dses)) {
cacti_log('WARNING: Long Responses Errors[' . cacti_sizeof($width_dses) . '] DS[' . implode(', ', $width_dses) . ']', false, 'POLLER');
}
if ($using_proc_function && $script_server_calls > 0) {
// close php server process
@fwrite($pipes[0], "quit\r\n");
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value = proc_close($cactiphp);
}
// take time and log performance data
$end = microtime(true);
cacti_log(sprintf(
'Time: %01.4f s, ' .
'Poller: %s, ' .
'Threads: N/A, ' .
'Devices: %d, ' .
'Items: %d, ' .
'Errors: %d',
round($end - $start, 4),
$poller_id,
$host_count,
cacti_sizeof($poller_items),
$tot_errors
), $print_data_to_stdout, 'POLLER', $medium);
} else {
cacti_log('NOTE: There are no items in your poller for this polling cycle!', true, 'POLLER', $medium);
}
// Try to check device status only for devices without poller items
if ($allhost) {
$sql_where = 'AND h.poller_id = ?';
$params = array($poller_id);
} else {
$sql_where = 'AND h.poller_id = ? AND h.id >= ? AND h.id <= ? ';
$params = array($poller_id, $first, $last);
}
$hosts = db_fetch_assoc_prepared("SELECT h.id, h.hostname, h.ping_port, h.ping_method, h.ping_retries, h.ping_timeout, h.availability_method
FROM host AS h
LEFT JOIN poller_item AS pi
ON h.id=pi.host_id
WHERE pi.host_id IS NULL
AND (h.disabled = '' OR h.disabled IS NULL)
$sql_where",
$params);
if (cacti_sizeof($hosts)) {
cacti_log('NOTE: Found ' . cacti_sizeof($hosts) . ' Device(s) for up/down validation only', true, 'POLLER', $medium);
foreach ($hosts as $host) {
$ping = new Net_Ping;
$ping->host = $host;
$ping->port = $host['ping_port'];
// perform the appropriate ping check of the host
if ($ping->ping($host['availability_method'], $host['ping_method'], $host['ping_timeout'], $host['ping_retries'])) {
update_host_status(HOST_UP, $host['id'], $ping, $host['availability_method'], $print_data_to_stdout);
cacti_log("Device[" . $host['id'] ."] STATUS: Device '" . $host['hostname'] . "' is UP.", $print_data_to_stdout, 'POLLER', debug_level($host['id'], POLLER_VERBOSITY_MEDIUM));
} else {
update_host_status(HOST_DOWN, $host['id'], $ping, $host['availability_method'], $print_data_to_stdout);
cacti_log("Device[" . $host['id'] . "] STATUS: Device '" . $host['hostname'] . "' is Down.", $print_data_to_stdout, 'POLLER', debug_level($host['id'], POLLER_VERBOSITY_MEDIUM));
}
}
}
// record the process as having completed
record_cmdphp_done();
// close the database connection
db_close();
exit(0);
// function to assist in logging
function debug_level($host_id, $level) {
global $debug;
static $debug_enabled = array();
if (!is_bool($debug_enabled[$host_id] ?? null)) {
if ($debug) {
$debug_enabled[$host_id] = true;
} else {
$debug_enabled[$host_id] = is_device_debug_enabled($host_id);
}
}
$level = $debug_enabled[$host_id] ? POLLER_VERBOSITY_NONE : $level;
return $level;
}
// let the poller server know about cmd.php being finished
function record_cmdphp_done($pid = '') {
global $poller_id, $poller_db_cnn_id;
if ($pid == '') {
$pid = getmypid();
}
db_execute_prepared(
'UPDATE poller_time
SET end_time=NOW()
WHERE poller_id = ?
AND pid = ?',
array($poller_id, $pid),
true,
$poller_db_cnn_id
);
}
// let cacti processes know that a poller has started
function record_cmdphp_started() {
global $poller_id, $poller_db_cnn_id;
db_execute_prepared(
"INSERT INTO poller_time
(poller_id, pid, start_time, end_time)
VALUES (?, ?, NOW(), '0000-00-00 00:00:00')",
array($poller_id, getmypid()),
true,
$poller_db_cnn_id
);
}
function open_snmp_session($host_id, &$item) {
global $sessions, $downhosts;
if (!isset($item['max_oids'])) {
$item['max_oids'] = read_config_option('max_get_size');
}
if (!isset($sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']]) && !isset($downhosts[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']])) {
$sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']] = cacti_snmp_session(
$item['hostname'],
$item['snmp_community'],
$item['snmp_version'],
$item['snmp_username'],
$item['snmp_password'],
$item['snmp_auth_protocol'],
$item['snmp_priv_passphrase'],
$item['snmp_priv_protocol'],
$item['snmp_context'],
$item['snmp_engine_id'],
$item['snmp_port'],
$item['snmp_timeout'],
read_config_option('snmp_retries'),
$item['max_oids']
);
if ($sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']] === false) {
unset($sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']]);
$downhosts[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']] = true;
return false;
}
}
return $sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']];
}
function snmp_mark_host_down($host_id, &$item) {
global $sessions, $downhosts;
unset($sessions[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']]);
$downhosts[$host_id . '_' . $item['snmp_version'] . '_' . $item['snmp_port']] = true;
}
function update_system_mibs($host_id) {
$system_mibs = array(
'snmp_sysDescr' => '.1.3.6.1.2.1.1.1.0',
'snmp_sysObjectID' => '.1.3.6.1.2.1.1.2.0',
'snmp_sysUpTimeInstanceAlt' => '.1.3.6.1.6.3.10.2.1.3.0',
'snmp_sysUpTimeInstance' => '.1.3.6.1.2.1.1.3.0',
'snmp_sysContact' => '.1.3.6.1.2.1.1.4.0',
'snmp_sysName' => '.1.3.6.1.2.1.1.5.0',
'snmp_sysLocation' => '.1.3.6.1.2.1.1.6.0'
);
$h = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array($host_id));
if (cacti_sizeof($h)) {
$session = open_snmp_session($host_id, $h);
$uptimeAltFound = false;
$uptime = false;
if ($session !== false) {
foreach ($system_mibs as $name => $oid) {
$value = cacti_snmp_session_get($session, $oid);
if ($name == 'snmp_sysUpTimeInstanceAlt' && $value > 0) {
$uptime = $value * 100;
$uptimeAltFound = true;
} elseif ($name == 'snmp_sysUpTimeInstance' && !$uptimeAltFound) {
$uptime = $value;
} elseif ($name != 'snmp_sysUpTimeInstanceAlt' && !empty($value)) {
db_execute_prepared(
"UPDATE host SET $name = ?
WHERE deleted = ''
AND id = ?",
array($value, $host_id)
);
}
}
if ($uptime !== false) {
db_execute_prepared(
"UPDATE host SET snmp_sysUpTimeInstance = ?
WHERE deleted = ''
AND id = ?",
array($uptime, $host_id)
);
}
} else {
cacti_log("WARNING: Unable to open session for System Mib collection for Device[$host_id]", false, 'POLLER');
}
}
}
function collect_device_data(&$item, &$error_ds, $script_timeout) {
global $print_data_to_stdout, $using_proc_function, $pipes, $cactiphp;
$thread_start = microtime(true);
$ds = $item['local_data_id'];
$host_id = $item['host_id'];
$output = 'U';
switch ($item['action']) {
case POLLER_ACTION_SNMP:
if (($item['snmp_version'] == 0) || (($item['snmp_community'] == '') && ($item['snmp_version'] != 3))) {
cacti_log("Device[$host_id] DS[$ds] ERROR: Invalid SNMP Data Source. Please either delete it from the database, or correct it.", $print_data_to_stdout, 'POLLER');
$output = 'U';
} else {
$session = open_snmp_session($host_id, $item);
if ($session !== false) {
$output = cacti_snmp_session_get($session, $item['arg1'], true);
if (!is_numeric($output)) {
if (prepare_validate_result($output) == false) {
$error_ds[$ds] = $ds;
if (read_config_option('spine_log_level') == 2) {
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] OID:" . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER');
}
$output = 'U';
}
}
} else {
snmp_mark_host_down($host_id, $item);
$output = 'U';
$error_ds[$ds] = $ds;
if (read_config_option('spine_log_level') == 2) {
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] OID:" . $item['arg1'] . ', output: U', $print_data_to_stdout, 'POLLER');
}
}
}
$total_time = (microtime(true) - $thread_start) * 1000;
cacti_log("Device[$host_id] DS[$ds] TT[" . round($total_time, 2) . '] SNMP: v' . $item['snmp_version'] . ': ' . $item['hostname'] . ', dsname: ' . $item['rrd_name'] . ', oid: ' . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
case POLLER_ACTION_SCRIPT:
$output = trim(exec_poll($item['arg1'], $script_timeout));
if ($output == 'U') {
$error_ds[$ds] = $ds;
if (read_config_option('spine_log_level') == 2) {
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] SCRIPT: " . $item['arg1'] . ', output: U', $print_data_to_stdout, 'POLLER');
}
} elseif (!is_numeric($output)) {
if (prepare_validate_result($output) == false) {
$error_ds[$ds] = $ds;
if (read_config_option('spine_log_level') == 2) {
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] SCRIPT: " . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER');
}
}
}
$total_time = (microtime(true) - $thread_start) * 1000;
cacti_log("Device[$host_id] DS[$ds] TT[" . round($total_time, 2) . '] SCRIPT: ' . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
case POLLER_ACTION_SCRIPT_PHP:
$output = trim(str_replace("\n", '', exec_poll_php($item['arg1'], $using_proc_function, $pipes, $cactiphp)));
if ($output == 'U') {
$error_ds[$ds] = $ds;
if (read_config_option('spine_log_level') == 2) {
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] SERVER: " . $item['arg1'] . ', output: U', $print_data_to_stdout, 'POLLER');
}
} elseif (!is_numeric($output)) {
if (prepare_validate_result($output) == false) {
$error_ds[$ds] = $ds;
if (read_config_option('spine_log_level') == 2) {
cacti_log("WARNING: Invalid Response, Device[$host_id] DS[$ds] SERVER: " . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER');
}
}
}
$total_time = (microtime(true) - $thread_start) * 1000;
cacti_log("Device[$host_id] DS[$ds] TT[" . round($total_time, 2) . '] SERVER: ' . $item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
default:
$error_ds[$ds] = $ds;
cacti_log("Device[$host_id] DS[$ds] ERROR: Invalid polling option: " . $item['action'], $print_data_to_stdout, 'POLLER');
}
return $output;
}
function ping_and_reindex_check(&$item, $mibs, $script_timeout) {
global $poller_id, $print_data_to_stdout, $set_spike_kill, $poller_db_cnn_id, $pipes, $cactiphp, $using_proc_function;
$ping = new Net_Ping;
$host_id = $item['host_id'];
$host = db_fetch_row_prepared('SELECT hostname, ping_port, ping_method, ping_retries, ping_timeout, availability_method
FROM host
WHERE id = ?',
array($host_id)
);
$ping->host = $item;
$ping->port = $host['ping_port'];
// perform the appropriate ping check of the host
if ($ping->ping($host['availability_method'], $host['ping_method'], $host['ping_timeout'], $host['ping_retries'])) {
$host_down = false;
update_host_status(HOST_UP, $host_id, $ping, $host['availability_method'], $print_data_to_stdout);
cacti_log("Device[$host_id] STATUS: Device '" . $item['hostname'] . "' is UP.", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_DEBUG));
if ($mibs && $host['availability_method'] != 0 && $host['availability_method'] != 3) {
update_system_mibs($host_id);
}
} else {
$host_down = true;
update_host_status(HOST_DOWN, $host_id, $ping, $host['availability_method'], $print_data_to_stdout);
cacti_log("Device[$host_id] STATUS: Device '" . $item['hostname'] . "' is Down.", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_DEBUG));
}
if (!$host_down) {
// do the reindex check for this host
$reindex = db_fetch_assoc_prepared('SELECT ' . SQL_NO_CACHE . ' pr.data_query_id, pr.action,
pr.op, pr.assert_value, pr.arg1
FROM poller_reindex AS pr
WHERE pr.host_id=?',
array($item['host_id'])
);
if (cacti_sizeof($reindex)) {
cacti_log("Device[$host_id] RECACHE: Processing " . cacti_sizeof($reindex) . " items in the auto reindex cache for '" . $item['hostname'] . "'.", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_DEBUG));
foreach ($reindex as $index_item) {
$assert_fail = false;
switch ($index_item['action']) {
case POLLER_ACTION_SNMP:
$session = open_snmp_session($host_id, $item);
if ($session !== false) {
if (trim($index_item['arg1']) == '.1.3.6.1.2.1.1.3.0') {
$output = cacti_snmp_session_get($session, '.1.3.6.1.6.3.10.2.1.3.0');
if ($output > 0) {
$output *= 100;
} else {
$output = cacti_snmp_session_get($session, $index_item['arg1']);
}
} else {
$output = cacti_snmp_session_get($session, $index_item['arg1']);
}
} else {
$output = 'U';
}
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE OID: ' . $index_item['arg1'] . ', (assert:' . $index_item['assert_value'] . ' ' . $index_item['op'] . ' output:' . $output . ')', $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
case POLLER_ACTION_SCRIPT:
$output = trim(exec_poll($index_item['arg1'], $script_timeout));
if (!is_numeric($output)) {
if (prepare_validate_result($output) == false) {
if (strlen($output) > 20) {
$strout = 20;
} else {
$strout = strlen($output);
}
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE Warning: Result from Script not valid. Partial Result: ' . substr($output, 0, $strout), $print_data_to_stdout, 'POLLER');
}
}
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE SCRIPT: ' . $index_item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
case POLLER_ACTION_SCRIPT_PHP:
$output = trim(str_replace("\n", '', exec_poll_php($index_item['arg1'], $using_proc_function, $pipes, $cactiphp)));
if (!is_numeric($output)) {
if (prepare_validate_result($output) == false) {
if (strlen($output) > 20) {
$strout = 20;
} else {
$strout = strlen($output);
}
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE WARNING: Result from Script Server not valid. Partial Result: ' . substr($output, 0, $strout), $print_data_to_stdout, 'POLLER');
}
}
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE SERVER: ' . $index_item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
case POLLER_ACTION_SNMP_COUNT:
$session = open_snmp_session($host_id, $item);
if ($session !== false) {
$output = cacti_sizeof(cacti_snmp_session_walk($session, $index_item['arg1']));
} else {
$output = 'U';
}
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE OID COUNT: ' . $index_item['arg1'] . ', output: ' . $output, $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
case POLLER_ACTION_SCRIPT_COUNT:
// count items found
$script_index_array = exec_into_array($index_item['arg1']);
$output = cacti_sizeof($script_index_array);
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE CMD COUNT: ' . $index_item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
case POLLER_ACTION_SCRIPT_PHP_COUNT:
$output = exec_into_array($index_item['arg1']);
$output = cacti_sizeof($output);
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE SERVER COUNT: ' . $index_item['arg1'] . ", output: $output", $print_data_to_stdout, 'POLLER', debug_level($host_id, POLLER_VERBOSITY_MEDIUM));
break;
default:
cacti_log("Device[$host_id] DQ[" . $index_item['data_query_id'] . '] RECACHE ERROR: Invalid reindex option: ' . $index_item['action'], $print_data_to_stdout, 'POLLER');
}
/* assert the result with the expected value in the
* db; recache if the assert fails
*/
/* TODO: remove magic ":" from poller_command["command"]; this may interfere with scripts */
if (($index_item['op'] == '=') && ($index_item['assert_value'] != trim($output))) {
cacti_log("Device[$host_id] HT[1] DQ[" . $index_item['data_query_id'] . "] RECACHE ASSERT FAILED '" . $index_item['assert_value'] . '=' . trim($output), $print_data_to_stdout, 'POLLER');
db_execute_prepared(
'REPLACE INTO poller_command
(poller_id, time, action, command)
VALUES (?, NOW(), ?, ?)',
array($poller_id, POLLER_COMMAND_REINDEX, $item['host_id'] . ':' . $index_item['data_query_id']),
true,
$poller_db_cnn_id
);
$assert_fail = true;
} elseif (($index_item['op'] == '>') && ($index_item['assert_value'] < trim($output))) {
cacti_log("Device[$host_id] HT[1] DQ[" . $index_item['data_query_id'] . "] RECACHE ASSERT FAILED '" . $index_item['assert_value'] . '>' . trim($output), $print_data_to_stdout, 'POLLER');
db_execute_prepared(