3
### check_rabbitmq_aliveness.pl
5
# Use the management aliveness-check to send/receive a message through a vhost
7
# Originally by Nathan Vonnahme, n8v at users dot sourceforge
8
# dot net, July 19 2006
10
##############################################################################
20
use vars qw($VERSION $PROGNAME $verbose $timeout);
23
# get the base name of this script for use in the examples
25
$PROGNAME = basename($0);
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
34
# Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory)
35
my $p = Nagios::Plugin->new(
36
usage => "Usage: %s [options] -H hostname",
39
blurb => 'This plugin uses the RabbitMQ management aliveness-check to send/receive a message through a vhost.',
42
$p->add_arg(spec => 'hostname|host|H=s',
43
help => "Specify the host to connect to",
46
$p->add_arg(spec => 'port=i',
47
help => "Specify the port to connect to (default: %s)",
51
$p->add_arg(spec => 'username|user|u=s',
52
help => "Username (default: %s)",
55
$p->add_arg(spec => 'password|p=s',
56
help => "Password (default: %s)",
60
$p->add_arg(spec => 'vhost=s',
61
help => "Specify the vhost to test (default: %s)",
65
$p->add_arg(spec => 'ssl|ssl!',
66
help => "Use SSL (default: false)",
70
$p->add_arg(spec => 'proxy|proxy!',
71
help => "Use environment proxy (default: true)",
75
# Parse arguments and process standard ones (e.g. usage, help, version)
79
# perform sanity checking on command line options
82
##############################################################################
85
my $hostname=$p->opts->hostname;
86
my $port=$p->opts->port;
87
my $vhost=uri_escape($p->opts->vhost);
89
my $url = sprintf("http%s://%s:%d/api/aliveness-test/%s", ($p->opts->ssl ? "s" : ""), $hostname, $port, $vhost);
91
my $ua = LWP::UserAgent->new(env_proxy => $p->opts->proxy);
92
$ua->agent($PROGNAME.' ');
93
$ua->timeout($p->opts->timeout);
94
# Different security domains in 2.5 and 2.6
95
$ua->credentials("$hostname:$port",
96
"RabbitMQ Management", $p->opts->username, $p->opts->password);
97
$ua->credentials("$hostname:$port",
98
"Management: Web UI", $p->opts->username, $p->opts->password);
99
my $req = HTTP::Request->new(GET => $url);
100
my $res = $ua->request($req);
102
if (!$res->is_success) {
103
# Deal with standard error conditions - make the messages more sensible
104
if ($res->code == 400) {
105
my $bodyref = decode_json $res->content;
106
$p->nagios_exit(CRITICAL, $bodyref->{'reason'});
108
$res->code == 404 and $p->nagios_die("Not found: ".$p->opts->vhost);
109
$res->code == 401 and $p->nagios_die("Access refused: ".$p->opts->vhost);
110
if ($res->code < 200 or $res->code > 400 ) {
111
$p->nagios_exit(CRITICAL, "Received ".$res->status_line." for vhost: ".$p->opts->vhost);
115
my $bodyref = decode_json $res->content;
116
$bodyref->{'status'} eq "ok" or $p->nagios_exit(CRITICAL, $res->content);
117
my($code, $message) = (OK, "vhost: ".$p->opts->vhost);
119
return_code => $code,
125
check_rabbitmq_aliveness - Nagios plugin using RabbitMQ management API to
126
check liveness by send/receive a message through a vhost
130
check_rabbitmq_aliveness [options] -H hostname
134
Use the management interface of RabbitMQ to check that the server is alive.
135
It declares a test queue, then publishes and consumes a message.
137
It uses Nagios::Plugin and accepts all standard Nagios options.
153
Set a timeout for the check in seconds
155
=item -H | --hostname | --host
157
The host to connect to
161
The port to connect to (default: 55672)
165
Use SSL when connecting (default: false)
169
The vhost to create the test queue within (default: /)
171
=item --username | --user
173
The user to connect as (default: guest)
177
The password for the user (default: guest)
183
The defaults all work with a standard fresh install of RabbitMQ, and all that
184
is needed is to specify the host to connect to:
186
check_rabbitmq_aliveness -H rabbit.example.com
188
This returns a standard Nagios result:
190
RABBITMQ_ALIVENESS OK - vhost: /
192
You can choose a different vhost to use for the check also:
194
check_rabbitmq_aliveness -H rabbit.example.com --vhost /foo
198
The check tries to provide useful error messages on the status line for
199
standard error conditions.
201
Otherwise it returns the HTTP Error message returned by the management
206
Returns zero if check is OK otherwise returns standard Nagios exit codes to
207
signify WARNING, UNKNOWN or CRITICAL state.
211
See Nagios::Plugin(3)
213
The RabbitMQ management plugin is described at
214
http://www.rabbitmq.com/management.html
218
This file is part of nagios-plugins-rabbitmq.
220
Copyright 2010, Platform 14.
222
Licensed under the Apache License, Version 2.0 (the "License");
223
you may not use this file except in compliance with the License.
224
You may obtain a copy of the License at
226
http://www.apache.org/licenses/LICENSE-2.0
228
Unless required by applicable law or agreed to in writing, software
229
distributed under the License is distributed on an "AS IS" BASIS,
230
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
231
See the License for the specific language governing permissions and
232
limitations under the License.
236
James Casey <jamesc.000@gmail.com>