Code

Merge branch 'collectd-4.3' into collectd-4.4
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 13 May 2008 14:58:11 +0000 (16:58 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 13 May 2008 14:58:11 +0000 (16:58 +0200)
Conflicts:

contrib/cussh.pl

1  2 
contrib/cussh.pl

diff --combined contrib/cussh.pl
index 6da2856d0d8b307387788da48aff2da58cf6353d,f25c1552270e89dd836f7ef78ec8d3853c005197..3fa410518ef43749eea2c88ce7f93587c47c268d
@@@ -1,7 -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
@@@ -56,19 -56,12 +56,19 @@@ use Collectd::Unixsock()
        my $path = $ARGV[0] || "/var/run/collectd-unixsock";
        my $sock = Collectd::Unixsock->new($path);
  
 +      my $cmds = {
 +              PUTVAL  => \&putval,
 +              GETVAL  => \&getval,
 +              FLUSH   => \&flush,
 +              LISTVAL => \&listval,
 +      };
 +
        if (! $sock) {
                print STDERR "Unable to connect to $path!\n";
                exit 1;
        }
  
 -      print "cussh version 0.1, 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";
                print "cussh> ";
                my $line = <STDIN>;
  
 -              last if ((! $line) || ($line =~ m/^quit$/i));
 +              last if (! $line);
 +
 +              chomp $line;
  
 -              my ($cmd) = $line =~ m/^(\w+)\s+/;
 +              last if ($line =~ m/^quit$/i);
 +
 +              my ($cmd) = $line =~ m/^(\w+)\s*/;
                $line = $';
  
                next if (! $cmd);
                $cmd = uc $cmd;
  
                my $f = undef;
 -              if ($cmd eq "PUTVAL") {
 -                      $f = \&putval;
 -              }
 -              elsif ($cmd eq "GETVAL") {
 -                      $f = \&getval;
 +              if (defined $cmds->{$cmd}) {
 +                      $f = $cmds->{$cmd};
                }
                else {
                        print STDERR "ERROR: Unknown command $cmd!\n";
@@@ -113,7 -105,7 +113,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));
  
        ($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<GETVAL> I<Identifier>
  
 -=item B<PUTVAL> I<Identifier> I<Valuelist>
 -
 -These commands follow the exact same syntax as described in
 -L<collectd-unixsock(5)>.
 -
  =cut
  
  sub putval {
  
        my $id = getid(\$line);
  
 -      return if (! $id);
 +      if (! $id) {
 +              print STDERR $sock->{'error'} . $/;
 +              return;
 +      }
  
        my ($time, @values) = split m/:/, $line;
 -      return $sock->putval(%$id, $time, \@values);
 +      return $sock->putval(%$id, time => $time, values => \@values);
  }
  
 +=item B<PUTVAL> I<Identifier> I<Valuelist>
 +
 +=cut
 +
  sub getval {
        my $sock = shift || return;
        my $line = shift || return;
  
        my $id = getid(\$line);
  
 -      return if (! $id);
 +      if (! $id) {
 +              print STDERR $sock->{'error'} . $/;
 +              return;
 +      }
  
        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";
        return 1;
  }
  
 +=item B<FLUSH> [B<timeout>=I<$timeout>] [B<plugin>=I<$plugin>[ ...]]
 +
 +=cut
 +
 +sub flush {
 +      my $sock = shift || return;
 +      my $line = shift;
 +
 +      my $res;
 +
 +      if (! $line) {
 +              $res = $sock->flush();
 +      }
 +      else {
 +              my %args = ();
 +
 +              foreach my $i (split m/ /, $line) {
 +                      my ($option, $value) = $i =~ m/^([^=]+)=(.+)$/;
 +                      next if (! ($option && $value));
 +
 +                      if ($option eq "plugin") {
 +                              push @{$args{"plugins"}}, $value;
 +                      }
 +                      elsif ($option eq "timeout") {
 +                              $args{"timeout"} = $value;
 +                      }
 +                      else {
 +                              print STDERR "Invalid option \"$option\".\n";
 +                              return;
 +                      }
 +              }
 +
 +              $res = $sock->flush(%args);
 +      }
 +
 +      if (! $res) {
 +              print STDERR $sock->{'error'} . $/;
 +              return;
 +      }
 +      return 1;
 +}
 +
 +=item B<LISTVAL>
 +
 +=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;
 +}
 +
 +=back
 +
 +These commands follow the exact same syntax as described in
 +L<collectd-unixsock(5)>.
 +
  =head1 SEE ALSO
  
  L<collectd(1)>, L<collectd-unisock(5)>