A small perl script that checks to see if a TCP port is open on a remote host and produces timing statistics for nagios.
Tries to create a session to a remote TCP port to test if the port is available, it also can produce timing statistics for Nagios to collect.

check_port.pl -p -h (-c -w -v)
-p # port number to examine
-h # hostname or ip address to contact
-c # the number of seconds to wait before a going critical
-w # the number of seconds to wait before a flagging a warning
-v # creates nagios performance information

e.g check_port.pl -p 80 -h www.google.com -c 1.5 -w 1.0 -v

Script:-

#!/usr/bin/perl  -w
#===============================================================================
#
#         FILE:  check_port.pl
#
#        USAGE:  check_port.pl -p <port> -h <host> (-c <critical> -w <warning> -v)
#
#===============================================================================

use strict;
use warnings;
use Socket;
use Getopt::Long;
use Time::HiRes qw(gettimeofday tv_interval);

my ($crit, $warn, $timeout, $host, $portnum, $verbose);
GetOptions(
    ‘crtitical=s’   => \$crit,
    ‘warning=s’     => \$warn,
    ‘timeout=s’     => \$timeout,
    ‘host=s’        => \$host,
    ‘port=s’        => \$portnum,
    ‘verbose’       => \$verbose) or HELP_MESSAGE();

sub testport {
    my ($host,$port,$protocol,$timeout) = @_;
    my $startsec;
    my $elapsed = 0;
    if (!defined $timeout) { $timeout = 10 }
    if (!defined $protocol) { $protocol = “tcp” }
    my $proto = getprotobyname($protocol);
    my $iaddr = inet_aton($host);
    if ( !defined $iaddr ){ return 3,$elapsed; }
    my $paddr = sockaddr_in($port, $iaddr);
    $startsec = [gettimeofday()];
    socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die “socket: $!”;
    eval {
        local $SIG{ALRM} = sub { die “timeout” };
        alarm($timeout);
        connect(SOCKET, $paddr) or error();
        alarm(0);
    };
    if ($@) {
        close SOCKET || die “close: $!”;
        $elapsed = tv_interval ($startsec, [gettimeofday]);
        return “1”,$elapsed;
    } else {
        close SOCKET || die “close: $!”;
        $elapsed = tv_interval ($startsec, [gettimeofday]);
        return “0”,$elapsed;
    }
}
sub HELP_MESSAGE {
    print “$0 -p <port> -h <host> (-c <critical> -w <warning> -v)\n”;
    print “\t -p <port> # port number to examine\n”;
    print “\t -h <hostname> # hostname or ip address to contact\n”;
    print “\t -c <seconds> # the number of seconds to wait before a going critical\n”;
    print “\t -w <seconds> # the number of seconds to wait before a flagging a warning\n”;
    print “\t -v # displays nagios performance information\n”;
    print “\te.g $0 -p 80 -h www.google.com -c 1.5 -w 1.0 -v\n”;
    exit(4);
}

sub printperf {
    my ($warning,$critical,$elapsed) = @_;
    if ((defined $warning) && (defined $critical)) {
        print “|rta=$elapsed” . “s;$warning;$critical;0;$critical”;
    } else {
        print “|rta=$elapsed”
    }
}

sub test {
    my ($critical,$warning,$host,$portnum,$timeout) = @_;
    my $proto = “tcp”;
    my ($rc,$elapsed) = testport($host,$portnum,$proto,$timeout);
    if ($rc == 0) {
        if (defined $critical) {
            if ($critical <= $elapsed) {
                return 2,$elapsed;
            }
        }
        if (defined $warning) {
            if ($warning <= $elapsed) {
                return 1,$elapsed;
            }
        }
        return $rc,$elapsed;
    } else {
        return 2,$elapsed;
    }
}

unless ((defined $portnum) && (defined $host)) {
    HELP_MESSAGE();
    exit 1;
}
if ((defined $crit) && (defined $warn)) {
    if ($crit <= $warn) {
        print “Error: warning is greater than critical will never reach warning\n”;
        exit 4;
    }
}

my @mess = qw(OK WARNING CRITICAL UNKNOWN);
my @mess2 = (“is responding”,”is slow responding”,”is not responding”,”host lookup failed”);
my ($rc,$elapsed) = test($crit,$warn,$host,$portnum,$timeout);
print “PORT $portnum $mess[$rc]: $host/$portnum $mess2[$rc]”;
if (defined $verbose) {
    printperf($warn,$crit,$elapsed);
}
exit($rc);

###################################################

Change in Nagios configuration :-

– copy script in “/usr/local/nagios/libexec/” and change permission

chown nagios:nagios check_port.pl
chmod 755 check_port.pl

– Create command in “command.cfg”

## Check Port status ##
define command{
        command_name check_port
        command_line /usr/local/nagios/libexec/check_port.pl -p $ARG1$ -h $HOSTADDRESS$ -c 1.5 -w 1.0 -v
}

 

and create service in service.cfg

define service{
                use                             service-check-conf
                host_name                       XXXXXX
                service_description             Check Conf Jonas_Port / AM
                check_command                   check_port!8080
                notification_interval           0
                contact_groups                  JiraAdmin
}

– also you can test on command “./check_port.pl -p 80 -h 10.238.26.148”

image

Leave a Reply