3 # collectd - contrib/cussh.pl
4 # Copyright (C) 2007-2009 Sebastian Harl
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; only version 2 of the License is applicable.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # Sebastian Harl <sh at tokkee.org>
25 cussh - collectd UNIX socket shell
29 B<cussh> [I<E<lt>pathE<gt>>]
33 B<collectd>'s unixsock plugin allows external programs to access the values it
34 has collected or received and to submit own values. This is a little
35 interactive frontend for this plugin.
41 =item I<E<lt>pathE<gt>>
43 The path to the UNIX socket provided by collectd's unixsock plugin. (Default:
44 F</var/run/collectd-unixsock>)
53 use Collectd::Unixsock();
56 my $path = $ARGV[0] || "/var/run/collectd-unixsock";
57 my $sock = Collectd::Unixsock->new($path);
63 GETTHRESHOLD => \&getthreshold,
66 PUTNOTIF => \&putnotif,
70 print STDERR "Unable to connect to $path!\n";
74 print "cussh version 0.2, Copyright (C) 2007-2008 Sebastian Harl\n"
75 . "cussh comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
76 . "and you are welcome to redistribute it under certain conditions.\n"
77 . "See the GNU General Public License 2 for more details.\n\n";
87 last if ($line =~ m/^quit$/i);
89 my ($cmd) = $line =~ m/^(\w+)\s*/;
96 if (defined $cmds->{$cmd}) {
100 print STDERR "ERROR: Unknown command $cmd!\n";
104 if (! $f->($sock, $line)) {
105 print STDERR "ERROR: Command failed!\n";
115 my $line = shift || return;
116 my $line_ptr = $line;
119 my $token_pattern = qr/[^"\s]+|"[^"]+"/;
121 while (my ($token) = $line_ptr =~ m/^($token_pattern)\s+/) {
126 if ($line_ptr =~ m/^$token_pattern$/) {
127 push @line, $line_ptr;
130 my ($token) = split m/ /, $line_ptr, 1;
131 print STDERR "Failed to parse line: $line\n";
132 print STDERR "Parse error near token \"$token\".\n";
136 foreach my $l (@line) {
137 if ($l =~ m/^"(.*)"$/) {
145 my $string = shift || return;
147 my ($h, $p, $pi, $t, $ti) =
148 $string =~ m#^([^/]+)/([^/-]+)(?:-([^/]+))?/([^/-]+)(?:-([^/]+))?\s*#;
151 return if ((! $h) || (! $p) || (! $t));
155 ($id{'host'}, $id{'plugin'}, $id{'type'}) = ($h, $p, $t);
157 $id{'plugin_instance'} = $pi if defined ($pi);
158 $id{'type_instance'} = $ti if defined ($ti);
163 my $ident = shift || return;
167 $string = $ident->{'host'} . "/" . $ident->{'plugin'};
169 if (defined $ident->{'plugin_instance'}) {
170 $string .= "-" . $ident->{'plugin_instance'};
173 $string .= "/" . $ident->{'type'};
175 if (defined $ident->{'type_instance'}) {
176 $string .= "-" . $ident->{'type_instance'};
191 my $line = shift || '';
193 my @line = tokenize($line);
194 my $cmd = shift (@line);
207 See the embedded Perldoc documentation for details. To do that, run:
211 PUTVAL <id> <value0> [<value1> ...]
213 Submits a value to the daemon.
218 Retrieves the current value or values from the daemon.
221 FLUSH [plugin=<plugin>] [timeout=<timeout>] [identifier=<id>] [...]
223 Sends a FLUSH command to the daemon.
228 Prints a list of available values.
231 PUTNOTIF severity=<severity> [...] message=<message>
233 Sends a notifications message to the daemon.
241 if (!exists ($text{$cmd}))
243 print STDOUT "Unknown command: " . uc ($cmd) . "\n\n";
247 print STDOUT $text{$cmd};
252 =item B<PUTVAL> I<Identifier> I<Valuelist>
257 my $sock = shift || return;
258 my $line = shift || return;
260 my @line = tokenize($line);
269 if (scalar(@line) < 2) {
270 print STDERR "Synopsis: PUTVAL <id> <value0> [<value1> ...]" . $/;
274 $id = getid($line[0]);
277 print STDERR "Invalid id \"$line[0]\"." . $/;
281 my ($time, @values) = split m/:/, $line;
282 $ret = $sock->putval(%$id, time => $time, values => \@values);
285 print STDERR "socket error: " . $sock->{'error'} . $/;
290 =item B<GETVAL> I<Identifier>
295 my $sock = shift || return;
296 my $line = shift || return;
298 my @line = tokenize($line);
307 if (scalar(@line) < 1) {
308 print STDERR "Synopsis: GETVAL <id>" . $/;
312 $id = getid($line[0]);
315 print STDERR "Invalid id \"$line[0]\"." . $/;
319 $vals = $sock->getval(%$id);
322 print STDERR "socket error: " . $sock->{'error'} . $/;
326 foreach my $key (keys %$vals) {
327 print "\t$key: $vals->{$key}\n";
332 =item B<GETTHRESHOLD> I<Identifier>
337 my $sock = shift || return;
338 my $line = shift || return;
340 my @line = tokenize($line);
349 if (scalar(@line) < 1) {
350 print STDERR "Synopsis: GETTHRESHOLD <id>" . $/;
354 $id = getid($line[0]);
357 print STDERR "Invalid id \"$line[0]\"." . $/;
361 $vals = $sock->getthreshold(%$id);
364 print STDERR "socket error: " . $sock->{'error'} . $/;
368 foreach my $key (keys %$vals) {
369 print "\t$key: $vals->{$key}\n";
374 =item B<FLUSH> [B<timeout>=I<$timeout>] [B<plugin>=I<$plugin>[ ...]]
379 my $sock = shift || return;
382 my @line = tokenize($line);
387 $res = $sock->flush();
392 foreach my $i (@line) {
393 my ($option, $value) = $i =~ m/^([^=]+)=(.+)$/;
394 next if (! ($option && $value));
396 if ($option eq "plugin") {
397 push @{$args{"plugins"}}, $value;
399 elsif ($option eq "timeout") {
400 $args{"timeout"} = $value;
402 elsif ($option eq "identifier") {
403 my $id = getid ($value);
406 print STDERR "Not a valid identifier: \"$value\"\n";
409 push @{$args{"identifier"}}, $id;
412 print STDERR "Invalid option \"$option\".\n";
417 $res = $sock->flush(%args);
421 print STDERR "socket error: " . $sock->{'error'} . $/;
431 my $sock = shift || return;
437 print STDERR "Synopsis: LISTVAL" . $/;
441 @res = $sock->listval();
444 print STDERR "socket error: " . $sock->{'error'} . $/;
448 foreach my $ident (@res) {
449 print $ident->{'time'} . " " . putid($ident) . $/;
454 =item B<PUTNOTIF> [[B<severity>=I<$severity>] [B<message>=I<$message>] [ ...]]
459 my $sock = shift || return;
460 my $line = shift || return;
462 my @line = tokenize($line);
467 foreach my $i (@line) {
468 my ($key, $val) = split m/=/, $i, 2;
470 $values{$key} = $val;
473 $values{'message'} = defined($values{'message'})
474 ? ($values{'message'} . ' ' . $key)
478 $values{'time'} ||= time();
480 $ret = $sock->putnotif(%values);
482 print STDERR "socket error: " . $sock->{'error'} . $/;
489 These commands follow the exact same syntax as described in
490 L<collectd-unixsock(5)>.
494 L<collectd(1)>, L<collectd-unisock(5)>
498 Written by Sebastian Harl E<lt>sh@tokkee.orgE<gt>.
500 B<collectd> has been written by Florian Forster and others.
504 Copyright (C) 2007 Sebastian Harl.
506 This program is free software; you can redistribute it and/or modify it under
507 the terms of the GNU General Public License as published by the Free Software
508 Foundation; only version 2 of the License is applicable.
512 # vim: set sw=4 ts=4 tw=78 noexpandtab :