Make min/max/avg graphs configurable
authorBenjamin Oechslein <benjamin.oechslein@informatik.uni-erlangen.de>
Fri, 9 Jan 2015 20:51:15 +0000 (21:51 +0100)
committerBenjamin Oechslein <benjamin.oechslein@informatik.uni-erlangen.de>
Fri, 9 Jan 2015 21:36:16 +0000 (22:36 +0100)
upsgraph.pl

index c8bc4c6577990bdef651b13e4f47235fc4a15022..4f660902158c975715a696fec3bf325ead762615 100755 (executable)
@@ -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\\: <span foreground="#FF0000">%.2lf</span>\\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 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\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 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\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";
Impressum, Datenschutz