Monday 9 January 2012

Port Check with Nagios


Multiple Port Check Plugin for Nagios

#!/bin/bash
#
# Usage: .//check_system_pp
#
# Discription:
# This plugin determine whether the server
# is running properly. It will check the following:
# * Are all the required TCP/IP ports open or not?
#
# Created:      25.12.2011 (IN)
#
#
#

##################################################################################
##
#
# Ports to check

PORT1="21"
PORT2="22"
PORT3="53"
PORT4="80"

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

PATH="/usr/bin:/usr/sbin:/bin:/sbin"

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

print_gpl() {
    echo "This program is free software; you can redistribute it and/or modify"
    echo "it under the terms of the GNU General Public License as published by"
    echo "the Free Software Foundation; either version 2 of the License, or"
    echo "(at your option) any later version."
    echo ""
    echo "This program is distributed in the hope that it will be useful,"
    echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
    echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
    echo "GNU General Public License for more details."
    echo ""
    echo "You should have received a copy of the GNU General Public License"
    echo "along with this program; if not, write to the Free Software"
    echo "Nilesh Kumar, New delhi 110067, India"
}

print_help(){
        echo "System process and port check script for Nagios."
        echo "Tested on RHE3/4/5/6, CentOS 4/5/6, Fedora 4, Solaris 9"
        echo ""
        echo "Usage: ./check_system_pp"
        echo "Website: http://www.nagiosexchange.org"
        echo ""
    print_gpl
}

while test -n "$1"
do
        case "$1" in
          *) print_help; exit $STATE_OK;;
        esac
done

check_port1()
{
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORT1`; do
                if [ `nmap 127.0.0.1 --system-dns | grep -w $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done

        if [ $PORTS -eq "1" ]; then
                exit_port1=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
        exit_port1=$STATE_OK
    fi
}
check_port1

check_port2()
{
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORT2`; do
                if [ `nmap 127.0.0.1 --system-dns | grep -w $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done

        if [ $PORTS -eq "1" ]; then
                exit_port2=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
        exit_port2=$STATE_OK
    fi
}
check_port2

check_port3()
{
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORT3`; do
                if [ `nmap 127.0.0.1 --system-dns | grep -w $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done

        if [ $PORTS -eq "1" ]; then
                exit_port3=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
        exit_port3=$STATE_OK
    fi
}
check_port3

check_port4()
{
        PORTS="0"
        ERROR_PORTS=""
        for NUM in `echo $PORT4`; do
                if [ `nmap 127.0.0.1 --system-dns | grep -w $NUM | grep -v grep | wc -l` -lt 1 ]; then
                        PORTS=1
                        ERROR_PORTS="$ERROR_PORTS""$NUM ";
                fi
        done

        if [ $PORTS -eq "1" ]; then
                exit_port4=$STATE_CRITICAL
        elif [ $PORTS -eq "0" ]; then
        exit_port4=$STATE_OK
    fi
}
check_port4

port_exit1=`expr $exit_port1`
port_exit2=`expr $exit_port2`
port_exit3=`expr $exit_port3`
port_exit4=`expr $exit_port4`

#
if [ $port_exit1 -eq "0" ]; then
    p1u=$PORT1,; else
    p1d=$PORT1,
fi
#
if [ $port_exit2 -eq "0" ]; then
    p2u=$PORT2,; else
    p2d=$PORT2,
fi
#
if [ $port_exit3 -eq "0" ]; then
    p3u=$PORT3,; else
    p3d=$PORT3,
fi
#
if [ $port_exit4 -eq "0" ]; then
    p4u=$PORT4; else
    p4d=$PORT4
fi
#

UP=`echo $p1u $p2u $p3u $p4u`
DOWN=`echo $p1d $p2d $p3d $p4d`

final_exit=`expr $exit_port1 + $exit_port2 + $exit_port3 + $exit_port4`

if [ $final_exit -eq "0" ]; then
    echo "Port $p1u $p2u $p3u $p4u UP and no any port DOWN"
    exitstatus=$STATE_OK
elif [ $final_exit -eq "1" ]; then
    echo "Port $p1u $p2u $p3u $p4u UP and Port $p1d $p2d $p3d $p4d DOWN"
    exitstatus=$STATE_WARNING
elif [ $final_exit -ge "1" ]; then
    echo "Port $p1u $p2u $p3u $p4u UP and Port $p1d $p2d $p3d $p4d DOWN"
    exitstatus=$STATE_CRITICAL
fi

exit $exitstatus

3 comments:

  1. It working perfect.

    ReplyDelete
  2. I am facing issue on nagios.script is working fine.how can i add nagios configure.

    Error: Service check command 'check_multipleport' specified in service 'check_multipleport' for host 'localhost' not defined anywhere!
    Checked 42 services.

    my configure details:-

    define service{
    use local-service
    host_name localhost
    service_description check_multipleport
    check_command check_multipleport
    }

    # 'check_multiplepor' command definition
    define command{
    command_name check_multiport
    command_line $USER1$/check_multipleport
    }

    ReplyDelete
    Replies
    1. This script is working with NRPE very well.

      Delete

Boot to UEFI Mode or legacy BIOS mode

Boot to UEFI Mode or legacy BIOS mode Choose UEFI or legacy BIOS modes while installing Windows. After Windows is installed, if you nee...