Skip to content

Instantly share code, notes, and snippets.

@tzengerink
Created June 22, 2012 15:54
Show Gist options
  • Save tzengerink/2973651 to your computer and use it in GitHub Desktop.
Save tzengerink/2973651 to your computer and use it in GitHub Desktop.
Uptime Monitor
#!/bin/bash
#
# UPTIME MONITOR
# --------------
# Usage: Setup a cronjob to execute the script and your done.
#
# Copyright (c) 2012, T. Zengerink
# Licensed under MIT License.
# See: https://raw.github.com/gist/3151357/9e8e01df4ee12b1f04cd61e0ecee3ea8bd6f617b/mit-license.txt
# Setup variables
FROM_EMAIL="t.zengerink@gmail.com"
TO_EMAIL="t.zengerink@gmail.com"
CURL_TIMEOUT="5"
# Check if server is up
is_up()
{
HOST="$1"
[[ "$2" ]] && TO="$2" || TO="10"
RESP=$(curl --connect-timeout $TO --write-out %{http_code} --silent --output /dev/null $HOST)
[[ "$RESP" == "000" ]] && return 1 || return 0
}
# Send e-mail
send_email()
{
(
echo "To: $3"
echo "Subject: Host $1 is down"
echo ""
echo "Host $1 is down."
) | sendmail -F "Uptime Monitor" -f "$2" -t
}
# Main
main()
{
for SERVER in "$@"
do
if ( ! is_up "$SERVER" "$CURL_TIMEOUT" )
then
send_email "$SERVER" "$FROM_EMAIL" "$TO_EMAIL"
fi
done
}
# Execute main script
[[ "$@" ]] && main "$@" || echo "Usage: $0 [host1] [host2] [host3]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment