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)

#!/bin/sh

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

HOST="192.168.0.229"


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

checksvc(){
SVC="$1"
SV="service $SVC status"
V=`ssh $HOST "$SV"`
        touch errors.txt

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


checksvc httpd
checksvc memcached
checksvc mysqld
checksvc crond
checksvc postfix

if [ -s errors.txt ]
then
        # errors present
        cat rc.head > mail.msg
        cat errors.txt >> mail.msg
        DATE=`date`
        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
else
        # no errors present
        echo ""
fi

# lets clean some junk up
rm mail.msg

1 thought on “Remote Check”

  1. Pingback: Pure Defect

Leave a Comment

Close Bitnami banner
Bitnami