#!/bin/bash -e datadir=/usr/local/rrd png=/home/thomas/public_html hosts="0.0.0.0/0 192.168.0.50/32 192.168.0.51/32 192.168.0.52/32 192.168.0.53/32 192.168.0.54/32 192.168.0.55/32 192.168.0.56/32 192.168.0.57/32 192.168.0.58/32 192.168.0.59/32 192.168.0.60/32" ports="0 80 873" month="$(( 31 * 24 ))" year="$(( 365 * 24 ))" #All durations calculated to hours # 6 hours, 1 day durations="6 24" # 1 week, 1 month, 3 months durations="$durations $(( 7 * 24 )) $month $(( 3 * $month ))" # 6 months, 1 year, 2 years, durations="$durations $(( 6 * $month )) $year $(( 2 * $year))" # 3 years, 6 years, 12 years durations="$durations $(( 3 * $year )) $(( 6 * $year )) $(( 12 * $year ))" case "$1" in create) step=300 for host in $hosts; do name=`echo $host | awk -F'/' '{print $1}'` echo -n "Creating $datadir/$name.rrd ..." rrdtool create $datadir/$name.rrd --step $step \ $( for port in $ports; do echo -n "DS:in$port:ABSOLUTE:" echo "$(( $step * 2 )):0:U " echo -n "DS:out$port:ABSOLUTE:" echo "$(( $step * 2 )):0:U " done ) \ $( for duration in $durations; do adjdur=$(( $duration * 60 * 60 / $step )) echo "RRA:AVERAGE:0.5:1:$adjdur " echo "RRA:MAX:0.5:1:$adjdur " done ) echo "done" done ;; rules) iptables -N tpac_in || true iptables -I INPUT 1 -j tpac_in || true iptables -I FORWARD 1 -j tpac_in || true iptables -N tpac_out || true iptables -I OUTPUT 1 -j tpac_out || true iptables -I FORWARD 1 -j tpac_out || true iptables -F tpac_in || true iptables -F tpac_out || true for host in $hosts; do for port in $ports; do if [ $port -eq 0 ]; then iptables -A tpac_in -i eth1 -d $host || true iptables -A tpac_out -o eth1 -s $host || true else iptables -A tpac_in -i eth1 -d $host \ -p tcp --sport $port || true iptables -A tpac_out -o eth1 -s $host \ -p tcp --dport $port || true fi done done ;; update) tpac_in=`mktemp` tpac_out=`mktemp` iptables -n -v -x --line-numbers -Z -L tpac_in > $tpac_in iptables -n -v -x --line-numbers -Z -L tpac_out > $tpac_out rule=1 for host in $hosts; do name=`echo $host | awk -F'/' '{print $1}'` data="N" for port in $ports; do in=`cat $tpac_in | grep "^$rule " | awk '{print $3}'` out=`cat $tpac_out | grep "^$rule " | awk '{print $3}'` data="${data}:$in:$out" rule=$(( $rule + 1 )) done rrdtool update $datadir/$name.rrd $data done rm $tpac_in $tpac_out ;; graph) for host in $hosts; do host=`echo $host | awk -F'/' '{print $1}'` hostres=$( host $host 127.0.0.1 | tail -1 | \ grep "domain name pointer" | \ awk '{print $5}' | cut -d. -f1 ) || true if [ "$host" == "0.0.0.0" ]; then title=Total elif [ `echo $hostres | wc -l` -gt 0 ]; then title="$hostres ($host)" else title=$host fi #for duration in $durations; do for duration in 6 24 168 744 2232 4464; do seconds=$(( $duration * 60 * 60 )) test $duration = 6 && fduration="6 Hours" test $duration = 24 && fduration="1 Day" test $duration = 168 && fduration="1 Week" test $duration = 744 && fduration="1 Month" test $duration = 2232 && fduration="3 Months" test $duration = 4464 && fduration="6 Months" test $duration = 8760 && fduration="1 Year" test $duration = 17520 && fduration="2 Years" test $duration = 26280 && fduration="3 Years" test $duration = 52560 && fduration="6 Years" test $duration = 105120 && fduration="12 Years" rrdtool graph $png/$host-$duration.png \ --imgformat=PNG \ --title "$title Traffic for $fduration" \ --start=-${duration}hours \ --vertical-label="Bytes/sec" \ --base 1024 -h 174 -w 503 \ $( for port in $ports; do echo -n "DEF:in$port=$datadir/" echo "$host.rrd:in$port:AVERAGE" echo -n "DEF:out$port=$datadir/" echo "$host.rrd:out$port:AVERAGE" done ) \ $( for port in $ports; do echo -n "CDEF:neg_out$port=" echo "out$port,-1,*" echo -n "CDEF:total_in$port=" echo -n "in$port,UN,0,in$port," echo "IF,$seconds,*" echo -n "CDEF:total_out$port=" echo -n "out$port,UN,0,out$port," echo "IF,$seconds,*" done ) \ "COMMENT:Service " \ "COMMENT:Max Avg Cur Total" \ "COMMENT: " \ "COMMENT:Max Avg Cur Total" \ $( for port in $ports; do if [ $port -eq 0 ]; then incolor=00FF00 outcolor=0000FF service=all-- elif [ $port -eq 80 ]; then incolor=00AA00 outcolor=0000AA service=web-- elif [ $port -eq 873 ]; then incolor=005500 outcolor=000055 service=rsync fi echo "COMMENT:\n" echo "COMMENT:$service" echo -n "AREA:in$port#$incolor:" echo "In" echo "LINE1:in$port#000000" echo -n "GPRINT:in$port:MAX:" echo "%3.lf%s" echo -n "GPRINT:in$port:AVERAGE:" echo "%3.lf%S" echo -n "GPRINT:in$port:LAST:" echo "%3.lf%S" echo -n "GPRINT:total_in$port:AVERAGE:" echo "%7.2lf%sB" echo -n "AREA:neg_out$port#$outcolor:" echo "Out" echo "LINE1:neg_out$port#000000" echo -n "GPRINT:out$port:MAX:" echo "%3.lf%s" echo -n "GPRINT:out$port:AVERAGE:" echo "%3.lf%S" echo -n "GPRINT:out$port:LAST:" echo "%3.lf%S" echo -n "GPRINT:total_out$port:AVERAGE:" echo "%7.2lf%sB" done ) \ HRULE:$((750*1024/8))#000000 \ HRULE:-$((128*1024/8))#000000 \ HRULE:0#000000 \ &> /dev/null done done ;; #echo "AREA:in$port#$incolor:In($port)" #echo "LINE1:in$port#000000" #echo "GPRINT:in$port:MAX:Max\:%3.lf%s" #echo -n "GPRINT:in$port:AVERAGE:" #echo "Avg\:%3.lf%S" #echo -n "GPRINT:in$port:LAST:" #echo "Current\:%3.lf%S" #echo -n "GPRINT:total_in$port:AVERAGE:" #echo "Total\:%7.2lf%sB" #echo -n "AREA:neg_out$port#$outcolor:" #echo "Out($port)" #echo "LINE1:neg_out$port#000000" #echo "GPRINT:out$port:MAX:Max\:%3.lf%s" #echo -n "GPRINT:out$port:AVERAGE:" #echo "Avg\:%3.lf%S" #echo -n "GPRINT:out$port:LAST:" #echo "Current\:%3.lf%S" #echo -n "GPRINT:total_out$port:AVERAGE:" #echo "Total\:%7.2lf%sB\n" html) echo "traffic" for host in $hosts; do host=`echo $host | awk -F'/' '{print $1}'` echo "" for duration in $durations; do echo "" done echo "" done echo "
" ;; *) echo "tpac (create|rules|update|graph|html)" exit ;; esac