Esempio n. 1
0
function rrdtool_function_update($update_cache_array, $rrd_struc) {
	/* lets count the number of rrd files processed */
	$rrds_processed = 0;

	while (list($rrd_path, $rrd_fields) = each($update_cache_array)) {
		$create_rrd_file = false;

		/* create the rrd if one does not already exist */
		if (!file_exists($rrd_path)) {
			rrdtool_function_create($rrd_fields["local_data_id"], false, $rrd_struc);

			$create_rrd_file = true;
		}

		if ((is_array($rrd_fields["times"])) && (sizeof($rrd_fields["times"]) > 0)) {
			ksort($rrd_fields["times"]);

			while (list($update_time, $field_array) = each($rrd_fields["times"])) {
				if (empty($update_time)) {
					/* default the rrdupdate time to now */
					$current_rrd_update_time = "N";
				}else if ($create_rrd_file == true) {
					/* for some reason rrdtool will not let you update using times less than the
					rrd create time */
					$current_rrd_update_time = "N";
				}else{
					$current_rrd_update_time = $update_time;
				}

				$i = 0; $rrd_update_template = ""; $rrd_update_values = $current_rrd_update_time . ":";
				while (list($field_name, $value) = each($field_array)) {
					$rrd_update_template .= $field_name;

					/* if we have "invalid data", give rrdtool an Unknown (U) */
					if ((!isset($value)) || (!is_numeric($value))) {
						$value = "U";
					}

					$rrd_update_values .= $value;

					if (($i+1) < count($field_array)) {
						$rrd_update_template .= ":";
						$rrd_update_values .= ":";
					}

					$i++;
				}

				rrdtool_execute("update $rrd_path --template $rrd_update_template $rrd_update_values", true, RRDTOOL_OUTPUT_STDOUT, $rrd_struc, "POLLER");
				$rrds_processed++;
			}
		}
	}

	return $rrds_processed;
}
Esempio n. 2
0
function ds_edit()
{
    global $colors, $struct_data_source, $struct_data_source_item, $data_source_types;
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var("id"));
    /* ==================================================== */
    $use_data_template = true;
    $host_id = 0;
    if (!empty($_GET["id"])) {
        $data_local = db_fetch_row("select host_id,data_template_id from data_local where id='" . $_GET["id"] . "'");
        $data = db_fetch_row("select * from data_template_data where local_data_id='" . $_GET["id"] . "'");
        if (isset($data_local["data_template_id"]) && $data_local["data_template_id"] >= 0) {
            $data_template = db_fetch_row("select id,name from data_template where id='" . $data_local["data_template_id"] . "'");
            $data_template_data = db_fetch_row("select * from data_template_data where data_template_id='" . $data_local["data_template_id"] . "' and local_data_id=0");
        } else {
            $_SESSION["sess_messages"] = 'Data Source "' . $_GET["id"] . '" does not exist.';
            header("Location: data_sources.php");
            exit;
        }
        $header_label = "[edit: " . get_data_source_title($_GET["id"]) . "]";
        if (empty($data_local["data_template_id"])) {
            $use_data_template = false;
        }
    } else {
        $header_label = "[new]";
        $use_data_template = false;
    }
    /* handle debug mode */
    if (isset($_GET["debug"])) {
        if ($_GET["debug"] == "0") {
            kill_session_var("ds_debug_mode");
        } elseif ($_GET["debug"] == "1") {
            $_SESSION["ds_debug_mode"] = true;
        }
    }
    include_once "./include/top_header.php";
    if (!empty($_GET["id"])) {
        ?>
		<table width="100%" align="center">
			<tr>
				<td class="textInfo" colspan="2" valign="top">
					<?php 
        print get_data_source_title($_GET["id"]);
        ?>
				</td>
				<td class="textInfo" align="right" valign="top">
					<span style="color: #c16921;">*<a href='data_sources.php?action=ds_edit&id=<?php 
        print isset($_GET["id"]) ? $_GET["id"] : 0;
        ?>
&debug=<?php 
        print isset($_SESSION["ds_debug_mode"]) ? "0" : "1";
        ?>
'>Turn <strong><?php 
        print isset($_SESSION["ds_debug_mode"]) ? "Off" : "On";
        ?>
</strong> Data Source Debug Mode.</a>
				</td>
			</tr>
		</table>
		<br>
		<?php 
    }
    html_start_box("<strong>Data Template Selection</strong> {$header_label}", "100%", $colors["header"], "3", "center", "");
    $form_array = array("data_template_id" => array("method" => "drop_sql", "friendly_name" => "Selected Data Template", "description" => "The name given to this data template.", "value" => isset($data_template) ? $data_template["id"] : "0", "none_value" => "None", "sql" => "select id,name from data_template order by name"), "host_id" => array("method" => "drop_sql", "friendly_name" => "Host", "description" => "Choose the host that this graph belongs to.", "value" => isset($_GET["host_id"]) ? $_GET["host_id"] : $data_local["host_id"], "none_value" => "None", "sql" => "select id,CONCAT_WS('',description,' (',hostname,')') as name from host order by description,hostname"), "_data_template_id" => array("method" => "hidden", "value" => isset($data_template) ? $data_template["id"] : "0"), "_host_id" => array("method" => "hidden", "value" => empty($data_local["host_id"]) ? isset($_GET["host_id"]) ? $_GET["host_id"] : "0" : $data_local["host_id"]), "_data_input_id" => array("method" => "hidden", "value" => isset($data["data_input_id"]) ? $data["data_input_id"] : "0"), "data_template_data_id" => array("method" => "hidden", "value" => isset($data) ? $data["id"] : "0"), "local_data_template_data_id" => array("method" => "hidden", "value" => isset($data) ? $data["local_data_template_data_id"] : "0"), "local_data_id" => array("method" => "hidden", "value" => isset($data) ? $data["local_data_id"] : "0"));
    draw_edit_form(array("config" => array(), "fields" => $form_array));
    html_end_box();
    /* only display the "inputs" area if we are using a data template for this data source */
    if (!empty($data["data_template_id"])) {
        $template_data_rrds = db_fetch_assoc("select * from data_template_rrd where local_data_id=" . $_GET["id"] . " order by data_source_name");
        html_start_box("<strong>Supplemental Data Template Data</strong>", "100%", $colors["header"], "3", "center", "");
        draw_nontemplated_fields_data_source($data["data_template_id"], $data["local_data_id"], $data, "|field|", "<strong>Data Source Fields</strong>", true, true, 0);
        draw_nontemplated_fields_data_source_item($data["data_template_id"], $template_data_rrds, "|field|_|id|", "<strong>Data Source Item Fields</strong>", true, true, true, 0);
        draw_nontemplated_fields_custom_data($data["id"], "value_|id|", "<strong>Custom Data</strong>", true, true, 0);
        form_hidden_box("save_component_data", "1", "");
        html_end_box();
    }
    if ((isset($_GET["id"]) || isset($_GET["new"])) && empty($data["data_template_id"])) {
        html_start_box("<strong>Data Source</strong>", "100%", $colors["header"], "3", "center", "");
        $form_array = array();
        while (list($field_name, $field_array) = each($struct_data_source)) {
            $form_array += array($field_name => $struct_data_source[$field_name]);
            if (!($use_data_template == false || !empty($data_template_data["t_" . $field_name]) || $field_array["flags"] == "NOTEMPLATE")) {
                $form_array[$field_name]["description"] = "";
            }
            $form_array[$field_name]["value"] = isset($data[$field_name]) ? $data[$field_name] : "";
            $form_array[$field_name]["form_id"] = empty($data["id"]) ? "0" : $data["id"];
            if (!($use_data_template == false || !empty($data_template_data["t_" . $field_name]) || $field_array["flags"] == "NOTEMPLATE")) {
                $form_array[$field_name]["method"] = "template_" . $form_array[$field_name]["method"];
            }
        }
        draw_edit_form(array("config" => array("no_form_tag" => true), "fields" => inject_form_variables($form_array, isset($data) ? $data : array())));
        html_end_box();
        /* fetch ALL rrd's for this data source */
        if (!empty($_GET["id"])) {
            $template_data_rrds = db_fetch_assoc("select id,data_source_name from data_template_rrd where local_data_id=" . $_GET["id"] . " order by data_source_name");
        }
        /* select the first "rrd" of this data source by default */
        if (empty($_GET["view_rrd"])) {
            $_GET["view_rrd"] = isset($template_data_rrds[0]["id"]) ? $template_data_rrds[0]["id"] : "0";
        }
        /* get more information about the rrd we chose */
        if (!empty($_GET["view_rrd"])) {
            $local_data_template_rrd_id = db_fetch_cell("select local_data_template_rrd_id from data_template_rrd where id=" . $_GET["view_rrd"]);
            $rrd = db_fetch_row("select * from data_template_rrd where id=" . $_GET["view_rrd"]);
            $rrd_template = db_fetch_row("select * from data_template_rrd where id={$local_data_template_rrd_id}");
            $header_label = "[edit: " . $rrd["data_source_name"] . "]";
        } else {
            $header_label = "";
        }
        $i = 0;
        if (isset($template_data_rrds)) {
            if (sizeof($template_data_rrds) > 1) {
                /* draw the data source tabs on the top of the page */
                print "\t<table class='tabs' width='100%' cellspacing='0' cellpadding='3' align='center'>\n\t\t\t\t\t<tr>\n";
                foreach ($template_data_rrds as $template_data_rrd) {
                    $i++;
                    print "\t<td " . ($template_data_rrd["id"] == $_GET["view_rrd"] ? "bgcolor='silver'" : "bgcolor='#DFDFDF'") . " nowrap='nowrap' width='" . (strlen($template_data_rrd["data_source_name"]) * 9 + 50) . "' align='center' class='tab'>\n\t\t\t\t\t\t\t\t<span class='textHeader'><a href='data_sources.php?action=ds_edit&id=" . $_GET["id"] . "&view_rrd=" . $template_data_rrd["id"] . "'>{$i}: " . $template_data_rrd["data_source_name"] . "</a>" . ($use_data_template == false ? " <a href='data_sources.php?action=rrd_remove&id=" . $template_data_rrd["id"] . "&local_data_id=" . $_GET["id"] . "'><img src='images/delete_icon.gif' border='0' alt='Delete'></a>" : "") . "</span>\n\t\t\t\t\t\t\t</td>\n\n\t\t\t\t\t\t\t<td width='1'></td>\n";
                }
                print "\n\t\t\t\t\t<td></td>\n\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n";
            } elseif (sizeof($template_data_rrds) == 1) {
                $_GET["view_rrd"] = $template_data_rrds[0]["id"];
            }
        }
        html_start_box("", "100%", $colors["header"], "3", "center", "");
        print "\t<tr>\n\t\t\t\t<td bgcolor='#" . $colors["header"] . "' class='textHeaderDark'>\n\t\t\t\t\t<strong>Data Source Item</strong> {$header_label}\n\t\t\t\t</td>\n\t\t\t\t<td class='textHeaderDark' align='right' bgcolor='" . $colors["header"] . "'>\n\t\t\t\t\t" . (!empty($_GET["id"]) && empty($data_template["id"]) ? "<strong><a class='linkOverDark' href='data_sources.php?action=rrd_add&id=" . $_GET["id"] . "'>New</a>&nbsp;</strong>" : "") . "\n\t\t\t\t</td>\n\t\t\t</tr>\n";
        /* data input fields list */
        if (empty($data["data_input_id"]) || db_fetch_cell("select type_id from data_input where id=" . $data["data_input_id"]) > "1") {
            unset($struct_data_source_item["data_input_field_id"]);
        } else {
            $struct_data_source_item["data_input_field_id"]["sql"] = "select id,CONCAT(data_name,' - ',name) as name from data_input_fields where data_input_id=" . $data["data_input_id"] . " and input_output='out' and update_rra='on' order by data_name,name";
        }
        $form_array = array();
        while (list($field_name, $field_array) = each($struct_data_source_item)) {
            $form_array += array($field_name => $struct_data_source_item[$field_name]);
            if (!($use_data_template == false || $rrd_template["t_" . $field_name] == "on")) {
                $form_array[$field_name]["description"] = "";
            }
            $form_array[$field_name]["value"] = isset($rrd) ? $rrd[$field_name] : "";
            if (!($use_data_template == false || $rrd_template["t_" . $field_name] == "on")) {
                $form_array[$field_name]["method"] = "template_" . $form_array[$field_name]["method"];
            }
        }
        draw_edit_form(array("config" => array("no_form_tag" => true), "fields" => array("data_template_rrd_id" => array("method" => "hidden", "value" => isset($rrd) ? $rrd["id"] : "0"), "local_data_template_rrd_id" => array("method" => "hidden", "value" => isset($rrd) ? $rrd["local_data_template_rrd_id"] : "0")) + $form_array));
        html_end_box();
        /* data source data goes here */
        data_edit();
        form_hidden_box("current_rrd", $_GET["view_rrd"], "0");
    }
    /* display the debug mode box if the user wants it */
    if (isset($_SESSION["ds_debug_mode"]) && isset($_GET["id"])) {
        ?>
		<table width="100%" align="center">
			<tr>
				<td>
					<span class="textInfo">Data Source Debug</span><br>
					<pre><?php 
        print rrdtool_function_create($_GET["id"], true, array());
        ?>
</pre>
				</td>
			</tr>
		</table>
		<?php 
    }
    if (isset($_GET["id"]) || isset($_GET["new"])) {
        form_hidden_box("save_component_data_source", "1", "");
    } else {
        form_hidden_box("save_component_data_source_new", "1", "");
    }
    form_save_button("data_sources.php");
    include_once "./include/bottom_footer.php";
}
Esempio n. 3
0
function process_poller_output_rt($rrdtool_pipe, $poller_id, $interval) {
	global $config;

	include_once($config["library_path"] . "/rrd.php");

	/* let's count the number of rrd files we processed */
	$rrds_processed = 0;

	/* create/update the rrd files */
	$results = db_fetch_assoc("SELECT
		poller_output_rt.output,
		poller_output_rt.time,
		poller_output_rt.local_data_id,
		poller_item.rrd_path,
		poller_item.rrd_name,
		poller_item.rrd_num
		FROM (poller_output_rt,poller_item)
		WHERE (poller_output_rt.local_data_id=poller_item.local_data_id
		AND poller_output_rt.rrd_name=poller_item.rrd_name)
		AND poller_output_rt.poller_id = $poller_id");

	if (sizeof($results) > 0) {
		/* create an array keyed off of each .rrd file */
		foreach ($results as $item) {
			$rt_graph_path    = read_config_option("realtime_cache_path") . "/realtime_" . $item["local_data_id"] . "_5.rrd";
			$data_source_path = get_data_source_path($item['local_data_id'], true);

			/* create rt rrd */
			if (!file_exists($rt_graph_path)) {
				/* get the syntax */
				$command = @rrdtool_function_create($item['local_data_id'], true);

				/* change permissions so that the poller can clear */
				@chmod($rt_graph_path, 0666);

				/* replace path */
				$command = str_replace($data_source_path, $rt_graph_path, $command);

				/* replace step */
				$command = preg_replace('/--step\s(\d+)/', '--step ' . $interval, $command);

				/* WIN32: before sending this command off to rrdtool, get rid
				of all of the '\' characters. Unix does not care; win32 does.
				Also make sure to replace all of the fancy \'s at the end of the line,
				but make sure not to get rid of the "\n"'s that are supposed to be
				in there (text format) */
				$command = str_replace("\\\n", " ", $command);

				/* create the rrdfile */
				shell_exec($command);
			}else{
				/* change permissions so that the poller can clear */
				@chmod($rt_graph_path, 0666);
			}

			/* now, let's update the path to keep the RRD's updated */
			$item["rrd_path"] = $rt_graph_path;

			/* cleanup the value */
			$value            = trim($item["output"]);
			$unix_time        = strtotime($item["time"]);

			$rrd_update_array{$item["rrd_path"]}["local_data_id"] = $item["local_data_id"];

			/* single one value output */
			if ((is_numeric($value)) || ($value == "U")) {
				$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = $value;
			/* multiple value output */
			}else{
				$values = explode(" ", $value);

				$rrd_field_names = array_rekey(db_fetch_assoc("SELECT
					data_template_rrd.data_source_name,
					data_input_fields.data_name
					FROM (data_template_rrd,data_input_fields)
					WHERE data_template_rrd.data_input_field_id=data_input_fields.id
					AND data_template_rrd.local_data_id=" . $item["local_data_id"]), "data_name", "data_source_name");

				for ($i=0; $i<count($values); $i++) {
					if (preg_match("/^([a-zA-Z0-9_\.-]+):([eE0-9\+\.-]+)$/", $values[$i], $matches)) {
						if (isset($rrd_field_names{$matches[1]})) {
							$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$rrd_field_names{$matches[1]}} = $matches[2];
						}
					}
				}
			}

			/* fallback values */
			if ((!isset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) && ($item["rrd_name"] != "")) {
				$rrd_update_array{$item["rrd_path"]}["times"][$unix_time]{$item["rrd_name"]} = "U";
			}else if ((!isset($rrd_update_array{$item["rrd_path"]}["times"][$unix_time])) && ($item["rrd_name"] == "")) {
				unset($rrd_update_array{$item["rrd_path"]});
			}
		}

		/* make sure each .rrd file has complete data */
		reset($results);
		foreach ($results as $item) {
			db_execute("DELETE FROM poller_output_rt
				WHERE local_data_id='" . $item["local_data_id"] . "'
				AND rrd_name='" . $item["rrd_name"] . "'
				AND time='" . $item["time"] . "'
				AND poller_id='" . $poller_id . "'");
		}

		$rrds_processed = rrdtool_function_update($rrd_update_array, $rrdtool_pipe);
	}

	return $rrds_processed;
}
Esempio n. 4
0
function data_source_edit() {
	global $colors;
	require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php");

	/* ================= input validation ================= */
	input_validate_input_number(get_request_var("id"));
	/* ==================================================== */

	$use_data_template = true;
	$device_id = 0;

	if (!empty($_GET["id"])) {
		$data_local 		= db_fetch_row("select device_id,data_template_id from data_local where id='" . $_GET["id"] . "'");
		$data       		= db_fetch_row("select * from data_template_data where local_data_id='" . $_GET["id"] . "'");
		$data_source_items 	= db_fetch_assoc("select * from data_template_rrd where local_data_id=" . $_GET["id"] . " order by data_source_name");

		if (isset($data_local["data_template_id"]) && $data_local["data_template_id"] >= 0) {
			$data_template      = db_fetch_row("select id,name from data_template where id='" . $data_local["data_template_id"] . "'");
			$data_template_data = db_fetch_row("select * from data_template_data where data_template_id='" . $data_local["data_template_id"] . "' and local_data_id=0");
		} else {
			$_SESSION["sess_messages"] = 'Data Source "' . $_GET["id"] . '" does not exist.';
			header ("Location: data_sources.php");
			exit;
		}

		$header_label = __("[edit: ") . get_data_source_title($_GET["id"]) . "]";

		if (empty($data_local["data_template_id"])) {
			$use_data_template = false;
		}
	}else{
		$header_label = __("[new]");

		$use_data_template = false;
	}

	/* handle debug mode */
	if (isset($_GET["debug"])) {
		if (get_request_var("debug") == "0") {
			kill_session_var("ds_debug_mode");
		}elseif (get_request_var("debug") == "1") {
			$_SESSION["ds_debug_mode"] = true;
		}
	}

	/* handle info mode */
	if (isset($_GET["info"])) {
		if (get_request_var("info") == "0") {
			kill_session_var("ds_info_mode");
		}elseif (get_request_var("info") == "1") {
			$_SESSION["ds_info_mode"] = true;
		}
	}

	include_once(CACTI_BASE_PATH . "/include/top_header.php");

	$tip_text = "";
	if (isset($data)) {
		$tip_text .= "<tr><td align=\\'right\\'><a class=\\'popup_item\\' id=\\'changeDSState\\' onClick=\\'changeDSState()\\' href=\\'#\\'>Unlock/Lock</a></td></tr>";
		$tip_text .= "<tr><td align=\\'right\\'><a class=\\'popup_item\\' href=\\'" . htmlspecialchars('data_sources.php?action=data_source_toggle_status&id=' . (isset($_GET["id"]) ? $_GET["id"] : 0) . '&newstate=' . (($data["active"] == CHECKED) ? "0" : "1")) . "\\'>" . (($data["active"] == CHECKED) ? __("Disable") : __("Enable")) . "</a></td></tr>";
		$tip_text .= "<tr><td align=\\'right\\'><a class=\\'popup_item\\' href=\\'" . htmlspecialchars('data_sources.php?action=data_source_edit&id=' . (isset($_GET["id"]) ? $_GET["id"] : 0) . '&debug=' . (isset($_SESSION["ds_debug_mode"]) ? "0" : "1")) . "\\'>" . __("Turn") . " <strong>" . (isset($_SESSION["ds_debug_mode"]) ? __("Off") : __(CHECKED)) . "</strong> " . __("Debug Mode") . "</a></td></tr>";
		$tip_text .= "<tr><td align=\\'right\\'><a class=\\'popup_item\\' href=\\'" . htmlspecialchars('data_sources.php?action=data_source_edit&id=' . (isset($_GET["id"]) ? $_GET["id"] : 0) . '&info=' . (isset($_SESSION["ds_info_mode"]) ? "0" : "1")) . "\\'>" . __("Turn") . " <strong>" . (isset($_SESSION["ds_info_mode"]) ? __("Off") : __(CHECKED)) . "</strong> " . __("RRD Info Mode") . "</a><td></tr>";
	}
	if (!empty($data_template["id"])) {
		$tip_text .= "<tr><td align=\\'right\\'><a class=\\'popup_item\\' href=\\'" . htmlspecialchars('data_templates.php?action=template_edit&id=' . (isset($data_template["id"]) ? $data_template["id"] : "0")) . "\\'>" . __("Edit Data Source Template") . "<br></a></td></td>";
	}
	if (!empty($_GET["device_id"]) || !empty($data_local["device_id"])) {
		$tip_text .= "<tr><td align=\\'right\\'><a class=\\'popup_item\\' href=\\'" . htmlspecialchars('devices.php?action=edit&id=' . (isset($_GET["device_id"]) ? $_GET["device_id"] : $data_local["device_id"])) . "\\'>" . __("Edit Host") . "</a></td></tr>";
	}

	if (!empty($_GET["id"])) {
		?>
		<script type="text/javascript">
		<!--
		var disabled = true;

		$().ready(function() {
			$("input").attr("disabled","disabled")
			$("select").attr("disabled","disabled")
			$("#cancel").removeAttr("disabled");
		});

		function changeDSState() {
			if (disabled) {
				$("input").removeAttr("disabled");
				$("select").removeAttr("disabled");
				$("#cancel").removeAttr("disabled");
				disabled = false;
			}else{
				$("input").attr("disabled","disabled")
				$("select").attr("disabled","disabled")
				disabled = true;
			}
		}
		-->
		</script>
		<table width="100%" align="center">
			<tr>
				<td class="textInfo" colspan="2" valign="top">
					<?php print get_data_source_title(get_request_var("id"));?>
				</td>
				<td style="white-space:nowrap;" align="right" class="w1"><a id='tooltip' class='popup_anchor' href='#' onMouseOver="Tip('<?php print $tip_text;?>', BGCOLOR, '#EEEEEE', FIX, ['tooltip', -20, 0], STICKY, true, SHADOW, true, CLICKCLOSE, true, FADEOUT, 400, TEXTALIGN, 'right', BORDERCOLOR, '#F5F5F5')" onMouseOut="UnTip()">Data Source Options</a></td>
			</tr>
		</table>
		<?php
	}

	print "<form method='post' action='" .  basename($_SERVER["PHP_SELF"]) . "' name='data_source_edit'>\n";
	html_start_box("<strong>" . __("Data Source Template Selection") . "</strong> $header_label", "100", $colors["header"], 0, "center", "");
	$header_items = array(__("Field"), __("Value"));
	print "<tr><td>";
	html_header($header_items, 1, true, 'template');

	$form_array = fields_data_source_form_list();
	$form_array["data_template_id"]["id"] = (isset($data_template["id"]) ? $data_template["id"] : "0");
	$form_array["data_template_id"]["name"] = db_fetch_cell("SELECT name FROM data_template WHERE id=" . $form_array["data_template_id"]["id"]);
	$form_array["device_id"]["id"] = (isset($_GET["device_id"]) ? $_GET["device_id"] : $data_local["device_id"]);
	$form_array["device_id"]["name"] = db_fetch_cell("SELECT CONCAT_WS('',description,' (',hostname,')') FROM device WHERE id=" . $form_array["device_id"]["id"]);

	draw_edit_form(
		array(
			"config" => array(),
			"fields" => $form_array
			)
		);

	print "</table></td></tr>";		/* end of html_header */
	html_end_box();
	form_hidden_box("hidden_data_template_id", (isset($data_template["id"]) ? $data_template["id"] : "0"), "");
	form_hidden_box("hidden_device_id", (empty($data_local["device_id"]) ? (isset($_GET["device_id"]) ? $_GET["device_id"] : "0") : $data_local["device_id"]), "");
	form_hidden_box("hidden_data_input_id", (isset($data["data_input_id"]) ? $data["data_input_id"] : "0"), "");
	form_hidden_box("data_template_data_id", (isset($data) ? $data["id"] : "0"), "");
	form_hidden_box("local_data_template_data_id", (isset($data) ? $data["local_data_template_data_id"] : "0"), "");
	form_hidden_box("local_data_id", (isset($data) ? $data["local_data_id"] : "0"), "");

	/* only display the "inputs" area if we are using a data template for this data source */
	if (!empty($data["data_template_id"])) {

		html_start_box("<strong>" . __("Supplemental Data Source Template Data") . "</strong>", "100", $colors["header"], 0, "center", "");

		draw_nontemplated_fields_data_source($data["data_template_id"], $data["local_data_id"], $data, "|field|", "<strong>" . __("Data Source Fields") . "</strong>", true, true, 0);
		draw_nontemplated_fields_data_source_item($data["data_template_id"], $data_source_items, "|field|_|id|", "<strong>" . __("Data Source Item Fields") . "</strong>", true, true, true, 0);
		draw_nontemplated_fields_custom_data($data["id"], "value_|id|", "<strong>" . __("Custom Data") . "</strong>", true, true, 0);

		html_end_box();

		form_hidden_box("save_component_data","1","");
	}

	if (((isset($_GET["id"])) || (isset($_GET["new"]))) && (empty($data["data_template_id"]))) {
		html_start_box("<strong>" . __("Data Source") . "</strong>", "100", $colors["header"], "3", "center", "");

		$form_array = array();

		$struct_data_source = data_source_form_list();
		while (list($field_name, $field_array) = each($struct_data_source)) {
			$form_array += array($field_name => $struct_data_source[$field_name]);

			$form_array[$field_name]["value"] = (isset($data[$field_name]) ? $data[$field_name] : "");
			$form_array[$field_name]["form_id"] = (empty($data["id"]) ? "0" : $data["id"]);

			if (!(($use_data_template == false) || (!empty($data_template_data{"t_" . $field_name})) || ($field_array["flags"] == "NOTEMPLATE"))) {
				$form_array[$field_name]["method"] = "template_" . $form_array[$field_name]["method"];
			}
		}

		draw_edit_form(
			array(
				"config" => array("no_form_tag" => true),
				"fields" => inject_form_variables($form_array, (isset($data) ? $data : array()))
				)
			);

		html_end_box();


		if (!empty($_GET["id"])) {

			html_start_box("<strong>" . __("Data Source Items") . "</strong>", "100", $colors["header"], "0", "center", "data_sources_items.php?action=item_edit&local_data_id=" . $_GET["id"], true);
			draw_data_template_items_list($data_source_items, "data_sources_items.php", "local_data_id=" . $_GET["id"], $use_data_template);
			html_end_box(false);
		}

		/* data source data goes here */
		data_source_data_edit();
	}

	/* display the debug mode box if the user wants it */
	if ((isset($_SESSION["ds_debug_mode"])) && (isset($_GET["id"]))) {
		?>
		<table width="100%" align="center">
			<tr>
				<td>
					<span class="textInfo"><?php print __("Data Source Debug");?></span><br>
					<pre><?php print rrdtool_function_create(get_request_var("id"), true, array());?></pre>
				</td>
			</tr>
		</table>
		<?php
	}

	if ((isset($_SESSION["ds_info_mode"])) && (isset($_GET["id"]))) {
		$rrd_info = rrdtool_function_info($_GET["id"]);

		if (sizeof($rrd_info["rra"])) {
			$diff = rrdtool_cacti_compare($_GET["id"], $rrd_info);
			rrdtool_info2html($rrd_info, $diff);
			rrdtool_tune($rrd_info["filename"], $diff, true);
		}
	}

	if ((isset($_GET["id"])) || (isset($_GET["new"]))) {
		form_hidden_box("save_component_data_source","1","");
	}else{
		form_hidden_box("save_component_data_source_new","1","");
	}

	form_save_button_alt();

	include_once(CACTI_BASE_PATH . "/access/js/data_source_item.js");
	include_once(CACTI_BASE_PATH . "/include/bottom_footer.php");
}
Esempio n. 5
0
function ds_edit()
{
    global $struct_data_source, $struct_data_source_item, $data_source_types;
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var_request('id'));
    input_validate_input_number(get_request_var_request('host_id'));
    /* ==================================================== */
    api_plugin_hook('data_source_edit_top');
    $use_data_template = true;
    $host_id = 0;
    if (!empty($_REQUEST['id'])) {
        $data_local = db_fetch_row_prepared('SELECT host_id, data_template_id FROM data_local WHERE id = ?', array($_REQUEST['id']));
        $data = db_fetch_row_prepared('SELECT * FROM data_template_data WHERE local_data_id = ?', array($_REQUEST['id']));
        if (isset($data_local['data_template_id']) && $data_local['data_template_id'] >= 0) {
            $data_template = db_fetch_row_prepared('SELECT id, name FROM data_template WHERE id = ?', array($data_local['data_template_id']));
            $data_template_data = db_fetch_row_prepared('SELECT * FROM data_template_data WHERE data_template_id = ? AND local_data_id = 0', array($data_local['data_template_id']));
        } else {
            $_SESSION['sess_messages'] = 'Data Source "' . $_REQUEST['id'] . '" does not exist.';
            header('Location: data_sources.php');
            exit;
        }
        $header_label = '[edit: ' . htmlspecialchars(get_data_source_title($_REQUEST['id'])) . ']';
        if (empty($data_local['data_template_id'])) {
            $use_data_template = false;
        }
    } else {
        $header_label = '[new]';
        $use_data_template = false;
    }
    /* handle debug mode */
    if (isset($_REQUEST['debug'])) {
        if ($_REQUEST['debug'] == '0') {
            kill_session_var('ds_debug_mode');
        } elseif ($_REQUEST['debug'] == '1') {
            $_SESSION['ds_debug_mode'] = true;
        }
    }
    top_header();
    if (!empty($_REQUEST['id'])) {
        ?>
		<table width='100%' align='center'>
			<tr>
				<td class='textInfo' colspan='2' valign='top'>
					<?php 
        print htmlspecialchars(get_data_source_title($_REQUEST['id']));
        ?>
				</td>
				<td class='textInfo' align='right' valign='top'>
					<span class='linkMarker'>*<a href='<?php 
        print htmlspecialchars('data_sources.php?action=ds_edit&id=' . (isset($_REQUEST['id']) ? $_REQUEST['id'] : '0'));
        ?>
&debug=<?php 
        print isset($_SESSION['ds_debug_mode']) ? '0' : '1';
        ?>
'>Turn <strong><?php 
        print isset($_SESSION['ds_debug_mode']) ? 'Off' : 'On';
        ?>
</strong> Data Source Debug Mode.</a><br>
					<?php 
        if (!empty($data_template['id'])) {
            ?>
<span class='linkMarker'>*<a href='<?php 
            print htmlspecialchars('data_templates.php?action=template_edit&id=' . (isset($data_template['id']) ? $data_template['id'] : '0'));
            ?>
'>Edit Data Template.</a><br><?php 
        }
        if (!empty($_REQUEST['host_id']) || !empty($data_local['host_id'])) {
            ?>
<span class='linkMarker'>*<a href='<?php 
            print htmlspecialchars('host.php?action=edit&id=' . (isset($_REQUEST['host_id']) ? $_REQUEST['host_id'] : $data_local['host_id']));
            ?>
'>Edit Device.</a><br><?php 
        }
        ?>
				</td>
			</tr>
		</table>
		<br>
		<?php 
    }
    html_start_box("<strong>Data Template Selection</strong> {$header_label}", '100%', '', '3', 'center', '');
    $form_array = array('data_template_id' => array('method' => 'drop_sql', 'friendly_name' => 'Selected Data Template', 'description' => 'The name given to this data template.', 'value' => isset($data_template) ? $data_template['id'] : '0', 'none_value' => 'None', 'sql' => 'SELECT id,name FROM data_template order by name'), 'host_id' => array('method' => 'drop_sql', 'friendly_name' => 'Device', 'description' => 'Choose the host that this graph belongs to.', 'value' => isset($_REQUEST['host_id']) ? $_REQUEST['host_id'] : $data_local['host_id'], 'none_value' => 'None', 'sql' => "SELECT id,CONCAT_WS('',description,' (',hostname,')') as name FROM host order by description,hostname"), '_data_template_id' => array('method' => 'hidden', 'value' => isset($data_template) ? $data_template['id'] : '0'), '_host_id' => array('method' => 'hidden', 'value' => empty($data_local['host_id']) ? isset($_REQUEST['host_id']) ? $_REQUEST['host_id'] : '0' : $data_local['host_id']), '_data_input_id' => array('method' => 'hidden', 'value' => isset($data['data_input_id']) ? $data['data_input_id'] : '0'), 'data_template_data_id' => array('method' => 'hidden', 'value' => isset($data) ? $data['id'] : '0'), 'local_data_template_data_id' => array('method' => 'hidden', 'value' => isset($data) ? $data['local_data_template_data_id'] : '0'), 'local_data_id' => array('method' => 'hidden', 'value' => isset($data) ? $data['local_data_id'] : '0'));
    draw_edit_form(array('config' => array(), 'fields' => $form_array));
    html_end_box();
    /* only display the "inputs" area if we are using a data template for this data source */
    if (!empty($data['data_template_id'])) {
        $template_data_rrds = db_fetch_assoc_prepared('SELECT * FROM data_template_rrd WHERE local_data_id = ? ORDER BY data_source_name', array($_REQUEST['id']));
        html_start_box('<strong>Supplemental Data Template Data</strong>', '100%', '', '3', 'center', '');
        draw_nontemplated_fields_data_source($data['data_template_id'], $data['local_data_id'], $data, '|field|', '<strong>Data Source Fields</strong>', true, true, 0);
        draw_nontemplated_fields_data_source_item($data['data_template_id'], $template_data_rrds, '|field|_|id|', '<strong>Data Source Item Fields</strong>', true, true, true, 0);
        draw_nontemplated_fields_custom_data($data['id'], 'value_|id|', '<strong>Custom Data</strong>', true, true, 0);
        form_hidden_box('save_component_data', '1', '');
        html_end_box();
    }
    if ((isset($_REQUEST['id']) || isset($_REQUEST['new'])) && empty($data['data_template_id'])) {
        html_start_box('<strong>Data Source</strong>', '100%', '', '3', 'center', '');
        $form_array = array();
        while (list($field_name, $field_array) = each($struct_data_source)) {
            $form_array += array($field_name => $struct_data_source[$field_name]);
            if (!($use_data_template == false || !empty($data_template_data['t_' . $field_name]) || $field_array['flags'] == 'NOTEMPLATE')) {
                $form_array[$field_name]['description'] = '';
            }
            $form_array[$field_name]['value'] = isset($data[$field_name]) ? $data[$field_name] : '';
            $form_array[$field_name]['form_id'] = empty($data['id']) ? '0' : $data['id'];
            if (!($use_data_template == false || !empty($data_template_data['t_' . $field_name]) || $field_array['flags'] == 'NOTEMPLATE')) {
                $form_array[$field_name]['method'] = 'template_' . $form_array[$field_name]['method'];
            }
        }
        draw_edit_form(array('config' => array('no_form_tag' => true), 'fields' => inject_form_variables($form_array, isset($data) ? $data : array())));
        html_end_box();
        /* fetch ALL rrd's for this data source */
        if (!empty($_REQUEST['id'])) {
            $template_data_rrds = db_fetch_assoc_prepared('SELECT id, data_source_name FROM data_template_rrd WHERE local_data_id = ? ORDER BY data_source_name', array($_REQUEST['id']));
        }
        /* select the first "rrd" of this data source by default */
        if (empty($_REQUEST['view_rrd'])) {
            $_REQUEST['view_rrd'] = isset($template_data_rrds[0]['id']) ? $template_data_rrds[0]['id'] : '0';
        }
        /* get more information about the rrd we chose */
        if (!empty($_REQUEST['view_rrd'])) {
            $local_data_template_rrd_id = db_fetch_cell_prepared('SELECT local_data_template_rrd_id FROM data_template_rrd WHERE id = ?', array($_REQUEST['view_rrd']));
            $rrd = db_fetch_row_prepared('SELECT * FROM data_template_rrd WHERE id = ?', array($_REQUEST['view_rrd']));
            $rrd_template = db_fetch_row_prepared('SELECT * FROM data_template_rrd WHERE id = ?', array($local_data_template_rrd_id));
            $header_label = '[edit: ' . $rrd['data_source_name'] . ']';
        } else {
            $header_label = '';
        }
        $i = 0;
        if (isset($template_data_rrds)) {
            if (sizeof($template_data_rrds) > 1) {
                /* draw the data source tabs on the top of the page */
                print "\t<table class='tabs' width='100%' cellspacing='0' cellpadding='3' align='center'>\n\t\t\t\t\t<tr>\n";
                foreach ($template_data_rrds as $template_data_rrd) {
                    $i++;
                    print "\t<td " . ($template_data_rrd['id'] == $_REQUEST['view_rrd'] ? "class='even'" : "class='odd'") . " width='" . (strlen($template_data_rrd['data_source_name']) * 9 + 50) . "' align='center' class='tab'>\n\t\t\t\t\t\t\t\t<span class='textHeader'><a href='" . htmlspecialchars('data_sources.php?action=ds_edit&id=' . $_REQUEST['id'] . '&view_rrd=' . $template_data_rrd['id']) . "'>{$i}: " . htmlspecialchars($template_data_rrd['data_source_name']) . '</a>' . ($use_data_template == false ? " <a href='" . htmlspecialchars('data_sources.php?action=rrd_remove&id=' . $template_data_rrd['id'] . '&local_data_id=' . $_REQUEST['id']) . "'><img src='images/delete_icon.gif' border='0' alt='Delete'></a>" : '') . "</span>\n\t\t\t\t\t\t\t</td>\n\n\t\t\t\t\t\t\t<td width='1'></td>\n";
                }
                print "\n\t\t\t\t\t<td></td>\n\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n";
            } elseif (sizeof($template_data_rrds) == 1) {
                $_REQUEST['view_rrd'] = $template_data_rrds[0]['id'];
            }
        }
        html_start_box('', '100%', '', '3', 'center', '');
        print "\t<tr>\n\t\t\t\t<td class='textHeaderDark'>\n\t\t\t\t\t<strong>Data Source Item</strong> {$header_label}\n\t\t\t\t</td>\n\t\t\t\t<td class='textHeaderDark' align='right'>\n\t\t\t\t\t" . (!empty($_REQUEST['id']) && empty($data_template['id']) ? "<strong><a class='linkOverDark' href='" . htmlspecialchars('data_sources.php?action=rrd_add&id=' . $_REQUEST['id']) . "'>New</a>&nbsp;</strong>" : '') . "\n\t\t\t\t</td>\n\t\t\t</tr>\n";
        /* data input fields list */
        if (empty($data['data_input_id']) || db_fetch_cell_prepared('SELECT type_id FROM data_input WHERE id = ?', array($data['data_input_id'])) > '1') {
            unset($struct_data_source_item['data_input_field_id']);
        } else {
            $struct_data_source_item['data_input_field_id']['sql'] = "SELECT id,CONCAT(data_name,' - ',name) as name FROM data_input_fields WHERE data_input_id=" . $data['data_input_id'] . " and input_output='out' and update_rra='on' order by data_name,name";
        }
        $form_array = array();
        while (list($field_name, $field_array) = each($struct_data_source_item)) {
            $form_array += array($field_name => $struct_data_source_item[$field_name]);
            if (!($use_data_template == false || $rrd_template['t_' . $field_name] == 'on')) {
                $form_array[$field_name]['description'] = '';
            }
            $form_array[$field_name]['value'] = isset($rrd) ? $rrd[$field_name] : '';
            if (!($use_data_template == false || $rrd_template['t_' . $field_name] == 'on')) {
                $form_array[$field_name]['method'] = 'template_' . $form_array[$field_name]['method'];
            }
        }
        draw_edit_form(array('config' => array('no_form_tag' => true), 'fields' => array('data_template_rrd_id' => array('method' => 'hidden', 'value' => isset($rrd) ? $rrd['id'] : '0'), 'local_data_template_rrd_id' => array('method' => 'hidden', 'value' => isset($rrd) ? $rrd['local_data_template_rrd_id'] : '0')) + $form_array));
        html_end_box();
        /* data source data goes here */
        data_edit();
        form_hidden_box('current_rrd', $_REQUEST['view_rrd'], '0');
    }
    /* display the debug mode box if the user wants it */
    if (isset($_SESSION['ds_debug_mode']) && isset($_REQUEST['id'])) {
        ?>
		<table width='100%' align='center'>
			<tr>
				<td>
					<span class='textInfo'>Data Source Debug</span><br>
					<pre><?php 
        print @rrdtool_function_create($_REQUEST['id'], true);
        ?>
</pre>
				</td>
			</tr>
		</table>
		<?php 
    }
    if (isset($_REQUEST['id']) || isset($_REQUEST['new'])) {
        form_hidden_box('save_component_data_source', '1', '');
    } else {
        form_hidden_box('save_component_data_source_new', '1', '');
    }
    form_save_button('data_sources.php');
    api_plugin_hook('data_source_edit_bottom');
    bottom_footer();
}
Esempio n. 6
0
function ds_edit()
{
    global $colors, $data_source_types;
    $host_id = 0;
    if (!empty($_GET["id"])) {
        $data_source = db_fetch_row("select * from data_source where id = " . $_GET["id"]);
        $data_source_items = db_fetch_assoc("select * from data_source_item where data_source_id = " . $_GET["id"]);
        if (!empty($data_source["data_template_id"])) {
            $data_template = db_fetch_row("select id,name from data_template where id='" . $data_source["data_template_id"] . "'");
        }
        $header_label = _("[edit: ") . api_data_source_title_get($_GET["id"]) . "]";
        /* get a list of all data input type fields for this data template */
        $data_input_type_fields = array_rekey(db_fetch_assoc("select name,value from data_source_field where data_source_id = " . $data_source["id"]), "name", array("value"));
    } else {
        $header_label = _("[new]");
        $data_input_type_fields = array();
    }
    /* handle debug mode */
    if (isset($_GET["debug"])) {
        if ($_GET["debug"] == "0") {
            kill_session_var("ds_debug_mode");
        } elseif ($_GET["debug"] == "1") {
            $_SESSION["ds_debug_mode"] = true;
        }
    }
    if (!empty($_GET["id"])) {
        ?>
		<table width="98%" align="center">
			<tr>
				<td class="textInfo" colspan="2" valign="top">
					<?php 
        print api_data_source_title_get($_GET["id"]);
        ?>
				</td>
				<td class="textInfo" align="right" valign="top">
					<span style="color: #c16921;">*<a href='data_sources.php?action=edit&id=<?php 
        print isset($_GET["id"]) ? $_GET["id"] : 0;
        ?>
&debug=<?php 
        print isset($_SESSION["ds_debug_mode"]) ? "0" : "1";
        ?>
'>Turn <strong><?php 
        print isset($_SESSION["ds_debug_mode"]) ? "Off" : "On";
        ?>
</strong> Data Source Debug Mode.</a>
				</td>
			</tr>
		</table>
		<br>
		<?php 
    }
    form_start("data_sources.php", "form_data_source");
    /* ==================== Box: Device/Template Selection ==================== */
    html_start_box("<strong>" . _("Device/Template Selection") . "</strong> {$header_label}");
    _data_source_field__data_template_id("data_template_id", isset($data_source) ? $data_source["data_template_id"] : "", empty($_GET["id"]) ? 0 : $_GET["id"]);
    _data_source_field__host_id("host_id", isset($data_source) ? $data_source["host_id"] : "", empty($_GET["id"]) ? 0 : $_GET["id"]);
    html_end_box();
    /* ==================== Box: Supplemental Template Data ==================== */
    /* only display the "inputs" area if we are using a data template for this data source */
    if (!empty($data_source["data_template_id"])) {
        ob_start();
        html_start_box("<strong>" . _("Supplemental Template Data") . "</strong>");
        $num_output_fields = draw_nontemplated_fields_data_input($data_source["data_template_id"], $data_input_type_fields, "dif_|field|", "<strong>" . _("Data Input") . "</strong>", true);
        if ($data_source["data_input_type"] == DATA_INPUT_TYPE_DATA_QUERY) {
            _data_source_input_field__data_query_hdr();
            _data_source_input_field__data_query_field_name("dif_data_query_field_name", $data_input_type_fields["data_query_id"]["value"], isset($data_input_type_fields["data_query_field_name"]["value"]) ? $data_input_type_fields["data_query_field_name"]["value"] : "", empty($_GET["id"]) ? 0 : $_GET["id"]);
            _data_source_input_field__data_query_field_value("dif_data_query_field_value", isset($data_input_type_fields["data_query_field_name"]["value"]) ? $data_input_type_fields["data_query_field_value"]["value"] : "", empty($_GET["id"]) ? 0 : $_GET["id"]);
            $num_output_fields += 2;
        }
        $num_output_fields += draw_nontemplated_fields_data_source($data_source["data_template_id"], $data_source, "ds||field|", "<strong>" . _("Data Source Fields") . "</strong>", true, true);
        $num_output_fields += draw_nontemplated_fields_data_source_item($data_source["data_template_id"], db_fetch_assoc("select * from data_source_item where data_source_id = " . $data_source["id"] . " order by data_source_name"), "dsi||field|||id|", "<strong>" . _("Data Source Item Fields") . "</strong>", true, true);
        html_end_box();
        if ($num_output_fields == 0) {
            ob_end_clean();
        } else {
            ob_end_flush();
        }
    }
    if (empty($data_source["data_template_id"]) && (isset($_GET["id"]) && is_numeric($_GET["id"]) || isset($_GET["host_id"]) && isset($_GET["data_template_id"]))) {
        /* determine current value for 'data_input_type' */
        if (isset($_GET["data_input_type"])) {
            $_data_input_type = $_GET["data_input_type"];
        } else {
            if (isset($data_source["data_input_type"])) {
                $_data_input_type = $data_source["data_input_type"];
            } else {
                $_data_input_type = DATA_INPUT_TYPE_SCRIPT;
            }
        }
        /* ==================== Box: Data Input ==================== */
        html_start_box("<strong>" . _("Data Input") . "</strong>");
        _data_source_input_field__data_input_type("data_input_type", false, $_data_input_type, empty($_GET["id"]) ? 0 : $_GET["id"]);
        /* grab the appropriate data input type form array */
        if ($_data_input_type == DATA_INPUT_TYPE_SCRIPT) {
            $scripts = api_script_list();
            if (sizeof($scripts) > 0) {
                /* determine current value for 'script_id' */
                if (isset($_GET["script_id"]) && is_numeric($_GET["script_id"])) {
                    $_script_id = $_GET["script_id"];
                } else {
                    if (isset($data_input_type_fields["script_id"])) {
                        $_script_id = $data_input_type_fields["script_id"]["value"];
                    } else {
                        /* default to the first item in the script list */
                        $_script_id = $scripts[0]["id"];
                    }
                }
                field_row_header(_("External Script"));
                _data_source_input_field__script_id("dif_script_id", "data_sources.php?action=edit" . (!empty($_GET["id"]) ? "&id=" . $_GET["id"] : "") . "&data_template_id=" . (isset($_GET["data_template_id"]) ? $_GET["data_template_id"] : (isset($data_source) ? $data_source["data_template_id"] : 0)) . "&host_id=" . (isset($_GET["host_id"]) ? $_GET["host_id"] : (isset($data_source) ? $data_source["host_id"] : 0)) . "&data_input_type={$_data_input_type}&script_id=|dropdown_value|", $_script_id);
                /* get each INPUT field for this script */
                $script_input_fields = db_fetch_assoc("select * from data_input_fields where data_input_id = {$_script_id} and input_output='in' order by name");
                if (sizeof($script_input_fields) > 0) {
                    field_row_header(_("Custom Input Fields"));
                    foreach ($script_input_fields as $field) {
                        _data_source_input_field__script("dif_" . $field["data_name"], $field["name"], false, isset($data_input_type_fields[$field["data_name"]]) ? $data_input_type_fields[$field["data_name"]]["value"] : "", "", "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    }
                }
            }
        } else {
            if ($_data_input_type == DATA_INPUT_TYPE_DATA_QUERY) {
                /* determine current value for 'data_query_id' */
                if (isset($_GET["data_query_id"]) && is_numeric($_GET["data_query_id"])) {
                    $_data_query_id = $_GET["data_query_id"];
                } else {
                    if (isset($data_input_type_fields["data_query_id"])) {
                        $_data_query_id = $data_input_type_fields["data_query_id"]["value"];
                    } else {
                        /* default to the first item in the data query list */
                        $_data_query_id = $data_queries[0]["id"];
                    }
                }
                field_row_header(_("Data Query"));
                _data_source_input_field__data_query_id("dif_data_query_id", "data_sources.php?action=edit" . (!empty($_GET["id"]) ? "&id=" . $_GET["id"] : "") . "&data_template_id=" . (isset($_GET["data_template_id"]) ? $_GET["data_template_id"] : (isset($data_source) ? $data_source["data_template_id"] : 0)) . "&host_id=" . (isset($_GET["host_id"]) ? $_GET["host_id"] : (isset($data_source) ? $data_source["host_id"] : 0)) . "&data_input_type={$_data_input_type}&data_query_id=|dropdown_value|", $_data_query_id);
            } else {
                if ($_data_input_type == DATA_INPUT_TYPE_SNMP) {
                    _data_source_input_field__device_hdr_generic();
                    _data_source_input_field__device_snmp_port("dif_snmp_port", false, isset($data_input_type_fields["snmp_port"]) ? $data_input_type_fields["snmp_port"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    _data_source_input_field__device_snmp_timeout("dif_snmp_timeout", false, isset($data_input_type_fields["snmp_timeout"]) ? $data_input_type_fields["snmp_timeout"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    _data_source_input_field__device_snmp_version("dif_snmp_version", false, isset($data_input_type_fields["snmp_version"]) ? $data_input_type_fields["snmp_version"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    _data_source_input_field__device_hdr_snmpv12();
                    _data_source_input_field__device_snmp_community("dif_snmp_community", false, isset($data_input_type_fields["snmp_community"]) ? $data_input_type_fields["snmp_community"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    _data_source_input_field__device_hdr_snmpv3();
                    _data_source_input_field__device_snmpv3_auth_username("dif_snmpv3_auth_username", false, isset($data_input_type_fields["snmpv3_auth_username"]) ? $data_input_type_fields["snmpv3_auth_username"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    _data_source_input_field__device_snmpv3_auth_password("dif_snmpv3_auth_password", false, isset($data_input_type_fields["snmpv3_auth_password"]) ? $data_input_type_fields["snmpv3_auth_password"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    _data_source_input_field__device_snmpv3_auth_protocol("dif_snmpv3_auth_protocol", false, isset($data_input_type_fields["snmpv3_auth_protocol"]) ? $data_input_type_fields["snmpv3_auth_protocol"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    _data_source_input_field__device_snmpv3_priv_passphrase("dif_snmpv3_priv_passphrase", false, isset($data_input_type_fields["snmpv3_priv_passphrase"]) ? $data_input_type_fields["snmpv3_priv_passphrase"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                    _data_source_input_field__device_snmpv3_priv_protocol("dif_snmpv3_priv_protocol", false, isset($data_input_type_fields["snmpv3_priv_protocol"]) ? $data_input_type_fields["snmpv3_priv_protocol"]["value"] : "", isset($_GET["id"]) ? $_GET["id"] : 0);
                }
            }
        }
        html_end_box();
        /* ==================== Box: Data Source ==================== */
        if (empty($_GET["id"])) {
            $rra_items = array();
        } else {
            $rra_items = api_data_source_rra_item_list($data_source["id"]);
        }
        html_start_box("<strong>" . _("Data Source") . "</strong>");
        _data_source_field__name("ds|name", false, empty($_GET["id"]) ? 0 : $_GET["id"]);
        _data_source_field__rra("preset_rra_id", false, isset($data_source["preset_rra_id"]) ? $data_source["preset_rra_id"] : "", empty($_GET["id"]) ? 0 : $_GET["id"]);
        _data_source_field__polling_interval("ds|polling_interval", false, isset($data_source["polling_interval"]) ? $data_source["polling_interval"] : "", empty($_GET["id"]) ? 0 : $_GET["id"]);
        _data_source_field__active("ds|active", false, isset($data_source["active"]) ? $data_source["active"] : "", empty($_GET["id"]) ? 0 : $_GET["id"]);
        html_end_box();
        /* ==================== Box: Data Source Item ==================== */
        html_start_box("<strong>" . _("Data Source Item") . "</strong>", empty($_GET["id"]) ? "" : "javascript:document.forms[0].action.value='item_add';submit_redirect(0, '" . htmlspecialchars("data_sources.php?action=item_add&id=" . $_GET["id"]) . "', '')");
        /* the user clicked the "add item" link. we need to make sure they get redirected back to
         * this page if an error occurs */
        if ($_GET["action"] == "item_add") {
            form_hidden_box("redirect_item_add", "x", "");
        }
        /* this allows a "blank" data template item to be displayed when the user wants to create
         * a new one */
        if (!isset($data_source_items) || sizeof($data_source_items) == 0 || $_GET["action"] == "item_add") {
            if (isset($data_source_items)) {
                $next_index = sizeof($data_source_items);
            } else {
                $next_index = 0;
            }
            $data_source_items[$next_index] = array();
        }
        if (sizeof($data_source_items) > 0) {
            if ($_data_input_type == DATA_INPUT_TYPE_SCRIPT) {
                $script_output_fields = db_fetch_assoc("select * from data_input_fields where data_input_id = {$_script_id} and input_output='out' order by name");
                $field_input_description = _("Script Output Field");
            } else {
                if ($_data_input_type == DATA_INPUT_TYPE_DATA_QUERY) {
                    $field_list = api_data_query_field_list($_data_query_id, DATA_QUERY_FIELD_TYPE_OUTPUT);
                    $data_query_output_fields = array();
                    if (sizeof($field_list) > 0) {
                        foreach ($field_list as $field) {
                            $data_query_output_fields[$field["name"]] = $field["name"] . " (" . $field["name_desc"] . ")";
                        }
                    }
                    $field_input_description = _("Data Query Output Field");
                } else {
                    if ($_data_input_type == DATA_INPUT_TYPE_SNMP) {
                        $field_input_description = _("SNMP OID");
                    }
                }
            }
            foreach ($data_source_items as $item) {
                if ($_data_input_type != DATA_INPUT_TYPE_NONE) {
                    ?>
					<tr bgcolor="<?php 
                    print $colors["header_panel_background"];
                    ?>
">
						<td class='textSubHeaderDark' colspan="2">
							<?php 
                    print isset($item["data_source_name"]) ? $item["data_source_name"] : "(" . _("New Data Source Item") . ")";
                    ?>
						</td>
						<td class='textSubHeaderDark' align='right'>
							<?php 
                    if (isset($item["id"]) && sizeof($data_source_items) > 1) {
                        print "[<a href='data_sources.php?action=item_remove&id=" . $item["id"] . "&data_source_id=" . $item["data_source_id"] . "' class='linkOverDark'>remove</a>]\n";
                    }
                    ?>
						</td>
					</tr>
					<tr bgcolor="#e1e1e1">
						<td width="50%" style="border-bottom: 1px solid #a1a1a1;">
							<font class='textEditTitle'>Field Input: <?php 
                    print $field_input_description;
                    ?>
</font><br>
						</td>
						<td style="border-bottom: 1px solid #a1a1a1;" colspan="2">
							<?php 
                    if ($_data_input_type == DATA_INPUT_TYPE_SCRIPT) {
                        form_dropdown("dsi|field_input_value|" . (isset($item["id"]) ? $item["id"] : "0"), $script_output_fields, "name", "data_name", isset($item["field_input_value"]) ? $item["field_input_value"] : "", "", "");
                    } else {
                        if ($_data_input_type == DATA_INPUT_TYPE_DATA_QUERY) {
                            form_dropdown("dsi|field_input_value|" . (isset($item["id"]) ? $item["id"] : "0"), $data_query_output_fields, "", "", isset($item["field_input_value"]) ? $item["field_input_value"] : "", "", "");
                        } else {
                            if ($_data_input_type == DATA_INPUT_TYPE_SNMP) {
                                form_text_box("dsi|field_input_value|" . (isset($item["id"]) ? $item["id"] : "0"), isset($item["field_input_value"]) ? $item["field_input_value"] : "", "", "100", 40, "text", 0);
                            }
                        }
                    }
                    ?>
						</td>
					</tr>
					<?php 
                }
                $_field_id = isset($item["id"]) ? $item["id"] : 0;
                field_reset_row_color();
                field_increment_row_color();
                _data_source_item_field__data_source_name("dsi|data_source_name|{$_field_id}", false, isset($item["data_source_name"]) ? $item["data_source_name"] : "", $_field_id);
                _data_source_item_field__rrd_minimum("dsi|rrd_minimum|{$_field_id}", false, isset($item["rrd_minimum"]) ? $item["rrd_minimum"] : "", $_field_id, "dsi|t_rrd_minimum|{$_field_id}");
                _data_source_item_field__rrd_maximum("dsi|rrd_maximum|{$_field_id}", false, isset($item["rrd_maximum"]) ? $item["rrd_maximum"] : "", $_field_id, "dsi|t_rrd_maximum|{$_field_id}");
                _data_source_item_field__data_source_type("dsi|data_source_type|{$_field_id}", false, isset($item["data_source_type"]) ? $item["data_source_type"] : "", $_field_id, "dsi|t_data_source_type|{$_field_id}");
                _data_source_item_field__rrd_heartbeat("dsi|rrd_heartbeat|{$_field_id}", false, isset($item["rrd_heartbeat"]) ? $item["rrd_heartbeat"] : "", $_field_id, "dsi|t_rrd_heartbeat|{$_field_id}");
            }
        }
        html_end_box();
    }
    /* display the debug mode box if the user wants it */
    if (isset($_SESSION["ds_debug_mode"]) && isset($_GET["id"])) {
        html_start_box("<strong>Data Source Debug</strong>", "98%", $colors["header_background"], "3", "center", "");
        print "<tr><td bgcolor=#'" . $colors["messagebar_background"] . "' <pre>" . rrdtool_function_create($_GET["id"], true, array()) . "</pre></td></tr>";
        html_end_box();
    }
    if (isset($_GET["id"]) || isset($_GET["host_id"]) && isset($_GET["data_template_id"])) {
        form_hidden_box("action_post", "data_source_edit");
    } else {
        form_hidden_box("action_post", "data_source_add");
    }
    form_hidden_box("data_source_id", get_get_var_number("id"));
    form_save_button("data_sources.php");
}