Debian Codegraph @

scripts/check_rabbitmq_cluster

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