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