Check JVM with Nagios Monitoring

Remote Nagios monitoring can be done in two different ways:

  • NRPE
  • Check by SSH

I prefer SSH monitoring for security reasons, but NRPE consumes fewer resources. I assume you have installed Nagios server and Nagios plugins on the remote server in /usr/lib/nagios/plugins. I use Nagios 3.5.1 for this demo.

Nagios SSH Connection

nagios user connects over SSH to the remote server on nagios user, so make sure you authorize its access on the remote machine. We have nagios user public key in /var/lib/nagios/.ssh/id_rsa.pub, so I will place this key in /home/nagios/.ssh/authorized_keys on the server that I want to monitor.

Define check_with_ssh command

We will define another command that use check_by_ssh plugin and run a command on remote server. Add this command in /etc/nagios3/commands.cfg.

Now with this custom command, we can run any Nagios plugin over SSH. For example, to monitor load on the remote server, we will have:

 

check_jmx

Documentation

I use this plugin to monitor used heap memory. You need a jar file and a script that you run it. Go to the remote server in /usr/lib/nagios/plugins and download the files here.

To download the Jar file:

To download the script:

Make it executable with chmod +x check_jmx  and change the owner of jar file with chown nagios:nagios *jmx*

To test the script, run this command:

changing port with your Tomcat JMX Port.

This command should return an output like this:

Anyway, previous output cannot be interpreted by Nagiosgraph add-on. If you use Nagiosgraph, you need to modify check_jmx, or create check_jmx2 with this contents:

Now, same command that you run, it will return this output:

It’s same result, but with this output, Nagiosgraph can get the performance data and make graphs.

Now, let’s create a Nagios service that check automatically. Add this in your cfg:

I disabled the Nagios notifications for this service, so if you want warning/critical notifications, remove that line.

In Nagiosgraph, we will see something like this:

 

check_jvm

Documentation

I use this plugin to check threads and non-heap memory used. Monitoring with this plugin requires running check_jvm with the user that run the Java application. To make sure nagios user can run this plugin with different user, you have to add an exception in /etc/sudoers, like this:

Now nagios can su  to run check_jvm. But we doesn’t have it yet. Let’s download it.

You need a jar file and check_jvm that you run it. Go to the remote server in /usr/lib/nagios/plugins and download the files here.

To download the Jar file:

To download check_jvm plugin:

Edit JvmInspector.jar path in check_jvm.

Make it executable with chmod +x check_jvm  and change the owner of jar file with chown nagios:nagios *jmx*

Next step is to find the name of your Java application. Run this as the user this Java applications run:

java -jar JvmInspector.jar all

This will output some Java processes with a lot of details (name, thread count, heap memory, non-heap memory etc). Get the name of your Java application and run check_jvm. You will have something like this:

-n stands for name. -p argument is for property. It can be threads, classes, heap, non-heap, sessions. -w stands for warning, and -c stands for critical.

The output of the last command will be something like this:

OK 199 |threads=199;;;

Now, let’s create Nagios services that check automatically threads and non-heap memory.

 

Examples of graphs for threads and for non-heap memory:

Leave a Reply

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