0) { foreach ($snmp_queries as $snmp_query) { db_execute("replace into host_snmp_query (host_id,snmp_query_id) values ($host_id," . $snmp_query["snmp_query_id"] . ")"); /* recache snmp data */ data_query($host_id, $snmp_query["snmp_query_id"]); } } } /* summarize the 'create graph from host template/snmp index' stuff into an array */ while (list($var, $val) = each($_POST)) { if (preg_match('/^cg_\d+$/', $var)) { $graph_template_id = preg_replace('/^cg_(\d+)$/', "\\1", $var); $selected_graphs["cg"][$graph_template_id][$graph_template_id] = true; }elseif (preg_match('/^ccg$/', $var)) { $selected_graphs["cg"]{$_POST["cg_g"]}{$_POST["cg_g"]} = true; }elseif (preg_match('/^sg_\d+_\S+$/', $var)) { $snmp_query_id = preg_replace('/^sg_(\d+)_(\S+)$/', "\\1", $var); $snmp_query_graph_id = $_POST{"sgg_" . $snmp_query_id}; $snmp_index = preg_replace('/^sg_(\d+)_(\S+)$/', "\\2", $var); $selected_graphs["sg"][$snmp_query_id][$snmp_query_graph_id][$snmp_index] = true; } } if (isset($selected_graphs)) { host_new_graphs($host_id, $_POST["host_template_id"], $selected_graphs); exit; } } if ((is_error_message()) || ($_POST["host_template_id"] != $_POST["_host_template_id"])) { header ("Location: host.php?action=edit&id=" . (empty($host_id) ? $_POST["id"] : $host_id)); }else{ header ("Location: host.php"); } } if (isset($_POST["save_component_new_graphs"])) { host_new_graphs_save(); header ("Location: host.php"); } } /* --------------------- Host Functions --------------------- */ function host_reload_query() { include_once ("include/snmp_functions.php"); data_query($_GET["host_id"], $_GET["id"]); } function host_remove_query() { db_execute("delete from host_snmp_cache where snmp_query_id=" . $_GET["id"] . " and host_id=" . $_GET["host_id"]); db_execute("delete from host_snmp_query where snmp_query_id=" . $_GET["id"] . " and host_id=" . $_GET["host_id"]); } function host_remove() { global $config; if ((read_config_option("remove_verification") == "on") && (!isset($_GET["confirm"]))) { include ('include/top_header.php'); form_confirm("Are You Sure?", "Are you sure you want to delete the host '" . db_fetch_cell("select description from host where id=" . $_GET["id"]) . "'?", $_SERVER["HTTP_REFERER"], "host.php?action=remove&id=" . $_GET["id"]); include ('include/bottom_footer.php'); exit; } if ((read_config_option("remove_verification") == "") || (isset($_GET["confirm"]))) { db_execute("delete from host where id=" . $_GET["id"]); db_execute("delete from host_snmp_query where host_id=" . $_GET["id"]); db_execute("delete from host_snmp_cache where host_id=" . $_GET["id"]); } } function host_new_graphs_save() { include_once ("include/utility_functions.php"); include_once ("include/snmp_functions.php"); global $struct_graph, $struct_data_source, $struct_graph_item, $struct_data_source_item, $paths; $selected_graphs_array = unserialize(stripslashes($_POST["selected_graphs_array"])); while (list($form_type, $form_array) = each($selected_graphs_array)) { $current_form_type = $form_type; while (list($form_id1, $form_array2) = each($form_array)) { if ($form_type == "cg") { $graph_template_id = $form_id1; }elseif ($form_type == "sg") { while (list($form_id2, $form_array3) = each($form_array2)) { $snmp_query_id = $form_id1; $snmp_query_graph_id = $form_id2; $snmp_index_array = $form_array3; /* we will use this at the very end when saving */ $snmp_query_to_snmp_query_graphs_array[$snmp_query_id] = $snmp_query_graph_id; } $graph_template_id = db_fetch_cell("select graph_template_id from snmp_query_graph where id=$snmp_query_graph_id"); $snmp_query_array = get_data_query_array($snmp_query_id); } unset($save); $save["id"] = 0; $save["graph_template_id"] = $graph_template_id; $save["host_id"] = $_POST["host_id"]; if ($current_form_type == "cg") { $local_graph_id = sql_save($save, "graph_local"); change_graph_template($local_graph_id, $graph_template_id, true); $new_graph_templates["cg"][$graph_template_id] = $local_graph_id; }elseif ($current_form_type == "sg") { reset($snmp_index_array); while (list($snmp_index, $true) = each($snmp_index_array)) { $local_graph_id = sql_save($save, "graph_local"); change_graph_template($local_graph_id, $graph_template_id, true); /* cache arrays; used down below */ if (isset($new_graph_templates["sg"][$snmp_query_id][$graph_template_id])) { $new_graph_templates["sg"][$snmp_query_id][$graph_template_id] .= $local_graph_id . ":"; }else{ $new_graph_templates["sg"][$snmp_query_id][$graph_template_id] = $local_graph_id . ":"; } $new_graph_snmp_indexes[$snmp_query_id][$local_graph_id] = $snmp_index; $new_graph_index_to_local[$snmp_query_id][$snmp_index] = $local_graph_id; /* 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_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($_POST{"g_" . $snmp_query_id . "_" . $graph_template_id . "_" . $snmp_index . "_" . $suggested_value["field_name"]})) { //$subs_string = subsitute_host_data($suggested_value["text"], "|", "|", $_POST["host_id"]); $subs_string = subsitute_snmp_query_data($suggested_value["text"], "|", "|", $_POST["host_id"], $snmp_query_id, $snmp_index); /* if there are no '|' characters, all of the subsitutions were successful */ if (!strstr($subs_string, "|squery")) { $_POST{"g_" . $snmp_query_id . "_" . $graph_template_id . "_" . $snmp_index . "_" . $suggested_value["field_name"]} = $subs_string; } } } } } } $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["host_id"] = $_POST["host_id"]; if ($current_form_type == "cg") { $local_data_id = sql_save($save, "data_local"); change_data_template($local_data_id, $data_template["id"]); $new_data_templates["cg"][$graph_template_id]{$data_template["id"]} = $local_data_id; }elseif ($current_form_type == "sg") { reset($snmp_index_array); while (list($snmp_index, $true) = each($snmp_index_array)) { $local_data_id = sql_save($save, "data_local"); change_data_template($local_data_id, $data_template["id"]); /* cache arrays; used down below */ if (isset($new_data_templates["sg"][$snmp_query_id]{$data_template["id"]})) { $new_data_templates["sg"][$snmp_query_id]{$data_template["id"]} .= $local_data_id . ":"; }else{ $new_data_templates["sg"][$snmp_query_id]{$data_template["id"]} = $local_data_id . ":"; } $new_data_snmp_indexes[$snmp_query_id][$local_data_id] = $snmp_index; $new_data_index_to_local[$snmp_query_id]{$data_template["id"]}[$snmp_index] = $local_data_id; /* 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_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($_POST{"d_" . $snmp_query_id . "_" . $graph_template_id . "_" . $data_template["id"] . "_" . $snmp_index . "_" . $suggested_value["field_name"]})) { //$subs_string = subsitute_host_data($suggested_value["text"], "|", "|", $_POST["host_id"]); $subs_string = subsitute_snmp_query_data($suggested_value["text"], "|", "|", $_POST["host_id"], $snmp_query_id, $snmp_index); /* if there are no '|' characters, all of the subsitutions were successful */ if (!strstr($subs_string, "|squery")) { $_POST{"d_" . $snmp_query_id . "_" . $graph_template_id . "_" . $data_template["id"] . "_" . $snmp_index . "_" . $suggested_value["field_name"]} = $subs_string; } } } } } } } } /* 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) { if ($current_form_type == "cg") { $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=" . $new_graph_templates["cg"][$graph_template_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=" . $new_data_templates["cg"][$graph_template_id]{$template_item["data_template_id"]}); db_execute("update graph_templates_item set task_item_id='$data_template_rrd_id' where id=$graph_template_item_id"); }elseif (($current_form_type == "sg") && (isset($new_graph_snmp_indexes[$snmp_query_id])) && (isset($new_data_snmp_indexes[$snmp_query_id]))) { reset($new_graph_snmp_indexes[$snmp_query_id]); while (list($local_graph_id, $snmp_index) = each($new_graph_snmp_indexes[$snmp_query_id])) { $local_data_id = $new_data_index_to_local[$snmp_query_id]{$template_item["data_template_id"]}[$snmp_index]; $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=$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"); } } } } } } } /* go ahead and write out values from the POST form to our new data */ while (list($var, $val) = each($_POST)) { if (preg_match("/^g_(\d+)_(\d+)_([^_]+)_(\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: (opt) snmp index, 4: field_name */ if (empty($matches[3])) { /* ALL new graphs */ db_execute("update graph_templates_graph set " . $matches[4] . "='$val' where " . (empty($matches[1]) ? "local_graph_id=" . $new_graph_templates["cg"]{$matches[2]} : array_to_sql_or(split(":", $new_graph_templates["sg"]{$matches[1]}{$matches[2]}),"local_graph_id"))); }else{ /* only new graphs for this snmp_index */ db_execute("update graph_templates_graph set " . $matches[4] . "='$val' where local_graph_id=" . $new_graph_index_to_local{$matches[1]}{$matches[3]}); } }elseif (preg_match("/^gi_(\d+)_(\d+)_(\d+)_(\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: graph_template_input_id, 4:field_name */ /* we need to find out which graph items will be affected by saving this particular item */ $item_list = db_fetch_assoc("select graph_templates_item.id from graph_template_input_defs,graph_templates_item where graph_template_input_defs.graph_template_item_id=graph_templates_item.local_graph_template_item_id and " . (empty($matches[1]) ? "graph_templates_item.local_graph_id=" . $new_graph_templates["cg"]{$matches[2]} : array_to_sql_or(split(":", $new_graph_templates["sg"]{$matches[1]}{$matches[2]}),"graph_templates_item.local_graph_id")) . " and graph_template_input_defs.graph_template_input_id=" . $matches[3]); /* loop through each item affected and update column data */ if (sizeof($item_list) > 0) { foreach ($item_list as $item) { db_execute("update graph_templates_item set " . $matches[4] . "='$val' where id=" . $item["id"]); } } }elseif (preg_match("/^d_(\d+)_(\d+)_(\d+)_([^_]+)_(\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4: (opt) snmp_index, 5:field_name */ if (empty($matches[4])) { /* ALL new graphs */ db_execute("update data_template_data set " . $matches[5] . "='$val' where " . (empty($matches[1]) ? "local_data_id=" . $new_data_templates["cg"]{$matches[2]}{$matches[3]} : array_to_sql_or(split(":", $new_data_templates["sg"]{$matches[1]}{$matches[3]}),"local_data_id"))); }else{ /* only new data sources for this snmp_index */ db_execute("update data_template_data set " . $matches[5] . "='$val' where local_data_id=" . $new_data_index_to_local{$matches[1]}{$matches[3]}{$matches[4]}); } }elseif (preg_match("/^di_(\d+)_(\d+)_(\d+)_(\d+)_(\w+)/", $var, $matches)) { /* 1: snmp_query_id, 2: graph_template_id, 3: data_template_id, 4:local_data_template_rrd_id, 5:field_name */ db_execute("update data_template_rrd set " . $matches[5] . "='$val' where " . (empty($matches[1]) ? "local_data_id=" . $new_data_templates["cg"]{$matches[2]}{$matches[3]} : array_to_sql_or(split(":", $new_data_templates["sg"]{$matches[1]}{$matches[3]}),"local_data_id")) . " and local_data_template_rrd_id=" . $matches[4]); }elseif ((preg_match("/^sg_(\d+)_(\d+)_(\d+)/", $var, $matches)) && (isset($new_data_snmp_indexes{$matches[1]}))) { /* 1: snmp_query_id, 2: data_template_id, 3: data_input_field_id */ reset($new_data_snmp_indexes{$matches[1]}); while (list($local_data_id, $snmp_index) = each($new_data_snmp_indexes{$matches[1]})) { $data_template_data_id = db_fetch_cell("select id from data_template_data where local_data_id=$local_data_id"); /* 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 (" . $matches[3] . ",$data_template_data_id,'','$val')"); $data_input_field_id = db_fetch_cell("select data_input_field_id from snmp_query_field where snmp_query_id=" . $matches[1] . " and action_id=2"); $snmp_cache_value = db_fetch_cell("select field_value from host_snmp_cache where host_id=" . $_POST["host_id"] . " and field_name='$val' and snmp_index='$snmp_index'"); /* 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_id,$data_template_data_id,'','$snmp_cache_value')"); /* set the expected output type (ie. bytes, errors, packets) */ $data_input_field_id = db_fetch_cell("select data_input_field_id from snmp_query_field where snmp_query_id=" . $matches[1] . " and action_id=3"); 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,'','" . $snmp_query_to_snmp_query_graphs_array{$matches[1]} . "')"); } } } /* lastly push host-specific information to our data sources */ push_out_host($_POST["host_id"],0); } function host_new_graphs($host_id, $host_template_id, $selected_graphs_array) { include_once ("include/snmp_functions.php"); include_once ("include/top_header.php"); global $colors, $paths, $struct_graph, $row_counter, $struct_data_source, $struct_graph_item, $struct_data_source_item; print "