Home Directory Plugins System Metrics Memory check_proc_mem.sh: check process memory usage

Search Exchange

Search All Sites

Nagios Live Webinars

Let our experts show you how Nagios can help your organization.

Contact Us

Phone: 1-888-NAGIOS-1
Email: sales@nagios.com

Login

Remember Me

Directory Tree

check_proc_mem.sh: check process memory usage

Current Version
1.0
Last Release Date
2014-10-26
Compatible With
  • Nagios 1.x
  • Nagios 2.x
  • Nagios 3.x
  • Nagios 4.x
  • Nagios XI
  • Nagios Fusion
  • Nagios Reactor
  • Nagios Network Analyzer
  • Nagios Log Server
License
GPL
Hits
29984
Files:
FileDescription
check_proc_mem.shcheck_proc_mem.sh
Network Monitoring Software - Download Nagios XI
Log Management Software - Nagios Log Server - Download
Netflow Analysis Software - Nagios Network Analyzer - Download
Check memory used by a given process

Check more: https://www.dennyzhang.com/nagois_monitor_process_memory
Reviews (2)
bymacko003, November 2, 2021
First of all, thanks your work!

As I see if you use --cmdpattern, the script counts only the first process' mem usage. So if you run httpd or other services with fork/child calculate with it.

I prefer to send back the warning and critical in perform data. So I modified the script a bit.

if [ "$memVmRSS" -ge "$4" ]; then
echo "Memory: CRITICAL VIRT: $memVmSize MB - RES: $memVmRSS MB used!|RES=$(($memVmRSS*1024*1024));$(($2*1024*1024));$(($4*1024*1024));;"
$(exit 2)
elif [ "$memVmRSS" -ge "$2" ]; then
echo "Memory: WARNING VIRT: $memVmSize MB - RES: $memVmRSS MB used!|RES=$(($memVmRSS*1024*1024));$(($2*1024*1024));$(($4*1024*1024));;"
$(exit 1)
else
echo "Memory: OK VIRT: $memVmSize MB - RES: $memVmRSS MB used!|RES=$(($memVmRSS*1024*1024));$(($2*1024*1024));$(($4*1024*1024));;"
$(exit 0)
fi
Hi and thanks for a great plugin. I ran into an issue because I had renamed the file from check_proc_mem.sh to just check_proc_mem as I like them w/ no file extension since that is how they commonly are in the plugin directory. This caused an issue in the grep -v portion. I added some things to accommodate this changes, here is the modified script that will work just in case someone else happens to rename it:

#!/bin/bash -e
##-------------------------------------------------------------------
## File: check_proc_mem.sh
## Author : Denny
## Description :
## --
##
## Link: http://www.dennyzhang.com/nagois_monitor_process_memory
##
## Created :
## Updated: Time-stamp:
##-------------------------------------------------------------------
SCRIPTNAME=$(basename $0)

if [ "$1" = "-w" ] && [ "$2" -gt "0" ] && \
[ "$3" = "-c" ] && [ "$4" -gt "0" ]; then
pidPattern=${5?"specify how to get pid"}

if [ "$pidPattern" = "--pidfile" ]; then
pidfile=${6?"pidfile to get pid"}
pid=$(cat $pidfile)
elif [ "$pidPattern" = "--cmdpattern" ]; then
cmdpattern=${6?"command line pattern to find out pid"}
pid=$(ps -ef | grep "$cmdpattern" | grep -v grep | grep -v ${SCRIPTNAME} | head -n 1 | awk -F' ' '{print $2}')
elif [ "$pidPattern" = "--pid" ]; then
pid=${6?"pid"}
else
echo "ERROR input for pidpattern"
exit 2
fi

if [ -z "$pid" ]; then
echo "ERROR: no related process is found"
exit 2
fi

memVmSize=`grep 'VmSize:' /proc/$pid/status | awk -F' ' '{print $2}'`
memVmSize=$(($memVmSize/1024))

memVmRSS=`grep 'VmRSS:' /proc/$pid/status | awk -F' ' '{print $2}'`
memVmRSS=$(($memVmRSS/1024))

if [ "$memVmRSS" -ge "$4" ]; then
echo "Memory: CRITICAL VIRT: $memVmSize MB - RES: $memVmRSS MB used!|RES=$(($memVmRSS*1024*1024));;;;"
$(exit 2)
elif [ "$memVmRSS" -ge "$2" ]; then
echo "Memory: WARNING VIRT: $memVmSize MB - RES: $memVmRSS MB used!|RES=$(($memVmRSS*1024*1024));;;;"
$(exit 1)
else
echo "Memory: OK VIRT: $memVmSize MB - RES: $memVmRSS MB used!|RES=$(($memVmRSS*1024*1024));;;;"
$(exit 0)
fi

else
echo "${SCRIPTNAME}"
echo ""
echo "Usage:"
echo "${SCRIPTNAME} -w -c "
echo ""
echo "Below: If tomcat use more than 1024MB resident memory, send warning"
echo "${SCRIPTNAME} -w 1024 -c 2048 --pidfile /var/run/tomcat7.pid"
echo "${SCRIPTNAME} -w 1024 -c 2048 --pid 11325"
echo "${SCRIPTNAME} -w 1024 -c 2048 --cmdpattern \"tomcat7.*java.*Dcom\""
echo ""
echo "Copyright (C) 2014 DennyZhang (denny.zhang001@gmail.com)"
exit
fi
## File - check_proc_mem.sh ends