#!/usr/bin/perl 
# nagios: +epn

=head1 NAME 

check_jmx4perl - Nagios check using jmx4perl for accessing JMX information 

=head1 SYNOPSIS 

 # Check used heap memory for absolute values
 check_jmx4perl --url http://localhost:8888/j4p \
                --name memory_used \
                --mbean java.lang:type=Memory \
                --attribute HeapMemoryUsage \ 
                --path used \
                --critical 10000000 \
                --warning   5000000 

 
 # Check for string values by comparing them literally
 check_jmx4perl --url http://localhost::8888/j4p \
                --mbean myDomain:name=myMBean \
                --attribute stringAttribute \
                --string \
                --critical 'Stopped' \
                --warning '!Started'

 # Check used heap memory for absolute values by using an alias
 check_jmx4perl --url http://localhost:8888/j4p \
                --alias MEMORY_HEAP_USED \
                --critical 10000000 \
                --warning   5000000 

 # Check that the used heap memory is below 80% of the available memory
 check_jmx4perl --url http://localhost:8888/j4p \
                --alias MEMORY_HEAP_USED \
                --base MEMORY_HEAP_MAX \ 
                --critical :80

 # Check that no more than 5 threads are started in a minute
 check_jmx4perl --url http://localhost:8888/j4p \
                --alias THREAD_COUNT_STARTED \
                --delta 60 \
                --critical 5

 # Execute a JMX operation on an MBean and use the return value for threshold
 check_jmx4perl --url http://localhost:8888/j4p \
                --mbean jmx4perl:type=Runtime \
                --operation getNrQueriesFor \
                --critical 10 \
                "operation" \
                "java.lang:type=Memory" \
                "gc" 

=head1 DESCRIPTION

Command for providing Nagios conmpliant output for JMX response fetched via
L<JMX::Jmx4Perl>. It knows about critical (via C<--critical>) and warning (via
C<--warning>) thresholds. 

You can also use direct string comparison. In this case C<--critical> and
C<--warning> are not treated as numerical values but as string types. They are
compared literally against the value retrieved and yield the corresponding
Nagios status if matched. If the threshold is given with a leading C<!>, the
condition is negated. E.g. a C<--critical '!Running'> given returns a
C<CRITICAL> if the value is I<not> equals to C<Running>. As a final option you
can also use a regular expression by using C<qr/.../> as threshold value
(substitute C<...> with the pattern to used for comparison). Boolean values are
returned as C<true> or C<false> strings from the agent, so you can check for
them as well with this kind of string comparison.

Whether string comparison or a numerical check is used is determined
automatically based on the value returned. You can enforce a particular mode by
providing the option C<--numeric> or C<--string> respectively.

Values obtained from the server can be interpreteted as relative values when
C<--base> is given. The argument provided here is first tried as an alias name
or checked as an absolute, numeric value. Alternatively, you can use a full
MBean/Attribute/Path specification by using a C</> as separator, e.g. 

  ... --base java.lang:type=Memory/HeapMemoryUsage/max ...

If one of these parts (the path is optional) contains a slash within its name,
the part must be URI encoded. I.e. all parts are URI decoded before usages as
naming pattern. In any case, using an alias or direct MBean names, the base
value is fetched first for calculating the relative value. For a numeric value
given here this is of course not necessary.

C<--delta> switches on incremental mode in which the difference between two
following values is used. The optional argument are interpreted as seconds
which are used for calculating the speed of growth. If not given, only the
difference between the current and the latest value is taken. C<--delta>
doesn't work with C<--base>.

With the option C<--operation> a JMX operation is triggered and the return
value used for threshold calculations. Additionally, if use an operation
alias with C<--alias>, you can ommit C<--operation> and C<--mbean>. Any
arguments required for the operation need to be provided as additional
arguments. 

The output of C<check_jmx4perl> can be highly customized. First of all, you can
provide a unit-of-measurement with the option C<--unit>. This specifies how the
the attribute or an operation's return value should be interpreted. The
units available are

  B  - Byte
  KB - Kilo Byte
  MB - Mega Byte
  GM - Giga Byte
  TM - Terra Byte
  
  us - Microseconds
  ms - Milliseconds
  s  - Seconds
  m  - Minutes
  h  - Hours
  d  - Days

The unit will be used for performance data as well as for the plugin's
output. Large numbers are converted to larger units (and reverse for small
number that are smaller than 1). E.g. C<2048 KB> is converted to C<2
MB>. However, beautifying by conversion is I<only> performed for the plugin
output, B<not> for the performance data for which no conversions happens at all.

Beside unit handling, you can provide your own label for the Nagios output via
C<--label>. The provided option is interpreted as a pattern with the following
placeholders: 

 %v   the absolute value
 %r   the relative value for relative calculations (--base)
 %u   the value's unit for the output when --unit is used
 %w   the the base value's for the output unit when --unit is used
 %b   the absolut base value as it is used with --base 
 %c   the Nagios exit code in the Form "OK", "WARNING", "CRITICAL" 
      or "UNKNOWN"
 %t   Threshold value which fails ("" when the check doesn't fail)
 %n   name, either calulated automatically or given with --name

Note that C<%u> and C<%w> are typically I<not> the same as the C<--unit>
option. They specify the unit I<after> the conversion for the plugin output as
described above. You can use the same modifiers as for C<sprintf> to fine tune
the output.

Example: 

 check_jmx4perl --url http://localhost:8888/j4p \
                --alias MEMORY_HEAP_USED \
                --base MEMORY_HEAP_MAX \ 
                --critical :80 \
                --label "Heap-Memory: %.2r% used (%.2v %u / %.2b %u)" \
                --unit B

will result in an output like

 OK - Heap-Memory: 3.48% used (17.68 MB / 508.06 MB) | '[MEMORY_HEAP_USED]'=3.48%;;:80


=cut

use FindBin qw ($Bin);
# Used for running directly out of the distrib dir,
# but we put the lib at last to avoid security issues
# on a proper installed system
#BEGIN { push(@INC, qq($Bin/../lib)); }
use lib qq($Bin/../lib);
use Nagios::Plugin::Functions;
use JMX::Jmx4Perl::Nagios::CheckJmx4Perl;

# Hack for avoiding a label in front of "OK" or "CRITICAL", in order to conform
# to the usual Nagios conventions and to use redundancy in the UI display.
BEGIN {
    no warnings 'redefine';
    *Nagios::Plugin::Functions::get_shortname = sub {
        return undef;
    };
}

# Create new object and use this for the check
# Hopefully it gets cleaned up aftewards if running
# within the embedded Nagios perl interpreter. At least, 
# we don't keep any references
JMX::Jmx4Perl::Nagios::CheckJmx4Perl->new(@ARGV)->execute();

=head1 LICENSE

This file is part of jmx4perl.

Jmx4perl 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, either version 2 of the License, or
(at your option) any later version.

jmx4perl 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 jmx4perl.  If not, see <http://www.gnu.org/licenses/>.

=head1 AUTHOR

roland@cpan.org

=cut
