forked from Cacti/plugin_thold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thold_webapi.php
1023 lines (853 loc) · 34.6 KB
/
thold_webapi.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/ |
+-------------------------------------------------------------------------+
*/
function thold_add_graphs_action_execute() {
global $config;
include_once($config['base_path'] . '/plugins/thold/thold_functions.php');
$host_id = get_filter_request_var('host_id');
$local_graph_id = get_filter_request_var('local_graph_id');
$thold_template_id = get_filter_request_var('thold_template_id');
$message = '';
$template = db_fetch_row_prepared('SELECT *
FROM thold_template
WHERE id = ?',
array($thold_template_id));
$temp = db_fetch_row_prepared('SELECT dtr.*
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON gti.task_item_id=dtr.id
LEFT JOIN graph_local AS gl
ON gl.id=gti.local_graph_id
WHERE gl.id = ?
LIMIT 1' ,
array($local_graph_id));
$data_template_id = $temp['data_template_id'];
$local_data_id = $temp['local_data_id'];
$data_source = db_fetch_row_prepared('SELECT *
FROM data_local
WHERE id = ?',
array($local_data_id));
$data_template_id = $data_source['data_template_id'];
/* allow duplicate thresholds, but only from differing templates */
$existing = db_fetch_assoc_prepared('SELECT id
FROM thold_data
WHERE local_data_id = ?
AND data_template_rrd_id = ?
AND thold_template_id = ?
AND template_enabled = "on"',
array($local_data_id, $data_template_id, $template['id']));
if (!cacti_sizeof($existing) && cacti_sizeof($template)) {
if ($local_graph_id) {
$rrd_id = db_fetch_cell_prepared('SELECT id
FROM data_template_rrd
WHERE local_data_id = ?
ORDER BY id LIMIT 1',
array($local_data_id));
$graph_template_id = db_fetch_cell_prepared('SELECT graph_template_id
FROM graph_templates_item
WHERE task_item_id = ?
AND local_graph_id = ?',
array($rrd_id, $local_graph_id));
$data_source_name = $template['data_source_name'];
$save = array();
$save['host_id'] = $data_source['host_id'];
$save['local_data_id'] = $local_data_id;
$save['local_graph_id'] = $local_graph_id;
$save['data_template_id'] = $data_template_id;
$save['graph_template_id'] = $graph_template_id;
$save = thold_create_thold_save_from_template($save, $template);
$save['name_cache'] = thold_expand_string($save, $save['name']);
$rrdlist = db_fetch_assoc_prepared('SELECT id, data_input_field_id
FROM data_template_rrd
WHERE local_data_id = ?
AND data_source_name = ?',
array($local_data_id, $data_source_name));
if (cacti_sizeof($rrdlist)) {
foreach ($rrdlist as $rrdrow) {
$data_rrd_id = $rrdrow['id'];
$save['data_template_rrd_id'] = $data_rrd_id;
$existing = db_fetch_assoc_prepared("SELECT id
FROM thold_data
WHERE local_data_id = ?
AND data_template_rrd_id = ?
AND thold_template_id = ?
AND template_enabled='on'",
array($local_data_id, $data_rrd_id, $template['id']));
if (count($existing) == 0) {
$save['id'] = 0;
$id = sql_save($save, 'thold_data');
if ($id) {
thold_template_update_threshold($id, $save['thold_template_id']);
$tname = db_fetch_cell_prepared('SELECT name
FROM data_template
WHERE id = ?',
array($data_template_id));
$name = $data_source_name;
if ($rrdrow['data_input_field_id'] != 0) {
$name = db_fetch_cell_prepared('SELECT name
FROM data_input_fields
WHERE id = ?',
array($rrdrow['data_input_field_id']));
}
plugin_thold_log_changes($id, 'created', " $tname [$name]");
$message .= "Created Threshold for the Graph '$tname' using the Data Source '$name'<br>";
}
}
}
}
}
}
if (strlen($message)) {
thold_raise_message($message, MESSAGE_LEVEL_INFO);
} else {
thold_raise_message(__('Threshold(s) Already Exists - No Thresholds Created', 'thold'), MESSAGE_LEVEL_INFO);
}
if (isset($_SESSION['graph_return'])) {
$return_to = $_SESSION['graph_return'];
unset($_SESSION['graph_return']);
kill_session_var('graph_return');
header('Location: ' . $return_to . (strpos($return_to, '?') !== false ? '&':'?') . 'header=false');
} else {
header('Location:' . $config['url_path'] . 'plugins/thold/thold.php?header=false');
}
}
function thold_add_graphs_action_prepare() {
global $config;
top_header();
thold_wizard();
bottom_footer();
}
function thold_add_graphs_action_array($action) {
$action['plugin_thold_create'] = __('Create Threshold from Template', 'thold');
return $action;
}
function thold_wizard() {
global $config;
include_once($config['base_path'] . '/lib/html_graph.php');
if (isset_request_var('usetemplate') && isset_request_var('local_graph_id')) {
$local_graph_id = get_filter_request_var('local_graph_id');
$graph_local = db_fetch_row_prepared('SELECT *
FROM graph_local
WHERE id = ?',
array($local_graph_id));
if (cacti_sizeof($graph_local)) {
if ($graph_local['snmp_query_id'] > 0) {
$graph_template_id = $graph_local['snmp_query_graph_id'];
} else {
$graph_template_id = $graph_local['graph_template_id'];
}
$host_id = $graph_local['host_id'];
$data_query_id = $graph_local['snmp_query_id'];
$snmp_index = $graph_local['snmp_index'];
$data_source_info = db_fetch_cell_prepared('SELECT GROUP_CONCAT(DISTINCT dl.data_template_id) AS ids
FROM data_local AS dl
INNER JOIN data_template_rrd AS dtr
ON dtr.local_data_id = dl.id
INNER JOIN graph_templates_item AS gti
ON dtr.id = gti.task_item_id
LEFT JOIN (
SELECT td.id, td.local_data_id, td.data_source_name, data_template_rrd_id
FROM thold_data AS td
INNER JOIN thold_template AS tt
ON tt.id = td.thold_template_id
WHERE td.host_id = ?
) AS td
ON td.local_data_id = dl.id
AND td.data_source_name = dtr.data_source_name
AND td.data_template_rrd_id = dtr.id
WHERE gti.local_graph_id = ?
AND td.id IS NULL',
array($host_id, $local_graph_id));
if ($data_source_info != '') {
$templates = db_fetch_assoc('SELECT id, name
FROM thold_template
WHERE data_template_id IN(' . $data_source_info . ')
AND thold_enabled = "on"');
if (cacti_sizeof($templates)) {
$thold_template_id = $templates[0]['id'];
$type_id = 'template';
} else {
$thold_template_id = '';
$type_id = 'thold';
}
$parts = explode(',', $data_source_info);
$data_template_id = $parts[0];
} else {
$thold_template_id = '';
$data_template_id = '';
$type_id = 'thold';
$templates = array();
}
$data_template_rrd_id = '';
set_request_var('type_id', $type_id);
set_request_var('thold_template_id', $thold_template_id);
set_request_var('my_host_id', $host_id);
set_request_var('local_graph_id', $local_graph_id);
set_request_var('data_template_rrd_id', $data_template_rrd_id);
set_request_var('data_template_id', $data_template_id);
set_request_var('data_query_id', $data_query_id);
set_request_var('snmp_index', $snmp_index);
} else {
return false;
}
} else {
$type_id = get_nfilter_request_var('type_id');
$thold_template_id = get_filter_request_var('thold_template_id');
$graph_template_id = get_filter_request_var('graph_template_id');
$host_id = get_filter_request_var('my_host_id');
$local_graph_id = get_filter_request_var('local_graph_id');
$data_template_rrd_id = get_filter_request_var('data_template_rrd_id');
$data_template_id = get_filter_request_var('data_template_id');
$data_query_id = get_filter_request_var('data_query_id');
$snmp_index = get_nfilter_request_var('snmp_index');
$templates = db_fetch_assoc('SELECT id, name
FROM thold_template
WHERE thold_enabled="on"
ORDER BY name');
}
$hosts = get_allowed_devices();
$show_go = false;
$form_array = array();
if ($type_id == '') {
$message = __('Threshold Creation Wizard [ Select a Threshold Type ]', 'thold');
} elseif ($type_id == 'template') {
if ($thold_template_id != '' && $graph_template_id != '' && $host_id != '' && $snmp_index != '') {
$show_go = true;
$message = '<tr><td class="center">' . __('Threshold Creation Wizard [ Enter Custom Data and press \'Create\' to Create your Threshold and Graph ]', 'thold');
} elseif ($thold_template_id != '' && $graph_template_id != '' && $host_id != '') {
$message = __('Threshold Creation Wizard [ Select Available Data Query Rows ]', 'thold');
} elseif ($thold_template_id != '' && $graph_template_id != '') {
$message = __('Threshold Creation Wizard [ Select a Device ]', 'thold');
} elseif ($thold_template_id != '') {
$message = __('Threshold Creation Wizard [ Select a Graph Template ]', 'thold');
} else {
$message = __('Threshold Creation Wizard [ Select a Threshold Template ]', 'thold');
}
} else {
if ($host_id != '' && $local_graph_id != '' && $data_template_rrd_id != '') {
$show_go = true;
$message = __('Threshold Creation Wizard [ Press \'Create\' to Create your Threshold ]', 'thold') . '</td></tr>';
} elseif ($host_id != '' && $local_graph_id != '') {
$message = __('Threshold Creation Wizard [ Select a Data Source ]', 'thold') . '</td></tr>';
} elseif ($host_id != '') {
$message = __('Threshold Creation Wizard [ Select a Graph ]', 'thold') . '</td></tr>';
} else {
$message = __('Threshold Creation Wizard [ Select a Device ]', 'thold') . '</td></tr>';
}
}
/* display the type dropdown */
$form_array['spacer'] = array(
'method' => 'spacer',
'friendly_name' => __('Threshold Creation Criteria', 'thold'),
);
$form_array['type_id'] = array(
'method' => 'drop_array',
'friendly_name' => __('Create Type', 'thold'),
'description' => __('Select a Threshold Type to use for creating this Threshold.', 'thold'),
'on_change' => 'applyTholdFilter()',
'value' => $type_id,
'array' => array(
'none' => __('Select a Threshold Type', 'thold'),
'thold' => __('Non Templated', 'thold'),
)
);
if (cacti_sizeof($templates)) {
$form_array['type_id']['array']['template'] = __('Threshold Template', 'thold');
}
if ($type_id == 'template') {
$form_array['thold_template_id'] = array(
'method' => 'drop_sql',
'friendly_name' => __('Threshold Template', 'thold'),
'description' => __('Select a Threshold Template that the Graph and Threshold will be based upon.', 'thold'),
'on_change' => 'applyTholdFilter()',
'value' => $thold_template_id,
'sql' => 'SELECT id, name FROM thold_template WHERE thold_enabled="on" ORDER BY name',
'none_value' => __('Select a Threshold Template', 'thold')
);
$host_ids = array();
$in_sql = array();
if ($thold_template_id != '') {
/* display the host dropdown */
$graph_templates = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT gt.id, gt.name
FROM graph_templates AS gt
INNER JOIN graph_templates_item AS gti
ON gt.id=gti.graph_template_id
AND local_graph_id=0
INNER JOIN data_template_rrd AS dtr
ON dtr.id=task_item_id
INNER JOIN thold_template AS tt
ON tt.data_template_id=dtr.data_template_id
AND tt.id = ?',
array($thold_template_id)),
'id', 'name'
);
// Limit ths hosts to only hosts that either have a graph template
// Listed as multiple, or do not have a threshold created
// Using the Graph Template listed
// If the Graph Template is associated with a Data Query
// make sure that your get all the Data Query based Graph Templates
if (cacti_sizeof($graph_templates)) {
$new_templates = array();
$hql = '';
$data_query_id = db_fetch_cell('SELECT snmp_query_id
FROM snmp_query_graph
WHERE graph_template_id IN (' . implode(', ', array_keys($graph_templates)) . ')');
if ($data_query_id) {
$data_template_id = db_fetch_cell_prepared('SELECT data_template_id
FROM thold_template
WHERE id = ?',
array($thold_template_id));
$templates = db_fetch_assoc_prepared('SELECT DISTINCT sqg.id, sqg.name
FROM snmp_query_graph AS sqg
INNER JOIN graph_templates_item AS gti
ON sqg.graph_template_id = gti.graph_template_id
AND gti.local_graph_id = 0
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id=dtr.id
AND dtr.local_data_id = 0
WHERE snmp_query_id IN(
SELECT snmp_query_id
FROM snmp_query_graph
WHERE graph_template_id IN (' . implode(', ', array_keys($graph_templates)) . ')
)
AND dtr.data_template_id = ?',
array($data_template_id));
if (cacti_sizeof($templates)) {
$new_templates = $templates;
foreach ($templates as $t) {
$in_sql[$t['id']] = $t['id'];
}
}
$host_templates = array_rekey(
db_fetch_assoc_prepared('SELECT host_template_id AS id
FROM host_template_snmp_query
WHERE snmp_query_id = ?',
array($data_query_id)),
'id', 'id');
if (cacti_sizeof($host_templates)) {
$hql = 'h.host_template_id IN(' . implode(', ', $host_templates) . ')';
} else {
$hosts_ids = array();
}
} else {
$host_template_ids = array_rekey(
db_fetch_assoc_prepared('SELECT host_template_id
FROM host_template_graph
WHERE graph_template_id = ?',
array($graph_template_id)),
'host_template_id', 'host_template_id');
if (cacti_sizeof($host_template_ids)) {
$hiql = ' WHERE h.host_template_id IN (' . implode(', ', $host_template_ids) . ')';
} else {
$hiql = ' WHERE 0 = 1';
}
foreach ($graph_templates as $key => $name) {
$new_templates[] = array('id' => $key, 'name' => $name);
$in_sql[$key] = $key;
}
$host_ids = array_rekey(db_fetch_assoc('SELECT DISTINCT rs.id
FROM (
SELECT h.id, gt.id AS gti, gt.multiple, h.host_template_id
FROM host AS h, graph_templates AS gt
' . $hiql . '
) AS rs
LEFT JOIN graph_local AS gl
ON gl.graph_template_id=rs.gti
AND gl.host_id=rs.id
WHERE (gti IN(' . implode(', ', $in_sql) . ')
AND host_id IS NULL)
OR rs.multiple = "on"'), 'id', 'id');
}
} else {
$host_ids = array('0');
}
if (!cacti_sizeof($in_sql)) {
$in_sql[] = 'NULL';
}
if (!$data_query_id) {
$gr_sql = 'SELECT gt.id, gt.name
FROM graph_templates AS gt
WHERE id IN (' . implode(', ', $in_sql) . ')
ORDER BY name';
} else {
$gr_sql = 'SELECT id, name
FROM snmp_query_graph AS gt
WHERE id IN (' . implode(', ', $in_sql) . ')
ORDER BY name';
}
$form_array['graph_template_id'] = array(
'method' => 'drop_sql',
'friendly_name' => __('Graph Template', 'thold'),
'description' => __('Select a Graph Template to use for the Graph to be created.', 'thold'),
'on_change' => 'applyTholdFilter()',
'value' => $graph_template_id,
'sql' => $gr_sql,
'none_value' => __('Select a Graph Template', 'thold')
);
}
if ($graph_template_id > 0) {
if (isset($hql) && $hql != '') {
$sql = "SELECT id, description AS name FROM host AS h WHERE $hql ORDER BY description";
} elseif (cacti_sizeof($host_ids)) {
$sql = 'SELECT id, description AS name FROM host AS h WHERE h.id IN (' . implode(', ', $host_ids) . ') ORDER BY description';
} else {
$sql = "SELECT id, description AS name FROM host AS h ORDER BY description";
}
$form_array['my_host_id'] = array(
'method' => 'drop_sql',
'friendly_name' => __('Device', 'thold'),
'description' => __('Select a Device to use for the Threshold and Graph to be created.', 'thold'),
'on_change' => 'applyTholdFilter()',
'value' => $host_id,
'sql' => $sql,
'none_value' => __('Select a Device', 'thold')
);
}
if ($host_id > 0 && $data_query_id > 0) {
$available_items = array();
$sort_field = db_fetch_cell_prepared('SELECT sort_field
FROM host_snmp_query
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $data_query_id));
if ($sort_field != '') {
$available_items = array('noneselected' => __('Select and Available Item', 'thold'));
$available_items += array_rekey(
db_fetch_assoc_prepared('SELECT hsc.snmp_index AS id, hsc.field_value AS name
FROM host_snmp_cache AS hsc
LEFT JOIN (
SELECT *
FROM graph_local AS gl
WHERE snmp_query_id = ?
AND host_id = ?
AND snmp_query_graph_id = ?
) AS gl
ON hsc.host_id = gl.host_id
AND hsc.snmp_query_id = gl.snmp_query_id
AND hsc.snmp_index = gl.snmp_index
WHERE gl.snmp_index IS NULL
AND hsc.host_id = ?
AND hsc.snmp_query_id = ?
AND field_name = ?',
array(
$data_query_id,
$host_id,
$graph_template_id,
$host_id,
$data_query_id,
$sort_field
)
),
'id', 'name'
);
}
$form_array['snmp_index'] = array(
'method' => 'drop_array',
'friendly_name' => __('Data Query Item', 'thold'),
'description' => __('Select the applicable row from the Data Query for the Graph and Threshold.', 'thold'),
'on_change' => 'applyTholdFilter()',
'value' => $snmp_index,
'array' => $available_items,
'default' => ''
);
} else {
$local_graph_id = 0;
$data_template_rrd_id = 0;
}
if ($data_query_id) {
$form_array['data_query_id'] = array(
'method' => 'hidden',
'value' => $data_query_id
);
$form_array['data_template_id'] = array(
'method' => 'hidden',
'value' => $data_template_id
);
} else {
$form_array['data_query_id'] = array(
'method' => 'hidden',
'value' => '0'
);
$form_array['data_template_id'] = array(
'method' => 'hidden',
'value' => '0'
);
}
top_header();
form_start('thold.php?action=save', 'chk');
html_start_box($message, '100%', false, '3', 'center', '');
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => $form_array
)
);
html_end_box(false);
html_start_box(__('Creation Notes', 'thold'), '100%', false, '3', 'center', '');
print '<tr><td><p style="padding:0px 5px"><b><font color=\'red\'>' . __('Important Note:', 'thold') .' </font></b>';
print __('This Threshold will be Templated. When using the Threshold Template option, you will be prompted for a Threshold Template, Graph Template, Device and possibly Data Query Item information before receiving the \'Create\' prompt at which time, if any overridable Graph or Data Source information is allowed at the Graph and Data Source Template level, you will be prompted for it. Then, by pressing the \'Create\' button, both the Graph and Threshold will be created simultaneously.', 'thold') . '</p></td></tr>';
html_end_box(false);
if ($data_query_id > 0) {
if ($snmp_index != 'noneselected' && $snmp_index != '' && $host_id > 0 && $thold_template_id > 0 && $graph_template_id > 0) {
$host_template_id = db_fetch_cell_prepared('SELECT host_template_id
FROM host
WHERE id = ?',
array($host_id));
$selected_graphs['sg'][$data_query_id][$graph_template_id][encode_data_query_index($snmp_index)] = true;
thold_graph_new_graphs('thold.php', $host_id, $host_template_id, $selected_graphs);
form_end();
}
} elseif ($host_id > 0 && $thold_template_id > 0 && $graph_template_id > 0) {
$host_template_id = db_fetch_cell_prepared('SELECT host_template_id
FROM host
WHERE id = ?',
array($host_id));
$selected_graphs['cg'][$graph_template_id][$graph_template_id] = true;
thold_graph_new_graphs('thold.php', $host_id, $host_template_id, $selected_graphs);
form_end();
}
} elseif ($type_id == 'thold') {
$host_template_ids = array_rekey(
db_fetch_assoc_prepared('SELECT host_template_id
FROM host_template_graph
WHERE graph_template_id = ?',
array($graph_template_id)),
'host_template_id', 'host_template_id');
if (cacti_sizeof($host_template_ids)) {
$hiql = ' AND host_template_id IN (' . implode(', ', $host_template_ids) . ')';
} else {
$hiql = ' AND 0 = 1';
}
if (get_selected_theme() != 'classic') {
$form_array['my_host_id'] = array(
'method' => 'drop_callback',
'friendly_name' => __('Device', 'thold'),
'description' => __('Select a Device to use for the Threshold and Graph to be created.', 'thold'),
'on_change' => 'applyTholdFilter()',
'action' => 'ajax_hosts',
'id' => $host_id,
'sql' => 'SELECT id, description AS name FROM host WHERE disabled != "" AND deleted != ""' . $hiql,
'value' => db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array($host_id)),
'none_value' => __('Select a Device', 'thold')
);
} else {
if ($hiql == ' AND 0 = 1') {
$hiql = '';
}
$hosts = array_rekey(get_allowed_devices($hiql),
'id', 'description'
);
$form_array['my_host_id'] = array(
'method' => 'drop_array',
'friendly_name' => __('Device', 'thold'),
'description' => __('Select a Device to use for the Threshold and Graph to be created.', 'thold'),
'on_change' => 'applyTholdFilter()',
'array' => $hosts,
'value' => $host_id,
'none_value' => __('Select a Device', 'thold')
);
}
if ($host_id > 0) {
$graphs = get_allowed_graphs('gl.host_id=' . $host_id);
$ng = array();
if (cacti_sizeof($graphs)) {
foreach ($graphs as $g) {
$ng[$g['local_graph_id']] = $g['title_cache'];
}
}
$form_array['local_graph_id'] = array(
'method' => 'drop_array',
'friendly_name' => __('Graph', 'thold'),
'description' => __('Select the Graph for the Threshold.', 'thold'),
'on_change' => 'applyTholdFilter()',
'value' => $local_graph_id,
'array' => $ng,
'none_value' => __('Select a Graph', 'thold')
);
}
if ($local_graph_id != '' && $host_id > 0) {
$dt_sql = 'SELECT DISTINCT dtr.local_data_id
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON gti.task_item_id=dtr.id
LEFT JOIN graph_local AS gl
ON gl.id=gti.local_graph_id
WHERE gl.id = ' . $local_graph_id;
$local_data_id = db_fetch_cell($dt_sql);
$dss = array_rekey(
db_fetch_assoc('SELECT DISTINCT id, data_source_name AS name
FROM data_template_rrd
WHERE local_data_id IN (' . $dt_sql . ')
ORDER BY data_source_name'),
'id', 'name');
$form_array['data_template_rrd_id'] = array(
'method' => 'drop_array',
'friendly_name' => __('Data Source', 'thold'),
'description' => __('Select a Data Source for the Threshold.', 'thold'),
'on_change' => 'applyTholdFilter()',
'value' => $data_template_rrd_id,
'array' => $dss,
'none_value' => __('Select a Data Source', 'thold')
);
$form_array['local_data_id'] = array(
'method' => 'hidden',
'value' => $local_data_id
);
} else {
$local_graph_id = 0;
$data_template_rrd_id = 0;
}
top_header();
form_start('thold.php?action=save', 'chk');
html_start_box($message, '100%', false, '3', 'center', '');
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => $form_array
)
);
if ($data_template_rrd_id > 0) {
print "<tr class='odd'>
<td colspan='2' class='saveRow'>
<input type='submit' value='" . __esc('Create', 'thold') . "'>
</td>
</tr>\n";
}
html_end_box(false);
html_start_box(__('Creation Notes', 'thold'), '100%', false, '3', 'center', '');
print '<tr><td><p style="padding:0px 5px"><b><font color=\'red\'>' . __('Important Note:', 'thold') .' </font></b>';
print __('This Threshold will <b>NOT</b> be Templated and will only work on existing Graphs. If you wish to both Create the Graph and the Threshold simultaneously, select Threshold Template from the drop down and continue until the \'Create\' button appears.', 'thold') . '</p></td></tr>';
html_end_box(false);
} else {
top_header();
form_start('thold.php?action=save', 'chk');
html_start_box($message, '100%', false, '3', 'center', '');
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => $form_array
)
);
html_end_box(false);
html_start_box(__('Creation Notes', 'thold'), '100%', false, '3', 'center', '');
print '<tr><td><p style="padding:0px 5px"><b><font color=\'red\'>' . __('Important Note:', 'thold') .' </font></b>';
print __('This Threshold will <b>NOT</b> be Templated. You can select either By Graph where you will then select an existing Device, Graph and Data Source before creating your Threshold, or you can select Threshold Template which will allow you to create a Non Templated Threshold and corresponding Graph simultaneously', 'thold') . '</p></td></tr>';
html_end_box(false);
}
if ($local_graph_id > 0) {
html_start_box(__('Selected Graph', 'thold'), '100%', '', '3', 'center', '');
print "<tr><td class='center'><p><img class='center' id='graphi' style='max-width:700px;' src='" . $config['url_path'] . "graph_image.php?local_graph_id=$local_graph_id&rra_id=0'></p></td></tr>";
html_end_box();
}
form_end();
?>
<script type='text/javascript'>
function applyTholdFilter() {
strURL = 'thold.php?action=add&header=false';
strURL += '&type_id=' + $('#type_id').val();
if ($('#type_id').val() == 'thold') {
if ($('#my_host_id').length && $('#my_host_id').val() > 0) {
strURL += '&my_host_id=' + $('#my_host_id').val();
}
if ($('#local_graph_id').length && $('#local_graph_id').val() > 0) {
strURL += '&local_graph_id=' + $('#local_graph_id').val();
}
if ($('#data_template_rrd_id').length && $('#data_template_rrd_id').val() > 0) {
strURL += '&data_template_rrd_id=' + $('#data_template_rrd_id').val();
}
} else {
if ($('#thold_template_id').length && $('#thold_template_id').val() > 0) {
strURL += '&thold_template_id=' + $('#thold_template_id').val();
}
if ($('#graph_template_id').length && $('#graph_template_id').val() > 0) {
strURL += '&graph_template_id=' + $('#graph_template_id').val();
}
if ($('#data_query_id').length && $('#data_query_id').val() > 0) {
strURL += '&data_query_id=' + $('#data_query_id').val();
}
if ($('#data_template_id').length && $('#data_template_id').val() > 0) {
strURL += '&data_template_id=' + $('#data_template_id').val();
}
if ($('#my_host_id').length && $('#my_host_id').val() != 0) {
strURL += '&my_host_id=' + $('#my_host_id').val();
}
if ($('#snmp_index').length && $('#snmp_index').val() != '') {
strURL += '&snmp_index=' + $('#snmp_index').val();
}
}
loadPageNoHeader(strURL, false, true);
}
$(function() {
if ($('#type_id').val() == 'template') {
$('#submit').prev().hide();
$('#submit').off().click(function(event) {
event.preventDefault();
json = $('input, select').serializeObject();
$.post('thold.php', json).done(function(data) {
$('#main').html(data);
applySkin();
window.scrollTo(0, 0);
});
});
}
});
</script>
<?php
bottom_footer();
}
function thold_new_graphs_save($host_id) {
$return_array = false;
$selected_graphs_array = cacti_unserialize(stripslashes(get_nfilter_request_var('selected_graphs_array')));
$values = 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 */
/* this is a new graph from template field */
if (empty($matches[1])) {
$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) {
/* this is a new graph from template field */
if (empty($matches[1])) {
$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_name */
/* this is a new graph from template field */
if (empty($matches[1])) {
$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)) {
/* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:data_input_field_id */
/* this is a new graph from template field */
if (empty($matches[1])) {
$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 */
/* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:local_data_template_rrd_id, 5:field_name */
/* this is a new graph from template field */
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;
}
}
}
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 */
if ($form_type == 'cg') {
$graph_template_id = $form_id1;
} elseif ($form_type == 'sg') {
foreach ($form_array2 as $form_id2 => $form_array3) {
$snmp_index_array = $form_array3;
$snmp_query_array['snmp_query_id'] = $form_id1;
$snmp_query_array['snmp_index_on'] = get_best_data_query_index_type($host_id, $form_id1);
$snmp_query_array['snmp_query_graph_id'] = $form_id2;
}
$graph_template_id = db_fetch_cell_prepared('SELECT graph_template_id
FROM snmp_query_graph
WHERE id = ?',
array($snmp_query_array['snmp_query_graph_id']));
}
if ($current_form_type == 'cg') {
$return_array = create_complete_graph_from_template($graph_template_id, $host_id, '', $values['cg']);
if (cacti_sizeof($return_array)) {
thold_raise_message(__esc('Created graph: %s', get_graph_title($return_array['local_graph_id']), 'thold'), MESSAGE_LEVEL_INFO);
/* lastly push host-specific information to our data sources */
foreach ($return_array['local_data_id'] as $item) {
push_out_host($host_id, $item);
}
}
} elseif ($current_form_type == 'sg') {
foreach($snmp_index_array as $snmp_index => $true) {
$snmp_query_array['snmp_index'] = decode_data_query_index($snmp_index, $snmp_query_array['snmp_query_id'], $host_id);
$return_array = create_complete_graph_from_template($graph_template_id, $host_id, $snmp_query_array, $values['sg'][$snmp_query_array['snmp_query_id']]);
if (cacti_sizeof($return_array)) {
thold_raise_message(__esc('Created graph: %s', get_graph_title($return_array['local_graph_id']), 'thold'), MESSAGE_LEVEL_INFO);
/* lastly push host-specific information to our data sources */
foreach ($return_array['local_data_id'] as $item) {
push_out_host($host_id, $item);
}
}
}
}
}
}
return $return_array;
}
function thold_graph_new_graphs($page, $host_id, $host_template_id, $selected_graphs_array) {
/* we use object buffering on this page to allow redirection to another page if no
fields are actually drawn */
ob_start();
top_header();
form_start($page);
$snmp_query_id = 0;
$num_output_fields = array();