#!/bin/bash #========================================================== # This script checks if your servers are up and running. If # server is down it sends an email to the email(s) listed in # $EMAIL. # The script requires you to have `mailx` setup. #========================================================== # Set connect timeout to 1min TIMEOUT=60 # Receiver Email. Can be a comma separated list of emails. EMAIL='me@mail.com' function getStatusCode { local result=$1 local host=$2 local cmd="curl --connect-timeout $TIMEOUT -s -o /dev/null -w \"%{http_code}\" $host" eval $result=$($cmd) } function checkCode { local code=$1 local host=$2 if [ $code != '200' -a $code != '302' ] then sendEmail "Server at $host is down." else echo "Server at $host is up." fi } function sendEmail { local msg=$1 echo $msg | mailx -A gmail -s "Server Report" $EMAIL } function isUp { local host=$1 getStatusCode code $host checkCode $code $host } #========================================================== # Define your servers here: #========================================================== SERVERS=(\ 'http://server1.com' \ 'http://server2.com' \ ) for server in ${SERVERS[@]} do isUp $server done