need help with my custom perl plugin

August Simonelli augusts at gmail.com
Wed Sep 5 00:48:39 CEST 2007


Hey all ...

I need to use nagios to check that a remote mail server can reach its relay.
the remote mail server is an appliance and all i can do on it is run a
telnet. to do this i've written a perl plugin that ssh's from the nagios box
to the appliance and then executes a telnet to the mail relay's port 25. the
script works perfectly on the command line - i can get all states (warn,
crit and ok).

Now, when i move it into nagios, i can only get critical and warning states.
for some reason, my test and exit for the OK state just gets missed and the
plugin returns the default warning status from the scripts last few lines
(see below).

i'm new to perl and nagios plugin writing, so i've probably messed up some
fundamental thing here ... but i can't seem to get it ...

any thoughts/corrections/criticisms/comments are greatly appreciated.

here's the script:

#!/usr/bin/perl -w
use strict;

use utils qw(%ERRORS $TIMEOUT);

# in case we just hang -
$SIG{ALRM} = sub {
    print "CRITICAL: Plugin timed out.\n";
    exit 2
};

alarm(10);

my ($host, $key, $remote) = @ARGV;
unless ($host and $key and $remote) {
    die "Usage: $0 hostname ssh_key remote_mail_server\n";
}

# default error
# my $connect_result = "Remote Test Failure";

open (SSH, "ssh -i $key $host telnet $remote 25|") or die "Can't open pipe:
$!";

# iterate through the loop and skip lines that don't have what we want.
then, store the last var - we know the telnet action is 4 lines
while (<SSH>){
    next if /^Trying/;
    next if /^Connected/;
    next if /^Escape/;
        if ($_ =~ /^220/) {
            print "$_ this works";
            exit($ERRORS{'OK'});
        }
        else {
            print "Connect died at: $_";
            exit($ERRORS{'CRITICAL'});
        }
close SSH;
}
print "fell thru";
exit($ERRORS{'WARNING'});
close SSH;


again, if i change the if statements to create an error (change ^Trying to
^rying for instance) nagios reports critical and clearly uses:

        else {
            print "Connect died at: $_";
            exit($ERRORS{'CRITICAL'});
        }

and $_ is set to the line that didn't match and caused the error ...

but when all those conditions ARE met nagios always makes it right past

        if ($_ =~ /^220/) {
            print "$_ this works";
            exit($ERRORS{'OK'});

and gives me

print "fell thru";
exit($ERRORS{'WARNING'});

printing out $_ in the while loop has the expected 220 line ...

ok ... hope this is not too verbose; sorry if this is just such bad code
that you all wanna slap me down! :-)

thanks,

august

ps. the service is defined as (names changed to protect the innocent):

define service{
        host_name               mailhost
        service_description     smtp relay check test
        is_volatile             0
        check_command
check_smtp_relay!/usr/local/nagios/.ssh/id_dsa!remoterelay.mydomain.com#
        max_check_attempts      3
        normal_check_interval   2
        retry_check_interval    1
        check_period            24x7
        notification_interval   240
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          unix-admins
        }

and the command is:

define command{
        command_name    check_smtp_relay
        command_line    $USER1$/smtpchecker.pl $HOSTADDRESS$ $ARG1$ $ARG2$
        }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/users/attachments/20070905/8e7203c0/attachment.html>
-------------- next part --------------
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
-------------- next part --------------
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null


More information about the Users mailing list