From 0e572cdc78ace5787d1392a3e497d04fbbb26e6c Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Mon, 8 Feb 2010 23:45:02 +0100 Subject: [PATCH] add raw tcp to query values (to read poempelfox temp) --- temperature.conf | 21 +++++++++++++++++++-- upsgraph.pl | 48 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/temperature.conf b/temperature.conf index 6aac5cb..68ed2c7 100644 --- a/temperature.conf +++ b/temperature.conf @@ -65,7 +65,7 @@ $hosts = [ 'vars' => { 'temp' => { 'name' => 'Internal Temperature', - 'oid' => '1.3.6.1.4.1.9.9.13.1.3.1.3', + 'oid' => '1.3.6.1.4.1.9.9.13.1.3.1.3.1004', 'min' => '0', 'max' => '200', }, @@ -82,12 +82,29 @@ $hosts = [ 'vars' => { 'temp' => { 'name' => 'Internal Temperature', - 'oid' => '1.3.6.1.4.1.9.9.13.1.3.1.3', + 'oid' => '1.3.6.1.4.1.9.9.13.1.3.1.3.1004', 'min' => '0', 'max' => '200', }, } }, + { + 'address' => "217.173.147.33", + 'name' => "effrafax", + 'rrdfile' => "/var/spool/upsgraph/temp.effrafax.rrd", + + 'fields' => [ 'az' ], + + 'vars' => { + 'az' => { + 'name' => 'Arbeitszimmer', + 'port' => '31337', + 'proto' => 'tcp', + 'min' => '0', + 'max' => '100', + }, + } + }, ]; 1; diff --git a/upsgraph.pl b/upsgraph.pl index 812de11..2e40196 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) { @@ -223,10 +252,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'}; } -- 2.39.5