Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active November 22, 2018 08:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save westonruter/4491955 to your computer and use it in GitHub Desktop.
Save westonruter/4491955 to your computer and use it in GitHub Desktop.
Script to ping URLs defined in urls.txt and alert you if they do not return 200 OK. You'll need to `sudo apt-get install bsd-mailx` to get the email, and you'll want to add a filter to make sure that $from is never sent to spam. A log is also kept in log.txt including timestamp, status code, and URL.
*/5 * * * * /home/pi/pinger/pinger.sh > /dev/null 2>&1
#!/bin/bash
from="pinger@raspberrypi.local"
to="username+pinger@gmail.com"
timeout=60
cd $(dirname $0)
function ping_url {
url=$1
url_hash=$(echo $url | md5sum | awk '{print $1}')
response_file=/tmp/pinger-response-${url_hash}
status_file=/tmp/pinger-status-${url_hash}
status=$(curl -s -S -i -m $timeout --stderr $response_file -o $response_file -w "%{http_code}" $url)
echo -e "\nURL: $url" >> $response_file
if [ -e $status_file ]; then
last_status=$(cat $status_file)
fi
if [ $status != 200 ] && [ $status != $last_status ]; then
mailx -s "Ping status $status for $url" -a "From: Pinger <$from>" $to < $response_file
fi
if [ ! -z "$last_status" ] && [ $last_status != 200 ] && [ $status == 200 ]; then
mailx -s "Ping status $status for $url EOM" -a "From: Pinger <$from>" $to < /dev/null
fi
echo $status > $status_file
echo $(date "+%Y%m%dT%H%M%S") $status $url | tee -a log.txt
}
for url in $(cat urls.txt); do
ping_url $url &
done
http://www.example.com/foo.html
http://www.example.net/bar/baz/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment