Monitoring bandwidth of containers

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search

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

# Bandwidth collection script for OpenVZ by Eric 'phpfreak' Rosebrock
# /
# Please read:
# 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:

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


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


## Default folder for containers

# 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.

# 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


# 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

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


                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 \

                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
                /usr/bin/rrdtool update $RRDFILE N:$IN:$OUT