Last active
November 22, 2018 08:15
-
-
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.
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
*/5 * * * * /home/pi/pinger/pinger.sh > /dev/null 2>&1 |
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 | |
from="[email protected]" | |
to="[email protected]" | |
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 |
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
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