Categories
Bash Operating Systems Programming Security

Check yourself

How do you know if your daemons go down? Some use services like pingdom but i’d rather check myself.

# crontab -e -u root
*/10 * * * * /opt/sh/check_services.sh
# vim /opt/sh/check_services.sh
#!/bin/bash

CMD="nc -vz"
HOST="localhost"
PORTS="53 80 443 953 3306 6697"
ERROR_MSG="Server, TCP port down:"
SMS="/opt/sh/sms.sh"

for PORT in ${PORTS} ; do
    ${CMD} "${HOST}" "${PORT}" &> /dev/null
    [ $? -ne 0 ] && ERRORS+="[$PORT] "
done

function cmd {
    echo "$*"
    $SMS "$*"
}

[ ! -z "$ERRORS" ] && cmd "$ERROR_MSG $ERRORS"

By Nimpen J. Nordström

System Developer and Network Security Enthusiast

Leave a Reply

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