2 # collectd - Collectd::Unixsock
3 # Copyright (C) 2007,2008 Florian octo Forster
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; only version 2 of the License is applicable.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 # Florian octo Forster <octo at verplant.org>
22 package Collectd::Unixsock;
26 Collectd::Unixsock - Abstraction layer for accessing the functionality by
27 collectd's unixsock plugin.
31 use Collectd::Unixsock ();
33 my $sock = Collectd::Unixsock->new ($path);
35 my $value = $sock->getval (%identifier);
36 $sock->putval (%identifier,
38 values => [123, 234, 345]);
44 collectd's unixsock plugin allows external programs to access the values it has
45 collected or received and to submit own values. This Perl-module is simply a
46 little abstraction layer over this interface to make it even easier for
47 programmers to interact with the daemon.
54 #use constant { NOTIF_FAILURE => 1, NOTIF_WARNING => 2, NOTIF_OKAY => 4 };
56 use Carp (qw(cluck confess));
58 use Regexp::Common (qw(number));
65 my $sock = IO::Socket::UNIX->new (Type => SOCK_STREAM, Peer => $path);
68 cluck ("Cannot open UNIX-socket $path: $!");
74 =head1 VALUE IDENTIFIER
76 The values in the collectd are identified using an five-tupel (host, plugin,
77 plugin-instance, type, type-instance) where only plugin-instance and
78 type-instance may be NULL (or undefined). Many functions expect an
79 I<%identifier> hash that has at least the members B<host>, B<plugin>, and
80 B<type>, possibly completed by B<plugin_instance> and B<type_instance>.
82 Usually you can pass this hash as follows:
84 $obj->method (host => $host, plugin => $plugin, type => $type, %other_args);
88 sub _create_identifier
95 if (!$args->{'host'} || !$args->{'plugin'} || !$args->{'type'})
97 cluck ("Need `host', `plugin' and `type'");
101 $host = $args->{'host'};
102 $plugin = $args->{'plugin'};
103 $plugin .= '-' . $args->{'plugin_instance'} if (defined ($args->{'plugin_instance'}));
104 $type = $args->{'type'};
105 $type .= '-' . $args->{'type_instance'} if (defined ($args->{'type_instance'}));
107 return ("$host/$plugin/$type");
108 } # _create_identifier
110 sub _parse_identifier
120 ($host, $plugin, $type) = split ('/', $string);
122 ($plugin, $plugin_instance) = split ('-', $plugin, 2);
123 ($type, $type_instance) = split ('-', $type, 2);
131 $ident->{'plugin_instance'} = $plugin_instance if (defined ($plugin_instance));
132 $ident->{'type_instance'} = $type_instance if (defined ($type_instance));
135 } # _parse_identifier
137 =head1 PUBLIC METHODS
141 =item I<$obj> = Collectd::Unixsock->B<new> ([I<$path>]);
143 Creates a new connection to the daemon. The optional I<$path> argument gives
144 the path to the UNIX socket of the C<unixsock plugin> and defaults to
145 F</var/run/collectd-unixsock>. Returns the newly created object on success and
153 my $path = @_ ? shift : '/var/run/collectd-unixsock';
154 my $sock = _create_socket ($path) or return;
164 =item I<$res> = I<$obj>-E<gt>B<getval> (I<%identifier>);
166 Requests a value-list from the daemon. On success a hash-ref is returned with
167 the name of each data-source as the key and the according value as, well, the
168 value. On error false is returned.
178 my $fh = $obj->{'sock'} or confess;
184 $identifier = _create_identifier (\%args) or return;
186 $msg = "GETVAL $identifier\n";
188 send ($fh, $msg, 0) or confess ("send: $!");
191 recv ($fh, $msg, 1024, 0) or confess ("recv: $!");
194 ($status, $msg) = split (' ', $msg, 2);
197 $obj->{'error'} = $msg;
201 for (split (' ', $msg))
204 if ($entry =~ m/^(\w+)=NaN$/)
208 elsif ($entry =~ m/^(\w+)=($RE{num}{real})$/)
210 $ret->{$1} = 0.0 + $2;
217 =item I<$obj>-E<gt>B<putval> (I<%identifier>, B<time> =E<gt> I<$time>, B<values> =E<gt> [...]);
219 Submits a value-list to the daemon. If the B<time> argument is omitted
220 C<time()> is used. The requierd argument B<values> is a reference to an array
221 of values that is to be submitted. The number of values must match the number
222 of values expected for the given B<type> (see L<VALUE IDENTIFIER>), though this
223 is checked by the daemon, not the Perl module. Also, gauge data-sources
224 (e.E<nbsp>g. system-load) may be C<undef>. Returns true upon success and false
235 my $fh = $obj->{'sock'} or confess;
240 $identifier = _create_identifier (\%args) or return;
241 if (!$args{'values'})
243 cluck ("Need argument `values'");
247 if (!ref ($args{'values'}))
249 $values = $args{'values'};
253 my $time = $args{'time'} ? $args{'time'} : time ();
254 $values = join (':', $time, map { defined ($_) ? $_ : 'U' } (@{$args{'values'}}));
257 $msg = "PUTVAL $identifier $values\n";
259 send ($fh, $msg, 0) or confess ("send: $!");
261 recv ($fh, $msg, 1024, 0) or confess ("recv: $!");
264 ($status, $msg) = split (' ', $msg, 2);
265 return (1) if ($status == 0);
267 $obj->{'error'} = $msg;
271 =item I<$res> = I<$obj>-E<gt>B<listval> ()
273 Queries a list of values from the daemon. The list is returned as an array of
274 hash references, where each hash reference is a valid identifier. The C<time>
275 member of each hash holds the epoch value of the last update of that value.
285 my $fh = $obj->{'sock'} or confess;
288 send ($fh, $msg, 0) or confess ("send: $!");
291 ($status, $msg) = split (' ', $msg, 2);
294 $obj->{'error'} = $msg;
298 for (my $i = 0; $i < $status; $i++)
306 ($time, $ident) = split (' ', $msg, 2);
308 $ident = _parse_identifier ($ident);
309 $ident->{'time'} = int ($time);
312 } # for (i = 0 .. $status)
317 =item I<$res> = I<$obj>-E<gt>B<putnotif> (B<severity> =E<gt> I<$severity>, B<message> =E<gt> I<$message>, ...);
319 Submits a notification to the daemon.
327 Sets the severity of the notification. The value must be one of the following
328 strings: C<failure>, C<warning>, or C<okay>. Case does not matter. This option
333 Sets the message of the notification. This option is mandatory.
337 Sets the time. If omitted, C<time()> is used.
339 =item I<Value identifier>
341 All the other fields of the value identifiers, B<host>, B<plugin>,
342 B<plugin_instance>, B<type>, and B<type_instance>, are optional. When given,
343 the notification is associated with the performance data of that identifier.
344 For more details, please see L<collectd-unixsock(5)>.
356 my $fh = $obj->{'sock'} or confess;
358 my $msg; # message sent to the socket
359 my $opt_msg; # message of the notification
361 if (!$args{'message'})
363 cluck ("Need argument `message'");
366 if (!$args{'severity'})
368 cluck ("Need argument `severity'");
371 $args{'severity'} = lc ($args{'severity'});
372 if (($args{'severity'} ne 'failure')
373 && ($args{'severity'} ne 'warning')
374 && ($args{'severity'} ne 'okay'))
376 cluck ("Invalid `severity: " . $args{'severity'});
382 $args{'time'} = time ();
385 $opt_msg = $args{'message'};
386 delete ($args{'message'});
389 . join (' ', map { $_ . '=' . $args{$_} } (keys %args))
390 . " message=$opt_msg\n";
392 send ($fh, $msg, 0) or confess ("send: $!");
394 recv ($fh, $msg, 1024, 0) or confess ("recv: $!");
396 ($status, $msg) = split (' ', $msg, 2);
397 return (1) if ($status == 0);
399 $obj->{'error'} = $msg;
403 =item I<$obj>-E<gt>destroy ();
405 Closes the socket before the object is destroyed. This function is also
406 automatically called then the object goes out of scope.
417 close ($obj->{'sock'});
418 delete ($obj->{'sock'});
432 L<collectd-unixsock(5)>
436 Florian octo Forster E<lt>octo@verplant.orgE<gt>