#!@@GOODSH@@
# -*- sh -*-
: << =cut

=head1 NAME

jmx_ - Wildcard plugin to monitor Java application servers via JMX

=head1 APPLICABLE SYSTEMS

Tested with Tomcat 4.1/5.0/5.5/6.0 on Sun JVM 5/6 and OpenJDK.

Any JVM that supports JMX should in theory do.

Needs nc in path for autoconf.

=head1 CONFIGURATION

  [jmx_*]
    env.ip 127.0.0.1
    env.port 5400
    env.category jvm
    env.username monitorRole
    env.password SomethingSecret

    env.JRE_HOME /usr/lib/jvm/java-6-sun/jre
    env.JAVA_OPTS -Xmx128m

Needed configuration on the Tomcat side: add

  -Dcom.sun.management.jmxremote \
  -Dcom.sun.management.jmxremote.port=5400 \
  -Dcom.sun.management.jmxremote.ssl=false \
  -Dcom.sun.management.jmxremote.authenticate=false

to CATALINA_OPTS in your startup scripts.

Replace authenticate=false with 
  -Dcom.sun.management.jmxremote.password.file=/etc/tomcat/jmxremote.password \
  -Dcom.sun.management.jmxremote.access.file=/etc/tomcat/jmxremote.access
 ...if you want authentication.

jmxremote.password:
 monitorRole SomethingSecret

jmxremote.access:
 monitorRole readonly

You may need higher access levels for some counters, notably ThreadsDeadlocked.

=head1 BUGS

No encryption supported in the JMX connection.

The plugins available reflect the most interesting aspects of a
JVM runtime.  This should be extended to cover things specific to
Tomcat, JBoss, Glassfish and so on.  Patches welcome.

=head1 AUTHORS

=encoding UTF-8

Mo Amini, Diyar Amin and Younes Hajji, Høgskolen i Oslo/Oslo
University College.

Shell script wrapper and integration by Erik Inge Bolsø, Redpill
Linpro AS.

Previous work on JMX plugin by Aleksey Studnev. Support for
authentication added by Ingvar Hagelund, Redpill Linpro AS.

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf suggest

=cut

MUNIN_JAR="@@JAVALIBDIR@@/munin-jmx-plugins.jar"

if [ "x$JRE_HOME" != "x" ] ; then
  JRE=$JRE_HOME/bin/java
  export JRE_HOME=$JRE_HOME
fi
JAVA_BIN=${JRE:-@@JAVARUN@@}

ip=${ip:-127.0.0.1}
port=${port:-5400}

if [ "x$1" = "xsuggest" ] ; then
  echo MultigraphAll
  exit 0
fi

if [ "x$1" = "xautoconf" ] ; then
    $JAVA_BIN -cp $MUNIN_JAR $JAVA_OPTS org.munin.plugin.jmx.PortProbe $port $ip
    CONNECT=$?

    $JAVA_BIN -? >/dev/null 2>&1
    JAVA=$?
    if [ $JAVA -ne 0 ] ; then
      echo "no (java runtime not found at $JAVA_BIN)"
      exit 0
    fi

    if [ ! -e $MUNIN_JAR ] ; then
      echo "no (munin jmx classes not found at $MUNIN_JAR)"
      exit 0
    fi

    if [ $CONNECT -eq 0 ] ; then
      echo "yes"
      exit 0
    else
      echo "no (connection to $ip:$port failed)"
      exit 0
    fi
fi

if [ "x$1" = "xconfig" ] ; then
  param=config
else
  param=Tomcat
fi

scriptname=${0##*/}
jmxfunc=${scriptname##*_}
prefix=${scriptname%_*}

if [ "x$jmxfunc" = "x" ] ; then
  echo "error, plugin must be symlinked in order to run"
  exit 1
fi

ip=$ip port=$port $JAVA_BIN -cp $MUNIN_JAR $JAVA_OPTS org.munin.plugin.jmx.$jmxfunc $param $prefix
