forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_sources.php
1767 lines (1499 loc) · 65.5 KB
/
data_sources.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-2021 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_device.php');
include_once('./lib/api_graph.php');
include_once('./lib/data_query.php');
include_once('./lib/html_form_template.php');
include_once('./lib/poller.php');
include_once('./lib/rrd.php');
include_once('./lib/template.php');
include_once('./lib/utility.php');
$ds_actions = array(
1 => __('Delete'),
3 => __('Change Device'),
8 => __('Reapply Suggested Names'),
6 => __('Enable'),
7 => __('Disable')
);
$ds_actions = api_plugin_hook_function('data_source_action_array', $ds_actions);
/* set default action */
set_default_action();
validate_data_source_vars();
switch (get_request_var('action')) {
case 'save':
form_save();
break;
case 'actions':
form_actions();
break;
case 'rrd_add':
ds_rrd_add();
break;
case 'rrd_remove':
ds_rrd_remove();
break;
case 'data_edit':
top_header();
data_edit();
bottom_footer();
break;
case 'ds_disable':
ds_disable();
break;
case 'ds_enable':
ds_enable();
break;
case 'ds_remove':
ds_remove();
header ('Location: data_sources.php');
break;
case 'ds_edit':
ds_edit();
break;
case 'ajax_hosts':
$sql_where = '';
if (get_request_var('site_id') > 0) {
$sql_where = 'site_id = ' . get_request_var('site_id');
}
get_allowed_ajax_hosts(true, 'applyFilter', $sql_where);
break;
case 'ajax_hosts_noany':
$sql_where = '';
if (get_request_var('site_id') > 0) {
$sql_where = 'site_id = ' . get_request_var('site_id');
}
get_allowed_ajax_hosts(false, 'applyFilter', $sql_where);
break;
default:
top_header();
ds();
bottom_footer();
break;
}
/* --------------------------
The Save Function
-------------------------- */
function form_save() {
if ((isset_request_var('save_component_data_source_new')) && (!isempty_request_var('data_template_id'))) {
$save['id'] = get_filter_request_var('local_data_id');
$save['host_id'] = get_filter_request_var('host_id');
$save['data_template_id'] = get_filter_request_var('data_template_id');
$local_data_id = sql_save($save, 'data_local');
change_data_template($local_data_id, get_request_var('data_template_id'));
/* update the title cache */
update_data_source_title_cache($local_data_id);
/* update host data */
if (!isempty_request_var('host_id')) {
push_out_host(get_request_var('host_id'), $local_data_id);
}
}
if ((isset_request_var('save_component_data')) && (!is_error_message())) {
/* ================= input validation ================= */
get_filter_request_var('data_template_data_id');
/* ==================================================== */
/* ok, first pull out all 'input' values so we know how much to save */
$input_fields = db_fetch_assoc_prepared("SELECT dtd.data_input_id, dl.host_id, dif.id, dif.input_output,
dif.data_name, dif.regexp_match, dif.allow_nulls, dif.type_code
FROM data_template_data AS dtd
LEFT JOIN data_input_fields AS dif
ON dif.data_input_id = dtd.data_input_id
LEFT JOIN data_local AS dl
ON dtd.local_data_id = dl.id
WHERE dtd.id = ?
AND dif.input_output='in'",
array(get_request_var('data_template_data_id')));
if (cacti_sizeof($input_fields)) {
foreach ($input_fields as $input_field) {
if (isset_request_var('value_' . $input_field['id'])) {
/* save the data into the 'data_input_data' table */
$form_value = get_nfilter_request_var('value_' . $input_field['id']);
/* we shouldn't enforce rules on fields the user cannot see (ie. templated ones) */
$data_template_id = db_fetch_cell_prepared('SELECT local_data_template_data_id
FROM data_template_data
WHERE id = ?',
array(get_request_var('data_template_data_id'))
);
$is_templated = db_fetch_cell_prepared('SELECT t_value
FROM data_input_data
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($input_field['id'], $data_template_id)
);
if ($is_templated == '') {
$allow_nulls = true;
} elseif ($input_field['allow_nulls'] == 'on') {
$allow_nulls = true;
} elseif (empty($input_field['allow_nulls'])) {
$allow_nulls = false;
}
/* run regexp match on input string */
$form_value = form_input_validate($form_value, 'value_' . $input_field['id'], $input_field['regexp_match'], $allow_nulls, 3);
if (!is_error_message()) {
db_execute_prepared("REPLACE INTO data_input_data
(data_input_field_id, data_template_data_id, t_value, value)
VALUES
(?, ?, '', ?)",
array($input_field['id'], get_request_var('data_template_data_id'), $form_value)
);
}
}
}
}
}
if ((isset_request_var('save_component_data_source')) && (!is_error_message())) {
/* ================= input validation ================= */
get_filter_request_var('current_rrd');
get_filter_request_var('rrd_step');
get_filter_request_var('data_input_id');
get_filter_request_var('data_source_profile_id');
get_filter_request_var('host_id');
get_filter_request_var('_host_id');
get_filter_request_var('_data_template_id');
/* ==================================================== */
$save1['id'] = get_filter_request_var('local_data_id');
$save1['data_template_id'] = get_filter_request_var('data_template_id');
$save1['host_id'] = get_filter_request_var('host_id');
$save2['id'] = get_filter_request_var('data_template_data_id');
$save2['local_data_template_data_id'] = get_filter_request_var('local_data_template_data_id');
$save2['data_template_id'] = get_filter_request_var('data_template_id');
$save2['data_input_id'] = form_input_validate(get_request_var('data_input_id'), 'data_input_id', '^[0-9]+$', true, 3);
$save2['name'] = form_input_validate(get_nfilter_request_var('name'), 'name', '', false, 3);
$save2['data_source_path'] = form_input_validate(get_nfilter_request_var('data_source_path'), 'data_source_path', '', true, 3);
$save2['active'] = form_input_validate((isset_request_var('active') ? get_nfilter_request_var('active') : ''), 'active', '', true, 3);
$save2['data_source_profile_id'] = form_input_validate(get_request_var('data_source_profile_id'), 'data_source_profile_id', '^[0-9]+$', false, 3);
$save2['rrd_step'] = form_input_validate(get_request_var('rrd_step'), 'rrd_step', '^[0-9]+$', false, 3);
if (!is_error_message()) {
$local_data_id = sql_save($save1, 'data_local');
$save2['local_data_id'] = $local_data_id;
$data_template_data_id = sql_save($save2, 'data_template_data');
if ($data_template_data_id) {
raise_message(1);
} else {
raise_message(2);
}
}
if (!is_error_message()) {
/* if this is a new data source and a template has been selected, skip item creation this time
otherwise it throws off the templatate creation because of the NULL data */
if (!isempty_request_var('local_data_id') || isempty_request_var('data_template_id')) {
/* if no template was set before the save, there will be only one data source item to save;
otherwise there might be >1 */
if (isempty_request_var('_data_template_id')) {
$rrds[0]['id'] = get_nfilter_request_var('current_rrd');
} else {
$rrds = db_fetch_assoc_prepared('SELECT id
FROM data_template_rrd
WHERE local_data_id = ?',
array(get_filter_request_var('local_data_id')));
}
if (cacti_sizeof($rrds)) {
foreach ($rrds as $rrd) {
if (isempty_request_var('_data_template_id')) {
$name_modifier = '';
} else {
$name_modifier = '_' . $rrd['id'];
}
$save3['id'] = $rrd['id'];
$save3['local_data_id'] = $local_data_id;
$save3['local_data_template_rrd_id'] = db_fetch_cell_prepared('SELECT local_data_template_rrd_id
FROM data_template_rrd
WHERE id = ?',
array($rrd['id']));
$save3['data_template_id'] = get_filter_request_var('data_template_id');
$save3['rrd_maximum'] = form_input_validate(get_nfilter_request_var("rrd_maximum$name_modifier"), "rrd_maximum$name_modifier", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$|\|query_ifSpeed\||\|query_ifHighSpeed\|", false, 3);
$save3['rrd_minimum'] = form_input_validate(get_nfilter_request_var("rrd_minimum$name_modifier"), "rrd_minimum$name_modifier", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$|\|query_ifSpeed\||\|query_ifHighSpeed\|", false, 3);
$save3['rrd_heartbeat'] = form_input_validate(get_nfilter_request_var("rrd_heartbeat$name_modifier"), "rrd_heartbeat$name_modifier", '^[0-9]+$', false, 3);
$save3['data_source_type_id'] = form_input_validate(get_nfilter_request_var("data_source_type_id$name_modifier"), "data_source_type_id$name_modifier", '^[0-9]+$', false, 3);
$save3['data_source_name'] = form_input_validate(get_nfilter_request_var("data_source_name$name_modifier"), "data_source_name$name_modifier", '^[a-zA-Z0-9_-]{1,19}$', false, 3);
$save3['data_input_field_id'] = form_input_validate((isset_request_var("data_input_field_id$name_modifier") ? get_nfilter_request_var("data_input_field_id$name_modifier") : '0'), "data_input_field_id$name_modifier", '', true, 3);
if ($save3['rrd_minimum'] != 'U' && $save3['rrd_maximum'] != 'U') {
if ($save3['rrd_minimum'] >= $save3['rrd_maximum']) {
raise_message(43);
$_SESSION['sess_error_fields']['rrd_maximum'] = 'rrd_maximum';
header('Location: data_sources.php?action=ds_edit&id=' . (empty($local_data_id) ? get_filter_request_var('local_data_id') : $local_data_id) . '&host_id=' . get_request_var('host_id') . '&view_rrd=' . (isset_request_var('current_rrd') ? get_nfilter_request_var('current_rrd') : '0'));
exit;
}
}
$data_template_rrd_id = sql_save($save3, 'data_template_rrd');
if ($data_template_rrd_id) {
raise_message(1);
} else {
raise_message(2);
}
}
}
}
}
if (!is_error_message()) {
if (get_request_var('data_template_id') != get_request_var('_data_template_id')) {
/* update all necessary template information */
change_data_template($local_data_id, get_request_var('data_template_id'));
} elseif (!isempty_request_var('data_template_id')) {
update_data_source_data_query_cache($local_data_id);
}
if (get_request_var('host_id') != get_request_var('_host_id')) {
/* push out all necessary host information */
push_out_host(get_request_var('host_id'), $local_data_id);
/* reset current host for display purposes */
$_SESSION['sess_data_source_current_host_id'] = get_request_var('host_id');
}
/* if no data source path has been entered, generate one */
if (isempty_request_var('data_source_path')) {
generate_data_source_path($local_data_id);
}
/* update the title cache */
update_data_source_title_cache($local_data_id);
}
}
/* update the poller cache last to make sure everything is fresh */
if ((!is_error_message()) && (!empty($local_data_id))) {
update_poller_cache($local_data_id, true);
}
if (isset_request_var('save_component_data_source_new') && isempty_request_var('data_template_id')) {
header('Location: data_sources.php?action=ds_edit&host_id=' . get_request_var('host_id') . '&new=1');
} elseif ((is_error_message()) || (get_filter_request_var('data_template_id') != get_filter_request_var('_data_template_id')) || (get_filter_request_var('data_input_id') != get_filter_request_var('_data_input_id')) || (get_filter_request_var('host_id') != get_filter_request_var('_host_id'))) {
header('Location: data_sources.php?action=ds_edit&id=' . (empty($local_data_id) ? get_filter_request_var('local_data_id') : $local_data_id) . '&host_id=' . get_request_var('host_id') . '&view_rrd=' . (isset_request_var('current_rrd') ? get_nfilter_request_var('current_rrd') : '0'));
} else {
header('Location: data_sources.php');
}
}
/* ------------------------
The "actions" function
------------------------ */
function form_actions() {
global $ds_actions;
/* ================= input validation ================= */
get_filter_request_var('drp_action', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z0-9_]+)$/')));
/* ==================================================== */
/* if we are to save this form, instead of display it */
if (isset_request_var('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
if (get_nfilter_request_var('drp_action') == '1') { /* delete */
if (!isset_request_var('delete_type')) {
set_request_var('delete_type', 1);
} else {
get_filter_request_var('delete_type');
}
switch (get_request_var('delete_type')) {
case '2': /* delete all graph items tied to this data source */
$data_template_rrds = array_rekey(db_fetch_assoc('SELECT id
FROM data_template_rrd
WHERE ' . array_to_sql_or($selected_items, 'local_data_id')), 'id', 'id');
$poller_ids = db_fetch_assoc('SELECT DISTINCT poller_id
FROM host AS h
INNER JOIN data_local AS dl
ON dl.host_id=h.id
WHERE poller_id > 1
AND id IN (' . implode(', ', $selected_items) . ')');
api_plugin_hook_function('graph_items_remove', $data_template_rrds);
/* loop through each data source item */
if (cacti_sizeof($data_template_rrds) > 0) {
db_execute('DELETE FROM graph_templates_item
WHERE task_item_id IN (' . implode(',', $data_template_rrds) . ')
AND local_graph_id > 0');
if (cacti_sizeof($poller_ids)) {
foreach($poller_ids as $poller_id) {
if (($rcnn_id = poller_push_to_remote_db_connect($poller_id, true)) !== false) {
db_execute('DELETE FROM graph_templates_item
WHERE task_item_id IN (' . implode(',', $data_template_rrds) . ')
AND local_graph_id > 0', true, $rcnn_id);
}
}
}
}
break;
case '3': /* delete all graphs tied to this data source */
$graphs = array_rekey(db_fetch_assoc('SELECT
graph_templates_graph.local_graph_id
FROM (data_template_rrd,graph_templates_item,graph_templates_graph)
WHERE graph_templates_item.task_item_id=data_template_rrd.id
AND graph_templates_item.local_graph_id=graph_templates_graph.local_graph_id
AND ' . array_to_sql_or($selected_items, 'data_template_rrd.local_data_id') . '
AND graph_templates_graph.local_graph_id > 0
GROUP BY graph_templates_graph.local_graph_id'), 'local_graph_id', 'local_graph_id');
if (cacti_sizeof($graphs) > 0) {
api_graph_remove_multi($graphs);
}
break;
}
api_data_source_remove_multi($selected_items);
} elseif (get_nfilter_request_var('drp_action') == '3') { // change host
get_filter_request_var('host_id');
api_data_source_change_host($selected_items, get_request_var('host_id'));
} elseif (get_nfilter_request_var('drp_action') == '6') { // data source enable
for ($i=0;($i<cacti_count($selected_items));$i++) {
api_data_source_enable($selected_items[$i]);
}
} elseif (get_nfilter_request_var('drp_action') == '7') { // data source disable
for ($i=0;($i<cacti_count($selected_items));$i++) {
api_data_source_disable($selected_items[$i]);
}
} elseif (get_nfilter_request_var('drp_action') == '8') { // reapply suggested data source naming
for ($i=0;($i<cacti_count($selected_items));$i++) {
api_reapply_suggested_data_source_data($selected_items[$i]);
update_data_source_title_cache($selected_items[$i]);
}
} else {
api_plugin_hook_function('data_source_action_execute', get_nfilter_request_var('drp_action'));
}
}
/* update snmpcache */
snmpagent_data_source_action_bottom(array(get_nfilter_request_var('drp_action'), $selected_items));
api_plugin_hook_function('data_source_action_bottom', array(get_nfilter_request_var('drp_action'), $selected_items));
header('Location: data_sources.php');
exit;
}
/* setup some variables */
$ds_list = ''; $i = 0;
/* loop through each of the graphs selected on the previous page and get more info about them */
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
$ds_list .= '<li>' . html_escape(get_data_source_title($matches[1])) . '</li>';
$ds_array[$i] = $matches[1];
$i++;
}
}
top_header();
form_start('data_sources.php');
html_start_box($ds_actions[get_nfilter_request_var('drp_action')], '60%', '', '3', 'center', '');
if (isset($ds_array) && cacti_sizeof($ds_array)) {
if (get_nfilter_request_var('drp_action') == '1') { /* delete */
$graphs = array();
/* find out which (if any) graphs are using this data source, so we can tell the user */
if (isset($ds_array)) {
$graphs = db_fetch_assoc('SELECT
graph_templates_graph.local_graph_id,
graph_templates_graph.title_cache
FROM (data_template_rrd,graph_templates_item,graph_templates_graph)
WHERE graph_templates_item.task_item_id=data_template_rrd.id
AND graph_templates_item.local_graph_id=graph_templates_graph.local_graph_id
AND ' . array_to_sql_or($ds_array, 'data_template_rrd.local_data_id') . '
AND graph_templates_graph.local_graph_id > 0
GROUP BY graph_templates_graph.local_graph_id
ORDER BY graph_templates_graph.title_cache');
}
print "<tr>
<td class='textArea'>
<p>" . __n('Click \'Continue\' to delete the following Data Source', 'Click \'Continue\' to delete following Data Sources', cacti_sizeof($ds_array)) . "</p>
<div class='itemlist'><ul>$ds_list</ul></div>";
if (cacti_sizeof($graphs)) {
print "<tr><td class='textArea'><p class='textArea'>" . __n('The following graph is using these data sources:', 'The following graphs are using these data sources:', cacti_sizeof($graphs)) . "</p>";
print '<div class="itemlist"><ul>';
foreach ($graphs as $graph) {
print '<li>' . html_escape($graph['title_cache']) . "</li>";
}
print '</ul></div>';
print '<br>';
form_radio_button('delete_type', '3', '1', __n('Leave the <strong>Graph</strong> untouched.', 'Leave all <strong>Graphs</strong> untouched.', cacti_sizeof($graphs)), '1'); print '<br>';
form_radio_button('delete_type', '3', '2', __n('Delete all <strong>Graph Items</strong> that reference this Data Source.', 'Delete all <strong>Graph Items</strong> that reference these Data Sources.', cacti_sizeof($ds_array)), '1'); print '<br>';
form_radio_button('delete_type', '3', '3', __n('Delete all <strong>Graphs</strong> that reference this Data Source.', 'Delete all <strong>Graphs</strong> that reference these Data Sources.', cacti_sizeof($ds_array)), '1'); print '<br>';
print '</td></tr>';
}
print "</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __n('Delete Data Source', 'Delete Data Sources', cacti_sizeof($ds_array)) . "'>";
} elseif (get_nfilter_request_var('drp_action') == '3') { // change host
print "<tr>
<td class='textArea'>
<p>" . __n('Choose a new Device for this Data Source and click \'Continue\'.', 'Choose a new Device for these Data Sources and click \'Continue\'', cacti_sizeof($ds_array)) . "</p>
<div class='itemlist'><ul>$ds_list</ul></div>
<p>" . __('New Device:') . "<br>"; form_dropdown('host_id', db_fetch_assoc("SELECT id, CONCAT_WS('',description,' (',hostname,')') AS name FROM host ORDER BY description, hostname"),'name','id','','','0'); print "</p>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Change Device') . "'>";
} elseif (get_nfilter_request_var('drp_action') == '6') { // data source enable
print "<tr>
<td class='textArea'>
<p>" . __n('Click \'Continue\' to enable the following Data Source.', 'Click \'Continue\' to enable all following Data Sources.', cacti_sizeof($ds_array)) . "</p>
<div class='itemlist'><ul>$ds_list</ul></div>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __n('Enable Data Source', 'Enable Data Sources', cacti_sizeof($ds_array)) . "'>";
} elseif (get_nfilter_request_var('drp_action') == '7') { // data source disable
print "<tr>
<td class='textArea'>
<p>" . __n('Click \'Continue\' to disable the following Data Source.', 'Click \'Continue\' to disable all following Data Sources.', cacti_sizeof($ds_array)) . "</p>
<div class='itemlist'><ul>$ds_list</ul></div>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __n('Disable Data Source', 'Disable Data Sources', cacti_sizeof($ds_array)) . "'>";
} elseif (get_nfilter_request_var('drp_action') == '8') { // reapply suggested data source naming
print "<tr>
<td class='textArea'>
<p>" . __n('Click \'Continue\' to re-apply the suggested name to the following Data Source.', 'Click \'Continue\' to re-apply the suggested names to all following Data Sources.', cacti_sizeof($ds_array)) . "</p>
<div class='itemlist'><ul>$ds_list</ul></div>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __n('Reapply Suggested Naming to Data Source', 'Reapply Suggested Naming to Data Sources', cacti_sizeof($ds_array)) . "'>";
} else {
$save['drp_action'] = get_nfilter_request_var('drp_action');
$save['ds_list'] = $ds_list;
$save['ds_array'] = (isset($ds_array)? $ds_array : array());
api_plugin_hook_function('data_source_action_prepare', $save);
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "'>";
}
} else {
raise_message(40);
header('Location: data_sources.php');
exit;
}
print "<tr>
<td class='saveRow'>
<input type='hidden' name='action' value='actions'>
<input type='hidden' name='selected_items' value='" . (isset($ds_array) ? serialize($ds_array) : '') . "'>
<input type='hidden' name='drp_action' value='" . html_escape(get_nfilter_request_var('drp_action')) . "'>
$save_html
</td>
</tr>";
html_end_box();
form_end();
bottom_footer();
}
/* ----------------------------
data - Custom Data
---------------------------- */
function data_edit($incform = true) {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
if (!isempty_request_var('id')) {
$data = db_fetch_row_prepared('SELECT id, data_input_id, data_template_id, name, local_data_id
FROM data_template_data
WHERE local_data_id = ?',
array(get_request_var('id')));
$template_data = db_fetch_row_prepared('SELECT id, data_input_id
FROM data_template_data
WHERE data_template_id = ?
AND local_data_id = 0',
array($data['data_template_id']));
$host = db_fetch_row_prepared('SELECT host.id, host.hostname
FROM (data_local, host)
WHERE data_local.host_id = host.id
AND data_local.id = ?',
array(get_request_var('id')));
}
if ($incform) {
form_start('data_sources.php', 'data_source_edit');
}
$i = 0;
if (!empty($data['data_input_id'])) {
/* get each INPUT field for this data input source */
$fields = db_fetch_assoc_prepared('SELECT *
FROM data_input_fields
WHERE data_input_id = ?
AND input_output = "in"
ORDER BY name',
array($data['data_input_id'])
);
$data_input_name = db_fetch_cell_prepared('SELECT name
FROM data_input
WHERE id = ?',
array($data['data_input_id']));
html_start_box(__('Custom Data [data input: %s]', html_escape($data_input_name)), '100%', '', '3', 'center', '');
/* loop through each field found */
if (cacti_sizeof($fields) > 0) {
foreach ($fields as $field) {
$data_input_data = db_fetch_row_prepared('SELECT *
FROM data_input_data
WHERE data_template_data_id = ?
AND data_input_field_id = ?',
array($data['id'], $field['id'])
);
if (cacti_sizeof($data_input_data) > 0) {
$old_value = $data_input_data['value'];
} else {
$old_value = '';
}
/* if data template then get t_value FROM template, else always allow user input */
if (empty($data['data_template_id'])) {
$can_template = 'on';
} else {
$can_template = db_fetch_cell_prepared('SELECT t_value
FROM data_input_data
WHERE data_template_data_id = ?
AND data_input_field_id = ?',
array($template_data['id'], $field['id'])
);
}
form_alternate_row();
if ((!empty($host['id'])) && (preg_match('/^' . VALID_HOST_FIELDS . '$/i', $field['type_code']))) {
print "<td style='width:50%;'><strong>" . html_escape($field['name']) . '</strong> ' . __('(From Device: %s)', html_escape($host['hostname'])) . "</td>";
print "<td><em>" . html_escape($old_value) . "</em></td>";
} elseif (empty($can_template)) {
print "<td style='width:50%;'><strong>" . html_escape($field['name']) . '</strong> ' . __('(From Data Template)') . "</td>";
print '<td><em>' . (empty($old_value) ? __('Nothing Entered') : html_escape($old_value)) . "</em></td>";
} else {
print "<td style='width:50%;'><strong>" . html_escape($field['name']) . "</strong></td>";
print '<td>';
draw_custom_data_row('value_' . $field['id'], $field['id'], $data['id'], $old_value);
print '</td>';
}
print "</tr>";
$i++;
}
} else {
print '<tr><td><em>' . __('No Input Fields for the Selected Data Input Source') . '</em></td></tr>';
}
html_end_box();
}
if ($incform) {
form_hidden_box('local_data_id', (isset($data) ? $data['local_data_id'] : '0'), '');
form_hidden_box('data_template_data_id', (isset($data) ? $data['id'] : '0'), '');
}
form_hidden_box('save_component_data', '1', '');
}
/* ------------------------
Data Source Functions
------------------------ */
function ds_rrd_remove() {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
db_execute_prepared('DELETE FROM data_template_rrd
WHERE id = ?',
array(get_request_var('id')));
db_execute_prepared('UPDATE graph_templates_item
SET task_item_id = 0
WHERE task_item_id = ?',
array(get_request_var('id')));
header('Location: data_sources.php?action=ds_edit&id=' . get_request_var('local_data_id'));
}
function ds_rrd_add() {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
db_execute_prepared("INSERT INTO data_template_rrd
(local_data_id, rrd_maximum, rrd_minimum, rrd_heartbeat, data_source_type_id, data_source_name)
VALUES (?, 100, 0, 600, 1, 'ds')",
array(get_request_var('id')));
$data_template_rrd_id = db_fetch_insert_id();
header('Location: data_sources.php?action=ds_edit&id=' . get_request_var('id') . "&view_rrd=$data_template_rrd_id");
}
function ds_disable() {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
api_data_source_disable(get_request_var('id'));
header('Location: data_sources.php?action=ds_edit&id=' . get_request_var('id'));
}
function ds_enable() {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
api_data_source_enable(get_request_var('id'));
header('Location: data_sources.php?action=ds_edit&id=' . get_request_var('id'));
}
function ds_edit() {
global $struct_data_source, $struct_data_source_item;
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('host_id');
/* ==================================================== */
api_plugin_hook('data_source_edit_top');
$use_data_template = true;
$data_template = array();
if (!isempty_request_var('id')) {
$data_local = db_fetch_row_prepared('SELECT host_id, data_template_id
FROM data_local
WHERE id = ?',
array(get_request_var('id')));
$data = db_fetch_row_prepared('SELECT *
FROM data_template_data
WHERE local_data_id = ?',
array(get_request_var('id')));
if (isset($data_local['data_template_id']) && $data_local['data_template_id'] >= 0) {
$data_template = db_fetch_row_prepared('SELECT id, name
FROM data_template
WHERE id = ?',
array($data_local['data_template_id']));
$data_template_data = db_fetch_row_prepared('SELECT *
FROM data_template_data
WHERE data_template_id = ?
AND local_data_id = 0',
array($data_local['data_template_id']));
} else {
raise_message(11);
header ('Location: data_sources.php');
exit;
}
$header_label = __esc('Data Template Selection [edit: %s]', get_data_source_title(get_request_var('id')));
if (empty($data_local['data_template_id'])) {
$use_data_template = false;
}
} else {
$header_label = __('Data Template Selection [new]');
$use_data_template = false;
}
/* handle debug mode */
if (isset_request_var('debug')) {
if (get_nfilter_request_var('debug') == '0') {
kill_session_var('ds_debug_mode');
} elseif (get_nfilter_request_var('debug') == '1') {
$_SESSION['ds_debug_mode'] = true;
}
}
/* handle debug mode */
if (isset_request_var('info')) {
if (get_nfilter_request_var('info') == '0') {
kill_session_var('ds_info_mode');
} elseif (get_nfilter_request_var('info') == '1') {
$_SESSION['ds_info_mode'] = true;
}
}
top_header();
if (!isempty_request_var('id')) {
$local_graph_ids = db_fetch_assoc_prepared('SELECT DISTINCT local_graph_id
FROM graph_templates_item AS gti
INNER JOIN data_template_rrd AS dtr
ON dtr.id = gti.task_item_id
WHERE local_data_id = ?',
array(get_request_var('id')));
?>
<table style='width:100%'>
<tr>
<td class='textInfo left' style='vertical-align:top;'>
<?php print html_escape(get_data_source_title(get_request_var('id')));?>
</td>
<td class='textInfo right' style='vertical-align:top;'>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('data_sources.php?action=ds_edit&id=' . (isset_request_var('id') ? get_request_var('id') : '0') . '&debug=' . (isset($_SESSION['ds_debug_mode']) ? '0' : '1'));?>'><?php print (isset($_SESSION['ds_debug_mode']) ? __('Turn Off Data Source Debug Mode.') : __('Turn On Data Source Debug Mode.'));?></a><br>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('data_sources.php?action=ds_edit&id=' . (isset_request_var('id') ? get_request_var('id') : '0') . '&info=' . (isset($_SESSION['ds_info_mode']) ? '0' : '1'));?>'><?php print (isset($_SESSION['ds_info_mode']) ? __('Turn Off Data Source Info Mode.') : __('Turn On Data Source Info Mode.'));?></a><br>
<?php
if (cacti_sizeof($local_graph_ids)) {
foreach($local_graph_ids as $id) {
$name = db_fetch_cell_prepared('SELECT title_cache
FROM graph_templates_graph
WHERE local_graph_id = ?',
array($id['local_graph_id']));
?><span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('graphs.php?action=graph_edit&id=' . $id['local_graph_id']);?>'><?php print __('Edit Graph: \'%s\'.', $name);?></a><br><?php
}
}
if (!empty($data_local['host_id'])) {
?><span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('host.php?action=edit&id=' . $data_local['host_id']);?>'><?php print __('Edit Device.');?></a><br><?php
}
if (!empty($data_template['id'])) {
?><span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('data_templates.php?action=template_edit&id=' . (isset($data_template['id']) ? $data_template['id'] : '0'));?>'><?php print __('Edit Data Template.');?></a><br><?php
}
if (isset_request_var('id') && get_request_var('id') > 0) {
?><span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('data_sources.php?action=ds_' . ($data['active'] == 'on' ? 'dis' : 'en') . 'able&id=' . get_request_var('id')) ?>'><?php print ($data['active'] == 'on' ? __('Disable Data Source.') : __('Enable Data Source.'));?></a><br>
<?php
}
?>
</td>
</tr>
</table>
<br>
<?php
}
form_start('data_sources.php', 'data_source');
html_start_box($header_label, '100%', true, '3', 'center', '');
if (cacti_sizeof($data_template)) {
$data_sources = db_fetch_cell_prepared('SELECT
GROUP_CONCAT(DISTINCT data_source_name ORDER BY data_source_name) AS data_source_names
FROM data_template_rrd
WHERE data_template_id = ?
GROUP BY data_template_id
ORDER BY data_source_names',
array($data_template['id'])
);
$dts = db_fetch_assoc_prepared('SELECT data_template_id,
GROUP_CONCAT(DISTINCT data_source_name ORDER BY data_source_name) AS data_source_names
FROM data_template_rrd
WHERE local_data_id=0
GROUP BY data_template_id
HAVING data_source_names = ?',
array($data_sources)
);
if (cacti_sizeof($dts)) {
foreach($dts as $dtid) {
$dtids[] = $dtid['data_template_id'];
}
}
$dtsql = 'SELECT id, name FROM data_template WHERE id IN(' . implode(',', $dtids) . ') ORDER BY name';
} else {
$dtsql = 'SELECT id, name FROM data_template ORDER BY name';
}
if (get_request_var('host_id') > 0) {
$hostDescription = db_fetch_cell_prepared('SELECT description
FROM host
WHERE id = ?',
array(get_request_var('host_id'))
);
} elseif (isset($data_local['host_id'])) {
$hostDescription = db_fetch_cell_prepared('SELECT description
FROM host
WHERE id = ?',
array($data_local['host_id'])
);
} else {
$hostDescription = '';
}
$form_array = array(
'data_template_id' => array(
'method' => 'drop_sql',
'friendly_name' => __('Selected Data Template'),
'description' => __('The name given to this data template. Please note that you may only change Graph Templates to a 100%$ compatible Graph Template, which means that it includes identical Data Sources.'),
'value' => (cacti_sizeof($data_template) ? $data_template['id'] : '0'),
'none_value' => (cacti_sizeof($data_template) ? '' : 'None'),
'sql' => $dtsql
),
'host_id' => array(
'method' => 'drop_callback',
'friendly_name' => __('Device'),
'description' => __('Choose the Device that this Data Source belongs to.'),
'none_value' => __('None'),
'sql' => 'SELECT id, description AS name FROM host ORDER BY name',
'action' => 'ajax_hosts_noany',
'id' => (isset($data_local['host_id']) ? $data_local['host_id'] : 0),
'value' => $hostDescription
),
'_data_template_id' => array(
'method' => 'hidden',
'value' => (isset($data_template['id']) ? $data_template['id'] : '0')
),
'_host_id' => array(
'method' => 'hidden',
'value' => (isset($data_local['host_id']) ? $data_local['host_id'] : '0')
),
'_data_input_id' => array(
'method' => 'hidden',
'value' => (isset($data['data_input_id']) ? $data['data_input_id'] : '0')
),
'data_template_data_id' => array(
'method' => 'hidden',
'value' => (isset($data['id']) ? $data['id'] : '0')
),
'local_data_template_data_id' => array(
'method' => 'hidden',
'value' => (isset($data['local_data_template_data_id']) ? $data['local_data_template_data_id'] : '0')
),
'local_data_id' => array(
'method' => 'hidden',
'value' => (isset($data['local_data_id']) ? $data['local_data_id'] : '0')
),
);
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => $form_array
)
);
html_end_box(true, true);
/* only display the "inputs" area if we are using a data template for this data source */
if (!empty($data['data_template_id'])) {
$template_data_rrds = db_fetch_assoc_prepared('SELECT *
FROM data_template_rrd
WHERE local_data_id = ?
ORDER BY data_source_name',
array(get_request_var('id')));
html_start_box(__('Supplemental Data Template Data'), '100%', true, '3', 'center', '');
draw_nontemplated_fields_data_source($data['data_template_id'], $data['local_data_id'], $data, '|field|', __('Data Source Fields'), true, true, 0);
draw_nontemplated_fields_data_source_item($data['data_template_id'], $template_data_rrds, '|field|_|id|', __('Data Source Item Fields'), true, true, true, 0);
draw_nontemplated_fields_custom_data($data['id'], 'value_|id|', __('Custom Data'), true, true, 0);
form_hidden_box('save_component_data','1','');
html_end_box(true, true);
}
if (((isset_request_var('id')) || (isset_request_var('new'))) && (empty($data['data_template_id']))) {
html_start_box( __('Data Source'), '100%', true, '3', 'center', '');
$form_array = array();
foreach ($struct_data_source as $field_name => $field_array) {
$form_array += array($field_name => $struct_data_source[$field_name]);