]> git.zerfleddert.de Git - upsgraph/blobdiff - upsgraph.pl
Make min/max/avg graphs configurable
[upsgraph] / upsgraph.pl
index 1e335acd1aa613b9a151140a4cf965b04a967203..4f660902158c975715a696fec3bf325ead762615 100755 (executable)
@@ -184,6 +184,62 @@ sub fetch_tcp_multi(@) {
        %values;
 }
 
+sub dayGraphFunc {
+       my $dataSrc = shift;
+       my $mode = shift;
+       my $color = shift;
+       my $label = shift;
+       my $dataPoints = shift;
+       my @args = ();
+       push @args, "CDEF:prev${mode}1=PREV(${dataSrc})";
+       for (my $i = 1; $i < $dataPoints - 1; ++$i) {
+               my $prev = $i;
+               my $next = $i+1;
+               push @args, "CDEF:prev${mode}${next}=PREV(prev${mode}${prev})";
+       }
+       my $dayCons = '';
+       my $consFunc = '';
+       for (my $i = 1; $i < $dataPoints; ++$i) {
+               $dayCons .= "prev${mode}${i},";
+               $consFunc .= ",${mode}";
+       }
+       push @args, "CDEF:day${mode}=${dayCons}${dataSrc}${consFunc}";
+       push @args, "CDEF:fillCalDay${mode}0=COUNT,${dataPoints},%,0,EQ,day${mode},UNKN,IF";
+       for (my $i = 1; $i < $dataPoints; ++$i) {
+               my $prev = $i-1;
+               my $next = $i;
+               push @args, "CDEF:fillCalDay${mode}${next}=PREV(fillCalDay${mode}${prev})";
+       }
+       my $fillPoint = '';
+       my $if = '';
+       for (my $i = 0; $i < $dataPoints; ++$i) {
+               $fillPoint .= "COUNT,${dataPoints},%,${i},EQ,fillCalDay${mode}${i},";
+               $if .= ",IF";
+       }
+       push @args, "CDEF:${mode}Curve=${fillPoint}UNKN${if}";
+       my $forwardShift = (24*60*60) * ($dataPoints - 1) / $dataPoints;
+       push @args, "SHIFT:${mode}Curve:-${forwardShift}";
+       push @args, "LINE1:${mode}Curve#${color}:${label}";
+       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";
@@ -424,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;
@@ -440,6 +515,7 @@ while(1) {
 
                        pop @graphdef;
                        pop @graphdef;
+                       pop @graphdef;
 
                        if ($rrd_result) {
                                print "Error while graphing: " . $rrd_result . "\n";
@@ -460,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";
@@ -487,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";
@@ -504,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