Skip to content

Instantly share code, notes, and snippets.

@zenja
Last active February 26, 2019 12:18
Show Gist options
  • Save zenja/340fdea9cb952fbd157f86966434736d to your computer and use it in GitHub Desktop.
Save zenja/340fdea9cb952fbd157f86966434736d to your computer and use it in GitHub Desktop.
Poor man's IP reachability monitoring and alerting script
#/usr/bin/env bash
main() {
local ip=$1; shift;
local maxGapMinutes=$1; shift;
local epochLastSuccess=`date +%s`
while true; do
# check if network is good to avoid false alarming
if [[ `network_is_good` != 'y' ]]; then
epochLastSuccess=`date +%s`
echo "*** Network is offline, skipping the test ***"
continue
fi
ping -c 1 "$ip"
code=$?
if [[ $code -eq 0 ]]; then
echo "Ping succeeded."
epochLastSuccess=`date +%s`
else
echo "Failed to ping!"
fi
epochCurr=`date +%s`
gapMinutes=`echo "(${epochCurr} - ${epochLastSuccess})/60" | bc`
if [[ $gapMinutes -gt $maxGapMinutes ]]; then
echo "Gap time is $gapMinutes minutes which is larger than $maxGapMinutes minutes. Alerting!"
make_noise
fi
sleep 1
done
}
network_is_good() {
ping -c 1 1.1.1.1 &>/dev/null
if [[ $? -eq 0 ]]; then
echo "y"
else
echo "n"
fi
}
# a long running function to constantly making sounds
make_noise() {
while true; do
echo "Alerting!! Please take action!!"
afplay /System/Library/Sounds/Glass.aiff
sleep 1
done
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment