Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Simple traffic counter script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Hasw
n00b
n00b


Joined: 31 Dec 2004
Posts: 68
Location: Germany

PostPosted: Wed Jan 26, 2005 7:21 pm    Post subject: Simple traffic counter script Reply with quote

Because I needed a easy method to count the traffic between the local net and the internet I wrote a simple script.

It uses iptables and send every day a traffic report mail:
Code:

Today:
-----------
Network->Inet:  5 MB
Inet->Network:  20 MB
Local->Inet:    2 MB
Inet->Local:    6 MB
=======================
Total sent:     7 MB
Total received: 26 MB


Total traffic:
--------------
Network->Inet:  118 MB
Inet->Network:  1199 MB
Local->Inet:    13 MB
Inet->Local:    16 MB
=======================
Total sent:     131 MB
Total received: 1215 MB


traffic_report.sh:
Code:
#!/bin/sh

# Local network
LOCAL_NET="192.168.0.0/24"
# IP of inet port
EXT_IP="192.168.1.1"
# Mail receiver
MAIL="root"
# Saved traffic data
DATA="/root/traffic.dat"
# AWK file for report
AWK="/root/bin/traffic_report.awk"

IPTABLES="/sbin/iptables"
DATE=$(date +%F)
SUBJECT="Subject: Traffic report for $DATE"


# Convert iptables byte count to MB
conv2mb ()
{
   case $1 in
       *K ) return $((`echo $1 | sed 's/K//'`/1000));;
        *M ) return $(echo $1 | sed 's/M//');;
        *G ) return $((`echo $1 | sed 's/K//'`*1000));;
    esac
       
    return 0;
}

# Create traffic chain
create_chain ()
{
   iptables -L traffic >/dev/null 2>&1
   
   if [ $? != 0 ]
   then
      $IPTABLES -N traffic
      $IPTABLES -A traffic -s $LOCAL_NET -d ! $LOCAL_NET
      $IPTABLES -A traffic -s $EXT_IP
      $IPTABLES -A traffic -s ! $LOCAL_NET -d $LOCAL_NET
      $IPTABLES -A traffic -d $EXT_IP

      $IPTABLES -I INPUT -j traffic
      $IPTABLES -I OUTPUT -j traffic
      $IPTABLES -I FORWARD -j traffic
   fi
}

# Save traffic data
save_traffic ()
{
   # Local net->Inet
   conv2mb $($IPTABLES -v -L traffic | grep "$LOCAL_NET.*!$LOCAL_NET" | awk '{print $2}')
   N2I=$?
   # Inet -> local net
   conv2mb $($IPTABLES -v -L traffic | grep "\!$LOCAL_NET.*$LOCAL_NET" | awk '{print $2}')
   I2N=$?

   # Local -> Inet
   conv2mb $($IPTABLES -v -L traffic | grep "$EXT_IP.*anywhere" | awk '{print $2}')
   L2I=$?
   conv2mb $($IPTABLES -v -L traffic | grep "anywhere.*$EXT_IP" | awk '{print $2}')
   I2L=$?

   TRAFFIC_DATA="$DATE: $N2I $I2N $L2I $I2L"

   # Update traffic data
   if [ -n "$(grep $DATE $DATA)" ]
   then
      cp $DATA $DATA.tmp
      sed -e "s/^$DATE.*$/$TRAFFIC_DATA/" $DATA.tmp > $DATA
      rm $DATA.tmp
   else
      echo $TRAFFIC_DATA >> $DATA
   fi
}

# Send mail with traffic report
send_mail ()
{
   TODAY="Today:\n-----------\n$(grep $DATE $DATA | awk -f $AWK)\n"
   TOTAL="Total traffic:\n--------------\n$(awk -f $AWK $DATA)"

   echo -e "To: $MAIL\n$SUBJECT\n\n$TODAY\n\n$TOTAL" | sendmail $MAIL
}

# Main program
create_chain
save_traffic
send_mail

# Zero traffic counters
if [ "$1" = "reset" ]
then
   iptables -Z traffic
fi


traffic_report.awk:
Code:

!/#/ { total_n2i += $2; total_i2n += $3; total_l2i += $4; total_i2l += $5 };

END {
   print "Network->Inet: ",total_n2i,"MB";
   print "Inet->Network: ",total_i2n,"MB";
   print "Local->Inet:   ",total_l2i,"MB";
   print "Inet->Local:   ",total_i2l,"MB";
   print "=======================";
   print "Total sent:    ",total_n2i+total_l2i,"MB";
   print "Total received:",total_i2n+total_i2l,"MB";
}


It differentiate the traffic between local host traffic (EXT_IP) and traffic forwarded for the local network (LOCAL_NET).
It saves the collected data in a file (DATA) used for total traffic calculation.
Change the other variables to your needs.

Normally it gets called once a day (but you can also run it multiple times a day): Just run ./traffic_report.sh. The traffic counter should be reset after a day (otherwise the display for the next day is wrong), for this you can run: ./traffic_counter.sh reset

This should be put in a second script copied to /etc/cron.daily/traffic.sh:

Code:

#!/bin/sh

# Send traffic report and zero counters
/root/bin/traffic_report.sh reset
Back to top
View user's profile Send private message
ba747heavy
Apprentice
Apprentice


Joined: 11 Aug 2003
Posts: 216
Location: New Mexico, USA

PostPosted: Wed Jan 26, 2005 7:30 pm    Post subject: Reply with quote

That is a pretty awesome script. Thanks.
_________________
Fred Clausen
"leet [speak] is a cry for help from a shallow mind" - Doomwookie Jan 05
Back to top
View user's profile Send private message
ahubu
Guru
Guru


Joined: 16 Aug 2003
Posts: 400
Location: Groningen, The Netherlands

PostPosted: Sat Jan 29, 2005 3:35 am    Post subject: Reply with quote

NIce script! Thanks a bunch, I think I'll add it to my cronjobcollection :)
_________________
Anne // Light travels faster than sound. That's why people appear bright until
you hear them speak.
-Unknown
Back to top
View user's profile Send private message
latz-twn
Tux's lil' helper
Tux's lil' helper


Joined: 08 Nov 2003
Posts: 95
Location: Luxembourg

PostPosted: Sat Jan 29, 2005 4:47 am    Post subject: Reply with quote

nice script elDuderino! Really something! And it has teached me how to actually find out how much transfer has been done! I never knew that you have to use iptables for that! ... :wink:

Nice job!
_________________
LinuxCounter: #353618
Back to top
View user's profile Send private message
lodder_
Apprentice
Apprentice


Joined: 06 Nov 2004
Posts: 162
Location: Knokke-Heist, Belgium

PostPosted: Sat Jan 29, 2005 9:59 am    Post subject: Reply with quote

thx man this i a very nice script going to use it directly
_________________
delodder.be
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum