0.8.7g-2.1+pia2p9+neutron1
[debian/cacti] / data_queries.php
1 <?php
2 /*
3  +-------------------------------------------------------------------------+
4  | Copyright (C) 2004-2010 The Cacti Group                                 |
5  |                                                                         |
6  | This program is free software; you can redistribute it and/or           |
7  | modify it under the terms of the GNU General Public License             |
8  | as published by the Free Software Foundation; either version 2          |
9  | of the License, or (at your option) any later version.                  |
10  |                                                                         |
11  | This program is distributed in the hope that it will be useful,         |
12  | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14  | GNU General Public License for more details.                            |
15  +-------------------------------------------------------------------------+
16  | Cacti: The Complete RRDTool-based Graphing Solution                     |
17  +-------------------------------------------------------------------------+
18  | This code is designed, written, and maintained by the Cacti Group. See  |
19  | about.php and/or the AUTHORS file for specific developer information.   |
20  +-------------------------------------------------------------------------+
21  | http://www.cacti.net/                                                   |
22  +-------------------------------------------------------------------------+
23 */
24
25 include("./include/auth.php");
26 include_once("./lib/data_query.php");
27
28 define("MAX_DISPLAY_PAGES", 21);
29
30 $dq_actions = array(
31         1 => "Delete"
32         );
33
34 /* set default action */
35 if (!isset($_REQUEST["action"])) { $_REQUEST["action"] = ""; }
36
37 switch ($_REQUEST["action"]) {
38         case 'save':
39                 form_save();
40
41                 break;
42         case 'actions':
43                 form_actions();
44
45                 break;
46         case 'item_moveup_dssv':
47                 data_query_item_moveup_dssv();
48
49                 header("Location: data_queries.php?action=item_edit&id=" . $_GET["snmp_query_graph_id"] . "&snmp_query_id=" . $_GET["snmp_query_id"]);
50                 break;
51         case 'item_movedown_dssv':
52                 data_query_item_movedown_dssv();
53
54                 header("Location: data_queries.php?action=item_edit&id=" . $_GET["snmp_query_graph_id"] . "&snmp_query_id=" . $_GET["snmp_query_id"]);
55                 break;
56         case 'item_remove_dssv':
57                 data_query_item_remove_dssv();
58
59                 header("Location: data_queries.php?action=item_edit&id=" . $_GET["snmp_query_graph_id"] . "&snmp_query_id=" . $_GET["snmp_query_id"]);
60                 break;
61         case 'item_moveup_gsv':
62                 data_query_item_moveup_gsv();
63
64                 header("Location: data_queries.php?action=item_edit&id=" . $_GET["snmp_query_graph_id"] . "&snmp_query_id=" . $_GET["snmp_query_id"]);
65                 break;
66         case 'item_movedown_gsv':
67                 data_query_item_movedown_gsv();
68
69                 header("Location: data_queries.php?action=item_edit&id=" . $_GET["snmp_query_graph_id"] . "&snmp_query_id=" . $_GET["snmp_query_id"]);
70                 break;
71         case 'item_remove_gsv':
72                 data_query_item_remove_gsv();
73
74                 header("Location: data_queries.php?action=item_edit&id=" . $_GET["snmp_query_graph_id"] . "&snmp_query_id=" . $_GET["snmp_query_id"]);
75                 break;
76         case 'item_remove':
77                 data_query_item_remove();
78
79                 header("Location: data_queries.php?action=edit&id=" . $_GET["snmp_query_id"]);
80                 break;
81         case 'item_edit':
82                 include_once("./include/top_header.php");
83
84                 data_query_item_edit();
85
86                 include_once("./include/bottom_footer.php");
87                 break;
88         case 'remove':
89                 data_query_remove();
90
91                 header ("Location: data_queries.php");
92                 break;
93         case 'edit':
94                 include_once("./include/top_header.php");
95
96                 data_query_edit();
97
98                 include_once("./include/bottom_footer.php");
99                 break;
100         default:
101                 include_once("./include/top_header.php");
102
103                 data_query();
104
105                 include_once("./include/bottom_footer.php");
106                 break;
107 }
108
109 /* --------------------------
110     The Save Function
111    -------------------------- */
112
113 function form_save() {
114         if (isset($_POST["save_component_snmp_query"])) {
115                 $save["id"] = $_POST["id"];
116                 $save["hash"] = get_hash_data_query($_POST["id"]);
117                 $save["name"] = form_input_validate($_POST["name"], "name", "", false, 3);
118                 $save["description"] = form_input_validate($_POST["description"], "description", "", true, 3);
119                 $save["xml_path"] = form_input_validate($_POST["xml_path"], "xml_path", "", false, 3);
120                 $save["data_input_id"] = $_POST["data_input_id"];
121
122                 if (!is_error_message()) {
123                         $snmp_query_id = sql_save($save, "snmp_query");
124
125                         if ($snmp_query_id) {
126                                 raise_message(1);
127                         }else{
128                                 raise_message(2);
129                         }
130                 }
131
132                 header("Location: data_queries.php?action=edit&id=" . (empty($snmp_query_id) ? $_POST["id"] : $snmp_query_id));
133         }elseif (isset($_POST["save_component_snmp_query_item"])) {
134                 /* ================= input validation ================= */
135                 input_validate_input_number(get_request_var_post("id"));
136                 /* ==================================================== */
137
138                 $redirect_back = false;
139
140                 $save["id"] = $_POST["id"];
141                 $save["hash"] = get_hash_data_query($_POST["id"], "data_query_graph");
142                 $save["snmp_query_id"] = $_POST["snmp_query_id"];
143                 $save["name"] = form_input_validate($_POST["name"], "name", "", false, 3);
144                 $save["graph_template_id"] = $_POST["graph_template_id"];
145
146                 if (!is_error_message()) {
147                         $snmp_query_graph_id = sql_save($save, "snmp_query_graph");
148
149                         if ($snmp_query_graph_id) {
150                                 raise_message(1);
151
152                                 /* if the user changed the graph template, go through and delete everything that
153                                 was associated with the old graph template */
154                                 if ($_POST["graph_template_id"] != $_POST["_graph_template_id"]) {
155                                         db_execute("delete from snmp_query_graph_rrd_sv where snmp_query_graph_id=$snmp_query_graph_id");
156                                         db_execute("delete from snmp_query_graph_sv where snmp_query_graph_id=$snmp_query_graph_id");
157                                         $redirect_back = true;
158                                 }
159
160                                 db_execute("delete from snmp_query_graph_rrd where snmp_query_graph_id=$snmp_query_graph_id");
161
162                                 while (list($var, $val) = each($_POST)) {
163                                         if (eregi("^dsdt_([0-9]+)_([0-9]+)_check", $var)) {
164                                                 $data_template_id = ereg_replace("^dsdt_([0-9]+)_([0-9]+).+", "\\1", $var);
165                                                 $data_template_rrd_id = ereg_replace("^dsdt_([0-9]+)_([0-9]+).+", "\\2", $var);
166
167                                                 db_execute ("replace into snmp_query_graph_rrd (snmp_query_graph_id,data_template_id,data_template_rrd_id,snmp_field_name) values($snmp_query_graph_id,$data_template_id,$data_template_rrd_id,'" . $_POST{"dsdt_" . $data_template_id . "_" . $data_template_rrd_id . "_snmp_field_output"} . "')");
168                                         }elseif ((eregi("^svds_([0-9]+)_x", $var, $matches)) && (!empty($_POST{"svds_" . $matches[1] . "_text"})) && (!empty($_POST{"svds_" . $matches[1] . "_field"}))) {
169                                                 /* suggested values -- data templates */
170                                                 $sequence = get_sequence(0, "sequence", "snmp_query_graph_rrd_sv", "snmp_query_graph_id=" . $_POST["id"]  . " and data_template_id=" . $matches[1] . " and field_name='" . $_POST{"svds_" . $matches[1] . "_field"} . "'");
171                                                 $hash = get_hash_data_query(0, "data_query_sv_data_source");
172                                                 db_execute("insert into snmp_query_graph_rrd_sv (hash,snmp_query_graph_id,data_template_id,sequence,field_name,text) values ('$hash'," . $_POST["id"] . "," . $matches[1] . ",$sequence,'" . $_POST{"svds_" . $matches[1] . "_field"} . "','" . $_POST{"svds_" . $matches[1] . "_text"} . "')");
173
174                                                 $redirect_back = true;
175                                                 clear_messages();
176                                         }elseif ((eregi("^svg_x", $var)) && (!empty($_POST{"svg_text"})) && (!empty($_POST{"svg_field"}))) {
177                                                 /* suggested values -- graph templates */
178                                                 $sequence = get_sequence(0, "sequence", "snmp_query_graph_sv", "snmp_query_graph_id=" . $_POST["id"] . " and field_name='" . $_POST{"svg_field"} . "'");
179                                                 $hash = get_hash_data_query(0, "data_query_sv_graph");
180                                                 db_execute("insert into snmp_query_graph_sv (hash,snmp_query_graph_id,sequence,field_name,text) values ('$hash'," . $_POST["id"] . ",$sequence,'" . $_POST{"svg_field"} . "','" . $_POST{"svg_text"} . "')");
181
182                                                 $redirect_back = true;
183                                                 clear_messages();
184                                         }
185                                 }
186                         }else{
187                                 raise_message(2);
188                         }
189                 }
190
191                 header("Location: data_queries.php?action=item_edit&id=" . (empty($snmp_query_graph_id) ? $_POST["id"] : $snmp_query_graph_id) . "&snmp_query_id=" . $_POST["snmp_query_id"]);
192         }
193 }
194
195 function form_actions() {
196         global $colors, $dq_actions;
197
198         /* if we are to save this form, instead of display it */
199         if (isset($_POST["selected_items"])) {
200                 $selected_items = unserialize(stripslashes($_POST["selected_items"]));
201
202                 if ($_POST["drp_action"] == "1") { /* delete */
203                         for ($i=0;($i<count($selected_items));$i++) {
204                                 /* ================= input validation ================= */
205                                 input_validate_input_number($selected_items[$i]);
206                                 /* ==================================================== */
207
208                                  data_query_remove($selected_items[$i]);
209                         }
210                 }
211
212                 header("Location: data_queries.php");
213                 exit;
214         }
215
216         /* setup some variables */
217         $dq_list = ""; $i = 0;
218
219         /* loop through each of the data queries and process them */
220         while (list($var,$val) = each($_POST)) {
221                 if (ereg("^chk_([0-9]+)$", $var, $matches)) {
222                         /* ================= input validation ================= */
223                         input_validate_input_number($matches[1]);
224                         /* ==================================================== */
225
226                         $dq_list .= "<li>" . htmlspecialchars(db_fetch_cell("SELECT snmp_query.name FROM snmp_query WHERE id='" . $matches[1] . "'")) . "<br>";
227                         $dq_array[$i] = $matches[1];
228
229                         $i++;
230                 }
231         }
232
233         include_once("./include/top_header.php");
234
235         html_start_box("<strong>" . $dq_actions{$_POST["drp_action"]} . "</strong>", "60%", $colors["header_panel"], "3", "center", "");
236
237         print "<form action='data_queries.php' method='post'>\n";
238
239         if (isset($dq_array) && sizeof($dq_array)) {
240                 if ($_POST["drp_action"] == "1") { /* delete */
241                         $graphs = array();
242
243                         print "
244                                 <tr>
245                                         <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'>
246                                                 <p>When you click \"Continue\" the following Data Querie(s) will be deleted.</p>
247                                                 <p><ul>$dq_list</ul></p>
248                                         </td>
249                                 </tr>\n";
250                 }
251
252                 $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'>&nbsp;<input type='submit' value='Continue' title='Delete Data Querie(s)'>";
253         }else{
254                 print "<tr><td bgcolor='#" . $colors["form_alternate1"]. "'><span class='textError'>You must select at least one data query.</span></td></tr>\n";
255                 $save_html = "<input type='button' value='Return' onClick='window.history.back()'>";
256         }
257
258         print " <tr>
259                         <td align='right' bgcolor='#eaeaea'>
260                                 <input type='hidden' name='action' value='actions'>
261                                 <input type='hidden' name='selected_items' value='" . (isset($dq_array) ? serialize($dq_array) : '') . "'>
262                                 <input type='hidden' name='drp_action' value='" . $_POST["drp_action"] . "'>
263                                 $save_html
264                         </td>
265                 </tr>
266                 ";
267
268         html_end_box();
269
270         include_once("./include/bottom_footer.php");
271 }
272
273 /* ----------------------------
274     Data Query Graph Functions
275    ---------------------------- */
276
277 function data_query_item_movedown_gsv() {
278         /* ================= input validation ================= */
279         input_validate_input_number(get_request_var("id"));
280         input_validate_input_number(get_request_var("snmp_query_graph_id"));
281         /* ==================================================== */
282
283         move_item_down("snmp_query_graph_sv", $_GET["id"], "snmp_query_graph_id=" . $_GET["snmp_query_graph_id"] . " and field_name='" . $_GET["field_name"] . "'");
284 }
285
286 function data_query_item_moveup_gsv() {
287         /* ================= input validation ================= */
288         input_validate_input_number(get_request_var("id"));
289         input_validate_input_number(get_request_var("snmp_query_graph_id"));
290         /* ==================================================== */
291
292         move_item_up("snmp_query_graph_sv", $_GET["id"], "snmp_query_graph_id=" . $_GET["snmp_query_graph_id"] . " and field_name='" . $_GET["field_name"] . "'");
293 }
294
295 function data_query_item_remove_gsv() {
296         /* ================= input validation ================= */
297         input_validate_input_number(get_request_var("id"));
298         /* ==================================================== */
299
300         db_execute("delete from snmp_query_graph_sv where id=" . $_GET["id"]);
301 }
302
303 function data_query_item_movedown_dssv() {
304         /* ================= input validation ================= */
305         input_validate_input_number(get_request_var("id"));
306         input_validate_input_number(get_request_var("data_template_id"));
307         input_validate_input_number(get_request_var("snmp_query_graph_id"));
308         /* ==================================================== */
309
310         move_item_down("snmp_query_graph_rrd_sv", $_GET["id"], "data_template_id=" . $_GET["data_template_id"] . " and snmp_query_graph_id=" . $_GET["snmp_query_graph_id"] . " and field_name='" . $_GET["field_name"] . "'");
311 }
312
313 function data_query_item_moveup_dssv() {
314         /* ================= input validation ================= */
315         input_validate_input_number(get_request_var("id"));
316         input_validate_input_number(get_request_var("data_template_id"));
317         input_validate_input_number(get_request_var("snmp_query_graph_id"));
318         /* ==================================================== */
319
320         move_item_up("snmp_query_graph_rrd_sv", $_GET["id"], "data_template_id=" . $_GET["data_template_id"] . " and snmp_query_graph_id=" . $_GET["snmp_query_graph_id"] . " and field_name='" . $_GET["field_name"] . "'");
321 }
322
323 function data_query_item_remove_dssv() {
324         /* ================= input validation ================= */
325         input_validate_input_number(get_request_var("id"));
326         /* ==================================================== */
327
328         db_execute("delete from snmp_query_graph_rrd_sv where id=" . $_GET["id"]);
329 }
330
331 function data_query_item_remove() {
332         /* ================= input validation ================= */
333         input_validate_input_number(get_request_var("id"));
334         input_validate_input_number(get_request_var("snmp_query_id"));
335         /* ==================================================== */
336
337         if ((read_config_option("deletion_verification") == "on") && (!isset($_GET["confirm"]))) {
338                 include("./include/top_header.php");
339                 form_confirm("Are You Sure?", "Are you sure you want to delete the Data Query Graph <strong>'" . db_fetch_cell("select name from snmp_query_graph where id=" . $_GET["id"]) . "'</strong>?", htmlspecialchars("data_queries.php?action=edit&id=" . $_GET["snmp_query_id"]), htmlspecialchars("data_queries.php?action=item_remove&id=" . $_GET["id"] . "&snmp_query_id=" . $_GET["snmp_query_id"]));
340                 include("./include/bottom_footer.php");
341                 exit;
342         }
343
344         if ((read_config_option("deletion_verification") == "") || (isset($_GET["confirm"]))) {
345                 db_execute("delete from snmp_query_graph where id=" . $_GET["id"]);
346                 db_execute("delete from snmp_query_graph_rrd where snmp_query_graph_id=" . $_GET["id"]);
347                 db_execute("delete from snmp_query_graph_rrd_sv where snmp_query_graph_id=" . $_GET["id"]);
348                 db_execute("delete from snmp_query_graph_sv where snmp_query_graph_id=" . $_GET["id"]);
349         }
350 }
351
352 function data_query_item_edit() {
353         global $colors, $fields_data_query_item_edit;
354
355         /* ================= input validation ================= */
356         input_validate_input_number(get_request_var("id"));
357         input_validate_input_number(get_request_var("snmp_query_id"));
358         /* ==================================================== */
359
360         if (!empty($_GET["id"])) {
361                 $snmp_query_item = db_fetch_row("select * from snmp_query_graph where id=" . $_GET["id"]);
362         }
363
364         $snmp_query = db_fetch_row("select name,xml_path from snmp_query where id=" . $_GET["snmp_query_id"]);
365         $header_label = "[edit: " . htmlspecialchars($snmp_query["name"]) . "]";
366
367         html_start_box("<strong>Associated Graph/Data Templates</strong> $header_label", "100%", $colors["header"], "3", "center", "");
368
369         draw_edit_form(array(
370                 "config" => array(),
371                 "fields" => inject_form_variables($fields_data_query_item_edit, (isset($snmp_query_item) ? $snmp_query_item : array()), $_GET)
372                 ));
373
374         html_end_box();
375
376         if (!empty($snmp_query_item["id"])) {
377                 html_start_box("<strong>Associated Data Templates</strong>", "100%", $colors["header"], "3", "center", "");
378
379                 $data_templates = db_fetch_assoc("select
380                         data_template.id,
381                         data_template.name
382                         from (data_template, data_template_rrd, graph_templates_item)
383                         where graph_templates_item.task_item_id=data_template_rrd.id
384                         and data_template_rrd.data_template_id=data_template.id
385                         and data_template_rrd.local_data_id=0
386                         and graph_templates_item.local_graph_id=0
387                         and graph_templates_item.graph_template_id=" . $snmp_query_item["graph_template_id"] . "
388                         group by data_template.id
389                         order by data_template.name");
390
391                 $i = 0;
392                 if (sizeof($data_templates) > 0) {
393                         foreach ($data_templates as $data_template) {
394                                 print " <tr bgcolor='#" . $colors["header_panel"] . "'>
395                                                 <td><span style='color: white; font-weight: bold;'>Data Template - " . $data_template["name"] . "</span></td>
396                                         </tr>";
397
398                                 $data_template_rrds = db_fetch_assoc("select
399                                         data_template_rrd.id,
400                                         data_template_rrd.data_source_name,
401                                         snmp_query_graph_rrd.snmp_field_name,
402                                         snmp_query_graph_rrd.snmp_query_graph_id
403                                         from data_template_rrd
404                                         left join snmp_query_graph_rrd on (snmp_query_graph_rrd.data_template_rrd_id=data_template_rrd.id and snmp_query_graph_rrd.snmp_query_graph_id=" . $_GET["id"] . " and snmp_query_graph_rrd.data_template_id=" . $data_template["id"] . ")
405                                         where data_template_rrd.data_template_id=" . $data_template["id"] . "
406                                         and data_template_rrd.local_data_id=0
407                                         order by data_template_rrd.data_source_name");
408
409                                 $i = 0;
410                                 if (sizeof($data_template_rrds) > 0) {
411                                         foreach ($data_template_rrds as $data_template_rrd) {
412                                                 if (empty($data_template_rrd["snmp_query_graph_id"])) {
413                                                         $old_value = "";
414                                                 }else{
415                                                         $old_value = "on";
416                                                 }
417
418                                                 form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++;
419                                                 ?>
420                                                         <td>
421                                                                 <table cellspacing="0" cellpadding="0" border="0" width="100%">
422                                                                         <tr>
423                                                                                 <td width="200">
424                                                                                         <strong>Data Source:</strong>
425                                                                                 </td>
426                                                                                 <td width="200">
427                                                                                         <?php print $data_template_rrd["data_source_name"];?>
428                                                                                 </td>
429                                                                                 <td width="1">
430                                                                                         <?php
431                                                                                         $snmp_queries = get_data_query_array($_GET["snmp_query_id"]);
432                                                                                         $xml_outputs = array();
433
434                                                                                         while (list($field_name, $field_array) = each($snmp_queries["fields"])) {
435                                                                                                 if ($field_array["direction"] == "output") {
436                                                                                                         $xml_outputs[$field_name] = $field_name . " (" . $field_array["name"] . ")";;
437                                                                                                 }
438                                                                                         }
439
440                                                                                         form_dropdown("dsdt_" . $data_template["id"] . "_" . $data_template_rrd["id"] . "_snmp_field_output",$xml_outputs,"","",$data_template_rrd["snmp_field_name"],"","");?>
441                                                                                 </td>
442                                                                                 <td align="right">
443                                                                                         <?php form_checkbox("dsdt_" . $data_template["id"] . "_" . $data_template_rrd["id"] . "_check", $old_value, "", "", "", $_GET["id"]); print "<br>";?>
444                                                                                 </td>
445                                                                         </tr>
446                                                                 </table>
447                                                         </td>
448                                                 </tr>
449                                                 <?php
450                                         }
451                                 }
452                         }
453                 }
454
455                 html_end_box();
456
457                 html_start_box("<strong>Suggested Values</strong>", "100%", $colors["header"], "0", "center", "");
458
459                 reset($data_templates);
460
461                 /* suggested values for data templates */
462                 if (sizeof($data_templates) > 0) {
463                 foreach ($data_templates as $data_template) {
464                         $suggested_values = db_fetch_assoc("select
465                                 text,
466                                 field_name,
467                                 id
468                                 from snmp_query_graph_rrd_sv
469                                 where snmp_query_graph_id=" . $_GET["id"] . "
470                                 and data_template_id=" . $data_template["id"] . "
471                                 order by field_name,sequence");
472
473                         print " <tr bgcolor='#" . $colors["header_panel"] . "'>
474                                         <td style='padding: 3px;'><span style='color: white; font-weight: bold;'>Data Template - " . htmlspecialchars($data_template["name"]) . "</span></td>
475                                 </tr>";
476
477                         $i = 0;
478                         if (sizeof($suggested_values) > 0) {
479                                 print "<tr><td><table cellspacing='0' cellpadding='3' border='0' width='100%'>\n";
480
481                                 foreach ($suggested_values as $suggested_value) {
482                                         form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++;
483                                         ?>
484                                                 <td width="120">
485                                                         <strong><?php print htmlspecialchars($suggested_value["field_name"]);?></strong>
486                                                 </td>
487                                                 <td>
488                                                         <?php print htmlspecialchars($suggested_value["text"]);?>
489                                                 </td>
490                                                 <td width="70">
491                                                         <a href="<?php print htmlspecialchars("data_queries.php?action=item_movedown_dssv&snmp_query_graph_id=" . $_GET["id"] . "&id=". $suggested_value["id"] . "&snmp_query_id=" . $_GET["snmp_query_id"] . "&data_template_id=" . $data_template["id"] . "&field_name=" . $suggested_value["field_name"]);?>"><img src="images/move_down.gif" border="0" alt="Move Down"></a>
492                                                         <a href="<?php print htmlspecialchars("data_queries.php?action=item_moveup_dssv&snmp_query_graph_id=" . $_GET["id"] . "&id=" . $suggested_value["id"] . "&snmp_query_id=" . $_GET["snmp_query_id"] . "&data_template_id=" . $data_template["id"] . "&field_name=" . $suggested_value["field_name"]);?>"><img src="images/move_up.gif" border="0" alt="Move Up"></a>
493                                                 </td>
494                                                 <td align="right">
495                                                         <a href="<?php print htmlspecialchars("data_queries.php?action=item_remove_dssv&snmp_query_graph_id=" . $_GET["id"] . "&id=" . $suggested_value["id"] . "&snmp_query_id=" . $_GET["snmp_query_id"] . "&data_template_id=" . $data_template["id"]);?>"><img src="images/delete_icon.gif" width="10" style="height:10px;width:10px;" border="0" alt="Delete"></a>
496                                                 </td>
497                                         </tr>
498                                         <?php
499                                 }
500
501                                 print "</table></td></tr>\n";
502                         }
503
504                         form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i);
505                         ?>
506                                 <td>
507                                         <table cellspacing="0" cellpadding="3" border="0" width="100%">
508                                                 <tr>
509                                                         <td width="1">
510                                                                 <input type="text" name="svds_<?php print $data_template["id"];?>_text" size="60">
511                                                         </td>
512                                                         <td width="220" nowrap>
513                                                                 &nbsp;Field Name:&nbsp;<input type="text" name="svds_<?php print $data_template["id"];?>_field" size="15">
514                                                         </td>
515                                                         <td>
516                                                                 &nbsp;<input type="submit" name="svds_<?php print $data_template["id"];?>_x" value="Add" title="Add Data Source Name Suggested Name">
517                                                         </td>
518                                                 </tr>
519                                         </table>
520                                 </td>
521                         </tr>
522                         <?php
523                 }
524                 }
525
526                 /* suggested values for graphs templates */
527                 $suggested_values = db_fetch_assoc("select
528                         text,
529                         field_name,
530                         id
531                         from snmp_query_graph_sv
532                         where snmp_query_graph_id=" . $_GET["id"] . "
533                         order by field_name,sequence");
534
535                 print " <tr bgcolor='#" . $colors["header_panel"] . "'>
536                                 <td style='padding: 3px;'><span style='color: white; font-weight: bold;'>Graph Template - " . htmlspecialchars(db_fetch_cell("select name from graph_templates where id=" . $snmp_query_item["graph_template_id"])) . "</span></td>
537                         </tr>";
538
539                 $i = 0;
540                 if (sizeof($suggested_values) > 0) {
541                         print "<tr><td><table cellspacing='0' cellpadding='3' border='0' width='100%'>\n";
542
543                         foreach ($suggested_values as $suggested_value) {
544                                 form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++;
545                                 ?>
546                                         <td width="120">
547                                                 <strong><?php print htmlspecialchars($suggested_value["field_name"]);?></strong>
548                                         </td>
549                                         <td>
550                                                 <?php print htmlspecialchars($suggested_value["text"]);?>
551                                         </td>
552                                         <td width="70">
553                                                 <a href="<?php print htmlspecialchars("data_queries.php?action=item_movedown_gsv&snmp_query_graph_id=" . $_GET["id"] . "&id=" . $suggested_value["id"] . "&snmp_query_id=" . $_GET["snmp_query_id"] . "&field_name=" . $suggested_value["field_name"]);?>"><img src="images/move_down.gif" border="0" alt="Move Down"></a>
554                                                 <a href="<?php print htmlspecialchars("data_queries.php?action=item_moveup_gsv&snmp_query_graph_id=" . $_GET["id"] . "&id=" . $suggested_value["id"] . "&snmp_query_id=" . $_GET["snmp_query_id"] . "&field_name=" . $suggested_value["field_name"]);?>"><img src="images/move_up.gif" border="0" alt="Move Up"></a>
555                                         </td>
556                                         <td align="right">
557                                                 <a href="<?php print htmlspecialchars("data_queries.php?action=item_remove_gsv&snmp_query_graph_id=" . $_GET["id"] . "&id=" . $suggested_value["id"] . "&snmp_query_id=" . $_GET["snmp_query_id"]);?>"><img src="images/delete_icon.gif" style="height:10px;width:10px;" border="0" alt="Delete"></a>
558                                         </td>
559                                 </tr>
560                                 <?php
561                         }
562
563                         print "</table></td></tr>\n";
564                 }
565
566                 form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i);
567                 ?>
568                         <td>
569                                 <table cellspacing="0" cellpadding="3" border="0" width="100%">
570                                         <tr>
571                                                 <td width="1">
572                                                         <input type="text" name="svg_text" size="60">
573                                                 </td>
574                                                 <td width="220" nowrap>
575                                                         &nbsp;Field Name:&nbsp;<input type="text" name="svg_field" size="15">
576                                                 </td>
577                                                 <td>
578                                                         &nbsp;<input type="submit" name="svg_x" value="Add" title="Add Graph Title Suggested Name">
579                                                 </td>
580                                         </tr>
581                                 </table>
582                         </td>
583                 </tr>
584                 <?php
585
586                 html_end_box();
587         }
588
589         form_save_button("data_queries.php?action=edit&id=" . $_GET["snmp_query_id"], "return");
590 }
591
592 /* ---------------------
593     Data Query Functions
594    --------------------- */
595
596 function data_query_remove($id) {
597         $snmp_query_graph = db_fetch_assoc("select id from snmp_query_graph where snmp_query_id=" . $id);
598
599         if (sizeof($snmp_query_graph) > 0) {
600         foreach ($snmp_query_graph as $item) {
601                 db_execute("delete from snmp_query_graph_rrd where snmp_query_graph_id=" . $item["id"]);
602         }
603         }
604
605         db_execute("delete from snmp_query where id=" . $id);
606         db_execute("delete from snmp_query_graph where snmp_query_id=" . $id);
607         db_execute("delete from host_template_snmp_query where snmp_query_id=" . $id);
608         db_execute("delete from host_snmp_query where snmp_query_id=" . $id);
609         db_execute("delete from host_snmp_cache where snmp_query_id=" . $id);
610 }
611
612 function data_query_edit() {
613         global $colors, $fields_data_query_edit, $config;
614
615         /* ================= input validation ================= */
616         input_validate_input_number(get_request_var("id"));
617         /* ==================================================== */
618
619         if (!empty($_GET["id"])) {
620                 $snmp_query = db_fetch_row("select * from snmp_query where id=" . $_GET["id"]);
621                 $header_label = "[edit: " . htmlspecialchars($snmp_query["name"]) . "]";
622         }else{
623                 $header_label = "[new]";
624         }
625
626         html_start_box("<strong>Data Queries</strong> $header_label", "100%", $colors["header"], "3", "center", "");
627
628         draw_edit_form(array(
629                 "config" => array(),
630                 "fields" => inject_form_variables($fields_data_query_edit, (isset($snmp_query) ? $snmp_query : array()))
631                 ));
632
633         html_end_box();
634
635         if (!empty($snmp_query["id"])) {
636                 $xml_filename = str_replace("<path_cacti>", $config["base_path"], $snmp_query["xml_path"]);
637
638                 if ((file_exists($xml_filename)) && (is_file($xml_filename))) {
639                         $text = "<font color='#0d7c09'><strong>Successfully located XML file</strong></font>";
640                         $xml_file_exists = true;
641                 }else{
642                         $text = "<font color='#ff0000'><strong>Could not locate XML file.</strong></font>";
643                         $xml_file_exists = false;
644                 }
645
646                 html_start_box("", "100%", "aaaaaa", "3", "center", "");
647                 print "<tr bgcolor='#f5f5f5'><td>$text</td></tr>";
648                 html_end_box();
649
650                 if ($xml_file_exists == true) {
651                         html_start_box("<strong>Associated Graph Templates</strong>", "100%", $colors["header"], "3", "center", "data_queries.php?action=item_edit&snmp_query_id=" . $snmp_query["id"]);
652
653                         print " <tr bgcolor='#" . $colors["header_panel"] . "'>
654                                         <td><span style='color: white; font-weight: bold;'>Name</span></td>
655                                         <td><span style='color: white; font-weight: bold;'>Graph Template Name</span></td>
656                                         <td></td>
657                                 </tr>";
658
659                         $snmp_query_graphs = db_fetch_assoc("select
660                                 snmp_query_graph.id,
661                                 graph_templates.name as graph_template_name,
662                                 snmp_query_graph.name
663                                 from snmp_query_graph
664                                 left join graph_templates on (snmp_query_graph.graph_template_id=graph_templates.id)
665                                 where snmp_query_graph.snmp_query_id=" . $snmp_query["id"] . "
666                                 order by snmp_query_graph.name");
667
668                         $i = 0;
669                         if (sizeof($snmp_query_graphs) > 0) {
670                         foreach ($snmp_query_graphs as $snmp_query_graph) {
671                                 form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++;
672                                 ?>
673                                         <td>
674                                                 <strong><a href="<?php print htmlspecialchars("data_queries.php?action=item_edit&id=" . $snmp_query_graph["id"] . "&snmp_query_id=" . $snmp_query["id"]);?>"><?php print htmlspecialchars($snmp_query_graph["name"]);?></a></strong>
675                                         </td>
676                                         <td>
677                                                 <?php print htmlspecialchars($snmp_query_graph["graph_template_name"]);?>
678                                         </td>
679                                         <td align="right">
680                                                 <a href="<?php print htmlspecialchars("data_queries.php?action=item_remove&id=" . $snmp_query_graph["id"] . "&snmp_query_id=" . $snmp_query["id"]);?>"><img src="images/delete_icon.gif" style="height:10px;width:10px;" border="0" alt="Delete"></a>
681                                         </td>
682                                 </tr>
683                                 <?php
684                         }
685                         }else{
686                                 print "<tr><td><em>No Graph Templates Defined.</em></td></tr>";
687                         }
688
689                         html_end_box();
690                 }
691         }
692
693         form_save_button("data_queries.php", "return");
694 }
695
696 function data_query() {
697         global $colors, $dq_actions;
698
699         /* clean up search string */
700         if (isset($_REQUEST["filter"])) {
701                 $_REQUEST["filter"] = sanitize_search_string(get_request_var("filter"));
702         }
703
704         /* clean up sort_column */
705         if (isset($_REQUEST["sort_column"])) {
706                 $_REQUEST["sort_column"] = sanitize_search_string(get_request_var("sort_column"));
707         }
708
709         /* clean up search string */
710         if (isset($_REQUEST["sort_direction"])) {
711                 $_REQUEST["sort_direction"] = sanitize_search_string(get_request_var("sort_direction"));
712         }
713
714         /* if the user pushed the 'clear' button */
715         if (isset($_REQUEST["clear_x"])) {
716                 kill_session_var("sess_data_queries_filter");
717                 kill_session_var("sess_data_queries_sort_column");
718                 kill_session_var("sess_data_queries_sort_direction");
719
720                 unset($_REQUEST["page"]);
721                 unset($_REQUEST["filter"]);
722                 unset($_REQUEST["sort_column"]);
723                 unset($_REQUEST["sort_direction"]);
724                 $_REQUEST["page"] = 1;
725         }
726
727         /* remember these search fields in session vars so we don't have to keep passing them around */
728         load_current_session_value("sort_column", "sess_data_queries_sort_column", "name");
729         load_current_session_value("sort_direction", "sess_data_queries_sort_direction", "ASC");
730         load_current_session_value("page", "sess_data_queries_current_page", "1");
731         load_current_session_value("filter", "sess_data_queries_filter", "");
732
733         html_start_box("<strong>Data Queries</strong>", "100%", $colors["header"], "3", "center", "data_queries.php?action=edit");
734
735         ?>
736         <tr bgcolor="#<?php print $colors["panel"];?>" class="noprint">
737                 <td class="noprint">
738                 <form name="form_graph_id" method="get" action="data_queries.php">
739                         <table width="100%" cellpadding="0" cellspacing="0">
740                                 <tr class="noprint">
741                                         <td nowrap style='white-space: nowrap;' width="50">
742                                                 Search:&nbsp;
743                                         </td>
744                                         <td width="1">
745                                                 <input type="text" name="filter" size="40" value="<?php print htmlspecialchars(get_request_var_request("filter"));?>">
746                                         </td>
747                                         <td nowrap style='white-space: nowrap;'>
748                                                 &nbsp;<input type="submit" value="Go" title="Set/Refresh Filters">
749                                                 <input type="submit" name="clear_x" value="Clear" title="Clear Filters">
750                                         </td>
751                                 </tr>
752                         </table>
753                         <input type='hidden' name='page' value='1'>
754                 </form>
755                 </td>
756         </tr>
757         <?php
758
759         html_end_box();
760
761         /* print checkbox form for validation */
762         print "<form name='chk' method='post' action='data_queries.php'>\n";
763
764         html_start_box("", "100%", $colors["header"], "3", "center", "");
765
766         /* form the 'where' clause for our main sql query */
767         if (strlen(get_request_var_request("filter"))) {
768                 $sql_where = "where (snmp_query.name like '%%" . get_request_var_request("filter") . "%%' OR data_input.name like '%%" . get_request_var_request("filter") . "%%')";
769         }else{
770                 $sql_where = "";
771         }
772
773         $total_rows = db_fetch_cell("SELECT
774                 count(*)
775                 FROM snmp_query INNER JOIN data_input ON (snmp_query.data_input_id=data_input.id)
776                 $sql_where");
777
778         $snmp_queries = db_fetch_assoc("SELECT
779                 snmp_query.id,
780                 snmp_query.name,
781                 data_input.name AS data_input_method
782                 FROM snmp_query INNER JOIN data_input ON (snmp_query.data_input_id=data_input.id)
783                 $sql_where
784                 ORDER BY " . get_request_var_request("sort_column") . " " . get_request_var_request("sort_direction") . "
785                 LIMIT " . (read_config_option("num_rows_device")*(get_request_var_request("page")-1)) . "," . read_config_option("num_rows_device"));
786
787         /* generate page list */
788         $url_page_select = get_page_list(get_request_var_request("page"), MAX_DISPLAY_PAGES, read_config_option("num_rows_device"), $total_rows, "data_queries.php?filter=" . get_request_var_request("filter"));
789
790         $nav = "<tr bgcolor='#" . $colors["header"] . "'>
791                         <td colspan='7'>
792                                 <table width='100%' cellspacing='0' cellpadding='0' border='0'>
793                                         <tr>
794                                                 <td align='left' class='textHeaderDark'>
795                                                         <strong>&lt;&lt; "; if (get_request_var_request("page") > 1) { $nav .= "<a class='linkOverDark' href='" . htmlspecialchars("data_queries.php?filter=" . get_request_var_request("filter") . "&page=" . (get_request_var_request("page")-1)) . "'>"; } $nav .= "Previous"; if (get_request_var_request("page") > 1) { $nav .= "</a>"; } $nav .= "</strong>
796                                                 </td>\n
797                                                 <td align='center' class='textHeaderDark'>
798                                                         Showing Rows " . ((read_config_option("num_rows_device")*(get_request_var_request("page")-1))+1) . " to " . ((($total_rows < read_config_option("num_rows_device")) || ($total_rows < (read_config_option("num_rows_device")*get_request_var_request("page")))) ? $total_rows : (read_config_option("num_rows_device")*get_request_var_request("page"))) . " of $total_rows [$url_page_select]
799                                                 </td>\n
800                                                 <td align='right' class='textHeaderDark'>
801                                                         <strong>"; if ((get_request_var_request("page") * read_config_option("num_rows_device")) < $total_rows) { $nav .= "<a class='linkOverDark' href='" . htmlspecialchars("data_queries.php?filter=" . get_request_var_request("filter") . "&page=" . (get_request_var_request("page")+1)) . "'>"; } $nav .= "Next"; if ((get_request_var_request("page") * read_config_option("num_rows_device")) < $total_rows) { $nav .= "</a>"; } $nav .= " &gt;&gt;</strong>
802                                                 </td>\n
803                                         </tr>
804                                 </table>
805                         </td>
806                 </tr>\n";
807
808         print $nav;
809
810         $display_text = array(
811                 "name" => array("Name", "ASC"),
812                 "data_input_method" => array("Data Input Method", "ASC"));
813
814         html_header_sort_checkbox($display_text, get_request_var_request("sort_column"), get_request_var_request("sort_direction"), false);
815
816         $i = 0;
817         if (sizeof($snmp_queries) > 0) {
818                 foreach ($snmp_queries as $snmp_query) {
819                         form_alternate_row_color($colors["alternate"],$colors["light"],$i, 'line' . $snmp_query["id"]); $i++;
820                         form_selectable_cell("<a class='linkEditMain' href='" . htmlspecialchars("data_queries.php?action=edit&id=" . $snmp_query["id"]) . "'>" . (strlen(get_request_var_request("filter")) ? eregi_replace("(" . preg_quote(get_request_var_request("filter")) . ")", "<span style='background-color: #F8D93D;'>\\1</span>", htmlspecialchars($snmp_query["name"])) : htmlspecialchars($snmp_query["name"])) . "</a>", $snmp_query["id"]);
821                         form_selectable_cell((strlen(get_request_var_request("filter")) ? eregi_replace("(" . preg_quote(get_request_var_request("filter")) . ")", "<span style='background-color: #F8D93D;'>\\1</span>", $snmp_query["data_input_method"]) : $snmp_query["data_input_method"]), $snmp_query["id"]);
822                         form_checkbox_cell($snmp_query["name"], $snmp_query["id"]);
823                         form_end_row();
824                 }
825
826                 print $nav;
827         }else{
828                 print "<tr><td><em>No Data Queries</em></td></tr>";
829         }
830
831         html_end_box(false);
832
833         /* draw the dropdown containing a list of available actions for this form */
834         draw_actions_dropdown($dq_actions);
835 }
836 ?>
837