Created
September 9, 2019 09:16
-
-
Save Leandros/b3362d5031883ec303b96ee10a1e7cea to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MONITORING | |
# v1.0.0 | |
# | |
### INSTALLATION | |
# | |
# 1. Install dependencies | |
# | |
# `apt-get install mailx curl openssl` | |
# | |
# 2. Setup `monitor.sh` script. | |
# | |
# Copy the script to a easily reachable location and mark it as | |
# executable (`chmod +x monitor.h`) | |
# | |
# 3. Setup cronjobs. | |
# | |
# It's recommended to run the monitor script at least every 5 minutes. | |
# This can be achieved with cronjobs, for example: | |
# */5 * * * * sudo /home/ubuntu/monitor.sh example.com | |
# This will execute the `monitor.sh` for domain `example.com` at every | |
# 5th minute. | |
# | |
### E-Mail Configuration | |
readonly SMTP_SERVER="smtp.example.com" | |
readonly SENDER_EMAIL="[email protected]" | |
readonly RECIPIENT_EMAIL="[email protected]" | |
### USAGE | |
readonly domain="$1" | |
if [ "$domain" = "" ]; then | |
echo "usage: monitor.sh domain" | |
exit 1 | |
fi | |
readonly cfgpath="$HOME/.uptimebot" | |
readonly varfile="$cfgpath/$domain" | |
mkdir -p "$cfgpath" | |
function _sendmail() { | |
mailx \ | |
-s "$1" \ | |
-r "$SENDER_EMAIL" \ | |
-S smtp="$SMTP_SERVER" \ | |
-S smtp-use-starttls \ | |
$RECIPIENT_EMAIL \ | |
< /dev/null | |
} | |
function sendmail() { | |
local ts | |
ts=$(date "+%s") | |
if [ -f "$varfile" ]; then | |
readonly lastmail=$(cat "$varfile") | |
readonly minutes=$(( ($(date "+%s") - lastmail) / 60 )) | |
if (( "$minutes" > 60 )); then | |
echo "$ts" > "$varfile" | |
_sendmail "$@" | |
fi | |
else | |
echo "$ts" > "$varfile" | |
_sendmail "$@" | |
fi | |
} | |
exitstatus=0 | |
readonly statuscode=$(curl -I -X GET -s -L -o /dev/null -w '%{response_code}' "https://$domain" 2> /dev/null) | |
if [[ "$statuscode" != "200" ]]; then | |
sendmail "$domain: status not 200" | |
exitstatus=1 | |
fi | |
readonly enddate=$(echo | openssl s_client -connect "$domain:443" -servername "$domain" 2> /dev/null | openssl x509 -noout -enddate | cut -b 10-) | |
readonly timestamp=$(date --date="$enddate" "+%s") | |
readonly current=$(date "+%s") | |
readonly hours=$(( (timestamp - current) / 3600 )) | |
if (( "$hours" < 48 )); then | |
sendmail "$domain: certificate expires in $hours hours" | |
exitstatus=1 | |
fi | |
# Remove file if status is ok! | |
if (( exitstatus == 0 )); then | |
if [ -f "$varfile" ]; then | |
readonly ts=$(cat "$varfile") | |
readonly now=$(date "+%s") | |
readonly hour=$(( (now - ts) / 3600 )) | |
readonly minute=$(( ((now - ts) % 3600 ) / 60 )) | |
printf -v message "offline for %02d:%02d" $hour $minute | |
sendmail "$domain: status OK - $message" | |
fi | |
rm -f "$varfile" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment