forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboost.php
More file actions
1635 lines (1356 loc) · 50.4 KB
/
boost.php
File metadata and controls
1635 lines (1356 loc) · 50.4 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
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
<?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/ |
+-------------------------------------------------------------------------+
*/
/**
* boost_array_orderby - performs a multicolumn sort of an
* array
*/
function boost_array_orderby() {
$args = func_get_args();
$data = array_shift($args);
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row) {
$tmp[$key] = $row[$field];
}
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
function boost_file_size_display($file_size, $digits = 2) {
if ($file_size > 1024) {
$file_size = $file_size / 1024;
if ($file_size > 1024) {
$file_size = $file_size / 1024;
if ($file_size > 1024) {
$file_size = $file_size / 1024;
return __('%s GBytes', number_format_i18n($file_size, $digits));
} else {
return __('%s MBytes', number_format_i18n($file_size, $digits));
}
} else {
return __('%s KBytes', number_format_i18n($file_size, $digits));
}
} else {
return __('%s Bytes', number_format_i18n($file_size, $digits));
}
}
function boost_get_total_rows() {
return db_fetch_cell("SELECT SUM(TABLE_ROWS)
FROM information_schema.tables
WHERE table_schema = SCHEMA()
AND (table_name LIKE 'poller_output_boost_arch_%'
OR table_name LIKE 'poller_output_boost')");
}
function boost_error_handler($errno, $errmsg, $filename, $linenum, $vars = []) {
if (read_config_option('log_verbosity') >= POLLER_VERBOSITY_DEBUG) {
/* define all error types */
$errortype = array(
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice'
);
if (defined('E_RECOVERABLE_ERROR')) {
$errortype[E_RECOVERABLE_ERROR] = 'Catchable Fatal Error';
}
if (defined('E_DEPRECATED')) {
$errortype[E_DEPRECATED] = 'Deprecated Warning';
}
/* create an error string for the log */
$err = "ERRNO:'" . $errno . "' TYPE:'" . $errortype[$errno] .
"' MESSAGE:'" . $errmsg . "' IN FILE:'" . $filename .
"' LINE NO:'" . $linenum . "'";
/* let's ignore some lesser issues */
if (substr_count($errmsg, 'date_default_timezone')) return;
if (substr_count($errmsg, 'Only variables')) return;
/* log the error to the Cacti log */
cacti_log('PROGERR: ' . $err, false, 'BOOST');
}
return;
}
function boost_check_correct_enabled() {
if ((read_config_option('boost_rrd_update_enable') == 'on') ||
(read_config_option('boost_rrd_update_system_enable') == 'on')) {
/* turn on the system level updates as that is what dictates "off" */
if (read_config_option('boost_rrd_update_system_enable') != 'on') {
db_execute("REPLACE INTO settings (name,value)
VALUES ('boost_rrd_update_system_enable','on')");
}
} else {
restore_error_handler();
return false;
}
return true;
}
function boost_poller_on_demand(&$results) {
global $config, $remote_db_cnn_id;
if ($config['poller_id'] > 1 && $config['connection'] == 'online') {
$conn = $remote_db_cnn_id;
} else {
$conn = false;
}
if (read_config_option('boost_rrd_update_enable') == 'on' || $config['poller_id'] > 1) {
set_config_option('boost_rrd_update_enable', 'on');
/* suppress warnings */
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL ^ E_DEPRECATED);
} else {
error_reporting(E_ALL);
}
/* install the boost error handler */
set_error_handler('boost_error_handler');
$out_buffer = '';
$sql_prefix = 'INSERT INTO poller_output_boost (local_data_id, rrd_name, time, output) VALUES ';
$sql_suffix = ' ON DUPLICATE KEY UPDATE output=VALUES(output)';
// Add 1 here for potential delimiter
$overhead = strlen($sql_prefix) + strlen($sql_suffix) + 1;
if (boost_check_correct_enabled()) {
/* if boost redirect is on, rows are being inserted directly */
if (read_config_option('boost_redirect') == 'on') {
restore_error_handler();
return false;
}
$max_allowed_packet = db_fetch_row("SHOW VARIABLES LIKE 'max_allowed_packet'");
$max_allowed_packet = $max_allowed_packet['Value'];
if (cacti_sizeof($results)) {
$delim = '';
$delim_len = 0;
$out_length = 0;
foreach($results as $result) {
$tmp_buffer =
"('" .
$result['local_data_id'] . "','" .
$result['rrd_name'] . "','" .
$result['time'] . "','" .
$result['output'] . "')";
$tmp_length = strlen($tmp_buffer);
// Calculate length of output buffer, plus overhead, plus the temp buffer
// is it greater than what SQL allows?
if (($out_length + $overhead + $tmp_length) > $max_allowed_packet) {
// Overall length was greater, but do we actually have anything
// already buffered? Or was it just the temp buffer that overflowed
// things?
if ($out_length > 0) {
db_execute($sql_prefix . $out_buffer . $sql_suffix, true, $conn);
}
// Make the temp buffer the starting point for the output buffer, but
// we don't need a delimiter at this point, so don't include it
$out_buffer = $tmp_buffer;
$out_length = $tmp_length;
} else {
// We didn't overflow so lets add the temp buffer to the output buffer
// and include the delimiter string/length. This will be a blank
// delimiter on the first iteration as the output buffer will always
// be blank.
$out_buffer .= $delim . $tmp_buffer;
$out_length += $delim_len + $tmp_length;
}
// Only on the first iteration do we need to set the delimiter as
// after that, we will always need it when we are not overflowing
if ($delim_len == 0) {
$delim = ',';
$delim_len = strlen($delim);
}
}
// output buffer had something left, lets flush it
if ($out_buffer != '') {
db_execute($sql_prefix . $out_buffer . $sql_suffix, true, $conn);
}
}
$return_value = false;
} else {
$return_value = true;
}
/* restore original error handler */
restore_error_handler();
return $return_value;
} else {
return true;
}
}
function boost_poller_id_check() {
global $config;
$storage_location = read_config_option('storage_location');
/* error out if running from a remote poller and the storage
* location is not the RRDproxy */
if ($config['poller_id'] > 1) {
if ($config['connection'] == 'online') {
if ($storage_location == 0) {
return false;
} else {
return true;
}
} else {
return false;
}
}
return true;
}
function boost_fetch_cache_check($local_data_id, $rrdtool_pipe = false) {
global $config;
if (read_config_option('boost_rrd_update_enable') == 'on') {
/* include poller processing routines */
include_once($config['library_path'] . '/poller.php');
/* check to see if boost can do its job */
if (!boost_poller_id_check()) {
return false;
}
/* suppress warnings */
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL ^ E_DEPRECATED);
} else {
error_reporting(E_ALL);
}
/* install the boost error handler */
set_error_handler('boost_error_handler');
/* process input parameters */
if (!is_resource($rrdtool_pipe)) {
$rrdtool_pipe = rrd_init();
$close_pipe = true;
} else {
$close_pipe = false;
}
/* get the information to populate into the rrd files */
if (boost_check_correct_enabled()) {
boost_process_poller_output($local_data_id, $rrdtool_pipe);
}
/* restore original error handler */
restore_error_handler();
/* close rrdtool */
if ($close_pipe) {
rrd_close($rrdtool_pipe);
}
}
}
function boost_return_cached_image(&$graph_data_array) {
if (isset($graph_data_array['export_csv'])) {
return false;
} elseif (isset($graph_data_array['export_realtime'])) {
return false;
} elseif (isset($graph_data_array['disable_cache']) && $graph_data_array['disable_cache'] == true) {
return false;
} elseif (read_config_option('boost_png_cache_enable') == 'on' && boost_determine_caching_state()) {
return true;
} else {
return false;
}
}
function boost_graph_cache_check($local_graph_id, $rra_id, $rrdtool_pipe, &$graph_data_array, $return = true) {
global $config;
/* include poller processing routines */
include_once($config['library_path'] . '/poller.php');
/* suppressnwarnings */
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL ^ E_DEPRECATED);
} else {
error_reporting(E_ALL);
}
/* install the boost error handler */
set_error_handler('boost_error_handler');
/* check to see if boost can do its job */
if (!boost_poller_id_check()) {
return false;
}
/* This is a realtime graph */
if (isset($graph_data_array['export_realtime'])) {
/* restore original error handler */
restore_error_handler();
return false;
}
/* if we are just printing the rrd command return */
if (isset($graph_data_array['print_source'])) {
/* restore original error handler */
restore_error_handler();
return false;
}
/* if we want to view the error message, then don't show the cache */
if ((isset($graph_data_array['output_flag'])) &&
($graph_data_array['output_flag'] == RRDTOOL_OUTPUT_STDERR)) {
/* restore original error handler */
restore_error_handler();
return false;
}
/* get the information to populate into the rrd files */
if (boost_check_correct_enabled()) {
/* before we make a graph, we need to check for rrd updates and perform them. */
$local_data_ids = db_fetch_assoc_prepared('SELECT DISTINCT
data_template_rrd.local_data_id
FROM graph_templates_item
INNER JOIN data_template_rrd
ON (graph_templates_item.task_item_id = data_template_rrd.id)
WHERE graph_templates_item.local_graph_id = ?
AND data_template_rrd.local_data_id > 0', array($local_graph_id));
/* first update the RRD files */
if (cacti_sizeof($local_data_ids)) {
$updates = 0;
foreach($local_data_ids as $local_data_id) {
$updates += boost_process_poller_output($local_data_id['local_data_id'], $rrdtool_pipe);
}
if ($updates) {
/* restore original error handler */
restore_error_handler();
return false;
}
}
}
if (isset($_SESSION['sess_current_timespan'])) {
$timespan = $_SESSION['sess_current_timespan'];
} else {
$timespan = 0;
}
/* check the graph cache and use it if it is valid, otherwise turn over to
* cacti's graphing functions.
*/
if (boost_return_cached_image($graph_data_array)) {
/* if timespan is greater than 1, it is a predefined, if it does not
* exist, it is the old fashioned MRTG type graph
*/
$cache_directory = read_config_option('boost_png_cache_directory');
if ($cache_directory != '') {
if (is_dir($cache_directory)) {
if (is_writable($cache_directory)) {
if ($rra_id > 0) {
$cache_file = $cache_directory . '/' . get_selected_theme() . '_lgi_' . $local_graph_id . '_rrai_' . $rra_id;
} else {
$cache_file = $cache_directory . '/' . get_selected_theme() . '_lgi_' . $local_graph_id . '_rrai_' . $rra_id . '_tsi_' . $timespan;
}
if (isset($graph_data_array['graph_height'])) {
$cache_file .= '_height_' . $graph_data_array['graph_height'];
}
if (isset($graph_data_array['graph_width'])) {
$cache_file .= '_width_' . $graph_data_array['graph_width'];
}
if (isset($graph_data_array['graph_nolegend'])) {
$cache_file .= '_thumb.png';
} else {
$cache_file .= '.png';
}
if (file_exists($cache_file)) {
$mod_time = filemtime($cache_file);
$poller_interval = read_config_option('poller_interval');
if (!isset($poller_interval)) {
$poller_interval = '300';
}
if (($mod_time + $poller_interval) > time()) {
if ($fileptr = fopen($cache_file, 'rb')) {
$output = fread($fileptr, filesize($cache_file));
fclose($fileptr);
/* restore original error handler */
restore_error_handler();
/* get access to the SNMP Cache of BOOST*/
$mc = new MibCache('CACTI-BOOST-MIB');
$mc->object('boostStatsTotalsImagesCacheReads')->count();
$mc->object('boostStatsLastUpdate')->set( time() );
return $output;
} else {
cacti_log("Attempting to open cache file '$cache_file' failed", false, 'BOOST', POLLER_VERBOSITY_DEBUG);
}
} else {
cacti_log("Boost Cache PNG Expired. Image '$cache_file' will be recreated", false, 'BOOST', POLLER_VERBOSITY_DEBUG);
}
}
} else {
cacti_log('ERROR: Boost Cache Directory is not writable! Can not cache images', false, 'BOOST');
}
} else {
cacti_log('ERROR: Boost Cache Directory does not exist! Can not cache images', false, 'BOOST');
}
} else {
cacti_log('ERROR: Boost Cache Directory variable is not set! Can not cache images', false, 'BOOST');
}
}
/* restore original error handler */
restore_error_handler();
return false;
}
function boost_prep_graph_array($graph_data_array) {
/* suppress warnings */
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL ^ E_DEPRECATED);
} else {
error_reporting(E_ALL);
}
/* install the boost error handler */
set_error_handler('boost_error_handler');
if (boost_determine_caching_state()) {
if (!isset($graph_data_array['output_flag'])) {
if (!isset($graph_data_array['print_source'])) {
$graph_data_array['output_flag'] = RRDTOOL_OUTPUT_STDOUT;
}
}
}
/* restore original error handler */
restore_error_handler();
return $graph_data_array;
}
function boost_graph_set_file(&$output, $local_graph_id, $rra_id) {
global $config, $boost_sock, $graph_data_array;
/* get access to the SNMP Cache of BOOST*/
$mc = new MibCache('CACTI-BOOST-MIB');
/* suppress warnings */
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL ^ E_DEPRECATED);
} else {
error_reporting(E_ALL);
}
/* install the boost error handler */
set_error_handler('boost_error_handler');
if (isset($_SESSION['sess_current_timespan'])) {
$timespan = $_SESSION['sess_current_timespan'];
} else {
$timespan = 0;
}
/* check the graph cache and use it if it is valid, otherwise turn over to
* cacti's graphing functions.
*/
if ((read_config_option('boost_png_cache_enable')) && (boost_determine_caching_state())) {
$cache_directory = read_config_option('boost_png_cache_directory');
if ($cache_directory != '') {
if (is_dir($cache_directory)) {
if ($rra_id > 0) {
$cache_file = $cache_directory . '/' . get_selected_theme() . '_lgi_' . $local_graph_id . '_rrai_' . $rra_id;
} else {
$cache_file = $cache_directory . '/' . get_selected_theme() . '_lgi_' . $local_graph_id . '_rrai_' . $rra_id . '_tsi_' . $timespan;
}
if (isset($graph_data_array['graph_height'])) {
$cache_file .= '_height_' . $graph_data_array['graph_height'];
}
if (isset($graph_data_array['graph_width'])) {
$cache_file .= '_width_' . $graph_data_array['graph_width'];
}
if (isset($graph_data_array['graph_nolegend'])) {
$cache_file .= '_thumb.png';
} else {
$cache_file .= '.png';
}
if (is_writable($cache_directory)) {
/* if the cache file was created in a prior step, save it */
if (strlen($output) > 10) {
if ($fileptr = fopen($cache_file, 'w')) {
fwrite($fileptr, $output, strlen($output));
fclose($fileptr);
chmod($cache_file, 0666);
/* count the number of images that had to be cached */
$mc->object('boostStatsTotalsImagesCacheWrites')->count();
$mc->object('boostStatsLastUpdate')->set( time() );
}
}
} else {
cacti_log('ERROR: Boost Cache Directory is not writable! Can not cache images', false, 'BOOST');
}
} else {
cacti_log('ERROR: Boost Cache Directory does not exist! Can not cache images', false, 'BOOST');
}
} else {
cacti_log('ERROR: Boost Cache Directory variable is not set! Can not cache images', false, 'BOOST');
}
}
/* restore original error handler */
restore_error_handler();
}
/* boost_timer - allows you to time events in boost and provide stats
@arg $area - a text string that determines what area is being measured
@arg $type - either 'start' or 'end' to start or end the timing */
function boost_timer($area, $type) {
global $boost_stats_log;
/* get the time */
$btime = microtime(true);
if ($type == BOOST_TIMER_START) {
$boost_stats_log[$area][BOOST_TIMER_START] = $btime;
} elseif ($type == BOOST_TIMER_END) {
if (isset($boost_stats_log[$area][BOOST_TIMER_START])) {
if (!isset($boost_stats_log[$area][BOOST_TIMER_TOTAL])) {
$boost_stats_log[$area][BOOST_TIMER_TOTAL] = 0;
$boost_stats_log[$area][BOOST_TIMER_CYCLES] = 0;
}
$boost_stats_log[$area][BOOST_TIMER_TOTAL] += $btime - $boost_stats_log[$area][BOOST_TIMER_START];
$boost_stats_log[$area][BOOST_TIMER_CYCLES]++;
unset($boost_stats_log[$area][BOOST_TIMER_START]);
}
}
}
function boost_timer_get_overhead() {
global $boost_stats_log;
$start = microtime(true);
$area = 'calibrate';
for ($i = 0; $i < BOOST_TIMER_OVERHEAD_MULTIPLIER; $i++) {
boost_timer($area, BOOST_TIMER_START);
boost_timer($area, BOOST_TIMER_END);
}
unset($boost_stats_log[$area]);
return (microtime(true) - $start);
}
/* boost_get_arch_table_names - returns current archive boost tables or false if no arch table is present currently */
function boost_get_arch_table_names($latest_table = '') {
$tableData = db_fetch_assoc("SHOW tables LIKE 'poller_output_boost_arch%'");
$tableNames = array();
if (cacti_sizeof($tableData)) {
foreach($tableData as $table) {
$table = array_values($table);
$tableNames[$table[0]] = $table[0];
}
}
if (!cacti_sizeof($tableNames)) {
$tableNames = array_rekey(
db_fetch_assoc("SELECT TABLE_NAME AS name
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = SCHEMA()
AND TABLE_NAME LIKE 'poller_output_boost_arch_%'
AND TABLE_ROWS > 0"),
'name', 'name'
);
}
if (!cacti_sizeof($tableNames)) {
if ($latest_table != '' && db_table_exists($latest_table)) {
$tableNames[$latest_table] = $latest_table;
return $tableNames;
} else {
return false;
}
} else {
return $tableNames;
}
}
/**
* boost_process_poller_output - grabs data from the 'poller_output' and 'poller_output_boost*'
* table and feeds to RRDtool for processing. This function has been repurposed for a
* single local_data_id. In the past, it was designed to handle one to many local_data_ids.
*
* The process works as follows:
*
* 1) Gather all the rows for the local_data_id from the archive tables on archive table
* at a time.
* 2) Gather all the rows from the main boost table
* 3) Delete those entries from all the aforementioned tables
* 4) Merge the results together
* 5) Process the entire result set
*
* @param (int) local_data_id - the local data id to update
* @param (resource) rrdtool_pipe - a pointer to the rrdtool process
*
* @return (void)
*/
function boost_process_poller_output($local_data_id, $rrdtool_pipe = '') {
global $config, $database_default, $boost_sock, $boost_timeout, $debug, $get_memory, $memory_used;
static $archive_table = false;
static $warning_issued;
cacti_system_zone_set();
include_once($config['library_path'] . '/rrd.php');
/* suppress warnings */
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL ^ E_DEPRECATED);
} else {
error_reporting(E_ALL);
}
/* install the boost error handler */
set_error_handler('boost_error_handler');
if (cacti_version_compare(get_rrdtool_version(), '1.5', '<')) {
while (!db_fetch_cell("SELECT GET_LOCK('boost.single_ds.$local_data_id', 1)")) {
usleep(50000);
}
}
$data_ids_to_get = read_config_option('boost_rrd_update_max_records_per_select');
$archive_tables = boost_get_arch_table_names($archive_table);
$results = array();
/* avoid getting rows in the middle of poller run */
$timestamp = db_fetch_cell('SELECT MIN(UNIX_TIMESTAMP(start_time))
FROM poller_time
WHERE end_time="0000-00-00"');
if (empty($timestamp)) {
$timestamp = time() - 10;
}
$query_string = '';
$sql_params = array();
$locks = false;
$temp_table = false;
if (cacti_count($archive_tables)) {
$temp_table = 'poller_output_boost_temp_' . $local_data_id . '_' . mt_rand();
db_execute("CREATE TEMPORARY TABLE $temp_table LIKE poller_output_boost");
foreach($archive_tables as $index => $table) {
db_execute_prepared("INSERT INTO $temp_table
SELECT *
FROM $table
WHERE local_data_id = ?",
array($local_data_id), false);
}
}
if ($temp_table !== false) {
db_execute_prepared("INSERT INTO $temp_table
SELECT *
FROM poller_output_boost
WHERE local_data_id = ?
AND time < FROM_UNIXTIME(?)",
array($local_data_id, $timestamp), false);
$query_string = "SELECT po.local_data_id, dl.data_template_id,
UNIX_TIMESTAMP(po.time) AS timestamp, po.rrd_name, po.output
FROM $temp_table AS po
INNER JOIN data_local AS dl
ON po.local_data_id = dl.id
WHERE po.local_data_id = ?
AND po.time < FROM_UNIXTIME(?)
ORDER BY time ASC, rrd_name ASC";
} else {
$query_string = "SELECT po.local_data_id, dl.data_template_id,
UNIX_TIMESTAMP(po.time) AS timestamp, po.rrd_name, po.output
FROM poller_output_boost AS po
INNER JOIN data_local AS dl
ON po.local_data_id = dl.id
WHERE po.local_data_id = ?
AND po.time < FROM_UNIXTIME(?)
ORDER BY time ASC, rrd_name ASC";
}
$sql_params[] = $local_data_id;
$sql_params[] = $timestamp;
boost_timer('get_records', BOOST_TIMER_START);
$results = db_fetch_assoc_prepared($query_string, $sql_params);
boost_timer('get_records', BOOST_TIMER_END);
$boost_results = cacti_sizeof($results);
if ($temp_table !== false) {
db_execute("DROP TEMPORARY TABLE $temp_table");
}
cacti_log('Local Data ID: ' . $local_data_id . ', Boost Results: ' . $boost_results, false, 'BOOST', POLLER_VERBOSITY_MEDIUM);
/* remove the entries from the table */
boost_timer('delete', BOOST_TIMER_START);
if (cacti_count($archive_tables)) {
foreach($archive_tables as $table) {
db_execute_prepared("DELETE IGNORE
FROM $table
WHERE local_data_id = ?",
array($local_data_id), false);
}
}
if (cacti_sizeof($results)) {
db_execute_prepared('DELETE FROM poller_output_boost
WHERE local_data_id = ?
AND time < FROM_UNIXTIME(?)',
array($local_data_id, $timestamp), false);
}
boost_timer('delete', BOOST_TIMER_END);
if (cacti_version_compare(get_rrdtool_version(), '1.5', '<')) {
db_execute("SELECT RELEASE_LOCK('boost.single_ds.$local_data_id')");
}
/* log memory */
if ($get_memory) {
$cur_memory = memory_get_usage();
if ($cur_memory > $memory_used) {
$memory_used = $cur_memory;
}
}
if (cacti_sizeof($results)) {
$rrdp_auto_close = false;
if (!$rrdtool_pipe) {
$rrdtool_pipe = rrd_init();
$rrdp_auto_close = true;
}
/* create an array keyed off of each .rrd file */
$time = -1;
$outbuf = '';
$last_update = -1;
$multi_vals_set = false;
$last_item = array(
'local_data_id' => -1,
'timestamp' => -1,
'rrd_name' => ''
);
/* we are going to blow away all record if ok */
$vals_in_buffer = 0;
$upd_string_len = read_config_option('boost_rrd_update_string_length');
/* initialize some variables */
$rrd_tmpl = '';
$rrd_path = '';
$outlen = 0;
$path_template = boost_get_rrd_filename_and_template($local_data_id);
if (cacti_sizeof($path_template)) {
$rrd_path = $path_template['rrd_path'];
$rrd_tmpl = $path_template['rrd_template'];
} else {
$rrd_path = db_fetch_cell_prepared('SELECT rrd_path
FROM poller_item
WHERE local_data_id = ?',
array($local_data_id));
}
cacti_log('The RRDpath is ' . $rrd_path, false, 'BOOST', POLLER_VERBOSITY_MEDIUM);
cacti_log('The RRDpath template is ' . $rrd_tmpl, false, 'BOOST', POLLER_VERBOSITY_MEDIUM);
$unused_data_source_names = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT dtr.data_source_name, dtr.data_source_name
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON dtr.id = gti.task_item_id
WHERE dtr.local_data_id = ?
AND gti.task_item_id IS NULL',
array($local_data_id)),
'data_source_name', 'data_source_name'
);
boost_timer('results_cycle', BOOST_TIMER_START);
/* go through each poller_output_boost entries and process */
foreach ($results as $item) {
if (cacti_sizeof($unused_data_source_names) && isset($unused_data_source_names[$item['rrd_name']])) {
continue;
}
/**
* detect duplicate records, this should not happen,
* but adding just in case.
*/
if ($last_item['timestamp'] == $item['timestamp'] && $last_item['rrd_name'] == $item['rrd_name']) {
cacti_log(sprintf('WARNING: Skipping %s:%s due to duplicate record...', $item['local_data_id'], $item['rrd_name']), false, 'BOOST');
continue;
}
/* don't generate error messages if the RRD has already been updated */
if ($time < $last_update && cacti_version_compare(get_rrdtool_version(), '1.5', '<')) {
cacti_log("WARNING: Stale Poller Data Found! Item Time:'" . $time . "', RRD Time:'" . $last_update . "' Ignoring Value!", false, 'BOOST', POLLER_VERBOSITY_HIGH);
$value = 'DNP';
} else {
$value = trim($item['output']);
}
if ($time != $item['timestamp']) {
if ($outlen > $upd_string_len) {
boost_timer('rrdupdate', BOOST_TIMER_START);
$return_value = boost_rrdtool_function_update($local_data_id, $rrd_path, $rrd_tmpl, $outbuf, $rrdtool_pipe);
boost_timer('rrdupdate', BOOST_TIMER_END);
$outbuf = '';
$outlen = 0;
$vals_in_buffer = 0;
/* check return status for delete operation */
if (strpos(trim($return_value), 'OK') === false && $return_value != '') {
cacti_log("WARNING: RRD Update Warning '" . $return_value . "' for Local Data ID '$local_data_id'", false, 'BOOST');
}
}
if (strpos($value, 'DNP') === false) {
$output = ' ' . $item['timestamp'];
$outbuf .= $output;
$outlen += strlen($output);
}
$time = $item['timestamp'];
}
/* single one value output */
if (strpos($value, 'DNP') !== false) {
/* continue, bad time */
} elseif ((is_numeric($value)) || ($value == 'U' && $item['rrd_name'] != '')) {
$output = ':' . $value;
$outbuf .= $output;
$outlen += strlen($output);
$vals_in_buffer++;
} elseif ((function_exists('is_hexadecimal')) && (is_hexadecimal($value))) {
$output = ':' . hexdec($value);
$outbuf .= $output;
$outlen += strlen($output);
$vals_in_buffer++;
} elseif (strpos($value, ':') !== false) {
$values = preg_split('/\s+/', $value);
if (!$multi_vals_set) {
if ($item['data_template_id'] > 0) {
$rrd_field_names = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT dtr.data_source_name, dif.data_name
FROM graph_templates_item AS gti
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id = dtr.id
INNER JOIN data_input_fields AS dif
ON dtr.data_input_field_id = dif.id
WHERE dtr.local_data_id = ?',
array($item['local_data_id'])),
'data_name', 'data_source_name'
);
$unused_data_source_names = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT dtr.data_source_name, dtr.data_source_name
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON dtr.id = gti.task_item_id
WHERE dtr.local_data_id = ?
AND gti.task_item_id IS NULL',
array($item['local_data_id'])),
'data_source_name', 'data_source_name'
);
} else {
$rrd_field_names = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT dtr.data_source_name, dif.data_name
FROM data_template_rrd AS dtr
INNER JOIN data_input_fields AS dif
ON dtr.data_input_field_id = dif.id
WHERE dtr.local_data_id = ?',
array($item['local_data_id'])),
'data_name', 'data_source_name'
);
$unused_data_source_names = array();
}
$rrd_tmpl = '';
}
$first_tmpl = true;
$multi_ok = false;
if (cacti_sizeof($values)) {
foreach($values as $value) {
$matches = explode(':', $value);
if (isset($rrd_field_names[$matches[0]])) {
$field = $rrd_field_names[$matches[0]];
if (cacti_sizeof($unused_data_source_names) && isset($unused_data_source_names[$field])) {
continue;
}
$multi_ok = true;
if (!$multi_vals_set) {
if (!$first_tmpl) {
$rrd_tmpl .= ':';
}
$rrd_tmpl .= $rrd_field_names[$matches[0]];
$first_tmpl = false;
}