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