2008
06.18

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 ??

The following two tabs change content below.

charlie root

VP of keeping it real / HNIC at pissedoffadmins.com
I bring the cold coffee.

Latest posts by charlie root (see all)

10 comments so far

Add Your Comment
  1. check_tcp works like a charm. Also thanks for the tips of the blogger~

  2. i also use the check_tcp plugin. easy to handle…

  3. Why not use check_tcp. It’s a common nagios plugin

  4. how it can check the port?

  5. check_tcp -H HOSTIPHERE -p PORTNUMBERHERE

  6. thanks, this worked like a charm..how do it make it work to produce graph for nagiosgraph

  7. this doesn’t work.

  8. Do you mean that we should create that script in /usr/local/nagios/libexec/port_check.sh and then call it from commands.cfg ?

    • yes

  9. Just what I needed. Excellent.