Merge branch 'collectd-4.3'
[collectd.git] / contrib / cussh.pl
1 #!/usr/bin/perl
2 #
3 # collectd - contrib/cussh.pl
4 # Copyright (C) 2007  Sebastian Harl
5 #
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.
9 #
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.
14 #
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
18 #
19 # Author:
20 #   Sebastian Harl <sh at tokkee.org>
21 #
22
23 =head1 NAME
24
25 cussh - collectd UNIX socket shell
26
27 =head1 SYNOPSIS
28
29 B<cussh> [I<E<lt>pathE<gt>>]
30
31 =head1 DESCRIPTION
32
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.
36
37 =head1 OPTIONS
38
39 =over 4
40
41 =item I<E<lt>pathE<gt>>
42
43 The path to the UNIX socket provided by collectd's unixsock plugin. (Default:
44 F</var/run/collectd-unixsock>)
45
46 =back
47
48 =cut
49
50 use strict;
51 use warnings;
52
53 use Collectd::Unixsock();
54
55 { # main
56         my $path = $ARGV[0] || "/var/run/collectd-unixsock";
57         my $sock = Collectd::Unixsock->new($path);
58
59         my $cmds = {
60                 PUTVAL => \&putval,
61                 GETVAL => \&getval,
62                 FLUSH  => \&flush,
63         };
64
65         if (! $sock) {
66                 print STDERR "Unable to connect to $path!\n";
67                 exit 1;
68         }
69
70         print "cussh version 0.2, Copyright (C) 2007 Sebastian Harl\n"
71                 . "cussh comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
72                 . "and you are welcome to redistribute it under certain conditions.\n"
73                 . "See the GNU General Public License 2 for more details.\n\n";
74
75         while (1) {
76                 print "cussh> ";
77                 my $line = <STDIN>;
78
79                 last if ((! $line) || ($line =~ m/^quit$/i));
80
81                 my ($cmd) = $line =~ m/^(\w+)\s+/;
82                 $line = $';
83
84                 next if (! $cmd);
85                 $cmd = uc $cmd;
86
87                 my $f = undef;
88                 if (defined $cmds->{$cmd}) {
89                         $f = $cmds->{$cmd};
90                 }
91                 else {
92                         print STDERR "ERROR: Unknown command $cmd!\n";
93                         next;
94                 }
95
96                 if (! $f->($sock, $line)) {
97                         print STDERR "ERROR: Command failed!\n";
98                         next;
99                 }
100         }
101
102         $sock->destroy();
103         exit 0;
104 }
105
106 sub getid {
107         my $string = shift || return;
108
109         print $$string . $/;
110         my ($h, $p, $pi, $t, $ti) =
111                 $$string =~ m/^(\w+)\/(\w+)(?:-(\w+))?\/(\w+)(?:-(\w+))?\s+/;
112         $$string = $';
113
114         return if ((! $h) || (! $p) || (! $t));
115
116         my %id = ();
117
118         ($id{'host'}, $id{'plugin'}, $id{'type'}) = ($h, $p, $t);
119
120         $id{'plugin_instance'} = $pi if ($pi);
121         $id{'type_instance'} = $ti if ($ti);
122         return \%id;
123 }
124
125 =head1 COMMANDS
126
127 =over 4
128
129 =item B<GETVAL> I<Identifier>
130
131 =item B<PUTVAL> I<Identifier> I<Valuelist>
132
133 These commands follow the exact same syntax as described in
134 L<collectd-unixsock(5)>.
135
136 =cut
137
138 sub putval {
139         my $sock = shift || return;
140         my $line = shift || return;
141
142         my $id = getid(\$line);
143
144         if (! $id) {
145                 print STDERR $sock->{'error'} . $/;
146                 return;
147         }
148
149         my ($time, @values) = split m/:/, $line;
150         return $sock->putval(%$id, $time, \@values);
151 }
152
153 sub getval {
154         my $sock = shift || return;
155         my $line = shift || return;
156
157         my $id = getid(\$line);
158
159         if (! $id) {
160                 print STDERR $sock->{'error'} . $/;
161                 return;
162         }
163
164         my $vals = $sock->getval(%$id);
165
166         return if (! $vals);
167
168         foreach my $key (keys %$vals) {
169                 print "\t$key: $vals->{$key}\n";
170         }
171         return 1;
172 }
173
174 sub flush {
175         my $sock = shift || return;
176         my $line = shift;
177
178         my $res;
179
180         if (! $line) {
181                 $res = $sock->flush();
182         }
183         else {
184                 my %args = ();
185
186                 foreach my $i (split m/ /, $line) {
187                         my ($option, $value) = $i =~ m/^([^=]+)=(.+)$/;
188                         next if (! ($option && $value));
189
190                         if ($option eq "plugin") {
191                                 push @{$args{"plugins"}}, $value;
192                         }
193                         elsif ($option eq "timeout") {
194                                 $args{"timeout"} = $value;
195                         }
196                         else {
197                                 print STDERR "Invalid option \"$option\".\n";
198                                 return;
199                         }
200                 }
201
202                 $res = $sock->flush(%args);
203         }
204
205         if (! $res) {
206                 print STDERR $sock->{'error'} . $/;
207                 return;
208         }
209         return 1;
210 }
211
212 =head1 SEE ALSO
213
214 L<collectd(1)>, L<collectd-unisock(5)>
215
216 =head1 AUTHOR
217
218 Written by Sebastian Harl E<lt>sh@tokkee.orgE<gt>.
219
220 B<collectd> has been written by Florian Forster and others.
221
222 =head1 COPYRIGHT
223
224 Copyright (C) 2007 Sebastian Harl.
225
226 This program is free software; you can redistribute it and/or modify it under
227 the terms of the GNU General Public License as published by the Free Software
228 Foundation; only version 2 of the License is applicable.
229
230 =cut
231
232 # vim: set sw=4 ts=4 tw=78 noexpandtab :