Open main menu

OpenVZ Virtuozzo Containers Wiki β

Monitoring bandwidth of containers

Revision as of 12:11, 18 March 2010 by Kir (talk | contribs) (no need for displaytitle)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Use the /proc/net/dev inside a container, dump stats into a round robin database (rrd)

#!/bin/bash
#
# Bandwidth collection script for OpenVZ by Eric 'phpfreak' Rosebrock
# http://www.serverpowered.com / http://www.thewebfreaks.com
# Please read: http://www.serverpowered.com/openvz-bandwidth-accounting.php
#
#
# No warranties on this script, use it at your own risk!
#
# Special thanks to Rick Blundell for helping me to get this thing going.
#
# Discussions: http://forum.openvz.org/index.php?t=tree&goto=1350

# BWVE is the bandwidth storage VE. This would have been created by the
# tutorial you followed as listed above.

BWVE=1002

# BWDIR is the directory inside the bandwdith VE where the .rrd files
# will be stored.

BWDIR=var/rrd/vz

## Default folder for containers
VE_PRIVATE=/var/lib/vz/private


# You should not need to edit anything below this line.

# Get the date from the first of this month to create new rrd files
TDATE=`date +%Y-%m-01`;
ETIME=`date +%s -d $TDATE`

# Define the full path to the bandwidth VE directory and the storage dir.
CBWDIR=$VE_PRIVATE/$BWVE/$BWDIR

# Test and make sure that directory is there, if not create it.
if ! test -d $CBWDIR; then
        echo $CBWDIR does not exist, creating it.
        mkdir $CBWDIR

fi

# Get a list of all of the VE's created on the system, not just the running ones.

for i in `ls $CBWDIR`; do
        VES=`echo $i | cut -f2 -d- | cut -f1 -d.`;

        # if the VE was there before, an .rrd file will exist. Now if the VE is gone,
        # Delete the .rrd file because its obviously not needed anymore.

        if ! test -d $VE_PRIVATE/$VES; then
                echo $i will be deleted
                rm -rf $CBWDIR/vps-$VES.rrd
        fi
done

# Time to do the data collection.
for i in `/usr/sbin/vzlist -Ho veid`; do

                RRDFILE=$CBWDIR/vps-$i.rrd

                if ! test -e $RRDFILE; then
                        echo $RRDFILE does not exist, creating.
                        /usr/bin/rrdtool create $RRDFILE -s 300 \
                                DS:ds0:DERIVE:600:0:1125000000 \
                                DS:ds1:DERIVE:600:0:1125000000 \
                                RRA:AVERAGE:0.5:1:600 \
                                RRA:AVERAGE:0.5:6:700 \
                                RRA:AVERAGE:0.5:24:775 \
                                RRA:AVERAGE:0.5:288:797 \
                                RRA:MAX:0.5:1:600 \
                                RRA:MAX:0.5:6:700 \
                                RRA:MAX:0.5:24:775 \
                                RRA:MAX:0.5:288:797 \
                                RRA:MIN:0.5:1:600 \
                                RRA:MIN:0.5:6:700 \
                                RRA:MIN:0.5:24:775 \
                                RRA:MIN:0.5:288:797
                fi

                X=`/usr/sbin/vzctl exec $i "grep venet0 /proc/net/dev"`
                eval `echo $X | cut -f2 -d: | awk '{printf"IN=%s\nOUT=%s\n", $1, $9}'`

                for g in IN OUT;do
                        if [ -z ${!g} ]; then
                                echo foo
                                eval $g=0
                        fi
                done
                /usr/bin/rrdtool update $RRDFILE N:$IN:$OUT
done