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.

Revisions

  1. nyxz created this gist Oct 3, 2014.
    58 changes: 58 additions & 0 deletions server-check.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    #!/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