-
-
Save Mario-NTT/8e90de561840271c9eb00c6dbb2259ae to your computer and use it in GitHub Desktop.
Bash script to monitor internet outages
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
# return 1 if alive, 0 if dead | |
ping_success() { | |
! ping -c1 -W1 8.8.8.8 >/dev/null 2>&1 | |
} | |
# Spinner taken from https://stackoverflow.com/a/12498395 | |
spinnerindex=0 | |
spinner='-\|/' | |
sleepy_spinner() { | |
for (( i=0; i<$(($1 * 2)); i++ )) | |
do | |
spinnerindex=$(( (i+1) %4 )) | |
printf "\r${spinner:$spinnerindex:1} $2" | |
sleep 0.5 | |
done | |
} | |
downstart=$(date +%s); | |
downend=$(date +%s); | |
downcount=0 | |
internetstatus=1 | |
while [ 1 ]; do | |
(ping_success) | |
pingresult=$? | |
if ((pingresult==0)) && ((internetstatus==1)); then | |
((internetstatus=0)) | |
((downcount++)) | |
downstart=$(date +%s); | |
dt=$(date +%Y-%m-%d" "%H:%M:%S); | |
printf "\r$dt Internet went down #$downcount\n" | |
elif ((pingresult==1)) && ((internetstatus==0)); then | |
((internetstatus=1)) | |
downend=$(date +%s); | |
dt=$(date +%Y-%m-%d" "%H:%M:%S); | |
downseconds=$(( ($downstart - $downend) * -1 )) | |
if ((downseconds < 60)); then | |
printf "\r$dt Internet alive after being offline for $downseconds seconds\n" | |
else | |
downminutes=$(( $downseconds / 60 )) | |
printf "\r$dt Internet alive after being offline for $downminutes minutes\n" | |
fi | |
fi | |
# Spin spinner so the user knows we're alive | |
if ((internetstatus==1)); then | |
(sleepy_spinner 15 "Online") | |
else | |
(sleepy_spinner 5 "No connection") | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment