shell script to monitor OpenVZ container running status

If you’re using OpenVZ, you owe it to yourself to take a look at /proc/user_beancounters every now and again (the last column of this file makes a lot of sense, as it indicates protential problems), but when errors occurred in a container, mostly you lose your chance to run any command in that VE.

In an attempt to implement a script to monitor this kind of issues for OpenVZ VPS, I created this shell script. It runs on the parent side, and checks each container’s status file /proc/user_beancounters. If the sum of the failcnt increased, it will print non-zero value which can be used to trigger an alert. Additionally, this script creates a summarized running status log for each container.

#!/bin/bash
# By joseph chen , Sep 28 2010
# This script is used to monitor virtual server's /proc/user_beancounters on
# the physic node.
# It creats a log file, and sums of the last column for each virtual server's
# /proc/user_beancounters, then compare this value with the last one.
# If the value increased, it prints a non-zero value, and further diagnostic is suggested.

BCounter="/proc/user_beancounters"
VZCTL="/usr/sbin/vzctl"

[ ! -d "/working/path" ] && mkdir /working/path
cd /working/path || exit

ovzlist=$(sudo /usr/sbin/vzlist -a |awk '/running/{print $1}')
for vz in $ovzlist
do
	## Create a track record for the running status
	echo ============= Time Now is: `date` ============== >> ${vz}.status
	sudo $VZCTL exec $vz cat $BCounter |awk '!/failcnt/{if(NF>4)if($NF>0) print}' 2>/dev/null >> ${vz}.status

	## Compare the last two values which stored in hidden files.
	sudo $VZCTL exec $vz cat $BCounter |awk 'BEGIN{sum=0}!/failcnt/{if(NF>4)if($NF>0) sum+=$NF}END{print sum}' >.${vz}.now

	# Initial the last value if not exist
	[ ! -f ".${vz}.last" ] && echo 0 > .${vz}.last

	# Overwrite the last value after comparison
	[ "$(cat .${vz}.last)" -ne "$(cat .${vz}.now)" ] && {
		cat .${vz}.now > .${vz}.last
		echo $vz && exit
	}
done
Share Button

Leave a comment

Your email address will not be published. Required fields are marked *