-
Notifications
You must be signed in to change notification settings - Fork 9
/
check_tomcat.pl
373 lines (325 loc) · 12.4 KB
/
check_tomcat.pl
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/perl
#############################################################################
# #
# This script was initially developed by Lonely Planet for internal use #
# and has kindly been made available to the Open Source community for #
# redistribution and further development under the terms of the #
# GNU General Public License v3: http://www.gnu.org/licenses/gpl.html #
# #
#############################################################################
# #
# This script is supplied 'as-is', in the hope that it will be useful, but #
# neither Lonely Planet nor the authors make any warranties or guarantees #
# as to its correct operation, including its intended function. #
# #
# Or in other words: #
# Test it yourself, and make sure it works for YOU. #
# #
#############################################################################
# Author: George Hansper e-mail: [email protected] #
#############################################################################
use strict;
use LWP;
use LWP::UserAgent;
use Getopt::Std;
use XML::XPath;
my %optarg;
my $getopt_result;
my $lwp_user_agent;
my $http_request;
my $http_response;
my $url;
my $body;
my @message;
my @message_perf;
my $exit = 0;
my @exit = qw/OK: WARNING: CRITICAL:/;
my $rcs_id = '$Id: check_tomcat.pl,v 1.3 2011/12/11 04:56:27 george Exp $';
my $rcslog = '
$Log: check_tomcat.pl,v $
Revision 1.3 2011/12/11 04:56:27 george
Added currentThreadCount to performance data.
Revision 1.2 2011/11/18 11:30:57 george
Added capability to extract the connector names, and check any or all tomcat connectors for sufficient free threads.
Stripped quotes from connector names to work around tomcat7 quirkiness.
Revision 1.1 2011/04/16 12:05:26 george
Initial revision
';
# Defaults...
my $timeout = 10; # Default timeout
my $host = 'localhost'; # default host header
my $host_ip = 'localhost'; # default IP
my $port = 80; # default port
my $user = 'nagios'; # default user
my $password = 'nagios'; # default password
my $uri = '/manager/status?XML=true'; #default URI
my $http = 'http';
my $connector_arg = undef;
my $warn_threads = "25%";
my $crit_threads = "10%";
# Memory thresholds are tight, because garbage collection kicks in only when memory is low anyway
my $warn_memory = "5%";
my $crit_memory = "2%";
my $xpath;
my %xpath_checks = (
maxThreads => '/status/connector/threadInfo/@maxThreads',
currentThreadCount => '/status/connector/threadInfo/@currentThreadCount',
currentThreadsBusy => '/status/connector/threadInfo/@currentThreadsBusy',
memMax => '/status/jvm/memory/@max',
memFree => '/status/jvm/memory/@free',
memTotal => '/status/jvm/memory/@total',
);
# XPath examples...
# /status/jvm/memory/@free
# /status/connector[attribute::name="http-8080"]/threadInfo/@maxThreads
# /status/connector/threadInfo/@* <- returns multiple nodes
my %xpath_check_results;
sub VERSION_MESSAGE() {
print "$^X\n$rcs_id\n";
}
sub HELP_MESSAGE() {
print <<EOF;
Usage:
$0 [-v] [-H hostname] [-I ip_address] [-p port] [-S] [-t time_out] [-l user] [-a password] [-w /xpath[=value]...] [-c /xpath[=value]...]
-H ... Hostname and Host: header (default: $host)
-I ... IP address (default: none)
-p ... Port number (default: ${port})
-S ... Use SSL connection
-v ... verbose messages
-t ... Seconds before connection times out. (default: $timeout)
-l ... username for authentication (default: $user)
-a ... password for authentication (default: embedded in script)
-u ... uri path, (default: $uri)
-n ... connector name, regular expression
eg 'ajp-bio-8009' or 'http-8080' or '^http-'.
default is to check: .*-port_number\$
Note: leading/trailing quotes and spaces are trimmed from the connector name for matching.
-w ... warning thresholds for threads,memory (memory in MB)
eg 20,50 or 10%,25% default is $warn_threads,$warn_memory
-c ... critical thresholds for threads,memory (memory in MB)
eg 10,20 or 5%,10%, default is $crit_threads,$crit_memory
Example:
$0 -H app01.signon.devint.lpo -p 8080 -t 5 -l nagios -a apples -u '/manager/status?XML=true'
$0 -H app01.signon.devint.lpo -p 8080 -w 10%,50 -c 5%,10
$0 -H app01.signon.devint.lpo -p 8080 -w 10%,50 -c 5%,10 -l admin -a admin -n .
Notes:
The -I parameters connects to a alternate hostname/IP, using the Host header from the -H parameter
To check ALL connectors mentioned in the status XML file, use '-n .'
'.' is a regular expression matching all connector names.
EOF
}
$getopt_result = getopts('hvSH:I:p:w:c:t:l:a:u:n:', \%optarg) ;
# Any invalid options?
if ( $getopt_result == 0 ) {
HELP_MESSAGE();
exit 1;
}
if ( $optarg{h} ) {
HELP_MESSAGE();
exit 0;
}
sub printv($) {
if ( $optarg{v} ) {
chomp( $_[-1] );
print STDERR @_;
print STDERR "\n";
}
}
if ( defined($optarg{t}) ) {
$timeout = $optarg{t};
}
# Is port number numeric?
if ( defined($optarg{p}) ) {
$port = $optarg{p};
if ( $port !~ /^[0-9][0-9]*$/ ) {
print STDERR <<EOF;
Port must be a decimal number, eg "-p 8080"
EOF
exit 1;
}
}
if ( defined($optarg{H}) ) {
$host = $optarg{H};
$host_ip = $host;
}
if ( defined($optarg{I}) ) {
$host_ip = $optarg{I};
if ( ! defined($optarg{H}) ) {
$host = $host_ip;
}
}
if ( defined($optarg{l}) ) {
$user = $optarg{l};
}
if ( defined($optarg{a}) ) {
$password = $optarg{a};
}
if ( defined($optarg{u}) ) {
$uri = $optarg{u};
}
if ( defined($optarg{S}) ) {
$http = 'https';
}
if ( defined($optarg{c}) ) {
my @threshold = split(/,/,$optarg{c});
if ( $threshold[0] ne "" ) {
$crit_threads = $threshold[0];
}
if ( $threshold[1] ne "" ) {
$crit_memory = $threshold[1];
}
}
if ( defined($optarg{n}) ) {
$connector_arg = $optarg{n};
} else {
$connector_arg = "-$port\$";
}
if ( defined($optarg{w}) ) {
my @threshold = split(/,/,$optarg{w});
if ( $threshold[0] ne "" ) {
$warn_threads = $threshold[0];
}
if ( $threshold[1] ne "" ) {
$warn_memory = $threshold[1];
}
}
*LWP::UserAgent::get_basic_credentials = sub {
return ( $user, $password );
};
# print $xpath_checks[0], "\n";
printv "Connecting to $host:${port}\n";
$lwp_user_agent = LWP::UserAgent->new;
$lwp_user_agent->timeout($timeout);
if ( $port == 80 || $port == 443 || $port eq "" ) {
$lwp_user_agent->default_header('Host' => $host);
} else {
$lwp_user_agent->default_header('Host' => "$host:$port");
}
$url = "$http://${host_ip}:${port}$uri";
$http_request = HTTP::Request->new(GET => $url);
printv "--------------- GET $url";
printv $lwp_user_agent->default_headers->as_string . $http_request->headers_as_string;
$http_response = $lwp_user_agent->request($http_request);
printv "---------------\n" . $http_response->protocol . " " . $http_response->status_line;
printv $http_response->headers_as_string;
printv "Content has " . length($http_response->content) . " bytes \n";
if ($http_response->is_success) {
$body = $http_response->content;
my $xpath = XML::XPath->new( xml => $body );
my $xpath_check;
# Parse the data out of the XML...
foreach $xpath_check ( keys %xpath_checks ) {
#print keys(%{$xpath_check}) , "\n";
my $path = $xpath_checks{$xpath_check};
$path =~ s{\$port}{$port};
#print $xpath_check->{xpath} , "\n";
my $nodeset = $xpath->find($path);
if ( $nodeset->get_nodelist == 0 ) {
push @message, "$path not found";
$exit |= 2;
push @message_perf, "$path=not_found";
next;
}
foreach my $node ($nodeset->get_nodelist) {
my $connector_name = $node->getParentNode()->getParentNode()->getAttribute("name");
$connector_name =~ s/^["'\s]+//;
$connector_name =~ s/["'\s]+$//;
my $value = $node->string_value();
if ( $value =~ /^"?([0-9.]+)"?$/ ) {
$value = $1;
} else {
push @message, "$path is not numeric";
$exit |= 2;
push @message_perf, "$path=not_numeric";
next;
}
if ( $xpath_check =~ /^mem/ ) {
# This is the .../memory/.. xpath, just store the value in the hash
$xpath_check_results{$xpath_check} = $value;
} elsif ( $connector_name =~ /${connector_arg}/ && $connector_name ne "" ) {
# This is a .../threadInfo/... xpath, put the result into a hash (key is connector_name)
$xpath_check_results{$xpath_check}{$connector_name} = $value;
}
}
}
# Now apply the logic and check the results
#----------------------------------------------
# Check memory
#----------------------------------------------
my $jvm_mem_available = $xpath_check_results{memFree} + $xpath_check_results{memMax} - $xpath_check_results{memTotal};
printv(sprintf("free=%d max=%d total=%d",$xpath_check_results{memFree}/1024, $xpath_check_results{memMax}/1024, $xpath_check_results{memTotal}/1024));
if ( $warn_memory =~ /(.*)%$/ ) {
$warn_memory = int($1 * $xpath_check_results{memMax} / 100);
} else {
# Convert to bytes
$warn_memory =int($warn_memory * 1024 * 1024);
}
printv("warning at $warn_memory bytes (". ( $warn_memory / 1024 /1024 )."MB) free, max=$xpath_check_results{memMax}");
if ( $crit_memory =~ /(.*)%$/ ) {
$crit_memory = int($1 * $xpath_check_results{memMax} / 100);
} else {
# Convert to bytes
$crit_memory = int($crit_memory * 1024 * 1024);
}
printv("critical at $crit_memory bytes (". ( $crit_memory / 1024 /1024 )."MB) free, max=$xpath_check_results{memMax}");
if ( $jvm_mem_available <= $crit_memory ) {
$exit |= 2;
push @message, sprintf("Memory critical <%d MB,",$crit_memory/1024/1024);
} elsif ( $jvm_mem_available <= $warn_memory ) {
$exit |= 1;
push @message, sprintf("Memory low <%d MB,",$warn_memory/1024/1024);
}
push @message, sprintf("memory in use %d MB (%d MB);",
( $xpath_check_results{memMax} - $jvm_mem_available ) / ( 1024 * 1024),
$xpath_check_results{memMax} / ( 1024 * 1024)
);
push @message_perf, "used=".( $xpath_check_results{memMax} - $jvm_mem_available ) . " free=$jvm_mem_available max=$xpath_check_results{memMax}";
#----------------------------------------------
# Check threads
#----------------------------------------------
my $name;
foreach $name ( keys( %{$xpath_check_results{currentThreadsBusy}} ) ) {
if ( $warn_threads =~ /(.*)%$/ ) {
$warn_threads = int($1 * $xpath_check_results{maxThreads}{$name} / 100);
}
printv("warning at $warn_threads threads free, max=$xpath_check_results{maxThreads}{$name}");
if ( $crit_threads =~ /(.*)%$/ ) {
$crit_threads = int($1 * $xpath_check_results{maxThreads}{$name} / 100);
}
printv("critical at $crit_threads threads free, max=$xpath_check_results{maxThreads}{$name}");
my $threads_available = $xpath_check_results{maxThreads}{$name} - $xpath_check_results{currentThreadsBusy}{$name};
if ( $threads_available <= $crit_threads ) {
$exit |= 2;
push @message, sprintf("Critical: free_threads<%d",$crit_threads);
} elsif ( $threads_available <= $warn_threads ) {
$exit |= 1;
push @message, sprintf("Warning: free_threads<%d",$warn_threads);
}
push @message, sprintf("threads[$name]=%d(%d);",
$xpath_check_results{currentThreadsBusy}{$name},
$xpath_check_results{maxThreads}{$name}
);
if ( defined($optarg{n}) ) {
push @message_perf, "currentThreadsBusy[$name]=$xpath_check_results{currentThreadsBusy}{$name} currentThreadCount[$name]=$xpath_check_results{currentThreadCount}{$name} maxThreads[$name]=$xpath_check_results{maxThreads}{$name}";
} else {
# For the sake of backwards-compatability of graphs etc...
push @message_perf, "currentThreadsBusy=$xpath_check_results{currentThreadsBusy}{$name} currentThreadCount=$xpath_check_results{currentThreadCount}{$name} maxThreads=$xpath_check_results{maxThreads}{$name}";
}
}
if ( keys(%{$xpath_check_results{currentThreadsBusy}}) == 0 ) {
# no matching connectors found - this is not OK.
$exit |= 1;
push @message, "Warning: No tomcat connectors matched name =~ /$connector_arg/";
}
} elsif ( $http_response->code == 401 ) {
print "WARNING: $url " . $http_response->protocol . " " . $http_response->status_line ."\n";
exit 1;
} else {
print "CRITICAL: $url " . $http_response->protocol . " " . $http_response->status_line ."\n";
exit 2;
}
if ( $exit == 3 ) {
$exit = 2;
}
print "$exit[$exit] ". join(" ",@message) . "|". join(" ",@message_perf) . "\n";
exit $exit;