3 # collectd - contrib/cussh.pl
4 # Copyright (C) 2007-2008 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 $string = shift || return;
117 my ($h, $p, $pi, $t, $ti) =
118 $$string =~ m#^([^/]+)/([^/-]+)(?:-([^/]+))?/([^/-]+)(?:-([^/]+))?\s*#;
121 return if ((! $h) || (! $p) || (! $t));
125 ($id{'host'}, $id{'plugin'}, $id{'type'}) = ($h, $p, $t);
127 $id{'plugin_instance'} = $pi if defined ($pi);
128 $id{'type_instance'} = $ti if defined ($ti);
133 my $ident = shift || return;
137 $string = $ident->{'host'} . "/" . $ident->{'plugin'};
139 if (defined $ident->{'plugin_instance'}) {
140 $string .= "-" . $ident->{'plugin_instance'};
143 $string .= "/" . $ident->{'type'};
145 if (defined $ident->{'type_instance'}) {
146 $string .= "-" . $ident->{'type_instance'};
169 See the embedded Perldoc documentation for details. To do that, run:
175 =item B<GETVAL> I<Identifier>
180 my $sock = shift || return;
181 my $line = shift || return;
183 my $id = getid(\$line);
186 print STDERR $sock->{'error'} . $/;
190 my ($time, @values) = split m/:/, $line;
191 return $sock->putval(%$id, time => $time, values => \@values);
194 =item B<PUTVAL> I<Identifier> I<Valuelist>
199 my $sock = shift || return;
200 my $line = shift || return;
202 my $id = getid(\$line);
205 print STDERR $sock->{'error'} . $/;
209 my $vals = $sock->getval(%$id);
212 print STDERR $sock->{'error'} . $/;
216 foreach my $key (keys %$vals) {
217 print "\t$key: $vals->{$key}\n";
222 =item B<FLUSH> [B<timeout>=I<$timeout>] [B<plugin>=I<$plugin>[ ...]]
227 my $sock = shift || return;
233 $res = $sock->flush();
238 foreach my $i (split m/ /, $line) {
239 my ($option, $value) = $i =~ m/^([^=]+)=(.+)$/;
240 next if (! ($option && $value));
242 if ($option eq "plugin") {
243 push @{$args{"plugins"}}, $value;
245 elsif ($option eq "timeout") {
246 $args{"timeout"} = $value;
248 elsif ($option eq "identifier") {
249 my $id = getid (\$value);
252 print STDERR "Not a valid identifier: \"$value\"\n";
255 push @{$args{"identifier"}}, $id;
258 print STDERR "Invalid option \"$option\".\n";
263 $res = $sock->flush(%args);
267 print STDERR $sock->{'error'} . $/;
278 my $sock = shift || return;
282 @res = $sock->listval();
285 print STDERR $sock->{'error'} . $/;
289 foreach my $ident (@res) {
290 print $ident->{'time'} . " " . putid($ident) . $/;
295 =item B<PUTNOTIF> [[B<severity>=I<$severity>] [B<message>=I<$message>] [ ...]]
300 my $sock = shift || return;
301 my $line = shift || return;
304 foreach my $i (split m/ /, $line) {
305 my($key,$val) = split m/=/, $i, 2;
307 $values{$key} = $val;
310 $values{'message'} .= ' '.$key;
313 $values{'time'} ||= time();
315 return $sock->putnotif(%values);
320 These commands follow the exact same syntax as described in
321 L<collectd-unixsock(5)>.
325 L<collectd(1)>, L<collectd-unisock(5)>
329 Written by Sebastian Harl E<lt>sh@tokkee.orgE<gt>.
331 B<collectd> has been written by Florian Forster and others.
335 Copyright (C) 2007 Sebastian Harl.
337 This program is free software; you can redistribute it and/or modify it under
338 the terms of the GNU General Public License as published by the Free Software
339 Foundation; only version 2 of the License is applicable.
343 # vim: set sw=4 ts=4 tw=78 noexpandtab :