Nagios Memeory Check Plugin – download & install


When I noticed that Nagios had no memory check plugin I decided to make a simple bash plugin for Nagios to check how much memory my machines were using. It works by reading /proc/meminfo so it should work on every system were you can read /proc/meminfo (most Linux and Unix based systems)
This plugin is released under the terms of the GPLv2 so please feel free to edit it as needed, you can download it from here. If you haven’t installed a plugin for Nagios before I’ll go through a simple tutorial now. First download this plugin with the below link or copy the below code to your Nagios plugin directory (Ubuntu 11.04 location: /usr/lib/nagios/plugins/)




Download Plugin

#!/bin/bash

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License only.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

PROGNAME=`basename $0`
VERSION="Version 1.0"
AUTHOR="2011, Tyler Allen (http://www.the-tech-tutorial.com/)"

LV_W=100;
LV_C=100;


#Print Version
print_version() {
    echo "$VERSION $AUTHOR"
}

#Print Help
usage(){
        echo $PROGNAME $VERSION
        echo $AUTHOR
        echo
        echo This is a Nagios plugin that will check the curremt memory usage of the system.
        echo
        echo OPTIONS:
        echo  -h Shows this help
        echo  -v Shows the Version
        echo  -w sets the warning level
        echo  -c sets the critical level
}

#Parmature Getter
while getopts "hvw:c:" opt; do
  case $opt in
    h)
      usage
      exit
      ;;
    v)
      print_version
      exit
      ;;
    w)
      LV_W=$OPTARG
      ;;
    c)
      LV_C=$OPTARG
      ;;

    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :) 
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

#Check the diffrence between the LV values
    if [ ${LV_W} -gt ${LV_C} ]
    then
        echo "Please adjust levels. The critical level must be higher than the warning level!"
        exit 666
    fi

#Values must be between 0 and 100
    if [ "$LV_W" -lt 0 -o "$LV_W" -gt 100 -o "$LV_C" -lt 0 -o "$LV_C" -gt 100 ]
    then
        echo "Warning and critical level values must be between 0 and 100."
        exit 666
    fi

#Get the figures
MEM_TOTAL=`grep "^MemTotal" /proc/meminfo|awk '{print $2}'`
TMP_MEM_FREE=`grep "^MemFree" /proc/meminfo|awk '{print $2}'`
TMP_MEM_USED=`expr $MEM_TOTAL - $TMP_MEM_FREE`
BUFFERS=`grep "^Buffers" /proc/meminfo|awk '{print $2}'`
CACHED=`grep "^Cached" /proc/meminfo|awk '{print $2}'`

P_MEM_FREE=`echo "scale=2; $TMP_MEM_FREE / $MEM_TOTAL * 100" | bc -l | sed 's/.[0-9][0-9]//'`
P_MEM_USED=`echo "scale=0; 100 - $P_MEM_FREE" | bc -l`

if [ ! -z "$LV_W" -a ! -z "$LV_C" ]
    then
        if [ ${P_MEM_USED} -ge ${LV_W} -a ${P_MEM_USED} -lt ${LV_C} ]
        then
            echo "WARNING - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_WR
        elif [ ${P_MEM_USED} -ge ${LV_C} ]
        then
            echo "CRITICAL - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_CR
        else
            echo "OK - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$LV_W;$LV_C 'mem_free'=$P_MEM_FREE"
            exit $ST_OK
        fi
    else
            echo "OK - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED 'mem_free'=$P_MEM_FREE"
            exit $ST_OK
    fi

Now you have saved this in your plugin folder we need to configure Nagios to read the new plugin, to do this we need to add some information to the commands.cfg file (Ubuntu 11.04 location: /etc/nagios3/commands.cfg). Once you’ve opened the file add the following lines:



#Memory Check Plugin command definition
define command {
        command_name chk_mem
        command_line /usr/lib/nagios/plugins/chk_mem.sh -w '$ARG1$' -c '$ARG2$'
}

Now we need to add the new command to a services attached to a host, first add the following lines to your services config file, for this example I am only going to add this service to the local host monitor (Ubuntu 11.04 location: /etc/nagios3/conf.d/localhost_nagios2.cfg)

#Define a service to check how much memory the system has left
#warning if >70% used critical if >90% used
define service{
        use generic-service         ; Name of service template to use
        host_name                   localhost
        service_description         Memory Use
        check_command               chk_mem!75%!90%
}

Now all you need to do is restart Nagios and you should see your new plugin in action.

sudo /etc/init.d/nagios3 restart

, , , , , , , , , , , ,

  1. No comments yet.

You must be logged in to post a comment.