public static function getDSFromRRDFile($file) { $info = rrd_info($file); $suchmuster = "/(ds\\[)(.*[a-zA-Z0-9_])(\\])/"; $ds = array(); foreach ($info as $key => $value) { preg_match($suchmuster, $key, $treffer); if (count($treffer) > 1) { if (!in_array($treffer[2], $ds)) { $ds[] = $treffer[2]; } } } return $ds; }
/** * 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, $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 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(RRDTOOL, 'graph', '-', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-s', -1 * $timespan_def['seconds']); 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; for ($i = 1; $i < count($rrd_cmd); $i++) { $cmd .= ' ' . escapeshellarg($rrd_cmd[$i]); } return $cmd; }
function GetGroupInfo(LOGGROUP $grp = NULL, $flags = 0) { $groups = array(); //array for the groups to be returned if ($this->munin_datafile) { foreach ($this->groups as $gid => &$group) { if ($grp && strcmp($grp->gid, $gid)) { continue; } //if a group is specified and existing group has same group id then skip this group if ($group['name']) { $name = $group['name']; } else { $name = false; } //else name = false if (!$name || $flags & REQUEST::NEED_INFO) { //if there was no name and there's a NEED_INFO flag if ($grp) { //if group is specified $grtest = $grp; $opts = $this->opts; } else { //no group is specified (easier to just throw error here since there can't really be a default .rrd-file)? $ginfo = array("db_group" => $gid); //create new groupinfo array $grtest = $this->CreateGroup($ginfo); //create new group $opts = $this->req->GetGroupOptions($grtest); //create new options for group } if (!$name) { //if name = false $name = $opts->Get('name', $gid); //get name from options, if there's no name use group id } } $groups[$gid] = array('gid' => $gid, 'name' => $name); if ($flags & REQUEST::NEED_INFO) { $tmp_group = new LOGGROUP($tmp_groupid = array("db_group" => $gid)); $items = $this::GetItemList($tmp_group); //change to getitemlist $rrd_fetch_opts = array("AVERAGE"); $min = false; $max = false; foreach ($items as $item) { $rrd_file = $this::GidToFilename($item['db_uid'], $this); if ($min === false) { $min = rrd_first($rrd_file); $max = rrd_last($rrd_file); } else { $first = rrd_first($rrd_file); if ($min > $first) { $min = $first; } $last = rrd_last($rrd_file); if ($max < $last) { $max = $last; } } } $groups[$gid]['first'] = $min; $groups[$gid]['last'] = $max; if ($flags & REQUEST::NEED_COUNT) { //if there's a NEED_COUNT flag $fetch = rrd_info($file); $record_num = ($groups[$gid]['last'] - $groups[$gid]['first']) / $fetch['step']; //calculate number of records $groups[$gid]['records'] = $record_num; } if ($flags & REQUEST::NEED_ITEMINFO) { //if there's a NEED_ITEMINFO flag $groups[$gid]['items'] = $this->GetItemList($grp); //get itemlist for specified group } } } //went through existing groups } else { if (!$this->groups) { return false; } foreach ($this->groups as $gid => &$group) { if ($grp && strcmp($grp->gid, $gid)) { continue; } //if a group is specified and existing group has same group id then skip this group if ($group['name']) { $name = $group['name']; } else { $name = false; } //else name = false if (!$name || $flags & REQUEST::NEED_INFO) { //if there was no name and there's a NEED_INFO flag if ($grp) { //if group is specified $grtest = $grp; $opts = $this->opts; } else { //no group is specified (easier to just throw error here since there can't really be a default .rrd-file)? $ginfo = array("db_group" => $gid); //create new groupinfo array $grtest = $this->CreateGroup($ginfo); //create new group $opts = $this->req->GetGroupOptions($grtest); //create new options for group } if (!$name) { //if name = false $name = $opts->Get('name', $gid); //get name from options, if there's no name use group id } } $groups[$gid] = array('gid' => $gid, 'name' => $name); if ($flags & REQUEST::NEED_COUNT) { $rrd_fetch_opts = array("AVERAGE"); $fetch = $this::Fetch($gid, $rrd_fetch_opts); $record_num = ($fetch['end'] - $fetch['start']) / $fetch['step']; $groups[$gid]['records'] = $record_num; } if ($flags & REQUEST::NEED_INFO) { //if there's a NEED_INFO flag $rrd_fetch_opts = array("AVERAGE"); $fetch = $this::Fetch($gid, $rrd_fetch_opts); $groups[$gid]['first'] = $fetch['start']; $groups[$gid]['last'] = $fetch['end']; if ($flags & REQUEST::NEED_COUNT) { //if there's a NEED_COUNT flag $record_num = ($fetch['end'] - $fetch['start']) / $fetch['step']; //calculate number of records $groups[$gid]['records'] = $record_num; } if ($flags & REQUEST::NEED_ITEMINFO) { //if there's a NEED_ITEMINFO flag $groups[$gid]['items'] = $this->GetItemList($grtest); //get itemlist for specified group } } } //went through existing groups } if ($grp && !$groups) { //if group is specified and there are no defined groups throw new ADEIException(translate("Invalid group (%s) is specified", $grp->gid)); } return $grp ? $groups[$grp->gid] : $groups; //if group was specified, return group in groups table at index group id, else return groups table }