#!/bin/bash
#
# To determine if Guest is busy or idle
#
if [ $# -ne 1 ]; then
echo "Usage for $0 : VM Name required as argument."
exit 1
fi
#
if [ $UID -eq 0 ]
then echo "This script should NOT be run as root."
exit 1
fi
#
# Source config file
if [ ! -f ~/.vbconfig ]; then
echo "Config File ~/.vbconfig not found!"
echo "Run: set-folders to create .vbconfig"
exit 1
fi
. ~/.vbconfig
#
# Virtual Machine Name
VB_VM_NAME="$1"
#
# Metric period in seconds
METRIC_PERIOD=1
# Metric sample size
METRIC_SAMPLE=10
# The minimum idle percentage (decimal assumed) to allow a snapshot to be taken
LOAD_THRESHOLD=9500
# Sleep between checks for CPU idle
SLEEP_INTERVAL=60
# Retry counter
SLEEP_COUNTER=120
# Total retry hours = (SLEEP_INTERVAL x SLEEP_COUNTER)/3600
#
#SLEEP_INTERVAL=5
#SLEEP_COUNTER=12
# Establis metric
VBoxManage metrics setup \
--period $METRIC_PERIOD --samples $METRIC_SAMPLE $VB_VM_NAME Guest/*
# Sleep for the sample period + 1 so we get a full range of metric
sleep 1
sleep $METRIC_SAMPLE
#
IDLE_AVG=0
COUNTER=0
# Loop until you run out of retry counter OR the load is below threshold
while [[ $IDLE_AVG -lt $LOAD_THRESHOLD && $COUNTER -lt $SLEEP_COUNTER ]]; do
if (( $COUNTER > 0 )); then
sleep $SLEEP_INTERVAL
fi
let COUNTER=COUNTER+1
IDLE_AVG=`VBoxManage metrics query $VB_VM_NAME Guest/CPU/Load/Idle:avg|awk '{print $3}'|tr -cd [:digit:]`
#echo "Iteration: $COUNTER, Idle Average: $IDLE_AVG"
done
VBoxManage metrics disable $VB_VM_NAME
# Validate if loop exited because of which reason
if (( $COUNTER < $SLEEP_COUNTER )); then
# The Guest is idle
echo "idle"
else
# The Guest is under prolonged heavy load
echo "busy"
fi
#
exit