summaryrefslogtreecommitdiffstats
path: root/contrib/check_javaproc.pl
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/check_javaproc.pl')
-rw-r--r--contrib/check_javaproc.pl278
1 files changed, 278 insertions, 0 deletions
diff --git a/contrib/check_javaproc.pl b/contrib/check_javaproc.pl
new file mode 100644
index 0000000..a9b52bc
--- /dev/null
+++ b/contrib/check_javaproc.pl
@@ -0,0 +1,278 @@
1#!/usr/bin/perl -w
2
3#
4# Author: Wim Rijnders, 17-10-2002
5#
6# Description:
7# -----------
8#
9# Nagios host script to check if any specified java processes are running.
10#
11# Implementation Notes:
12# ---------------------
13#
14# check_disk_smb was used as a starting point, since it was written in perl.
15#
16# This script has been created and tested on Linux RH 7.1.
17#
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)
21#
22########################################################################
23####
24
25require 5.004;
26use POSIX;
27use strict;
28use Getopt::Long;
29use vars qw($opt_w $opt_c $verbose $classname);
30use vars qw($PROGNAME);
31use lib "utils.pm" ;
32use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
33
34$PROGNAME="check_javaprocs";
35sub getJavaList ();
36sub check_ranges ($ $ $ $);
37
38Getopt::Long::Configure('bundling', 'no_ignore_case');
39GetOptions
40 ("V|version" => \&version,
41 "h|help" => \&help,
42 "v|verbose" => \$verbose,
43 "w|warning=s" => \$opt_w,
44 "c|critical=s" => \$opt_c,
45 "n|name=s" => \$classname
46 );
47
48
49my $state = 'OK';
50my $min_warn = undef
51my $max_warn = undef;
52my $min_crit = undef;
53my $max_crit = undef;
54
55
56($opt_w) || ($opt_w = shift);
57check_ranges($opt_w,\$min_warn, \$max_warn, "warning");
58($opt_c) || ($opt_c = shift);
59check_ranges($opt_c,\$min_crit, \$max_crit, "critical");
60
61
62#
63# Determine # of running processes for the java programs that interest us.
64#
65my @javalist = getJavaList();
66
67my $total = 0;
68my $msgout = "";
69my @fields;
70
71if ( defined $classname ) {
72
73 #filter out a single java process based on class name
74 foreach (@javalist) {
75 @fields = split(/\s+/, $_);
76 $total = $fields[-1] and last if $classname eq $fields[0];
77 }
78 $msgout .= "$total processes for $classname\n";
79} else {
80 #Handle all java processes
81 $msgout .= "\n";
82 foreach (@javalist) {
83 @fields = split(/\s+/, $_);
84
85 $total += $fields[-1];
86 $msgout .= " $fields[-1] processes for ";
87 $msgout .= (scalar @fields > 1)? $fields[0] : "unknown" ;
88 $msgout .= "\n";
89 }
90 my $msgtotal = "$total java processes for ". scalar @javalist . " applications";
91
92 if ( defined $verbose ) {
93 $msgout = $msgtotal . $msgout;
94 } else {
95 $msgout = $msgtotal;
96 }
97
98}
99
100#
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:
105#
106# check_javaproc -w 5:10 -c 3:12
107# proper specification of the ranges is the responsibility of the script user.
108#
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);
114
115print $msgout;
116print "$state\n" if ($verbose);
117exit $ERRORS{$state};
118
119###################################
120# Support routines for Nagios
121###################################
122sub check_ranges($$$$) {
123 my ($opt, $min, $max, $rangename) = @_;
124
125 if ( defined $opt ) {
126 if ( $opt =~ /^([0-9]*)\:([0-9]*)$/) {
127 $$min = $1 if $1 > 0;
128 $$max= $2 if $2 > 0;
129 } else {
130 usage("Invalid $rangename range: $opt\n");
131 }
132 }
133
134 if ( defined $$min && defined $$max ) {
135 usage("Min value of $rangename range larger than max value: $opt\n") if ( $$min > $$max);
136 }
137}
138
139sub print_usage () {
140 print "Usage: $PROGNAME [-v] [-w <min:max>] [-c <min:max>] [ -n <classname>]\n";
141}
142
143sub print_help () {
144 revision();
145 print "Copyright (c) 2002 by Wim Rijnders
146
147Perl Check java processes plugin for Nagios
148
149";
150 print_usage();
151 print "
152-v, --verbose
153 Return additional information.
154 Intended as a command-line aid, not recommended for Nagios script usage.
155
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.
159
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.
163
164-n, --name=STRING
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.
167
168";
169 support();
170}
171
172sub revision() {
173 print_revision($PROGNAME,'$Revision$ ');
174}
175
176sub version () {
177 revision();
178 exit $ERRORS{'OK'};
179}
180
181sub help () {
182 print_help();
183 exit $ERRORS{'OK'};
184}
185
186###################################
187# Routines for delivering the data
188###################################
189
190#
191# Generate a formatted list of running java processes.
192#
193# Returns an array of strings having the following syntax:
194#
195# <java class running as main> <parameters if any> <#processes for this class>
196#
197sub getJavaList() {
198
199 my @output;
200
201 # Untaint
202 local $ENV{'PATH'} = '/bin:/usr/bin';
203 local $ENV{'BASH_ENV'} = '~/.bashrc';
204
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
208 # their parents
209 @output=`ps -AHo \"\%a\" -ww`;
210
211 #remove preceding whitespace and final EOL
212 foreach (@output) {
213 s/^\s*//;
214 chop;
215 }
216
217 #Combine any consecutive processes with exactly the same command line
218 #into a single item
219 @output = checkSameLine(@output);
220
221 #Filter out all java processes
222 my @javalist;
223 for (my $i = 0; $i < scalar @output; ++$i) {
224 push @javalist, $output[$i] if $output[$i] =~ /^\S*java/;
225 }
226
227 foreach (@javalist) {
228 #The java statement at the beginning is redundant; remove it
229 s/^\S*java//;
230
231 #remove all defines
232 s/\-D\S+//g;
233
234 #remove classpath
235 s/\-(classpath|cp)\s+\S+//g;
236
237 #remove any other parameters we don't want to see
238 s/\-server\s+//g;
239 s/\-X\S*\s+//g;
240
241 #remove any redundant whitespaces at the beginning
242 s/^\s+//;
243
244 }
245
246 @javalist;
247}
248
249
250#
251# Combine all consecutive lines with an identical command line
252# to a signle line with a count at the end
253#
254sub checkSameLine {
255 my @input = @_;
256 my @output;
257 my $prevline= "";
258 my $prevcount = 0;
259
260 foreach my $a (@input) {
261 if ( $prevline eq $a) {
262 ++$prevcount;
263 } else {
264 push @output, $prevline . " " . ($prevcount + 1);
265 $prevcount = 0;
266 }
267 $prevline = $a;
268 }
269
270 #don't forget the last item!
271 if ( $prevcount > 0 ) {
272 push @output, $prevline . " " . ($prevcount + 1);
273 }
274
275 @output;
276}
277
278#======= end check_javaproc =====