How to monitor server memory usage with Nagios Remote Plugin Executor (NRPE)

In a previous tutorial, we have seen how we can set up Nagios Remote Plugin Executor (NRPE) in an existing Nagios setup. However, the scripts and plugins needed to monitor memory usage do not come with stock Nagios. In this tutorial, we will see how we can configure NRPE to monitor RAM usage of a remote server.

The script that we will use for monitoring RAM is available at Nagios Exchange, as well as the creators' Github repository.

Assuming that NRPE has already been set up, we start the process by downloading the script in the server that we want to monitor.

Preparing Remote Servers

On Debain/Ubuntu:

# cd /usr/lib/nagios/plugins/
# wget https://raw.githubusercontent.com/justintime/nagios-plugins/master/check_mem/check_mem.pl
# mv check_mem.pl check_mem
# chmod +x check_mem

On RHEL/CentOS:

# cd /usr/lib64/nagios/plugins/ (or /usr/lib/nagios/plugins/ for 32-bit)
# wget https://raw.githubusercontent.com/justintime/nagios-plugins/master/check_mem/check_mem.pl
# mv check_mem.pl check_mem
# chmod +x check_mem

You can check whether the script generates output properly by manually running the following command on localhost. When used with NRPE, this command is supposed to check free memory, warn when free memory is less than 20%, and generate critical alarm when free memory is less than 10%.

# ./check_mem -f -w 20 -c 10
OK - 34.0% (2735744 kB) free.|TOTAL=8035340KB;;;; USED=5299596KB;6428272;7231806;; FREE=2735744KB;;;; CACHES=2703504KB;;;;

If you see something like the above as an output, that means the command is working okay.

Now that the script is ready, we define the command to check RAM usage for NRPE. As mentioned before, the command will check free memory, warn when free memory is less than 20%, and generate critical alarm when free memory is less than 10%.

# vim /etc/nagios/nrpe.cfg

For Debian/Ubuntu:

command[check_mem]=/usr/lib/nagios/plugins/check_mem  -f -w 20 -c 10

For RHEL/CentOS 32 bit:

command[check_mem]=/usr/lib/nagios/plugins/check_mem  -f -w 20 -c 10

For RHEL/CentOS 64 bit:

command[check_mem]=/usr/lib64/nagios/plugins/check_mem  -f -w 20 -c 10

Preparing Nagios Server

In the Nagios server, we define a custom command for NRPE. The command can be stored in any directory within Nagios. To keep the tutorial simple, we will put the command definition in /etc/nagios directory.

For Debian/Ubuntu:

# vim /etc/nagios3/conf.d/nrpe_command.cfg
define command{
        command_name check_nrpe
        command_line /usr/lib/nagios/plugins/check_nrpe -H '$HOSTADDRESS$'  -c '$ARG1$'
}

For RHEL/CentOS 32 bit:

# vim /etc/nagios/objects/nrpe_command.cfg
define command{
        command_name check_nrpe
        command_line /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ 
        }

For RHEL/CentOS 64 bit:

# vim /etc/nagios/objects/nrpe_command.cfg
define command{
        command_name check_nrpe
        command_line /usr/lib64/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ 
        }

Now we define the service check in Nagios.

On Debian/Ubuntu:

# vim /etc/nagios3/conf.d/nrpe_service_check.cfg
define service{
        use                            local-service
        host_name                      remote-server 
        service_description            Check RAM
        check_command                  check_nrpe!check_mem
}

On RHEL/CentOS:

# vim /etc/nagios/objects/nrpe_service_check.cfg
define service{
        use                            local-service
        host_name                      remote-server 
        service_description            Check RAM
        check_command                  check_nrpe!check_mem
}

Finally, we restart the Nagios service.

On Debian/Ubuntu:

# service nagios3 restart

On RHEL/CentOS 6:

# service nagios restart

On RHEL/CentOS 7:

# systemctl restart nagios.service

Troubleshooting

Nagios should start checking RAM usage of a remote-server using NRPE. If you are having any problem, you could check the following.

  1. Make sure that NRPE port is allowed all the way to the remote host. Default NRPE port is TCP 5666.
  2. You could try manually checking NRPE operation by executing the check_nrpe command: /usr/lib/nagios/plugins/check_nrpe -H remote-server
  3. You could also try to run the check_mem command manually: /usr/lib/nagios/plugins/check_nrpe -H remote-server –c check_mem
  4. In the remote server, set debug=1 in /etc/nagios/nrpe.cfg. Restart the NRPE service and check the log file /var/log/messages (RHEL/CentOS) or /var/log/syslog (Debain/Ubuntu). The log files should contain relevant information if there is any configuration or permission errors. If there are not hits in the log, it is very likely that the requests are not reaching the remote server due to port filtering at some point.

To sum up, this tutorial demonstrated how we can easily tune NRPE to monitor RAM usage of remote servers. The process is as simple as downloading the script, defining the commands, and restarting the services. Hope this helps.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Sarmed Rahman is an IT professional based in Australia. He writes tutorial articles on technology every now and then from a belief that knowledge grows through sharing. During his free time, he loves gaming and spending time with his friends.

8 thoughts on “How to monitor server memory usage with Nagios Remote Plugin Executor (NRPE)

  1. Nice post. Shows how to take advantage of using NRPE which is briliant extension to Nagios.
    But IMHO bad example with RAM monitoring on Linux. As we know Linux will very soon allocate almost all free memory. On my servers I only monitor if SWAP is in use.
    However, keep up with good work on this blog :)

    • SNMP uses udp while NRPE works on tcp and over ssl. If you have heavy loaded system, SNMP will fail. That was the main reason why I replaced SNMP by NRPE.
      With NRPE you can do much more than memory monitoring. E.g. I am using NRPE to check if a database server is up. I do not need to expose the database server to the Internet just for monitoring purposes. NRPE connects to database locally instead of using check_tcp plugin over network. Check online docs and you will see how powerful tool NRPE is.

    • Of course you could monitor memory using SNMP. This tutorial focused on how to do the same using NRPE. This way, your existing Nagios setup can also be used to monitor RAM, and generate alerts as needed.

  2. What Piotr said; see https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Tuning_and_Optimizing_Red_Hat_Enterprise_Linux_for_Oracle_9i_and_10g_Databases/chap-Oracle_9i_and_10g_Tuning_Guide-Memory_Usage_and_Page_Cache.html
    Specifically: "Linux always tries to use RAM to speed up disk operations by using available memory for buffers (file system metadata) and cache (pages with actual contents of files or block devices). This helps the system to run faster because disk information is already in memory which saves I/O operations. If space is needed by programs or applications like Oracle, then Linux will free up the buffers and cache to yield memory for the applications."

  3. Hi,

    Can I pass arguments from Nagios server to client via NRPE?
    I tried it this way. Can this be done?
    Please let me know what am I doing incorrectly.

    Contents of ".cfg" file on Nagios server::

       define service {
              use                             generic-service
              host_name                       server-to-monitor.com
              service_description             RAM
              check_command                   check_nrpe!check_mem!20!10
              }
    

    and contents of nrpe.cfg on monitoring server::

     
            command[check_mem]=/usr/lib64/nagios/plugins/check_mem -C -f -w $ARG1$ -c $ARG2$
    

    Then, restarted the Nagios and NRPE services.

    The Nagios UI shows "Status" as "Unknown" and "Status Information" as "*** You must define WARN and CRITICAL levels! "

    Please help me fix this.

Leave a comment

Your email address will not be published. Required fields are marked *