Debian Codegraph @

scripts/check_rabbitmq_overview

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