3
# check_rabbitmq_overview
5
# Use the management APIs to count pending messages in broker
10
use Nagios::Plugin qw(OK CRITICAL WARNING UNKNOWN);
11
use Nagios::Plugin::Functions qw(%STATUS_TEXT);
16
use vars qw($VERSION $PROGNAME $verbose $timeout);
19
# get the base name of this script for use in the examples
21
$PROGNAME = basename($0);
23
my $p = Nagios::Plugin->new(
24
usage => "Usage: %s [options] -H hostname",
27
blurb => 'This plugin uses the RabbitMQ management to count messages in server.',
30
$p->add_arg(spec => 'hostname|host|H=s',
31
help => "Specify the host to connect to",
34
$p->add_arg(spec => 'port=i',
35
help => "Specify the port to connect to (default: %s)",
39
$p->add_arg(spec => 'username|user|u=s',
40
help => "Username (default: %s)",
43
$p->add_arg(spec => 'password|p=s',
44
help => "Password (default: %s)",
49
spec => 'warning|w=s',
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.},
58
spec => 'critical|c=s',
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.},
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)
78
# perform sanity checking on command line options
80
if (defined $p->opts->warning) {
81
my @warning = split(',', $p->opts->warning);
82
$p->nagios_die("You should specify three ranges for --warning argument") unless $#warning == 2;
84
$warning{'messages'} = shift @warning;
85
$warning{'messages_ready'} = shift @warning;
86
$warning{'messages_unacknowledged'} = shift @warning;
90
if (defined $p->opts->critical) {
91
my @critical = split(',', $p->opts->critical);
92
$p->nagios_die("You should specify three ranges for --critical argument") unless $#critical == 2;
94
$critical{'messages'} = shift @critical;
95
$critical{'messages_ready'} = shift @critical;
96
$critical{'messages_unacknowledged'} = shift @critical;
100
##############################################################################
103
my $hostname=$p->opts->hostname;
104
my $port=$p->opts->port;
106
my $ua = LWP::UserAgent->new(env_proxy => $p->opts->proxy);
107
$ua->agent($PROGNAME.' ');
108
$ua->timeout($p->opts->timeout);
109
# Different security domains in 2.5 and 2.6
110
$ua->credentials("$hostname:$port",
111
"RabbitMQ Management", $p->opts->username, $p->opts->password);
112
$ua->credentials("$hostname:$port",
113
"Management: Web UI", $p->opts->username, $p->opts->password);
116
my $url = sprintf("http%s://%s:%d/api/overview", ($p->opts->ssl ? "s" : ""), $hostname, $port);
117
my ($retcode, $result) = request($url);
118
if ($retcode != 200) {
119
$p->nagios_exit(CRITICAL, "$result : /api/overview");
122
my @metrics = ( "messages", "messages_ready", "messages_unacknowledged");
123
for my $metric (@metrics) {
125
$warning = $warning{$metric} if (defined $warning{$metric} and $warning{$metric} != -1);
126
my $critical = undef;
127
$critical = $critical{$metric} if (defined $critical{$metric} and $critical{$metric} != -1);
129
my $value = $result->{'queue_totals'}->{$metric};
130
my $code = $p->check_threshold(check => $value, warning => $warning, critical=> $critical);
131
$p->add_message($code, sprintf("$metric ".$STATUS_TEXT{$code}." (%d)", $value)) ;
132
$p->add_perfdata(label=>$metric, value => $value, warning=>$warning, critical=> $critical);
135
my ($code, $message) = $p->check_messages(join_all=>', ');
136
$p->nagios_exit(return_code => $code, message => $message);
141
my $req = HTTP::Request->new(GET => $url);
142
my $res = $ua->request($req);
144
if (!$res->is_success) {
145
# Deal with standard error conditions - make the messages more sensible
146
if ($res->code == 400) {
147
my $bodyref = decode_json $res->content;
148
return (400, $bodyref->{'reason'});
151
$res->code == 404 and return (404, "Not Found");
152
$res->code == 401 and return (401, "Access Refused");
153
$res->status_line =~ /Can\'t connect/ and return (500, "Connection Refused : $url");
154
if ($res->code < 200 or $res->code > 400 ) {
155
return ($res->code, "Received ".$res->status_line);
158
my $bodyref = decode_json $res->content;
159
return($res->code, $bodyref);
164
check_rabbitmq_overview - Nagios plugin using RabbitMQ management API to
165
count the messages pending on the broker
169
check_rabbitmq_overview [options] -H hostname
173
Use the management interface of RabbitMQ to count the number of pending,
174
ready and unacknowledged messages. These are published as performance
175
metrics for the check.
177
Critical and warning thresholds can be set for each of the metrics.
179
It uses Nagios::Plugin and accepts all standard Nagios options.
195
Set a timeout for the check in seconds
197
=item -H | --hostname | --host
199
The host to connect to
203
The port to connect to (default: 55672)
207
Use SSL when connecting (default: false)
209
=item --username | --user
211
The user to connect as (default: guest)
215
The password for the user (default: guest)
219
The warning levels for each count of messages, messages_ready and
220
messages_unacknowledged. This field consists of three comma-separated
221
integers. Specify -1 if no threshold for a particular count.
223
=item -c | --critical
225
The critical levels for each count of messages, messages_ready and
226
messages_unacknowledged. This field consists of three comma-separated
227
integers. Specify -1 if no threshold for a particular count
233
The defaults all work with a standard fresh install of RabbitMQ, and all that
234
is needed is to specify the host to connect to:
236
check_rabbitmq_overview -H rabbit.example.com
238
This returns a standard Nagios result:
240
RABBITMQ_OVERVIEW OK - messages OK (25794) messages_ready OK (22971)
241
messages_unacknowledged OK (2823) | messages=25794;;
242
messages_ready=22971;; messages_unacknowledged=2823;;
246
The check tries to provide useful error messages on the status line for
247
standard error conditions.
249
Otherwise it returns the HTTP Error message returned by the management
254
Returns zero if check is OK otherwise returns standard Nagios exit codes to
255
signify WARNING, UNKNOWN or CRITICAL state.
259
See Nagios::Plugin(3)
261
The RabbitMQ management plugin is described at
262
http://www.rabbitmq.com/management.html
266
This file is part of nagios-plugins-rabbitmq.
268
Copyright 2010, Platform 14.
270
Licensed under the Apache License, Version 2.0 (the "License");
271
you may not use this file except in compliance with the License.
272
You may obtain a copy of the License at
274
http://www.apache.org/licenses/LICENSE-2.0
276
Unless required by applicable law or agreed to in writing, software
277
distributed under the License is distributed on an "AS IS" BASIS,
278
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
279
See the License for the specific language governing permissions and
280
limitations under the License.
284
James Casey <jamesc.000@gmail.com>