perl external command example

shadih rahman shadhin71 at gmail.com
Wed Jan 28 18:28:30 CET 2009


Can someone please give me an example how to submit external command using
perl script.  I am trying to do this and its now working.  The command I am
passing to named pipe is something like this.  Thanks
"ACKNOWLEDGE_HOST_PROBLEM;hostname;1;1;1;1;sr2690;testing
"1233162775


my code is like this.


#!/usr/bin/perl
#use strict;
#use Getopt::Long qw(:config no_ignore_case);
use warnings;
use Data::Dumper;

use Getopt::Long ;

my ($action, $host, $service, $username, $comment, $HELP);
my $now=`date +%s`;
chomp ( $now );
my $commandfile='/var/log/nagios/rw/nagios.cmd';

open CF, ">$commandfile" or die $!;

my $usage = <<EOF;
  usage: $0  -a action -H host -s service -u username -c commnent

Options:
 -a --action
 -m, --host=STRING
    Host name or IP Address
 -s, --service=STRING
    Service Name
 -u, --username=STRING
    The user to authenticate
 -c, --comment=STRING
     Comment here
 -h, --help
    Print detailed help screen
EOF

# handle cmdline args
GetOptions( "a|action=s"   => \$action,
            "m|host=s"     => \$host,
        "s|service=s"  => \$service,
        "u|username=s" => \$username,
        "c|comment=s"  => \$comment,
        "h|help"       => \$HELP
             );
#print "$usage" if ($HELP && $action eq '');
if ( $HELP && ( ! ( defined ( $action) ) ) ) {
print "$usage";
}
&process_command();

sub process_command {
  #if (( $action eq 'status') || ( $action eq 'check') ||  ( $action eq
'reschedule')
  #       ||  ( $action eq 'acknowledge') ||  ( $action eq 'ack') ||
($action eq 'dis_check')
  #       || ($action eq 'ena_check') || ($action eq 'dis_not') || ($action
eq 'ena_not')) {

  if ( $action eq 'acknowledge' ) {
    process_acknowledge ();
  }
  elsif ( $action eq 'reschedule' ) {
    process_reschedule ();
  }
  else {
print " Only possible actions are status check reschedule acknowledge
disable_check enable_check disable_notification  and enable_notification\n";
  }
}

sub process_acknowledge {
  if ( defined ( $service ) ) {
    send_to_named_pipe (
"ACKNOWLEDGE_SVC_PROBLEM;$host;$service;1;1;1;$username;$comment" );
  }
  else {
    send_to_named_pipe (
"ACKNOWLEDGE_HOST_PROBLEM;$host;1;1;1;1;$username;$comment" );
}
}

sub process_reschedule {
  if ( defined ( $service ) ) {
    send_to_named_pipe (
"SCHEDULE_FORCED_SVC_CHECK;host1;service1;1110741500" );
  }
  else {
    send_to_named_pipe ( "SCHEDULE_FORCED_HOST_CHECK;$host;1110741500\n" );
  }
}
sub send_to_named_pipe {
  my ($command )  = @_;
  chomp ( $command );

  if ( defined ( $command ) ) {
    print CF "\"$command\n\"$now";


  }

}
close CF;


-- 
Cordially,
Shadhin Rahman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/developers/attachments/20090128/f1103a61/attachment.html>
-------------- next part --------------
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
-------------- next part --------------
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list