Repository navigation
Expand file tree
/
Copy paththold_add.php
More file actions
586 lines (516 loc) · 20.3 KB
/
Copy paththold_add.php
File metadata and controls
586 lines (516 loc) · 20.3 KB
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
<?php
/*
ex: set tabstop=4 shiftwidth=4 autoindent:
+-------------------------------------------------------------------------+
| Copyright (C) 2011 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
chdir('../../');
include_once('./include/auth.php');
$host = $graph = $ds = $dt = '';
if (isset($_REQUEST['hostid']) && $_REQUEST['hostid'] != '') {
input_validate_input_number($_REQUEST['hostid']);
$host = $_REQUEST['hostid'];
} else {
$host = 0;
}
if (isset($_SERVER["HTTP_REFERER"]) && (substr_count($_SERVER["HTTP_REFERER"], "graph_view.php") || substr_count($_SERVER["HTTP_REFERER"], "graph.php"))) {
$_SESSION["graph_return"] = $_SERVER["HTTP_REFERER"];
}
if (isset($_REQUEST['graphid']) && $_REQUEST['graphid'] != '') {
input_validate_input_number($_REQUEST['graphid']);
$graph = $_REQUEST['graphid'];
if ($host == 0) {
$host = db_fetch_cell('SELECT host_id FROM graph_local WHERE id = ' . $graph);
}
} else {
$graph = 0;
}
if (isset($_REQUEST['doaction']) && $_REQUEST['doaction'] != '') {
input_validate_input_number($_REQUEST['graphid']);
$graph = $_REQUEST['graphid'];
if ($_REQUEST['doaction'] == 1) {
header("Location:" . $config['url_path'] . "plugins/thold/thold_add.php?graphid=$graph\n\n");
} else {
$temp = db_fetch_row("SELECT dtr.*
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON gti.task_item_id=dtr.id
LEFT JOIN graph_local AS gl
ON gl.id=gti.local_graph_id
WHERE gl.id=$graph");
$dt = $temp['data_template_id'];
header("Location:" . $config['url_path'] . "plugins/thold/thold_templates.php?action=add&data_template_id=$dt\n\n");
}
exit;
}
if (isset($_REQUEST['dsid']) && $_REQUEST['dsid'] != '') {
input_validate_input_number($_REQUEST['dsid']);
$ds = $_REQUEST['dsid'];
}
if (isset($_REQUEST['dt']) && $_REQUEST['dt'] != '') {
// Correct the data template if we have a datasource
if ($ds) {
$dt = db_fetch_cell("SELECT local_data_id FROM data_template_rrd WHERE id = $ds");
} else {
input_validate_input_number($_REQUEST['dt']);
$dt = $_REQUEST['dt'];
}
}
if (isset($_POST['save']) && $_POST['save'] == 'save') {
header("Location: thold.php?rra=$dt&view_rrd=$ds\n\n");
exit;
}
if (isset($_REQUEST['usetemplate']) && $_REQUEST['usetemplate'] != '') {
if (isset($_REQUEST['thold_template_id']) && $_REQUEST['thold_template_id'] != '') {
if ($_REQUEST['thold_template_id'] == '0') {
thold_add_select_host();
} else {
thold_add_graphs_action_execute();
}
} else {
thold_add_graphs_action_prepare($graph);
}
} else {
thold_add_select_host();
}
function thold_add_graphs_action_execute() {
global $config, $host, $graph;
include_once($config['base_path'] . '/plugins/thold/thold_functions.php');
$message = "";
input_validate_input_number($_REQUEST["thold_template_id"]);
$template = db_fetch_row("SELECT * FROM thold_template WHERE id=" . $_REQUEST["thold_template_id"]);
$temp = db_fetch_row("SELECT dtr.*
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON gti.task_item_id=dtr.id
LEFT JOIN graph_local AS gl
ON gl.id=gti.local_graph_id
WHERE gl.id=$graph");
$data_template_id = $temp['data_template_id'];
$local_data_id = $temp['local_data_id'];
$data_source = db_fetch_row("SELECT * FROM data_local WHERE id=" . $local_data_id);
$data_template_id = $data_source['data_template_id'];
/* allow duplicate thresholds, but only from differing templates */
$existing = db_fetch_assoc('SELECT id
FROM thold_data
WHERE rra_id=' . $local_data_id . '
AND data_id=' . $data_template_id . '
AND template=' . $template['id'] . " AND template_enabled='on'");
if (count($existing) == 0 && count($template)) {
if ($graph) {
$rrdlookup = db_fetch_cell("SELECT id FROM data_template_rrd WHERE local_data_id=$local_data_id order by id LIMIT 1");
$grapharr = db_fetch_row("SELECT graph_template_id FROM graph_templates_item WHERE task_item_id=$rrdlookup and local_graph_id = $graph");
$desc = db_fetch_cell('SELECT name_cache FROM data_template_data WHERE local_data_id=' . $local_data_id . ' LIMIT 1');
$data_source_name = $template['data_source_name'];
$insert = array();
$name = thold_format_name($template, $graph, $local_data_id, $data_source_name);
$insert['name'] = $name;
$insert['host_id'] = $data_source['host_id'];
$insert['rra_id'] = $local_data_id;
$insert['graph_id'] = $graph;
$insert['data_template'] = $data_template_id;
$insert['graph_template'] = $grapharr['graph_template_id'];
$insert['thold_hi'] = $template['thold_hi'];
$insert['thold_low'] = $template['thold_low'];
$insert['thold_fail_trigger'] = $template['thold_fail_trigger'];
$insert['thold_enabled'] = $template['thold_enabled'];
$insert['thold_warning_hi'] = $template['thold_warning_hi'];
$insert['thold_warning_low'] = $template['thold_warning_low'];
$insert['thold_warning_fail_trigger'] = $template['thold_warning_fail_trigger'];
$insert['bl_ref_time_range'] = $template['bl_ref_time_range'];
$insert['bl_pct_down'] = $template['bl_pct_down'];
$insert['bl_pct_up'] = $template['bl_pct_up'];
$insert['bl_fail_trigger'] = $template['bl_fail_trigger'];
$insert['bl_alert'] = $template['bl_alert'];
$insert['repeat_alert'] = $template['repeat_alert'];
$insert['notify_extra'] = $template['notify_extra'];
$insert['alert_phones_extra'] = $template['alert_phones_extra'];
$insert['alert_command'] = $template['alert_command'];
$insert['warning_command'] = $template['warning_command'];
$insert['cdef'] = $template['cdef'];
$insert['template'] = $template['id'];
$insert['template_enabled'] = 'on';
$rrdlist = db_fetch_assoc("SELECT id, data_input_field_id
FROM data_template_rrd
WHERE local_data_id='$local_data_id'
AND data_source_name='$data_source_name'");
$int = array('id', 'data_template_id', 'data_source_id', 'thold_fail_trigger', 'bl_ref_time_range', 'bl_pct_down', 'bl_pct_up', 'bl_fail_trigger', 'bl_alert', 'repeat_alert', 'cdef');
foreach ($rrdlist as $rrdrow) {
$data_rrd_id = $rrdrow['id'];
$insert['data_id'] = $data_rrd_id;
$existing = db_fetch_assoc("SELECT id
FROM thold_data
WHERE rra_id='$local_data_id'
AND data_id='$data_rrd_id'
AND template='" . $template['id'] . "' AND template_enabled='on'");
if (count($existing) == 0) {
$insert['id'] = 0;
$id = sql_save($insert, 'thold_data');
if ($id) {
thold_template_update_threshold ($id, $insert['template']);
$l = db_fetch_assoc("SELECT name FROM data_template where id=$data_template_id");
$tname = $l[0]['name'];
$name = $data_source_name;
if ($rrdrow['data_input_field_id'] != 0) {
$l = db_fetch_assoc('SELECT name FROM data_input_fields where id=' . $rrdrow['data_input_field_id']);
$name = $l[0]['name'];
}
plugin_thold_log_changes($id, 'created', " $tname [$name]");
$message .= "Created threshold for the Graph '<i>$tname</i>' using the Data Source '<i>$name</i>'<br>";
}
}
}
}
}
if (strlen($message)) {
$_SESSION['thold_message'] = "<font size=-2>$message</font>";
}else{
$_SESSION['thold_message'] = "<font size=-2>Threshold(s) Already Exists - No Thresholds Created</font>";
}
raise_message('thold_created');
if (isset($_SESSION["graph_return"])) {
$return_to = $_SESSION["graph_return"];
unset($_SESSION["graph_return"]);
kill_session_var("graph_return");
header('Location: ' . $return_to);
}else{
header("Location:" . $config['url_path'] . "plugins/thold/listthold.php\n\n");
}
}
function thold_add_graphs_action_prepare($graph) {
global $colors, $config;
include($config['include_path'] . '/top_header.php');
html_start_box("<strong>Create Threshold from Template</strong>", "60%", $colors["header_panel"], "3", "center", "");
print "<form action='thold_add.php' method='POST'>\n";
/* get the valid thold templates
* remove those hosts that do not have any valid templates
*/
$templates = "";
$found_list = "";
$not_found = "";
$data_template_id = db_fetch_cell("SELECT dtr.data_template_id
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON gti.task_item_id=dtr.id
LEFT JOIN graph_local AS gl
ON gl.id=gti.local_graph_id
WHERE gl.id=$graph");
if ($data_template_id != "") {
if (sizeof(db_fetch_assoc("SELECT id FROM thold_template WHERE data_template_id=$data_template_id"))) {
$found_list .= "<li>" . get_graph_title($graph) . "</li>";
if (strlen($templates)) {
$templates .= ", $data_template_id";
}else{
$templates = "$data_template_id";
}
}else{
$not_found .= "<li>" . get_graph_title($graph) . "</li>";
}
}else{
$not_found .= "<li>" . get_graph_title($graph) . "</li>";
}
if (strlen($templates)) {
$sql = "SELECT id, name FROM thold_template WHERE data_template_id IN (" . $templates . ") ORDER BY name";
}else{
$sql = "SELECT id, name FROM thold_template ORDER BY name";
}
print " <tr>
<td colspan='2' class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>";
if (strlen($found_list)) {
if (strlen($not_found)) {
print "<p>The following Graph has no Threshold Templates associated with them</p>";
print "<ul>" . $not_found . "</ul>";
}
print "<p>Are you sure you wish to create Thresholds for this Graph?
<ul>" . $found_list . "</ul>
</td>
</tr>\n
";
if (isset($_REQUEST["tree_id"])) {
input_validate_input_number($_REQUEST["tree_id"]);
}else{
$_REQUEST["tree_id"] = "";
}
if (isset($_REQUEST["leaf_id"])) {
input_validate_input_number($_REQUEST["leaf_id"]);
}else{
$_REQUEST["leaf_id"] = "";
}
$form_array = array(
'general_header' => array(
'friendly_name' => 'Available Threshold Templates',
'method' => 'spacer',
),
'thold_template_id' => array(
'method' => 'drop_sql',
'friendly_name' => 'Select a Threshold Template',
'description' => '',
'none_value' => 'None',
'value' => 'None',
'sql' => $sql
),
'tree_id' => array(
'method' => 'hidden',
'value' => $_REQUEST['tree_id']
),
'action2' => array(
'method' => 'hidden',
'value' => $_REQUEST['action2']
),
'leaf_id' => array(
'method' => 'hidden',
'value' => $_REQUEST['leaf_id']
),
'usetemplate' => array(
'method' => 'hidden',
'value' => 1
),
'graphid' => array(
'method' => 'hidden',
'value' => $graph
)
);
draw_edit_form(
array(
"config" => array("no_form_tag" => true),
"fields" => $form_array
)
);
}else{
if (strlen($not_found)) {
print "<p>There are no Threshold Templates associated with the following Graph</p>";
print "<ul>" . $not_found . "</ul>";
}
if (isset($_REQUEST["tree_id"])) {
input_validate_input_number($_REQUEST["tree_id"]);
}else{
$_REQUEST["tree_id"] = "";
}
if (isset($_REQUEST["leaf_id"])) {
input_validate_input_number($_REQUEST["leaf_id"]);
}else{
$_REQUEST["leaf_id"] = "";
}
$form_array = array(
'general_header' => array(
'friendly_name' => 'Please select an action',
'method' => 'spacer',
),
'doaction' => array(
'method' => 'drop_array',
'friendly_name' => '',
'description' => '',
'value' => 'None',
'array' => array(1=>'Create a new Threshold', 2=>'Create a Threshold Template')
),
'tree_id' => array(
'method' => 'hidden',
'value' => $_REQUEST['tree_id']
),
'action2' => array(
'method' => 'hidden',
'value' => $_REQUEST['action2']
),
'leaf_id' => array(
'method' => 'hidden',
'value' => $_REQUEST['leaf_id']
),
'usetemplate' => array(
'method' => 'hidden',
'value' => 1
),
'graphid' => array(
'method' => 'hidden',
'value' => $graph
)
);
draw_edit_form(
array(
"config" => array("no_form_tag" => true),
"fields" => $form_array
)
);
}
if (!strlen($not_found)) {
$save_html = "<input type='submit' value='Yes'>";
print " <tr>
<td align='right' bgcolor='#eaeaea'>
<input type='hidden' name='action' value='actions'>
<input type='button' onClick='javascript:history.go(-1)' value='No' title='Cancel'>
$save_html
</td>
</tr>";
} else {
$save_html = "<input type='submit' value='Go'>";
print " <tr>
<td align='right' bgcolor='#eaeaea'>
<input type='button' onClick='javascript:history.go(-1)' value='Cancel' title='Cancel'>
$save_html
</td>
</tr>";
}
html_end_box();
include_once("./include/bottom_footer.php");
}
function thold_add_graphs_action_array($action) {
$action['plugin_thold_create'] = 'Create Threshold from Template';
return $action;
}
function thold_add_select_host() {
global $colors, $config, $host, $graph, $ds;
/* get policy information for the sql where clause */
$current_user = db_fetch_row("SELECT * FROM user_auth WHERE id=" . $_SESSION["sess_user_id"]);
$sql_where = get_graph_permissions_sql($current_user["policy_graphs"], $current_user["policy_hosts"], $current_user["policy_graph_templates"]);
$hosts = db_fetch_assoc("SELECT DISTINCT host.id, CONCAT_WS('',host.description,' (',host.hostname,')') AS name
FROM (graph_templates_graph, host)
LEFT JOIN graph_local ON (graph_local.host_id=host.id)
LEFT JOIN graph_templates ON (graph_templates.id=graph_local.graph_template_id)
LEFT JOIN user_auth_perms ON ((graph_templates_graph.local_graph_id=user_auth_perms.item_id AND user_auth_perms.type=1 AND user_auth_perms.user_id=" . $_SESSION["sess_user_id"] . ") OR (host.id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id=" . $_SESSION["sess_user_id"] . ") OR (graph_templates.id=user_auth_perms.item_id AND user_auth_perms.type=4 AND user_auth_perms.user_id=" . $_SESSION["sess_user_id"] . "))
WHERE graph_templates_graph.local_graph_id=graph_local.id
" . (empty($sql_where) ? "" : "AND $sql_where") . "
ORDER BY name");
include($config['include_path'] . '/top_header.php');
html_start_box('<strong>Threshold Creation Wizard</strong>', '50%', $colors['header'], '3', 'center', '');
echo '<tr><td><form action="thold_add.php" method="post" name="tholdform">';
if ($host == '') {
print '<center><h3>Please select a Host</h3></center>';
} else if ($graph == '') {
print '<center><h3>Please select a Graph</h3></center>';
} else if ($ds == '') {
print '<center><h3>Please select a Data Source</h3></center>';
} else {
print '<center><h3>Please press "Create" to activate your Threshold</h3></center>';
}
/* display the host dropdown */
?>
<center><table>
<tr>
<td width='70' style='white-space:nowrap;'>
<b>Host:</b>
</td>
<td style='width:1;'>
<select name=hostid onChange="applyTholdFilterChange(document.tholdform, 'host')">
<option value=""></option><?php
foreach ($hosts as $row) {
echo "<option value='" . $row['id'] . "'" . ($row['id'] == $host ? ' selected' : '') . '>' . $row['name'] . '</option>';
}?>
</select>
</td>
</tr><?php
if ($host != '') {
$graphs = db_fetch_assoc("SELECT
graph_templates_graph.id,
graph_templates_graph.local_graph_id,
graph_templates_graph.title_cache
FROM (graph_templates_graph,graph_local)
LEFT JOIN host ON (host.id=graph_local.host_id)
LEFT JOIN graph_templates ON (graph_templates.id=graph_local.graph_template_id)
LEFT JOIN user_auth_perms ON ((graph_templates_graph.local_graph_id=user_auth_perms.item_id and user_auth_perms.type=1 and user_auth_perms.user_id=" . $_SESSION["sess_user_id"] . ") OR (host.id=user_auth_perms.item_id and user_auth_perms.type=3 and user_auth_perms.user_id=" . $_SESSION["sess_user_id"] . ") OR (graph_templates.id=user_auth_perms.item_id and user_auth_perms.type=4 and user_auth_perms.user_id=" . $_SESSION["sess_user_id"] . "))
WHERE graph_templates_graph.local_graph_id=graph_local.id
AND graph_templates.id IS NOT NULL
" . (empty($sql_where) ? "" : "AND $sql_where") . "
AND host.id = $host
ORDER BY title_cache");
/* display the graphs dropdown */
?>
<tr>
<td width='70' style='white-space:nowrap;'>
<b>Graph:</b>
</td>
<td>
<select name=graphid onChange="applyTholdFilterChange(document.tholdform, 'graph')">
<option value=""></option><?php
foreach ($graphs as $row) {
echo "<option value='" . $row['local_graph_id'] . "'" . ($row['local_graph_id'] == $graph ? ' selected' : '') . '>' . $row['title_cache'] . '</option>';
}?>
</select>
</td>
</tr><?php
} else {
?>
<tr>
<td>
<input type=hidden name=graphid value="">
</td>
</tr><?php
}
if ($graph != '') {
$dt_sql = 'SELECT DISTINCT dtr.local_data_id
FROM data_template_rrd AS dtr
LEFT JOIN graph_templates_item AS gti
ON gti.task_item_id=dtr.id
LEFT JOIN graph_local AS gl
ON gl.id=gti.local_graph_id
WHERE gl.id = ' . $graph;
$dt = db_fetch_cell($dt_sql);
$dss = db_fetch_assoc('SELECT DISTINCT id, data_source_name
FROM data_template_rrd
WHERE local_data_id IN (' . $dt_sql . ') ORDER BY data_source_name');
/* show the data source options */
?>
<tr>
<td width='70' style='white-space:nowrap;'>
<b>Data Source:</b>
</td>
<td>
<input type=hidden name=dt value="<?php print $dt;?>">
<select name=dsid onChange="applyTholdFilterChange(document.tholdform, 'ds')">
<option value=""></option><?php
foreach ($dss as $row) {
echo "<option value='" . $row['id'] . "'" . ($row['id'] == $ds ? ' selected' : '') . '>' . $row['data_source_name'] . '</option>';
}?>
</select>
</td>
</tr><?php
} else {
?>
<tr>
<td>
<input type=hidden name=dsid value="">
</td>
</tr><?php
}
if ($ds != '') {
echo '<tr><td colspan=2><input type=hidden name=save value="save"><br><center><input type="submit" value="Create" title="Create Threshold"></center></td></tr>';
} else {
echo '<tr><td colspan=2><br><br><br></td></tr>';
}
echo '</table></form></td></tr>';
html_end_box();
if ($graph != '') {
print "<br><center><img id=graphi name=graphi src='../../graph_image.php?local_graph_id=$graph&rra_id=0'><center><br><br>";
}
}
?>
<script type="text/javascript">
<!--
function applyTholdFilterChange(objForm, target) {
strURL = '?hostid=' + objForm.hostid.value;
if (target != 'host') {
strURL = strURL + '&graphid=' + objForm.graphid.value;
}
if (target == 'ds') {
strURL = strURL + '&dsid=' + objForm.dsid.value;
}
document.location = strURL;
}
-->
</script>