/**
 * Draw stack-graph for set of RRD files
 * @opts Graph options like colors
 * @sources List of array(name, file, ds)
 * @return Commandline to call RRDGraph in order to generate the final graph
 */
function collectd_draw_meta_line(&$opts, &$sources)
{
    global $config;
    $timespan_def = null;
    if (!isset($opts['timespan'])) {
        $timespan_def = reset($config['timespan']);
    } else {
        foreach ($config['timespan'] as &$ts) {
            if ($ts['name'] == $opts['timespan']) {
                $timespan_def = $ts;
            }
        }
    }
    if (!isset($opts['title'])) {
        $opts['title'] = '未知的标题';
    }
    if (!isset($opts['rrd_opts'])) {
        $opts['rrd_opts'] = array();
    }
    if (!isset($opts['colors'])) {
        $opts['colors'] = array();
    }
    if (isset($opts['logarithmic']) && $opts['logarithmic']) {
        array_unshift($opts['rrd_opts'], '-o');
    }
    #	$cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-t', $opts['title']);
    #	$cmd = array_merge($cmd, $config['rrd_opts_array'], $opts['rrd_opts']);
    $cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height']);
    $cmd = array('-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height']);
    if ($config['rrd_width'] <= "300") {
        $small_opts = array('--font', 'LEGEND:7:mono', '--font', 'AXIS:6:mono', '--font-render-mode', 'normal');
        $cmd = array_merge($cmd, $small_opts);
    }
    $max_inst_name = 0;
    foreach ($sources as &$inst_data) {
        $inst_name = $inst_data['name'];
        $file = $inst_data['file'];
        $ds = isset($inst_data['ds']) ? $inst_data['ds'] : 'value';
        if (strlen($inst_name) > $max_inst_name) {
            $max_inst_name = strlen($inst_name);
        }
        if (!is_file($file)) {
            continue;
        }
        $cmd[] = 'DEF:' . $inst_name . '_min=' . $file . ':' . $ds . ':MIN';
        $cmd[] = 'DEF:' . $inst_name . '_avg=' . $file . ':' . $ds . ':AVERAGE';
        $cmd[] = 'DEF:' . $inst_name . '_max=' . $file . ':' . $ds . ':MAX';
    }
    foreach ($sources as &$inst_data) {
        $inst_name = $inst_data['name'];
        $legend = sprintf('%s', $inst_name);
        while (strlen($legend) < $max_inst_name) {
            $legend .= ' ';
        }
        $number_format = isset($opts['number_format']) ? $opts['number_format'] : '%6.1lf';
        if (isset($opts['colors'][$inst_name])) {
            $line_color = new CollectdColor($opts['colors'][$inst_name]);
        } else {
            $line_color = new CollectdColor('random');
        }
        $cmd[] = 'LINE1:' . $inst_name . '_avg#' . $line_color->as_string() . ':' . $legend;
        if (!(isset($opts['tinylegend']) && $opts['tinylegend'])) {
            $cmd[] = 'GPRINT:' . $inst_name . '_min:MIN:' . $number_format . '';
            $cmd[] = 'GPRINT:' . $inst_name . '_avg:AVERAGE:' . $number_format . '';
            $cmd[] = 'GPRINT:' . $inst_name . '_max:MAX:' . $number_format . '';
            $cmd[] = 'GPRINT:' . $inst_name . '_avg:LAST:' . $number_format . '\\l';
        }
    }
    $rrdcmd = RRDTOOL;
    for ($i = 1; $i < count($cmd); $i++) {
        $rrdcmd .= ' ' . escapeshellarg($cmd[$i]);
    }
    return $rrdcmd;
}
/**
 * Draw stack-graph for set of RRD files
 * @opts Graph options like colors
 * @sources List of array(name, file, ds)
 * @return Commandline to call RRDGraph in order to generate the final graph
 */
function collectd_draw_meta_line(&$opts, &$sources) {
	global $config;
	$timespan_def = null;
	if (!isset($opts['timespan']))
		$timespan_def = reset($config['timespan']);
	else foreach ($config['timespan'] as &$ts)
		if ($ts['name'] == $opts['timespan'])
			$timespan_def = $ts;

	if (!isset($opts['title']))
		$opts['title'] = 'Unknown title';
	if (!isset($opts['rrd_opts']))
		$opts['rrd_opts'] = array();
	if (!isset($opts['colors']))
		$opts['colors'] = array();
	if (isset($opts['logarithmic']) && $opts['logarithmic'])
		array_unshift($opts['rrd_opts'], '-o');

	$cmd = array(RRDTOOL, 'graph', '-', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-s', -1*$timespan_def['seconds'], '-t', $opts['title']);
	$cmd = array_merge($cmd, $config['rrd_opts'], $opts['rrd_opts']);
	$max_inst_name = 0;

	foreach ($sources as &$inst_data) {
		$inst_name = str_replace('!', '_', $inst_data['name']);
		$file      = $inst_data['file'];
		$ds        = isset($inst_data['ds']) ? $inst_data['ds'] : 'value';

		if (strlen($inst_name) > $max_inst_name)
			$max_inst_name = strlen($inst_name);

		if (!is_file($file))
			continue;

		$cmd[] = 'DEF:'.$inst_name.'_min='.rrd_escape($file).':'.$ds.':MIN';
		$cmd[] = 'DEF:'.$inst_name.'_avg='.rrd_escape($file).':'.$ds.':AVERAGE';
		$cmd[] = 'DEF:'.$inst_name.'_max='.rrd_escape($file).':'.$ds.':MAX';
	}

	foreach ($sources as &$inst_data) {
		$inst_name = str_replace('!', '_', $inst_data['name']);
		$legend = sprintf('%s', $inst_name);
		while (strlen($legend) < $max_inst_name)
			$legend .= ' ';
		$number_format = isset($opts['number_format']) ? $opts['number_format'] : '%6.1lf';

		if (isset($opts['colors'][$inst_name]))
			$line_color = new CollectdColor($opts['colors'][$inst_name]);
		else
			$line_color = new CollectdColor('random');

		$cmd[] = 'LINE1:'.$inst_name.'_avg#'.$line_color->as_string().':'.$legend;
		if (!(isset($opts['tinylegend']) && $opts['tinylegend'])) {
			$cmd[] = 'GPRINT:'.$inst_name.'_min:MIN:'.$number_format.' Min,';
			$cmd[] = 'GPRINT:'.$inst_name.'_avg:AVERAGE:'.$number_format.' Avg,';
			$cmd[] = 'GPRINT:'.$inst_name.'_max:MAX:'.$number_format.' Max,';
			$cmd[] = 'GPRINT:'.$inst_name.'_avg:LAST:'.$number_format.' Last\\l';
		}
	}

	$rrdcmd = RRDTOOL;
	for ($i = 1; $i < count($cmd); $i++)
		$rrdcmd .= ' '.escapeshellarg($cmd[$i]);
	return $rrdcmd;
}