Skip to content

Instantly share code, notes, and snippets.

@tomislacker
Last active May 18, 2017 14:47
Show Gist options
  • Save tomislacker/cd0506ba2020e192d60a44e79955453e to your computer and use it in GitHub Desktop.
Save tomislacker/cd0506ba2020e192d60a44e79955453e to your computer and use it in GitHub Desktop.
vmstat_monitor
#!/bin/bash
###############################################################################
# vmstat_monitor.sh
#
# Options:
# $1 Delay, in seconds, between readings. Must be >0
# $2 Count of readings to take. This should always be 2
#
# If the VMSTAT_DEBUG variable is set non-empty, debug output of all values of
# variables will be outputed to stderr.
###############################################################################
#################
# Configuration #
###############################################################################
DEFAULT_DELAY=1
DEFAULT_COUNT=2
#############
# Functions #
###############################################################################
get_vmstat ()
{
local delay=${1:-1}
local count=${2:-2}
local vmstat_output=$(vmstat ${delay} ${count})
if [ -n "$VMSTAT_DEBUG" ]
then
echo >&2 "<vmstat delay=${delay} count=${count}>"
echo >&2 "$vmstat_output"
echo >&2 "</vmstat>"
fi
echo "$vmstat_output" | tail -n 1
}
#############
# Execution #
###############################################################################
DELAY=${1:-${DEFAULT_DELAY}}
COUNT=${2:-${DEFAULT_COUNT}}
VMSTAT=( $(get_vmstat ${DELAY} ${COUNT}) )
# See `man vmstat` for details on the below values and
# ensure to run once with VMSTAT_DEBUG to sanity check
# that the values are correct for your system.
PROCS_R=${VMSTAT[0]}
PROCS_B=${VMSTAT[1]}
MEM_SWPD=${VMSTAT[2]}
MEM_FREE=${VMSTAT[3]}
MEM_BUFF=${VMSTAT[4]}
MEM_CACHE=${VMSTAT[5]}
SWAP_SI=${VMSTAT[6]}
SWAP_SO=${VMSTAT[7]}
IO_BI=${VMSTAT[8]}
IO_BO=${VMSTAT[9]}
# These are reported in units per second
SYSTEM_IN=${VMSTAT[10]}
SYSTEM_CS=${VMSTAT[11]}
CPU_US=${VMSTAT[12]}
CPU_SY=${VMSTAT[13]}
CPU_ID=${VMSTAT[14]}
CPU_WA=${VMSTAT[15]}
CPU_ST=${VMSTAT[16]}
if [ -n "$VMSTAT_DEBUG" ]
then
echo -e "VMSTAT:\t${VMSTAT[@]}" >&2
for v in PROCS_{R,B} \
MEM_{SWPD,FREE,BUFF,CACHE,INACT,ACTIVE} \
SWAP_{SI,SO} \
IO_{BI,BO} \
SYSTEM_{IN,CS} \
CPU_{US,SY,ID,WA,ST}
do
echo -e "${v}:\t${!v}" >&2
done
fi
########
# TODO #
###############################################################################
# Format your output for whatever monitoring tool you're using such as Zabbix,
# Nagios, Centreon, etc.
###############################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment