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));
76 my $sock = IO::Socket::UNIX->new (Type => SOCK_STREAM, Peer => $path);
79 cluck ("Cannot open UNIX-socket $path: $!");
85 =head1 VALUE IDENTIFIERS
87 The values in the collectd are identified using an five-tuple (host, plugin,
88 plugin-instance, type, type-instance) where only plugin-instance and
89 type-instance may be NULL (or undefined). Many functions expect an
90 I<%identifier> hash that has at least the members B<host>, B<plugin>, and
91 B<type>, possibly completed by B<plugin_instance> and B<type_instance>.
93 Usually you can pass this hash as follows:
95 $obj->method (host => $host, plugin => $plugin, type => $type, %other_args);
99 sub _create_identifier
106 if (!$args->{'host'} || !$args->{'plugin'} || !$args->{'type'})
108 cluck ("Need `host', `plugin' and `type'");
112 $host = $args->{'host'};
113 $plugin = $args->{'plugin'};
114 $plugin .= '-' . $args->{'plugin_instance'} if (defined ($args->{'plugin_instance'}));
115 $type = $args->{'type'};
116 $type .= '-' . $args->{'type_instance'} if (defined ($args->{'type_instance'}));
118 return ("$host/$plugin/$type");
119 } # _create_identifier
121 sub _parse_identifier
131 ($host, $plugin, $type) = split ('/', $string);
133 ($plugin, $plugin_instance) = split ('-', $plugin, 2);
134 ($type, $type_instance) = split ('-', $type, 2);
142 $ident->{'plugin_instance'} = $plugin_instance if (defined ($plugin_instance));
143 $ident->{'type_instance'} = $type_instance if (defined ($type_instance));
146 } # _parse_identifier
152 if ($string =~ m/^\w+$/)
157 $string =~ s#\\#\\\\#g;
158 $string =~ s#"#\\"#g;
159 $string = "\"$string\"";
164 =head1 PUBLIC METHODS
168 =item I<$obj> = Collectd::Unixsock->B<new> ([I<$path>]);
170 Creates a new connection to the daemon. The optional I<$path> argument gives
171 the path to the UNIX socket of the C<unixsock plugin> and defaults to
172 F</var/run/collectd-unixsock>. Returns the newly created object on success and
180 my $path = @_ ? shift : '/var/run/collectd-unixsock';
181 my $sock = _create_socket ($path) or return;
191 =item I<$res> = I<$obj>-E<gt>B<getval> (I<%identifier>);
193 Requests a value-list from the daemon. On success a hash-ref is returned with
194 the name of each data-source as the key and the according value as, well, the
195 value. On error false is returned.
205 my $fh = $obj->{'sock'} or confess ('object has no filehandle');
211 $identifier = _create_identifier (\%args) or return;
213 $msg = 'GETVAL ' . _escape_argument ($identifier) . "\n";
221 ($status, $msg) = split (' ', $msg, 2);
224 $obj->{'error'} = $msg;
228 for (my $i = 0; $i < $status; $i++)
232 _debug "<- $entry\n";
234 if ($entry =~ m/^(\w+)=NaN$/)
238 elsif ($entry =~ m/^(\w+)=($RE{num}{real})$/)
240 $ret->{$1} = 0.0 + $2;
247 =item I<$obj>-E<gt>B<putval> (I<%identifier>, B<time> =E<gt> I<$time>, B<values> =E<gt> [...]);
249 Submits a value-list to the daemon. If the B<time> argument is omitted
250 C<time()> is used. The required argument B<values> is a reference to an array
251 of values that is to be submitted. The number of values must match the number
252 of values expected for the given B<type> (see L<VALUE IDENTIFIERS>), though
253 this is checked by the daemon, not the Perl module. Also, gauge data-sources
254 (e.E<nbsp>g. system-load) may be C<undef>. Returns true upon success and false
265 my $fh = $obj->{'sock'} or confess;
271 if (defined $args{'interval'})
273 $interval = ' interval='
274 . _escape_argument ($args{'interval'});
277 $identifier = _create_identifier (\%args) or return;
278 if (!$args{'values'})
280 cluck ("Need argument `values'");
284 if (!ref ($args{'values'}))
286 $values = $args{'values'};
292 if ("ARRAY" ne ref ($args{'values'}))
294 cluck ("Invalid `values' argument (expected an array ref)");
298 if (! scalar @{$args{'values'}})
300 cluck ("Empty `values' array");
304 $time = $args{'time'} ? $args{'time'} : time ();
305 $values = join (':', $time, map { defined ($_) ? $_ : 'U' } (@{$args{'values'}}));
309 . _escape_argument ($identifier)
311 . ' ' . _escape_argument ($values) . "\n";
319 ($status, $msg) = split (' ', $msg, 2);
320 return (1) if ($status == 0);
322 $obj->{'error'} = $msg;
326 =item I<$res> = I<$obj>-E<gt>B<listval> ()
328 Queries a list of values from the daemon. The list is returned as an array of
329 hash references, where each hash reference is a valid identifier. The C<time>
330 member of each hash holds the epoch value of the last update of that value.
340 my $fh = $obj->{'sock'} or confess;
343 print $fh "LISTVAL\n";
348 ($status, $msg) = split (' ', $msg, 2);
351 $obj->{'error'} = $msg;
355 for (my $i = 0; $i < $status; $i++)
364 ($time, $ident) = split (' ', $msg, 2);
366 $ident = _parse_identifier ($ident);
367 $ident->{'time'} = int ($time);
370 } # for (i = 0 .. $status)
375 =item I<$res> = I<$obj>-E<gt>B<putnotif> (B<severity> =E<gt> I<$severity>, B<message> =E<gt> I<$message>, ...);
377 Submits a notification to the daemon.
385 Sets the severity of the notification. The value must be one of the following
386 strings: C<failure>, C<warning>, or C<okay>. Case does not matter. This option
391 Sets the message of the notification. This option is mandatory.
395 Sets the time. If omitted, C<time()> is used.
397 =item I<Value identifier>
399 All the other fields of the value identifiers, B<host>, B<plugin>,
400 B<plugin_instance>, B<type>, and B<type_instance>, are optional. When given,
401 the notification is associated with the performance data of that identifier.
402 For more details, please see L<collectd-unixsock(5)>.
414 my $fh = $obj->{'sock'} or confess;
416 my $msg; # message sent to the socket
418 if (!$args{'message'})
420 cluck ("Need argument `message'");
423 if (!$args{'severity'})
425 cluck ("Need argument `severity'");
428 $args{'severity'} = lc ($args{'severity'});
429 if (($args{'severity'} ne 'failure')
430 && ($args{'severity'} ne 'warning')
431 && ($args{'severity'} ne 'okay'))
433 cluck ("Invalid `severity: " . $args{'severity'});
439 $args{'time'} = time ();
443 . join (' ', map { $_ . '=' . _escape_argument ($args{$_}) } (keys %args))
453 ($status, $msg) = split (' ', $msg, 2);
454 return (1) if ($status == 0);
456 $obj->{'error'} = $msg;
460 =item I<$obj>-E<gt>B<flush> (B<timeout> =E<gt> I<$timeout>, B<plugins> =E<gt> [...], B<identifier> =E<gt> [...]);
470 If this option is specified, only data older than I<$timeout> seconds is
475 If this option is specified, only the selected plugins will be flushed. The
476 argument is a reference to an array of strings.
480 If this option is specified, only the given identifier(s) will be flushed. The
481 argument is a reference to an array of identifiers. Identifiers, in this case,
482 are hash references and have the members as outlined in L<VALUE IDENTIFIERS>.
493 my $fh = $obj->{'sock'} or confess;
498 if (defined ($args{'timeout'}))
500 $msg .= " timeout=" . $args{'timeout'};
503 if ($args{'plugins'})
505 foreach my $plugin (@{$args{'plugins'}})
507 $msg .= " plugin=" . $plugin;
511 if ($args{'identifier'})
513 for (@{$args{'identifier'}})
518 if (ref ($identifier) ne 'HASH')
520 cluck ("The argument of the `identifier' "
521 . "option must be an array reference "
522 . "of hash references.");
526 $ident_str = _create_identifier ($identifier);
532 $msg .= ' identifier=' . _escape_argument ($ident_str);
545 ($status, $msg) = split (' ', $msg, 2);
546 return (1) if ($status == 0);
548 $obj->{'error'} = $msg;
557 return ($obj->{'error'});
562 =item I<$obj>-E<gt>destroy ();
564 Closes the socket before the object is destroyed. This function is also
565 automatically called then the object goes out of scope.
576 close ($obj->{'sock'});
577 delete ($obj->{'sock'});
591 L<collectd-unixsock(5)>
595 Florian octo Forster E<lt>octo@verplant.orgE<gt>