/** * Draw RRD file based on it's structure * @host * @plugin * @pinst * @type * @tinst * @opts * @return Commandline to call RRDGraph in order to generate the final graph */ function collectd_draw_rrd($host, $plugin, $pinst = null, $type, $tinst = null, $opts = array()) { 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['rrd_opts'])) { $opts['rrd_opts'] = array(); } if (isset($opts['logarithmic']) && $opts['logarithmic']) { array_unshift($opts['rrd_opts'], '-o'); } $rrdinfo = null; $rrdfile = sprintf('%s/%s%s%s/%s%s%s', $host, $plugin, is_null($pinst) ? '' : '-', $pinst, $type, is_null($tinst) ? '' : '-', $tinst); foreach ($config['datadirs'] as $datadir) { if (is_file($datadir . '/' . $rrdfile . '.rrd')) { $rrdinfo = rrdtool_file_info($datadir . '/' . $rrdfile . '.rrd'); if (isset($rrdinfo['RRA']) && is_array($rrdinfo['RRA'])) { break; } else { $rrdinfo = null; } } } if (is_null($rrdinfo)) { return false; } $graph = array(); $has_avg = false; $has_max = false; $has_min = false; reset($rrdinfo['RRA']); $l_max = 0; while (list($k, $v) = each($rrdinfo['RRA'])) { if ($v['cf'] == 'MAX') { $has_max = true; } else { if ($v['cf'] == 'AVERAGE') { $has_avg = true; } else { if ($v['cf'] == 'MIN') { $has_min = true; } } } } // Build legend. This may not work for all RRDs, i don't know :) if ($has_avg) { $graph[] = "COMMENT: Last"; } if ($has_min) { $graph[] = "COMMENT: Min"; } if ($has_max) { $graph[] = "COMMENT: Max"; } if ($has_avg) { $graph[] = "COMMENT: Avg\\n"; } reset($rrdinfo['DS']); while (list($k, $v) = each($rrdinfo['DS'])) { if (strlen($k) > $l_max) { $l_max = strlen($k); } if ($has_min) { $graph[] = sprintf('DEF:%s_min=%s:%s:MIN', $k, $rrdinfo['filename'], $k); } if ($has_avg) { $graph[] = sprintf('DEF:%s_avg=%s:%s:AVERAGE', $k, $rrdinfo['filename'], $k); } if ($has_max) { $graph[] = sprintf('DEF:%s_max=%s:%s:MAX', $k, $rrdinfo['filename'], $k); } } if ($has_min && $has_max || $has_min && $has_avg || $has_avg && $has_max) { $n = 1; reset($rrdinfo['DS']); while (list($k, $v) = each($rrdinfo['DS'])) { $graph[] = sprintf('LINE:%s_%s', $k, $has_min ? 'min' : 'avg'); $graph[] = sprintf('CDEF:%s_var=%s_%s,%s_%s,-', $k, $k, $has_max ? 'max' : 'avg', $k, $has_min ? 'min' : 'avg'); $graph[] = sprintf('AREA:%s_var#%s::STACK', $k, rrd_get_color($n++, false)); } } reset($rrdinfo['DS']); $n = 1; while (list($k, $v) = each($rrdinfo['DS'])) { $graph[] = sprintf('LINE1:%s_avg#%s:%s ', $k, rrd_get_color($n++, true), $k . substr(' ', 0, $l_max - strlen($k))); if (isset($opts['tinylegend']) && $opts['tinylegend']) { continue; } if ($has_avg) { $graph[] = sprintf('GPRINT:%s_avg:AVERAGE:%%5.1lf%%s', $k, $has_max || $has_min || $has_avg ? ',' : "\\l"); } if ($has_min) { $graph[] = sprintf('GPRINT:%s_min:MIN:%%5.1lf%%s', $k, $has_max || $has_avg ? ',' : "\\l"); } if ($has_max) { $graph[] = sprintf('GPRINT:%s_max:MAX:%%5.1lf%%s', $k, $has_avg ? ',' : "\\l"); } if ($has_avg) { $graph[] = sprintf('GPRINT:%s_avg:LAST:%%5.1lf%%s\\l', $k); } } #$rrd_cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-t', $rrdfile); #$rrd_cmd = array(RRDTOOL, 'graph', '-', '-E', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height']); $rrd_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"); $rrd_cmd = array_merge($rrd_cmd, $small_opts); } $rrd_cmd = array_merge($rrd_cmd, $config['rrd_opts_array'], $opts['rrd_opts'], $graph); $cmd = RRDTOOL; $cmd = ''; for ($i = 1; $i < count($rrd_cmd); $i++) { $cmd .= ' ' . escapeshellarg($rrd_cmd[$i]); } return $cmd; }
/** * Draw RRD file based on it's structure * @host * @plugin * @pinst * @type * @tinst * @opts * @return Commandline to call RRDGraph in order to generate the final graph */ function collectd_draw_rrd($host, $plugin, $pinst = null, $type, $tinst = null, $opts = array()) { 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['rrd_opts'])) $opts['rrd_opts'] = array(); if (isset($opts['logarithmic']) && $opts['logarithmic']) array_unshift($opts['rrd_opts'], '-o'); $rrdinfo = null; $rrdfile = sprintf('%s/%s%s%s/%s%s%s', $host, $plugin, is_null($pinst) ? '' : '-', $pinst, $type, is_null($tinst) ? '' : '-', $tinst); foreach ($config['datadirs'] as $datadir) if (is_file($datadir.'/'.$rrdfile.'.rrd')) { $rrdinfo = rrd_info($datadir.'/'.$rrdfile.'.rrd'); if (isset($rrdinfo['RRA']) && is_array($rrdinfo['RRA'])) break; else $rrdinfo = null; } if (is_null($rrdinfo)) return false; $graph = array(); $has_avg = false; $has_max = false; $has_min = false; reset($rrdinfo['RRA']); $l_max = 0; while (list($k, $v) = each($rrdinfo['RRA'])) { if ($v['cf'] == 'MAX') $has_max = true; else if ($v['cf'] == 'AVERAGE') $has_avg = true; else if ($v['cf'] == 'MIN') $has_min = true; } reset($rrdinfo['DS']); while (list($k, $v) = each($rrdinfo['DS'])) { if (strlen($k) > $l_max) $l_max = strlen($k); if ($has_min) $graph[] = sprintf('DEF:%s_min=%s:%s:MIN', $k, rrd_escape($rrdinfo['filename']), $k); if ($has_avg) $graph[] = sprintf('DEF:%s_avg=%s:%s:AVERAGE', $k, rrd_escape($rrdinfo['filename']), $k); if ($has_max) $graph[] = sprintf('DEF:%s_max=%s:%s:MAX', $k, rrd_escape($rrdinfo['filename']), $k); } if ($has_min && $has_max || $has_min && $has_avg || $has_avg && $has_max) { $n = 1; reset($rrdinfo['DS']); while (list($k, $v) = each($rrdinfo['DS'])) { $graph[] = sprintf('LINE:%s_%s', $k, $has_min ? 'min' : 'avg'); $graph[] = sprintf('CDEF:%s_var=%s_%s,%s_%s,-', $k, $k, $has_max ? 'max' : 'avg', $k, $has_min ? 'min' : 'avg'); $graph[] = sprintf('AREA:%s_var#%s::STACK', $k, rrd_get_color($n++, false)); } } reset($rrdinfo['DS']); $n = 1; while (list($k, $v) = each($rrdinfo['DS'])) { $graph[] = sprintf('LINE1:%s_avg#%s:%s ', $k, rrd_get_color($n++, true), $k.substr(' ', 0, $l_max-strlen($k))); if (isset($opts['tinylegend']) && $opts['tinylegend']) continue; if ($has_avg) $graph[] = sprintf('GPRINT:%s_avg:AVERAGE:%%5.1lf%%s Avg%s', $k, $has_max || $has_min || $has_avg ? ',' : "\\l"); if ($has_min) $graph[] = sprintf('GPRINT:%s_min:MIN:%%5.1lf%%s Max%s', $k, $has_max || $has_avg ? ',' : "\\l"); if ($has_max) $graph[] = sprintf('GPRINT:%s_max:MAX:%%5.1lf%%s Max%s', $k, $has_avg ? ',' : "\\l"); if ($has_avg) $graph[] = sprintf('GPRINT:%s_avg:LAST:%%5.1lf%%s Last\\l', $k); } $rrd_cmd = array(RRDTOOL, 'graph', '-', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-s', -1*$timespan_def['seconds'], '-t', $rrdfile); $rrd_cmd = array_merge($rrd_cmd, $config['rrd_opts'], $opts['rrd_opts'], $graph); $cmd = RRDTOOL; for ($i = 1; $i < count($rrd_cmd); $i++) $cmd .= ' '.escapeshellarg($rrd_cmd[$i]); return $cmd; }