Changes

Jump to: navigation, search

Traffic accounting with iptables

109 bytes added, 20:41, 25 November 2006
use headings in Scripting: to better organize contents; some minor spelling fixes
Here are some scripting ideas
first a small script to get === Get VEIDs of all vz id's for later onrunning VEs ===
<pre>
host2:~/bin# cat vz-all-running
</pre>
second a small script witch get === Get all ip's IPs of running vz'sVEs ===
<pre>
host2:~/bin# cat vz-all-running-ip
</pre>
and a small script to set === Set up all needed iptable iptables rules===
<pre>
host2:~/bin# cat vz-iptables-create-rules
</pre>
a small script to generate === Generate a traffic.log===
Please use crontab to run this script once per hour or day to collect your traffic statistics.
<pre>
# reset the counter
iptables -Z
# update the ip table iptables rules if there is a any change in vz'sVEs
./vz-iptables-create-rules
# copy the trafficlog file to a webserver where users can take see their traffic 
# please mind to use
# ssh-keygen -t rsa
# to generate ssh keys
# and append the new public key from your hardware node (~/.ssh/id_rsa.pub)
# to ~/.ssh/authorized_keys2 on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS
# in order for the below scp command to not ask for root password
scp $trafficlog USER@HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE/tmp/$HOSTNAME-traffic
</pre>
a small sample === Sample php script to store the trafficlog in a database === Below script will process traffic.log and store the data into a MySQL Database on the HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS
<pre>
HOST-TO-SHOW-THE-TRAFFIC-TO-THE-USERS:/var/www/OPENVZ-CONTROL-WEB-SITE# cat traffic-read.php
</pre>
=== A SQL querry query to get the traffic for the last 30 days===
<pre>
SELECT sum(bytes)
</pre>
=== Notes === As you see this way can be time-consuming in case of a big number of VEs.  
So if anybody has scripts that automate all the process — you are welcome!
[[Category: HOWTO]]
[[Category: Networking]]

Navigation menu