Skip to content

Instantly share code, notes, and snippets.

@purefan
Created January 26, 2016 09:48
Show Gist options
  • Save purefan/52428d9eba5667a9b0c1 to your computer and use it in GitHub Desktop.
Save purefan/52428d9eba5667a9b0c1 to your computer and use it in GitHub Desktop.
Simple ping wrapper
#!/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