]> git.zerfleddert.de Git - upsgraph/commitdiff
each graph now has its own rrd file
authorMichael Gernoth <michael@gernoth.net>
Mon, 8 Feb 2010 11:38:19 +0000 (12:38 +0100)
committerMichael Gernoth <michael@gernoth.net>
Mon, 8 Feb 2010 11:38:19 +0000 (12:38 +0100)
upsgraph.pl

index 19d8147ab0395c20f1edddd9d90776c96fdce9fa..3d35c2a1059304bad881cfa1a5d7c01f070804da 100755 (executable)
@@ -30,20 +30,20 @@ my @fields = @UPSGRAPH::fields;
 my $vars = $UPSGRAPH::vars;
 
 sub rrdcreate(@) {
-       my $rrdfile = shift;
+       my $newrrd = shift;
+       my $field = shift;
        my $start = shift;
 
-       my @cmd = ("${rrdfile}", "--step=${step}");
+       my @cmd = ("${newrrd}", "--step=${step}");
 
        if (defined($start)) {
                push @cmd, "--start=${start}";
        }
 
-       foreach my $var (@fields) {
-               push @cmd, "DS:${var}:GAUGE:600:" .
-                       $vars->{$var}->{'min'} . ":" .
-                       $vars->{$var}->{'max'} . " ";
-       }
+       push @cmd, "DS:${field}:GAUGE:600:" .
+               $vars->{$field}->{'min'} . ":" .
+               $vars->{$field}->{'max'} . " ";
+
        push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
 
        RRDs::create(@cmd);
@@ -63,49 +63,45 @@ if ($> == 0) {
        $< = $> = $ARGV[1];
 }
 
-if (! -e "${rrdfile}") {
-       print "Creating ${rrdfile}...\n";
-       rrdcreate("${rrdfile}");
-}
-
-my $rrdinfo = RRDs::info("${rrdfile}");
-if (RRDs::error) {
-       print "Error while getting info: " . RRDs::error . "\n";
-       exit 1;
-}
+if (-e "${rrdfile}") {
+       print "Reading old ${rrdfile} to preserve data...\n";
 
-if ($rrdinfo->{'rra[0].rows'} != $keep) {
-       print "Resizing ${rrdfile} from " . $rrdinfo->{'rra[0].rows'} .
-           " to ${keep} samples.\n";
+       my $rrdinfo = RRDs::info("${rrdfile}");
+       if (RRDs::error) {
+               print "Error while getting info: " . RRDs::error . "\n";
+               exit 1;
+       }
 
        (my $start, my $ostep, my $names, my $data) =
                RRDs::fetch("${rrdfile}",
-               "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
-               "AVERAGE");
+                               "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
+                               "AVERAGE");
 
        if (RRDs::error) {
                print "Error while fetching data: " . RRDs::error . "\n";
                exit 1;
        }
 
-       rrdcreate("${rrdfile}.new", (${start}-${ostep}));
-
-       print "Preserving data since " . localtime($start) . "\n";
+       foreach my $field (@$names) {
+               if (! -e "${rrdfile}.${field}") {
+                       rrdcreate("${rrdfile}.${field}","${field}",(${start}-${ostep}));
+               }
+       }
 
        my $pos = $start;
        foreach my $line (@$data) {
-               my $vline = "${pos}";
+               foreach my $field (@$names) {
+                       my $val = shift (@$line);
+                       next if (!defined($val));
 
-               foreach my $val (@$line) {
-                       $val = 'U' if (!defined($val));
-                       $vline .= ":${val}";
-               }
-               RRDs::update("${rrdfile}.new", $vline) or die "Can't insert data\n";
+                       RRDs::update("${rrdfile}.${field}", "${pos}:${val}");
+                       if (RRDs::error) {
+                               print "Can't insert data: " . RRDs::error . "\n";
+                               exit 1;
+                       }
 
-               if (RRDs::error) {
-                       print "Error while updating: " . RRDs::error . "\n";
-                       exit 1;
                }
+
                $pos += $ostep;
 
                if ((($pos-$start)/$ostep) == $#$data) {
@@ -114,17 +110,72 @@ if ($rrdinfo->{'rra[0].rows'} != $keep) {
        }
 
        rename("${rrdfile}", "${rrdfile}.old") or die "Can't rename old file: $!\n";
-       rename("${rrdfile}.new", "${rrdfile}") or die "Can't rename new file: $!\n";
+}
 
-       $rrdinfo = RRDs::info("${rrdfile}");
+foreach my $field (@fields) {
+       if (! -e "${rrdfile}.${field}") {
+               print "Creating ${rrdfile}.${field}...\n";
+               rrdcreate("${rrdfile}.${field}","${field}");
+       }
+
+       my $rrdinfo = RRDs::info("${rrdfile}.${field}");
        if (RRDs::error) {
                print "Error while getting info: " . RRDs::error . "\n";
                exit 1;
        }
 
        if ($rrdinfo->{'rra[0].rows'} != $keep) {
-               print "Failed!\n";
-               exit 1;
+               print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
+                       " to ${keep} samples.\n";
+
+               (my $start, my $ostep, my $names, my $data) =
+                       RRDs::fetch("${rrdfile}.${field}",
+                                       "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
+                                       "AVERAGE");
+
+               if (RRDs::error) {
+                       print "Error while fetching data: " . RRDs::error . "\n";
+                       exit 1;
+               }
+
+               rrdcreate("${rrdfile}.${field}.new", "${field}", (${start}-${ostep}));
+
+               print "Preserving data since " . localtime($start) . "\n";
+
+               my $pos = $start;
+               foreach my $line (@$data) {
+                       my $vline = "${pos}";
+
+                       foreach my $val (@$line) {
+                               $val = 'U' if (!defined($val));
+                               $vline .= ":${val}";
+                       }
+                       RRDs::update("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
+
+                       if (RRDs::error) {
+                               print "Error while updating: " . RRDs::error . "\n";
+                               exit 1;
+                       }
+                       $pos += $ostep;
+
+                       if ((($pos-$start)/$ostep) == $#$data) {
+                               last;
+                       }
+               }
+
+               rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
+               rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
+
+               $rrdinfo = RRDs::info("${rrdfile}.${field}");
+               if (RRDs::error) {
+                       print "Error while getting info: " . RRDs::error . "\n";
+                       exit 1;
+               }
+
+               if ($rrdinfo->{'rra[0].rows'} != $keep) {
+                       print "Failed!\n";
+                       exit 1;
+               }
        }
 }
 
@@ -156,14 +207,12 @@ while(1) {
 
        $session->close;
 
-       my $vline = "N";
        foreach my $var (@fields) {
                if (!(defined($vars->{$var}->{'value'}))) {
                        $vars->{$var}->{'value'} = 'U';
                }
-               $vline .= ":" . $vars->{$var}->{'value'};
+               RRDs::update("${rrdfile}.${var}", "N:" . $vars->{$var}->{'value'});
        }
-       RRDs::update("${rrdfile}", $vline);
        if (RRDs::error) {
                print "Error while updating: " . RRDs::error . "\n";
        }
@@ -174,7 +223,7 @@ while(1) {
        print HTML '<body bgcolor="#ffffff">';
 
        foreach my $var (@fields) {
-               my @graphdef = ("-t", $vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}:${var}:AVERAGE", "LINE1:${var}#FF0000");
+               my @graphdef = ("-t", $vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
                (my $averages, my $width, my $height) =
                        RRDs::graph("${outdir}/${var}.png.new",
                        "-w", "720", @graphdef);
Impressum, Datenschutz