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 = get_data_query_row_index($data_query_id, $host_id, $field["index_type"], $field["index_value"]); 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 */ api_data_source_title_cache_update($local_data_id); } }
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 duplicate_data_source($_local_data_id, $_data_template_id, $data_source_title) { global $struct_data_source, $struct_data_source_item; if (!empty($_local_data_id)) { $data_local = db_fetch_row("select * from data_local where id=$_local_data_id"); $data_template_data = db_fetch_row("select * from data_template_data where local_data_id=$_local_data_id"); $data_template_rrds = db_fetch_assoc("select * from data_template_rrd where local_data_id=$_local_data_id"); $data_input_datas = db_fetch_assoc("select * from data_input_data where data_template_data_id=" . $data_template_data["id"]); $data_template_data_rras = db_fetch_assoc("select * from data_template_data_rra where data_template_data_id=" . $data_template_data["id"]); /* create new entry: data_local */ $save["id"] = 0; $save["data_template_id"] = $data_local["data_template_id"]; $save["host_id"] = $data_local["host_id"]; $save["snmp_query_id"] = $data_local["snmp_query_id"]; $save["snmp_index"] = $data_local["snmp_index"]; $local_data_id = sql_save($save, "data_local"); $data_template_data["name"] = str_replace("<ds_title>", $data_template_data["name"], $data_source_title); }elseif (!empty($_data_template_id)) { $data_template = db_fetch_row("select * from data_template where id=$_data_template_id"); $data_template_data = db_fetch_row("select * from data_template_data where data_template_id=$_data_template_id and local_data_id=0"); $data_template_rrds = db_fetch_assoc("select * from data_template_rrd where data_template_id=$_data_template_id and local_data_id=0"); $data_input_datas = db_fetch_assoc("select * from data_input_data where data_template_data_id=" . $data_template_data["id"]); $data_template_data_rras = db_fetch_assoc("select * from data_template_data_rra where data_template_data_id=" . $data_template_data["id"]); /* create new entry: data_template */ $save["id"] = 0; $save["hash"] = get_hash_data_template(0); $save["name"] = str_replace("<template_title>", $data_template["name"], $data_source_title); $data_template_id = sql_save($save, "data_template"); } unset($save); unset($struct_data_source["rra_id"]); unset($struct_data_source["data_source_path"]); reset($struct_data_source); /* create new entry: data_template_data */ $save["id"] = 0; $save["local_data_id"] = (isset($local_data_id) ? $local_data_id : 0); $save["local_data_template_data_id"] = (isset($data_template_data["local_data_template_data_id"]) ? $data_template_data["local_data_template_data_id"] : 0); $save["data_template_id"] = (!empty($_local_data_id) ? $data_template_data["data_template_id"] : $data_template_id); $save["name_cache"] = $data_template_data["name_cache"]; while (list($field, $array) = each($struct_data_source)) { $save{$field} = $data_template_data{$field}; if ($array["flags"] != "ALWAYSTEMPLATE") { $save{"t_" . $field} = $data_template_data{"t_" . $field}; } } $data_template_data_id = sql_save($save, "data_template_data"); /* create new entry(s): data_template_rrd */ if (sizeof($data_template_rrds) > 0) { foreach ($data_template_rrds as $data_template_rrd) { unset($save); reset($struct_data_source_item); $save["id"] = 0; $save["local_data_id"] = (isset($local_data_id) ? $local_data_id : 0); $save["local_data_template_rrd_id"] = (isset($data_template_rrd["local_data_template_rrd_id"]) ? $data_template_rrd["local_data_template_rrd_id"] : 0); $save["data_template_id"] = (!empty($_local_data_id) ? $data_template_rrd["data_template_id"] : $data_template_id); while (list($field, $array) = each($struct_data_source_item)) { $save{$field} = $data_template_rrd{$field}; } $data_template_rrd_id = sql_save($save, "data_template_rrd"); } } /* create new entry(s): data_input_data */ if (sizeof($data_input_datas) > 0) { foreach ($data_input_datas as $data_input_data) { db_execute("insert into data_input_data (data_input_field_id,data_template_data_id,t_value,value) values (" . $data_input_data["data_input_field_id"] . ",$data_template_data_id,'" . $data_input_data["t_value"] . "','" . $data_input_data["value"] . "')"); } } /* create new entry(s): data_template_data_rra */ if (sizeof($data_template_data_rras) > 0) { foreach ($data_template_data_rras as $data_template_data_rra) { db_execute("insert into data_template_data_rra (data_template_data_id,rra_id) values ($data_template_data_id, " . $data_template_data_rra["rra_id"] . ")"); } } if (!empty($_local_data_id)) { api_data_source_title_cache_update($local_data_id); } }
function api_data_source_host_update($data_source_id, $host_id) { /* sanity checks */ validate_id_die($data_source_id, "data_source_id"); validate_id_die($host_id, "host_id", true); db_execute("UPDATE data_source SET host_id = " . sql_sanitize($host_id) . " WHERE id = " . sql_sanitize($data_source_id)); /* make sure that host variables in the title stay up to date */ api_data_source_title_cache_update($data_source_id); }
function form_actions() { global $colors, $ds_actions; /* if we are to save this form, instead of display it */ if (isset($_POST["selected_items"])) { $selected_items = unserialize(stripslashes($_POST["selected_items"])); if ($_POST["drp_action"] == "1") { /* delete */ if (!isset($_POST["delete_type"])) { $_POST["delete_type"] = 1; } switch ($_POST["delete_type"]) { case '2': /* delete all graph items tied to this data source */ $data_source_items = db_fetch_assoc("select id from data_source_item where " . array_to_sql_or($selected_items, "data_source_id")); /* loop through each data source item */ if (sizeof($data_source_items) > 0) { foreach ($data_source_items as $item) { db_execute("delete from graph_item where data_source_item_id = " . $item["id"]); } } break; case '3': /* delete all graphs tied to this data source */ $graphs = db_fetch_assoc("select distinct\n\t\t\t\t\t\tgraph.id\n\t\t\t\t\t\tfrom data_source_item,graph_item,graph\n\t\t\t\t\t\twhere graph_item.data_source_item_id=data_source_item.id\n\t\t\t\t\t\tand graph_item.graph_id=graph.id\n\t\t\t\t\t\tand " . array_to_sql_or($selected_items, "data_source_item.data_source_id")); if (sizeof($graphs) > 0) { foreach ($graphs as $graph) { api_graph_remove($graph["id"]); } } break; } for ($i = 0; $i < count($selected_items); $i++) { api_data_source_remove($selected_items[$i]); } } elseif ($_POST["drp_action"] == "2") { /* change graph template */ for ($i = 0; $i < count($selected_items); $i++) { change_data_template($selected_items[$i], $_POST["data_template_id"]); } } elseif ($_POST["drp_action"] == "3") { /* change host */ for ($i = 0; $i < count($selected_items); $i++) { db_execute("update data_local set host_id=" . $_POST["host_id"] . " where id=" . $selected_items[$i]); push_out_host($_POST["host_id"], $selected_items[$i]); api_data_source_title_cache_update($selected_items[$i]); } } elseif ($_POST["drp_action"] == "4") { /* duplicate */ for ($i = 0; $i < count($selected_items); $i++) { duplicate_data_source($selected_items[$i], 0, $_POST["title_format"]); } } elseif ($_POST["drp_action"] == "5") { /* data source -> data template */ for ($i = 0; $i < count($selected_items); $i++) { data_source_to_data_template($selected_items[$i], $_POST["title_format"]); } } elseif ($_POST["drp_action"] == "6") { /* data source enable */ for ($i = 0; $i < count($selected_items); $i++) { api_data_source_enable($selected_items[$i]); } } elseif ($_POST["drp_action"] == "7") { /* data source disable */ for ($i = 0; $i < count($selected_items); $i++) { api_data_source_disable($selected_items[$i]); } } header("Location: data_sources.php"); exit; } /* setup some variables */ $ds_list = ""; $i = 0; /* loop through each of the graphs selected on the previous page and get more info about them */ while (list($var, $val) = each($_POST)) { if (ereg("^chk_([0-9]+)\$", $var, $matches)) { $ds_list .= "<li>" . api_data_source_title_get($matches[1]) . "<br>"; $ds_array[$i] = $matches[1]; } $i++; } require_once CACTI_BASE_PATH . "/include/top_header.php"; html_start_box("<strong>" . $ds_actions[$_POST["drp_action"]] . "</strong>", "60%", $colors["header_panel_background"], "3", "center", ""); print "<form action='data_sources.php' method='post'>\n"; if ($_POST["drp_action"] == "1") { /* delete */ $graphs = array(); /* find out which (if any) graphs are using this data source, so we can tell the user */ if (isset($ds_array)) { $graphs = db_fetch_assoc("select distinct\n\t\t\t\tgraph.id,\n\t\t\t\tgraph.title_cache\n\t\t\t\tfrom data_source_item,graph_item,graph\n\t\t\t\twhere graph_item.data_source_item_id=data_source_item.id\n\t\t\t\tand graph_item.graph_id=graph.id\n\t\t\t\tand " . array_to_sql_or($ds_array, "data_source_item.data_source_id") . "\n\t\t\t\torder by graph.title_cache"); } print "\t<tr>\n\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\n\t\t\t\t\t<p>" . _("Are you sure you want to delete the following data sources?") . "</p>\n\t\t\t\t\t<p>{$ds_list}</p>\n\t\t\t\t\t"; if (sizeof($graphs) > 0) { print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td class='textArea'><p class='textArea'>The following graphs are using these data sources:</p>\n"; foreach ($graphs as $graph) { print "<strong>" . $graph["title_cache"] . "</strong><br>\n"; } print "<br>"; form_radio_button("delete_type", "3", "1", _("Leave the graphs untouched."), "1"); print "<br>"; form_radio_button("delete_type", "3", "2", _("Delete all <strong>graph items</strong> that reference these data sources."), "1"); print "<br>"; form_radio_button("delete_type", "3", "3", _("Delete all <strong>graphs</strong> that reference these data sources."), "1"); print "<br>"; print "</td></tr>"; } print "\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t"; } elseif ($_POST["drp_action"] == "2") { /* change graph template */ print "\t<tr>\n\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\n\t\t\t\t\t<p>" . _("Choose a data template and click save to change the data template for\n\t\t\t\t\tthe following data souces. Be aware that all warnings will be suppressed during the\n\t\t\t\t\tconversion, so graph data loss is possible.") . "</p>\n\t\t\t\t\t<p>{$ds_list}</p>\n\t\t\t\t\t<p><strong>" . _("New Data Template:") . "</strong><br>"; form_dropdown("data_template_id", db_fetch_assoc("select data_template.id,data_template.name from data_template order by data_template.name"), "name", "id", "", "", "0"); print "</p>\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t"; } elseif ($_POST["drp_action"] == "3") { /* change device */ print "\t<tr>\n\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\n\t\t\t\t\t<p>" . _("Choose a new host for these data sources:") . "</p>\n\t\t\t\t\t<p>{$ds_list}</p>\n\t\t\t\t\t<p><strong>" . _("New Device:") . "</strong><br>"; form_dropdown("host_id", db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host order by description,hostname"), "name", "id", "", "", "0"); print "</p>\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t"; } elseif ($_POST["drp_action"] == "4") { /* duplicate */ print "\t<tr>\n\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\n\t\t\t\t\t<p>" . _("When you click save, the following data sources will be duplicated. You can\n\t\t\t\t\toptionally change the title format for the new data sources.") . "</p>\n\t\t\t\t\t<p>{$ds_list}</p>\n\t\t\t\t\t<p><strong>" . _("Title Format:") . "</strong><br>"; form_text_box("title_format", "<ds_title> (1)", "", "255", "30", "text"); print "</p>\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t"; } elseif ($_POST["drp_action"] == "5") { /* graph -> graph template */ print "\t<tr>\n\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\n\t\t\t\t\t<p>" . _("When you click save, the following data sources will be converted into data templates.\n\t\t\t\t\tYou can optionally change the title format for the new data templates.") . "</p>\n\t\t\t\t\t<p>{$ds_list}</p>\n\t\t\t\t\t<p><strong>" . _("Title Format:") . "</strong><br>"; form_text_box("title_format", "<ds_title> " . _("Template"), "", "255", "30", "text"); print "</p>\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t"; } elseif ($_POST["drp_action"] == "6") { /* data source enable */ print "\t<tr>\n\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\n\t\t\t\t\t<p>When you click yes, the following data sources will be enabled.</p>\n\t\t\t\t\t<p>{$ds_list}</p>\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t"; } elseif ($_POST["drp_action"] == "7") { /* data source disable */ print "\t<tr>\n\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\n\t\t\t\t\t<p>When you click yes, the following data sources will be disabled.</p>\n\t\t\t\t\t<p>{$ds_list}</p>\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t"; } if (!isset($ds_array)) { print "<tr><td bgcolor='#" . $colors["form_alternate1"] . "'><span class='textError'>" . _("You must select at least one data source.") . "</span></td></tr>\n"; $save_html = ""; } else { $save_html = "<input type='image' src='" . html_get_theme_images_path("button_yes.gif") . "' alt='" . _("Save") . "' align='absmiddle'>"; } print "\t<tr>\n\t\t\t<td align='right' bgcolor='#" . $colors["buttonbar_background"] . "'>\n\t\t\t\t<input type='hidden' name='action' value='actions'>\n\t\t\t\t<input type='hidden' name='selected_items' value='" . (isset($ds_array) ? serialize($ds_array) : '') . "'>\n\t\t\t\t<input type='hidden' name='drp_action' value='" . $_POST["drp_action"] . "'>\n\t\t\t\t<a href='data_sources.php'><img src='" . html_get_theme_images_path("button_no.gif") . "' alt='" . _("Cancel") . "' align='absmiddle' border='0'></a>\n\t\t\t\t{$save_html}\n\t\t\t</td>\n\t\t</tr>\n\t\t"; html_end_box(); require_once CACTI_BASE_PATH . "/include/bottom_footer.php"; }