#!/bin/bash # ====================================================================== # # Check RAM usage # # requirements: # - bc # # DOC: https://access.redhat.com/solutions/406773 # explains /proc/meminfo # # SYNTAX: # check_memory [-w [int] -c [int] -s [int]] # -w Warning level for RAM usage [%]; default: 75 # -c Critical level for RAM usage; default: 90 # -s Critical level for SWAP usage; default: 5 # # ---------------------------------------------------------------------- # 2020-03-16 v1.0 <axel.hahn@iml.unibe.ch> # 2020-03-25 v1.1 <axel.hahn@iml.unibe.ch> added slab; fix usage based on MemAvailable # 2020-04-28 v1.2 <axel.hahn@iml.unibe.ch> allow small swap usage before critical; update output RAM + Swap usage # 2020-07-08 v1.3 <axel.hahn@iml.unibe.ch> FIX: set "ph." instead "ps." # 2020-07-16 v1.4 <axel.hahn@iml.unibe.ch> FIX: add unit MB in the performance data (see #3939) # ====================================================================== . `dirname $0`/inc_pluginfunctions # ---------------------------------------------------------------------- # FUNCTIONS # ---------------------------------------------------------------------- # get a memory status value # /proc/meminfo conatains values in kB - it is divided by 1024 # --> return values are MB function getMemvalue(){ echo `grep "^$1:" /proc/meminfo | awk '{ print $2 }' ` / 1024 | bc } # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- # --- check required tools ph.reqire bc # --- check param -h if [ "$1" = "-h" ]; then echo " usage: $0 [ -w value -c value -h ] -w Warning level for RAM usage [%]; default: 75 -c Critical level for RAM usage; default: 90 -s Critical level for SWAP usage; default: 5 -h this help " exit 0 fi # set default / override from command line params typeset -i iWarnLimit=` ph.getValueWithParam 75 w "$@"` typeset -i iCriticalLimit=` ph.getValueWithParam 90 c "$@"` typeset -i iCriticalSwap=` ph.getValueWithParam 5 s "$@"` # --- RAM typeset -i ramTotal=` getMemvalue MemTotal` typeset -i ramAvail=` getMemvalue MemAvailable` typeset -i ramUsed=$ramTotal-$ramAvail typeset -i ramFree=` getMemvalue MemFree` typeset -i ramBuffers=` getMemvalue Buffers` typeset -i ramCached=` getMemvalue Cached` typeset -i ramSReclaim=` getMemvalue SReclaimable` typeset -i ramUsage=`echo "($ramUsed) *100 / $ramTotal " | bc` # --- Swap typeset -i swapTotal=` getMemvalue SwapTotal` typeset -i swapFree=` getMemvalue SwapFree` typeset -i swapCached=` getMemvalue SwapCached` typeset -i swapUsage=0 if [ $swapTotal -gt 0 ]; then swapUsage=`echo "($swapCached)*100 / $swapTotal " | bc` fi # --- set status ph.setStatusByLimit $ramUsage $iWarnLimit $iCriticalLimit # if [ $swapCached -gt 0 ]; then if [ $swapUsage -ge $iCriticalSwap ]; then ph.setStatus "critical" fi if [ $swapTotal -gt 0 ]; then ph.status "RAM usage $ramUsage % of $ramTotal MB .. SWAP usage $swapCached MB = $swapUsage % of $swapTotal MB total" else ph.status "RAM usage $ramUsage % of $ramTotal MB (machine has no Swap)" fi # --- show details echo cat /proc/meminfo | egrep "^(Mem|Cache|Buffers|Swap|Slab|SReclaimable)" # --- performance data ph.perfadd "memory-total" "${ramTotal}MB" "" "" 0 ${ramTotal} ph.perfadd "memory-used" "${ramUsed}MB" "" "" 0 ${ramTotal} ph.perfadd "memory-avail" "${ramAvail}MB" "" "" 0 ${ramTotal} ph.perfadd "memory-free" "${ramFree}MB" "" "" 0 ${ramTotal} ph.perfadd "memory-buffers" "${ramBuffers}MB" "" "" 0 ${ramTotal} ph.perfadd "memory-cached" "${ramCached}MB" "" "" 0 ${ramTotal} ph.perfadd "memory-sreclaim" "${ramSReclaim}MB" "" "" 0 ${ramTotal} # add swap performance data only if swap is available if [ $swapTotal -gt 0 ]; then ph.perfadd "swap-total" "${swapTotal}MB" "" "" 0 ${swapTotal} ph.perfadd "swap-free" "${swapFree}MB" "" "" 0 ${swapTotal} ph.perfadd "swap-cached" "${swapCached}MB" "" "" 0 ${swapTotal} fi ph.exit # ----------------------------------------------------------------------