| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
### check_rabbitmq_partition |
| 4 |
|
| 5 |
# Use the management API to check if partitions error conditions exist. |
| 6 |
|
| 7 |
# baseed on check_rabbitmq_watermark.pl, |
| 8 |
# Originally by Nathan Vonnahme, n8v at users dot sourceforge |
| 9 |
# dot net, July 19 2006 |
| 10 |
|
| 11 |
############################################################################## |
| 12 |
# prologue |
| 13 |
use strict; |
| 14 |
use warnings; |
| 15 |
|
| 16 |
use Monitoring::Plugin; |
| 17 |
use LWP::UserAgent; |
| 18 |
use URI::Escape; |
| 19 |
use JSON; |
| 20 |
|
| 21 |
use vars qw($VERSION $PROGNAME $verbose $timeout); |
| 22 |
$VERSION = '2.0.3'; |
| 23 |
|
| 24 |
# get the base name of this script for use in the examples |
| 25 |
use File::Basename; |
| 26 |
$PROGNAME = basename($0); |
| 27 |
|
| 28 |
|
| 29 |
############################################################################## |
| 30 |
# define and get the command line options. |
| 31 |
# see the command line option guidelines at |
| 32 |
# http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS |
| 33 |
|
| 34 |
|
| 35 |
# Instantiate Monitoring::Plugin object (the 'usage' parameter is mandatory) |
| 36 |
my $p = Monitoring::Plugin->new( |
| 37 |
usage => "Usage: %s [options] -H hostname", |
| 38 |
license => "", |
| 39 |
version => $VERSION, |
| 40 |
blurb => 'This plugin uses the RabbitMQ management API to check if the mem_alarm has been triggered', |
| 41 |
); |
| 42 |
|
| 43 |
$p->add_arg(spec => 'hostname|host|H=s', |
| 44 |
help => "Specify the host to connect to", |
| 45 |
required => 1 |
| 46 |
); |
| 47 |
$p->add_arg(spec => 'port=i', |
| 48 |
help => "Specify the port to connect to (default: %s)", |
| 49 |
default => 15672 |
| 50 |
); |
| 51 |
$p->add_arg(spec => 'node|n=s', |
| 52 |
help => "Specify the node name (default is hostname)" |
| 53 |
); |
| 54 |
$p->add_arg(spec => 'username|user|u=s', |
| 55 |
help => "Username (default: %s)", |
| 56 |
default => "guest", |
| 57 |
); |
| 58 |
$p->add_arg(spec => 'password|p=s', |
| 59 |
help => "Password (default: %s)", |
| 60 |
default => "guest" |
| 61 |
); |
| 62 |
|
| 63 |
$p->add_arg(spec => 'ssl|ssl!', |
| 64 |
help => "Use SSL (default: false)", |
| 65 |
default => 0 |
| 66 |
); |
| 67 |
|
| 68 |
$p->add_arg(spec => 'ssl_strict|ssl_strict!', |
| 69 |
help => "Verify SSL certificate (default: true)", |
| 70 |
default => 1 |
| 71 |
); |
| 72 |
|
| 73 |
$p->add_arg(spec => 'proxy|proxy!', |
| 74 |
help => "Use environment proxy (default: true)", |
| 75 |
default => 1 |
| 76 |
); |
| 77 |
$p->add_arg(spec => 'proxyurl=s', |
| 78 |
help => "Use proxy url like http://proxy.domain.com:8080", |
| 79 |
); |
| 80 |
|
| 81 |
$p->add_arg(spec => 'rname|r=s', |
| 82 |
help => "RabbitName (default: %s)", |
| 83 |
default => "rabbit", |
| 84 |
); |
| 85 |
|
| 86 |
# Parse arguments and process standard ones (e.g. usage, help, version) |
| 87 |
$p->getopts; |
| 88 |
|
| 89 |
|
| 90 |
# perform sanity checking on command line options |
| 91 |
|
| 92 |
|
| 93 |
############################################################################## |
| 94 |
# check stuff. |
| 95 |
|
| 96 |
my $hostname=$p->opts->hostname; |
| 97 |
my $port=$p->opts->port; |
| 98 |
|
| 99 |
my $nodename = $p->opts->node; |
| 100 |
|
| 101 |
my $rname = $p->opts->rname; |
| 102 |
|
| 103 |
if (!$nodename) { |
| 104 |
$hostname =~ /^([a-zA-Z0-9-]*)/; |
| 105 |
$nodename = $1; |
| 106 |
} |
| 107 |
|
| 108 |
my $url = sprintf("http%s://%s:%d/api/nodes/%s\@%s", ($p->opts->ssl ? "s" : ""), $hostname, $port, $rname, $nodename); |
| 109 |
my $ua = LWP::UserAgent->new; |
| 110 |
if (defined $p->opts->proxyurl) |
| 111 |
{ |
| 112 |
$ua->proxy('http', $p->opts->proxyurl); |
| 113 |
} |
| 114 |
elsif($p->opts->proxy == 1 ) |
| 115 |
{ |
| 116 |
$ua->env_proxy; |
| 117 |
} |
| 118 |
$ua->agent($PROGNAME.' '); |
| 119 |
$ua->timeout($p->opts->timeout); |
| 120 |
if ($p->opts->ssl and $ua->can('ssl_opts')) { |
| 121 |
$ua->ssl_opts(verify_hostname => $p->opts->ssl_strict); |
| 122 |
} |
| 123 |
|
| 124 |
my $req = HTTP::Request->new(GET => $url); |
| 125 |
$req->authorization_basic($p->opts->username, $p->opts->password); |
| 126 |
my $res = $ua->request($req); |
| 127 |
|
| 128 |
if (!$res->is_success) { |
| 129 |
# Deal with standard error conditions - make the messages more sensible |
| 130 |
if ($res->code == 400) { |
| 131 |
my $bodyref = decode_json $res->content; |
| 132 |
$p->nagios_exit(CRITICAL, $bodyref->{'reason'}); |
| 133 |
} |
| 134 |
$res->code == 404 and $p->nagios_die("Not found"); |
| 135 |
$res->code == 401 and $p->nagios_die("Access refused"); |
| 136 |
if ($res->code < 200 or $res->code > 400 ) { |
| 137 |
$p->nagios_exit(CRITICAL, "Received ".$res->status_line); |
| 138 |
} |
| 139 |
} |
| 140 |
my $bodyref = decode_json $res->content; |
| 141 |
|
| 142 |
my $partitions=$bodyref->{'partitions'}; |
| 143 |
ref($partitions) eq "ARRAY" or $p->nagios_exit(CRITICAL, $res->content); |
| 144 |
scalar(@$partitions)==0 or $p->nagios_exit(CRITICAL, "Partitions detected: @$partitions"); |
| 145 |
|
| 146 |
my($code, $message) = (OK, "No Partitions"); |
| 147 |
$p->nagios_exit( |
| 148 |
return_code => $code, |
| 149 |
message => $message |
| 150 |
); |
| 151 |
|
| 152 |
=head1 NAME |
| 153 |
|
| 154 |
check_rabbitmq_partition - Nagios plugin using RabbitMQ management API to check if a cluster partition has occured |
| 155 |
|
| 156 |
=head1 SYNOPSIS |
| 157 |
|
| 158 |
check_rabbitmq_partition [options] -H hostname |
| 159 |
|
| 160 |
=head1 DESCRIPTION |
| 161 |
|
| 162 |
Use the management interface of RabbitMQ to check if a cluster partition has occured. |
| 163 |
|
| 164 |
It uses Monitoring::Plugin and accepts all standard Nagios options. |
| 165 |
|
| 166 |
=head1 OPTIONS |
| 167 |
|
| 168 |
=over |
| 169 |
|
| 170 |
=item -h | --help |
| 171 |
|
| 172 |
Display help text |
| 173 |
|
| 174 |
=item -v | --verbose |
| 175 |
|
| 176 |
Verbose output |
| 177 |
|
| 178 |
=item -t | --timeout |
| 179 |
|
| 180 |
Set a timeout for the check in seconds |
| 181 |
|
| 182 |
=item -H | --hostname | --host |
| 183 |
|
| 184 |
The host to connect to |
| 185 |
|
| 186 |
=item --port |
| 187 |
|
| 188 |
The port to connect to (default: 15672) |
| 189 |
|
| 190 |
=item --ssl |
| 191 |
|
| 192 |
Use SSL when connecting (default: false) |
| 193 |
|
| 194 |
=item -n | --node |
| 195 |
|
| 196 |
The node name (default is hostname) |
| 197 |
|
| 198 |
=item -r | --rname |
| 199 |
|
| 200 |
The rabbit name (default is rabbit) |
| 201 |
|
| 202 |
=item --username | --user |
| 203 |
|
| 204 |
The user to connect as (default: guest) |
| 205 |
|
| 206 |
=item -p | --password |
| 207 |
|
| 208 |
The password for the user (default: guest) |
| 209 |
|
| 210 |
=back |
| 211 |
|
| 212 |
=head1 EXAMPLES |
| 213 |
|
| 214 |
The defaults all work with a standard fresh install of RabbitMQ, and all that |
| 215 |
is needed is to specify the host to connect to: |
| 216 |
|
| 217 |
check_rabbitmq_node -H rabbit.example.com |
| 218 |
|
| 219 |
This returns a standard Nagios result: |
| 220 |
|
| 221 |
RABBITMQ_NODE OK - No Partitions |
| 222 |
|
| 223 |
=head1 ERRORS |
| 224 |
|
| 225 |
The check tries to provide useful error messages on the status line for |
| 226 |
standard error conditions. |
| 227 |
|
| 228 |
Otherwise it returns the HTTP Error message returned by the management |
| 229 |
interface. |
| 230 |
|
| 231 |
=head1 EXIT STATUS |
| 232 |
|
| 233 |
Returns zero if check is OK otherwise returns standard Nagios exit codes to |
| 234 |
signify WARNING, UNKNOWN or CRITICAL state. |
| 235 |
|
| 236 |
=head1 SEE ALSO |
| 237 |
|
| 238 |
See Monitoring::Plugin(3) |
| 239 |
|
| 240 |
The RabbitMQ management plugin is described at |
| 241 |
http://www.rabbitmq.com/management.html |
| 242 |
|
| 243 |
=head1 LICENSE |
| 244 |
|
| 245 |
This file is part of nagios-plugins-rabbitmq. |
| 246 |
|
| 247 |
Copyright 2010, Platform 14. |
| 248 |
|
| 249 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 250 |
you may not use this file except in compliance with the License. |
| 251 |
You may obtain a copy of the License at |
| 252 |
|
| 253 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 254 |
|
| 255 |
Unless required by applicable law or agreed to in writing, software |
| 256 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 257 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 258 |
See the License for the specific language governing permissions and |
| 259 |
limitations under the License. |
| 260 |
|
| 261 |
=head1 AUTHOR |
| 262 |
|
| 263 |
James Casey <[email protected]> |
| 264 |
|
| 265 |
=cut |
| 266 |
|
| 267 |
1; |