<?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=Robferrer</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=Robferrer"/>
	<link rel="alternate" type="text/html" href="https://wiki.openvz.org/Special:Contributions/Robferrer"/>
	<updated>2026-06-10T04:40:11Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Talk:Setting_up_an_iptables_firewall&amp;diff=13871</id>
		<title>Talk:Setting up an iptables firewall</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Talk:Setting_up_an_iptables_firewall&amp;diff=13871"/>
		<updated>2013-06-14T11:16:34Z</updated>

		<summary type="html">&lt;p&gt;Robferrer: /* Firewall rules in the wrong order? */ new section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The directions on this page for Container based firewalling didn't work for me at all.&lt;br /&gt;
However the Article at the Parallels Virtuozzo Knowledgebase regarding this issue worked perfectly.&lt;br /&gt;
URL: http://kb.parallels.com/en/746&lt;br /&gt;
&lt;br /&gt;
== Hardware Firewall mode not working ==&lt;br /&gt;
&lt;br /&gt;
I have tried step by step to enable a hardware lie firewall but i have big issues with existing virtual server that acts as a mailserver.&lt;br /&gt;
Basically with firewall activated the transaction time is around 48 seconds, with firewall deactivated about 0,700 seconds.&lt;br /&gt;
What on earth could possible be the cause? The FORWARD rule is that bad on VZ?&lt;br /&gt;
&lt;br /&gt;
== Firewall rules in the wrong order? ==&lt;br /&gt;
&lt;br /&gt;
I've been using the firewall script for a while and it works great. That is until I tried to ban an IP address, and it didn't work.&lt;br /&gt;
&lt;br /&gt;
I'm not an iptables expert, so was a bit wary about messing around too much, but my theory is all the iptables -I (insert) should be iptables -A (append), which has the effect of running the rules in the oposite order to intended. This means the source I wanted to block was matching an OKPORT before getting to the BANNED section.&lt;br /&gt;
&lt;br /&gt;
In fact to fix my problem I just moved the BANNED section between the DMZS and OKPORTS, which had the desired effect.&lt;br /&gt;
&lt;br /&gt;
I'd love to see anyone's comments. [[User:Robferrer|Robferrer]] ([[User talk:Robferrer|talk]]) 07:16, 14 June 2013 (EDT)&lt;/div&gt;</summary>
		<author><name>Robferrer</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Fair-share_scheduling&amp;diff=8600</id>
		<title>Fair-share scheduling</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Fair-share_scheduling&amp;diff=8600"/>
		<updated>2010-05-07T15:35:14Z</updated>

		<summary type="html">&lt;p&gt;Robferrer: spelling (example)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Fair-share scheduling is a method of allotting CPU time to multiple processes in a defined way.  When multiple processes need a CPU, operating systems frequently allot CPU time slices on a round robin, equal time basis for processes with the same priority.  An example is that if there are 4 processes wanting the CPU then each process will get 1/4 of a second every second.  This is not to say that each process will get 1/4 of a second all at one time.  Most operating systems define a time slice, something like 50 msec (arbitrary number that doesn't necessarily represent any OS) and will give each process a time slice.  So in this completely made up case, Each process will get 5 time slices every second.  &lt;br /&gt;
&lt;br /&gt;
Long ago someone said that we need a way of giving people with legitimate requirements more of the time than less needy people.  &amp;quot;Need&amp;quot; often reflects what level of service the user wants to pay for.  The fair-share scheduler was born.  There have been a number of implementations over the years, but on of the most common was to simply assign weights to users such that one user may get twice as many time slices in a given time period as others.  This has been expanded often to groups so that processes belonging to a group of users would be given the same number of time slices as another group.&lt;br /&gt;
&lt;br /&gt;
For example if group A had 8 processes running and group B had 4, then each group would get half the time, meaning that each process in the A group would only get 6.25% of the time (8*6.5 = 50) and each process in group B would get 12.5% of the time.&lt;br /&gt;
&lt;br /&gt;
There are a number of different implementations of the fair-share scheduler other than the ones described here.  I know of one that actually kept track of the accumulated time over a time period for a user/group and would use that information in it's calculations.  The theory was that if you'd been a cpu hog in the past, you might not get as much time right now.  This calculation also was also time weighted such that your share of time in the last second was much more important than a second a few minutes ago.&lt;/div&gt;</summary>
		<author><name>Robferrer</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Talk:Traffic_accounting_with_iptables&amp;diff=5888</id>
		<title>Talk:Traffic accounting with iptables</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Talk:Traffic_accounting_with_iptables&amp;diff=5888"/>
		<updated>2008-05-14T11:08:54Z</updated>

		<summary type="html">&lt;p&gt;Robferrer: New page:  == IPTables bytecounter overflow == I added a warning about the cron period. Our iptables bytecounters overflowed within an hour when we had constant download at 60Mbps. We calculated (ba...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== IPTables bytecounter overflow ==&lt;br /&gt;
I added a warning about the cron period. Our iptables bytecounters overflowed within an hour when we had constant download at 60Mbps. We calculated (based on the maxiumum value we had seen accounted and the 100Mbps network connection) that we should run the cron every 15 mins. Should consider even more frequently if using a Gbit connection.[[User:Robferrer|Robferrer]] 07:08, 14 May 2008 (EDT)&lt;/div&gt;</summary>
		<author><name>Robferrer</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=5887</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=5887"/>
		<updated>2008-05-14T11:05:34Z</updated>

		<summary type="html">&lt;p&gt;Robferrer: /* Generate a traffic.log */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[container]]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 container with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[container]] 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 [[container]] 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 many bytes container 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 container can be catched by FORWARD chain of iptables module in [[container0]],&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 container 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 saying, that restarting a container 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-container statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing container 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 [[container]] and [[container0]] 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 by doingit 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 container on the node&lt;br /&gt;
: Just add the rules like above for each container IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per container.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a container you have to summarize over all IPs that this container 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 CTIDs of all running containers ===&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 containers ===&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;
&lt;br /&gt;
(Warning, the counters can overflow if there is too much traffic within that period. Would recommend 15 minute intervals if you expect a lot of traffic)&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 containers&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;source lang=&amp;quot;text&amp;quot;&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;/source&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&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-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;/source&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;source lang=&amp;quot;mysql&amp;quot;&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;/source&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 containers. &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;
== See also ==&lt;br /&gt;
* [[Traffic accounting through proc]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: Monitoring]]&lt;/div&gt;</summary>
		<author><name>Robferrer</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Traffic_accounting_with_iptables&amp;diff=5886</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=5886"/>
		<updated>2008-05-14T11:05:20Z</updated>

		<summary type="html">&lt;p&gt;Robferrer: /* Generate a traffic.log */  - cron interval warning&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Suppose you need to know how much traffic your [[container]]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 container with one IP address on the [[Hardware Node]]&lt;br /&gt;
with only one network interface. To be more exact, assume that [[container]] 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 [[container]] 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 many bytes container 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 container can be catched by FORWARD chain of iptables module in [[container0]],&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 container 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 saying, that restarting a container 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-container statistic, but rather per-IP statistic. Thus you must be careful&lt;br /&gt;
then changing container 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 [[container]] and [[container0]] 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 by doingit 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 container on the node&lt;br /&gt;
: Just add the rules like above for each container IP.&lt;br /&gt;
&lt;br /&gt;
; More than one IP per container.&lt;br /&gt;
: For each IP add the rules like above. When counting the complete traffic of a container you have to summarize over all IPs that this container 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 CTIDs of all running containers ===&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 containers ===&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;
(Warning, the counters can overflow if there is too much traffic within that period. Would recommend 15 minute intervals if you expect a lot of traffic)&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 containers&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;source lang=&amp;quot;text&amp;quot;&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;/source&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&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-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;/source&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;source lang=&amp;quot;mysql&amp;quot;&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;/source&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 containers. &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;
== See also ==&lt;br /&gt;
* [[Traffic accounting through proc]]&lt;br /&gt;
&lt;br /&gt;
[[Category: Networking]]&lt;br /&gt;
[[Category: Monitoring]]&lt;/div&gt;</summary>
		<author><name>Robferrer</name></author>
		
	</entry>
</feed>