Debian Codegraph @

scripts/check_rabbitmq_watermark

raw
1 #!/usr/bin/env perl
2
3 ### check_rabbitmq_watermark.pl
4
5 # Use the management API to check if mem_alarm or disk_free_alarm has been triggered.
6
7 # Originally by Nathan Vonnahme, n8v at users dot sourceforge
8 # dot net, July 19 2006
9
10 ##############################################################################
11 # prologue
12 use strict;
13 use warnings;
14
15 use Monitoring::Plugin;
16 use LWP::UserAgent;
17 use URI::Escape;
18 use JSON;
19
20 use vars qw($VERSION $PROGNAME $verbose $timeout);
21 $VERSION = '2.0.3';
22
23 # get the base name of this script for use in the examples
24 use File::Basename;
25 $PROGNAME = basename($0);
26
27
28 ##############################################################################
29 # define and get the command line options.
30 # see the command line option guidelines at
31 # http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS
32
33
34 # Instantiate Monitoring::Plugin object (the 'usage' parameter is mandatory)
35 my $p = Monitoring::Plugin->new(
36 usage => "Usage: %s [options] -H hostname",
37 license => "",
38 version => $VERSION,
39 blurb => 'This plugin uses the RabbitMQ management API to check if the mem_alarm or disk_free_alarm has been triggered',
40 );
41
42 $p->add_arg(spec => 'hostname|host|H=s',
43 help => "Specify the host to connect to",
44 required => 1
45 );
46 $p->add_arg(spec => 'port=i',
47 help => "Specify the port to connect to (default: %s)",
48 default => 15672
49 );
50 $p->add_arg(spec => 'node|n=s',
51 help => "Specify the node name (default is hostname)"
52 );
53 $p->add_arg(spec => 'username|user|u=s',
54 help => "Username (default: %s)",
55 default => "guest",
56 );
57 $p->add_arg(spec => 'password|p=s',
58 help => "Password (default: %s)",
59 default => "guest"
60 );
61
62 $p->add_arg(spec => 'vhost=s',
63 help => "Specify the vhost to test (default: %s)",
64 default => "/"
65 );
66
67 $p->add_arg(spec => 'ssl|ssl!',
68 help => "Use SSL (default: false)",
69 default => 0
70 );
71
72 $p->add_arg(spec => 'ssl_strict|ssl_strict!',
73 help => "Verify SSL certificate (default: true)",
74 default => 1
75 );
76
77 $p->add_arg(spec => 'proxy|proxy!',
78 help => "Use environment proxy (default: true)",
79 default => 1
80 );
81 $p->add_arg(spec => 'proxyurl=s',
82 help => "Use proxy url like http://proxy.domain.com:8080",
83 );
84
85 $p->add_arg(spec => 'rname|r=s',
86 help => "RabbitName (default: %s)",
87 default => "rabbit",
88 );
89
90
91 # Parse arguments and process standard ones (e.g. usage, help, version)
92 $p->getopts;
93
94
95 # perform sanity checking on command line options
96
97
98 ##############################################################################
99 # check stuff.
100
101 my $hostname=$p->opts->hostname;
102 my $port=$p->opts->port;
103 my $vhost=uri_escape($p->opts->vhost);
104
105 my $nodename = $p->opts->node;
106
107 my $rname = $p->opts->rname;
108
109 if (!$nodename) {
110 $hostname =~ /^([a-zA-Z0-9-]*)/;
111 $nodename = $1;
112 }
113
114 my $url = sprintf("http%s://%s:%d/api/nodes/%s\@%s", ($p->opts->ssl ? "s" : ""), $hostname, $port, $rname, $nodename);
115
116 my $ua = LWP::UserAgent->new;
117 if (defined $p->opts->proxyurl)
118 {
119 $ua->proxy('http', $p->opts->proxyurl);
120 }
121 elsif($p->opts->proxy == 1 )
122 {
123 $ua->env_proxy;
124 }
125 $ua->agent($PROGNAME.' ');
126 $ua->timeout($p->opts->timeout);
127 if ($p->opts->ssl and $ua->can('ssl_opts')) {
128 $ua->ssl_opts(verify_hostname => $p->opts->ssl_strict);
129 }
130 my $req = HTTP::Request->new(GET => $url);
131 $req->authorization_basic($p->opts->username, $p->opts->password);
132 my $res = $ua->request($req);
133
134 if (!$res->is_success) {
135 # Deal with standard error conditions - make the messages more sensible
136 if ($res->code == 400) {
137 my $bodyref = decode_json $res->content;
138 $p->nagios_exit(CRITICAL, $bodyref->{'reason'});
139 }
140 $res->code == 404 and $p->nagios_die("Not found");
141 $res->code == 401 and $p->nagios_die("Access refused");
142 if ($res->code < 200 or $res->code > 400 ) {
143 $p->nagios_exit(CRITICAL, "Received ".$res->status_line);
144 }
145 }
146
147 my $bodyref = decode_json $res->content;
148 my @checks = ("mem_alarm", "disk_free_alarm");
149
150 for my $check (@checks) {
151 if ($bodyref->{$check} eq 0) {
152 $p->add_message(OK, "$check");
153 } else {
154 $p->add_message(CRITICAL, "$check");
155 }
156 }
157
158 my($code, $message) = $p->check_messages(join_all=>', ');
159 $p->nagios_exit(
160 return_code => $code,
161 message => $message
162 );
163
164 =head1 NAME
165
166 check_rabbitmq_watermark - Nagios plugin using RabbitMQ management API to check if the mem_alarm or disk_free_alarm has been triggered
167
168 =head1 SYNOPSIS
169
170 check_rabbitmq_watermark [options] -H hostname
171
172 =head1 DESCRIPTION
173
174 Use the management interface of RabbitMQ to check if the mem_alarm or disk_free_alarm has been triggered.
175
176 It uses Monitoring::Plugin and accepts all standard Nagios options.
177
178 =head1 OPTIONS
179
180 =over
181
182 =item -h | --help
183
184 Display help text
185
186 =item -v | --verbose
187
188 Verbose output
189
190 =item -t | --timeout
191
192 Set a timeout for the check in seconds
193
194 =item -H | --hostname | --host
195
196 The host to connect to
197
198 =item --port
199
200 The port to connect to (default: 15672)
201
202 =item --ssl
203
204 Use SSL when connecting (default: false)
205
206 =item --vhost
207
208 The vhost to create the test queue within (default: /)
209
210 =item -n | --node
211
212 The node name (default is hostname)
213
214 =item -r | --rname
215
216 The rabbit name (default is rabbit)
217
218 =item --username | --user
219
220 The user to connect as (default: guest)
221
222 =item -p | --password
223
224 The password for the user (default: guest)
225
226 =back
227
228 =head1 EXAMPLES
229
230 The defaults all work with a standard fresh install of RabbitMQ, and all that
231 is needed is to specify the host to connect to:
232
233 check_rabbitmq_watermark -H rabbit.example.com
234
235 This returns a standard Nagios result:
236
237 RABBITMQ_ALIVENESS OK - vhost: /
238
239 You can choose a different vhost to use for the check also:
240
241 check_rabbitmq_watermark -H rabbit.example.com --vhost /foo
242
243 =head1 ERRORS
244
245 The check tries to provide useful error messages on the status line for
246 standard error conditions.
247
248 Otherwise it returns the HTTP Error message returned by the management
249 interface.
250
251 =head1 EXIT STATUS
252
253 Returns zero if check is OK otherwise returns standard Nagios exit codes to
254 signify WARNING, UNKNOWN or CRITICAL state.
255
256 =head1 SEE ALSO
257
258 See Monitoring::Plugin(3)
259
260 The RabbitMQ management plugin is described at
261 http://www.rabbitmq.com/management.html
262
263 =head1 LICENSE
264
265 This file is part of nagios-plugins-rabbitmq.
266
267 Copyright 2010, Platform 14.
268
269 Licensed under the Apache License, Version 2.0 (the "License");
270 you may not use this file except in compliance with the License.
271 You may obtain a copy of the License at
272
273 http://www.apache.org/licenses/LICENSE-2.0
274
275 Unless required by applicable law or agreed to in writing, software
276 distributed under the License is distributed on an "AS IS" BASIS,
277 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
278 See the License for the specific language governing permissions and
279 limitations under the License.
280
281 =head1 AUTHOR
282
283 James Casey <[email protected]>
284
285 =cut
286
287 1;