/** create_complete_graph_from_template - creates a graph and all necessary data sources based on a graph template @param int $graph_template_id - the id of the graph template that will be used to create the new graph @param int $device_id - the id of the device to associate the new graph and data sources with @param array $snmp_query_array - if the new data sources are to be based on a data query, specify the necessary data query information here. it must contain the following information: $snmp_query_array["snmp_query_id"] $snmp_query_array["snmp_index_on"] $snmp_query_array["snmp_query_graph_id"] $snmp_query_array["snmp_index"] @param array $suggested_values_array - any additional information to be included in the new graphs or data sources must be included in the array. data is to be included in the following format: $values["cg"][graph_template_id]["graph_template"][field_name] = $value // graph template $values["cg"][graph_template_id]["graph_template_item"][graph_template_item_id][field_name] = $value // graph template item $values["cg"][data_template_id]["data_template"][field_name] = $value // data template $values["cg"][data_template_id]["data_template_item"][data_template_item_id][field_name] = $value // data template item $values["sg"][data_query_id][graph_template_id]["graph_template"][field_name] = $value // graph template (w/ data query) $values["sg"][data_query_id][graph_template_id]["graph_template_item"][graph_template_item_id][field_name] = $value // graph template item (w/ data query) $values["sg"][data_query_id][data_template_id]["data_template"][field_name] = $value // data template (w/ data query) $values["sg"][data_query_id][data_template_id]["data_template_item"][data_template_item_id][field_name] = $value // data template item (w/ data query) */ function create_complete_graph_from_template($graph_template_id, $device_id, $snmp_query_array, &$suggested_values_array) { global $config; include_once(CACTI_BASE_PATH . "/lib/data_query.php"); /* create the graph */ $save["id"] = 0; $save["graph_template_id"] = $graph_template_id; $save["device_id"] = $device_id; $cache_array["local_graph_id"] = sql_save($save, "graph_local"); change_graph_template($cache_array["local_graph_id"], $graph_template_id, true); if (is_array($snmp_query_array)) { /* suggested values for snmp query code */ $suggested_values = db_fetch_assoc("select text,field_name from snmp_query_graph_sv where snmp_query_graph_id=" . $snmp_query_array["snmp_query_graph_id"] . " order by sequence"); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { /* once we find a match; don't try to find more */ if (!isset($suggested_values_graph[$graph_template_id]{$suggested_value["field_name"]})) { $subs_string = substitute_snmp_query_data($suggested_value["text"], $device_id, $snmp_query_array["snmp_query_id"], $snmp_query_array["snmp_index"], read_config_option("max_data_query_field_length"), false); /* if there are no '|' characters, all of the substitutions were successful */ if (!strstr($subs_string, "|query")) { db_execute("update graph_templates_graph set " . $suggested_value["field_name"] . "='" . addslashes($subs_string) . "' where local_graph_id=" . $cache_array["local_graph_id"]); /* once we find a working value, stop */ $suggested_values_graph[$graph_template_id]{$suggested_value["field_name"]} = true; } } } } } /* suggested values: graph */ if (isset($suggested_values_array[$graph_template_id]["graph_template"])) { while (list($field_name, $field_value) = each($suggested_values_array[$graph_template_id]["graph_template"])) { db_execute("update graph_templates_graph set $field_name='$field_value' where local_graph_id=" . $cache_array["local_graph_id"]); } } /* suggested values: graph item */ if (isset($suggested_values_array[$graph_template_id]["graph_template_item"])) { while (list($graph_template_item_id, $field_array) = each($suggested_values_array[$graph_template_id]["graph_template_item"])) { while (list($field_name, $field_value) = each($field_array)) { $graph_item_id = db_fetch_cell("select id from graph_templates_item where local_graph_template_item_id=$graph_template_item_id and local_graph_id=" . $cache_array["local_graph_id"]); db_execute("update graph_templates_item set $field_name='$field_value' where id=$graph_item_id"); } } } update_graph_title_cache($cache_array["local_graph_id"]); /* create each data source */ $data_templates = db_fetch_assoc("select data_template.id, data_template.name, data_template_rrd.data_source_name from (data_template, data_template_rrd, graph_templates_item) where graph_templates_item.task_item_id=data_template_rrd.id and data_template_rrd.data_template_id=data_template.id and data_template_rrd.local_data_id=0 and graph_templates_item.local_graph_id=0 and graph_templates_item.graph_template_id=" . $graph_template_id . " group by data_template.id order by data_template.name"); if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { unset($save); $save["id"] = 0; $save["data_template_id"] = $data_template["id"]; $save["device_id"] = $device_id; $cache_array["local_data_id"]{$data_template["id"]} = sql_save($save, "data_local"); change_data_template($cache_array["local_data_id"]{$data_template["id"]}, $data_template["id"]); $data_template_data_id = db_fetch_cell("select id from data_template_data where local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); if (is_array($snmp_query_array)) { /* suggested values for snmp query code */ $suggested_values = db_fetch_assoc("select text,field_name from snmp_query_graph_rrd_sv where snmp_query_graph_id=" . $snmp_query_array["snmp_query_graph_id"] . " and data_template_id=" . $data_template["id"] . " order by sequence"); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { /* once we find a match; don't try to find more */ if (!isset($suggested_values_ds{$data_template["id"]}{$suggested_value["field_name"]})) { $subs_string = substitute_snmp_query_data($suggested_value["text"], $device_id, $snmp_query_array["snmp_query_id"], $snmp_query_array["snmp_index"], read_config_option("max_data_query_field_length"), false); /* if there are no '|' characters, all of the substitutions were successful */ if (!strstr($subs_string, "|query")) { if (sizeof(db_fetch_row("show columns from data_template_data like '" . $suggested_value["field_name"] . "'"))) { db_execute("update data_template_data set " . $suggested_value["field_name"] . "='" . addslashes($subs_string) . "' where local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); } /* once we find a working value, stop */ $suggested_values_ds{$data_template["id"]}{$suggested_value["field_name"]} = true; if ((sizeof(db_fetch_row("show columns from data_template_rrd like '" . $suggested_value["field_name"] . "'"))) && (!substr_count($subs_string, "|"))) { db_execute("update data_template_rrd set " . $suggested_value["field_name"] . "='" . $subs_string . "' where local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); } } } } } } if (is_array($snmp_query_array)) { $data_input_field = array_rekey(db_fetch_assoc("SELECT data_input_fields.id, data_input_fields.type_code FROM (snmp_query,data_input,data_input_fields) WHERE snmp_query.data_input_id=data_input.id AND data_input.id=data_input_fields.data_input_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') AND snmp_query.id=" . $snmp_query_array["snmp_query_id"]), "type_code", "id"); $snmp_cache_value = db_fetch_cell("SELECT field_value FROM device_snmp_cache WHERE device_id='$device_id' AND snmp_query_id='" . $snmp_query_array["snmp_query_id"] . "' AND field_name='" . $snmp_query_array["snmp_index_on"] . "' AND snmp_index='" . $snmp_query_array["snmp_index"] . "'"); /* save the value to index on (ie. ifindex, ifip, etc) */ db_execute("REPLACE INTO data_input_data (data_input_field_id, data_template_data_id, t_value, value) VALUES (" . $data_input_field["index_type"] . ", $data_template_data_id, '', '" . $snmp_query_array["snmp_index_on"] . "')"); /* save the actual value (ie. 3, 192.168.1.101, etc) */ db_execute("REPLACE INTO data_input_data (data_input_field_id,data_template_data_id,t_value,value) VALUES (" . $data_input_field["index_value"] . ",$data_template_data_id,'','" . addslashes($snmp_cache_value) . "')"); /* set the expected output type (ie. bytes, errors, packets) */ db_execute("REPLACE INTO data_input_data (data_input_field_id,data_template_data_id,t_value,value) VALUES (" . $data_input_field["output_type"] . ",$data_template_data_id,'','" . $snmp_query_array["snmp_query_graph_id"] . "')"); /* now that we have put data into the 'data_input_data' table, update the snmp cache for ds's */ update_data_source_data_query_cache($cache_array["local_data_id"]{$data_template["id"]}); } /* suggested values: data source */ if (isset($suggested_values_array[$graph_template_id]["data_template"]{$data_template["id"]})) { reset($suggested_values_array[$graph_template_id]["data_template"]{$data_template["id"]}); while (list($field_name, $field_value) = each($suggested_values_array[$graph_template_id]["data_template"]{$data_template["id"]})) { db_execute("UPDATE data_template_data SET $field_name='$field_value' WHERE local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); } } /* suggested values: data source item */ if (isset($suggested_values_array[$graph_template_id]["data_template_item"])) { reset($suggested_values_array[$graph_template_id]["data_template_item"]); while (list($data_template_item_id, $field_array) = each($suggested_values_array[$graph_template_id]["data_template_item"])) { while (list($field_name, $field_value) = each($field_array)) { $data_source_item_id = db_fetch_cell("select id from data_template_rrd where local_data_template_rrd_id=$data_template_item_id and local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); db_execute("UPDATE data_template_rrd SET $field_name='$field_value' WHERE id=$data_source_item_id"); } } } /* suggested values: custom data */ if (isset($suggested_values_array[$graph_template_id]["custom_data"]{$data_template["id"]})) { reset($suggested_values_array[$graph_template_id]["custom_data"]{$data_template["id"]}); while (list($data_input_field_id, $field_value) = each($suggested_values_array[$graph_template_id]["custom_data"]{$data_template["id"]})) { db_execute("replace into data_input_data (data_input_field_id,data_template_data_id,t_value,value) values ($data_input_field_id,$data_template_data_id,'','$field_value')"); } } update_data_source_title_cache($cache_array["local_data_id"]{$data_template["id"]}); } } /* connect the dots: graph -> data source(s) */ $template_item_list = db_fetch_assoc("select graph_templates_item.id, data_template_rrd.id as data_template_rrd_id, data_template_rrd.data_template_id from (graph_templates_item,data_template_rrd) where graph_templates_item.task_item_id=data_template_rrd.id and graph_templates_item.graph_template_id=$graph_template_id and local_graph_id=0 and task_item_id>0"); /* loop through each item affected and update column data */ if (sizeof($template_item_list) > 0) { foreach ($template_item_list as $template_item) { $local_data_id = $cache_array["local_data_id"]{$template_item["data_template_id"]}; $graph_template_item_id = db_fetch_cell("select id from graph_templates_item where local_graph_template_item_id=" . $template_item["id"] . " and local_graph_id=" . $cache_array["local_graph_id"]); $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_template_rrd_id=" . $template_item["data_template_rrd_id"] . " and local_data_id=$local_data_id"); if (!empty($data_template_rrd_id)) { db_execute("update graph_templates_item set task_item_id='$data_template_rrd_id' where id=$graph_template_item_id"); } } } /* this will not work until the ds->graph dots are connected */ if (is_array($snmp_query_array)) { update_graph_data_query_cache($cache_array["local_graph_id"]); } # now that we have the id of the new host, we may plugin postprocessing code $save["id"] = $cache_array["local_graph_id"]; $save["graph_template_id"] = $graph_template_id; // attention: unset! if (is_array($snmp_query_array)) { $save["snmp_query_id"] = $snmp_query_array["snmp_query_id"]; $save["snmp_index"] = $snmp_query_array["snmp_index"]; } else { $save["snmp_query_id"] = 0; $save["snmp_index"] = 0; } api_plugin_hook_function('create_complete_graph_from_template', $save); return $cache_array; }
function form_actions() { /* modify for multi user start */ if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) { $rows = db_fetch_assoc("\r\n SELECT graph_local.id FROM graph_local \r\n INNER JOIN host ON graph_local.host_id = host.id\r\n INNER JOIN user_auth_perms ON host.id = user_auth_perms.item_id AND user_auth_perms.user_id = '" . $_SESSION["sess_user_id"] . "' AND user_auth_perms.type = '3'"); foreach ($rows as $row) { $graphs[] = $row["id"]; } } /* modify for multi user end */ global $colors, $graph_actions; /* if we are to save this form, instead of display it */ if (isset($_POST["selected_items"])) { $selected_items = unserialize(stripslashes($_POST["selected_items"])); /* modify for multi user start */ if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) { for ($i = 0; $i < count($selected_items); $i++) { if (!in_array($selected_items[$i], $graphs)) { access_denied(); } } } /* modify for multi user end */ if ($_POST["drp_action"] == "1") { /* delete */ if (!isset($_POST["delete_type"])) { $_POST["delete_type"] = 1; } for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ } switch ($_POST["delete_type"]) { case '2': /* delete all data sources referenced by this graph */ $data_sources = array_rekey(db_fetch_assoc("SELECT data_template_data.local_data_id\r\n\t\t\t\t\t\tFROM (data_template_rrd, data_template_data, graph_templates_item)\r\n\t\t\t\t\t\tWHERE graph_templates_item.task_item_id=data_template_rrd.id\r\n\t\t\t\t\t\tAND data_template_rrd.local_data_id=data_template_data.local_data_id\r\n\t\t\t\t\t\tAND " . array_to_sql_or($selected_items, "graph_templates_item.local_graph_id") . "\r\n\t\t\t\t\t\tAND data_template_data.local_data_id > 0"), "local_data_id", "local_data_id"); if (sizeof($data_sources)) { api_data_source_remove_multi($data_sources); api_plugin_hook_function('data_source_remove', $data_sources); } break; } api_graph_remove_multi($selected_items); api_plugin_hook_function('graphs_remove', $selected_items); } elseif ($_POST["drp_action"] == "2") { /* change graph template */ input_validate_input_number(get_request_var_post("graph_template_id")); for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ change_graph_template($selected_items[$i], $_POST["graph_template_id"], true); } } elseif ($_POST["drp_action"] == "3") { /* duplicate */ for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ duplicate_graph($selected_items[$i], 0, $_POST["title_format"]); } } elseif ($_POST["drp_action"] == "4") { /* graph -> graph template */ for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ graph_to_graph_template($selected_items[$i], $_POST["title_format"]); } } elseif (preg_match("/^tr_([0-9]+)\$/", $_POST["drp_action"], $matches)) { /* place on tree */ input_validate_input_number(get_request_var_post("tree_id")); input_validate_input_number(get_request_var_post("tree_item_id")); for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_tree_item_save(0, $_POST["tree_id"], TREE_ITEM_TYPE_GRAPH, $_POST["tree_item_id"], "", $selected_items[$i], read_graph_config_option("default_rra_id"), 0, 0, 0, false); } } elseif ($_POST["drp_action"] == "5") { /* change host */ input_validate_input_number(get_request_var_post("host_id")); for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ db_execute("update graph_local set host_id=" . $_POST["host_id"] . " where id=" . $selected_items[$i]); update_graph_title_cache($selected_items[$i]); } } elseif ($_POST["drp_action"] == "6") { /* reapply suggested naming */ for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_reapply_suggested_graph_title($selected_items[$i]); update_graph_title_cache($selected_items[$i]); } } elseif ($_POST["drp_action"] == "7") { /* resize graphs */ input_validate_input_number(get_request_var_post("graph_width")); input_validate_input_number(get_request_var_post("graph_height")); for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_resize_graphs($selected_items[$i], $_POST["graph_width"], $_POST["graph_height"]); } } else { api_plugin_hook_function('graphs_action_execute', $_POST['drp_action']); } header("Location: graphs.php"); exit; } /* setup some variables */ $graph_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 (preg_match("/^chk_([0-9]+)\$/", $var, $matches)) { /* ================= input validation ================= */ input_validate_input_number($matches[1]); /* ==================================================== */ /* modify for multi user start */ if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) { if (!in_array($matches[1], $graphs)) { access_denied(); } } /* modify for multi user end */ $graph_list .= "<li>" . get_graph_title($matches[1]) . "</li>"; $graph_array[$i] = $matches[1]; $i++; } } include_once "./include/top_header.php"; /* add a list of tree names to the actions dropdown */ add_tree_names_to_actions_array(); html_start_box("<strong>" . $graph_actions[$_POST["drp_action"]] . "</strong>", "60%", $colors["header_panel"], "3", "center", ""); print "<form action='graphs.php' method='post'>\n"; if (isset($graph_array) && sizeof($graph_array)) { if ($_POST["drp_action"] == "1") { /* delete */ $graphs = array(); /* find out which (if any) data sources are being used by this graph, so we can tell the user */ if (isset($graph_array) && sizeof($graph_array)) { $data_sources = db_fetch_assoc("select\r\n\t\t\t\t\tdata_template_data.local_data_id,\r\n\t\t\t\t\tdata_template_data.name_cache\r\n\t\t\t\t\tfrom (data_template_rrd,data_template_data,graph_templates_item)\r\n\t\t\t\t\twhere graph_templates_item.task_item_id=data_template_rrd.id\r\n\t\t\t\t\tand data_template_rrd.local_data_id=data_template_data.local_data_id\r\n\t\t\t\t\tand " . array_to_sql_or($graph_array, "graph_templates_item.local_graph_id") . "\r\n\t\t\t\t\tand data_template_data.local_data_id > 0\r\n\t\t\t\t\tgroup by data_template_data.local_data_id\r\n\t\t\t\t\torder by data_template_data.name_cache"); } print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be deleted. Please note, Data Source(s) should be deleted only if they are only used by these Graph(s)\r\n\t\t\t\t\t\tand not others.</p>\r\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>"; if (isset($data_sources) && sizeof($data_sources)) { print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td class='textArea'><p class='textArea'>The following Data Source(s) are in use by these Graph(s):</p>\n"; print "<ul>"; foreach ($data_sources as $data_source) { print "<li><strong>" . $data_source["name_cache"] . "</strong></li>\n"; } print "</ul>"; print "<br>"; form_radio_button("delete_type", "1", "1", "Leave the Data Source(s) untouched.", "1"); print "<br>"; form_radio_button("delete_type", "1", "2", "Delete all <strong>Data Source(s)</strong> referenced by these Graph(s).", "1"); print "<br>"; print "</td></tr>"; } print "\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Delete Graph(s)'>"; } elseif ($_POST["drp_action"] == "2") { /* change graph template */ /* modify for multi user start */ $sql_where = ""; if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) { $sql_where = "WHERE graph_templates.name NOT LIKE '%@system'"; } print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>Choose a Graph Template and click \"Continue\" to change the Graph Template for\r\n\t\t\t\t\t\tthe following Graph(s). Be aware that all warnings will be suppressed during the\r\n\t\t\t\t\t\tconversion, so Graph data loss is possible.</p>\r\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\r\n\t\t\t\t\t\t<p><strong>New Graph Template:</strong><br>"; form_dropdown("graph_template_id", db_fetch_assoc("select graph_templates.id,graph_templates.name from graph_templates {$sql_where} order by name"), "name", "id", "", "", "0"); print "</p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t"; /* modify for multi user end */ $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Change Graph Template'>"; } elseif ($_POST["drp_action"] == "3") { /* duplicate */ print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be duplicated. You can\r\n\t\t\t\t\t\toptionally change the title format for the new Graph(s).</p>\r\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\r\n\t\t\t\t\t\t<p><strong>Title Format:</strong><br>"; form_text_box("title_format", "<graph_title> (1)", "", "255", "30", "text"); print "</p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Duplicate Graph(s)'>"; } elseif ($_POST["drp_action"] == "4") { /* graph -> graph template */ print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be converted into Graph Template(s).\r\n\t\t\t\t\t\tYou can optionally change the title format for the new Graph Template(s).</p>\r\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\r\n\t\t\t\t\t\t<p><strong>Title Format:</strong><br>"; form_text_box("title_format", "<graph_title> Template", "", "255", "30", "text"); print "</p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Convert to Graph Template'>"; } elseif (preg_match("/^tr_([0-9]+)\$/", $_POST["drp_action"], $matches)) { /* place on tree */ print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be placed under the Tree Branch selected below.</p>\r\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\r\n\t\t\t\t\t\t<p><strong>Destination Branch:</strong><br>"; grow_dropdown_tree($matches[1], "tree_item_id", "0"); print "</p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n\r\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Place Graph(s) on Tree'>"; } elseif ($_POST["drp_action"] == "5") { /* change host */ /* modify for multi user start */ if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) { $sql_join = "INNER JOIN user_auth_perms ON host.id = user_auth_perms.item_id AND user_auth_perms.user_id = '" . $_SESSION["sess_user_id"] . "' AND user_auth_perms.type = '3'"; } print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>Choose a new Device for these Graph(s) and click \"Continue\"</p>\r\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\r\n\t\t\t\t\t\t<p><strong>New Host:</strong><br>"; form_dropdown("host_id", db_fetch_assoc("select host.id,CONCAT_WS('',host.description,' (',host.hostname,')') as name from host order by description,hostname"), "name", "id", "", "", "0"); print "</p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t"; /* modify for multi user end */ $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Change Graph(s) Associated Device'>"; } elseif ($_POST["drp_action"] == "6") { /* reapply suggested naming to host */ print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will have thier suggested naming convensions\r\n\t\t\t\t\t\trecalculated and applied to the Graph(s).</p>\r\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Reapply Suggested Naming to Graph(s)'>"; } elseif ($_POST["drp_action"] == "7") { /* resize graphs */ print "\t<tr>\r\n\t\t\t\t\t<td class='textArea' bgcolor='#" . $colors["form_alternate1"] . "'>\r\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be resized per your specifications.</p>\r\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\r\n\t\t\t\t\t\t<p><strong>Graph Height:</strong><br>"; form_text_box("graph_height", "", "", "255", "30", "text"); print "</p>\r\n\t\t\t\t\t\t<p><strong>Graph Width:</strong><br>"; form_text_box("graph_width", "", "", "255", "30", "text"); print "</p>\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\n\r\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Resize Selected Graph(s)'>"; } else { $save['drp_action'] = $_POST['drp_action']; $save['graph_list'] = $graph_list; $save['graph_array'] = isset($graph_array) ? $graph_array : array(); api_plugin_hook_function('graphs_action_prepare', $save); $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue'>"; } } else { print "<tr><td bgcolor='#" . $colors["form_alternate1"] . "'><span class='textError'>You must select at least one graph.</span></td></tr>\n"; $save_html = "<input type='button' value='Return' onClick='window.history.back()'>"; } print "\t<tr>\r\n\t\t\t<td align='right' bgcolor='#eaeaea'>\r\n\t\t\t\t<input type='hidden' name='action' value='actions'>\r\n\t\t\t\t<input type='hidden' name='selected_items' value='" . (isset($graph_array) ? serialize($graph_array) : '') . "'>\r\n\t\t\t\t<input type='hidden' name='drp_action' value='" . $_POST["drp_action"] . "'>\r\n\t\t\t\t{$save_html}\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t\t"; html_end_box(); include_once "./include/bottom_footer.php"; }
function form_actions() { global $colors, $graph_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 */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ if (!isset($_POST["delete_type"])) { $_POST["delete_type"] = 1; } switch ($_POST["delete_type"]) { case '2': /* delete all data sources referenced by this graph */ $data_sources = db_fetch_assoc("SELECT " . "data_template_data.local_data_id " . "FROM " . "(data_template_rrd, " . "data_template_data, " . "graph_templates_item) " . "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=" . $selected_items[$i] . " " . "AND data_template_data.local_data_id > 0"); if (sizeof($data_sources) > 0) { foreach ($data_sources as $data_source) { api_data_source_remove($data_source["local_data_id"]); } } break; } api_graph_remove($selected_items[$i]); } }elseif ($_POST["drp_action"] == "2") { /* change graph template */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); input_validate_input_number(get_request_var_post("graph_template_id")); /* ==================================================== */ change_graph_template($selected_items[$i], $_POST["graph_template_id"], true); } }elseif ($_POST["drp_action"] == "3") { /* duplicate */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ duplicate_graph($selected_items[$i], 0, $_POST["title_format"]); } }elseif ($_POST["drp_action"] == "4") { /* graph -> graph template */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ graph_to_graph_template($selected_items[$i], $_POST["title_format"]); } }elseif (ereg("^tr_([0-9]+)$", $_POST["drp_action"], $matches)) { /* place on tree */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); input_validate_input_number(get_request_var_post("tree_id")); input_validate_input_number(get_request_var_post("tree_item_id")); /* ==================================================== */ api_tree_item_save(0, $_POST["tree_id"], TREE_ITEM_TYPE_GRAPH, $_POST["tree_item_id"], "", $selected_items[$i], read_graph_config_option("default_rra_id"), 0, 0, 0, false); } }elseif ($_POST["drp_action"] == "5") { /* change host */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); input_validate_input_number(get_request_var_post("host_id")); /* ==================================================== */ db_execute("update graph_local set host_id=" . $_POST["host_id"] . " where id=" . $selected_items[$i]); update_graph_title_cache($selected_items[$i]); } }elseif ($_POST["drp_action"] == "6") { /* reapply suggested naming */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_reapply_suggested_graph_title($selected_items[$i]); update_graph_title_cache($selected_items[$i]); } }elseif ($_POST["drp_action"] == "7") { /* resize graphs */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_resize_graphs($selected_items[$i], $_POST["graph_width"], $_POST["graph_height"]); } } header("Location: graphs.php"); exit; } /* setup some variables */ $graph_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)) { /* ================= input validation ================= */ input_validate_input_number($matches[1]); /* ==================================================== */ $graph_list .= "<li>" . get_graph_title($matches[1]) . "<br>"; $graph_array[$i] = $matches[1]; } $i++; } include_once("./include/top_header.php"); /* add a list of tree names to the actions dropdown */ add_tree_names_to_actions_array(); html_start_box("<strong>" . $graph_actions{$_POST["drp_action"]} . "</strong>", "60%", $colors["header_panel"], "3", "center", ""); print "<form action='graphs.php' method='post'>\n"; if ($_POST["drp_action"] == "1") { /* delete */ $graphs = array(); /* find out which (if any) data sources are being used by this graph, so we can tell the user */ if (isset($graph_array)) { $data_sources = db_fetch_assoc("select data_template_data.local_data_id, data_template_data.name_cache from (data_template_rrd,data_template_data,graph_templates_item) 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 " . array_to_sql_or($graph_array, "graph_templates_item.local_graph_id") . " and data_template_data.local_data_id > 0 group by data_template_data.local_data_id order by data_template_data.name_cache"); } print " <tr> <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'> <p>Are you sure you want to delete the following graphs?</p> <p>$graph_list</p> "; if (sizeof($data_sources) > 0) { print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td class='textArea'><p class='textArea'>The following data sources are in use by these graphs:</p>\n"; foreach ($data_sources as $data_source) { print "<strong>" . $data_source["name_cache"] . "</strong><br>\n"; } print "<br>"; form_radio_button("delete_type", "1", "1", "Leave the data sources untouched.", "1"); print "<br>"; form_radio_button("delete_type", "1", "2", "Delete all <strong>data sources</strong> referenced by these graphs.", "1"); print "<br>"; print "</td></tr>"; } print " </td> </tr>\n "; }elseif ($_POST["drp_action"] == "2") { /* change graph template */ print " <tr> <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'> <p>Choose a graph template and click save to change the graph template for the following graphs. Be aware that all warnings will be suppressed during the conversion, so graph data loss is possible.</p> <p>$graph_list</p> <p><strong>New Graph Template:</strong><br>"; form_dropdown("graph_template_id",db_fetch_assoc("select graph_templates.id,graph_templates.name from graph_templates order by name"),"name","id","","","0"); print "</p> </td> </tr>\n "; }elseif ($_POST["drp_action"] == "3") { /* duplicate */ print " <tr> <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'> <p>When you click save, the following graphs will be duplicated. You can optionally change the title format for the new graphs.</p> <p>$graph_list</p> <p><strong>Title Format:</strong><br>"; form_text_box("title_format", "<graph_title> (1)", "", "255", "30", "text"); print "</p> </td> </tr>\n "; }elseif ($_POST["drp_action"] == "4") { /* graph -> graph template */ print " <tr> <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'> <p>When you click save, the following graphs will be converted into graph templates. You can optionally change the title format for the new graph templates.</p> <p>$graph_list</p> <p><strong>Title Format:</strong><br>"; form_text_box("title_format", "<graph_title> Template", "", "255", "30", "text"); print "</p> </td> </tr>\n "; }elseif (ereg("^tr_([0-9]+)$", $_POST["drp_action"], $matches)) { /* place on tree */ print " <tr> <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'> <p>When you click save, the following graphs will be placed under the branch selected below.</p> <p>$graph_list</p> <p><strong>Destination Branch:</strong><br>"; grow_dropdown_tree($matches[1], "tree_item_id", "0"); print "</p> </td> </tr>\n <input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n "; }elseif ($_POST["drp_action"] == "5") { /* change host */ print " <tr> <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'> <p>Choose a new host for these graphs:</p> <p>$graph_list</p> <p><strong>New Host:</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> </td> </tr>\n "; }elseif ($_POST["drp_action"] == "6") { /* reapply suggested naming to host */ print " <tr> <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'> <p>When you click save, the following graphs will have thier suggested naming convensions recalculated and applies to the graphs.</p> <p>$graph_list</p> </td> </tr>\n "; }elseif ($_POST["drp_action"] == "7") { /* reapply suggested naming to host */ print " <tr> <td class='textArea' bgcolor='#" . $colors["form_alternate1"]. "'> <p>When you click save, the following graphs will be resized per your specifications.</p> <p>$graph_list</p> <p><strong>Graph Height:</strong><br>"; form_text_box("graph_height", "", "", "255", "30", "text"); print "</p> <p><strong>Graph Width:</strong><br>"; form_text_box("graph_width", "", "", "255", "30", "text"); print "</p> </td> </tr>\n "; } if (!isset($graph_array)) { print "<tr><td bgcolor='#" . $colors["form_alternate1"]. "'><span class='textError'>You must select at least one graph.</span></td></tr>\n"; $save_html = ""; }else{ $save_html = "<input type='image' src='images/button_yes.gif' alt='Save' align='absmiddle'>"; } print " <tr> <td align='right' bgcolor='#eaeaea'> <input type='hidden' name='action' value='actions'> <input type='hidden' name='selected_items' value='" . (isset($graph_array) ? serialize($graph_array) : '') . "'> <input type='hidden' name='drp_action' value='" . $_POST["drp_action"] . "'> <a href='graphs.php'><img src='images/button_no.gif' alt='Cancel' align='absmiddle' border='0'></a> $save_html </td> </tr> "; html_end_box(); include_once("./include/bottom_footer.php"); }
function create_complete_graph_from_template($graph_template_id, $host_id, $snmp_query_array, &$suggested_values_array) { global $config; include_once $config["library_path"] . "/data_query.php"; /* create the graph */ $save["id"] = 0; $save["graph_template_id"] = $graph_template_id; $save["host_id"] = $host_id; $cache_array["local_graph_id"] = sql_save($save, "graph_local"); change_graph_template($cache_array["local_graph_id"], $graph_template_id, true); if (is_array($snmp_query_array)) { /* suggested values for snmp query code */ $suggested_values = db_fetch_assoc("select text,field_name from snmp_query_graph_sv where snmp_query_graph_id=" . $snmp_query_array["snmp_query_graph_id"] . " order by sequence"); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { /* once we find a match; don't try to find more */ if (!isset($suggested_values_graph[$graph_template_id][$suggested_value["field_name"]])) { $subs_string = substitute_snmp_query_data($suggested_value["text"], $host_id, $snmp_query_array["snmp_query_id"], $snmp_query_array["snmp_index"], read_config_option("max_data_query_field_length")); /* if there are no '|' characters, all of the substitutions were successful */ if (!strstr($subs_string, "|query")) { db_execute("update graph_templates_graph set " . $suggested_value["field_name"] . "='" . addslashes($suggested_value["text"]) . "' where local_graph_id=" . $cache_array["local_graph_id"]); /* once we find a working value, stop */ $suggested_values_graph[$graph_template_id][$suggested_value["field_name"]] = true; } } } } } /* suggested values: graph */ if (isset($suggested_values_array[$graph_template_id]["graph_template"])) { while (list($field_name, $field_value) = each($suggested_values_array[$graph_template_id]["graph_template"])) { db_execute("update graph_templates_graph set {$field_name}='{$field_value}' where local_graph_id=" . $cache_array["local_graph_id"]); } } /* suggested values: graph item */ if (isset($suggested_values_array[$graph_template_id]["graph_template_item"])) { while (list($graph_template_item_id, $field_array) = each($suggested_values_array[$graph_template_id]["graph_template_item"])) { while (list($field_name, $field_value) = each($field_array)) { $graph_item_id = db_fetch_cell("select id from graph_templates_item where local_graph_template_item_id={$graph_template_item_id} and local_graph_id=" . $cache_array["local_graph_id"]); db_execute("update graph_templates_item set {$field_name}='{$field_value}' where id={$graph_item_id}"); } } } update_graph_title_cache($cache_array["local_graph_id"]); /* create each data source */ $data_templates = db_fetch_assoc("select\n\t\tdata_template.id,\n\t\tdata_template.name,\n\t\tdata_template_rrd.data_source_name\n\t\tfrom (data_template, data_template_rrd, graph_templates_item)\n\t\twhere graph_templates_item.task_item_id=data_template_rrd.id\n\t\tand data_template_rrd.data_template_id=data_template.id\n\t\tand data_template_rrd.local_data_id=0\n\t\tand graph_templates_item.local_graph_id=0\n\t\tand graph_templates_item.graph_template_id=" . $graph_template_id . "\n\t\tgroup by data_template.id\n\t\torder by data_template.name"); if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { unset($save); $save["id"] = 0; $save["data_template_id"] = $data_template["id"]; $save["host_id"] = $host_id; $cache_array["local_data_id"][$data_template["id"]] = sql_save($save, "data_local"); change_data_template($cache_array["local_data_id"][$data_template["id"]], $data_template["id"]); $data_template_data_id = db_fetch_cell("select id from data_template_data where local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); if (is_array($snmp_query_array)) { /* suggested values for snmp query code */ $suggested_values = db_fetch_assoc("select text,field_name from snmp_query_graph_rrd_sv where snmp_query_graph_id=" . $snmp_query_array["snmp_query_graph_id"] . " and data_template_id=" . $data_template["id"] . " order by sequence"); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { /* once we find a match; don't try to find more */ if (!isset($suggested_values_ds[$data_template["id"]][$suggested_value["field_name"]])) { $subs_string = substitute_snmp_query_data($suggested_value["text"], $host_id, $snmp_query_array["snmp_query_id"], $snmp_query_array["snmp_index"], read_config_option("max_data_query_field_length")); /* if there are no '|' characters, all of the substitutions were successful */ if (!strstr($subs_string, "|query")) { if (sizeof(db_fetch_row("show columns from data_template_data like '" . $suggested_value["field_name"] . "'"))) { db_execute("update data_template_data set " . $suggested_value["field_name"] . "='" . addslashes($suggested_value["text"]) . "' where local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); } /* once we find a working value, stop */ $suggested_values_ds[$data_template["id"]][$suggested_value["field_name"]] = true; if (sizeof(db_fetch_row("show columns from data_template_rrd like '" . $suggested_value["field_name"] . "'")) && !substr_count($subs_string, "|")) { db_execute("update data_template_rrd set " . $suggested_value["field_name"] . "='" . $subs_string . "' where local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); } } } } } } if (is_array($snmp_query_array)) { $data_input_field = array_rekey(db_fetch_assoc("select\n\t\t\t\tdata_input_fields.id,\n\t\t\t\tdata_input_fields.type_code\n\t\t\t\tfrom (snmp_query,data_input,data_input_fields)\n\t\t\t\twhere snmp_query.data_input_id=data_input.id\n\t\t\t\tand data_input.id=data_input_fields.data_input_id\n\t\t\t\tand (data_input_fields.type_code='index_type' or data_input_fields.type_code='index_value' or data_input_fields.type_code='output_type')\n\t\t\t\tand snmp_query.id=" . $snmp_query_array["snmp_query_id"]), "type_code", "id"); $snmp_cache_value = db_fetch_cell("select field_value from host_snmp_cache where host_id='{$host_id}' and snmp_query_id='" . $snmp_query_array["snmp_query_id"] . "' and field_name='" . $snmp_query_array["snmp_index_on"] . "' and snmp_index='" . $snmp_query_array["snmp_index"] . "'"); /* save the value to index on (ie. ifindex, ifip, etc) */ db_execute("replace into data_input_data (data_input_field_id,data_template_data_id,t_value,value) values (" . $data_input_field["index_type"] . ",{$data_template_data_id},'','" . $snmp_query_array["snmp_index_on"] . "')"); /* save the actual value (ie. 3, 192.168.1.101, etc) */ db_execute("replace into data_input_data (data_input_field_id,data_template_data_id,t_value,value) values (" . $data_input_field["index_value"] . ",{$data_template_data_id},'','" . addslashes($snmp_cache_value) . "')"); /* set the expected output type (ie. bytes, errors, packets) */ db_execute("replace into data_input_data (data_input_field_id,data_template_data_id,t_value,value) values (" . $data_input_field["output_type"] . ",{$data_template_data_id},'','" . $snmp_query_array["snmp_query_graph_id"] . "')"); /* now that we have put data into the 'data_input_data' table, update the snmp cache for ds's */ update_data_source_data_query_cache($cache_array["local_data_id"][$data_template["id"]]); } /* suggested values: data source */ if (isset($suggested_values_array[$graph_template_id]["data_template"][$data_template["id"]])) { reset($suggested_values_array[$graph_template_id]["data_template"][$data_template["id"]]); while (list($field_name, $field_value) = each($suggested_values_array[$graph_template_id]["data_template"][$data_template["id"]])) { db_execute("update data_template_data set {$field_name}='{$field_value}' where local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); } } /* suggested values: data source item */ if (isset($suggested_values_array[$graph_template_id]["data_template_item"])) { reset($suggested_values_array[$graph_template_id]["data_template_item"]); while (list($data_template_item_id, $field_array) = each($suggested_values_array[$graph_template_id]["data_template_item"])) { while (list($field_name, $field_value) = each($field_array)) { $data_source_item_id = db_fetch_cell("select id from data_template_rrd where local_data_template_rrd_id={$data_template_item_id} and local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); db_execute("update data_template_rrd set {$field_name}='{$field_value}' where id={$data_source_item_id}"); } } } /* suggested values: custom data */ if (isset($suggested_values_array[$graph_template_id]["custom_data"][$data_template["id"]])) { reset($suggested_values_array[$graph_template_id]["custom_data"][$data_template["id"]]); while (list($data_input_field_id, $field_value) = each($suggested_values_array[$graph_template_id]["custom_data"][$data_template["id"]])) { db_execute("replace into data_input_data (data_input_field_id,data_template_data_id,t_value,value) values ({$data_input_field_id},{$data_template_data_id},'','{$field_value}')"); } } update_data_source_title_cache($cache_array["local_data_id"][$data_template["id"]]); } } /* connect the dots: graph -> data source(s) */ $template_item_list = db_fetch_assoc("select\n\t\tgraph_templates_item.id,\n\t\tdata_template_rrd.id as data_template_rrd_id,\n\t\tdata_template_rrd.data_template_id\n\t\tfrom (graph_templates_item,data_template_rrd)\n\t\twhere graph_templates_item.task_item_id=data_template_rrd.id\n\t\tand graph_templates_item.graph_template_id={$graph_template_id}\n\t\tand local_graph_id=0\n\t\tand task_item_id>0"); /* loop through each item affected and update column data */ if (sizeof($template_item_list) > 0) { foreach ($template_item_list as $template_item) { $local_data_id = $cache_array["local_data_id"][$template_item["data_template_id"]]; $graph_template_item_id = db_fetch_cell("select id from graph_templates_item where local_graph_template_item_id=" . $template_item["id"] . " and local_graph_id=" . $cache_array["local_graph_id"]); $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_template_rrd_id=" . $template_item["data_template_rrd_id"] . " and local_data_id={$local_data_id}"); if (!empty($data_template_rrd_id)) { db_execute("update graph_templates_item set task_item_id='{$data_template_rrd_id}' where id={$graph_template_item_id}"); } } } /* this will not work until the ds->graph dots are connected */ if (is_array($snmp_query_array)) { update_graph_data_query_cache($cache_array["local_graph_id"]); } return $cache_array; }
function form_actions() { global $colors, $graph_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 */ for ($i = 0; $i < count($selected_items); $i++) { if (!isset($_POST["delete_type"])) { $_POST["delete_type"] = 1; } switch ($_POST["delete_type"]) { case '2': /* delete all data sources referenced by this graph */ $data_sources = db_fetch_assoc("select distinct\n\t\t\t\t\t\t\tdata_source.id\n\t\t\t\t\t\t\tfrom data_source_item,data_source,graph_item\n\t\t\t\t\t\t\twhere graph_item.data_source_item_id=data_source_item.id\n\t\t\t\t\t\t\tand data_source_item.data_source_id=data_source.id\n\t\t\t\t\t\t\tand " . array_to_sql_or($selected_items, "graph_item.graph_id") . "\n\t\t\t\t\t\t\torder by data_source.name_cache"); if (sizeof($data_sources) > 0) { foreach ($data_sources as $data_source) { api_data_source_remove($data_source["id"]); } } break; } api_graph_remove($selected_items[$i]); } } elseif ($_POST["drp_action"] == "2") { /* change graph template */ for ($i = 0; $i < count($selected_items); $i++) { change_graph_template($selected_items[$i], $_POST["graph_template_id"], true); } } elseif ($_POST["drp_action"] == "3") { /* duplicate */ for ($i = 0; $i < count($selected_items); $i++) { duplicate_graph($selected_items[$i], 0, $_POST["title_format"]); } } elseif ($_POST["drp_action"] == "4") { /* graph -> graph template */ for ($i = 0; $i < count($selected_items); $i++) { graph_to_graph_template($selected_items[$i], $_POST["title_format"]); } } elseif (ereg("^tr_([0-9]+)\$", $_POST["drp_action"], $matches)) { /* place on tree */ for ($i = 0; $i < count($selected_items); $i++) { api_tree_item_save(0, $_POST["tree_id"], TREE_ITEM_TYPE_GRAPH, $_POST["tree_item_id"], "", $selected_items[$i], read_graph_config_option("default_rra_id"), 0, 0, 0, false); } } elseif ($_POST["drp_action"] == "5") { /* change host */ for ($i = 0; $i < count($selected_items); $i++) { db_execute("update graph set host_id = " . $_POST["host_id"] . " where id = " . $selected_items[$i]); api_graph_title_cache_update($selected_items[$i]); } } elseif ($_POST["drp_action"] == "6") { /* reapply suggested naming */ for ($i = 0; $i < count($selected_items); $i++) { api_reapply_suggested_graph_title($selected_items[$i]); api_graph_title_cache_update($selected_items[$i]); } } elseif ($_POST["drp_action"] == "7") { /* resize graphs */ for ($i = 0; $i < count($selected_items); $i++) { api_graph_resize($selected_items[$i], $_POST["graph_width"], $_POST["graph_height"]); } } header("Location: graphs.php"); exit; } /* setup some variables */ $graph_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)) { $graph_list .= "<li>" . db_fetch_cell("select title_cache from graph where id = " . $matches[1]) . "<br>"; $graph_array[$i] = $matches[1]; } $i++; } require_once CACTI_BASE_PATH . "/include/top_header.php"; html_start_box("<strong>" . $graph_actions[$_POST["drp_action"]] . "</strong>", "60%", $colors["header_panel_background"], "3", "center", ""); print "<form action='graphs.php' method='post'>\n"; if ($_POST["drp_action"] == "1") { /* delete */ $graphs = array(); /* find out which (if any) data sources are being used by this graph, so we can tell the user */ if (isset($graph_array)) { $data_sources = db_fetch_assoc("select distinct\n\t\t\t\tdata_source.id,\n\t\t\t\tdata_source.name_cache\n\t\t\t\tfrom data_source_item,data_source,graph_item\n\t\t\t\twhere graph_item.data_source_item_id=data_source_item.id\n\t\t\t\tand data_source_item.data_source_id=data_source.id\n\t\t\t\tand " . array_to_sql_or($graph_array, "graph_item.graph_id") . "\n\t\t\t\torder by data_source.name_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 graphs?") . "</p>\n\t\t\t\t\t<p>{$graph_list}</p>\n\t\t\t\t\t"; if (sizeof($data_sources) > 0) { print "<tr bgcolor='#" . $colors["form_alternate1"] . "'><td class='textArea'><p class='textArea'>" . _("The following data sources are in use by these graphs:") . "</p>\n"; foreach ($data_sources as $data_source) { print "<strong>" . $data_source["name_cache"] . "</strong><br>\n"; } print "<br>"; form_radio_button("delete_type", "1", "1", _("Leave the data sources untouched."), "1"); print "<br>"; form_radio_button("delete_type", "1", "2", _("Delete all <strong>data sources</strong> referenced by these graphs."), "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 graph template and click save to change the graph template for\n\t\t\t\t\tthe following graphs. 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>{$graph_list}</p>\n\t\t\t\t\t<p><strong>" . _("New Graph Template:") . "</strong><br>"; form_dropdown("graph_template_id", db_fetch_assoc("select graph_templates.id,graph_templates.name from graph_templates"), "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") { /* 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 graphs will be duplicated. You can\n\t\t\t\t\toptionally change the title format for the new graphs.") . "</p>\n\t\t\t\t\t<p>{$graph_list}</p>\n\t\t\t\t\t<p><strong>" . _("Title Format:") . "</strong><br>"; form_text_box("title_format", "<graph_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"] == "4") { /* 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 graphs will be converted into graph templates.\n\t\t\t\t\tYou can optionally change the title format for the new graph templates.") . "</p>\n\t\t\t\t\t<p>{$graph_list}</p>\n\t\t\t\t\t<p><strong>" . _("Title Format:") . "</strong><br>"; form_text_box("title_format", "<graph_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"] == "5") { /* 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 graphs:") . "</p>\n\t\t\t\t\t<p>{$graph_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"] == "6") { /* reapply suggested naming to host */ 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 graphs will have thier suggested naming convensions\n\t\t\t\t\trecalculated and applies to the graphs.") . "</p>\n\t\t\t\t\t<p>{$graph_list}</p>\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t"; } elseif ($_POST["drp_action"] == "7") { /* reapply suggested naming to host */ 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 graphs will be resized per your specifications.</p>\n\t\t\t\t\t<p>{$graph_list}</p>\n\t\t\t\t\t<p><strong>Graph Height:</strong><br>"; form_text_box("graph_height", "", "", "255", "30", "text"); print "</p>\n\t\t\t\t\t<p><strong>Graph Width:</strong><br>"; form_text_box("graph_width", "", "", "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"] == "8") { /* place on tree */ $trees = db_fetch_assoc("select id,name from graph_tree order by name"); 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 graphs will be placed under the branch selected\n\t\t\t\t\tbelow.") . "</p>\n\t\t\t\t\t<p>{$graph_list}</p>\n\t\t\t\t\t<p><strong>" . _("Destination Branch:") . "</strong><br>"; html_tree_dropdown_draw($matches[1], "tree_item_id", "0"); print "</p>\n\t\t\t\t</td>\n\t\t\t</tr>\n\n\t\t\t<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n\n\t\t\t"; } if (!isset($graph_array)) { print "<tr><td bgcolor='#" . $colors["form_alternate1"] . "'><span class='textError'>" . _("You must select at least one graph.") . "</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($graph_array) ? serialize($graph_array) : '') . "'>\n\t\t\t\t<input type='hidden' name='drp_action' value='" . $_POST["drp_action"] . "'>\n\t\t\t\t<a href='graphs.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"; }
function push_out_graph_item($graph_template_item_id) { global $struct_graph_item; /* get information about this graph template */ $graph_template_item = db_fetch_row("select * from graph_templates_item where id=$graph_template_item_id"); /* must be a graph template */ if ($graph_template_item["graph_template_id"] == 0) { return 0; } /* find out if any graphs actual contain this item */ if (sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_template_item_id=$graph_template_item_id")) == 0) { /* if not, reapply the template to push out the new item */ $attached_graphs = db_fetch_assoc("select local_graph_id from graph_templates_graph where graph_template_id=" . $graph_template_item["graph_template_id"] . " and local_graph_id>0"); if (sizeof($attached_graphs) > 0) { foreach ($attached_graphs as $item) { change_graph_template($item["local_graph_id"], $graph_template_item["graph_template_id"], true); } } } /* this is trickier with graph_items than with the actual graph... we have to make sure not to overwrite any items covered in the "graph item inputs". the same thing applies to graphs, but is easier to detect there (t_* columns). */ $graph_item_inputs = db_fetch_assoc("select graph_template_input.column_name, graph_template_input_defs.graph_template_item_id from graph_template_input, graph_template_input_defs where graph_template_input.graph_template_id=" . $graph_template_item["graph_template_id"] . " and graph_template_input.id=graph_template_input_defs.graph_template_input_id and graph_template_input_defs.graph_template_item_id=$graph_template_item_id"); $graph_item_inputs = array_rekey($graph_item_inputs, "column_name", "graph_template_item_id"); /* loop through each graph item column name (from the above array) */ reset($struct_graph_item); while (list($field_name, $field_array) = each($struct_graph_item)) { /* are we allowed to push out the column? */ if (!isset($graph_item_inputs[$field_name])) { db_execute("update graph_templates_item set $field_name='$graph_template_item[$field_name]' where local_graph_template_item_id=" . $graph_template_item["id"]); } } }
function create_complete_graph_from_template($graph_template_id, $host_id, $snmp_query_array, &$suggested_values_array) { global $config; include_once($config["library_path"] . "/data_query.php"); /* create the graph */ $save["id"] = 0; $save["graph_template_id"] = $graph_template_id; $save["host_id"] = $host_id; $cache_array["local_graph_id"] = sql_save($save, "graph_local"); change_graph_template($cache_array["local_graph_id"], $graph_template_id, true); /* This next block is simply designed to find the best suggested template * name for our new graph. The suggested values are held in * snmp_query_graph_sv and the graph is updated in graph_templates_graph. */ if (is_array($snmp_query_array)) { /* suggested values for snmp query code */ $suggested_values = db_fetch_assoc("select text,field_name from snmp_query_graph_sv where snmp_query_graph_id=" . $snmp_query_array["snmp_query_graph_id"] . " order by sequence"); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { /* once we find a match; don't try to find more */ if (!isset($suggested_values_graph[$graph_template_id]{$suggested_value["field_name"]})) { $subs_string = substitute_snmp_query_data($suggested_value["text"], $host_id, $snmp_query_array["snmp_query_id"], $snmp_query_array["snmp_index"], read_config_option("max_data_query_field_length")); /* if there are no '|' characters, all of the substitutions were successful */ if (!strstr($subs_string, "|query")) { db_execute("update graph_templates_graph set " . $suggested_value["field_name"] . "='" . addslashes($suggested_value["text"]) . "' where local_graph_id=" . $cache_array["local_graph_id"]); /* once we find a working value, stop */ $suggested_values_graph[$graph_template_id]{$suggested_value["field_name"]} = true; } } } } } /* suggested values: graph */ if (isset($suggested_values_array[$graph_template_id]["graph_template"])) { while (list($field_name, $field_value) = each($suggested_values_array[$graph_template_id]["graph_template"])) { db_execute("update graph_templates_graph set $field_name='$field_value' where local_graph_id=" . $cache_array["local_graph_id"]); } } /* suggested values: graph item */ if (isset($suggested_values_array[$graph_template_id]["graph_template_item"])) { while (list($graph_template_item_id, $field_array) = each($suggested_values_array[$graph_template_id]["graph_template_item"])) { while (list($field_name, $field_value) = each($field_array)) { $graph_item_id = db_fetch_cell("select id from graph_templates_item where local_graph_template_item_id=$graph_template_item_id and local_graph_id=" . $cache_array["local_graph_id"]); db_execute("update graph_templates_item set $field_name='$field_value' where id=$graph_item_id"); } } } update_graph_title_cache($cache_array["local_graph_id"]); /* create each data source */ /* FIXED: group by */ $data_templates = db_fetch_assoc("select data_template.id, data_template.name, max(data_template_rrd.data_source_name) as data_source_name from (data_template cross join data_template_rrd cross join graph_templates_item) where graph_templates_item.task_item_id=data_template_rrd.id and data_template_rrd.data_template_id=data_template.id and data_template_rrd.local_data_id=0 and graph_templates_item.local_graph_id=0 and graph_templates_item.graph_template_id=" . $graph_template_id . " group by data_template.id, data_template.name order by data_template.name"); if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { unset($save); $save["id"] = 0; $save["data_template_id"] = $data_template["id"]; $save["host_id"] = $host_id; $cache_array["local_data_id"]{$data_template["id"]} = sql_save($save, "data_local"); change_data_template($cache_array["local_data_id"]{$data_template["id"]}, $data_template["id"]); $data_template_data_id = db_fetch_cell("select id from data_template_data where local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); if (is_array($snmp_query_array)) { /* suggested values for snmp query code */ $suggested_values = db_fetch_assoc("select text,field_name from snmp_query_graph_rrd_sv where snmp_query_graph_id=" . $snmp_query_array["snmp_query_graph_id"] . " and data_template_id=" . $data_template["id"] . " order by sequence"); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { /* once we find a match; don't try to find more */ if (!isset($suggested_values_ds{$data_template["id"]}{$suggested_value["field_name"]})) { $subs_string = substitute_snmp_query_data($suggested_value["text"], $host_id, $snmp_query_array["snmp_query_id"], $snmp_query_array["snmp_index"], read_config_option("max_data_query_field_length")); /* if there are no '|' characters, all of the substitutions were successful */ if (!strstr($subs_string, "|query")) { if (sql_column_exists("data_template_data", $suggested_value["field_name"])) { db_execute("update data_template_data set " . $suggested_value["field_name"] . "='" . addslashes($suggested_value["text"]) . "' where local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); } /* once we find a working value, stop */ $suggested_values_ds{$data_template["id"]}{$suggested_value["field_name"]} = true; if (sql_column_exists("data_template_data", $suggested_value["field_name"]) && (!substr_count($subs_string, "|"))) { db_execute("update data_template_rrd set " . $suggested_value["field_name"] . "='" . $subs_string . "' where local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); } } } } } } if (is_array($snmp_query_array)) { $data_input_field = array_rekey(db_fetch_assoc("select data_input_fields.id, data_input_fields.type_code from (snmp_query cross join data_input cross join data_input_fields) where snmp_query.data_input_id=data_input.id and data_input.id=data_input_fields.data_input_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') and snmp_query.id=" . $snmp_query_array["snmp_query_id"]), "type_code", "id"); $snmp_cache_value = db_fetch_cell("select field_value from host_snmp_cache where host_id='$host_id' and snmp_query_id='" . $snmp_query_array["snmp_query_id"] . "' and field_name='" . $snmp_query_array["snmp_index_on"] . "' and snmp_index='" . $snmp_query_array["snmp_index"] . "'"); /* save the value to index on (ie. ifindex, ifip, etc) */ db_replace("data_input_data",array("data_input_field_id"=>$data_input_field["index_type"],"data_template_data_id"=>$data_template_data_id,"t_value"=>"","value"=>$snmp_query_array["snmp_index_on"]),array("data_input_field_id","data_template_data_id"),true); /* save the actual value (ie. 3, 192.168.1.101, etc) */ db_replace("data_input_data",array("data_input_field_id"=>$data_input_field["index_value"],"data_template_data_id"=>$data_template_data_id,"t_value"=>"","value"=>addslashes($snmp_cache_value)),array("data_input_field_id","data_template_data_id"),true); /* set the expected output type (ie. bytes, errors, packets) */ db_replace("data_input_data",array("data_input_field_id"=>$data_input_field["output_type"],"data_template_data_id"=>$data_template_data_id,"t_value"=>"","value"=>$snmp_query_array["snmp_query_graph_id"]),array("data_input_field_id","data_template_data_id"),true); /* now that we have put data into the 'data_input_data' table, update the snmp cache for ds's */ update_data_source_data_query_cache($cache_array["local_data_id"]{$data_template["id"]}); } /* suggested values: data source */ if (isset($suggested_values_array[$graph_template_id]["data_template"]{$data_template["id"]})) { reset($suggested_values_array[$graph_template_id]["data_template"]{$data_template["id"]}); while (list($field_name, $field_value) = each($suggested_values_array[$graph_template_id]["data_template"]{$data_template["id"]})) { db_execute("update data_template_data set $field_name='$field_value' where local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); } } /* suggested values: data source item */ if (isset($suggested_values_array[$graph_template_id]["data_template_item"])) { reset($suggested_values_array[$graph_template_id]["data_template_item"]); while (list($data_template_item_id, $field_array) = each($suggested_values_array[$graph_template_id]["data_template_item"])) { while (list($field_name, $field_value) = each($field_array)) { $data_source_item_id = db_fetch_cell("select id from data_template_rrd where local_data_template_rrd_id=$data_template_item_id and local_data_id=" . $cache_array["local_data_id"]{$data_template["id"]}); db_execute("update data_template_rrd set $field_name='$field_value' where id=$data_source_item_id"); } } } /* suggested values: custom data */ if (isset($suggested_values_array[$graph_template_id]["custom_data"]{$data_template["id"]})) { reset($suggested_values_array[$graph_template_id]["custom_data"]{$data_template["id"]}); while (list($data_input_field_id, $field_value) = each($suggested_values_array[$graph_template_id]["custom_data"]{$data_template["id"]})) { db_replace("data_input_data",array("data_input_field_id"=>$data_input_field_id,"data_template_data_id"=>$data_template_data_id,"t_value"=>"","value"=>$field_value),array("data_input_field_id","data_template_data_id"),true); } } update_data_source_title_cache($cache_array["local_data_id"]{$data_template["id"]}); } } /* connect the dots: graph -> data source(s) */ $template_item_list = db_fetch_assoc("select graph_templates_item.id, data_template_rrd.id as data_template_rrd_id, data_template_rrd.data_template_id from (graph_templates_item cross join data_template_rrd) where graph_templates_item.task_item_id=data_template_rrd.id and graph_templates_item.graph_template_id=$graph_template_id and local_graph_id=0 and task_item_id>0"); /* loop through each item affected and update column data */ if (sizeof($template_item_list) > 0) { foreach ($template_item_list as $template_item) { $local_data_id = $cache_array["local_data_id"]{$template_item["data_template_id"]}; $graph_template_item_id = db_fetch_cell("select id from graph_templates_item where local_graph_template_item_id=" . $template_item["id"] . " and local_graph_id=" . $cache_array["local_graph_id"]); $data_template_rrd_id = db_fetch_cell("select id from data_template_rrd where local_data_template_rrd_id=" . $template_item["data_template_rrd_id"] . " and local_data_id=$local_data_id"); if (!empty($data_template_rrd_id)) { db_execute("update graph_templates_item set task_item_id='$data_template_rrd_id' where id=$graph_template_item_id"); } } } /* this will not work until the ds->graph dots are connected */ if (is_array($snmp_query_array)) { update_graph_data_query_cache($cache_array["local_graph_id"]); } return $cache_array; }
function form_actions() { global $graph_actions; /* ================= input validation ================= */ input_validate_input_regex(get_request_var_post('drp_action'), '^([a-zA-Z0-9_]+)$'); /* ==================================================== */ /* 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; } for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ } switch ($_POST['delete_type']) { case '2': /* delete all data sources referenced by this graph */ $data_sources = array_rekey(db_fetch_assoc('SELECT data_template_data.local_data_id FROM (data_template_rrd, data_template_data, graph_templates_item) 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 ' . array_to_sql_or($selected_items, 'graph_templates_item.local_graph_id') . ' AND data_template_data.local_data_id > 0'), 'local_data_id', 'local_data_id'); if (sizeof($data_sources)) { api_data_source_remove_multi($data_sources); api_plugin_hook_function('data_source_remove', $data_sources); } break; } api_graph_remove_multi($selected_items); api_plugin_hook_function('graphs_remove', $selected_items); } elseif ($_POST['drp_action'] == '2') { /* change graph template */ input_validate_input_number(get_request_var_post('graph_template_id')); for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ change_graph_template($selected_items[$i], $_POST['graph_template_id'], true); } } elseif ($_POST['drp_action'] == '3') { /* duplicate */ for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ duplicate_graph($selected_items[$i], 0, $_POST['title_format']); } } elseif ($_POST['drp_action'] == '4') { /* graph -> graph template */ for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ graph_to_graph_template($selected_items[$i], $_POST['title_format']); } } elseif (preg_match('/^tr_([0-9]+)$/', $_POST['drp_action'], $matches)) { /* place on tree */ input_validate_input_number(get_request_var_post('tree_id')); input_validate_input_number(get_request_var_post('tree_item_id')); for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_tree_item_save(0, $_POST['tree_id'], TREE_ITEM_TYPE_GRAPH, $_POST['tree_item_id'], '', $selected_items[$i], read_graph_config_option('default_rra_id'), 0, 0, 0, false); } } elseif ($_POST['drp_action'] == '5') { /* change host */ input_validate_input_number(get_request_var_post('host_id')); for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ db_execute_prepared('UPDATE graph_local SET host_id = ? WHERE id = ?', array($_POST['host_id'], $selected_items[$i])); update_graph_title_cache($selected_items[$i]); } } elseif ($_POST['drp_action'] == '6') { /* reapply suggested naming */ for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_reapply_suggested_graph_title($selected_items[$i]); update_graph_title_cache($selected_items[$i]); } } elseif ($_POST['drp_action'] == '7') { /* resize graphs */ input_validate_input_number(get_request_var_post('graph_width')); input_validate_input_number(get_request_var_post('graph_height')); for ($i = 0; $i < count($selected_items); $i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_resize_graphs($selected_items[$i], $_POST['graph_width'], $_POST['graph_height']); } } else { api_plugin_hook_function('graphs_action_execute', $_POST['drp_action']); } /* update snmpcache */ snmpagent_graphs_action_bottom(array($_POST['drp_action'], $selected_items)); api_plugin_hook_function('graphs_action_bottom', array($_POST['drp_action'], $selected_items)); header('Location: graphs.php'); exit; } /* setup some variables */ $graph_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 (preg_match('/^chk_([0-9]+)$/', $var, $matches)) { /* ================= input validation ================= */ input_validate_input_number($matches[1]); /* ==================================================== */ $graph_list .= '<li>' . htmlspecialchars(get_graph_title($matches[1])) . '</li>'; $graph_array[$i] = $matches[1]; $i++; } } top_header(); /* add a list of tree names to the actions dropdown */ add_tree_names_to_actions_array(); html_start_box('<strong>' . $graph_actions[$_POST['drp_action']] . '</strong>', '60%', '', '3', 'center', ''); print "<form action='graphs.php' method='post'>\n"; if (isset($graph_array) && sizeof($graph_array)) { if ($_POST['drp_action'] == '1') { /* delete */ $graphs = array(); /* find out which (if any) data sources are being used by this graph, so we can tell the user */ if (isset($graph_array) && sizeof($graph_array)) { $data_sources = db_fetch_assoc('select data_template_data.local_data_id, data_template_data.name_cache from (data_template_rrd,data_template_data,graph_templates_item) 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 ' . array_to_sql_or($graph_array, 'graph_templates_item.local_graph_id') . ' and data_template_data.local_data_id > 0 group by data_template_data.local_data_id order by data_template_data.name_cache'); } print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be deleted. Please note, Data Source(s) should be deleted only if they are only used by these Graph(s)\n\t\t\t\t\t\tand not others.</p>\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>"; if (isset($data_sources) && sizeof($data_sources)) { print "<tr><td class='textArea'><p>The following Data Source(s) are in use by these Graph(s):</p>\n"; print '<ul>'; foreach ($data_sources as $data_source) { print '<li><strong>' . $data_source['name_cache'] . "</strong></li>\n"; } print '</ul>'; print '<br>'; form_radio_button('delete_type', '1', '2', "Leave the Data Source(s) untouched. Not applicable for Graphs created under 'New Graphs' or WHERE the Graphs were created automatically.", '2'); print '<br>'; form_radio_button('delete_type', '2', '2', 'Delete all <strong>Data Source(s)</strong> referenced by these Graph(s).', '2'); print '<br>'; print '</td></tr>'; } print "\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Delete Graph(s)'>"; } elseif ($_POST['drp_action'] == '2') { /* change graph template */ print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>Choose a Graph Template and click \"Continue\" to change the Graph Template for\n\t\t\t\t\t\tthe following Graph(s). Be aware that all warnings will be suppressed during the\n\t\t\t\t\t\tconversion, so Graph data loss is possible.</p>\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\n\t\t\t\t\t\t<p><strong>New Graph Template:</strong><br>"; form_dropdown('graph_template_id', db_fetch_assoc('SELECT graph_templates.id,graph_templates.name FROM graph_templates ORDER BY name'), 'name', 'id', '', '', '0'); print "</p>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Change Graph Template'>"; } elseif ($_POST['drp_action'] == '3') { /* duplicate */ print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be duplicated. You can\n\t\t\t\t\t\toptionally change the title format for the new Graph(s).</p>\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\n\t\t\t\t\t\t<p><strong>Title Format:</strong><br>"; form_text_box('title_format', '<graph_title> (1)', '', '255', '30', 'text'); print "</p>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Duplicate Graph(s)'>"; } elseif ($_POST['drp_action'] == '4') { /* graph -> graph template */ print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be converted into Graph Template(s).\n\t\t\t\t\t\tYou can optionally change the title format for the new Graph Template(s).</p>\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\n\t\t\t\t\t\t<p><strong>Title Format:</strong><br>"; form_text_box('title_format', '<graph_title> Template', '', '255', '30', 'text'); print "</p>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Convert to Graph Template'>"; } elseif (preg_match('/^tr_([0-9]+)$/', $_POST['drp_action'], $matches)) { /* place on tree */ print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be placed under the Tree Branch selected below.</p>\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\n\t\t\t\t\t\t<p><strong>Destination Branch:</strong><br>"; grow_dropdown_tree($matches[1], '0', 'tree_item_id', '0'); print "</p>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t<input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Place Graph(s) on Tree'>"; } elseif ($_POST['drp_action'] == '5') { /* change host */ print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>Choose a new Device for these Graph(s) and click \"Continue\"</p>\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\n\t\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\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Change Graph(s) Associated Device'>"; } elseif ($_POST['drp_action'] == '6') { /* reapply suggested naming to host */ print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will have thier suggested naming convensions\n\t\t\t\t\t\trecalculated and applied to the Graph(s).</p>\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Reapply Suggested Naming to Graph(s)'>"; } elseif ($_POST['drp_action'] == '7') { /* resize graphs */ print "\t<tr>\n\t\t\t\t\t<td class='textArea'>\n\t\t\t\t\t\t<p>When you click \"Continue\", the following Graph(s) will be resized per your specifications.</p>\n\t\t\t\t\t\t<p><ul>{$graph_list}</ul></p>\n\t\t\t\t\t\t<p><strong>Graph Height:</strong><br>"; form_text_box('graph_height', '', '', '255', '30', 'text'); print '</p> <p><strong>Graph Width:</strong><br>'; form_text_box('graph_width', '', '', '255', '30', 'text'); print "</p>\n\t\t\t\t\t</td>\n\t\t\t\t</tr>\n\n\t\t\t\t"; $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue' title='Resize Selected Graph(s)'>"; } else { $save['drp_action'] = $_POST['drp_action']; $save['graph_list'] = $graph_list; $save['graph_array'] = isset($graph_array) ? $graph_array : array(); api_plugin_hook_function('graphs_action_prepare', $save); $save_html = "<input type='button' value='Cancel' onClick='window.history.back()'> <input type='submit' value='Continue'>"; } } else { print "<tr><td class='even'><span class='textError'>You must select at least one graph.</span></td></tr>\n"; $save_html = "<input type='button' value='Return' onClick='window.history.back()'>"; } print "\t<tr>\n\t\t\t<td align='right' class='saveRow'>\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($graph_array) ? serialize($graph_array) : '') . "'>\n\t\t\t\t<input type='hidden' name='drp_action' value='" . $_POST['drp_action'] . "'>\n\t\t\t\t{$save_html}\n\t\t\t</td>\n\t\t</tr>\n\t\t"; html_end_box(); bottom_footer(); }
function create_complete_graph_from_template($graph_template_id, $host_id, $snmp_query_array, &$suggested_values_array) { global $config; include_once $config["library_path"] . "/data_query.php"; /* create the graph */ $save["id"] = 0; $save["graph_template_id"] = $graph_template_id; $save["host_id"] = $host_id; $cache_array["local_graph_id"] = sql_save($save, "graph_local"); change_graph_template($cache_array["local_graph_id"], $graph_template_id, true); if (is_array($snmp_query_array)) { /* suggested values for snmp query code */ $suggested_values = db_fetch_assoc("SELECT text,field_name FROM snmp_query_graph_sv WHERE snmp_query_graph_id=" . $snmp_query_array["snmp_query_graph_id"] . " ORDER BY sequence"); $suggested_values_graph = array(); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { /* once we find a match; don't try to find more */ if (!isset($suggested_values_graph[$graph_template_id][$suggested_value["field_name"]])) { $subs_string = substitute_snmp_query_data($suggested_value["text"], $host_id, $snmp_query_array["snmp_query_id"], $snmp_query_array["snmp_index"], read_config_option("max_data_query_field_length")); /* if there are no '|' characters, all of the substitutions were successful */ if (!strstr($subs_string, "|query")) { db_execute("UPDATE graph_templates_graph SET " . $suggested_value["field_name"] . "='" . $suggested_value["text"] . "' WHERE local_graph_id=" . $cache_array["local_graph_id"]); /* once we find a working value, stop */ $suggested_values_graph[$graph_template_id][$suggested_value["field_name"]] = true; } } } } } /* suggested values: graph */ if (isset($suggested_values_array[$graph_template_id]["graph_template"])) { while (list($field_name, $field_value) = each($suggested_values_array[$graph_template_id]["graph_template"])) { db_execute("UPDATE graph_templates_graph SET {$field_name}='{$field_value}' WHERE local_graph_id=" . $cache_array["local_graph_id"]); } } /* suggested values: graph item */ if (isset($suggested_values_array[$graph_template_id]["graph_template_item"])) { while (list($graph_template_item_id, $field_array) = each($suggested_values_array[$graph_template_id]["graph_template_item"])) { while (list($field_name, $field_value) = each($field_array)) { $graph_item_id = db_fetch_cell("SELECT id FROM graph_templates_item WHERE local_graph_template_item_id={$graph_template_item_id} AND local_graph_id=" . $cache_array["local_graph_id"]); db_execute("UPDATE graph_templates_item SET {$field_name}='{$field_value}' WHERE id={$graph_item_id}"); } } } update_graph_title_cache($cache_array["local_graph_id"]); /* create each data source */ $data_templates = db_fetch_assoc("SELECT\n\t\tdata_template.id,\n\t\tdata_template.name,\n\t\tdata_template_rrd.data_source_name\n\t\tFROM (data_template, data_template_rrd, graph_templates_item)\n\t\tWHERE graph_templates_item.task_item_id=data_template_rrd.id\n\t\tAND data_template_rrd.data_template_id=data_template.id\n\t\tAND data_template_rrd.local_data_id=0\n\t\tAND graph_templates_item.local_graph_id=0\n\t\tAND graph_templates_item.graph_template_id=" . $graph_template_id . "\n\t\tGROUP BY data_template.id\n\t\tORDER BY data_template.name"); if (sizeof($data_templates) > 0) { foreach ($data_templates as $data_template) { unset($save); $save["id"] = 0; $save["data_template_id"] = $data_template["id"]; $save["host_id"] = $host_id; $cache_array["local_data_id"][$data_template["id"]] = sql_save($save, "data_local"); change_data_template($cache_array["local_data_id"][$data_template["id"]], $data_template["id"]); $data_template_data_id = db_fetch_cell("SELECT id FROM data_template_data WHERE local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); if (is_array($snmp_query_array)) { /* suggested values for snmp query code */ $suggested_values = db_fetch_assoc("SELECT text,field_name FROM snmp_query_graph_rrd_sv WHERE snmp_query_graph_id=" . $snmp_query_array["snmp_query_graph_id"] . " AND data_template_id=" . $data_template["id"] . " ORDER BY sequence"); $suggested_values_ds = array(); if (sizeof($suggested_values) > 0) { foreach ($suggested_values as $suggested_value) { /* once we find a match; don't try to find more */ if (!isset($suggested_values_ds[$data_template["id"]][$suggested_value["field_name"]])) { $subs_string = substitute_snmp_query_data($suggested_value["text"], $host_id, $snmp_query_array["snmp_query_id"], $snmp_query_array["snmp_index"], read_config_option("max_data_query_field_length")); /* if there are no '|' characters, all of the substitutions were successful */ if (!strstr($subs_string, "|query")) { if (sizeof(db_fetch_row("show columns FROM data_template_data LIKE '" . $suggested_value["field_name"] . "'"))) { db_execute("UPDATE data_template_data SET " . $suggested_value["field_name"] . "='" . $suggested_value["text"] . "' WHERE local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); } /* once we find a working value, stop */ $suggested_values_ds[$data_template["id"]][$suggested_value["field_name"]] = true; if (sizeof(db_fetch_row("show columns FROM data_template_rrd LIKE '" . $suggested_value["field_name"] . "'")) && !substr_count($subs_string, "|")) { db_execute("UPDATE data_template_rrd SET " . $suggested_value["field_name"] . "='" . $suggested_value["text"] . "' WHERE local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); } } } } } } if (is_array($snmp_query_array)) { $data_input_field = array_rekey(db_fetch_assoc("SELECT\n\t\t\t\tdata_input_fields.id,\n\t\t\t\tdata_input_fields.type_code\n\t\t\t\tFROM (snmp_query,data_input,data_input_fields)\n\t\t\t\tWHERE snmp_query.data_input_id=data_input.id\n\t\t\t\tAND data_input.id=data_input_fields.data_input_id\n\t\t\t\tAND (data_input_fields.type_code='index_type'\n\t\t\t\t\tOR data_input_fields.type_code='index_value'\n\t\t\t\t\tOR data_input_fields.type_code='output_type')\n\t\t\t\tAND snmp_query.id=" . $snmp_query_array["snmp_query_id"]), "type_code", "id"); $snmp_cache_value = db_fetch_cell("SELECT field_value\n\t\t\t\tFROM host_snmp_cache\n\t\t\t\tWHERE host_id='{$host_id}'\n\t\t\t\tAND snmp_query_id='" . $snmp_query_array["snmp_query_id"] . "'\n\t\t\t\tAND field_name='" . $snmp_query_array["snmp_index_on"] . "'\n\t\t\t\tAND snmp_index='" . $snmp_query_array["snmp_index"] . "'"); /* save the value to index on (ie. ifindex, ifip, etc) */ db_execute("REPLACE INTO data_input_data\n\t\t\t\t(data_input_field_id, data_template_data_id, t_value, value)\n\t\t\t\tVALUES (" . $data_input_field["index_type"] . ", {$data_template_data_id}, '', '" . $snmp_query_array["snmp_index_on"] . "')"); /* save the actual value (ie. 3, 192.168.1.101, etc) */ db_execute("REPLACE INTO data_input_data\n\t\t\t\t(data_input_field_id,data_template_data_id,t_value,value)\n\t\t\t\tVALUES (" . $data_input_field["index_value"] . ",{$data_template_data_id},'','" . addslashes($snmp_cache_value) . "')"); /* set the expected output type (ie. bytes, errors, packets) */ db_execute("REPLACE INTO data_input_data\n\t\t\t\t(data_input_field_id,data_template_data_id,t_value,value)\n\t\t\t\tVALUES (" . $data_input_field["output_type"] . ",{$data_template_data_id},'','" . $snmp_query_array["snmp_query_graph_id"] . "')"); /* now that we have put data into the 'data_input_data' table, update the snmp cache for ds's */ update_data_source_data_query_cache($cache_array["local_data_id"][$data_template["id"]]); } /* suggested values: data source */ if (isset($suggested_values_array[$graph_template_id]["data_template"][$data_template["id"]])) { reset($suggested_values_array[$graph_template_id]["data_template"][$data_template["id"]]); while (list($field_name, $field_value) = each($suggested_values_array[$graph_template_id]["data_template"][$data_template["id"]])) { db_execute("UPDATE data_template_data\n\t\t\t\t\tSET {$field_name}='{$field_value}'\n\t\t\t\t\tWHERE local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); } } /* suggested values: data source item */ if (isset($suggested_values_array[$graph_template_id]["data_template_item"])) { reset($suggested_values_array[$graph_template_id]["data_template_item"]); while (list($data_template_item_id, $field_array) = each($suggested_values_array[$graph_template_id]["data_template_item"])) { while (list($field_name, $field_value) = each($field_array)) { $data_source_item_id = db_fetch_cell("SELECT id FROM data_template_rrd WHERE local_data_template_rrd_id={$data_template_item_id} AND local_data_id=" . $cache_array["local_data_id"][$data_template["id"]]); db_execute("UPDATE data_template_rrd\n\t\t\t\t\t\tSET {$field_name}='{$field_value}'\n\t\t\t\t\t\tWHERE id={$data_source_item_id}"); } } } /* suggested values: custom data */ if (isset($suggested_values_array[$graph_template_id]["custom_data"][$data_template["id"]])) { reset($suggested_values_array[$graph_template_id]["custom_data"][$data_template["id"]]); while (list($data_input_field_id, $field_value) = each($suggested_values_array[$graph_template_id]["custom_data"][$data_template["id"]])) { db_execute("REPLACE INTO data_input_data (data_input_field_id,data_template_data_id,t_value,value) values ({$data_input_field_id},{$data_template_data_id},'','{$field_value}')"); } } update_data_source_title_cache($cache_array["local_data_id"][$data_template["id"]]); } } /* connect the dots: graph -> data source(s) */ $template_item_list = db_fetch_assoc("SELECT\n\t\tgraph_templates_item.id,\n\t\tdata_template_rrd.id AS data_template_rrd_id,\n\t\tdata_template_rrd.data_template_id\n\t\tFROM (graph_templates_item,data_template_rrd)\n\t\tWHERE graph_templates_item.task_item_id=data_template_rrd.id\n\t\tAND graph_templates_item.graph_template_id={$graph_template_id}\n\t\tAND local_graph_id=0\n\t\tAND task_item_id>0"); /* loop through each item affected and update column data */ if (sizeof($template_item_list) > 0) { foreach ($template_item_list as $template_item) { $local_data_id = $cache_array["local_data_id"][$template_item["data_template_id"]]; $graph_template_item_id = db_fetch_cell("SELECT id FROM graph_templates_item WHERE local_graph_template_item_id=" . $template_item["id"] . " AND local_graph_id=" . $cache_array["local_graph_id"]); $data_template_rrd_id = db_fetch_cell("SELECT id FROM data_template_rrd WHERE local_data_template_rrd_id=" . $template_item["data_template_rrd_id"] . " AND local_data_id={$local_data_id}"); if (!empty($data_template_rrd_id)) { db_execute("UPDATE graph_templates_item SET task_item_id='{$data_template_rrd_id}' WHERE id={$graph_template_item_id}"); } } } /* this will not work until the ds->graph dots are connected */ if (is_array($snmp_query_array)) { update_graph_data_query_cache($cache_array["local_graph_id"]); } # now that we have the id of the new host, we may plugin postprocessing code $save["id"] = $cache_array["local_graph_id"]; $save["graph_template_id"] = $graph_template_id; // attention: unset! if (is_array($snmp_query_array)) { $save["snmp_query_id"] = $snmp_query_array["snmp_query_id"]; $save["snmp_index"] = $snmp_query_array["snmp_index"]; } else { $save["snmp_query_id"] = 0; $save["snmp_index"] = 0; } api_plugin_hook_function('create_complete_graph_from_template', $save); return $cache_array; }
function graph_form_actions() { global $colors; require(CACTI_BASE_PATH . "/include/graph_tree/graph_tree_arrays.php"); require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php"); /* if we are to save this form, instead of display it */ if (isset($_POST["selected_items"])) { $selected_items = unserialize(stripslashes($_POST["selected_items"])); if (get_request_var_post("drp_action") === GRAPH_ACTION_DELETE) { /* delete */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ if (!isset($_POST["delete_type"])) { $_POST["delete_type"] = 1; } switch (get_request_var_post("delete_type")) { case '2': /* delete all data sources referenced by this graph */ $data_sources = db_fetch_assoc("SELECT " . "data_template_data.local_data_id " . "FROM " . "(data_template_rrd, " . "data_template_data, " . "graph_templates_item) " . "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=" . $selected_items[$i] . " " . "AND data_template_data.local_data_id > 0"); if (sizeof($data_sources) > 0) { foreach ($data_sources as $data_source) { api_data_source_remove($data_source["local_data_id"]); } } break; } api_graph_remove($selected_items[$i]); } }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_CHANGE_TEMPLATE) { /* change graph template */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); input_validate_input_number(get_request_var_post("graph_template_id")); /* ==================================================== */ change_graph_template($selected_items[$i], get_request_var_post("graph_template_id"), true); } }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_DUPLICATE) { /* duplicate */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ duplicate_graph($selected_items[$i], 0, get_request_var_post("title_format")); } }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_CONVERT_TO_TEMPLATE) { /* graph -> graph template */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ graph_to_graph_template($selected_items[$i], get_request_var_post("title_format")); } }elseif (preg_match("/^tr_([0-9]+)$/", get_request_var_post("drp_action"), $matches)) { /* place on tree */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); input_validate_input_number(get_request_var_post("tree_id")); input_validate_input_number(get_request_var_post("tree_item_id")); /* ==================================================== */ api_tree_item_save(0, get_request_var_post("tree_id"), TREE_ITEM_TYPE_GRAPH, get_request_var_post("tree_item_id"), "", $selected_items[$i], read_graph_config_option("default_rra_id"), 0, 0, 0, false); } }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_CHANGE_HOST) { /* change device */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); input_validate_input_number(get_request_var_post("device_id")); /* ==================================================== */ db_execute("update graph_local set device_id=" . $_POST["device_id"] . " where id=" . $selected_items[$i]); update_graph_title_cache($selected_items[$i]); } }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_REAPPLY_SUGGESTED_NAMES) { /* reapply suggested naming */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_reapply_suggested_graph_title($selected_items[$i]); update_graph_title_cache($selected_items[$i]); } }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_RESIZE) { /* resize graphs */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ api_resize_graphs($selected_items[$i], get_request_var_post('graph_width'), get_request_var_post('graph_height')); } }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_ENABLE_EXPORT) { /* enable graph export */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ db_execute("UPDATE graph_templates_graph SET export='on' WHERE local_graph_id=" . $selected_items[$i]); } }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_DISABLE_EXPORT) { /* disable graph export */ for ($i=0;($i<count($selected_items));$i++) { /* ================= input validation ================= */ input_validate_input_number($selected_items[$i]); /* ==================================================== */ db_execute("UPDATE graph_templates_graph SET export='' WHERE local_graph_id=" . $selected_items[$i]); } } else { api_plugin_hook_function('graphs_action_execute', get_request_var_post('drp_action')); } header("Location: graphs.php"); exit; } /* setup some variables */ $graph_list = ""; $i = 0; $graph_array = array(); /* loop through each of the graphs selected on the previous page and get more info about them */ while (list($var,$val) = each($_POST)) { if (preg_match("/^chk_([0-9]+)$/", $var, $matches)) { /* ================= input validation ================= */ input_validate_input_number($matches[1]); /* ==================================================== */ $graph_list .= "<li>" . get_graph_title($matches[1]) . "<br>"; $graph_array[$i++] = $matches[1]; } } include_once(CACTI_BASE_PATH . "/include/top_header.php"); /* add a list of tree names to the actions dropdown */ $graph_actions = array_merge(graph_actions_list(), api_tree_add_tree_names_to_actions_array()); $graph_actions[ACTION_NONE] = __("None"); html_start_box("<strong>" . $graph_actions{get_request_var_post("drp_action")} . "</strong>", "60", $colors["header_panel"], "3", "center", ""); print "<form action='graphs.php' method='post'>\n"; if (sizeof($graph_array)) { if (get_request_var_post("drp_action") === ACTION_NONE) { /* NONE */ print " <tr> <td class='textArea'> <p>" . __("You did not select a valid action. Please select 'Return' to return to the previous menu.") . "</p> </td> </tr>\n"; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_DELETE) { /* delete */ $graphs = array(); /* find out which (if any) data sources are being used by this graph, so we can tell the user */ if (isset($graph_array)) { $data_sources = db_fetch_assoc("select data_template_data.local_data_id, data_template_data.name_cache from (data_template_rrd,data_template_data,graph_templates_item) 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 " . array_to_sql_or($graph_array, "graph_templates_item.local_graph_id") . " and data_template_data.local_data_id > 0 group by data_template_data.local_data_id order by data_template_data.name_cache"); } print " <tr> <td class='textArea'> <p>" . __("Are you sure you want to delete the following graphs?") . "</p> <p>$graph_list</p> "; if (sizeof($data_sources) > 0) { print "<tr class='rowAlternate1'><td class='textArea'><p class='textArea'>" . __("The following data sources are in use by these graphs:") . "</p>\n"; foreach ($data_sources as $data_source) { print "<strong>" . $data_source["name_cache"] . "</strong><br>\n"; } print "<br>"; form_radio_button("delete_type", "1", "1", __("Leave the data sources untouched."), "1"); print "<br>"; form_radio_button("delete_type", "1", "2", __("Delete all <strong>data sources</strong> referenced by these graphs."), "1"); print "<br>"; print "</td></tr>"; } print " </td> </tr>\n "; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_CHANGE_TEMPLATE) { /* change graph template */ print " <tr> <td class='textArea'> <p>" . __("Choose a graph template and click save to change the graph template for the following graphs. Be aware that all warnings will be suppressed during the conversion, so graph data loss is possible.") . "</p> <p>$graph_list</p> <p><strong>" . __("New Graph Template:") . "</strong><br>"; form_dropdown("graph_template_id",db_fetch_assoc("select graph_templates.id,graph_templates.name from graph_templates order by name"),"name","id","","","0"); print "</p> </td> </tr>\n "; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_DUPLICATE) { /* duplicate */ print " <tr> <td class='textArea'> <p>" . __("When you click save, the following graphs will be duplicated. You can optionally change the title format for the new graphs.") . "</p> <p>$graph_list</p> <p><strong>" . __("Title Format:") . "</strong><br>"; form_text_box("title_format", __("<graph_title> (1)"), "", "255", "30", "text"); print "</p> </td> </tr>\n "; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_CONVERT_TO_TEMPLATE) { /* graph -> graph template */ print " <tr> <td class='textArea'> <p>" . __("When you click save, the following graphs will be converted into graph templates. You can optionally change the title format for the new graph templates.") . "</p> <p>$graph_list</p> <p><strong>" . __("Title Format:") . "</strong><br>"; form_text_box("title_format", __("<graph_title> Template"), "", "255", "30", "text"); print "</p> </td> </tr>\n "; }elseif (preg_match("/^tr_([0-9]+)$/", get_request_var_post("drp_action"), $matches)) { /* place on tree */ print " <tr> <td class='textArea'> <p>" . __("When you click save, the following graphs will be placed under the branch selected below.") . "</p> <p>$graph_list</p> <p><strong>" . __("Destination Branch:") . "</strong><br>"; grow_dropdown_tree($matches[1], "tree_item_id", "0"); print "</p> </td> </tr>\n <input type='hidden' name='tree_id' value='" . $matches[1] . "'>\n "; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_CHANGE_HOST) { /* change device */ print " <tr> <td class='textArea'> <p>" . __("Choose a new device for these graphs:") . "</p> <p>$graph_list</p> <p><strong>" . __("New Host:") . "</strong><br>"; form_dropdown("device_id",db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from device order by description,hostname"),"name","id","","","0"); print "</p> </td> </tr>\n "; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_REAPPLY_SUGGESTED_NAMES) { /* reapply suggested naming to device */ print " <tr> <td class='textArea'> <p>" . __("When you click save, the following graphs will have their suggested naming conventions recalculated and applied to the graphs.") . "</p> <p>$graph_list</p> </td> </tr>\n "; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_RESIZE) { /* reapply suggested naming to device */ print " <tr> <td class='textArea'> <p>" . __("When you click save, the following graphs will be resized per your specifications.") . "</p> <p>$graph_list</p> <p><strong>" . __("Graph Height:") . "</strong><br>"; form_text_box("graph_height", "", "", "255", "30", "text"); print "</p> <p><strong>" . __("Graph Width:") . "</strong><br>"; form_text_box("graph_width", "", "", "255", "30", "text"); print "</p> </td> </tr>\n "; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_ENABLE_EXPORT) { /* enable graph export */ print " <tr> <td class='textArea'> <p>" . __("When you click save, the following graphs will be enabled for graph export.") . "</p> <p>$graph_list</p> </td> </tr>\n "; }elseif (get_request_var_post("drp_action") === GRAPH_ACTION_DISABLE_EXPORT) { /* disable graph export */ print " <tr> <td class='textArea'> <p>" . __("When you click save, the following graphs will be disabled for graph export.") . "</p> <p>$graph_list</p> </td> </tr>\n "; } else { $save['drp_action'] = $_POST['drp_action']; $save['graph_list'] = $graph_list; $save['graph_array'] = $graph_array; api_plugin_hook_function('graphs_action_prepare', $save); } } else { print " <tr> <td class='textArea'> <p>" . __("You must first select a Graph. Please select 'Return' to return to the previous menu.") . "</p> </td> </tr>\n"; } if (!sizeof($graph_array) || get_request_var_post("drp_action") === ACTION_NONE) { form_return_button_alt(); }else{ form_yesno_button_alt(serialize($graph_array), get_request_var_post("drp_action")); } html_end_box(); include_once(CACTI_BASE_PATH . "/include/bottom_footer.php"); }