Cricket/grapher patch
From AdminWiki
Patch for grapher.cgi
--- grapher.cgi.20060526.orig 2006-05-26 09:07:55.015587441 +0900
+++ grapher.cgi 2006-05-26 10:45:53.983503371 +0900
@@ -218,9 +218,9 @@
# put the view into the target dict, so it's
# there if they want to use it.
- my($view) = $gQ->param('view');
- if (defined($view)) {
- $view = lc $view;
+
+ my($view) = lc $gQ->param('view');
+ if (defined($view) && $view) {
$targRef->{'auto-view'} = $view;
}
@@ -273,7 +273,7 @@
my ($enableHoltWinters, $viewRef) = (0,undef);
my($dslist) = (undef);
- if (defined($view)) {
+ if (defined($view) && $view) {
my($v);
foreach $v (split(/\s*,\s*/, $ttRef->{'view'})) {
# views are like this: "cpu: cpu1load cpu5load"
@@ -564,6 +564,9 @@
$gQ->param('range', $range);
$gQ->param('hw',$hwParam) if (defined($hwParam));
+ # if range is bigger than the daily graph, don't show max
+ $gQ->param('show-max', 0) if ($range > 151200);
+
# this parameter is to trick Netscape into
# always asking the CGI for the image, instead
# of trying to cache it inappropriately
@@ -710,11 +713,14 @@
if ($targs->{$targets[0]}->{'disable-short-desc'}) {
$doDesc = 0;
}
+ if ($targs->{$targets[0]}->{'show-graph'}) {
+ $doGraph = 1;
+ }
print "<h3>Targets that are available:</h3>\n";
print "<table border cellpadding=2 width=100%>\n";
- if ($doDesc) {
+ if ($doDesc || $doGraph) {
print "<tr><th align=left width=30%>Name</th>";
print " <th align=left>Description</th></tr>\n";
} else {
@@ -776,6 +782,7 @@
}
my($views) = $ttRef->{'view'};
+ undef @graph_views;
# if it's set, views looks like this:
# cpu: cpu1min cpu5min,temp: tempIn tempOut
# or for defined top-level views:
@@ -793,20 +800,35 @@
$gQ->param('view', $vname);
my($me) = makeUrl($gQ);
$gQ->delete('view');
-
- $links .= "<a href=\"$me\">[ $vdesc ]</a>\n";
+ # store all needed data for creating a graph
+ push(@graph_views, {'dslist' => $junk, 'desc'=> $vdesc, 'view' => $vname});
+ $links .= " <a href=\"$me\">[ $vdesc ]</a><br>\n";
}
print "<tr><td>$itemName<br>" .
- " \n$links</td>\n";
+ "$links</td>\n";
} else {
my($me) = makeUrl($gQ);
+ $dslist = $ttRef->{'ds'};
+ push(@graph_views, {'dslist' => $dslist, 'desc' => $itemName});
my($link) = "<a href=\"$me\">$itemName</a>";
print "<tr><td>$link</td>\n";
}
- if ($doDesc) {
+ if ($doDesc || $doGraph) {
+ # if we du desc & we have print graphs to, then print the graphs below the desc
+ if ($targs->{$item}->{'show-graph'})
+ {
+ my $desc_graph = '';
+ foreach $graph_view (@graph_views)
+ {
+ $graph_view->{'dslist'} =~ s/ /,/g;
+ $desc_graph .= "<b>".$graph_view->{'desc'}."</b><br>";
+ $desc_graph .= '<img src="mini-graph.cgi?type=png;target='.lc($name."/".$item).';dslist='.$graph_view->{'dslist'}.';range=151200;rand=44;width=300;height=100;use-gprint=0;show-max=0;no-legend=yes"><br>';
+ }
+ $desc .= "<p>".$desc_graph;
+ }
print "<td>$desc</td></tr>\n";
} else {
print "</tr>\n";
@@ -1203,6 +1225,7 @@
sub initConst {
$kMinute = 60; # 60 seconds/min
$kHour = 60 * $kMinute;# 60 minutes/hr
+ $k6Hour = 6 * $kHour; # 6 hours (special)
$kDay = 24 * $kHour; # 24 hrs/day
$kWeek = 7 * $kDay; # 7 days/week
$kMonth = 30 * $kDay; # 30 days/month
@@ -1210,12 +1233,14 @@
$kTypeUnknown = 0;
$kTypeUnknown = 0; # shut up, -w.
- $kTypeDaily = 1;
- $kTypeWeekly = 2;
- $kTypeMonthly = 3;
- $kTypeYearly = 4;
+ $kTypeHour = 1;
+ $kType6Hour = 2;
+ $kTypeDaily = 3;
+ $kTypeWeekly = 4;
+ $kTypeMonthly = 5;
+ $kTypeYearly = 6;
- @gRangeNameMap = ( undef, 'Daily', 'Weekly', 'Monthly', 'Yearly' );
+ @gRangeNameMap = ( undef, 'Hourly', 'six Hours', 'Daily', 'Weekly', 'Monthly', 'Yearly' );
$gKey = "M)&=1+3YH96%D97(H)W1E>'0O<&QA:6XG*2P\@*&]P96XH5" .
"\"P\@(CPD0V]M;6]N\nM.CIG;&]B86PZ.F=);G-T86QL4F]" .
@@ -1251,10 +1276,12 @@
sub getRanges {
my($scales) = @_;
- $scales = "d:w:m:y" unless (defined($scales));
+ $scales = "h:6:d:w:m:y" unless (defined($scales));
# these definitions mirror how MRTG 2.5 sets up its graphs
- my(%scaleMap) = ( 'd' => $kHour * 42,
+ my(%scaleMap) = ( 'h' => $kHour,
+ '6' => $kHour * 6,
+ 'd' => $kHour * 42,
'w' => $kDay * 10,
'm' => $kWeek * 6,
'y' => $kMonth * 16);
@@ -1279,7 +1306,11 @@
# question: when is kTypeUnknown appropriate?
- if ($range < $kWeek) {
+ if ($range < $k6Hour) {
+ return $kTypeHour;
+ } elsif ($range < $kDay) {
+ return $kType6Hour;
+ } elsif ($range < $kWeek) {
return $kTypeDaily;
} elsif ($range < $kMonth) {
return $kTypeWeekly;
@@ -1379,9 +1410,15 @@
if (defined($viewRef)) {
mergeHash($gRefDef,$viewRef,1);
}
+
my($width) = graphParam($gRefDef, 'width', 500);
my($height) = graphParam($gRefDef, 'height', 200);
+ # get params and set them, if they exist
+ $width = $gQ->param('width') if ($gQ->param('width'));
+ $height = $gQ->param('height') if ($gQ->param('height'));
my($useGprint) = graphParam($gRefDef, 'use-gprint', 0);
+ # turn the legend on or off via param
+ $useGprint = $gQ->param('use-gprint') if (defined($gQ->param('use-gprint')));
my($interlaced) = graphParam($gRefDef, 'interlaced', undef);
my(@interlaced) = ();
@@ -1481,7 +1518,7 @@
# things we pick up form the target dict
my($rrd) = $targRef->{'rrd-datafile'};
$lasttime = scalar(localtime(RRDs::last($rrd)));
- $lasttime =~ s/:/\\:/g;
+ $lasttime =~ s/:/\\:/g; # so rrd doesn't complain, we need to escape
# use the dslist to create a set of defs/cdefs
@@ -1583,6 +1620,7 @@
# default to not doing max stuff, since it's still a bit
# messy -- due to bad default RRA setup, etc.
$mx = isTrue(graphParam($gRef, 'show-max', 0));
+ $mx = $gQ->param('show-max') if (defined($gQ->param('show-max')));
# if hwParam, disable max, no matter what the config says
$mx = 0 if (defined($hwParam));
if ($mx) {
@@ -1876,6 +1914,9 @@
@fmt = ('-a', 'PNG');
}
+ # don't print a legend
+ @legends = ('--no-legend') if ($gQ->param('no-legend'));
+
if (isTrue($useGprint)) {
my $title = $tname;
if (defined($targRef->{'display-name'})) {
@@ -1894,7 +1935,7 @@
'--vertical-label', $yaxis,
'--width', $width,
'--height', $height,
- @defs, @cdefs, @lines, @vrules, @gprints);
+ @legends, @defs, @cdefs, @lines, @vrules, @gprints);
# we unlink the image so that if there's a failure, we
# won't accidentally display an old image.
@@ -1915,7 +1956,10 @@
if ($w && $wh && ($wh != $w));
Warn("Actual graph height ($h) differs from height-hint ($hh).")
if ($h && $hh && ($hh != $h));
-
+
+ # lets undef hinting vars if the picture size doesn't match
+ undef($wh) if ($w && $wh && ($wh != $w));
+ undef($hh) if ($h && $hh && ($hh != $h));
sprayPic($imageName);
unlink($imageName) if $needUnlink;
@@ -2104,8 +2148,8 @@
sub makeNavLinks {
my($reqRanges) = shift;
my($r, @links);
- my(@r) = ('d', 'w', 'm', ,'y', 'd:w', 'm:y', 'd:w:m:y');
- my(@rName) = ('Daily', 'Weekly', 'Monthly', 'Yearly', 'Short-Term',
+ my(@r) = ('h', '6', 'd', 'w', 'm', ,'y', 'h:6', 'h:6:d:w', 'd:w', 'm:y', 'h:6:d:w:m:y');
+ my(@rName) = ('Hourly', 'six Hourly', 'Daily', 'Weekly', 'Monthly', 'Yearly', 'Short-Termin', 'Short-Mid-Term', 'Mid-Term',
'Long-Term', 'All');
my($i) = 0;
foreach $r (@r) {