Remote Check

So I’ve been working on setting up a way to monitor a server at work to prevent my “assistant” from screwing up too badly and I’ve come up with a temporary solution until I can work out a much more robust script.

There are a few required elements, like the rc.head and rc.tail files which basically are the top and bottom of the email it sends out. Everything else is created and removed while the script is running making for a mostly clean experience. There needs to be at least (and ideally only) a one-way SSH trust from the testing machine to the target machine. I setup everything this way because the machine actually running the script is locked down to keep my boss and assistant from touching (see also: breaking) anything. Even if some fool removes the vixie-cron package this script continues to function until either a loss of network connectivity occurs or someone breaks the ssh trust (both of which in later versions will cause alert emails to go out)


# Script requires rc.head and rc.tail to be at least present, handy places
# to put some generic information to be included in each alert email

> errors.txt # this should zero out the file


# function: checksvc
# argument: service name to check
# returns: string statement 

SV="service $SVC status"
V=`ssh $HOST "$SV"`
        touch errors.txt

if echo $V | grep -q 'is running'
        #echo "$1 is running"
        echo ""
        echo "$1 is NOT running on host $HOST" >> errors.txt

checksvc httpd
checksvc memcached
checksvc mysqld
checksvc crond
checksvc postfix

if [ -s errors.txt ]
        # errors present
        cat rc.head > mail.msg
        cat errors.txt >> mail.msg
        echo "Scan run at $DATE" >> mail.msg
        cat rc.tail >> mail.msg
        mail -s '[ALERT]Services NOT running' -a errors.txt first.last@host.tld < mail.msg
        # no errors present
        echo ""

# lets clean some junk up
rm mail.msg

1 thought on “Remote Check”

  1. Pingback: Pure Defect

Leave a Comment

Close Bitnami banner