| 1 |
#!/bin/sh |
| 2 |
###################################################################### |
| 3 |
# _ _ _ _ |
| 4 |
# (_) | | | | | | |
| 5 |
# _ _ __ | |_ ___ | |_ __ _| | |
| 6 |
# | | '_ \| __/ _ \| __/ _` | | |
| 7 |
# | | |_) | || (_) | || (_| | | |
| 8 |
# |_| .__/ \__\___/ \__\__,_|_| |
| 9 |
# | | |
| 10 |
# |_| V0.3 Dec. 13, 2001 |
| 11 |
# By Antoine Megens |
| 12 |
# [email protected] |
| 13 |
# |
| 14 |
# RRD daemon script to measure total IP bandwidth usage |
| 15 |
# without the need for an SNMP daemon |
| 16 |
# |
| 17 |
# part of iptotal package for FREESCO |
| 18 |
# |
| 19 |
###################################################################### |
| 20 |
|
| 21 |
. @sysconfdir@/iptotal.cfg |
| 22 |
|
| 23 |
ARCHIVE_DONE=0 |
| 24 |
|
| 25 |
do_archive() { |
| 26 |
DAYNAME=`${DATECMD} +%d%b%Y` |
| 27 |
DOARCHIVE=0 |
| 28 |
|
| 29 |
TIMESTAMP=`${DATECMD} +%H%M` |
| 30 |
if [ ${TIMESTAMP} -gt 2358 ]; then # Just before Midnight? |
| 31 |
if [ ${ARCHIVE_DONE} = 0 ]; then # and not archived yet? |
| 32 |
ARCHIVEFILE=${ARCHIVE}/${DAYNAME}.png |
| 33 |
DOARCHIVE=1 |
| 34 |
fi |
| 35 |
fi |
| 36 |
if [ ${TIMESTAMP} -lt 0005 ]; then # Just after Midnight? |
| 37 |
ARCHIVE_DONE=0 # clear flag |
| 38 |
fi |
| 39 |
if [ ${DOARCHIVE} = 1 ]; then |
| 40 |
# |
| 41 |
# create a daily graph from the current rrd data in archive |
| 42 |
# |
| 43 |
@RRDTOOL@ graph ${ARCHIVEFILE} \ |
| 44 |
--title "Total data throughput (${DAYNAME})" \ |
| 45 |
--imgformat PNG \ |
| 46 |
--width 600 \ |
| 47 |
--height 150 \ |
| 48 |
--end 'now-60s' \ |
| 49 |
--start 'end-24h' \ |
| 50 |
DEF:in=@datadir@/iptotal.rrd:input:AVERAGE \ |
| 51 |
DEF:out=@datadir@/iptotal.rrd:output:AVERAGE \ |
| 52 |
DEF:maxin=@datadir@/iptotal.rrd:input:MAX \ |
| 53 |
DEF:maxout=@datadir@/iptotal.rrd:output:MAX \ |
| •54 |
AREA:in#009828:"In kbyte(s)" \ |
| 55 |
LINE2:out#ff0000:"Out kbyte(s)" \ |
| 56 |
LINE2:maxin#0030ff:"Max In" \ |
| 57 |
LINE2:maxout#ff8040:"Max Out" |
| 58 |
|
| 59 |
# |
| 60 |
# now remove files older than 30 days from archive |
| 61 |
# |
| 62 |
${ROOT_DIR}/bin/find ${ARCHIVE}/*.png +30 -type f exec rm -rf {} \; |
| 63 |
|
| 64 |
ARCHIVE_DONE=1 |
| 65 |
fi |
| 66 |
} |
| 67 |
|
| 68 |
###################################################################### |
| 69 |
# |
| 70 |
# go to work.... |
| 71 |
# |
| 72 |
if [ ! -f $ETH_DBASE ]; then |
| 73 |
|
| 74 |
echo Creating new RRD database... |
| 75 |
|
| 76 |
@RRDTOOL@ create $ETH_DBASE \ |
| 77 |
--step ${INTERVAL} \ |
| 78 |
DS:total:GAUGE:120:0:10000 \ |
| 79 |
DS:input:GAUGE:120:0:10000 \ |
| 80 |
DS:output:GAUGE:120:0:10000 \ |
| 81 |
RRA:AVERAGE:0.5:1:1440 \ |
| 82 |
RRA:AVERAGE:0.5:60:1200 \ |
| 83 |
RRA:MIN:0.5:60:1200 \ |
| 84 |
RRA:MAX:0.5:60:1200 \ |
| 85 |
RRA:AVERAGE:0.5:1440:365 \ |
| 86 |
RRA:MIN:0.5:1440:365 \ |
| 87 |
RRA:MAX:0.5:1440:365 |
| 88 |
|
| 89 |
# make it readable for user nobody (HTTP user) |
| •90 |
chown www-data:www-data $ETH_DBASE |
| 91 |
fi |
| 92 |
|
| 93 |
# |
| 94 |
# collect data, but only if.. |
| 95 |
# |
| 96 |
if [ -f $ETH_DBASE ]; then |
| 97 |
# |
| 98 |
# the database exists, and.. |
| 99 |
# |
| 100 |
if [ -f ${ROOT_DIR}/sbin/iptotal ]; then |
| 101 |
# |
| 102 |
# iptotal is installed... |
| 103 |
|
| 104 |
#-------------------------------------------------- |
| 105 |
# save PID of this endless loop, so we can stop it |
| 106 |
#-------------------------------------------------- |
| 107 |
echo $$ > /var/run/iptotal.pid |
| 108 |
|
| 109 |
while true |
| 110 |
do |
| 111 |
#------------------------------------------------- |
| 112 |
# run iptotal and get output in TOTAL |
| 113 |
#------------------------------------------------- |
| 114 |
RESULT=`${ROOT_DIR}/sbin/iptotal -r ${INTERVAL} ${ETHDEV}` |
| 115 |
|
| 116 |
total=0; |
| 117 |
input=0; |
| 118 |
output=0; |
| 119 |
|
| 120 |
set $RESULT |
| 121 |
# |
| 122 |
# $1 $2 $3 $4 $5 $6 $7 $8 |
| •123 |
#Total: 0 kbyte(s) In: 0 kbyte(s) Out: 0 kbyte(s) |
| 124 |
#-------------------------------------------------- |
| 125 |
# we need 2nd, 5th and 8th element of the output |
| 126 |
#-------------------------------------------------- |
| 127 |
if [ ! -z "$2" ]; then |
| 128 |
total=$2 |
| 129 |
fi |
| 130 |
if [ ! -z "$5" ]; then |
| 131 |
input=$5 |
| 132 |
fi |
| 133 |
if [ ! -z "$8" ]; then |
| 134 |
output=$8 |
| 135 |
fi |
| 136 |
|
| 137 |
# |
| 138 |
# show result in text file for web page to include |
| 139 |
# |
| •140 |
if [ ! -f @datadir@/result.txt ]; then |
| 141 |
touch @datadir@/result.txt |
| 142 |
chown www-data:www-data @datadir@/result.txt |
| 143 |
fi |
| 144 |
echo $RESULT > @datadir@/result.txt |
| 145 |
|
| 146 |
#--------------------------- |
| 147 |
# update the database |
| 148 |
#--------------------------- |
| 149 |
@RRDTOOL@ update $ETH_DBASE \ |
| 150 |
--template total:input:output \ |
| 151 |
N:$total:$input:$output |
| 152 |
|
| 153 |
# |
| 154 |
# check if daily graph should be created in archive |
| 155 |
# |
| 156 |
do_archive |
| 157 |
done |
| 158 |
fi |
| 159 |
fi |