Perl EVENT HANDLER SSH Remote Command Exection

Jason daelic at gmail.com
Tue Dec 2 02:23:50 CET 2008


I guess a good break from it was all I needed. Coming back from the holiday
weekend I took another look and have solved my issue by adding the following
to my script:
$ENV{'HOME'} = '/home/nagios';

Thanks, everyone, for the assistance provided.

On Wed, Nov 26, 2008 at 3:06 PM, Badri Pillai <badri at diglinks.com> wrote:

>  Hi Jason,
>
>
>
> I tried the following perl code as plugin and worked for me.
>
>
>
> #!/usr/bin/perl
>
>
>
> open L,">>/tmp/ssh_check.log" or die "Can't open log";
>
>
>
> print L "perl check..\n";
>
> print L scalar localtime(),"\n" ;
>
> my $string =`/usr/bin/ssh -n -i /var/log/nagios/id_nagios_rsa root\@
> EXAMPLE.COM uname -a`;
>
> print L $string;
>
>
>
> close L;
>
>
>
> print "Check done";
>
>
>
> exit 0;
>
>
>
> May be:
>
>
>
> 1)      Embedded perl? In nagios 3.x you can disable easily
>
> 2)      OR the Net::SSH:Perl module, to be honest I've never used it and
> is not installed on my test system.
>
>
>
> Hope you find the problem soon.
>
>
>
> Regards,
>
>
>
> Badri
>
>
>
> *Von:* Jason [mailto:daelic at gmail.com]
> *Gesendet:* Mittwoch, 26. November 2008 21:47
> *An:* Nagios Developers List
> *Betreff:* Re: [Nagios-devel] Perl EVENT HANDLER SSH Remote Command
> Exection
>
>
>
> Ok, I've re-tooled, using more simplified logic for the purpose of testing
> the SSH connection. The script now has no logic, it just attempts to ssh to
> the remote host and run /usr/bin/uname -a.
>
>
>
> I've also explicitly defined my SSH key.
>
>
>
> As before, the script works perfectly when run manually as nagios user via
> the command line, but fails at the SSH call when executed via event handler
> from the nagios daemon.
>
>
>
> I'm beginning to wonder if Nagios & Net::SSH::Perl simply do not play nice
> with each other.
>
>
>
> I've attached my simplified script if anyone wants to see the exact code in
> full context.
>
>
>
> On Wed, Nov 26, 2008 at 10:26 AM, Jason <daelic at gmail.com> wrote:
>
> Yes, I am using public key authentication. It works fine when I run the
> script manually from the command prompt under the Nagios user, just not when
> the script is launched via nagios.
>
>
>
> Combining your reply with the reply from Sascha Runschke, I'm wondering if
> it's not finding $HOME/.ssh due to the environment being stripped down. I'm
> going to try explicitly defining my private key.
>
>
>
> I did put in full paths to my remote commands, and that did not resolve the
> problem.
>
>
>
> On Tue, Nov 25, 2008 at 11:34 PM, Badri Pillai <badri at diglinks.com> wrote:
>
> Hi Jason,
>
> I assume you are using public key authentication.
>
> If yes,
> I don't see ssh key to be used  being passed as argument,
> cause the key files are searched by default in "$HOME/.ssh"
> may be this directory is not accessible for the server?
>
> Other options to check -> if you  need a  " pty??  or not.
>
>
> Regards,
>
> BP
>
> Jason wrote:
> > 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?
> >
> >
>
> > ------------------------------------------------------------------------
>
> >
> > -------------------------------------------------------------------------
> > 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=/
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Nagios-devel mailing list
> > Nagios-devel at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/nagios-devel
> >
>
>
> -------------------------------------------------------------------------
> 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=/
> _______________________________________________
> Nagios-devel mailing list
> Nagios-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nagios-devel
>
>
>
>
>
> -------------------------------------------------------------------------
> 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=/
> _______________________________________________
> Nagios-devel mailing list
> Nagios-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nagios-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/developers/attachments/20081201/66d424b8/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