The problem#
I used to have an occasionally unreliable internet connection. I wanted logs of exactly how unreliable it was and an easy way to have notice when it was back up.
The solution#
Use cron
to check online status once a minute and write the
result to a file. An easy way to check is to confirm that google.com will
reply to a ping (this does give a false negative in the unlikely
event that Google is down).
To run a script every minute, put a file in /etc/cron.d
containing the line
* * * * * root /root/bin/online-check
where /root/bin/online-check
is the following script:
#!/bin/sh
# Check if computer is online by attempting to ping google.com.
PING_RESULT="`ping -c 2 google.com 2>/dev/null`"
if [ $? -eq 0 ] && ! echo "$PING_RESULT" | grep -F '64 bytes from 192.168.' >/dev/null 2>/dev/null
then
ONLINE="online"
else
ONLINE="offline"
fi
echo "`date '+%Y-%m-%d %T%z'` $ONLINE" >> /var/log/online.log
The details and pretty printing#
Log details#
That script creates a log in /var/log/online.log
like
2010-09-27 05:58:21-0700 offline
2010-09-27 05:59:21-0700 offline
2010-09-27 06:00:02-0700 online
2010-09-27 06:01:02-0700 online
Note the check for a response from 192.168.
due to
some failures resulting in getting a ping reply from my router. Your
network setup may vary and may display different failure modes.
As a little extra redundancy, -c 2
tries two pings.
Generating summaries#
Here are a few scripts I wrote for summarizing the log:
display-offline-ranges
:
#!/bin/sh
grep -B1 -A1 offline /var/log/online.log |
grep -v offline |
cut -d ' ' -f 1,2
Example output showing the start and end times of two offline periods:
$ display-offline-ranges
2010-09-28 23:28:12-0700
2010-09-29 06:44:02-0700
--
2010-09-29 09:10:02-0700
2010-09-29 09:16:02-0700
--
display-offline-times
:
#!/bin/sh
uniq -f2 -cd /var/log/online.log | grep offline
Example output showing offline periods with their length in minutes and starting time:
$ display-offline-times
117 2010-09-27 04:04:21-0700 offline
12 2010-09-28 14:46:21-0700 offline
435 2010-09-28 23:29:12-0700 offline
offline-times-csv
(the CSV version of the above):
#!/bin/sh
echo 'Minutes offline,Date,Time'
display-offline-times |
grep -v ' [2-5] ' |
sed "s/ offline//" |
sed 's/\([0-9]\) /\1,/g' |
sed 's/^ *//'
Example output (for feeding into a spreadsheet program):
$ offline-times-csv
Minutes offline,Date,Time
117,2010-09-27,04:04:21-0700
12,2010-09-28,14:46:21-0700
435,2010-09-28,23:29:12-0700
Pretty printing#
The following just pretty prints the log as it is generated. It colors offline lines with a red background and online lines with a green background so the difference is easily visible across a room. That way you can glance up from whatever else you are doing while waiting for your internet connect to return. The result looks like
2010-09-27 05:58:21-0700 offline 2010-09-27 05:59:21-0700 offline 2010-09-27 06:00:02-0700 online 2010-09-27 06:01:02-0700 online
tail_online_status
:
#!/bin/sh
tailf /var/log/online.log | while read line
do
if (echo $line | grep -Fq online)
then
printf "\033[48;5;2m"
else
printf "\033[48;5;1m"
fi
echo $line
done
tailf
is for following log files as they are written.
The printf
commands are using ANSI escape codes to set the
background color of the terminal.
Comments
Have something to add? Post a comment by sending an email to comments@aweirdimagination.net. You may use Markdown for formatting.
There are no comments yet.