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

Backing up Minecraft

I run an in-house server for myself and some friends and I got tired of manually running backups, by logging in, stopping the server, making the tar.gz file and then moving it around, so I threw together this simple script.  It relies upon an active screen session for the server and a named window called Server to be present, but other than that it should be pretty straight forward how to works.


# change the argument for -S to whatever screen -ls says is the right value
# this allows us to target the running screen sessions so if there is a server
# reboot then this script must be updated (until I work out the code to make
# all that happen automatically

screen -S 436.minecraft -p Server -X stuff '/say Preparing for Server Backup
/say please get to a safe area soon (2 minutes)
sleep 2m # this should be 2m when testing is done
screen -S 436.minecraft -p Server -X stuff '/say commencing backup process now
/say this should take about 15 minutes max
/say server is going down NOW!

cd mc-server
tar -cf "mc-backup-"`date +%Y_%m_%d`".tar" world*
gzip "mc-backup-"`date +%Y_%m_%d`".tar"
mv "mc-backup-"`date +%Y_%m_%d`".tar.gz" ~/backups

screen -S 436.minecraft -p Server -X stuff './

It wouldn’t be hard to make the script scp the file to a different server or possibly push to a cloud provider like Dropbox. The key here is that it also brings the server back up so you could very easily make this a completely automatic task if so inclined.