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);
65 PUTNOTIF => \&putnotif,
69 print STDERR "Unable to connect to $path!\n";
73 print "cussh version 0.2, Copyright (C) 2007-2008 Sebastian Harl\n"
74 . "cussh comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
75 . "and you are welcome to redistribute it under certain conditions.\n"
76 . "See the GNU General Public License 2 for more details.\n\n";
86 last if ($line =~ m/^quit$/i);
88 my ($cmd) = $line =~ m/^(\w+)\s*/;
95 if (defined $cmds->{$cmd}) {
99 print STDERR "ERROR: Unknown command $cmd!\n";
103 if (! $f->($sock, $line)) {
104 print STDERR "ERROR: Command failed!\n";
114 my $line = shift || return;
115 my $line_ptr = $line;
118 my $token_pattern = qr/[^"\s]+|"[^"]+"/;
120 while (my ($token) = $line_ptr =~ m/^($token_pattern)\s+/) {
125 if ($line_ptr =~ m/^$token_pattern$/) {
126 push @line, $line_ptr;
129 my ($token) = split m/ /, $line_ptr, 1;
130 print STDERR "Failed to parse line: $line\n";
131 print STDERR "Parse error near token \"$token\".\n";
135 foreach my $l (@line) {
136 if ($l =~ m/^"(.*)"$/) {
144 my $string = shift || return;
146 my ($h, $p, $pi, $t, $ti) =
147 $string =~ m#^([^/]+)/([^/-]+)(?:-([^/]+))?/([^/-]+)(?:-([^/]+))?\s*#;
150 return if ((! $h) || (! $p) || (! $t));
154 ($id{'host'}, $id{'plugin'}, $id{'type'}) = ($h, $p, $t);
156 $id{'plugin_instance'} = $pi if defined ($pi);
157 $id{'type_instance'} = $ti if defined ($ti);
162 my $ident = shift || return;
166 $string = $ident->{'host'} . "/" . $ident->{'plugin'};
168 if (defined $ident->{'plugin_instance'}) {
169 $string .= "-" . $ident->{'plugin_instance'};
172 $string .= "/" . $ident->{'type'};
174 if (defined $ident->{'type_instance'}) {
175 $string .= "-" . $ident->{'type_instance'};
190 my $line = shift || '';
192 my @line = tokenize($line);
193 my $cmd = shift (@line);
205 See the embedded Perldoc documentation for details. To do that, run:
209 PUTVAL <id> <value0> [<value1> ...]
211 Submits a value to the daemon.
216 Retrieves the current value or values from the daemon.
219 FLUSH [plugin=<plugin>] [timeout=<timeout>] [identifier=<id>] [...]
221 Sends a FLUSH command to the daemon.
226 Prints a list of available values.
229 PUTNOTIF severity=<severity> [...] message=<message>
231 Sends a notifications message to the daemon.
239 if (!exists ($text{$cmd}))
241 print STDOUT "Unknown command: " . uc ($cmd) . "\n\n";
245 print STDOUT $text{$cmd};
250 =item B<PUTVAL> I<Identifier> I<Valuelist>
255 my $sock = shift || return;
256 my $line = shift || return;
258 my @line = tokenize($line);
267 if (scalar(@line) < 2) {
268 print STDERR "Synopsis: PUTVAL <id> <value0> [<value1> ...]" . $/;
272 $id = getid($line[0]);
275 print STDERR "Invalid id \"$line[0]\"." . $/;
279 my ($time, @values) = split m/:/, $line;
280 $ret = $sock->putval(%$id, time => $time, values => \@values);
283 print STDERR "socket error: " . $sock->{'error'} . $/;
288 =item B<GETVAL> I<Identifier>
293 my $sock = shift || return;
294 my $line = shift || return;
296 my @line = tokenize($line);
305 if (scalar(@line) < 1) {
306 print STDERR "Synopsis: GETVAL <id>" . $/;
310 $id = getid($line[0]);
313 print STDERR "Invalid id \"$line[0]\"." . $/;
317 $vals = $sock->getval(%$id);
320 print STDERR "socket error: " . $sock->{'error'} . $/;
324 foreach my $key (keys %$vals) {
325 print "\t$key: $vals->{$key}\n";
330 =item B<FLUSH> [B<timeout>=I<$timeout>] [B<plugin>=I<$plugin>[ ...]]
335 my $sock = shift || return;
338 my @line = tokenize($line);
343 $res = $sock->flush();
348 foreach my $i (@line) {
349 my ($option, $value) = $i =~ m/^([^=]+)=(.+)$/;
350 next if (! ($option && $value));
352 if ($option eq "plugin") {
353 push @{$args{"plugins"}}, $value;
355 elsif ($option eq "timeout") {
356 $args{"timeout"} = $value;
358 elsif ($option eq "identifier") {
359 my $id = getid ($value);
362 print STDERR "Not a valid identifier: \"$value\"\n";
365 push @{$args{"identifier"}}, $id;
368 print STDERR "Invalid option \"$option\".\n";
373 $res = $sock->flush(%args);
377 print STDERR "socket error: " . $sock->{'error'} . $/;
387 my $sock = shift || return;
393 print STDERR "Synopsis: LISTVAL" . $/;
397 @res = $sock->listval();
400 print STDERR "socket error: " . $sock->{'error'} . $/;
404 foreach my $ident (@res) {
405 print $ident->{'time'} . " " . putid($ident) . $/;
410 =item B<PUTNOTIF> [[B<severity>=I<$severity>] [B<message>=I<$message>] [ ...]]
415 my $sock = shift || return;
416 my $line = shift || return;
418 my @line = tokenize($line);
423 foreach my $i (@line) {
424 my ($key, $val) = split m/=/, $i, 2;
426 $values{$key} = $val;
429 $values{'message'} = defined($values{'message'})
430 ? ($values{'message'} . ' ' . $key)
434 $values{'time'} ||= time();
436 $ret = $sock->putnotif(%values);
438 print STDERR "socket error: " . $sock->{'error'} . $/;
445 These commands follow the exact same syntax as described in
446 L<collectd-unixsock(5)>.
450 L<collectd(1)>, L<collectd-unisock(5)>
454 Written by Sebastian Harl E<lt>sh@tokkee.orgE<gt>.
456 B<collectd> has been written by Florian Forster and others.
460 Copyright (C) 2007 Sebastian Harl.
462 This program is free software; you can redistribute it and/or modify it under
463 the terms of the GNU General Public License as published by the Free Software
464 Foundation; only version 2 of the License is applicable.
468 # vim: set sw=4 ts=4 tw=78 noexpandtab :