~ubuntu-branches/ubuntu/wily/nagios-plugins-rabbitmq/wily-proposed

« back to all changes in this revision

Viewing changes to scripts/check_rabbitmq_partition

  • Committer: Package Import Robot
  • Author(s): Cyril Bouthors
  • Date: 2014-04-18 10:57:00 UTC
  • Revision ID: package-import@ubuntu.com-20140418105700-rx7126mtagy8fj9b
Tags: 20140418
Fixed debian/postinst

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 Nagios::Plugin ;
 
17
use LWP::UserAgent;
 
18
use URI::Escape;
 
19
use JSON;
 
20
 
 
21
use vars qw($VERSION $PROGNAME  $verbose $timeout);
 
22
$VERSION = '1.0';
 
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 Nagios::Plugin object (the 'usage' parameter is mandatory)
 
36
my $p = Nagios::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 => 55672
 
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 => 'proxy|proxy!',
 
69
    help => "Use environment proxy (default: true)",
 
70
    default => 1
 
71
);
 
72
 
 
73
# Parse arguments and process standard ones (e.g. usage, help, version)
 
74
$p->getopts;
 
75
 
 
76
 
 
77
# perform sanity checking on command line options
 
78
 
 
79
 
 
80
##############################################################################
 
81
# check stuff.
 
82
 
 
83
my $hostname=$p->opts->hostname;
 
84
my $port=$p->opts->port;
 
85
 
 
86
my $nodename = $p->opts->node;
 
87
 
 
88
if (!$nodename) {
 
89
    $hostname =~ /^([a-zA-Z0-9-]*)/;
 
90
    $nodename = $1;
 
91
}
 
92
 
 
93
my $url = sprintf("http%s://%s:%d/api/nodes/rabbit\@%s", ($p->opts->ssl ? "s" : ""), $hostname, $port, $nodename);
 
94
 
 
95
my $ua = LWP::UserAgent->new(env_proxy => $p->opts->proxy);
 
96
$ua->agent($PROGNAME.' ');
 
97
$ua->timeout($p->opts->timeout);
 
98
# Different security domains in 2.5 and 2.6
 
99
$ua->credentials("$hostname:$port",
 
100
    "RabbitMQ Management", $p->opts->username, $p->opts->password);
 
101
$ua->credentials("$hostname:$port",
 
102
    "Management: Web UI", $p->opts->username, $p->opts->password);
 
103
my $req = HTTP::Request->new(GET => $url);
 
104
my $res = $ua->request($req);
 
105
 
 
106
if (!$res->is_success) {
 
107
    # Deal with standard error conditions - make the messages more sensible
 
108
    if ($res->code == 400) {
 
109
        my $bodyref = decode_json $res->content;
 
110
        $p->nagios_exit(CRITICAL, $bodyref->{'reason'});
 
111
    }
 
112
    $res->code == 404 and $p->nagios_die("Not found");
 
113
    $res->code == 401 and $p->nagios_die("Access refused");
 
114
    if ($res->code < 200 or $res->code > 400 ) {
 
115
        $p->nagios_exit(CRITICAL, "Received ".$res->status_line);
 
116
    }
 
117
}
 
118
my $bodyref = decode_json $res->content;
 
119
 
 
120
my $partitions=$bodyref->{'partitions'};
 
121
ref($partitions) eq "ARRAY" or $p->nagios_exit(CRITICAL, $res->content);
 
122
scalar(@$partitions)==0 or $p->nagios_exit(CRITICAL, "Partitions detected: @$partitions");
 
123
 
 
124
my($code, $message) = (OK, "No Partitions");
 
125
$p->nagios_exit(
 
126
    return_code => $code,
 
127
    message => $message
 
128
);
 
129
 
 
130
=head1 NAME
 
131
 
 
132
check_rabbitmq_partition - Nagios plugin using RabbitMQ management API to check if a cluster partition has occured
 
133
 
 
134
=head1 SYNOPSIS
 
135
 
 
136
check_rabbitmq_partition [options] -H hostname
 
137
 
 
138
=head1 DESCRIPTION
 
139
 
 
140
Use the management interface of RabbitMQ to check if a cluster partition has occured.
 
141
 
 
142
It uses Nagios::Plugin and accepts all standard Nagios options.
 
143
 
 
144
=head1 OPTIONS
 
145
 
 
146
=over
 
147
 
 
148
=item -h | --help
 
149
 
 
150
Display help text
 
151
 
 
152
=item -v | --verbose
 
153
 
 
154
Verbose output
 
155
 
 
156
=item -t | --timeout
 
157
 
 
158
Set a timeout for the check in seconds
 
159
 
 
160
=item -H | --hostname | --host
 
161
 
 
162
The host to connect to
 
163
 
 
164
=item --port
 
165
 
 
166
The port to connect to (default: 55672)
 
167
 
 
168
=item --ssl
 
169
 
 
170
Use SSL when connecting (default: false)
 
171
 
 
172
=item -n | --node
 
173
 
 
174
The node name (default is hostname)
 
175
 
 
176
=item --username | --user
 
177
 
 
178
The user to connect as (default: guest)
 
179
 
 
180
=item --pass
 
181
 
 
182
The password for the user (default: guest)
 
183
 
 
184
=back
 
185
 
 
186
=head1 EXAMPLES
 
187
 
 
188
The defaults all work with a standard fresh install of RabbitMQ, and all that
 
189
is needed is to specify the host to connect to:
 
190
 
 
191
    check_rabbitmq_node -H rabbit.example.com
 
192
 
 
193
This returns a standard Nagios result:
 
194
 
 
195
    RABBITMQ_NODE OK - No Partitions
 
196
 
 
197
=head1 ERRORS
 
198
 
 
199
The check tries to provide useful error messages on the status line for
 
200
standard error conditions.
 
201
 
 
202
Otherwise it returns the HTTP Error message returned by the management
 
203
interface.
 
204
 
 
205
=head1 EXIT STATUS
 
206
 
 
207
Returns zero if check is OK otherwise returns standard Nagios exit codes to
 
208
signify WARNING, UNKNOWN or CRITICAL state.
 
209
 
 
210
=head1 SEE ALSO
 
211
 
 
212
See Nagios::Plugin(3)
 
213
 
 
214
The RabbitMQ management plugin is described at
 
215
http://www.rabbitmq.com/management.html
 
216
 
 
217
=head1 LICENSE
 
218
 
 
219
This file is part of nagios-plugins-rabbitmq.
 
220
 
 
221
Copyright 2010, Platform 14.
 
222
 
 
223
Licensed under the Apache License, Version 2.0 (the "License");
 
224
you may not use this file except in compliance with the License.
 
225
You may obtain a copy of the License at
 
226
 
 
227
   http://www.apache.org/licenses/LICENSE-2.0
 
228
 
 
229
Unless required by applicable law or agreed to in writing, software
 
230
distributed under the License is distributed on an "AS IS" BASIS,
 
231
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
232
See the License for the specific language governing permissions and
 
233
limitations under the License.
 
234
 
 
235
=head1 AUTHOR
 
236
 
 
237
James Casey <jamesc.000@gmail.com>
 
238
 
 
239
=cut
 
240
 
 
241
1;