/
graphs.php
1492 lines (1293 loc) · 67.7 KB
/
graphs.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-2011 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/utility.php");
include_once("./lib/api_graph.php");
include_once("./lib/api_tree.php");
include_once("./lib/api_data_source.php");
include_once("./lib/template.php");
include_once("./lib/html_tree.php");
include_once("./lib/html_form_template.php");
include_once("./lib/rrd.php");
include_once("./lib/data_query.php");
define("MAX_DISPLAY_PAGES", 21);
/* modify for multi user start */
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
$graph_actions = array(
1 => "Delete",
2 => "Change Graph Template",
5 => "Change Host",
6 => "Reapply Suggested Names",
3 => "Duplicate"
);
} else {
$graph_actions = array(
1 => "Delete",
2 => "Change Graph Template",
5 => "Change Host",
6 => "Reapply Suggested Names",
7 => "Resize Graphs",
3 => "Duplicate",
4 => "Convert to Graph Template"
);
$graph_actions = api_plugin_hook_function('graphs_action_array', $graph_actions);
}
/* set default action */
if (!isset($_REQUEST["action"])) { $_REQUEST["action"] = ""; }
switch ($_REQUEST["action"]) {
case 'save':
form_save();
break;
case 'actions':
form_actions();
break;
case 'graph_diff':
include_once("./include/top_header.php");
graph_diff();
include_once("./include/bottom_footer.php");
break;
case 'item':
include_once("./include/top_header.php");
item();
include_once("./include/bottom_footer.php");
break;
case 'graph_remove':
graph_remove();
header("Location: graphs.php");
break;
case 'graph_edit':
include_once("./include/top_header.php");
graph_edit();
include_once("./include/bottom_footer.php");
break;
default:
include_once("./include/top_header.php");
graph();
include_once("./include/bottom_footer.php");
break;
}
/* --------------------------
Global Form Functions
-------------------------- */
function add_tree_names_to_actions_array() {
/* modify for multi user start */
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) return;
/* modify for multi user start */
global $graph_actions;
/* add a list of tree names to the actions dropdown */
$trees = db_fetch_assoc("select id,name from graph_tree order by name");
if (sizeof($trees) > 0) {
foreach ($trees as $tree) {
$graph_actions{"tr_" . $tree["id"]} = "Place on a Tree (" . $tree["name"] . ")";
}
}
}
/* --------------------------
The Save Function
-------------------------- */
function form_save() {
/* modify for multi user start */
if(!empty($_POST["host_id"])) {
if (!check_host($_POST["host_id"])) access_denied();
}
/* modify for multi user end */
if ((isset($_POST["save_component_graph_new"])) && (!empty($_POST["graph_template_id"]))) {
/* ================= input validation ================= */
input_validate_input_number(get_request_var_post("graph_template_id"));
/* ==================================================== */
/* modify for multi user start */
if (!check_resource_count(RESOURCE_GRAPH)) access_denied();
/* modify for multi user end */
$save["id"] = $_POST["local_graph_id"];
$save["graph_template_id"] = $_POST["graph_template_id"];
$save["host_id"] = $_POST["host_id"];
$local_graph_id = sql_save($save, "graph_local");
change_graph_template($local_graph_id, $_POST["graph_template_id"], true);
/* update the title cache */
update_graph_title_cache($local_graph_id);
}
if (isset($_POST["save_component_graph"])) {
/* ================= input validation ================= */
input_validate_input_number(get_request_var_post("graph_template_id"));
input_validate_input_number(get_request_var_post("_graph_template_id"));
/* ==================================================== */
$save1["id"] = $_POST["local_graph_id"];
$save1["host_id"] = $_POST["host_id"];
$save1["graph_template_id"] = $_POST["graph_template_id"];
$save2["id"] = $_POST["graph_template_graph_id"];
$save2["local_graph_template_graph_id"] = $_POST["local_graph_template_graph_id"];
$save2["graph_template_id"] = $_POST["graph_template_id"];
$save2["image_format_id"] = form_input_validate($_POST["image_format_id"], "image_format_id", "", true, 3);
$save2["title"] = form_input_validate($_POST["title"], "title", "", false, 3);
$save2["height"] = form_input_validate($_POST["height"], "height", "^[0-9]+$", false, 3);
$save2["width"] = form_input_validate($_POST["width"], "width", "^[0-9]+$", false, 3);
$save2["upper_limit"] = form_input_validate($_POST["upper_limit"], "upper_limit", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$", ((strlen($_POST["upper_limit"]) === 0) ? true : false), 3);
$save2["lower_limit"] = form_input_validate($_POST["lower_limit"], "lower_limit", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$", ((strlen($_POST["lower_limit"]) === 0) ? true : false), 3);
$save2["vertical_label"] = form_input_validate($_POST["vertical_label"], "vertical_label", "", true, 3);
$save2["slope_mode"] = form_input_validate((isset($_POST["slope_mode"]) ? $_POST["slope_mode"] : ""), "slope_mode", "", true, 3);
$save2["auto_scale"] = form_input_validate((isset($_POST["auto_scale"]) ? $_POST["auto_scale"] : ""), "auto_scale", "", true, 3);
$save2["auto_scale_opts"] = form_input_validate($_POST["auto_scale_opts"], "auto_scale_opts", "", true, 3);
$save2["auto_scale_log"] = form_input_validate((isset($_POST["auto_scale_log"]) ? $_POST["auto_scale_log"] : ""), "auto_scale_log", "", true, 3);
$save2["scale_log_units"] = form_input_validate((isset($_POST["scale_log_units"]) ? $_POST["scale_log_units"] : ""), "scale_log_units", "", true, 3);
$save2["auto_scale_rigid"] = form_input_validate((isset($_POST["auto_scale_rigid"]) ? $_POST["auto_scale_rigid"] : ""), "auto_scale_rigid", "", true, 3);
$save2["auto_padding"] = form_input_validate((isset($_POST["auto_padding"]) ? $_POST["auto_padding"] : ""), "auto_padding", "", true, 3);
$save2["base_value"] = form_input_validate($_POST["base_value"], "base_value", "^[0-9]+$", false, 3);
$save2["export"] = form_input_validate((isset($_POST["export"]) ? $_POST["export"] : ""), "export", "", true, 3);
$save2["unit_value"] = form_input_validate($_POST["unit_value"], "unit_value", "", true, 3);
$save2["unit_exponent_value"] = form_input_validate($_POST["unit_exponent_value"], "unit_exponent_value", "^-?[0-9]+$", true, 3);
if (!is_error_message()) {
$local_graph_id = sql_save($save1, "graph_local");
}
if (!is_error_message()) {
$save2["local_graph_id"] = $local_graph_id;
$graph_templates_graph_id = sql_save($save2, "graph_templates_graph");
if ($graph_templates_graph_id) {
raise_message(1);
/* if template information chanegd, update all necessary template information */
if ($_POST["graph_template_id"] != $_POST["_graph_template_id"]) {
/* check to see if the number of graph items differs, if it does; we need user input */
if ((!empty($_POST["graph_template_id"])) && (!empty($_POST["local_graph_id"])) && (sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_id=$local_graph_id")) != sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_id=0 and graph_template_id=" . $_POST["graph_template_id"])))) {
/* set the template back, since the user may choose not to go through with the change
at this point */
db_execute("update graph_local set graph_template_id=" . $_POST["_graph_template_id"] . " where id=$local_graph_id");
db_execute("update graph_templates_graph set graph_template_id=" . $_POST["_graph_template_id"] . " where local_graph_id=$local_graph_id");
header("Location: graphs.php?action=graph_diff&id=$local_graph_id&graph_template_id=" . $_POST["graph_template_id"]);
exit;
}
}
}else{
raise_message(2);
}
/* update the title cache */
update_graph_title_cache($local_graph_id);
}
if ((!is_error_message()) && ($_POST["graph_template_id"] != $_POST["_graph_template_id"])) {
change_graph_template($local_graph_id, $_POST["graph_template_id"], true);
}elseif (!empty($_POST["graph_template_id"])) {
update_graph_data_query_cache($local_graph_id);
}
}
if (isset($_POST["save_component_input"])) {
/* ================= input validation ================= */
input_validate_input_number(get_request_var_post("local_graph_id"));
/* ==================================================== */
/* first; get the current graph template id */
$graph_template_id = db_fetch_cell("select graph_template_id from graph_local where id=" . $_POST["local_graph_id"]);
/* get all inputs that go along with this graph template */
$input_list = db_fetch_assoc("select id,column_name from graph_template_input where graph_template_id=$graph_template_id");
if (sizeof($input_list) > 0) {
/* modify for multi user start */
$sql_join = "";
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
$sql_join = "
INNER JOIN graph_local ON graph_templates_item.local_graph_id = graph_local.id
INNER JOIN host ON graph_local.host_id = host.id
INNER JOIN user_auth_perms ON host.id = user_auth_perms.item_id AND user_auth_perms.user_id = '" . $_SESSION["sess_user_id"] ."' AND user_auth_perms.type = '3'";
}
foreach ($input_list as $input) {
/* we need to find out which graph items will be affected by saving this particular item */
$item_list = db_fetch_assoc("select
graph_templates_item.id
from (graph_template_input_defs,graph_templates_item)
$sql_join
where graph_template_input_defs.graph_template_item_id=graph_templates_item.local_graph_template_item_id
and graph_templates_item.local_graph_id=" . $_POST["local_graph_id"] . "
and graph_template_input_defs.graph_template_input_id=" . $input["id"]);
/* modify for multi user end */
/* loop through each item affected and update column data */
if (sizeof($item_list) > 0) {
foreach ($item_list as $item) {
/* if we are changing templates, the POST vars we are searching for here will not exist.
this is because the db and form are out of sync here, but it is ok to just skip over saving
the inputs in this case. */
if (isset($_POST{$input["column_name"] . "_" . $input["id"]})) {
db_execute("update graph_templates_item set " . $input["column_name"] . "='" . $_POST{$input["column_name"] . "_" . $input["id"]} . "' where id=" . $item["id"]);
}
}
}
}
}
}
if (isset($_POST["save_component_graph_diff"])) {
if ($_POST["type"] == "1") {
$intrusive = true;
}elseif ($_POST["type"] == "2") {
$intrusive = false;
}
change_graph_template($_POST["local_graph_id"], $_POST["graph_template_id"], $intrusive);
}
if ((isset($_POST["save_component_graph_new"])) && (empty($_POST["graph_template_id"]))) {
header("Location: graphs.php?action=graph_edit&host_id=" . $_POST["host_id"] . "&new=1");
}elseif ((is_error_message()) || (empty($_POST["local_graph_id"])) || (isset($_POST["save_component_graph_diff"])) || ($_POST["graph_template_id"] != $_POST["_graph_template_id"]) || ($_POST["host_id"] != $_POST["_host_id"])) {
header("Location: graphs.php?action=graph_edit&id=" . (empty($local_graph_id) ? $_POST["local_graph_id"] : $local_graph_id) . (isset($_POST["host_id"]) ? "&host_id=" . $_POST["host_id"] : ""));
}else{
header("Location: graphs.php");
}
}
/* ------------------------
The "actions" function
------------------------ */
function form_actions() {
/* modify for multi user start */
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
$rows = db_fetch_assoc("
SELECT graph_local.id FROM graph_local
INNER JOIN host ON graph_local.host_id = host.id
INNER JOIN user_auth_perms ON host.id = user_auth_perms.item_id AND user_auth_perms.user_id = '" . $_SESSION["sess_user_id"] ."' AND user_auth_perms.type = '3'");
foreach ($rows as $row) {
$graphs[] = $row["id"];
}
}
/* modify for multi user end */
global $colors, $graph_actions;
/* if we are to save this form, instead of display it */
if (isset($_POST["selected_items"])) {
$selected_items = unserialize(stripslashes($_POST["selected_items"]));
/* modify for multi user start */
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
for ($i=0;($i<count($selected_items));$i++) {
if (!in_array($selected_items[$i], $graphs)) access_denied();
}
}
/* modify for multi user end */
if ($_POST["drp_action"] == "1") { /* delete */
if (!isset($_POST["delete_type"])) { $_POST["delete_type"] = 1; }
for ($i=0;($i<count($selected_items));$i++) {
/* ================= input validation ================= */
input_validate_input_number($selected_items[$i]);
/* ==================================================== */
}
switch ($_POST["delete_type"]) {
case '2': /* delete all data sources referenced by this graph */
$data_sources = array_rekey(db_fetch_assoc("SELECT data_template_data.local_data_id
FROM (data_template_rrd, data_template_data, graph_templates_item)
WHERE graph_templates_item.task_item_id=data_template_rrd.id
AND data_template_rrd.local_data_id=data_template_data.local_data_id
AND " . array_to_sql_or($selected_items, "graph_templates_item.local_graph_id") . "
AND data_template_data.local_data_id > 0"), "local_data_id", "local_data_id");
if (sizeof($data_sources)) {
api_data_source_remove_multi($data_sources);
api_plugin_hook_function('data_source_remove', $data_sources);
}
break;
}
api_graph_remove_multi($selected_items);
api_plugin_hook_function('graphs_remove', $selected_items);
}elseif ($_POST["drp_action"] == "2") { /* change graph template */
input_validate_input_number(get_request_var_post("graph_template_id"));
for ($i=0;($i<count($selected_items));$i++) {
/* ================= input validation ================= */
input_validate_input_number($selected_items[$i]);
/* ==================================================== */
change_graph_template($selected_items[$i], $_POST["graph_template_id"], true);
}
}elseif ($_POST["drp_action"] == "3") { /* duplicate */
for ($i=0;($i<count($selected_items));$i++) {
/* ================= input validation ================= */
input_validate_input_number($selected_items[$i]);
/* ==================================================== */
duplicate_graph($selected_items[$i], 0, $_POST["title_format"]);
}
}elseif ($_POST["drp_action"] == "4") { /* graph -> graph template */
for ($i=0;($i<count($selected_items));$i++) {
/* ================= input validation ================= */
input_validate_input_number($selected_items[$i]);
/* ==================================================== */
graph_to_graph_template($selected_items[$i], $_POST["title_format"]);
}
}elseif (preg_match("/^tr_([0-9]+)$/", $_POST["drp_action"], $matches)) { /* place on tree */
input_validate_input_number(get_request_var_post("tree_id"));
input_validate_input_number(get_request_var_post("tree_item_id"));
for ($i=0;($i<count($selected_items));$i++) {
/* ================= input validation ================= */
input_validate_input_number($selected_items[$i]);
/* ==================================================== */
api_tree_item_save(0, $_POST["tree_id"], TREE_ITEM_TYPE_GRAPH, $_POST["tree_item_id"], "", $selected_items[$i], read_graph_config_option("default_rra_id"), 0, 0, 0, false);
}
}elseif ($_POST["drp_action"] == "5") { /* change host */
input_validate_input_number(get_request_var_post("host_id"));
for ($i=0;($i<count($selected_items));$i++) {
/* ================= input validation ================= */
input_validate_input_number($selected_items[$i]);
/* ==================================================== */
db_execute("update graph_local set host_id=" . $_POST["host_id"] . " where id=" . $selected_items[$i]);
update_graph_title_cache($selected_items[$i]);
}
}elseif ($_POST["drp_action"] == "6") { /* reapply suggested naming */
for ($i=0;($i<count($selected_items));$i++) {
/* ================= input validation ================= */
input_validate_input_number($selected_items[$i]);
/* ==================================================== */
api_reapply_suggested_graph_title($selected_items[$i]);
update_graph_title_cache($selected_items[$i]);
}
}elseif ($_POST["drp_action"] == "7") { /* resize graphs */
input_validate_input_number(get_request_var_post("graph_width"));
input_validate_input_number(get_request_var_post("graph_height"));
for ($i=0;($i<count($selected_items));$i++) {
/* ================= input validation ================= */
input_validate_input_number($selected_items[$i]);
/* ==================================================== */
api_resize_graphs($selected_items[$i], $_POST["graph_width"], $_POST["graph_height"]);
}
} else {
api_plugin_hook_function('graphs_action_execute', $_POST['drp_action']);
}
header("Location: graphs.php");
exit;
}
/* setup some variables */
$graph_list = ""; $i = 0;
/* loop through each of the graphs selected on the previous page and get more info about them */
while (list($var,$val) = each($_POST)) {
if (preg_match("/^chk_([0-9]+)$/", $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
/* modify for multi user start */
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
if (!in_array($matches[1], $graphs)) access_denied();
}
/* modify for multi user end */
$graph_list .= "<li>" . get_graph_title($matches[1]) . "</li>";
$graph_array[$i] = $matches[1];
$i++;
}
}
include_once("./include/top_header.php");
/* add a list of tree names to the actions dropdown */
add_tree_names_to_actions_array();
html_start_box("<strong>" . $graph_actions{$_POST["drp_action"]} . "</strong>", "60%", $colors["header_panel"], "3", "center", "");
print "<form action='graphs.php' method='post'>\n";
if (isset($graph_array) && sizeof($graph_array)) {
if ($_POST["drp_action"] == "1") { /* delete */
$graphs = array();
/* find out which (if any) data sources are being used by this graph, so we can tell the user */
if (isset($graph_array) && sizeof($graph_array)) {
$data_sources = db_fetch_assoc("select
data_template_data.local_data_id,
data_template_data.name_cache
from (data_template_rrd,data_template_data,graph_templates_item)
where graph_templates_item.task_item_id=data_template_rrd.id
and data_template_rrd.local_data_id=data_template_data.local_data_id
and " . array_to_sql_or($graph_array, "graph_templates_item.local_graph_id") . "
and data_template_data.local_data_id > 0
group by data_template_data.local_data_id
order by data_template_data.name_cache");
}
print " <tr>
<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
<p>When you click \"Continue\", the following Graph(s) will be deleted. Please note, Data Source(s) should be deleted only if they are only used by these Graph(s)
and not others.</p>
<p><ul>$graph_list</ul></p>";
if (isset($data_sources) && sizeof($data_sources)) {
print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td class='textArea'><p class='textArea'>The following Data Source(s) are in use by these Graph(s):</p>\n";
print "<ul>";
foreach ($data_sources as $data_source) {
print "<li><strong>" . $data_source["name_cache"] . "</strong></li>\n";
}
print "</ul>";
print "<br>";
form_radio_button("delete_type", "1", "1", "Leave the Data Source(s) untouched.", "1"); print "<br>";
form_radio_button("delete_type", "1", "2", "Delete all <strong>Data Source(s)</strong> referenced by these Graph(s).", "1"); print "<br>";
print "</td></tr>";
}
print "
</td>
</tr>\n
";
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Delete Graph(s)'>";
}elseif ($_POST["drp_action"] == "2") { /* change graph template */
/* modify for multi user start */
$sql_where = "";
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
$sql_where = "WHERE graph_templates.name NOT LIKE '%@system'";
}
print " <tr>
<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
<p>Choose a Graph Template and click \"Continue\" to change the Graph Template for
the following Graph(s). Be aware that all warnings will be suppressed during the
conversion, so Graph data loss is possible.</p>
<p><ul>$graph_list</ul></p>
<p><strong>New Graph Template:</strong><br>"; form_dropdown("graph_template_id",db_fetch_assoc("select graph_templates.id,graph_templates.name from graph_templates $sql_where order by name"),"name","id","","","0"); print "</p>
</td>
</tr>\n
";
/* modify for multi user end */
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Change Graph Template'>";
}elseif ($_POST["drp_action"] == "3") { /* duplicate */
print " <tr>
<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
<p>When you click \"Continue\", the following Graph(s) will be duplicated. You can
optionally change the title format for the new Graph(s).</p>
<p><ul>$graph_list</ul></p>
<p><strong>Title Format:</strong><br>"; form_text_box("title_format", "<graph_title> (1)", "", "255", "30", "text"); print "</p>
</td>
</tr>\n
";
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Duplicate Graph(s)'>";
}elseif ($_POST["drp_action"] == "4") { /* graph -> graph template */
print " <tr>
<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
<p>When you click \"Continue\", the following Graph(s) will be converted into Graph Template(s).
You can optionally change the title format for the new Graph Template(s).</p>
<p><ul>$graph_list</ul></p>
<p><strong>Title Format:</strong><br>"; form_text_box("title_format", "<graph_title> Template", "", "255", "30", "text"); print "</p>
</td>
</tr>\n
";
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Convert to Graph Template'>";
}elseif (preg_match("/^tr_([0-9]+)$/", $_POST["drp_action"], $matches)) { /* place on tree */
print " <tr>
<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
<p>When you click \"Continue\", the following Graph(s) will be placed under the Tree Branch selected below.</p>
<p><ul>$graph_list</ul></p>
<p><strong>Destination Branch:</strong><br>"; grow_dropdown_tree($matches[1], "tree_item_id", "0"); print "</p>
</td>
</tr>\n
<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n
";
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Place Graph(s) on Tree'>";
}elseif ($_POST["drp_action"] == "5") { /* change host */
/* modify for multi user start */
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
$sql_join = "INNER JOIN user_auth_perms ON host.id = user_auth_perms.item_id AND user_auth_perms.user_id = '" . $_SESSION["sess_user_id"] ."' AND user_auth_perms.type = '3'";
}
print " <tr>
<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
<p>Choose a new Device for these Graph(s) and click \"Continue\"</p>
<p><ul>$graph_list</ul></p>
<p><strong>New Host:</strong><br>"; form_dropdown("host_id",db_fetch_assoc("select host.id,CONCAT_WS('',host.description,' (',host.hostname,')') as name from host order by description,hostname"),"name","id","","","0"); print "</p>
</td>
</tr>\n
";
/* modify for multi user end */
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Change Graph(s) Associated Device'>";
}elseif ($_POST["drp_action"] == "6") { /* reapply suggested naming to host */
print " <tr>
<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
<p>When you click \"Continue\", the following Graph(s) will have thier suggested naming convensions
recalculated and applied to the Graph(s).</p>
<p><ul>$graph_list</ul></p>
</td>
</tr>\n
";
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Reapply Suggested Naming to Graph(s)'>";
}elseif ($_POST["drp_action"] == "7") { /* resize graphs */
print " <tr>
<td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
<p>When you click \"Continue\", the following Graph(s) will be resized per your specifications.</p>
<p><ul>$graph_list</ul></p>
<p><strong>Graph Height:</strong><br>"; form_text_box("graph_height", "", "", "255", "30", "text"); print "</p>
<p><strong>Graph Width:</strong><br>"; form_text_box("graph_width", "", "", "255", "30", "text"); print "</p>
</td>
</tr>\n
";
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Resize Selected Graph(s)'>";
} else {
$save['drp_action'] = $_POST['drp_action'];
$save['graph_list'] = $graph_list;
$save['graph_array'] = (isset($graph_array) ? $graph_array : array());
api_plugin_hook_function('graphs_action_prepare', $save);
$save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue'>";
}
}else{
print "<tr><td bgcolor='#" . $colors["form_alternate1"]. "'><span class='textError'>You must select at least one graph.</span></td></tr>\n";
$save_html = "<input type='button' value='Return' onClick='window.history.back()'>";
}
print " <tr>
<td align='right' bgcolor='#eaeaea'>
<input type='hidden' name='action' value='actions'>
<input type='hidden' name='selected_items' value='" . (isset($graph_array) ? serialize($graph_array) : '') . "'>
<input type='hidden' name='drp_action' value='" . $_POST["drp_action"] . "'>
$save_html
</td>
</tr>
";
html_end_box();
include_once("./include/bottom_footer.php");
}
/* -----------------------
item - Graph Items
----------------------- */
function item() {
global $colors, $consolidation_functions, $graph_item_types, $struct_graph_item;
/* ================= input validation ================= */
input_validate_input_number(get_request_var("id"));
/* ==================================================== */
if (empty($_GET["id"])) {
$template_item_list = array();
$header_label = "[new]";
}else{
$template_item_list = db_fetch_assoc("select
graph_templates_item.id,
graph_templates_item.text_format,
graph_templates_item.value,
graph_templates_item.hard_return,
graph_templates_item.graph_type_id,
graph_templates_item.consolidation_function_id,
data_template_rrd.data_source_name,
cdef.name as cdef_name,
colors.hex
from graph_templates_item
left join data_template_rrd on (graph_templates_item.task_item_id=data_template_rrd.id)
left join data_local on (data_template_rrd.local_data_id=data_local.id)
left join data_template_data on (data_local.id=data_template_data.local_data_id)
left join cdef on (cdef_id=cdef.id)
left join colors on (color_id=colors.id)
where graph_templates_item.local_graph_id=" . $_GET["id"] . "
order by graph_templates_item.sequence");
$host_id = db_fetch_cell("select host_id from graph_local where id=" . $_GET["id"]);
$header_label = "[edit: " . htmlspecialchars(get_graph_title($_GET["id"])) . "]";
}
$graph_template_id = db_fetch_cell("select graph_template_id from graph_local where id=" . $_GET["id"]);
if (empty($graph_template_id)) {
$add_text = "graphs_items.php?action=item_edit&local_graph_id=" . $_GET["id"] . "&host_id=$host_id";
}else{
$add_text = "";
}
html_start_box("<strong>Graph Items</strong> $header_label", "100%", $colors["header"], "3", "center", $add_text);
draw_graph_items_list($template_item_list, "graphs_items.php", "local_graph_id=" . $_GET["id"], (empty($graph_template_id) ? false : true));
html_end_box();
}
/* ------------------------------------
graph - Graphs
------------------------------------ */
function graph_diff() {
global $colors, $struct_graph_item, $graph_item_types, $consolidation_functions;
/* ================= input validation ================= */
input_validate_input_number(get_request_var("id"));
input_validate_input_number(get_request_var("graph_template_id"));
/* ==================================================== */
$template_query = "select
graph_templates_item.id,
graph_templates_item.text_format,
graph_templates_item.value,
graph_templates_item.hard_return,
graph_templates_item.consolidation_function_id,
graph_templates_item.graph_type_id,
CONCAT_WS(' - ',data_template_data.name,data_template_rrd.data_source_name) as task_item_id,
cdef.name as cdef_id,
colors.hex as color_id
from graph_templates_item
left join data_template_rrd on (graph_templates_item.task_item_id=data_template_rrd.id)
left join data_local on (data_template_rrd.local_data_id=data_local.id)
left join data_template_data on (data_local.id=data_template_data.local_data_id)
left join cdef on (cdef_id=cdef.id)
left join colors on (color_id=colors.id)";
/* first, get information about the graph template as that's what we're going to model this
graph after */
$graph_template_items = db_fetch_assoc("
$template_query
where graph_templates_item.graph_template_id=" . $_GET["graph_template_id"] . "
and graph_templates_item.local_graph_id=0
order by graph_templates_item.sequence");
/* next, get information about the current graph so we can make the appropriate comparisons */
$graph_items = db_fetch_assoc("
$template_query
where graph_templates_item.local_graph_id=" . $_GET["id"] . "
order by graph_templates_item.sequence");
$graph_template_inputs = db_fetch_assoc("select
graph_template_input.column_name,
graph_template_input_defs.graph_template_item_id
from (graph_template_input,graph_template_input_defs)
where graph_template_input.id=graph_template_input_defs.graph_template_input_id
and graph_template_input.graph_template_id=" . $_GET["graph_template_id"]);
/* ok, we want to loop through the array with the GREATEST number of items so we don't have to worry
about tacking items on the end */
if (sizeof($graph_template_items) > sizeof($graph_items)) {
$items = $graph_template_items;
}else{
$items = $graph_items;
}
?>
<table style="background-color: #f5f5f5; border: 1px solid #aaaaaa;" width="100%" align="center">
<tr>
<td class="textArea">
The template you have selected requires some changes to be made to the structure of
your graph. Below is a preview of your graph along with changes that need to be completed
as shown in the left-hand column.
</td>
</tr>
</table>
<br>
<?php
html_start_box("<strong>Graph Preview</strong>", "100%", $colors["header"], "3", "center", "");
$graph_item_actions = array("normal" => "", "add" => "+", "delete" => "-");
$group_counter = 0; $i = 0; $mode = "normal"; $_graph_type_name = "";
if (sizeof($items) > 0) {
foreach ($items as $item) {
reset($struct_graph_item);
/* graph grouping display logic */
$bold_this_row = false; $use_custom_row_color = false; $action_css = ""; unset($graph_preview_item_values);
if ((sizeof($graph_template_items) > sizeof($graph_items)) && ($i >= sizeof($graph_items))) {
$mode = "add";
$user_message = "When you click save, the items marked with a '<strong>+</strong>' will be added <strong>(Recommended)</strong>.";
}elseif ((sizeof($graph_template_items) < sizeof($graph_items)) && ($i >= sizeof($graph_template_items))) {
$mode = "delete";
$user_message = "When you click save, the items marked with a '<strong>-</strong>' will be removed <strong>(Recommended)</strong>.";
}
/* here is the fun meshing part. first we check the graph template to see if there is an input
for each field of this row. if there is, we revert to the value stored in the graph, if not
we revert to the value stored in the template. got that? ;) */
for ($j=0; ($j < count($graph_template_inputs)); $j++) {
if ($graph_template_inputs[$j]["graph_template_item_id"] == (isset($graph_template_items[$i]["id"]) ? $graph_template_items[$i]["id"] : "")) {
/* if we find out that there is an "input" covering this field/item, use the
value from the graph, not the template */
$graph_item_field_name = (isset($graph_template_inputs[$j]["column_name"]) ? $graph_template_inputs[$j]["column_name"] : "");
$graph_preview_item_values[$graph_item_field_name] = (isset($graph_items[$i][$graph_item_field_name]) ? $graph_items[$i][$graph_item_field_name] : "");
}
}
/* go back through each graph field and find out which ones haven't been covered by the
"inputs" above. for each one, use the value from the template */
while (list($field_name, $field_array) = each($struct_graph_item)) {
if ($mode == "delete") {
$graph_preview_item_values[$field_name] = (isset($graph_items[$i][$field_name]) ? $graph_items[$i][$field_name] : "");
}elseif (!isset($graph_preview_item_values[$field_name])) {
$graph_preview_item_values[$field_name] = (isset($graph_template_items[$i][$field_name]) ? $graph_template_items[$i][$field_name] : "");
}
}
/* "prepare" array values */
$consolidation_function_id = $graph_preview_item_values["consolidation_function_id"];
$graph_type_id = $graph_preview_item_values["graph_type_id"];
/* color logic */
if (($graph_item_types[$graph_type_id] != "GPRINT") && ($graph_item_types[$graph_type_id] != $_graph_type_name)) {
$bold_this_row = true; $use_custom_row_color = true; $hard_return = "";
if ($group_counter % 2 == 0) {
$alternate_color_1 = "EEEEEE";
$alternate_color_2 = "EEEEEE";
$custom_row_color = "D5D5D5";
}else{
$alternate_color_1 = $colors["alternate"];
$alternate_color_2 = $colors["alternate"];
$custom_row_color = "D2D6E7";
}
$group_counter++;
}
$_graph_type_name = $graph_item_types[$graph_type_id];
/* alternating row colors */
if ($use_custom_row_color == false) {
if ($i % 2 == 0) {
$action_column_color = $alternate_color_1;
}else{
$action_column_color = $alternate_color_2;
}
}else{
$action_column_color = $custom_row_color;
}
print "<tr bgcolor='#$action_column_color'>"; $i++;
/* make the left-hand column blue or red depending on if "add"/"remove" mode is set */
if ($mode == "add") {
$action_column_color = $colors["header"];
$action_css = "";
}elseif ($mode == "delete") {
$action_column_color = "C63636";
$action_css = "text-decoration: line-through;";
}
if ($bold_this_row == true) {
$action_css .= " font-weight:bold;";
}
/* draw the TD that shows the user whether we are going to: KEEP, ADD, or DROP the item */
print "<td width='1%' bgcolor='#$action_column_color' style='font-weight: bold; color: white;'>" . $graph_item_actions[$mode] . "</td>";
print "<td style='$action_css'><strong>Item # " . $i . "</strong></td>\n";
if (empty($graph_preview_item_values["task_item_id"])) { $graph_preview_item_values["task_item_id"] = "No Task"; }
switch (true) {
case preg_match("/(AREA|STACK|GPRINT|LINE[123])/", $_graph_type_name):
$matrix_title = "(" . $graph_preview_item_values["task_item_id"] . "): " . $graph_preview_item_values["text_format"];
break;
case preg_match("/(HRULE|VRULE)/", $_graph_type_name):
$matrix_title = "HRULE: " . $graph_preview_item_values["value"];
break;
case preg_match("/(COMMENT)/", $_graph_type_name):
$matrix_title = "COMMENT: " . $graph_preview_item_values["text_format"];
break;
}
/* use the cdef name (if in use) if all else fails */
if ($matrix_title == "") {
if ($graph_preview_item_values["cdef_id"] != "") {
$matrix_title .= "CDEF: " . $graph_preview_item_values["cdef_id"];
}
}
if ($graph_preview_item_values["hard_return"] == "on") {
$hard_return = "<strong><font color=\"#FF0000\"><HR></font></strong>";
}
print "<td style='$action_css'>" . htmlspecialchars($matrix_title) . $hard_return . "</td>\n";
print "<td style='$action_css'>" . $graph_item_types{$graph_preview_item_values["graph_type_id"]} . "</td>\n";
print "<td style='$action_css'>" . $consolidation_functions{$graph_preview_item_values["consolidation_function_id"]} . "</td>\n";
print "<td" . ((!empty($graph_preview_item_values["color_id"])) ? " bgcolor='#" . $graph_preview_item_values["color_id"] . "'" : "") . " width='1%'> </td>\n";
print "<td style='$action_css'>" . $graph_preview_item_values["color_id"] . "</td>\n";
print "</tr>";
}
}else{
form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],0); ?>
<td colspan="7">
<em>No Items</em>
</td>
</tr><?php
}
html_end_box();
?>
<form action="graphs.php" method="post">
<table style="background-color: #f5f5f5; border: 1px solid #aaaaaa;" width="100%" align="center">
<tr>
<td class="textArea">
<input type='radio' name='type' value='1' checked> <?php print $user_message;?><br>
<input type='radio' name='type' value='2'> When you click save, the graph items will remain untouched (could cause inconsistencies).
</td>
</tr>
</table>
<br>
<input type="hidden" name="action" value="save">
<input type="hidden" name="save_component_graph_diff" value="1">
<input type="hidden" name="local_graph_id" value="<?php print $_GET["id"];?>">
<input type="hidden" name="graph_template_id" value="<?php print $_GET["graph_template_id"];?>">
<?php
form_save_button("graphs.php?action=graph_edit&id=" . $_GET["id"]);
}
function graph_edit() {
global $colors, $struct_graph, $image_types, $consolidation_functions, $graph_item_types, $struct_graph_item;
/* ================= input validation ================= */
input_validate_input_number(get_request_var("id"));
/* ==================================================== */
/* modify for multi user start */
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
// graph add
if ($_GET["id"] == "") {
if ($_GET["host_id"] != -1) {
input_validate_input_number(get_request_var("host_id"));
if (!check_host($_GET["host_id"])) access_denied();
}
// graph edit
} else {
$permission = check_graph($_GET["id"]);
if ($permission != GRAPH_PRIVATE && $permission != GRAPH_PRIVATE + GRAPH_PUBLIC) access_denied();
}
}
/* modify for multi user end */
$use_graph_template = true;
if (!empty($_GET["id"])) {
$local_graph_template_graph_id = db_fetch_cell("select local_graph_template_graph_id from graph_templates_graph where local_graph_id=" . $_GET["id"]);
$graphs = db_fetch_row("select * from graph_templates_graph where local_graph_id=" . $_GET["id"]);
$graphs_template = db_fetch_row("select * from graph_templates_graph where id=$local_graph_template_graph_id");
$host_id = db_fetch_cell("select host_id from graph_local where id=" . $_GET["id"]);
$header_label = "[edit: " . htmlspecialchars(get_graph_title($_GET["id"])) . "]";
if ($graphs["graph_template_id"] == "0") {
$use_graph_template = false;
}
}else{
$header_label = "[new]";
$use_graph_template = false;
}
/* modify for multi user start */
if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) unset($_GET["debug"]);
/* modify for multi user end */
/* handle debug mode */
if (isset($_GET["debug"])) {
if ($_GET["debug"] == "0") {
kill_session_var("graph_debug_mode");
}elseif ($_GET["debug"] == "1") {
$_SESSION["graph_debug_mode"] = true;
}
}
if (!empty($_GET["id"])) {
?>
<table width="100%" align="center">
<tr>
<td class="textInfo" colspan="2" valign="top">
<?php print htmlspecialchars(get_graph_title($_GET["id"]));?>
</td>
<td class="textInfo" align="right" valign="top">
<?php /* modify for multi user start */ if ($_SESSION["permission"] == ACCESS_ADMINISTRATOR) { ?>
<span style="color: #c16921;">*<a href='<?php print htmlspecialchars("graphs.php?action=graph_edit&id=" . (isset($_GET["id"]) ? $_GET["id"] : "0") . "&debug=" . (isset($_SESSION["graph_debug_mode"]) ? "0" : "1"));?>'>Turn <strong><?php print (isset($_SESSION["graph_debug_mode"]) ? "Off" : "On");?></strong> Graph Debug Mode.</a></span><br>
<?php }
if (!empty($graphs["graph_template_id"]) && $_SESSION["permission"] == ACCESS_ADMINISTRATOR) {
?><span style="color: #c16921;">*<a href='<?php print htmlspecialchars("graph_templates.php?action=template_edit&id=" . (isset($graphs["graph_template_id"]) ? $graphs["graph_template_id"] : "0"));?>'>Edit Graph Template.</a></span><br><?php
}
/* modify for multi user end */
if (!empty($_GET["host_id"]) || !empty($host_id)) {
?><span style="color: #c16921;">*<a href='<?php print htmlspecialchars("host.php?action=edit&id=" . (isset($_GET["host_id"]) ? $_GET["host_id"] : $host_id));?>'>Edit Host.</a></span><br><?php
}
?>
</td>
</tr>
</table>
<br>
<?php
}
html_start_box("<strong>Graph Template Selection</strong> $header_label", "100%", $colors["header"], "3", "center", "");
$form_array = array(
"graph_template_id" => array(
"method" => "drop_sql",
"friendly_name" => "Selected Graph Template",
"description" => "Choose a graph template to apply to this graph. Please note that graph data may be lost if you change the graph template after one is already applied.",
"value" => (isset($graphs) ? $graphs["graph_template_id"] : "0"),
"none_value" => "None",
"sql" => "select graph_templates.id,graph_templates.name from graph_templates order by name"
),
"host_id" => array(
"method" => "drop_sql",
"friendly_name" => "Host",
"description" => "Choose the host that this graph belongs to.",
"value" => (isset($_GET["host_id"]) ? $_GET["host_id"] : $host_id),
"none_value" => "None",
"sql" => "select id,CONCAT_WS('',description,' (',hostname,')') as name from host order by description,hostname"
),
"graph_template_graph_id" => array(
"method" => "hidden",
"value" => (isset($graphs) ? $graphs["id"] : "0")
),
"local_graph_id" => array(
"method" => "hidden",
"value" => (isset($graphs) ? $graphs["local_graph_id"] : "0")