Main Page
From Trafmetux
Trafmetux: Traffic Meter for Linux
[edit] About trafmetux
Trafmetux is an Open-Source network Traffic Meter for Linux. It tells you how much IP traffic has been seen by the linux kernel module during the last few minutes, hours, days, weeks, months. It's very useful when you have a limited internet connection with quotas. For instance, many mobile broadband ISP provide only 3GB/month. In that case, you need to know how many Megabytes you already used. Trafmetux is released under the GPL-v2 license.
Here are example of statistics that it can provide (three monitors were used: input, output, total):
[edit] traffic per hour
# trafmetux -t 5 show-hourly input output total 2008-10-27 19:00-20:00: [input= 24.21 MB] [output= 3.37 MB] [total= 27.58 MB] 2008-10-27 20:00-21:00: [input= 16.65 MB] [output= 10.43 MB] [total= 27.08 MB] 2008-10-27 21:00-22:00: [input= 3.21 MB] [output= 823.61 KB] [total= 4.01 MB] 2008-10-27 22:00-23:00: [input= 10.30 MB] [output= 1.82 MB] [total= 12.12 MB] 2008-10-27 23:00-00:00: [input= 1.89 MB] [output= 494.08 KB] [total= 2.37 MB]
[edit] traffic per day
# trafmetux -t 5 show-daily input output total 2008-10-21: [input= 98.83 MB] [output= 16.46 MB] [total= 115.29 MB] 2008-10-22: [input= 100.27 MB] [output= 34.19 MB] [total= 134.46 MB] 2008-10-23: [input= 40.27 MB] [output= 5.39 MB] [total= 45.66 MB] 2008-10-24: [input= 542.83 MB] [output= 50.77 MB] [total= 593.60 MB] 2008-10-25: [input= 107.99 MB] [output= 93.05 MB] [total= 201.04 MB]
[edit] traffic per week
# trafmetux -t 2 show-weekly input output total 2008-10-19 - 2008-10-25: [input= 1.24 GB] [output= 245.86 MB] [total= 1.48 GB] 2008-10-26 - 2008-11-01: [input= 1.33 GB] [output= 625.27 MB] [total= 1.89 GB]
[edit] Requirements
Trafmetux has been tested on Linux-2.6.25 with iptables-1.4.1. Anyway, it should work with all recent linux-2.6 kernels, and with all iptables-1.4.x. The sources of the kernel have to be installed on your system so that the kernel module can be compiled.
[edit] Download the latest sources
You can download the current sources on the sourceforge project page:
[edit] ChangesLog
- 0.1.8 (2008-11-02):
- fixed a bug in the user space programs with the summer-time/winter-time change.
- 0.1.7 (2008-10-25):
- fixed a bug in the way the units are displayed (when size > 1GB)
- provide a kernel patch to compile trafmetux in the kernel (not as a module)
- 0.1.6 (2008-10-20):
- fixed compilation problem with the kernel module on 32bits linux os
- 0.1.5 (2008-10-19):
- optimized the code that reads the statistics again
- 0.1.4 (2008-10-19):
- using dynamic memory allocation in the kernel module to save memory
- fixed the destruction of the monitor objects when we remove a rule
- optimized the code that reads the statistics so that do_show() is faster
- 0.1.3 (2008-10-18):
- The stats stored in /var/lib/trafmetux are now splitted in daily log files
- Added option '-f day-of-week' to choose which day is the first day of the weeks
- Added option '-u unit' to choose thedisplay unit (KiB, MiB, GiB, ...)
- 0.1.2 (2008-10-16):
- First public release
[edit] How to install trafmetux
[edit] 1) compile and install iptables-1.4.x patched for trafmetux
- tar xfj /path/to/iptables-1.4.2.tar.bz2
- cd iptables-1.4.2
- cat /path/to/trafmetux-0.1.8/iptables-patch/iptables-1.4.2-trafmetux-0.1.8.patch | patch -p1
- ./configure && make && make install
[edit] 2) compile and install the trafmetux kernel support
There are two options to add support for trafmetux in your kernel. Just use one of them (either 2a or 2b):
[edit] 2a) compile and install the kernel module (xt_TRAFMETUX.ko)
This is the most common method, it's easier since you can keep your current kernel, but you need the development tools (gcc, make, ...) and your kernel sources must be installed so that the compilation works.
# cd /path/to/trafmetux-0.1.8/kernel-module # make # mkdir -p /lib/modules/$(uname -r)/net/ # cp xt_TRAFMETUX.ko /lib/modules/$(uname -r)/net/ # depmod -ae
[edit] 2b) patch the kernel sources and compile your own kernel
This is the best option if you want to have your own kernel or if you don't want any kernel module to be installed on your system.
# cd /usr/src
# tar xfjp /path/to/linux-2.6.xx.tar.bz2
# cd /usr/src/linux-2.6.xx
# cat /path/to/trafmetux-0.1.8/kernel-patch/linux-2.6-trafmetux-0.1.8.patch | patch -p1
# execute the interface to choose the options with either "make menuconfig" or "make xconfig"
# say Y or M to CONFIG_NETFILTER_XT_TARGET_TRAFMETUX ('Y' to build it with the kernel image)
# save your kernel options
# compile the kernel with "make && make modules_install"
[edit] 3) compile and install the user-space program (trafmetux)
- cd /path/to/trafmetux-0.1.8/trafmetux-src
- make
- cp trafmetux /usr/sbin/trafmetux
[edit] 4) setup iptables to use the new netfilter target
You must use a netfilter configuration that use the TRAFMETUX target to take into account all the packets that hits these rules. Here is how we can create several monitors: one for incoming packets on wlan0, another one for the outgoing packets from wlan0, another one for the total (in+out), the fourth one for all the ssh/scp traffic through eth0, and the fifth one monitors all the traffic to a specific ip address.
# ---- flush all rules iptables -t mangle -F iptables -t mangle -X # ---- load the trafmetux module grep xt_TRAFMETUX /proc/modules && rmmod xt_TRAFMETUX modprobe xt_TRAFMETUX # ---- first monitor: increase stats in 'input' for all incoming tcp packets on wlan0 iptables -t mangle -A PREROUTING -i wlan0 -p tcp -j TRAFMETUX --monitor input # ---- second monitor: increase stats in 'output' for all outgoing tcp packets on wlan0 iptables -t mangle -A POSTROUTING -o wlan0 -p tcp -j TRAFMETUX --monitor output # ---- third monitor: increase stats in 'inout' for all tcp packets on wlan0 in both ways iptables -t mangle -A PREROUTING -i wlan0 -p tcp -j TRAFMETUX --monitor inout iptables -t mangle -A POSTROUTING -o wlan0 -p tcp -j TRAFMETUX --monitor inout # ---- fourth monitor: show all the ssh/scp traffic that goes through eth0 iptables -t mangle -A PREROUTING -i eth0 -p tcp --sport 22 -j TRAFMETUX --monitor ssh iptables -t mangle -A POSTROUTING -o eth0 -p tcp --dport 22 -j TRAFMETUX --monitor ssh # ---- fifth monitor: show all the traffic from/to my private server iptables -t mangle -A PREROUTING -i eth0 -s 123.123.123.123 -j TRAFMETUX --monitor myserver iptables -t mangle -A POSTROUTING -o eth0 -d 123.123.123.123 -j TRAFMETUX --monitor myserver
[edit] 5) run "/usr/sbin/trafmetux update <monitor>" from cron every minute
Here is an example of crontab with five trafmetux monitors.
* * * * * /usr/sbin/trafmetux update input * * * * * /usr/sbin/trafmetux update output * * * * * /usr/sbin/trafmetux update inout * * * * * /usr/sbin/trafmetux update ssh * * * * * /usr/sbin/trafmetux update myserver
You can see that trafmetux update creates daily per-monitor statistics files:
# ls -l /var/lib/trafmetux/*2008-10-27 -rw-r--r-- 1 root root 93 2008-10-27 22:46 /var/lib/trafmetux/http-2008-10-27 -rw-r--r-- 1 root root 5084 2008-10-27 22:46 /var/lib/trafmetux/input-2008-10-27 -rw-r--r-- 1 root root 5577 2008-10-27 22:46 /var/lib/trafmetux/output-2008-10-27 -rw-r--r-- 1 root root 23 2008-10-27 22:46 /var/lib/trafmetux/ssh-2008-10-27 -rw-r--r-- 1 root root 5723 2008-10-27 22:46 /var/lib/trafmetux/myserver-2008-10-27
It will use the statistics created in /proc/trafmetux/ by the kernel module to generate long term statistics in /var/lib/trafmetux/
[edit] 6) see the statistics about the traffic
You can get hourly / daily / weeky / monthly / total statistics.
# trafmetux -t 3 show-hourly input output inout 2008-10-12 20:00-21:00: [input= 49.92 MB] [output= 19.38 MB] [inout= 69.30 MB] 2008-10-12 21:00-22:00: [input= 13.43 MB] [output= 5.00 MB] [inout= 18.43 MB] 2008-10-12 22:00-23:00: [input= 2.41 MB] [output= 377.01 KB] [inout= 2.77 MB]
You can use option "-t" to have only recent statistics. Here is how you can see the statistics about the last two days:
# trafmetux -t 2 show-daily input output inout 2008-10-13: [input= 149.75 MB] [output= 24.77 MB] [inout= 174.52 MB] 2008-10-14: [input= 41.58 MB] [output= 7.74 MB] [inout= 49.32 MB]
[edit] Contact
You can contact the author. The email address is protected by an anti-spam system.
