Ejemplo n.º 1
0
function draw_graph_item_editor($graph_X_id, $form_type, $disable_controls) {
	global $colors;

	require_once(CACTI_BASE_PATH . "/lib/graph/graph_utility.php");
	require_once(CACTI_BASE_PATH . "/include/graph/graph_constants.php");
	require(CACTI_BASE_PATH . "/include/graph/graph_arrays.php");
	require(CACTI_BASE_PATH . "/include/data_source/data_source_arrays.php");

	$graph_actions = array(
		1 => _("Delete Items"),
		2 => _("Duplicate Items")
		);

	if ($form_type == "graph_template") {
		$item_list = db_fetch_assoc("select
			graph_template_item.id,
			graph_template_item.legend_format,
			graph_template_item.legend_value,
			graph_template_item.gprint_format,
			graph_template_item.hard_return,
			graph_template_item.graph_item_type,
			graph_template_item.consolidation_function,
			graph_template_item.color,
			graph_template_item.sequence,
			graph_template.auto_padding,
			data_template_item.data_source_name
			from (graph_template_item,graph_template)
			left join data_template_item on (graph_template_item.data_template_item_id=data_template_item.id)
			where graph_template_item.graph_template_id=graph_template.id
			and graph_template_item.graph_template_id = $graph_X_id
			order by graph_template_item.sequence");

		$url_filename = "graph_templates_items.php";
		$url_data = "&graph_template_id=$graph_X_id";
	}else if ($form_type == "graph") {
		$item_list = db_fetch_assoc("select
			graph_item.id,
			graph_item.legend_format,
			graph_item.legend_value,
			graph_item.gprint_format,
			graph_item.hard_return,
			graph_item.graph_item_type,
			graph_item.consolidation_function,
			graph_item.color,
			graph_item.sequence,
			graph.auto_padding,
			data_source_item.data_source_name
			from (graph_item,graph)
			left join data_source_item on (graph_item.data_source_item_id=data_source_item.id)
			where graph_item.graph_id=graph.id
			and graph_item.graph_id = $graph_X_id
			order by graph_item.sequence");

		$url_filename = "graphs_items.php";
		$url_data = "&graph_id=$graph_X_id";
	}else{
		return;
	}

	?>
	<tr bgcolor='#<?php echo $colors["header_panel_background"];?>'>
		<td width='12'>
			&nbsp;
		</td>
		<td width='60' class='textSubHeaderDark'>
			Item
		</td>
		<td class='textSubHeaderDark'>
			Graph Item Type
		</td>
		<td class='textSubHeaderDark'>
			Data Source
		</td>
		<td class='textSubHeaderDark'>
			Legend Text
		</td>
		<td class='textSubHeaderDark'>
			Color
		</td>
		<td class='textSubHeaderDark'>
			CF Type
		</td>
		<td>
			&nbsp;
		</td>
		<td>
			&nbsp;
		</td>
		<td width='1%' align='right' bgcolor='#819bc0' style='<?php echo get_checkbox_style();?>'>
			<input type='checkbox' style='margin: 0px;' name='all' title='<?php echo _("Select All");?>' onClick='graph_item_rows_selection(this.checked)'>
		</td>
	</tr>
	<?php

	if (sizeof($item_list) > 0) {
		/* calculate auto padding information and other information that we will need below */
		$max_pad_length = 0;
		$total_num_rows = 0;

		for ($i=0; $i<sizeof($item_list); $i++) {
			if (($i == 0) || (!empty($item_list{$i-1}["hard_return"]))) {
				if (strlen($item_list[$i]["legend_format"]) > $max_pad_length) {
					$max_pad_length = strlen($item_list[$i]["legend_format"]);
				}

				$total_num_rows++;
			}
		}

		$i = 0;
		$row_counter = 1;

		/* preload expand/contract icons */
		echo "<script type='text/javascript'>\nvar auxImg;\nauxImg = new Image();auxImg.src = '" . html_get_theme_images_path("show.gif") . "';\nauxImg.src = '" . html_get_theme_images_path("hide.gif") . "';\n</script>\n";

		/* initialize JS variables */
		echo "<script type='text/javascript'>\nvar item_row_list = new Array()\n</script>\n";

		foreach ($item_list as $item) {
			$matrix_title = "";
			$hard_return = "";
			$show_moveup = true;
			$show_movedown = true;

			if (is_graph_item_type_primary($item["graph_item_type"])) {
				$matrix_title = "(" . $item["data_source_name"] . "): " . $item["legend_format"];
			}else if (($item["graph_item_type"] == GRAPH_ITEM_TYPE_HRULE) || ($item["graph_item_type"] == GRAPH_ITEM_TYPE_VRULE)) {
				$matrix_title = $graph_item_types{$item["graph_item_type"]} . ": " . $item["legend_value"];
			}else if ($item["graph_item_type"] == GRAPH_ITEM_TYPE_COMMENT) {
				$matrix_title = "COMMENT: " . $item["legend_format"];
			}

			if (!empty($item["hard_return"])) {
				$hard_return = "<strong><font color=\"#FF0000\">&lt;HR&gt;</font></strong>";
			}

			if (($i == 0) || (!empty($item_list{$i-1}["hard_return"]))) {
				?>
				<tr bgcolor="#<?php echo $colors["form_custom1"];?>">
					<td width='12' style='border-bottom: 1px solid #b5b5b5;' align='center'>
						<a href="javascript:graph_item_row_visibility(<?php echo $row_counter;?>)"><img id='img_<?php echo $row_counter;?>' src='<?php echo html_get_theme_images_path("hide.gif");?>' border='0' title='<?php echo _("Collapse Row");?>' alt='<?php echo _("Collapse Row");?>' align='absmiddle'></a>
					</td>
					<td style='border-right: 1px solid #b5b5b5; border-bottom: 1px solid #b5b5b5;' width='60'>
						<strong><?php echo _("Row #") . $row_counter;?></strong>
					</td>
					<td colspan='5' style='font-family: monospace; color: #515151; cursor: pointer; border-bottom: 1px solid #b5b5b5;' onClick="graph_item_row_visibility(<?php echo $row_counter;?>)" nowrap>
						<pre><?php
						$j = $i;
						$graph_item_row = array();
						do {
							$_item = $item_list[$j];
							if (is_graph_item_type_primary($_item["graph_item_type"])) {
								if ($_item["color"] != "") {
									echo "<img src='" . html_get_theme_images_path("transparent_line.gif") . "'style='width: 9px; height: 9px; border: 1px solid #000000; background-color: #" . $_item["color"] . "' border='0' align='absmiddle' alt=''>&nbsp;";
								}
							}

							if ($_item["graph_item_type"] == GRAPH_ITEM_TYPE_GPRINT) {
								printf(" " . $_item["legend_format"] . $_item["gprint_format"], "0", "");
							}else{
								echo $_item["legend_format"];
							}

							/* the first item of the row is where auto padding is applied */
							if ($i == $j) {
								echo (empty($_item["auto_padding"])) ? "" : str_repeat(" ", (($max_pad_length + 1) - strlen($_item["legend_format"])));
							}

							/* keep track of each item in this row so we can create a JS array below */
							$graph_item_row[] = $_item["id"];

							$j++;
						} while ((empty($item_list{$j-1}["hard_return"])) && (($j+1)<=sizeof($item_list)));
						?></pre>
					</td>
					<td align='center' width='15' style='border-bottom: 1px solid #b5b5b5;'>
						<?php if ($row_counter < $total_num_rows) { ?>
						<a href='<?php echo $url_filename;?>?action=row_movedown&row=<?php echo $row_counter;?><?php echo $url_data;?>'><img src='<?php echo html_get_theme_images_path("move_down.gif");?>' border='0' title='<?php echo _("Move Item Down");?>' alt='<?php echo _("Move Item Down");?>'></a>
						<?php }else{ ?>
						&nbsp;
						<?php } ?>
					</td>
					<td align='left' width='25' style='border-bottom: 1px solid #b5b5b5;'>
						<?php if ($i > 0) { ?>
						<a href='<?php echo $url_filename;?>?action=row_moveup&row=<?php echo $row_counter;?><?php echo $url_data;?>'><img src='<?php echo html_get_theme_images_path("move_up.gif");?>' border='0' title='<?php echo _("Move Item Up");?>' alt='<?php echo _("Move Item Up");?>'></a>
						<?php }else{ ?>
						&nbsp;
						<?php } ?>
					</td>
					<td style="<?php echo get_checkbox_style();?> border-bottom: 1px solid #b5b5b5;" width="1%" align="right">
						<input type='checkbox' style='margin: 0px;' onClick="graph_item_row_selection(<?php echo $row_counter;?>)" name='row_chk_<?php echo $row_counter;?>' id='row_chk_<?php echo $row_counter;?>' title="<?php echo _('Row #') . $row_counter;?>">
					</td>
				</tr>
				<?php

				/* create a JS array of graph items in each row */
				echo "<script type='text/javascript'>\nitem_row_list[$row_counter] = new Array(";

				for ($j=0; $j<sizeof($graph_item_row); $j++) {
					echo "'" . $graph_item_row[$j] . "'" . (($j+1) < sizeof($graph_item_row) ? "," : "");
				}

				echo ")\n</script>\n";

				$row_counter++;
			}

			/* only show arrows when they are supposed to be shown */
			if ($i == 0) {
				$show_moveup = false;
			}else if (($i+1) == sizeof($item_list)) {
				$show_movedown = false;
			}

			if (empty($item["graph_template_item_group_id"])) {
				$row_color = $colors["form_alternate1"];
			}else{
				$row_color = $colors["alternate"];
			}

			?>
			<tr id="tr_<?php echo $item["id"];?>" bgcolor="#<?php echo $row_color;?>">
				<td width='12' align='center'>
					&nbsp;
				</td>
				<td width='60' style='border-right: 1px solid #b5b5b5;'>
					<a href='<?php echo $url_filename;?>?action=edit&id=<?php echo $item["id"];?>&<?php echo $url_data;?>'><?php echo _("Item #") . ($i+1);?></a>
				</td>
				<td>
					<?php echo $graph_item_types{$item["graph_item_type"]};?>
				</td>
				<td>
					<?php echo $item["data_source_name"];?>
				</td>
				<td>
					<?php echo $item["legend_format"];?><?php echo (empty($item["hard_return"]) ? "" : "<span style='color: red; font-weight: bold;'>&lt;HR&gt;</span>");?>
				</td>
				<td>
					<?php echo $item["color"];?>
				</td>
				<td>
					<?php echo $consolidation_functions{$item["consolidation_function"]};?>
				</td>
				<td width='15' align='center'>
					<?php if (($i+1) < sizeof($item_list)) { ?>
					<a href='<?php echo $url_filename;?>?action=item_movedown&id=<?php echo $item["id"];?>&<?php echo $url_data;?>'><img src='<?php echo html_get_theme_images_path("move_down.gif");?>' border='0' title='<?php echo _("Move Item Down");?>' alt='<?php echo _("Move Item Down");?>'></a>
					<?php } ?>
				</td>
				<td width='25' align='left'>
					<?php if ($i > 0) { ?>
					<a href='<?php echo $url_filename;?>?action=item_moveup&id=<?php echo $item["id"];?>&<?php echo $url_data;?>'><img src='<?php echo html_get_theme_images_path("move_up.gif");?>' border='0' title='<?php echo _("Move Item Up");?>' alt='<?php echo _("Move Item Up");?>'></a>
					<?php } ?>
				</td>
				<td width='1' style="<?php echo get_checkbox_style();?>" align="right">
					<input type='checkbox' style='margin: 0px;' name='chk_gi_<?php echo $item["id"];?>' id='chk_<?php echo $item["id"];?>' title="<?php echo _('Item #') . ($i + 1);?>">
				</td>
			</tr>
			<?php

			$i++;
		}

		/* create a JS array for each row */
		echo "<script type='text/javascript'>\nvar item_rows = new Array(";

		for ($j=1; $j<$row_counter; $j++) {
			echo $j . (($j+1) < $row_counter ? "," : "");
		}

		echo ")\n</script>\n";

		?>
		<tr bgcolor='#ffffff'>
			<td colspan='10' style='border-top: 1px dashed #a1a1a1;'>
				<?php draw_actions_dropdown($graph_actions, 2, 100); ?>
			</td>
		</tr>
		<?php
	}else{
		echo "<tr><td><em>" . _("No graph items found.") . "</em></td></tr>\n";
	}
}
Ejemplo n.º 2
0
function variable_bandwidth_summation($var_divisor, $var_type, $var_precision, $var_timespan, &$graph_item, &$graph_items, $graph_start, $graph_end, $rra_step, $ds_step) {
	global $summation_cache;

	require_once(CACTI_BASE_PATH . "/lib/graph/graph_utility.php");

	if (is_numeric($var_timespan)) {
		$summation_timespan_start = -$var_timespan;
	}else{
		$summation_timespan_start = $graph_start;
	}

	if ($var_type == "current") {
		if (!isset($summation_cache{$graph_item["local_data_id"]})) {
			$summation_cache{$graph_item["local_data_id"]} = bandwidth_summation($graph_item["local_data_id"], $summation_timespan_start, $graph_end, $rra_step, $ds_step);
		}
	}elseif ($var_type == "total") {
		for ($t=0; $t<count($graph_items); $t++) {
			if ((!isset($summation_cache{$graph_items[$t]["local_data_id"]})) && (!empty($graph_items[$t]["local_data_id"]))) {
				$summation_cache{$graph_items[$t]["local_data_id"]} = bandwidth_summation($graph_items[$t]["local_data_id"], $summation_timespan_start, $graph_end, $rra_step, $ds_step);
			}
		}
	}elseif ($var_type == "atomic") {
		if (!isset($summation_cache{$graph_item["local_data_id"]})) {
			$summation_cache{$graph_item["local_data_id"]} = bandwidth_summation($graph_item["local_data_id"], $summation_timespan_start, $graph_end, $rra_step, 1);
		}
	}

	$summation = 0;

	/* format the output according to args passed to the variable */
	if (($var_type == "current") || ($var_type == "atomic")) {
		$summation = $summation_cache{$graph_item["local_data_id"]}{$graph_item["data_source_name"]};
	}elseif ($var_type == "total") {
		for ($t=0; $t<count($graph_items); $t++) {
			if ((is_graph_item_type_primary($graph_items[$t]["graph_type_id"])) && (!empty($graph_items[$t]["data_template_rrd_id"]))) {
				$local_summation = $summation_cache{$graph_items[$t]["local_data_id"]}{$graph_items[$t]["data_source_name"]};

				$summation += $local_summation;
			}
		}
	}

	if (is_numeric($var_divisor)) {
		$summation /= pow(10, intval($var_divisor));
	}elseif ($var_divisor == "auto") {
		if ($summation < 1000) {
			$summation_label = "bytes";
		}elseif ($summation < 1000000) {
			$summation_label = "KB";
			$summation /= 1000;
		}elseif ($summation < 1000000000) {
			$summation_label = "MB";
			$summation /= 1000000;
		}elseif ($summation < 1000000000000) {
			$summation_label = "GB";
			$summation /= 1000000000;
		}else{
			$summation_label = "TB";
			$summation /= 1000000000000;
		}
	}

	/* determine the floating point precision */
	if (is_numeric($var_precision)) {
		$round_to = $var_precision;
	}else{
		$round_to = 2;
	}

	/* substitute in the final result and round off to two decimal digits */
	if (isset($summation_label)) {
		return round($summation, $round_to) . " $summation_label";
	}else{
		return round($summation, $round_to);
	}
}
Ejemplo n.º 3
0
function rrdtool_function_graph($graph_id, $rra_id, $graph_data_array, $rrd_struc = array(),$syslog_facility = FACIL_WEBUI) {
	global $colors;

	require_once(CACTI_BASE_PATH . "/lib/sys/auth.php");
	require_once(CACTI_BASE_PATH . "/lib/sys/graph_variable.php");
	require_once(CACTI_BASE_PATH . "/lib/graph/graph_info.php");
	require_once(CACTI_BASE_PATH . "/lib/graph/graph_utility.php");
	require_once(CACTI_BASE_PATH . "/lib/data_source/data_source_info.php");
	require_once(CACTI_BASE_PATH . "/include/graph/graph_constants.php");
	require(CACTI_BASE_PATH . "/include/graph/graph_arrays.php");
	require(CACTI_BASE_PATH . "/include/data_source/data_source_arrays.php");

	/* set the rrdtool default font */
	if (read_config_option("path_rrdtool_default_font")) {
		putenv("RRD_DEFAULT_FONT=" . read_config_option("path_rrdtool_default_font"));
	}

	/* before we do anything; make sure the user has permission to view this graph,
	if not then get out */
	if ((read_config_option("auth_method") != "0") && (isset($_SESSION["sess_user_id"]))) {
		$access_denied = !(is_graph_allowed($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_source.rrd_step
		from data_source,data_source_item,graph_item
		where graph_item.data_source_item_id=data_source_item.id
		and data_source_item.data_source_id=data_source.id
		and graph_item.graph_id = $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 = api_graph_associated_rras_list($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.host_id,
		graph.title_cache,
		graph.vertical_label,
		graph.auto_scale,
		graph.auto_scale_opts,
		graph.auto_scale_log,
		graph.auto_scale_rigid,
		graph.auto_padding,
		graph.base_value,
		graph.upper_limit,
		graph.lower_limit,
		graph.x_grid,
		graph.y_grid,
		graph.y_grid_alt,
		graph.no_minor,
		graph.height,
		graph.width,
		graph.image_format,
		graph.unit_value,
		graph.unit_length,
		graph.unit_exponent_value,
		graph.export
		from graph
		where graph.id = $graph_id");

	/* lets make that sql query... */
	$graph_items = db_fetch_assoc("select
		graph_item.id,
		graph_item.cdef,
		graph_item.legend_format,
		graph_item.legend_value,
		graph_item.hard_return,
		graph_item.consolidation_function,
		graph_item.graph_item_type,
		graph_item.gprint_format,
		graph_item.color,
		graph_item.data_source_item_id,
		data_source_item.data_source_id,
		data_source_item.rrd_minimum,
		data_source_item.rrd_maximum,
		data_source_item.data_source_name
		from graph_item
		left join data_source_item on (graph_item.data_source_item_id=data_source_item.id)
		where graph_item.graph_id = $graph_id
		order by graph_item.sequence");

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

	/* define some variables */
	$scale = "";
	$rigid = "";
	$no_minor = "";
	$y_grid = "";
	$y_grid_alt = "";
	$x_grid = "";
	$unit_value = "";
	$unit_length = "";
	$unit_exponent_value = "";
	$graph_legend = "";
	$graph_defs = "";
	$txt_graph_items = "";
	$text_padding = "";
	$greatest_text_format = 0;
	$last_graph_type = "";

	if (empty($graph["auto_scale"])) {
		$scale =  "--upper-limit=" . $graph["upper_limit"] . RRD_NL;
		$scale .= "--lower-limit=" . $graph["lower_limit"] . RRD_NL;
	}else{
		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 (!empty($graph["auto_scale_log"])) {
			$scale .= "--logarithmic" . RRD_NL;
		}
	}

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

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

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

	if (!empty($graph["y_grid_alt"])) {
		$y_grid_alt = "--alt-y-grid" . RRD_NL;
	}

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

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

	if (is_numeric($graph["unit_length"])) {
		$unit_length = "--units-length=" . $graph["unit_length"] . RRD_NL;
	}

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

	if (is_numeric($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)) {
			if (read_config_option("rrdtool_version") == "rrd-1.2.x") {
				$graph_legend .= "COMMENT:\"" . _("From ") . str_replace(":", "\:", date($graph_date, time()+$graph_data_array["graph_start"])) . _(" To ") . str_replace(":", "\:", date($graph_date, time()+$graph_data_array["graph_end"])) . "\\c\"" . RRD_NL . "COMMENT:\"  \\n\"" . RRD_NL;
			}else {
				$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)) {
			if (read_config_option("rrdtool_version") == "rrd-1.2.x") {
				$graph_legend .= "COMMENT:\"" . _("From ") . str_replace(":", "\:", date($graph_date, $graph_data_array["graph_start"])) . _(" To ") . str_replace(":", "\:", date($graph_date, $graph_data_array["graph_end"])) . "\\c\"" . RRD_NL . "COMMENT:\"  \\n\"" . RRD_NL;
			}else {
				$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=" . $graph_image_types{$graph["image_format"]} . RRD_NL .
		"--start=$graph_start" . RRD_NL .
		"--end=$graph_end" . RRD_NL .
		"-c CANVAS#" . $colors["rrd_canvas"] . " -c FONT#" . $colors["rrd_font"] . " -c BACK#" . $colors["rrd_back"] . " " .
		"--title=\"" . str_replace("\"", "\\\"", $graph["title_cache"]) . "\"" . RRD_NL .
		"$rigid" .
		"--base=" . $graph["base_value"] . RRD_NL .
		"--height=$graph_height" . RRD_NL .
		"--width=$graph_width" . RRD_NL .
		"$scale" .
		"$x_grid" .
		"$y_grid" .
		"$y_grid_alt" .
		"$no_minor" .
		"$unit_value" .
		"$unit_length" .
		"$unit_exponent_value" .
		"$graph_legend" .
		"--vertical-label=\"" . $graph["vertical_label"] . "\"" . RRD_NL;

	/* rrdtool 1.2.x does not provide smooth lines, let's force it */
	if (read_config_option("rrdtool_version") == "rrd-1.2.x") {
		$graph_opts .= "--slope-mode" . RRD_NL;
	}

	/* rrdtool 1.2 font options */
	if (read_config_option("rrdtool_version") == "rrd-1.2.x") {
		/* title fonts */
		if (file_exists(read_graph_config_option("title_font"))) {
			$graph_opts .= "--font TITLE:" . read_graph_config_option("title_size") . ":" . read_graph_config_option("title_font") . RRD_NL;
        }elseif (file_exists(read_config_option("title_font"))) {
			$graph_opts .= "--font TITLE:" . read_config_option("title_size") . ":" . read_config_option("title_font") . RRD_NL;
		}

		/* axis fonts */
		if (file_exists(read_graph_config_option("axis_font"))) {
			$graph_opts .= "--font AXIS:" . read_graph_config_option("axis_size") . ":" . read_graph_config_option("axis_font") . RRD_NL;
        }elseif (file_exists(read_config_option("axis_font"))) {
			$graph_opts .= "--font AXIS:" . read_config_option("axis_size") . ":" . read_config_option("axis_font") . RRD_NL;
		}

		/* legend fonts */
		if (file_exists(read_graph_config_option("legend_font"))) {
			$graph_opts .= "--font LEGEND:" . read_graph_config_option("legend_size") . ":" . read_graph_config_option("legend_font") . RRD_NL;
        }elseif (file_exists(read_config_option("legend_font"))) {
			$graph_opts .= "--font LEGEND:" . read_config_option("legend_size") . ":" . read_config_option("legend_font") . RRD_NL;
        }

		/* unit fonts */
		if (file_exists(read_graph_config_option("unit_font"))) {
			$graph_opts .= "--font UNIT:" . read_graph_config_option("unit_size") . ":" . read_graph_config_option("unit_font") . RRD_NL;
        }elseif (file_exists(read_config_option("unit_font"))) {
			$graph_opts .= "--font UNIT:" . read_config_option("unit_size") . ":" . read_config_option("unit_font") . RRD_NL;
        }
	}

	$i = 0;
	if (sizeof($graph_items > 0)) {
		foreach ($graph_items as $graph_item) {
			/* mimic the old behavior: LINE[123], AREA, and STACK items use the CF specified in the graph item */
			if (is_graph_item_type_primary($graph_item["graph_item_type"])) {
				$graph_cf = $graph_item["consolidation_function"];
			/* all other types are based on the AVERAGE CF */
			}else{
				$graph_cf = 1;
			}

			if ((!empty($graph_item["data_source_id"])) && (!isset($cf_ds_cache{$graph_item["data_source_item_id"]}[$graph_cf]))) {
				/* use a user-specified ds path if one is entered */
				$data_source_path = api_data_source_path_get($graph_item["data_source_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_cf] . RRD_NL;

					$cf_ds_cache{$graph_item["data_source_item_id"]}[$graph_cf] = "$i";

					$i++;
				}
			}

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

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

			/* the following fields will be searched for graph variables */
			$variable_fields = array(
				"legend_format" => array(
					"process_no_legend" => false
					),
				"legend_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_data_query_variables($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_data_query_variables($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|total_peak|all_max_current|all_max_peak|aggregate_max|aggregate_sum|aggregate)(:(\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[1], $match[2], $match[3], (isset($match[5]) ? $match[5] : 2), $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[1], $match[2], $match[3], $match[4], $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 (empty($graph_item["hard_return"])) {
					$hardreturn[$graph_item_id] = "";
				}else{
					$hardreturn[$graph_item_id] = "\\n";
				}

				/* +++++++++++++++++++++++ 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 (!empty($graph["auto_padding"])) {
					/* only applies to AREA and STACK */
					if (is_graph_item_type_primary($graph_item["graph_item_type"])) {
						$text_format_lengths{$graph_item["data_source_item_id"]} = strlen($graph_variables["legend_format"][$graph_item_id]);

						if ((strlen($graph_variables["legend_format"][$graph_item_id]) > $greatest_text_format) && ($graph_item_types{$graph_item["graph_item_type"]} != GRAPH_ITEM_TYPE_COMMENT)) {
							$greatest_text_format = strlen($graph_variables["legend_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_source_item_id"]}{$graph_item["consolidation_function"]})) {
			$cf_id = $graph_item["consolidation_function"];
		}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_source_item_id"]}[1])) {
				$cf_id = 1; /* CF: AVERAGE */
			}elseif (isset($cf_ds_cache{$graph_item["data_source_item_id"]}[3])) {
				$cf_id = 3; /* CF: MAX */
			}elseif (isset($cf_ds_cache{$graph_item["data_source_item_id"]}[2])) {
				$cf_id = 2; /* CF: MIN */
			}elseif (isset($cf_ds_cache{$graph_item["data_source_item_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"])) && (!isset($cdef_cache{$graph_item["cdef"]}{$graph_item["data_source_item_id"]}[$cf_id]))) {
			$cdef_string = $graph_item["cdef"];

			/* create cdef string for "total all data sources" if requested */
			if ((strstr($cdef_string, "ALL_DATA_SOURCES_DUPS")) || (strstr($cdef_string, "ALL_DATA_SOURCES_NODUPS"))) {
				$item_count = 0;
				for ($t=0;($t<count($graph_items));$t++) {
					if ((is_graph_item_type_primary($graph_items[$t]["graph_item_type"])) && (!empty($graph_items[$t]["data_source_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_source_item_id"]}[$cf_id])) {
							$def_name = generate_graph_def_name(strval($cf_ds_cache{$graph_items[$t]["data_source_item_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 ((strpos($cdef_string, "SIMILAR_DATA_SOURCES_DUPS")) || (strpos($cdef_string, "SIMILAR_DATA_SOURCES_NODUPS"))) {
				$sources_seen = array();
				$item_count = 0;

				for ($t=0;($t<count($graph_items));$t++) {
					if ((is_graph_item_type_primary($graph_items[$t]["graph_item_type"])) && (!empty($graph_items[$t]["data_source_item_id"])) && ($graph_item["data_source_name"] == $graph_items[$t]["data_source_name"])) {
						/* if the user screws up CF settings, PHP will generate warnings if left unchecked */
						if (isset($cf_ds_cache{$graph_items[$t]["data_source_item_id"]}[$cf_id]) && (!isset($sources_seen{$graph_items[$t]["data_source_item_id"]}))) {
							$def_name = generate_graph_def_name(strval($cf_ds_cache{$graph_items[$t]["data_source_item_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_source_item_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_source_item_id"]}[$cf_id]) ? $cf_ds_cache{$graph_item["data_source_item_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"]}{$graph_item["data_source_item_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["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"])) && (!empty($graph["auto_padding"])) && (isset($text_format_lengths{$graph_item["data_source_item_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_source_item_id"]});
			$text_padding = str_pad("", $pad_number);

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

			$last_graph_type = $graph_item["graph_item_type"];
		}

		/* 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["graph_item_type"];

		/* 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"] == "") {
			if (isset($cf_ds_cache{$graph_item["data_source_item_id"]}[$cf_id])) {
				$data_source_name = generate_graph_def_name(strval($cf_ds_cache{$graph_item["data_source_item_id"]}[$cf_id]));
			}else{
				$data_source_name = "";
			}
		}else{
			$data_source_name = "cdef" . generate_graph_def_name(strval($cdef_cache{$graph_item["cdef"]}{$graph_item["data_source_item_id"]}[$cf_id]));
		}

		/* to make things easier... if there is no text format set; set blank text */
		if (!isset($graph_variables["legend_format"][$graph_item_id])) {
			$graph_variables["legend_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 (is_graph_item_type_primary($graph_item["graph_item_type"])) {
			$graph_variables["legend_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["legend_format"][$graph_item_id]); /* escape colons */
			$txt_graph_items .= $graph_item_types{$graph_item["graph_item_type"]} . ":" . $data_source_name . "#" . $graph_item["color"] . ":" . "\"" . $graph_variables["legend_format"][$graph_item_id] . $hardreturn[$graph_item_id] . "\" ";
		}elseif ($graph_item["graph_item_type"] == GRAPH_ITEM_TYPE_COMMENT) {
			$txt_graph_items .= $graph_item_types{$graph_item["graph_item_type"]} . ":\"" . $graph_variables["legend_format"][$graph_item_id] . $hardreturn[$graph_item_id] . "\" ";
		}elseif (($graph_item["graph_item_type"] == GRAPH_ITEM_TYPE_GPRINT) && (!isset($graph_data_array["graph_nolegend"]))) {
			$graph_variables["legend_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["legend_format"][$graph_item_id]); /* escape colons */
			$txt_graph_items .= $graph_item_types{$graph_item["graph_item_type"]} . ":" . $data_source_name . ":" . $consolidation_functions{$graph_item["consolidation_function"]} . ":\"$text_padding" . $graph_variables["legend_format"][$graph_item_id] . $graph_item["gprint_format"] . $hardreturn[$graph_item_id] . "\" ";
		}elseif ($graph_item["graph_item_type"] == GRAPH_ITEM_TYPE_HRULE) {
			$graph_variables["legend_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["legend_format"][$graph_item_id]); /* escape colons */
			$graph_variables["legend_value"][$graph_item_id] = str_replace(":", "\:", $graph_variables["legend_value"][$graph_item_id]); /* escape colons */
			$txt_graph_items .= $graph_item_types{$graph_item["graph_item_type"]} . ":" . $graph_variables["legend_value"][$graph_item_id] . "#" . $graph_item["color"] . ":\"" . $graph_variables["legend_format"][$graph_item_id] . $hardreturn[$graph_item_id] . "\" ";
		}elseif ($graph_item["graph_item_type"] == GRAPH_ITEM_TYPE_VRULE) {
			$graph_variables["legend_format"][$graph_item_id] = str_replace(":", "\:", $graph_variables["legend_format"][$graph_item_id]); /* escape colons */

			$value_array = explode(":", $graph_item["legend_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_item_type"]} . ":" . $value . "#" . $graph_item["color"] . ":\"" . $graph_variables["legend_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, FACIL_POLLER);
			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,$syslog_facility);
		}
	}
}