forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.php
1941 lines (1638 loc) · 66.9 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-2023 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
include('./include/auth.php');
include_once('./lib/api_automation.php');
include_once('./lib/api_data_source.php');
include_once('./lib/api_device.php');
include_once('./lib/api_graph.php');
include_once('./lib/api_tree.php');
include_once('./lib/data_query.php');
include_once('./lib/html_tree.php');
include_once('./lib/ping.php');
include_once('./lib/poller.php');
include_once('./lib/reports.php');
include_once('./lib/snmp.php');
include_once('./lib/template.php');
include_once('./lib/utility.php');
$device_actions = array(
1 => __('Delete'),
2 => __('Enable'),
3 => __('Disable'),
4 => __('Change Device Settings'),
5 => __('Clear Statistics'),
6 => __('Apply Automation Rules'),
7 => __('Sync to Device Template'),
8 => __('Place Device on Report')
);
$device_actions = api_plugin_hook_function('device_action_array', $device_actions);
/* set default action */
set_default_action();
switch (get_request_var('action')) {
case 'export':
host_export();
break;
case 'save':
form_save();
break;
case 'reindex':
host_reindex();
header('Location: host.php?action=edit&id=' . get_request_var('host_id'));
break;
case 'actions':
form_actions();
break;
case 'gt_add':
get_filter_request_var('host_id');
host_add_gt();
header('Location: host.php?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?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?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?action=edit&id=' . get_request_var('host_id'));
break;
case 'query_change':
get_filter_request_var('host_id');
host_change_query();
header('Location: host.php?action=edit&id=' . get_request_var('host_id'));
break;
case 'query_reload':
get_filter_request_var('host_id');
host_reload_query();
raise_message('query_reloaded', __('Data Query Re-indexed.'), MESSAGE_LEVEL_INFO);
header('Location: host.php?action=edit&id=' . get_request_var('host_id'));
break;
case 'query_verbose':
get_filter_request_var('host_id');
host_reload_query();
raise_message('query_reloaded', __('Device Data Query Re-indexed. Verbose output displayed.'), MESSAGE_LEVEL_INFO);
header('Location: host.php?action=edit&id=' . get_request_var('host_id') . '&display_dq_details=true');
break;
case 'edit':
top_header();
host_edit();
bottom_footer();
break;
case 'ping_host':
$host_id = get_filter_request_var('id');
api_device_ping_device($host_id);
break;
case 'enable_debug':
enable_device_debug(get_filter_request_var('host_id'));
raise_message('enable_debug', __('Device Debugging Enabled for Device.'), MESSAGE_LEVEL_INFO);
header('Location: host.php?action=edit&id=' . get_request_var('host_id'));
break;
case 'disable_debug':
disable_device_debug(get_filter_request_var('host_id'));
raise_message('disable_debug', __('Device Debugging Disabled for Device.'), MESSAGE_LEVEL_INFO);
header('Location: host.php?action=edit&id=' . get_request_var('host_id'));
break;
case 'repopulate':
if (get_filter_request_var('host_id') > 0) {
push_out_host(get_request_var('host_id'));
raise_message('repopulate_message', __('Poller Cache for Device Refreshed.'), MESSAGE_LEVEL_INFO);
} else {
raise_message('repopulate_error', __('ERROR: Invalid Device ID.'), MESSAGE_LEVEL_ERROR);
}
header('Location: host.php?action=edit&id=' . get_request_var('host_id'));
break;
case 'ajax_locations':
get_site_locations();
break;
default:
top_header();
host();
bottom_footer();
break;
}
/* --------------------------
Global Form Functions
-------------------------- */
function host_reindex() {
global $config;
$start = microtime(true);
shell_exec(read_config_option('path_php_binary') . ' -q ' . CACTI_PATH_CLI . '/poller_reindex_hosts.php --qid=all --id=' . get_filter_request_var('host_id'));
$end = microtime(true);
$total_time = $end - $start;
$items = db_fetch_cell_prepared(
'SELECT COUNT(*)
FROM host_snmp_cache
WHERE host_id = ?',
array(get_filter_request_var('host_id'))
);
raise_message('host_reindex', __('Device Reindex Completed in %0.2f seconds. There were %d items updated.', $total_time, $items), MESSAGE_LEVEL_INFO);
}
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 (cacti_sizeof($trees)) {
foreach ($trees as $tree) {
$device_actions['tr_' . $tree['id']] = __esc('Place on a Tree (%s)', $tree['name']);
}
}
}
function get_site_locations() {
$return = array();
$term = get_nfilter_request_var('term');
$host_id = $_SESSION['cur_device_id'];
$args = ["%$term%"];
$where = '';
if (read_config_option('site_location_filter') && $_SESSION['cur_device_id']) {
$site_id = db_fetch_cell_prepared('SELECT site_id
FROM host
WHERE id = ?',
array($host_id));
$args []= $site_id;
$where = 'AND site_id = ?';
}
$locations = db_fetch_assoc_prepared("SELECT DISTINCT location
FROM host
WHERE location LIKE ?
AND location != ''
AND location IS NOT NULL
$where
ORDER BY location",
$args);
if (cacti_sizeof($locations)) {
foreach ($locations as $l) {
$return[] = array('label' => $l['location'], 'value' => $l['location'], 'id' => $l['location']);
}
}
if (!cacti_sizeof($return)) {
$return[] = array('label' => __('None'), 'value' => '', 'id' => __('None'));
}
print json_encode($return);
}
/* --------------------------
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(14);
} elseif (get_nfilter_request_var('snmp_version') == 3 && (get_nfilter_request_var('snmp_priv_passphrase') != get_nfilter_request_var('snmp_priv_passphrase_confirm'))) {
raise_message(13);
} 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('snmp_engine_id'),
get_nfilter_request_var('max_oids'),
get_nfilter_request_var('device_threads'),
get_nfilter_request_var('poller_id'),
get_nfilter_request_var('site_id'),
get_nfilter_request_var('external_id'),
get_nfilter_request_var('location'),
get_nfilter_request_var('bulk_walk_size')
);
if ($host_id !== false) {
api_plugin_hook_function('host_save', array('host_id' => $host_id));
}
}
header('Location: host.php?action=edit&id=' . (empty($host_id) ? get_nfilter_request_var('id') : $host_id));
}
}
/* ------------------------
The "actions" function
------------------------ */
function form_actions() {
global $device_actions, $device_change_fields, $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_request_var('drp_action') == '2') { // Enable Selected Devices
api_device_enable_devices($selected_items);
} elseif (get_request_var('drp_action') == '3') { // Disable Selected Devices
api_device_disable_devices($selected_items);
} elseif (get_request_var('drp_action') == '4') { // change device options
ini_set('max_execution_time', '-1');
api_device_change_options($selected_items, $_POST);
} elseif (get_request_var('drp_action') == '5') { // Clear Statistics for Selected Devices
api_device_clear_statistics($selected_items);
} elseif (get_request_var('drp_action') == '7') { // sync to device template
ini_set('max_execution_time', '-1');
api_device_sync_device_templates($selected_items);
} elseif (get_request_var('drp_action') == '8') { // place device on report
if (!reports_add_devices(get_filter_request_var('report_id'), $selected_items, get_filter_request_var('timespan'), get_filter_request_var('align'))) {
$name = db_fetch_cell_prepared(
'SELECT name
FROM reports
WHERE id = ?',
array(get_request_var('report_id'))
);
raise_message('reports_add_error', __('Unable to add some Devices to Report \'%s\'', $name), MESSAGE_LEVEL_WARN);
}
} elseif (get_request_var('drp_action') == '1') { // delete
ini_set('max_execution_time', '-1');
if (!isset_request_var('delete_type')) {
set_request_var('delete_type', 2);
}
api_device_remove_multi($selected_items, get_filter_request_var('delete_type'));
} elseif (preg_match('/^tr_([0-9]+)$/', get_request_var('drp_action'), $matches)) { // place on tree
get_filter_request_var('tree_id');
get_filter_request_var('tree_item_id');
foreach ($selected_items as $selected_item) {
api_tree_item_save(0, get_nfilter_request_var('tree_id'), TREE_ITEM_TYPE_HOST, get_nfilter_request_var('tree_item_id'), '', 0, $selected_item, 0, 1, 1, false);
}
} elseif (get_request_var('drp_action') == 6) { // automation
cacti_log(__FUNCTION__ . ' called, action: ' . get_request_var('drp_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 */
foreach ($selected_items as $host_id) {
automation_update_device($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');
exit;
}
/* setup some variables */
$host_list = '';
$host_array = array();
/* loop through each of the host templates selected on the previous page and get more info about them */
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1], 'chk[1]');
/* ==================================================== */
$host_list .= '<li>' . html_escape(db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array($matches[1]))) . '</li>';
$host_array[] = $matches[1];
}
}
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_request_var('drp_action')], '60%', '', '3', 'center', '');
if (isset($host_array) && cacti_sizeof($host_array)) {
if (get_request_var('drp_action') == '2') { // Enable Devices
print "<tr>
<td colspan='2' class='textArea'>
<p>" . __('Click \'Continue\' to enable the following Device(s).') . "</p>
<div class='itemlist'><ul>$host_list</ul></div>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('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>
<div class='itemlist'><ul>$host_list</ul></div>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Disable Device(s)') . "'>";
} elseif (get_nfilter_request_var('drp_action') == '4') { // Change Device options
print "<tr>
<td colspan='2' class='textArea'>
<p>" . __('Click \'Continue\' to change the Device options below for multiple Device(s). Please check the box next to the fields you want to update, and then fill in the new value.') . "</p>
<div class='itemlist'><ul>$host_list</ul></div>
</td>
</tr>";
$form_array = array();
foreach ($fields_host_edit as $field_name => $field_array) {
if (api_device_change_field_match($field_name)) {
$form_array += array($field_name => $fields_host_edit[$field_name]);
$form_array[$field_name]['value'] = '';
if (read_config_option('hide_form_description') == 'on') {
$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'),
'class' => 'ui-state-disabled',
'value' => ''
);
$form_array['location']['method'] = 'textbox';
$form_array['location']['default'] = '';
$form_array['location']['size'] = '20';
$form_array['location']['max_length'] = '40';
}
}
draw_edit_form(
array(
'config' => array('no_form_tag' => true),
'fields' => $form_array
)
);
device_javascript();
device_change_javascript();
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Change Device(s) SNMP Options') . "'>";
} elseif (get_request_var('drp_action') == '5') { // Clear Statistics for Selected Devices
print "<tr>
<td colspan='2' class='textArea'>
<p>" . __('Click \'Continue\' to clear the counters for the following Device(s).') . "</p>
<div class='itemlist'><ul>$host_list</ul></div>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Clear Statistics on Device(s)') . "'>";
} elseif (get_nfilter_request_var('drp_action') == '7') { // sync device template
print " <tr>
<td colspan='2' class='textArea'>
<p>" . __('Click \'Continue\' to Synchronize the following Device(s) to their Device Template.') . "</p>
<div class='itemlist'><ul>$host_list</ul></div>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Synchronize Device(s)') . "'>";
} elseif (get_request_var('drp_action') == '1') { // Delete
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to delete the following Device(s).') . "</p>
<div class='itemlist'><ul>$host_list</ul></div>";
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 Graph(s) and Data Source(s).'), '1');
print '<br>';
print '</td></tr>
</td>
</tr>';
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Delete Device(s)') . "'>";
} elseif (preg_match('/^tr_([0-9]+)$/', get_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>
<div class='itemlist'><ul>$host_list</ul></div>
<p>" . __('Destination Branch:') . '<br>';
grow_dropdown_tree($matches[1], '0', 'tree_item_id', '0');
print "</p>
</td>
</tr>
<input type='hidden' name='tree_id' value='" . $matches[1] . "'>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Place Device(s) on Tree') . "'>";
} elseif (get_request_var('drp_action') == 6) { // automation
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to apply Automation Rules to the following Devices(s).') . "</p>
<div class='itemlist'><ul>$host_list</ul></div>
</td>
</tr>";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Run Automation on Device(s)') . "'>";
} elseif (get_request_var('drp_action') == '8') {
global $alignment, $graph_timespans;
$reports = db_fetch_assoc_prepared(
'SELECT id, name
FROM reports
WHERE user_id = ?
ORDER BY name',
array($_SESSION[SESS_USER_ID])
);
if (cacti_sizeof($reports)) {
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to add the selected Graphs to the Report below.') . "</p>
<div class='itemlist'><ul>$host_list</ul></div>
</td>
</tr>
<tr><td>" . __('Report Name') . '<br>';
form_dropdown('report_id', $reports, 'name', 'id', '', '', '0');
print '</td></tr>';
print '<tr><td>' . __('Timespan') . '<br>';
form_dropdown('timespan', $graph_timespans, '', '', '', '', read_user_setting('default_timespan'));
print '</td></tr>';
print '<tr><td>' . __('Align') . '<br>';
form_dropdown('align', $alignment, '', '', '', '', REPORTS_ALIGN_CENTER);
print "</td></tr>\n";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Add Devices to Report') . "'>";
} else {
print "<tr><td class='even'><span class='textError'>" . __('You currently have no Reports defined.') . "</span></td></tr>\n";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Return') . "' onClick='cactiReturnTo()'>";
}
} else {
$save['drp_action'] = get_request_var('drp_action');
$save['host_list'] = $host_list;
$save['host_array'] = $host_array;
api_plugin_hook_function('device_action_prepare', $save);
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "'>";
}
} else {
raise_message(40);
header('Location: host.php');
exit;
}
print "<tr>
<td colspan='2' class='saveRow'>
<input type='hidden' name='action' value='actions'>
<input type='hidden' name='selected_items' value='" . serialize($host_array) . "'>
<input type='hidden' name='drp_action' value='" . html_escape(get_nfilter_request_var('drp_action')) . "'>
$save_html
</td>
</tr>";
html_end_box();
form_end();
bottom_footer();
}
/* -------------------
Device Export Function
------------------- */
function host_export() {
host_validate_vars();
$hosts = get_device_records($total_rows, 9999999);
$stdout = fopen('php://output', 'w');
header('Content-type: application/excel');
header('Content-Disposition: attachment; filename=cacti-devices-' . time() . '.csv');
if (cacti_sizeof($hosts)) {
$columns = array_keys($hosts[0]);
fputcsv($stdout, $columns);
foreach ($hosts as $h) {
fputcsv($stdout, $h);
}
}
fclose($stdout);
}
/* -------------------
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');
/* ==================================================== */
api_device_dq_add(get_request_var('host_id'), get_request_var('snmp_query_id'), get_request_var('reindex_method'));
}
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_change_query() {
/* ================= input validation ================= */
get_filter_request_var('data_query_id');
get_filter_request_var('host_id');
get_filter_request_var('reindex_method');
/* ==================================================== */
api_device_dq_change(get_request_var('host_id'), get_request_var('data_query_id'), get_request_var('reindex_method'));
}
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_edit() {
global $fields_host_edit, $reindex_types;
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
api_plugin_hook('host_edit_top');
$header_label = __('Device [new]');
$debug_link = '';
$repop_link = '';
if (!isempty_request_var('id')) {
$_SESSION['cur_device_id'] = get_request_var('id');
$host = db_fetch_row_prepared('SELECT *
FROM host
WHERE id = ?',
array(get_request_var('id'))
);
if (cacti_sizeof($host)) {
$header_label = __esc('Device [edit: %s]', $host['description']);
if (is_device_debug_enabled($host['id'])) {
$debug_link = "<span class='linkMarker'>*</span><a class='hyperLink' href='" . html_escape('host.php?action=disable_debug&host_id=' . $host['id']) . "'>" . __('Disable Device Debug') . '</a><br>';
} else {
$debug_link = "<span class='linkMarker'>*</span><a class='hyperLink' href='" . html_escape('host.php?action=enable_debug&host_id=' . $host['id']) . "'>" . __('Enable Device Debug') . '</a><br>';
}
$repop_link = "<span class='linkMarker'>*</span><a class='hyperLink' href='" . html_escape('host.php?action=repopulate&host_id=' . $host['id']) . "'>" . __('Repopulate Poller Cache') . '</a><br>';
$repop_link .= "<span class='linkMarker'>*</span><a class='hyperLink' href='" . html_escape('utilities.php?poller_action=-1&action=view_poller_cache&host_id=' . $host['id'] . '&template_id=-1&filter=&rows=-1') . "'>" . __('View Poller Cache') . '</a><br>';
/* append uptime data */
$header_label .= __(' [ In state since \'%s\', Uptime since \'%s\' ]', get_timeinstate($host, true), get_uptime($host, true));
}
} else {
$_SESSION['cur_device_id'] = 0;
}
if (!empty($host['id'])) {
?>
<table class='hostInfoHeader' style='width:100%'>
<tr>
<td class='textInfo left'>
<?php print html_escape($host['description']); ?> (<?php print html_escape($host['hostname']); ?>)<br />
</td>
<td rowspan='2' class='textInfo right' style='vertical-align:top'>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('host.php?action=edit'); ?>'><?php print __('Create New Device'); ?></a><br>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('graphs_new.php?reset=true&host_id=' . $host['id']); ?>'><?php print __('Create Graphs for this Device'); ?></a><br>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('host.php?action=reindex&host_id=' . $host['id']); ?>'><?php print __('Re-Index Device'); ?></a><br>
<?php print $debug_link; ?>
<?php print $repop_link; ?>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('data_sources.php?reset=true&host_id=' . $host['id'] . '&ds_rows=30&filter=&template_id=-1&method_id=-1&page=1'); ?>'><?php print __('Data Source List'); ?></a><br>
<span class='linkMarker'>*</span><a class='hyperLink' href='<?php print html_escape('graphs.php?reset=true&host_id=' . $host['id'] . '&graph_rows=30&filter=&template_id=-1&page=1'); ?>'><?php print __('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'><?php print __('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($header_label, '100%', true, '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(true, true);
device_javascript(!empty($host['id']));
if (!empty($host['id'])) {
html_start_box(__('Associated Graph Templates'), '100%', '', '3', 'center', '');
html_header(
array(
array('display' => __('Graph Template Name'), 'align' => 'left', 'nohide' => true),
array('display' => __('Status'), 'align' => 'left', 'nohide' => true)
),
2
);
$selected_graph_templates = db_fetch_assoc_prepared(
'
SELECT result.id, result.name, graph_local.id AS graph_local_id
FROM (
SELECT DISTINCT gt.id, gt.name
FROM graph_templates AS gt
INNER JOIN host_graph AS hg
ON gt.id = hg.graph_template_id
WHERE hg.host_id = ?
) AS result
LEFT JOIN graph_local
ON graph_local.graph_template_id = result.id
AND graph_local.host_id = ?
ORDER BY result.name',
array(get_request_var('id'), get_request_var('id'))
);
$available_graph_templates = db_fetch_assoc_prepared(
'SELECT DISTINCT gt.id, gt.name
FROM graph_templates AS gt
LEFT JOIN snmp_query_graph AS sqg
ON sqg.graph_template_id = gt.id
INNER JOIN graph_templates_item AS gti
ON gti.graph_template_id = gt.id
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id = dtr.id
INNER JOIN data_template_data AS dtd
ON dtd.data_template_id = dtr.data_template_id
WHERE sqg.name IS NULL
AND gti.local_graph_id = 0
AND dtr.local_data_id = 0
AND gt.id NOT IN (SELECT graph_template_id FROM host_graph WHERE host_id = ?)
ORDER BY gt.name',
array(get_request_var('id'))
);
$i = 0;
$displayed_templates = array();
if (cacti_sizeof($selected_graph_templates)) {
foreach ($selected_graph_templates as $item) {
if (isset($displayed_templates[$item['id']])) {
continue;
} else {
$displayed_templates[$item['id']] = true;
}
$i++;
form_alternate_row("gt$i", true);
/* get status information for this graph template */
$is_being_graphed = $item['graph_local_id'] > 0;
?>
<td class='nowrap' style="padding: 4px;">
<strong><?php print $i; ?>)</strong> <?php print html_escape($item['name']); ?>
</td>
<td class='nowrap'>
<?php print(($is_being_graphed == true) ? "<span class='beingGraphed'>" . __('Is Being Graphed') . "</span> (<a class='linkEditMain' href='" . html_escape('graphs.php?action=graph_edit&id=' . $item['graph_local_id']) . "'>" . __('Edit') . '</a>)' : "<span class='notBeingGraphed'>" . __('Not Being Graphed') . '</span>'); ?>
</td>
<td class='nowrap right'>
<span title='<?php print __esc('Delete Graph Template Association'); ?>' class='deletequery fa fa-times' id='gtremove<?php print $item['id']; ?>' data-id='<?php print $item['id']; ?>'></span>
</td>
<?php
form_end_row();
}
} else {
print "<tr class='tableRow'><td colspan='3'><em>" . __('No associated graph templates.') . '</em></td></tr>';
}
?>
<tr class='odd'>
<td class='saveRow' colspan='3'>
<table>
<tr style='line-height:10px;'>
<td class='nowrap templateAdd' style='padding-right:15px;'>
<?php print __('Add Graph Template'); ?>
</td>
<td class='noHide'>
<?php form_dropdown('graph_template_id', $available_graph_templates, 'name', 'id', '', '', ''); ?>
</td>
<td class='noHide'>
<input type='button' class='ui-button ui-corner-all ui-widget' value='<?php print __esc('Add'); ?>' id='add_gt' title='<?php print __esc('Add Graph Template to Device'); ?>'>
</td>
</tr>
</table>
</td>
</tr>
<?php
html_end_box();
if ((isset_request_var('display_dq_details')) && (isset($_SESSION['debug_log']['data_query']))) {
$dbg_copy_uid = generate_hash();
?>
<div id='dqdebug' class='cactiTable'>
<div id='clipboardHeader<?php print $dbg_copy_uid; ?>'>
<div class='cactiTableTitle'>
<span style='padding:3px;'><?php print __('Data Query Debug Information'); ?></span>
</div>
<div class='cactiTableButton'>
<span>
<a class='linkCopyDark cactiTableCopy' id='copyToClipboard<?php print $dbg_copy_uid; ?>'><?php print __('Copy'); ?></a>
<a id='dbghide' class='fa fa-times' href='#'><?php print __('Hide'); ?></a>
</span>
</div>
<table class='cactiTable' id='clipboardData<?php print $dbg_copy_uid; ?>'>
<tr class='tableRow'>
<td class='debug'>
<span><?php print debug_log_return('data_query'); ?></span>
</td>
</tr>
</table>
</div>
</div>
<?php
}
html_start_box(__('Associated Data Queries'), '100%', '', '3', 'center', '');
html_header(
array(
array('display' => __('Data Query Name'), 'align' => 'left', 'nohide' => true),
array('display' => __('Re-Index Method'), 'align' => 'left', 'nohide' => true),
array('display' => __('Status'), 'align' => 'left'),
array('display' => __('Actions'), 'align' => 'right')
)
);
if ($host['snmp_version'] == 0) {
$sql_where1 = ' AND snmp_query.data_input_id != 2';
$sql_where2 = ' WHERE snmp_query.data_input_id != 2 AND';
} else {
$sql_where1 = '';
$sql_where2 = ' WHERE';
}
$sql_where2 .= ' snmp_query.id NOT IN(SELECT snmp_query_id FROM host_snmp_query WHERE host_id = ' . get_request_var('id') . ')';
$selected_data_queries = db_fetch_assoc_prepared(
"SELECT snmp_query.id,
snmp_query.name, host_snmp_query.reindex_method, IFNULL(`items`.`itemCount`, 0) AS itemCount, IFNULL(`rows`.`rowCount`, 0) AS rowCount
FROM snmp_query
INNER JOIN host_snmp_query
ON snmp_query.id = host_snmp_query.snmp_query_id
AND host_snmp_query.host_id = ?
LEFT JOIN (
SELECT snmp_query_id, COUNT(*) AS `itemCount`
FROM host_snmp_cache
WHERE host_id = ?
GROUP BY snmp_query_id
) AS `items`
ON items.snmp_query_id = snmp_query.id
LEFT JOIN (
SELECT snmp_query_id, COUNT(DISTINCT snmp_index) AS `rowCount`
FROM host_snmp_cache
WHERE host_id = ?
GROUP BY snmp_query_id
) AS `rows`
ON rows.snmp_query_id = snmp_query.id
$sql_where1
ORDER BY snmp_query.name",
array(get_request_var('id'), get_request_var('id'), get_request_var('id'))
);
$available_data_queries = db_fetch_assoc("SELECT snmp_query.id, snmp_query.name
FROM snmp_query
$sql_where2
ORDER BY snmp_query.name");
$i = 0;
if (cacti_sizeof($selected_data_queries)) {
foreach ($selected_data_queries as $item) {
$i++;
form_alternate_row("dg$i", true);
$status = 'success';
?>
<td style='padding:4px;'>
<strong><?php print $i; ?>)</strong> <?php print html_escape($item['name']); ?>
</td>
<td class='nowrap'>
<?php device_reindex_methods($item, $host); ?>
</td>
<td>
<?php print(($status == 'success') ? "<span class='success'>" . __('Success') . '</span>' : "<span class='failed'>" . __('Fail')) . '</span>' . __(' [%d Items, %d Rows]', $item['itemCount'], $item['rowCount']); ?>