Difference between revisions of "Traffic accounting through proc"

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
(Script)
(no need to strip whitespace; use $() instead of `` since it's easier to read; other fixes for simplicity/readability/formatting)
Line 25: Line 25:
  
 
# Time to do the data collection.
 
# Time to do the data collection.
for veid in `/usr/sbin/vzlist -o veid -H | sed 's/ //g'`
+
for veid in $(/usr/sbin/vzlist -o veid -H)
 
do       
 
do       
 
         RRDFILE="/var/lib/rrd/$veid.rrd"
 
         RRDFILE="/var/lib/rrd/$veid.rrd"
Line 50: Line 50:
  
  
the following script uses previously generated RRD's, you'll get nice PNG pictures and index.html file with them:
+
the following script uses previously generated RRDs, you'll get nice PNG pictures and index.html file with them:
  
 
<source lang="bash">
 
<source lang="bash">
Line 64: Line 64:
 
for RRD in *.rrd
 
for RRD in *.rrd
 
do
 
do
         CTID=`echo $RRD|awk -F\. '{print $1}'`
+
         CTID=$(echo $RRD | sed 's/.rrd$//')
  
         # list of intervals, 1d = last day, 1w = last week and so..
+
         # list of intervals, 1d = last day, 1w = last week and so on
 
         for INT in 1h 1d 1w 1m 1y
 
         for INT in 1h 1d 1w 1m 1y
 
         do
 
         do
                 /usr/bin/rrdtool graph $www_path/${CTID}-${INT}.png \
+
                 /usr/bin/rrdtool graph ${www_path}/${CTID}-${INT}.png \
 
                         --start now-$INT --end now \
 
                         --start now-$INT --end now \
 
                         -w 1000 -h 500 \
 
                         -w 1000 -h 500 \
Line 75: Line 75:
 
                         DEF:in=$RRD:IN:AVERAGE AREA:in#aea:'IN' LINE1:in#0e0 \
 
                         DEF:in=$RRD:IN:AVERAGE AREA:in#aea:'IN' LINE1:in#0e0 \
 
                         DEF:out=$RRD:OUT:AVERAGE AREA:out#eaa:'OUT' LINE1:out#e00
 
                         DEF:out=$RRD:OUT:AVERAGE AREA:out#eaa:'OUT' LINE1:out#e00
                 echo "<p><img src=\"${CTID}-${INT}.png\"><br/><br/></p>" >> $www_path/index.html
+
                 echo "<p><img src=\"${CTID}-${INT}.png\"><br/><br/></p>" >> ${www_path}/index.html
 
         done
 
         done
 
done
 
done
Line 81: Line 81:
  
 
== Setup ==
 
== Setup ==
Add this to cron to run every vz_direct_traffic_log every 5 min and vz_direct_traffic_render every 30 min:
+
Add this to cron to run <code>vz_direct_traffic_log</code> every 5 minutes and <code>vz_direct_traffic_render</code> every 30 minutes:
  
  # echo -e "*/5 * * * * root /path/to/script/vz_direct_traffic_log\n*/30 * * * * root /path/to/script/vz_direct_traffic_render " >> \
+
  # cat << EOF >> /etc/crontab
  /etc/crontab && /etc/init.d/crontab restart
+
*/5 * * * * root /path/to/script/vz_direct_traffic_log
 +
*/30 * * * * root /path/to/script/vz_direct_traffic_render
 +
EOF
  
 +
Alternatively, use the cron system of OpenVZ
  
Better use the cron system of OpenVZ
+
# cat << EOF > /etc/vz/cron/vz-traffic.sh
 
+
  */5 * * * * root /path/to/script/vz_direct_traffic_log
  # echo -e "*/5 * * * * root /path/to/script/vz_direct_traffic_log\n*/30 * * * * root /path/to/script/vz_direct_traffic_render " >> \
+
*/30 * * * * root /path/to/script/vz_direct_traffic_render
  /etc/vz/cron/vz-traffic.sh && /etc/init.d/vz update-cron
+
EOF
 +
# /etc/init.d/vz update-cron
  
 
To check the output of this later run:
 
To check the output of this later run:

Revision as of 08:12, 16 June 2010

Traffic Accounting through /proc/net/dev

Introduction

Traffic accounting in OpenVZ can be done through data collection against /proc/net/dev in the individual containers on a HN.

Prerequisites

  • OpenVZ
  • RRDTool
  • GNU AWK

Script

First create a directory to store your RRD files:

# mkdir /var/lib/rrd/

Then install this script:

#!/bin/sh
# Script Name:  vz_direct_traffic_log
# Author:       "Brian Harrington, Alticon Inc" <bharrington@alticon.net>
# Website:      htty://www.alticon.net
# Editor:       "Denis Titov, ISP Okeanika" <denis@okeanika.net.ua>

# Time to do the data collection.
for veid in $(/usr/sbin/vzlist -o veid -H)
do      
        RRDFILE="/var/lib/rrd/$veid.rrd"
        if ! test -e $RRDFILE; then
                echo $RRDFILE does not exist, creating.

                # Place your preferred RRD Creation command here
                # or use this one, following command will create new RRD file with 300 sec step (for 5 min cron)
                /usr/bin/rrdtool create $RRDFILE --step 300 \
                        DS:IN:COUNTER:1000:0:U \
                        DS:OUT:COUNTER:1000:0:U \
                        RRA:AVERAGE:0.5:1:600000 \
                        RRA:AVERAGE:0.5:100:60000 \
                        RRA:AVERAGE:0.5:1000:6000
        fi
        # Parse out the inbound/outbound traffic and assign them to the corresponding variables     
        eval `/usr/sbin/vzctl exec $veid "grep venet0 /proc/net/dev"  |  \
                awk -F: '{print $2}' | awk '{printf"CTIN=%-15d\nCTOUT=%-15d\n", $1, $9}'`

        # Send the data to the corresponding RRD time with (N)now as the update time
        /usr/bin/rrdtool update $RRDFILE N:$CTIN:$CTOUT
done


the following script uses previously generated RRDs, you'll get nice PNG pictures and index.html file with them:

#!/bin/sh
# Script Name:  vz_direct_traffic_render
# Author:       "Denis Titov, ISP Okeanika" <denis@okeanika.net.ua>
# Website:      http://www.okeanika.net.ua

rrd_path=/var/lib/rrd
www_path=/var/www/html/traf

cd $rrd_path
for RRD in *.rrd
do
        CTID=$(echo $RRD | sed 's/.rrd$//')

        # list of intervals, 1d = last day, 1w = last week and so on
        for INT in 1h 1d 1w 1m 1y
        do
                /usr/bin/rrdtool graph ${www_path}/${CTID}-${INT}.png \
                        --start now-$INT --end now \
                        -w 1000 -h 500 \
                        --title "CTID $CTID - Interval $INT" \
                        DEF:in=$RRD:IN:AVERAGE AREA:in#aea:'IN' LINE1:in#0e0 \
                        DEF:out=$RRD:OUT:AVERAGE AREA:out#eaa:'OUT' LINE1:out#e00
                echo "<p><img src=\"${CTID}-${INT}.png\"><br/><br/></p>" >> ${www_path}/index.html
        done
done

Setup

Add this to cron to run vz_direct_traffic_log every 5 minutes and vz_direct_traffic_render every 30 minutes:

# cat << EOF >> /etc/crontab
*/5 * * * * root /path/to/script/vz_direct_traffic_log
*/30 * * * * root /path/to/script/vz_direct_traffic_render
EOF

Alternatively, use the cron system of OpenVZ

# cat << EOF > /etc/vz/cron/vz-traffic.sh
*/5 * * * * root /path/to/script/vz_direct_traffic_log
*/30 * * * * root /path/to/script/vz_direct_traffic_render
EOF
# /etc/init.d/vz update-cron

To check the output of this later run:

# rrdtool fetch /var/lib/rrd/<veid>.rrd AVERAGE -s -3600

See also