File: tomcat_threads.in

package info (click to toggle)
munin 2.0.76-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,064 kB
  • sloc: perl: 11,684; java: 1,924; sh: 1,632; makefile: 636; javascript: 365; python: 267
file content (125 lines) | stat: -rw-r--r-- 3,683 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!@@PERL@@
# -*- 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:

 timeout   - Connection timeout
 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 Munin::Plugin;


my $ret = undef;

if(!eval "require LWP::UserAgent;") {
    $ret = "LWP::UserAgent not found";
}

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

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= exists $ENV{'connector'}? $ENV{'connector'}: "http-".$PORT;

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

if(exists $ARGV[0] and $ARGV[0] eq "autoconf") {
    if($ret) {
	print "no ($ret)\n";
	exit 0;
    }
    my $au = LWP::UserAgent->new(timeout => $TIMEOUT);
    my $repsonse = $au->request(HTTP::Request->new('GET',$url));
    if($repsonse->is_success and $repsonse->content =~ /<status>.*<\/status>/im) {
	print "yes\n";
	exit 0;
    } else {
	print "no (no tomcat status)\n";
	exit 0;
    }
}

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 tomcat\n";
    print "graph_total total\n";
    print "graph_order busy idle\n";
    print "busy.label busy threads\n";
    print "busy.draw AREA\n";
    print "idle.label idle threads\n";
    print "idle.draw STACK\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);

if($xml->{'connector'}->{$CONNECTOR}->{'threadInfo'}->[0]->{'currentThreadsBusy'} &&
    $xml->{'connector'}->{$CONNECTOR}->{'threadInfo'}->[0]->{'currentThreadCount'}) {
    print "busy.value " . $xml->{'connector'}->{$CONNECTOR}->{'threadInfo'}->[0]->{'currentThreadsBusy'} . "\n";
    print "idle.value " .
	  ($xml->{'connector'}->{$CONNECTOR}->{'threadInfo'}->[0]->{'currentThreadCount'} -
	  $xml->{'connector'}->{$CONNECTOR}->{'threadInfo'}->[0]->{'currentThreadsBusy'}) . "\n";
} else {
    print "busy.value U\n";
    print "idle.value U\n";
}

# vim:syntax=perl