update_data_source_title_cache($item); } /* modify for multi user end */ } /* add this graph template to the list of associated graph templates for this host */ db_execute("replace into host_graph (host_id,graph_template_id) values (" . $hostId . "," . $templateId . ")"); echo "Graph Added - graph-id: (" . $returnArray["local_graph_id"] . ") - data-source-ids: ({$dataSourceId})\n"; } elseif ($graph_type == "ds") { if ($dsGraph["snmpQueryId"] == "" || $dsGraph["snmpQueryType"] == "" || $dsGraph["snmpField"] == "" || $dsGraph["snmpValue"] == "") { echo "ERROR: For graph-type of 'ds' you must supply more options\n"; display_help(); exit(1); } $snmp_query_array = array(); $snmp_query_array["snmp_query_id"] = $dsGraph["snmpQueryId"]; $snmp_query_array["snmp_index_on"] = get_best_data_query_index_type($hostId, $dsGraph["snmpQueryId"]); $snmp_query_array["snmp_query_graph_id"] = $dsGraph["snmpQueryType"]; $snmp_indexes = db_fetch_assoc("SELECT snmp_index\r\n\t\t\tFROM host_snmp_cache\r\n\t\t\tWHERE host_id=" . $hostId . "\r\n\t\t\tAND snmp_query_id=" . $dsGraph["snmpQueryId"] . "\r\n\t\t\tAND field_name='" . $dsGraph["snmpField"] . "'\r\n\t\t\tAND field_value='" . $dsGraph["snmpValue"] . "'"); if (sizeof($snmp_indexes)) { foreach ($snmp_indexes as $snmp_index) { $snmp_query_array["snmp_index"] = $snmp_index["snmp_index"]; $existsAlready = db_fetch_cell("SELECT id\r\n\t\t\t\t\tFROM graph_local\r\n\t\t\t\t\tWHERE graph_template_id={$templateId}\r\n\t\t\t\t\tAND host_id={$hostId}\r\n\t\t\t\t\tAND snmp_query_id=" . $dsGraph["snmpQueryId"] . "\r\n\t\t\t\t\tAND snmp_index='" . $snmp_query_array["snmp_index"] . "'"); if (isset($existsAlready) && $existsAlready > 0) { if ($graphTitle != "") { db_execute("UPDATE graph_templates_graph\r\n\t\t\t\t\t\t\tSET title = \"{$graphTitle}\"\r\n\t\t\t\t\t\t\tWHERE local_graph_id = {$existsAlready}"); update_graph_title_cache($existsAlready); } $dataSourceId = db_fetch_cell("SELECT\r\n\t\t\t\t\t\tdata_template_rrd.local_data_id\r\n\t\t\t\t\t\tFROM graph_templates_item, data_template_rrd\r\n\t\t\t\t\t\tWHERE graph_templates_item.local_graph_id = " . $existsAlready . "\r\n\t\t\t\t\t\tAND graph_templates_item.task_item_id = data_template_rrd.id\r\n\t\t\t\t\t\tLIMIT 1"); /* modify for multi user start */ if ($dataTitle != "") { db_execute("UPDATE data_template_data\r\n SET name=\"{$dataTitle}\"\r\n WHERE local_data_id=" . $dataSourceId);
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 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 create_ds_graphs($args) { $hostId = $args["hostId"]; $hostTemplateId = $args["hostTemplateId"]; $description = $args["description"]; $queryTypeIds = $args["queryTypeIds"]; $snmpQueryId = $args["snmpQueryId"]; if (!isset($hostId) || !isset($description) || !isset($queryTypeIds) || !isset($snmpQueryId)) { echo "ERROR: create_ds_graph: Missing required arguments\n"; exit(1); } /* Check if host has associated data query */ $host_data_query = "SELECT snmp_query_id\n FROM host_snmp_query\n WHERE host_id='{$hostId}'\n AND snmp_query_id='{$snmpQueryId}'"; $snmpQuery = db_fetch_cell($host_data_query); if (!$snmpQuery) { // The query is not yet int the database. Insert it debug("Inserting missing host_snmp_query"); $insertQuery = "REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) \n VALUES ({$hostId},{$snmpQueryId},2)"; $r = db_execute($insertQuery); if (!$r) { echo "ERROR: DB operation failed for {$insertQuery}\n"; return 0; } // recache snmp data debug("Running Data query for new query id: {$snmpQueryId}"); run_data_query($hostId, $snmpQueryId); } $snmpQueryArray = array(); $snmpQueryArray["snmp_query_id"] = $snmpQueryId; $snmpQueryArray["snmp_index_on"] = get_best_data_query_index_type($hostId, $snmpQueryId); $indexes_query = "SELECT snmp_index\n FROM host_snmp_cache\n WHERE host_id='{$hostId}'\n AND snmp_query_id='{$snmpQueryId}'"; if (isset($args["snmpCriteria"]) && $args["snmpCriteria"] != "") { $indexes_query .= " AND " . $args["snmpCriteria"]; } $snmpIndexes = db_fetch_assoc($indexes_query); if (sizeof($snmpIndexes)) { $graphsCreated = 0; $graphs = db_fetch_assoc("SELECT id, snmp_index, graph_template_id\n FROM graph_local\n WHERE host_id={$hostId}\n AND snmp_query_id={$snmpQueryId}"); foreach ($graphs as $row) { $graphsBySnmpIndex[$row["snmp_index"]][$row["graph_template_id"]] = $row["id"]; } foreach ($queryTypeIds as $queryTypeId => $templateId) { $snmpQueryArray["snmp_query_graph_id"] = $queryTypeId; foreach ($snmpIndexes as $row) { $snmpIndex = $row["snmp_index"]; if (isset($graphsBySnmpIndex[$snmpIndex][$templateId])) { $graphId = $graphsBySnmpIndex[$snmpIndex][$templateId]; debug("{$description}: Graph already exists: ({$graphId})"); continue; } $snmpQueryArray["snmp_index"] = $snmpIndex; $empty = array(); $returnArray = create_complete_graph_from_template($templateId, $hostId, $snmpQueryArray, $empty); echo "{$description}: Added Graph id: " . $returnArray["local_graph_id"] . "\n"; $graphsCreated++; } } if ($graphsCreated > 0) { push_out_host($hostId, 0); return $graphsCreated; } } else { debug("{$description}: No rows in query: {$indexes_query}"); } }
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"); } } } } } }
} /* get all object that have to be scanned */ $data_queries = db_fetch_assoc("SELECT " . "`data_local`.`host_id`, " . "`data_local`.`snmp_query_id`, " . "`data_local`.`snmp_index`, " . "`data_template_data`.`local_data_id`, " . "`data_template_data`.`data_input_id`, " . "`data_input_data`.`data_template_data_id`, " . "`data_input_data`.`data_input_field_id`, " . "`data_input_data`.`value` " . "FROM data_local " . "LEFT JOIN data_template_data ON data_local.id=data_template_data.local_data_id " . "LEFT JOIN data_input_fields ON data_template_data.data_input_id = data_input_fields.data_input_id " . "LEFT JOIN data_input_data ON ( " . "data_template_data.id = data_input_data.data_template_data_id " . "AND data_input_fields.id = data_input_data.data_input_field_id " . ") " . $sql_where . " "); /* issue warnings and start message if applicable */ print "WARNING: Do not interrupt this script. Reordering can take quite some time\n"; debug("There are '" . sizeof($data_queries) . "' data query index items to run"); $i = 1; if (sizeof($data_queries)) { foreach ($data_queries as $data_query) { if (!$debug) { print "."; } /* fetch current index_order from data_query XML definition and put it into host_snmp_query */ update_data_query_sort_cache($data_query["host_id"], $data_query["snmp_query_id"]); /* build array required for function call */ $data_query["snmp_index_on"] = get_best_data_query_index_type($data_query["host_id"], $data_query["snmp_query_id"]); /* as we request the output_type, "value" gives the snmp_query_graph_id */ $data_query["snmp_query_graph_id"] = $data_query["value"]; debug("Data Query #'" . $i . "' host: '" . $data_query["host_id"] . "' SNMP Query Id: '" . $data_query["snmp_query_id"] . "' Index: " . $data_query["snmp_index"] . "' Index On: " . $data_query["snmp_index_on"]); update_snmp_index_order($data_query); $i++; } } /** * perform sql updates for all required tables for new index_sort_order * @arg array $snmp_query_array * $host_id * snmp_query_id * snmp_index_on * snmp_query_graph_id * snmp_index
function createDQGraph($snmp_query_array, $graphTitle, $force) { /* is this data query already associated (independent of the reindex method) */ $exists_already = db_fetch_cell("SELECT COUNT(device_id) FROM device_snmp_query WHERE device_id=" . $snmp_query_array["device_id"] . " AND snmp_query_id=" . $snmp_query_array["snmp_query_id"]); if ((isset($exists_already)) && ($exists_already > 0)) { /* yes: do nothing, everything's fine */ }else{ db_execute("REPLACE INTO device_snmp_query (device_id,snmp_query_id,reindex_method) " . "VALUES (" . $snmp_query_array["device_id"] . "," . $snmp_query_array["snmp_query_id"] . "," . $snmp_query_array["reindex_method"] . ")"); /* recache snmp data, this is time consuming, * but should happen only once even if multiple graphs * are added for the same data query * because we checked above, if dq was already associated */ run_data_query($snmp_query_array["device_id"], $snmp_query_array["snmp_query_id"]); } $snmp_query_array["snmp_index_on"] = get_best_data_query_index_type($snmp_query_array["device_id"], $snmp_query_array["snmp_query_id"]); $snmp_indexes = db_fetch_assoc("SELECT snmp_index " . "FROM device_snmp_cache " . "WHERE device_id=" . $snmp_query_array["device_id"] . " " . "AND snmp_query_id=" . $snmp_query_array["snmp_query_id"] . " " . "AND field_name='" . $snmp_query_array["snmp_field"] . "' " . "AND field_value='" . $snmp_query_array["snmp_value"] . "'"); if (sizeof($snmp_indexes)) { foreach ($snmp_indexes as $snmp_index) { $snmp_query_array["snmp_index"] = $snmp_index["snmp_index"]; $existsAlready = db_fetch_cell("SELECT id " . "FROM graph_local " . "WHERE graph_template_id=" . $snmp_query_array["graph_template_id"] . " " . "AND device_id=" . $snmp_query_array["device_id"] . " " . "AND snmp_query_id=" . $snmp_query_array["snmp_query_id"] . " " . "AND snmp_index='" . $snmp_query_array["snmp_index"] . "'"); if (isset($existsAlready) && $existsAlready > 0) { $dataSourceId = db_fetch_cell("SELECT data_template_rrd.local_data_id FROM graph_templates_item, data_template_rrd WHERE graph_templates_item.local_graph_id = " . $existsAlready . " AND graph_templates_item.task_item_id = data_template_rrd.id LIMIT 1"); echo __("NOTE: Not Adding Graph - this graph already exists - graph-id: (%d) - data-source-id: (%d)", $existsAlready, $dataSourceId) . "\n"; continue; } $empty = array(); /* Suggested Values are not been implemented */ $returnArray = create_complete_graph_from_template($snmp_query_array["graph_template_id"], $snmp_query_array["device_id"], $snmp_query_array, $empty); if ($graphTitle != "") { db_execute("UPDATE graph_templates_graph " . "SET title='" . $graphTitle ."' " . "WHERE local_graph_id=" . $returnArray["local_graph_id"]); update_graph_title_cache($returnArray["local_graph_id"]); } $dataSourceId = db_fetch_cell("SELECT " . "data_template_rrd.local_data_id " . "FROM graph_templates_item, data_template_rrd " . "WHERE graph_templates_item.local_graph_id = " . $returnArray["local_graph_id"] . " " . "AND graph_templates_item.task_item_id = data_template_rrd.id " . "LIMIT 1"); foreach($returnArray["local_data_id"] as $item) { push_out_device($snmp_query_array["device_id"], $item); $dataSourceId .= (strlen($dataSourceId) ? ", " : "") . $item; } echo __("Graph Added - graph-id: (%d) - data-source-ids: (%d)", $returnArray["local_graph_id"], $dataSourceId) . "\n"; } }else{ echo __("ERROR: Could not find snmp-field %s (%d) for device-id %d (%s)", $snmp_query_array["snmp_field"], $snmp_query_array["snmp_value"], $snmp_query_array["device_id"], $devices[$snmp_query_array["device_id"]]["hostname"]) . "\n"; echo __("Try php -q graph_list.php --device-id=%s --list-snmp-fields", $snmp_query_array["device_id"]) . "\n"; exit(1); } }
function create_ds_graphs($args) { $hostId = $args["hostId"]; $hostTemplateId = $args["hostTemplateId"]; $description = $args["description"]; $queryTypeIds = $args["queryTypeIds"]; $snmpQueryId = $args["snmpQueryId"]; $ignoreIntsLike = $args["ignoreIntsLike"]; if (!isset($hostId) || !isset($description) || !isset($queryTypeIds) || !isset($snmpQueryId)) { echo "ERROR: create_ds_graph: Missing required arguments\n"; exit(1); } /* Check if host has associated data query */ $host_data_query = "SELECT snmp_query_id\n FROM host_snmp_query\n WHERE host_id='{$hostId}'\n AND snmp_query_id='{$snmpQueryId}'"; $snmpQuery = db_fetch_cell($host_data_query); if (!$snmpQuery) { // The query is not yet int the database. Insert it debug("Inserting missing host_snmp_query"); $insertQuery = "REPLACE INTO host_snmp_query (host_id,snmp_query_id,reindex_method) \n VALUES ({$hostId},{$snmpQueryId},2)"; $r = db_execute($insertQuery); if (!$r) { echo "ERROR: DB operation failed for {$insertQuery}\n"; return 0; } // recache snmp data debug("Running Data query for new query id: {$snmpQueryId}"); run_data_query($hostId, $snmpQueryId); } $snmpQueryArray = array(); $snmpQueryArray["snmp_query_id"] = $snmpQueryId; $snmpQueryArray["snmp_index_on"] = get_best_data_query_index_type($hostId, $snmpQueryId); $indexes_query = "SELECT snmp_index\n FROM host_snmp_cache\n WHERE host_id='{$hostId}'\n AND snmp_query_id='{$snmpQueryId}'"; if (isset($args["snmpCriteria"]) && $args["snmpCriteria"] != "") { $indexes_query .= " AND " . $args["snmpCriteria"]; } $snmpIndexes = db_fetch_assoc($indexes_query); // Interfaces to ignore if ($snmpQueryId == 1 && count($snmpIndexes) && isset($ignoreIntsLike) && count($ignoreIntsLike)) { $ignQuery = "SELECT snmp_index\n FROM host_snmp_cache\n WHERE host_id='{$hostId}'\n AND snmp_query_id=1\n AND field_name='ifDescr'"; $patts = array(); foreach ($ignoreIntsLike as $patt) { array_push($patts, "field_value LIKE '{$patt}'"); } $crit = implode(' OR ', $patts); $ignQuery .= " AND ({$crit})"; $ignIndexes = db_fetch_assoc($ignQuery); // Make into an associative array for faster lookups $ignHash = array(); foreach ($ignIndexes as $row) { $ignHash[$row["snmp_index"]] = TRUE; } if (count($ignHash)) { // Now exclude the indexes that matched the ignore patterns $temparr = array(); foreach ($snmpIndexes as $row) { if (!isset($ignHash[$row["snmp_index"]])) { array_push($temparr, $row); } } $snmpIndexes = $temparr; } } if (count($snmpIndexes)) { $graphsCreated = 0; $graphs = db_fetch_assoc("SELECT id, snmp_index, graph_template_id\n FROM graph_local\n WHERE host_id={$hostId}\n AND snmp_query_id={$snmpQueryId}"); foreach ($graphs as $row) { $graphsBySnmpIndex[$row["snmp_index"]][$row["graph_template_id"]] = $row["id"]; } foreach ($queryTypeIds as $queryTypeId => $templateId) { $snmpQueryArray["snmp_query_graph_id"] = $queryTypeId; foreach ($snmpIndexes as $row) { $snmpIndex = $row["snmp_index"]; if (isset($graphsBySnmpIndex[$snmpIndex][$templateId])) { $graphId = $graphsBySnmpIndex[$snmpIndex][$templateId]; debug("{$description}: Graph already exists: ({$graphId})"); continue; } $snmpQueryArray["snmp_index"] = $snmpIndex; $empty = array(); $returnArray = create_complete_graph_from_template($templateId, $hostId, $snmpQueryArray, $empty); echo "{$description}: Added Graph id: " . $returnArray["local_graph_id"] . "\n"; $graphsCreated++; } } if ($graphsCreated > 0) { push_out_host($hostId, 0); return $graphsCreated; } } else { debug("{$description}: No rows in query: {$indexes_query}"); } }