simple nagios port check

here is a simple little nagios script to check the status of a port with ouput – sounds stupid but i needed it to check 2 specific ports for ouput and it works.

#!/bin/bash
if [ -z $2 ]; then
        if [ -z $1 ]; then
                printf "needs \$1 & \$2 (address & port number)\n"
                exit 2
        else
                printf "needs \$2 (port number)\n"
                exit 2
        fi
fi

function port {
        if [ $? -ne 0 ] ; then
                printf "warning: "
                        if [ $? -eq 6 ]; then
                                echo "cant resolve host - check dns"
                                exit 2
                        fi
                if [ $? -eq 7 ]; then
                        echo "cant connect to host"
                        exit 2
                fi
                printf "port may be closed\n"
        else
                printf "port is open\n"
                exit 0
        fi
        }
curl -s -o "/dev/null" ${1}:${2}
## -o writes output to file instead of stdout
## -s runs silent mode
port;

syntax: port_check.sh <address of machine> <port>

simple enough right ??

«
»

Leave a Reply

Your email address will not be published. Required fields are marked *