X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=bindings%2Fperl%2FCollectd%2FUnixsock.pm;h=199a47c5ae304515d2e1ac5b6c84f796bd55951e;hb=24b8995d6ffaba6e1ad59cc330938466454059e7;hp=af274a588e5eed3a8d7503b72326c318ae13eb1a;hpb=07a3dec2e1a60416dcf4c6172755691097a2fb39;p=collectd.git diff --git a/bindings/perl/Collectd/Unixsock.pm b/bindings/perl/Collectd/Unixsock.pm index af274a58..199a47c5 100644 --- a/bindings/perl/Collectd/Unixsock.pm +++ b/bindings/perl/Collectd/Unixsock.pm @@ -57,8 +57,19 @@ use Carp (qw(cluck confess)); use IO::Socket::UNIX; use Regexp::Common (qw(number)); +our $Debug = 0; + return (1); +sub _debug +{ + if (!$Debug) + { + return; + } + print @_; +} + sub _create_socket { my $path = shift; @@ -134,6 +145,22 @@ sub _parse_identifier return ($ident); } # _parse_identifier +sub _escape_argument +{ + my $string = shift; + + if ($string =~ m/^\w+$/) + { + return ("$string"); + } + + $string =~ s#\\#\\\\#g; + $string =~ s#"#\\"#g; + $string = "\"$string\""; + + return ($string); +} + =head1 PUBLIC METHODS =over 4 @@ -169,13 +196,13 @@ value. On error false is returned. =cut -sub getval +sub getval # {{{ { my $obj = shift; my %args = @_; my $status; - my $fh = $obj->{'sock'} or confess; + my $fh = $obj->{'sock'} or confess ('object has no filehandle'); my $msg; my $identifier; @@ -183,13 +210,13 @@ sub getval $identifier = _create_identifier (\%args) or return; - $msg = "GETVAL $identifier\n"; - #print "-> $msg"; - send ($fh, $msg, 0) or confess ("send: $!"); + $msg = 'GETVAL ' . _escape_argument ($identifier) . "\n"; + _debug "-> $msg"; + print $fh $msg; - $msg = undef; - recv ($fh, $msg, 1024, 0) or confess ("recv: $!"); - #print "<- $msg"; + $msg = <$fh>; + chomp ($msg); + _debug "<- $msg\n"; ($status, $msg) = split (' ', $msg, 2); if ($status <= 0) @@ -198,9 +225,12 @@ sub getval return; } - for (split (' ', $msg)) + for (my $i = 0; $i < $status; $i++) { - my $entry = $_; + my $entry = <$fh>; + chomp ($entry); + _debug "<- $entry\n"; + if ($entry =~ m/^(\w+)=NaN$/) { $ret->{$1} = undef; @@ -212,7 +242,64 @@ sub getval } return ($ret); -} # getval +} # }}} sub getval + +=item I<$res> = I<$obj>-EB (I<%identifier>); + +Requests a threshold from the daemon. On success a hash-ref is returned with +the threshold data. On error false is returned. + +=cut + +sub getthreshold # {{{ +{ + my $obj = shift; + my %args = @_; + + my $status; + my $fh = $obj->{'sock'} or confess ('object has no filehandle'); + my $msg; + my $identifier; + + my $ret = {}; + + $identifier = _create_identifier (\%args) or return; + + $msg = 'GETTHRESHOLD ' . _escape_argument ($identifier) . "\n"; + _debug "-> $msg"; + print $fh $msg; + + $msg = <$fh>; + chomp ($msg); + _debug "<- $msg\n"; + + ($status, $msg) = split (' ', $msg, 2); + if ($status <= 0) + { + $obj->{'error'} = $msg; + return; + } + + for (my $i = 0; $i < $status; $i++) + { + my $entry = <$fh>; + chomp ($entry); + _debug "<- $entry\n"; + + if ($entry =~ m/^([^:]+):\s*(\S.*)$/) + { + my $key = $1; + my $value = $2; + + $key =~ s/^\s+//; + $key =~ s/\s+$//; + + $ret->{$key} = $value; + } + } + + return ($ret); +} # }}} sub getthreshold =item I<$obj>-EB (I<%identifier>, B