Skip to content

Instantly share code, notes, and snippets.

@scrothers
Created March 5, 2012 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scrothers/1981920 to your computer and use it in GitHub Desktop.
Save scrothers/1981920 to your computer and use it in GitHub Desktop.
A /very/ simple OpenVZ load average aggregator with fancy colors.
#!/bin/bash
# License: GNU/GPL Open Source License v3
# Authors:
# Steven Crothers <steven.crothers@gmail.com>
# Jean-Marc Pigeon <jmp@safe.ca>
# Default variables
NO_HEADER=0
NO_COLOR=0
# Get the options that the script was ran with
while getopts "hHC" OPTION; do
case $OPTION in
h)
echo "usage $0 options"
echo "OPTIONS:"
echo " -h Show this help message"
echo " -H Disable the header"
echo " -C Disable colors"
exit 1
;;
H)
NO_HEADER=1
;;
C)
NO_COLOR=1
;;
esac
done
# Print the header dependant on what flag was passed
if [ $NO_HEADER -eq 0 ]; then
printf " %-15s %-20s %-15s %-9s %s\r\n" "VEID" "Hostname" "Load: 1min" "5min" "15min"
fi
## Get the load for all the VEs on a hardware node
for VEID in /proc/vz/container/[0-9]*; do
VEID=$(basename $VEID)
source /etc/vz/conf/$VEID.conf
LOADTOTAL=$(vzctl exec $VEID cat /proc/loadavg | awk '{print $1,$2,$3}')
LOAD_1MIN=$(echo $LOADTOTAL | awk '{print $1}')
LOAD_5MIN=$(echo $LOADTOTAL | awk '{print $2}')
LOAD_15MIN=$(echo $LOADTOTAL | awk '{print $3}')
LOAD=${LOAD_1MIN/.*}
if [ $NO_COLOR -eq 0 ]; then
[ $LOAD -lt 3 ] && COLOR="\033[1;32m"
[ $LOAD -ge 3 ] && COLOR="\033[1;33m"
[ $LOAD -ge 7 ] && COLOR="\033[1;31m"
fi
[ ${#HOSTNAME} -gt 20 ] && HOSTNAME="${HOSTNAME:0:17}..."
printf "$COLOR %-15s %-20s %10s %9s %10s\033[0m\r\n" $VEID $HOSTNAME $LOAD_1MIN $LOAD_5MIN $LOAD_15MIN
done | sort -t ' ' -k 3
## Get the hardware nodes load average
LOADTOTAL=$(cat /proc/loadavg | awk '{print $1,$2,$3}')
LOAD_1MIN=$(echo $LOADTOTAL | awk '{print $1}')
LOAD_5MIN=$(echo $LOADTOTAL | awk '{print $2}')
LOAD_15MIN=$(echo $LOADTOTAL | awk '{print $3}')
LOAD=${LOAD_1MIN/.*}
if [ $NO_COLOR -eq 0 ]; then
[ $LOAD -lt 3 ] && COLOR="\033[1;32m"
[ $LOAD -ge 3 ] && COLOR="\033[1;33m"
[ $LOAD -ge 7 ] && COLOR="\033[1;31m"
fi
[ ${#HOSTNAME} -gt 20 ] && HOSTNAME="${HOSTNAME:0:17}..."
printf "$COLOR %-15s %-20s %10s %9s %10s\033[0m\r\n" Total $HOSTNAME $LOAD_1MIN $LOAD_5MIN $LOAD_15MIN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment