X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;ds=sidebyside;f=contrib%2Fcussh.pl;h=ee4c89396b98d0ecaa9ae2e1769b26876a4cc787;hb=98062438c25226e2e4a28ab629de5dfa27a79913;hp=c19c3f3092c659a860e108b753fac6505dcba3c6;hpb=ef493fef09db7227dcaedc2a3cae4a4a1ee4e1a9;p=collectd.git diff --git a/contrib/cussh.pl b/contrib/cussh.pl index c19c3f30..ee4c8939 100755 --- a/contrib/cussh.pl +++ b/contrib/cussh.pl @@ -1,7 +1,7 @@ #!/usr/bin/perl # # collectd - contrib/cussh.pl -# Copyright (C) 2007 Sebastian Harl +# Copyright (C) 2007-2008 Sebastian Harl # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the @@ -57,9 +57,12 @@ use Collectd::Unixsock(); my $sock = Collectd::Unixsock->new($path); my $cmds = { - PUTVAL => \&putval, - GETVAL => \&getval, - FLUSH => \&flush, + HELP => \&cmd_help, + PUTVAL => \&putval, + GETVAL => \&getval, + FLUSH => \&flush, + LISTVAL => \&listval, + PUTNOTIF => \&putnotif, }; if (! $sock) { @@ -67,7 +70,7 @@ use Collectd::Unixsock(); exit 1; } - print "cussh version 0.2, Copyright (C) 2007 Sebastian Harl\n" + print "cussh version 0.2, Copyright (C) 2007-2008 Sebastian Harl\n" . "cussh comes with ABSOLUTELY NO WARRANTY. This is free software,\n" . "and you are welcome to redistribute it under certain conditions.\n" . "See the GNU General Public License 2 for more details.\n\n"; @@ -76,9 +79,13 @@ use Collectd::Unixsock(); print "cussh> "; my $line = ; - last if ((! $line) || ($line =~ m/^quit$/i)); + last if (! $line); - my ($cmd) = $line =~ m/^(\w+)\s+/; + chomp $line; + + last if ($line =~ m/^quit$/i); + + my ($cmd) = $line =~ m/^(\w+)\s*/; $line = $'; next if (! $cmd); @@ -108,7 +115,7 @@ sub getid { print $$string . $/; my ($h, $p, $pi, $t, $ti) = - $$string =~ m/^(\w+)\/(\w+)(?:-(\w+))?\/(\w+)(?:-(\w+))?\s+/; + $$string =~ m#^([^/]+)/([^/-]+)(?:-([^/]+))?/([^/-]+)(?:-([^/]+))?\s*#; $$string = $'; return if ((! $h) || (! $p) || (! $t)); @@ -117,21 +124,55 @@ sub getid { ($id{'host'}, $id{'plugin'}, $id{'type'}) = ($h, $p, $t); - $id{'plugin_instance'} = $pi if ($pi); - $id{'type_instance'} = $ti if ($ti); + $id{'plugin_instance'} = $pi if defined ($pi); + $id{'type_instance'} = $ti if defined ($ti); return \%id; } +sub putid { + my $ident = shift || return; + + my $string; + + $string = $ident->{'host'} . "/" . $ident->{'plugin'}; + + if (defined $ident->{'plugin_instance'}) { + $string .= "-" . $ident->{'plugin_instance'}; + } + + $string .= "/" . $ident->{'type'}; + + if (defined $ident->{'type_instance'}) { + $string .= "-" . $ident->{'type_instance'}; + } + return $string; +} + =head1 COMMANDS =over 4 -=item B I +=item B -=item B I I +=cut -These commands follow the exact same syntax as described in -L. +sub cmd_help { + print < I =cut @@ -147,9 +188,13 @@ sub putval { } my ($time, @values) = split m/:/, $line; - return $sock->putval(%$id, $time, \@values); + return $sock->putval(%$id, time => $time, values => \@values); } +=item B I I + +=cut + sub getval { my $sock = shift || return; my $line = shift || return; @@ -163,7 +208,10 @@ sub getval { my $vals = $sock->getval(%$id); - return if (! $vals); + if (! $vals) { + print STDERR $sock->{'error'} . $/; + return; + } foreach my $key (keys %$vals) { print "\t$key: $vals->{$key}\n"; @@ -171,6 +219,10 @@ sub getval { return 1; } +=item B [B=I<$timeout>] [B=I<$plugin>[ ...]] + +=cut + sub flush { my $sock = shift || return; my $line = shift; @@ -193,6 +245,15 @@ sub flush { elsif ($option eq "timeout") { $args{"timeout"} = $value; } + elsif ($option eq "identifier") { + my $id = getid (\$value); + if (!$id) + { + print STDERR "Not a valid identifier: \"$value\"\n"; + next; + } + push @{$args{"identifier"}}, $id; + } else { print STDERR "Invalid option \"$option\".\n"; return; @@ -209,6 +270,56 @@ sub flush { return 1; } +=item B + +=cut + +sub listval { + my $sock = shift || return; + + my @res; + + @res = $sock->listval(); + + if (! @res) { + print STDERR $sock->{'error'} . $/; + return; + } + + foreach my $ident (@res) { + print $ident->{'time'} . " " . putid($ident) . $/; + } + return 1; +} + +=item B [[B=I<$severity>] [B=I<$message>] [ ...]] + +=cut + +sub putnotif { + my $sock = shift || return; + my $line = shift || return; + + my (%values) = (); + foreach my $i (split m/ /, $line) { + my($key,$val) = split m/=/, $i, 2; + if ($key && $val) { + $values{$key} = $val; + } + else { + $values{'message'} .= ' '.$key; + } + } + $values{'time'} ||= time(); + my(@tmp) = %values; + return $sock->putnotif(%values); +} + +=back + +These commands follow the exact same syntax as described in +L. + =head1 SEE ALSO L, L