From b57167a10dcdf5c36f21f058e2aecf277aff54bc Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Thu, 17 Jan 2013 12:36:20 +0100 Subject: [PATCH 1/1] support for multiple values in one tcp connection and example config --- klimaschrank.conf | 54 +++++++++++++++++++++++++++++++++++++++++++++++ upsgraph.pl | 34 +++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 klimaschrank.conf diff --git a/klimaschrank.conf b/klimaschrank.conf new file mode 100644 index 0000000..8acd1c2 --- /dev/null +++ b/klimaschrank.conf @@ -0,0 +1,54 @@ +package UPSGRAPH; + +$outdir="/var/www/klimaschrank/"; +$step=60; + +$hosts = [ + { + 'address' => "131.188.42.24", + 'name' => "klimaschrank", + 'rrdfile' => "/var/www/klimaschrank/.work/klimaschrank.rrd", + + 'fields' => [ 'kaltw', 'vorlauf', 'ruecklauf', 'abluft', 'raum' ], + + 'vars' => { + 'kaltw' => { + 'name' => 'Kaltwasser', + 'port' => '1337', + 'proto' => 'tcp_multi', + 'multi_id' => '6328', + 'multi_delimiter' => " ", + }, + 'vorlauf' => { + 'name' => 'Vorlauftemperatur', + 'port' => '1337', + 'proto' => 'tcp_multi', + 'multi_id' => '2d28', + 'multi_delimiter' => " ", + }, + 'ruecklauf' => { + 'name' => 'Ruecklauftemperatur', + 'port' => '1337', + 'proto' => 'tcp_multi', + 'multi_id' => '5228', + 'multi_delimiter' => " ", + }, + 'abluft' => { + 'name' => 'Ablufttemperatur', + 'port' => '1337', + 'proto' => 'tcp_multi', + 'multi_id' => 'f528', + 'multi_delimiter' => " ", + }, + 'raum' => { + 'name' => 'Raumtemperatur', + 'port' => '1337', + 'proto' => 'tcp_multi', + 'multi_id' => '9028', + 'multi_delimiter' => " ", + }, + } + }, +]; + +1; diff --git a/upsgraph.pl b/upsgraph.pl index 2783fcf..10f622b 100755 --- a/upsgraph.pl +++ b/upsgraph.pl @@ -137,6 +137,31 @@ 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); + + while(<$sock>) { + chomp; + (my $key, my $value) = split(/${delimiter}/, $_); + $value=~ s/\s//g; + $values{$key} = $value; + } + + close($sock); + + %values; +} + if ($> == 0) { if (@ARGV != 2) { print STDERR "Running as root, please provide UID as 2th argument!\n"; @@ -333,6 +358,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 +371,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); -- 2.39.5