Difference between revisions of "Monitoring bandwidth of containers"
(add script) |
m (no need for displaytitle) |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | + | Use the /proc/net/dev inside a container, dump stats into a round robin database (rrd) | |
− | |||
− | Use the /proc/dev | ||
<pre> | <pre> | ||
Line 26: | Line 24: | ||
BWDIR=var/rrd/vz | BWDIR=var/rrd/vz | ||
+ | |||
+ | ## Default folder for containers | ||
+ | VE_PRIVATE=/var/lib/vz/private | ||
Line 35: | Line 36: | ||
# Define the full path to the bandwidth VE directory and the storage dir. | # Define the full path to the bandwidth VE directory and the storage dir. | ||
− | CBWDIR= | + | CBWDIR=$VE_PRIVATE/$BWVE/$BWDIR |
# Test and make sure that directory is there, if not create it. | # Test and make sure that directory is there, if not create it. | ||
Line 52: | Line 53: | ||
# Delete the .rrd file because its obviously not needed anymore. | # Delete the .rrd file because its obviously not needed anymore. | ||
− | if ! test -d | + | if ! test -d $VE_PRIVATE/$VES; then |
echo $i will be deleted | echo $i will be deleted | ||
rm -rf $CBWDIR/vps-$VES.rrd | rm -rf $CBWDIR/vps-$VES.rrd | ||
Line 83: | Line 84: | ||
X=`/usr/sbin/vzctl exec $i "grep venet0 /proc/net/dev"` | X=`/usr/sbin/vzctl exec $i "grep venet0 /proc/net/dev"` | ||
− | eval `echo $X | cut -f2 -d: | awk '{printf"IN=% | + | eval `echo $X | cut -f2 -d: | awk '{printf"IN=%s\nOUT=%s\n", $1, $9}'` |
for g in IN OUT;do | for g in IN OUT;do | ||
Line 96: | Line 97: | ||
[[Category: HOWTO]] | [[Category: HOWTO]] | ||
− | |||
[[Category: Monitoring]] | [[Category: Monitoring]] | ||
+ | [[Category: Networking]] |
Latest revision as of 12:11, 18 March 2010
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