Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#!/bin/sh
#
# Author: Jan Collijs
# Email: jan.collijs@inuits.eu
# Date: October 2012
# Updated: December 2014 by Omar Reygaert
# Updated: February 2018 by Jan Collijs
# Implemented available memory check over free memory based on /proc/meminfo
# Purpose: Icinga-check to check the memory usage of the system
PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1.00 $' | sed -e 's/[^0-9.]//g'`
. $PROGPATH/utils.sh
WARNINGMEMPERCENTAGE=15
CRITICALMEMPERCENTAGE=10
WARNINGSWAOPERCENTAGE=25
CRITICALSWAPPERCENTAGE=50
# Retrieving the data from the server
TOTALMEM=`vmstat -s -SM | grep "total memory" | cut -f1 -d'M' | tr -d ' '`
USEDMEM=`vmstat -s -SM | grep " active memory" | cut -f1 -d'M' | tr -d ' '`
FREEMEM=`vmstat -s -SM | grep "free memory" | cut -f1 -d'M' | tr -d ' '`
CACHEDMEM=`vmstat -s -SM | grep "swap cache" | cut -f1 -d's' | tr -d ' '`
AVAILABLEMEM=$(cat /proc/meminfo | grep "MemAvailable" | cut -f2 -d':' | tr -s " " | cut -f2 -d' ')
AVAILABLEMEM=$(($AVAILABLEMEM / 1024 ))
TOTALSWAP=`vmstat -s -SM | grep "total swap" | cut -f1 -d'M' | tr -d ' '`
USEDSWAP=`vmstat -s -SM | grep "used swap" | cut -f1 -d'M' | tr -d ' '`
FREESWAP=`vmstat -s -SM | grep "free swap" | cut -f1 -d'M' | tr -d ' '`
# Calculating the percentages for memory & swap
USEDMEMPERCENTAGE=$(( $USEDMEM * 100 / $TOTALMEM))
FREEMEMPERCENTAGE=$(( $FREEMEM * 100 / $TOTALMEM))
AVAILABLEMEMPERCENTAGE=$(( $AVAILABLEMEM * 100 / $TOTALMEM))
USEDSWAPPERCENTAGE=$(( $USEDSWAP * 100 / $TOTALSWAP))
FREESWAPPERCENTAGE=$(( $FREESWAP * 100 / $TOTALSWAP))
# Compiling the memory message & icinga state
if [ $AVAILABLEMEMPERCENTAGE -lt $WARNINGMEMPERCENTAGE ];then
MEMMESSAGE="WARNING: only $AVAILABLEMEMPERCENTAGE% is available, $USEDMEMPERCENTAGE% of memory used"
MSTATE=$STATE_WARNING
elif [ $AVAILABLEMEMPERCENTAGE -lt $CRITICALMEMPERCENTAGE ];then
MEMMESSAGE="CRITICAL: only $AVAILABLEMEMPERCENTAGE% is available, $USEDMEMPERCENTAGE% of memory used"
MSTATE=$STATE_CRITICAL
else
MEMMESSAGE="OK: $AVAILABLEMEMPERCENTAGE% is available, $USEDMEMPERCENTAGE% of memory used"
MSTATE=$STATE_OK
fi
# Compiling the swap message & icinga state
if [ $USEDSWAPPERCENTAGE -gt $WARNINGSWAOPERCENTAGE ];then
SWAPMESSAGE="WARNING: $USEDSWAPPERCENTAGE% of swap used, only $FREESWAPPERCENTAGE% is free"
SSTATE=$STATE_WARNING
elif [ $USEDSWAPPERCENTAGE -gt $CRITICALSWAPPERCENTAGE ];then
SWAPMESSAGE="CRITICAL: $USEDSWAPPERCENTAGE% of swap used, $FREESWAPPERCENTAGE% is free"
SSTATE=$STATE_CRITICAL
else
SWAPMESSAGE="OK: $USEDSWAPPERCENTAGE% of swap used, $FREESWAPPERCENTAGE% is free"
SSTATE=$STATE_OK
fi
# Retrieving the correct state for icinga and distributing it
if [ $SSTATE -lt 0 -o $SSTATE -gt 3 -o $MSTATE -lt 0 -o $MSTATE -gt 3 ]; then
STATE=$STATE_UNKNOWN
elif [ $SSTATE -eq 0 -a $MSTATE -eq 0 ]; then
STATE=$STATE_OK
elif [ $SSTATE -eq 1 -a $MSTATE -eq 1 ]; then
STATE=$STATE_WARNING
elif [ $SSTATE -eq 2 -a $MSTATE -eq 2 ]; then
STATE=$STATE_CRITICAL
elif [ $SSTATE -eq 3 -a $MSTATE -eq 3 ]; then
STATE=$STATE_UNKNOWN
elif [ $SSTATE -eq 3 -o $MSTATE -eq 3 ]; then
STATE=$STATE_UNKNOWN
elif [ $SSTATE -eq 2 -o $MSTATE -eq 2 ]; then
STATE=$STATE_CRITICAL
elif [ $SSTATE -eq 1 -o $MSTATE -eq 1 ]; then
STATE=$STATE_WARNING
elif [ $SSTATE -eq 0 -o $MSTATE -eq 0 ]; then
STATE=$STATE_OK
else
STATE=$STATE_UNKNOWN
fi
# Distributing the ouput message and exiting with the nagios state
echo "$MEMMESSAGE / $SWAPMESSAGE"
exit $STATE