Perl EVENT HANDLER SSH Remote Command Exection

Jason daelic at gmail.com
Tue Nov 25 22:47:38 CET 2008


Hello,
I've written an Event Handler using Perl. The intent is to verify on the
remote box that we do need to restart the daemon in question, and then
perform a restart if needed.

The script works perfectly via the command line when I execute it as the
nagios user. Unfortunately, if the script doesn't work properly when it is
executed by nagios in response to a state change. It dies before completion.

I've put in various hooks (Basicly just made it send me an email at various
stages of the script) to determine exactly where it dies, and have isolated
it. Here is the portion that causes the script to die when nagios executes
it. (Please trust me and assume that $hostname and $username contain proper
values. I've tested the variables extensively to insure that they are being
populated.)

If I run the below, I'll see a log entry and an email for Step 1, but
nothing for Step 2 and beyond.

#!/usr/bin/perl
require '/usr/local/scripts/subs.pl';

report_findings("Step 1",".");
my $command = "grep check_tacplus /usr/local/nagios/etc/nrpe.commands";
my %tac_expected = run_command($command, $hostname, $username);
report_findings("Step 2",".");

sub run_command {
        my $command = $_[0];
        my $host = $_[1];
        my $username = $_[2];

        my %result = SSHexec($command, $hostname, $username);
        if ($result{'err'}) {
                my $subject = "Automated refreshtac failed on $hostname";
                my $message = "Error when attempting SSH connection to
$hostname:\n";
                $message .= "  ".$result{'err'}."\n";
                report_findings($subject, $message, $recipients);
                exit;
        }
        return %result;
}

sub SSHexec {
        use Net::SSH::Perl;

        my $CMD = $_[0];
        my $HOST = $_[1];
        my $USR = $_[2];
        my %RESULT;

        if (pingHost($HOST)) {
                %RESULT = ("err", "Connectivity Failure");
                return %RESULT;
        }

        my $ssh = Net::SSH::Perl->new($HOST, options => [ "protocol '2,1'"
]);
        unless ($ssh->login($USR)) {
                %RESULT = ("err", "Authentication Failure");
                return %RESULT;
        }
        my ($out,$err,$exit) = $ssh->cmd($CMD);
        %RESULT = ("out", $out,
                        "err", $err,
                        "exit", $exit);
        return %RESULT;
}

&report_findings is nothing but a wrapper for logging and sending email.
This works up until the SSH call.

&SSHexec is contained in /usr/local/scripts/subs.pl which holds common
subroutines that I use frequently when writing scripts. I use other subs
from there, so I know it's getting loaded.

I cannot figure out why this is working as an automation, when it works
perfectly otherwise. Any advice/hints/suggestions?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/developers/attachments/20081125/dd0dbeca/attachment.html>
-------------- next part --------------
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
-------------- 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