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

check_mem.sh

Rating
8 votes
Favoured:
0
Current Version
2.0
Last Release Date
2015-07-14
Compatible With
  • Nagios 2.x
  • Nagios 3.x
License
GPL
Hits
107750
Files:
FileDescription
check_memNagios plugin
check_mem.phppnp template
check_mem.pngcheck_mem.png
Network Monitoring Software - Download Nagios XI
Log Management Software - Nagios Log Server - Download
Netflow Analysis Software - Nagios Network Analyzer - Download
check_mem.sh
check_mem is a bash script that checks buffer, cache and used memory with performance data enable
root@localhost # ./check_mem
[MEMORY] Total: 7713 MB - Used: 2402 MB - 31% [SWAP] Total: 8189 MB - Used: 0 MB - 0% | MTOTAL=8087736320;;;; MUSED=2516549632;;;; MCACHE=2323607552;;;; MBUFFER=140218368;;;; STOTAL=8587833344;;;; SUSED=0;;;;



To use it with nrpe:


add command:

# check_nrpe_memory
define command {
command_name check_nrpe_memory
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c memory
}


define service:

# Memory
define service {
use default-service
host_name some_host
service_description Memory Usage
check_command check_nrpe_memory
}

and define nrpe command:

command[memory]=/opt/plugins/check_mem -w 80 -c 90
Reviews (7)
byFonant, September 2, 2015
Nice script, however I've noticed that my server with 16G of RAM results in the "total" value in bytes from the "free" command being truncated: a bug in "free" rather this script. This results in erroneous values from this script, and the "USED" value can go negative.

I've updated my copy so it uses kilobytes instead of bytes for the fine-resolution numbers.
bydot, November 17, 2014
2 of 2 people found this review helpful
Why do you call 'free' eight times? Why not 100?
substitute this stuff
"
memTotal_b=`free -b |grep Mem |awk '{print $2}'`
memFree_b=`free -b |grep Mem |awk '{print $4}'`
memBuffer_b=`free -b |grep Mem |awk '{print $6}'`
memCache_b=`free -b |grep Mem |awk '{print $7}'`

memTotal_m=`free -m |grep Mem |awk '{print $2}'`
memFree_m=`free -m |grep Mem |awk '{print $4}'`
memBuffer_m=`free -m |grep Mem |awk '{print $6}'`
memCache_m=`free -m |grep Mem |awk '{print $7}'`
"

with

mem=`free -m |grep Mem`
and
"free -m |grep Mem"
with echo $mem
byniemi, November 5, 2014
Nagios 3.0.6
making some changes, remove some spaces and ";" and it works on my nagios. Thanks to Athor!
======================================[code]=======================
#!/bin/bash

if [ "$1" = "-w" ] && [ "$2" -gt "0" ] && [ "$3" = "-c" ] && [ "$4" -gt "0" ]; then

memTotal_b=`free -b |grep Mem |awk '{print $2}'`
memFree_b=`free -b |grep Mem |awk '{print $4}'`
memBuffer_b=`free -b |grep Mem |awk '{print $6}'`
memCache_b=`free -b |grep Mem |awk '{print $7}'`

memTotal_m=`free -m |grep Mem |awk '{print $2}'`
memFree_m=`free -m |grep Mem |awk '{print $4}'`
memBuffer_m=`free -m |grep Mem |awk '{print $6}'`
memCache_m=`free -m |grep Mem |awk '{print $7}'`

memUsed_b=$(($memTotal_b-$memFree_b-$memBuffer_b-$memCache_b))
memUsed_m=$(($memTotal_m-$memFree_m-$memBuffer_m-$memCache_m))

memUsedPrc=$((($memUsed_b*100)/$memTotal_b))


if [ "$memUsedPrc" -ge "$4" ]; then
echo "Memory: CRITICAL Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used!|Total=$memTotal_b;;;Used=$memUsed_b;;;Cache=$memCache_b;;;Buffer=$memBuffer_b;;;"
$(exit 2)
elif [ "$memUsedPrc" -ge "$2" ]; then
echo "Memory: WARNING Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used!|Total=$memTotal_b;;;Used=$memUsed_b;;;Cache=$memCache_b;;;Buffer=$memBuffer_b;;;"
$(exit 1)
else
echo "Memory: OK Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used|Total=$memTotal_b;;;Used=$memUsed_b;;;Cache=$memCache_b;;Buffer=$memBuffer_b;;;"
$(exit 0)
fi

else
echo "check_mem v1.1"
echo ""
echo "Usage:"
echo "check_mem.sh -w -c "
echo ""
echo "warnlevel and critlevel is percentage value without %"
echo ""
echo "Copyright (C) 2012 Lukasz Gogolin (lukasz.gogolin@gmail.com)"
exit
fi
======================================[code]=======================
bywhereisaaron, April 12, 2014
1 of 1 people found this review helpful
I tested this plug-in and liked it a lot. There are others but this one doesn't have any dependencies other than 'bash' and 'free', and makes a nice chart.

I felt it was a little inefficient so I made some tweaks to reduce the number of forks required. I also tweaked the chart. My changes are on Github.

https://github.com/whereisaaron/linux-check-mem-nagios-plugin
byHFroyen, October 17, 2013
I also cat the check_mem.sh and touch new file and paste it in. In the nrpe_local.cfg I put /path/to/plugins/check_mem.sh -w value -c value
I put the PHP file in the pnp-templates path, made command and service definition.

Is it also possible to do this for Windows Servers via NSClient++ or NSCP?

Great job on this!
Love the plugin and its very useful. Only in my setup it produces separate graphs for every type of memory metric. On your sample image you have a consolidated graphs where different metrics are shown in different colors. How did you do it? Thanks in advance!
bydick, January 25, 2013
2 of 2 people found this review helpful
This plugin works nicely with very good looking pnp charting support. Only problems I had were not related to the code itself.

The check_mem.sh script download comes in a weird encoding that does not work in Linux.

check_mem.sh: UTF-8 Unicode (with BOM) text, with CRLF line terminators

I didn't feel like looking around to convert this so I just cat the file and pasted into a new file, which worked fine.

Also note that the pnp template file, check_nrpe_memory.php, should be moved to /etc/pnp4nagios/templates or where your pnp configs are set to. Also unless you name the check nrpe_memory, you're going to have to rename the file to check_.php

Thanks for the great plugin and pnp template.