Difference between revisions of "User:Kako/check vpsconf ressources.sh"

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
(First Version: $Id: check_vpsconf_ressources.sh,v 1.1 2009/08/12 10:54:49 thomas_c Exp $)
 
(checking now mem by using vzmemcheck)
 
Line 18: Line 18:
 
#
 
#
 
# @author Claudio Thomas, 2009
 
# @author Claudio Thomas, 2009
# @version $Id: check_vpsconf_ressources.sh,v 1.1 2009/08/12 10:54:49 thomas_c Exp $
+
# @version $Id: check_vpsconf_ressources.sh,v 1.3 2009/08/12 14:04:50 thomas_c Exp $
 
# @license LGPL
 
# @license LGPL
 
# @see http://wiki.openvz.org/UBC_systemwide_configuration
 
# @see http://wiki.openvz.org/UBC_systemwide_configuration
Line 36: Line 36:
 
# Functions
 
# Functions
 
# ------------------------------------------------------------------
 
# ------------------------------------------------------------------
function ok() {
+
ok() {
 
echo "\033[0;32mOK $1\033[0m";
 
echo "\033[0;32mOK $1\033[0m";
 
}
 
}
function warn() {
+
warn() {
 
echo "\033[1;33mWARN $1\033[0m";
 
echo "\033[1;33mWARN $1\033[0m";
 
}
 
}
function err() {
+
err() {
 
echo "\033[0;31mERR $1\033[0m";
 
echo "\033[0;31mERR $1\033[0m";
 
}
 
}
function incWarnings() {
+
incWarnings() {
 
w=`expr $w + 1`;
 
w=`expr $w + 1`;
 
}
 
}
function checkDisk() {
+
checkDisk() {
 
# DISKSPACE
 
# DISKSPACE
 
diskspaces=`egrep ^DISKSPACE $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
diskspaces=`egrep ^DISKSPACE $VZCONF_PATH/*.conf | cut -d '"' -f 2`
Line 83: Line 83:
 
if [ "$sum_hard" -lt "$diskinode_real" ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi  
 
if [ "$sum_hard" -lt "$diskinode_real" ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi  
 
}
 
}
function checkMem() {
+
checkMem() {
mem_real_kb=`cat /proc/meminfo | grep 'MemTotal' | tr -s ' ' | cut -d ' ' -f 2`
+
mem=`vzmemcheck -v`;
lowmem_real_kb=`cat /proc/meminfo | grep 'LowTotal' | tr -s ' ' | cut -d ' ' -f 2`
+
echo -e "$mem";
mem_real_mb=`expr $mem_real_kb / 1024`
+
mem_sumary=`echo $mem | grep 'Summary' | tr -s ' ' | cut -d ':' -f 2`
lowmem_real_mb=`expr $lowmem_real_kb / 1024`
+
for percent in $mem_sumary; do
+
st=`echo "$percent < 100" | bc`;
echo -e "TOTAL MEM:\t$mem_real_mb MB";
+
if [ $st -eq 0 ]; then incWarnings; fi
#------------------
+
done
# ---- LOW RAM ----
 
# @see: http://wiki.openvz.org/UBC_systemwide_configuration#.E2.80.9CLow_memory.E2.80.9D
 
RAM_kb=`echo "scale=4;0.4 * $lowmem_real_kb" | bc`
 
RAM_mb=`echo "scale=4;$RAM_kb / 1024" | bc`
 
echo -e "LOW MEM:\t$lowmem_real_mb MB (40% -> $RAM_mb MB)";
 
 
# ALLSOCKETBUF LIMIT = tcprcvbuf+tcpsndbuf+dgramrcvbuf+othersockbuf
 
tcprcvbuf_b=`egrep ^TCPRCVBUF $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
tcpsndbuf_b=`egrep ^TCPSNDBUF $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
dgramrcvbuf_b=`egrep ^DGRAMRCVBUF $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
othersockbuf_b=`egrep ^OTHERSOCKBUF $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
sum_soft_b=0;
 
sum_hard_b=0;
 
for limit in $tcprcvbuf_b; do
 
soft_b=`echo $limit | cut -d ':' -f 1`;
 
hard_b=`echo $limit | cut -d ':' -f 2`;
 
sum_soft_b=`expr $sum_soft_b + $soft_b`;
 
sum_hard_b=`expr $sum_hard_b + $hard_b`;
 
done;
 
for limit in $tcpsndbuf_b; do
 
soft_b=`echo $limit | cut -d ':' -f 1`;
 
hard_b=`echo $limit | cut -d ':' -f 2`;
 
sum_soft_b=`expr $sum_soft_b + $soft_b`;
 
sum_hard_b=`expr $sum_hard_b + $hard_b`;
 
done;
 
for limit in $dgramrcvbuf_b; do
 
soft_b=`echo $limit | cut -d ':' -f 1`;
 
hard_b=`echo $limit | cut -d ':' -f 2`;
 
sum_soft_b=`expr $sum_soft_b + $soft_b`;
 
sum_hard_b=`expr $sum_hard_b + $hard_b`;
 
done;
 
for limit in $othersockbuf_b; do
 
soft_b=`echo $limit | cut -d ':' -f 1`;
 
hard_b=`echo $limit | cut -d ':' -f 2`;
 
sum_soft_b=`expr $sum_soft_b + $soft_b`;
 
sum_hard_b=`expr $sum_hard_b + $hard_b`;
 
done;
 
# ALLSOCKETBUF current = tcprcvbuf+tcpsndbuf+dgramrcvbuf+othersockbuf
 
tcprcvbuf_b=`grep tcprcvbuf /proc/user_beancounters | grep -v $MAX_VAL | tr -s ' ' | cut -d ' ' -f 3`
 
tcpsndbuf_b=`grep tcpsndbuf /proc/user_beancounters | grep -v $MAX_VAL | tr -s ' ' | cut -d ' ' -f 3`
 
dgramrcvbuf_b=`grep dgramrcvbuf /proc/user_beancounters | grep -v $MAX_VAL | tr -s ' ' | cut -d ' ' -f 3`
 
othersockbuf_b=`grep othersockbuf /proc/user_beancounters | grep -v $MAX_VAL | tr -s ' ' | cut -d ' ' -f 3`
 
sum_cur_b=0;
 
for current in $tcprcvbuf_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
for current in $tcpsndbuf_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
for current in $dgramrcvbuf_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
for current in $othersockbuf_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
# normalize (Values were in Byte, so convert to KB)
 
sum_soft_kb=`expr $sum_soft_b / 1024`;
 
sum_hard_kb=`expr $sum_hard_b / 1024`;
 
sum_cur_kb=`expr $sum_cur_b / 1024`;
 
ALLSOCKETBUF_soft_kb=$sum_soft_kb;
 
ALLSOCKETBUF_hard_kb=$sum_hard_kb;
 
ALLSOCKETBUF_cur_kb=$sum_cur_kb;
 
ALLSOCKETBUF_soft_mb=`echo "scale=2;$ALLSOCKETBUF_soft_kb / 1024" | bc`
 
ALLSOCKETBUF_hard_mb=`echo "scale=2;$ALLSOCKETBUF_hard_kb / 1024" | bc`
 
ALLSOCKETBUF_cur_mb=`echo "scale=2;$ALLSOCKETBUF_cur_kb / 1024" | bc`
 
echo -n "-ALLSOCKETBUF: ";
 
echo -en "\tSOFT: $ALLSOCKETBUF_soft_mb MB";
 
echo -en "\tHARD: $ALLSOCKETBUF_hard_mb MB";
 
echo -e "\tCurrent: $ALLSOCKETBUF_cur_mb MB";
 
 
# KMEMSIZE LIMIT
 
kmemsize_b=`egrep ^KMEMSIZE $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
sum_soft_b=0;
 
sum_hard_b=0;
 
for limit in $kmemsize_b; do
 
soft_b=`echo $limit | cut -d ':' -f 1`;
 
hard_b=`echo $limit | cut -d ':' -f 2`;
 
sum_soft_b=`expr $sum_soft_b + $soft_b`;
 
sum_hard_b=`expr $sum_hard_b + $hard_b`;
 
done;
 
# KMEMSIZE current
 
kmemsize_b=`grep kmemsize /proc/user_beancounters | grep -v '0:' | tr -s ' ' | cut -d ' ' -f 4`
 
sum_cur_b=0;
 
for current in $kmemsize_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
# normalize (Values were in Byte, so convert to KB)
 
sum_soft_kb=`expr $sum_soft_b / 1024`;
 
sum_hard_kb=`expr $sum_hard_b / 1024`;
 
sum_cur_kb=`expr $sum_cur_b / 1024`;
 
KMEMSIZE_soft_kb=$sum_soft_kb;
 
KMEMSIZE_hard_kb=$sum_hard_kb;
 
KMEMSIZE_cur_kb=$sum_cur_kb;
 
KMEMSIZE_soft_mb=`echo "scale=2;$KMEMSIZE_soft_kb / 1024" | bc`
 
KMEMSIZE_hard_mb=`echo "scale=2;$KMEMSIZE_hard_kb / 1024" | bc`
 
KMEMSIZE_cur_mb=`echo "scale=2;$KMEMSIZE_cur_kb / 1024" | bc`
 
echo -n "-KMEMSIZE: ";
 
echo -en "\tSOFT: $KMEMSIZE_soft_mb MB";
 
echo -en "\tHARD: $KMEMSIZE_hard_mb MB";
 
echo -e "\tCurrent: $KMEMSIZE_cur_mb MB";
 
 
 
level=`echo "scale=4;( $KMEMSIZE_cur_kb + $ALLSOCKETBUF_cur_kb ) / $RAM_kb" | bc`
 
echo -en "  LowMem Utilization: ($KMEMSIZE_cur_mb + $ALLSOCKETBUF_cur_mb) / $RAM_mb  \t\t=> $level Level";
 
st=`echo "$level < 1" | bc`;
 
if [ $st -eq 1 ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi
 
level=`echo "scale=4;( $KMEMSIZE_hard_kb + $ALLSOCKETBUF_hard_kb ) / $RAM_kb" | bc`
 
echo -en "  LowMem Commitment:  ($KMEMSIZE_hard_mb + $ALLSOCKETBUF_hard_mb) / $RAM_mb\t\t=> $level Level";
 
st=`echo "$level < 1" | bc`;
 
if [ $st -eq 1 ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi
 
 
 
#------------------
 
#---- TOTAL RAM ----
 
# @see: http://wiki.openvz.org/UBC_systemwide_configuration#Total_RAM
 
physpages_b=`grep physpages /proc/user_beancounters | grep -v '$MAX_VAL $MAX_VAL' | tr -s ' ' | cut -d ' ' -f 3`
 
sum_cur=0;
 
for current in $physpages_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
# normalize (Values were in Byte, so convert to KB)
 
sum_cur_kb=`expr $sum_cur_b / 1024`;
 
PHYSPAGES_cur_kb=$sum_cur_kb;
 
PHYSPAGES_cur_mb=`echo "scale=2;$PHYSPAGES_cur_kb / 1024" | bc`
 
amount_kb=`expr $PHYSPAGES_cur_kb + $KMEMSIZE_cur_kb + $ALLSOCKETBUF_cur_kb`
 
amount_mb=`expr $amount_kb / 1024`;
 
level=`echo "scale=4;$amount_kb / $mem_real_kb" | bc`
 
echo -en "  TotMem Utilization: ($PHYSPAGES_cur_mb + $KMEMSIZE_cur_mb + $ALLSOCKETBUF_cur_mb) / $mem_real_mb\t=> $amount_mb MB / $mem_real_mb MB ($level Level)";
 
st=`echo "$level < 1" | bc`;
 
if [ $st -eq 1 ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn`"; incWarnings; fi
 
 
#------------------
 
#---- RAM+SWAP ----
 
# @see: http://wiki.openvz.org/UBC_systemwide_configuration#Memory_and_swap_space
 
swapmem_real_kb=`cat /proc/meminfo | grep 'SwapTotal' | tr -s ' ' | cut -d ' ' -f 2`
 
swapmem_real_mb=`expr $swapmem_real_kb / 1024`
 
echo "";
 
echo -en "Swap MEM:\t$swapmem_real_mb MB";
 
mem4_kb=`expr 4 \* $mem_real_kb`
 
if [ "$swapmem_real_kb" -lt "$mem4_kb" ]; then
 
if [ "$mem_real_kb" -lt "$swapmem_real_kb" ]; then echo -e "\t-- `ok`";
 
else echo -e "\t-- `warn '!!! Swap should be > Total Mem'`"; incWarnings; fi
 
else echo -e "\t-- `warn '!!! Swap should be < 4*Total Mem'`"; incWarnings; fi
 
 
# OOMGUARPAGES LIMIT
 
oomguarpages_b=`egrep ^OOMGUARPAGES $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
sum_soft_b=0;
 
sum_hard_b=0;
 
for limit in $oomguarpages_b; do
 
soft_b=`echo $limit | cut -d ':' -f 1`;
 
hard_b=`echo $limit | cut -d ':' -f 2`;
 
sum_soft_b=`expr $sum_soft_b + $soft_b`;
 
sum_hard_b=`expr $sum_hard_b + $hard_b`;
 
done;
 
oomguarpages_b=`grep oomguarpages /proc/user_beancounters | grep -v '2147483647 2147483647' | tr -s ' ' | cut -d ' ' -f 3`;
 
sum_cur_b=0;
 
for current in $oomguarpages_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
# normalize (Values were in Byte, so convert to KB)
 
sum_cur_kb=`expr $sum_cur_b / 1024`;
 
OOMGUARPAGES_soft_kb=$sum_soft_kb;
 
OOMGUARPAGES_hard_kb=$sum_hard_kb;
 
OOMGUARPAGES_cur_kb=$sum_cur_kb;
 
OOMGUARPAGES_soft_mb=`echo "scale=2;$OOMGUARPAGES_soft_kb / 1024" | bc`
 
OOMGUARPAGES_hard_mb=`echo "scale=2;$OOMGUARPAGES_hard_kb / 1024" | bc`
 
OOMGUARPAGES_cur_mb=`echo "scale=2;$OOMGUARPAGES_cur_kb / 1024" | bc`
 
echo -n "-OOMGUARPAGES: ";
 
echo -en "\tSOFT: $OOMGUARPAGES_soft_mb MB";
 
echo -en "\tHARD: $OOMGUARPAGES_hard_mb MB";
 
echo -e "\tCurrent: $OOMGUARPAGES_cur_mb MB";
 
 
 
level=`echo "scale=4;( $OOMGUARPAGES_cur_kb + $KMEMSIZE_cur_kb + $ALLSOCKETBUF_cur_kb ) / ( $RAM_kb + swapmem_real_kb )" | bc`
 
echo -en "  Swap Utilization: ($OOMGUARPAGES_cur_mb + $KMEMSIZE_cur_mb + $ALLSOCKETBUF_cur_mb) / ($RAM_mb + $swapmem_real_mb) \t\t=> $level Level";
 
st=`echo "$level < 1" | bc`;
 
if [ $st -eq 1 ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi
 
level=`echo "scale=4;( $OOMGUARPAGES_hard_kb + $KMEMSIZE_hard_kb + $ALLSOCKETBUF_hard_kb ) / ( $RAM_kb + swapmem_real_kb )" | bc`
 
echo -en "  Swap Commitment:  ($OOMGUARPAGES_hard_mb + $KMEMSIZE_hard_mb + $ALLSOCKETBUF_hard_mb) / ($RAM_mb + $swapmem_real_mb) \t\t=> $level Level";
 
st=`echo "$level < 1" | bc`;
 
if [ $st -eq 1 ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi
 
 
 
#--------------------------
 
#---- Allocated Memory ----
 
# @see: http://wiki.openvz.org/UBC_systemwide_configuration#Allocated_memory
 
# PRIVVMPAGES LIMIT
 
privvmpages_b=`egrep ^PRIVVMPAGES $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
sum_soft_b=0;
 
sum_hard_b=0;
 
for limit in $privvmpages_b; do
 
soft_b=`echo $limit | cut -d ':' -f 1`;
 
hard_b=`echo $limit | cut -d ':' -f 2`;
 
sum_soft_b=`expr $sum_soft_b + $soft_b`;
 
sum_hard_b=`expr $sum_hard_b + $hard_b`;
 
done;
 
privvmpages_b=`grep privvmpages /proc/user_beancounters | grep -v '2147483647 2147483647' | tr -s ' ' | cut -d ' ' -f 3`;
 
sum_cur_b=0;
 
for current in $privvmpages_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
# normalize (Values were in Byte, so convert to KB)
 
sum_cur_kb=`expr $sum_cur_b / 1024`;
 
PRIVVMPAGES_soft_kb=$sum_soft_kb;
 
PRIVVMPAGES_hard_kb=$sum_hard_kb;
 
PRIVVMPAGES_cur_kb=$sum_cur_kb;
 
PRIVVMPAGES_soft_mb=`echo "scale=2;$PRIVVMPAGES_soft_kb / 1024" | bc`
 
PRIVVMPAGES_hard_mb=`echo "scale=2;$PRIVVMPAGES_hard_kb / 1024" | bc`
 
PRIVVMPAGES_cur_mb=`echo "scale=2;$PRIVVMPAGES_cur_kb / 1024" | bc`
 
echo -n "-PRIVVMPAGES: ";
 
echo -en "\tSOFT: $PRIVVMPAGES_soft_mb MB";
 
echo -en "\tHARD: $PRIVVMPAGES_hard_mb MB";
 
echo -e "\tCurrent: $PRIVVMPAGES_cur_mb MB";
 
 
 
# VMGUARPAGES LIMIT
 
vmguarpages_b=`egrep ^VMGUARPAGES $VZCONF_PATH/*.conf | cut -d '"' -f 2`
 
sum_soft_b=0;
 
sum_hard_b=0;
 
for limit in $vmguarpages_b; do
 
soft_b=`echo $limit | cut -d ':' -f 1`;
 
hard_b=`echo $limit | cut -d ':' -f 2`;
 
sum_soft_b=`expr $sum_soft_b + $soft_b`;
 
sum_hard_b=`expr $sum_hard_b + $hard_b`;
 
done;
 
vmguarpages_b=`grep vmguarpages /proc/user_beancounters | grep -v '2147483647 2147483647' | tr -s ' ' | cut -d ' ' -f 3`;
 
sum_cur_b=0;
 
for current in $vmguarpages_b; do
 
sum_cur_b=`expr $sum_cur_b + $current`;
 
done;
 
# normalize (Values were in Byte, so convert to KB)
 
sum_cur_kb=`expr $sum_cur_b / 1024`;
 
VMGUARPAGES_soft_kb=$sum_soft_kb;
 
VMGUARPAGES_hard_kb=$sum_hard_kb;
 
VMGUARPAGES_cur_kb=$sum_cur_kb;
 
VMGUARPAGES_soft_mb=`echo "scale=2;$VMGUARPAGES_soft_kb / 1024" | bc`
 
VMGUARPAGES_hard_mb=`echo "scale=2;$VMGUARPAGES_hard_kb / 1024" | bc`
 
VMGUARPAGES_cur_mb=`echo "scale=2;$VMGUARPAGES_cur_kb / 1024" | bc`
 
echo -n "-VMGUARPAGES: ";
 
echo -en "\tSOFT: $VMGUARPAGES_soft_mb MB";
 
echo -en "\tHARD: $VMGUARPAGES_hard_mb MB";
 
echo -e "\tCurrent: $VMGUARPAGES_cur_mb MB";
 
 
 
level=`echo "scale=4;( ($PRIVVMPAGES_cur_kb*4096) + $KMEMSIZE_cur_kb + $ALLSOCKETBUF_cur_kb ) / ( $RAM_kb + swapmem_real_kb )" | bc`
 
echo -en "  Alloc Utilization: (($PRIVVMPAGES_cur_mb*4096) + $KMEMSIZE_cur_mb + $ALLSOCKETBUF_cur_mb) / ($RAM_mb + $swapmem_real_mb) \t\t=> $level Level";
 
st=`echo "$level < 1" | bc`;
 
if [ $st -eq 1 ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi
 
level=`echo "scale=4;( $VMGUARPAGES_soft_kb + $KMEMSIZE_hard_kb + $ALLSOCKETBUF_hard_kb ) / ( $RAM_kb + swapmem_real_kb )" | bc`
 
echo -en "  Alloc Commitment:  (($VMGUARPAGES_soft_kb*4096) + $KMEMSIZE_hard_mb + $ALLSOCKETBUF_hard_mb) / ($RAM_mb + $swapmem_real_mb) \t\t=> $level Level";
 
st=`echo "$level < 1" | bc`;
 
if [ $st -eq 1 ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi
 
level=`echo "scale=4;( $PRIVVMPAGES_hard_kb + $KMEMSIZE_hard_kb + $ALLSOCKETBUF_hard_kb ) / ( $RAM_kb + swapmem_real_kb )" | bc`
 
echo -en "  Alloc limit:  (($PRIVVMPAGES_hard_mb*4096) + $KMEMSIZE_hard_mb + $ALLSOCKETBUF_hard_mb) / ($RAM_mb + $swapmem_real_mb) \t\t=> $level Level";
 
st=`echo "$level < 1" | bc`;
 
if [ $st -eq 1 ]; then echo -e "\t-- `ok`"; else echo -e "\t-- `warn '!!!'`"; incWarnings; fi
 
 
 
 
}
 
}
 
 
# ------------------------------------------------------------------
 
# ------------------------------------------------------------------
 
# MAIN
 
# MAIN
Line 350: Line 98:
 
echo "      http://wiki.openvz.org/UBC_systemwide_configuration";
 
echo "      http://wiki.openvz.org/UBC_systemwide_configuration";
 
echo "";
 
echo "";
echo "Harddisk check:";
+
echo "Harddisk check: ($VZVPS_PATH)";
 
echo "----------------------------------------------";
 
echo "----------------------------------------------";
 
checkDisk;
 
checkDisk;
 
echo "";
 
echo "";
echo "Memory check: (level < 1 OK)";
+
echo "Memory check: (< 100%)";
 
echo "----------------------------------------------";
 
echo "----------------------------------------------";
 
checkMem;
 
checkMem;
 
 
 
 
echo "";
 
echo "";

Latest revision as of 14:06, 12 August 2009

#!/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;