Skip to content

Instantly share code, notes, and snippets.

@josacar
Created February 23, 2016 14:57
Show Gist options
  • Save josacar/52e9cf55e3b0485e1d5f to your computer and use it in GitHub Desktop.
Save josacar/52e9cf55e3b0485e1d5f to your computer and use it in GitHub Desktop.
Check memory
#!/usr/bin/env bash
#
# Evaluate free system memory from Linux based systems based on percentage
# This was forked from Sensu Community Plugins
#
# Requires: awk
#
# Date: 2007-11-12
# Author: Thomas Borger - ESG
# Date: 2012-04-02
# Modified: Norman Harman - [email protected]
# Date: 2013-9-30
# Modified: Mario Harvey - Zumetrics
# Date: 2015-01-10
# Modified Ollie Armstrong <[email protected]>
# Date: 2016-02-23
# Modified: Jose Luis Salas <[email protected]>
# get arguments
# #RED
while getopts 'w:c:hp' OPT; do
case $OPT in
w) WARN=$OPTARG;;
c) CRIT=$OPTARG;;
h) hlp="yes";;
p) perform="yes";;
*) unknown="yes";;
esac
done
# usage
HELP="
usage: $0 [ -w value -c value -p -h ]
-w --> Warning Percentage < value
-c --> Critical Percentage < value
-p --> print out performance data
-h --> print this help screen
"
if [ "$hlp" = "yes" ]; then
echo "$HELP"
exit 0
fi
WARN=${WARN:=0}
CRIT=${CRIT:=0}
#Get total memory available on machine
TotalMem=$(free -m | grep Mem | awk '{ print $2 }')
#Determine amount of free memory on the machine
set -o pipefail
FreeMem=$(free -m | grep buffers/cache | awk '{ print $4 }')
if [ $? -ne 0 ];
then
FreeMem=$(free -m | grep Mem | awk '{ print $7 }')
fi
#Get percentage of free memory
FreePer=$(awk -vFreeMem="$FreeMem" -vTotalMem="$TotalMem" 'BEGIN{printf "%.3f", FreeMem/TotalMem * 100 }' | cut -d "." -f1)
#Get actual memory usage percentage by subtracting free memory percentage from 100
UsedPer=$((100-$FreePer))
if [ "$UsedPer" = "" ]; then
echo "MEM UNKNOWN -"
exit 3
fi
if [ "$perform" = "yes" ]; then
output="system memory usage: $UsedPer% | free memory="$UsedPer"MB;$WARN;$CRIT;0"
else
output="system memory usage: $UsedPer%"
fi
if (( $UsedPer >= $CRIT )); then
echo "MEM CRITICAL - $output"
exit 2
elif (( $UsedPer >= $WARN )); then
echo "MEM WARNING - $output"
exit 1
else
echo "MEM OK - $output"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment