function host_new_graphs_save() { $selected_graphs_array = unserialize(stripslashes($_POST['selected_graphs_array'])); /* form an array that contains all of the data on the previous form */ while (list($var, $val) = each($_POST)) { if (preg_match('/^g_(\\d+)_(\\d+)_(\\w+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['graph_template'][$matches[3]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['graph_template'][$matches[3]] = $val; } } elseif (preg_match('/^gi_(\\d+)_(\\d+)_(\\d+)_(\\w+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: graph_template_input_id, 4:field_name */ /* ================= input validation ================= */ input_validate_input_number($matches[3]); /* ==================================================== */ /* we need to find out which graph items will be affected by saving this particular item */ $item_list = db_fetch_assoc_prepared('SELECT graph_template_item_id FROM graph_template_input_defs WHERE graph_template_input_id = ?', array($matches[3])); /* loop through each item affected and update column data */ if (sizeof($item_list) > 0) { foreach ($item_list as $item) { if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['graph_template_item'][$item['graph_template_item_id']][$matches[4]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['graph_template_item'][$item['graph_template_item_id']][$matches[4]] = $val; } } } } elseif (preg_match('/^d_(\\d+)_(\\d+)_(\\d+)_(\\w+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['data_template'][$matches[3]][$matches[4]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['data_template'][$matches[3]][$matches[4]] = $val; } } elseif (preg_match('/^c_(\\d+)_(\\d+)_(\\d+)_(\\d+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:data_input_field_id */ if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['custom_data'][$matches[3]][$matches[4]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['custom_data'][$matches[3]][$matches[4]] = $val; } } elseif (preg_match('/^di_(\\d+)_(\\d+)_(\\d+)_(\\d+)_(\\w+)/', $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:local_data_template_rrd_id, 5:field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values['cg'][$matches[2]]['data_template_item'][$matches[4]][$matches[5]] = $val; } else { /* this is a data query field */ $values['sg'][$matches[1]][$matches[2]]['data_template_item'][$matches[4]][$matches[5]] = $val; } } } debug_log_clear('new_graphs'); while (list($form_type, $form_array) = each($selected_graphs_array)) { $current_form_type = $form_type; while (list($form_id1, $form_array2) = each($form_array)) { /* enumerate information from the arrays stored in post variables */ if ($form_type == 'cg') { $graph_template_id = $form_id1; } elseif ($form_type == 'sg') { while (list($form_id2, $form_array3) = each($form_array2)) { $snmp_index_array = $form_array3; $snmp_query_array['snmp_query_id'] = $form_id1; $snmp_query_array['snmp_index_on'] = get_best_data_query_index_type($_POST['host_id'], $form_id1); $snmp_query_array['snmp_query_graph_id'] = $form_id2; } $graph_template_id = db_fetch_cell_prepared('SELECT graph_template_id FROM snmp_query_graph WHERE id = ?', array($snmp_query_array['snmp_query_graph_id'])); } if ($current_form_type == 'cg') { $return_array = create_complete_graph_from_template($graph_template_id, $_POST['host_id'], '', $values['cg']); debug_log_insert('new_graphs', 'Created graph: ' . get_graph_title($return_array['local_graph_id'])); /* lastly push host-specific information to our data sources */ if (sizeof($return_array['local_data_id'])) { # we expect at least one data source associated foreach ($return_array['local_data_id'] as $item) { push_out_host($_POST['host_id'], $item); } } else { debug_log_insert('new_graphs', 'ERROR: no Data Source associated. Check Template'); } } elseif ($current_form_type == 'sg') { while (list($snmp_index, $true) = each($snmp_index_array)) { $snmp_query_array['snmp_index'] = decode_data_query_index($snmp_index, $snmp_query_array['snmp_query_id'], $_POST['host_id']); $return_array = create_complete_graph_from_template($graph_template_id, $_POST['host_id'], $snmp_query_array, $values['sg'][$snmp_query_array['snmp_query_id']]); debug_log_insert('new_graphs', 'Created graph: ' . get_graph_title($return_array['local_graph_id'])); /* lastly push host-specific information to our data sources */ if (sizeof($return_array['local_data_id'])) { # we expect at least one data source associated foreach ($return_array['local_data_id'] as $item) { push_out_host($_POST['host_id'], $item); } } else { debug_log_insert('new_graphs', 'ERROR: no Data Source associated. Check Template'); } } } } } }
function host_new_graphs_save() { $validation_array = array(); $selected_graphs_array = unserialize(stripslashes($_POST["selected_graphs_array"])); $map_id_to_index_array = unserialize(stripslashes($_POST["map_id_to_index_array"])); /* form an array that contains all of the data on the previous form */ while (list($var, $val) = each($_POST)) { if (preg_match("/^g_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: data_query_id, 2: graph_template_id, 3: field_name */ foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) { $selected_graphs_array[$uniq_id]["graph_template"][$matches[3]] = $val; } $validation_array["graph_template"][$matches[3]][$var] = $val; } elseif (preg_match("/^gi_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: data_query_id, 2: graph_template_id, 3: graph_template_input_id, 4: field_name */ foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) { $selected_graphs_array[$uniq_id]["graph_template_item"][$matches[2]][$matches[3]] = $val; } $validation_array["graph_template_item"][$matches[4]][$var] = $val; } elseif (preg_match("/^d_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: data_query_id, 2: graph_template_id, 3: data_template_id, 4: field_name */ foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) { $selected_graphs_array[$uniq_id]["data_template"][$matches[3]][$matches[4]] = $val; } $validation_array["data_template"][$matches[4]][$var] = $val; } elseif (preg_match("/^c_(\\d+)_(\\d+)_(\\d+)_(\\d+)/", $var, $matches)) { /* 1: data_query_id, 2: graph_template_id, 3: data_template_id, 4: data_input_field_name */ foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) { $selected_graphs_array[$uniq_id]["custom_data"][$matches[3]][$matches[4]] = $val; } $validation_array["custom_data"][$matches[4]][$var] = $val; } elseif (preg_match("/^di_(\\d+)_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: data_query_id, 2: graph_template_id, 3: data_template_id, 4: data_template_item_id, 5: field_name */ foreach ($map_id_to_index_array[empty($matches[1]) ? "gt" : "dq"][empty($matches[1]) ? $matches[2] : $matches[1]] as $uniq_id) { $selected_graphs_array[$uniq_id]["data_template_item"][$matches[3]][$matches[4]][$matches[5]] = $val; } $validation_array["data_template_item"][$matches[5]][$var] = $val; } } /* first pass: form validation */ while (list($type, $type_array) = each($validation_array)) { while (list($field_name, $field_array) = each($type_array)) { while (list($form_field_name, $value) = each($field_array)) { $_v_arr = array($field_name => $value); if ($type == "data_template") { $_sv_arr = array(); field_register_error(api_data_source_fields_validate($_v_arr, $_sv_arr, $form_field_name, "")); } else { if ($type == "custom_data") { field_register_error(api_data_source_input_fields_validate($_v_arr, $form_field_name)); } else { if ($type == "data_template_item") { $_v_arr["id"] = 0; field_register_error(api_data_source_item_fields_validate($_v_arr, $form_field_name)); } else { if ($type == "graph_template") { $_sv_arr = array(); field_register_error(api_graph_fields_validate($_v_arr, $_sv_arr, $form_field_name, "")); } else { if ($type == "graph_template_item") { field_register_error(api_graph_item_fields_validate($_v_arr, $form_field_name)); } } } } } } } } /* form validation failed: redirect back */ if (is_error_message()) { /* cache all post field values */ init_post_field_cache(); host_new_graphs($selected_graphs_array, $map_id_to_index_array); /* form validation passed: save the data on the form */ } else { debug_log_clear("new_graphs"); foreach ($selected_graphs_array as $uniq_id => $skel) { if (isset($skel["custom_data"]["all_dq"])) { $is_data_query_graph = true; $data_query_id = isset($skel["custom_data"]["all_dq"]["data_query_id"]) ? $skel["custom_data"]["all_dq"]["data_query_id"] : 0; /* decode the data query index into its literal form */ $data_query_index = decode_data_query_index(isset($skel["custom_data"]["all_dq"]["data_query_index"]) ? $skel["custom_data"]["all_dq"]["data_query_index"] : 0, get_data_query_indexes($data_query_id, $_POST["host_id"])); } else { $is_data_query_graph = false; $data_query_id = 0; $data_query_index = ""; } $create_info = generate_complete_graph($skel["graph_template_id"], $_POST["host_id"], $data_query_id, $data_query_index); /* set the appropriate 'custom_data' keys */ if ($is_data_query_graph == true) { /* pick the data query field name to index on */ $data_query_field_name = get_best_data_query_index_type($_POST["host_id"], $data_query_id); /* get a list of all data templates associated with this graph template that are of type DATA_INPUT_TYPE_DATA_QUERY */ $dq_data_templates = get_data_templates_from_graph_template($skel["graph_template_id"], DATA_INPUT_TYPE_DATA_QUERY); if (sizeof($dq_data_templates) > 0) { foreach ($dq_data_templates as $data_template) { $skel["custom_data"][$data_template["id"]]["data_query_id"] = $data_query_id; $skel["custom_data"][$data_template["id"]]["data_query_index"] = $data_query_index; $skel["custom_data"][$data_template["id"]]["data_query_field_name"] = $data_query_field_name; $skel["custom_data"][$data_template["id"]]["data_query_field_value"] = get_data_query_row_value($data_query_id, $_POST["host_id"], $data_query_field_name, $data_query_index); } } } /* update user specified data: data source-specific fields */ foreach (array_keys($create_info["data_source"]) as $data_template_id) { if (isset($skel["data_template"][$data_template_id])) { if (!api_data_source_save($create_info["data_source"][$data_template_id], $skel["data_template"][$data_template_id])) { log_save("Problems updating new data source [ID#" . $create_info["data_source"][$data_template_id] . "], data template [ID#{$data_template_id}] from user data", SEV_ERROR); } } if (isset($skel["data_template_item"][$data_template_id])) { foreach ($skel["data_template_item"][$data_template_id] as $data_source_item_id => $data_template_item_array) { if (!api_data_source_item_save($data_source_item_id, $data_template_item_array)) { log_save("Problems updating new data source [item] [ID#" . $create_info["data_source"][$data_template_id] . "], data template [ID#{$data_template_id}] from user data", SEV_ERROR); } } } if (isset($skel["custom_data"][$data_template_id])) { if (!api_data_source_fields_save($create_info["data_source"][$data_template_id], $skel["custom_data"][$data_template_id])) { log_save("Problems updating new data source (fields) [ID#" . $create_info["data_source"][$data_template_id] . "], data template [ID#{$data_template_id}] from user data", SEV_ERROR); } } /* update the title cache */ api_data_source_title_cache_update($create_info["data_source"][$data_template_id]); /* update poller cache */ update_poller_cache($create_info["data_source"][$data_template_id]); } /* update user specified data: graph-specific fields */ foreach (array_keys($create_info["graph"]) as $graph_template_id) { if (isset($skel["graph_template"][$graph_template_id])) { if (!api_graph_save($create_info["graph"][$graph_template_id], $skel["graph_template"][$graph_template_id])) { log_save("Problems updating new graph [ID#" . $create_info["graph"][$graph_template_id] . "], graph template [ID#{$graph_template_id}] from user data", SEV_ERROR); } } if (isset($skel["graph_template_item"][$graph_template_id])) { foreach ($skel["graph_template_item"][$graph_template_id] as $graph_template_item_input_id => $value) { if (!api_graph_template_item_input_propagate($graph_template_item_input_id, $value)) { log_save("Problems updating new graph [item] [ID#" . $create_info["graph"][$graph_template_id] . "], graph template [ID#{$graph_template_id}] from user data", SEV_ERROR); } } } /* update the title cache */ api_graph_title_cache_update($create_info["graph"][$graph_template_id]); } debug_log_insert("new_graphs", _("Created graph: ") . api_graph_title_get($create_info["graph"][$skel["graph_template_id"]])); } /* lastly push host-specific information to our data sources */ //push_out_host($_POST["host_id"], 0); } }
function query_snmp_host($host_id, $snmp_query_id) { global $config; include_once($config["library_path"] . "/snmp.php"); $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'"); $snmp_queries = get_data_query_array($snmp_query_id); if ($host["hostname"] == "") { debug_log_insert("data_query", "Invalid host_id: $host_id"); return false; } /* invalid xml check */ if ((!is_array($snmp_queries)) || (sizeof($snmp_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."); /* fetch specified index at specified OID */ $snmp_index = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $snmp_queries["oid_index"], $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"], read_config_option("snmp_retries"), SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP walk for list of indexes @ '" . $snmp_queries["oid_index"] . "'"); /* no data found; get out */ if (!$snmp_index) { debug_log_insert("data_query", "No SNMP data returned"); return false; } /* the last octet of the oid is the index by default */ $index_parse_regexp = '.*\.([0-9]+)$'; /* parse the index if required */ if (isset($snmp_queries["oid_index_parse"])) { $index_parse_regexp = str_replace("OID/REGEXP:", "", $snmp_queries["oid_index_parse"]); for ($i=0; $i<sizeof($snmp_index); $i++) { $snmp_index[$i]["value"] = ereg_replace($index_parse_regexp, "\\1", $snmp_index[$i]["oid"]); } } 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($snmp_queries["fields"])) { if ((!isset($field_array["oid"])) && ($field_array["source"] == "index")) { for ($i=0; $i<sizeof($snmp_index); $i++) { debug_log_insert("data_query", "Inserting index data [value='" . $snmp_index[$i]["value"] . "']"); db_execute("replace into host_snmp_cache (host_id, snmp_query_id, field_name, field_value, snmp_index, oid) values ('$host_id','$snmp_query_id','$field_name','" . $snmp_index[$i]["value"] . "','" . $snmp_index[$i]["value"] . "','')"); } }else if (($field_array["method"] == "get") && ($field_array["direction"] == "input")) { debug_log_insert("data_query", "Located input field '$field_name' [get]"); if ($field_array["source"] == "value") { for ($i=0; $i<sizeof($snmp_index); $i++) { $oid = $field_array["oid"] . "." . $snmp_index[$i]["value"]; $value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid, $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_WEBUI); debug_log_insert("data_query", "Executing SNMP get for data @ '$oid' [value='$value']"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ('$host_id','$snmp_query_id','$field_name','$value','" . $snmp_index[$i]["value"] . "','$oid')"); } } }else if (($field_array["method"] == "walk") && ($field_array["direction"] == "input")) { debug_log_insert("data_query", "Located input field '$field_name' [walk]"); $snmp_data = array(); $snmp_data = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $field_array["oid"], $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"], read_config_option("snmp_retries"), SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP walk for data @ '" . $field_array["oid"] . "'"); if ($field_array["source"] == "value") { for ($i=0; $i<sizeof($snmp_data); $i++) { $snmp_index = ereg_replace($index_parse_regexp, "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . ".$snmp_index"; if ($field_name == "ifOperStatus") { if ((substr_count(strtolower($snmp_data[$i]["value"]), "down")) || ($snmp_data[$i]["value"] == "2")) { $snmp_data[$i]["value"] = "Down"; }else if ((substr_count(strtolower($snmp_data[$i]["value"]), "up")) || ($snmp_data[$i]["value"] == "1")) { $snmp_data[$i]["value"] = "Up"; }else{ $snmp_data[$i]["value"] = "Testing"; } } debug_log_insert("data_query", "Found item [$field_name='" . $snmp_data[$i]["value"] . "'] index: $snmp_index [from value]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ('$host_id','$snmp_query_id','$field_name','" . $snmp_data[$i]["value"] . "','$snmp_index','$oid')"); } }elseif (substr($field_array["source"], 0, 11) == "OID/REGEXP:") { for ($i=0; $i<sizeof($snmp_data); $i++) { $value = ereg_replace(str_replace("OID/REGEXP:", "", $field_array["source"]), "\\1", $snmp_data[$i]["oid"]); if (isset($snmp_queries["oid_index_parse"])) { $snmp_index = ereg_replace($index_parse_regexp, "\\1", $snmp_data[$i]["oid"]); }else if ((isset($snmp_data[$i]["value"])) && ($snmp_data[$i]["value"] != "")) { $snmp_index = $snmp_data[$i]["value"]; } /* correct bogus index value */ /* found in some devices such as an EMC Cellera */ if ($snmp_index == 0) { $snmp_index = 1; } $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", "Found item [$field_name='$value'] index: $snmp_index [from regexp oid parse]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ('$host_id','$snmp_query_id','$field_name','$value','$snmp_index','$oid')"); } }elseif (substr($field_array["source"], 0, 13) == "VALUE/REGEXP:") { for ($i=0; $i<sizeof($snmp_data); $i++) { $value = ereg_replace(str_replace("VALUE/REGEXP:", "", $field_array["source"]), "\\1", $snmp_data[$i]["value"]); $snmp_index = ereg_replace($index_parse_regexp, "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", "Found item [$field_name='$value'] index: $snmp_index [from regexp value parse]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ('$host_id','$snmp_query_id','$field_name','$value','$snmp_index','$oid')"); } } } } return true; }
/** * verify a given index_order * @param array $raw_xml - parsed XML array * @return bool - index_order field valid */ function verify_index_order($raw_xml) { /* invalid xml check */ if (!is_array($raw_xml) || sizeof($raw_xml) == 0) { debug_log_insert("data_query", "Error parsing XML file into an array."); return false; } $xml_inputs = array(); /* 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 */ array_push($xml_inputs, $field_name); } } $all_index_order_fields_found = true; /* 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 < count($index_order_array); $i++) { $all_index_order_fields_found = $all_index_order_fields_found && in_array($index_order_array[$i], $xml_inputs); } } else { /* the xml file does not contain an index order */ } return $all_index_order_fields_found; }
function host_new_graphs_save() { $selected_graphs_array = unserialize(stripslashes($_POST["selected_graphs_array"])); /* form an array that contains all of the data on the previous form */ while (list($var, $val) = each($_POST)) { if (preg_match("/^g_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["graph_template"][$matches[3]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["graph_template"][$matches[3]] = $val; } } elseif (preg_match("/^gi_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: graph_template_input_id, 4:field_name */ /* ================= input validation ================= */ input_validate_input_number($matches[3]); /* ==================================================== */ /* we need to find out which graph items will be affected by saving this particular item */ $item_list = db_fetch_assoc("select\n\t\t\t\tgraph_template_item_id\n\t\t\t\tfrom graph_template_input_defs\n\t\t\t\twhere graph_template_input_id=" . $matches[3]); /* loop through each item affected and update column data */ if (sizeof($item_list) > 0) { foreach ($item_list as $item) { if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["graph_template_item"][$item["graph_template_item_id"]][$matches[4]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["graph_template_item"][$item["graph_template_item_id"]][$matches[4]] = $val; } } } } elseif (preg_match("/^d_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["data_template"][$matches[3]][$matches[4]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["data_template"][$matches[3]][$matches[4]] = $val; } } elseif (preg_match("/^c_(\\d+)_(\\d+)_(\\d+)_(\\d+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:data_input_field_id */ if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["custom_data"][$matches[3]][$matches[4]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["custom_data"][$matches[3]][$matches[4]] = $val; } } elseif (preg_match("/^di_(\\d+)_(\\d+)_(\\d+)_(\\d+)_(\\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:local_data_template_rrd_id, 5:field_name */ if (empty($matches[1])) { /* this is a new graph from template field */ $values["cg"][$matches[2]]["data_template_item"][$matches[4]][$matches[5]] = $val; } else { /* this is a data query field */ $values["sg"][$matches[1]][$matches[2]]["data_template_item"][$matches[4]][$matches[5]] = $val; } } } debug_log_clear("new_graphs"); while (list($form_type, $form_array) = each($selected_graphs_array)) { $current_form_type = $form_type; while (list($form_id1, $form_array2) = each($form_array)) { /* enumerate information from the arrays stored in post variables */ if ($form_type == "cg") { $graph_template_id = $form_id1; } elseif ($form_type == "sg") { while (list($form_id2, $form_array3) = each($form_array2)) { $snmp_index_array = $form_array3; $snmp_query_array["snmp_query_id"] = $form_id1; $snmp_query_array["snmp_index_on"] = get_best_data_query_index_type($_POST["host_id"], $form_id1); $snmp_query_array["snmp_query_graph_id"] = $form_id2; } $graph_template_id = db_fetch_cell("select graph_template_id from snmp_query_graph where id=" . $snmp_query_array["snmp_query_graph_id"]); } if ($current_form_type == "cg") { $return_array = create_complete_graph_from_template($graph_template_id, $_POST["host_id"], "", $values["cg"]); debug_log_insert("new_graphs", "Created graph: " . get_graph_title($return_array["local_graph_id"])); /* lastly push host-specific information to our data sources */ if (sizeof($return_array["local_data_id"])) { # we expect at least one data source associated foreach ($return_array["local_data_id"] as $item) { push_out_host($_POST["host_id"], $item); } } else { debug_log_insert("new_graphs", "ERROR: no Data Source associated. Check Template"); } } elseif ($current_form_type == "sg") { while (list($snmp_index, $true) = each($snmp_index_array)) { $snmp_query_array["snmp_index"] = decode_data_query_index($snmp_index, $snmp_query_array["snmp_query_id"], $_POST["host_id"]); $return_array = create_complete_graph_from_template($graph_template_id, $_POST["host_id"], $snmp_query_array, $values["sg"][$snmp_query_array["snmp_query_id"]]); debug_log_insert("new_graphs", "Created graph: " . get_graph_title($return_array["local_graph_id"])); /* lastly push host-specific information to our data sources */ if (sizeof($return_array["local_data_id"])) { # we expect at least one data source associated foreach ($return_array["local_data_id"] as $item) { push_out_host($_POST["host_id"], $item); } } else { debug_log_insert("new_graphs", "ERROR: no Data Source associated. Check Template"); } } } } } }
/** rewrite_snmp_enum_value - returns rewritten $value based on rewrite map @param string $field_name - name of field being rewritten, used for cache purpuses @param string $value - value to be translated @param string $map - translation map in serialize() form @return string - rewritten value if possible, original one otherwise */ function rewrite_snmp_enum_value($field_name, $value=NULL, $map=NULL) { static $mapcache = array(); if ($field_name === NULL) { $mapcache = array(); return NULL; } if(is_array($map)){ # produced from XML, needs to be reformatted $newmap = array(); foreach($map as $index => $item){ if(!isset($item['match']) || !isset($item['replace'])){ debug_log_insert("data_query", "Bogus rewrite_value item found, index='$index'"); continue; } $newmap[$item['match']] = $item['replace']; } $map = $newmap; }else{ # XML contains serialized filter set $map = unserialize($map); } if ($map === FALSE || !is_array($map)) { debug_log_insert("data_query", __("Cannot parse translation map (rewrite_value)")); return $value; } if (!isset($mapcache[$field_name])) { $mapcache[$field_name] = array(); foreach ($map as $src => $dst) { if (preg_match('/^REGEXP(NC)?:(.*)$/', $src, $matches)) { if($matches[1] == "NC") $src = '/' . str_replace('/', '\/', $matches[2]) . '/i'; else $src = '/' . str_replace('/', '\/', $matches[1]) . '/'; } else { $src = '/^' . str_replace('/^', '\/', $src) . '$/'; } $mapcache[$field_name][$src] = $dst; } } foreach ($mapcache[$field_name] as $src => $dst) { if (preg_match($src, $value)) { $nvalue = preg_replace($src, $dst, $value); debug_log_insert("data_query", __("rewrite_value: ") . "'$value' => '$nvalue'"); $value = $nvalue; break; } } return $value; }
function api_data_query_snmp_execute_field($host_id, $data_query_field_id) { require_once(CACTI_BASE_PATH . "/include/data_query/data_query_constants.php"); require_once(CACTI_BASE_PATH . "/lib/sys/snmp.php"); require_once(CACTI_BASE_PATH . "/lib/data_query/data_query_info.php"); require_once(CACTI_BASE_PATH . "/lib/device/device_info.php"); /* fetch information about the data query field */ $data_query_field = api_data_query_field_get($data_query_field_id); /* fetch information about the associated device */ $device = api_device_get($host_id); debug_log_insert("data_query", "Walking OID '" . $data_query_field["source"] . "'"); /* walk the base snmp oid to get a raw list of values */ $snmp_output = cacti_snmp_walk( $device["hostname"], $device["snmp_community"], $data_query_field["source"], $device["snmp_version"], $device["snmpv3_auth_username"], $device["snmpv3_auth_password"], $device["snmpv3_auth_protocol"], $device["snmpv3_priv_passphrase"], $device["snmpv3_priv_protocol"], $device["snmp_port"], $device["snmp_timeout"], SNMP_WEBUI); /* make sure some data has been returned */ if (!$snmp_output) { debug_log_insert("data_query", _("No SNMP data returned when walking OID '" . $data_query_field["source"] . "'")); return false; } debug_log_insert("data_query", _("Parsing output using method type '" . $data_query_field["method_type"] . "'")); $values_array = array(); switch ($data_query_field["method_type"]) { /* use the values returned from the snmpwalk without modification */ case DATA_QUERY_FIELD_METHOD_VALUE: foreach ($snmp_output as $row) { $values_array{$row["oid"]} = array("value" => $row["value"], "value_parsed" => $row["value"]); } break; /* apply a regular expression to the values returned from the snmpwalk */ case DATA_QUERY_FIELD_METHOD_VALUE_PARSE: foreach ($snmp_output as $row) { /* a match was found; grab the first hit */ if (ereg($data_query_field["method_value"], $row["value"], $matches)) { $values_array{$row["oid"]} = array("value" => $row["value"], "value_parsed" => $matches[1]); /* no match was found. use an empty string */ }else{ $values_array{$row["oid"]} = array("value" => "", "value_parsed" => ""); } } break; /* use the last N octets of the oid for each value */ case DATA_QUERY_FIELD_METHOD_OID_OCTET: foreach ($snmp_output as $row) { $octets = explode(".", $row["oid"]); $_new_oid = ""; /* start at the sizeof(array)-Nth item, and move forward */ for ($i=$data_query_field["method_value"]; $i>0; $i--) { $_new_oid .= $octets{sizeof($octets)-$i} . ($i > 1 ? "." : ""); } $values_array{$row["oid"]} = array("value" => $row["value"], "value_parsed" => $_new_oid); } break; /* apply a regular expression to the oid's returned from the snmpwalk */ case DATA_QUERY_FIELD_METHOD_OID_PARSE: foreach ($snmp_output as $row) { /* a match was found; grab the first hit */ if (ereg($data_query_field["method_value"], $row["oid"], $matches)) { $values_array{$row["oid"]} = array("value" => $row["value"], "value_parsed" => $matches[1]); /* no match was found. use an empty string */ }else{ $values_array{$row["oid"]} = array("value" => "", "value_parsed" => ""); } } break; } return $values_array; }
function query_snmp_host($host_id, $snmp_query_id) { global $config; include_once($config["library_path"] . "/snmp.php"); $host = db_fetch_row("select hostname,snmp_community,snmp_version,snmp_username,snmp_password,snmp_port,snmp_timeout from host where id=$host_id"); $snmp_queries = get_data_query_array($snmp_query_id); if ((empty($host["hostname"])) || ($snmp_queries == false)) { debug_log_insert("data_query", "Error parsing XML file into an array."); return false; } debug_log_insert("data_query", "XML file parsed ok."); /* fetch specified index at specified OID */ $snmp_index = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $snmp_queries["oid_index"], $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_port"], $host["snmp_timeout"], SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP walk for list of indexes @ '" . $snmp_queries["oid_index"] . "'"); /* no data found; get out */ if (!$snmp_index) { debug_log_insert("data_query", "No SNMP data returned"); return false; } 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($snmp_queries["fields"])) { if (($field_array["method"] == "get") && ($field_array["direction"] == "input")) { debug_log_insert("data_query", "Located input field '$field_name' [get]"); if ($field_array["source"] == "value") { for ($i=0;($i<sizeof($snmp_index));$i++) { $oid = $field_array["oid"] . "." . $snmp_index[$i]["value"]; $value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_port"], $host["snmp_timeout"], SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP get for data @ '$oid' [value='$value']"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ($host_id,$snmp_query_id,'$field_name','$value'," . $snmp_index[$i]["value"] . ",'$oid')"); } } }elseif (($field_array["method"] == "walk") && ($field_array["direction"] == "input")) { debug_log_insert("data_query", "Located input field '$field_name' [walk]"); $snmp_data = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $field_array["oid"], $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_port"], $host["snmp_timeout"], SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP walk for data @ '" . $field_array["oid"] . "'"); if ($field_array["source"] == "value") { for ($i=0;($i<sizeof($snmp_data));$i++) { $snmp_index = ereg_replace('.*\.([0-9]+)$', "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . ".$snmp_index"; if ($field_name == "ifOperStatus") { if ($snmp_data[$i]["value"] == "down(2)") $snmp_data[$i]["value"] = "Down"; if ($snmp_data[$i]["value"] == "up(1)") $snmp_data[$i]["value"] = "Up"; } debug_log_insert("data_query", "Found item [$field_name='" . $snmp_data[$i]["value"] . "'] index: $snmp_index [from value]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ($host_id,$snmp_query_id,'$field_name','" . $snmp_data[$i]["value"] . "',$snmp_index,'$oid')"); } }elseif (ereg("^OID/REGEXP:", $field_array["source"])) { for ($i=0;($i<sizeof($snmp_data));$i++) { $value = ereg_replace(ereg_replace("^OID/REGEXP:", "", $field_array["source"]), "\\1", $snmp_data[$i]["oid"]); if ((!isset($snmp_data[$i]["value"])) || ($snmp_data[$i]["value"] == "")) { /* do nothing */ } else { $snmp_index = $snmp_data[$i]["value"]; } /* correct bogus index value */ /* found in some devices such as an EMC Cellera */ if ($snmp_index == 0) { $snmp_index = 1; } $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", "Found item [$field_name='$value'] index: $snmp_index [from regexp oid parse]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ($host_id,$snmp_query_id,'$field_name','$value',$snmp_index,'$oid')"); } }elseif (ereg("^VALUE/REGEXP:", $field_array["source"])) { for ($i=0;($i<sizeof($snmp_data));$i++) { $value = ereg_replace(ereg_replace("^VALUE/REGEXP:", "", $field_array["source"]), "\\1", $snmp_data[$i]["value"]); $snmp_index = ereg_replace('.*\.([0-9]+)$', "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", "Found item [$field_name='$value'] index: $snmp_index [from regexp value parse]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ($host_id,$snmp_query_id,'$field_name','$value',$snmp_index,'$oid')"); } } } } return true; }