A Weird Imagination

Logging online status

Posted in

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:


# 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
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:


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


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):

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

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


tailf /var/log/online.log | while read line
    if (echo $line | grep -Fq online)
        printf "\033[48;5;2m"
        printf "\033[48;5;1m"
    echo $line

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.


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.