Monitoring Liferay with Nagios, Jolokia and JMX4Perl

How do I monitor Liferay? That’s a question I’ve heard a lot lately. Well the standard way of getting some information about the application is by using JMX. The downside of JMX is that it’s a Java only standard and the only remote connection is by using RMI which doesn’t really sit well with non Java monitoring software like very popular Nagios. Another hurdle might be that your network admin might not be inclined to open up RMI access to the jvm.

There’s a nice agent called Jolokia that can provide a http bridge to JMX. You can install it as java agent in pretty much any java app or deploy it as a webapp. With Jolokia installed you can query any MBeans for their values using a simple http GET and get the data as JSON objects. JMX4Perl is a perl module and scripts that provide a easy way to run those queries through Jolokia. One of those scripts is check_jmx4perl which can be used in nagios service checks.

Okay so now we know that we are going to need Nagios, Jolokia and JMX4Perl to monitor the Liferay JVM but what should we monitor? Well that depends on what information you are interested in but at minimum I would monitor ajp or http thread usage as well as heap utilization. Just by monitoring those values you’ll know when your JVM is becomes unresponsive and can also get some early warning that there’s issues for example heap usage goes over warning threashold and never returns to normal or keeps constantly going over the threshold which could indicate they you don’t have enough heap allocated.

I’m going to assume that you have  nagios installed and configured and I will only go through how to install Jolokia and configure some checks for threads and heap. So let’s start by installing JMX4Perl.

Installing JMX4Perl is pretty simple with cpan. You just launch cpan command line client and install it like this:

cpan> install JMX::Jmx4Perl

Next you’ll need to download Jolokia and deploy the jolokia.war to your app server. For this example I’m going to assume that you are using Tomcat 7. Once you’ve deployed Jolokia it’s usually good idea to restrict who can query it. For this example we are just going to restrict it to a certain IP address (the Nagios server) and limit it to read operations only. Since I don’t like modifying the war we are going to tell Jolokia where to find the policy file through a context parameter. Create a jolokia.xml in tomcat/conf/Catalina/localhost with following content:

<Context path="/jolokia">
        <Parameter name="policyLocation" value="file:///etc/jolokia/jolokia-access.xml" />

That tells Jolokia to look for the policy file jolokia-access.xml from /etc/jolokia/jolokia-access.xml. This is great when you are running multiple tomcats in the same server and want them to share the jolokia policy file.

Now go ahead and create the jolokia-access.xml in /etc/jolokia

<?xml version="1.0" encoding="utf-8"?>
                <host>[YOUR NAGIOS SERVER IP]</host>

Next we need to create configuration for jmx4perl. In /etc/jmx4perl/jmx4perl.cfg we are going to include some preconfigured checks extend them. Tomcat 7 you need to add quotes around the thread pool name. We also need to set warning and critical levels for alerts. You’ll also need to add a Server for each tomcat you want to monitor.

# Default definitions
include default/memory.cfg
include default/tomcat.cfg

# ==========================
# Check definitions

<Check tc7_connector_threads>
	Use = relative_base($1,$2)
	Label = Connector $0 : $BASE
	Value = Catalina:name="$0",type=ThreadPool/currentThreadCount
	Base = Catalina:name="$0",type=ThreadPool/maxThreads
	Critical 95
	Warning 90

<Check j4p_memory_heap>
	Use memory_heap
	Critical 95
	Warning 90

<Server tomcat>
	Url http://MY_TOMCAT_HOSTNAME:8080/jolokia

Then in /etc/nagios3/commands.cfg we’ll need to add a check command for jmx4perl and we’ll use the check_jmx4perl script to do that.

define command {
	command_name    check_j4p_cmd
	command_line    /usr/local/bin/check_jmx4perl --unknown-is-critical --config /etc/jmx4perl/jmx4perl.cfg --server $ARG1$ --check $ARG2$ $ARG3$

Then we need to define a service to monitor in /etc/nagios3/conf.d/host-MY_TOMCAT_HOSTNAME.cfg

define service {
	use generic-service
	service_description Tomcat Heap Memory
	check_command check_j4p_cmd!tomcat!j4p_memory_heap!x

define service {
	use generic-service
	service_description Tomcat AJP Threads
	check_command check_j4p_cmd!tomcat!tc7_connector_threads!ajp-bio-8009

The check above is for your tomcat heap and the other one is for Tomcat 7 AJP threads.

Now you should all the pieces to implement your own monitoring using Nagios, Jolokia and JMX4Perl. You should also remember that you can apply this to any JEE application not just Liferay.

  • Mark Bednarski

    Hi Mika

    I tried to follow your explanations on my ubuntu 14.04 machine, but when I run:
    cpan> install JMX::Jmx4Perl

    Unfortunately I get the following error.
    By any chance you have an idea what could have gone wrong?

    Mark Building R/RO/ROLAND/jmx4perl-1.11.tar.gz

    Warning: Prerequisite ‘XML::Twig => 0′ for ‘ROLAND/jmx4perl-1.11.tar.gz’ failed when processing ‘MIROD/XML-Twig-3.48.tar.gz’ with ‘make_test => NO one dependency not OK (XML::Parser); additionally test harness failed’. Continuing, but chances to succeed are limited.

    Warning: Prerequisite ‘XML::LibXML => 0′ for ‘ROLAND/jmx4perl-1.11.tar.gz’ failed when processing ‘SHLOMIF/XML-LibXML-2.0118.tar.gz’ with ‘writemakefile => NO — No ‘Makefile’ created

    ‘. Continuing, but chances to succeed are limited.

    Building jmx4perl


    ./Build — OK

    Running Build test

    t/10_handler.t ……… ok

    t/20_alias.t ……….. ok

    t/30_request.t ……… ok

    t/40_check_jmx4perl.t .. skipped: Nagios::Plugin not installed

    t/50_config.t ………. ok

    t/60_parse_name.t …… ok

    t/70_pod_syntax.t …… skipped: these tests are for release candidate testing

    All tests successful.

    Files=7, Tests=66, 1 wallclock secs ( 0.04 usr 0.00 sys + 0.50 cusr 0.05 csys = 0.59 CPU)

    Result: PASS


    Tests succeeded but 3 dependencies missing (XML::Twig,XML::LibXML,Nagios::Plugin)


    [dependencies] — NA

    Running Build install

    make test had returned bad status, won’t install without force

    Could not read metadata file. Falling back to other methods to determine prerequisites

    Failed during this command:

    SHLOMIF/XML-LibXML-2.0118.tar.gz : writemakefile NO — No ‘Makefile’ created

    TODDR/XML-Parser-2.44.tar.gz : make NO

    MIROD/XML-Twig-3.48.tar.gz : make_test NO one dependency not OK (XML::Parser); additionally test harness failed

    ROLAND/jmx4perl-1.11.tar.gz : make_test NO 3 dependencies missing (XML::Twig,XML::LibXML,Nagios::Plugin

    • Mika Koivisto

      Hi Mark

      It seems it’s missing few dependencies XML::Twig, XML::LibXML and Nagios::Plugin. You can install them via CPAN also or you can use APT which is what I usually prefer. I only use CPAN for perl modules not found as a debian package. Just run apt-get install libxml-twig-perl libxml-libxml-perl libnagios-plugin-perl and you should have those missing libs.

      • Mark Bednarski

        This did the trick. Thank you very much!