forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automation_templates.php
610 lines (504 loc) · 19.1 KB
/
automation_templates.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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2017 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');
$at_actions = array(
1 => __('Delete')
);
/* set default action */
set_default_action();
switch (get_request_var('action')) {
case 'save':
form_save();
break;
case 'ajax_dnd':
automation_template_dnd();
break;
case 'actions':
form_actions();
break;
case 'movedown':
automation_movedown();
header('Location: automation_templates.php?header=false');
break;
case 'moveup':
automation_moveup();
header('Location: automation_templates.php?header=false');
break;
case 'remove':
automation_remove();
header('Location: automation_templates.php?header=false');
break;
case 'edit':
top_header();
template_edit();
bottom_footer();
break;
default:
top_header();
template();
bottom_footer();
break;
}
function automation_template_dnd() {
/* ================= Input validation ================= */
get_filter_request_var('id');
/* ================= Input validation ================= */
if (!isset_request_var('template_ids') || !is_array(get_nfilter_request_var('template_ids'))) exit;
/* template table contains one row defined as 'nodrag&nodrop' */
// unset($_REQUEST['template_ids'][0]);
/* delivered template ids has to be exactly the same like we have stored */
$old_order = array();
$i = 1;
foreach(get_nfilter_request_var('template_ids') as $sequence => $id) {
if (empty($id)) continue;
$new_order[$i] = str_replace('line', '', $id);
$i++;
}
$items = db_fetch_assoc('SELECT id, sequence FROM automation_templates ORDER BY sequence');
if (sizeof($items)) {
$i = 1;
foreach($items as $item) {
$old_order[$i] = $item['id'];
$i++;
}
} else {
header('Location: automation_templates.php?header=false');
exit;
}
/* perform some sanity checks */
if (sizeof(array_diff($new_order, $old_order)) > 0) {
header('Location: automation_templates.php?header=false');
exit;
}
if (sizeof(array_diff_key($new_order, $old_order))>0) {
header('Location: automation_templates.php?header=false');
exit;
}
foreach($new_order as $sequence => $id) {
input_validate_input_number($sequence);
input_validate_input_number($id);
db_execute_prepared('UPDATE automation_templates SET sequence = ? WHERE id = ?', array($sequence, $id));
}
header('Location: automation_templates.php?header=false');
exit;
}
function automation_movedown() {
move_item_down('automation_templates', get_filter_request_var('id'));
}
function automation_moveup() {
move_item_up('automation_templates', get_filter_request_var('id'));
}
function automation_remove() {
db_execute_prepared('DELETE FROM automation_templates WHERE id = ?', array(get_filter_request_var('id')));
}
function form_actions() {
global $at_actions;
/* ================= input validation ================= */
get_filter_request_var('drp_action', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z0-9_]+)$/')));
/* ==================================================== */
/* if we are to save this form, instead of display it */
if (isset_request_var('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
if (get_nfilter_request_var('drp_action') == '1') { /* delete */
db_execute('DELETE FROM automation_templates WHERE ' . array_to_sql_or($selected_items, 'id'));
}
}
header('Location: automation_templates.php?header=false');
exit;
}
/* setup some variables */
$at_list = ''; $i = 0;
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
$at_list .= '<li>' . html_escape(db_fetch_cell_prepared('SELECT ht.name FROM automation_templates AS at INNER JOIN host_template AS ht ON ht.id=at.host_template WHERE at.id = ?', array($matches[1]))) . '</li>';
$at_array[$i] = $matches[1];
$i++;
}
}
top_header();
form_start('automation_templates.php');
html_start_box($at_actions[get_nfilter_request_var('drp_action')], '60%', '', '3', 'center', '');
if (isset($at_array) && sizeof($at_array)) {
if (get_nfilter_request_var('drp_action') == '1') { /* delete */
print "<tr>
<td class='textArea' class='odd'>
<p>" . __('Click \'Continue\' to delete the folling Automation Template(s).') . "</p>
<div class='itemlist'><ul>$at_list</ul></div>
</td>
</tr>\n";
$save_html = "<input type='button' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc('Delete Automation Template(s)') . "'>";
}
} else {
print "<tr><td class='odd'><span class='textError'>" . __('You must select at least one Automation Template.') . "</span></td></tr>\n";
$save_html = "<input type='button' value='" . __esc('Return') . "' onClick='cactiReturnTo()'>";
}
print "<tr>
<td class='saveRow'>
<input type='hidden' name='action' value='actions'>
<input type='hidden' name='selected_items' value='" . (isset($at_array) ? serialize($at_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();
}
function form_save() {
if (isset_request_var('save_component_template')) {
$redirect_back = false;
$save['id'] = get_nfilter_request_var('id');
$save['host_template'] = form_input_validate(get_nfilter_request_var('host_template'), 'host_template', '', false, 3);
$save['availability_method'] = form_input_validate(get_nfilter_request_var('availability_method'), 'availability_method', '', false, 3);
$save['sysDescr'] = get_nfilter_request_var('sysDescr');
$save['sysName'] = get_nfilter_request_var('sysName');
$save['sysOid'] = get_nfilter_request_var('sysOid');
if (function_exists('filter_var')) {
$save['sysDescr'] = filter_var($save['sysDescr'], FILTER_SANITIZE_STRING);
} else {
$save['sysDescr'] = strip_tags($save['sysDescr']);
}
if (!is_error_message()) {
$template_id = sql_save($save, 'automation_templates');
if ($template_id) {
raise_message(1);
} else {
raise_message(2);
}
}
if (is_error_message() || isempty_request_var('id')) {
header('Location: automation_templates.php?header=false&id=' . (empty($template_id) ? get_nfilter_request_var('id') : $template_id));
} else {
header('Location: automation_templates.php?header=false');
}
}
}
function automation_get_child_branches($tree_id, $id, $spaces, $headers) {
$items = db_fetch_assoc_prepared('SELECT id, title
FROM graph_tree_items
WHERE graph_tree_id = ?
AND host_id=0
AND local_graph_id=0
AND parent = ?
ORDER BY position', array($tree_id, $id));
$spaces .= '--';
if (sizeof($items)) {
foreach($items as $i) {
$headers['tr_' . $tree_id . '_bi_' . $i['id']] = $spaces . ' ' . $i['title'];
$headers = automation_get_child_branches($tree_id, $i['id'], $spaces, $headers);
}
}
return $headers;
}
function automation_get_tree_headers() {
$headers = array();
$trees = db_fetch_assoc('SELECT id, name FROM graph_tree ORDER BY name');
foreach ($trees as $tree) {
$headers['tr_' . $tree['id'] . '_br_0'] = $tree['name'];
$spaces = '';
$headers = automation_get_child_branches($tree['id'], 0, $spaces, $headers);
}
return $headers;
}
function template_edit() {
global $availability_options;
$host_template_names = db_fetch_assoc('SELECT id, name FROM host_template');
$template_names = array();
if (sizeof($host_template_names)) {
foreach ($host_template_names as $ht) {
$template_names[$ht['id']] = $ht['name'];
}
}
$fields_automation_template_edit = array(
'host_template' => array(
'method' => 'drop_array',
'friendly_name' => __('Device Template'),
'description' => __('Select a Device Template that Devices will be matched to.'),
'value' => '|arg1:host_template|',
'array' => $template_names,
),
'availability_method' => array(
'method' => 'drop_array',
'friendly_name' => __('Availability Method'),
'description' => __('Choose the Availability Method to use for Discovered Devices.'),
'value' => '|arg1:availability_method|',
'default' => read_config_option('availability_method'),
'array' => $availability_options,
),
'sysDescr' => array(
'method' => 'textbox',
'friendly_name' => __('System Description Match'),
'description' => __('This is a unique string that will be matched to a devices sysDescr string to pair it to this Automation Template. Any Perl regular expression can be used in addition to any SQL Where expression.'),
'value' => '|arg1:sysDescr|',
'max_length' => '255',
),
'sysName' => array(
'method' => 'textbox',
'friendly_name' => __('System Name Match'),
'description' => __('This is a unique string that will be matched to a devices sysName string to pair it to this Automation Template. Any Perl regular expression can be used in addition to any SQL Where expression.'),
'value' => '|arg1:sysName|',
'max_length' => '128',
),
'sysOid' => array(
'method' => 'textbox',
'friendly_name' => __('System OID Match'),
'description' => __('This is a unique string that will be matched to a devices sysOid string to pair it to this Automation Template. Any Perl regular expression can be used in addition to any SQL Where expression.'),
'value' => '|arg1:sysOid|',
'max_length' => '128',
),
'id' => array(
'method' => 'hidden_zero',
'value' => '|arg1:id|'
),
'save_component_template' => array(
'method' => 'hidden',
'value' => '1'
)
);
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
if (!isempty_request_var('id')) {
$host_template = db_fetch_row_prepared('SELECT *
FROM automation_templates
WHERE id = ?',
array(get_request_var('id')));
if (isset($template_names[$host_template['host_template']])) {
$header_label = __('Automation Templates [edit: %s]', html_escape($template_names[$host_template['host_template']]));
} else {
$header_label = __('Automation Templates for [Deleted Template]');
}
} else {
$header_label = __('Automation Templates [new]');
set_request_var('id', 0);
}
form_start('automation_templates.php', 'form_network');
html_start_box($header_label, '100%', true, '3', 'center', '');
draw_edit_form(
array(
'config' => array('no_form_tag' => 'true'),
'fields' => inject_form_variables($fields_automation_template_edit, (isset($host_template) ? $host_template : array()))
)
);
html_end_box(true, true);
form_save_button('automation_templates.php');
}
function template() {
global $at_actions, $item_rows, $availability_options;
/* ================= input validation and session storage ================= */
$filters = array(
'rows' => array(
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
),
'page' => array(
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
),
'filter' => array(
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => '',
'options' => array('options' => 'sanitize_search_string')
)
);
validate_store_request_vars($filters, 'sess_autot');
/* ================= input validation ================= */
if (get_request_var('rows') == '-1') {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
html_start_box(__('Device Automation Templates'), '100%', '', '3', 'center', 'automation_templates.php?action=edit');
?>
<tr class='even'>
<td>
<form id='form_at' action='automation_templates.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search');?>
</td>
<td>
<input id='filter' type='text' size='25' value='<?php print html_escape_request_var('filter');?>'>
</td>
<td>
<?php print __('Templates');?>
</td>
<td>
<select id='rows' onChange='applyFilter()'>
<option value='-1'<?php print (get_request_var('rows') == '-1' ? ' selected>':'>') . __('Default');?></option>
<?php
if (sizeof($item_rows) > 0) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'"; if (get_request_var('rows') == $key) { print ' selected'; } print '>' . html_escape($value) . "</option>\n";
}
}
?>
</select>
</td>
<td>
<span>
<input type='button' id='refresh' value='<?php print __esc('Go');?>' title='<?php print __esc('Set/Refresh Filters');?>'>
<input type='button' id='clear' value='<?php print __esc('Clear');?>' title='<?php print __esc('Clear Filters');?>'>
</span>
</td>
</tr>
</table>
</form>
<script type='text/javascript'>
function applyFilter() {
strURL = 'automation_templates.php' +
'?filter=' + $('#filter').val() +
'&rows=' + $('#rows').val() +
'&has_graphs=' + $('#has_graphs').is(':checked') +
'&header=false';
loadPageNoHeader(strURL);
}
function clearFilter() {
strURL = 'automation_templates.php?clear=1&header=false';
loadPageNoHeader(strURL);
}
$(function() {
$('#refresh').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#form_at').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
</script>
</td>
</tr>
<?php
html_end_box();
/* form the 'where' clause for our main sql query */
if (get_request_var('filter') != '') {
$sql_where = "WHERE (name LIKE '%" . get_request_var('filter') . "%' OR " .
"sysName LIKE '%" . get_request_var('filter') . "%' OR " .
"sysDescr LIKE '%" . get_request_var('filter') . "%' OR " .
"sysOID LIKE '%" . get_request_var('filter') . "%')";
} else {
$sql_where = '';
}
$total_rows = db_fetch_cell("SELECT COUNT(*)
FROM automation_templates AS at
LEFT JOIN host_template AS ht
ON ht.id=at.host_template
$sql_where");
$dts = db_fetch_assoc("SELECT at.*, '' AS sysName, ht.name
FROM automation_templates AS at
LEFT JOIN host_template AS ht
ON ht.id=at.host_template
$sql_where
ORDER BY sequence " .
' LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows);
$nav = html_nav_bar('automation_templates.php', MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 7, __('Templates'), 'page', 'main');
form_start('automation_templates.php', 'chk');
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
$display_text = array(
array('display' => __('Template Name'), 'align' => 'left'),
array('display' => __('Availability Method'), 'align' => 'left'),
array('display' => __('System Description Match'), 'align' => 'left'),
array('display' => __('System Name Match'), 'align' => 'left'),
array('display' => __('System ObjectId Match'), 'align' => 'left')
);
if (read_config_option('drag_and_drop') == '') {
$display_text[] = array('display' => __('Order'), 'align' => 'center');
}
html_header_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false);
$i = 1;
$total_items = sizeof($dts);
if (sizeof($dts)) {
foreach ($dts as $dt) {
if ($dt['name'] == '') {
$name = __('Unknown Template');
} else {
$name = $dt['name'];
}
form_alternate_row('line' . $dt['id'], true);
form_selectable_cell(filter_value($name, get_request_var('filter'), 'automation_templates.php?action=edit&id=' . $dt['id']), $dt['id']);
form_selectable_cell($availability_options[$dt['availability_method']], $dt['id']);
form_selectable_cell(filter_value($dt['sysDescr'], get_request_var('filter')), $dt['id']);
form_selectable_cell(filter_value($dt['sysName'], get_request_var('filter')), $dt['id']);
form_selectable_cell(filter_value($dt['sysOid'], get_request_var('filter')), $dt['id']);
if (read_config_option('drag_and_drop') == '') {
$add_text = '';
if ($i < $total_items && $total_items > 1) {
$add_text .= '<a class="pic fa fa-caret-down moveArrow" href="' . html_escape('automation_templates.php?action=movedown&id=' . $dt['id']) . '" title="' . __esc('Move Down') . '"></a>';
} else {
$add_text .= '<span class="moveArrowNone"></span>';
}
if ($i > 1 && $i <= $total_items) {
$add_text .= '<a class="pic fa fa-caret-up moveArrow" href="' . html_escape('automation_templates.php?action=moveup&id=' . $dt['id']) . '" title="' . __esc('Move Up') . '"></a>';
} else {
$add_text .= '<span class="moveArrowNone"></span>';
}
form_selectable_cell($add_text, $dt['id'], '', 'center');
}
form_checkbox_cell($name, $dt['id']);
form_end_row();
$i++;
}
} else {
print "<tr><td><em>" . __('No Automation Device Templates Found') . "</em></td></tr>\n";
}
html_end_box(false);
if (sizeof($dts)) {
print $nav;
}
/* draw the dropdown containing a list of available actions for this form */
draw_actions_dropdown($at_actions);
form_end();
?>
<script type='text/javascript'>
$(function() {
$('#automation_templates2_child').attr('id', 'template_ids');
$('img.action').click(function() {
strURL = $(this).attr('href');
loadPageNoHeader(strURL);
});
<?php if (read_config_option('drag_and_drop') == 'on') { ?>
$('#template_ids').find('tr:first').addClass('nodrag').addClass('nodrop');
$('#template_ids').tableDnD({
onDrop: function(table, row) {
loadPageNoHeader('automation_templates.php?action=ajax_dnd&'+$.tableDnD.serialize());
}
});
<?php } ?>
});
</script>
<?php
}