]> git.zerfleddert.de Git - upsgraph/blobdiff - upsgraph.pl
Development changes: DBG output + don't fork away
[upsgraph] / upsgraph.pl
index c8bc4c6577990bdef651b13e4f47235fc4a15022..3f17d38409199cf128004ac4f26fe57606ee10ee 100755 (executable)
@@ -35,7 +35,7 @@ my $hosts = $UPSGRAPH::hosts;
 
 sub rrd_update(@) {
        my @args = @_;
-
+       print "Updating: ". Dumper(\@_);
        if ($use_rrds == 1) {
                RRDs::update(@args);
                $rrd_result = RRDs::error;
@@ -47,7 +47,7 @@ sub rrd_update(@) {
 sub rrd_graph(@) {
        my @args = @_;
        my @rrd_out = ();
-
+       print "Graphing: " . Dumper(\@_);
        if ($use_rrds == 1) {
                @rrd_out = RRDs::graph(@args);
                $rrd_result = RRDs::error;
@@ -93,6 +93,7 @@ sub rrdcreate(@) {
        RRDs::create(@cmd);
        if (RRDs::error) {
                print "Error while creating: " . RRDs::error . "\n";
+               print Dumper(\@cmd);
                exit 1;
        }
 }
@@ -223,6 +224,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";
@@ -398,11 +416,11 @@ foreach my $host (@$hosts) {
        }
 }
 
-my $child = fork();
-
-die "fork failed!" if (!defined($child));
-
-exit 0 if ($child != 0);
+#my $child = fork();
+#
+#die "fork failed!" if (!defined($child));
+#
+#exit 0 if ($child != 0);
 
 while(1) {
        open(HTML, ">${outdir}/index.html.new");
@@ -463,11 +481,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 +516,7 @@ while(1) {
 
                        pop @graphdef;
                        pop @graphdef;
+                       pop @graphdef;
 
                        if ($rrd_result) {
                                print "Error while graphing: " . $rrd_result . "\n";
@@ -499,19 +537,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 +567,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 +588,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