What’s the main problem with running a server at home – the 50% uptime figure… (OK, it’s not that bad 🙂
One of my Zoneedit passwords expired the other day and I forgot to change it on the server (duh)…
So – I created a really simple cron script to check all the expected services are up at each domain I host. It does this by connecting to a remote server (assuming you have SSH access to some external machine) and attempting to connect back to each domain on a particular port. I check for SMTP and HTTP.
And here it is:
[sourcecode language=’sh’]
#!/bin/bash
#
# Script to test if all the domains are up and working.
# It upens a connection to a remote server and tries to connect
# back to the given port of each domain. If the connect fails, or times out
# this script prints an error to stderr and returns false.
#
# To use, make this file executable and make a cronjob to run it or place
# the script in /etc/cron.hourly.
#
# Wilson Waters 20100824
#
# domains to test (space separeted list)
DOMAINS=”mail.mydomain.com mail.someother.domain.”
# remote server to test from
SERVER=””
# port to try and connect to
PORT=25
# timeout on connect after this many seconds
TIMEOUT=5
#user to connect to the remote host as
USER=”root”
#—————————————————————————
error=0
for domain in $DOMAINS
do
command=”ssh $USER@$SERVER nc -z -w$TIMEOUT $domain $PORT”
output=`$command 2>&1`
if [ $? -ne 0 ]; then
error=1
echo “Connect error $domain:$PORT ($output)” >&2
fi
done
exit $error
[/sourcecode]
UPDATE
I recently updated this script to be a little more useful and robust. It now sends mail directly from the script (rather than via cron) and allows you to set a port for each host.
[sourcecode language=’sh’]
#!/bin/bash
#
# Script to test if all the domains are up and working.
# It upens a connection to a remote server and tries to connect
# back to the given port of each domain. If the connect fails, or times out
# this script prints an error to stderr and returns false.
# domains to test (space separeted list)
DOMAINS=”mail.alintech.com.au www.alintech.com.au:80:”
# remote server to test from
SERVER=”somedomain.net”
# port to try and connect to
DEFAULT_PORT=25
# timeout on connect after this many seconds
TIMEOUT=5
MAIL_RECIPIENT=blablabla@alintech.com.au
#—————————————————————————
for domain in $DOMAINS
do
# work out the port
port=$DEFAULT_PORT
IFS=’:’ read -ra addr <<< "$domain"
if [[ ${#addr[@]} = 2 ]]; then
domain=${addr[0]}
port=${addr[1]}
fi
# ping it
if [ "X$SERVER" == "X" ]; then
command="nc -v -z -w$TIMEOUT $domain $port"
else
command="ssh $SERVER nc -v -z -w$TIMEOUT $domain $port"
fi
output=`$command 2>&1`
res=$?
output=”${output}\nTime: `date`”
if [ $res -ne 0 ]; then
echo -e $output | /usr/bin/mail -s “Connect error $domain” $MAIL_RECIPIENT
fi
done
[/sourcecode]
#1 by wilson on August 25, 2010 - 9:09 am
Apparently the wordpress sourcecode plugin sucks…
Just replace the two > with > to make the script work.