<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Golbs</id>
	<title>OpenVZ Virtuozzo Containers Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Golbs"/>
	<link rel="alternate" type="text/html" href="https://wiki.openvz.org/Special:Contributions/Golbs"/>
	<updated>2026-06-10T03:00:12Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2590</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2590"/>
		<updated>2006-12-19T13:58:01Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Generate a traffic.log */  bugfix for exact values&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
If you want to process the results with a script it is useful to use the &amp;quot;-x&amp;quot; or &amp;quot;--exact&amp;quot; option of iptables&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nvx -L FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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).&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nvx -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nvx -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nvx -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nvx -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
=== Get VEIDs of all running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get all IPs of running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set up all needed iptables rules ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a traffic.log ===&lt;br /&gt;
Please use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nvx -L FORWARD | grep &amp;quot; $i &amp;quot; | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the iptables rules if there is a any change in VEs&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can see their traffic&lt;br /&gt;
&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node (~/.ssh/id_rsa.pub)&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # in order for the below scp command to not ask for root password&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sample php script to store the trafficlog in a database ===&lt;br /&gt;
&lt;br /&gt;
Below script will process traffic.log and store the data into a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN-traffic&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A SQL query to get the traffic for the last 30 days ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of a big number of VEs. &lt;br /&gt;
&lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2589</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2589"/>
		<updated>2006-12-19T13:57:01Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Solution */  add a hint for exact value&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
If you want to process the results with a script it is useful to use the &amp;quot;-x&amp;quot; or &amp;quot;--exact&amp;quot; option of iptables&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nvx -L FORWARD&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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).&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nvx -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nvx -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nvx -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nvx -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
=== Get VEIDs of all running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get all IPs of running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set up all needed iptables rules ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a traffic.log ===&lt;br /&gt;
Please use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep &amp;quot; $i &amp;quot; | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the iptables rules if there is a any change in VEs&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can see their traffic&lt;br /&gt;
&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node (~/.ssh/id_rsa.pub)&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # in order for the below scp command to not ask for root password&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sample php script to store the trafficlog in a database ===&lt;br /&gt;
&lt;br /&gt;
Below script will process traffic.log and store the data into a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN-traffic&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A SQL query to get the traffic for the last 30 days ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of a big number of VEs. &lt;br /&gt;
&lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2584</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2584"/>
		<updated>2006-12-16T00:47:25Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Generate a traffic.log */ Bug fix&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
=== Get VEIDs of all running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get all IPs of running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set up all needed iptables rules ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a traffic.log ===&lt;br /&gt;
Please use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep &amp;quot; $i &amp;quot; | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the iptables rules if there is a any change in VEs&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can see their traffic&lt;br /&gt;
&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node (~/.ssh/id_rsa.pub)&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # in order for the below scp command to not ask for root password&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sample php script to store the trafficlog in a database ===&lt;br /&gt;
&lt;br /&gt;
Below script will process traffic.log and store the data into a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN-traffic&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A SQL query to get the traffic for the last 30 days ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of a big number of VEs. &lt;br /&gt;
&lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2571</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2571"/>
		<updated>2006-12-06T14:39:35Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* bug fix in Generate a traffic.log*/&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
=== Get VEIDs of all running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get all IPs of running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set up all needed iptables rules ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a traffic.log ===&lt;br /&gt;
Please use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep ' $i ' | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the iptables rules if there is a any change in VEs&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can see their traffic&lt;br /&gt;
&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node (~/.ssh/id_rsa.pub)&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # in order for the below scp command to not ask for root password&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sample php script to store the trafficlog in a database ===&lt;br /&gt;
&lt;br /&gt;
Below script will process traffic.log and store the data into a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN-traffic&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A SQL query to get the traffic for the last 30 days ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of a big number of VEs. &lt;br /&gt;
&lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2547</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2547"/>
		<updated>2006-11-25T20:49:24Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Generate a traffic.log */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
=== Get VEIDs of all running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Get all IPs of running VEs ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set up all needed iptables rules ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Generate a traffic.log ===&lt;br /&gt;
Please use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the iptables rules if there is a any change in VEs&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can see their traffic&lt;br /&gt;
&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node (~/.ssh/id_rsa.pub)&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # in order for the below scp command to not ask for root password&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sample php script to store the trafficlog in a database ===&lt;br /&gt;
&lt;br /&gt;
Below script will process traffic.log and store the data into a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN-traffic&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== A SQL query to get the traffic for the last 30 days ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of a big number of VEs. &lt;br /&gt;
&lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2545</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2545"/>
		<updated>2006-11-25T20:35:08Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log&lt;br /&gt;
Please use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the ip table rules if there is a any change in vz's&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can take their traffic&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node ~/.ssh/id_rsa.pub&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/login/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small sample php script to store the trafficlog in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN-traffic&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SQL querry to get the traffic for the last 30 days&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2544</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2544"/>
		<updated>2006-11-25T20:34:19Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log&lt;br /&gt;
Please use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the ip table rules if there is a any change in vz's&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can take their traffic&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node ~/.ssh/id_rsa.pub&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/login/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small sample php script to store the trafficlog in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SQL querry to get the traffic for the last 30 days&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2543</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2543"/>
		<updated>2006-11-25T20:32:48Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the ip table rules if there is a any change in vz's&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can take their traffic&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node ~/.ssh/id_rsa.pub&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/login/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small sample php script to store the trafficlog in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SQL querry to get the traffic for the last 30 days&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2542</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2542"/>
		<updated>2006-11-25T20:30:17Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the ip table rules if there is a any change in vz's&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can take their traffic&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node ~/.ssh/id_rsa.pub&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 # please mind to use .htaccess to secure this &lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/login/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small sample php script to store the trafficlog in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SQL querry to get the traffic for the last 30 days&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2541</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2541"/>
		<updated>2006-11-25T20:27:55Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the ip table rules if there is a any change in vz's&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 # copy the trafficlog file to a webserver where users can take their traffic&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node ~/.ssh/id_rsa.pub&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/login/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small sample php script to store the trafficlog in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SQL querry to get the traffic for the last 30 days&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
SELECT sum(bytes)&lt;br /&gt;
FROM Traffic&lt;br /&gt;
WHERE ip = 'INSERT-YOUR-IP-HERE'&lt;br /&gt;
AND measuringtime &amp;gt; ( now() - INTERVAL 1 MONTH)&lt;br /&gt;
GROUP BY ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2540</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2540"/>
		<updated>2006-11-25T20:14:19Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 # reset the counter&lt;br /&gt;
 iptables -Z&lt;br /&gt;
 # update the ip table rules if there is a any change in vz's&lt;br /&gt;
 ./vz-iptables-create-rules&lt;br /&gt;
&lt;br /&gt;
 # copy the trafficlog file to a webserver where users can take their traffic&lt;br /&gt;
 # please mind to use&lt;br /&gt;
 # ssh-keygen -t rsa&lt;br /&gt;
 # to generate ssh keys&lt;br /&gt;
 # and append the new public key from your hardware node ~/.ssh/id_rsa.pub&lt;br /&gt;
 # to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic&lt;br /&gt;
 &lt;br /&gt;
 # clear the copied trafficlog&lt;br /&gt;
 cp /dev/null $trafficlog&lt;br /&gt;
 # start a php script to store the traffic in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
 wget -q http://HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS/login/traffic-read.php?HN=$HOSTNAME -O /dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small sample php script to store the trafficlog in a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?&lt;br /&gt;
 $MySQL_Host=&amp;quot;INSERT-YOUR-MYSQL-HOST-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_User=&amp;quot;INSERT-YOUR-MYSQL-USER-HERE&amp;quot;;&lt;br /&gt;
 $MySQL_Passw=&amp;quot;INSERT-YOUR-MYSQL-PASSWORD-HERE&amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
 mysql_connect(&amp;quot;$MySQL_Host&amp;quot;,&amp;quot;$MySQL_User&amp;quot;,&amp;quot;$MySQL_Passw&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
 $HN=trim(addslashes($_GET[&amp;quot;HN&amp;quot;])); // Hardware Node&lt;br /&gt;
&lt;br /&gt;
 $handle = fopen (&amp;quot;tmp/$HN&amp;quot;,&amp;quot;r&amp;quot;);&lt;br /&gt;
 while (!feof($handle)) {&lt;br /&gt;
   $line = fgets($handle, 4096);&lt;br /&gt;
   list($date,$time,$ip,$traffic)=explode(&amp;quot; &amp;quot;,$line);&lt;br /&gt;
   if($traffic&amp;gt;0) {mysql($db,&amp;quot;insert into Traffic (ip,measuringtime,bytes) values('$ip','$date $time','$traffic')&amp;quot;);}&lt;br /&gt;
 } &lt;br /&gt;
 fclose($handle);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2538</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2538"/>
		<updated>2006-11-25T19:42:42Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist -H -oveid | sed 's/ //g;'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
vzlist -H -o ip&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;&amp;amp;1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can use crontab to run this script once per hour or day to collect your traffic statistics.&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process — you are welcome!&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2517</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2517"/>
		<updated>2006-11-19T19:59:30Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist |grep run|tr -s [:blank:]|cut -d' ' -f2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
for i in `./vz-all-running`; do vzctl exec $i ifconfig |grep 'venet0:0' -A1|tail -n1|cut -d':' -f2|cut -d' ' -f1; done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-generate-traffic-log&lt;br /&gt;
trafficlog=&amp;quot;/var/log/vz-traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can use crontab to run this script once per hour or day to collect your traffic statistics&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process - you are welcome!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2516</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2516"/>
		<updated>2006-11-19T19:56:11Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist |grep run|tr -s [:blank:]|cut -d' ' -f2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
for i in `./vz-all-running`; do vzctl exec $i ifconfig |grep 'venet0:0' -A1|tail -n1|cut -d':' -f2|cut -d' ' -f1; done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set up all needed iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-traffic&lt;br /&gt;
trafficlog=&amp;quot;./traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can use crontab to run this script once per hour or day to collect your traffic statistics&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process - you are welcome!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2515</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2515"/>
		<updated>2006-11-19T19:54:56Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist |grep run|tr -s [:blank:]|cut -d' ' -f2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
for i in `./vz-all-running`; do vzctl exec $i ifconfig |grep 'venet0:0' -A1|tail -n1|cut -d':' -f2|cut -d' ' -f1; done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set all iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-traffic&lt;br /&gt;
trafficlog=&amp;quot;./traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can use crontab to run this script once per hour or day to collect your traffic statistics&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process - you are welcome!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2514</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2514"/>
		<updated>2006-11-19T19:49:52Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist |grep run|tr -s [:blank:]|cut -d' ' -f2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
for i in `./vz-all-running`; do vzctl exec $i ifconfig |grep 'venet0:0' -A1|tail -n1|cut -d':' -f2|cut -d' ' -f1; done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set all iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
a small script to generate a traffic.log&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-traffic&lt;br /&gt;
trafficlog=&amp;quot;./traffic.log&amp;quot;&lt;br /&gt;
for i in `./vz-all-running-ip` ;&lt;br /&gt;
 do&lt;br /&gt;
  echo -n `date &amp;quot;+%Y-%m-%d %H:%M:%S&amp;quot;` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo -n &amp;quot; $i &amp;quot; &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
  echo `iptables -nv -L FORWARD | grep $i | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` &amp;gt;&amp;gt; $trafficlog&lt;br /&gt;
 done&lt;br /&gt;
 iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process - you are welcome!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2513</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2513"/>
		<updated>2006-11-19T18:27:14Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process - you are welcome!&lt;br /&gt;
&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist |grep run|tr -s [:blank:]|cut -d' ' -f2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
for i in `./vz-all-running`; do vzctl exec $i ifconfig |grep 'venet0:0' -A1|tail -n1|cut -d':' -f2|cut -d' ' -f1; done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set all iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2512</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2512"/>
		<updated>2006-11-19T18:26:10Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process - you are welcome!&lt;br /&gt;
&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running&lt;br /&gt;
vzlist |grep run|tr -s [:blank:]|cut -d' ' -f2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
second a small script witch get all ip's of running vz's&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-all-running-ip&lt;br /&gt;
for i in `./vz-all-running`; do vzctl exec $i ifconfig |grep 'venet0:0' -A1|tail -n1|cut -d':' -f2|cut -d' ' -f1; done&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and a small script to set all the iptable rules&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
host2:~/bin# cat vz-iptables-create-rules&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -D FORWARD -s $i; iptables -D FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
for i in `./vz-all-running-ip`;  do iptables -A FORWARD -s $i; iptables -A FORWARD -d $i; done &amp;gt;/dev/null 2&amp;gt;/dev/null&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2511</id>
		<title>Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=2511"/>
		<updated>2006-11-19T18:22:01Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Scripting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[VE]]s eat. It can be easily done&lt;br /&gt;
using iptables.&lt;br /&gt;
&lt;br /&gt;
== Situation description ==&lt;br /&gt;
Let's consider the very simple situation: one VE with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[VE]] ID is &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt;, the IP address of the [[HN]]&lt;br /&gt;
is &amp;lt;tt&amp;gt;192.168.0.56&amp;lt;/tt&amp;gt;, the network interface name is &amp;lt;tt&amp;gt;eth0&amp;lt;/tt&amp;gt;, and the IP address of the [[VE]] is &amp;lt;tt&amp;gt;192.168.0.117&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
You wish to know how much bytes VE 200 eats. One more assumption is that there are no iptables rules&lt;br /&gt;
on HN now. All these assumption are only for clarity!&lt;br /&gt;
&lt;br /&gt;
== Solution ==&lt;br /&gt;
Almost any traffic that goes to and from a VE can be catched by FORWARD chain of iptables module in [[VE0]],&lt;br /&gt;
thus we add such rules:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -A FORWARD -s 192.168.0.117&lt;br /&gt;
# iptables -A FORWARD -d 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
It means that all traffic forwarded to IP 192.168.0.117 and from IP 192.168.0.117 will be accounted.&lt;br /&gt;
To obtain current traffic usage of VE you can issue the command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 243 packets, 28089 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    8   832            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   15  1052            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Bytes''' column is the column we need. It's worth to say, that restarting a VE doesn't affect accounting,&lt;br /&gt;
it remains right. But if you restart your [[hardware node]], all the rules and consequently statistics are dropped.&lt;br /&gt;
So it is recommended to&lt;br /&gt;
* run some cron job that dumps statistics to some file&lt;br /&gt;
* add init script that creates iptables rules on [[HN]] start.&lt;br /&gt;
&lt;br /&gt;
As is easy to see, it's not per-VE statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing VE IP addresses, otherwise you'll get mess of results.&lt;br /&gt;
&lt;br /&gt;
By saying ''almost any traffic'' I mean that traffic between a [[VE]] and [[VE0]] is not accounted by rules above.&lt;br /&gt;
Not sure if it can be useful for anybody, but to account such traffic these rules are needed:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
iptables -I INPUT 1 -i venet0 -d 192.168.0.117&lt;br /&gt;
iptables -I OUTPUT 1 -o venet0 -s 192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To observe results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L INPUT&lt;br /&gt;
Chain INPUT (policy ACCEPT 542 packets, 63745 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   35  4533            all  --  venet0 *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -nv -L OUTPUT&lt;br /&gt;
Chain OUTPUT (policy ACCEPT 247 packets, 27847 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   48  4724            all  --  *      venet0  192.168.0.117        0.0.0.0/0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you need to zero counters this command works:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -Z&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The disadvantage is that doing this way you zero all counters in all rules. If it is not what you need,&lt;br /&gt;
you can just replace the rule with the same rule:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
   44  5151            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
# iptables -R FORWARD 1 -s 192.168.0.117&lt;br /&gt;
# iptables -nv -L FORWARD&lt;br /&gt;
Chain FORWARD (policy ACCEPT 101 packets, 10715 bytes)&lt;br /&gt;
 pkts bytes target     prot opt in     out     source               destination&lt;br /&gt;
    0     0            all  --  *      *       192.168.0.117        0.0.0.0/0&lt;br /&gt;
   57  5564            all  --  *      *       0.0.0.0/0            192.168.0.117&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More complicated cases ==&lt;br /&gt;
Well, now, when we know how to work in the easiest case, we'll try to understand what to do in&lt;br /&gt;
more complicated situations.&lt;br /&gt;
&lt;br /&gt;
; More than one VE on the node&lt;br /&gt;
: Just add the rules like above for each VE IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per VE.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a VE you have to summarize over all IPs that this VE owns.&lt;br /&gt;
&lt;br /&gt;
; More interfaces on the HN.&lt;br /&gt;
: Nothing to do! :)&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
As you see this way can be time-consuming in case of big number of VEs. &lt;br /&gt;
So if anybody has scripts that automate all the process - you are welcome!&lt;br /&gt;
&lt;br /&gt;
Here are some scripting ideas&lt;br /&gt;
&lt;br /&gt;
first a small script to get all vz id's for later on&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Networking]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Installation_on_Debian/old&amp;diff=2349</id>
		<title>Installation on Debian/old</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Installation_on_Debian/old&amp;diff=2349"/>
		<updated>2006-09-21T18:16:36Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Edit apt settings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Stable =&lt;br /&gt;
&lt;br /&gt;
== Edit apt settings ==&lt;br /&gt;
&lt;br /&gt;
add to your &amp;quot;/etc/apt/sources.list&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 deb http://debian.systs.org/ stable openvz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and get the new package lists&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# apt-get update&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Packages at debian.systs.org (dso) ==&lt;br /&gt;
&lt;br /&gt;
Debian Packages used for OpenVZ (i386):&lt;br /&gt;
&lt;br /&gt;
kernel(s): Version 022stab078.14&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-smp&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-entnosplit&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-enterprise&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
tool(s):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 vzctl&lt;br /&gt;
 vzquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
template(s):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 vzctl-template-debian&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing the utilities and kernels ==&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 # aptitude install kernel-image-2.6.8-stable-ovz vzctl vzquota vzctl-template-debian&lt;br /&gt;
&lt;br /&gt;
Maybe you need to update you &amp;quot;linux-loader&amp;quot; like lilo or grub:&lt;br /&gt;
&lt;br /&gt;
for the &amp;quot;GRUB&amp;quot;:&lt;br /&gt;
 # /sbin/grub-update&lt;br /&gt;
&lt;br /&gt;
Reboot in your new Debian Stable OpenVZ System&lt;br /&gt;
&lt;br /&gt;
 # reboot&lt;br /&gt;
&lt;br /&gt;
That's all :-)&lt;br /&gt;
&lt;br /&gt;
=Unstable=&lt;br /&gt;
&lt;br /&gt;
OpenVZ is now a part of Debian Sid (a.k.a. &amp;quot;unstable&amp;quot;) repository. This article describes how to install OpenVZ on a Debian Sid system.&lt;br /&gt;
&lt;br /&gt;
== Installing the utilities and kernel patch ==&lt;br /&gt;
&lt;br /&gt;
To install the OpenVZ kernel patch and utilities, run the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
apt-get update&lt;br /&gt;
apt-get install kernel-patch-openvz vzctl vzquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the kernel package ==&lt;br /&gt;
&lt;br /&gt;
To create a kernel package, you need to download and unpack 2.6.16 “vanilla” kernel first:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cd /usr/src/&lt;br /&gt;
wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.16.tar.bz2&lt;br /&gt;
tar xjf linux-2.6.16.tar.bz2&lt;br /&gt;
cd linux-2.6.16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(Note that you do need a vanilla kernel for this, because the OpenVZ kernel patch doesn't apply cleanly to the Debian linux-source-2.6.16 package; see [http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=377707 Debian bug #377707].)&lt;br /&gt;
&lt;br /&gt;
Next, get the proper kernel config from [http://download.openvz.org/kernel/devel/026test015.1/configs/ download.openvz.org]. Below is the example of using smp config for i686:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://download.openvz.org/kernel/devel/026test015.1/configs/kernel-2.6.16-026test015-i686-smp.config.ovz&lt;br /&gt;
mv kernel-2.6.16-026test015-i686-smp.config.ovz .config&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This example uses a config file for the 026test015 kernel patch. If the kernel-patch-openvz package you installed is a different version, download a config file that corresponds with your version}}&lt;br /&gt;
&lt;br /&gt;
Now you can apply openvz kernel patch and compile the kernel:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
make-kpkg --added_patches openvz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Installation]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Installation_on_Debian/old&amp;diff=2348</id>
		<title>Installation on Debian/old</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Installation_on_Debian/old&amp;diff=2348"/>
		<updated>2006-09-21T18:14:07Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Edit apt settings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Stable =&lt;br /&gt;
&lt;br /&gt;
== Edit apt settings ==&lt;br /&gt;
&lt;br /&gt;
add to your &amp;quot;/etc/apt/sources.list&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 deb http://debian.systs.org/ stable openvz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
get the new package lists&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# apt-get update&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Packages at debian.systs.org (dso) ==&lt;br /&gt;
&lt;br /&gt;
Debian Packages used for OpenVZ (i386):&lt;br /&gt;
&lt;br /&gt;
kernel(s): Version 022stab078.14&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-smp&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-entnosplit&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-enterprise&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
tool(s):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 vzctl&lt;br /&gt;
 vzquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
template(s):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 vzctl-template-debian&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing the utilities and kernels ==&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 # aptitude install kernel-image-2.6.8-stable-ovz vzctl vzquota vzctl-template-debian&lt;br /&gt;
&lt;br /&gt;
Maybe you need to update you &amp;quot;linux-loader&amp;quot; like lilo or grub:&lt;br /&gt;
&lt;br /&gt;
for the &amp;quot;GRUB&amp;quot;:&lt;br /&gt;
 # /sbin/grub-update&lt;br /&gt;
&lt;br /&gt;
Reboot in your new Debian Stable OpenVZ System&lt;br /&gt;
&lt;br /&gt;
 # reboot&lt;br /&gt;
&lt;br /&gt;
That's all :-)&lt;br /&gt;
&lt;br /&gt;
=Unstable=&lt;br /&gt;
&lt;br /&gt;
OpenVZ is now a part of Debian Sid (a.k.a. &amp;quot;unstable&amp;quot;) repository. This article describes how to install OpenVZ on a Debian Sid system.&lt;br /&gt;
&lt;br /&gt;
== Installing the utilities and kernel patch ==&lt;br /&gt;
&lt;br /&gt;
To install the OpenVZ kernel patch and utilities, run the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
apt-get update&lt;br /&gt;
apt-get install kernel-patch-openvz vzctl vzquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the kernel package ==&lt;br /&gt;
&lt;br /&gt;
To create a kernel package, you need to download and unpack 2.6.16 “vanilla” kernel first:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cd /usr/src/&lt;br /&gt;
wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.16.tar.bz2&lt;br /&gt;
tar xjf linux-2.6.16.tar.bz2&lt;br /&gt;
cd linux-2.6.16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(Note that you do need a vanilla kernel for this, because the OpenVZ kernel patch doesn't apply cleanly to the Debian linux-source-2.6.16 package; see [http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=377707 Debian bug #377707].)&lt;br /&gt;
&lt;br /&gt;
Next, get the proper kernel config from [http://download.openvz.org/kernel/devel/026test015.1/configs/ download.openvz.org]. Below is the example of using smp config for i686:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://download.openvz.org/kernel/devel/026test015.1/configs/kernel-2.6.16-026test015-i686-smp.config.ovz&lt;br /&gt;
mv kernel-2.6.16-026test015-i686-smp.config.ovz .config&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This example uses a config file for the 026test015 kernel patch. If the kernel-patch-openvz package you installed is a different version, download a config file that corresponds with your version}}&lt;br /&gt;
&lt;br /&gt;
Now you can apply openvz kernel patch and compile the kernel:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
make-kpkg --added_patches openvz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Installation]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Installation_on_Debian/old&amp;diff=2347</id>
		<title>Installation on Debian/old</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Installation_on_Debian/old&amp;diff=2347"/>
		<updated>2006-09-21T18:12:18Z</updated>

		<summary type="html">&lt;p&gt;Golbs: /* Edit apt settings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Stable =&lt;br /&gt;
&lt;br /&gt;
== Edit apt settings ==&lt;br /&gt;
&lt;br /&gt;
add to your &amp;quot;/etc/apt/sources.list&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 deb http://debian.systs.org/ stable openvz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
apt-get update&lt;br /&gt;
&lt;br /&gt;
== Packages at debian.systs.org (dso) ==&lt;br /&gt;
&lt;br /&gt;
Debian Packages used for OpenVZ (i386):&lt;br /&gt;
&lt;br /&gt;
kernel(s): Version 022stab078.14&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-smp&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-entnosplit&lt;br /&gt;
 kernel-image-2.6.8-stable-ovz-enterprise&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
tool(s):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 vzctl&lt;br /&gt;
 vzquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
template(s):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 vzctl-template-debian&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing the utilities and kernels ==&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 # aptitude install kernel-image-2.6.8-stable-ovz vzctl vzquota vzctl-template-debian&lt;br /&gt;
&lt;br /&gt;
Maybe you need to update you &amp;quot;linux-loader&amp;quot; like lilo or grub:&lt;br /&gt;
&lt;br /&gt;
for the &amp;quot;GRUB&amp;quot;:&lt;br /&gt;
 # /sbin/grub-update&lt;br /&gt;
&lt;br /&gt;
Reboot in your new Debian Stable OpenVZ System&lt;br /&gt;
&lt;br /&gt;
 # reboot&lt;br /&gt;
&lt;br /&gt;
That's all :-)&lt;br /&gt;
&lt;br /&gt;
=Unstable=&lt;br /&gt;
&lt;br /&gt;
OpenVZ is now a part of Debian Sid (a.k.a. &amp;quot;unstable&amp;quot;) repository. This article describes how to install OpenVZ on a Debian Sid system.&lt;br /&gt;
&lt;br /&gt;
== Installing the utilities and kernel patch ==&lt;br /&gt;
&lt;br /&gt;
To install the OpenVZ kernel patch and utilities, run the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
apt-get update&lt;br /&gt;
apt-get install kernel-patch-openvz vzctl vzquota&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Creating the kernel package ==&lt;br /&gt;
&lt;br /&gt;
To create a kernel package, you need to download and unpack 2.6.16 “vanilla” kernel first:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cd /usr/src/&lt;br /&gt;
wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.16.tar.bz2&lt;br /&gt;
tar xjf linux-2.6.16.tar.bz2&lt;br /&gt;
cd linux-2.6.16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(Note that you do need a vanilla kernel for this, because the OpenVZ kernel patch doesn't apply cleanly to the Debian linux-source-2.6.16 package; see [http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=377707 Debian bug #377707].)&lt;br /&gt;
&lt;br /&gt;
Next, get the proper kernel config from [http://download.openvz.org/kernel/devel/026test015.1/configs/ download.openvz.org]. Below is the example of using smp config for i686:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
wget http://download.openvz.org/kernel/devel/026test015.1/configs/kernel-2.6.16-026test015-i686-smp.config.ovz&lt;br /&gt;
mv kernel-2.6.16-026test015-i686-smp.config.ovz .config&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Note|This example uses a config file for the 026test015 kernel patch. If the kernel-patch-openvz package you installed is a different version, download a config file that corresponds with your version}}&lt;br /&gt;
&lt;br /&gt;
Now you can apply openvz kernel patch and compile the kernel:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
make-kpkg --added_patches openvz&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: HOWTO]]&lt;br /&gt;
[[Category: Installation]]&lt;/div&gt;</summary>
		<author><name>Golbs</name></author>
		
	</entry>
</feed>