Debian Codegraph @

scripts/check_rabbitmq_connections

raw
1 #!/usr/bin/env perl
2 #
3 # check_rabbitmq_connections
4 #
5 # Use the management APIs to check amount of connections
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 API to monitor connections.',
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=THRESHOLD[,THRESHOLD[,THRESHOLD[,THRESHOLD]]]
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=THRESHOLD[,THRESHOLD[,THRESHOLD[,THRESHOLD]]]
61 Critical thresholds specified in order that the metrics are returned.
62 Specify -1 if no critical threshold.},
63 );
64
65 $p->add_arg(
66 spec => 'clientuser=s',
67 help => 'Specify the client username to limit the connections for (optional)',
68 );
69
70 $p->add_arg(spec => 'ssl|ssl!',
71 help => "Use SSL (default: false)",
72 default => 0
73 );
74
75 $p->add_arg(spec => 'ssl_strict|ssl_strict!',
76 help => "Verify SSL certificate (default: true)",
77 default => 1
78 );
79
80 $p->add_arg(spec => 'proxy|proxy!',
81 help => "Use environment proxy (default: true)",
82 default => 1
83 );
84 $p->add_arg(spec => 'proxyurl=s',
85 help => "Use proxy url like http://proxy.domain.com:8080",
86 );
87
88 # Parse arguments and process standard ones (e.g. usage, help, version)
89 $p->getopts;
90
91
92 # perform sanity checking on command line options
93 my %warning;
94 if (defined $p->opts->warning) {
95 my @warning = split(',', $p->opts->warning);
96 $p->nagios_die("You should specify 1 to 4 ranges for --warning argument") unless $#warning < 4;
97
98 $warning{'connections'} = shift @warning;
99 $warning{'connections_notrunning'} = shift @warning;
100 $warning{'receive_rate'} = shift @warning;
101 $warning{'send_rate'} = shift @warning;
102 }
103
104 my %critical;
105 if (defined $p->opts->critical) {
106 my @critical = split(',', $p->opts->critical);
107 $p->nagios_die("You should specify specify 1 to 4 ranges for --critical argument") unless $#critical < 4;
108
109 $critical{'connections'} = shift @critical;
110 $critical{'connections_notrunning'} = shift @critical;
111 $critical{'receive_rate'} = shift @critical;
112 $critical{'send_rate'} = shift @critical;
113 }
114
115
116 ##############################################################################
117 # check stuff.
118
119 my $hostname=$p->opts->hostname;
120 my $port=$p->opts->port;
121
122 my $ua = LWP::UserAgent->new;
123 if (defined $p->opts->proxyurl)
124 {
125 $ua->proxy('http', $p->opts->proxyurl);
126 }
127 elsif($p->opts->proxy == 1 )
128 {
129 $ua->env_proxy;
130 }
131 $ua->agent($PROGNAME.' ');
132 $ua->timeout($p->opts->timeout);
133 if ($p->opts->ssl and $ua->can('ssl_opts')) {
134 $ua->ssl_opts(verify_hostname => $p->opts->ssl_strict);
135 }
136
137 my $url = sprintf("http%s://%s:%d/api/connections", ($p->opts->ssl ? "s" : ""), $hostname, $port);
138 my ($retcode, $result) = request($url);
139 if ($retcode != 200) {
140 $p->nagios_exit(CRITICAL, "$result : $url");
141 }
142
143 my $values = {};
144 $values->{'connections'} = 0;
145 $values->{'connections_notrunning'} = 0;
146 $values->{'receive_rate'} = 0;
147 $values->{'send_rate'} = 0;
148
149 for my $connection (@$result) {
150 if (not defined($p->opts->clientuser) or $p->opts->clientuser eq $connection->{"user"}) {
151 $values->{'connections'}++;
152 $values->{'connections_notrunning'}++ if $connection->{"state"} ne "running";
153 $values->{'receive_rate'} += $connection->{"recv_oct_details"}->{"rate"};
154 $values->{'send_rate'} += $connection->{"send_oct_details"}->{"rate"};
155 }
156 }
157
158 my @metrics = ("connections", "connections_notrunning", "receive_rate", "send_rate");
159 for my $metric (@metrics) {
160 my $warning = undef;
161 $warning = $warning{$metric} if (defined $warning{$metric} and $warning{$metric} ne -1);
162 my $critical = undef;
163 $critical = $critical{$metric} if (defined $critical{$metric} and $critical{$metric} ne -1);
164
165 my $value = 0;
166 $value = $values->{$metric} if defined $values->{$metric};
167 my $code = $p->check_threshold(check => $value, warning => $warning, critical=> $critical);
168 $p->add_message($code, sprintf("$metric ".$STATUS_TEXT{$code}." (%d)", $value)) ;
169 $p->add_perfdata(label=>$metric, value => $value, warning=>$warning, critical=> $critical);
170 }
171
172 my ($code, $message) = $p->check_messages(join_all=>', ');
173 $p->nagios_exit(return_code => $code, message => $message);
174
175
176 sub request {
177 my ($url) = @_;
178 my $req = HTTP::Request->new(GET => $url);
179 $req->authorization_basic($p->opts->username, $p->opts->password);
180 my $res = $ua->request($req);
181
182 if (!$res->is_success) {
183 # Deal with standard error conditions - make the messages more sensible
184 if ($res->code == 400) {
185 my $bodyref = decode_json $res->content;
186 return (400, $bodyref->{'reason'});
187
188 }
189 $res->code == 404 and return (404, "Not Found");
190 $res->code == 401 and return (401, "Access Refused");
191 $res->status_line =~ /Can\'t connect/ and return (500, "Connection Refused : $url");
192 if ($res->code < 200 or $res->code > 400 ) {
193 return ($res->code, "Received ".$res->status_line);
194 }
195 }
196 my $bodyref = decode_json $res->content;
197 return($res->code, $bodyref);
198 }
199
200 =head1 NAME
201
202 check_rabbitmq_connections - Nagios plugin using RabbitMQ management API to
203 count the connections running, their state and optionally limit these checks to
204 specific connected client user accounts.
205
206 =head1 SYNOPSIS
207
208 check_rabbitmq_connections [options] -H hostname
209
210 =head1 DESCRIPTION
211
212 Use the management interface of RabbitMQ to count the number of established
213 connections, those that are not in state running and also their throughput. All
214 values are published as performance metrics for the check.
215
216 Critical and warning thresholds can be set for each of the metric.
217
218 It uses Monitoring::Plugin and accepts all standard Nagios options.
219
220 =head1 OPTIONS
221
222 =over
223
224 =item -h | --help
225
226 Display help text
227
228 =item -v | --verbose
229
230 Verbose output
231
232 =item -t | --timeout
233
234 Set a timeout for the check in seconds
235
236 =item -H | --hostname | --host
237
238 The host to connect to
239
240 =item --port
241
242 The port to connect to (default: 15672)
243
244 =item --ssl
245
246 Use SSL when connecting (default: false)
247
248 =item --username | --user
249
250 The user to connect as (default: guest)
251
252 =item -p | --password
253
254 The password for the user (default: guest)
255
256 =item -w | --warning
257
258 The warning levels for each count of connections established, connections
259 in a non-running state (flow, blocked), receive rate and send rate. This
260 field consists of one to four comma-separated thresholds. Specify -1 if
261 no threshold for a particular count.
262
263 =item -c | --critical
264
265 The critical levels for each count of connections established, connections
266 in a non-running state (flow, blocked), receive rate and send rate. This
267 field consists of one to four comma-separated thresholds. Specify -1 if
268 no threshold for a particular count.
269
270 =item --clientuser
271
272 Specify the client username to limit the connections checks for.
273
274 =back
275
276 =head1 THRESHOLD FORMAT
277
278 The format of thresholds specified in --warning and --critical arguments
279 is defined at <http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT>.
280
281 For example to be crtical if more than 5 connections, more than 2 connections not running,
282 less than 200b/s received use
283
284 --critical=5,2,200,-1
285
286 =head1 EXAMPLES
287
288 The defaults all work with a standard fresh install of RabbitMQ, and all that
289 is needed is to specify the host to connect to:
290
291 check_rabbitmq_connections -H localhost -w 1: -c 1:
292
293 This returns a standard Nagios result:
294
295 RABBITMQ_CONNECTIONS CRITICAL - connections CRITICAL (0),
296 connections_notrunning WARNING (0), receive_rate OK (0) send_rate OK (0) |
297 connections=0;;1: connections_notrunning=0;1:; receive_rate=0;; send_rate=0;;
298
299 =head1 ERRORS
300
301 The check tries to provide useful error messages on the status line for
302 standard error conditions.
303
304 Otherwise it returns the HTTP Error message returned by the management
305 interface.
306
307 =head1 EXIT STATUS
308
309 Returns zero if check is OK otherwise returns standard Nagios exit codes to
310 signify WARNING, UNKNOWN or CRITICAL state.
311
312 =head1 SEE ALSO
313
314 See Monitoring::Plugin(3)
315
316 The RabbitMQ management plugin is described at
317 http://www.rabbitmq.com/management.html
318
319 =head1 LICENSE
320
321 This file is part of nagios-plugins-rabbitmq.
322
323 Copyright 2010, Platform 14.
324
325 Licensed under the Apache License, Version 2.0 (the "License");
326 you may not use this file except in compliance with the License.
327 You may obtain a copy of the License at
328
329 http://www.apache.org/licenses/LICENSE-2.0
330
331 Unless required by applicable law or agreed to in writing, software
332 distributed under the License is distributed on an "AS IS" BASIS,
333 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
334 See the License for the specific language governing permissions and
335 limitations under the License.
336
337 =head1 AUTHOR
338
339 James Casey <[email protected]>
340
341 =cut
342
343 1;