4
# Author: Wim Rijnders, 17-10-2002
9
# Nagios host script to check if any specified java processes are running.
11
# Implementation Notes:
12
# ---------------------
14
# check_disk_smb was used as a starting point, since it was written in perl.
16
# This script has been created and tested on Linux RH 7.1.
18
# I tried OS-X Darwin (BSD), but the ps command works differently.
19
# Notably, you can't get a combined list of child processes. The best approach
20
# appears to be to use 'ps -wwaxo command' combined with 'ps -M' (or suchlike)
22
########################################################################
29
use vars qw($opt_w $opt_c $verbose $classname);
30
use vars qw($PROGNAME);
32
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
34
$PROGNAME="check_javaprocs";
36
sub check_ranges ($ $ $ $);
38
Getopt::Long::Configure('bundling', 'no_ignore_case');
40
("V|version" => \&version,
42
"v|verbose" => \$verbose,
43
"w|warning=s" => \$opt_w,
44
"c|critical=s" => \$opt_c,
45
"n|name=s" => \$classname
56
($opt_w) || ($opt_w = shift);
57
check_ranges($opt_w,\$min_warn, \$max_warn, "warning");
58
($opt_c) || ($opt_c = shift);
59
check_ranges($opt_c,\$min_crit, \$max_crit, "critical");
63
# Determine # of running processes for the java programs that interest us.
65
my @javalist = getJavaList();
71
if ( defined $classname ) {
73
#filter out a single java process based on class name
75
@fields = split(/\s+/, $_);
76
$total = $fields[-1] and last if $classname eq $fields[0];
78
$msgout .= "$total processes for $classname\n";
80
#Handle all java processes
83
@fields = split(/\s+/, $_);
85
$total += $fields[-1];
86
$msgout .= " $fields[-1] processes for ";
87
$msgout .= (scalar @fields > 1)? $fields[0] : "unknown" ;
90
my $msgtotal = "$total java processes for ". scalar @javalist . " applications";
92
if ( defined $verbose ) {
93
$msgout = $msgtotal . $msgout;
101
# Set the state with the data we now have accumulated
102
# Note that due to the order of testing, warnings have precedence over
103
# criticals. This is logical, since you should be able to create a criticals
104
# range which encompasses a warning range. eg. following should be possible:
106
# check_javaproc -w 5:10 -c 3:12
107
# proper specification of the ranges is the responsibility of the script user.
109
$state = 'CRITICAL' if (defined $min_crit && $total < $min_crit);
110
$state = 'CRITICAL' if (defined $max_crit && $total > $max_crit);
111
$state = 'CRITICAL' if (!defined $min_crit && !defined $max_crit && $total==0 );
112
$state = 'WARNING' if (defined $min_warn && $total < $min_warn);
113
$state = 'WARNING' if (defined $max_warn && $total > $max_warn);
116
print "$state\n" if ($verbose);
117
exit $ERRORS{$state};
119
###################################
120
# Support routines for Nagios
121
###################################
122
sub check_ranges($$$$) {
123
my ($opt, $min, $max, $rangename) = @_;
125
if ( defined $opt ) {
126
if ( $opt =~ /^([0-9]*)\:([0-9]*)$/) {
127
$$min = $1 if $1 > 0;
130
usage("Invalid $rangename range: $opt\n");
134
if ( defined $$min && defined $$max ) {
135
usage("Min value of $rangename range larger than max value: $opt\n") if ( $$min > $$max);
140
print "Usage: $PROGNAME [-v] [-w <min:max>] [-c <min:max>] [ -n <classname>]\n";
145
print "Copyright (c) 2002 by Wim Rijnders
147
Perl Check java processes plugin for Nagios
153
Return additional information.
154
Intended as a command-line aid, not recommended for Nagios script usage.
156
-w, --warning=INTEGER:INTEGER
157
Minimum and maximum number of processes outside of which a warning will be
158
generated. If omitted, no warning is generated.
160
-c, --critical=INTEGER:INTEGER
161
Minimum and maximum number of processes outside of which a critical will be
162
generated. If omitted, a critical is generated if no processes are running.
165
Name of class specified on the java command line (from which main() is run).
166
If omitted, all java processes are taken into account.
173
print_revision($PROGNAME,'$Revision: 1.1 $ ');
186
###################################
187
# Routines for delivering the data
188
###################################
191
# Generate a formatted list of running java processes.
193
# Returns an array of strings having the following syntax:
195
# <java class running as main> <parameters if any> <#processes for this class>
202
local $ENV{'PATH'} = '/bin:/usr/bin';
203
local $ENV{'BASH_ENV'} = '~/.bashrc';
205
# We are only interested in the full command line
206
# The -H opstion is important for the order of the processes;
207
# this option ensures that all child processes are listed under
209
@output=`ps -AHo \"\%a\" -ww`;
211
#remove preceding whitespace and final EOL
217
#Combine any consecutive processes with exactly the same command line
219
@output = checkSameLine(@output);
221
#Filter out all java processes
223
for (my $i = 0; $i < scalar @output; ++$i) {
224
push @javalist, $output[$i] if $output[$i] =~ /^\S*java/;
227
foreach (@javalist) {
228
#The java statement at the beginning is redundant; remove it
235
s/\-(classpath|cp)\s+\S+//g;
237
#remove any other parameters we don't want to see
241
#remove any redundant whitespaces at the beginning
251
# Combine all consecutive lines with an identical command line
252
# to a signle line with a count at the end
260
foreach my $a (@input) {
261
if ( $prevline eq $a) {
264
push @output, $prevline . " " . ($prevcount + 1);
270
#don't forget the last item!
271
if ( $prevcount > 0 ) {
272
push @output, $prevline . " " . ($prevcount + 1);
278
#======= end check_javaproc =====