function api_graph_template_remove($graph_template_id) {
	require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_info.php");

	/* sanity checks */
	validate_id_die($graph_template_id, "graph_template_id");

	/* delete all graph template items */
	$graph_template_items = api_graph_template_item_list($graph_template_id);

	if (is_array($graph_template_items) > 0) {
		foreach ($graph_template_items as $item) {
			api_graph_template_item_remove($item["id"], false);
		}
	}

	/* base tables */
	db_delete("graph_template_suggested_value",
		array(
			"graph_template_id" => array("type" => DB_TYPE_INTEGER, "value" => $graph_template_id)
			));
	db_delete("graph_template_item_input",
		array(
			"graph_template_id" => array("type" => DB_TYPE_INTEGER, "value" => $graph_template_id)
			));
	db_delete("graph_template_item",
		array(
			"graph_template_id" => array("type" => DB_TYPE_INTEGER, "value" => $graph_template_id)
			));
	db_delete("graph_template",
		array(
			"graph_template_id" => array("type" => DB_TYPE_INTEGER, "value" => $graph_template_id)
			));

	/* host templates */
	db_delete("host_template_graph",
		array(
			"graph_template_id" => array("type" => DB_TYPE_INTEGER, "value" => $graph_template_id)
			));

	/* attached graphs */
	db_execute("UPDATE graph SET graph_template_id = 0 WHERE graph_template_id = " . sql_sanitize($graph_template_id));
}
Beispiel #2
0
function copy_graph_template_to_graph($graph_template_id, $host_id = 0, $data_query_id = 0, $data_query_index = "") {
	require_once(CACTI_BASE_PATH . "/lib/sys/variable.php");
	require_once(CACTI_BASE_PATH . "/lib/graph/graph_update.php");
	require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php");
	require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_info.php");

	/* sanity check for $graph_template_id */
	if ((!is_numeric($graph_template_id)) || (empty($graph_template_id))) {
		return false;
	}

	/* sanity check for $host_id */
	if (!is_numeric($host_id)) {
		return false;
	}

	/* fetch field lists */
	$fields_graph = api_graph_form_list();
	$fields_graph_item = api_graph_item_form_list();

	$graph_template = api_graph_template_get($graph_template_id);

	if (sizeof($graph_template) > 0) {
		/* copy down per-graph only fields */
		$_fields = array();
		$_fields["id"] = "0";
		$_fields["graph_template_id"] = $graph_template_id;
		$_fields["host_id"] = $host_id;

		/* evaluate suggested values: data query-based graphs */
		if ((!empty($data_query_id)) && ($data_query_index != "")) {
			$_fields["title"] = evaluate_data_query_suggested_values($host_id, $data_query_id, $data_query_index, "graph_template_suggested_value", "graph_template_id = " . sql_sanitize($graph_template_id) . " and field_name = 'title'", 0);
		/* evaluate suggested values: non-data query-based graphs */
		}else{
			$_fields["title"] = db_fetch_cell("select value from graph_template_suggested_value where graph_template_id = " . sql_sanitize($graph_template_id) . " and field_name = 'title' order by sequence limit 1");
		}

		/* copy down all visible fields */
		foreach (array_keys($fields_graph) as $field_name) {
			if (isset($graph_template[$field_name])) {
				$_fields[$field_name] = $graph_template[$field_name];
			}
		}

		if (api_graph_save(0, $_fields, true)) {
			$graph_id = db_fetch_insert_id();

			log_save("Cloning graph [ID#$graph_id] from template [ID#$graph_template_id]", SEV_DEBUG);

			/* move onto the graph items */
			$graph_template_items = api_graph_template_item_list($graph_template_id);

			if (sizeof($graph_template_items) > 0) {
				foreach ($graph_template_items as $graph_template_item) {
					/* copy down per-graph only fields */
					$_fields = array();
					$_fields["id"]	= "0";
					$_fields["graph_id"] = $graph_id;
					$_fields["graph_template_item_id"] = $graph_template_item["id"]; /* this allows us to connect the dots later */

					foreach (array_keys($fields_graph_item) as $field_name) {
						if (isset($graph_template_item[$field_name])) {
							$_fields[$field_name] = $graph_template_item[$field_name];
						}
					}

					if (!api_graph_item_save(0, $_fields)) {
						log_save("Save error in api_graph_item_save()", SEV_ERROR);
					}
				}
			}

			return $graph_id;
		}else{
			log_save("Save error in api_graph_save()", SEV_ERROR);

			return false;
		}
	}

	return false;
}
Beispiel #3
0
function &package_graph_template_export($graph_template_id, $indent = 3) {
	require_once(CACTI_BASE_PATH . "/lib/graph_template/graph_template_info.php");
	require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php");

	$xml = "";

	/*
	 * XML Tag: <template>
	 */

	/* obtain a list of all graph template specific fields */
	$graph_template_fields = api_graph_template_form_list();
	/* obtain a copy of this specfic graph template */
	$graph_template = api_graph_template_get($graph_template_id);

	$_xml = "";
	foreach (array_keys($graph_template_fields) as $field_name) {
		/* create an XML key for each graph template field */
		$_xml .= package_xml_tag_get($field_name, xml_character_encode($graph_template[$field_name]), $indent + 2);
	}

	/* append the result onto the final XML string */
	$xml .= package_xml_tag_get("template", $_xml, $indent + 1, true);

	/*
	 * XML Tag: <graph>
	 */

	/* obtain a list of all graph specific fields */
	$graph_fields = api_graph_form_list();

	$_xml = "";
	foreach (array_keys($graph_fields) as $field_name) {
		/* check because the 'title' column does not exist */
		if (isset($graph_template[$field_name])) {
			/* create an XML key for each graph field */
			$_xml .= package_xml_tag_get($field_name, xml_character_encode($graph_template[$field_name]), $indent + 2);
		}

		/* create an XML key for each "template" graph field */
		$_xml .= package_xml_tag_get("t_" . $field_name, xml_character_encode($graph_template{"t_" . $field_name}), $indent + 2);
	}

	/* append the result onto the final XML string */
	$xml .= package_xml_tag_get("graph", $_xml, $indent + 1, true);

	/*
	 * XML Tag: <items>
	 */

	/* obtain a list of all graph template item specific fields */
	$graph_template_items_fields = api_graph_template_item_form_list();
	/* obtain a list of all graph template items associated with this graph template */
	$graph_template_items = api_graph_template_item_list($graph_template_id);

	$_xml = "";
	if (sizeof($graph_template_items) > 0) {
		foreach ($graph_template_items as $graph_template_item) {
			$__xml = "";
			foreach (array_keys($graph_template_items_fields) as $field_name) {
				if ($field_name == "data_template_item_id") {
					/* create an XML key for the 'data_template_item_id' field, making sure to resolve internal  ID's */
					$__xml .= package_xml_tag_get($field_name, xml_character_encode(package_hash_get($graph_template_item[$field_name], "data_template_item")), $indent + 3);
				}else{
					/* create an XML key for each graph template item field */
					$__xml .= package_xml_tag_get($field_name, xml_character_encode($graph_template_item[$field_name]), $indent + 3);
				}
			}

			/* append the result onto a temporary XML string */
			$_xml .= package_xml_tag_get(package_hash_get($graph_template_item["id"], "graph_template_item"), $__xml, $indent + 2, true);
		}
	}

	/* append the result onto the final XML string */
	$xml .= package_xml_tag_get("items", $_xml, $indent + 1, true);

	/*
	 * XML Tag: <inputs>
	 */

	/* obtain a list of all graph template item input specific fields */
	$graph_template_inputs_fields = api_graph_template_item_input_form_list();
	/* obtain a list of all graph template item inputs associated with this graph template */
	$graph_template_inputs = api_graph_template_item_input_list($graph_template_id);

	$_xml = "";
	if (sizeof($graph_template_inputs) > 0) {
		foreach ($graph_template_inputs as $graph_template_input) {
			$__xml = "";
			foreach (array_keys($graph_template_inputs_fields) as $field_name) {
				/* create an XML key for each graph template item input field */
				$__xml .= package_xml_tag_get($field_name, xml_character_encode($graph_template_input[$field_name]), $indent + 3);
			}

			/* obtain a list of each item associated with this graph template item input */
			$graph_template_input_items = api_graph_template_item_input_item_list($graph_template_id);

			if (sizeof($graph_template_input_items) > 0) {
				$i = 0; $items_list = "";
				foreach ($graph_template_input_items as $graph_template_item_id) {
					/* create a delimited list of each item, making sure to resolve internal ID's */
					$items_list .= package_hash_get($graph_template_item_id, "graph_template_item") . (($i + 1) < sizeof($graph_template_input_items) ? "|" : "");

					$i++;
				}
			}

			/* add the items list that we created above */
			$__xml .= package_xml_tag_get("items", $items_list, $indent + 3);

			/* append the result onto a temporary XML string */
			$_xml .= package_xml_tag_get(package_hash_get($graph_template_input["id"], "graph_template_input"), $__xml, $indent + 2, true);
		}
	}

	/* append the result onto the final XML string */
	$xml .= package_xml_tag_get("inputs", $_xml, $indent + 1, true);

	/*
	 * XML Tag: <suggested_values>
	 */

	/* obtain a list of all suggested values associated with this graph template */
	$graph_template_suggested_values = api_graph_template_suggested_values_list($graph_template_id);

	$_xml = "";
	if (sizeof($graph_template_suggested_values) > 0) {
		$i = 0;
		foreach ($graph_template_suggested_values as $graph_template_suggested_value) {
			$__xml = "";

			/* create an XML key for each suggested value field */
			$__xml .= package_xml_tag_get("field_name", xml_character_encode($graph_template_suggested_value["field_name"]), $indent + 3);
			$__xml .= package_xml_tag_get("sequence", xml_character_encode($graph_template_suggested_value["sequence"]), $indent + 3);
			$__xml .= package_xml_tag_get("value", xml_character_encode($graph_template_suggested_value["value"]), $indent + 3);

			/* break out each row into its own key */
			$_xml .= package_xml_tag_get("item_" . str_pad($i, 5, "0", STR_PAD_LEFT), $__xml, $indent + 2, true);

			$i++;
		}
	}

	/* append the result onto the final XML string */
	$xml .= package_xml_tag_get("suggested_values", $_xml, $indent + 1, true);

	/* wrap the whole XML string into a 'graph_template' tag and return it */
	$xml = package_xml_tag_get(package_hash_get($graph_template_id, "graph_template"), $xml, $indent, true);

	return $xml;
}