forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.php
1537 lines (1283 loc) · 58.2 KB
/
host.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-2016 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_data_source.php');
include_once('./lib/api_tree.php');
include_once('./lib/html_tree.php');
include_once('./lib/api_graph.php');
include_once('./lib/snmp.php');
include_once('./lib/ping.php');
include_once('./lib/data_query.php');
include_once('./lib/api_device.php');
$device_actions = array(
1 => 'Delete',
2 => 'Enable',
3 => 'Disable',
4 => 'Change SNMP Options',
5 => 'Clear Statistics',
6 => 'Change Availability Options',
7 => 'Apply Automation Rules'
);
$device_actions = api_plugin_hook_function('device_action_array', $device_actions);
/* set default action */
set_default_action();
switch (get_request_var('action')) {
case 'save':
form_save();
break;
case 'actions':
form_actions();
break;
case 'gt_add':
get_filter_request_var('host_id');
host_add_gt();
header('Location: host.php?header=false&action=edit&id=' . get_request_var('host_id'));
break;
case 'gt_remove':
get_filter_request_var('host_id');
host_remove_gt();
header('Location: host.php?header=false&action=edit&id=' . get_request_var('host_id'));
break;
case 'query_add':
get_filter_request_var('host_id');
host_add_query();
header('Location: host.php?header=false&action=edit&id=' . get_request_var('host_id'));
break;
case 'query_remove':
get_filter_request_var('host_id');
host_remove_query();
header('Location: host.php?header=false&action=edit&id=' . get_request_var('host_id'));
break;
case 'query_reload':
get_filter_request_var('host_id');
host_reload_query();
header('Location: host.php?header=false&action=edit&id=' . get_request_var('host_id'));
break;
case 'query_verbose':
get_filter_request_var('host_id');
host_reload_query();
header('Location: host.php?header=' . (isset_request_var('header') && get_nfilter_request_var('header') == 'true' ? 'true':'false') . '&action=edit&id=' . get_request_var('host_id') . '&display_dq_details=true#dqdbg');
break;
case 'edit':
top_header();
host_edit();
bottom_footer();
break;
case 'ping_host':
ping_host();
break;
default:
top_header();
host();
bottom_footer();
break;
}
/* --------------------------
Global Form Functions
-------------------------- */
function add_tree_names_to_actions_array() {
global $device_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)) {
foreach ($trees as $tree) {
$device_actions{'tr_' . $tree['id']} = 'Place on a Tree (' . $tree['name'] . ')';
}
}
}
/* --------------------------
The Save Function
-------------------------- */
function form_save() {
if (isset_request_var('save_component_host')) {
if (get_nfilter_request_var('snmp_version') == 3 && (get_nfilter_request_var('snmp_password') != get_nfilter_request_var('snmp_password_confirm'))) {
raise_message(4);
}else{
get_filter_request_var('id');
get_filter_request_var('host_template_id');
$host_id = api_device_save(get_nfilter_request_var('id'), get_nfilter_request_var('host_template_id'), get_nfilter_request_var('description'),
trim(get_nfilter_request_var('hostname')), get_nfilter_request_var('snmp_community'), get_nfilter_request_var('snmp_version'),
get_nfilter_request_var('snmp_username'), get_nfilter_request_var('snmp_password'),
get_nfilter_request_var('snmp_port'), get_nfilter_request_var('snmp_timeout'),
(isset_request_var('disabled') ? get_nfilter_request_var('disabled') : ''),
get_nfilter_request_var('availability_method'), get_nfilter_request_var('ping_method'),
get_nfilter_request_var('ping_port'), get_nfilter_request_var('ping_timeout'),
get_nfilter_request_var('ping_retries'), get_nfilter_request_var('notes'),
get_nfilter_request_var('snmp_auth_protocol'), get_nfilter_request_var('snmp_priv_passphrase'),
get_nfilter_request_var('snmp_priv_protocol'), get_nfilter_request_var('snmp_context'),
get_nfilter_request_var('max_oids'), get_nfilter_request_var('device_threads'));
if ($host_id !== false) {
api_plugin_hook_function('host_save', array('host_id' => $host_id));
}
}
header('Location: host.php?header=false&action=edit&id=' . (empty($host_id) ? get_nfilter_request_var('id') : $host_id));
}
}
/* ------------------------
The "actions" function
------------------------ */
function form_actions() {
global $device_actions, $fields_host_edit;
/* ================= 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') == '2') { /* Enable Selected Devices */
for ($i=0;($i<count($selected_items));$i++) {
db_execute_prepared("UPDATE host SET disabled = '' WHERE id = ?", array($selected_items[$i]));
/* update poller cache */
$data_sources = db_fetch_assoc_prepared('SELECT id FROM data_local WHERE host_id = ?', array($selected_items[$i]));
$poller_items = $local_data_ids = array();
if (sizeof($data_sources)) {
foreach ($data_sources as $data_source) {
$local_data_ids[] = $data_source['id'];
$poller_items = array_merge($poller_items, update_poller_cache($data_source['id']));
}
}
if (sizeof($local_data_ids)) {
poller_update_poller_cache_from_buffer($local_data_ids, $poller_items);
}
}
}elseif (get_nfilter_request_var('drp_action') == '3') { /* Disable Selected Devices */
for ($i=0;($i<count($selected_items));$i++) {
db_execute_prepared("UPDATE host SET disabled='on' WHERE id = ?", array($selected_items[$i]));
/* update poller cache */
db_execute_prepared('DELETE FROM poller_item WHERE host_id = ?', array($selected_items[$i]));
db_execute_prepared('DELETE FROM poller_reindex WHERE host_id = ?', array($selected_items[$i]));
}
}elseif (get_nfilter_request_var('drp_action') == '4') { /* change snmp options */
for ($i=0;($i<count($selected_items));$i++) {
reset($fields_host_edit);
while (list($field_name, $field_array) = each($fields_host_edit)) {
if (isset_request_var("t_$field_name")) {
db_execute_prepared("UPDATE host SET $field_name = ? WHERE id = ?", array(get_nfilter_request_var($field_name), $selected_items[$i]));
}
}
push_out_host($selected_items[$i]);
}
}elseif (get_nfilter_request_var('drp_action') == '5') { /* Clear Statisitics for Selected Devices */
for ($i=0;($i<count($selected_items));$i++) {
db_execute_prepared("UPDATE host SET min_time = '9.99999', max_time = '0', cur_time = '0', avg_time = '0',
total_polls = '0', failed_polls = '0', availability = '100.00'
where id = ?", array($selected_items[$i]));
}
}elseif (get_nfilter_request_var('drp_action') == '6') { /* change availability options */
for ($i=0;($i<count($selected_items));$i++) {
reset($fields_host_edit);
while (list($field_name, $field_array) = each($fields_host_edit)) {
if (isset_request_var("t_$field_name")) {
db_execute_prepared("UPDATE host SET $field_name = ? WHERE id = ?", array(get_nfilter_request_var($field_name), $selected_items[$i]));
}
}
push_out_host($selected_items[$i]);
}
}elseif (get_nfilter_request_var('drp_action') == '1') { /* delete */
if (!isset_request_var('delete_type')) {
set_request_var('delete_type', 2);
}
$data_sources_to_act_on = array();
$graphs_to_act_on = array();
$devices_to_act_on = array();
for ($i=0; $i<count($selected_items); $i++) {
$data_sources = db_fetch_assoc('SELECT
data_local.id AS local_data_id
FROM data_local
WHERE ' . array_to_sql_or($selected_items, 'data_local.host_id'));
if (sizeof($data_sources)) {
foreach ($data_sources as $data_source) {
$data_sources_to_act_on[] = $data_source['local_data_id'];
}
}
if (get_nfilter_request_var('delete_type') == 2) {
$graphs = db_fetch_assoc('SELECT
graph_local.id AS local_graph_id
FROM graph_local
WHERE ' . array_to_sql_or($selected_items, 'graph_local.host_id'));
if (sizeof($graphs)) {
foreach ($graphs as $graph) {
$graphs_to_act_on[] = $graph['local_graph_id'];
}
}
}
$devices_to_act_on[] = $selected_items[$i];
}
switch (get_nfilter_request_var('delete_type')) {
case '1': /* leave graphs and data_sources in place, but disable the data sources */
api_data_source_disable_multi($data_sources_to_act_on);
api_plugin_hook_function('data_source_remove', $data_sources_to_act_on);
break;
case '2': /* delete graphs/data sources tied to this device */
api_data_source_remove_multi($data_sources_to_act_on);
api_graph_remove_multi($graphs_to_act_on);
api_plugin_hook_function('graphs_remove', $graphs_to_act_on);
break;
}
api_device_remove_multi($devices_to_act_on);
api_plugin_hook_function('device_remove', $devices_to_act_on);
}elseif (preg_match('/^tr_([0-9]+)$/', get_nfilter_request_var('drp_action'), $matches)) { /* place on tree */
get_filter_request_var('tree_id');
get_filter_request_var('tree_item_id');
for ($i=0;($i<count($selected_items));$i++) {
api_tree_item_save(0, get_nfilter_request_var('tree_id'), TREE_ITEM_TYPE_HOST, get_nfilter_request_var('tree_item_id'), '', 0, $selected_items[$i], 1, 1, false);
}
}elseif (get_nfilter_request_var('drp_action') == 7) { /* automation */
cacti_log(__FUNCTION__ . ' called, action: ' . $action, true, 'AUTOM8 TRACE', POLLER_VERBOSITY_MEDIUM);
cacti_log(__FUNCTION__ . ', items: ' . get_nfilter_request_var('selected_items'), true, 'AUTOM8 TRACE', POLLER_VERBOSITY_MEDIUM);
/* work on all selected hosts */
for ($i=0;($i<count($selected_items));$i++) {
$host_id = $selected_items[$i];
cacti_log(__FUNCTION__ . ' Host[' . $host_id . ']', true, 'AUTOM8 TRACE', POLLER_VERBOSITY_MEDIUM);
/* select all graph templates associated with this host, but exclude those where
* a graph already exists (table graph_local has a known entry for this host/template) */
$sql = 'SELECT gt.*
FROM graph_templates AS gt
INNER JOIN host_graph AS hg
ON gt.id=hg.graph_template_id
WHERE hg.host_id=' . $host_id . '
AND gt.id NOT IN (
SELECT gl.graph_template_id
FROM graph_local AS gl
WHERE host_id=' . $host_id . '
)';
$graph_templates = db_fetch_assoc($sql);
cacti_log(__FUNCTION__ . ' Host[' . $host_id . '], sql: ' . $sql, true, 'AUTOM8 TRACE', POLLER_VERBOSITY_MEDIUM);
/* create all graph template graphs */
if (sizeof($graph_templates)) {
foreach ($graph_templates as $graph_template) {
cacti_log(__FUNCTION__ . ' Host[' . $host_id . '], graph: ' . $graph_template['id'], true, 'AUTOM8 TRACE', POLLER_VERBOSITY_MEDIUM);
automation_execute_graph_template($host_id, $graph_template['id']);
}
}
/* all associated data queries */
$data_queries = db_fetch_assoc('SELECT sq.*,
hsq.reindex_method
FROM snmp_query AS sq
INNER JOIN host_snmp_query AS hsq
ON sq.id=hsq.snmp_query_id
WHERE hsq.host_id=' . $host_id);
/* create all data query graphs */
if (sizeof($data_queries)) {
foreach ($data_queries as $data_query) {
cacti_log(__FUNCTION__ . ' Host[' . $host_id . '], dq[' . $data_query['id'] . ']', true, 'AUTOM8 TRACE', POLLER_VERBOSITY_MEDIUM);
automation_execute_data_query($host_id, $data_query['id']);
}
}
/* now handle tree rules for that host */
cacti_log(__FUNCTION__ . ' Host[' . $host_id . '], create_tree for host: ' . $host_id, true, 'AUTOM8 TRACE', POLLER_VERBOSITY_MEDIUM);
automation_execute_device_create_tree($host_id);
}
} else {
api_plugin_hook_function('device_action_execute', get_nfilter_request_var('drp_action'));
}
}
/* update snmpcache */
snmpagent_device_action_bottom(array(get_nfilter_request_var('drp_action'), $selected_items));
api_plugin_hook_function('device_action_bottom', array(get_nfilter_request_var('drp_action'), $selected_items));
header('Location: host.php?header=false');
exit;
}
/* setup some variables */
$host_list = ''; $i = 0;
/* loop through each of the host templates 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]);
/* ==================================================== */
$host_list .= '<li>' . htmlspecialchars(db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array($matches[1]))) . '</li>';
$host_array[$i] = $matches[1];
$i++;
}
}
top_header();
/* add a list of tree names to the actions dropdown */
add_tree_names_to_actions_array();
form_start('host.php');
html_start_box($device_actions[get_nfilter_request_var('drp_action')], '60%', '', '3', 'center', '');
if (isset($host_array) && sizeof($host_array)) {
if (get_nfilter_request_var('drp_action') == '2') { /* Enable Devices */
print "<tr>
<td colspan='2' class='textArea'>
<p>Click 'Continue' to enable the following Device(s).</p>
<p><ul>$host_list</ul></p>
</td>
</tr>\n";
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue' title='Enable Device(s)'>";
}elseif (get_nfilter_request_var('drp_action') == '3') { /* Disable Devices */
print " <tr>
<td colspan='2' class='textArea'>
<p>Click 'Continue' to disable the following Device(s).</p>
<p><ul>$host_list</ul></p>
</td>
</tr>\n";
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue' title='Disable Device(s)'>";
}elseif (get_nfilter_request_var('drp_action') == '4') { /* change snmp options */
print "<tr>
<td colspan='2' class='textArea'>
<p>Click 'Continue' to change SNMP parameters for the following Device(s).
Please check the box next to the fields you want to update, and then fill in the new value.</p>
<p><ul>$host_list</ul></p>
</td>
</tr>\n";
$form_array = array();
while (list($field_name, $field_array) = each($fields_host_edit)) {
if ((preg_match('/^snmp_/', $field_name)) ||
($field_name == 'max_oids')) {
$form_array += array($field_name => $fields_host_edit[$field_name]);
$form_array[$field_name]['value'] = '';
$form_array[$field_name]['description'] = '';
$form_array[$field_name]['form_id'] = 0;
$form_array[$field_name]['sub_checkbox'] = array(
'name' => 't_' . $field_name,
'friendly_name' => 'Update this Field',
'value' => ''
);
}
}
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => $form_array
)
);
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue' title='Change Device(s) SNMP Options'>";
}elseif (get_nfilter_request_var('drp_action') == '6') { /* change availability options */
print "<tr>
<td colspan='2' class='textArea'>
<p>Click 'Continue' to change Availability parameters for the following Device(s).
Please check the box next to the fields you want to update, then fill in the new value.</p>
<p><ul>$host_list</ul></p>
</td>
</tr>\n";
$form_array = array();
while (list($field_name, $field_array) = each($fields_host_edit)) {
if (preg_match('/(availability_method|ping_method|ping_port|ping_timeout|ping_retries)/', $field_name)) {
$form_array += array($field_name => $fields_host_edit[$field_name]);
$form_array[$field_name]['value'] = '';
$form_array[$field_name]['description'] = '';
$form_array[$field_name]['form_id'] = 0;
$form_array[$field_name]['sub_checkbox'] = array(
'name' => 't_' . $field_name,
'friendly_name' => 'Update this Field',
'value' => ''
);
}
}
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => $form_array
)
);
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue' title='Change Device(s) Availability Options'>";
}elseif (get_nfilter_request_var('drp_action') == '5') { /* Clear Statisitics for Selected Devices */
print "<tr>
<td colspan='2' class='textArea'>
<p>Click 'Continue' to clear the counters for the following Device(s).</p>
<p><ul>$host_list</ul></p>
</td>
</tr>\n";
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue' title='Clear Statistics on Device(s)'>";
}elseif (get_nfilter_request_var('drp_action') == '1') { /* delete */
print "<tr>
<td class='textArea'>
<p>Click 'Continue' to delete the following Device(s).</p>
<p><ul>$host_list</ul></p>\n";
form_radio_button('delete_type', '2', '1', 'Leave all Graph(s) and Data Source(s) untouched. Data Source(s) will be disabled however.', '1'); print '<br>';
form_radio_button('delete_type', '2', '2', 'Delete all associated <strong>Graph(s)</strong> and <strong>Data Source(s)</strong>.', '1'); print '<br>';
print "</td></tr>
</td>
</tr>\n";
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue' title='Delete Device(s)'>";
}elseif (preg_match('/^tr_([0-9]+)$/', get_nfilter_request_var('drp_action'), $matches)) { /* place on tree */
print "<tr>
<td class='textArea'>
<p>Click 'Continue' to place the following Device(s) under the branch selected below.</p>
<p><ul>$host_list</ul></p>
<p><strong>Destination Branch:</strong><br>\n";
grow_dropdown_tree($matches[1], '0', 'tree_item_id', '0');
print "</p>
</td>
</tr>
<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n";
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue' title='Place Device(s) on Tree'>";
}elseif (get_nfilter_request_var('drp_action') == 7) { /* automation */
print "<tr>
<td class='textArea'>
<p>Click 'Continue' to apply Automation Rules to the following Devices(s)</p>
<p><ul>$host_list</ul></p>
</td>
</tr>\n";
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue' title='Run Automation on Device(s)'>";
} else {
$save['drp_action'] = get_nfilter_request_var('drp_action');
$save['host_list'] = $host_list;
$save['host_array'] = (isset($host_array)? $host_array : array());
api_plugin_hook_function('device_action_prepare', $save);
$save_html = "<input type='button' value='Cancel' onClick='cactiReturnTo()'> <input type='submit' value='Continue'>";
}
}else{
print "<tr><td class='even'><span class='textError'>You must select at least one device.</span></td></tr>\n";
$save_html = "<input type='button' value='Return' onClick='cactiReturnTo()'>";
}
print "<tr>
<td colspan='2' class='saveRow'>
<input type='hidden' name='action' value='actions'>
<input type='hidden' name='selected_items' value='" . (isset($host_array) ? serialize($host_array) : '') . "'>
<input type='hidden' name='drp_action' value='" . get_nfilter_request_var('drp_action') . "'>
$save_html
</td>
</tr>\n";
html_end_box();
form_end();
bottom_footer();
}
/* -------------------
Data Query Functions
------------------- */
function host_add_query() {
/* ================= input validation ================= */
get_filter_request_var('host_id');
get_filter_request_var('snmp_query_id');
get_filter_request_var('reindex_method');
/* ==================================================== */
db_execute_prepared('REPLACE INTO host_snmp_query (host_id, snmp_query_id, reindex_method) VALUES (?, ?, ?)', array(get_nfilter_request_var('host_id'), get_nfilter_request_var('snmp_query_id'), get_nfilter_request_var('reindex_method')));
/* recache snmp data */
run_data_query(get_nfilter_request_var('host_id'), get_nfilter_request_var('snmp_query_id'));
}
function host_reload_query() {
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('host_id');
/* ==================================================== */
run_data_query(get_request_var('host_id'), get_request_var('id'));
}
function host_remove_query() {
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('host_id');
/* ==================================================== */
api_device_dq_remove(get_request_var('host_id'), get_request_var('id'));
}
function host_add_gt() {
/* ================= input validation ================= */
get_filter_request_var('host_id');
get_filter_request_var('graph_template_id');
/* ==================================================== */
db_execute_prepared('REPLACE INTO host_graph (host_id, graph_template_id) VALUES (?, ?)', array(get_nfilter_request_var('host_id'), get_nfilter_request_var('graph_template_id')));
automation_hook_graph_template(get_nfilter_request_var('host_id'), get_nfilter_request_var('graph_template_id'));
api_plugin_hook_function('add_graph_template_to_host', array('host_id' => get_nfilter_request_var('host_id'), 'graph_template_id' => get_nfilter_request_var('graph_template_id')));
}
function host_remove_gt() {
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('host_id');
/* ==================================================== */
api_device_gt_remove(get_request_var('host_id'), get_request_var('id'));
}
/* ---------------------
Device Functions
--------------------- */
function host_remove() {
global $config;
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
if ((read_config_option('deletion_verification') == 'on') && (!isset_request_var('confirm'))) {
top_header();
form_confirm('Are You Sure?', "Are you sure you want to delete the host <strong>'" . htmlspecialchars(db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array(get_request_var('id')))) . "'</strong>?", htmlspecialchars('host.php'), htmlspecialchars('host.php?action=remove&id=' . get_request_var('id')));
bottom_footer();
exit;
}
if ((read_config_option('deletion_verification') == '') || (isset_request_var('confirm'))) {
api_device_remove(get_request_var('id'));
}
}
function ping_host() {
get_filter_request_var('id');
if (isempty_request_var('id')) {
return "";
}
$host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array(get_request_var('id')));
$am = $host['availability_method'];
$anym = false;
if ($am == AVAIL_SNMP || $am == AVAIL_SNMP_GET_NEXT ||
$am == AVAIL_SNMP_GET_SYSDESC || $am == AVAIL_SNMP_AND_PING ||
$am == AVAIL_SNMP_OR_PING) {
$anym = true;
print "SNMP Information<br>\n";
print "<span class='monoSpace'>\n";
if (($host['snmp_community'] == '' && $host['snmp_username'] == '') || $host['snmp_version'] == 0) {
print "<span style='color: #ab3f1e; font-weight: bold;'>SNMP not in use</span>\n";
}else{
$snmp_system = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.1.0', $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'),SNMP_WEBUI);
/* modify for some system descriptions */
/* 0000937: System output in host.php poor for Alcatel */
if (substr_count($snmp_system, '00:')) {
$snmp_system = str_replace('00:', '', $snmp_system);
$snmp_system = str_replace(':', ' ', $snmp_system);
}
if ($snmp_system == '') {
print "<span class='hostDown'>SNMP error</span>\n";
}else{
$snmp_uptime = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.3.0', $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
$snmp_hostname = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.5.0', $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
$snmp_location = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.6.0', $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
$snmp_contact = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.4.0', $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
print '<strong>System:</strong> ' . html_split_string($snmp_system) . "<br>\n";
$days = intval($snmp_uptime / (60*60*24*100));
$remainder = $snmp_uptime % (60*60*24*100);
$hours = intval($remainder / (60*60*100));
$remainder = $remainder % (60*60*100);
$minutes = intval($remainder / (60*100));
print "<strong>Uptime:</strong> $snmp_uptime";
print " ($days days, $hours hours, $minutes minutes)<br>\n";
print "<strong>Hostname:</strong> $snmp_hostname<br>\n";
print "<strong>Location:</strong> $snmp_location<br>\n";
print "<strong>Contact:</strong> $snmp_contact<br>\n";
}
}
print "</span>\n";
}
if ($am == AVAIL_PING || $am == AVAIL_SNMP_AND_PING || $am == AVAIL_SNMP_OR_PING) {
$anym = true;
/* create new ping socket for host pinging */
$ping = new Net_Ping;
$ping->host = $host;
$ping->port = $host['ping_port'];
/* perform the appropriate ping check of the host */
$ping_results = $ping->ping(AVAIL_PING, $host['ping_method'], $host['ping_timeout'], $host['ping_retries']);
if ($ping_results == true) {
$host_down = false;
$class = 'hostUp';
}else{
$host_down = true;
$class = 'hostDown';
}
print "Ping Results<br>\n";
print "<span class='" . $class . "'>" . $ping->ping_response . "</span>\n";
}
if ($anym == false) {
print "No Ping or SNMP Availability Check In Use<br><br>\n";
}
}
function host_edit() {
global $fields_host_edit, $reindex_types;
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
api_plugin_hook('host_edit_top');
if (!isempty_request_var('id')) {
$host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array(get_request_var('id')));
$header_label = '[edit: ' . htmlspecialchars($host['description']) . ']';
}else{
$header_label = '[new]';
}
if (!empty($host['id'])) {
?>
<table style='width:100%'>
<tr>
<td class='textInfo left'>
<?php print htmlspecialchars($host['description']);?> (<?php print htmlspecialchars($host['hostname']);?>)
</td>
<td rowspan='2' class='textInfo right' style='vertical-align:top'>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print htmlspecialchars('graphs_new.php?host_id=' . $host['id']);?>'>Create Graphs for this Device</a><br>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print htmlspecialchars('data_sources.php?host_id=' . $host['id'] . '&ds_rows=30&filter=&template_id=-1&method_id=-1&page=1');?>'>Data Source List</a><br>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print htmlspecialchars('graphs.php?host_id=' . $host['id'] . '&graph_rows=30&filter=&template_id=-1&page=1');?>'>Graph List</a>
<?php api_plugin_hook('device_edit_top_links'); ?>
</td>
</tr>
<tr>
<td style='vertical-align:top;' class='textHeader'>
<div id='ping_results'>Contacting Device <i style='font-size:12px;' class='fa fa-spin fa-spinner'></i><br><br></div>
</td>
</tr>
</table>
<?php
}
form_start('host.php', 'host_form');
html_start_box("Device $header_label", '100%', '', '3', 'center', '');
/* preserve the host template id if passed in via a GET variable */
if (!isempty_request_var('host_template_id')) {
$fields_host_edit['host_template_id']['value'] = get_filter_request_var('host_template_id');
}
draw_edit_form(array(
'config' => array('no_form_tag' => true),
'fields' => inject_form_variables($fields_host_edit, (isset($host) ? $host : array()))
));
html_end_box();
?>
<script type="text/javascript">
// default snmp information
var snmp_community = $('#snmp_community').val();
var snmp_username = $('#snmp_username').val();
var snmp_password = $('#snmp_password').val();
var snmp_auth_protocol = $('#snmp_auth_protocol').val();
var snmp_priv_passphrase = $('#snmp_priv_passphrase').val();
var snmp_priv_protocol = $('#snmp_priv_protocol').val();
var snmp_context = $('#snmp_context').val();
var snmp_port = $('#snmp_port').val();
var snmp_timeout = $('#snmp_timeout').val();
var max_oids = $('#max_oids').val();
// default ping methods
var ping_method = $('#ping_method').val();
var ping_port = $('#ping_port').val();
var ping_timeout = $('#ping_timeout').val();
var ping_retries = $('#ping_retries').val();
function setPing() {
availability_method = $('#availability_method').val();
ping_method = $('#ping_method').val();
switch(availability_method) {
case '0': // none
$('#row_ping_method').css('display', 'none');
$('#row_ping_port').css('display', 'none');
$('#row_ping_timeout').css('display', 'none');
$('#row_ping_retries').css('display', 'none');
break;
case '2': // snmp
case '5': // snmp sysDesc
case '6': // snmp getNext
$('#row_ping_method').css('display', 'none');
$('#row_ping_port').css('display', 'none');
$('#row_ping_timeout').css('display', '');
$('#row_ping_retries').css('display', '');
break;
default: // ping ok
switch(ping_method) {
case '1': // ping icmp
$('#row_ping_method').css('display', '');
$('#row_ping_port').css('display', 'none');
$('#row_ping_timeout').css('display', '');
$('#row_ping_retries').css('display', '');
break;
case '2': // ping udp
case '3': // ping tcp
$('#row_ping_method').css('display', '');
$('#row_ping_port').css('display', '');
$('#row_ping_timeout').css('display', '');
$('#row_ping_retries').css('display', '');
break;
}
break;
}
}
function setAvailability() {
if ($('#snmp_version').val() == '0') {
methods = [
{ value: '0', text: 'None' },
{ value: '3', text: 'Ping' }
];
if ($('#availability_method').val() != '3' && $('#availability_method').val() != '0') {
$('#availability_method').val('3');
}
$('#availability_method').replaceOptions(methods, $('#availability_method').val());
}else{
methods = [
{ value: '0', text: 'None' },
{ value: '1', text: 'Ping and SNMP Uptime' },
{ value: '2', text: 'SNMP Uptime' },
{ value: '3', text: 'Ping' },
{ value: '4', text: 'Ping or SNMP Uptime' },
{ value: '5', text: 'SNMP Desc' },
{ value: '6', text: 'SNMP GetNext' }
];
$('#availability_method').replaceOptions(methods, $('#availability_method').val());
}
switch($('#availability_method').val()) {
case '0': // availability none
$('#row_ping_method').hide();
$('#ping_method').val('1');
$('#row_ping_timeout').hide();
$('#row_ping_port').hide();
$('#row_ping_timeout').hide();
$('#row_ping_retrie').hide();
break;
case '1': // ping and snmp sysUptime
case '3': // ping
case '4': // ping or snmp sysUptime
$('#row_ping_method').show();
break;
case '2': // snmp sysUptime
case '5': // snmp sysDesc
case '6': // snmp getNext
$('#row_ping_method').hide();
$('#ping_method').val('1');
break;
}
if ($('#availability_method-button').length) {
$('#availability_method').selectmenu('refresh');
}
}
function changeHostForm() {
setSNMP();
setAvailability();
setPing();
}
function setSNMP() {
snmp_version = $('#snmp_version').val();
switch(snmp_version) {
case '0': // No SNMP
$('#row_snmp_username').hide();
$('#row_snmp_password').hide();
$('#row_snmp_community').hide();
$('#row_snmp_auth_protocol').hide();
$('#row_snmp_priv_passphrase').hide();
$('#row_snmp_priv_protocol').hide();
$('#row_snmp_context').hide();
$('#row_snmp_port').hide();
$('#row_snmp_timeout').hide();
$('#row_max_oids').hide();
break;
case '1': // SNMP v1
case '2': // SNMP v2c
$('#row_snmp_username').hide();
$('#row_snmp_password').hide();
$('#row_snmp_community').show();
$('#row_snmp_auth_protocol').hide();
$('#row_snmp_priv_passphrase').hide();
$('#row_snmp_priv_protocol').hide();
$('#row_snmp_context').hide();
$('#row_snmp_port').show();
$('#row_snmp_timeout').show();
$('#row_max_oids').show();
break;
case '3': // SNMP v3
$('#row_snmp_username').show();
$('#row_snmp_password').show();
$('#row_snmp_community').hide();
$('#row_snmp_auth_protocol').show();
$('#row_snmp_priv_passphrase').show();
$('#row_snmp_priv_protocol').show();
$('#row_snmp_context').show();
$('#row_snmp_port').show();
$('#row_snmp_timeout').show();
$('#row_max_oids').show();
break;
}
}
$(function() {
$('[id^="reload"]').click(function(data) {
$(this).removeClass('fa-circle-o').addClass('fa-circle-o-notch fa-spin');
strURL = 'host.php?action=query_reload&id='+$(this).attr('data-id')+'&host_id='+$('#id').val();
loadPageNoHeader(strURL);
});
$('[id^="verbose"]').click(function(data) {
strURL = 'host.php?action=query_verbose&id='+$(this).attr('data-id')+'&host_id='+$('#id').val();
loadPageNoHeader(strURL);
});
$('[id^="remove"]').click(function(data) {
strURL = 'host.php?action=query_remove&id='+$(this).attr('data-id')+'&host_id='+$('#id').val();
loadPageNoHeader(strURL);
});
$('[id^="gtremove"]').click(function(data) {
strURL = 'host.php?action=gt_remove&id='+$(this).attr('data-id')+'&host_id='+$('#id').val();
loadPageNoHeader(strURL);
});
$('#add_dq').click(function() {
$.post('host.php?action=query_add', { host_id: $('#id').val(), snmp_query_id: $('#snmp_query_id').val(), reindex_method: $('#reindex_method').val(), __csrf_magic: csrfMagicToken }).done(function(data) {