perfSONAR_PS::Services::MP::Agent::CommandLine - A module that will run a command and


perfSONAR_PS-Services-PingER documentation  | view source Contained in the perfSONAR_PS-Services-PingER distribution.

Index


NAME

Top

perfSONAR_PS::Services::MP::Agent::CommandLine - A module that will run a command and return it's output.

DESCRIPTION

Top

Inherited perfSONAR_PS::MP::Agent::Base class that allows a command to be executed. Specific tools should inherit from this class and override parse() in order to be able to format the command line output in a well understood data structure.

SYNOPSIS

Top

  # command line to run, variables are indicated with the '%...%' notation
  my $command = '/bin/ping -c %count% %destination%';

  # options to use, the above keys defined in $command will be 
  # substituted with the following values
  my %options = (
      'count' => 10,
      'destination' => 'localhost',
  );

  # create and setup a new Agent  
  my $agent = perfSONAR_PS::Services::MP::Agent::CommandLine( $command, $options );
  $agent->init();

  # collect the results (i.e. run the command)
  if( $mp->collectMeasurements() == 0 )
  {

  	# get the raw datastructure for the measurement
  	print "Results:\n" . $self->results() . "\n";

  }
  # opps! something went wrong! :(
  else {

    print STDERR "Command: '" . $self->commandString() . "' failed with result '" . $self->results() . "': " . $agent->error() . "\n"; 

  }




new( $command, $options, $namespace)

Creates a new agent class

  $command = complete path to command line tool to run, eg /bin/ping
  $options = special parsed string of arguments for the tool; this string will have variables replaced at run time
  $namespace = perfSONAR_PS::XML::Namespace object

command( $string )

accessor/mutator function for the generic command to run (normally set in the constructor). This command should have variable fields marked up between '%'s. For example for a ping we would have something like:

  '/bin/ping -c %count% %destination%';

This would then have the values to these variables substituted in at runtime. =cut sub command { my $self = shift; if ( @_ ) { $self->{"CMD"} = shift; } return $self->{"CMD"}; }

commandString( $string )

accessor/mutator function for the actual command line to run; this would be the run time command after $self->command() has had the relevant variables substituted.

options( \%hash )

accessor/mutator function for the variable=value set to substitute the $self->command() line with. ie for the ping example with command(), we would have:

  $self->command( '/bin/ping -c %count% %destination%' );
  $self->options( {
      'count' => 10,
      'destination' => 'localhost',
  	};

which would result in /bin/ping -c 10 localhost

init( )

does anything necessary before running the collect() such as modifying the options etc. Check to see that the command exists =cut sub init { my $self = shift; my ( $cmd, @other) = split /\s+/, $self->command(); if( ! -e $cmd ) { $self->error( "Executable '$cmd' not found."); return -1; }

	# work out dns and ip address of source (ie this host)
	my $src = Net::Domain::hostfqdn;
	#use Data::Dumper;
	#$logger->fatal( Dumper $src );
	$self->source( $src  );
	# TODO: check to make sure we pick up correct ip
	$self->sourceIp( Socket::inet_ntoa(
    	scalar gethostbyname( $self->source() || 'localhost' )
    ));

    #$logger->fatal( "\n\n\n\nSOURCE: " . $self->source() . '  ' . $self->sourceIp() . "\n\n\n");

	return 0;
}




collectMeasurements( )

Runs the command with the options specified in constructor. The return of this method should be

 -1 = something failed
  0 = command ran okay

on success, this method should call the parse() method to determine the relevant performance output from the tool.

parse( )

Given the output of the command as a ref to an array of strings (\@array), and the original command line that generated those strings ($commandLine) at time $time (epoch secs),

do something with that output and store in into $self->{RESULTS}.

Return:

  -1 = could not parse output
   0 = everything parsed okay

The data structure is completely arbitary, but should be understood by the inherited class.


perfSONAR_PS-Services-PingER documentation  | view source Contained in the perfSONAR_PS-Services-PingER distribution.