Debian Codegraph @

scripts/check_rabbitmq_aliveness

raw
1 #!/usr/bin/env perl
2
3 ### check_rabbitmq_aliveness.pl
4
5 # Use the management aliveness-check to send/receive a message through a vhost
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 aliveness-check to send/receive a message through a vhost.',
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
51 $p->add_arg(spec => 'username|user|u=s',
52 help => "Username (default: %s)",
53 default => "guest",
54 );
55 $p->add_arg(spec => 'password|p=s',
56 help => "Password (default: %s)",
57 default => "guest"
58 );
59
60 $p->add_arg(spec => 'vhost=s',
61 help => "Specify the vhost to test (default: %s)",
62 default => "/"
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 # Parse arguments and process standard ones (e.g. usage, help, version)
84 $p->getopts;
85
86
87 # perform sanity checking on command line options
88
89
90 ##############################################################################
91 # check stuff.
92
93 my $hostname=$p->opts->hostname;
94 my $port=$p->opts->port;
95 my $vhost=uri_escape($p->opts->vhost);
96
97 my $url = sprintf("http%s://%s:%d/api/aliveness-test/%s", ($p->opts->ssl ? "s" : ""), $hostname, $port, $vhost);
98
99 my $ua = LWP::UserAgent->new;
100 if (defined $p->opts->proxyurl)
101 {
102 $ua->proxy('http', $p->opts->proxyurl);
103 }
104 elsif($p->opts->proxy == 1 )
105 {
106 $ua->env_proxy;
107 }
108 $ua->agent($PROGNAME.' ');
109 $ua->timeout($p->opts->timeout);
110 if ($p->opts->ssl and $ua->can('ssl_opts')) {
111 $ua->ssl_opts(verify_hostname => $p->opts->ssl_strict);
112 }
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 $p->nagios_exit(CRITICAL, $bodyref->{'reason'});
122 }
123 $res->code == 404 and $p->nagios_die("Not found: ".$p->opts->vhost);
124 $res->code == 401 and $p->nagios_die("Access refused: ".$p->opts->vhost);
125 if ($res->code < 200 or $res->code > 400 ) {
126 $p->nagios_exit(CRITICAL, "Received ".$res->status_line." for vhost: ".$p->opts->vhost);
127 }
128 }
129
130 my $bodyref = decode_json $res->content;
131 $bodyref->{'status'} eq "ok" or $p->nagios_exit(CRITICAL, $res->content);
132 my($code, $message) = (OK, "vhost: ".$p->opts->vhost);
133 $p->nagios_exit(
134 return_code => $code,
135 message => $message
136 );
137
138 =head1 NAME
139
140 check_rabbitmq_aliveness - Nagios plugin using RabbitMQ management API to
141 check liveness by send/receive a message through a vhost
142
143 =head1 SYNOPSIS
144
145 check_rabbitmq_aliveness [options] -H hostname
146
147 =head1 DESCRIPTION
148
149 Use the management interface of RabbitMQ to check that the server is alive.
150 It declares a test queue, then publishes and consumes a message.
151
152 It uses Monitoring::Plugin and accepts all standard Nagios options.
153
154 =head1 OPTIONS
155
156 =over
157
158 =item -h | --help
159
160 Display help text
161
162 =item -v | --verbose
163
164 Verbose output
165
166 =item -t | --timeout
167
168 Set a timeout for the check in seconds
169
170 =item -H | --hostname | --host
171
172 The host to connect to
173
174 =item --port
175
176 The port to connect to (default: 15672)
177
178 =item --ssl
179
180 Use SSL when connecting (default: false)
181
182 =item --vhost
183
184 The vhost to create the test queue within (default: /)
185
186 =item --username | --user
187
188 The user to connect as (default: guest)
189
190 =item -p | --password
191
192 The password for the user (default: guest)
193
194 =back
195
196 =head1 EXAMPLES
197
198 The defaults all work with a standard fresh install of RabbitMQ, and all that
199 is needed is to specify the host to connect to:
200
201 check_rabbitmq_aliveness -H rabbit.example.com
202
203 This returns a standard Nagios result:
204
205 RABBITMQ_ALIVENESS OK - vhost: /
206
207 You can choose a different vhost to use for the check also:
208
209 check_rabbitmq_aliveness -H rabbit.example.com --vhost /foo
210
211 =head1 ERRORS
212
213 The check tries to provide useful error messages on the status line for
214 standard error conditions.
215
216 Otherwise it returns the HTTP Error message returned by the management
217 interface.
218
219 =head1 EXIT STATUS
220
221 Returns zero if check is OK otherwise returns standard Nagios exit codes to
222 signify WARNING, UNKNOWN or CRITICAL state.
223
224 =head1 SEE ALSO
225
226 See Monitoring::Plugin(3)
227
228 The RabbitMQ management plugin is described at
229 http://www.rabbitmq.com/management.html
230
231 =head1 LICENSE
232
233 This file is part of nagios-plugins-rabbitmq.
234
235 Copyright 2010, Platform 14.
236
237 Licensed under the Apache License, Version 2.0 (the "License");
238 you may not use this file except in compliance with the License.
239 You may obtain a copy of the License at
240
241 http://www.apache.org/licenses/LICENSE-2.0
242
243 Unless required by applicable law or agreed to in writing, software
244 distributed under the License is distributed on an "AS IS" BASIS,
245 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
246 See the License for the specific language governing permissions and
247 limitations under the License.
248
249 =head1 AUTHOR
250
251 James Casey <[email protected]>
252
253 =cut
254
255 1;