From 235b08abaa2e746a485090fd0760cdd1e85c09af Mon Sep 17 00:00:00 2001 From: Benjamin Oechslein Date: Fri, 9 Jan 2015 21:51:15 +0100 Subject: [PATCH] Make min/max/avg graphs configurable --- upsgraph.pl | 68 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/upsgraph.pl b/upsgraph.pl index c8bc4c6..4f66090 100755 --- a/upsgraph.pl +++ b/upsgraph.pl @@ -223,6 +223,23 @@ sub dayGraphFunc { return \@args; } +sub cfgToGraphDef { + my $cfg = shift; + my $varname = shift; + my $dpPerDay= shift; + my @graphDef = (); + foreach my $subGraph (@$cfg) { + if ($subGraph eq 'avg') { + push @graphDef, "LINE1:${varname}-avg#FF0000"; + } elsif ($subGraph eq 'day-min') { + push @graphDef, @{dayGraphFunc("${varname}-min", 'MIN', '0000ff', 'Day Minimum Temperature', $dpPerDay)}; + } elsif ($subGraph eq 'day-max') { + push @graphDef, @{dayGraphFunc("${varname}-max", 'MAX', '00ff00', 'Day Maximum Temperature', $dpPerDay)}; + } + } + return \@graphDef; +} + if ($> == 0) { if (@ARGV != 2) { print STDERR "Running as root, please provide UID as 2th argument!\n"; @@ -463,11 +480,30 @@ while(1) { } foreach my $var (@{$host->{'fields'}}) { - my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000"); - - push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN", "LINE1:${var}-min#0000FF"; - push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX", "LINE1:${var}-max#00FF00"; - push @graphdef, "VDEF:cur=${var},LAST"; + my $graphWidth = 365 * 3; + my $graphConfig; + if (defined $vars->{$var}->{'graph'}) { + $graphConfig = $vars->{$var}->{'graph'}; + foreach my $subGraph (qw(day week year)) { + if (!defined($graphConfig->{$subGraph})) { + $graphConfig->{$subGraph} = [ 'avg']; + } + } + } else { + $graphConfig = { + 'day' => [ 'avg' ], + 'week' => [ 'avg' ], + 'year' => [ 'avg' ], + }; + } + + my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}); + + push @graphdef, "DEF:${var}-avg=${rrdfile}.${var}:${var}:AVERAGE"; + push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN"; + push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX"; + push @graphdef, "LINE1:${var}-avg#FF0000"; + push @graphdef, "VDEF:cur=${var}-avg,LAST"; push @graphdef, 'GPRINT:cur:Current\\: %.2lf\\r'; my $mtime; @@ -479,6 +515,7 @@ while(1) { pop @graphdef; pop @graphdef; + pop @graphdef; if ($rrd_result) { print "Error while graphing: " . $rrd_result . "\n"; @@ -499,19 +536,22 @@ while(1) { push @graphdef, "VDEF:min=${var}-min,MINIMUM"; push @graphdef, "GPRINT:min:Minimum\\: %.2lf"; - push @graphdef, "VDEF:avg=${var},AVERAGE"; + push @graphdef, "VDEF:avg=${var}-avg,AVERAGE"; push @graphdef, "GPRINT:avg:Average\\: %.2lf"; push @graphdef, "VDEF:max=${var}-max,MAXIMUM"; push @graphdef, "GPRINT:max:Maximum\\: %.2lf"; - push @graphdef, "VDEF:cur=${var},LAST"; + push @graphdef, "VDEF:cur=${var}-avg,LAST"; push @graphdef, "GPRINT:cur:Current\\: %.2lf"; + my @dayGraphDef = @graphdef; + push @dayGraphDef, @{cfgToGraphDef($graphConfig->{'day'}, ${var}, 24)}; + $mtime=(stat("${outdir}/${hostname}.${var}.long.png.work"))[9]; ($averages, $width, $height) = rrd_graph("${outdir}/${hostname}.${var}.long.png.work", - "-w", "1008", @graphdef); + "-w", $graphWidth, @dayGraphDef); if ($rrd_result) { print "Error while graphing: " . $rrd_result . "\n"; @@ -526,9 +566,13 @@ while(1) { print HTML2 "
\n"; $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9]; + + my @weekGraphDef = @graphdef; + push @weekGraphDef, @{cfgToGraphDef($graphConfig->{'week'}, ${var}, 24)}; + ($averages, $width, $height) = rrd_graph("${outdir}/${hostname}.${var}.week.png.work", - "-w", "1008", "-e", "now", "-s", "end-1w", @graphdef); + "-w", "$graphWidth", "-e", "now", "-s", "00:00-8d", @weekGraphDef); if ($rrd_result) { print "Error while graphing: " . $rrd_result . "\n"; @@ -543,9 +587,13 @@ while(1) { print HTML2 "
\n"; $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9]; + + my @yearGraphDef = @graphdef; + push @yearGraphDef, @{cfgToGraphDef($graphConfig->{'year'}, ${var}, 3)}; + ($averages, $width, $height) = rrd_graph("${outdir}/${hostname}.${var}.year.png.work", - "-w", "1008", "-e", "now", "-s", "end-1y", @graphdef); + "-w", "$graphWidth", "-e", "00:00", "-s", "end-365d", @yearGraphDef); if ($rrd_result) { print "Error while graphing: " . $rrd_result . "\n"; -- 2.39.5