diff options
author | Subhendu Ghosh <sghosh@users.sourceforge.net> | 2002-11-18 21:58:58 (GMT) |
---|---|---|
committer | Subhendu Ghosh <sghosh@users.sourceforge.net> | 2002-11-18 21:58:58 (GMT) |
commit | e799d3f2549e808a6a96dc41a6dac783aef648c2 (patch) | |
tree | 1fa0577136c77559a2f88dba8aeea0bd6282036c | |
parent | 6b2b23058730c44fea2e802076ff65388860c94a (diff) | |
download | monitoring-plugins-e799d3f2549e808a6a96dc41a6dac783aef648c2.tar.gz |
javaproc, sendim
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@211 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r-- | contrib/check_javaproc.pl | 278 | ||||
-rw-r--r-- | contrib/nagios_sendim.pl | 66 |
2 files changed, 344 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 | |||
25 | require 5.004; | ||
26 | use POSIX; | ||
27 | use strict; | ||
28 | use Getopt::Long; | ||
29 | use vars qw($opt_w $opt_c $verbose $classname); | ||
30 | use vars qw($PROGNAME); | ||
31 | use lib "utils.pm" ; | ||
32 | use utils qw($TIMEOUT %ERRORS &print_revision &support &usage); | ||
33 | |||
34 | $PROGNAME="check_javaprocs"; | ||
35 | sub getJavaList (); | ||
36 | sub check_ranges ($ $ $ $); | ||
37 | |||
38 | Getopt::Long::Configure('bundling', 'no_ignore_case'); | ||
39 | GetOptions | ||
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 | |||
49 | my $state = 'OK'; | ||
50 | my $min_warn = undef | ||
51 | my $max_warn = undef; | ||
52 | my $min_crit = undef; | ||
53 | my $max_crit = undef; | ||
54 | |||
55 | |||
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"); | ||
60 | |||
61 | |||
62 | # | ||
63 | # Determine # of running processes for the java programs that interest us. | ||
64 | # | ||
65 | my @javalist = getJavaList(); | ||
66 | |||
67 | my $total = 0; | ||
68 | my $msgout = ""; | ||
69 | my @fields; | ||
70 | |||
71 | if ( 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 | |||
115 | print $msgout; | ||
116 | print "$state\n" if ($verbose); | ||
117 | exit $ERRORS{$state}; | ||
118 | |||
119 | ################################### | ||
120 | # Support routines for Nagios | ||
121 | ################################### | ||
122 | sub 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 | |||
139 | sub print_usage () { | ||
140 | print "Usage: $PROGNAME [-v] [-w <min:max>] [-c <min:max>] [ -n <classname>]\n"; | ||
141 | } | ||
142 | |||
143 | sub print_help () { | ||
144 | revision(); | ||
145 | print "Copyright (c) 2002 by Wim Rijnders | ||
146 | |||
147 | Perl 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 | |||
172 | sub revision() { | ||
173 | print_revision($PROGNAME,'$Revision$ '); | ||
174 | } | ||
175 | |||
176 | sub version () { | ||
177 | revision(); | ||
178 | exit $ERRORS{'OK'}; | ||
179 | } | ||
180 | |||
181 | sub 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 | # | ||
197 | sub 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 | # | ||
254 | sub 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 ===== | ||
diff --git a/contrib/nagios_sendim.pl b/contrib/nagios_sendim.pl new file mode 100644 index 0000000..02870a6 --- /dev/null +++ b/contrib/nagios_sendim.pl | |||
@@ -0,0 +1,66 @@ | |||
1 | #!/usr/bin/perl -w | ||
2 | # | ||
3 | # SENDIM 1.0 by Sergio Freire (sergio-s-freire@ptinovacao.pt) | ||
4 | # Nagios plugin to send notifications using instant messages through a jabber server | ||
5 | # | ||
6 | # Note: a) you can send messages to several different IM systems like ICQ,AIM,MSN,etc... | ||
7 | # b) to test this plugin you can execute it with the arguments needed and write the message followed by a CTRL+D | ||
8 | # | ||
9 | # Please check http://www.jabber.org and http://www.jabberstudio.org for more information on Jabber Instant Messaging | ||
10 | |||
11 | |||
12 | use Net::Jabber qw(Client); | ||
13 | use Getopt::Std; | ||
14 | |||
15 | my $tmp; | ||
16 | my $mensagem=""; | ||
17 | getopts("u:p:t:"); | ||
18 | if ( (!defined($opt_u)) || (!defined($opt_p)) || (!defined($opt_t))) | ||
19 | { | ||
20 | print "USE: sendim -u user_JID -p password -t destination_JID\n"; | ||
21 | print 'EXAMPLE: sendim -u nagios@jabber.org -p nagios -t bitcoder@nagios.org'."\n"; | ||
22 | print " (send an instant message as user nagios\@jabber.org to bitcoder\@jabber.org)\n"; | ||
23 | exit; | ||
24 | } | ||
25 | |||
26 | my @buf=split('@',$opt_u); | ||
27 | my $login=$buf[0]; | ||
28 | @buf=split('/',$buf[1]); | ||
29 | my $server=$buf[0]; | ||
30 | my $resource=$buf[1] || "nagios"; | ||
31 | my $password=$opt_p; | ||
32 | my $jid_dest=$opt_t; | ||
33 | my $debug=0; # Set debug=1 to enable output of debug information | ||
34 | |||
35 | while ($tmp=<STDIN>) | ||
36 | { | ||
37 | $mensagem.=$tmp; | ||
38 | } | ||
39 | |||
40 | print "LOGIN: $login\nSERVER: $server\nRESOURCE: $resource\n" if $debug; | ||
41 | print "TO: $jid_dest\n" if $debug; | ||
42 | |||
43 | $Con1 = new Net::Jabber::Client(); | ||
44 | $Con1->Connect(hostname=>$server); | ||
45 | |||
46 | if ($Con1->Connected()) { | ||
47 | print "CON1: We are connected to the server...\n" if $debug; | ||
48 | } | ||
49 | |||
50 | @result1 = $Con1->AuthSend(username=>$login, | ||
51 | password=>$password, | ||
52 | resource=>$resource); | ||
53 | |||
54 | |||
55 | $Con1->PresenceSend(); | ||
56 | $Con1->Process(1); | ||
57 | |||
58 | @result1=$Con1->MessageSend( to=>$jid_dest, | ||
59 | subject=>"nagios", | ||
60 | body=>$mensagem, | ||
61 | type=>"chat", | ||
62 | priority=>1); | ||
63 | |||
64 | $Con1->Process(1); | ||
65 | $Con1->Disconnect(); | ||
66 | exit; | ||