예제 #1
0
function update_data_query_cache($host_id, $data_query_id) {
	$graphs = db_fetch_assoc("select id from graph_local where host_id = '$host_id' and snmp_query_id = '$data_query_id'");

	if (sizeof($graphs) > 0) {
		foreach ($graphs as $graph) {
			update_graph_data_query_cache($graph["id"]);
		}
	}

	$data_sources = db_fetch_assoc("select id from data_local where host_id = '$host_id' and snmp_query_id = '$data_query_id'");

	if (sizeof($data_sources) > 0) {
		foreach ($data_sources as $data_source) {
			update_data_source_data_query_cache($data_source["id"]);
		}
	}
}
예제 #2
0
파일: template.php 프로젝트: songchin/Cacti
/** 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;
}
예제 #3
0
function form_save()
{
    if (isset($_POST["save_component_data_source_new"]) && !empty($_POST["data_template_id"])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post("host_id"));
        input_validate_input_number(get_request_var_post("data_template_id"));
        /* ==================================================== */
        $save["id"] = $_POST["local_data_id"];
        $save["data_template_id"] = $_POST["data_template_id"];
        $save["host_id"] = $_POST["host_id"];
        $local_data_id = sql_save($save, "data_local");
        change_data_template($local_data_id, $_POST["data_template_id"]);
        /* update the title cache */
        update_data_source_title_cache($local_data_id);
        /* update host data */
        if (!empty($_POST["host_id"])) {
            push_out_host($_POST["host_id"], $local_data_id);
        }
    }
    if (isset($_POST["save_component_data"]) && !is_error_message()) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post("data_template_data_id"));
        /* ==================================================== */
        /* ok, first pull out all 'input' values so we know how much to save */
        $input_fields = db_fetch_assoc("select\n\t\t\tdata_template_data.data_input_id,\n\t\t\tdata_local.host_id,\n\t\t\tdata_input_fields.id,\n\t\t\tdata_input_fields.input_output,\n\t\t\tdata_input_fields.data_name,\n\t\t\tdata_input_fields.regexp_match,\n\t\t\tdata_input_fields.allow_nulls,\n\t\t\tdata_input_fields.type_code\n\t\t\tfrom data_template_data\n\t\t\tleft join data_input_fields on (data_input_fields.data_input_id=data_template_data.data_input_id)\n\t\t\tleft join data_local on (data_template_data.local_data_id=data_local.id)\n\t\t\twhere data_template_data.id=" . $_POST["data_template_data_id"] . "\n\t\t\tand data_input_fields.input_output='in'");
        if (sizeof($input_fields) > 0) {
            foreach ($input_fields as $input_field) {
                if (isset($_POST["value_" . $input_field["id"]])) {
                    /* save the data into the 'data_input_data' table */
                    $form_value = $_POST["value_" . $input_field["id"]];
                    /* we shouldn't enforce rules on fields the user cannot see (ie. templated ones) */
                    $is_templated = db_fetch_cell("select t_value from data_input_data where data_input_field_id=" . $input_field["id"] . " and data_template_data_id=" . db_fetch_cell("select local_data_template_data_id from data_template_data where id=" . $_POST["data_template_data_id"]));
                    if ($is_templated == "") {
                        $allow_nulls = true;
                    } elseif ($input_field["allow_nulls"] == "on") {
                        $allow_nulls = true;
                    } elseif (empty($input_field["allow_nulls"])) {
                        $allow_nulls = false;
                    }
                    /* run regexp match on input string */
                    $form_value = form_input_validate($form_value, "value_" . $input_field["id"], $input_field["regexp_match"], $allow_nulls, 3);
                    if (!is_error_message()) {
                        db_execute("replace into data_input_data (data_input_field_id,data_template_data_id,t_value,value) values\n\t\t\t\t\t\t(" . $input_field["id"] . "," . $_POST["data_template_data_id"] . ",'','{$form_value}')");
                    }
                }
            }
        }
    }
    if (isset($_POST["save_component_data_source"]) && !is_error_message()) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post("local_data_id"));
        input_validate_input_number(get_request_var_post("current_rrd"));
        input_validate_input_number(get_request_var_post("data_template_id"));
        input_validate_input_number(get_request_var_post("host_id"));
        /* ==================================================== */
        $save1["id"] = $_POST["local_data_id"];
        $save1["data_template_id"] = $_POST["data_template_id"];
        $save1["host_id"] = $_POST["host_id"];
        $save2["id"] = $_POST["data_template_data_id"];
        $save2["local_data_template_data_id"] = $_POST["local_data_template_data_id"];
        $save2["data_template_id"] = $_POST["data_template_id"];
        $save2["data_input_id"] = form_input_validate($_POST["data_input_id"], "data_input_id", "", true, 3);
        $save2["name"] = form_input_validate($_POST["name"], "name", "", false, 3);
        $save2["data_source_path"] = form_input_validate($_POST["data_source_path"], "data_source_path", "", true, 3);
        $save2["active"] = form_input_validate(isset($_POST["active"]) ? $_POST["active"] : "", "active", "", true, 3);
        $save2["rrd_step"] = form_input_validate($_POST["rrd_step"], "rrd_step", "^[0-9]+\$", false, 3);
        if (!is_error_message()) {
            $local_data_id = sql_save($save1, "data_local");
            $save2["local_data_id"] = $local_data_id;
            $data_template_data_id = sql_save($save2, "data_template_data");
            if ($data_template_data_id) {
                raise_message(1);
            } else {
                raise_message(2);
            }
        }
        if (!is_error_message()) {
            /* if this is a new data source and a template has been selected, skip item creation this time
            			otherwise it throws off the templatate creation because of the NULL data */
            if (!empty($_POST["local_data_id"]) || empty($_POST["data_template_id"])) {
                /* if no template was set before the save, there will be only one data source item to save;
                			otherwise there might be >1 */
                if (empty($_POST["_data_template_id"])) {
                    $rrds[0]["id"] = $_POST["current_rrd"];
                } else {
                    $rrds = db_fetch_assoc("select id from data_template_rrd where local_data_id=" . $_POST["local_data_id"]);
                }
                if (sizeof($rrds) > 0) {
                    foreach ($rrds as $rrd) {
                        if (empty($_POST["_data_template_id"])) {
                            $name_modifier = "";
                        } else {
                            $name_modifier = "_" . $rrd["id"];
                        }
                        $save3["id"] = $rrd["id"];
                        $save3["local_data_id"] = $local_data_id;
                        $save3["local_data_template_rrd_id"] = db_fetch_cell("select local_data_template_rrd_id from data_template_rrd where id=" . $rrd["id"]);
                        $save3["data_template_id"] = $_POST["data_template_id"];
                        $save3["rrd_maximum"] = form_input_validate($_POST["rrd_maximum{$name_modifier}"], "rrd_maximum{$name_modifier}", "^-?[0-9]+\$", false, 3);
                        $save3["rrd_minimum"] = form_input_validate($_POST["rrd_minimum{$name_modifier}"], "rrd_minimum{$name_modifier}", "^-?[0-9]+\$", false, 3);
                        $save3["rrd_heartbeat"] = form_input_validate($_POST["rrd_heartbeat{$name_modifier}"], "rrd_heartbeat{$name_modifier}", "^[0-9]+\$", false, 3);
                        $save3["data_source_type_id"] = $_POST["data_source_type_id{$name_modifier}"];
                        $save3["data_source_name"] = form_input_validate($_POST["data_source_name{$name_modifier}"], "data_source_name{$name_modifier}", "^[a-zA-Z0-9_-]{1,19}\$", false, 3);
                        $save3["data_input_field_id"] = form_input_validate(isset($_POST["data_input_field_id{$name_modifier}"]) ? $_POST["data_input_field_id{$name_modifier}"] : "0", "data_input_field_id{$name_modifier}", "", true, 3);
                        $data_template_rrd_id = sql_save($save3, "data_template_rrd");
                        if ($data_template_rrd_id) {
                            raise_message(1);
                        } else {
                            raise_message(2);
                        }
                    }
                }
            }
        }
        if (!is_error_message()) {
            if (!empty($_POST["rra_id"])) {
                /* save entries in 'selected rras' field */
                db_execute("delete from data_template_data_rra where data_template_data_id={$data_template_data_id}");
                for ($i = 0; $i < count($_POST["rra_id"]); $i++) {
                    /* ================= input validation ================= */
                    input_validate_input_number($_POST["rra_id"][$i]);
                    /* ==================================================== */
                    db_execute("insert into data_template_data_rra (rra_id,data_template_data_id)\n\t\t\t\t\t\tvalues (" . $_POST["rra_id"][$i] . ",{$data_template_data_id})");
                }
            }
            if ($_POST["data_template_id"] != $_POST["_data_template_id"]) {
                /* update all necessary template information */
                change_data_template($local_data_id, $_POST["data_template_id"]);
            } elseif (!empty($_POST["data_template_id"])) {
                update_data_source_data_query_cache($local_data_id);
            }
            if ($_POST["host_id"] != $_POST["_host_id"]) {
                /* push out all necessary host information */
                push_out_host($_POST["host_id"], $local_data_id);
                /* reset current host for display purposes */
                $_SESSION["sess_data_source_current_host_id"] = $_POST["host_id"];
            }
            /* if no data source path has been entered, generate one */
            if (empty($_POST["data_source_path"])) {
                generate_data_source_path($local_data_id);
            }
            /* update the title cache */
            update_data_source_title_cache($local_data_id);
        }
    }
    /* update the poller cache last to make sure everything is fresh */
    if (!is_error_message() && !empty($local_data_id)) {
        update_poller_cache($local_data_id, false);
    }
    if (isset($_POST["save_component_data_source_new"]) && empty($_POST["data_template_id"])) {
        header("Location: data_sources.php?action=ds_edit&host_id=" . $_POST["host_id"] . "&new=1");
    } elseif (is_error_message() || $_POST["data_template_id"] != $_POST["_data_template_id"] || $_POST["data_input_id"] != $_POST["_data_input_id"] || $_POST["host_id"] != $_POST["_host_id"]) {
        header("Location: data_sources.php?action=ds_edit&id=" . (empty($local_data_id) ? $_POST["local_data_id"] : $local_data_id) . "&host_id=" . $_POST["host_id"] . "&view_rrd=" . (isset($_POST["current_rrd"]) ? $_POST["current_rrd"] : "0"));
    } else {
        header("Location: data_sources.php");
    }
}
예제 #4
0
function data_source_form_save() {
	if ((isset($_POST["save_component_data_source_new"])) && (!empty($_POST["data_template_id"]))) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("device_id"));
		input_validate_input_number(get_request_var_post("data_template_id"));
		/* ==================================================== */

		$save["id"] = $_POST["local_data_id"];
		$save["data_template_id"] = $_POST["data_template_id"];
		$save["device_id"] = $_POST["device_id"];

		$local_data_id = sql_save($save, "data_local");

		change_data_template($local_data_id, get_request_var_post("data_template_id"));

		/* update the title cache */
		update_data_source_title_cache($local_data_id);

		/* update device data */
		if (!empty($_POST["device_id"])) {
			push_out_device(get_request_var_post("device_id"), $local_data_id);
		}
	}

	if ((isset($_POST["save_component_data"])) && (!is_error_message())) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("data_template_data_id"));
		/* ==================================================== */

		/* ok, first pull out all 'input' values so we know how much to save */
		$input_fields = db_fetch_assoc("select
			data_template_data.data_input_id,
			data_local.device_id,
			data_input_fields.id,
			data_input_fields.input_output,
			data_input_fields.data_name,
			data_input_fields.regexp_match,
			data_input_fields.allow_nulls,
			data_input_fields.type_code
			from data_template_data
			left join data_input_fields on (data_input_fields.data_input_id=data_template_data.data_input_id)
			left join data_local on (data_template_data.local_data_id=data_local.id)
			where data_template_data.id=" . $_POST["data_template_data_id"] . "
			and data_input_fields.input_output='in'");

		if (sizeof($input_fields) > 0) {
		foreach ($input_fields as $input_field) {
			if (isset($_POST{"value_" . $input_field["id"]})) {
				/* save the data into the 'data_input_data' table */
				$form_value = $_POST{"value_" . $input_field["id"]};

				/* we shouldn't enforce rules on fields the user cannot see (ie. templated ones) */
				$is_templated = db_fetch_cell("select t_value from data_input_data where data_input_field_id=" . $input_field["id"] . " and data_template_data_id=" . db_fetch_cell("select local_data_template_data_id from data_template_data where id=" . $_POST["data_template_data_id"]));

				if ($is_templated == "") {
					$allow_nulls = true;
				}elseif ($input_field["allow_nulls"] == CHECKED) {
					$allow_nulls = true;
				}elseif (empty($input_field["allow_nulls"])) {
					$allow_nulls = false;
				}

				/* run regexp match on input string */
				$form_value = form_input_validate($form_value, "value_" . $input_field["id"], $input_field["regexp_match"], $allow_nulls, 3);

				if (!is_error_message()) {
					db_execute("replace into data_input_data (data_input_field_id,data_template_data_id,t_value,value) values
						(" . $input_field["id"] . "," . get_request_var_post("data_template_data_id") . ",'','$form_value')");
				}
			}
		}
		}
	}

	if ((isset($_POST["save_component_data_source"])) && (!is_error_message())) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("local_data_id"));
		input_validate_input_number(get_request_var_post("current_rrd"));
		input_validate_input_number(get_request_var_post("data_template_id"));
		input_validate_input_number(get_request_var_post("device_id"));
		/* ==================================================== */

		$save1["id"] = $_POST["local_data_id"];
		$save1["data_template_id"] = $_POST["data_template_id"];
		$save1["device_id"] = $_POST["device_id"];

		$save2["id"] = $_POST["data_template_data_id"];
		$save2["local_data_template_data_id"] = $_POST["local_data_template_data_id"];
		$save2["data_template_id"] = $_POST["data_template_id"];
		$save2["data_input_id"] = form_input_validate($_POST["data_input_id"], "data_input_id", "", true, 3);
		$save2["name"] = form_input_validate($_POST["name"], "name", "", false, 3);
		$save2["data_source_path"] = form_input_validate($_POST["data_source_path"], "data_source_path", "", true, 3);
		$save2["active"] = form_input_validate((isset($_POST["active"]) ? $_POST["active"] : ""), "active", "", true, 3);
		$save2["rrd_step"] = form_input_validate($_POST["rrd_step"], "rrd_step", "^[0-9]+$", false, 3);

		if (!is_error_message()) {
			$local_data_id = sql_save($save1, "data_local");

			$save2["local_data_id"] = $local_data_id;
			$data_template_data_id = sql_save($save2, "data_template_data");

			if ($data_template_data_id) {
				raise_message(1);
			}else{
				raise_message(2);
			}
		}

#		if (!is_error_message()) {
#			/* if this is a new data source and a template has been selected, skip item creation this time
#			otherwise it throws off the templatate creation because of the NULL data */
#			if ((!empty($_POST["local_data_id"])) || (empty($_POST["data_template_id"]))) {
#				/* if no template was set before the save, there will be only one data source item to save;
#				otherwise there might be >1 */
#				if (empty($_POST["hidden_data_template_id"])) {
#					$rrds[0]["id"] = $_POST["current_rrd"];
#				}else{
#					$rrds = db_fetch_assoc("select id from data_template_rrd where local_data_id=" . $_POST["local_data_id"]);
#				}
#
#				if (sizeof($rrds) > 0) {
#				foreach ($rrds as $rrd) {
#					if (empty($_POST["hidden_data_template_id"])) {
#						$name_modifier = "";
#					}else{
#						$name_modifier = "_" . $rrd["id"];
#					}
#
#					$save3["id"] = $rrd["id"];
#					$save3["local_data_id"] = $local_data_id;
#					$save3["local_data_template_rrd_id"] = db_fetch_cell("select local_data_template_rrd_id from data_template_rrd where id=" . $rrd["id"]);
#					$save3["data_template_id"] = $_POST["data_template_id"];
#					$save3["rrd_maximum"] = form_input_validate($_POST["rrd_maximum$name_modifier"], "rrd_maximum$name_modifier", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$", false, 3);
#					$save3["rrd_minimum"] = form_input_validate($_POST["rrd_minimum$name_modifier"], "rrd_minimum$name_modifier", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$", false, 3);
#					$save3["rrd_heartbeat"] = form_input_validate($_POST["rrd_heartbeat$name_modifier"], "rrd_heartbeat$name_modifier", "^[0-9]+$", false, 3);
#					$save3["data_source_type_id"] = $_POST["data_source_type_id$name_modifier"];
#					$save3["data_source_name"] = form_input_validate($_POST["data_source_name$name_modifier"], "data_source_name$name_modifier", "^[a-zA-Z0-9_-]{1,19}$", false, 3);
#					$save3["data_input_field_id"] = form_input_validate((isset($_POST["data_input_field_id$name_modifier"]) ? $_POST["data_input_field_id$name_modifier"] : "0"), "data_input_field_id$name_modifier", "", true, 3);
#
#					$data_template_rrd_id = sql_save($save3, "data_template_rrd");
#
#					if ($data_template_rrd_id) {
#						raise_message(1);
#					}else{
#						raise_message(2);
#					}
#				}
#				}
#			}
#		}

		if (!is_error_message()) {
			if (!empty($_POST["rra_id"])) {
				/* save entries in 'selected rras' field */
				db_execute("delete from data_template_data_rra where data_template_data_id=$data_template_data_id");

				for ($i=0; ($i < count($_POST["rra_id"])); $i++) {
					/* ================= input validation ================= */
					input_validate_input_number($_POST["rra_id"][$i]);
					/* ==================================================== */

					db_execute("insert into data_template_data_rra (rra_id,data_template_data_id)
						values (" . $_POST["rra_id"][$i] . ",$data_template_data_id)");
				}
			}

			if ($_POST["data_template_id"] != $_POST["hidden_data_template_id"]) {
				/* update all necessary template information */
				change_data_template($local_data_id, get_request_var_post("data_template_id"));
			}elseif (!empty($_POST["data_template_id"])) {
				update_data_source_data_query_cache($local_data_id);
			}

			if ($_POST["device_id"] != $_POST["hidden_device_id"]) {
				/* push out all necessary device information */
				push_out_device(get_request_var_post("device_id"), $local_data_id);

				/* reset current device for display purposes */
				$_SESSION["sess_data_source_currenthidden_device_id"] = $_POST["device_id"];
			}

			/* if no data source path has been entered, generate one */
			if (empty($_POST["data_source_path"])) {
				generate_data_source_path($local_data_id);
			}

			/* update the title cache */
			update_data_source_title_cache($local_data_id);
		}
	}

	/* update the poller cache last to make sure everything is fresh */
	if ((!is_error_message()) && (!empty($local_data_id))) {
		update_poller_cache($local_data_id, true);
	}

	if ((isset($_POST["save_component_data_source_new"])) && (empty($_POST["data_template_id"]))) {
		header("Location: data_sources.php?action=data_source_edit&device_id=" . $_POST["device_id"] . "&new=1");
	}elseif ((is_error_message()) || ($_POST["data_template_id"] != $_POST["hidden_data_template_id"]) || ($_POST["data_input_id"] != $_POST["hidden_data_input_id"]) || ($_POST["device_id"] != $_POST["hidden_device_id"])) {
		header("Location: data_sources.php?action=data_source_edit&id=" . (empty($local_data_id) ? $_POST["local_data_id"] : $local_data_id) . "&device_id=" . $_POST["device_id"] . "&view_rrd=" . (isset($_POST["current_rrd"]) ? $_POST["current_rrd"] : "0"));
	}else{
		header("Location: data_sources.php");
	}
	exit;
}
예제 #5
0
/**
 * perform sql updates for all required tables for new index_sort_order
 * @arg array $snmp_query_array
 * 				$host_id
 * 				snmp_query_id
 * 				snmp_index_on
 * 				snmp_query_graph_id
 * 				snmp_index
 * 				$data_template_data_id	
 * 				$local_data_id
 * 
 * this code stems from lib/template.php, function create_complete_graph_from_template
 */
function update_snmp_index_order($data_query)
{
    if (is_array($data_query)) {
        $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=" . $data_query["snmp_query_id"]), "type_code", "id");
        $snmp_cache_value = db_fetch_cell("SELECT field_value " . "FROM host_snmp_cache " . "WHERE host_id='" . $data_query["host_id"] . "' " . "AND snmp_query_id='" . $data_query["snmp_query_id"] . "' " . "AND field_name='" . $data_query["snmp_index_on"] . "' " . "AND snmp_index='" . $data_query["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_query["data_template_data_id"] . ", '', '" . $data_query["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_query["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_query["data_template_data_id"] . ",'','" . $data_query["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($data_query["local_data_id"]);
    }
}
예제 #6
0
파일: template.php 프로젝트: songchin/Cacti
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;
}
예제 #7
0
파일: data_query.php 프로젝트: MrWnn/cacti
function update_data_query_cache($host_id, $data_query_id)
{
    $graphs = db_fetch_assoc("SELECT id FROM graph_local WHERE host_id = '{$host_id}' AND snmp_query_id = '{$data_query_id}'");
    if (sizeof($graphs) > 0) {
        foreach ($graphs as $graph) {
            update_graph_data_query_cache($graph["id"]);
        }
    }
    $data_sources = db_fetch_assoc("SELECT id FROM data_local WHERE host_id = '{$host_id}' AND snmp_query_id = '{$data_query_id}'");
    if (sizeof($data_sources) > 0) {
        foreach ($data_sources as $data_source) {
            update_data_source_data_query_cache($data_source["id"]);
        }
    }
}
예제 #8
0
파일: template.php 프로젝트: songchin/Cacti
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;
}
예제 #9
0
function form_save()
{
    if (isset($_POST['save_component_data_source_new']) && !empty($_POST['data_template_id'])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post('host_id'));
        input_validate_input_number(get_request_var_post('data_template_id'));
        /* ==================================================== */
        $save['id'] = $_POST['local_data_id'];
        $save['data_template_id'] = $_POST['data_template_id'];
        $save['host_id'] = $_POST['host_id'];
        $local_data_id = sql_save($save, 'data_local');
        change_data_template($local_data_id, $_POST['data_template_id']);
        /* update the title cache */
        update_data_source_title_cache($local_data_id);
        /* update host data */
        if (!empty($_POST['host_id'])) {
            push_out_host($_POST['host_id'], $local_data_id);
        }
    }
    if (isset($_POST['save_component_data']) && !is_error_message()) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post('data_template_data_id'));
        /* ==================================================== */
        /* ok, first pull out all 'input' values so we know how much to save */
        $input_fields = db_fetch_assoc_prepared("SELECT\n\t\t\tdata_template_data.data_input_id,\n\t\t\tdata_local.host_id,\n\t\t\tdata_input_fields.id,\n\t\t\tdata_input_fields.input_output,\n\t\t\tdata_input_fields.data_name,\n\t\t\tdata_input_fields.regexp_match,\n\t\t\tdata_input_fields.allow_nulls,\n\t\t\tdata_input_fields.type_code\n\t\t\tFROM data_template_data\n\t\t\tLEFT JOIN data_input_fields ON (data_input_fields.data_input_id = data_template_data.data_input_id)\n\t\t\tLEFT JOIN data_local ON (data_template_data.local_data_id = data_local.id)\n\t\t\tWHERE data_template_data.id = ?\n\t\t\tAND data_input_fields.input_output='in'", array($_POST['data_template_data_id']));
        if (sizeof($input_fields) > 0) {
            foreach ($input_fields as $input_field) {
                if (isset($_POST['value_' . $input_field['id']])) {
                    /* save the data into the 'data_input_data' table */
                    $form_value = $_POST['value_' . $input_field['id']];
                    /* we shouldn't enforce rules on fields the user cannot see (ie. templated ones) */
                    $is_templated = db_fetch_cell('SELECT t_value FROM data_input_data WHERE data_input_field_id=' . $input_field['id'] . ' and data_template_data_id=' . db_fetch_cell_prepared('SELECT local_data_template_data_id FROM data_template_data WHERE id = ?', array($_POST['data_template_data_id'])));
                    if ($is_templated == '') {
                        $allow_nulls = true;
                    } elseif ($input_field['allow_nulls'] == 'on') {
                        $allow_nulls = true;
                    } elseif (empty($input_field['allow_nulls'])) {
                        $allow_nulls = false;
                    }
                    /* run regexp match on input string */
                    $form_value = form_input_validate($form_value, 'value_' . $input_field['id'], $input_field['regexp_match'], $allow_nulls, 3);
                    if (!is_error_message()) {
                        db_execute_prepared("REPLACE INTO data_input_data (data_input_field_id, data_template_data_id, t_value, value) VALUES (?, ?, '', ?)", array($input_field['id'], $_POST['data_template_data_id'], $form_value));
                    }
                }
            }
        }
    }
    if (isset($_POST['save_component_data_source']) && !is_error_message()) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post('local_data_id'));
        input_validate_input_number(get_request_var_post('current_rrd'));
        input_validate_input_number(get_request_var_post('data_template_id'));
        input_validate_input_number(get_request_var_post('host_id'));
        /* ==================================================== */
        $save1['id'] = $_POST['local_data_id'];
        $save1['data_template_id'] = $_POST['data_template_id'];
        $save1['host_id'] = $_POST['host_id'];
        $save2['id'] = $_POST['data_template_data_id'];
        $save2['local_data_template_data_id'] = $_POST['local_data_template_data_id'];
        $save2['data_template_id'] = $_POST['data_template_id'];
        $save2['data_input_id'] = form_input_validate($_POST['data_input_id'], 'data_input_id', '', true, 3);
        $save2['name'] = form_input_validate($_POST['name'], 'name', '', false, 3);
        $save2['data_source_path'] = form_input_validate($_POST['data_source_path'], 'data_source_path', '', true, 3);
        $save2['active'] = form_input_validate(isset($_POST['active']) ? $_POST['active'] : '', 'active', '', true, 3);
        $save2['rrd_step'] = form_input_validate($_POST['rrd_step'], 'rrd_step', '^[0-9]+$', false, 3);
        if (!is_error_message()) {
            $local_data_id = sql_save($save1, 'data_local');
            $save2['local_data_id'] = $local_data_id;
            $data_template_data_id = sql_save($save2, 'data_template_data');
            if ($data_template_data_id) {
                raise_message(1);
            } else {
                raise_message(2);
            }
        }
        if (!is_error_message()) {
            /* if this is a new data source and a template has been selected, skip item creation this time
            			otherwise it throws off the templatate creation because of the NULL data */
            if (!empty($_POST['local_data_id']) || empty($_POST['data_template_id'])) {
                /* if no template was set before the save, there will be only one data source item to save;
                			otherwise there might be >1 */
                if (empty($_POST['_data_template_id'])) {
                    $rrds[0]['id'] = $_POST['current_rrd'];
                } else {
                    $rrds = db_fetch_assoc_prepared('SELECT id FROM data_template_rrd WHERE local_data_id = ?', array($_POST['local_data_id']));
                }
                if (sizeof($rrds) > 0) {
                    foreach ($rrds as $rrd) {
                        if (empty($_POST['_data_template_id'])) {
                            $name_modifier = '';
                        } else {
                            $name_modifier = '_' . $rrd['id'];
                        }
                        $save3['id'] = $rrd['id'];
                        $save3['local_data_id'] = $local_data_id;
                        $save3['local_data_template_rrd_id'] = db_fetch_cell_prepared('SELECT local_data_template_rrd_id FROM data_template_rrd WHERE id = ?', array($rrd['id']));
                        $save3['data_template_id'] = $_POST['data_template_id'];
                        $save3['rrd_maximum'] = form_input_validate($_POST["rrd_maximum{$name_modifier}"], "rrd_maximum{$name_modifier}", "^(-?([0-9]+(\\.[0-9]*)?|[0-9]*\\.[0-9]+)([eE][+\\-]?[0-9]+)?)|U\$", false, 3);
                        $save3['rrd_minimum'] = form_input_validate($_POST["rrd_minimum{$name_modifier}"], "rrd_minimum{$name_modifier}", "^(-?([0-9]+(\\.[0-9]*)?|[0-9]*\\.[0-9]+)([eE][+\\-]?[0-9]+)?)|U\$", false, 3);
                        $save3['rrd_heartbeat'] = form_input_validate($_POST["rrd_heartbeat{$name_modifier}"], "rrd_heartbeat{$name_modifier}", '^[0-9]+$', false, 3);
                        $save3['data_source_type_id'] = $_POST["data_source_type_id{$name_modifier}"];
                        $save3['data_source_name'] = form_input_validate($_POST["data_source_name{$name_modifier}"], "data_source_name{$name_modifier}", '^[a-zA-Z0-9_-]{1,19}$', false, 3);
                        $save3['data_input_field_id'] = form_input_validate(isset($_POST["data_input_field_id{$name_modifier}"]) ? $_POST["data_input_field_id{$name_modifier}"] : '0', "data_input_field_id{$name_modifier}", '', true, 3);
                        $data_template_rrd_id = sql_save($save3, 'data_template_rrd');
                        if ($data_template_rrd_id) {
                            raise_message(1);
                        } else {
                            raise_message(2);
                        }
                    }
                }
            }
        }
        if (!is_error_message()) {
            if (!empty($_POST['rra_id'])) {
                /* save entries in 'selected rras' field */
                db_execute_prepared('DELETE FROM data_template_data_rra WHERE data_template_data_id = ?', array($data_template_data_id));
                for ($i = 0; $i < count($_POST['rra_id']); $i++) {
                    /* ================= input validation ================= */
                    input_validate_input_number($_POST['rra_id'][$i]);
                    /* ==================================================== */
                    db_execute_prepared('INSERT INTO data_template_data_rra (rra_id, data_template_data_id)
						VALUES (?, ?)', array($_POST['rra_id'][$i], $data_template_data_id));
                }
            }
            if ($_POST['data_template_id'] != $_POST['_data_template_id']) {
                /* update all necessary template information */
                change_data_template($local_data_id, $_POST['data_template_id']);
            } elseif (!empty($_POST['data_template_id'])) {
                update_data_source_data_query_cache($local_data_id);
            }
            if ($_POST['host_id'] != $_POST['_host_id']) {
                /* push out all necessary host information */
                push_out_host($_POST['host_id'], $local_data_id);
                /* reset current host for display purposes */
                $_SESSION['sess_data_source_current_host_id'] = $_POST['host_id'];
            }
            /* if no data source path has been entered, generate one */
            if (empty($_POST['data_source_path'])) {
                generate_data_source_path($local_data_id);
            }
            /* update the title cache */
            update_data_source_title_cache($local_data_id);
        }
    }
    /* update the poller cache last to make sure everything is fresh */
    if (!is_error_message() && !empty($local_data_id)) {
        update_poller_cache($local_data_id, true);
    }
    if (isset($_POST['save_component_data_source_new']) && empty($_POST['data_template_id'])) {
        header('Location: data_sources.php?action=ds_edit&host_id=' . $_POST['host_id'] . '&new=1');
    } elseif (is_error_message() || $_POST['data_template_id'] != $_POST['_data_template_id'] || $_POST['data_input_id'] != $_POST['_data_input_id'] || $_POST['host_id'] != $_POST['_host_id']) {
        header('Location: data_sources.php?action=ds_edit&id=' . (empty($local_data_id) ? $_POST['local_data_id'] : $local_data_id) . '&host_id=' . $_POST['host_id'] . '&view_rrd=' . (isset($_POST['current_rrd']) ? $_POST['current_rrd'] : '0'));
    } else {
        header('Location: data_sources.php');
    }
}
예제 #10
0
파일: template.php 프로젝트: MrWnn/cacti
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;
}