function data_query_item_edit() { global $colors, $fields_data_query_item_edit; if (!empty($_GET["id"])) { $snmp_query_item = db_fetch_row("select * from snmp_query_graph where id=" . $_GET["id"]); } $snmp_query = db_fetch_row("select name,xml_path from snmp_query where id=" . $_GET["snmp_query_id"]); $header_label = "[edit: " . $snmp_query["name"] . "]"; html_start_box("<strong>Associated Graph/Data Templates</strong> $header_label", "98%", $colors["header"], "3", "center", ""); draw_edit_form(array( "config" => array(), "fields" => inject_form_variables($fields_data_query_item_edit, (isset($snmp_query_item) ? $snmp_query_item : array()), $_GET) )); html_end_box(); if (!empty($snmp_query_item["id"])) { html_start_box("<strong>Associated Data Templates</strong>", "98%", $colors["header"], "3", "center", ""); $data_templates = db_fetch_assoc("select data_template.id, data_template.name from data_template, data_template_rrd, graph_templates_item where graph_templates_item.task_item_id=data_template_rrd.id and data_template_rrd.data_template_id=data_template.id and data_template_rrd.local_data_id=0 and graph_templates_item.local_graph_id=0 and graph_templates_item.graph_template_id=" . $snmp_query_item["graph_template_id"] . " group by data_template.id order by data_template.name"); $i = 0; if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { print " <tr bgcolor='#" . $colors["header_panel"] . "'> <td><span style='color: white; font-weight: bold;'>Data Template - " . $data_template["name"] . "</span></td> </tr>"; $data_template_rrds = db_fetch_assoc("select data_template_rrd.id, data_template_rrd.data_source_name, snmp_query_graph_rrd.snmp_field_name, snmp_query_graph_rrd.snmp_query_graph_id from data_template_rrd 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"] . ") where data_template_rrd.data_template_id=" . $data_template["id"] . " and data_template_rrd.local_data_id=0 order by data_template_rrd.data_source_name"); $i = 0; if (sizeof($data_template_rrds) > 0) { foreach ($data_template_rrds as $data_template_rrd) { if (empty($data_template_rrd["snmp_query_graph_id"])) { $old_value = ""; }else{ $old_value = "on"; } form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++; ?> <td> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <tr> <td width="200"> <strong>Data Source:</strong> </td> <td width="200"> <?php print $data_template_rrd["data_source_name"];?> </td> <td width="1"> <?php $snmp_queries = get_data_query_array($_GET["snmp_query_id"]); $xml_outputs = array(); while (list($field_name, $field_array) = each($snmp_queries["fields"])) { if ($field_array["direction"] == "output") { $xml_outputs[$field_name] = $field_name . " (" . $field_array["name"] . ")";; } } form_dropdown("dsdt_" . $data_template["id"] . "_" . $data_template_rrd["id"] . "_snmp_field_output",$xml_outputs,"","",$data_template_rrd["snmp_field_name"],"","");?> </td> <td align="right"> <?php form_checkbox("dsdt_" . $data_template["id"] . "_" . $data_template_rrd["id"] . "_check", $old_value, "", "",$_GET["id"]); print "<br>";?> </td> </tr> </table> </td> </tr> <?php } } } } html_end_box(); html_start_box("<strong>Suggested Values</strong>", "98%", $colors["header"], "0", "center", ""); reset($data_templates); /* suggested values for data templates */ if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { $suggested_values = db_fetch_assoc("select text, field_name, id from snmp_query_graph_rrd_sv where snmp_query_graph_id=" . $_GET["id"] . " and data_template_id=" . $data_template["id"] . " order by field_name,sequence"); print " <tr bgcolor='#" . $colors["header_panel"] . "'> <td style='padding: 3px;'><span style='color: white; font-weight: bold;'>Data Template - " . $data_template["name"] . "</span></td> </tr>"; $i = 0; if (sizeof($suggested_values) > 0) { print "<tr><td><table cellspacing='0' cellpadding='3' border='0' width='100%'>\n"; foreach ($suggested_values as $suggested_value) { form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++; ?> <td width="120"> <strong><?php print $suggested_value["field_name"];?></strong> </td> <td> <?php print $suggested_value["text"];?> </td> <td width="70"> <a href="data_queries.php?action=item_movedown_dssv&snmp_query_graph_id=<?php print $_GET["id"];?>&id=<?php print $suggested_value["id"];?>&snmp_query_id=<?php print $_GET["snmp_query_id"];?>&data_template_id=<?php print $data_template["id"];?>&field_name=<?php print $suggested_value["field_name"];?>"><img src="images/move_down.gif" border="0" alt="Move Down"></a> <a href="data_queries.php?action=item_moveup_dssv&snmp_query_graph_id=<?php print $_GET["id"];?>&id=<?php print $suggested_value["id"];?>&snmp_query_id=<?php print $_GET["snmp_query_id"];?>&data_template_id=<?php print $data_template["id"];?>&field_name=<?php print $suggested_value["field_name"];?>"><img src="images/move_up.gif" border="0" alt="Move Up"></a> </td> <td align="right"> <a href="data_queries.php?action=item_remove_dssv&snmp_query_graph_id=<?php print $_GET["id"];?>&id=<?php print $suggested_value["id"];?>&snmp_query_id=<?php print $_GET["snmp_query_id"];?>&data_template_id=<?php print $data_template["id"];?>"><img src="images/delete_icon.gif" width="10" height="10" border="0" alt="Delete"></a> </td> </tr> <?php } print "</table></td></tr>\n"; } form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); ?> <td> <table cellspacing="0" cellpadding="3" border="0" width="100%"> <tr> <td width="1"> <input type="text" name="svds_<?php print $data_template["id"];?>_text" size="30"> </td> <td width="220" nowrap> Field Name: <input type="text" name="svds_<?php print $data_template["id"];?>_field" size="15"> </td> <td> <input type="image" src="images/button_add.gif" name="svds_<?php print $data_template["id"];?>" alt="Add" align="absmiddle"> </td> </tr> </table> </td> </tr> <?php } } /* suggested values for graphs templates */ $suggested_values = db_fetch_assoc("select text, field_name, id from snmp_query_graph_sv where snmp_query_graph_id=" . $_GET["id"] . " order by field_name,sequence"); print " <tr bgcolor='#" . $colors["header_panel"] . "'> <td style='padding: 3px;'><span style='color: white; font-weight: bold;'>Graph Template - " . db_fetch_cell("select name from graph_templates where id=" . $snmp_query_item["graph_template_id"]) . "</span></td> </tr>"; $i = 0; if (sizeof($suggested_values) > 0) { print "<tr><td><table cellspacing='0' cellpadding='3' border='0' width='100%'>\n"; foreach ($suggested_values as $suggested_value) { form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); $i++; ?> <td width="120"> <strong><?php print $suggested_value["field_name"];?></strong> </td> <td> <?php print $suggested_value["text"];?> </td> <td width="70"> <a href="data_queries.php?action=item_movedown_gsv&snmp_query_graph_id=<?php print $_GET["id"];?>&id=<?php print $suggested_value["id"];?>&snmp_query_id=<?php print $_GET["snmp_query_id"];?>&field_name=<?php print $suggested_value["field_name"];?>"><img src="images/move_down.gif" border="0" alt="Move Down"></a> <a href="data_queries.php?action=item_moveup_gsv&snmp_query_graph_id=<?php print $_GET["id"];?>&id=<?php print $suggested_value["id"];?>&snmp_query_id=<?php print $_GET["snmp_query_id"];?>&field_name=<?php print $suggested_value["field_name"];?>"><img src="images/move_up.gif" border="0" alt="Move Up"></a> </td> <td align="right"> <a href="data_queries.php?action=item_remove_gsv&snmp_query_graph_id=<?php print $_GET["id"];?>&id=<?php print $suggested_value["id"];?>&snmp_query_id=<?php print $_GET["snmp_query_id"];?>"><img src="images/delete_icon.gif" width="10" height="10" border="0" alt="Delete"></a> </td> </tr> <?php } print "</table></td></tr>\n"; } form_alternate_row_color($colors["form_alternate1"],$colors["form_alternate2"],$i); ?> <td> <table cellspacing="0" cellpadding="3" border="0" width="100%"> <tr> <td width="1"> <input type="text" name="svg_text" size="30"> </td> <td width="220" nowrap> Field Name: <input type="text" name="svg_field" size="15"> </td> <td> <input type="image" src="images/button_add.gif" name="svg" alt="Add" align="absmiddle"> </td> </tr> </table> </td> </tr> <?php html_end_box(); } form_save_button("data_queries.php?action=edit&id=" . $_GET["snmp_query_id"]); }
function update_reindex_cache($host_id, $data_query_id) { global $config; include_once $config["library_path"] . "/data_query.php"; include_once $config["library_path"] . "/snmp.php"; /* will be used to keep track of sql statements to execute later on */ $recache_stack = array(); $host = db_fetch_row("select hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout from host where id={$host_id}"); $data_query = db_fetch_row("select reindex_method, sort_field from host_snmp_query where host_id={$host_id} and snmp_query_id={$data_query_id}"); $data_query_type = db_fetch_cell("select data_input.type_id from (data_input,snmp_query) where data_input.id=snmp_query.data_input_id and snmp_query.id={$data_query_id}"); $data_query_xml = get_data_query_array($data_query_id); switch ($data_query["reindex_method"]) { case DATA_QUERY_AUTOINDEX_NONE: break; case DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME: /* the uptime backwards method requires snmp, so make sure snmp is actually enabled * on this device first */ if ($host["snmp_version"] > 0) { if (isset($data_query_xml["oid_uptime"])) { $oid_uptime = $data_query_xml["oid_uptime"]; } elseif (isset($data_query_xml["uptime_oid"])) { $oid_uptime = $data_query_xml["uptime_oid"]; } else { $oid_uptime = ".1.3.6.1.2.1.1.3.0"; } $assert_value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid_uptime, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], SNMP_POLLER); $recache_stack[] = "('{$host_id}', '{$data_query_id}'," . POLLER_ACTION_SNMP . ", '<', '{$assert_value}', '{$oid_uptime}', '1')"; } break; case DATA_QUERY_AUTOINDEX_INDEX_NUM_CHANGE: /* this method requires that some command/oid can be used to determine the * current number of indexes in the data query * pay ATTENTION to quoting! * the script parameters are usually enclosed in single tics: ' * so we have to enclose the whole list of parameters in double tics: " * */ /* the assert_value counts the number of distinct indexes currently available in host_snmp_cache * we do NOT make use of <oid_num_indexes> or the like! * this works, even if no <oid_num_indexes> was given */ $assert_value = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id={$host_id} and snmp_query_id={$data_query_id} group by snmp_index")); /* now, we have to build the (list of) commands that are later used on a recache event * the result of those commands will be compared to the assert_value we have just computed * on a comparison failure, a reindex event will be generated */ switch ($data_query_type) { case DATA_INPUT_TYPE_SNMP_QUERY: if (isset($data_query_xml["oid_num_indexes"])) { /* we have a specific OID for counting indexes */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP . ", '=', '{$assert_value}', '" . $data_query_xml["oid_num_indexes"] . "', '1')"; } else { /* count all indexes found */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP_COUNT . ", '=', '{$assert_value}', '" . $data_query_xml["oid_index"] . "', '1')"; } break; case DATA_INPUT_TYPE_SCRIPT_QUERY: if (isset($data_query_xml["arg_num_indexes"])) { /* we have a specific request for counting indexes */ /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id)) . "', '1')"; } else { /* count all indexes found */ /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT_COUNT . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_index"], $data_query_xml["script_path"], $host_id)) . "', '1')"; } break; case DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER: if (isset($data_query_xml["arg_num_indexes"])) { /* we have a specific request for counting indexes */ /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT_PHP . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path($data_query_xml["script_function"] . " " . (isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id)) . "', '1')"; } else { /* count all indexes found */ # TODO: push the correct assert value /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */ #$recache_stack[] = "($host_id, $data_query_id," . POLLER_ACTION_SCRIPT_PHP_COUNT . ", '=', '$assert_value', '" . addslashes(get_script_query_path($data_query_xml["script_function"] . " " . (isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_index"], $data_query_xml["script_path"], $host_id)) . "', '1')"; # omit the assert value until we are able to run an 'index' command through script server } break; } break; case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION: $primary_indexes = db_fetch_assoc("select snmp_index,oid,field_value from host_snmp_cache where host_id={$host_id} and snmp_query_id={$data_query_id} and field_name='" . $data_query["sort_field"] . "'"); if (sizeof($primary_indexes) > 0) { foreach ($primary_indexes as $index) { $assert_value = $index["field_value"]; if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP . ", '=', '{$assert_value}', '" . $data_query_xml["fields"][$data_query["sort_field"]]["oid"] . "." . $index["snmp_index"] . "', '1')"; } else { if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { $recache_stack[] = "('{$host_id}', '{$data_query_id}'," . POLLER_ACTION_SCRIPT . ", '=', '{$assert_value}', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_get"] . " " . $data_query_xml["fields"][$data_query["sort_field"]]["query_name"] . " " . $index["snmp_index"], $data_query_xml["script_path"], $host_id) . "', '1')"; } } } } break; } if (sizeof($recache_stack)) { poller_update_poller_reindex_from_buffer($host_id, $data_query_id, $recache_stack); } }
function update_poller_cache($local_data_id, $commit = false) { global $config; require_once(CACTI_BASE_PATH . "/include/data_input/data_input_constants.php"); include_once(CACTI_BASE_PATH . "/lib/data_query.php"); include_once(CACTI_BASE_PATH . "/lib/api_poller.php"); $poller_items = array(); $data_input = db_fetch_row("select data_input.id, data_input.type_id, data_template_data.id as data_template_data_id, data_template_data.data_template_id, data_template_data.active, data_template_data.rrd_step from (data_template_data,data_input) where data_template_data.data_input_id=data_input.id and data_template_data.local_data_id=$local_data_id"); $data_source = db_fetch_row("select device_id,snmp_query_id,snmp_index from data_local where id=$local_data_id"); /* we have to perform some additional sql queries if this is a "query" */ if (($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) || ($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY) || ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER)){ $field = data_query_field_list($data_input["data_template_data_id"]); if (strlen($field["output_type"])) { $output_type_sql = "and snmp_query_graph_rrd.snmp_query_graph_id=" . $field["output_type"]; }else{ $output_type_sql = ""; } $outputs = db_fetch_assoc("select snmp_query_graph_rrd.snmp_field_name, data_template_rrd.id as data_template_rrd_id from (snmp_query_graph_rrd,data_template_rrd) where snmp_query_graph_rrd.data_template_rrd_id=data_template_rrd.local_data_template_rrd_id $output_type_sql and snmp_query_graph_rrd.data_template_id=" . $data_input["data_template_id"] . " and data_template_rrd.local_data_id=$local_data_id"); } if ($data_input["active"] == CHECKED) { if (($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT) || ($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER)) { /* script */ /* fall back to non-script server actions if the user is running a version of php older than 4.3 */ if (($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) && (function_exists("proc_open"))) { $action = POLLER_ACTION_SCRIPT_PHP; $script_path = get_full_script_path($local_data_id); }else if (($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) && (!function_exists("proc_open"))) { $action = POLLER_ACTION_SCRIPT; $script_path = read_config_option("path_php_binary") . " -q " . get_full_script_path($local_data_id); }else{ $action = POLLER_ACTION_SCRIPT; $script_path = get_full_script_path($local_data_id); } $num_output_fields = sizeof(db_fetch_assoc("select id from data_input_fields where data_input_id=" . $data_input["id"] . " and input_output='out' and update_rra='on'")); if ($num_output_fields == 1) { $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id=$local_data_id"); $data_source_item_name = get_data_source_item_name($data_template_rrd_id); }else{ $data_source_item_name = ""; } $poller_items[] = api_poller_cache_item_add($data_source["device_id"], array(), $local_data_id, $data_input["rrd_step"], $action, $data_source_item_name, 1, addslashes($script_path)); }else if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP) { /* snmp */ /* get the device override fields */ $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id=$local_data_id"); /* get device fields first */ $device_fields = array_rekey(db_fetch_assoc("SELECT data_input_fields.type_code, data_input_data.value FROM data_input_fields LEFT JOIN data_input_data ON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ") WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname')) AND data_input_data.value != ''"), "type_code", "value"); $data_template_fields = array_rekey(db_fetch_assoc("SELECT data_input_fields.type_code, data_input_data.value FROM data_input_fields LEFT JOIN data_input_data ON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=$data_template_id) WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname')) AND data_template_data_id=$data_template_id AND data_input_data.value != ''"), "type_code", "value"); if (sizeof($device_fields)) { if (sizeof($data_template_fields)) { foreach($data_template_fields as $key => $value) { if (!isset($device_fields[$key])) { $device_fields[$key] = $value; } } } } elseif (sizeof($data_template_fields)) { $device_fields = $data_template_fields; } $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id=$local_data_id"); $poller_items[] = api_poller_cache_item_add($data_source["device_id"], $device_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($data_template_rrd_id), 1, (isset($device_fields["snmp_oid"]) ? $device_fields["snmp_oid"] : "")); }else if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) { /* snmp query */ $snmp_queries = get_data_query_array($data_source["snmp_query_id"]); /* get the device override fields */ $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id=$local_data_id"); /* get device fields first */ $device_fields = array_rekey(db_fetch_assoc("SELECT data_input_fields.type_code, data_input_data.value FROM data_input_fields LEFT JOIN data_input_data ON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ") WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname')) AND data_input_data.value != ''"), "type_code", "value"); $data_template_fields = array_rekey(db_fetch_assoc("SELECT data_input_fields.type_code, data_input_data.value FROM data_input_fields LEFT JOIN data_input_data ON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=$data_template_id) WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname')) AND data_template_data_id=$data_template_id AND data_input_data.value != ''"), "type_code", "value"); if (sizeof($device_fields)) { if (sizeof($data_template_fields)) { foreach($data_template_fields as $key => $value) { if (!isset($device_fields[$key])) { $device_fields[$key] = $value; } } } } elseif (sizeof($data_template_fields)) { $device_fields = $data_template_fields; } if (sizeof($outputs) > 0) { foreach ($outputs as $output) { if (isset($snmp_queries["fields"]{$output["snmp_field_name"]}["oid"])) { $oid_suffix = $data_source["snmp_index"]; if(isset($snmp_queries["fields"]{$output["snmp_field_name"]}["rewrite_index"])){ $oid_suffix = data_query_rewrite_indexes($errmsg, $data_source["device_id"], $data_source["snmp_query_id"], $snmp_queries["fields"]{$output["snmp_field_name"]}["rewrite_index"], $oid_suffix); if($oid_suffix == NULL){ // rewriting index failed for some reason if(sizeof($errmsg)){ foreach($errmsg as $message){ cacti_log(__("Field '%s':", $output["snmp_field_name"]) . $message, false, "POLLER"); } } continue; } } $oid = $snmp_queries["fields"]{$output["snmp_field_name"]}["oid"] . "." . $oid_suffix; if (isset($snmp_queries["fields"]{$output["snmp_field_name"]}["oid_suffix"])) { $oid .= "." . $snmp_queries["fields"]{$output["snmp_field_name"]}["oid_suffix"]; } } if (!empty($oid)) { $poller_items[] = api_poller_cache_item_add($data_source["device_id"], $device_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), $oid); } } } }else if (($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY) || ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER)) { /* script query */ $script_queries = get_data_query_array($data_source["snmp_query_id"]); /* get the device override fields */ $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id=$local_data_id"); /* get device fields first */ $device_fields = array_rekey(db_fetch_assoc("SELECT data_input_fields.type_code, data_input_data.value FROM data_input_fields LEFT JOIN data_input_data ON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ") WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname')) AND data_input_data.value != ''"), "type_code", "value"); $data_template_fields = array_rekey(db_fetch_assoc("SELECT data_input_fields.type_code, data_input_data.value FROM data_input_fields LEFT JOIN data_input_data ON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=$data_template_id) WHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname')) AND data_template_data_id=$data_template_id AND data_input_data.value != ''"), "type_code", "value"); if (sizeof($device_fields)) { if (sizeof($data_template_fields)) { foreach($data_template_fields as $key => $value) { if (!isset($device_fields[$key])) { $device_fields[$key] = $value; } } } } elseif (sizeof($data_template_fields)) { $device_fields = $data_template_fields; } if (sizeof($outputs) > 0) { foreach ($outputs as $output) { if (isset($script_queries["fields"]{$output["snmp_field_name"]}["query_name"])) { $identifier = $script_queries["fields"]{$output["snmp_field_name"]}["query_name"]; /* fall back to non-script server actions if the user is running a version of php older than 4.3 */ if (($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) && (function_exists("proc_open"))) { $action = POLLER_ACTION_SCRIPT_PHP; $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"] . " " . $script_queries["script_function"], $data_source["device_id"]); }else if (($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) && (!function_exists("proc_open"))) { $action = POLLER_ACTION_SCRIPT; $script_path = read_config_option("path_php_binary") . " -q " . get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["device_id"]); }else{ $action = POLLER_ACTION_SCRIPT; $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["device_id"]); } } if (isset($script_path)) { $poller_items[] = api_poller_cache_item_add($data_source["device_id"], $device_fields, $local_data_id, $data_input["rrd_step"], $action, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), addslashes($script_path)); } } } } } if ($commit) { poller_update_poller_cache_from_buffer((array)$local_data_id, $poller_items); } else { return $poller_items; } }
function graphs() { /* ================= input validation ================= */ input_validate_input_number(get_request_var_request("host_id")); input_validate_input_number(get_request_var_request("graph_type")); /* ==================================================== */ /* clean up search string */ if (isset($_REQUEST["filter"])) { $_REQUEST["filter"] = sanitize_search_string(get_request_var("filter")); } /* if the user pushed the 'clear' button */ if (isset($_REQUEST["clear_x"])) { kill_session_var("sess_graphs_new_filter"); unset($_REQUEST["filter"]); $changed = true; } else { /* if any of the settings changed, reset the page number */ $changed = false; $changed += check_changed("host_id", "sess_graphs_new_host_id"); $changed += check_changed("graph_type", "sess_graphs_new_graph_type"); $changed += check_changed("filter", "sess_graphs_new_filter"); } load_current_session_value("host_id", "sess_graphs_new_host_id", db_fetch_cell("select id from host order by description,hostname limit 1")); load_current_session_value("graph_type", "sess_graphs_new_graph_type", read_config_option("default_graphs_new_dropdown")); load_current_session_value("filter", "sess_graphs_new_filter", ""); $host = db_fetch_row("select id,description,hostname,host_template_id from host where id=" . $_REQUEST["host_id"]); $row_limit = read_config_option("num_rows_data_query"); $debug_log = debug_log_return("new_graphs"); $header = " [ " . htmlspecialchars($host["description"]) . " (" . htmlspecialchars($host["hostname"]) . ") " . (!empty($host["host_template_id"]) ? htmlspecialchars(db_fetch_cell("select name from host_template where id=" . $host["host_template_id"])) : '') . " ]"; html_start_box("<strong>New Graphs for</strong> {$header}", "100%", "", "3", "center", ""); form_alternate_row(); print "<td>"; if (!empty($debug_log)) { debug_log_clear("new_graphs"); if (read_config_option("cacti_popup_messages") == "on") { ?> <div id='message'> <?php print "<table align='center' class='cactiTable' width='100%'><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>"; ?> <?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>"; ?> </div> <?php } else { ?> <table width='100%' class='textArea' align='center'> <tr class='even'> <td style="padding: 3px; font-family: monospace;"> <ul style='margin:0px 5px;padding-left:10px'><?php print $debug_log; ?> </ul> </td> </tr> </table> <br><?php } } ?> <script type="text/javascript"> <!-- <?php if (read_config_option("cacti_popup_messages") == "on") { ?> var obj = document.getElementById('message'); if (obj) { if (window.innerHeight) { height = window.innerHeight; width = window.innerWidth; }else{ height = document.body.clientHeight; width = document.body.clientWidth; } obj.style.class = "popupBox"; cw = obj.offsetWidth; // Adjust for IE6 if (!cw) cw = 150; ch = obj.offsetHeight; obj.style.top = '65px'; obj.style.left = ((width/2) - (cw/2) - 88)+'px'; } <?php } ?> function applyGraphsNewFilterChange(objForm) { strURL = '?graph_type=' + objForm.graph_type.value; strURL = strURL + '&host_id=' + objForm.host_id.value; strURL = strURL + '&filter=' + objForm.filter.value;; document.location = strURL; } --> </script> <form name="form_graphs_new" action="graphs_new.php"> <table width="100%" class="textHeader" cellpadding="2" align="left"> <tr> <td width="55" valign='top'> Host: </td> <td width="1" valign='top'> <select name="host_id" onChange="applyGraphsNewFilterChange(document.form_graphs_new)"> <?php $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host order by description,hostname"); if (sizeof($hosts) > 0) { foreach ($hosts as $item) { print "<option value='" . $item["id"] . "'"; if ($_REQUEST["host_id"] == $item["id"]) { print " selected"; } print ">" . htmlspecialchars($item["name"]) . "</option>\n"; } } ?> </select> </td> <td style='white-space:nowrap;' valign='top' width="100"> Graph Types: </td> <td width="1" valign='top'> <select name="graph_type" onChange="applyGraphsNewFilterChange(document.form_graphs_new)"> <option value="-2"<?php if ($_REQUEST["graph_type"] == "-2") { ?> selected<?php } ?> >All</option> <option value="-1"<?php if ($_REQUEST["graph_type"] == "-1") { ?> selected<?php } ?> >Graph Template Based</option> <?php $snmp_queries = db_fetch_assoc("SELECT\n\t\t\t\t\tsnmp_query.id,\n\t\t\t\t\tsnmp_query.name,\n\t\t\t\t\tsnmp_query.xml_path\n\t\t\t\t\tFROM (snmp_query,host_snmp_query)\n\t\t\t\t\tWHERE host_snmp_query.snmp_query_id=snmp_query.id\n\t\t\t\t\tAND host_snmp_query.host_id=" . $host["id"] . "\n\t\t\t\t\tORDER BY snmp_query.name"); if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $query) { print "<option value='" . $query["id"] . "'"; if ($_REQUEST["graph_type"] == $query["id"]) { print " selected"; } print ">" . $query["name"] . "</option>\n"; } } ?> </select> </td> <td rowspan="2" class="textInfo" align="right" valign="top"> <span class="linkMarker">*</span><a href="<?php print htmlspecialchars("host.php?action=edit&id=" . $_REQUEST["host_id"]); ?> ">Edit this Host</a><br> <span class="linkMarker">*</span><a href="<?php print htmlspecialchars("host.php?action=edit"); ?> ">Create New Host</a><br> <?php api_plugin_hook('graphs_new_top_links'); ?> </td> </tr> <?php if ($_REQUEST["graph_type"] > 0) { ?> <tr> <td width="55" valign='top'> Search: </td> <td valign='top'> <input type="text" name="filter" size="30" value="<?php print htmlspecialchars(get_request_var_request("filter")); ?> "> </td> <td colspan='2' valign='top'> <input type="submit" value="Go" title="Set/Refresh Filters"> <input type="submit" name="clear_x" value="Clear" title="Clear Filters"> </td> </tr> <?php } else { form_hidden_box("filter", $_REQUEST["filter"], ""); } ?> </table> </form> </td> </tr> <?php html_end_box(); ?> <form name="chk" method="post" action="graphs_new.php"> <?php $total_rows = sizeof(db_fetch_assoc("select graph_template_id from host_graph where host_id=" . $_REQUEST["host_id"])); $i = 0; if ($changed) { foreach ($snmp_queries as $query) { kill_session_var("sess_graphs_new_page" . $query["id"]); unset($_REQUEST["page" . $query["id"]]); load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1"); } } if ($_REQUEST["graph_type"] > 0) { load_current_session_value("page" . $_REQUEST["graph_type"], "sess_graphs_new_page" . $_REQUEST["graph_type"], "1"); } else { if ($_REQUEST["graph_type"] == -2) { foreach ($snmp_queries as $query) { load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1"); } } } $script = "<script type='text/javascript'>\nvar gt_created_graphs = new Array();\nvar created_graphs = new Array()\n"; if ($_REQUEST["graph_type"] < 0) { html_start_box("<strong>Graph Templates</strong>", "100%", "", "3", "center", ""); print "<tr class='tableHeader'>\n\t\t\t\t<td class='tableSubHeaderColumn'>Graph Template Name</td>\n\t\t\t\t<td width='1%' align='center' class='tableSubHeaderCheckbox' 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\n\t\t\t</tr>\n"; $graph_templates = db_fetch_assoc("SELECT\n\t\t\tgraph_templates.id AS graph_template_id,\n\t\t\tgraph_templates.name AS graph_template_name\n\t\t\tFROM (host_graph,graph_templates)\n\t\t\tWHERE host_graph.graph_template_id=graph_templates.id\n\t\t\tAND host_graph.host_id=" . $_REQUEST["host_id"] . "\n\t\t\tORDER BY graph_templates.name"); $template_graphs = db_fetch_assoc("SELECT\n\t\t\tgraph_local.graph_template_id\n\t\t\tFROM (graph_local,host_graph)\n\t\t\tWHERE graph_local.graph_template_id=host_graph.graph_template_id\n\t\t\tAND graph_local.host_id=host_graph.host_id\n\t\t\tAND graph_local.host_id=" . $host["id"] . "\n\t\t\tGROUP BY graph_local.graph_template_id"); if (sizeof($template_graphs) > 0) { $script .= "var gt_created_graphs = new Array("; $cg_ctr = 0; foreach ($template_graphs as $template_graph) { $script .= ($cg_ctr > 0 ? "," : "") . "'" . $template_graph["graph_template_id"] . "'"; $cg_ctr++; } $script .= ")\n"; } /* create a row for each graph template associated with the host template */ if (sizeof($graph_templates) > 0) { foreach ($graph_templates as $graph_template) { $query_row = $graph_template["graph_template_id"]; print "<tr id='gt_line{$query_row}' class='" . ($i % 2 == 0 ? "odd" : "even") . "'>"; $i++; print "\t\t<td onClick='gt_select_line(" . $graph_template["graph_template_id"] . ");'>\n\t\t\t\t\t\t<span id='gt_text{$query_row}" . "_0'><strong>Create:</strong> " . htmlspecialchars($graph_template["graph_template_name"]) . "</span>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td align='right'>\n\t\t\t\t\t\t<input type='checkbox' name='cg_{$query_row}' id='cg_{$query_row}' onClick='gt_update_selection_indicators();'>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>"; } } $script .= "gt_update_deps(1);\n"; $available_graph_templates = db_fetch_assoc("SELECT\n\t\t\tgraph_templates.id, graph_templates.name\n\t\t\tFROM snmp_query_graph RIGHT JOIN graph_templates\n\t\t\tON (snmp_query_graph.graph_template_id = graph_templates.id)\n\t\t\tWHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name"); /* create a row at the bottom that lets the user create any graph they choose */ print "\t<tr class='" . ($i % 2 == 0 ? "odd" : "even") . "'>\n\t\t\t\t<td colspan='2' width='60' nowrap>\n\t\t\t\t\t<strong>Create:</strong> "; form_dropdown("cg_g", $available_graph_templates, "name", "id", "", "(Select a graph type to create)", "", "textArea"); print "\t\t</td>\n\t\t\t</tr>"; html_end_box(); } if ($_REQUEST["graph_type"] != -1) { $snmp_queries = db_fetch_assoc("SELECT\n\t\t\tsnmp_query.id,\n\t\t\tsnmp_query.name,\n\t\t\tsnmp_query.xml_path\n\t\t\tFROM (snmp_query,host_snmp_query)\n\t\t\tWHERE host_snmp_query.snmp_query_id=snmp_query.id\n\t\t\tAND host_snmp_query.host_id=" . $host["id"] . ($_REQUEST["graph_type"] != -2 ? " AND snmp_query.id=" . $_REQUEST["graph_type"] : '') . "\n\t\t\tORDER BY snmp_query.name"); if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $snmp_query) { unset($total_rows); if (!$changed) { $page = $_REQUEST["page" . $snmp_query["id"]]; } else { $page = 1; } $xml_array = get_data_query_array($snmp_query["id"]); $num_input_fields = 0; $num_visible_fields = 0; if ($xml_array != false) { /* loop through once so we can find out how many input fields there are */ reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { $num_input_fields++; if (!isset($total_rows)) { $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}'"); } } } } if (!isset($total_rows)) { $total_rows = 0; } $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"); if (sizeof($snmp_query_graphs) > 0) { foreach ($snmp_query_graphs as $snmp_query_graph) { $created_graphs = db_fetch_assoc("SELECT DISTINCT\n\t\t\t\t\t\tdata_local.snmp_index\n\t\t\t\t\t\tFROM (data_local,data_template_data)\n\t\t\t\t\t\tLEFT JOIN data_input_data ON (data_template_data.id=data_input_data.data_template_data_id)\n\t\t\t\t\t\tLEFT JOIN data_input_fields ON (data_input_data.data_input_field_id=data_input_fields.id)\n\t\t\t\t\t\tWHERE data_local.id=data_template_data.local_data_id\n\t\t\t\t\t\tAND data_input_fields.type_code='output_type'\n\t\t\t\t\t\tAND data_input_data.value='" . $snmp_query_graph["id"] . "'\n\t\t\t\t\t\tAND data_local.host_id=" . $host["id"]); $script .= "created_graphs[" . $snmp_query_graph["id"] . "] = new Array("; $cg_ctr = 0; if (sizeof($created_graphs) > 0) { foreach ($created_graphs as $created_graph) { $script .= ($cg_ctr > 0 ? "," : "") . "'" . encode_data_query_index($created_graph["snmp_index"]) . "'"; $cg_ctr++; } } $script .= ")\n"; } } print "\t<table width='100%' class='cactiTable' align='center' cellpadding='3' cellspacing='0'>\n\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan='" . ($num_input_fields + 1) . "'>\n\t\t\t\t\t\t\t<table cellspacing='0' cellpadding='0' width='100%' >\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<td class='textHeaderDark'>\n\t\t\t\t\t\t\t\t\t\t<strong>Data Query</strong> [" . $snmp_query["name"] . "]\n\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t<td align='right' nowrap>\n\t\t\t\t\t\t\t\t\t\t<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>\n\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>"; if ($xml_array != false) { $html_dq_header = ""; $snmp_query_indexes = array(); reset($xml_array["fields"]); /* if there is a where clause, get the matching snmp_indexes */ $sql_where = ""; if (strlen($_REQUEST["filter"])) { $sql_where = ""; $indexes = db_fetch_assoc("SELECT DISTINCT snmp_index\n\t\t\t\t\t\tFROM host_snmp_cache\n\t\t\t\t\t\tWHERE field_value LIKE '%%" . $_REQUEST["filter"] . "%%'\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query["id"] . "\n\t\t\t\t\t\tAND host_id=" . $host["id"]); if (sizeof($indexes)) { foreach ($indexes as $index) { if (strlen($sql_where)) { $sql_where .= ", '" . $index["snmp_index"] . "'"; } else { $sql_where .= " AND snmp_index IN('" . $index["snmp_index"] . "'"; } } $sql_where .= ")"; } } if (strlen($_REQUEST["filter"]) == 0 || strlen($_REQUEST["filter"]) && sizeof($indexes)) { /* determine the sort order */ if (isset($xml_array["index_order_type"])) { if ($xml_array["index_order_type"] == "numeric") { $sql_order = "ORDER BY CAST(snmp_index AS unsigned)"; } else { if ($xml_array["index_order_type"] == "alphabetic") { $sql_order = "ORDER BY snmp_index"; } else { if ($xml_array["index_order_type"] == "natural") { $sql_order = "ORDER BY INET_ATON(snmp_index)"; } else { $sql_order = ""; } } } } else { $sql_order = ""; } /* get the unique field values from the database */ $field_names = db_fetch_assoc("SELECT DISTINCT field_name\n\t\t\t\t\t\tFROM host_snmp_cache\n\t\t\t\t\t\tWHERE host_id=" . $host["id"] . "\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query["id"]); /* build magic query */ $sql_query = "SELECT host_id, snmp_query_id, snmp_index"; $num_visible_fields = sizeof($field_names); $i = 0; if (sizeof($field_names) > 0) { foreach ($field_names as $column) { $field_name = $column["field_name"]; $sql_query .= ", MAX(CASE WHEN field_name='{$field_name}' THEN field_value ELSE NULL END) AS '{$field_name}'"; $i++; } } $sql_query .= " FROM host_snmp_cache\n\t\t\t\t\t\tWHERE host_id=" . $host["id"] . "\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query["id"] . "\n\t\t\t\t\t\t{$sql_where}\n\t\t\t\t\t\tGROUP BY host_id, snmp_query_id, snmp_index\n\t\t\t\t\t\t{$sql_order}\n\t\t\t\t\t\tLIMIT " . $row_limit * ($page - 1) . "," . $row_limit; $rows_query = "SELECT host_id, snmp_query_id, snmp_index\n\t\t\t\t\t\tFROM host_snmp_cache\n\t\t\t\t\t\tWHERE host_id=" . $host["id"] . "\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query["id"] . "\n\t\t\t\t\t\t{$sql_where}\n\t\t\t\t\t\tGROUP BY host_id, snmp_query_id, snmp_index"; $snmp_query_indexes = db_fetch_assoc($sql_query); $total_rows = sizeof(db_fetch_assoc($rows_query)); if (($page - 1) * $row_limit > $total_rows) { $page = 1; $_REQUEST["page" . $query["id"]] = $page; load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1"); } $nav = html_nav_bar("graphs_new.php", MAX_DISPLAY_PAGES, $page, $row_limit, $total_rows, 15, "Items", "page" . $snmp_query["id"]); print $nav; while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { foreach ($field_names as $row) { if ($row["field_name"] == $field_name) { $html_dq_header .= "<td class='tableSubHeaderColumn'>" . $field_array["name"] . "</td>\n"; break; } } } } if (!sizeof($snmp_query_indexes)) { print "<tr class='odd'><td>This data query returned 0 rows, perhaps there was a problem executing this\n\t\t\t\t\t\t\tdata query. You can <a href='" . htmlspecialchars("host.php?action=query_verbose&id=" . $snmp_query["id"] . "&host_id=" . $host["id"]) . "'>run this data\n\t\t\t\t\t\t\tquery in debug mode</a> to get more information.</td></tr>\n"; } else { print "<tr class='tableHeader'>\n\t\t\t\t\t\t\t\t{$html_dq_header}\n\t\t\t\t\t\t\t\t<td width='1%' align='center' class='tableSubHeaderCheckbox' 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\n\t\t\t\t\t\t\t</tr>\n"; } $row_counter = 0; $column_counter = 0; $fields = array_rekey($field_names, "field_name", "field_name"); if (sizeof($snmp_query_indexes) > 0) { foreach ($snmp_query_indexes as $row) { $query_row = $snmp_query["id"] . "_" . encode_data_query_index($row["snmp_index"]); print "<tr id='line{$query_row}' class='" . ($row_counter % 2 == 0 ? "odd" : "even") . "'>"; $i++; $column_counter = 0; reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { if (in_array($field_name, $fields)) { if (isset($row[$field_name])) { 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"]) ? preg_replace("/(" . preg_quote($_REQUEST["filter"]) . ")/i", "<span style='background-color: #F8D93D;'>\\1</span>", $row[$field_name]) : $row[$field_name]) . "</span></td>"; } else { 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>"; } $column_counter++; } } } print "<td align='right'>"; print "<input type='checkbox' name='sg_{$query_row}' id='sg_{$query_row}' onClick='dq_update_selection_indicators();'>"; print "</td>"; print "</tr>\n"; $row_counter++; } } if ($total_rows > $row_limit) { print $nav; } } else { print "<tr class='odd'><td colspan='2' style='color: red; font-size: 12px; font-weight: bold;'>Search Returned no Rows.</td></tr>\n"; } } else { print "<tr class='odd'><td colspan='2' style='color: red; font-size: 12px; font-weight: bold;'>Error in data query.</td></tr>\n"; } print "</table>"; /* draw the graph template drop down here */ $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"); if (sizeof($data_query_graphs) == 1) { echo "<input type='hidden' id='sgg_" . $snmp_query["id"] . "' name='sgg_" . $snmp_query["id"] . "' value='" . $data_query_graphs[0]["id"] . "'>\n"; } elseif (sizeof($data_query_graphs) > 1) { print "\t<table align='center' width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='1' valign='top'>\n\t\t\t\t\t\t\t\t<img src='images/arrow.gif' alt=''> \n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td align='right'>\n\t\t\t\t\t\t\t\t<span style='font-size: 12px; font-style: italic;'>Select a graph type:</span> \n\t\t\t\t\t\t\t\t<select name='sgg_" . $snmp_query["id"] . "' id='sgg_" . $snmp_query["id"] . "' onChange='dq_update_deps(" . $snmp_query["id"] . "," . (isset($column_counter) ? $column_counter : "") . ");'>\n\t\t\t\t\t\t\t\t\t"; html_create_list($data_query_graphs, "name", "id", "0"); print "\n\t\t\t\t\t\t\t\t</select>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>"; } print "<br>"; $script .= "dq_update_deps(" . $snmp_query["id"] . "," . $num_visible_fields . ");\n"; } } } if (strlen($script)) { $script .= "</script>\n"; print $script; } form_hidden_box("save_component_graph", "1", ""); form_hidden_box("host_id", $host["id"], "0"); form_hidden_box("host_template_id", $host["host_template_id"], "0"); if (isset($_SERVER["HTTP_REFERER"]) && !substr_count($_SERVER["HTTP_REFERER"], "graphs_new")) { $_REQUEST["returnto"] = basename($_SERVER["HTTP_REFERER"]); } load_current_session_value("returnto", "sess_graphs_new_returnto", ""); form_save_button($_REQUEST["returnto"]); print "<script type='text/javascript'>dq_update_selection_indicators();</script>\n"; print "<script type='text/javascript'>gt_update_selection_indicators();</script>\n"; }
function update_poller_cache($local_data_id, $truncate_performed = false) { global $config; include_once($config["library_path"] . "/data_query.php"); include_once($config["library_path"] . "/api_poller.php"); $data_input = db_fetch_row("select data_input.id, data_input.type_id, data_template_data.id as data_template_data_id, data_template_data.data_template_id, data_template_data.active, data_template_data.rrd_step from (data_template_data,data_input) where data_template_data.data_input_id=data_input.id and data_template_data.local_data_id=$local_data_id"); $data_source = db_fetch_row("select host_id,snmp_query_id,snmp_index from data_local where id=$local_data_id"); /* clear cache for this local_data_id */ if (!$truncate_performed) { db_execute("delete from poller_item where local_data_id=$local_data_id"); } /* we have to perform some additional sql queries if this is a "query" */ if (($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) || ($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY) || ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER)){ $field = data_query_field_list($data_input["data_template_data_id"]); if (($data_input["type_id"] != DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) && (empty($field["output_type"]))) { return; } if (strlen($field["output_type"])) { $output_type_sql = "and snmp_query_graph_rrd.snmp_query_graph_id=" . $field["output_type"]; }else{ $output_type_sql = ""; } $outputs = db_fetch_assoc("select snmp_query_graph_rrd.snmp_field_name, data_template_rrd.id as data_template_rrd_id from (snmp_query_graph_rrd,data_template_rrd) where snmp_query_graph_rrd.data_template_rrd_id=data_template_rrd.local_data_template_rrd_id $output_type_sql and snmp_query_graph_rrd.data_template_id=" . $data_input["data_template_id"] . " and data_template_rrd.local_data_id=$local_data_id"); } if ($data_input["active"] == "on") { if (($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT) || ($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER)) { /* script */ /* fall back to non-script server actions if the user is running a version of php older than 4.3 */ if (($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) && (function_exists("proc_open"))) { $action = POLLER_ACTION_SCRIPT_PHP; $script_path = get_full_script_path($local_data_id); }else if (($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) && (!function_exists("proc_open"))) { $action = POLLER_ACTION_SCRIPT; $script_path = read_config_option("path_php_binary") . " -q " . get_full_script_path($local_data_id); }else{ $action = POLLER_ACTION_SCRIPT; $script_path = get_full_script_path($local_data_id); } $num_output_fields = sizeof(db_fetch_assoc("select id from data_input_fields where data_input_id=" . $data_input["id"] . " and input_output='out' and update_rra='on'")); if ($num_output_fields == 1) { $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id=$local_data_id"); $data_source_item_name = get_data_source_item_name($data_template_rrd_id); }else{ $data_source_item_name = ""; } api_poller_cache_item_add($data_source["host_id"], array(), $local_data_id, $data_input["rrd_step"], $action, $data_source_item_name, 1, addslashes($script_path)); }else if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP) { /* snmp */ $host_fields = array_rekey(db_fetch_assoc("select data_input_fields.type_code, data_input_data.value from data_input_fields left join data_input_data on (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ") where (data_input_fields.type_code='snmp_oid' or data_input_fields.type_code='hostname' or data_input_fields.type_code='snmp_community' or data_input_fields.type_code='snmp_username' or data_input_fields.type_code='snmp_password' or data_input_fields.type_code='snmp_auth_protocol' or data_input_fields.type_code='snmp_priv_passphrase' or data_input_fields.type_code='snmp_priv_protocol' or data_input_fields.type_code='snmp_context' or data_input_fields.type_code='snmp_version' or data_input_fields.type_code='snmp_port' or data_input_fields.type_code='snmp_timeout') and data_input_data.value != ''"), "type_code", "value"); $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id=$local_data_id"); api_poller_cache_item_add($data_source["host_id"], $host_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($data_template_rrd_id), 1, (isset($host_fields["snmp_oid"]) ? $host_fields["snmp_oid"] : "")); }else if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) { /* snmp query */ $snmp_queries = get_data_query_array($data_source["snmp_query_id"]); if (sizeof($outputs) > 0) { foreach ($outputs as $output) { if (isset($snmp_queries["fields"]{$output["snmp_field_name"]}["oid"])) { $oid = $snmp_queries["fields"]{$output["snmp_field_name"]}["oid"] . "." . $data_source["snmp_index"]; } if (!empty($oid)) { api_poller_cache_item_add($data_source["host_id"], array(), $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), $oid); } } } }else if (($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY) || ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER)) { /* script query */ $script_queries = get_data_query_array($data_source["snmp_query_id"]); if (sizeof($outputs) > 0) { foreach ($outputs as $output) { if (isset($script_queries["fields"]{$output["snmp_field_name"]}["query_name"])) { $identifier = $script_queries["fields"]{$output["snmp_field_name"]}["query_name"]; /* fall back to non-script server actions if the user is running a version of php older than 4.3 */ if (($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) && (function_exists("proc_open"))) { $action = POLLER_ACTION_SCRIPT_PHP; $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"] . " " . $script_queries["script_function"], $data_source["host_id"]); }else if (($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) && (!function_exists("proc_open"))) { $action = POLLER_ACTION_SCRIPT; $script_path = read_config_option("path_php_binary") . " -q " . get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["host_id"]); }else{ $action = POLLER_ACTION_SCRIPT; $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["host_id"]); } } if (isset($script_path)) { api_poller_cache_item_add($data_source["host_id"], array(), $local_data_id, $data_input["rrd_step"], $action, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), addslashes($script_path)); } } } } } }
/** displaySNMPValuesExtended * * @param int $deviceId * @param string $fields * @param int $snmpQueryId * @param bool $quietMode */ function displaySNMPValuesExtended($deviceId, $fields, $snmpQueryId, $quietMode = FALSE) { $exit_code = 1; # assume an error until we've printed sth $req_fields = array(); if (strlen($fields) > 0) { # remove unwanted blanks $query_FieldSpec = str_replace(" ", "", $fields); # add tics for SQL query $query_FieldSpec = str_replace(",", "','", $query_FieldSpec); $query_FieldSpec = " AND field_name in ('" . $query_FieldSpec . "')"; } else { $query_FieldSpec = ""; } $xml_array = get_data_query_array($snmpQueryId); if ($xml_array != false) { /* loop through once so we can find out how many input fields there are */ reset($xml_array["fields"]); while (list ($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { # if spec was given ... if (strlen($fields) > 0) { # ... but current field doesn't match (make it case-insensitive, beware of the users!) if (strpos(strtolower($fields), strtolower($field_name)) === false) { # skip it continue; } } $req_fields[$field_name] = $xml_array["fields"][$field_name]; /* initialize column lengths */ $req_fields[$field_name]["length"] = $quietMode ? 0 : max(strlen($field_array["name"]), strlen($field_name)); if (!isset ($total_rows)) { $total_rows = db_fetch_cell("SELECT COUNT(*) FROM device_snmp_cache WHERE device_id=" . $deviceId . " AND snmp_query_id=" . $snmpQueryId . " AND field_name='$field_name'"); } } } if (!isset ($total_rows)) { if (!sizeof($req_fields)) { echo __("ERROR: Invalid --snmp-field-spec (found: %s) given", $fields) . "\n"; echo __("Try --list-snmp-fields") . "\n"; return (1); } else { echo __("ERROR: No cached SNMP values found for this SNMP Query") . "\n"; return (1); } } $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=" . $snmpQueryId . " ORDER BY snmp_query_graph.name"); reset($req_fields); $snmp_query_indexes = array (); $sql_order = ""; /* get the unique field values from the database */ $field_names = db_fetch_assoc("SELECT DISTINCT " . "field_name " . "FROM " . "device_snmp_cache " . "WHERE " . "device_id=" . $deviceId . " AND " . "snmp_query_id=" . $snmpQueryId . $query_FieldSpec); /* build magic query */ $sql_query = "SELECT device_id, snmp_query_id, snmp_index"; if (sizeof($field_names) > 0) { foreach ($field_names as $column) { $field_name = $column["field_name"]; $sql_query .= ", MAX(CASE WHEN field_name='$field_name' THEN field_value ELSE NULL END) AS '$field_name'"; } } else { echo __("ERROR: No SNMP field names found for this SNMP Query") . "\n"; return (1); } $sql_query .= " FROM device_snmp_cache " . "WHERE device_id=" . $deviceId . " AND snmp_query_id=" . $snmpQueryId . $query_FieldSpec . " GROUP BY device_id, snmp_query_id, snmp_index " . $sql_order; $snmp_query_indexes = db_fetch_assoc($sql_query); if (!sizeof($snmp_query_indexes)) { print __("This data query returned 0 rows, perhaps there was a problem executing this data query.") . "\n"; return (1); } if (sizeof($snmp_query_indexes) > 0) { foreach ($snmp_query_indexes as $row) { # determine length of each data field reset($req_fields); while (list ($field_name, $field_array) = each($req_fields)) { /* verify, that the requested field is known in snmp_cache */ if (isset($row[$field_name])) { $req_fields[$field_name]["length"] = max($req_fields[$field_name]["length"],strlen($row[$field_name])); } } } } if (!$quietMode) { if ($fields === "") { echo __("Known values for device-id") . " " . $deviceId . ":\n"; } else { echo __("Known values for device-id") . " " . $deviceId . ": (" . $fields . ")\n"; } # now print headers: field identifier and field names reset($req_fields); while (list ($field_name, $field_array) = each($req_fields)) { foreach ($field_names as $row) { if ($row["field_name"] == $field_name) { print(str_pad($field_name, $req_fields[$field_name]["length"]+1)); break; } } } print "\n"; reset($req_fields); while (list ($field_name, $field_array) = each($req_fields)) { foreach ($field_names as $row) { if ($row["field_name"] == $field_name) { print(str_pad($field_array["name"], $req_fields[$field_name]["length"]+1)); break; } } } print "\n"; } # and data, finally if (sizeof($snmp_query_indexes) > 0) { foreach ($snmp_query_indexes as $row) { reset($req_fields); while (list ($field_name, $field_array) = each($req_fields)) { if (isset($row[$field_name])) { print(str_pad($row[$field_name], $req_fields[$field_name]["length"]+1)); $exit_code = 0; } } print "\n"; } } } if (!$quietMode) { echo "\n"; } return($exit_code); }
function graphs() { global $colors; /* ================= input validation ================= */ input_validate_input_number(get_request_var_request("host_id")); input_validate_input_number(get_request_var_request("graph_type")); /* ==================================================== */ /* clean up search string */ if (isset($_REQUEST["filter"])) { $_REQUEST["filter"] = sanitize_search_string(get_request_var("filter")); } /* if the user pushed the 'clear' button */ if (isset($_REQUEST["clear_x"])) { kill_session_var("sess_graphs_new_host_id"); kill_session_var("sess_graphs_new_graph_type"); kill_session_var("sess_graphs_new_filter"); unset($_REQUEST["host_id"]); unset($_REQUEST["graph_type"]); unset($_REQUEST["filter"]); $changed = true; } else { /* if any of the settings changed, reset the page number */ $changed = 0; $changed += check_changed("host_id", "sess_graphs_new_host_id"); $changed += check_changed("graph_type", "sess_graphs_new_graph_type"); $changed += check_changed("filter", "sess_graphs_new_filter"); } load_current_session_value("host_id", "sess_graphs_new_host_id", db_fetch_cell("select id from host order by description,hostname limit 1")); load_current_session_value("graph_type", "sess_graphs_new_graph_type", read_config_option("default_graphs_new_dropdown")); load_current_session_value("filter", "sess_graphs_new_filter", ""); $host = db_fetch_row("select id,description,hostname,host_template_id from host where id=" . $_REQUEST["host_id"]); $row_limit = read_config_option("num_rows_data_query"); $debug_log = debug_log_return("new_graphs"); ?> <script type="text/javascript"> <!-- function applyGraphsNewFilterChange(objForm) { strURL = '?graph_type=' + objForm.graph_type.value; strURL = strURL + '&host_id=' + objForm.host_id.value; strURL = strURL + '&filter=' + objForm.filter.value;; document.location = strURL; } --> </script> <?php if (!empty($debug_log)) { debug_log_clear("new_graphs"); ?> <table width='100%' style='background-color: #f5f5f5; border: 1px solid #bbbbbb;' align='center'> <tr bgcolor="<?php print $colors["light"]; ?> "> <td style="padding: 3px; font-family: monospace;"> <?php print $debug_log; ?> </td> </tr> </table> <br> <?php } ?> <form name="form_graphs_new"> <table width="100%" cellpadding="4" align="center"> <tr> <td nowrap style='white-space: nowrap;' width="30%" class="textInfo"> <?php print $host["description"]; ?> (<?php print $host["hostname"]; ?> ) </td> <td align="left" class="textInfo" colspan="2" style="color: #aaaaaa;"> <?php if (!empty($host["host_template_id"])) { print db_fetch_cell("select name from host_template where id=" . $host["host_template_id"]); } ?> </td> </tr> </table> <table width="100%" cellpadding="0" align="center"> <tr> <td nowrap style='white-space: nowrap;' width="55" class="textArea"> Host: </td> <td width="1"> <select name="host_id" onChange="applyGraphsNewFilterChange(document.form_graphs_new)"> <?php $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host order by description,hostname"); if (sizeof($hosts) > 0) { foreach ($hosts as $item) { print "<option value='" . $item["id"] . "'"; if ($_REQUEST["host_id"] == $item["id"]) { print " selected"; } print ">" . $item["name"] . "</option>\n"; } } ?> </select> </td> <td nowrap style='white-space: nowrap;' width="100" class="textArea"> Graph Types: </td> <td width="1"> <select name="graph_type" onChange="applyGraphsNewFilterChange(document.form_graphs_new)"> <option value="-2"<?php if ($_REQUEST["graph_type"] == "-2") { ?> selected<?php } ?> >All</option> <option value="-1"<?php if ($_REQUEST["graph_type"] == "-1") { ?> selected<?php } ?> >Graph Template Based</option> <?php $snmp_queries = db_fetch_assoc("SELECT\n\t\t\t\t\tsnmp_query.id,\n\t\t\t\t\tsnmp_query.name,\n\t\t\t\t\tsnmp_query.xml_path\n\t\t\t\t\tFROM (snmp_query,host_snmp_query)\n\t\t\t\t\tWHERE host_snmp_query.snmp_query_id=snmp_query.id\n\t\t\t\t\tAND host_snmp_query.host_id=" . $host["id"] . "\n\t\t\t\t\tORDER BY snmp_query.name"); if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $query) { print "<option value='" . $query["id"] . "'"; if ($_REQUEST["graph_type"] == $query["id"]) { print " selected"; } print ">" . $query["name"] . "</option>\n"; } } ?> </select> </td> <td nowrap style='white-space: nowrap;' class="textInfo" align="center" valign="top"> <span style="white-space: nowrap; color: #c16921;">*</span><a href="host.php?action=edit&id=<?php print $_REQUEST["host_id"]; ?> ">Edit this Host</a><br> <span style="white-space: nowrap; color: #c16921;">*</span><a href="host.php?action=edit">Create New Host</a> </td> </tr> </table> <?php if ($_REQUEST["graph_type"] > 0) { ?> <table width="100%" cellpadding="0" align="center"> <tr> <td nowrap style='white-space: nowrap;' width="55" class="textArea"> Search: </td> <td nowrap style='white-space: nowrap;' width="200"> <input type="text" name="filter" size="30" width="200" value="<?php print $_REQUEST["filter"]; ?> "> </td> <td align="left" nowrap style='white-space: nowrap;'> <input type="image" src="images/button_go.gif" name="go" alt="Go" border="0" align="absmiddle"> <input type="image" src="images/button_clear.gif" name="clear" alt="Clear" border="0" align="absmiddle"> </td> </tr> </table> <?php } else { form_hidden_box("filter", $_REQUEST["filter"], ""); } ?> </form> <form name="chk" method="post" action="graphs_new.php"> <?php $total_rows = sizeof(db_fetch_assoc("select graph_template_id from host_graph where host_id=" . $_REQUEST["host_id"])); $i = 0; if ($_REQUEST["graph_type"] > 0) { load_current_session_value("page" . $_REQUEST["graph_type"], "sess_graphs_new_page" . $_REQUEST["graph_type"], "1"); } else { if ($_REQUEST["graph_type"] == -2) { foreach ($snmp_queries as $query) { load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1"); } } } if ($_REQUEST["graph_type"] < 0) { html_start_box("<strong>Graph Templates</strong>", "100%", $colors["header"], "3", "center", ""); print "\t<tr bgcolor='#" . $colors["header_panel"] . "'>\n\t\t\t\t<td class='textSubHeaderDark'>Graph Template Name</td>\n\t\t\t\t<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\n\t\t\t</tr>\n"; $graph_templates = db_fetch_assoc("SELECT\n\t\t\tgraph_templates.id AS graph_template_id,\n\t\t\tgraph_templates.name AS graph_template_name\n\t\t\tFROM (host_graph,graph_templates)\n\t\t\tWHERE host_graph.graph_template_id=graph_templates.id\n\t\t\tAND host_graph.host_id=" . $_REQUEST["host_id"] . "\n\t\t\tORDER BY graph_templates.name"); $template_graphs = db_fetch_assoc("SELECT\n\t\t\tgraph_local.graph_template_id\n\t\t\tFROM (graph_local,host_graph)\n\t\t\tWHERE graph_local.graph_template_id=host_graph.graph_template_id\n\t\t\tAND graph_local.host_id=host_graph.host_id\n\t\t\tAND graph_local.host_id=" . $host["id"] . "\n\t\t\tGROUP BY graph_local.graph_template_id"); print "<script type='text/javascript'>\nvar gt_created_graphs = new Array()\n</script>\n"; if (sizeof($template_graphs) > 0) { print "<script type='text/javascript'>\n<!--\n"; print "var gt_created_graphs = new Array("; $cg_ctr = 0; foreach ($template_graphs as $template_graph) { print ($cg_ctr > 0 ? "," : "") . "'" . $template_graph["graph_template_id"] . "'"; $cg_ctr++; } print ")\n"; print "//-->\n</script>\n"; } /* create a row for each graph template associated with the host template */ if (sizeof($graph_templates) > 0) { foreach ($graph_templates as $graph_template) { $query_row = $graph_template["graph_template_id"]; print "<tr id='gt_line{$query_row}' bgcolor='#" . ($i % 2 == 0 ? "ffffff" : $colors["light"]) . "'>"; $i++; print "\t\t<td onClick='gt_select_line(" . $graph_template["graph_template_id"] . ");'><span id='gt_text{$query_row}" . "_0'>\n\t\t\t\t\t\t<span id='gt_text{$query_row}" . "_0'><strong>Create:</strong> " . $graph_template["graph_template_name"] . "</span>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td align='right'>\n\t\t\t\t\t\t<input type='checkbox' name='cg_{$query_row}' id='cg_{$query_row}' onClick='gt_update_selection_indicators();'>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>"; } } print "<script type='text/javascript'>gt_update_deps(1);</script>\n"; $available_graph_templates = db_fetch_assoc("SELECT\n\t\t\tgraph_templates.id, graph_templates.name\n\t\t\tFROM snmp_query_graph RIGHT JOIN graph_templates\n\t\t\tON (snmp_query_graph.graph_template_id = graph_templates.id)\n\t\t\tWHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name"); /* create a row at the bottom that lets the user create any graph they choose */ print "\t<tr bgcolor='#" . ($i % 2 == 0 ? "ffffff" : $colors["light"]) . "'>\n\t\t\t\t<td colspan='2' width='60' nowrap>\n\t\t\t\t\t<strong>Create:</strong> "; form_dropdown("cg_g", $available_graph_templates, "name", "id", "", "(Select a graph type to create)", "", "textArea"); print "\t\t</td>\n\t\t\t</tr>"; html_end_box(); } if ($_REQUEST["graph_type"] != -1) { $snmp_queries = db_fetch_assoc("SELECT\n\t\t\tsnmp_query.id,\n\t\t\tsnmp_query.name,\n\t\t\tsnmp_query.xml_path\n\t\t\tFROM (snmp_query,host_snmp_query)\n\t\t\tWHERE host_snmp_query.snmp_query_id=snmp_query.id\n\t\t\tAND host_snmp_query.host_id=" . $host["id"] . ($_REQUEST["graph_type"] != -2 ? " AND snmp_query.id=" . $_REQUEST["graph_type"] : '') . "\n\t\t\tORDER BY snmp_query.name"); print "<script type='text/javascript'>\nvar created_graphs = new Array()\n</script>\n"; if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $snmp_query) { unset($total_rows); if (!$changed) { $page = $_REQUEST["page" . $snmp_query["id"]]; } else { $page = 1; } $xml_array = get_data_query_array($snmp_query["id"]); $num_input_fields = 0; $num_visible_fields = 0; if ($xml_array != false) { /* loop through once so we can find out how many input fields there are */ reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { $num_input_fields++; if (!isset($total_rows)) { $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}'"); } } } } if (!isset($total_rows)) { $total_rows = 0; } $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"); if (sizeof($snmp_query_graphs) > 0) { print "<script type='text/javascript'>\n<!--\n"; foreach ($snmp_query_graphs as $snmp_query_graph) { $created_graphs = db_fetch_assoc("SELECT DISTINCT\n\t\t\t\t\t\tdata_local.snmp_index\n\t\t\t\t\t\tFROM (data_local,data_template_data)\n\t\t\t\t\t\tLEFT JOIN data_input_data ON (data_template_data.id=data_input_data.data_template_data_id)\n\t\t\t\t\t\tLEFT JOIN data_input_fields ON (data_input_data.data_input_field_id=data_input_fields.id)\n\t\t\t\t\t\tWHERE data_local.id=data_template_data.local_data_id\n\t\t\t\t\t\tAND data_input_fields.type_code='output_type'\n\t\t\t\t\t\tAND data_input_data.value='" . $snmp_query_graph["id"] . "'\n\t\t\t\t\t\tAND data_local.host_id=" . $host["id"]); print "created_graphs[" . $snmp_query_graph["id"] . "] = new Array("; $cg_ctr = 0; if (sizeof($created_graphs) > 0) { foreach ($created_graphs as $created_graph) { print ($cg_ctr > 0 ? "," : "") . "'" . encode_data_query_index($created_graph["snmp_index"]) . "'"; $cg_ctr++; } } print ")\n"; } print "//-->\n</script>\n"; } print "\t<table width='100%' style='background-color: #" . $colors["form_alternate2"] . "; border: 1px solid #" . $colors["header"] . ";' align='center' cellpadding='3' cellspacing='0'>\n\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td bgcolor='#" . $colors["header"] . "' colspan='" . ($num_input_fields + 1) . "'>\n\t\t\t\t\t\t\t<table cellspacing='0' cellpadding='0' width='100%' >\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<td class='textHeaderDark'>\n\t\t\t\t\t\t\t\t\t\t<strong>Data Query</strong> [" . $snmp_query["name"] . "]\n\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t<td align='right' nowrap>\n\t\t\t\t\t\t\t\t\t\t<a href='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='Reload Associated Query' border='0' align='absmiddle'></a>\n\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>"; if ($xml_array != false) { $html_dq_header = ""; $snmp_query_indexes = array(); reset($xml_array["fields"]); /* if there is a where clause, get the matching snmp_indexes */ $sql_where = ""; if (strlen($_REQUEST["filter"])) { $sql_where = ""; $indexes = db_fetch_assoc("SELECT DISTINCT snmp_index\n\t\t\t\t\t\tFROM host_snmp_cache\n\t\t\t\t\t\tWHERE field_value LIKE '%%" . $_REQUEST["filter"] . "%%'\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query["id"] . "\n\t\t\t\t\t\tAND host_id=" . $host["id"]); if (sizeof($indexes)) { foreach ($indexes as $index) { if (strlen($sql_where)) { $sql_where .= ", '" . $index["snmp_index"] . "'"; } else { $sql_where .= " AND snmp_index IN('" . $index["snmp_index"] . "'"; } } $sql_where .= ")"; } } if (strlen($_REQUEST["filter"]) == 0 || strlen($_REQUEST["filter"]) && sizeof($indexes)) { /* determine the sort order */ if (isset($xml_array["index_order_type"])) { if ($xml_array["index_order_type"] == "numeric") { $sql_order = "ORDER BY CAST(snmp_index AS unsigned)"; } else { if ($xml_array["index_order_type"] == "alphabetic") { $sql_order = "ORDER BY snmp_index"; } else { if ($xml_array["index_order_type"] == "natural") { $sql_order = "ORDER BY INET_ATON(snmp_index)"; } else { $sql_order = ""; } } } } else { $sql_order = ""; } /* get the unique field values from the database */ $field_names = db_fetch_assoc("SELECT DISTINCT field_name\n\t\t\t\t\t\tFROM host_snmp_cache\n\t\t\t\t\t\tWHERE host_id=" . $host["id"] . "\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query["id"]); /* build magic query */ $sql_query = "SELECT host_id, snmp_query_id, snmp_index"; $num_visible_fields = sizeof($field_names); $i = 0; if (sizeof($field_names) > 0) { foreach ($field_names as $column) { $field_name = $column["field_name"]; $sql_query .= ", MAX(CASE WHEN field_name='{$field_name}' THEN field_value ELSE NULL END) AS '{$field_name}'"; $i++; } } $sql_query .= " FROM host_snmp_cache\n\t\t\t\t\t\tWHERE host_id=" . $host["id"] . "\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query["id"] . "\n\t\t\t\t\t\t{$sql_where}\n\t\t\t\t\t\tGROUP BY host_id, snmp_query_id, snmp_index\n\t\t\t\t\t\t{$sql_order}\n\t\t\t\t\t\tLIMIT " . $row_limit * ($page - 1) . "," . $row_limit; $rows_query = "SELECT host_id, snmp_query_id, snmp_index\n\t\t\t\t\t\tFROM host_snmp_cache\n\t\t\t\t\t\tWHERE host_id=" . $host["id"] . "\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query["id"] . "\n\t\t\t\t\t\t{$sql_where}\n\t\t\t\t\t\tGROUP BY host_id, snmp_query_id, snmp_index"; $snmp_query_indexes = db_fetch_assoc($sql_query); $total_rows = sizeof(db_fetch_assoc($rows_query)); if (($page - 1) * $row_limit > $total_rows) { $page = 1; $_REQUEST["page" . $query["id"]] = $page; load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1"); } if ($total_rows > $row_limit) { /* generate page list */ $url_page_select = get_page_list($page, MAX_DISPLAY_PAGES, $row_limit, $total_rows, "graphs_new.php?", "page" . $snmp_query["id"]); $nav = "<tr bgcolor='#" . $colors["header"] . "' class='noprint'>\n\t\t\t\t\t\t\t\t\t<td colspan='15'>\n\t\t\t\t\t\t\t\t\t\t<table width='100%' cellspacing='0' cellpadding='0' border='0'>\n\t\t\t\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t\t\t\t<td align='left' class='textHeaderDark'>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<strong><< "; if ($page > 1) { $nav .= "<a class='linkOverDark' href='graphs_new.php?page" . $snmp_query["id"] . "=" . ($page - 1) . "'>"; } $nav .= "Previous"; if ($page > 1) { $nav .= "</a>"; } $nav .= "</strong>\n\t\t\t\t\t\t\t\t\t\t\t\t</td>\n\n\t\t\t\t\t\t\t\t\t\t\t\t<td align='center' class='textHeaderDark'>\n\t\t\t\t\t\t\t\t\t\t\t\t\tShowing 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}]\n\t\t\t\t\t\t\t\t\t\t\t\t</td>\n\n\t\t\t\t\t\t\t\t\t\t\t\t<td align='right' class='textHeaderDark'>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<strong>"; if ($page * $row_limit < $total_rows) { $nav .= "<a class='linkOverDark' href='graphs_new.php?page" . $snmp_query["id"] . "=" . ($page + 1) . "'>"; } $nav .= "Next"; if ($page * $row_limit < $total_rows) { $nav .= "</a>"; } $nav .= " >></strong>\n\t\t\t\t\t\t\t\t\t\t\t\t</td>\n\n\t\t\t\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t</tr>\n"; print $nav; } while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { foreach ($field_names as $row) { if ($row["field_name"] == $field_name) { $html_dq_header .= "<td height='1'><strong><font color='#" . $colors["header_text"] . "'>" . $field_array["name"] . "</font></strong></td>\n"; break; } } } } if (!sizeof($snmp_query_indexes)) { print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td>This data query returned 0 rows, perhaps there was a problem executing this\n\t\t\t\t\t\t\tdata query. You can <a href='host.php?action=query_verbose&id=" . $snmp_query["id"] . "&host_id=" . $host["id"] . "'>run this data\n\t\t\t\t\t\t\tquery in debug mode</a> to get more information.</td></tr>\n"; } else { print "\t<tr bgcolor='#" . $colors["header_panel"] . "'>\n\t\t\t\t\t\t\t\t{$html_dq_header}\n\t\t\t\t\t\t\t\t<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\n\t\t\t\t\t\t\t</tr>\n"; } $row_counter = 0; $column_counter = 0; $fields = array_rekey($field_names, "field_name", "field_name"); if (sizeof($snmp_query_indexes) > 0) { foreach ($snmp_query_indexes as $row) { $query_row = $snmp_query["id"] . "_" . encode_data_query_index($row["snmp_index"]); print "<tr id='line{$query_row}' bgcolor='#" . ($row_counter % 2 == 0 ? "ffffff" : $colors["light"]) . "'>"; $i++; $column_counter = 0; reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { if (in_array($field_name, $fields)) { if (isset($row[$field_name])) { 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>"; } else { 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>"; } $column_counter++; } } } print "<td align='right'>"; print "<input type='checkbox' name='sg_{$query_row}' id='sg_{$query_row}' onClick='dq_update_selection_indicators();'>"; print "</td>"; print "</tr>\n"; $row_counter++; } } if ($total_rows > $row_limit) { print $nav; } } else { 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"; } } else { 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"; } print "</table>"; /* draw the graph template drop down here */ $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"); if (sizeof($data_query_graphs) == 1) { form_hidden_box("sgg_" . $snmp_query["id"] . "' id='sgg_" . $snmp_query["id"], $data_query_graphs[0]["id"], ""); } elseif (sizeof($data_query_graphs) > 1) { print "\t<table align='center' width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='1' valign='top'>\n\t\t\t\t\t\t\t\t<img src='images/arrow.gif' alt='' align='absmiddle'> \n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td align='right'>\n\t\t\t\t\t\t\t\t<span style='font-size: 12px; font-style: italic;'>Select a graph type:</span> \n\t\t\t\t\t\t\t\t<select name='sgg_" . $snmp_query["id"] . "' id='sgg_" . $snmp_query["id"] . "' onChange='dq_update_deps(" . $snmp_query["id"] . "," . $column_counter . ");'>\n\t\t\t\t\t\t\t\t\t"; html_create_list($data_query_graphs, "name", "id", "0"); print "\n\t\t\t\t\t\t\t\t</select>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>"; } print "<br>"; print "<script type='text/javascript'>dq_update_deps(" . $snmp_query["id"] . "," . $num_visible_fields . ");</script>\n"; } } } form_hidden_box("save_component_graph", "1", ""); form_hidden_box("host_id", $host["id"], "0"); form_hidden_box("host_template_id", $host["host_template_id"], "0"); form_save_button(isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "index.php"); print "<script type='text/javascript'>dq_update_selection_indicators();</script>\n"; print "<script type='text/javascript'>gt_update_selection_indicators();</script>\n"; }
function update_poller_cache($local_data_id, $commit = false) { global $config; include_once $config["library_path"] . "/data_query.php"; include_once $config["library_path"] . "/api_poller.php"; $poller_items = array(); $data_input = db_fetch_row("select\r\n\t\tdata_input.id,\r\n\t\tdata_input.type_id,\r\n\t\tdata_template_data.id as data_template_data_id,\r\n\t\tdata_template_data.data_template_id,\r\n\t\tdata_template_data.active,\r\n\t\tdata_template_data.rrd_step\r\n\t\tfrom (data_template_data,data_input)\r\n\t\twhere data_template_data.data_input_id=data_input.id\r\n\t\tand data_template_data.local_data_id={$local_data_id}"); $data_source = db_fetch_row("select host_id,snmp_query_id,snmp_index from data_local where id={$local_data_id}"); /* we have to perform some additional sql queries if this is a "query" */ if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY || $data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY || $data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) { $field = data_query_field_list($data_input["data_template_data_id"]); if (strlen($field["output_type"])) { $output_type_sql = "and snmp_query_graph_rrd.snmp_query_graph_id='" . $field["output_type"] . "'"; } else { $output_type_sql = ""; } $outputs = db_fetch_assoc("select\r\n\t\t\tsnmp_query_graph_rrd.snmp_field_name,\r\n\t\t\tdata_template_rrd.id as data_template_rrd_id\r\n\t\t\tfrom (snmp_query_graph_rrd,data_template_rrd)\r\n\t\t\twhere snmp_query_graph_rrd.data_template_rrd_id=data_template_rrd.local_data_template_rrd_id\r\n\t\t\t{$output_type_sql}\r\n\t\t\tand snmp_query_graph_rrd.data_template_id=" . $data_input["data_template_id"] . "\r\n\t\t\tand data_template_rrd.local_data_id={$local_data_id}\r\n\t\t\torder by data_template_rrd.id"); } if ($data_input["active"] == "on") { if ($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT || $data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER) { /* script */ /* fall back to non-script server actions if the user is running a version of php older than 4.3 */ if ($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER && function_exists("proc_open")) { $action = POLLER_ACTION_SCRIPT_PHP; $script_path = get_full_script_path($local_data_id); } else { if ($data_input["type_id"] == DATA_INPUT_TYPE_PHP_SCRIPT_SERVER && !function_exists("proc_open")) { $action = POLLER_ACTION_SCRIPT; $script_path = read_config_option("path_php_binary") . " -q " . get_full_script_path($local_data_id); } else { $action = POLLER_ACTION_SCRIPT; $script_path = get_full_script_path($local_data_id); } } $num_output_fields = sizeof(db_fetch_assoc("select id from data_input_fields where data_input_id=" . $data_input["id"] . " and input_output='out' and update_rra='on'")); if ($num_output_fields == 1) { $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id={$local_data_id}"); $data_source_item_name = get_data_source_item_name($data_template_rrd_id); } else { $data_source_item_name = ""; } $poller_items[] = api_poller_cache_item_add($data_source["host_id"], array(), $local_data_id, $data_input["rrd_step"], $action, $data_source_item_name, 1, addslashes($script_path)); } else { if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP) { /* snmp */ /* get the host override fields */ $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id={$local_data_id}"); /* get host fields first */ $host_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname') OR (type_code='host_id'))\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value"); $data_template_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id={$data_template_id})\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_template_data_id={$data_template_id}\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value"); if (sizeof($host_fields)) { if (sizeof($data_template_fields)) { foreach ($data_template_fields as $key => $value) { if (!isset($host_fields[$key])) { $host_fields[$key] = $value; } } } } elseif (sizeof($data_template_fields)) { $host_fields = $data_template_fields; } $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_id={$local_data_id}"); $poller_items[] = api_poller_cache_item_add($data_source["host_id"], $host_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($data_template_rrd_id), 1, isset($host_fields["snmp_oid"]) ? $host_fields["snmp_oid"] : ""); } else { if ($data_input["type_id"] == DATA_INPUT_TYPE_SNMP_QUERY) { /* snmp query */ $snmp_queries = get_data_query_array($data_source["snmp_query_id"]); /* get the host override fields */ $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id={$local_data_id}"); /* get host fields first */ $host_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value"); $data_template_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id={$data_template_id})\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_template_data_id={$data_template_id}\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value"); if (sizeof($host_fields)) { if (sizeof($data_template_fields)) { foreach ($data_template_fields as $key => $value) { if (!isset($host_fields[$key])) { $host_fields[$key] = $value; } } } } elseif (sizeof($data_template_fields)) { $host_fields = $data_template_fields; } if (sizeof($outputs) > 0) { foreach ($outputs as $output) { if (isset($snmp_queries["fields"][$output["snmp_field_name"]]["oid"])) { $oid = $snmp_queries["fields"][$output["snmp_field_name"]]["oid"] . "." . $data_source["snmp_index"]; if (isset($snmp_queries["fields"][$output["snmp_field_name"]]["oid_suffix"])) { $oid .= "." . $snmp_queries["fields"][$output["snmp_field_name"]]["oid_suffix"]; } } if (!empty($oid)) { $poller_items[] = api_poller_cache_item_add($data_source["host_id"], $host_fields, $local_data_id, $data_input["rrd_step"], 0, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), $oid); } } } } else { if ($data_input["type_id"] == DATA_INPUT_TYPE_SCRIPT_QUERY || $data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) { /* script query */ $script_queries = get_data_query_array($data_source["snmp_query_id"]); /* get the host override fields */ $data_template_id = db_fetch_cell("SELECT data_template_id FROM data_template_data WHERE local_data_id={$local_data_id}"); /* get host fields first */ $host_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=" . $data_input["data_template_data_id"] . ")\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value"); $data_template_fields = array_rekey(db_fetch_assoc("SELECT\r\n\t\t\t\tdata_input_fields.type_code,\r\n\t\t\t\tdata_input_data.value\r\n\t\t\t\tFROM data_input_fields\r\n\t\t\t\tLEFT JOIN data_input_data\r\n\t\t\t\tON (data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id={$data_template_id})\r\n\t\t\t\tWHERE ((type_code LIKE 'snmp_%') OR (type_code='hostname'))\r\n\t\t\t\tAND data_template_data_id={$data_template_id}\r\n\t\t\t\tAND data_input_data.value != ''"), "type_code", "value"); if (sizeof($host_fields)) { if (sizeof($data_template_fields)) { foreach ($data_template_fields as $key => $value) { if (!isset($host_fields[$key])) { $host_fields[$key] = $value; } } } } elseif (sizeof($data_template_fields)) { $host_fields = $data_template_fields; } if (sizeof($outputs) > 0) { foreach ($outputs as $output) { if (isset($script_queries["fields"][$output["snmp_field_name"]]["query_name"])) { $identifier = $script_queries["fields"][$output["snmp_field_name"]]["query_name"]; /* fall back to non-script server actions if the user is running a version of php older than 4.3 */ if ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER && function_exists("proc_open")) { $action = POLLER_ACTION_SCRIPT_PHP; $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"] . " " . $script_queries["script_function"], $data_source["host_id"]); } else { if ($data_input["type_id"] == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER && !function_exists("proc_open")) { $action = POLLER_ACTION_SCRIPT; $script_path = read_config_option("path_php_binary") . " -q " . get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["host_id"]); } else { $action = POLLER_ACTION_SCRIPT; $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] : "") . " " . $script_queries["arg_get"] . " " . $identifier . " " . $data_source["snmp_index"], $script_queries["script_path"], $data_source["host_id"]); } } } if (isset($script_path)) { $poller_items[] = api_poller_cache_item_add($data_source["host_id"], $host_fields, $local_data_id, $data_input["rrd_step"], $action, get_data_source_item_name($output["data_template_rrd_id"]), sizeof($outputs), addslashes($script_path)); } } } } } } } } if ($commit) { poller_update_poller_cache_from_buffer((array) $local_data_id, $poller_items); } else { return $poller_items; } }
function graphs() { global $colors; /* ================= input validation ================= */ input_validate_input_number(get_request_var_request("host_id")); /* ==================================================== */ /* use the first host in the list as the default */ if ((!isset($_SESSION["sess_graphs_new_host_id"])) && (empty($_REQUEST["host_id"]))) { $_REQUEST["host_id"] = db_fetch_cell("select id from host order by description,hostname limit 1"); } /* remember these search fields in session vars so we don't have to keep passing them around */ if (isset($_REQUEST["host_id"])) { $_SESSION["sess_graphs_new_host_id"] = $_REQUEST["host_id"]; }else{ $_REQUEST["host_id"] = $_SESSION["sess_graphs_new_host_id"]; } $host = db_fetch_row("select id,description,hostname,host_template_id from host where id=" . $_REQUEST["host_id"]); $debug_log = debug_log_return("new_graphs"); if (!empty($debug_log)) { debug_log_clear("new_graphs"); ?> <table width='98%' style='background-color: #f5f5f5; border: 1px solid #bbbbbb;' align='center'> <tr bgcolor="<?php print $colors["light"];?>"> <td style="padding: 3px; font-family: monospace;"> <?php print $debug_log;?> </td> </tr> </table> <br> <?php } ?> <form name="form_graph_id"> <table width="98%" align="center"> <tr> <td class="textInfo" colspan="2"> <?php print $host["description"];?> (<?php print $host["hostname"];?>) </td> <td align="right" class="textInfo" style="color: #aaaaaa;"> <?php if (!empty($host["host_template_id"])) { print db_fetch_cell("select name from host_template where id=" . $host["host_template_id"]); } ?> </td> </tr> <tr> <td> </td> </tr> <tr> <td class="textArea" style="padding: 3px;" width="300" nowrap> Create new graphs for the following host: </td> <td class="textInfo" rowspan="2" valign="top"> <span style="color: #c16921;">*</span><a href="host.php?action=edit&id=<?php print $_REQUEST["host_id"];?>">Edit this Host</a><br> <span style="color: #c16921;">*</span><a href="host.php?action=edit">Create New Host</a> </td> </tr> <td> <select name="cbo_graph_id" onChange="window.location=document.form_graph_id.cbo_graph_id.options[document.form_graph_id.cbo_graph_id.selectedIndex].value"> <?php $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host order by description,hostname"); if (sizeof($hosts) > 0) { foreach ($hosts as $item) { print "<option value='graphs_new.php?host_id=" . $item["id"] . "'"; if ($_REQUEST["host_id"] == $item["id"]) { print " selected"; } print ">" . $item["name"] . "</option>\n"; } } ?> </select> </td> </tr> </table> </form> <form name="chk" method="post" action="graphs_new.php"> <?php $total_rows = sizeof(db_fetch_assoc("select graph_template_id from host_graph where host_id=" . $_REQUEST["host_id"])); /* we give users the option to turn off the javascript features for data queries with lots of rows */ if (read_config_option("max_data_query_javascript_rows") >= $total_rows) { $use_javascript = true; }else{ $use_javascript = false; } html_start_box("<strong>Graph Templates</strong>", "98%", $colors["header"], "3", "center", ""); print " <tr bgcolor='#" . $colors["header_panel"] . "'> <td class='textSubHeaderDark'>Graph Template Name</td> <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 </tr>\n"; $graph_templates = db_fetch_assoc("select graph_templates.id as graph_template_id, graph_templates.name as graph_template_name from (host_graph,graph_templates) where host_graph.graph_template_id=graph_templates.id and host_graph.host_id=" . $_REQUEST["host_id"] . " order by graph_templates.name"); $template_graphs = db_fetch_assoc("select graph_local.graph_template_id from (graph_local,host_graph) where graph_local.graph_template_id=host_graph.graph_template_id and graph_local.host_id=host_graph.host_id and graph_local.host_id=" . $host["id"] . " group by graph_local.graph_template_id"); print "<script type='text/javascript'>\nvar gt_created_graphs = new Array()\n</script>\n"; if ((sizeof($template_graphs) > 0) && ($use_javascript == true)) { print "<script type='text/javascript'>\n<!--\n"; print "var gt_created_graphs = new Array("; $cg_ctr = 0; foreach ($template_graphs as $template_graph) { print (($cg_ctr > 0) ? "," : "") . "'" . $template_graph["graph_template_id"] . "'"; $cg_ctr++; } print ")\n"; print "//-->\n</script>\n"; } /* create a row for each graph template associated with the host template */ $i = 0; if (sizeof($graph_templates) > 0) { foreach ($graph_templates as $graph_template) { $query_row = $graph_template["graph_template_id"]; print "<tr id='gt_line$query_row' bgcolor='#" . (($i % 2 == 0) ? "ffffff" : $colors["light"]) . "'>"; $i++; print " <td" . (($use_javascript == true) ? " onClick='gt_select_line(" . $graph_template["graph_template_id"] . ");'" : "") . "><span id='gt_text$query_row" . "_0'> <span id='gt_text$query_row" . "_0'><strong>Create:</strong> " . $graph_template["graph_template_name"] . "</span> </td> <td align='right'> <input type='checkbox' name='cg_$query_row' id='cg_$query_row'" . (($use_javascript == true) ? " onClick='gt_update_selection_indicators();'" : "") . "> </td> </tr>"; } } if ($use_javascript == true) { print "<script type='text/javascript'>gt_update_deps(1);</script>\n"; } $available_graph_templates = db_fetch_assoc("SELECT graph_templates.id, graph_templates.name FROM snmp_query_graph RIGHT JOIN graph_templates ON (snmp_query_graph.graph_template_id = graph_templates.id) WHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name"); /* create a row at the bottom that lets the user create any graph they choose */ print " <tr bgcolor='#" . (($i % 2 == 0) ? "ffffff" : $colors["light"]) . "'> <td colspan='2' width='60' nowrap> <strong>Create:</strong> "; form_dropdown("cg_g", $available_graph_templates, "name", "id", "", "(Select a graph type to create)", "", "font-size: 10px;"); print " </td> </tr>"; html_end_box(); $snmp_queries = db_fetch_assoc("select snmp_query.id, snmp_query.name, snmp_query.xml_path from (snmp_query,host_snmp_query) where host_snmp_query.snmp_query_id=snmp_query.id and host_snmp_query.host_id=" . $host["id"] . " order by snmp_query.name"); print "<script type='text/javascript'>\nvar created_graphs = new Array()\n</script>\n"; if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $snmp_query) { unset($total_rows); $xml_array = get_data_query_array($snmp_query["id"]); $num_input_fields = 0; $num_visible_fields = 0; if ($xml_array != false) { /* loop through once so we can find out how many input fields there are */ reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { $num_input_fields++; if (!isset($total_rows)) { $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'"); } } } } if (!isset($total_rows)) { $total_rows = 0; } /* we give users the option to turn off the javascript features for data queries with lots of rows */ if (read_config_option("max_data_query_javascript_rows") >= $total_rows) { $use_javascript = true; }else{ $use_javascript = false; } $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"); if ((sizeof($snmp_query_graphs) > 0) && ($use_javascript == true)) { print "<script type='text/javascript'>\n<!--\n"; foreach ($snmp_query_graphs as $snmp_query_graph) { $created_graphs = db_fetch_assoc("select distinct data_local.snmp_index from (data_local,data_template_data) left join data_input_data on (data_template_data.id=data_input_data.data_template_data_id) left join data_input_fields on (data_input_data.data_input_field_id=data_input_fields.id) where data_local.id=data_template_data.local_data_id and data_input_fields.type_code='output_type' and data_input_data.value='" . $snmp_query_graph["id"] . "' and data_local.host_id=" . $host["id"]); print "created_graphs[" . $snmp_query_graph["id"] . "] = new Array("; $cg_ctr = 0; if (sizeof($created_graphs) > 0) { foreach ($created_graphs as $created_graph) { print (($cg_ctr > 0) ? "," : "") . "'" . encode_data_query_index($created_graph["snmp_index"]) . "'"; $cg_ctr++; } } print ")\n"; } print "//-->\n</script>\n"; } print " <table width='98%' style='background-color: #" . $colors["form_alternate2"] . "; border: 1px solid #" . $colors["header"] . ";' align='center' cellpadding='3' cellspacing='0'>\n <tr> <td bgcolor='#" . $colors["header"] . "' colspan='" . ($num_input_fields+1) . "'> <table cellspacing='0' cellpadding='0' width='100%' > <tr> <td class='textHeaderDark'> <strong>Data Query</strong> [" . $snmp_query["name"] . "] </td> <td align='right' nowrap> <a href='graphs_new.php?action=query_reload&id=" . $snmp_query["id"] . "&host_id=" . $host["id"] . "'><img src='images/reload_icon_small.gif' alt='Reload Associated Query' border='0' align='absmiddle'></a> </td> </tr> </table> </td> </tr>"; if ($xml_array != false) { $html_dq_header = ""; $snmp_query_indexes = array(); reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { $raw_data = db_fetch_assoc("select field_value,snmp_index from host_snmp_cache where host_id=" . $host["id"] . " and field_name='$field_name' and snmp_query_id=" . $snmp_query["id"]); /* don't even both to display the column if it has no data */ if (sizeof($raw_data) > 0) { /* draw each header item <TD> */ $html_dq_header .= "<td height='1'><strong><font color='#" . $colors["header_text"] . "'>" . $field_array["name"] . "</font></strong></td>\n"; foreach ($raw_data as $data) { $snmp_query_data[$field_name]{$data["snmp_index"]} = $data["field_value"]; if (!in_array($data["snmp_index"], $snmp_query_indexes,TRUE)) { array_push($snmp_query_indexes, $data["snmp_index"]); } } $num_visible_fields++; }elseif (sizeof($raw_data) == 0) { /* we are choosing to not display this column, so unset the associated field in the xml array so it is not drawn */ unset($xml_array["fields"][$field_name]); } } } /* if the user specified a prefered sort order; sort the list of indexes before displaying them */ if (isset($xml_array["index_order_type"])) { if ($xml_array["index_order_type"] == "numeric") { usort($snmp_query_indexes, "usort_numeric"); }else if ($xml_array["index_order_type"] == "alphabetic") { usort($snmp_query_indexes, "usort_alphabetic"); }else if ($xml_array["index_order_type"] == "natural") { usort($snmp_query_indexes, "usort_natural"); } } if ($num_visible_fields == 0) { print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td>This data query returned 0 rows, perhaps there was a problem executing this data query. You can <a href='host.php?action=query_verbose&id=" . $snmp_query["id"] . "&host_id=" . $host["id"] . "'>run this data query in debug mode</a> to get more information.</td></tr>\n"; }else{ print " <tr bgcolor='#" . $colors["header_panel"] . "'> $html_dq_header <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);" . (($use_javascript == true) ? "dq_update_selection_indicators();" : "") . "'></td>\n </tr>\n"; } $row_counter = 0; if (sizeof($snmp_query_indexes) > 0) { while (list($id, $snmp_index) = each($snmp_query_indexes)) { $query_row = $snmp_query["id"] . "_" . encode_data_query_index($snmp_index); print "<tr id='line$query_row' bgcolor='#" . (($row_counter % 2 == 0) ? "ffffff" : $colors["light"]) . "'>"; $i++; $column_counter = 0; reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { if (isset($snmp_query_data[$field_name][$snmp_index])) { print "<td " . (($use_javascript == true) ? "onClick='dq_select_line(" . $snmp_query["id"] . ",\"" . encode_data_query_index($snmp_index) . "\");'" : "") ."><span id='text$query_row" . "_" . $column_counter . "'>" . $snmp_query_data[$field_name][$snmp_index] . "</span></td>"; }else{ print "<td " . (($use_javascript == true) ? "onClick='dq_select_line(" . $snmp_query["id"] . ",\"" . encode_data_query_index($snmp_index) . "\");'" : "") . "><span id='text$query_row" . "_" . $column_counter . "'></span></td>"; } $column_counter++; } } print "<td align='right'>"; print "<input type='checkbox' name='sg_$query_row' id='sg_$query_row' " . (($use_javascript == true) ? "onClick='dq_update_selection_indicators();'" : "") . ">"; print "</td>"; print "</tr>\n"; $row_counter++; } } }else{ 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"; } print "</table>"; /* draw the graph template drop down here */ $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"); if (sizeof($data_query_graphs) == 1) { form_hidden_box("sgg_" . $snmp_query["id"] . "' id='sgg_" . $snmp_query["id"], $data_query_graphs[0]["id"], ""); }elseif (sizeof($data_query_graphs) > 1) { print " <table align='center' width='98%'> <tr> <td width='1' valign='top'> <img src='images/arrow.gif' alt='' align='absmiddle'> </td> <td align='right'> <span style='font-size: 12px; font-style: italic;'>Select a graph type:</span> <select name='sgg_" . $snmp_query["id"] . "' id='sgg_" . $snmp_query["id"] . "' " . (($use_javascript == true) ? "onChange='dq_update_deps(" . $snmp_query["id"] . "," . $num_visible_fields . ");'" : "") . "> "; html_create_list($data_query_graphs,"name","id","0"); print " </select> </td> </tr> </table>"; } print "<br>"; if ($use_javascript == true) { print "<script type='text/javascript'>dq_update_deps(" . $snmp_query["id"] . "," . ($num_visible_fields) . ");</script>\n"; } } } form_hidden_box("save_component_graph", "1", ""); form_hidden_box("host_id", $host["id"], "0"); form_hidden_box("host_template_id", $host["host_template_id"], "0"); form_save_button((isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "index.php")); print "<script type='text/javascript'>dq_update_selection_indicators();</script>\n"; print "<script type='text/javascript'>gt_update_selection_indicators();</script>\n"; }
function data_query_item_edit() { global $fields_data_query_item_edit; /* ================= input validation ================= */ input_validate_input_number(get_request_var("id")); input_validate_input_number(get_request_var("snmp_query_id")); /* ==================================================== */ if (!empty($_GET["id"])) { $snmp_query_item = db_fetch_row("select * from snmp_query_graph where id=" . $_GET["id"]); } $snmp_query = db_fetch_row("select name,xml_path from snmp_query where id=" . $_GET["snmp_query_id"]); $header_label = "[edit: " . htmlspecialchars($snmp_query["name"]) . "]"; html_start_box("<strong>Associated Graph/Data Templates</strong> {$header_label}", "100%", "", "3", "center", ""); draw_edit_form(array("config" => array(), "fields" => inject_form_variables($fields_data_query_item_edit, isset($snmp_query_item) ? $snmp_query_item : array(), $_GET))); html_end_box(); if (!empty($snmp_query_item["id"])) { html_start_box("<strong>Associated Data Templates</strong>", "100%", "", "3", "center", ""); $data_templates = db_fetch_assoc("select\n\t\t\tdata_template.id,\n\t\t\tdata_template.name\n\t\t\tfrom (data_template, data_template_rrd, graph_templates_item)\n\t\t\twhere graph_templates_item.task_item_id=data_template_rrd.id\n\t\t\tand data_template_rrd.data_template_id=data_template.id\n\t\t\tand data_template_rrd.local_data_id=0\n\t\t\tand graph_templates_item.local_graph_id=0\n\t\t\tand graph_templates_item.graph_template_id=" . $snmp_query_item["graph_template_id"] . "\n\t\t\tgroup by data_template.id\n\t\t\torder by data_template.name"); $i = 0; if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { print "<tr class='tableHeader'>\n\t\t\t\t\t\t<td class='textSubHeaderDark'>Data Template - " . $data_template["name"] . "</td>\n\t\t\t\t\t</tr>"; $data_template_rrds = db_fetch_assoc("select\n\t\t\t\t\tdata_template_rrd.id,\n\t\t\t\t\tdata_template_rrd.data_source_name,\n\t\t\t\t\tsnmp_query_graph_rrd.snmp_field_name,\n\t\t\t\t\tsnmp_query_graph_rrd.snmp_query_graph_id\n\t\t\t\t\tfrom data_template_rrd\n\t\t\t\t\tleft 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"] . ")\n\t\t\t\t\twhere data_template_rrd.data_template_id=" . $data_template["id"] . "\n\t\t\t\t\tand data_template_rrd.local_data_id=0\n\t\t\t\t\torder by data_template_rrd.data_source_name"); $i = 0; if (sizeof($data_template_rrds) > 0) { foreach ($data_template_rrds as $data_template_rrd) { if (empty($data_template_rrd["snmp_query_graph_id"])) { $old_value = ""; } else { $old_value = "on"; } form_alternate_row(); ?> <td> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <tr> <td width="200"> <strong>Data Source:</strong> </td> <td width="200"> <?php print $data_template_rrd["data_source_name"]; ?> </td> <td width="1"> <?php $snmp_queries = get_data_query_array($_GET["snmp_query_id"]); $xml_outputs = array(); while (list($field_name, $field_array) = each($snmp_queries["fields"])) { if ($field_array["direction"] == "output") { $xml_outputs[$field_name] = $field_name . " (" . $field_array["name"] . ")"; } } form_dropdown("dsdt_" . $data_template["id"] . "_" . $data_template_rrd["id"] . "_snmp_field_output", $xml_outputs, "", "", $data_template_rrd["snmp_field_name"], "", ""); ?> </td> <td align="right"> <?php form_checkbox("dsdt_" . $data_template["id"] . "_" . $data_template_rrd["id"] . "_check", $old_value, "", "", "", $_GET["id"]); print "<br>"; ?> </td> </tr> </table> </td> <?php form_end_row(); } } } } html_end_box(); html_start_box("<strong>Suggested Values</strong>", "100%", "", "3", "center", ""); reset($data_templates); /* suggested values for data templates */ if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { $suggested_values = db_fetch_assoc("select\n\t\t\t\ttext,\n\t\t\t\tfield_name,\n\t\t\t\tid\n\t\t\t\tfrom snmp_query_graph_rrd_sv\n\t\t\t\twhere snmp_query_graph_id=" . $_GET["id"] . "\n\t\t\t\tand data_template_id=" . $data_template["id"] . "\n\t\t\t\torder by field_name,sequence"); print "<tr class='tableHeader'>\n\t\t\t\t\t<td class='textSubHeaderDark'>Data Template - " . htmlspecialchars($data_template["name"]) . "</td>\n\t\t\t\t</tr>"; $i = 0; if (sizeof($suggested_values) > 0) { print "<tr><td><table cellspacing='0' cellpadding='0' border='0' width='100%'>\n"; foreach ($suggested_values as $suggested_value) { form_alternate_row(); ?> <td width="120"> <strong><?php print htmlspecialchars($suggested_value["field_name"]); ?> </strong> </td> <td> <?php print htmlspecialchars($suggested_value["text"]); ?> </td> <td width="70"> <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> <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> </td> <td align="right"> <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> </td> </tr> <?php } print "</table></td></tr>\n"; } form_alternate_row(); ?> <td> <table cellspacing="0" cellpadding="3" border="0" width="100%"> <tr> <td width="1"> <input type="text" name="svds_<?php print $data_template["id"]; ?> _text" size="60"> </td> <td width="220" nowrap> Field Name: <input type="text" name="svds_<?php print $data_template["id"]; ?> _field" size="15"> </td> <td> <input type="submit" name="svds_<?php print $data_template["id"]; ?> _x" value="Add" title="Add Data Source Name Suggested Name"> </td> </tr> </table> </td> </tr> <?php } } /* suggested values for graphs templates */ $suggested_values = db_fetch_assoc("select\n\t\t\ttext,\n\t\t\tfield_name,\n\t\t\tid\n\t\t\tfrom snmp_query_graph_sv\n\t\t\twhere snmp_query_graph_id=" . $_GET["id"] . "\n\t\t\torder by field_name,sequence"); print "<tr class='tableHeader'>\n\t\t\t\t<td class='textSubHeaderDark'>Graph Template - " . htmlspecialchars(db_fetch_cell("select name from graph_templates where id=" . $snmp_query_item["graph_template_id"])) . "</td>\n\t\t\t</tr>"; $i = 0; if (sizeof($suggested_values) > 0) { print "<tr><td><table cellspacing='0' cellpadding='0' border='0' width='100%'>\n"; foreach ($suggested_values as $suggested_value) { form_alternate_row(); ?> <td width="120"> <strong><?php print htmlspecialchars($suggested_value["field_name"]); ?> </strong> </td> <td> <?php print htmlspecialchars($suggested_value["text"]); ?> </td> <td width="70"> <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> <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> </td> <td align="right"> <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> </td> </tr> <?php } print "</table></td></tr>\n"; } form_alternate_row(); ?> <td> <table cellspacing="0" cellpadding="3" border="0" width="100%"> <tr> <td width="1"> <input type="text" name="svg_text" size="60"> </td> <td width="220" nowrap> Field Name: <input type="text" name="svg_field" size="15"> </td> <td> <input type="submit" name="svg_x" value="Add" title="Add Graph Title Suggested Name"> </td> </tr> </table> </td> </tr> <?php html_end_box(); } form_save_button("data_queries.php?action=edit&id=" . $_GET["snmp_query_id"], "return"); }
function data_query_item_edit() { global $fields_data_query_item_edit; /* ================= input validation ================= */ input_validate_input_number(get_request_var_request('id')); input_validate_input_number(get_request_var_request('snmp_query_id')); /* ==================================================== */ if (!empty($_REQUEST['id'])) { $snmp_query_item = db_fetch_row_prepared('SELECT * FROM snmp_query_graph WHERE id = ?', array($_REQUEST['id'])); } $snmp_query = db_fetch_row_prepared('SELECT name, xml_path FROM snmp_query WHERE id = ?', array($_REQUEST['snmp_query_id'])); $header_label = '[edit: ' . htmlspecialchars($snmp_query['name']) . ']'; html_start_box("<strong>Associated Graph/Data Templates</strong> {$header_label}", '100%', '', '3', 'center', ''); draw_edit_form(array('config' => array(), 'fields' => inject_form_variables($fields_data_query_item_edit, isset($snmp_query_item) ? $snmp_query_item : array(), $_REQUEST))); html_end_box(); ?> <script type='text/javascript'> $('#graph_template_id').change(function() { $('#name').val($(this).children(':selected').text()); }); </script> <?php if (!empty($snmp_query_item['id'])) { html_start_box('<strong>Associated Data Templates</strong>', '100%', '', '3', 'center', ''); $data_templates = db_fetch_assoc_prepared('SELECT data_template.id, data_template.name FROM (data_template, data_template_rrd, graph_templates_item) WHERE graph_templates_item.task_item_id = data_template_rrd.id AND data_template_rrd.data_template_id = data_template.id AND data_template_rrd.local_data_id = 0 AND graph_templates_item.local_graph_id = 0 AND graph_templates_item.graph_template_id = ? GROUP BY data_template.id ORDER BY data_template.name', array($snmp_query_item['graph_template_id'])); $i = 0; if (sizeof($data_templates)) { foreach ($data_templates as $data_template) { print "<tr class='tableHeader'>\n\t\t\t\t\t<th>Data Template - " . $data_template['name'] . '</th> </tr>'; $data_template_rrds = db_fetch_assoc_prepared('SELECT data_template_rrd.id, data_template_rrd.data_source_name, snmp_query_graph_rrd.snmp_field_name, snmp_query_graph_rrd.snmp_query_graph_id FROM data_template_rrd 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 = ? AND snmp_query_graph_rrd.data_template_id = ?) WHERE data_template_rrd.data_template_id = ? AND data_template_rrd.local_data_id = 0 ORDER BY data_template_rrd.data_source_name', array($_REQUEST['id'], $data_template['id'], $data_template['id'])); $i = 0; if (sizeof($data_template_rrds) > 0) { foreach ($data_template_rrds as $data_template_rrd) { if (empty($data_template_rrd['snmp_query_graph_id'])) { $old_value = ''; } else { $old_value = 'on'; } form_alternate_row(); ?> <td> <table cellspacing="0" cellpadding="2" border="0"> <tr> <td width="200"> <strong>Data Source:</strong> </td> <td width="200"> <?php print $data_template_rrd['data_source_name']; ?> </td> <td> <?php $snmp_queries = get_data_query_array($_REQUEST['snmp_query_id']); $xml_outputs = array(); while (list($field_name, $field_array) = each($snmp_queries['fields'])) { if ($field_array['direction'] == 'output') { $xml_outputs[$field_name] = $field_name . ' (' . $field_array['name'] . ')'; } } form_dropdown('dsdt_' . $data_template['id'] . '_' . $data_template_rrd['id'] . '_snmp_field_output', $xml_outputs, '', '', $data_template_rrd['snmp_field_name'], '', ''); ?> </td> <td align="right"> <?php form_checkbox('dsdt_' . $data_template['id'] . '_' . $data_template_rrd['id'] . '_check', $old_value, '', '', '', $_REQUEST['id']); print '<br>'; ?> </td> </tr> </table> </td> <?php form_end_row(); } } } } html_end_box(); html_start_box('<strong>Suggested Values - Graph Names</strong>', '100%', '', '3', 'center', ''); /* suggested values for graphs templates */ $suggested_values = db_fetch_assoc_prepared('SELECT text, field_name, id FROM snmp_query_graph_sv WHERE snmp_query_graph_id = ? ORDER BY field_name, sequence', array($_REQUEST['id'])); html_header(array('Name', '', 'Equation'), 2); if (sizeof($suggested_values)) { foreach ($suggested_values as $suggested_value) { form_alternate_row(); ?> <td width='120'> <strong><?php print htmlspecialchars($suggested_value['field_name']); ?> </strong> </td> <td width='40' align='center'> <span class='remover' style='pointer:cursor;' href='<?php print htmlspecialchars('data_queries.php?action=item_movedown_gsv&snmp_query_graph_id=' . $_REQUEST['id'] . '&id=' . $suggested_value['id'] . '&snmp_query_id=' . $_REQUEST['snmp_query_id'] . '&field_name=' . $suggested_value['field_name']); ?> '><img src='images/move_down.gif' border='0' alt='Move Down'></span> <span class='remover' style='pointer:cursor;' href='<?php print htmlspecialchars('data_queries.php?action=item_moveup_gsv&snmp_query_graph_id=' . $_REQUEST['id'] . '&id=' . $suggested_value['id'] . '&snmp_query_id=' . $_REQUEST['snmp_query_id'] . '&field_name=' . $suggested_value['field_name']); ?> '><img src='images/move_up.gif' border='0' alt='Move Up'></span> </td> <td> <?php print htmlspecialchars($suggested_value['text']); ?> </td> <td align='right'> <span class='remover' style='pointer:cursor;' href='<?php print htmlspecialchars('data_queries.php?action=item_remove_gsv&snmp_query_graph_id=' . $_REQUEST['id'] . '&id=' . $suggested_value['id'] . '&snmp_query_id=' . $_REQUEST['snmp_query_id']); ?> '><img src='images/delete_icon.gif' style='height:10px;width:10px;' border='0' alt='Delete'></span> </td> </tr> <?php } } form_alternate_row(); ?> <td colspan='4'> <table cellspacing='0' cellpadding='3' border='0'> <tr> <td style='white-space:nowrap;'> Field Name: </td> <td> <input type='text' id='svg_field' size='15'> </td> <td style='white-space:nowrap;'> Suggested Value: </td> <td width='1'> <input type='text' id='svg_text' size='60'> </td> <td> <input id='svg_x' type='button' name='svg_x' value='Add' title='Add Graph Title Suggested Name'> </td> </tr> </table> </td> <?php form_end_row(); html_end_box(); html_start_box('<strong>Suggested Values - Data Source Names</strong>', '100%', '', '3', 'center', ''); reset($data_templates); /* suggested values for data templates */ if (sizeof($data_templates)) { foreach ($data_templates as $data_template) { $suggested_values = db_fetch_assoc_prepared('SELECT text, field_name, id FROM snmp_query_graph_rrd_sv WHERE snmp_query_graph_id = ? AND data_template_id = ? ORDER BY field_name, sequence', array($_REQUEST['id'], $data_template['id'])); html_header(array('Name', '', 'Equation'), 2); if (sizeof($suggested_values)) { $prev_name = ''; foreach ($suggested_values as $suggested_value) { form_alternate_row(); ?> <td width='120'> <strong><?php print htmlspecialchars($suggested_value['field_name']); ?> </strong> </td> <td width='40' align='center'> <span class='remover' style='pointer:cursor;' href='<?php print htmlspecialchars('data_queries.php?action=item_movedown_dssv&snmp_query_graph_id=' . $_REQUEST['id'] . '&id=' . $suggested_value['id'] . '&snmp_query_id=' . $_REQUEST['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'></span> <span class='remover' style='pointer:cursor;' href='<?php print htmlspecialchars('data_queries.php?action=item_moveup_dssv&snmp_query_graph_id=' . $_REQUEST['id'] . '&id=' . $suggested_value['id'] . '&snmp_query_id=' . $_REQUEST['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'></span> </td> <td style='white-space:nowrap'> <?php print htmlspecialchars($suggested_value['text']); ?> </td> <td align='right'> <span class='remover' style='pointer:cursor;' href='<?php print htmlspecialchars('data_queries.php?action=item_remove_dssv&snmp_query_graph_id=' . $_REQUEST['id'] . '&id=' . $suggested_value['id'] . '&snmp_query_id=' . $_REQUEST['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'></span> </td> </tr> <?php $prev_name = $suggested_value['field_name']; } } form_alternate_row(); ?> <td colspan='4'> <table cellspacing='0' cellpadding='3' border='0'> <tr> <td style='white-space:nowrap;'> Field Name: </td> <td> <input id='svds_field' type='text' name='svds_<?php print $data_template['id']; ?> _field' size='15'> </td> <td style='white-space:nowrap;'> Suggested Value: </td> <td width='1'> <input id='svds_text' type='text' name='svds_<?php print $data_template['id']; ?> _text' size='60'> </td> <td> <input id='svds_x' type='button' name='svds_<?php print $data_template['id']; ?> _x' value='Add' title='Add Data Source Name Suggested Name'> </td> </tr> </table> <script type='text/javascript'> $('.remover').click(function() { href=$(this).attr('href'); $.get(href, function(data) { $('form[action="data_queries.php"]').unbind(); $('#main').html(data); applySkin(); }); }); $('input[id="svg_x"]').click(function() { $.post('data_queries.php', { '_graph_template_id':$('#_graph_template_id').val(), 'action':'save', 'name':$('#name').val(), 'graph_template_id':$('#graph_template_id').val(), 'id':$('#id').val(), 'header':'false', 'save_component_snmp_query_item':'1', 'snmp_query_id':$('#snmp_query_id').val(), 'svg_field':$('#svg_field').val(), 'svg_text':$('#svg_text').val(), 'svg_x':'Add' }).done(function(data) { $('#main').html(data); applySkin(); }); }); $('input[id="svds_x"]').click(function() { var svds_text_name=$('#svds_text').attr('name'); var svds_field_name=$('#svds_field').attr('name'); var svds_x_name=$('#svds_x').attr('name'); var jSON = $.parseJSON('{ ' + '"_graph_template_id":"'+$('#_graph_template_id').val() + '", ' + '"action":"save", ' + '"name":"'+$('#name').val() + '", ' + '"graph_template_id":"'+$('#graph_template_id').val() + '", ' + '"id":"'+$('#id').val() + '", ' + '"header":"false", ' + '"save_component_snmp_query_item":"1", ' + '"snmp_query_id":"'+$('#snmp_query_id').val() + '", ' + '"'+svds_field_name+'":"'+$('#svds_field').val() + '", ' + '"'+svds_text_name+'":"'+$('#svds_text').val() + '", ' + '"'+svds_x_name+'":"Add" }'); $.post('data_queries.php', jSON).done(function(data) { $('#main').html(data); applySkin(); }); }); </script> </td> <?php form_end_row(); } } html_end_box(); } form_save_button('data_queries.php?action=edit&id=' . $_REQUEST['snmp_query_id'], 'return'); }
function update_data_query_sort_cache($host_id, $data_query_id) { $raw_xml = get_data_query_array($data_query_id); /* get a list of valid data query types */ $valid_index_types = get_ordered_index_type_list($host_id, $data_query_id); /* something is probably wrong with the data query */ if (sizeof($valid_index_types) == 0) { $sort_field = ""; } else { /* grab the first field off the list */ $sort_field = $valid_index_types[0]; } /* substitute variables */ if (isset($raw_xml["index_title_format"])) { $title_format = str_replace("|chosen_order_field|", "|query_{$sort_field}|", $raw_xml["index_title_format"]); } else { $title_format = "|query_{$sort_field}|"; } /* update the cache */ /* TODO: if both $sort field AND $title_format are empty, this yields funny results */ db_execute("UPDATE host_snmp_query SET sort_field = '{$sort_field}', title_format = '{$title_format}' WHERE host_id = '{$host_id}' AND snmp_query_id = '{$data_query_id}'"); }
function graphs_new() { global $colors; /* ================= input validation ================= */ input_validate_input_number(get_request_var_request("device_id")); input_validate_input_number(get_request_var_request("graph_type")); /* ==================================================== */ /* clean up search string */ if (isset($_REQUEST["filter"])) { $_REQUEST["filter"] = sanitize_search_string(get_request_var("filter")); } /* if the user pushed the 'clear' button */ if (isset($_REQUEST["clear_x"])) { if (!substr_count($_SERVER["REQUEST_URI"], "/devices.php")) { kill_session_var("sess_graphs_new_device_id"); } kill_session_var("sess_graphs_new_graph_type"); kill_session_var("sess_graphs_new_filter"); if (!substr_count($_SERVER["REQUEST_URI"], "/devices.php")) { unset($_REQUEST["device_id"]); } unset($_REQUEST["graph_type"]); unset($_REQUEST["filter"]); $changed = true; }else{ /* if any of the settings changed, reset the page number */ $changed = 0; $changed += check_changed("device_id", "sess_graphs_new_device_id"); $changed += check_changed("graph_type", "sess_graphs_new_graph_type"); $changed += check_changed("filter", "sess_graphs_new_filter"); } load_current_session_value("device_id", "sess_graphs_new_device_id", db_fetch_cell("select id from device order by description,hostname limit 1")); load_current_session_value("graph_type", "sess_graphs_new_graph_type", read_config_option("default_graphs_new_dropdown")); load_current_session_value("filter", "sess_graphs_new_filter", ""); if (substr_count($_SERVER["REQUEST_URI"], "/devices.php")) { $file = "devices.php?action=edit&tab=newgraphs&id=" . $_REQUEST["device_id"]; $file2 = "devices.php"; }else{ $file = "graphs_new.php?device_id=". $_REQUEST["device_id"]; $file2 = "graphs_new.php"; } $device = db_fetch_row("select id,description,hostname,device_template_id from device where id=" . $_REQUEST["device_id"]); $row_limit = read_config_option("num_rows_data_query"); $debug_log = debug_log_return("new_graphs"); ?> <script type="text/javascript"> <!-- function applyGraphsNewFilterChange(objForm) { strURL = '?action=edit&tab=newgraphs'; strURL = strURL + '&graph_type=' + objForm.graph_type.value; strURL = strURL + '&device_id=' + objForm.device_id.value; strURL = strURL + '&filter=' + objForm.filter.value;; document.location = strURL; } --> </script> <?php html_start_box("<strong>" . $device["description"] . "(" . $device["hostname"] . ")</strong> " . db_fetch_cell("select name from device_template where id=" . $device["device_template_id"]), "100", $colors["header"], "3", "center", ""); ?> <tr class='rowAlternate2'> <td> <form name="form_graphs_new" method="post" action="<?php print $file2;?>"> <table cellpadding="0" align="left"> <tr> <?php if (!isset($_REQUEST["tab"])) { ?> <td class="nw50 textGraphFilter"> Host: </td> <td width="1"> <select name="device_id" onChange="applyGraphsNewFilterChange(document.form_graphs_new)"> <?php $devices = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from device order by description,hostname"); if (sizeof($devices) > 0) { foreach ($devices as $item) { print "<option value='" . $item["id"] . "'"; if (get_request_var_request("device_id") == $item["id"]) { print " selected"; } print ">" . $item["name"] . "</option>\n"; } } ?> </select> </td> <?php } ?> <td class="nw50 textGraphFilter"> <?php print __("Type:");?> </td> <td width="1"> <select name="graph_type" onChange="applyGraphsNewFilterChange(document.form_graphs_new)"> <option value="-2"<?php if (get_request_var_request("graph_type") == "-2") {?> selected<?php }?>><?php print __("All");?></option> <option value="-1"<?php if (get_request_var_request("graph_type") == "-1") {?> selected<?php }?>><?php print __("Graph Template Based");?></option> <?php $snmp_queries = db_fetch_assoc("SELECT snmp_query.id, snmp_query.name, snmp_query.xml_path FROM (snmp_query,device_snmp_query) WHERE device_snmp_query.snmp_query_id=snmp_query.id AND device_snmp_query.device_id=" . $device["id"] . " ORDER BY snmp_query.name"); if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $query) { print "<option value='" . $query["id"] . "'"; if (get_request_var_request("graph_type") == $query["id"]) { print " selected"; } print ">" . $query["name"] . "</option>\n"; } } ?> </select> </td> <td style="white-space:nowrap;width:1%;" class="textInfo" align="center" valign="top"> <?php if (!isset($_REQUEST["tab"])) { ?><span class="tabedit">*</span><a href="devices.php?action=edit&id=<?php print $_REQUEST["device_id"];?>"><?php print __("Edit this Host");?></a><br><?php } ?> <?php api_plugin_hook('graphs_new_top_links'); ?> </td> </tr> </table> <?php if (get_request_var_request("graph_type") > 0) {?> <table cellpadding="0" align="left"> <tr> <td class="nw50 textGraphFilter"> Search: </td> <td class="nw200"> <input type="text" name="filter" size="30" width="200" value="<?php print $_REQUEST["filter"];?>"> </td> <td align="left" class="nw120"> <input type="submit" name="go" value="<?php print __("Go");?>" align="middle"> <input type="submit" name="clear_x" value="<?php print __("Clear");?>" align="middle"> <input type="hidden" name="action" value="edit"> <input type="hidden" name="tab" value="newgraphs"> </td> </tr> </table> <?php }else{ form_hidden_box("filter", get_request_var_request("filter"), ""); }?> </form> </td> </tr> <?php html_end_box(false); $total_rows = sizeof(db_fetch_assoc("select graph_template_id from device_graph where device_id=" . $_REQUEST["device_id"])); $i = 0; if (get_request_var_request("graph_type") > 0) { load_current_session_value("page" . get_request_var_request("graph_type"), "sess_graphs_new_page" . get_request_var_request("graph_type"), "1"); }else if (get_request_var_request("graph_type") == -2) { foreach($snmp_queries as $query) { load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1"); } } print "<form name='chk' method='post' action='" . $file2 . "'>"; if (get_request_var_request("graph_type") < 0) { $graph_templates = db_fetch_assoc("SELECT graph_templates.id AS graph_template_id, graph_templates.name AS graph_template_name FROM (device_graph,graph_templates) WHERE device_graph.graph_template_id=graph_templates.id AND device_graph.device_id=" . $_REQUEST["device_id"] . " ORDER BY graph_templates.name"); $template_graphs = db_fetch_assoc("SELECT graph_local.graph_template_id FROM (graph_local,device_graph) WHERE graph_local.graph_template_id=device_graph.graph_template_id AND graph_local.device_id=device_graph.device_id AND graph_local.device_id=" . $device["id"] . " GROUP BY graph_local.graph_template_id"); if (sizeof($template_graphs) > 0) { print "<script type='text/javascript'>\n<!--\n"; print "var gt_created_graphs = new Array("; $cg_ctr = 0; foreach ($template_graphs as $template_graph) { print (($cg_ctr > 0) ? "," : "") . "'" . $template_graph["graph_template_id"] . "'"; $cg_ctr++; } print ")\n"; print "//-->\n</script>\n"; } else { print "<script type='text/javascript'>\nvar gt_created_graphs = new Array()\n</script>\n"; } print "<script type='text/javascript'>gt_update_deps(1);</script>\n"; print "<script type='text/javascript'>\nvar created_graphs = new Array()\n</script>\n"; html_start_box("<strong>" . __("Graph Templates") . "</strong>", "100", $colors["header"], "3", "center", ""); print " <tr class='rowSubHeader'> <td class='textSubHeaderDark'>" . __("Graph Template Name") . "</td> <td class='rowSubHeader' width='1%' align='center' 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 </tr>\n"; /* create a row for each graph template associated with the device template */ if (sizeof($graph_templates) > 0) { foreach ($graph_templates as $graph_template) { $query_row = $graph_template["graph_template_id"]; print "<tr id='gt_line$query_row' bgcolor='#" . (($i % 2 == 0) ? "ffffff" : $colors["light"]) . "'>"; $i++; print " <td onClick='gt_select_line(" . $graph_template["graph_template_id"] . ");'><span id='gt_text$query_row" . "_0'> <strong>" . __("Create:") . "</strong> " . $graph_template["graph_template_name"] . "</span> </td> <td align='right'> <input type='checkbox' name='cg_$query_row' id='cg_$query_row' onClick='gt_update_selection_indicators();'> </td> </tr>"; } } $available_graph_templates = db_fetch_assoc("SELECT graph_templates.id, graph_templates.name FROM snmp_query_graph RIGHT JOIN graph_templates ON (snmp_query_graph.graph_template_id = graph_templates.id) WHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name"); /* create a row at the bottom that lets the user create any graph they choose */ print " <tr bgcolor='#" . (($i % 2 == 0) ? "ffffff" : $colors["light"]) . "'> <td colspan='2' width='60' nowrap> <strong>" . __("Create:") . "</strong> "; form_dropdown("cg_g", $available_graph_templates, "name", "id", "", "(Select a graph type to create)", "", "textArea"); print " </td> </tr>"; html_end_box(); } if ($_REQUEST["graph_type"] != -1) { $snmp_queries = db_fetch_assoc("SELECT snmp_query.id, snmp_query.name, snmp_query.xml_path FROM (snmp_query,device_snmp_query) WHERE device_snmp_query.snmp_query_id=snmp_query.id AND device_snmp_query.device_id=" . $device["id"] . ($_REQUEST["graph_type"] != -2 ? " AND snmp_query.id=" . $_REQUEST["graph_type"] : '') . " ORDER BY snmp_query.name"); if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $snmp_query) { unset($total_rows); if (isset($_REQUEST["page" . $snmp_query["id"]])) { $page = $_REQUEST["page" . $snmp_query["id"]]; }elseif (!$changed) { $page = $_REQUEST["page" . $snmp_query["id"]]; }else{ $page = 1; } $xml_array = get_data_query_array($snmp_query["id"]); $num_input_fields = 0; $num_visible_fields = 0; if ($xml_array != false) { /* loop through once so we can find out how many input fields there are */ reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { $num_input_fields++; if (!isset($total_rows)) { $total_rows = db_fetch_cell("SELECT count(*) FROM device_snmp_cache WHERE device_id=" . $device["id"] . " and snmp_query_id=" . $snmp_query["id"] . " AND field_name='$field_name'"); } } } } if (!isset($total_rows)) { $total_rows = 0; } $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"); if (sizeof($snmp_query_graphs) > 0) { print "<script type='text/javascript'>\n<!--\n"; foreach ($snmp_query_graphs as $snmp_query_graph) { $created_graphs = db_fetch_assoc("SELECT DISTINCT data_local.snmp_index FROM (data_local,data_template_data) LEFT JOIN data_input_data ON (data_template_data.id=data_input_data.data_template_data_id) LEFT JOIN data_input_fields ON (data_input_data.data_input_field_id=data_input_fields.id) WHERE data_local.id=data_template_data.local_data_id AND data_input_fields.type_code='output_type' AND data_input_data.value='" . $snmp_query_graph["id"] . "' AND data_local.device_id=" . $device["id"]); print "created_graphs[" . $snmp_query_graph["id"] . "] = new Array("; $cg_ctr = 0; if (sizeof($created_graphs) > 0) { foreach ($created_graphs as $created_graph) { print (($cg_ctr > 0) ? "," : "") . "'" . encode_data_query_index($created_graph["snmp_index"]) . "'"; $cg_ctr++; } } print ")\n"; } print "//-->\n</script>\n"; } html_start_box_dq($snmp_query["name"], $snmp_query["id"], $device["id"], $num_input_fields+1, "100", $colors["header"], "0", "center"); if ($xml_array != false) { $html_dq_header = ""; $snmp_query_indexes = array(); reset($xml_array["fields"]); /* if there is a where clause, get the matching snmp_indexes */ $sql_where = ""; if (strlen(get_request_var_request("filter"))) { $sql_where = ""; $indexes = db_fetch_assoc("SELECT DISTINCT snmp_index FROM device_snmp_cache WHERE field_value LIKE '%%" . get_request_var_request("filter") . "%%' AND snmp_query_id=" . $snmp_query["id"] . " AND device_id=" . $device["id"]); if (sizeof($indexes)) { foreach($indexes as $index) { if (strlen($sql_where)) { $sql_where .= ", '" . $index["snmp_index"] . "'"; }else{ $sql_where .= " AND snmp_index IN('" . $index["snmp_index"] . "'"; } } $sql_where .= ")"; } } if ((strlen(get_request_var_request("filter")) == 0) || ((strlen(get_request_var_request("filter"))) && (sizeof($indexes)))) { /* determine the sort order */ if (isset($xml_array["index_order_type"])) { if ($xml_array["index_order_type"] == "numeric") { $sql_order = "ORDER BY CAST(snmp_index AS unsigned)"; }else if ($xml_array["index_order_type"] == "alphabetic") { $sql_order = "ORDER BY snmp_index"; }else if ($xml_array["index_order_type"] == "natural") { $sql_order = "ORDER BY INET_ATON(snmp_index)"; }else{ $sql_order = ""; } }else{ $sql_order = ""; } /* get the unique field values from the database */ $field_names = db_fetch_assoc("SELECT DISTINCT field_name FROM device_snmp_cache WHERE device_id=" . $device["id"] . " AND snmp_query_id=" . $snmp_query["id"]); /* build magic query */ $sql_query = "SELECT device_id, snmp_query_id, snmp_index"; $num_visible_fields = sizeof($field_names); $i = 0; if (sizeof($field_names) > 0) { foreach($field_names as $column) { $field_name = $column["field_name"]; $sql_query .= ", MAX(CASE WHEN field_name='$field_name' THEN field_value ELSE NULL END) AS '$field_name'"; $i++; } } $sql_query .= " FROM device_snmp_cache WHERE device_id=" . $device["id"] . " AND snmp_query_id=" . $snmp_query["id"] . " $sql_where GROUP BY device_id, snmp_query_id, snmp_index $sql_order LIMIT " . ($row_limit*($page-1)) . "," . $row_limit; $rows_query = "SELECT device_id, snmp_query_id, snmp_index FROM device_snmp_cache WHERE device_id=" . $device["id"] . " AND snmp_query_id=" . $snmp_query["id"] . " $sql_where GROUP BY device_id, snmp_query_id, snmp_index"; $snmp_query_indexes = db_fetch_assoc($sql_query); $total_rows = sizeof(db_fetch_assoc($rows_query)); if (($page-1) * $row_limit > $total_rows) { $page = 1; $_REQUEST["page" . $query["id"]] = $page; load_current_session_value("page" . $query["id"], "sess_graphs_new_page" . $query["id"], "1"); } if (substr_count($_SERVER["REQUEST_URI"], "/devices.php")) { $file = "devices.php?action=edit&tab=newgraphs&id=" . $_REQUEST["device_id"]; $file2 = "devices.php"; }else{ $file = "graphs_new.php?device_id=". $_REQUEST["device_id"]; $file2 = "graphs_new.php"; } if ($total_rows > $row_limit) { /* generate page list navigation */ $nav = html_create_nav($page, MAX_DISPLAY_PAGES, $row_limit, $total_rows, 40, $file, "page" . $snmp_query["id"]); print $nav; } while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { foreach($field_names as $row) { if ($row["field_name"] == $field_name) { # $html_dq_header .= "<td height='1' style='padding:0px 5px 0px 5px;' onMousemove='doColResize(this,event)' onMouseover='doColResize(this,event)' onMouseup='doneColResize()'><strong><font color='#" . $colors["header_text"] . "'>" . $field_array["name"] . "</font></strong></td>\n"; $html_dq_header .= "<th style='padding:0px 5px 0px 5px;' onMousemove='doColResize(this,event)' onMouseover='doColResize(this,event)' onMouseup='doneColResize()' class='textSubHeaderDark'><strong><font color='#" . $colors["header_text"] . "'>" . $field_array["name"] . "</font></strong></th>\n"; break; } } } } if (!sizeof($snmp_query_indexes)) { print "<tr class='rowAlternate1'><td>" . __("This data query returned 0 rows, perhaps there was a problem executing this data query. You can %s run this data query in debug mode %s to get more information.", "<a href='" . htmlspecialchars("devices.php?action=query_verbose&id=" . $snmp_query["id"] . "&device_id=" . $device["id"]) . "'>", "</a>") . "</td></tr>\n"; }else{ print "<tr class='rowSubHeader'> $html_dq_header <td class='rowSubHeader' width='1%' align='center' 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 </tr>\n"; } $row_counter = 0; $column_counter = 0; $fields = array_rekey($field_names, "field_name", "field_name"); if (sizeof($snmp_query_indexes) > 0) { foreach($snmp_query_indexes as $row) { $query_row = $snmp_query["id"] . "_" . encode_data_query_index($row["snmp_index"]); form_alternate_row_color("line" . $query_row, true); $column_counter = 0; reset($xml_array["fields"]); while (list($field_name, $field_array) = each($xml_array["fields"])) { if ($field_array["direction"] == "input") { if (in_array($field_name, $fields)) { if (isset($row[$field_name])) { print "<td style='line-height: 1.5em;padding:0px 5px 0px 5px;' onClick='dq_select_line(" . $snmp_query["id"] . ",\"" . encode_data_query_index($row["snmp_index"]) . "\");'><span id='text$query_row" . "_" . $column_counter . "'>" . (strlen($_REQUEST["filter"]) ? preg_replace("/(" . preg_quote($_REQUEST["filter"]) . ")/i", "<span class=\"filter\">\\1</span>", $row[$field_name]) : $row[$field_name]) . "</span></td>"; }else{ print "<td style='line-height: 1.5em;padding:0px 5px 0px 5px;' onClick='dq_select_line(" . $snmp_query["id"] . ",\"" . encode_data_query_index($row["snmp_index"]) . "\");'><span id='text$query_row" . "_" . $column_counter . "'></span></td>"; } $column_counter++; } } } print "<td style='padding-right: 4px;' align='right'>"; print "<input type='checkbox' name='sg_$query_row' id='sg_$query_row' onClick='dq_update_selection_indicators();'>"; print "</td>"; print "</tr>\n"; $row_counter++; } } if ($total_rows > $row_limit) { print $nav; } }else{ print "<tr class='rowAlternate1'><td colspan='2' style='color: red; font-size: 12px; font-weight: bold;'>" . __("Search Returned no Rows.") . "</td></tr>\n"; } }else{ print "<tr class='rowAlternate1'><td colspan='2' style='color: red; font-size: 12px; font-weight: bold;'>" . __("Error in data query.") . "</td></tr>\n"; } /* draw the graph template drop down here */ $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"); if (sizeof($data_query_graphs) == 1) { html_end_box(); form_hidden_box("sgg_" . $snmp_query["id"], $data_query_graphs[0]["id"], ""); }elseif (sizeof($data_query_graphs) > 1) { html_end_box(FALSE); print "<table align='center' width='100%'> <tr> <td width='1' valign='top'> <img src='images/arrow.gif' alt='' align='middle'> </td> <td align='right'> <span class=\"italic\">" . __("Select a graph type:") . "</span> <select name='sgg_" . $snmp_query["id"] . "' id='sgg_" . $snmp_query["id"] . "' onChange='dq_update_deps(" . $snmp_query["id"] . "," . $column_counter . ");'> "; html_create_list($data_query_graphs,"name","id","0"); print " </select> </td> </tr> </table>"; } print "<script type='text/javascript'>dq_update_deps(" . $snmp_query["id"] . "," . ($num_visible_fields) . ");</script>\n"; } } } form_hidden_box("save_component_graph", "1", ""); form_hidden_box("device_id", $device["id"], "0"); form_hidden_box("device_template_id", $device["device_template_id"], "0"); form_save_button_alt("url!" . (isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "")); if (!empty($debug_log)) { debug_log_clear("new_graphs"); ?> <table class='topBoxAlt'> <tr> <td class='mono'> <?php print $debug_log;?> </td> </tr> </table> <br> <?php } print "<script type='text/javascript'>dq_update_selection_indicators();</script>\n"; print "<script type='text/javascript'>gt_update_selection_indicators();</script>\n"; }
function data_query_item_edit() { global $colors; require_once(CACTI_BASE_PATH . "/lib/data_query/data_query_info.php"); /* ================= input validation ================= */ input_validate_input_number(get_request_var("id")); input_validate_input_number(get_request_var("snmp_query_id")); /* ==================================================== */ if (!empty($_GET["id"])) { $snmp_query_item = db_fetch_row("select * from snmp_query_graph where id=" . $_GET["id"]); } if (!empty($_GET["snmp_query_id"])) { $snmp_query = db_fetch_row("select name,xml_path from snmp_query where id=" . get_request_var("snmp_query_id")); $header_label = __("[edit: ") . $snmp_query["name"] . "]"; }else{ $header_label = __("[new]"); } print "<form method='post' action='" . basename($_SERVER["PHP_SELF"]) . "' name='data_query_item_edit'>\n"; html_start_box("<strong>" . __("Associated Graph/Data Source Templates") . "</strong> $header_label", "100", $colors["header"], 0, "center", ""); $header_items = array(__("Field"), __("Value")); print "<tr><td>"; html_header($header_items, 1, false, 'assoc_templates', 'left wp100'); draw_edit_form(array( "config" => array(), "fields" => inject_form_variables(data_query_item_form_list(), (isset($snmp_query_item) ? $snmp_query_item : array()), $_GET) )); print "</table></td></tr>"; /* end of html_header */ html_end_box(true); if (!empty($snmp_query_item["id"])) { html_start_box("<strong>" . __("Associated Data Source Templates") . "</strong>", "100", $colors["header"], "0", "center", "", false, "assoc_data_templates"); $header_items = array(__("Data Source Name"), __("Associated XML Field"), __("Use this Field")); print "<tr><td>"; html_header($header_items, 1, true, 'data_templates', 'left wp100'); $data_templates = db_fetch_assoc("select data_template.id, data_template.name from (data_template, data_template_rrd, graph_templates_item) where graph_templates_item.task_item_id=data_template_rrd.id and data_template_rrd.data_template_id=data_template.id and data_template_rrd.local_data_id=0 and graph_templates_item.local_graph_id=0 and graph_templates_item.graph_template_id=" . $snmp_query_item["graph_template_id"] . " group by data_template.id order by data_template.name"); $i = 0; if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { print " <tr class='rowHeader'> <td><span>Data Source Template - " . $data_template["name"] . "</span></td> <td></td> <td></td> </tr>"; $data_template_rrds = db_fetch_assoc("select data_template_rrd.id, data_template_rrd.data_source_name, snmp_query_graph_rrd.snmp_field_name, snmp_query_graph_rrd.snmp_query_graph_id from data_template_rrd 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"] . ") where data_template_rrd.data_template_id=" . $data_template["id"] . " and data_template_rrd.local_data_id=0 order by data_template_rrd.data_source_name"); if (sizeof($data_template_rrds) > 0) { foreach ($data_template_rrds as $data_template_rrd) { if (empty($data_template_rrd["snmp_query_graph_id"])) { $old_value = ""; }else{ $old_value = CHECKED; } form_alternate_row_color("data_template_rrd" . $data_template_rrd["id"]); print "<td>\n"; print $data_template_rrd["data_source_name"]; print "</td>\n<td>"; $snmp_queries = get_data_query_array($_GET["snmp_query_id"]); $xml_outputs = array(); while (list($field_name, $field_array) = each($snmp_queries["fields"])) { if ($field_array["direction"] == "output") { $xml_outputs[$field_name] = $field_name . " (" . $field_array["name"] . ")";; } } form_dropdown("dsdt_" . $data_template["id"] . "_" . $data_template_rrd["id"] . "_snmp_field_output",$xml_outputs,"","",$data_template_rrd["snmp_field_name"],"",""); print "</td>\n<td align='right'>"; form_checkbox("dsdt_" . $data_template["id"] . "_" . $data_template_rrd["id"] . "_check", $old_value, "", "", "", get_request_var("id")); print "<br>"; print "</td>\n"; form_end_row(); } } } } print "</table></td></tr>"; /* end of html_header */ html_end_box(); html_start_box("<strong>" . __("Suggested Values: Data Source Templates") . "</strong>", "100", $colors["header"], 0, "center", ""); reset($data_templates); /* suggested values for data templates */ if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { $header_items = array(__("Data Source Template") . " - " . $data_template["name"], " "); print "<tr><td>"; html_header($header_items, 2, true, 'data_template_suggested_values_' . $data_template["id"], 'left wp100'); $suggested_values = db_fetch_assoc("select text, field_name, id from snmp_query_graph_rrd_sv where snmp_query_graph_id=" . $_GET["id"] . " and data_template_id=" . $data_template["id"] . " order by field_name,sequence"); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { form_alternate_row_color($suggested_value["id"], true); ?> <td> <strong><?php print $suggested_value["field_name"];?></strong> </td> <td> <?php print $suggested_value["text"];?> </td> <td align="right"> <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 class="buttonSmall" src="images/delete_icon.gif" alt="<?php print __("Delete");?>" align='right'> </a> </td> <?php form_end_row(); } } form_alternate_row_color("nodrag" . $data_template["id"], false, "nodrag nodrop"); ?> <td> <input type="text" name="svds_<?php print $data_template["id"];?>_field" size="15"> </td> <td> <input type="text" name="svds_<?php print $data_template["id"];?>_text" size="30"> </td> <td align="right"> <input type="submit" value="<?php print __("Add");?>" name="svds_<?php print $data_template["id"];?>_x"> </td> <?php form_end_row(); print "</table></td></tr>"; /* end of html_header */ } } html_end_box(false); /* we need a new javascript for each table */ if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { print(" <script type='text/javascript'> $('#data_template_suggested_values_" . $data_template["id"] . "').tableDnD({ onDrop: function(table, row) { $('#AjaxResult').load(\"lib/ajax/jquery.tablednd/data_query_dt_sv.ajax.php?dt_id=" . $data_template["id"] . ">_id=" . $_GET["id"] . "&\"+$.tableDnD.serialize()); } }); </script>\n"); } } /* suggested values for graphs templates */ $suggested_values = db_fetch_assoc("select text, field_name, id from snmp_query_graph_sv where snmp_query_graph_id=" . $_GET["id"] . " order by field_name,sequence"); html_start_box("<strong>" . __("Suggested Values: Graph Templates") . "</strong>", "100", $colors["header"], 0, "center", ""); $header_items = array(__("Graph Template") . " - " . db_fetch_cell("select name from graph_templates where id=" . $snmp_query_item["graph_template_id"]), " "); print "<tr><td>"; html_header($header_items, 2, true, 'graph_template_suggested_values_' . get_request_var("id"), 'left wp100'); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { form_alternate_row_color($suggested_value["id"], true); ?> <td> <strong><?php print $suggested_value["field_name"];?></strong> </td> <td> <?php print $suggested_value["text"];?> </td> <td align="right"> <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 class="buttonSmall" src="images/delete_icon.gif" alt="<?php print __("Delete");?>" align='right'> </a> </td> <?php form_end_row(); } } form_alternate_row_color("nodrag", false, "nodrag nodrop"); ?> <td> <input type="text" name="svg_field" size="15"> </td> <td> <input type="text" name="svg_text" size="30"> </td> <td align="right"> <input type="submit" value="<?php print __("Add");?>" name="svg_x"> </td> <?php form_end_row(); print "</table></td></tr>"; html_end_box(); } ?> <script type="text/javascript"> $('#graph_template_suggested_values_<?php print get_request_var("id");?>').tableDnD({ onDrop: function(table, row) { alert("lib/ajax/jquery.tablednd/data_query_gt_sv.ajax.php?gt_id=<?php print $_GET["id"];?>&"+$.tableDnD.serialize()); $('#AjaxResult').load("lib/ajax/jquery.tablednd/data_query_gt_sv.ajax.php?gt_id=<?php print $_GET["id"];?>&"+$.tableDnD.serialize()); } }); </script> <?php form_save_button_alt("path!data_queries.php|action!edit|id!" . get_request_var("snmp_query_id")); }
function update_data_query_sort_cache($host_id, $data_query_id) { $raw_xml = get_data_query_array($data_query_id); /* get a list of valid data query types */ $valid_index_types = get_ordered_index_type_list($host_id, $data_query_id); /* something is probably wrong with the data query */ if (sizeof($valid_index_types) == 0) { $sort_field = ""; }else{ /* grab the first field off the list */ $sort_field = $valid_index_types[0]; } /* substitute variables */ if (isset($raw_xml["index_title_format"])) { $title_format = str_replace("|chosen_order_field|", "|query_$sort_field|", $raw_xml["index_title_format"]); }else{ $title_format = "|query_$sort_field|"; } /* update the cache */ db_execute("update host_snmp_query set sort_field = '$sort_field', title_format = '$title_format' where host_id = '$host_id' and snmp_query_id = '$data_query_id'"); }
function update_reindex_cache($host_id, $data_query_id) { global $config; include_once($config["library_path"] . "/data_query.php"); include_once($config["library_path"] . "/snmp.php"); /* will be used to keep track of sql statements to execute later on */ $recache_stack = array(); $host = db_fetch_row("select hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout from host where id=$host_id"); $data_query = db_fetch_row("select reindex_method, sort_field from host_snmp_query where host_id=$host_id and snmp_query_id=$data_query_id"); $data_query_type = db_fetch_cell("select data_input.type_id from (data_input,snmp_query) where data_input.id=snmp_query.data_input_id and snmp_query.id=$data_query_id"); $data_query_xml = get_data_query_array($data_query_id); switch ($data_query["reindex_method"]) { case DATA_QUERY_AUTOINDEX_NONE: break; case DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME: /* the uptime backwards method requires snmp, so make sure snmp is actually enabled * on this device first */ if ($host["snmp_community"] != "") { if (isset($data_query_xml["oid_uptime"])) { $oid_uptime = $data_query_xml["uptime_oid"]; }else{ $oid_uptime = ".1.3.6.1.2.1.1.3.0"; } $assert_value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid_uptime, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], SNMP_POLLER); array_push($recache_stack, "insert into poller_reindex (host_id,data_query_id,action,op,assert_value,arg1) values ($host_id,$data_query_id,0,'<','$assert_value','$oid_uptime')"); } break; case DATA_QUERY_AUTOINDEX_INDEX_NUM_CHANGE: /* this method requires that some command/oid can be used to determine the * current number of indexes in the data query */ $assert_value = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=$host_id and snmp_query_id=$data_query_id group by snmp_index")); if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { if (isset($data_query_xml["oid_num_indexes"])) { array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["oid_num_indexes"] . "')"); } }else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { if (isset($data_query_xml["arg_num_indexes"])) { array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id) . "')"); } } break; case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION: $primary_indexes = db_fetch_assoc("select snmp_index,oid,field_value from host_snmp_cache where host_id=$host_id and snmp_query_id=$data_query_id and field_name='" . $data_query["sort_field"] . "'"); if (sizeof($primary_indexes) > 0) { foreach ($primary_indexes as $index) { $assert_value = $index["field_value"]; if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["fields"]{$data_query["sort_field"]}["oid"] . "." . $index["snmp_index"] . "')"); }else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_get"] . " " . $data_query_xml["fields"]{$data_query["sort_field"]}["query_name"] . " " . $index["snmp_index"], $data_query_xml["script_path"], $host_id) . "')"); } } } break; } /* save the delete for last since we need to reference this table in the code above */ db_execute("delete from poller_reindex where host_id=$host_id and data_query_id=$data_query_id"); for ($i=0; $i<count($recache_stack); $i++) { db_execute($recache_stack[$i]); } }
function graphs() { global $item_rows; /* ================= input validation ================= */ input_validate_input_number(get_request_var_request('host_id')); input_validate_input_number(get_request_var_request('graph_type')); input_validate_input_number(get_request_var_request('rows')); /* ==================================================== */ /* clean up search string */ if (isset($_REQUEST['filter'])) { $_REQUEST['filter'] = sanitize_search_string(get_request_var_request('filter')); } /* if the user pushed the 'clear' button */ if (isset($_REQUEST['clear_x'])) { kill_session_var('sess_graphs_new_filter'); kill_session_var('sess_default_rows'); unset($_REQUEST['filter']); unset($_REQUEST['rows']); $changed = true; } else { /* if any of the settings changed, reset the page number */ $changed = false; $changed += check_changed('host_id', 'sess_graphs_new_host_id'); $changed += check_changed('graph_type', 'sess_graphs_new_graph_type'); $changed += check_changed('filter', 'sess_graphs_new_filter'); $changed += check_changed('rows', 'sess_default_rows'); } load_current_session_value('host_id', 'sess_graphs_new_host_id', db_fetch_cell('SELECT id FROM host ORDER BY description, hostname LIMIT 1')); load_current_session_value('graph_type', 'sess_graphs_new_graph_type', read_config_option('default_graphs_new_dropdown')); load_current_session_value('filter', 'sess_graphs_new_filter', ''); load_current_session_value('rows', 'sess_default_rows', read_config_option('num_rows_table')); if (!empty($_REQUEST['host_id'])) { $host = db_fetch_row_prepared('SELECT id, description, hostname, host_template_id FROM host WHERE id = ?', array($_REQUEST['host_id'])); $header = ' [ ' . htmlspecialchars($host['description']) . ' (' . htmlspecialchars($host['hostname']) . ') ' . (!empty($host['host_template_id']) ? htmlspecialchars(db_fetch_cell_prepared('SELECT name FROM host_template WHERE id = ?', array($host['host_template_id']))) : '') . ' ]'; } else { $host = array(); $header = 'None Host Type'; } $row_limit = get_request_var_request('rows'); html_start_box("<strong>New Graphs for</strong> {$header}", '100%', '', '3', 'center', ''); form_alternate_row(); print '<td class="even">'; ?> <script type='text/javascript'> <!-- function applyFilter() { strURL = '?graph_type=' + $('#graph_type').val(); strURL = strURL + '&host_id=' + $('#host_id').val(); strURL = strURL + '&filter=' + $('#filter').val();; strURL = strURL + '&rows=' + $('#rows').val();; document.location = strURL; } --> </script> <form name='form_graphs_new' action='graphs_new.php'> <table width='100%' cellpadding='2' cellspacing='0' border='0' align='left'> <tr> <?php print html_host_filter($_REQUEST['host_id']); ?> <td style='white-space:nowrap;' width='1'> Graph Types </td> <td width='1'> <select id='graph_type' name='graph_type' onChange='applyFilter()'> <option value='-2'<?php if ($_REQUEST['graph_type'] == '-2') { ?> selected<?php } ?> >All</option> <option value='-1'<?php if ($_REQUEST['graph_type'] == '-1') { ?> selected<?php } ?> >Graph Template Based</option> <?php $snmp_queries = db_fetch_assoc_prepared('SELECT snmp_query.id, snmp_query.name, snmp_query.xml_path FROM (snmp_query, host_snmp_query) WHERE host_snmp_query.snmp_query_id = snmp_query.id AND host_snmp_query.host_id = ? ORDER BY snmp_query.name', array($host['id'])); if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $query) { print "<option value='" . $query['id'] . "'"; if ($_REQUEST['graph_type'] == $query['id']) { print ' selected'; } print '>' . $query['name'] . "</option>\n"; } } ?> </select> </td> <td width='50'> Rows </td> <td> <select id='rows' name='rows' onChange='applyFilter()'> <?php if (sizeof($item_rows) > 0) { foreach ($item_rows as $key => $value) { print "<option value='" . $key . "'"; if (get_request_var_request('rows') == $key) { print ' selected'; } print '>' . htmlspecialchars($value) . "</option>\n"; } } ?> </select> </td> <td rowspan='3' class='textInfo' align='right' valign='top'> <span class='linkMarker'>*</span><a class='hyperLink' href='<?php print htmlspecialchars('host.php?action=edit&id=' . $_REQUEST['host_id']); ?> '>Edit this Device</a><br> <span class='linkMarker'>*</span><a class='hyperLink' href='<?php print htmlspecialchars('host.php?action=edit'); ?> '>Create New Device</a><br> <?php api_plugin_hook('graphs_new_top_links'); ?> </td> </tr> <tr style='<?php if ($_REQUEST['graph_type'] <= 0) { ?> display:none;<?php } ?> '> <td width='50'> Search </td> <td style='white-space:nowrap;'> <input id='filter' type='text' name='filter' size='25' value='<?php print htmlspecialchars(get_request_var_request('filter')); ?> '> </td> <td colspan='3' style='white-space:nowrap;'> <input type='submit' value='Go' title='Set/Refresh Filters'> <input type='submit' name='clear_x' value='Clear' title='Clear Filters'> </td> </tr> </table> </form> </td> </tr> <?php html_end_box(); ?> <form name='chk' method='post' action='graphs_new.php'> <?php $total_rows = sizeof(db_fetch_assoc_prepared('SELECT graph_template_id FROM host_graph WHERE host_id = ?', array($_REQUEST['host_id']))); $i = 0; if ($changed) { foreach ($snmp_queries as $query) { kill_session_var('sess_graphs_new_page' . $query['id']); unset($_REQUEST['page' . $query['id']]); load_current_session_value('page' . $query['id'], 'sess_graphs_new_page' . $query['id'], '1'); } } if ($_REQUEST['graph_type'] > 0) { load_current_session_value('page' . $_REQUEST['graph_type'], 'sess_graphs_new_page' . $_REQUEST['graph_type'], '1'); } else { if ($_REQUEST['graph_type'] == -2) { foreach ($snmp_queries as $query) { load_current_session_value('page' . $query['id'], 'sess_graphs_new_page' . $query['id'], '1'); } } } $script = "<script type='text/javascript'>\nvar gt_created_graphs = new Array();\nvar created_graphs = new Array()\n"; if ($_REQUEST['graph_type'] < 0) { html_start_box('<strong>Graph Templates</strong>', '100%', '', '3', 'center', ''); print "<tr class='tableHeader'>\n\t\t\t\t<td class='tableSubHeaderColumn'>Graph Template Name</td>\n\t\t\t\t<td width='1%' align='center' class='tableSubHeaderCheckbox' style='" . get_checkbox_style() . "'><input type='checkbox' style='margin: 0px;' name='all_cg' title='Select All' onClick='SelectAll(\"sg\",this.checked);'></td>\n\n\t\t\t</tr>\n"; $graph_templates = db_fetch_assoc_prepared('SELECT graph_templates.id AS graph_template_id, graph_templates.name AS graph_template_name FROM (host_graph, graph_templates) WHERE host_graph.graph_template_id = graph_templates.id AND host_graph.host_id = ? ORDER BY graph_templates.name', array($_REQUEST['host_id'])); if (!empty($_REQUEST['host_id'])) { $template_graphs = db_fetch_assoc_prepared('SELECT graph_local.graph_template_id FROM (graph_local, host_graph) WHERE graph_local.graph_template_id = host_graph.graph_template_id AND graph_local.host_id = host_graph.host_id AND graph_local.host_id = ? GROUP BY graph_local.graph_template_id', array($host['id'])); if (sizeof($template_graphs) > 0) { $script .= 'var gt_created_graphs = new Array('; $cg_ctr = 0; foreach ($template_graphs as $template_graph) { $script .= ($cg_ctr > 0 ? ',' : '') . "'" . $template_graph['graph_template_id'] . "'"; $cg_ctr++; } $script .= ")\n"; } } /* create a row for each graph template associated with the host template */ if (sizeof($graph_templates) > 0) { foreach ($graph_templates as $graph_template) { $query_row = $graph_template['graph_template_id']; print "<tr id='gt_line{$query_row}' class='selectable " . ($i % 2 == 0 ? 'odd' : 'even') . "'>"; $i++; print "<td>\n\t\t\t\t\t\t<span id='gt_text{$query_row}" . "_0'>" . htmlspecialchars($graph_template['graph_template_name']) . "</span>\n\t\t\t\t\t</td>\n\t\t\t\t\t<td align='right' class='checkbox'>\n\t\t\t\t\t\t<input type='checkbox' name='cg_{$query_row}' id='cg_{$query_row}'>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>"; } } html_end_box(); html_start_box('', '100%', '', '3', 'center', ''); $available_graph_templates = db_fetch_assoc('SELECT graph_templates.id, graph_templates.name FROM snmp_query_graph RIGHT JOIN graph_templates ON (snmp_query_graph.graph_template_id = graph_templates.id) WHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name'); /* create a row at the bottom that lets the user create any graph they choose */ print "\t<tr class='even'>\n\t\t\t\t<td width='1'><i>Create</i></td>\n\t\t\t\t<td align='left'>"; form_dropdown('cg_g', $available_graph_templates, 'name', 'id', '', '(Select a graph type to create)', '', 'textArea'); print '</td> </tr>'; html_end_box(); } if ($_REQUEST['graph_type'] != -1 && !empty($_REQUEST['host_id'])) { $snmp_queries = db_fetch_assoc('SELECT snmp_query.id, snmp_query.name, snmp_query.xml_path FROM (snmp_query,host_snmp_query) WHERE host_snmp_query.snmp_query_id=snmp_query.id AND host_snmp_query.host_id=' . $host['id'] . ($_REQUEST['graph_type'] != -2 ? ' AND snmp_query.id=' . $_REQUEST['graph_type'] : '') . ' ORDER BY snmp_query.name'); if (sizeof($snmp_queries) > 0) { foreach ($snmp_queries as $snmp_query) { unset($total_rows); if (!$changed) { $page = $_REQUEST['page' . $snmp_query['id']]; } else { $page = 1; } $xml_array = get_data_query_array($snmp_query['id']); $num_input_fields = 0; $num_visible_fields = 0; if ($xml_array != false) { /* loop through once so we can find out how many input fields there are */ reset($xml_array['fields']); while (list($field_name, $field_array) = each($xml_array['fields'])) { if ($field_array['direction'] == 'input') { $num_input_fields++; if (!isset($total_rows)) { $total_rows = db_fetch_cell_prepared('SELECT count(*) FROM host_snmp_cache WHERE host_id = ? AND snmp_query_id = ? AND field_name = ?', array($host['id'], $snmp_query['id'], $field_name)); } } } } if (!isset($total_rows)) { $total_rows = 0; } $snmp_query_graphs = db_fetch_assoc_prepared('SELECT snmp_query_graph.id,snmp_query_graph.name FROM snmp_query_graph WHERE snmp_query_graph.snmp_query_id = ? ORDER BY snmp_query_graph.name', array($snmp_query['id'])); if (sizeof($snmp_query_graphs) > 0) { foreach ($snmp_query_graphs as $snmp_query_graph) { $created_graphs = db_fetch_assoc_prepared("SELECT DISTINCT\n\t\t\t\t\t\tdata_local. snmp_index\n\t\t\t\t\t\tFROM (data_local, data_template_data)\n\t\t\t\t\t\tLEFT JOIN data_input_data ON (data_template_data.id = data_input_data.data_template_data_id)\n\t\t\t\t\t\tLEFT JOIN data_input_fields ON (data_input_data.data_input_field_id = data_input_fields.id)\n\t\t\t\t\t\tWHERE data_local.id = data_template_data.local_data_id\n\t\t\t\t\t\tAND data_input_fields.type_code = 'output_type'\n\t\t\t\t\t\tAND data_input_data.value = ?\n\t\t\t\t\t\tAND data_local.host_id = ?", array($snmp_query_graph['id'], $host['id'])); $script .= 'created_graphs[' . $snmp_query_graph['id'] . '] = new Array('; $cg_ctr = 0; if (sizeof($created_graphs) > 0) { foreach ($created_graphs as $created_graph) { $script .= ($cg_ctr > 0 ? ',' : '') . "'" . encode_data_query_index($created_graph['snmp_index']) . "'"; $cg_ctr++; } } $script .= ")\n"; } } print "\t<table width='100%' class='cactiTable' align='center' cellpadding='3' cellspacing='0'>\n\n\t\t\t\t\t<tr class='cactiTableTitle'>\n\t\t\t\t\t\t<td colspan='" . ($num_input_fields + 1) . "'>\n\t\t\t\t\t\t\t<table cellspacing='0' cellpadding='0' width='100%' >\n\t\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t\t<td class='textHeaderDark'>\n\t\t\t\t\t\t\t\t\t\t<strong>Data Query</strong> [" . $snmp_query['name'] . "]\n\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t\t<td align='right' nowrap>\n\t\t\t\t\t\t\t\t\t\t<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='absmiddle'></a>\n\t\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t</table>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t</tr>"; if ($xml_array != false) { $html_dq_header = ''; $snmp_query_indexes = array(); reset($xml_array['fields']); /* if there is a where clause, get the matching snmp_indexes */ $sql_where = ''; if (strlen($_REQUEST['filter'])) { $sql_where = ''; $indexes = db_fetch_assoc("SELECT DISTINCT snmp_index\n\t\t\t\t\t\tFROM host_snmp_cache\n\t\t\t\t\t\tWHERE field_value LIKE '%%" . $_REQUEST['filter'] . "%%'\n\t\t\t\t\t\tAND snmp_query_id=" . $snmp_query['id'] . "\n\t\t\t\t\t\tAND host_id=" . $host['id']); if (sizeof($indexes)) { foreach ($indexes as $index) { if (strlen($sql_where)) { $sql_where .= ", '" . $index['snmp_index'] . "'"; } else { $sql_where .= " AND snmp_index IN('" . $index['snmp_index'] . "'"; } } $sql_where .= ')'; } } if (strlen($_REQUEST['filter']) == 0 || strlen($_REQUEST['filter']) && sizeof($indexes)) { /* determine the sort order */ if (isset($xml_array['index_order_type'])) { if ($xml_array['index_order_type'] == 'numeric') { $sql_order = 'ORDER BY CAST(snmp_index AS unsigned)'; } else { if ($xml_array['index_order_type'] == 'alphabetic') { $sql_order = 'ORDER BY snmp_index'; } else { if ($xml_array['index_order_type'] == 'natural') { $sql_order = 'ORDER BY INET_ATON(snmp_index)'; } else { $sql_order = ''; } } } } else { $sql_order = ''; } /* get the unique field values from the database */ $field_names = db_fetch_assoc_prepared('SELECT DISTINCT field_name FROM host_snmp_cache WHERE host_id = ? AND snmp_query_id = ?', array($host['id'], $snmp_query['id'])); /* build magic query */ $sql_query = 'SELECT host_id, snmp_query_id, snmp_index'; $num_visible_fields = sizeof($field_names); $i = 0; if (sizeof($field_names) > 0) { foreach ($field_names as $column) { $field_name = $column['field_name']; $sql_query .= ", MAX(CASE WHEN field_name='{$field_name}' THEN field_value ELSE NULL END) AS '{$field_name}'"; $i++; } } $sql_query .= ' FROM host_snmp_cache WHERE host_id=' . $host['id'] . ' AND snmp_query_id=' . $snmp_query['id'] . "\n\t\t\t\t\t\t{$sql_where}\n\t\t\t\t\t\tGROUP BY host_id, snmp_query_id, snmp_index\n\t\t\t\t\t\t{$sql_order}\n\t\t\t\t\t\tLIMIT " . $row_limit * ($page - 1) . ',' . $row_limit; $rows_query = 'SELECT host_id, snmp_query_id, snmp_index FROM host_snmp_cache WHERE host_id=' . $host['id'] . ' AND snmp_query_id=' . $snmp_query['id'] . "\n\t\t\t\t\t\t{$sql_where}\n\t\t\t\t\t\tGROUP BY host_id, snmp_query_id, snmp_index"; $snmp_query_indexes = db_fetch_assoc($sql_query); $total_rows = sizeof(db_fetch_assoc($rows_query)); if (($page - 1) * $row_limit > $total_rows) { $page = 1; $_REQUEST['page' . $query['id']] = $page; load_current_session_value('page' . $query['id'], 'sess_graphs_new_page' . $query['id'], '1'); } $nav = html_nav_bar('graphs_new.php', MAX_DISPLAY_PAGES, $page, $row_limit, $total_rows, 15, 'Items', 'page' . $snmp_query['id']); print $nav; while (list($field_name, $field_array) = each($xml_array['fields'])) { if ($field_array['direction'] == 'input' && sizeof($field_names)) { foreach ($field_names as $row) { if ($row['field_name'] == $field_name) { $html_dq_header .= "<td class='tableSubHeaderColumn'>" . $field_array['name'] . "</td>\n"; break; } } } } if (!sizeof($snmp_query_indexes)) { print "<tr class='odd'><td>This Data Query returned 0 rows, perhaps there was a problem executing this\n\t\t\t\t\t\t\tData Query. You can <a href='" . htmlspecialchars('host.php?action=query_verbose&id=' . $snmp_query['id'] . '&host_id=' . $host['id']) . "'>run this Data Query in debug mode</a> to get more information.</td></tr>\n"; } else { print "<tr class='tableHeader'>\n\t\t\t\t\t\t\t\t{$html_dq_header}\n\t\t\t\t\t\t\t\t<td width='1%' align='center' class='tableSubHeaderCheckbox' 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)'></td>\n\n\t\t\t\t\t\t\t</tr>\n"; } $row_counter = 0; $column_counter = 0; $fields = array_rekey($field_names, 'field_name', 'field_name'); if (sizeof($snmp_query_indexes) > 0) { foreach ($snmp_query_indexes as $row) { $query_row = $snmp_query['id'] . '_' . encode_data_query_index($row['snmp_index']); print "<tr id='line{$query_row}' class='selectable " . ($row_counter % 2 == 0 ? 'odd' : 'even') . "'>"; $i++; $column_counter = 0; reset($xml_array['fields']); while (list($field_name, $field_array) = each($xml_array['fields'])) { if ($field_array['direction'] == 'input') { if (in_array($field_name, $fields)) { if (isset($row[$field_name])) { print "<td><span id='text{$query_row}" . '_' . $column_counter . "'>" . (strlen($_REQUEST['filter']) ? preg_replace('/(' . preg_quote($_REQUEST['filter']) . ')/i', "<span class='filteredValue'>\\1</span>", $row[$field_name]) : $row[$field_name]) . '</span></td>'; } else { print "<td><span id='text{$query_row}" . '_' . $column_counter . "'></span></td>"; } $column_counter++; } } } print "<td class='checkbox' align='right'>"; print "<input type='checkbox' name='sg_{$query_row}' id='sg_{$query_row}'>"; print '</td>'; print "</tr>\n"; $row_counter++; } } if ($total_rows > $row_limit) { print $nav; } } else { print "<tr class='odd'><td class='textError'>Search Returned no Rows.</td></tr>\n"; } } else { print "<tr class='odd'><td class='textError'>Error in data query.</td></tr>\n"; } print '</table>'; /* draw the graph template drop down here */ $data_query_graphs = db_fetch_assoc_prepared('SELECT snmp_query_graph.id, snmp_query_graph.name FROM snmp_query_graph WHERE snmp_query_graph.snmp_query_id = ? ORDER BY snmp_query_graph.name', array($snmp_query['id'])); if (sizeof($data_query_graphs) == 1) { echo "<input type='hidden' id='sgg_" . $snmp_query['id'] . "' name='sgg_" . $snmp_query['id'] . "' value='" . $data_query_graphs[0]['id'] . "'>\n"; } elseif (sizeof($data_query_graphs) > 1) { print "\t<table align='center' width='100%'>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width='100%' valign='middle'>\n\t\t\t\t\t\t\t\t<img src='images/arrow.gif' align='absmiddle' alt=''>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td style='white-space:nowrap;font-style: italic;'' align='right'>\n\t\t\t\t\t\t\t\tSelect a Graph Type to Create\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t<td align='right'>\n\t\t\t\t\t\t\t\t<select name='sgg_" . $snmp_query['id'] . "' id='sgg_" . $snmp_query['id'] . "' onChange='dqUpdateDeps(" . $snmp_query['id'] . ',' . (isset($column_counter) ? $column_counter : '') . ");'>\n\t\t\t\t\t\t\t\t\t"; html_create_list($data_query_graphs, 'name', 'id', '0'); print "\n\t\t\t\t\t\t\t\t</select>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n"; } print '<br>'; $script .= 'dqUpdateDeps(' . $snmp_query['id'] . ',' . $num_visible_fields . ");\n"; } } } if (strlen($script)) { $script .= "</script>\n"; print $script; } form_hidden_box('save_component_graph', '1', ''); if (!empty($_REQUEST['host_id'])) { form_hidden_box('host_id', $host['id'], '0'); form_hidden_box('host_template_id', $host['host_template_id'], '0'); } if (isset($_SERVER['HTTP_REFERER']) && !substr_count($_SERVER['HTTP_REFERER'], 'graphs_new')) { $_REQUEST['returnto'] = basename($_SERVER['HTTP_REFERER']); } load_current_session_value('returnto', 'sess_graphs_new_returnto', ''); form_save_button($_REQUEST['returnto']); }
function update_reindex_cache($device_id, $data_query_id) { global $config; require_once(CACTI_BASE_PATH . "/include/data_query/data_query_constants.php"); require_once(CACTI_BASE_PATH . "/include/data_input/data_input_constants.php"); include_once(CACTI_BASE_PATH . "/lib/data_query.php"); include_once(CACTI_BASE_PATH . "/lib/snmp.php"); /* will be used to keep track of sql statements to execute later on */ $recache_stack = array(); $device = db_fetch_row("select hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout from device where id=$device_id"); $data_query = db_fetch_row("select reindex_method, sort_field from device_snmp_query where device_id=$device_id and snmp_query_id=$data_query_id"); $data_query_type = db_fetch_cell("select data_input.type_id from (data_input,snmp_query) where data_input.id=snmp_query.data_input_id and snmp_query.id=$data_query_id"); $data_query_xml = get_data_query_array($data_query_id); switch ($data_query["reindex_method"]) { case DATA_QUERY_AUTOINDEX_NONE: break; case DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME: /* the uptime backwards method requires snmp, so make sure snmp is actually enabled * on this device first */ if ($device["snmp_community"] != "") { if (isset($data_query_xml["oid_uptime"])) { $oid_uptime = $data_query_xml["oid_uptime"]; }elseif (isset($data_query_xml["uptime_oid"])) { $oid_uptime = $data_query_xml["uptime_oid"]; }else{ $oid_uptime = ".1.3.6.1.2.1.1.3.0"; } $assert_value = cacti_snmp_get($device["hostname"], $device["snmp_community"], $oid_uptime, $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], SNMP_POLLER); $recache_stack[] = "('$device_id', '$data_query_id', '0', '<', '$assert_value', '$oid_uptime', '1')"; } break; case DATA_QUERY_AUTOINDEX_INDEX_COUNT_CHANGE: /* this method requires that some command/oid can be used to determine the * current number of indexes in the data query */ $assert_value = sizeof(db_fetch_assoc("select snmp_index from device_snmp_cache where device_id=$device_id and snmp_query_id=$data_query_id group by snmp_index")); if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { if (isset($data_query_xml["oid_num_indexes"])) { $recache_stack[] = "($device_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["oid_num_indexes"] . "', '1')"; } }else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { if (isset($data_query_xml["arg_num_indexes"])) { $recache_stack[] = "($device_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $device_id) . "', '1')"; } } break; case DATA_QUERY_AUTOINDEX_VALUE_CHANGE: /* this method uses the value of the index OID to determine if a re-index is required */ $assert_value = db_fetch_cell("select assert_value from poller_reindex where device_id=$device_id and data_query_id=$data_query_id"); if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { if (isset($data_query_xml["oid_num_indexes"])) { $recache_stack[] = "($device_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["oid_num_indexes"] . "', '1')"; } }else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { if (isset($data_query_xml["arg_num_indexes"])) { $recache_stack[] = "($device_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $device_id) . "', '1')"; } } break; case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION: $primary_indexes = db_fetch_assoc("select snmp_index,oid,field_value from device_snmp_cache where device_id=$device_id and snmp_query_id=$data_query_id and field_name='" . $data_query["sort_field"] . "'"); if (sizeof($primary_indexes) > 0) { foreach ($primary_indexes as $index) { $assert_value = $index["field_value"]; if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { $recache_stack[] = "($device_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["fields"]{$data_query["sort_field"]}["oid"] . "." . $index["snmp_index"] . "', '1')"; }else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { $recache_stack[] = "('$device_id', '$data_query_id', '1', '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_get"] . " " . $data_query_xml["fields"]{$data_query["sort_field"]}["query_name"] . " " . $index["snmp_index"], $data_query_xml["script_path"], $device_id) . "', '1')"; } } } break; } if (sizeof($recache_stack)) { poller_update_poller_reindex_from_buffer($device_id, $data_query_id, $recache_stack); } }