예제 #1
1
파일: rrd.php 프로젝트: songchin/Cacti
function rrdtool_function_graph($local_graph_id, $rra_id, $graph_data_array, $rrd_struc = array()) {
	global $config;

	include_once($config["library_path"] . "/cdef.php");
	include_once($config["library_path"] . "/graph_variables.php");
	include($config["include_path"] . "/config_arrays.php");

	/* before we do anything; make sure the user has permission to view this graph,
	if not then get out */
	if ((read_config_option("global_auth") == "on") && (isset($_SESSION["sess_user_id"]))) {
		$access_denied = !(is_graph_allowed($local_graph_id));

		if ($access_denied == true) {
			return "GRAPH ACCESS DENIED";
		}
	}

	/* find the step and how often this graph is updated with new data */
	$ds_step = db_fetch_cell("select
		data_template_data.rrd_step
		from data_template_data,data_template_rrd,graph_templates_item
		where graph_templates_item.task_item_id=data_template_rrd.id
		and data_template_rrd.local_data_id=data_template_data.local_data_id
		and graph_templates_item.local_graph_id=$local_graph_id
		limit 0,1");
	$ds_step = empty($ds_step) ? 300 : $ds_step;

	/* if no rra was specified, we need to figure out which one RRDTool will choose using
	 * "best-fit" resolution fit algorithm */
	if (empty($rra_id)) {
		if ((empty($graph_data_array["graph_start"])) || (empty($graph_data_array["graph_end"]))) {
			$rra["rows"] = 600;
			$rra["steps"] = 1;
			$rra["timespan"] = 86400;
		}else{
			/* get a list of RRAs related to this graph */
			$rras = get_associated_rras($local_graph_id);

			if (sizeof($rras) > 0) {
				foreach ($rras as $unchosen_rra) {
					/* the timespan specified in the RRA "timespan" field may not be accurate */
					$real_timespan = ($ds_step * $unchosen_rra["steps"] * $unchosen_rra["rows"]);

					/* make sure the current start/end times fit within each RRA's timespan */
					if ( (($graph_data_array["graph_end"] - $graph_data_array["graph_start"]) <= $real_timespan) && ((time() - $graph_data_array["graph_start"]) <= $real_timespan) ) {
						/* is this RRA better than the already chosen one? */
						if ((isset($rra)) && ($unchosen_rra["steps"] < $rra["steps"])) {
							$rra = $unchosen_rra;
						}else if (!isset($rra)) {
							$rra = $unchosen_rra;
						}
					}
				}
			}

			if (!isset($rra)) {
				$rra["rows"] = 600;
				$rra["steps"] = 1;
			}
		}
	}else{
		$rra = db_fetch_row("select timespan,rows,steps from rra where id=$rra_id");
	}

	$seconds_between_graph_updates = ($ds_step * $rra["steps"]);

	$graph = db_fetch_row("select
		graph_local.host_id,
		graph_local.snmp_query_id,
		graph_local.snmp_index,
		graph_templates_graph.title_cache,
		graph_templates_graph.vertical_label,
		graph_templates_graph.auto_scale,
		graph_templates_graph.auto_scale_opts,
		graph_templates_graph.auto_scale_log,
		graph_templates_graph.auto_scale_rigid,
		graph_templates_graph.auto_padding,
		graph_templates_graph.base_value,
		graph_templates_graph.upper_limit,
		graph_templates_graph.lower_limit,
		graph_templates_graph.height,
		graph_templates_graph.width,
		graph_templates_graph.image_format_id,
		graph_templates_graph.unit_value,
		graph_templates_graph.unit_exponent_value,
		graph_templates_graph.export
		from graph_templates_graph,graph_local
		where graph_local.id=graph_templates_graph.local_graph_id
		and graph_templates_graph.local_graph_id=$local_graph_id");

	/* lets make that sql query... */
	$graph_items = db_fetch_assoc("select
		graph_templates_item.id as graph_templates_item_id,
		graph_templates_item.cdef_id,
		graph_templates_item.text_format,
		graph_templates_item.value,
		graph_templates_item.hard_return,
		graph_templates_item.consolidation_function_id,
		graph_templates_item.graph_type_id,
		graph_templates_gprint.gprint_text,
		colors.hex,
		data_template_rrd.id as data_template_rrd_id,
		data_template_rrd.local_data_id,
		data_template_rrd.rrd_minimum,
		data_template_rrd.rrd_maximum,
		data_template_rrd.data_source_name,
		data_template_rrd.local_data_template_rrd_id
		from graph_templates_item
		left join data_template_rrd on graph_templates_item.task_item_id=data_template_rrd.id
		left join colors on graph_templates_item.color_id=colors.id
		left join graph_templates_gprint on graph_templates_item.gprint_id=graph_templates_gprint.id
		where graph_templates_item.local_graph_id=$local_graph_id
		order by graph_templates_item.sequence");

	/* +++++++++++++++++++++++ GRAPH OPTIONS +++++++++++++++++++++++ */

	/* define some variables */
	$scale = "";
	$rigid = "";
	$unit_exponent_value = "";
	$graph_legend = "";
	$graph_defs = "";
	$txt_graph_items = "";
	$text_padding = "";
	$greatest_text_format = 0;
	$last_graph_type = "";

		if ($graph["auto_scale"] == "on") {
		if ($graph["auto_scale_opts"] == "1") {
			$scale = "--alt-autoscale" . RRD_NL;
		}elseif ($graph["auto_scale_opts"] == "2") {
			$scale = "--alt-autoscale-max" . RRD_NL;
			$scale .= "--lower-limit=" . $graph["lower_limit"] . RRD_NL;
		}

		if ($graph["auto_scale_log"] == "on") {
			$scale .= "--logarithmic" . RRD_NL;
		}
	}else{
		$scale =  "--upper-limit=" . $graph["upper_limit"] . RRD_NL;
		$scale .= "--lower-limit=" . $graph["lower_limit"] . RRD_NL;
	}

	if ($graph["auto_scale_rigid"] == "on") {
		$rigid = "--rigid" . RRD_NL;
	}

	if (!empty($graph["unit_value"])) {
		$unit_value = "--unit=" . $graph["unit_value"] . RRD_NL;
	}

	if (ereg("^[0-9]+$", $graph["unit_exponent_value"])) {
		$unit_exponent_value = "--units-exponent=" . $graph["unit_exponent_value"] . RRD_NL;
	}

	/*
	 * optionally you can specify and array that overrides some of the db's values, lets set
	 * that all up here
	 */

	/* override: graph start time */
	if ((!isset($graph_data_array["graph_start"])) || ($graph_data_array["graph_start"] == "0")) {
		$graph_start = -($rra["timespan"]);
	}else{
		$graph_start = $graph_data_array["graph_start"];
	}

	/* override: graph end time */
	if ((!isset($graph_data_array["graph_end"])) || ($graph_data_array["graph_end"] == "0")) {
		$graph_end = -($seconds_between_graph_updates);
	}else{
		$graph_end = $graph_data_array["graph_end"];
	}

	/* override: graph height (in pixels) */
	if (isset($graph_data_array["graph_height"])) {
		$graph_height = $graph_data_array["graph_height"];
	}else{
		$graph_height = $graph["height"];
	}

	/* override: graph width (in pixels) */
	if (isset($graph_data_array["graph_width"])) {
		$graph_width = $graph_data_array["graph_width"];
	}else{
		$graph_width = $graph["width"];
	}

	/* override: skip drawing the legend? */
	if (isset($graph_data_array["graph_nolegend"])) {
		$graph_legend = "--no-legend" . RRD_NL;
	}else{
		$graph_legend = "";
	}

	/* export options */
	if (isset($graph_data_array["export"])) {
		$graph_opts = read_config_option("path_html_export") . "/" . $graph_data_array["export_filename"] . RRD_NL;
	}else{
		if (empty($graph_data_array["output_filename"])) {
				$graph_opts = "-" . RRD_NL;
		}else{
			$graph_opts = $graph_data_array["output_filename"] . RRD_NL;
		}
	}

	/* setup date format */
	$date_fmt = read_graph_config_option("default_date_format");
	$datechar = read_graph_config_option("default_datechar");

	if ($datechar == GDC_HYPHEN) {
		$datechar = "-";
	}else {
		$datechar = "/";
	}

	switch ($date_fmt) {
		case GD_MO_D_Y:
			$graph_date = "m" . $datechar . "d" . $datechar . "Y H:i:s";
			break;
		case GD_MN_D_Y:
			$graph_date = "M" . $datechar . "d" . $datechar . "Y H:i:s";
			break;
		case GD_D_MO_Y:
			$graph_date = "d" . $datechar . "m" . $datechar . "Y H:i:s";
			break;
		case GD_D_MN_Y:
			$graph_date = "d" . $datechar . "M" . $datechar . "Y H:i:s";
			break;
		case GD_Y_MO_D:
			$graph_date = "Y" . $datechar . "m" . $datechar . "d H:i:s";
			break;
		case GD_Y_MN_D:
			$graph_date = "Y" . $datechar . "M" . $datechar . "d H:i:s";
			break;
	}

	/* display the timespan for zoomed graphs */
	if ((isset($graph_data_array["graph_start"])) && (isset($graph_data_array["graph_end"]))) {
		if (($graph_data_array["graph_start"] < 0) && ($graph_data_array["graph_end"] < 0)) {
			$graph_legend .= "COMMENT:\"From " . date($graph_date, time()+$graph_data_array["graph_start"]) . " To " . date($graph_date, time()+$graph_data_array["graph_end"]) . "\\c\"" . RRD_NL . "COMMENT:\"\\n\"" . RRD_NL;
		}else if (($graph_data_array["graph_start"] >= 0) && ($graph_data_array["graph_end"] >= 0)) {
			$graph_legend .= "COMMENT:\"From " . date($graph_date, $graph_data_array["graph_start"]) . " To " . date($graph_date, $graph_data_array["graph_end"]) . "\\c\"" . RRD_NL . "COMMENT:\"\\n\"" . RRD_NL;
		}
	}

	/* basic graph options */
	$graph_opts .=
		"--imgformat=" . $image_types{$graph["image_format_id"]} . RRD_NL .
		"--start=$graph_start" . RRD_NL .
		"--end=$graph_end" . RRD_NL .
		"--title=\"" . $graph["title_cache"] . "\"" . RRD_NL .
		"$rigid" .
		"--base=" . $graph["base_value"] . RRD_NL .
		"--height=$graph_height" . RRD_NL .
		"--width=$graph_width" . RRD_NL .
		"$scale" .
		"$unit_exponent_value" .
		"$graph_legend" .
		"--vertical-label=\"" . $graph["vertical_label"] . "\"" . RRD_NL;

	$i = 0;
	if (sizeof($graph_items > 0)) {
	foreach ($graph_items as $graph_item) {
		if ((ereg("(AREA|STACK|LINE[123])", $graph_item_types{$graph_item["graph_type_id"]})) && ($graph_item["data_source_name"] != "")) {
			/* use a user-specified ds path if one is entered */
			$data_source_path = get_data_source_path($graph_item["local_data_id"], true);

			/* FOR WIN32: Escape all colon for drive letters (ex. D\:/path/to/rra) */
			$data_source_path = str_replace(":", "\:", $data_source_path);

			if (!empty($data_source_path)) {
				/* NOTE: (Update) Data source DEF names are created using the graph_item_id; then passed
				to a function that matches the digits with letters. rrdtool likes letters instead
				of numbers in DEF names; especially with CDEF's. cdef's are created
				the same way, except a 'cdef' is put on the beginning of the hash */
				$graph_defs .= "DEF:" . generate_graph_def_name(strval($i)) . "=\"$data_source_path\":" . $graph_item["data_source_name"] . ":" . $consolidation_functions{$graph_item["consolidation_function_id"]} . RRD_NL;

				//print "ds: " . $graph_item["data_template_rrd_id"] . "<br>";
				$cf_ds_cache{$graph_item["data_template_rrd_id"]}{$graph_item["consolidation_function_id"]} = "$i";

				$i++;
			}
		}

		/* +++++++++++++++++++++++ LEGEND: TEXT SUBSITUTION (<>'s) +++++++++++++++++++++++ */

		/* note the current item_id for easy access */
		$graph_item_id = $graph_item["graph_templates_item_id"];

		/* the following fields will be searched for graph variables */
		$variable_fields = array(
			"text_format" => array(
				"process_no_legend" => false
				),
			"value" => array(
				"process_no_legend" => true
				)
			);

		/* loop through each field that we want to substitute values for:
		currently: text format and value */
		while (list($field_name, $field_array) = each($variable_fields)) {
			/* certain fields do not require values when the legend is not to be shown */
			if (($field_array["process_no_legend"] == false) && (isset($graph_data_array["graph_nolegend"]))) {
				continue;
			}

			$graph_variables[$field_name][$graph_item_id] = $graph_item[$field_name];

			/* date/time substitution */
			if (strstr($graph_variables[$field_name][$graph_item_id], "|date_time|")) {
				$graph_variables[$field_name][$graph_item_id] = str_replace("|date_time|", date('D d M H:i:s T Y', strtotime(db_fetch_cell("select value from settings where name='date'"))), $graph_variables[$field_name][$graph_item_id]);
			}

			/* data query variables */
			if (preg_match("/\|query_[a-zA-Z0-9_]+\|/", $graph_variables[$field_name][$graph_item_id])) {
				/* default to the graph data query information from the graph */
				if (empty($graph_item["local_data_id"])) {
					$graph_variables[$field_name][$graph_item_id] = substitute_snmp_query_data($graph_variables[$field_name][$graph_item_id], $graph["host_id"], $graph["snmp_query_id"], $graph["snmp_index"]);
				/* use the data query information from the data source if possible */
				}else{
					$data_local = db_fetch_row("select snmp_index,snmp_query_id,host_id from data_local where id='" . $graph_item["local_data_id"] . "'");
					$graph_variables[$field_name][$graph_item_id] = substitute_snmp_query_data($graph_variables[$field_name][$graph_item_id], $data_local["host_id"], $data_local["snmp_query_id"], $data_local["snmp_index"]);
				}
			}

			/* 95th percentile */
			if (preg_match_all("/\|95:(bits|bytes):(\d):(current|total|max)(:(\d))?\|/", $graph_variables[$field_name][$graph_item_id], $matches, PREG_SET_ORDER)) {
				foreach ($matches as $match) {
					$graph_variables[$field_name][$graph_item_id] = str_replace($match[0], variable_ninety_fifth_percentile($match, $graph_item, $graph_items, $graph_start, $graph_end), $graph_variables[$field_name][$graph_item_id]);
				}
			}

			/* bandwidth summation */
			if (preg_match_all("/\|sum:(\d|auto):(current|total|atomic):(\d):(\d+|auto)\|/", $graph_variables[$field_name][$graph_item_id], $matches, PREG_SET_ORDER)) {
				foreach ($matches as $match) {
					$graph_variables[$field_name][$graph_item_id] = str_replace($match[0], variable_bandwidth_summation($match, $graph_item, $graph_items, $graph_start, $graph_end, $rra["steps"], $ds_step), $graph_variables[$field_name][$graph_item_id]);
				}
			}
		}

		/* if we are not displaying a legend there is no point in us even processing the auto padding,
		text format stuff. */
		if (!isset($graph_data_array["graph_nolegend"])) {
			/* set hard return variable if selected (\n) */
			if ($graph_item["hard_return"] == "on") {
				$hardreturn[$graph_item_id] = "\\n";
			}else{
				$hardreturn[$graph_item_id] = "";
			}

			/* +++++++++++++++++++++++ LEGEND: AUTO PADDING (<>'s) +++++++++++++++++++++++ */

			/* PADDING: remember this is not perfect! its main use is for the basic graph setup of:
			AREA - GPRINT-CURRENT - GPRINT-AVERAGE - GPRINT-MAXIMUM \n
			of course it can be used in other situations, however may not work as intended.
			If you have any additions to this small peice of code, feel free to send them to me. */
			if ($graph["auto_padding"] == "on") {
				/* only applies to AREA and STACK */
				if (ereg("(AREA|STACK|LINE[123])", $graph_item_types{$graph_item["graph_type_id"]})) {
					$text_format_lengths{$graph_item["data_template_rrd_id"]} = strlen($graph_variables["text_format"][$graph_item_id]);

					if ((strlen($graph_variables["text_format"][$graph_item_id]) > $greatest_text_format) && ($graph_item_types{$graph_item["graph_type_id"]} != "COMMENT")) {
						$greatest_text_format = strlen($graph_variables["text_format"][$graph_item_id]);
					}
				}
			}
		}
	}
	}

	/* +++++++++++++++++++++++ GRAPH ITEMS: CDEF's +++++++++++++++++++++++ */

	$i = 0;
	reset($graph_items);

	if (sizeof($graph_items) > 0) {
	foreach ($graph_items as $graph_item) {
		/* first we need to check if there is a DEF for the current data source/cf combination. if so,
		we will use that */
		if (isset($cf_ds_cache{$graph_item["data_template_rrd_id"]}{$graph_item["consolidation_function_id"]})) {
			$cf_id = $graph_item["consolidation_function_id"];
		}else{
		/* if there is not a DEF defined for the current data source/cf combination, then we will have to
		improvise. choose the first available cf in the following order: AVERAGE, MAX, MIN, LAST */
			if (isset($cf_ds_cache{$graph_item["data_template_rrd_id"]}[1])) {
				$cf_id = 1; /* CF: AVERAGE */
			}elseif (isset($cf_ds_cache{$graph_item["data_template_rrd_id"]}[3])) {
				$cf_id = 3; /* CF: MAX */
			}elseif (isset($cf_ds_cache{$graph_item["data_template_rrd_id"]}[2])) {
				$cf_id = 2; /* CF: MIN */
			}elseif (isset($cf_ds_cache{$graph_item["data_template_rrd_id"]}[4])) {
				$cf_id = 4; /* CF: LAST */
			}else{
				$cf_id = 1; /* CF: AVERAGE */
			}
		}

		/* make cdef string here; a note about CDEF's in cacti. A CDEF is neither unique to a
		data source of global cdef, but is unique when those two variables combine. */
		$cdef_graph_defs = ""; $cdef_total_ds = ""; $cdef_similar_ds = "";

		if ((!empty($graph_item["cdef_id"])) && (!isset($cdef_cache{$graph_item["cdef_id"]}{$graph_item["data_template_rrd_id"]}[$cf_id]))) {
			$cdef_string = get_cdef($graph_item["cdef_id"]);

			/* create cdef string for "total all data sources" if requested */
			if (ereg("ALL_DATA_SOURCES_(NO)?DUPS", $cdef_string)) {
				$item_count = 0;
				for ($t=0;($t<count($graph_items));$t++) {
					if ((ereg("(AREA|STACK|LINE[123])", $graph_item_types{$graph_items[$t]["graph_type_id"]})) && (!empty($graph_items[$t]["data_template_rrd_id"]))) {
						/* if the user screws up CF settings, PHP will generate warnings if left unchecked */
						if (isset($cf_ds_cache{$graph_items[$t]["data_template_rrd_id"]}[$cf_id])) {
							$def_name = generate_graph_def_name(strval($cf_ds_cache{$graph_items[$t]["data_template_rrd_id"]}[$cf_id]));
							$cdef_total_ds .= ($item_count == 0 ? "" : ",") . "TIME," . (time() - $seconds_between_graph_updates) . ",GT,$def_name,$def_name,UN,0,$def_name,IF,IF"; /* convert unknowns to '0' first */
							$item_count++;
						}
					}
				}

				/* if there is only one item to total, don't even bother with the summation. otherwise
				cdef=a,b,c,+,+ is fine. */
				if ($item_count > 1) {
					$cdef_total_ds .= str_repeat(",+", ($item_count - 2)) . ",+";
				}
			}

			/* create cdef string for "total similar data sources" if requested */
			if (ereg("SIMILAR_DATA_SOURCES_(NO)?DUPS", $cdef_string) ) {
				$sources_seen = array();
				$item_count = 0;

				for ($t=0;($t<count($graph_items));$t++) {
					if ((ereg("(AREA|STACK|LINE[123])", $graph_item_types{$graph_items[$t]["graph_type_id"]})) && (!empty($graph_items[$t]["data_template_rrd_id"])) && ($graph_item["data_source_name"] == $graph_items[$t]["data_source_name"]) && ($graph_item["graph_templates_item_id"] != $graph_items[$t]["graph_templates_item_id"])) {
						/* if the user screws up CF settings, PHP will generate warnings if left unchecked */
						if (isset($cf_ds_cache{$graph_items[$t]["data_template_rrd_id"]}[$cf_id]) && (!isset($sources_seen{$graph_items[$t]["data_template_rrd_id"]}))) {
							$def_name = generate_graph_def_name(strval($cf_ds_cache{$graph_items[$t]["data_template_rrd_id"]}[$cf_id]));
							$cdef_similar_ds .= ($item_count == 0 ? "" : ",") . "TIME," . (time() - $seconds_between_graph_updates) . ",GT,$def_name,$def_name,UN,0,$def_name,IF,IF"; /* convert unknowns to '0' first */
							$sources_seen{$graph_items[$t]["data_template_rrd_id"]} = 1;
							$item_count++;
						}
					}
				}

				/* if there is only one item to total, don't even bother with the summation. otherwise
				cdef=a,b,c,+,+ is fine. */
				if ($item_count > 1) {
					$cdef_similar_ds .= str_repeat(",+", ($item_count - 2)) . ",+";
				}
			}

			$cdef_string = str_replace("CURRENT_DATA_SOURCE", generate_graph_def_name(strval((isset($cf_ds_cache{$graph_item["data_template_rrd_id"]}[$cf_id]) ? $cf_ds_cache{$graph_item["data_template_rrd_id"]}[$cf_id] : "0"))), $cdef_string);
			$cdef_string = str_replace("ALL_DATA_SOURCES_NODUPS", $cdef_total_ds, $cdef_string);
			$cdef_string = str_replace("SIMILAR_DATA_SOURCES_NODUPS", $cdef_similar_ds, $cdef_string);

			/* data source item variables */
			$cdef_string = str_replace("CURRENT_DS_MINIMUM_VALUE", (empty($graph_item["rrd_minimum"]) ? "0" : $graph_item["rrd_minimum"]), $cdef_string);
			$cdef_string = str_replace("CURRENT_DS_MAXIMUM_VALUE", (empty($graph_item["rrd_maximum"]) ? "0" : $graph_item["rrd_maximum"]), $cdef_string);
			$cdef_string = str_replace("CURRENT_GRAPH_MINIMUM_VALUE", (empty($graph["lower_limit"]) ? "0" : $graph["lower_limit"]), $cdef_string);
			$cdef_string = str_replace("CURRENT_GRAPH_MAXIMUM_VALUE", (empty($graph["upper_limit"]) ? "0" : $graph["upper_limit"]), $cdef_string);

			/* make the initial "virtual" cdef name: 'cdef' + [a,b,c,d...] */
			$cdef_graph_defs .= "CDEF:cdef" . generate_graph_def_name(strval($i)) . "=";
			$cdef_graph_defs .= $cdef_string;
			$cdef_graph_defs .= " \\\n";

			/* the CDEF cache is so we do not create duplicate CDEF's on a graph */
			$cdef_cache{$graph_item["cdef_id"]}{$graph_item["data_template_rrd_id"]}[$cf_id] = "$i";
		}

		/* add the cdef string to the end of the def string */
		$graph_defs .= $cdef_graph_defs;

		/* note the current item_id for easy access */
		$graph_item_id = $graph_item["graph_templates_item_id"];

		/* if we are not displaying a legend there is no point in us even processing the auto padding,
		text format stuff. */
		if ((!isset($graph_data_array["graph_nolegend"])) && ($graph["auto_padding"] == "on") && (isset($text_format_lengths{$graph_item["data_template_rrd_id"]}))) {
			/* we are basing how much to pad on area and stack text format,
			not gprint. but of course the padding has to be displayed in gprint,
			how fun! */

			$pad_number = ($greatest_text_format - $text_format_lengths{$graph_item["data_template_rrd_id"]});
			//cacti_log("MAX: $greatest_text_format, CURR: $text_format_lengths[$item_dsid], DSID: $item_dsid");
			$text_padding = str_pad("", $pad_number);

			/* two GPRINT's in a row screws up the padding, lets not do that */
			if (($graph_item_types{$graph_item["graph_type_id"]} == "GPRINT") && ($last_graph_type == "GPRINT")) {
				$text_padding = "";
			}

			$last_graph_type = $graph_item_types{$graph_item["graph_type_id"]};
		}

		/* we put this in a variable so it can be manipulated before mainly used
		if we want to skip it, like below */
		$current_graph_item_type = $graph_item_types{$graph_item["graph_type_id"]};

		/* IF this graph item has a data source... get a DEF name for it, or the cdef if that applies
		to this graph item */
		if ($graph_item["cdef_id"] == "0") {
			if (isset($cf_ds_cache{$graph_item["data_template_rrd_id"]}[$cf_id])) {
				$data_source_name = generate_graph_def_name(strval($cf_ds_cache{$graph_item["data_template_rrd_id"]}[$cf_id]));
			}else{
				$data_source_name = "";
			}
		}else{
			$data_source_name = "cdef" . generate_graph_def_name(strval($cdef_cache{$graph_item["cdef_id"]}{$graph_item["data_template_rrd_id"]}[$cf_id]));
		}

		/* to make things easier... if there is no text format set; set blank text */
		if (!isset($graph_variables["text_format"][$graph_item_id])) {
			$graph_variables["text_format"][$graph_item_id] = "";
		}

		if (!isset($hardreturn[$graph_item_id])) {
			$hardreturn[$graph_item_id] = "";
		}

		/* +++++++++++++++++++++++ GRAPH ITEMS +++++++++++++++++++++++ */

		/* most of the calculations have been done above. now we have for print everything out
		in an RRDTool-friendly fashion */
		if (ereg("^(AREA|STACK|LINE[123])$", $graph_item_types{$graph_item["graph_type_id"]})) {
			$graph_variables["text_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["text_format"][$graph_item_id]); /* escape colons */
			$txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":" . $data_source_name . "#" . $graph_item["hex"] . ":" . "\"" . $graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id] . "\" ";
		}elseif ($graph_item_types{$graph_item["graph_type_id"]} == "COMMENT") {
			$txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":\"" . $graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id] . "\" ";
		}elseif (($graph_item_types{$graph_item["graph_type_id"]} == "GPRINT") && (!isset($graph_data_array["graph_nolegend"]))) {
			$graph_variables["text_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["text_format"][$graph_item_id]); /* escape colons */
			$txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":" . $data_source_name . ":" . $consolidation_functions{$graph_item["consolidation_function_id"]} . ":\"$text_padding" . $graph_variables["text_format"][$graph_item_id] . $graph_item["gprint_text"] . $hardreturn[$graph_item_id] . "\" ";
		}elseif ($graph_item_types{$graph_item["graph_type_id"]} == "HRULE") {
			$graph_variables["text_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["text_format"][$graph_item_id]); /* escape colons */
			$graph_variables["value"][$graph_item_id] = str_replace(":", "\:", $graph_variables["value"][$graph_item_id]); /* escape colons */
			$txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":" . $graph_variables["value"][$graph_item_id] . "#" . $graph_item["hex"] . ":\"" . $graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id] . "\" ";
		}elseif ($graph_item_types{$graph_item["graph_type_id"]} == "VRULE") {
			$graph_variables["text_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["text_format"][$graph_item_id]); /* escape colons */

			$value_array = explode(":", $graph_item["value"]);

			if ($value_array[0] < 0) {
				$value = date("U") - (-3600 * $value_array[0]) - 60 * $value_array[1];
			}else{
				$value = date("U", mktime($value_array[0],$value_array[1],0));
			}

			$txt_graph_items .= $graph_item_types{$graph_item["graph_type_id"]} . ":" . $value . "#" . $graph_item["hex"] . ":\"" . $graph_variables["text_format"][$graph_item_id] . $hardreturn[$graph_item_id] . "\" ";
		}

		$i++;

		if ($i < sizeof($graph_items)) {
			$txt_graph_items .= RRD_NL;
		}
	}
	}

	/* either print out the source or pass the source onto rrdtool to get us a nice PNG */
	if (isset($graph_data_array["print_source"])) {
		print "<PRE>" . read_config_option("path_rrdtool") . " graph $graph_opts$graph_defs$txt_graph_items</PRE>";
	}else{
		if (isset($graph_data_array["export"])) {
			rrdtool_execute("graph $graph_opts$graph_defs$txt_graph_items", false, RRDTOOL_OUTPUT_NULL, $rrd_struc);
			return 0;
		}else{
			if (isset($graph_data_array["output_flag"])) {
				$output_flag = $graph_data_array["output_flag"];
			}else{
				$output_flag = RRDTOOL_OUTPUT_GRAPH_DATA;
			}

			return rrdtool_execute("graph $graph_opts$graph_defs$txt_graph_items", false, $output_flag, $rrd_struc);
		}
	}
}
예제 #2
0
function api_reapply_suggested_graph_title($local_graph_id) {
	global $config;

	/* get graphs template id */
	$graph_template_id = db_fetch_cell("select graph_template_id from graph_templates_graph where local_graph_id=" . $local_graph_id);

	/* if a non-template graph, simply return */
	if ($graph_template_id == 0) {
		return;
	}

	/* get the host associated with this graph */
	$graph_local = db_fetch_row("select host_id, graph_template_id, snmp_query_id, snmp_index from graph_local where id=" . $local_graph_id);
	$snmp_query_graph_id = db_fetch_cell("select id from snmp_query_graph where graph_template_id=" . $graph_local["graph_template_id"] .
										" and snmp_query_id=" . $graph_local["snmp_query_id"]);

	/* get the suggested values from the suggested values cache */
	$suggested_values = db_fetch_assoc("select text,field_name from snmp_query_graph_sv where snmp_query_graph_id=" . $snmp_query_graph_id . " order by sequence");

	if (sizeof($suggested_values) > 0) {
	foreach ($suggested_values as $suggested_value) {
		/* once we find a match; don't try to find more */
		if (!isset($suggested_values_graph[$graph_template_id]{$suggested_value["field_name"]})) {
			$subs_string = substitute_snmp_query_data($suggested_value["text"], $graph_local["host_id"], $graph_local["snmp_query_id"], $graph_local["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=" . $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=" . $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=" . $local_graph_id);
				db_execute("update graph_templates_item set $field_name='$field_value' where id=$graph_item_id");
			}
		}
	}
}
예제 #3
0
파일: variables.php 프로젝트: MrWnn/cacti
function expand_title($host_id, $snmp_query_id, $snmp_index, $title)
{
    if (strstr($title, '|') && !empty($host_id)) {
        if ($snmp_query_id != '0' && $snmp_index != '') {
            return substitute_snmp_query_data(null_out_substitutions(substitute_host_data($title, '|', '|', $host_id)), $host_id, $snmp_query_id, $snmp_index, read_config_option('max_data_query_field_length'));
        } else {
            return null_out_substitutions(substitute_host_data($title, '|', '|', $host_id));
        }
    } else {
        return null_out_substitutions($title);
    }
}
예제 #4
0
function api_reapply_suggested_data_source_title($local_data_id)
{
    global $config;
    $data_template_data_id = db_fetch_cell("select id from data_template_data where local_data_id={$local_data_id}");
    if (empty($data_template_data_id)) {
        return;
    }
    /* require query type data sources only (snmp_query_id > 0) */
    $data_local = db_fetch_row("SELECT id, host_id, data_template_id, snmp_query_id, snmp_index FROM data_local WHERE snmp_query_id>0 AND id={$local_data_id}");
    /* if this is not a data query graph, simply return */
    if (!isset($data_local["host_id"])) {
        return;
    }
    $snmp_query_graph_id = db_fetch_cell("SELECT " . "data_input_data.value from data_input_data " . "JOIN data_input_fields ON (data_input_data.data_input_field_id=data_input_fields.id) " . "JOIN data_template_data ON (data_template_data.id = data_input_data.data_template_data_id) " . "WHERE data_input_fields.type_code = 'output_type' " . "AND data_template_data.local_data_id=" . $data_local["id"]);
    /* no snmp query graph id found */
    if ($snmp_query_graph_id == 0) {
        return;
    }
    $suggested_values = db_fetch_assoc("SELECT " . "text, " . "field_name " . "FROM snmp_query_graph_rrd_sv " . "WHERE snmp_query_graph_id=" . $snmp_query_graph_id . " " . "AND data_template_id=" . $data_local["data_template_id"] . " " . "AND field_name = 'name' " . "ORDER BY sequence");
    $suggested_values_data = array();
    if (sizeof($suggested_values) > 0) {
        foreach ($suggested_values as $suggested_value) {
            if (!isset($suggested_values_data[$suggested_value["field_name"]])) {
                $subs_string = substitute_snmp_query_data($suggested_value["text"], $data_local["host_id"], $data_local["snmp_query_id"], $data_local["snmp_index"], read_config_option("max_data_query_field_length"));
                /* if there are no '|query' characters, all of the substitutions were successful */
                if (!substr_count($subs_string, "|query")) {
                    db_execute("UPDATE data_template_data SET " . $suggested_value["field_name"] . "='" . $suggested_value["text"] . "' WHERE local_data_id=" . $local_data_id);
                    /* once we find a working value for that very field, stop */
                    $suggested_values_data[$suggested_value["field_name"]] = true;
                }
            }
        }
    }
}
예제 #5
0
function boost_rrdtool_function_create($local_data_id, $initial_time, $show_source)
{
    global $config;
    include $config["include_path"] . "/global_arrays.php";
    $data_source_path = get_data_source_path($local_data_id, TRUE);
    /* ok, if that passes lets check to make sure an rra does not already
    	exist, the last thing we want to do is overright data! */
    if ($show_source != TRUE) {
        if (file_exists($data_source_path) == TRUE) {
            return -1;
        }
    }
    /* the first thing we must do is make sure there is at least one
    	rra associated with this data source... *
    	UPDATE: As of version 0.6.6, we are splitting this up into two
    	SQL strings because of the multiple DS per RRD support. This is
    	not a big deal however since this function gets called once per
    	data source */
    $rras = db_fetch_assoc("select\r\n\t\tdata_template_data.rrd_step,\r\n\t\trra.x_files_factor,\r\n\t\trra.steps,\r\n\t\trra.rows,\r\n\t\trra_cf.consolidation_function_id,\r\n\t\t(rra.rows*rra.steps) as rra_order\r\n\t\tfrom data_template_data\r\n\t\tleft join data_template_data_rra on (data_template_data.id=data_template_data_rra.data_template_data_id)\r\n\t\tleft join rra on (data_template_data_rra.rra_id=rra.id)\r\n\t\tleft join rra_cf on (rra.id=rra_cf.rra_id)\r\n\t\twhere data_template_data.local_data_id={$local_data_id}\r\n\t\tand (rra.steps is not null or rra.rows is not null)\r\n\t\torder by rra_cf.consolidation_function_id,rra_order");
    /* if we find that this DS has no RRA associated; get out.  This would
     * indicate that a data sources has been deleted
     */
    if (sizeof($rras) <= 0) {
        return FALSE;
    }
    /* back off the initial time to allow updates */
    $initial_time -= 300;
    /* create the "--step" line */
    $create_ds = RRD_NL . "--start " . $initial_time . " --step " . $rras[0]["rrd_step"] . " " . RRD_NL;
    /* query the data sources to be used in this .rrd file */
    $data_sources = db_fetch_assoc("SELECT\r\n\t\tdata_template_rrd.id,\r\n\t\tdata_template_rrd.rrd_heartbeat,\r\n\t\tdata_template_rrd.rrd_minimum,\r\n\t\tdata_template_rrd.rrd_maximum,\r\n\t\tdata_template_rrd.data_source_type_id\r\n\t\tFROM data_template_rrd\r\n\t\tWHERE data_template_rrd.local_data_id={$local_data_id}\r\n\t\tORDER BY local_data_template_rrd_id");
    /* ONLY make a new DS entry if:
    	- There is multiple data sources and this item is not the main one.
    	- There is only one data source (then use it) */
    if (sizeof($data_sources) > 0) {
        foreach ($data_sources as $data_source) {
            /* use the cacti ds name by default or the user defined one, if entered */
            $data_source_name = get_data_source_item_name($data_source["id"]);
            if (empty($data_source["rrd_maximum"])) {
                /* in case no maximum is given, use "Undef" value */
                $data_source["rrd_maximum"] = "U";
            } elseif (strpos($data_source["rrd_maximum"], "|query_") !== false) {
                if ($data_source["rrd_maximum"] == "|query_ifSpeed|" || $data_source["rrd_maximum"] == "|query_ifHighSpeed|") {
                    $highSpeed = db_fetch_cell("SELECT field_value\r\n\t\t\t\t\tFROM host_snmp_cache\r\n\t\t\t\t\tWHERE host_id=" . $data_local["host_id"] . "\r\n\t\t\t\t\tAND snmp_query_id=" . $data_local["snmp_query_id"] . "\r\n\t\t\t\t\tAND snmp_index='" . $data_local["snmp_index"] . "'\r\n\t\t\t\t\tAND field_name='ifHighSpeed'");
                    if (!empty($highSpeed)) {
                        $data_source["rrd_maximum"] = $highSpeed * 1000000;
                    } else {
                        $data_source["rrd_maximum"] = substitute_snmp_query_data("|query_ifSpeed|", $data_local["host_id"], $data_local["snmp_query_id"], $data_local["snmp_index"]);
                    }
                } else {
                    $data_source["rrd_maximum"] = substitute_snmp_query_data($data_source["rrd_maximum"], $data_local["host_id"], $data_local["snmp_query_id"], $data_local["snmp_index"]);
                }
            } elseif ($data_source["rrd_maximum"] != "U" && (int) $data_source["rrd_maximum"] <= (int) $data_source["rrd_minimum"]) {
                /* max > min required, but take care of an "Undef" value */
                $data_source["rrd_maximum"] = (int) $data_source["rrd_minimum"] + 1;
            }
            /* min==max==0 won't work with rrdtool */
            if ($data_source["rrd_minimum"] == 0 && $data_source["rrd_maximum"] == 0) {
                $data_source["rrd_maximum"] = "U";
            }
            $create_ds .= "DS:{$data_source_name}:" . $data_source_types[$data_source["data_source_type_id"]] . ":" . $data_source["rrd_heartbeat"] . ":" . $data_source["rrd_minimum"] . ":" . $data_source["rrd_maximum"] . RRD_NL;
        }
    }
    $create_rra = "";
    /* loop through each available RRA for this DS */
    foreach ($rras as $rra) {
        $create_rra .= "RRA:" . $consolidation_functions[$rra["consolidation_function_id"]] . ":" . $rra["x_files_factor"] . ":" . $rra["steps"] . ":" . $rra["rows"] . RRD_NL;
    }
    /* check for structured path configuration, if in place verify directory
    	   exists and if not create it.
    	 */
    if (read_config_option("extended_paths") == "on") {
        if (!is_dir(dirname($data_source_path))) {
            if (mkdir(dirname($data_source_path), 0775)) {
                if ($config["cacti_server_os"] != "win32") {
                    $owner_id = fileowner($config["rra_path"]);
                    $group_id = filegroup($config["rra_path"]);
                    if (chown(dirname($data_source_path), $owner_id) && chgrp(dirname($data_source_path), $group_id)) {
                        /* permissions set ok */
                    } else {
                        cacti_log("ERROR: Unable to set directory permissions for '" . dirname($data_source_path) . "'", FALSE);
                    }
                }
            } else {
                cacti_log("ERROR: Unable to create directory '" . dirname($data_source_path) . "'", FALSE);
            }
        }
    }
    if ($show_source == TRUE) {
        return read_config_option("path_rrdtool") . " create" . RRD_NL . "{$data_source_path}{$create_ds}{$create_rra}";
    } else {
        return boost_rrdtool_execute("create {$data_source_path} {$create_ds}{$create_rra}", FALSE, RRDTOOL_OUTPUT_STDOUT);
    }
}
예제 #6
0
파일: boost.php 프로젝트: MrWnn/cacti
function boost_rrdtool_function_create($local_data_id, $initial_time, $show_source)
{
    global $config;
    include $config['include_path'] . '/global_arrays.php';
    $data_source_path = get_data_source_path($local_data_id, true);
    /* ok, if that passes lets check to make sure an rra does not already
    	exist, the last thing we want to do is overright data! */
    if ($show_source != true) {
        if (file_exists($data_source_path) == true) {
            return -1;
        }
    }
    /* the first thing we must do is make sure there is at least one
    	rra associated with this data source... *
    	UPDATE: As of version 0.6.6, we are splitting this up into two
    	SQL strings because of the multiple DS per RRD support. This is
    	not a big deal however since this function gets called once per
    	data source */
    $rras = db_fetch_assoc_prepared('SELECT
		data_template_data.rrd_step,
		rra.x_files_factor,
		rra.steps,
		rra.rows,
		rra_cf.consolidation_function_id,
		(rra.rows * rra.steps) AS rra_order
		FROM data_template_data
		LEFT JOIN data_template_data_rra ON (data_template_data.id = data_template_data_rra.data_template_data_id)
		LEFT JOIN rra ON (data_template_data_rra.rra_id = rra.id)
		LEFT JOIN rra_cf ON (rra.id = rra_cf.rra_id)
		WHERE data_template_data.local_data_id = ?
		AND (rra.steps is not null OR rra.rows is not null)
		ORDER BY rra_cf.consolidation_function_id, rra_order', array($local_data_id));
    /* if we find that this DS has no RRA associated; get out.  This would
     * indicate that a data sources has been deleted
     */
    if (sizeof($rras) <= 0) {
        return false;
    }
    /* back off the initial time to allow updates */
    $initial_time -= 300;
    /* create the "--step" line */
    $create_ds = RRD_NL . '--start ' . $initial_time . ' --step ' . $rras[0]['rrd_step'] . ' ' . RRD_NL;
    /* query the data sources to be used in this .rrd file */
    $data_sources = db_fetch_assoc_prepared('SELECT
		data_template_rrd.id,
		data_template_rrd.rrd_heartbeat,
		data_template_rrd.rrd_minimum,
		data_template_rrd.rrd_maximum,
		data_template_rrd.data_source_type_id
		FROM data_template_rrd
		WHERE data_template_rrd.local_data_id = ?
		ORDER BY local_data_template_rrd_id', array($local_data_id));
    /* ONLY make a new DS entry if:
    	- There is multiple data sources and this item is not the main one.
    	- There is only one data source (then use it) */
    if (sizeof($data_sources) > 0) {
        foreach ($data_sources as $data_source) {
            /* use the cacti ds name by default or the user defined one, if entered */
            $data_source_name = get_data_source_item_name($data_source['id']);
            if (empty($data_source['rrd_maximum'])) {
                /* in case no maximum is given, use "Undef" value */
                $data_source['rrd_maximum'] = 'U';
            } elseif (strpos($data_source['rrd_maximum'], '|query_') !== false) {
                $data_local = db_fetch_row_prepared('SELECT * FROM data_local WHERE id = ?', array($local_data_id));
                if ($data_source['rrd_maximum'] == '|query_ifSpeed|' || $data_source['rrd_maximum'] == '|query_ifHighSpeed|') {
                    $highSpeed = db_fetch_cell_prepared("SELECT field_value\n\t\t\t\t\tFROM host_snmp_cache\n\t\t\t\t\tWHERE host_id = ?\n\t\t\t\t\tAND snmp_query_id = ?\n\t\t\t\t\tAND snmp_index = ?\n\t\t\t\t\tAND field_name = 'ifHighSpeed'", array($data_local['host_id'], $data_local['snmp_query_id'], $data_local['snmp_index']));
                    if (!empty($highSpeed)) {
                        $data_source['rrd_maximum'] = $highSpeed * 1000000;
                    } else {
                        $data_source['rrd_maximum'] = substitute_snmp_query_data('|query_ifSpeed|', $data_local['host_id'], $data_local['snmp_query_id'], $data_local['snmp_index']);
                    }
                } else {
                    $data_source['rrd_maximum'] = substitute_snmp_query_data($data_source['rrd_maximum'], $data_local['host_id'], $data_local['snmp_query_id'], $data_local['snmp_index']);
                }
            } elseif ($data_source['rrd_maximum'] != 'U' && (int) $data_source['rrd_maximum'] <= (int) $data_source['rrd_minimum']) {
                /* max > min required, but take care of an "Undef" value */
                $data_source['rrd_maximum'] = (int) $data_source['rrd_minimum'] + 1;
            }
            /* min==max==0 won't work with rrdtool */
            if ($data_source['rrd_minimum'] == 0 && $data_source['rrd_maximum'] == 0) {
                $data_source['rrd_maximum'] = 'U';
            }
            $create_ds .= "DS:{$data_source_name}:" . $data_source_types[$data_source['data_source_type_id']] . ':' . $data_source['rrd_heartbeat'] . ':' . $data_source['rrd_minimum'] . ':' . $data_source['rrd_maximum'] . RRD_NL;
        }
    }
    $create_rra = '';
    /* loop through each available RRA for this DS */
    foreach ($rras as $rra) {
        $create_rra .= 'RRA:' . $consolidation_functions[$rra['consolidation_function_id']] . ':' . $rra['x_files_factor'] . ':' . $rra['steps'] . ':' . $rra['rows'] . RRD_NL;
    }
    /* check for structured path configuration, if in place verify directory
    	   exists and if not create it.
    	 */
    if (read_config_option('extended_paths') == 'on') {
        if (!is_dir(dirname($data_source_path))) {
            if (mkdir(dirname($data_source_path), 0775)) {
                if ($config['cacti_server_os'] != 'win32') {
                    $owner_id = fileowner($config['rra_path']);
                    $group_id = filegroup($config['rra_path']);
                    if (chown(dirname($data_source_path), $owner_id) && chgrp(dirname($data_source_path), $group_id)) {
                        /* permissions set ok */
                    } else {
                        cacti_log("ERROR: Unable to set directory permissions for '" . dirname($data_source_path) . "'", false);
                    }
                }
            } else {
                cacti_log("ERROR: Unable to create directory '" . dirname($data_source_path) . "'", false);
            }
        }
    }
    if ($show_source == true) {
        return read_config_option('path_rrdtool') . ' create' . RRD_NL . "{$data_source_path}{$create_ds}{$create_rra}";
    } else {
        return boost_rrdtool_execute("create {$data_source_path} {$create_ds}{$create_rra}", false, RRDTOOL_OUTPUT_STDOUT);
    }
}
예제 #7
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;
}
예제 #8
0
파일: rrd.php 프로젝트: songchin/Cacti
function rrd_substitute_host_query_data($txt_graph_item, $graph, $graph_item) {
	/* replace host variables in graph elements */
	$txt_graph_item = substitute_host_data($txt_graph_item, '|','|', $graph["host_id"]);

	/* replace query variables in graph elements */
	if (preg_match("/\|query_[a-zA-Z0-9_]+\|/", $txt_graph_item)) {
		/* default to the graph data query information from the graph */
		if (empty($graph_item["local_data_id"])) {
			return substitute_snmp_query_data($txt_graph_item, $graph["host_id"], $graph["snmp_query_id"], $graph["snmp_index"]);
		/* use the data query information from the data source if possible */
		}else{
			$data_local = db_fetch_row("select snmp_index,snmp_query_id,host_id from data_local where id='" . $graph_item["local_data_id"] . "'");
			return substitute_snmp_query_data($txt_graph_item, $data_local["host_id"], $data_local["snmp_query_id"], $data_local["snmp_index"]);
		}
	}else{
		return $txt_graph_item;
	}
}
예제 #9
0
function api_reapply_suggested_data_source_title($local_data_id)
{
    global $config;
    $data_template_data_id = db_fetch_cell("select id from data_template_data where local_data_id={$local_data_id}");
    if (empty($data_template_data_id)) {
        return;
    }
    $data_local = db_fetch_row("select host_id, data_template_id, snmp_query_id, snmp_index from data_local where id={$local_data_id}");
    $suggested_values = db_fetch_assoc("select text,field_name from snmp_query_graph_rrd_sv where data_template_id=" . $data_local["data_template_id"] . " order by sequence");
    if (sizeof($suggested_values) > 0) {
        foreach ($suggested_values as $suggested_value) {
            if (!isset($suggested_values_data[$data_template_data_id][$suggested_value["field_name"]])) {
                $subs_string = substitute_snmp_query_data($suggested_value["text"], $data_local["host_id"], $data_local["snmp_query_id"], $data_local["snmp_index"], read_config_option("max_data_query_field_length"));
                /* if there are no '|query' characters, all of the substitutions were successful */
                if (!substr_count($subs_string, "|query") && $suggested_value["field_name"] == "name") {
                    db_execute("update data_template_data set " . $suggested_value["field_name"] . "='" . $suggested_value["text"] . "' where local_data_id=" . $local_data_id);
                    /* once we find a working value, stop */
                    $suggested_values_data[$data_template_data_id][$suggested_value["field_name"]] = true;
                }
            }
        }
    }
}
예제 #10
0
function api_reapply_suggested_graph_title($local_graph_id)
{
    global $config;
    /* get graphs template id */
    $graph_template_id = db_fetch_cell("select graph_template_id from graph_templates_graph where local_graph_id=" . $local_graph_id);
    /* if a non-template graph, simply return */
    if ($graph_template_id == 0) {
        return;
    }
    /* get the host associated with this graph for data queries only
     * there's no "reapply suggested title" for "simple" graph templates */
    $graph_local = db_fetch_row("select host_id, graph_template_id, snmp_query_id, snmp_index from graph_local where snmp_query_id>0 AND id=" . $local_graph_id);
    /* if this is not a data query graph, simply return */
    if (!isset($graph_local["host_id"])) {
        return;
    }
    /* get data source associated with the graph */
    $data_local = db_fetch_cell("SELECT " . "data_template_data.local_data_id " . "FROM (data_template_rrd,data_template_data,graph_templates_item) " . "WHERE graph_templates_item.task_item_id=data_template_rrd.id " . "AND data_template_rrd.local_data_id=data_template_data.local_data_id " . "AND graph_templates_item.local_graph_id=" . $local_graph_id . " " . "GROUP BY data_template_data.local_data_id");
    $snmp_query_graph_id = db_fetch_cell("SELECT " . "data_input_data.value from data_input_data " . "JOIN data_input_fields ON (data_input_data.data_input_field_id=data_input_fields.id) " . "JOIN data_template_data ON (data_template_data.id = data_input_data.data_template_data_id) " . "WHERE data_input_fields.type_code = 'output_type' " . "AND data_template_data.local_data_id=" . $data_local);
    /* no snmp query graph id found */
    if ($snmp_query_graph_id == 0) {
        return;
    }
    /* get the suggested values from the suggested values cache */
    $suggested_values = db_fetch_assoc("SELECT " . "text, " . "field_name " . "FROM snmp_query_graph_sv " . "WHERE snmp_query_graph_id=" . $snmp_query_graph_id . " " . "AND field_name = 'title' " . "ORDER BY sequence");
    $found = false;
    if (sizeof($suggested_values) > 0) {
        foreach ($suggested_values as $suggested_value) {
            /* once we find a match; don't try to find more */
            if (!$found) {
                $subs_string = substitute_snmp_query_data($suggested_value["text"], $graph_local["host_id"], $graph_local["snmp_query_id"], $graph_local["snmp_index"], read_config_option("max_data_query_field_length"));
                /* if there are no '|' characters, all of the substitutions were successful */
                if (!substr_count($subs_string, "|query")) {
                    db_execute("UPDATE graph_templates_graph SET " . $suggested_value["field_name"] . "='" . $suggested_value["text"] . "' WHERE local_graph_id=" . $local_graph_id);
                    /* once we find a working value, stop */
                    $found = true;
                }
            }
        }
    }
}
예제 #11
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;
}
예제 #12
0
파일: data_query.php 프로젝트: MrWnn/cacti
function get_formatted_data_query_index($host_id, $data_query_id, $data_query_index)
{
    /* FROM the xml; cached in 'host_snmp_query' */
    $sort_cache = db_fetch_row("SELECT sort_field,title_format FROM host_snmp_query WHERE host_id='{$host_id}' AND snmp_query_id='{$data_query_id}'");
    return substitute_snmp_query_data($sort_cache["title_format"], $host_id, $data_query_id, $data_query_index);
}
예제 #13
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;
}
예제 #14
0
function expand_title($host_id, $snmp_query_id, $snmp_index, $title) {
	if ((strstr($title, "|")) && (!empty($host_id))) {
		if (($snmp_query_id != "0") && ($snmp_index != "")) {
			return substitute_snmp_query_data(null_out_substitutions(substitute_host_data($title, "|", "|", $host_id)), $host_id, $snmp_query_id, $snmp_index, read_config_option("max_data_query_field_length"));
		}else{
			return null_out_substitutions(substitute_host_data($title, "|", "|", $host_id));
		}
	}else{
		return null_out_substitutions($title);
	}
}
예제 #15
0
function api_reapply_suggested_data_source_title($local_data_id)
{
    global $config;
    $data_template_data_id = db_fetch_cell_prepared('SELECT id FROM data_template_data WHERE local_data_id = ?', array($local_data_id));
    if (empty($data_template_data_id)) {
        return;
    }
    /* require query type data sources only (snmp_query_id > 0) */
    $data_local = db_fetch_row_prepared('SELECT id, host_id, data_template_id, snmp_query_id, snmp_index FROM data_local WHERE snmp_query_id > 0 AND id = ?', array($local_data_id));
    /* if this is not a data query graph, simply return */
    if (!isset($data_local['host_id'])) {
        return;
    }
    $snmp_query_graph_id = db_fetch_cell_prepared("SELECT \n\t\tdata_input_data.value FROM data_input_data \n\t\tJOIN data_input_fields ON (data_input_data.data_input_field_id = data_input_fields.id) \n\t\tJOIN data_template_data ON (data_template_data.id = data_input_data.data_template_data_id) \n\t\tWHERE data_input_fields.type_code = 'output_type' \n\t\tAND data_template_data.local_data_id = ?", array($data_local['id']));
    /* no snmp query graph id found */
    if ($snmp_query_graph_id == 0) {
        return;
    }
    $suggested_values = db_fetch_assoc_prepared("SELECT \n\t\ttext, \n\t\tfield_name \n\t\tFROM snmp_query_graph_rrd_sv \n\t\tWHERE snmp_query_graph_id = ?\n\t\tAND data_template_id = ?\n\t\tAND field_name = 'name'\n\t\tORDER BY sequence", array($snmp_query_graph_id, $data_local['data_template_id']));
    $suggested_values_data = array();
    if (sizeof($suggested_values) > 0) {
        foreach ($suggested_values as $suggested_value) {
            if (!isset($suggested_values_data[$suggested_value['field_name']])) {
                $subs_string = substitute_snmp_query_data($suggested_value['text'], $data_local['host_id'], $data_local['snmp_query_id'], $data_local['snmp_index'], read_config_option('max_data_query_field_length'));
                /* if there are no '|query' characters, all of the substitutions were successful */
                if (!substr_count($subs_string, '|query')) {
                    db_execute_prepared('UPDATE data_template_data SET ' . $suggested_value['field_name'] . ' = ? WHERE local_data_id = ?', array($suggested_value['text'], $local_data_id));
                    /* once we find a working value for that very field, stop */
                    $suggested_values_data[$suggested_value['field_name']] = true;
                }
            }
        }
    }
}
예제 #16
0
function get_formatted_data_query_index($host_id, $data_query_id, $data_query_index) {
	/* from the xml; cached in 'host_snmp_query' */
	$sort_cache = db_fetch_row("select sort_field,title_format from host_snmp_query where host_id='$host_id' and snmp_query_id='$data_query_id'");

	return substitute_snmp_query_data($sort_cache["title_format"], $host_id, $data_query_id, $data_query_index);
}
예제 #17
0
파일: rrd.php 프로젝트: MrWnn/cacti
function rrd_substitute_host_query_data($txt_graph_item, $graph, $graph_item)
{
    /* replace host variables in graph elements */
    $host_id = 0;
    if (empty($graph["host_id"])) {
        /* if graph has no associated host determine host_id from graph item data source */
        if (!empty($graph_item["local_data_id"])) {
            $host_id = db_fetch_cell("SELECT host_id FROM data_local WHERE id='" . $graph_item["local_data_id"] . "'");
        }
    } else {
        $host_id = $graph["host_id"];
    }
    $txt_graph_item = substitute_host_data($txt_graph_item, '|', '|', $host_id);
    /* replace query variables in graph elements */
    if (preg_match("/\\|query_[a-zA-Z0-9_]+\\|/", $txt_graph_item)) {
        /* default to the graph data query information from the graph */
        if (empty($graph_item["local_data_id"])) {
            $txt_graph_item = substitute_snmp_query_data($txt_graph_item, $graph["host_id"], $graph["snmp_query_id"], $graph["snmp_index"]);
            /* use the data query information from the data source if possible */
        } else {
            $data_local = db_fetch_row("SELECT snmp_index,snmp_query_id,host_id FROM data_local WHERE id='" . $graph_item["local_data_id"] . "'");
            $txt_graph_item = substitute_snmp_query_data($txt_graph_item, $data_local["host_id"], $data_local["snmp_query_id"], $data_local["snmp_index"]);
        }
    }
    /* replace query variables in graph elements */
    if (preg_match("/\\|input_[a-zA-Z0-9_]+\\|/", $txt_graph_item)) {
        return substitute_data_input_data($txt_graph_item, $graph, $graph_item["local_data_id"]);
    }
    return $txt_graph_item;
}
예제 #18
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;
}
예제 #19
0
파일: api_graph.php 프로젝트: MrWnn/cacti
function api_reapply_suggested_graph_title($local_graph_id)
{
    global $config;
    /* get graphs template id */
    $graph_template_id = db_fetch_cell_prepared('SELECT graph_template_id FROM graph_templates_graph WHERE local_graph_id = ?', array($local_graph_id));
    /* if a non-template graph, simply return */
    if ($graph_template_id == 0) {
        return;
    }
    /* get the host associated with this graph for data queries only
     * there's no "reapply suggested title" for "simple" graph templates */
    $graph_local = db_fetch_row_prepared('SELECT host_id, graph_template_id, snmp_query_id, snmp_index FROM graph_local WHERE snmp_query_id>0 AND id = ?', array($local_graph_id));
    /* if this is not a data query graph, simply return */
    if (!isset($graph_local['host_id'])) {
        return;
    }
    /* get data source associated with the graph */
    $data_local = db_fetch_cell_prepared('SELECT 
		data_template_data.local_data_id
		FROM (data_template_rrd,data_template_data,graph_templates_item)
		WHERE graph_templates_item.task_item_id=data_template_rrd.id
		AND data_template_rrd.local_data_id=data_template_data.local_data_id
		AND graph_templates_item.local_graph_id = ?
		GROUP BY data_template_data.local_data_id', array($local_graph_id));
    $snmp_query_graph_id = db_fetch_cell_prepared("SELECT \n\t\tdata_input_data.value from data_input_data \n\t\tJOIN data_input_fields ON (data_input_data.data_input_field_id=data_input_fields.id) \n\t\tJOIN data_template_data ON (data_template_data.id = data_input_data.data_template_data_id) \n\t\tWHERE data_input_fields.type_code = 'output_type' \n\t\tAND data_template_data.local_data_id = ?", array($data_local));
    /* no snmp query graph id found */
    if ($snmp_query_graph_id == 0) {
        return;
    }
    /* get the suggested values from the suggested values cache */
    $suggested_values = db_fetch_assoc_prepared("SELECT \n\t\ttext, \n\t\tfield_name \n\t\tFROM snmp_query_graph_sv \n\t\tWHERE snmp_query_graph_id = ?\n\t\tAND field_name = 'title'\n\t\tORDER BY sequence", array($snmp_query_graph_id));
    $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[$suggested_value['field_name']])) {
                $subs_string = substitute_snmp_query_data($suggested_value['text'], $graph_local['host_id'], $graph_local['snmp_query_id'], $graph_local['snmp_index'], read_config_option('max_data_query_field_length'));
                /* if there are no '|' characters, all of the substitutions were successful */
                if (!substr_count($subs_string, '|query')) {
                    db_execute_prepared('UPDATE graph_templates_graph SET ' . $suggested_value['field_name'] . ' = ? WHERE local_graph_id = ?', array($suggested_value['text'], $local_graph_id));
                    /* once we find a working value for this very field, stop */
                    $suggested_values_graph[$suggested_value['field_name']] = true;
                }
            }
        }
    }
}