EOT;
	if (isset($CONFIG['page_refresh']) && is_numeric($CONFIG['page_refresh'])) {
		echo <<
EOT;
	}
	if ($CONFIG['graph_type'] == 'canvas') {
		echo <<
	
	
	
	
	
	
	
	
	
EOT;
	}
echo <<
EOT;
}
function html_end() {
	global $CONFIG;
	$git = '/usr/bin/git';
	$changelog = $CONFIG['webdir'].'/doc/CHANGELOG';
	$version = 'v?';
	if (file_exists($git) && is_dir($CONFIG['webdir'].'/.git')) {
		chdir($CONFIG['webdir']);
		$version = exec($git.' describe --tags');
	} elseif (file_exists($changelog)) {
		$changelog = file($changelog);
		$version = explode(' ', $changelog[0]);
		$version = 'v'.$version[0];
	}
	echo <<
EOT;
	if ($CONFIG['graph_type'] == 'canvas') {
		echo <<
EOT;
		if ($CONFIG['rrd_fetch_method'] == 'async') {
		echo <<
EOT;
		} else {
		echo <<
EOT;
		}
	}
echo <<
EOT;
}
function plugin_header($host, $plugin) {
	global $CONFIG;
	return printf("\n", $CONFIG['weburl'], $host, $plugin, $plugin);
}
function plugins_list($host, $selected_plugins = array()) {
	global $CONFIG;
	$plugins = collectd_plugins($host);
	echo '';
	echo '
Plugins ';
	echo '
';
	printf("overview  \n",
		selected_overview($selected_plugins),
		$CONFIG['weburl'],
		$host
	);
	# first the ones defined as ordered
	foreach($CONFIG['overview'] as $plugin) {
		if (in_array($plugin, $plugins)) {
			printf("%4\$s  \n",
				selected_plugin($plugin, $selected_plugins),
				$CONFIG['weburl'],
				$host,
				$plugin
			);
		}
	}
	# other plugins
	foreach($plugins as $plugin) {
		if (!in_array($plugin, $CONFIG['overview'])) {
			printf("%4\$s  \n",
				selected_plugin($plugin, $selected_plugins),
				$CONFIG['weburl'],
				$host,
				$plugin
			);
		}
	}
	echo ' ';
	echo '
 ';
}
function selected_overview($selected_plugins) {
	if (count($selected_plugins) > 1) {
		return 'class="selected"';
	}
	return '';
}
function selected_plugin($plugin, $selected_plugins) {
	if (in_array($plugin, $selected_plugins)) {
		return 'class="selected"';
	}
	return '';
}
function selected_timerange($value1, $value2) {
	if ($value1 == $value2) {
		return 'class="selected"';
	}
	return '';
}
function host_summary($cat, $hosts) {
	global $CONFIG;
	$rrd = new RRDTool($CONFIG['rrdtool']);
	printf('', $cat);
	printf('%s ', $cat);
	echo "\n";
	$row_style = array(0 => "even", 1 => "odd");
	$host_counter = 0;
	foreach($hosts as $host) {
		$host_counter++;
		printf('', $row_style[$host_counter % 2]);
		printf('%s  ',
			$CONFIG['weburl'],$host, $host);
		if ($CONFIG['showload']) {
			require_once 'type/Default.class.php';
			$load = array('h' => $host, 'p' => 'load', 't' => 'load');
			$obj = new Type_Default($CONFIG, $load);
			$obj->collectd_flush();
			$rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd');
			# ignore if file does not exist
			if (!$rrd_info)
				continue;
			if (isset($rrd_info['ds[shortterm].last_ds']) &&
				isset($rrd_info['ds[midterm].last_ds']) &&
				isset($rrd_info['ds[longterm].last_ds'])) {
				$cores = count(group_plugindata(collectd_plugindata($host, 'cpu')));
				foreach (array('ds[shortterm].last_ds', 'ds[midterm].last_ds', 'ds[longterm].last_ds') as $info) {
					$class = '';
					if ($cores > 0 && $rrd_info[$info] > $cores * 2)
						$class = ' class="crit"';
					elseif ($cores > 0 && $rrd_info[$info] > $cores)
						$class = ' class="warn"';
					printf('%.2f ', $class, $rrd_info[$info]);
				}
			}
		}
		if ($CONFIG['showmem']) {
			$rrd_info_mu = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/memory/memory-used.rrd');
			$rrd_info_mf = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/memory/memory-free.rrd');
			$rrd_info_bf = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/memory/memory-buffered.rrd');
			$rrd_info_ca = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/memory/memory-cached.rrd');
			# ignore if file does not exist
			if (!$rrd_info_mu || !$rrd_info_mf || !$rrd_info_bf || !$rrd_info_ca)
				continue;
			$info='ds[value].last_ds';
			if (isset($rrd_info_mu[$info]) && isset($rrd_info_mf[$info]) && isset($rrd_info_bf[$info]) && isset($rrd_info_ca[$info]) ) {
				$percent_mem =	$rrd_info_mu[$info] * 100 / ($rrd_info_mu[$info] + $rrd_info_mf[$info] + $rrd_info_bf[$info] + $rrd_info_ca[$info]);
				$class = '';
				if ($percent_mem > 90)
					$class = ' class="crit"';
				elseif ($percent_mem > 70)
					$class = ' class="warn"';
				printf('%d%% ', $class, $percent_mem);
			}
		}
		print " \n";
	}
	echo "
\n";
	echo " \n";
}
function breadcrumbs() {
	$path = '';
	if (validate_get(GET('h'), 'host'))
		$path .= ' - '.ucfirst(GET('h'));
	if (validate_get(GET('p'), 'plugin'))
		$path .= ' - '.ucfirst(GET('p'));
	if (validate_get(GET('pi'), 'pinstance'))
		$path .= ' - '.GET('pi');
	if (validate_get(GET('t'), 'type') && validate_get(GET('p'), 'plugin') && GET('t') != GET('p'))
		$path .= ' - '.GET('t');
	if (validate_get(GET('ti'), 'tinstance'))
		$path .= ' - '.GET('ti');
	return $path;
}
# generate graph url's for a plugin of a host
function graphs_from_plugin($host, $plugin, $overview=false) {
	global $CONFIG;
	if (!$plugindata = collectd_plugindata($host, $plugin))
		return false;
	if (!$plugindata = group_plugindata($plugindata))
		return false;
	if (!$plugindata = plugin_sort($plugindata))
		return false;
	foreach ($plugindata as $items) {
		if (
			$overview && isset($CONFIG['overview_filter'][$plugin]) &&
			$CONFIG['overview_filter'][$plugin] !== array_intersect_assoc($CONFIG['overview_filter'][$plugin], $items)
		) {
			continue;
		}
		$items['h'] = $host;
		$time = array_key_exists($plugin, $CONFIG['time_range'])
			? $CONFIG['time_range'][$plugin]
			: $CONFIG['time_range']['default'];
		if ($CONFIG['graph_type'] == 'canvas') {
			chdir($CONFIG['webdir']);
			isset($items['p']) ? $_GET['p'] = $items['p'] : $_GET['p'] = '';
			isset($items['pi']) ? $_GET['pi'] = $items['pi'] : $_GET['pi'] = '';
			isset($items['t']) ? $_GET['t'] = $items['t'] : $_GET['t'] = '';
			isset($items['ti']) ? $_GET['ti'] = $items['ti'] : $_GET['ti'] = '';
			include $CONFIG['webdir'].'/graph.php';
		} else {
			printf(' '."\n",
				$CONFIG['weburl'],
				build_url('detail.php', $items, $time),
				$CONFIG['weburl'],
				build_url('graph.php', $items, $time)
			);
		}
	}
}
# generate an url with GET values from $items
function build_url($base, $items, $s=NULL) {
	global $CONFIG;
	if (!is_array($items))
		return false;
	if (!is_numeric($s))
		$s = $CONFIG['time_range']['default'];
	$i=0;
	foreach ($items as $key => $value) {
		# don't include empty values
		if ($value == 'NULL')
			continue;
		$base .= sprintf('%s%s=%s', $i==0 ? '?' : '&', $key, $value);
		$i++;
	}
	if (!isset($items['s']))
		$base .= '&s='.$s;
	return $base;
}