X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph/blobdiff_plain/f49c04f7a2365b6bedf0828ca03dc4403bf12c8c..f447c2a1a5ed06c5fceda8f5476a1341cfc0f7f3:/upsgraph.pl diff --git a/upsgraph.pl b/upsgraph.pl index 2783fcf..3077e23 100755 --- a/upsgraph.pl +++ b/upsgraph.pl @@ -11,6 +11,7 @@ if ((@ARGV != 1) && (@ARGV != 2)) { use Net::SNMP; use IO::Socket::INET; +use IO::Select; use RRDs; use File::Copy; use Data::Dumper; @@ -78,7 +79,7 @@ sub rrdcreate(@) { push @cmd, "DS:${field}:GAUGE:600:" . $vars->{$field}->{'min'} . ":" . - $vars->{$field}->{'max'} . " "; + $vars->{$field}->{'max'}; push @cmd, "RRA:AVERAGE:0.5:1:${keep}"; @@ -124,7 +125,13 @@ sub fetch_tcp(@) { 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); @@ -137,6 +144,39 @@ sub fetch_tcp(@) { $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"; @@ -333,6 +373,7 @@ while(1) { my $vars = $host->{'vars'}; my $rrdfile = $host->{'rrdfile'}; my $hostname = $host->{'name'}; + my %multi_values = (); 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'}); + } 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); @@ -394,7 +443,7 @@ while(1) { print HTML "
\n"; open (HTML2, ">${outdir}/${hostname}.${var}.html.new"); - print HTML2 '' . $vars->{$var}->{'name'} . ''; + print HTML2 '' . $vars->{$var}->{'name'} . ''; print HTML2 ''; push @graphdef, "VDEF:min=${var},MINIMUM"; @@ -424,7 +473,7 @@ while(1) { } } - print HTML2 "
"; + print HTML2 "
\n"; $mtime=(stat("${outdir}/${hostname}.${var}.week.png.work"))[9]; ($averages, $width, $height) = @@ -441,7 +490,7 @@ while(1) { } } - print HTML2 "
"; + print HTML2 "
\n"; $mtime=(stat("${outdir}/${hostname}.${var}.year.png.work"))[9]; ($averages, $width, $height) = @@ -458,7 +507,7 @@ while(1) { } } - print HTML2 "
"; + print HTML2 "
\n"; print HTML2 "\n"; close(HTML2);