#!/bin/sh
#
# macsat RRDTool script for ASUS WL-500g (Deluxe) v1.1 (18-07-2005)
#
# (c) 2005 by macsat@macsat.com
# Feel free to distribute and improve :-)
#
#
# Configuration Start
# Directory for storing RRD Databases
RRDDATA=/usr/local/src/rrd/
# Directory for storing webpages / images
RRDIMG=/var/www/html/rrd/
# Configuration End
#Output date for log...
date
if [ ! -d "${RRDDATA}" ]
then
echo "RRD Database dir: $RRDDATA does not exist...Creating Now...."
mkdir -p "${RRDDATA}"
fi
if [ ! -d "${RRDIMG}" ]
then
echo "RRD Image / web dir: $RRDIMG does not exist....Creating Now...."
mkdir -p "${RRDIMG}"
fi
# $1 = html file $2 = Period
CreateHTML ()
{
echo "<HTML><HEAD><TITLE>豐田大郡網路流量 </TITLE></HEAD><BODY> <h3>網路介面流量圖 - ${2} </h3>" > "${1}"
echo "<center><a href='index.html'>每日流量</a> | <a href='week.html'> 週流量 </a> | <a href='month.html'> 月流量 </a> | <a href='year.html'> 年流量 </a>" >> "${1}"
echo "<br><br><img src='wan${2}ppp0.png'><br><img src='wan${2}ppp1.png'><br><img src='wan${2}ppp2.png'><br><img src='wan${2}ppp3.png'><br><img src='wan${2}ppp4.png'><br><img src='wan${2}ppp5.png'><br><img src='lan${2}eth1.png'><br><br></CENTER></BODY></HTML>" >> "${1}"
echo "Create started..."
}
if [ ! -f "${RRDIMG}/index.html" ]
then
echo " index.html does not exist.....Creating Now...."
CreateHTML "${RRDIMG}/index.html" day
fi
if [ ! -f "${RRDIMG}/week.html" ]
then
echo " week.html does not exist.....Creating Now...."
CreateHTML "${RRDIMG}/week.html" week
fi
if [ ! -f "${RRDIMG}/month.html" ]
then
echo " month.html does not exist.....Creating Now...."
CreateHTML "${RRDIMG}/month.html" month
fi
if [ ! -f "${RRDIMG}/year.html" ]
then
echo " year.html does not exist.....Creating Now...."
CreateHTML "${RRDIMG}/year.html" year
fi
face=`ifconfig |grep 'ppp[012345] Link' |awk '{print $1}'`
LANIF="eth1"
CreateRRD ()
{
rrdtool create "${1}" \
DS:in:DERIVE:600:0:12500000 \
DS:out:DERIVE:600:0:12500000 \
RRA:AVERAGE:0.5:1:576 \
RRA:AVERAGE:0.5:6:672 \
RRA:AVERAGE:0.5:24:732 \
RRA:AVERAGE:0.5:144:1460
}
echo "LAN Interface: ${LANIF}"
LANRRD="${RRDDATA}/${LANIF}.rrd"
if [ ! -f "${LANRRD}" ]
then
echo "RRD file : ${LANRRD} does not exist...Creating Now..."
CreateRRD "${LANRRD}"
fi
CreateGraph ()
{
rrdtool graph "${1}.new" -a PNG -s -"${2}" -w 550 -h 240 -v "bytes/s" \
'DEF:ds1='${3}':in:AVERAGE' \
'DEF:ds2='${3}':out:AVERAGE' \
'LINE1:ds1#00FF00:Incoming Traffic' \
GPRINT:ds1:MAX:"Max %6.2lf %s" \
GPRINT:ds1:MIN:"Min %6.2lf %s" \
GPRINT:ds1:AVERAGE:"Avg %6.2lf %s" \
GPRINT:ds1:LAST:"Curr %6.2lf %s\n" \
'LINE1:ds2#0000FF:Outgoing Traffic' \
GPRINT:ds2:MAX:"Max %6.2lf %s" \
GPRINT:ds2:MIN:"Min %6.2lf %s" \
GPRINT:ds2:AVERAGE:"Avg %6.2lf %s" \
GPRINT:ds2:LAST:"Curr %6.2lf %s" \
-t "${4}"
mv -f "${1}.new" "${1}"
}
LANIN=`ifconfig "${LANIF}" |grep bytes|awk -F ":" '{print $2}'|awk '{print $1}'`
LANOUT=`ifconfig "${LANIF}" |grep bytes|awk -F ":" '{print $3}'|awk '{print $1}'`
echo "LAN -> in: ${LANIN} out: ${LANOUT}"
`rrdupdate "${LANRRD}" -t in:out N:"${LANIN}":"${LANOUT}"`
MTIME=`date "+%M"`
HTIME=`date "+%H"`
if [ "${MTIME}" = 00 ] || [ "${MTIME}" = 10 ] || [ "${MTIME}" = 20 ] || [ "${MTIME}" = 30 ] || [ "${MTIME}" = 40 ] || [ "${MTIME}" = 50 ];
then
# 1 Day Graph
echo "Daily Graphs created....."
#CreateGraph "${RRDIMG}/wanday.png" 86400 "${WANRRD}" WAN
CreateGraph "${RRDIMG}/landay${LANIF}.png" 86400 "${LANRRD}" LAN${LANIF}
#CreateGraph "${RRDIMG}/wlanday.png" 86400 "${WLANRRD}" WLAN
fi
# Update Weekly graph once an hour
if [ "${MTIME}" = 00 ];
then
# 1 Week Graph
echo "Weekly Graphs created....."
#CreateGraph "${RRDIMG}/wanweek.png" 604800 "${WANRRD}" WAN
CreateGraph "${RRDIMG}/lanweek${LANIF}.png" 604800 "${LANRRD}" LAN${LANIF}
#CreateGraph "${RRDIMG}/wlanweek.png" 604800 "${WLANRRD}" WLAN
fi
# Update Monthly and Yearly graphs once a day (maby twice a day on 12h settings)
if [ "${HTIME}" = 04 ] && [ "${MTIME}" = 15 ];
then
# 1 Month Graph
echo "Monthly Graphs Created...."
#CreateGraph "${RRDIMG}/wanmonth.png" 2678400 "${WANRRD}" WAN
CreateGraph "${RRDIMG}/lanmonth${LANIF}.png" 2678400 "${LANRRD}" LAN${LANIF}
#CreateGraph "${RRDIMG}/wlanmonth.png" 2678400 "${WLANRRD}" WLAN
# 1 Year Graph
echo "Yearly Graphs Created...."
#CreateGraph "${RRDIMG}/wanyear.png" 31536000 "${WANRRD}" WAN
CreateGraph "${RRDIMG}/lanyear${LANIF}.png" 31536000 "${LANRRD}" LAN${LANIF}
#CreateGraph "${RRDIMG}/wlanyear.png" 31536000 "${WLANRRD}" WLAN
fi
echo " <------------------------------------------------------------->"
echo " "
#WLANIF=`nvram get wl0_ifname`
for WANIF in $face
do
#debug lines
echo "WAN Interface: ${WANIF}"
#echo "WLAN Interface: ${WLANIF}"
WANRRD="${RRDDATA}/${WANIF}.rrd"
#WLANRRD="${RRDDATA}/${WLANIF}.rrd"
if [ ! -f "${WANRRD}" ]
then
echo "RRD file : ${WANRRD} does not exist...Creating Now..."
CreateRRD "${WANRRD}"
fi
#if [ ! -f "${WLANRRD}" ]
# then
# echo "RRD file : ${WLANRRD} does not exist...Creating Now..."
# CreateRRD "${WLANRRD}"
#fi
WANIN=`ifconfig "${WANIF}" |grep bytes|awk -F ":" '{print $2}'|awk '{print $1}'`
WANOUT=`ifconfig "${WANIF}" |grep bytes|awk -F ":" '{print $3}'|awk '{print $1}'`
#WLANIN=`ifconfig "${WLANIF}" |grep bytes|awk -F ":" '{print $2}'|awk '{print $1}'`
#WLANOUT=`ifconfig "${WLANIF}" |grep bytes|awk -F ":" '{print $3}'|awk '{print $1}'`
# Debug
echo "WAN -> in: ${WANIN} out: ${WANOUT} "
#echo "WLAN -> in: ${WLANIN} out: ${WLANOUT}"
# Update the Databases
`rrdupdate "${WANRRD}" -t in:out N:"${WANIN}":"${WANOUT}"`
#`rrdupdate "${WLANRRD}" -t in:out N:"${WLANIN}":"${WLANOUT}"`
# $1 = ImageFile , $2 = Time in secs to go back , $3 = RRDfil , $4 = GraphText
# Set time-variables
MTIME=`date "+%M"`
HTIME=`date "+%H"`
# Update Daily graphs every 10 mins
if [ "${MTIME}" = 00 ] || [ "${MTIME}" = 10 ] || [ "${MTIME}" = 20 ] || [ "${MTIME}" = 30 ] || [ "${MTIME}" = 40 ] || [ "${MTIME}" = 50 ];
then
# 1 Day Graph
echo "Daily Graphs created....."
CreateGraph "${RRDIMG}/wanday${WANIF}.png" 86400 "${WANRRD}" WAN${WANIF}
#CreateGraph "${RRDIMG}/landay.png" 86400 "${LANRRD}" LAN
#CreateGraph "${RRDIMG}/wlanday.png" 86400 "${WLANRRD}" WLAN
fi
# Update Weekly graph once an hour
if [ "${MTIME}" = 00 ];
then
# 1 Week Graph
echo "Weekly Graphs created....."
CreateGraph "${RRDIMG}/wanweek${WANIF}.png" 604800 "${WANRRD}" WAN${WANIF}
#CreateGraph "${RRDIMG}/lanweek.png" 604800 "${LANRRD}" LAN
#CreateGraph "${RRDIMG}/wlanweek.png" 604800 "${WLANRRD}" WLAN
fi
# Update Monthly and Yearly graphs once a day (maby twice a day on 12h settings)
if [ "${HTIME}" = 04 ] && [ "${MTIME}" = 15 ];
then
# 1 Month Graph
echo "Monthly Graphs Created...."
CreateGraph "${RRDIMG}/wanmonth${WANIF}.png" 2678400 "${WANRRD}" WAN${WANIF}
#CreateGraph "${RRDIMG}/lanmonth.png" 2678400 "${LANRRD}" LAN
#CreateGraph "${RRDIMG}/wlanmonth.png" 2678400 "${WLANRRD}" WLAN
# 1 Year Graph
echo "Yearly Graphs Created...."
CreateGraph "${RRDIMG}/wanyear${WANIF}.png" 31536000 "${WANRRD}" WAN${WANIF}
#CreateGraph "${RRDIMG}/lanyear.png" 31536000 "${LANRRD}" LAN
#CreateGraph "${RRDIMG}/wlanyear.png" 31536000 "${WLANRRD}" WLAN
fi
echo " <------------------------------------------------------------->"
echo " "
done