contrib/cussh.pl: Improve parsing of identifiers.
[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         if (! $sock) {
60                 print STDERR "Unable to connect to $path!\n";
61                 exit 1;
62         }
63
64         print "cussh version 0.1, Copyright (C) 2007 Sebastian Harl\n"
65                 . "cussh comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
66                 . "and you are welcome to redistribute it under certain conditions.\n"
67                 . "See the GNU General Public License 2 for more details.\n\n";
68
69         while (1) {
70                 print "cussh> ";
71                 my $line = <STDIN>;
72
73                 last if ((! $line) || ($line =~ m/^quit$/i));
74
75                 my ($cmd) = $line =~ m/^(\w+)\s+/;
76                 $line = $';
77
78                 next if (! $cmd);
79                 $cmd = uc $cmd;
80
81                 my $f = undef;
82                 if ($cmd eq "PUTVAL") {
83                         $f = \&putval;
84                 }
85                 elsif ($cmd eq "GETVAL") {
86                         $f = \&getval;
87                 }
88                 else {
89                         print STDERR "ERROR: Unknown command $cmd!\n";
90                         next;
91                 }
92
93                 if (! $f->($sock, $line)) {
94                         print STDERR "ERROR: Command failed!\n";
95                         next;
96                 }
97         }
98
99         $sock->destroy();
100         exit 0;
101 }
102
103 sub getid {
104         my $string = shift || return;
105
106         print $$string . $/;
107         my ($h, $p, $pi, $t, $ti) =
108 <<<<<<< collectd-4.3:contrib/cussh.pl
109                 $$string =~ m/^(\w+)\/(\w+)(?:-(\w+))?\/(\w+)(?:-(\w+))?\s+/;
110 =======
111                 $$string =~ m#^([^/]+)/([^/-]+)(?:-([^/]+))?/([^/-]+)(?:-([^/]+))?\s*#;
112 >>>>>>> local:contrib/cussh.pl
113         $$string = $';
114
115         return if ((! $h) || (! $p) || (! $t));
116
117         my %id = ();
118
119         ($id{'host'}, $id{'plugin'}, $id{'type'}) = ($h, $p, $t);
120
121         $id{'plugin_instance'} = $pi if defined ($pi);
122         $id{'type_instance'} = $ti if defined ($ti);
123         return \%id;
124 }
125
126 =head1 COMMANDS
127
128 =over 4
129
130 =item B<GETVAL> I<Identifier>
131
132 =item B<PUTVAL> I<Identifier> I<Valuelist>
133
134 These commands follow the exact same syntax as described in
135 L<collectd-unixsock(5)>.
136
137 =cut
138
139 sub putval {
140         my $sock = shift || return;
141         my $line = shift || return;
142
143         my $id = getid(\$line);
144
145         return if (! $id);
146
147         my ($time, @values) = split m/:/, $line;
148         return $sock->putval(%$id, $time, \@values);
149 }
150
151 sub getval {
152         my $sock = shift || return;
153         my $line = shift || return;
154
155         my $id = getid(\$line);
156
157         return if (! $id);
158
159         my $vals = $sock->getval(%$id);
160
161         return if (! $vals);
162
163         foreach my $key (keys %$vals) {
164                 print "\t$key: $vals->{$key}\n";
165         }
166         return 1;
167 }
168
169 =head1 SEE ALSO
170
171 L<collectd(1)>, L<collectd-unisock(5)>
172
173 =head1 AUTHOR
174
175 Written by Sebastian Harl E<lt>sh@tokkee.orgE<gt>.
176
177 B<collectd> has been written by Florian Forster and others.
178
179 =head1 COPYRIGHT
180
181 Copyright (C) 2007 Sebastian Harl.
182
183 This program is free software; you can redistribute it and/or modify it under
184 the terms of the GNU General Public License as published by the Free Software
185 Foundation; only version 2 of the License is applicable.
186
187 =cut
188
189 # vim: set sw=4 ts=4 tw=78 noexpandtab :