Debian Codegraph @

scripts/check_rabbitmq_objects

raw
1 #!/usr/bin/env perl
2 #
3 # check_rabbitmq_objects
4 #
5 # Use the management APIs to count various server objects -
6 # vhost, exchange, binding, queue, channel
7 #
8 use strict;
9 use warnings;
10
11 use Monitoring::Plugin;
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 various objects.',
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(spec => 'ssl|ssl!',
49 help => "Use SSL (default: false)",
50 default => 0
51 );
52
53 $p->add_arg(spec => 'ssl_strict|ssl_strict!',
54 help => "Verify SSL certificate (default: true)",
55 default => 1
56 );
57
58 $p->add_arg(spec => 'proxy|proxy!',
59 help => "Use environment proxy (default: true)",
60 default => 1
61 );
62 $p->add_arg(spec => 'proxyurl=s',
63 help => "Use proxy url like http://proxy.domain.com:8080",
64 );
65
66 # Parse arguments and process standard ones (e.g. usage, help, version)
67 $p->getopts;
68
69
70 # perform sanity checking on command line options
71
72
73 ##############################################################################
74 # check stuff.
75
76 my $hostname=$p->opts->hostname;
77 my $port=$p->opts->port;
78
79 my $ua = LWP::UserAgent->new;
80 if (defined $p->opts->proxyurl)
81 {
82 $ua->proxy('http', $p->opts->proxyurl);
83 }
84 elsif($p->opts->proxy == 1 )
85 {
86 $ua->env_proxy;
87 }
88 $ua->agent($PROGNAME.' ');
89 $ua->timeout($p->opts->timeout);
90 if ($p->opts->ssl and $ua->can('ssl_opts')) {
91 $ua->ssl_opts(verify_hostname => $p->opts->ssl_strict);
92 }
93
94 my @calls = ("vhost", "exchange", "binding", "queue", "channel");
95 for my $call (@calls) {
96 my $url = sprintf("http%s://%s:%d/api/%ss", ($p->opts->ssl ? "s" : ""), $hostname, $port, $call);
97 my ($code, $result) = request($url);
98 if ($code != 200) {
99 $p->nagios_exit(CRITICAL, "$result : /api/". $call."s");
100 }
101 # we're always returned a list - count it
102 my $count = $#{$result};
103 $count = 0 if $count == -1;
104 $p->add_message(OK, sprintf("$call (%.2f%%)", $count)) ;
105 $p->add_perfdata(label=>$call, value => $count);
106
107 }
108
109 $p->nagios_exit(return_code => OK, message => "Gathered Object Counts");
110
111 sub request {
112 my ($url) = @_;
113 my $req = HTTP::Request->new(GET => $url);
114 $req->authorization_basic($p->opts->username, $p->opts->password);
115 my $res = $ua->request($req);
116
117 if (!$res->is_success) {
118 # Deal with standard error conditions - make the messages more sensible
119 if ($res->code == 400) {
120 my $bodyref = decode_json $res->content;
121 return (400, $bodyref->{'reason'});
122
123 }
124 $res->code == 404 and return (404, "Not Found");
125 $res->code == 401 and return (401, "Access Refused");
126 $res->status_line =~ /Can\'t connect/ and return (500, "Connection Refused : $url");
127 if ($res->code < 200 or $res->code > 400 ) {
128 return ($res->code, "Received ".$res->status_line);
129 }
130 }
131 my $bodyref = decode_json $res->content;
132 return($res->code, $bodyref);
133 }
134
135 =head1 NAME
136
137 check_rabbitmq_objects - Nagios plugin using RabbitMQ management API to
138 count the number of various broker objects
139
140 =head1 SYNOPSIS
141
142 check_rabbitmq_objects [options] -H hostname
143
144 =head1 DESCRIPTION
145
146 Use the management interface of RabbitMQ to count the number of various
147 broker objects. These are published as performance metrics for the check.
148
149 Currently the following objects are counted:
150
151 =over
152
153 =item * vhost
154
155 =item * exchange
156
157 =item * binding
158
159 =item * queue
160
161 =item * channel
162
163 =back
164
165 It uses Monitoring::Plugin and accepts all standard Nagios options.
166
167 =head1 OPTIONS
168
169 =over
170
171 =item -h | --help
172
173 Display help text
174
175 =item -v | --verbose
176
177 Verbose output
178
179 =item -t | --timeout
180
181 Set a timeout for the check in seconds
182
183 =item -H | --hostname | --host
184
185 The host to connect to
186
187 =item --port
188
189 The port to connect to (default: 15672)
190
191 =item --ssl
192
193 Use SSL when connecting (default: false)
194
195 =item --username | --user
196
197 The user to connect as (default: guest)
198
199 =item -p | --password
200
201 The password for the user (default: guest)
202
203 =back
204
205 =head1 EXAMPLES
206
207 The defaults all work with a standard fresh install of RabbitMQ, and all that
208 is needed is to specify the host to connect to:
209
210 check_rabbitmq_objects -H rabbit.example.com
211
212 This returns a standard Nagios result:
213
214 RABBITMQ_OBJECTS OK - Gathered Object Counts | vhost=1;; exchange=7;;
215 binding=2;; queue=1;; channel=0;;
216
217 =head1 ERRORS
218
219 The check tries to provide useful error messages on the status line for
220 standard error conditions.
221
222 Otherwise it returns the HTTP Error message returned by the management
223 interface.
224
225 =head1 EXIT STATUS
226
227 Returns zero if check is OK otherwise returns standard Nagios exit codes to
228 signify WARNING, UNKNOWN or CRITICAL state.
229
230 =head1 SEE ALSO
231
232 See Monitoring::Plugin(3)
233
234 The RabbitMQ management plugin is described at
235 http://www.rabbitmq.com/management.html
236
237 =head1 LICENSE
238
239 This file is part of nagios-plugins-rabbitmq.
240
241 Copyright 2010, Platform 14.
242
243 Licensed under the Apache License, Version 2.0 (the "License");
244 you may not use this file except in compliance with the License.
245 You may obtain a copy of the License at
246
247 http://www.apache.org/licenses/LICENSE-2.0
248
249 Unless required by applicable law or agreed to in writing, software
250 distributed under the License is distributed on an "AS IS" BASIS,
251 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
252 See the License for the specific language governing permissions and
253 limitations under the License.
254
255 =head1 AUTHOR
256
257 James Casey <[email protected]>
258
259 =cut
260
261 1;