Created
January 26, 2016 09:48
-
-
Save purefan/52428d9eba5667a9b0c1 to your computer and use it in GitHub Desktop.
Simple ping wrapper
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 | |
DOCURL=true; | |
echo $0 | |
runCurl() { | |
PING=$(curl --connect-timeout 1 --max-time 1 --head -s ${1}); | |
ISUP=false; | |
if [[ $PING == *"200 OK"* ]]; then | |
ISUP=true; | |
fi | |
} | |
runPing() { | |
PING=$(ping -c 1 -t 2 $1 | grep "% packet"); | |
if [[ $PING == *" 0.0% packet"* ]]; then | |
ISUP=true; | |
fi | |
} | |
while [ true ]; do | |
PREV=$ISUP; | |
if [ "${DOCURL}" = true ]; then | |
runCurl $1; | |
sleep 2; | |
else | |
runPing $1; | |
fi | |
WHEN=$(date); | |
if [ "${ISUP}" = true ]; then | |
if [ "${PREV}" = $ISUP ]; then | |
echo -n "."; | |
else | |
echo "${WHEN} ${1} Its all good"; | |
fi | |
else | |
echo "${WHEN} ${1} is down!!"; | |
LOG="/var/log/${1}.downtime.log" | |
echo -e "${WHEN} Failed:\n\t${PING}" >> $LOG | |
tput bel; | |
sleep 1; | |
tput bel; | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment