forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphs_new.php
1016 lines (854 loc) · 35.5 KB
/
graphs_new.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
<?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/ |
+-------------------------------------------------------------------------+
*/
include('./include/auth.php');
include_once('./lib/api_data_source.php');
include_once('./lib/api_graph.php');
include_once('./lib/api_tree.php');
include_once('./lib/data_query.php');
include_once('./lib/html_form_template.php');
include_once('./lib/html_graph.php');
include_once('./lib/sort.php');
include_once('./lib/snmp.php');
include_once('./lib/poller.php');
include_once('./lib/template.php');
include_once('./lib/utility.php');
/* set default action */
set_default_action();
switch (get_request_var('action')) {
case 'save':
form_save();
break;
case 'query_reload':
host_reload_query();
header('Location: graphs_new.php?host_id=' . get_request_var('host_id') . '&header=false');
break;
case 'ajax_hosts':
get_allowed_ajax_hosts(false, false);
break;
case 'ajax_hosts_noany':
get_allowed_ajax_hosts(false);
break;
case 'ajax_save':
save_default_query_option();
break;
case 'ajax_save_filter':
save_user_filter();
break;
default:
top_header();
graphs();
bottom_footer();
break;
}
/* --------------------------
The Save Function
-------------------------- */
function save_default_query_option() {
$data_query = get_filter_request_var('query');
$default = get_filter_request_var('item');
set_user_setting('default_sgg_' . $data_query, $default);
print __('Default Settings Saved') . "\n";
}
function save_user_filter() {
$rows = get_filter_request_var('rows');
if ($rows == -1) {
$rows = read_config_option('num_rows_table');
}
$graph_type = get_filter_request_var('graph_type');
set_user_setting('num_rows_table', $rows);
set_user_setting('graph_type', $graph_type);
}
function store_get_selected_dq_index($snmp_query_id) {
// Always restore the last used filter, otherwise, use the default
if (!is_numeric($snmp_query_id)) {
return false;
} elseif (isset_request_var('sgg_' . $snmp_query_id)) {
$selected = get_filter_request_var('sgg_' . $snmp_query_id);
} elseif (isset($_SESSION['sess_sgg_' . $snmp_query_id])) {
$selected = $_SESSION['sess_sgg_' . $snmp_query_id];
} else {
$selected = read_user_setting('default_sgg_' . $snmp_query_id);
}
$_SESSION['sess_sgg_' . $snmp_query_id] = $selected;
return $selected;
}
function form_save() {
if (isset_request_var('save_component_graph')) {
$form_data = array();
/* summarize the 'create graph from host template/snmp index' stuff into an array */
foreach ($_POST as $var => $val) {
/* save form data */
$form_data[$var] = $val;
if (preg_match('/^cg_(\d+)$/', $var, $matches)) {
$selected_graphs['cg'][$matches[1]][$matches[1]] = true;
} elseif (preg_match('/^cg_g$/', $var)) {
if (get_nfilter_request_var('cg_g') > 0) {
$selected_graphs['cg'][get_nfilter_request_var('cg_g')][get_nfilter_request_var('cg_g')] = true;
}
} elseif (preg_match('/^sg_(\d+)_([a-f0-9]{32})$/', $var, $matches)) {
$selected_graphs['sg'][$matches[1]][get_nfilter_request_var('sgg_' . $matches[1])][$matches[2]] = true;
}
if (strpos($var, 'sgg_') !== false) {
$snmp_query_id = str_replace('sgg_', '', $var);
input_validate_input_number($snmp_query_id);
store_get_selected_dq_index($snmp_query_id);
}
}
/* save the json_encoded form data in case of an error */
$form_data['header'] = false;
$_SESSION['sess_graphs_new_form'] = json_encode($form_data);
$_SESSION['sess_grn_returnto'] = 'graphs_new.php';
if (!isset_request_var('host_id')) {
$host_id = 0;
} else {
$host_id = get_filter_request_var('host_id');
}
if (!isset_request_var('host_template_id')) {
$host_template_id = 0;
} else {
$host_template_id = get_filter_request_var('host_template_id');
}
if (isset($selected_graphs)) {
html_graph_new_graphs('graphs_new.php', $host_id, $host_template_id, $selected_graphs);
exit;
}
header('Location: graphs_new.php?host_id=' . $host_id . '&header=false');
}
if (isset_request_var('save_component_new_graphs')) {
host_new_graphs_save(get_filter_request_var('host_id'));
header('Location: graphs_new.php?host_id=' . get_filter_request_var('host_id') . '&header=false');
}
}
/* -------------------
Data Query Functions
------------------- */
function host_reload_query() {
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('host_id');
/* ==================================================== */
run_data_query(get_request_var('host_id'), get_request_var('id'));
}
/* -------------------
New Graph Functions
------------------- */
function host_new_graphs_save($host_id) {
$selected_graphs_array = cacti_unserialize(stripslashes(get_nfilter_request_var('selected_graphs_array')));
$values = array();
$form_data = array();
/* form an array that contains all of the data on the previous form */
foreach ($_POST as $var => $val) {
if (preg_match('/^g_(\d+)_(\d+)_(\w+)/', $var, $matches)) { // 1: snmp_query_id, 2: graph_template_id, 3: field_name
if (empty($matches[1])) { // this is a new graph from template field
$values['cg'][$matches[2]]['graph_template'][$matches[3]] = $val;
} else { // this is a data query field
$values['sg'][$matches[1]][$matches[2]]['graph_template'][$matches[3]] = $val;
}
} elseif (preg_match('/^gi_(\d+)_(\d+)_(\d+)_(\w+)/', $var, $matches)) { // 1: snmp_query_id, 2: graph_template_id, 3: graph_template_input_id, 4:field_name
/* ================= input validation ================= */
input_validate_input_number($matches[3]);
/* ==================================================== */
/* we need to find out which graph items will be affected by saving this particular item */
$item_list = db_fetch_assoc_prepared('SELECT
graph_template_item_id
FROM graph_template_input_defs
WHERE graph_template_input_id = ?',
array($matches[3]));
/* loop through each item affected and update column data */
if (cacti_sizeof($item_list)) {
foreach ($item_list as $item) {
if (empty($matches[1])) { // this is a new graph from template field
$values['cg'][$matches[2]]['graph_template_item'][$item['graph_template_item_id']][$matches[4]] = $val;
} else { // this is a data query field
$values['sg'][$matches[1]][$matches[2]]['graph_template_item'][$item['graph_template_item_id']][$matches[4]] = $val;
}
}
}
} elseif (preg_match('/^d_(\d+)_(\d+)_(\d+)_(\w+)/', $var, $matches)) { // 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:field_nam
if (empty($matches[1])) { // this is a new graph from template field
$values['cg'][$matches[2]]['data_template'][$matches[3]][$matches[4]] = $val;
} else { // this is a data query field
$values['sg'][$matches[1]][$matches[2]]['data_template'][$matches[3]][$matches[4]] = $val;
}
} elseif (preg_match('/^c_(\d+)_(\d+)_(\d+)_(\d+)/', $var, $matches)) {
/**
* Custom Data. Need to validate these against the input regular expressions
*
* Index offsets
* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:data_input_field_id
*/
$input_field_id = $matches[4];
$idata = db_fetch_row_prepared('SELECT *
FROM data_input_fields
WHERE id = ?',
array($input_field_id));
$val = form_input_validate($val, $var, $idata['regexp_match'], $idata['allow_nulls'], 3);
if (empty($matches[1])) { // this is a new graph from template field
$values['cg'][$matches[2]]['custom_data'][$matches[3]][$matches[4]] = $val;
} else { // this is a data query field
$values['sg'][$matches[1]][$matches[2]]['custom_data'][$matches[3]][$matches[4]] = $val;
}
} elseif (preg_match('/^di_(\d+)_(\d+)_(\d+)_(\d+)_(\w+)/', $var, $matches)) { // 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:local_data_template_rrd_id, 5:field_name
if (empty($matches[1])) { // this is a new graph from template field
$values['cg'][$matches[2]]['data_template_item'][$matches[4]][$matches[5]] = $val;
} else { // this is a data query field
$values['sg'][$matches[1]][$matches[2]]['data_template_item'][$matches[4]][$matches[5]] = $val;
}
}
}
if (!is_error_message()) {
debug_log_clear('new_graphs');
foreach ($selected_graphs_array as $form_type => $form_array) {
$current_form_type = $form_type;
foreach ($form_array as $form_id1 => $form_array2) {
/* enumerate information from the arrays stored in post variables */
create_save_graph($host_id, $form_type, $form_id1, $form_array2, $values);
}
}
} else {
$form_data = $_SESSION['sess_graphs_new_form'];
kill_session_var('sess_graphs_new_form');
?>
<script type='text/javascript'>
var formData=<?php print $form_data;?>;
$(function() {
loadPageUsingPost('graphs_new.php', formData);
});
</script>
<?php
exit;
}
}
/* -------------------
Graph Functions
------------------- */
function graphs() {
global $config, $item_rows;
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'filter' => array(
'filter' => FILTER_DEFAULT,
'pageset' => true,
'default' => ''
),
'host_id' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => db_fetch_cell('SELECT id FROM host ORDER BY description, hostname LIMIT 1')
),
'graph_type' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => read_user_setting('graph_type', read_config_option('default_graphs_new_dropdown'), true)
)
);
validate_store_request_vars($filters, 'sess_grn');
/* ================= input validation ================= */
if (get_request_var('rows') == '-1') {
$rows = read_user_setting('num_rows_table', read_config_option('num_rows_table'), true);
} else {
$rows = get_request_var('rows');
}
if (!isempty_request_var('host_id')) {
$host = db_fetch_row_prepared('SELECT id, description, hostname, host_template_id
FROM host
WHERE id = ?',
array(get_request_var('host_id')));
if (cacti_sizeof($host)) {
$name = db_fetch_cell_prepared('SELECT name
FROM host_template
WHERE id = ?',
array($host['host_template_id']));
$header = __esc('New Graphs for [ %s ] (%s %s)', $host['description'], $host['hostname'], (!empty($host['host_template_id']) ? $name:''));
} else {
$header = __('New Graphs for [ All Devices ]');
$host['id'] = -1;
$host['host_template_id'] = 0;
}
} else {
$host['id'] = 0;
$host['host_template_id'] = 0;
$header = __('New Graphs for None Host Type');
}
html_start_box($header, '100%', '', '3', 'center', '');
print '<tr><td class="even">';
?>
<script type='text/javascript'>
function applyFilter() {
strURL = '?graph_type=' + $('#graph_type').val();
strURL += '&host_id=' + $('#host_id').val();
strURL += '&filter=' + $('#filter').val();
strURL += '&rows=' + $('#rows').val();
strURL += '&header=false';
loadPageNoHeader(strURL);
}
function clearFilter() {
loadPageNoHeader('graphs_new.php?clear=true&header=false');
}
function saveFilter() {
strURL = 'graphs_new.php?action=ajax_save_filter' +
'&rows=' + $('#rows').val() +
'&graph_type=' + $('#graph_type').val();
$.get(strURL)
.done(function(data) {
$('#text').show().text('<?php print __('Filter Settings Saved');?>').fadeOut(2000);
})
.fail(function(data) {
getPresentHTTPError(data);
});
}
$(function() {
$('[id^="reload"]').click(function(data) {
$(this).addClass('fa-spin');
loadPageNoHeader('graphs_new.php?action=query_reload&header=false&id='+$(this).attr('data-id')+'&host_id='+$('#host_id').val());
});
$('#clear').click(function() {
clearFilter();
});
$('#save').click(function() {
saveFilter();
});
$('#graphs_new').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
</script>
<form id='graphs_new' action='graphs_new.php'>
<table class='cactiTable'>
<tr><td style='width:70%;'>
<table class='filterTable'>
<tr>
<?php print html_host_filter(get_request_var('host_id'), 'applyFilter', '', true, true);?>
<td>
<?php print __('Graph Types');?>
</td>
<td>
<select id='graph_type' name='graph_type' onChange='applyFilter()'>
<option value='-2'<?php if (get_request_var('graph_type') == '-2') {?> selected<?php }?>><?php print __('All');?></option>
<option value='-1'<?php if (get_request_var('graph_type') == '-1') {?> selected<?php }?>><?php print __('Graph Template Based');?></option>
<?php
$snmp_queries = db_fetch_assoc_prepared('SELECT sq.id, sq.name
FROM snmp_query AS sq
INNER JOIN host_snmp_query AS hsq
ON hsq.snmp_query_id = sq.id
WHERE hsq.host_id = ?
ORDER BY sq.name',
array($host['id']));
if (cacti_sizeof($snmp_queries) > 0) {
foreach ($snmp_queries as $query) {
print "<option value='" . $query['id'] . "'"; if (get_request_var('graph_type') == $query['id']) { print ' selected'; } print '>' . html_escape($query['name']) . '</option>';
}
}
?>
</select>
</td>
<td>
<span>
<input type='submit' class='ui-button ui-corner-all ui-widget' id='refresh' value='<?php print __esc('Go');?>' title='<?php print __esc('Set/Refresh Filters');?>'>
<input type='button' class='ui-button ui-corner-all ui-widget' id='clear' value='<?php print __esc('Clear');?>' title='<?php print __esc('Clear Filters');?>'>
<input type='button' class='ui-button ui-corner-all ui-widget' id='save' value='<?php print __esc('Save');?>' title='<?php print __esc('Save Filters');?>'>
</span>
</td>
<td id='text'></td>
</tr>
</table>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search');?>
</td>
<td>
<input type='text' class='ui-state-default ui-corner-all' id='filter' name='filter' size='25' value='<?php print html_escape_request_var('filter');?>'>
</td>
<td>
<?php print __('Rows');?>
</td>
<td>
<select id='rows' name='rows' onChange='applyFilter()'>
<option value='-1'<?php print (get_request_var('rows') == '-1' ? ' selected>':'>') . __('Default');?></option>
<?php
if (cacti_sizeof($item_rows) > 0) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'"; if (get_request_var('rows') == $key) { print ' selected'; } print '>' . html_escape($value) . '</option>';
}
}
?>
</select>
</td>
</tr>
</table>
</td>
<td class='textInfo right'>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('host.php?action=edit&id=' . get_request_var('host_id'));?>'><?php print __('Edit this Device');?></a><br>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('host.php?action=edit');?>'><?php print __('Create New Device');?></a><br>
<?php api_plugin_hook('graphs_new_top_links'); ?>
</td>
</tr>
</table>
</form>
</td>
</tr>
<?php
html_end_box();
form_start('graphs_new.php', 'chk');
$total_rows = cacti_sizeof(db_fetch_assoc_prepared('SELECT graph_template_id
FROM host_graph
WHERE host_id = ?',
array(get_request_var('host_id'))));
$i = 0;
if (get_request_var('changed')) {
foreach($snmp_queries as $query) {
kill_session_var('sess_grn_page' . $query['id']);
unset_request_var('page' . $query['id']);
load_current_session_value('page' . $query['id'], 'sess_grn_page' . $query['id'], '1');
}
}
if (get_request_var('graph_type') > 0) {
/* validate the page filter */
if (isset_request_var('page' . get_request_var('graph_type'))) {
get_filter_request_var('page' . get_request_var('graph_type'));
}
load_current_session_value('page' . get_request_var('graph_type'), 'sess_grn_page' . get_request_var('graph_type'), '1');
} else if (get_request_var('graph_type') == -2) {
foreach($snmp_queries as $query) {
/* validate the page filter */
if (isset_request_var('page' . $query['id'])) {
get_filter_request_var('page' . $query['id']);
}
load_current_session_value('page' . $query['id'], 'sess_grn_page' . $query['id'], '1');
}
}
$script = "<script type='text/javascript'>\nvar created_graphs = new Array();\n";
if (get_request_var('graph_type') < 0) {
html_start_box(__('New Graph Template'), '100%', '', '3', 'center', '');
$available_graph_templates = db_fetch_assoc_prepared('SELECT gt.id, gt.name
FROM graph_templates AS gt
LEFT JOIN snmp_query_graph AS sqg
ON sqg.graph_template_id = gt.id
WHERE sqg.name IS NULL
AND gt.id NOT IN (SELECT graph_template_id FROM host_graph WHERE host_id = ?)
AND gt.multiple = ""
UNION
SELECT id, name
FROM graph_templates AS gt
WHERE multiple = "on"
ORDER BY name',
array(get_request_var('host_id'))
);
/* create a row at the bottom that lets the user create any graph they choose */
print "<tr class='even'>
<td class='left' style='width:1%'>";
form_dropdown('cg_g', $available_graph_templates, 'name', 'id', '', __('(Select a graph type to create)'), '', 'textArea');
print '</td>
<td class="left">
<input type="submit" class="create ui-button ui-corner-all ui-widget ui-state-active" id="submit" value="' . __('Create') . '" role="button">
</td>
</tr>';
html_end_box();
html_start_box(__('Graph Templates'), '100%', '', '3', 'center', '');
print "<tr class='tableHeader'>
<th class='tableSubHeaderColumn'>" . __('Graph Template Name') . "</th>
<th class='tableSubHeaderCheckbox'><input class='checkbox' type='checkbox' id='all_cg' title='" . __esc('Select All') . "' onClick='selectAll(\"sg\",this.checked)'><label class='formCheckboxLabel' title='" . __esc('Select All Rows'). "' for='all_cg'></label></th>
</tr>";
if (get_request_var('filter') != '') {
$sql_where = 'AND gt.name LIKE ' . db_qstr('%' . get_request_var('filter') . '%');
} else {
$sql_where = '';
}
if (!isempty_request_var('host_id')) {
$template_graphs = db_fetch_assoc_prepared('SELECT
DISTINCT gl.graph_template_id
FROM graph_local AS gl
INNER JOIN graph_templates AS gt
ON gt.id=gl.graph_template_id
WHERE gl.host_id = ?
AND gt.multiple = ""
AND gl.snmp_query_id = 0
GROUP BY gl.graph_template_id',
array($host['id']));
if (cacti_sizeof($template_graphs)) {
$script .= 'var gt_created_graphs = new Array(';
$cg_ctr = 0;
foreach ($template_graphs as $template_graph) {
$script .= (($cg_ctr > 0) ? ',' : '') . "'" . $template_graph['graph_template_id'] . "'";
$cg_ctr++;
}
$script .= ");\n";
} else {
$script .= 'var gt_created_graphs = new Array();';
}
}
$graph_templates = db_fetch_assoc_prepared("SELECT
gt.id AS graph_template_id,
gt.name AS graph_template_name
FROM host_graph AS hg
INNER JOIN graph_templates AS gt
ON hg.graph_template_id=gt.id
WHERE hg.host_id = ?
$sql_where
ORDER BY gt.name",
array(get_request_var('host_id'))
);
/* create a row for each graph template associated with the host template */
if (cacti_sizeof($graph_templates)) {
foreach ($graph_templates as $graph_template) {
$query_row = $graph_template['graph_template_id'];
print "<tr id='gt_line$query_row' style='display:table-row' class='selectable " . (($i % 2 == 0) ? 'odd' : 'even') . "'>"; $i++;
print "<td>
<span id='gt_text$query_row" . "_0'>" . filter_value($graph_template['graph_template_name'], get_request_var('filter')) . "</span>
</td>
<td class='checkbox' style='width:1%;'>
<input class='checkbox' type='checkbox' name='cg_$query_row' id='cg_$query_row'>
<label class='formCheckboxLabel' for='cg_$query_row'></label>
</td>
</tr>";
}
}
html_end_box();
}
if (get_request_var('graph_type') != -1 && !isempty_request_var('host_id')) {
$params = array();
$params[] = $host['id'];
if (get_request_var('graph_type') != -2) {
$params[] = get_request_var('graph_type');
$sql = ' AND sq.id = ?';
} else {
$sql = '';
}
$snmp_queries = db_fetch_assoc_prepared("SELECT sq.id, sq.name
FROM snmp_query AS sq
INNER JOIN host_snmp_query AS hsq
ON hsq.snmp_query_id = sq.id
WHERE hsq.host_id = ?
$sql
ORDER BY sq.name",
$params);
if (cacti_sizeof($snmp_queries)) {
foreach ($snmp_queries as $snmp_query) {
unset($total_rows);
if (!get_request_var('changed')) {
$page = get_filter_request_var('page' . $snmp_query['id']);
} else {
$page = 1;
}
$xml_array = get_data_query_array($snmp_query['id']);
$num_input_fields = 0;
$num_visible_fields = 0;
$message_raised = false;
if (cacti_sizeof($xml_array)) {
/* loop through once so we can find out how many input fields there are */
if (isset($xml_array['fields'])) {
foreach ($xml_array['fields'] as $field_name => $field_array) {
if (!is_array($field_array)) {
if (!$message_raised) {
raise_message('xmlerror', __('Error Parsing Data Query Resource XML file for Data Query \'%s\' with id of \'%s\'', $snmp_query['id']), MESSAGE_LEVEL_ERROR);
$message_raised = true;
}
} elseif (isset($field_array['direction'])) {
if ($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output') {
$num_input_fields++;
if (!isset($total_rows)) {
$total_rows = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?',
array($host['id'], $snmp_query['id'], $field_name));
}
}
} else {
raise_message('xmlfielderr' . $field_name, __('Error Parsing Data Query Resource XML file for Data Query \'%s\' with id \'%s\'. Field Name \'%s\' missing a \'direction\' attribute', $snmp_query['name'], $snmp_query['id'], $field_name), MESSAGE_LEVEL_ERROR);
}
}
} elseif (!$message_raised) {
raise_message('xmlerror', __('Error Parsing Data Query Resource XML file for Data Query \'%s\' with id \'%s\'', $snmp_query['name'], $snmp_query['id']), MESSAGE_LEVEL_ERROR);
$message_raised = true;
}
}
if (!isset($total_rows)) {
$total_rows = 0;
}
$snmp_query_graphs = db_fetch_assoc_prepared('SELECT id, name
FROM snmp_query_graph
WHERE snmp_query_id = ?
ORDER BY name',
array($snmp_query['id']));
if (cacti_sizeof($snmp_query_graphs)) {
foreach ($snmp_query_graphs as $snmp_query_graph) {
$created_graphs = db_fetch_assoc_prepared('SELECT DISTINCT snmp_index
FROM graph_local
WHERE snmp_query_graph_id = ?
AND host_id = ?',
array($snmp_query_graph['id'], $host['id']));
$script .= 'created_graphs[' . $snmp_query_graph['id'] . '] = new Array(';
$cg_ctr = 0;
if (cacti_sizeof($created_graphs)) {
foreach ($created_graphs as $created_graph) {
$script .= ($cg_ctr > 0 ? ',' : '') . "'" . encode_data_query_index($created_graph['snmp_index']) . "'";
++$cg_ctr;
}
}
$script .= ")\n";
}
}
print "<div class='cactiTable'>
<div>
<div class='cactiTableTitle'>
<span>" . __esc('Data Query [%s]', $snmp_query['name']) . "</span>
</div>
<div class='cactiTableButton'>
<span class='reloadquery fa fa-sync' id='reload" . $snmp_query['id'] . "' data-id='" . $snmp_query['id'] . "'></span>
</div>
</div>
</div>";
if (cacti_sizeof($xml_array)) {
$html_dq_header = '';
/* if there is a where clause, get the matching snmp_indexes */
$sql_where = '';
if (get_request_var('filter') != '') {
$sql_where = '';
$indexes = db_fetch_assoc_prepared('SELECT DISTINCT snmp_index
FROM host_snmp_cache
WHERE field_value LIKE ?
AND snmp_query_id = ?
AND host_id = ?',
array('%' . get_request_var('filter') . '%', $snmp_query['id'], $host['id']));
if (cacti_sizeof($indexes)) {
foreach($indexes as $index) {
if ($sql_where != '') {
$sql_where .= ", '" . $index['snmp_index'] . "'";
} else {
$sql_where .= " AND snmp_index IN('" . $index['snmp_index'] . "'";
}
}
$sql_where .= ')';
}
}
if (get_request_var('filter') == '' || (get_request_var('filter') != '' && cacti_sizeof($indexes))) {
/* determine the sort order */
if (isset($xml_array['index_order_type'])) {
if ($xml_array['index_order_type'] == 'numeric') {
$sql_order = 'ORDER BY CAST(snmp_index AS unsigned)';
} else if ($xml_array['index_order_type'] == 'alphabetic') {
$sql_order = 'ORDER BY snmp_index';
} else if ($xml_array['index_order_type'] == 'natural') {
$sql_order = 'ORDER BY INET_ATON(snmp_index)';
} else {
$sql_order = '';
}
} else {
$sql_order = '';
}
/* get the unique field values from the database */
$field_names = db_fetch_assoc_prepared('SELECT DISTINCT field_name
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?',
array($host['id'], $snmp_query['id']));
/* build magic query */
$sql_query = 'SELECT host_id, snmp_query_id, snmp_index';
$num_visible_fields = cacti_sizeof($field_names);
$i = 0;
if (cacti_sizeof($field_names)) {
foreach($field_names as $column) {
$field_name = $column['field_name'];
$sql_query .= ", MAX(CASE WHEN field_name='$field_name' THEN field_value ELSE NULL END) AS '$field_name'";
$i++;
}
}
$sql_query_worder = $sql_query . ' FROM host_snmp_cache
WHERE host_id=' . $host['id'] . '
AND snmp_query_id=' . $snmp_query['id'] . "
$sql_where
GROUP BY host_id, snmp_query_id, snmp_index
$sql_order
LIMIT " . ($rows*($page-1)) . ',' . $rows;
$sql_query .= ' FROM host_snmp_cache
WHERE host_id=' . $host['id'] . '
AND snmp_query_id=' . $snmp_query['id'] . "
$sql_where
GROUP BY host_id, snmp_query_id, snmp_index
LIMIT " . ($rows*($page-1)) . ',' . $rows;
$rows_query = 'SELECT host_id, snmp_query_id, snmp_index
FROM host_snmp_cache
WHERE host_id=' . $host['id'] . '
AND snmp_query_id=' . $snmp_query['id'] . "
$sql_where
GROUP BY host_id, snmp_query_id, snmp_index";
$snmp_query_indexes = db_fetch_assoc($sql_query);
if (cacti_sizeof($snmp_query_indexes)) {
$snmp_query_indexes = db_fetch_assoc($sql_query_worder);
}
$total_rows = cacti_sizeof(db_fetch_assoc($rows_query));
if (($page - 1) * $rows > $total_rows) {
$page = 1;
set_request_var('page' . $query['id'], $page);
load_current_session_value('page' . $query['id'], 'sess_grn_page' . $query['id'], '1');
}
$nav = html_nav_bar('graphs_new.php', MAX_DISPLAY_PAGES, $page, $rows, $total_rows, 15, __('Items'), 'page' . $snmp_query['id']);
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
foreach ($xml_array['fields'] as $field_name => $field_array) {
if (($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output') && cacti_sizeof($field_names)) {
foreach($field_names as $row) {
if ($row['field_name'] == $field_name) {
$html_dq_header .= "<th class='tableSubHeaderColumn'>" . $field_array['name'] . '</th>';
break;
}
}
}
}
if (!cacti_sizeof($snmp_query_indexes)) {
print "<tr class='odd'><td>" . __('This Data Query returned 0 rows, perhaps there was a problem executing this Data Query.') . "<a href='" . html_escape('host.php?action=query_verbose&id=' . $snmp_query['id'] . '&host_id=' . $host['id']) . "'>" . __('You can run this Data Query in debug mode') . "</a> " . __('From there you can get more information.') . '</td></tr>';
} else {
print "<tr class='tableHeader'>
$html_dq_header
<th class='tableSubHeaderCheckbox'><input class='checkbox' id='all_" . $snmp_query['id'] . "' type='checkbox' name='all_" . $snmp_query['id'] . "' title='" . __esc('Select All') . "' onClick='selectAll(\"sg_" . $snmp_query['id'] . "\",this.checked)'><label class='formCheckboxLabel' title='" . __esc('Select All Rows'). "' for='all_" . $snmp_query['id'] . "'></label></th>
</tr>";
}
/* disable graph creation if there are no associated Graph Templates */
$enabled = db_fetch_cell_prepared('SELECT COUNT(*)
FROM snmp_query_graph
WHERE snmp_query_id = ?',
array($snmp_query['id']));
if (!$enabled) {
$disabled_text = __esc('The index is disabled due to the Data Query having no associated Graph Templates.');
} else {
$disabled_text = '';
}
$row_counter = 0;
$column_counter = 0;
$fields = array_rekey($field_names, 'field_name', 'field_name');
if (cacti_sizeof($snmp_query_indexes)) {
foreach($snmp_query_indexes as $row) {
$query_row = $snmp_query['id'] . '_' . encode_data_query_index($row['snmp_index']);
if ($enabled) {
print "<tr id='dqline$query_row' class='selectable " . (($row_counter % 2 == 0) ? 'odd' : 'even') . "'>"; $i++;
} else {
print "<tr id='nodqline$query_row' title='$disabled_text' class='selectable notemplate " . (($row_counter % 2 == 0) ? 'odd' : 'even') . "'>"; $i++;
}
$column_counter = 0;
foreach ($xml_array['fields'] as $field_name => $field_array) {
if ($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output') {
if (in_array($field_name, $fields)) {
if (isset($row[$field_name])) {
print "<td title='" . html_escape($row[$field_name]) . "'><span class='textOverflow' id='text$query_row" . '_' . $column_counter . "'>" . filter_value($row[$field_name], get_request_var('filter')) . '</span></td>';
} else {
print "<td><span class='textOverflow' id='text$query_row" . '_' . $column_counter . "'></span></td>";
}
$column_counter++;
}
}
}
print "<td style='width:1%;' class='checkbox'>";
if ($enabled) {
print "<input class='checkbox' type='checkbox' name='sg_$query_row' id='sg_$query_row'><label class='formCheckboxLabel' for='sg_$query_row'></label>";
} else {
print "<input class='checkbox' type='checkbox' disabled name='sg_$query_row' id='sg_$query_row'><label class='formCheckboxLabel' for='sg_$query_row'></label>";
}
print '</td>';
print '</tr>';
$row_counter++;
}
}
} else {
html_start_box('', '100%', '', '3', 'center', '');
print "<tr class='odd'><td class='textError'>" . __('Search Returned no Rows.') . '</td></tr>';
}
} else {
html_start_box('', '100%', '', '3', 'center', '');
print "<tr class='odd'><td class='textError'>" . __('Error in Data Query. This could be due to the following: File Permissions, or a missing or improperly formatted Data Query XML file.') . '</td></tr>';
}
html_end_box();
/* draw the graph template drop down here */
$data_query_graphs = db_fetch_assoc_prepared('SELECT id, name
FROM snmp_query_graph
WHERE snmp_query_id = ?
ORDER BY name',
array($snmp_query['id']));
if (cacti_sizeof($data_query_graphs) == 1) {
print "<input type='hidden' id='sgg_" . $snmp_query['id'] . "' name='sgg_" . $snmp_query['id'] . "' value='" . $data_query_graphs[0]['id'] . "'>";
} elseif (cacti_sizeof($data_query_graphs) > 1) {
print "<div class='break'></div>";
html_start_box('', '100%', '', '3', 'center', '');
$selected = store_get_selected_dq_index($snmp_query['id']);
print "<tr>
<td>
<img src='" . $config['url_path'] . "images/arrow.gif' alt=''>
</td>
<td class='right' style='width:100%'>
" . __('Select a Graph Type to Create') . "
</td>
<td class='right'>
<input type='button' class='ui-button ui-corner-all ui-widget default' id='default_" . $snmp_query['id'] . "' value='" . __esc('Set Default') . "' title='" . __esc('Make selection default') . "'>
</td>
<td class='right'>
<select class='dqselect' name='sgg_" . $snmp_query['id'] . "' id='sgg_" . $snmp_query['id'] . "' onChange='dqUpdateDeps(" . $snmp_query['id'] . ',' . (isset($column_counter) ? $column_counter:'') . ");'>
"; html_create_list($data_query_graphs, 'name', 'id', $selected); print "
</select>
</td>
</tr>";
html_end_box();
}
$script .= 'dqUpdateDeps(' . $snmp_query['id'] . ',' . $num_visible_fields . ");\n";
}
}
}
if ($script != '') {
$script .= "$('.default').click(function() { $.get('graphs_new.php?action=ajax_save&query=" . (isset($snmp_query['id']) ? $snmp_query['id']:'') . "'+'&item='+$(\".dqselect\").val()).fail(function(data) { getPresentHTTPError(data); });}); $('tr.notemplate').tooltip();</script>";
print $script;
}
form_hidden_box('save_component_graph', '1', '');
if (!isempty_request_var('host_id')) {
form_hidden_box('host_id', $host['id'], '0');
form_hidden_box('host_template_id', $host['host_template_id'], '0');
}
if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != '') {
$referer_url = parse_url($_SERVER['HTTP_REFERER']);
if ($_SERVER['SERVER_NAME'] != $referer_url['host']) {
/* Potential security exploit 1 */
set_request_var('returnto', 'host.php');
} elseif (strpos($_SERVER['HTTP_REFERER'], 'graphs_new') === false) {
set_request_var('returnto', basename($_SERVER['HTTP_REFERER']));
} else {
set_request_var('returnto', 'host.php');
}
} elseif (isset_request_var('returnto') && get_nfilter_request_var('returnto') != '') {
$returnto_url = parse_url(get_nfilter_request_var('returnto'));