]> git.zerfleddert.de Git - upsgraph/blobdiff - upsgraph.pl
Fix creation of new rrd files: Trailing space seems to be harmful
[upsgraph] / upsgraph.pl
index 2783fcf75e86ba259cd0ddad1a3469f1a4eeb0f5..3077e2357abaa0c458b56e1428a4b783782335c8 100755 (executable)
@@ -11,6 +11,7 @@ if ((@ARGV != 1) && (@ARGV != 2)) {
 
 use Net::SNMP;
 use IO::Socket::INET;
 
 use Net::SNMP;
 use IO::Socket::INET;
+use IO::Select;
 use RRDs;
 use File::Copy;
 use Data::Dumper;
 use RRDs;
 use File::Copy;
 use Data::Dumper;
@@ -78,7 +79,7 @@ sub rrdcreate(@) {
 
        push @cmd, "DS:${field}:GAUGE:600:" .
                $vars->{$field}->{'min'} . ":" .
 
        push @cmd, "DS:${field}:GAUGE:600:" .
                $vars->{$field}->{'min'} . ":" .
-               $vars->{$field}->{'max'} . " ";
+               $vars->{$field}->{'max'};
 
        push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
 
 
        push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
 
@@ -124,7 +125,13 @@ sub fetch_tcp(@) {
 
        return undef if (!$sock);
 
 
        return undef if (!$sock);
 
-       chomp(my $value = <$sock>);
+       my $select = IO::Select->new($sock);
+
+       my $value = undef;
+
+       if ($select->can_read(1)) {
+               chomp($value) if (sysread($sock, $value, 4096) > 0);
+       }
 
        close($sock);
 
 
        close($sock);
 
@@ -137,6 +144,39 @@ sub fetch_tcp(@) {
        $value;
 }
 
        $value;
 }
 
+sub fetch_tcp_multi(@) {
+       my $address = shift;
+       my $port = shift;
+       my $delimiter = shift;
+       my %values;
+
+       my $sock = IO::Socket::INET->new(PeerAddr => $address,
+                       PeerPort => $port,
+                       Proto => 'tcp',
+                       Timeout => 1);
+
+       return undef if (!$sock);
+
+       my $select = IO::Select->new($sock);
+
+       while($select->can_read(1)) {
+               if (sysread($sock, my $buf, 16384) > 0) {
+                       $buf=~s/\r//g;
+                       foreach my $line (split(/\n/, $buf)) {
+                               (my $key, my $value) = split(/${delimiter}/, $line);
+                               $value=~ s/\s//g;
+                               $values{$key} = $value;
+                       }
+               } else {
+                       last;
+               }
+       }
+
+       close($sock);
+
+       %values;
+}
+
 if ($> == 0) {
        if (@ARGV != 2) {
                print STDERR "Running as root, please provide UID as 2th argument!\n";
 if ($> == 0) {
        if (@ARGV != 2) {
                print STDERR "Running as root, please provide UID as 2th argument!\n";
@@ -333,6 +373,7 @@ while(1) {
                my $vars = $host->{'vars'};
                my $rrdfile = $host->{'rrdfile'};
                my $hostname = $host->{'name'};
                my $vars = $host->{'vars'};
                my $rrdfile = $host->{'rrdfile'};
                my $hostname = $host->{'name'};
+               my %multi_values = ();
 
                foreach my $var (@{$host->{'fields'}}) {
                        delete $vars->{$var}->{'value'};
 
                foreach my $var (@{$host->{'fields'}}) {
                        delete $vars->{$var}->{'value'};
@@ -345,6 +386,14 @@ while(1) {
                                $result = fetch_snmp($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
                        } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
                                $result = fetch_tcp($host->{'address'}, $vars->{$var}->{'port'});
                                $result = fetch_snmp($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'});
                        } elsif ($vars->{$var}->{'proto'} eq 'tcp') {
                                $result = fetch_tcp($host->{'address'}, $vars->{$var}->{'port'});
+                       } elsif ($vars->{$var}->{'proto'} eq 'tcp_multi') {
+                               if (defined($multi_values{$vars->{$var}->{'multi_id'}})) {
+                                       $result = $multi_values{$vars->{$var}->{'multi_id'}}
+                               } else {
+                                       my %values = fetch_tcp_multi($host->{'address'}, $vars->{$var}->{'port'}, $vars->{$var}->{'multi_delimiter'});
+                                       @multi_values{keys %values} = values %values;
+                                       $result = $multi_values{$vars->{$var}->{'multi_id'}};
+                               }
                        }
 
                        next unless (defined $result);
                        }
 
                        next unless (defined $result);
@@ -394,7 +443,7 @@ while(1) {
                        print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
 
                        open (HTML2, ">${outdir}/${hostname}.${var}.html.new");
                        print HTML "<a href=\"${hostname}.${var}.html\"><img src=\"${hostname}.${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a><br>\n";
 
                        open (HTML2, ">${outdir}/${hostname}.${var}.html.new");
-                       print HTML2 '<html><head><meta http-equiv="refresh" content="60"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="pragma" content="no-cache"/><meta http_equiv="expires" content="Sat, 26 Jul 1997 05:00:00 GMT"/><title>' . $vars->{$var}->{'name'} . '</title></head>';
+                       print HTML2 '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="refresh" content="60"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="pragma" content="no-cache"/><meta http_equiv="expires" content="Sat, 26 Jul 1997 05:00:00 GMT"/><title>' . $vars->{$var}->{'name'} . '</title></head>';
                        print HTML2 '<body bgcolor="#ffffff">';
 
                        push @graphdef, "VDEF:min=${var},MINIMUM";
                        print HTML2 '<body bgcolor="#ffffff">';
 
                        push @graphdef, "VDEF:min=${var},MINIMUM";
@@ -424,7 +473,7 @@ while(1) {
                                }
                        }
 
                                }
                        }
 
-                       print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>";
+                       print HTML2 "<img src=\"${hostname}.${var}.long.png\" width=\"${width}\" height=\"${height}\"><br>\n";
 
                        $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
                        ($averages, $width, $height) =
 
                        $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9];
                        ($averages, $width, $height) =
@@ -441,7 +490,7 @@ while(1) {
                                }
                        }
 
                                }
                        }
 
-                       print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>";
+                       print HTML2 "<img src=\"${hostname}.${var}.week.png\" width=\"${width}\" height=\"${height}\"><br>\n";
 
                        $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
                        ($averages, $width, $height) =
 
                        $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9];
                        ($averages, $width, $height) =
@@ -458,7 +507,7 @@ while(1) {
                                }
                        }
 
                                }
                        }
 
-                       print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>";
+                       print HTML2 "<img src=\"${hostname}.${var}.year.png\" width=\"${width}\" height=\"${height}\"><br>\n";
 
                        print HTML2 "</body></html>\n";
                        close(HTML2);
 
                        print HTML2 "</body></html>\n";
                        close(HTML2);
Impressum, Datenschutz