Friday, 7 August 2009

Monitor your ADSL Connection for Outages & Uptime

I had the misfortune of choosing demon as my ISP and for the first couple of months managed to exceed my fair use policy and experience extended outages. In an effort to document my issues I wrote a couple of scripts on a NetBSD box that monitor the connection and automatically create a graph using gnuplot of the uptime.

The scripts are driven by a crontab script:

$ crontab -l
@hourly /home/msw/router/getstats
5,10,15,20,25,30,35,40,45,50,55 * * * * /home/msw/router/
5,10,15,20,25,30,35,40,45,50,55 * * * * /home/msw/router/
0,15,30,45 * * * * /home/msw/router/

Every five minutes the script attempts to ping one of the demon nameservers using its' IP address:

TIME="`date +%C%y%m%d%H%M`"
/sbin/ping -c 1 > /dev/null 2>&1
if [ "$?" == "0" ]

This generates a line in the LOGFILE which looks like this:

200907091945 1
200907091950 1
200907091955 1
200907092005 1
200907092010 1
200907092015 1
200907092020 1
200907092025 1
200907092030 1
200907092035 1

where the first number is the date and time and the second number is a '1' if the ping was successful, or a '0' if it was not.

The script turns these numbers into a nice graph with the help of GnuPlot then puts the resulting jpeg onto the local apache webserver:

sleep 30
export PATH=/bin:/sbin:/usr/pkg/bin
export RS=/home/msw/router
export HTDOCS=/usr/pkg/share/httpd/htdocs
export STATS_FILE=$RS/ping-stats.txt
export JPG_FILE=ping-stats.jpg
export SHARE_DIR=/usr/local/archive
export HTTPD_DIR=/usr/pkg/share/httpd/htdocs
cp $STATS_FILE $RS/ping-stats-copy.txt
gnuplot $RS/plot-stats.gnuplot > $SHARE_DIR/$JPG_FILE

The gnuplot script plot-stats.gnuplot that works the magic:

set terminal jpeg large size 1024, 600
#set terminal dumb
set title "Demon Internet HomeOffice 8000, hostname: waldo"
set lmargin 10
set rmargin 10
set tmargin 5
set bmargin 8
set xdata time
set format x "%d/%m"
set xlabel "Date, 2009" offset 0,-2
set ylabel "ADSL Status"
set yrange [-1:2]
set ytics 0,1,1 ("Off" 0, "On" 1)
set mxtics 6
set timefmt "%Y%m%d%H%M%S"
plot "/home/msw/router/ping-stats-copy.txt" using 1:2 with lines title
"Status at `date`"
#set title "ADSL status" offset 0,-10
# - generated on %Y%m%d %H%M"
show title

Then once every 1/4 hour the script pushes the stats up to my remote web host (assuming the connection is up of course!)

cp /usr/local/archive/ping-stats.jpg /tmp
cd /tmp
cd public_html
put ping-stats.jpg

The end result being this lovely image:


mondaytuesdaywednesdaythursdayfridaysaturdaysunday said...

This is a neat simple idea. I'm going to see if I can get it running on my system as my DSL has recently started dropping out numerous times a day.

One note: Vixie cron now supports simple interval arguments so you can make your crontab entries a bit more succinct:

5,10,15,20,25,30,35,40,45,50,55 * * * * /home/msw/router/


*/5 * * * * /home/msw/router/

Realm174 Uggla said...

This is pretty awesome!! I was googling for something similar, came across your page. I'm fairly noob to Linux, I have some basic working knowledge, but your step by step made everything crystal clear. I even modified it successfully (after a few attempts) to fit my needs. Thank you.