Repository
Munin (master)
Last change
2018-03-29
Graph Categories
Family
manual
Capabilities
Language
Perl
License
GPL-2.0-only
Authors

tomcat_threads

Name

tomcat_threads - Plugin to monitor the number of tomcat-threads running on the machine, and (in addition to a simple process count), separate then into “busy” or “idle” servers.

Configuration

The following environment variables are used by this plugin:

url       - Override default status-url
port      - HTTP port number
user      - Manager username
password  - Manager password
connector - Override connector to monitor

Usage

Requirements: Needs access to http://<user>:<password>@localhost:8080/manager/status?XML=true (or modify the address for another host).

Tomcat 5.0 or higher.

A munin-user in $CATALINA_HOME/conf/tomcat-users.xml should be set up for this to work.

Tip: To see if it’s already set up correctly, just run this plugin with the parameter “autoconf”. If you get a “yes”, everything should work like a charm already.

tomcat-users.xml example: <user username=“munin” password="<set this>" roles=“standard,manager”/>

Author

Rune Nordbøe Skillingstad runesk@linpro.no

License

GPLv2

Magic Markers

#%# family=manual
#%# capabilities=autoconf
#!/usr/bin/perl

=head1 NAME

tomcat_threads - Plugin to monitor the number of tomcat-threads
running on the machine, and (in addition to a simple process count),
separate then into "busy" or "idle" servers.

=head1 CONFIGURATION

The following environment variables are used by this plugin:

 url       - Override default status-url
 port      - HTTP port number
 user      - Manager username
 password  - Manager password
 connector - Override connector to monitor

=head1 USAGE

Requirements: Needs access to
http://<user>:<password>@localhost:8080/manager/status?XML=true (or
modify the address for another host).

Tomcat 5.0 or higher.

A munin-user in $CATALINA_HOME/conf/tomcat-users.xml should be set up
for this to work.

Tip: To see if it's already set up correctly, just run this plugin
with the parameter "autoconf". If you get a "yes", everything should
work like a charm already.

tomcat-users.xml example:
    <user username="munin" password="<set this>" roles="standard,manager"/>

=head1 AUTHOR

Rune Nordbøe Skillingstad <runesk@linpro.no>

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

 #%# family=manual
 #%# capabilities=autoconf

=cut

use strict;
use warnings;

use Munin::Plugin::HTTP;
use URI::Escape;

my $ret = undef;

if(!eval "require XML::Simple;") {
    $ret .= "XML::Simple not found";
}

my $UA       = Munin::Plugin::HTTP->new;
my $URL      = exists $ENV{'url'}      ? $ENV{'url'}      : "http://%s:%s\@%s:%d/manager/status?XML=true";
my $PORT     = exists $ENV{'port'}     ? $ENV{'port'}     : exists $ENV{'ports'} ? $ENV{'ports'} : 8080;
my $HOST     = exists $ENV{'host'}     ? $ENV{'host'}     : "127.0.0.1";
my $USER     = exists $ENV{'user'}     ? $ENV{'user'}     : "munin";
my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : "munin";
my $TIMEOUT  = exists $ENV{'timeout'}  ? $ENV{'timeout'}  : 30;
my $CONNECTOR= $ENV{'connector'};

my $url = sprintf $URL, uri_escape($USER), uri_escape($PASSWORD), $HOST, $PORT;

if(exists $ARGV[0] and $ARGV[0] eq "autoconf") {
    if($ret) {
        print "no ($ret)\n";
        exit 0;
    }
    my $response = $UA->get($url);
    if($response->is_success and $response->content =~ /<status>.*<\/status>/im) {
	print "yes\n";
	exit 0;
    } else {
        print "no (no tomcat status)\n";
        exit 0;
    }
}

my $ua = LWP::UserAgent->new(timeout => $TIMEOUT);
my $xs = new XML::Simple;
my $response = $ua->request(HTTP::Request->new('GET',$url));
my %options = ( KeyAttr => { connector => 'name' }, ForceArray => 1 );
my $xml = $xs->XMLin($response->content, %options);

my @connectors;
if(defined $CONNECTOR) {
    push @connectors, $CONNECTOR;
} else {
    @connectors = keys %{$xml->{'connector'}};
}

if(exists $ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Tomcat threads\n";
    print "graph_args --base 1000 -l 0\n";
    print "graph_vlabel threads\n";
    print "graph_category appserver\n";
    print "graph_total total\n";
    my @idle;
    my @busy;
    my $first = 1;
    for my $connector (@connectors) {
        my $clean = clean_fieldname($connector);
        print "${clean}_busy.label $connector busy threads\n";
        if($first) {
            print "${clean}_busy.draw AREA\n";
            $first = 0;
        } else {
            print "${clean}_busy.draw STACK\n";
        }
        print "${clean}_idle.label $connector idle threads\n";
        print "${clean}_idle.draw STACK\n";

        push @busy, "${clean}_busy";
        push @idle, "${clean}_idle";
    }
    print "graph_order " . join(" ", @busy) . " " . join(" ", @idle) . "\n";

} else {
    for my $connector (@connectors) {
        my $clean = clean_fieldname($connector);
        if(exists $xml->{'connector'}->{$connector}->{'threadInfo'}->[0]->{'currentThreadsBusy'} &&
            exists $xml->{'connector'}->{$connector}->{'threadInfo'}->[0]->{'currentThreadCount'}) {
            print "${clean}_busy.value " . $xml->{'connector'}->{$connector}->{'threadInfo'}->[0]->{'currentThreadsBusy'} . "\n";
            print "${clean}_idle.value " .
              ($xml->{'connector'}->{$connector}->{'threadInfo'}->[0]->{'currentThreadCount'} -
              $xml->{'connector'}->{$connector}->{'threadInfo'}->[0]->{'currentThreadsBusy'}) . "\n";
        } else {
            print "${clean}_busy.value U\n";
            print "${clean}_idle.value U\n";
        }
    }
}