", $config["base_path"], $xml_file_path); if (!file_exists($xml_file_path)) { debug_log_insert("data_query", "Could not find data query XML file at '$xml_file_path'"); return false; } debug_log_insert("data_query", "Found data query XML file at '$xml_file_path'"); $data = implode("",file($xml_file_path)); return xml2array($data); } function query_script_host($host_id, $snmp_query_id) { $script_queries = get_data_query_array($snmp_query_id); /* invalid xml check */ if ((!is_array($script_queries)) || (sizeof($script_queries) == 0)) { debug_log_insert("data_query", "Error parsing XML file into an array."); return false; } debug_log_insert("data_query", "XML file parsed ok."); if (isset($script_queries["script_server"])) { $script_queries["script_path"] = "|path_php_binary| -q " . $script_queries["script_path"]; } $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_index"], $script_queries["script_path"], $host_id); /* fetch specified index at specified OID */ $script_index_array = exec_into_array($script_path); debug_log_insert("data_query", "Executing script for list of indexes '$script_path'"); db_execute("delete from host_snmp_cache where host_id=$host_id and snmp_query_id=$snmp_query_id"); while (list($field_name, $field_array) = each($script_queries["fields"])) { if ($field_array["direction"] == "input") { $script_path = get_script_query_path((isset($script_queries["arg_prepend"]) ? $script_queries["arg_prepend"] . " ": "") . $script_queries["arg_query"] . " " . $field_array["query_name"], $script_queries["script_path"], $host_id); $script_data_array = exec_into_array($script_path); debug_log_insert("data_query", "Executing script query '$script_path'"); for ($i=0;($i ifIndex [index_value] => 3 [output_type] => 13 ) */ function data_query_field_list($data_template_data_id) { $field = db_fetch_assoc("select data_input_fields.type_code, data_input_data.value from (data_input_fields,data_input_data) where data_input_fields.id=data_input_data.data_input_field_id and data_input_data.data_template_data_id=$data_template_data_id and (data_input_fields.type_code='index_type' or data_input_fields.type_code='index_value' or data_input_fields.type_code='output_type')"); $field = array_rekey($field, "type_code", "value"); if ((!isset($field["index_type"])) || (!isset($field["index_value"])) || (!isset($field["output_type"]))) { return 0; }else{ return $field; } } /* encode_data_query_index - encodes a data query index value so that it can be included inside of a form @arg $index - the index name to encode @returns - the encoded data query index */ function encode_data_query_index($index) { return md5($index); } /* decode_data_query_index - decodes a data query index value so that it can be read from a form @arg $encoded_index - the index that was encoded with encode_data_query_index() @arg $data_query_id - the id of the data query that this index belongs to @arg $encoded_index - the id of the host that this index belongs to @returns - the decoded data query index */ function decode_data_query_index($encoded_index, $data_query_id, $host_id) { /* yes, i know MySQL has a MD5() function that would make this a bit quicker. however i would like to keep things abstracted for now so Cacti works with ADODB fully when i get around to porting my db calls */ $indexes = 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 (sizeof($indexes) > 0) { foreach ($indexes as $index) { if (encode_data_query_index($index["snmp_index"]) == $encoded_index) { return $index["snmp_index"]; } } } } /* update_data_query_cache - updates the local data query cache for each graph and data source tied to this host/data query @arg $host_id - the id of the host to refresh @arg $data_query_id - the id of the data query to refresh */ function update_data_query_cache($host_id, $data_query_id) { $graphs = db_fetch_assoc("select id from graph_local where host_id = '$host_id' and snmp_query_id = '$data_query_id'"); if (sizeof($graphs) > 0) { foreach ($graphs as $graph) { update_graph_data_query_cache($graph["id"]); } } $data_sources = db_fetch_assoc("select id from data_local where host_id = '$host_id' and snmp_query_id = '$data_query_id'"); if (sizeof($data_sources) > 0) { foreach ($data_sources as $data_source) { update_data_source_data_query_cache($data_source["id"]); } } } /* update_graph_data_query_cache - updates the local data query cache for a particular graph @arg $local_graph_id - the id of the graph to update the data query cache for */ function update_graph_data_query_cache($local_graph_id) { $host_id = db_fetch_cell("select host_id from graph_local where id=$local_graph_id"); $field = data_query_field_list(db_fetch_cell("select data_template_data.id from (graph_templates_item,data_template_rrd,data_template_data) where graph_templates_item.task_item_id=data_template_rrd.id and data_template_rrd.local_data_id=data_template_data.local_data_id and graph_templates_item.local_graph_id=$local_graph_id limit 0,1")); if (empty($field)) { return; } $data_query_id = db_fetch_cell("select snmp_query_id from snmp_query_graph where id='" . $field["output_type"] . "'"); $index = data_query_index($field["index_type"], $field["index_value"], $host_id, $data_query_id); if (($data_query_id != "0") && ($index != "")) { db_execute("update graph_local set snmp_query_id='$data_query_id',snmp_index='$index' where id=$local_graph_id"); /* update graph title cache */ update_graph_title_cache($local_graph_id); } } /* update_data_source_data_query_cache - updates the local data query cache for a particular data source @arg $local_data_id - the id of the data source to update the data query cache for */ function update_data_source_data_query_cache($local_data_id) { $host_id = db_fetch_cell("select host_id from data_local where id=$local_data_id"); $field = data_query_field_list(db_fetch_cell("select data_template_data.id from data_template_data where data_template_data.local_data_id=$local_data_id")); if (empty($field)) { return; } $data_query_id = db_fetch_cell("select snmp_query_id from snmp_query_graph where id='" . $field["output_type"] . "'"); $index = data_query_index($field["index_type"], $field["index_value"], $host_id, $data_query_id); if (($data_query_id != "0") && ($index != "")) { db_execute("update data_local set snmp_query_id='$data_query_id',snmp_index='$index' where id='$local_data_id'"); /* update data source title cache */ update_data_source_title_cache($local_data_id); } } /* get_formatted_data_query_indexes - obtains a list of indexes for a host/data query that is sorted by the chosen index field and formatted using the data query index title format @arg $host_id - the id of the host which contains the data query @arg $data_query_id - the id of the data query to retrieve a list of indexes for @returns - an array formatted like the following: $arr[snmp_index] = "formatted data query index string" */ function get_formatted_data_query_indexes($host_id, $data_query_id) { global $config; include_once($config["library_path"] . "/sort.php"); if (empty($data_query_id)) { return array("" => "Unknown Index"); } /* from the xml; cached in 'host_snmp_query' */ $sort_cache = db_fetch_row("select sort_field,title_format from host_snmp_query where host_id='$host_id' and snmp_query_id='$data_query_id'"); /* get a list of data query indexes and the field value that we are supposed to sort */ $sort_field_data = array_rekey(db_fetch_assoc("select graph_local.snmp_index, host_snmp_cache.field_value from (graph_local,host_snmp_cache) where graph_local.host_id=host_snmp_cache.host_id and graph_local.snmp_query_id=host_snmp_cache.snmp_query_id and graph_local.snmp_index=host_snmp_cache.snmp_index and graph_local.snmp_query_id=$data_query_id and graph_local.host_id=$host_id and host_snmp_cache.field_name='" . $sort_cache["sort_field"] . "' group by graph_local.snmp_index"), "snmp_index", "field_value"); /* sort the data using the "data query index" sort algorithm */ uasort($sort_field_data, "usort_data_query_index"); $sorted_results = array(); while (list($snmp_index, $sort_field_value) = each($sort_field_data)) { $sorted_results[$snmp_index] = substitute_snmp_query_data($sort_cache["title_format"], $host_id, $data_query_id, $snmp_index); } return $sorted_results; } /* get_formatted_data_query_index - obtains a single index for a host/data query/data query index that is formatted using the data query index title format @arg $host_id - the id of the host which contains the data query @arg $data_query_id - the id of the data query which contains the data query index @arg $data_query_index - the index to retrieve the formatted name for @returns - a string containing the formatted name for the given data query index */ function get_formatted_data_query_index($host_id, $data_query_id, $data_query_index) { /* from the xml; cached in 'host_snmp_query' */ $sort_cache = db_fetch_row("select sort_field,title_format from host_snmp_query where host_id='$host_id' and snmp_query_id='$data_query_id'"); return substitute_snmp_query_data($sort_cache["title_format"], $host_id, $data_query_id, $data_query_index); } /* get_ordered_index_type_list - builds an ordered list of data query index types that are valid given a list of data query indexes that will be checked against the data query cache @arg $host_id - the id of the host which contains the data query @arg $data_query_id - the id of the data query to build the type list from @arg $data_query_index_array - an array containing each data query index to use when checking each data query type for validity. a valid data query type will contain no empty or duplicate values for each row in the cache that matches one of the $data_query_index_array @returns - an array of data query types either ordered or unordered depending on whether the xml file has a manual ordering preference specified */ function get_ordered_index_type_list($host_id, $data_query_id, $data_query_index_array = array()) { $raw_xml = get_data_query_array($data_query_id); /* invalid xml check */ if ((!is_array($raw_xml)) || (sizeof($raw_xml) == 0)) { return array(); } $xml_outputs = array(); /* create an SQL string that contains each index in this snmp_index_id */ $sql_or = array_to_sql_or($data_query_index_array, "snmp_index"); /* check for nonunique query parameter, set value */ if (isset($raw_xml["index_type"])) { if ($raw_xml["index_type"] == "nonunique") { $nonunique = 1; }else{ $nonunique = 0; } } else { $nonunique = 0; } /* list each of the input fields for this snmp query */ while (list($field_name, $field_array) = each($raw_xml["fields"])) { if ($field_array["direction"] == "input") { /* create a list of all values for this index */ if (sizeof($data_query_index_array) == 0) { $field_values = db_fetch_assoc("select field_value from host_snmp_cache where host_id=$host_id and snmp_query_id=$data_query_id and field_name='$field_name'"); }else{ $field_values = db_fetch_assoc("select field_value from host_snmp_cache where host_id=$host_id and snmp_query_id=$data_query_id and field_name='$field_name' and $sql_or"); } /* aggregate the above list so there is no duplicates */ $aggregate_field_values = array_rekey($field_values, "field_value", "field_value"); /* fields that contain duplicate or empty values are not suitable to index off of */ if (!((sizeof($aggregate_field_values) < sizeof($field_values)) || (in_array("", $aggregate_field_values) == true) || (sizeof($aggregate_field_values) == 0)) || ($nonunique)) { array_push($xml_outputs, $field_name); } } } $return_array = array(); /* the xml file contains an ordered list of "indexable" fields */ if (isset($raw_xml["index_order"])) { $index_order_array = explode(":", $raw_xml["index_order"]); for ($i=0; $i 0) { foreach ($data_queries as $data_query) { update_data_query_sort_cache($host_id, $data_query["snmp_query_id"]); } } } /* get_best_data_query_index_type - returns the best available data query index type using the sort cache @arg $host_id - the id of the host which contains the data query @arg $data_query_id - the id of the data query to fetch the best data query index type for @returns - a string containing containing best data query index type. this will be one of the valid input field names as specified in the data query xml file */ function get_best_data_query_index_type($host_id, $data_query_id) { return db_fetch_cell("select sort_field from host_snmp_query where host_id = '$host_id' and snmp_query_id = '$data_query_id'"); } /* get_script_query_path - builds the complete script query executable path @arg $args - the variable that contains any arguments to be appended to the argument list (variables will be substituted in this function) @arg $script_path - the path on the disk to the script file @arg $host_id - the id of the host that this script query belongs to @returns - a full path to the script query script containing all arguments */ function get_script_query_path($args, $script_path, $host_id) { global $config; include_once($config["library_path"] . "/variables.php"); /* get any extra arguments that need to be passed to the script */ if (!empty($args)) { $extra_arguments = substitute_host_data($args, "|", "|", $host_id); }else{ $extra_arguments = ""; } /* get a complete path for out target script */ return substitute_script_query_path($script_path) . " $extra_arguments"; } ?>