コード例 #1
0
ファイル: data_query.php プロジェクト: songchin/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"]);
		}
	}
}
コード例 #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
ファイル: graphs.php プロジェクト: songchin/Cacti
function form_save() {
	if ((isset($_POST["save_component_graph_new"])) && (!empty($_POST["graph_template_id"]))) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("graph_template_id"));
		/* ==================================================== */

		$save["id"] = $_POST["local_graph_id"];
		$save["graph_template_id"] = $_POST["graph_template_id"];
		$save["host_id"] = $_POST["host_id"];

		$local_graph_id = sql_save($save, "graph_local");

		change_graph_template($local_graph_id, $_POST["graph_template_id"], true);

		/* update the title cache */
		update_graph_title_cache($local_graph_id);
	}

	if (isset($_POST["save_component_graph"])) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("graph_template_id"));
		input_validate_input_number(get_request_var_post("_graph_template_id"));
		/* ==================================================== */

		$save1["id"] = $_POST["local_graph_id"];
		$save1["host_id"] = $_POST["host_id"];
		$save1["graph_template_id"] = $_POST["graph_template_id"];

		$save2["id"] = $_POST["graph_template_graph_id"];
		$save2["local_graph_template_graph_id"] = $_POST["local_graph_template_graph_id"];
		$save2["graph_template_id"] = $_POST["graph_template_id"];
		$save2["image_format_id"] = form_input_validate($_POST["image_format_id"], "image_format_id", "", true, 3);
		$save2["title"] = form_input_validate($_POST["title"], "title", "", false, 3);
		$save2["height"] = form_input_validate($_POST["height"], "height", "^[0-9]+$", false, 3);
		$save2["width"] = form_input_validate($_POST["width"], "width", "^[0-9]+$", false, 3);
		$save2["upper_limit"] = form_input_validate($_POST["upper_limit"], "upper_limit", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$", false, 3);
		$save2["lower_limit"] = form_input_validate($_POST["lower_limit"], "lower_limit", "^(-?([0-9]+(\.[0-9]*)?|[0-9]*\.[0-9]+)([eE][+\-]?[0-9]+)?)|U$", false, 3);
		$save2["vertical_label"] = form_input_validate($_POST["vertical_label"], "vertical_label", "", true, 3);
		$save2["slope_mode"] = form_input_validate((isset($_POST["slope_mode"]) ? $_POST["slope_mode"] : ""), "slope_mode", "", true, 3);
		$save2["auto_scale"] = form_input_validate((isset($_POST["auto_scale"]) ? $_POST["auto_scale"] : ""), "auto_scale", "", true, 3);
		$save2["auto_scale_opts"] = form_input_validate($_POST["auto_scale_opts"], "auto_scale_opts", "", true, 3);
		$save2["auto_scale_log"] = form_input_validate((isset($_POST["auto_scale_log"]) ? $_POST["auto_scale_log"] : ""), "auto_scale_log", "", true, 3);
		$save2["scale_log_units"] = form_input_validate((isset($_POST["scale_log_units"]) ? $_POST["scale_log_units"] : ""), "scale_log_units", "", true, 3);
		$save2["auto_scale_rigid"] = form_input_validate((isset($_POST["auto_scale_rigid"]) ? $_POST["auto_scale_rigid"] : ""), "auto_scale_rigid", "", true, 3);
		$save2["auto_padding"] = form_input_validate((isset($_POST["auto_padding"]) ? $_POST["auto_padding"] : ""), "auto_padding", "", true, 3);
		$save2["base_value"] = form_input_validate($_POST["base_value"], "base_value", "^[0-9]+$", false, 3);
		$save2["export"] = form_input_validate((isset($_POST["export"]) ? $_POST["export"] : ""), "export", "", true, 3);
		$save2["unit_value"] = form_input_validate($_POST["unit_value"], "unit_value", "", true, 3);
		$save2["unit_exponent_value"] = form_input_validate($_POST["unit_exponent_value"], "unit_exponent_value", "^-?[0-9]+$", true, 3);

		if (!is_error_message()) {
			$local_graph_id = sql_save($save1, "graph_local");
		}

		if (!is_error_message()) {
			$save2["local_graph_id"] = $local_graph_id;
			$graph_templates_graph_id = sql_save($save2, "graph_templates_graph");

			if ($graph_templates_graph_id) {
				raise_message(1);

				/* if template information chanegd, update all necessary template information */
				if ($_POST["graph_template_id"] != $_POST["_graph_template_id"]) {
					/* check to see if the number of graph items differs, if it does; we need user input */
					if ((!empty($_POST["graph_template_id"])) && (!empty($_POST["local_graph_id"])) && (sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_id=$local_graph_id")) != sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_id=0 and graph_template_id=" . $_POST["graph_template_id"])))) {
						/* set the template back, since the user may choose not to go through with the change
						at this point */
						db_execute("update graph_local set graph_template_id=" . $_POST["_graph_template_id"] . " where id=$local_graph_id");
						db_execute("update graph_templates_graph set graph_template_id=" . $_POST["_graph_template_id"] . " where local_graph_id=$local_graph_id");

						header("Location: graphs.php?action=graph_diff&id=$local_graph_id&graph_template_id=" . $_POST["graph_template_id"]);
						exit;
					}
				}
			}else{
				raise_message(2);
			}

			/* update the title cache */
			update_graph_title_cache($local_graph_id);
		}

		if ((!is_error_message()) && ($_POST["graph_template_id"] != $_POST["_graph_template_id"])) {
			change_graph_template($local_graph_id, $_POST["graph_template_id"], true);
		}elseif (!empty($_POST["graph_template_id"])) {
			update_graph_data_query_cache($local_graph_id);
		}
	}

	if (isset($_POST["save_component_input"])) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("local_graph_id"));
		/* ==================================================== */

		/* first; get the current graph template id */
		$graph_template_id = db_fetch_cell("select graph_template_id from graph_local where id=" . $_POST["local_graph_id"]);

		/* get all inputs that go along with this graph template */
		$input_list = db_fetch_assoc("select id,column_name from graph_template_input where graph_template_id=$graph_template_id");

		if (sizeof($input_list) > 0) {
		foreach ($input_list as $input) {
			/* 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 graph_templates_item.local_graph_id=" . $_POST["local_graph_id"] . "
				and graph_template_input_defs.graph_template_input_id=" . $input["id"]);

			/* loop through each item affected and update column data */
			if (sizeof($item_list) > 0) {
			foreach ($item_list as $item) {
				/* if we are changing templates, the POST vars we are searching for here will not exist.
				this is because the db and form are out of sync here, but it is ok to just skip over saving
				the inputs in this case. */
				if (isset($_POST{$input["column_name"] . "_" . $input["id"]})) {
					db_execute("update graph_templates_item set " . $input["column_name"] . "='" . $_POST{$input["column_name"] . "_" . $input["id"]} . "' where id=" . $item["id"]);
				}
			}
			}
		}
		}
	}

	if (isset($_POST["save_component_graph_diff"])) {
		if ($_POST["type"] == "1") {
			$intrusive = true;
		}elseif ($_POST["type"] == "2") {
			$intrusive = false;
		}

		change_graph_template($_POST["local_graph_id"], $_POST["graph_template_id"], $intrusive);
	}

	if ((isset($_POST["save_component_graph_new"])) && (empty($_POST["graph_template_id"]))) {
		header("Location: graphs.php?action=graph_edit&host_id=" . $_POST["host_id"] . "&new=1");
	}elseif ((is_error_message()) || (empty($_POST["local_graph_id"])) || (isset($_POST["save_component_graph_diff"])) || ($_POST["graph_template_id"] != $_POST["_graph_template_id"]) || ($_POST["host_id"] != $_POST["_host_id"])) {
		header("Location: graphs.php?action=graph_edit&id=" . (empty($local_graph_id) ? $_POST["local_graph_id"] : $local_graph_id) . (isset($_POST["host_id"]) ? "&host_id=" . $_POST["host_id"] : ""));
	}else{
		header("Location: graphs.php");
	}
}
コード例 #4
0
ファイル: graphs.php プロジェクト: resmon/resmon-cacti
function form_save()
{
    /* modify for multi user start */
    if (!empty($_POST["host_id"])) {
        if (!check_host($_POST["host_id"])) {
            access_denied();
        }
    }
    /* modify for multi user end */
    if (isset($_POST["save_component_graph_new"]) && !empty($_POST["graph_template_id"])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post("graph_template_id"));
        /* ==================================================== */
        /* modify for multi user start */
        if (!check_resource_count(RESOURCE_GRAPH)) {
            access_denied();
        }
        /* modify for multi user end */
        $save["id"] = $_POST["local_graph_id"];
        $save["graph_template_id"] = $_POST["graph_template_id"];
        $save["host_id"] = $_POST["host_id"];
        $local_graph_id = sql_save($save, "graph_local");
        change_graph_template($local_graph_id, $_POST["graph_template_id"], true);
        /* update the title cache */
        update_graph_title_cache($local_graph_id);
    }
    if (isset($_POST["save_component_graph"])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post("graph_template_id"));
        input_validate_input_number(get_request_var_post("_graph_template_id"));
        /* ==================================================== */
        $save1["id"] = $_POST["local_graph_id"];
        $save1["host_id"] = $_POST["host_id"];
        $save1["graph_template_id"] = $_POST["graph_template_id"];
        $save2["id"] = $_POST["graph_template_graph_id"];
        $save2["local_graph_template_graph_id"] = $_POST["local_graph_template_graph_id"];
        $save2["graph_template_id"] = $_POST["graph_template_id"];
        $save2["image_format_id"] = form_input_validate($_POST["image_format_id"], "image_format_id", "", true, 3);
        $save2["title"] = form_input_validate($_POST["title"], "title", "", false, 3);
        $save2["height"] = form_input_validate($_POST["height"], "height", "^[0-9]+\$", false, 3);
        $save2["width"] = form_input_validate($_POST["width"], "width", "^[0-9]+\$", false, 3);
        $save2["upper_limit"] = form_input_validate($_POST["upper_limit"], "upper_limit", "^(-?([0-9]+(\\.[0-9]*)?|[0-9]*\\.[0-9]+)([eE][+\\-]?[0-9]+)?)|U\$", strlen($_POST["upper_limit"]) === 0 ? true : false, 3);
        $save2["lower_limit"] = form_input_validate($_POST["lower_limit"], "lower_limit", "^(-?([0-9]+(\\.[0-9]*)?|[0-9]*\\.[0-9]+)([eE][+\\-]?[0-9]+)?)|U\$", strlen($_POST["lower_limit"]) === 0 ? true : false, 3);
        $save2["vertical_label"] = form_input_validate($_POST["vertical_label"], "vertical_label", "", true, 3);
        $save2["slope_mode"] = form_input_validate(isset($_POST["slope_mode"]) ? $_POST["slope_mode"] : "", "slope_mode", "", true, 3);
        $save2["auto_scale"] = form_input_validate(isset($_POST["auto_scale"]) ? $_POST["auto_scale"] : "", "auto_scale", "", true, 3);
        $save2["auto_scale_opts"] = form_input_validate($_POST["auto_scale_opts"], "auto_scale_opts", "", true, 3);
        $save2["auto_scale_log"] = form_input_validate(isset($_POST["auto_scale_log"]) ? $_POST["auto_scale_log"] : "", "auto_scale_log", "", true, 3);
        $save2["scale_log_units"] = form_input_validate(isset($_POST["scale_log_units"]) ? $_POST["scale_log_units"] : "", "scale_log_units", "", true, 3);
        $save2["auto_scale_rigid"] = form_input_validate(isset($_POST["auto_scale_rigid"]) ? $_POST["auto_scale_rigid"] : "", "auto_scale_rigid", "", true, 3);
        $save2["auto_padding"] = form_input_validate(isset($_POST["auto_padding"]) ? $_POST["auto_padding"] : "", "auto_padding", "", true, 3);
        $save2["base_value"] = form_input_validate($_POST["base_value"], "base_value", "^[0-9]+\$", false, 3);
        $save2["export"] = form_input_validate(isset($_POST["export"]) ? $_POST["export"] : "", "export", "", true, 3);
        $save2["unit_value"] = form_input_validate($_POST["unit_value"], "unit_value", "", true, 3);
        $save2["unit_exponent_value"] = form_input_validate($_POST["unit_exponent_value"], "unit_exponent_value", "^-?[0-9]+\$", true, 3);
        if (!is_error_message()) {
            $local_graph_id = sql_save($save1, "graph_local");
        }
        if (!is_error_message()) {
            $save2["local_graph_id"] = $local_graph_id;
            $graph_templates_graph_id = sql_save($save2, "graph_templates_graph");
            if ($graph_templates_graph_id) {
                raise_message(1);
                /* if template information chanegd, update all necessary template information */
                if ($_POST["graph_template_id"] != $_POST["_graph_template_id"]) {
                    /* check to see if the number of graph items differs, if it does; we need user input */
                    if (!empty($_POST["graph_template_id"]) && !empty($_POST["local_graph_id"]) && sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_id={$local_graph_id}")) != sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_id=0 and graph_template_id=" . $_POST["graph_template_id"]))) {
                        /* set the template back, since the user may choose not to go through with the change
                        			at this point */
                        db_execute("update graph_local set graph_template_id=" . $_POST["_graph_template_id"] . " where id={$local_graph_id}");
                        db_execute("update graph_templates_graph set graph_template_id=" . $_POST["_graph_template_id"] . " where local_graph_id={$local_graph_id}");
                        header("Location: graphs.php?action=graph_diff&id={$local_graph_id}&graph_template_id=" . $_POST["graph_template_id"]);
                        exit;
                    }
                }
            } else {
                raise_message(2);
            }
            /* update the title cache */
            update_graph_title_cache($local_graph_id);
        }
        if (!is_error_message() && $_POST["graph_template_id"] != $_POST["_graph_template_id"]) {
            change_graph_template($local_graph_id, $_POST["graph_template_id"], true);
        } elseif (!empty($_POST["graph_template_id"])) {
            update_graph_data_query_cache($local_graph_id);
        }
    }
    if (isset($_POST["save_component_input"])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post("local_graph_id"));
        /* ==================================================== */
        /* first; get the current graph template id */
        $graph_template_id = db_fetch_cell("select graph_template_id from graph_local where id=" . $_POST["local_graph_id"]);
        /* get all inputs that go along with this graph template */
        $input_list = db_fetch_assoc("select id,column_name from graph_template_input where graph_template_id={$graph_template_id}");
        if (sizeof($input_list) > 0) {
            /* modify for multi user start */
            $sql_join = "";
            if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) {
                $sql_join = "\r\n                INNER JOIN graph_local ON graph_templates_item.local_graph_id = graph_local.id \r\n                INNER JOIN host ON graph_local.host_id = host.id\r\n                INNER JOIN user_auth_perms ON host.id = user_auth_perms.item_id AND user_auth_perms.user_id = '" . $_SESSION["sess_user_id"] . "' AND user_auth_perms.type = '3'";
            }
            foreach ($input_list as $input) {
                /* we need to find out which graph items will be affected by saving this particular item */
                $item_list = db_fetch_assoc("select\r\n\t\t\t\tgraph_templates_item.id\r\n\t\t\t\tfrom (graph_template_input_defs,graph_templates_item)\r\n                {$sql_join}\r\n\t\t\t\twhere graph_template_input_defs.graph_template_item_id=graph_templates_item.local_graph_template_item_id\r\n\t\t\t\tand graph_templates_item.local_graph_id=" . $_POST["local_graph_id"] . "\r\n\t\t\t\tand graph_template_input_defs.graph_template_input_id=" . $input["id"]);
                /* modify for multi user end */
                /* loop through each item affected and update column data */
                if (sizeof($item_list) > 0) {
                    foreach ($item_list as $item) {
                        /* if we are changing templates, the POST vars we are searching for here will not exist.
                        			this is because the db and form are out of sync here, but it is ok to just skip over saving
                        			the inputs in this case. */
                        if (isset($_POST[$input["column_name"] . "_" . $input["id"]])) {
                            db_execute("update graph_templates_item set " . $input["column_name"] . "='" . $_POST[$input["column_name"] . "_" . $input["id"]] . "' where id=" . $item["id"]);
                        }
                    }
                }
            }
        }
    }
    if (isset($_POST["save_component_graph_diff"])) {
        if ($_POST["type"] == "1") {
            $intrusive = true;
        } elseif ($_POST["type"] == "2") {
            $intrusive = false;
        }
        change_graph_template($_POST["local_graph_id"], $_POST["graph_template_id"], $intrusive);
    }
    if (isset($_POST["save_component_graph_new"]) && empty($_POST["graph_template_id"])) {
        header("Location: graphs.php?action=graph_edit&host_id=" . $_POST["host_id"] . "&new=1");
    } elseif (is_error_message() || empty($_POST["local_graph_id"]) || isset($_POST["save_component_graph_diff"]) || $_POST["graph_template_id"] != $_POST["_graph_template_id"] || $_POST["host_id"] != $_POST["_host_id"]) {
        header("Location: graphs.php?action=graph_edit&id=" . (empty($local_graph_id) ? $_POST["local_graph_id"] : $local_graph_id) . (isset($_POST["host_id"]) ? "&host_id=" . $_POST["host_id"] : ""));
    } else {
        header("Location: graphs.php");
    }
}
コード例 #5
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;
}
コード例 #6
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"]);
        }
    }
}
コード例 #7
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;
}
コード例 #8
0
ファイル: graphs.php プロジェクト: MrWnn/cacti
function form_save()
{
    if (isset($_POST['save_component_graph_new']) && !empty($_POST['graph_template_id'])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post('graph_template_id'));
        /* ==================================================== */
        $save['id'] = $_POST['local_graph_id'];
        $save['graph_template_id'] = $_POST['graph_template_id'];
        $save['host_id'] = $_POST['host_id'];
        $local_graph_id = sql_save($save, 'graph_local');
        change_graph_template($local_graph_id, $_POST['graph_template_id'], true);
        /* update the title cache */
        update_graph_title_cache($local_graph_id);
    }
    if (isset($_POST['save_component_graph'])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post('graph_template_id'));
        input_validate_input_number(get_request_var_post('_graph_template_id'));
        /* ==================================================== */
        $save1['id'] = $_POST['local_graph_id'];
        $save1['host_id'] = $_POST['host_id'];
        $save1['graph_template_id'] = $_POST['graph_template_id'];
        $save2['id'] = $_POST['graph_template_graph_id'];
        $save2['local_graph_template_graph_id'] = $_POST['local_graph_template_graph_id'];
        $save2['graph_template_id'] = $_POST['graph_template_id'];
        $save2['image_format_id'] = form_input_validate($_POST['image_format_id'], 'image_format_id', '', true, 3);
        $save2['title'] = form_input_validate($_POST['title'], 'title', '', false, 3);
        $save2['height'] = form_input_validate($_POST['height'], 'height', '^[0-9]+$', false, 3);
        $save2['width'] = form_input_validate($_POST['width'], 'width', '^[0-9]+$', false, 3);
        $save2['upper_limit'] = form_input_validate($_POST['upper_limit'], 'upper_limit', "^(-?([0-9]+(\\.[0-9]*)?|[0-9]*\\.[0-9]+)([eE][+\\-]?[0-9]+)?)|U\$", strlen($_POST['upper_limit']) === 0 ? true : false, 3);
        $save2['lower_limit'] = form_input_validate($_POST['lower_limit'], 'lower_limit', "^(-?([0-9]+(\\.[0-9]*)?|[0-9]*\\.[0-9]+)([eE][+\\-]?[0-9]+)?)|U\$", strlen($_POST['lower_limit']) === 0 ? true : false, 3);
        $save2['vertical_label'] = form_input_validate($_POST['vertical_label'], 'vertical_label', '', true, 3);
        $save2['slope_mode'] = form_input_validate(isset($_POST['slope_mode']) ? $_POST['slope_mode'] : '', 'slope_mode', '', true, 3);
        $save2['auto_scale'] = form_input_validate(isset($_POST['auto_scale']) ? $_POST['auto_scale'] : '', 'auto_scale', '', true, 3);
        $save2['auto_scale_opts'] = form_input_validate($_POST['auto_scale_opts'], 'auto_scale_opts', '', true, 3);
        $save2['auto_scale_log'] = form_input_validate(isset($_POST['auto_scale_log']) ? $_POST['auto_scale_log'] : '', 'auto_scale_log', '', true, 3);
        $save2['scale_log_units'] = form_input_validate(isset($_POST['scale_log_units']) ? $_POST['scale_log_units'] : '', 'scale_log_units', '', true, 3);
        $save2['auto_scale_rigid'] = form_input_validate(isset($_POST['auto_scale_rigid']) ? $_POST['auto_scale_rigid'] : '', 'auto_scale_rigid', '', true, 3);
        $save2['auto_padding'] = form_input_validate(isset($_POST['auto_padding']) ? $_POST['auto_padding'] : '', 'auto_padding', '', true, 3);
        $save2['base_value'] = form_input_validate($_POST['base_value'], 'base_value', '^[0-9]+$', false, 3);
        $save2['export'] = form_input_validate(isset($_POST['export']) ? $_POST['export'] : '', 'export', '', true, 3);
        $save2['unit_value'] = form_input_validate($_POST['unit_value'], 'unit_value', '', true, 3);
        $save2['unit_exponent_value'] = form_input_validate($_POST['unit_exponent_value'], 'unit_exponent_value', '^-?[0-9]+$', true, 3);
        if (!is_error_message()) {
            $local_graph_id = sql_save($save1, 'graph_local');
        }
        if (!is_error_message()) {
            $save2['local_graph_id'] = $local_graph_id;
            $graph_templates_graph_id = sql_save($save2, 'graph_templates_graph');
            if ($graph_templates_graph_id) {
                raise_message(1);
                /* if template information chanegd, update all necessary template information */
                if ($_POST['graph_template_id'] != $_POST['_graph_template_id']) {
                    /* check to see if the number of graph items differs, if it does; we need user input */
                    if (!empty($_POST['graph_template_id']) && !empty($_POST['local_graph_id']) && sizeof(db_fetch_assoc_prepared('SELECT id FROM graph_templates_item WHERE local_graph_id = ?', array($local_graph_id))) != sizeof(db_fetch_assoc_prepared('SELECT id from graph_templates_item WHERE local_graph_id = 0 AND graph_template_id = ?', array($_POST['graph_template_id'])))) {
                        /* set the template back, since the user may choose not to go through with the change
                        			at this point */
                        db_execute_prepared('UPDATE graph_local SET graph_template_id = ? WHERE id = ?', array($_POST['_graph_template_id'], $local_graph_id));
                        db_execute_prepared('UPDATE graph_templates_graph SET graph_template_id = ? WHERE local_graph_id = ?', array($_POST['_graph_template_id'], $local_graph_id));
                        header('Location: graphs.php?action=graph_diff&id=$local_graph_id&graph_template_id=' . $_POST['graph_template_id']);
                        exit;
                    }
                }
            } else {
                raise_message(2);
            }
            /* update the title cache */
            update_graph_title_cache($local_graph_id);
        }
        if (!is_error_message() && $_POST['graph_template_id'] != $_POST['_graph_template_id']) {
            change_graph_template($local_graph_id, $_POST['graph_template_id'], true);
        } elseif (!empty($_POST['graph_template_id'])) {
            update_graph_data_query_cache($local_graph_id);
        }
    }
    if (isset($_POST['save_component_input'])) {
        /* ================= input validation ================= */
        input_validate_input_number(get_request_var_post('local_graph_id'));
        /* ==================================================== */
        /* first; get the current graph template id */
        $graph_template_id = db_fetch_cell_prepared('SELECT graph_template_id FROM graph_local WHERE id = ?', array($_POST['local_graph_id']));
        /* get all inputs that go along with this graph template, if templated */
        if ($graph_template_id > 0) {
            $input_list = db_fetch_assoc_prepared('SELECT id, column_name FROM graph_template_input WHERE graph_template_id = ?', array($graph_template_id));
            if (sizeof($input_list) > 0) {
                foreach ($input_list as $input) {
                    /* we need to find out which graph items will be affected by saving this particular item */
                    $item_list = db_fetch_assoc_prepared('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 graph_templates_item.local_graph_id = ?
						AND graph_template_input_defs.graph_template_input_id = ?', array($_POST['local_graph_id'], $input['id']));
                    /* loop through each item affected and update column data */
                    if (sizeof($item_list) > 0) {
                        foreach ($item_list as $item) {
                            /* if we are changing templates, the POST vars we are searching for here will not exist.
                            		 this is because the db and form are out of sync here, but it is ok to just skip over saving
                            		 the inputs in this case. */
                            if (isset($_POST[$input['column_name'] . '_' . $input['id']])) {
                                db_execute_prepared('UPDATE graph_templates_item SET ' . $input['column_name'] . ' = ? WHERE id = ?', array($_POST[$input['column_name'] . '_' . $input['id']], $item['id']));
                            }
                        }
                    }
                }
            }
        }
    }
    if (isset($_POST['save_component_graph_diff'])) {
        if ($_POST['type'] == '1') {
            $intrusive = true;
        } elseif ($_POST['type'] == '2') {
            $intrusive = false;
        }
        change_graph_template($_POST['local_graph_id'], $_POST['graph_template_id'], $intrusive);
    }
    if (isset($_POST['save_component_graph_new']) && empty($_POST['graph_template_id'])) {
        header('Location: graphs.php?action=graph_edit&host_id=' . $_POST['host_id'] . '&new=1');
    } elseif (is_error_message() || empty($_POST['local_graph_id']) || isset($_POST['save_component_graph_diff']) || $_POST['graph_template_id'] != $_POST['_graph_template_id'] || $_POST['host_id'] != $_POST['_host_id']) {
        header('Location: graphs.php?action=graph_edit&id=' . (empty($local_graph_id) ? $_POST['local_graph_id'] : $local_graph_id) . (isset($_POST['host_id']) ? '&host_id=' . $_POST['host_id'] : ''));
    } else {
        header('Location: graphs.php');
    }
}
コード例 #9
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;
}
コード例 #10
0
ファイル: graphs_form.php プロジェクト: songchin/Cacti
function graph_form_save() {
	if ((isset($_POST["save_component_graph_new"])) && (!empty($_POST["graph_template_id"]))) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("graph_template_id"));
		/* ==================================================== */

		$save["id"] = $_POST["local_graph_id"];
		$save["graph_template_id"] = $_POST["graph_template_id"];
		$save["device_id"] = $_POST["device_id"];

		$local_graph_id = sql_save($save, "graph_local");

		change_graph_template($local_graph_id, get_request_var_post("graph_template_id"), true);

		/* update the title cache */
		update_graph_title_cache($local_graph_id);
	}

	if (isset($_POST["save_component_graph"])) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("graph_template_id"));
		input_validate_input_number(get_request_var_post("_graph_template_id"));
		/* ==================================================== */

		$save1["id"] = form_input_validate($_POST["local_graph_id"], "local_graph_id", "^[0-9]+$", false, 3);
		$save1["device_id"] = form_input_validate($_POST["device_id"], "device_id", "^[-0-9]+$", false, 3);
		$save1["graph_template_id"] = form_input_validate($_POST["graph_template_id"], "graph_template_id", "^[0-9]+$", false, 3);

		$save2["id"] = form_input_validate($_POST["graph_template_graph_id"], "graph_template_graph_id", "^[0-9]+$", false, 3);
		$save2["local_graph_template_graph_id"] = form_input_validate($_POST["local_graph_template_graph_id"], "local_graph_template_graph_id", "^[0-9]+$", false, 3);
		$save2["graph_template_id"] = form_input_validate($_POST["graph_template_id"], "graph_template_id", "^[0-9]+$", false, 3);
		$save2["image_format_id"] = form_input_validate((isset($_POST["image_format_id"]) ? $_POST["image_format_id"] : ""), "image_format_id", "", true, 3);
		$save2["title"] = form_input_validate((isset($_POST["title"]) ? $_POST["title"] : ""), "title", "", (isset($_POST["t_title"]) ? true : false), 3);
		$save2["height"] = form_input_validate((isset($_POST["height"]) ? $_POST["height"] : ""), "height", "^[0-9]+$", (isset($_POST["t_height"]) ? true : false), 3);
		$save2["width"] = form_input_validate((isset($_POST["width"]) ? $_POST["width"] : ""), "width", "^[0-9]+$", (isset($_POST["t_width"]) ? true : false), 3);
		$save2["upper_limit"] = form_input_validate((isset($_POST["upper_limit"]) ? $_POST["upper_limit"] : ""), "upper_limit", "", (isset($_POST["t_upper_limit"]) ? true : false), 3);
		$save2["lower_limit"] = form_input_validate((isset($_POST["lower_limit"]) ? $_POST["lower_limit"] : ""), "lower_limit", "", (isset($_POST["t_lower_limit"]) ? true : false), 3);
		$save2["vertical_label"] = form_input_validate((isset($_POST["vertical_label"]) ? $_POST["vertical_label"] : ""), "vertical_label", "", true, 3);
		$save2["slope_mode"] = form_input_validate((isset($_POST["slope_mode"]) ? $_POST["slope_mode"] : ""), "slope_mode", "", true, 3);
		$save2["auto_scale"] = form_input_validate((isset($_POST["auto_scale"]) ? $_POST["auto_scale"] : ""), "auto_scale", "", true, 3);
		$save2["auto_scale_opts"] = form_input_validate((isset($_POST["auto_scale_opts"]) ? $_POST["auto_scale_opts"] : ""), "auto_scale_opts", "", true, 3);
		$save2["auto_scale_log"] = form_input_validate((isset($_POST["auto_scale_log"]) ? $_POST["auto_scale_log"] : ""), "auto_scale_log", "", true, 3);
		$save2["scale_log_units"] = form_input_validate((isset($_POST["scale_log_units"]) ? $_POST["scale_log_units"] : ""), "scale_log_units", "", true, 3);
		$save2["auto_scale_rigid"] = form_input_validate((isset($_POST["auto_scale_rigid"]) ? $_POST["auto_scale_rigid"] : ""), "auto_scale_rigid", "", true, 3);
		$save2["alt_y_grid"] = form_input_validate((isset($_POST["alt_y_grid"]) ? $_POST["alt_y_grid"] : ""), "alt_y_grid", "", true, 3);
		$save2["auto_padding"] = form_input_validate((isset($_POST["auto_padding"]) ? $_POST["auto_padding"] : ""), "auto_padding", "", true, 3);
		$save2["base_value"] = form_input_validate((isset($_POST["base_value"]) ? $_POST["base_value"] : ""), "base_value", "^(1000|1024)$", (isset($_POST["t_base_value"]) ? true : false), 3);
		$save2["export"] = form_input_validate((isset($_POST["export"]) ? $_POST["export"] : ""), "export", "", true, 3);
		$save2["unit_value"] = form_input_validate((isset($_POST["unit_value"]) ? $_POST["unit_value"] : ""), "unit_value", "^(none|NONE|[0-9]+:[0-9]+$)", true, 3);
		$save2["unit_exponent_value"] = form_input_validate((isset($_POST["unit_exponent_value"]) ? $_POST["unit_exponent_value"] : ""), "unit_exponent_value", "^-?[0-9]+$", true, 3);

		$save2["t_right_axis"] = form_input_validate((isset($_POST["t_right_axis"]) ? $_POST["t_right_axis"] : ""), "t_right_axis", "", true, 3);
		$save2["right_axis"] = form_input_validate((isset($_POST["right_axis"]) ? $_POST["right_axis"] : ""), "right_axis", "^[.0-9]+:-?[.0-9]+$", true, 3);
		$save2["t_right_axis_label"] = form_input_validate((isset($_POST["t_right_axis_label"]) ? $_POST["t_right_axis_label"] : ""), "t_right_axis_label", "", true, 3);
		$save2["right_axis_label"] = form_input_validate((isset($_POST["right_axis_label"]) ? $_POST["right_axis_label"] : ""), "right_axis_label", "", true, 3);
		$save2["t_right_axis_format"] = form_input_validate((isset($_POST["t_right_axis_format"]) ? $_POST["t_right_axis_format"] : ""), "t_right_axis_format", "", true, 3);
		$save2["right_axis_format"] = form_input_validate((isset($_POST["right_axis_format"]) ? $_POST["right_axis_format"] : ""), "right_axis_format", "^[0-9]+$", true, 3);
		$save2["t_only_graph"] = form_input_validate((isset($_POST["t_only_graph"]) ? $_POST["t_only_graph"] : ""), "t_only_graph", "", true, 3);
		$save2["only_graph"] = form_input_validate((isset($_POST["only_graph"]) ? $_POST["only_graph"] : ""), "only_graph", "", true, 3);
		$save2["t_full_size_mode"] = form_input_validate((isset($_POST["t_full_size_mode"]) ? $_POST["t_full_size_mode"] : ""), "t_full_size_mode", "", true, 3);
		$save2["full_size_mode"] = form_input_validate((isset($_POST["full_size_mode"]) ? $_POST["full_size_mode"] : ""), "full_size_mode", "", true, 3);
		$save2["t_no_gridfit"] = form_input_validate((isset($_POST["t_no_gridfit"]) ? $_POST["t_no_gridfit"] : ""), "t_no_gridfit", "", true, 3);
		$save2["no_gridfit"] = form_input_validate((isset($_POST["no_gridfit"]) ? $_POST["no_gridfit"] : ""), "no_gridfit", "", true, 3);
		$save2["t_x_grid"] = form_input_validate((isset($_POST["t_x_grid"]) ? $_POST["t_x_grid"] : ""), "t_x_grid", "", true, 3);
		$save2["x_grid"] = form_input_validate((isset($_POST["x_grid"]) ? $_POST["x_grid"] : ""), "x_grid", "^[0-9]+$", true, 3);
		$save2["t_unit_length"] = form_input_validate((isset($_POST["t_unit_length"]) ? $_POST["t_unit_length"] : ""), "t_unit_length", "", true, 3);
		$save2["unit_length"] = form_input_validate((isset($_POST["unit_length"]) ? $_POST["unit_length"] : ""), "unit_length", "^[0-9]+$", true, 3);
		$save2["t_colortag_back"] = form_input_validate((isset($_POST["t_colortag_back"]) ? $_POST["t_colortag_back"] : ""), "t_colortag_back", "", true, 3);
		$save2["colortag_back"] = form_input_validate((isset($_POST["colortag_back"]) ? $_POST["colortag_back"] : ""), "colortag_back", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_canvas"] = form_input_validate((isset($_POST["t_colortag_canvas"]) ? $_POST["t_colortag_canvas"] : ""), "t_colortag_canvas", "", true, 3);
		$save2["colortag_canvas"] = form_input_validate((isset($_POST["colortag_canvas"]) ? $_POST["colortag_canvas"] : ""), "colortag_canvas", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_shadea"] = form_input_validate((isset($_POST["t_colortag_shadea"]) ? $_POST["t_colortag_shadea"] : ""), "t_colortag_shadea", "", true, 3);
		$save2["colortag_shadea"] = form_input_validate((isset($_POST["colortag_shadea"]) ? $_POST["colortag_shadea"] : ""), "colortag_shadea", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_shadeb"] = form_input_validate((isset($_POST["t_colortag_shadeb"]) ? $_POST["t_colortag_shadeb"] : ""), "t_colortag_shadeb", "", true, 3);
		$save2["colortag_shadeb"] = form_input_validate((isset($_POST["colortag_shadeb"]) ? $_POST["colortag_shadeb"] : ""), "colortag_shadeb", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_grid"] = form_input_validate((isset($_POST["t_colortag_grid"]) ? $_POST["t_colortag_grid"] : ""), "t_colortag_grid", "", true, 3);
		$save2["colortag_grid"] = form_input_validate((isset($_POST["colortag_grid"]) ? $_POST["colortag_grid"] : ""), "colortag_grid", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_mgrid"] = form_input_validate((isset($_POST["t_colortag_mgrid"]) ? $_POST["t_colortag_mgrid"] : ""), "t_colortag_mgrid", "", true, 3);
		$save2["colortag_mgrid"] = form_input_validate((isset($_POST["colortag_mgrid"]) ? $_POST["colortag_mgrid"] : ""), "colortag_mgrid", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_font"] = form_input_validate((isset($_POST["t_colortag_font"]) ? $_POST["t_colortag_font"] : ""), "t_colortag_font", "", true, 3);
		$save2["colortag_font"] = form_input_validate((isset($_POST["colortag_font"]) ? $_POST["colortag_font"] : ""), "colortag_font", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_axis"] = form_input_validate((isset($_POST["t_colortag_axis"]) ? $_POST["t_colortag_axis"] : ""), "t_colortag_axis", "", true, 3);
		$save2["colortag_axis"] = form_input_validate((isset($_POST["colortag_axis"]) ? $_POST["colortag_axis"] : ""), "colortag_axis", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_frame"] = form_input_validate((isset($_POST["t_colortag_frame"]) ? $_POST["t_colortag_frame"] : ""), "t_colortag_frame", "", true, 3);
		$save2["colortag_frame"] = form_input_validate((isset($_POST["colortag_frame"]) ? $_POST["colortag_frame"] : ""), "colortag_frame", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_colortag_arrow"] = form_input_validate((isset($_POST["t_colortag_arrow"]) ? $_POST["t_colortag_arrow"] : ""), "t_colortag_arrow", "", true, 3);
		$save2["colortag_arrow"] = form_input_validate((isset($_POST["colortag_arrow"]) ? $_POST["colortag_arrow"] : ""), "colortag_arrow", "^[0-9a-fA-F]{0,8}$", true, 3);
		$save2["t_font_render_mode"] = form_input_validate((isset($_POST["t_font_render_mode"]) ? $_POST["t_font_render_mode"] : ""), "t_font_render_mode", "", true, 3);
		$save2["font_render_mode"] = form_input_validate((isset($_POST["font_render_mode"]) ? $_POST["font_render_mode"] : ""), "font_render_mode", "", true, 3);
		$save2["t_font_smoothing_threshold"] = form_input_validate((isset($_POST["t_font_smoothing_threshold"]) ? $_POST["t_font_smoothing_threshold"] : ""), "t_font_smoothing_threshold", "", true, 3);
		$save2["font_smoothing_threshold"] = form_input_validate((isset($_POST["font_smoothing_threshold"]) ? $_POST["font_smoothing_threshold"] : ""), "font_smoothing_threshold", "^[0-9]*$", true, 3);
		$save2["t_graph_render_mode"] = form_input_validate((isset($_POST["t_graph_render_mode"]) ? $_POST["t_graph_render_mode"] : ""), "t_graph_render_mode", "", true, 3);
		$save2["graph_render_mode"] = form_input_validate((isset($_POST["graph_render_mode"]) ? $_POST["graph_render_mode"] : ""), "graph_render_mode", "", true, 3);
		$save2["t_pango_markup"] = form_input_validate((isset($_POST["t_pango_markup"]) ? $_POST["t_pango_markup"] : ""), "t_pango_markup", "", true, 3);
		$save2["pango_markup"] = form_input_validate((isset($_POST["pango_markup"]) ? $_POST["pango_markup"] : ""), "pango_markup", "", true, 3);
		$save2["t_interlaced"] = form_input_validate((isset($_POST["t_interlaced"]) ? $_POST["t_interlaced"] : ""), "t_interlaced", "", true, 3);
		$save2["interlaced"] = form_input_validate((isset($_POST["interlaced"]) ? $_POST["interlaced"] : ""), "interlaced", "", true, 3);
		$save2["t_tab_width"] = form_input_validate((isset($_POST["t_tab_width"]) ? $_POST["t_tab_width"] : ""), "t_tab_width", "", true, 3);
		$save2["tab_width"] = form_input_validate((isset($_POST["tab_width"]) ? $_POST["tab_width"] : ""), "tab_width", "^[0-9]*$", true, 3);
		$save2["t_watermark"] = form_input_validate((isset($_POST["t_watermark"]) ? $_POST["t_watermark"] : ""), "t_watermark", "", true, 3);
		$save2["watermark"] = form_input_validate((isset($_POST["watermark"]) ? $_POST["watermark"] : ""), "watermark", "", true, 3);
		$save2["t_force_rules_legend"] = form_input_validate((isset($_POST["t_force_rules_legend"]) ? $_POST["t_force_rules_legend"] : ""), "t_force_rules_legend", "", true, 3);
		$save2["force_rules_legend"] = form_input_validate((isset($_POST["force_rules_legend"]) ? $_POST["force_rules_legend"] : ""), "force_rules_legend", "", true, 3);
		$save2["t_legend_position"] = form_input_validate((isset($_POST["t_legend_position"]) ? $_POST["t_legend_position"] : ""), "t_legend_position", "", true, 3);
		$save2["legend_position"] = form_input_validate((isset($_POST["legend_position"]) ? $_POST["legend_position"] : ""), "legend_position", "", true, 3);
		$save2["t_legend_direction"] = form_input_validate((isset($_POST["t_legend_direction"]) ? $_POST["t_legend_direction"] : ""), "t_legend_direction", "", true, 3);
		$save2["legend_direction"] = form_input_validate((isset($_POST["legend_direction"]) ? $_POST["legend_direction"] : ""), "legend_direction", "", true, 3);
		$save2["t_grid_dash"] = form_input_validate((isset($_POST["t_grid_dash"]) ? $_POST["t_grid_dash"] : ""), "t_grid_dash", "", true, 3);
		$save2["grid_dash"] = form_input_validate((isset($_POST["grid_dash"]) ? $_POST["grid_dash"] : ""), "grid_dash", "^[0-9]*:[0-9]*$", true, 3);
		$save2["t_border"] = form_input_validate((isset($_POST["t_border"]) ? $_POST["t_border"] : ""), "t_border", "", true, 3);
		$save2["border"] = form_input_validate((isset($_POST["border"]) ? $_POST["border"] : ""), "border", "^[0-9]*$", true, 3);

		if (!is_error_message()) {
			$local_graph_id = sql_save($save1, "graph_local");
		}

		if (!is_error_message()) {
			$save2["local_graph_id"] = $local_graph_id;
			$graph_templates_graph_id = sql_save($save2, "graph_templates_graph");

			if ($graph_templates_graph_id) {
				raise_message(1);

				/* if template information chanegd, update all necessary template information */
				if ($_POST["graph_template_id"] != $_POST["_graph_template_id"]) {
					/* check to see if the number of graph items differs, if it does; we need user input */
					if ((!empty($_POST["graph_template_id"])) && (!empty($_POST["local_graph_id"])) && (sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_id=$local_graph_id")) != sizeof(db_fetch_assoc("select id from graph_templates_item where local_graph_id=0 and graph_template_id=" . $_POST["graph_template_id"])))) {
						/* set the template back, since the user may choose not to go through with the change
						at this point */
						db_execute("update graph_local set graph_template_id=" . $_POST["_graph_template_id"] . " where id=$local_graph_id");
						db_execute("update graph_templates_graph set graph_template_id=" . $_POST["_graph_template_id"] . " where local_graph_id=$local_graph_id");

						header("Location: graphs.php?action=graph_diff&id=$local_graph_id&graph_template_id=" . $_POST["graph_template_id"]);
						exit;
					}
				}
			}else{
				raise_message(2);
			}

			/* update the title cache */
			update_graph_title_cache($local_graph_id);
		}

		if ((!is_error_message()) && ($_POST["graph_template_id"] != $_POST["_graph_template_id"])) {
			change_graph_template($local_graph_id, get_request_var_post("graph_template_id"), true);
		}elseif (!empty($_POST["graph_template_id"])) {
			update_graph_data_query_cache($local_graph_id);
		}
	}

	if (isset($_POST["save_component_input"])) {
		/* ================= input validation ================= */
		input_validate_input_number(get_request_var_post("local_graph_id"));
		/* ==================================================== */

		/* first; get the current graph template id */
		$graph_template_id = db_fetch_cell("select graph_template_id from graph_local where id=" . $_POST["local_graph_id"]);

		/* get all inputs that go along with this graph template */
		$input_list = db_fetch_assoc("select id,column_name from graph_template_input where graph_template_id=$graph_template_id");

		if (sizeof($input_list) > 0) {
		foreach ($input_list as $input) {
			/* 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 graph_templates_item.local_graph_id=" . $_POST["local_graph_id"] . "
				and graph_template_input_defs.graph_template_input_id=" . $input["id"]);

			/* loop through each item affected and update column data */
			if (sizeof($item_list) > 0) {
			foreach ($item_list as $item) {
				/* if we are changing templates, the POST vars we are searching for here will not exist.
				this is because the db and form are out of sync here, but it is ok to just skip over saving
				the inputs in this case. */
				if (isset($_POST{$input["column_name"] . "_" . $input["id"]})) {
					db_execute("update graph_templates_item set " . $input["column_name"] . "='" . $_POST{$input["column_name"] . "_" . $input["id"]} . "' where id=" . $item["id"]);
				}
			}
			}
		}
		}
	}

	if (isset($_POST["save_component_graph_diff"])) {
		if (get_request_var_post("type") == "1") {
			$intrusive = true;
		}elseif (get_request_var_post("type") == "2") {
			$intrusive = false;
		}

		change_graph_template(get_request_var_post("local_graph_id"), get_request_var_post("graph_template_id"), $intrusive);
	}

	if ((isset($_POST["save_component_graph_new"])) && (empty($_POST["graph_template_id"]))) {
		header("Location: graphs.php?action=graph_edit&device_id=" . $_POST["device_id"] . "&new=1");
	}elseif ((is_error_message()) || (empty($_POST["local_graph_id"])) || (isset($_POST["save_component_graph_diff"])) || ($_POST["graph_template_id"] != $_POST["_graph_template_id"]) || ($_POST["device_id"] != $_POST["_device_id"])) {
		header("Location: graphs.php?action=graph_edit&id=" . (empty($local_graph_id) ? $_POST["local_graph_id"] : $local_graph_id) . (isset($_POST["device_id"]) ? "&device_id=" . $_POST["device_id"] : ""));
	}else{
		header("Location: graphs.php");
	}
	exit;
}