Open main menu

OpenVZ Virtuozzo Containers Wiki β

Changes

Traffic accounting with iptables

312 bytes added, 09:28, 15 December 2011
m
Reverted edits by TermPaperServices (talk) to last revision by Kir
Suppose you need to know how much traffic your [[VEcontainer]]s eat. It can be easily done
using iptables.
== Situation description ==
Let's consider the very simple situation: one VE container with one IP address on the [[Hardware Node]]with only one network interface. To be more exact, assume that [[VEcontainer]] ID is <tt>200</tt>, the IP address of the [[HN]]is <tt>192.168.0.56</tt>, the network interface name is <tt>eth0</tt>, and the IP address of the [[VEcontainer]] is <tt>192.168.0.117</tt>.
You wish to know how many bytes VE container 200 eats. One more assumption is that there are no iptables rules
on HN now. All these assumption are only for clarity!
== Solution ==
Almost any traffic that goes to and from a VE container can be catched by FORWARD chain of iptables module in [[VE0container0]],
thus we add such rules:
<pre>
</pre>
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.
To obtain current traffic usage of VE container you can issue the command:
<pre>
# iptables -nv -L FORWARD
15 1052 all -- * * 0.0.0.0/0 192.168.0.117
</pre>
'''Bytes''' column is the column we need. It's worth saying, that restarting a VE container doesn't affect accounting,
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.
So it is recommended to
You will get the exact value of the packet and byte counters, instead of only the rounded number in K’s (multiples of 1000) M’s (multiples of 1000K) or G’s (multiples of 1000M).
As is easy to see, it's not per-VE container statistic, but rather per-IP statistic. Thus you must be carefulthen changing VE container IP addresses, otherwise you'll get mess of results.
By saying ''almost any traffic'' I mean that traffic between a [[VEcontainer]] and [[VE0container0]] is not accounted by rules above.
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:
<pre>
more complicated situations.
; More than one VE container on the node: Just add the rules like above for each VE container IP.
; More than one IP per VEcontainer.: For each IP add the rules like above. When counting the complete traffic of a VE container you have to summarize over all IPs that this VE container owns.
; More interfaces on the HN.
Here are some scripting ideas
=== Get CTIDs of all running VEs containers ===
<pre>
host2:~/bin# cat vz-all-running
</pre>
=== Get all IPs of running VEs containers ===
<pre>
host2:~/bin# cat vz-all-running-ip
=== Generate a traffic.log ===
Please use crontab to run this script once per hour or day to collect your traffic statistics.
 
(Warning, the counters can overflow if there is too much traffic within that period. Would recommend 15 minute intervals if you expect a lot of traffic)
<pre>
host2:~/bin# cat vz-generate-traffic-log
# reset the counter
iptables -Z
# update the iptables rules if there is a any change in VEscontainers
./vz-iptables-create-rules
$MySQL_Passw="INSERT-YOUR-MYSQL-PASSWORD-HERE";
mysql_connect("$MySQL_Host","$MySQL_User","$MySQL_Passw");
$HN=trim(addslashes($_GET["HN"])); // Hardware Node
$handle = fopen ("tmp/{$HN}-traffic","r");
while (!feof($handle)) {
$line = fgets($handle, 4096);
list($date,$time,$ip,$traffic)=explode(" ",$line);
if($traffic>0) {mysql($db,"insert into Traffic (ip,measuringtime,bytes) values('{$ip}','{$date } {$time}','{$traffic}')");}
}
fclose($handle);
=== Notes ===
As you see this way can be time-consuming in case of a big number of VEscontainers.
So if anybody has scripts that automate all the process — you are welcome!