danielhilst

collectd-perfdata.pl

Sep 27th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.52 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use Socket;
  5. use Getopt::Std;
  6. use IO::Select;
  7. use Data::Dumper;
  8.  
  9. # my $unixsock_path = "/opt/collectd/var/run/collectd-unixsock";
  10.  
  11.  
  12. my %opts;
  13. my $debug = undef;
  14. my @ready = undef;
  15. my $buf = undef;
  16. my $buflen = 1024;
  17. my $fh = undef;
  18. my $timeout = 2;
  19. my $success = undef;
  20. my @response_buf = ();
  21. my $cmd = undef;;
  22. my ($unixsock_path, $valpath, $val);
  23.  
  24. sub usage {
  25.     print <<EOS;
  26. Usage: $0 [-s UNIXSOCK] -p VALPATH -v VAL | -P
  27.     -s UNIXSOCK    is the path to collectd socket, /opt/collectd/var/run/collectd-unixsock per default
  28.     -h             print this help message
  29.     -p VALPATH     is the path to value to be updated, see unixsock plugin manual for more information
  30.     -v VAL         is the value to be inputed on VALPATH
  31.     -P             open a prompt so that you can talk to collectds unixsock plugin directly
  32.     -d             debug mode
  33. EOS
  34. }
  35.  
  36. sub init {
  37.     my ($unixsock, $s);
  38.     socket($unixsock, PF_UNIX, SOCK_STREAM, 0) or die $!;
  39.     connect($unixsock, sockaddr_un($unixsock_path)) or die $!;
  40.     $s = IO::Select->new($unixsock);
  41.     return $unixsock, $s;
  42. }
  43.  
  44. # This is a prompt test
  45. sub prompt {
  46.     my ($unixsock, $s) = @_;
  47.     my ($fh);
  48.  
  49.     while(1) {
  50.     print ":> "; # prompt
  51.     my $buf = <STDIN>;
  52.    
  53.     print "Sending $buf";
  54.     send($unixsock, $buf, 0) or die $!;
  55.     while (@ready = $s->can_read(1)) {
  56.         foreach $fh (@ready) {
  57.         die "assertion failed \$fh == $unixsock" unless $fh == $unixsock;
  58.         recv($fh, $buf, $buflen, 0) or die $!;
  59.         print $buf;
  60.         }
  61.     }
  62.     }
  63. }
  64.  
  65.  
  66.  
  67. # Get options
  68. getopts("dhPp:v:s:", \%opts);
  69. $unixsock_path = $opts{"s"} || "/opt/collectd/var/run/collectd-unixsock";
  70. $valpath = $opts{"p"};
  71. $val = $opts{"v"};
  72. $debug = $opts{"d"};
  73.  
  74. # Print help and exit
  75. if ($opts{h}) {
  76.     usage;
  77.     exit(0);
  78. }
  79.  
  80. # Start prompt, exit after it returns
  81. if ($opts{P}) {
  82.     my ($unixsocket, $s) = init();
  83.     prompt($unixsocket, $s);
  84.     exit(0);
  85. }
  86.  
  87.  
  88. # Normal execution.. parameter checking
  89. unless ($unixsock_path and $valpath and $val) {
  90.     print "Wrong arguments\n";
  91.     usage;
  92.     exit(1);
  93. }
  94.  
  95. my ($unixsock, $s) = init();
  96.  
  97. # Ask for value
  98. $cmd = "GETVAL $valpath\n";
  99. print "Sending: $cmd" if $debug;
  100. send ($unixsock, $cmd, 0) or die $!;
  101.  
  102.  
  103. # Receive response
  104. $success = 0;
  105. print "begin response {\n" if $debug;
  106. @ready = $s->can_read($timeout);
  107. foreach $fh (@ready) {
  108.     die "assertion failed \$fh == $unixsock" unless $fh == $unixsock;
  109.     recv($unixsock, $buf, $buflen, 0);
  110.     print $buf if $debug;
  111.     if ($buf =~ /Value found/) {
  112.     $success = 1;
  113.     next;
  114.     }
  115. }
  116. print "} end of response\n" if $debug;
  117.  
  118. # If the value is there update it
  119. if ($success) {
  120.     @ready = $s->can_write($timeout);
  121.     foreach $fh (@ready) {
  122.     die "assertion failed \$fh == $unixsock" unless $fh == $unixsock;
  123.     $cmd = "PUTVAL $valpath N:$val\n";
  124.     print "Sending: $cmd" if $debug;
  125.     send($fh, $cmd, 0) or die $!;
  126.     }
  127. } else {
  128.     print "Timeout occurr\n" unless @ready;
  129.     exit(1);
  130. }
  131.  
  132.  
  133.  
  134. # Get response
  135. $success = 0;
  136. undef $fh;
  137. undef @ready;
  138. print "begin response {\n" if $debug;
  139. @ready = $s->can_read($timeout);
  140. foreach $fh (@ready) {
  141.     die "assertion failed \$fh == $unixsock" unless $fh == $unixsock;
  142.     recv($unixsock, $buf, $buflen, 0);
  143.     print "\t$buf" if $debug;
  144.     if ($buf =~ /Success/) {
  145.     $success = 1;
  146.     next;
  147.     }
  148. }
  149. print "} end of response\n" if $debug;
  150.  
  151. # Report success or failure
  152. if ($success) {
  153.     exit(0);
  154. } else {
  155.     print "Can't update $valpath with $val\nTimeout!\n";
  156.     exit(1);
  157. }
Advertisement
Add Comment
Please, Sign In to add comment