Check your user beancounters in OpenVZ

Knowing if you adjusted your user beancounters in OpenVZ right can be quite difficult. The OpenVZ wiki has a great article about the systemwide UBC configuration but I takes a bit of effort to calculate all of them, and over again when you’ve adjusted them. So I made a simple script that checks the systemwide UBC parameters of all running containers (VPS). You can find detailed information about the results in article; http://wiki.openvz.org/UBC_systemwide_configuration

If you need any help or think that something is wrong with the script please drop a line in the comments. Thank you!

This script has to be run on the hardwarenode itself.

#!/bin/bash
#
# Script to display UBC systemwide configuration as explained in article; http://wiki.openvz.org/UBC_systemwide_configuration
#
# Written by ezeyme.com
#
# Script is as it is. No warranty given.

declare -A ubc_parm_current
declare -A ubc_parm_barrier
declare -A ubc_parm_limit

# Get system memory
tmem=`cat /proc/meminfo |grep MemTotal|awk '{print $2}'`    # Total System Memory
#echo "tmem: "$tmem
tswap=`cat /proc/meminfo |grep SwapTotal|awk '{print $2}'`  # Total Swap
#echo "tswap: "$tswap

# Calculate normal system memory utilization
let tmem_plus_swap=tmem+tswap
#echo "tmem_plus_swap: "$tmem_plus_swap
minmem_util=$(echo 'scale=4; '$tmem'/'$tmem_plus_swap | bc)     # Ram Size/Ramsize + swap size
let htswap=tswap/2                              # 1/2 swap size
#echo "htswap: "$htswap
let tmem_plus_htswap=tmem+htswap
#echo "tmem_plus_htswap: "$tmem_plus_htswap
maxmem_util=$(echo 'scale=4; '$tmem_plus_htswap'/'$tmem_plus_swap | bc)   # Ram Size + 1/2 swap size/Ram Size + swap size

echo "Minimum memory utilization: "$minmem_util
echo "Maximum memory utilization: "$maxmem_util

# Get counters from running VPS's
ubc=(kmemsize privvmpages physpages vmguarpages oomguarpages tcpsndbuf tcprcvbuf othersockbuf dgramrcvbuf)

for veid in `vzlist -o veid -H`;
do

echo
echo -n "VEID: "$veid

vzctl exec $veid cat /proc/user_beancounters > output.tmp

        for i in ${ubc[@]};
        do
                if [[ $i == "kmemsize" ]];
                then
                        ubc_parm_current[$i]=`cat output.tmp | grep $i | awk '{print $3}'`
                        ubc_parm_barrier[$i]=`cat output.tmp | grep $i | awk '{print $5}'`
                        ubc_parm_limit[$i]=`cat output.tmp | grep $i | awk '{print $6}'`

                else
                        ubc_parm_current[$i]=`cat output.tmp | grep $i | awk '{print $2}'`
                        ubc_parm_barrier[$i]=`cat output.tmp | grep $i | awk '{print $4}'`
                        ubc_parm_limit[$i]=`cat output.tmp | grep $i | awk '{print $5}'`
                fi
        echo -n "."
        done

let oomguard_tot_current=oomguard_tot_current+${ubc_parm_current['oomguarpages']}
let oomguard_tot_barrier=oomguard_tot_barrier+${ubc_parm_barrier['oomguarpages']}

let privvm_tot_current=privvm_tot_current+${ubc_parm_current['privvmpages']}
let vmguar_tot_barrier=vmguar_tot_barrier+${ubc_parm_barrier['vmguarpages']}
let privvm_tot_limit=privvm_tot_limit+${ubc_parm_limit['privvmpages']}

let kmemsize_tot_current=kmemsize_tot_current+${ubc_parm_current['kmemsize']}
let kmemsize_tot_limit=kmemsize_tot_limit+${ubc_parm_limit['kmemsize']}
let allsocketbuf_tot_current=allsocketbuf_tot_current+${ubc_parm_current['tcpsndbuf']}+${ubc_parm_current['tcprcvbuf']}+${ubc_parm_current['othersockbuf']}+${ubc_parm_current['dgramrcvbuf']}
let allsocketbuf_tot_limit=allsocketbuf_tot_limit+${ubc_parm_limit['tcpsndbuf']}+${ubc_parm_limit['tcprcvbuf']}+${ubc_parm_limit['othersockbuf']}+${ubc_parm_limit['dgramrcvbuf']}

done

let oomguard_util=oomguard_tot_current*4096
let oomguard_util=oomguard_util+kmemsize_tot_current+allsocketbuf_tot_current

let oomguard_commit=oomguard_tot_barrier*4096
let oomguard_commit=oomguard_commit+kmemsize_tot_current+allsocketbuf_tot_current

let privv_util=privv_tot_current*4096
let privv_util=privv_util+kmemsize_tot_current+allsocketbuf_tot_current

let privv_limit=privv_tot_limit*4096
let privv_limit=privv_limit+kmemsize_tot_limit+allsocketbuf_tot_limit

let vmguar_commit=vmguar_tot_barrier*4096
let vmguar_commit=vmguar_commit+kmemsize_tot_current+allsocketbuf_tot_current

let tmem=tmem*1024
let tswap=tswap*1024
let memtotal=tmem+tswap

echo
echo -n "Oomguardpages utilization: "; echo 'scale=4; '$oomguard_util' / '$memtotal | bc
echo
echo -n "Oomguardpages commitment : "; echo 'scale=4; '$oomguard_commit' / '$memtotal | bc
echo
echo -n "Privvmpages utilization  : "; echo 'scale=4; '$privv_util' / '$memtotal | bc
echo
echo -n "Vmguapages commitment    : "; echo 'scale=4; '$vmguar_commit' / '$memtotal | bc
echo
echo -n "Privvmpages limitation   : "; echo 'scale=4; '$privv_limit' / '$memtotal | bc

About Esmé