Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use Socket;
- use Getopt::Std;
- use IO::Select;
- use Data::Dumper;
- # my $unixsock_path = "/opt/collectd/var/run/collectd-unixsock";
- my %opts;
- my $debug = undef;
- my @ready = undef;
- my $buf = undef;
- my $buflen = 1024;
- my $fh = undef;
- my $timeout = 2;
- my $success = undef;
- my @response_buf = ();
- my $cmd = undef;;
- my ($unixsock_path, $valpath, $val);
- sub usage {
- print <<EOS;
- Usage: $0 [-s UNIXSOCK] -p VALPATH -v VAL | -P
- -s UNIXSOCK is the path to collectd socket, /opt/collectd/var/run/collectd-unixsock per default
- -h print this help message
- -p VALPATH is the path to value to be updated, see unixsock plugin manual for more information
- -v VAL is the value to be inputed on VALPATH
- -P open a prompt so that you can talk to collectds unixsock plugin directly
- -d debug mode
- EOS
- }
- sub init {
- my ($unixsock, $s);
- socket($unixsock, PF_UNIX, SOCK_STREAM, 0) or die $!;
- connect($unixsock, sockaddr_un($unixsock_path)) or die $!;
- $s = IO::Select->new($unixsock);
- return $unixsock, $s;
- }
- # This is a prompt test
- sub prompt {
- my ($unixsock, $s) = @_;
- my ($fh);
- while(1) {
- print ":> "; # prompt
- my $buf = <STDIN>;
- print "Sending $buf";
- send($unixsock, $buf, 0) or die $!;
- while (@ready = $s->can_read(1)) {
- foreach $fh (@ready) {
- die "assertion failed \$fh == $unixsock" unless $fh == $unixsock;
- recv($fh, $buf, $buflen, 0) or die $!;
- print $buf;
- }
- }
- }
- }
- # Get options
- getopts("dhPp:v:s:", \%opts);
- $unixsock_path = $opts{"s"} || "/opt/collectd/var/run/collectd-unixsock";
- $valpath = $opts{"p"};
- $val = $opts{"v"};
- $debug = $opts{"d"};
- # Print help and exit
- if ($opts{h}) {
- usage;
- exit(0);
- }
- # Start prompt, exit after it returns
- if ($opts{P}) {
- my ($unixsocket, $s) = init();
- prompt($unixsocket, $s);
- exit(0);
- }
- # Normal execution.. parameter checking
- unless ($unixsock_path and $valpath and $val) {
- print "Wrong arguments\n";
- usage;
- exit(1);
- }
- my ($unixsock, $s) = init();
- # Ask for value
- $cmd = "GETVAL $valpath\n";
- print "Sending: $cmd" if $debug;
- send ($unixsock, $cmd, 0) or die $!;
- # Receive response
- $success = 0;
- print "begin response {\n" if $debug;
- @ready = $s->can_read($timeout);
- foreach $fh (@ready) {
- die "assertion failed \$fh == $unixsock" unless $fh == $unixsock;
- recv($unixsock, $buf, $buflen, 0);
- print $buf if $debug;
- if ($buf =~ /Value found/) {
- $success = 1;
- next;
- }
- }
- print "} end of response\n" if $debug;
- # If the value is there update it
- if ($success) {
- @ready = $s->can_write($timeout);
- foreach $fh (@ready) {
- die "assertion failed \$fh == $unixsock" unless $fh == $unixsock;
- $cmd = "PUTVAL $valpath N:$val\n";
- print "Sending: $cmd" if $debug;
- send($fh, $cmd, 0) or die $!;
- }
- } else {
- print "Timeout occurr\n" unless @ready;
- exit(1);
- }
- # Get response
- $success = 0;
- undef $fh;
- undef @ready;
- print "begin response {\n" if $debug;
- @ready = $s->can_read($timeout);
- foreach $fh (@ready) {
- die "assertion failed \$fh == $unixsock" unless $fh == $unixsock;
- recv($unixsock, $buf, $buflen, 0);
- print "\t$buf" if $debug;
- if ($buf =~ /Success/) {
- $success = 1;
- next;
- }
- }
- print "} end of response\n" if $debug;
- # Report success or failure
- if ($success) {
- exit(0);
- } else {
- print "Can't update $valpath with $val\nTimeout!\n";
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment