Created
June 22, 2012 15:54
-
-
Save tzengerink/2973651 to your computer and use it in GitHub Desktop.
Uptime Monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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="[email protected]" | |
TO_EMAIL="[email protected]" | |
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