X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph/blobdiff_plain/6c1071ee6b37201ba1e39703a198c35d219fc647..2c148a8ed56c09cb34ba4c44f5d6b84c06afb50c:/upsgraph.pl diff --git a/upsgraph.pl b/upsgraph.pl index 812de11..24d196a 100755 --- a/upsgraph.pl +++ b/upsgraph.pl @@ -6,6 +6,7 @@ if ((@ARGV != 1) && (@ARGV != 2)) { } use Net::SNMP; +use IO::Socket::INET; use RRDs; use Data::Dumper; @@ -54,15 +55,43 @@ sub fetch_snmp(@) { (my $session, my $error) = Net::SNMP->session(Hostname => $address, Community => $community); - die "session error: $error" unless ($session); + if (!$session) { + print STDERR "session error: $error"; + } $session->translate(0); - my $val = $session->get_request($oid); + my $result = $session->get_request($oid); $session->close; - $val; + return undef if (!defined($result)); + + $result->{$oid}; +} + +sub fetch_tcp(@) { + my $address = shift; + my $port = shift; + + my $sock = IO::Socket::INET->new(PeerAddr => $address, + PeerPort => $port, + Proto => 'tcp', + Timeout => 1); + + return undef if (!$sock); + + chomp(my $value = <$sock>); + + close($sock); + + if (!$value) { + return undef; + } + + $value=~ s/\s//g; + + $value; } if ($> == 0) { @@ -212,10 +241,17 @@ exit 0 if ($child != 0); while(1) { open(HTML, ">${outdir}/index.html.new"); - print HTML 'USV status'; + print HTML 'Status'; print HTML ''; foreach my $host (@$hosts) { + print HTML "[{'name'}."\">".${host}->{'name'}."] "; + } + print HTML "
\n"; + + foreach my $host (@$hosts) { + print HTML "
\n"; + print HTML "{'name'}."\">\n"; my $vars = $host->{'vars'}; my $rrdfile = $host->{'rrdfile'}; my $hostname = $host->{'name'}; @@ -223,10 +259,19 @@ while(1) { foreach my $var (@{$host->{'fields'}}) { delete $vars->{$var}->{'value'}; - my $result = fetch_snmp($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'}); + my $result; + + if ((!defined($vars->{$var}->{'proto'})) || + ($vars->{$var}->{'proto'} eq '') || + ($vars->{$var}->{'proto'} eq 'snmp')) { + $result = fetch_snmp($host->{'address'}, $host->{'community'}, $vars->{$var}->{'oid'}); + } elsif ($vars->{$var}->{'proto'} eq 'tcp') { + $result = fetch_tcp($host->{'address'}, $vars->{$var}->{'port'}); + } + next unless (defined $result); - $vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}}; + $vars->{$var}->{'value'} = $result; if (defined($vars->{$var}->{'factor'})) { $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'}; } @@ -254,7 +299,7 @@ while(1) { rename("${outdir}/${hostname}.${var}.png.new", "${outdir}/${hostname}.${var}.png"); } - print HTML ""; + print HTML "
\n"; open (HTML2, ">${outdir}/${hostname}.${var}.html.new"); print HTML2 "" . $vars->{$var}->{'name'} . "";