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
SHELL=/bin/sh
MAILTO=msw
@hourly /home/msw/router/getstats
5,10,15,20,25,30,35,40,45,50,55 * * * * /home/msw/router/ping-internet.sh
5,10,15,20,25,30,35,40,45,50,55 * * * * /home/msw/router/plot-stats.sh
0,15,30,45 * * * * /home/msw/router/upload-stats.sh

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

#!/usr/pkg/bin/bash
LOGFILE=/home/msw/router/ping-stats.txt
TIME="`date +%C%y%m%d%H%M`"
/sbin/ping -c 1 158.152.1.58 > /dev/null 2>&1
if [ "$?" == "0" ]
then
STATUS=1
else
STATUS=0
fi
echo "$TIME $STATUS" >> $LOGFILE

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 plot-stats.sh script turns these numbers into a nice graph with the help of GnuPlot then puts the resulting jpeg onto the local apache webserver:

#!/usr/pkg/bin/bash
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
cp $SHARE_DIR/$JPG_FILE $HTTPD_DIR

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 upload-stats.sh script pushes the stats up to my remote web host (assuming the connection is up of course!)

#!/usr/pkg/bin/bash
cp /usr/local/archive/ping-stats.jpg /tmp
cd /tmp
ftp ftp://username:password@ftp.wickensonline.co.uk <<-END_OF_INPUT
bin
cd public_html
put ping-stats.jpg
dir
bye
END_OF_INPUT

The end result being this lovely image:

2 comments:

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/ping-internet.sh

becomes:

*/5 * * * * /home/msw/router/ping-internet.sh

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.