#!/bin/sh
# 
# Get satellite link stats for the world to see.
# 
# This is derived from getdslstats, but doesn't include
# $Id: getsatstats,v 1.1 2010/12/29 00:00:37 grog Exp $

PATH=$PATH:/usr/local/bin
BIN=/home/grog/bin
HTML=/home/grog/public_html/net

# Calculate time zone offset from UTC
# First get current time.  We need this mainly because of DST.
NOW=`date +%G%m%d%H%M.%S`
# Convert to seconds at UTC
UTC=`TZ=GMT date -j $NOW +%s`
# And in local time zone
LOCAL=`date -j $NOW +%s`
# The difference is the time zone offset.
TZOFFSET=`expr $UTC - $LOCAL`
# Number of seconds between UNIX Epoch and gnuplot Y2K base.
Y2K=946684800

NOW=`date +%s`
db=`grep db adslstats.html |sed 's:.*<B>::;  s: db.*::'`
set $db
downm=$1
if expr $downm \> 1000 >/dev/null; then
  downm=`expr $downm - 2147483648`
fi
upm=$2
kbps=`grep Kbps adslstats.html |sed 's:.*<B>::;  s: Kbps.*::'`
echo $NOW $downm $upm $kbps >> ~/public_html/net/dslstats

# copy today's and yesterday's data to a temporary file
# These files get rolled over at midnight by the weekly job.
NETDIR=/home/grog/public_html/net/
cat $NETDIR/dslstats.0 $NETDIR/dslstats > /var/tmp/dslstats

# same for the link status
cat $NETDIR/linkstats.0 $NETDIR/linkstats > /var/tmp/linkstats

# Now plot results
# Last 4 hours
NOW=`date +%s`
GNUNOW=`expr $NOW - $Y2K`
START=`expr $GNUNOW - 14400`

( echo "set xrange [$START+$TZOFFSET:$GNUNOW+$TZOFFSET]"; \
	sed < $BIN/dslplot "s:TZOFFSET:$TZOFFSET:; s:RANGE:last 4 hours:; s:UN:un:; s:SMOOTH::; s:with lines:with steps:; s:SIZE:540,360:") \
	| gnuplot > $HTML/dslstats-4-hours.png
( echo "set xrange [$START+$TZOFFSET:$GNUNOW+$TZOFFSET]"; \
	sed < $BIN/dslplot "s:TZOFFSET:$TZOFFSET:; s:RANGE:last 4 hours:; s:UN:un:; s:SMOOTH::; s:with lines:with steps:; s:SIZE:1600,1200:") \
	| gnuplot > $HTML/dslstats-4-hours.big.png
( echo "set xrange [$START+$TZOFFSET:$GNUNOW+$TZOFFSET]"; \
	sed < $BIN/dslplot "s:TZOFFSET:$TZOFFSET:; s:RANGE:last 4 hours:; s:UN::; s:SMOOTH: smooth bezier:; s:SIZE:540,360:") \
	| gnuplot > $HTML/dslstats-4-hours-smoothed.png
( echo "set xrange [$START+$TZOFFSET:$GNUNOW+$TZOFFSET]"; \
	sed < $BIN/dslplot "s:TZOFFSET:$TZOFFSET:; s:RANGE:last 4 hours:; s:UN::; s:SMOOTH: smooth bezier:; s:SIZE:1600,1200:") \
	| gnuplot > $HTML/dslstats-4-hours-smoothed.big.png

# Last 24 hours
NOW=`date +%s`
GNUNOW=`expr $NOW - $Y2K`
START=`expr $GNUNOW - 86400`
( echo "set xrange [$START+$TZOFFSET:$GNUNOW+$TZOFFSET]"; \
	sed < $BIN/dslplot "s:TZOFFSET:$TZOFFSET:; s:RANGE:last 24 hours:; s:UN:un:; s:SMOOTH::; s:with lines:with steps:; s:SIZE:540,360:") \
	| gnuplot > $HTML/dslstats-24-hours.png
( echo "set xrange [$START+$TZOFFSET:$GNUNOW+$TZOFFSET]"; \
	sed < $BIN/dslplot "s:TZOFFSET:$TZOFFSET:; s:RANGE:last 24 hours:; s:UN:un:; s:SMOOTH::; s:with lines:with steps:; s:SIZE:1600,1200:") \
	| gnuplot > $HTML/dslstats-24-hours.big.png
( echo "set xrange [$START+$TZOFFSET:$GNUNOW+$TZOFFSET]"; \
	sed < $BIN/dslplot "s:TZOFFSET:$TZOFFSET:; s:RANGE:last 24 hours:; s:UN::; s:SMOOTH: smooth bezier:; s:SIZE:540,360:") \
	| gnuplot > $HTML/dslstats-24-hours-smoothed.png
( echo "set xrange [$START+$TZOFFSET:$GNUNOW+$TZOFFSET]"; \
	sed < $BIN/dslplot "s:TZOFFSET:$TZOFFSET:; s:RANGE:last 24 hours:; s:UN::; s:SMOOTH: smooth bezier:; s:SIZE:1600,1200:") \
	| gnuplot > $HTML/dslstats-24-hours-smoothed.big.png

