User:Kako/check vpsconf ressources.sh

From OpenVZ Virtuozzo Containers Wiki
Revision as of 14:06, 12 August 2009 by Kako (talk | contribs) (checking now mem by using vzmemcheck)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/bin/bash
#
# This script should check all formulas writen down in 
# http://wiki.openvz.org/UBC_systemwide_configuration
# The result is 
# - human-readble
# - machine-readable (result-code)
#
# Conding infos:
# On the wiki-pages of openvz "soft limit/barier" and "hard limit/limit" are used often as sinonyms.
#   I used "soft" for "barier" and "hard" for "limit"
# Variable names are defined in dependence of their units 
#   *_b => bytes
#   *_kb => kilobytes
#   *_mb => megabytes
# Calculation is done with KB, but displayed are MB, because the numbers are smallers
#
# @author	Claudio Thomas, 2009
# @version	$Id: check_vpsconf_ressources.sh,v 1.3 2009/08/12 14:04:50 thomas_c Exp $
# @license	LGPL
# @see		http://wiki.openvz.org/UBC_systemwide_configuration
# @uses		egrep, grep, expr, bc, df, tr, cut (in other words, it should run on every linux server :-)
# @return 	0 - all OK, 1 - at least one warning
# ------------------------------------------------------------------
# Project/User specific values
VZVPS_PATH='/vz';			# Directory where die VPS files are hold (to find out amount of free disk space)

# OpenVZ Values
VZCONF_PATH='/etc/vz/conf';	# Directory where the VPS-configurations can be found
MAX_VAL='2147483647';		# Maximal/highest number in /proc/user_beancounters

# internal vars
w=0;
# ------------------------------------------------------------------
# Functions
# ------------------------------------------------------------------
ok() {
	echo "\033[0;32mOK $1\033[0m";
}
warn() {
	echo "\033[1;33mWARN $1\033[0m";
}
err() {
	echo "\033[0;31mERR $1\033[0m";
}
incWarnings() {
	w=`expr $w + 1`;
}
checkDisk() {
	# DISKSPACE
	diskspaces=`egrep ^DISKSPACE $VZCONF_PATH/*.conf | cut -d '"' -f 2`
	diskspace_real=`df -k $VZVPS_PATH | grep $VZVPS_PATH | tr -s " " |cut -d ' ' -f 2`
	sum_soft=0;
	sum_hard=0;
	for limit in $diskspaces; do
		soft=`echo $limit | cut -d ':' -f 1`;
		hard=`echo $limit | cut -d ':' -f 2`;
		sum_soft=`expr $sum_soft + $soft`;
		sum_hard=`expr $sum_hard + $hard`;
	done;
	echo -n "DISKPACE:  "; # Angabe in KBytes
	echo -en "\tSOFT: `expr $sum_soft / 1024` MB";
	echo -en "\tHARD: `expr $sum_hard / 1024` MB";
	echo -en "\tMAX : `expr $diskspace_real / 1024` MB";
	if [ "$sum_hard" -lt "$diskspace_real" ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi 
	
	# DISKINODES
	diskinodes=`egrep ^DISKINODES $VZCONF_PATH/*.conf | cut -d '"' -f 2`
	diskinode_real=`df -ki $VZVPS_PATH | grep $VZVPS_PATH | tr -s " " |cut -d ' ' -f 2`
	sum_soft=0;
	sum_hard=0;
	for limit in $diskinodes; do
		soft=`echo $limit | cut -d ':' -f 1`;
		hard=`echo $limit | cut -d ':' -f 2`;
		sum_soft=`expr $sum_soft + $soft`;
		sum_hard=`expr $sum_hard + $hard`;
	done;
	echo -n "DISKINODES: "; # Angabe in KBytes
	echo -en "\tSOFT: `expr $sum_soft / 1024` MB";
	echo -en "\tHARD: `expr $sum_hard / 1024` MB";
	echo -en "\tMAX : `expr $diskinode_real / 1024` MB";
	if [ "$sum_hard" -lt "$diskinode_real" ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi 
}
checkMem() {
	mem=`vzmemcheck -v`;
	echo -e "$mem";
	mem_sumary=`echo $mem | grep 'Summary' | tr -s ' ' | cut -d ':' -f 2`
	for percent in $mem_sumary; do
		st=`echo "$percent < 100" | bc`;
		if [ $st -eq 0 ]; then incWarnings; fi
	done
}
# ------------------------------------------------------------------
# MAIN
# ------------------------------------------------------------------
echo "INFO: See near informations to calculation and values at";
echo "      http://wiki.openvz.org/UBC_systemwide_configuration";
echo "";
echo "Harddisk check: ($VZVPS_PATH)";
echo "----------------------------------------------";
checkDisk;
echo "";
echo "Memory check: (< 100%)";
echo "----------------------------------------------";
checkMem;
	
echo "";
if [ "$w" -gt "0" ]; then 
	echo -e "`warn`: $w warnings were diplayed. Check the output.";
	exit 1; 
fi
echo -e "`ok`: All paremeters seems to have good values.";
exit 0;