#!/bin/sh
# $Id: tcpcheck,v 1.3 2011/02/08 01:05:01 grog Exp $
#
# Check TCP network connectivity at regular intervals.
# Time interval between attempts
INTERVAL=60
if [ "$1" != "" ]; then
  LINKSTATS=$1
else
  LINKSTATS=/home/grog/public_html/net/3gtcpstats
fi
NOW=`date +%s`
# Fetch a tiny file from ozlabs and time how long it took.  If it
# comes at all, the "speed" is 4 / time; i.e. we'd expect it to come
# in about 2 seconds, which would be speed 2.  If it takes 20
# seconds, speed drops to .2, etc.
DURATION=`/usr/bin/time 2>&1 ftp -o /var/tmp/ozlabs.org http://www.lemis.com/grog/emptyfile \
| awk  '/real/ { print $1 }'`
if [ $? -ne 0 ]; then 
  # timed out: no speed at all
  # this is a kludge, but it should work
  SPEED=10000
else
  SPEED=$DURATION
fi
echo $NOW $SPEED '	#' `date` >> $LINKSTATS
