Imported Upstream version 0.8.7g
[debian/cacti] / graphs_new.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 include_once("./lib/utility.php");
28 include_once("./lib/sort.php");
29 include_once("./lib/html_form_template.php");
30 include_once("./lib/template.php");
31
32 define("MAX_DISPLAY_PAGES", 21);
33
34 /* set default action */
35 if (!isset($_REQUEST["action"])) { $_REQUEST["action"] = ""; }
36 switch ($_REQUEST["action"]) {
37         case 'save':
38                 form_save();
39
40                 break;
41         case 'query_reload':
42                 host_reload_query();
43
44                 header("Location: graphs_new.php?host_id=" . $_GET["host_id"]);
45                 break;
46         default:
47                 include_once("./include/top_header.php");
48
49                 graphs();
50
51                 include_once("./include/bottom_footer.php");
52                 break;
53 }
54
55 /* --------------------------
56     The Save Function
57    -------------------------- */
58
59 function form_save() {
60         if (isset($_POST["save_component_graph"])) {
61                 /* summarize the 'create graph from host template/snmp index' stuff into an array */
62                 while (list($var, $val) = each($_POST)) {
63                         if (preg_match('/^cg_(\d+)$/', $var, $matches)) {
64                                 $selected_graphs["cg"]{$matches[1]}{$matches[1]} = true;
65                         }elseif (preg_match('/^cg_g$/', $var)) {
66                                 if ($_POST["cg_g"] > 0) {
67                                         $selected_graphs["cg"]{$_POST["cg_g"]}{$_POST["cg_g"]} = true;
68                                 }
69                         }elseif (preg_match('/^sg_(\d+)_([a-f0-9]{32})$/', $var, $matches)) {
70                                 $selected_graphs["sg"]{$matches[1]}{$_POST{"sgg_" . $matches[1]}}{$matches[2]} = true;
71                         }
72                 }
73
74                 if (isset($selected_graphs)) {
75                         host_new_graphs($_POST["host_id"], $_POST["host_template_id"], $selected_graphs);
76                         exit;
77                 }
78
79                 header("Location: graphs_new.php?host_id=" . $_POST["host_id"]);
80         }
81
82         if (isset($_POST["save_component_new_graphs"])) {
83                 host_new_graphs_save();
84
85                 header("Location: graphs_new.php?host_id=" . $_POST["host_id"]);
86         }
87 }
88
89 /* ---------------------
90     Misc Functions
91    --------------------- */
92
93 function draw_edit_form_row($field_array, $field_name, $previous_value) {
94         $field_array["value"] = $previous_value;
95
96         draw_edit_form(
97                 array(
98                         "config" => array(
99                                 "no_form_tag" => true,
100                                 "force_row_color" => "F5F5F5"
101                                 ),
102                         "fields" => array(
103                                 $field_name => $field_array
104                                 )
105                         )
106                 );
107 }
108
109 /* -------------------
110     Data Query Functions
111    ------------------- */
112
113 function host_reload_query() {
114         run_data_query($_GET["host_id"], $_GET["id"]);
115 }
116
117 /* -------------------
118     New Graph Functions
119    ------------------- */
120
121 function host_new_graphs_save() {
122         $selected_graphs_array = unserialize(stripslashes($_POST["selected_graphs_array"]));
123
124         /* form an array that contains all of the data on the previous form */
125         while (list($var, $val) = each($_POST)) {
126                 if (preg_match("/^g_(\d+)_(\d+)_(\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: field_name */
127                         if (empty($matches[1])) { /* this is a new graph from template field */
128                                 $values["cg"]{$matches[2]}["graph_template"]{$matches[3]} = $val;
129                         }else{ /* this is a data query field */
130                                 $values["sg"]{$matches[1]}{$matches[2]}["graph_template"]{$matches[3]} = $val;
131                         }
132                 }elseif (preg_match("/^gi_(\d+)_(\d+)_(\d+)_(\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: graph_template_input_id, 4:field_name */
133                         /* ================= input validation ================= */
134                         input_validate_input_number($matches[3]);
135                         /* ==================================================== */
136
137                         /* we need to find out which graph items will be affected by saving this particular item */
138                         $item_list = db_fetch_assoc("select
139                                 graph_template_item_id
140                                 from graph_template_input_defs
141                                 where graph_template_input_id=" . $matches[3]);
142
143                         /* loop through each item affected and update column data */
144                         if (sizeof($item_list) > 0) {
145                         foreach ($item_list as $item) {
146                                 if (empty($matches[1])) { /* this is a new graph from template field */
147                                         $values["cg"]{$matches[2]}["graph_template_item"]{$item["graph_template_item_id"]}{$matches[4]} = $val;
148                                 }else{ /* this is a data query field */
149                                         $values["sg"]{$matches[1]}{$matches[2]}["graph_template_item"]{$item["graph_template_item_id"]}{$matches[4]} = $val;
150                                 }
151                         }
152                         }
153                 }elseif (preg_match("/^d_(\d+)_(\d+)_(\d+)_(\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:field_name */
154                         if (empty($matches[1])) { /* this is a new graph from template field */
155                                 $values["cg"]{$matches[2]}["data_template"]{$matches[3]}{$matches[4]} = $val;
156                         }else{ /* this is a data query field */
157                                 $values["sg"]{$matches[1]}{$matches[2]}["data_template"]{$matches[3]}{$matches[4]} = $val;
158                         }
159                 }elseif (preg_match("/^c_(\d+)_(\d+)_(\d+)_(\d+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:data_input_field_id */
160                         if (empty($matches[1])) { /* this is a new graph from template field */
161                                 $values["cg"]{$matches[2]}["custom_data"]{$matches[3]}{$matches[4]} = $val;
162                         }else{ /* this is a data query field */
163                                 $values["sg"]{$matches[1]}{$matches[2]}["custom_data"]{$matches[3]}{$matches[4]} = $val;
164                         }
165                 }elseif (preg_match("/^di_(\d+)_(\d+)_(\d+)_(\d+)_(\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:local_data_template_rrd_id, 5:field_name */
166                         if (empty($matches[1])) { /* this is a new graph from template field */
167                                 $values["cg"]{$matches[2]}["data_template_item"]{$matches[4]}{$matches[5]} = $val;
168                         }else{ /* this is a data query field */
169                                 $values["sg"]{$matches[1]}{$matches[2]}["data_template_item"]{$matches[4]}{$matches[5]} = $val;
170                         }
171                 }
172         }
173
174         debug_log_clear("new_graphs");
175
176         while (list($form_type, $form_array) = each($selected_graphs_array)) {
177                 $current_form_type = $form_type;
178
179                 while (list($form_id1, $form_array2) = each($form_array)) {
180                         /* enumerate information from the arrays stored in post variables */
181                         if ($form_type == "cg") {
182                                 $graph_template_id = $form_id1;
183                         }elseif ($form_type == "sg") {
184                                 while (list($form_id2, $form_array3) = each($form_array2)) {
185                                         $snmp_index_array = $form_array3;
186
187                                         $snmp_query_array["snmp_query_id"] = $form_id1;
188                                         $snmp_query_array["snmp_index_on"] = get_best_data_query_index_type($_POST["host_id"], $form_id1);
189                                         $snmp_query_array["snmp_query_graph_id"] = $form_id2;
190                                 }
191
192                                 $graph_template_id = db_fetch_cell("select graph_template_id from snmp_query_graph where id=" . $snmp_query_array["snmp_query_graph_id"]);
193                         }
194
195                         if ($current_form_type == "cg") {
196                                 $return_array = create_complete_graph_from_template($graph_template_id, $_POST["host_id"], "", $values["cg"]);
197
198                                 debug_log_insert("new_graphs", "Created graph: " . get_graph_title($return_array["local_graph_id"]));
199
200                                 /* lastly push host-specific information to our data sources */
201                                 if (sizeof($return_array["local_data_id"])) { # we expect at least one data source associated
202                                         foreach($return_array["local_data_id"] as $item) {
203                                                 push_out_host($_POST["host_id"], $item);
204                                         }
205                                 } else {
206                                         debug_log_insert("new_graphs", "ERROR: no Data Source associated. Check Template");
207                                 }
208                         }elseif ($current_form_type == "sg") {
209                                 while (list($snmp_index, $true) = each($snmp_index_array)) {
210                                         $snmp_query_array["snmp_index"] = decode_data_query_index($snmp_index, $snmp_query_array["snmp_query_id"], $_POST["host_id"]);
211
212                                         $return_array = create_complete_graph_from_template($graph_template_id, $_POST["host_id"], $snmp_query_array, $values["sg"]{$snmp_query_array["snmp_query_id"]});
213
214                                         debug_log_insert("new_graphs", "Created graph: " . get_graph_title($return_array["local_graph_id"]));
215
216                                         /* lastly push host-specific information to our data sources */
217                                         if (sizeof($return_array["local_data_id"])) { # we expect at least one data source associated
218                                                 foreach($return_array["local_data_id"] as $item) {
219                                                         push_out_host($_POST["host_id"], $item);
220                                                 }
221                                         } else {
222                                                 debug_log_insert("new_graphs", "ERROR: no Data Source associated. Check Template");
223                                         }
224                                 }
225                         }
226                 }
227         }
228 }
229
230 function host_new_graphs($host_id, $host_template_id, $selected_graphs_array) {
231         global $colors;
232
233         /* we use object buffering on this page to allow redirection to another page if no
234         fields are actually drawn */
235         ob_start();
236
237         include_once("./include/top_header.php");
238
239         print "<form method='post' action='graphs_new.php'>\n";
240
241         $snmp_query_id = 0;
242         $num_output_fields = array();
243
244         while (list($form_type, $form_array) = each($selected_graphs_array)) {
245                 while (list($form_id1, $form_array2) = each($form_array)) {
246                         if ($form_type == "cg") {
247                                 $graph_template_id = $form_id1;
248
249                                 html_start_box("<strong>Create Graph from '" . db_fetch_cell("select name from graph_templates where id=$graph_template_id") . "'", "100%", $colors["header"], "3", "center", "");
250                         }elseif ($form_type == "sg") {
251                                 while (list($form_id2, $form_array3) = each($form_array2)) {
252                                         /* ================= input validation ================= */
253                                         input_validate_input_number($snmp_query_id);
254                                         /* ==================================================== */
255
256                                         $snmp_query_id = $form_id1;
257                                         $snmp_query_graph_id = $form_id2;
258                                         $num_graphs = sizeof($form_array3);
259
260                                         $snmp_query = db_fetch_row("select
261                                                 snmp_query.name,
262                                                 snmp_query.xml_path
263                                                 from snmp_query
264                                                 where snmp_query.id=$snmp_query_id");
265
266                                         $graph_template_id = db_fetch_cell("select graph_template_id from snmp_query_graph where id=$snmp_query_graph_id");
267                                 }
268
269                                 /* DRAW: Data Query */
270                                 html_start_box("<strong>Create $num_graphs Graph" . (($num_graphs>1) ? "s" : "") . " from '" . db_fetch_cell("select name from snmp_query where id=$snmp_query_id") . "'", "100%", $colors["header"], "3", "center", "");
271                         }
272
273                         /* ================= input validation ================= */
274                         input_validate_input_number($graph_template_id);
275                         /* ==================================================== */
276
277                         $data_templates = db_fetch_assoc("select
278                                 data_template.name as data_template_name,
279                                 data_template_rrd.data_source_name,
280                                 data_template_data.*
281                                 from (data_template, data_template_rrd, data_template_data, graph_templates_item)
282                                 where graph_templates_item.task_item_id=data_template_rrd.id
283                                 and data_template_rrd.data_template_id=data_template.id
284                                 and data_template_data.data_template_id=data_template.id
285                                 and data_template_rrd.local_data_id=0
286                                 and data_template_data.local_data_id=0
287                                 and graph_templates_item.local_graph_id=0
288                                 and graph_templates_item.graph_template_id=" . $graph_template_id . "
289                                 group by data_template.id
290                                 order by data_template.name");
291
292                         $graph_template = db_fetch_row("select
293                                 graph_templates.name as graph_template_name,
294                                 graph_templates_graph.*
295                                 from (graph_templates, graph_templates_graph)
296                                 where graph_templates.id=graph_templates_graph.graph_template_id
297                                 and graph_templates.id=" . $graph_template_id . "
298                                 and graph_templates_graph.local_graph_id=0");
299                         $graph_template_name = db_fetch_cell("select name from graph_templates where id=" . $graph_template_id);
300
301                         array_push($num_output_fields, draw_nontemplated_fields_graph($graph_template_id, $graph_template, "g_$snmp_query_id" . "_" . $graph_template_id . "_|field|", "<strong>Graph</strong> [Template: " . $graph_template["graph_template_name"] . "]", false, false, (isset($snmp_query_graph_id) ? $snmp_query_graph_id : 0)));
302                         array_push($num_output_fields, draw_nontemplated_fields_graph_item($graph_template_id, 0, "gi_" . $snmp_query_id . "_" . $graph_template_id . "_|id|_|field|", "<strong>Graph Items</strong> [Template: " . $graph_template_name . "]", false));
303
304                         /* DRAW: Data Sources */
305                         if (sizeof($data_templates) > 0) {
306                         foreach ($data_templates as $data_template) {
307                                 array_push($num_output_fields, draw_nontemplated_fields_data_source($data_template["data_template_id"], 0, $data_template, "d_" . $snmp_query_id . "_" . $graph_template_id . "_" . $data_template["data_template_id"] . "_|field|", "<strong>Data Source</strong> [Template: " . $data_template["data_template_name"] . "]", false, false, (isset($snmp_query_graph_id) ? $snmp_query_graph_id : 0)));
308
309                                 $data_template_items = db_fetch_assoc("select
310                                         data_template_rrd.*
311                                         from data_template_rrd
312                                         where data_template_rrd.data_template_id=" . $data_template["data_template_id"] . "
313                                         and local_data_id=0");
314
315                                 array_push($num_output_fields, draw_nontemplated_fields_data_source_item($data_template["data_template_id"], $data_template_items, "di_" . $snmp_query_id . "_" . $graph_template_id . "_" . $data_template["data_template_id"] . "_|id|_|field|", "", false, false, false, (isset($snmp_query_graph_id) ? $snmp_query_graph_id : 0)));
316                                 array_push($num_output_fields, draw_nontemplated_fields_custom_data($data_template["id"], "c_" . $snmp_query_id . "_" . $graph_template_id . "_" . $data_template["data_template_id"] . "_|id|", "<strong>Custom Data</strong> [Template: " . $data_template["data_template_name"] . "]", false, false, $snmp_query_id));
317                         }
318                         }
319
320                         html_end_box();
321                 }
322         }
323
324         /* no fields were actually drawn on the form; just save without prompting the user */
325         if (array_sum($num_output_fields) == 0) {
326                 ob_end_clean();
327
328                 /* since the user didn't actually click "Create" to POST the data; we have to
329                 pretend like they did here */
330                 $_POST["host_template_id"] = $host_template_id;
331                 $_POST["host_id"] = $host_id;
332                 $_POST["save_component_new_graphs"] = "1";
333                 $_POST["selected_graphs_array"] = serialize($selected_graphs_array);
334
335                 host_new_graphs_save();
336
337                 header("Location: graphs_new.php?host_id=" . $_POST["host_id"]);
338                 exit;
339         }
340
341         /* flush the current output buffer to the browser */
342         ob_end_flush();
343
344         form_hidden_box("host_template_id", $host_template_id, "0");
345         form_hidden_box("host_id", $host_id, "0");
346         form_hidden_box("save_component_new_graphs", "1", "");
347         print "<input type='hidden' name='selected_graphs_array' value='" . serialize($selected_graphs_array) . "'>\n";
348
349         if (!substr_count($_SERVER["HTTP_REFERER"], "graphs_new")) {
350                 $_REQUEST["returnto"] = basename($_SERVER["HTTP_REFERER"]);
351         }
352         load_current_session_value("returnto", "sess_graphs_new_returnto", "");
353
354         form_save_button($_REQUEST["returnto"]);
355
356         include_once("./include/bottom_footer.php");
357 }
358
359 /* -------------------
360     Graph Functions
361    ------------------- */
362
363 function graphs() {
364         global $colors;
365
366         /* ================= input validation ================= */
367         input_validate_input_number(get_request_var_request("host_id"));
368         input_validate_input_number(get_request_var_request("graph_type"));
369         /* ==================================================== */
370
371         /* clean up search string */
372         if (isset($_REQUEST["filter"])) {
373                 $_REQUEST["filter"] = sanitize_search_string(get_request_var("filter"));
374         }
375
376         /* if the user pushed the 'clear' button */
377         if (isset($_REQUEST["clear_x"])) {
378                 kill_session_var("sess_graphs_new_filter");
379                 unset($_REQUEST["filter"]);
380                 $changed = true;
381         }else{
382                 /* if any of the settings changed, reset the page number */
383                 $changed = false;
384                 $changed += check_changed("host_id",    "sess_graphs_new_host_id");
385                 $changed += check_changed("graph_type", "sess_graphs_new_graph_type");
386                 $changed += check_changed("filter",     "sess_graphs_new_filter");
387         }
388
389         load_current_session_value("host_id",    "sess_graphs_new_host_id",    db_fetch_cell("select id from host order by description,hostname limit 1"));
390         load_current_session_value("graph_type", "sess_graphs_new_graph_type", read_config_option("default_graphs_new_dropdown"));
391         load_current_session_value("filter",     "sess_graphs_new_filter",     "");
392
393         $host      = db_fetch_row("select id,description,hostname,host_template_id from host where id=" . $_REQUEST["host_id"]);
394         $row_limit = read_config_option("num_rows_data_query");
395         $debug_log = debug_log_return("new_graphs");
396
397         if (!empty($debug_log)) {
398                 debug_log_clear("new_graphs");
399                 if (read_config_option("cacti_popup_messages") == "on") { ?>
400                 <div id='message'>
401                         <?php print "<table align='center' style='width:100%;background-color:#" . $colors["header"] . ";'><tr><td style='align:center;padding:3px;font-weight:bold;font-size:10pt;text-align:center;'>Graphs Created</td><td style='width:1px;align:right;'><input type='button' value='Clear' onClick='javascript:document.getElementById(\"message\").style.display=\"none\"' style='align=right;'></td></tr></table>";?>
402                         <?php print "<table align='left' style='width:100%;'><tr><td><ul style='text-align:left;white-space:nowrap;color:#000000;padding:2px 10px;margin:10px;'>" . $debug_log . "</ul></td></tr></table>";?>
403                 </div>
404                 <?php }else{ ?>
405                 <table width='100%' style='background-color: #f5f5f5; border: 1px solid #bbbbbb;' align='center'>
406                         <tr bgcolor="<?php print $colors["light"];?>">
407                                 <td style="padding: 3px; font-family: monospace;">
408                                         <ul style='margin:0px 5px;padding-left:10px'><?php print $debug_log;?></ul>
409                                 </td>
410                         </tr>
411                 </table>
412                 <br><?php }
413         }
414         ?>
415         <script type="text/javascript">
416         <!--
417         <?php if (read_config_option("cacti_popup_messages") == "on") {?>
418         var obj = document.getElementById('message');
419
420         if (obj) {
421                 if (window.innerHeight) {
422                         height = window.innerHeight;
423                         width  = window.innerWidth;
424                 }else{
425                         height = document.body.clientHeight;
426                         width  = document.body.clientWidth;
427                 }
428                 obj.style.position = "absolute";
429                 obj.style.padding = "0px";
430                 obj.style.display = "";
431                 obj.style.overflow = "auto";
432                 obj.style.color = "#FFFFFF";
433                 obj.style.backgroundColor = "#<?php print $colors["light"];?>";
434                 obj.style.border = "1px solid #<?php print $colors["header"];?>";
435                 cw = obj.offsetWidth;
436                 // Adjust for IE6
437                 if (!cw) cw = 150;
438                 ch = obj.offsetHeight;
439                 obj.style.top = '65px';
440                 obj.style.left = ((width/2) - (cw/2) - 88)+'px';
441         }
442         <?php } ?>
443
444         function applyGraphsNewFilterChange(objForm) {
445                 strURL = '?graph_type=' + objForm.graph_type.value;
446                 strURL = strURL + '&host_id=' + objForm.host_id.value;
447                 strURL = strURL + '&filter=' + objForm.filter.value;;
448                 document.location = strURL;
449         }
450
451         -->
452         </script>
453         <form name="form_graphs_new" action="graphs_new.php">
454         <table width="100%" cellpadding="4" align="center">
455                 <tr>
456                         <td nowrap style='white-space: nowrap;' width="30%" class="textInfo">
457                                 <?php print htmlspecialchars($host["description"]);?> (<?php print htmlspecialchars($host["hostname"]);?>)
458                         </td>
459                         <td align="left" class="textInfo" colspan="2" style="color: #aaaaaa;">
460                                 <?php
461                                 if (!empty($host["host_template_id"])) {
462                                         print htmlspecialchars(db_fetch_cell("select name from host_template where id=" . $host["host_template_id"]));
463                                 }
464                                 ?>
465                         </td>
466                 </tr>
467         </table>
468         <table width="100%" cellpadding="0" align="center">
469                 <tr>
470                         <td nowrap style='white-space: nowrap;' width="55" class="textArea">
471                                 Host:&nbsp;
472                         </td>
473                         <td width="1">
474                                 <select name="host_id" onChange="applyGraphsNewFilterChange(document.form_graphs_new)">
475                                 <?php
476                                 $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host order by description,hostname");
477
478                                 if (sizeof($hosts) > 0) {
479                                         foreach ($hosts as $item) {
480                                                 print "<option value='" . $item["id"] . "'"; if ($_REQUEST["host_id"] == $item["id"]) { print " selected"; } print ">" . htmlspecialchars($item["name"]) . "</option>\n";
481                                         }
482                                 }
483                                 ?>
484                                 </select>
485                         </td>
486                         <td nowrap style='white-space: nowrap;' width="100" class="textArea">
487                                 &nbsp;Graph Types:&nbsp;
488                         </td>
489                         <td width="1">
490                                 <select name="graph_type" onChange="applyGraphsNewFilterChange(document.form_graphs_new)">
491                                 <option value="-2"<?php if ($_REQUEST["graph_type"] == "-2") {?> selected<?php }?>>All</option>
492                                 <option value="-1"<?php if ($_REQUEST["graph_type"] == "-1") {?> selected<?php }?>>Graph Template Based</option>
493                                 <?php
494
495                                 $snmp_queries = db_fetch_assoc("SELECT
496                                         snmp_query.id,
497                                         snmp_query.name,
498                                         snmp_query.xml_path
499                                         FROM (snmp_query,host_snmp_query)
500                                         WHERE host_snmp_query.snmp_query_id=snmp_query.id
501                                         AND host_snmp_query.host_id=" . $host["id"] . "
502                                         ORDER BY snmp_query.name");
503
504                                 if (sizeof($snmp_queries) > 0) {
505                                 foreach ($snmp_queries as $query) {
506                                         print "<option value='" . $query["id"] . "'"; if ($_REQUEST["graph_type"] == $query["id"]) { print " selected"; } print ">" . $query["name"] . "</option>\n";
507                                 }
508                                 }
509                                 ?>
510                                 </select>
511                         </td>
512                         <td nowrap style='white-space: nowrap;' class="textInfo" align="center" valign="top">
513                                 <span style="white-space: nowrap; color: #c16921;">*</span><a href="<?php print htmlspecialchars("host.php?action=edit&id=" . $_REQUEST["host_id"]);?>">Edit this Host</a><br>
514                                 <span style="white-space: nowrap; color: #c16921;">*</span><a href="<?php print htmlspecialchars("host.php?action=edit");?>">Create New Host</a>
515                         </td>
516                 </tr>
517         </table>
518         <?php if ($_REQUEST["graph_type"] > 0) {?>
519         <table width="100%" cellpadding="0" align="center">
520                 <tr>
521                         <td nowrap style='white-space: nowrap;' width="55" class="textArea">
522                                 Search:&nbsp;
523                         </td>
524                         <td nowrap style='white-space: nowrap;' width="200">
525                                 <input type="text" name="filter" size="30" value="<?php print htmlspecialchars(get_request_var_request("filter"));?>">
526                         </td>
527                         <td align="left" nowrap style='white-space: nowrap;'>
528                                 &nbsp;<input type="submit" value="Go" title="Set/Refresh Filters">
529                                 <input type="submit" name="clear_x" value="Clear" title="Clear Filters">
530                         </td>
531                 </tr>
532         </table>
533         <?php }else{
534                 form_hidden_box("filter", $_REQUEST["filter"], "");
535         }?>
536         </form>
537
538         <form name="chk" method="post" action="graphs_new.php">
539         <?php
540         $total_rows = sizeof(db_fetch_assoc("select graph_template_id from host_graph where host_id=" . $_REQUEST["host_id"]));
541
542         $i = 0;
543
544         if ($changed) {
545                 foreach($snmp_queries as $query) {
546                         kill_session_var("sess_graphs_new_page" . $query["id"]);
547                         unset($_REQUEST["page" . $query["id"]]);
548                         load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1");
549                 }
550         }
551
552         if ($_REQUEST["graph_type"] > 0) {
553                 load_current_session_value("page" . $_REQUEST["graph_type"], "sess_graphs_new_page" . $_REQUEST["graph_type"], "1");
554         }else if ($_REQUEST["graph_type"] == -2) {
555                 foreach($snmp_queries as $query) {
556                         load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1");
557                 }
558         }
559
560         $script = "<script type='text/javascript'>\nvar gt_created_graphs = new Array();\nvar created_graphs = new Array()\n";
561
562         if ($_REQUEST["graph_type"] < 0) {
563                 html_start_box("<strong>Graph Templates</strong>", "100%", $colors["header"], "3", "center", "");
564
565                 print " <tr bgcolor='#" . $colors["header_panel"] . "'>
566                                 <td class='textSubHeaderDark'>Graph Template Name</td>
567                                 <td width='1%' align='center' bgcolor='#819bc0' style='" . get_checkbox_style() . "'><input type='checkbox' style='margin: 0px;' name='all_cg' title='Select All' onClick='SelectAll(\"cg\",this.checked);gt_update_selection_indicators();'></td>\n
568                         </tr>\n";
569
570                 $graph_templates = db_fetch_assoc("SELECT
571                         graph_templates.id AS graph_template_id,
572                         graph_templates.name AS graph_template_name
573                         FROM (host_graph,graph_templates)
574                         WHERE host_graph.graph_template_id=graph_templates.id
575                         AND host_graph.host_id=" . $_REQUEST["host_id"] . "
576                         ORDER BY graph_templates.name");
577
578                 $template_graphs = db_fetch_assoc("SELECT
579                         graph_local.graph_template_id
580                         FROM (graph_local,host_graph)
581                         WHERE graph_local.graph_template_id=host_graph.graph_template_id
582                         AND graph_local.host_id=host_graph.host_id
583                         AND graph_local.host_id=" . $host["id"] . "
584                         GROUP BY graph_local.graph_template_id");
585
586                 if (sizeof($template_graphs) > 0) {
587                         $script .= "var gt_created_graphs = new Array(";
588
589                         $cg_ctr = 0;
590                         foreach ($template_graphs as $template_graph) {
591                                 $script .= (($cg_ctr > 0) ? "," : "") . "'" . $template_graph["graph_template_id"] . "'";
592
593                                 $cg_ctr++;
594                         }
595
596                         $script .= ")\n";
597                 }
598
599                 /* create a row for each graph template associated with the host template */
600                 if (sizeof($graph_templates) > 0) {
601                 foreach ($graph_templates as $graph_template) {
602                         $query_row = $graph_template["graph_template_id"];
603
604                         print "<tr id='gt_line$query_row' bgcolor='#" . (($i % 2 == 0) ? "ffffff" : $colors["light"]) . "'>"; $i++;
605
606                         print "         <td onClick='gt_select_line(" . $graph_template["graph_template_id"] . ");'>
607                                                 <span id='gt_text$query_row" . "_0'><strong>Create:</strong> " . htmlspecialchars($graph_template["graph_template_name"]) . "</span>
608                                         </td>
609                                         <td align='right'>
610                                                 <input type='checkbox' name='cg_$query_row' id='cg_$query_row' onClick='gt_update_selection_indicators();'>
611                                         </td>
612                                 </tr>";
613                 }
614                 }
615
616                 $script .= "gt_update_deps(1);\n";
617
618                 $available_graph_templates = db_fetch_assoc("SELECT
619                         graph_templates.id, graph_templates.name
620                         FROM snmp_query_graph RIGHT JOIN graph_templates
621                         ON (snmp_query_graph.graph_template_id = graph_templates.id)
622                         WHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name");
623
624                 /* create a row at the bottom that lets the user create any graph they choose */
625                 print " <tr bgcolor='#" . (($i % 2 == 0) ? "ffffff" : $colors["light"]) . "'>
626                                 <td colspan='2' width='60' nowrap>
627                                         <strong>Create:</strong>&nbsp;";
628                                         form_dropdown("cg_g", $available_graph_templates, "name", "id", "", "(Select a graph type to create)", "", "textArea");
629                 print "         </td>
630                         </tr>";
631
632                 html_end_box();
633         }
634
635         if ($_REQUEST["graph_type"] != -1) {
636                 $snmp_queries = db_fetch_assoc("SELECT
637                         snmp_query.id,
638                         snmp_query.name,
639                         snmp_query.xml_path
640                         FROM (snmp_query,host_snmp_query)
641                         WHERE host_snmp_query.snmp_query_id=snmp_query.id
642                         AND host_snmp_query.host_id=" . $host["id"] .
643                         ($_REQUEST["graph_type"] != -2 ? " AND snmp_query.id=" . $_REQUEST["graph_type"] : '') . "
644                         ORDER BY snmp_query.name");
645
646                 if (sizeof($snmp_queries) > 0) {
647                 foreach ($snmp_queries as $snmp_query) {
648                         unset($total_rows);
649
650                         if (!$changed) {
651                                 $page = $_REQUEST["page" . $snmp_query["id"]];
652                         }else{
653                                 $page = 1;
654                         }
655
656                         $xml_array = get_data_query_array($snmp_query["id"]);
657
658                         $num_input_fields = 0;
659                         $num_visible_fields = 0;
660
661                         if ($xml_array != false) {
662                                 /* loop through once so we can find out how many input fields there are */
663                                 reset($xml_array["fields"]);
664                                 while (list($field_name, $field_array) = each($xml_array["fields"])) {
665                                         if ($field_array["direction"] == "input") {
666                                                 $num_input_fields++;
667
668                                                 if (!isset($total_rows)) {
669                                                         $total_rows = db_fetch_cell("SELECT count(*) FROM host_snmp_cache WHERE host_id=" . $host["id"] . " and snmp_query_id=" . $snmp_query["id"] . " AND field_name='$field_name'");
670                                                 }
671                                         }
672                                 }
673                         }
674
675                         if (!isset($total_rows)) {
676                                 $total_rows = 0;
677                         }
678
679                         $snmp_query_graphs = db_fetch_assoc("SELECT snmp_query_graph.id,snmp_query_graph.name FROM snmp_query_graph WHERE snmp_query_graph.snmp_query_id=" . $snmp_query["id"] . " ORDER BY snmp_query_graph.name");
680
681                         if (sizeof($snmp_query_graphs) > 0) {
682                                 foreach ($snmp_query_graphs as $snmp_query_graph) {
683                                         $created_graphs = db_fetch_assoc("SELECT DISTINCT
684                                                 data_local.snmp_index
685                                                 FROM (data_local,data_template_data)
686                                                 LEFT JOIN data_input_data ON (data_template_data.id=data_input_data.data_template_data_id)
687                                                 LEFT JOIN data_input_fields ON (data_input_data.data_input_field_id=data_input_fields.id)
688                                                 WHERE data_local.id=data_template_data.local_data_id
689                                                 AND data_input_fields.type_code='output_type'
690                                                 AND data_input_data.value='" . $snmp_query_graph["id"] . "'
691                                                 AND data_local.host_id=" . $host["id"]);
692
693                                         $script .= "created_graphs[" . $snmp_query_graph["id"] . "] = new Array(";
694
695                                         $cg_ctr = 0;
696                                         if (sizeof($created_graphs) > 0) {
697                                         foreach ($created_graphs as $created_graph) {
698                                                 $script .= (($cg_ctr > 0) ? "," : "") . "'" . encode_data_query_index($created_graph["snmp_index"]) . "'";
699
700                                                 $cg_ctr++;
701                                         }
702                                         }
703
704                                         $script .= ")\n";
705                                 }
706                         }
707
708                         print " <table width='100%' style='background-color: #" . $colors["form_alternate2"] . "; border: 1px solid #" . $colors["header"] . ";' align='center' cellpadding='3' cellspacing='0'>\n
709                                         <tr>
710                                                 <td bgcolor='#" . $colors["header"] . "' colspan='" . ($num_input_fields+1) . "'>
711                                                         <table  cellspacing='0' cellpadding='0' width='100%' >
712                                                                 <tr>
713                                                                         <td class='textHeaderDark'>
714                                                                                 <strong>Data Query</strong> [" . $snmp_query["name"] . "]
715                                                                         </td>
716                                                                         <td align='right' nowrap>
717                                                                                 <a href='" . htmlspecialchars("graphs_new.php?action=query_reload&id=" . $snmp_query["id"] . "&host_id=" . $host["id"]) . "'><img src='images/reload_icon_small.gif' title='Reload Associated Query' alt='' border='0' align='middle'></a>
718                                                                         </td>
719                                                                 </tr>
720                                                         </table>
721                                                 </td>
722                                         </tr>";
723
724                         if ($xml_array != false) {
725                                 $html_dq_header = "";
726                                 $snmp_query_indexes = array();
727
728                                 reset($xml_array["fields"]);
729
730                                 /* if there is a where clause, get the matching snmp_indexes */
731                                 $sql_where = "";
732                                 if (strlen($_REQUEST["filter"])) {
733                                         $sql_where = "";
734                                         $indexes = db_fetch_assoc("SELECT DISTINCT snmp_index
735                                                 FROM host_snmp_cache
736                                                 WHERE field_value LIKE '%%" . $_REQUEST["filter"] . "%%'
737                                                 AND snmp_query_id=" . $snmp_query["id"] . "
738                                                 AND host_id=" . $host["id"]);
739
740                                         if (sizeof($indexes)) {
741                                                 foreach($indexes as $index) {
742                                                         if (strlen($sql_where)) {
743                                                                 $sql_where .= ", '" . $index["snmp_index"] . "'";
744                                                         }else{
745                                                                 $sql_where .= " AND snmp_index IN('" . $index["snmp_index"] . "'";
746                                                         }
747                                                 }
748
749                                                 $sql_where .= ")";
750                                         }
751                                 }
752
753                                 if ((strlen($_REQUEST["filter"]) == 0) ||
754                                         ((strlen($_REQUEST["filter"])) && (sizeof($indexes)))) {
755                                         /* determine the sort order */
756                                         if (isset($xml_array["index_order_type"])) {
757                                                 if ($xml_array["index_order_type"] == "numeric") {
758                                                         $sql_order = "ORDER BY CAST(snmp_index AS unsigned)";
759                                                 }else if ($xml_array["index_order_type"] == "alphabetic") {
760                                                         $sql_order = "ORDER BY snmp_index";
761                                                 }else if ($xml_array["index_order_type"] == "natural") {
762                                                         $sql_order = "ORDER BY INET_ATON(snmp_index)";
763                                                 }else{
764                                                         $sql_order = "";
765                                                 }
766                                         }else{
767                                                 $sql_order = "";
768                                         }
769
770                                         /* get the unique field values from the database */
771                                         $field_names = db_fetch_assoc("SELECT DISTINCT field_name
772                                                 FROM host_snmp_cache
773                                                 WHERE host_id=" . $host["id"] . "
774                                                 AND snmp_query_id=" . $snmp_query["id"]);
775
776                                         /* build magic query */
777                                         $sql_query  = "SELECT host_id, snmp_query_id, snmp_index";
778                                         $num_visible_fields = sizeof($field_names);
779                                         $i = 0;
780                                         if (sizeof($field_names) > 0) {
781                                                 foreach($field_names as $column) {
782                                                         $field_name = $column["field_name"];
783                                                         $sql_query .= ", MAX(CASE WHEN field_name='$field_name' THEN field_value ELSE NULL END) AS '$field_name'";
784                                                         $i++;
785                                                 }
786                                         }
787
788                                         $sql_query .= " FROM host_snmp_cache
789                                                 WHERE host_id=" . $host["id"] . "
790                                                 AND snmp_query_id=" . $snmp_query["id"] . "
791                                                 $sql_where
792                                                 GROUP BY host_id, snmp_query_id, snmp_index
793                                                 $sql_order
794                                                 LIMIT " . ($row_limit*($page-1)) . "," . $row_limit;
795
796                                         $rows_query = "SELECT host_id, snmp_query_id, snmp_index
797                                                 FROM host_snmp_cache
798                                                 WHERE host_id=" . $host["id"] . "
799                                                 AND snmp_query_id=" . $snmp_query["id"] . "
800                                                 $sql_where
801                                                 GROUP BY host_id, snmp_query_id, snmp_index";
802
803                                         $snmp_query_indexes = db_fetch_assoc($sql_query);
804
805                                         $total_rows = sizeof(db_fetch_assoc($rows_query));
806
807                                         if (($page - 1) * $row_limit > $total_rows) {
808                                                 $page = 1;
809                                                 $_REQUEST["page" . $query["id"]] = $page;
810                                                 load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1");
811                                         }
812
813                                         if ($total_rows > $row_limit) {
814                                                 /* generate page list */
815                                                 $url_page_select = get_page_list($page, MAX_DISPLAY_PAGES, $row_limit, $total_rows, "graphs_new.php?", "page" . $snmp_query["id"]);
816
817                                                 $nav = "<tr bgcolor='#" . $colors["header"] . "' class='noprint'>
818                                                                         <td colspan='15'>
819                                                                                 <table width='100%' cellspacing='0' cellpadding='0' border='0'>
820                                                                                         <tr>
821                                                                                                 <td align='left' class='textHeaderDark'>
822                                                                                                         <strong>&lt;&lt; "; if ($page > 1) { $nav .= "<a class='linkOverDark' href='" . htmlspecialchars("graphs_new.php?page" . $snmp_query["id"] . "=" . ($page-1)) . "'>"; } $nav .= "Previous"; if ($page > 1) { $nav .= "</a>"; } $nav .= "</strong>
823                                                                                                 </td>\n
824                                                                                                 <td align='center' class='textHeaderDark'>
825                                                                                                         Showing Rows " . (($row_limit*($page-1))+1) . " to " . ((($total_rows < $row_limit) || ($total_rows < ($row_limit*$page))) ? $total_rows : ($row_limit*$page)) . " of $total_rows [$url_page_select]
826                                                                                                 </td>\n
827                                                                                                 <td align='right' class='textHeaderDark'>
828                                                                                                         <strong>"; if (($page * $row_limit) < $total_rows) { $nav .= "<a class='linkOverDark' href='" . htmlspecialchars("graphs_new.php?page" . $snmp_query["id"] . "=" . ($page+1)) . "'>"; } $nav .= "Next"; if (($page * $row_limit) < $total_rows) { $nav .= "</a>"; } $nav .= " &gt;&gt;</strong>
829                                                                                                 </td>\n
830                                                                                         </tr>
831                                                                                 </table>
832                                                                         </td>
833                                                                 </tr>\n";
834
835                                                 print $nav;
836                                         }
837
838                                         while (list($field_name, $field_array) = each($xml_array["fields"])) {
839                                                 if ($field_array["direction"] == "input") {
840                                                         foreach($field_names as $row) {
841                                                                 if ($row["field_name"] == $field_name) {
842                                                                         $html_dq_header .= "<td style='height:1px;'><strong><font color='#" . $colors["header_text"] . "'>" . $field_array["name"] . "</font></strong></td>\n";
843                                                                         break;
844                                                                 }
845                                                         }
846                                                 }
847                                         }
848
849                                         if (!sizeof($snmp_query_indexes)) {
850                                                 print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td>This data query returned 0 rows, perhaps there was a problem executing this
851                                                         data query. You can <a href='" . htmlspecialchars("host.php?action=query_verbose&id=" . $snmp_query["id"] . "&host_id=" . $host["id"]) . "'>run this data
852                                                         query in debug mode</a> to get more information.</td></tr>\n";
853                                         }else{
854                                                 print " <tr bgcolor='#" . $colors["header_panel"] . "'>
855                                                                 $html_dq_header
856                                                                 <td width='1%' align='center' bgcolor='#819bc0' style='" . get_checkbox_style() . "'><input type='checkbox' style='margin: 0px;' name='all_" . $snmp_query["id"] . "' title='Select All' onClick='SelectAll(\"sg_" . $snmp_query["id"] . "\",this.checked);dq_update_selection_indicators();'></td>\n
857                                                         </tr>\n";
858                                         }
859
860                                         $row_counter    = 0;
861                                         $column_counter = 0;
862                                         $fields         = array_rekey($field_names, "field_name", "field_name");
863                                         if (sizeof($snmp_query_indexes) > 0) {
864                                         foreach($snmp_query_indexes as $row) {
865                                                 $query_row = $snmp_query["id"] . "_" . encode_data_query_index($row["snmp_index"]);
866
867                                                 print "<tr id='line$query_row' bgcolor='#" . (($row_counter % 2 == 0) ? "ffffff" : $colors["light"]) . "'>"; $i++;
868
869                                                 $column_counter = 0;
870                                                 reset($xml_array["fields"]);
871                                                 while (list($field_name, $field_array) = each($xml_array["fields"])) {
872                                                         if ($field_array["direction"] == "input") {
873                                                                 if (in_array($field_name, $fields)) {
874                                                                         if (isset($row[$field_name])) {
875                                                                                 print "<td onClick='dq_select_line(" . $snmp_query["id"] . ",\"" . encode_data_query_index($row["snmp_index"]) . "\");'><span id='text$query_row" . "_" . $column_counter . "'>" . (strlen($_REQUEST["filter"]) ? eregi_replace("(" . preg_quote($_REQUEST["filter"]) . ")", "<span style='background-color: #F8D93D;'>\\1</span>", $row[$field_name]) : $row[$field_name]) . "</span></td>";
876                                                                         }else{
877                                                                                 print "<td onClick='dq_select_line(" . $snmp_query["id"] . ",\"" . encode_data_query_index($row["snmp_index"]) . "\");'><span id='text$query_row" . "_" . $column_counter . "'></span></td>";
878                                                                         }
879
880                                                                         $column_counter++;
881                                                                 }
882                                                         }
883                                                 }
884
885                                                 print "<td align='right'>";
886                                                 print "<input type='checkbox' name='sg_$query_row' id='sg_$query_row' onClick='dq_update_selection_indicators();'>";
887                                                 print "</td>";
888                                                 print "</tr>\n";
889
890                                                 $row_counter++;
891                                         }
892                                         }
893
894                                         if ($total_rows > $row_limit) {
895                                                 print $nav;
896                                         }
897                                 }else{
898                                         print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td colspan='2' style='color: red; font-size: 12px; font-weight: bold;'>Search Returned no Rows.</td></tr>\n";
899                                 }
900                         }else{
901                                 print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td colspan='2' style='color: red; font-size: 12px; font-weight: bold;'>Error in data query.</td></tr>\n";
902                         }
903
904                         print "</table>";
905
906                         /* draw the graph template drop down here */
907                         $data_query_graphs = db_fetch_assoc("select snmp_query_graph.id,snmp_query_graph.name from snmp_query_graph where snmp_query_graph.snmp_query_id=" . $snmp_query["id"] . " order by snmp_query_graph.name");
908
909                         if (sizeof($data_query_graphs) == 1) {
910                                 echo "<input type='hidden' id='sgg_" . $snmp_query["id"] . "' name='sgg_" . $snmp_query["id"] . "' value='" . $data_query_graphs[0]["id"] . "'>\n";
911                         }elseif (sizeof($data_query_graphs) > 1) {
912                                 print " <table align='center' width='100%'>
913                                                 <tr>
914                                                         <td width='1' valign='top'>
915                                                                 <img src='images/arrow.gif' alt=''>&nbsp;
916                                                         </td>
917                                                         <td align='right'>
918                                                                 <span style='font-size: 12px; font-style: italic;'>Select a graph type:</span>&nbsp;
919                                                                 <select name='sgg_" . $snmp_query["id"] . "' id='sgg_" . $snmp_query["id"] . "' onChange='dq_update_deps(" . $snmp_query["id"] . "," . (isset($column_counter) ? $column_counter:"") . ");'>
920                                                                         "; html_create_list($data_query_graphs,"name","id","0"); print "
921                                                                 </select>
922                                                         </td>
923                                                 </tr>
924                                         </table>";
925                         }
926
927                         print "<br>";
928
929                         $script .= "dq_update_deps(" . $snmp_query["id"] . "," . $num_visible_fields . ");\n";
930                 }
931                 }
932         }
933
934         if (strlen($script)) {
935                 $script .= "</script>\n";
936                 print $script;
937         }
938
939         form_hidden_box("save_component_graph", "1", "");
940         form_hidden_box("host_id", $host["id"], "0");
941         form_hidden_box("host_template_id", $host["host_template_id"], "0");
942
943         if (isset($_SERVER["HTTP_REFERER"]) && !substr_count($_SERVER["HTTP_REFERER"], "graphs_new")) {
944                 $_REQUEST["returnto"] = basename($_SERVER["HTTP_REFERER"]);
945         }
946         load_current_session_value("returnto", "sess_graphs_new_returnto", "");
947
948         form_save_button($_REQUEST["returnto"]);
949
950         print "<script type='text/javascript'>dq_update_selection_indicators();</script>\n";
951         print "<script type='text/javascript'>gt_update_selection_indicators();</script>\n";
952 }
953 ?>