Skip to content

Instantly share code, notes, and snippets.

@nyxz
Created October 3, 2014 12:57
Show Gist options
  • Save nyxz/a7a4d17e0acf6bced660 to your computer and use it in GitHub Desktop.
Save nyxz/a7a4d17e0acf6bced660 to your computer and use it in GitHub Desktop.
This is a script to check if your servers are up and running by using curl to get the status code from the response.
#!/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='[email protected]'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment