Debian Codegraph @

scripts/check_rabbitmq_server

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/env perl
2
3 ### check_rabbitmq_aliveness.pl
4
5 # Use the management overview to check server statistics
6
7 ##############################################################################
8 # prologue
9 use strict;
10 use warnings;
11
12 use Monitoring::Plugin qw(OK CRITICAL WARNING UNKNOWN);
13 use Monitoring::Plugin::Functions qw(%STATUS_TEXT);
14 use LWP::UserAgent;
15 use URI::Escape;
16 use JSON;
17
18 use Data::Dumper;
19
20 use vars qw($VERSION $PROGNAME $verbose $timeout $code $message);
21 $VERSION = '2.0.3';
22
23 # get the base name of this script for use in the examples
24 use File::Basename;
25 $PROGNAME = basename($0);
26
27
28 ##############################################################################
29 # define and get the command line options.
30 # see the command line option guidelines at
31 # http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS
32
33
34 # Instantiate Monitoring::Plugin object (the 'usage' parameter is mandatory)
35 my $p = Monitoring::Plugin->new(
36 usage => "Usage: %s [options] -H hostname",
37 license => "",
38 version => $VERSION,
39 blurb => 'This plugin uses the RabbitMQ management node API to check server process parameters.',
40 );
41
42 $p->add_arg(spec => 'hostname|host|H=s',
43 help => "Specify the host to connect to",
44 required => 1
45 );
46 $p->add_arg(spec => 'port=i',
47 help => "Specify the port to connect to (default: %s)",
48 default => 15672
49 );
50 $p->add_arg(spec => 'node|n=s',
51 help => "Specify the node name (default is hostname)"
52 );
53
54 $p->add_arg(
55 spec => 'warning|w=s',
56
57 help =>
58 qq{-w, --warning=INTEGER,INTEGER,INTEGER,INTEGER
59 Warning thresholds specified in order that the metrics are returned.
60 (Default : %s)},
61 # required => 1,
62 default => "80,80,80,80",
63 );
64
65 $p->add_arg(
66 spec => 'critical|c=s',
67 help =>
68 qq{-c, --critical=INTEGER,INTEGER,INTEGER,INTEGER
69 Warning thresholds specified in order that the metrics are returned.
70 (Default: %s) },
71 default => "90,90,90,90",
72 );
73
74 $p->add_arg(spec => 'username|user|u=s',
75 help => "Username (default: %s)",
76 default => "guest",
77 );
78 $p->add_arg(spec => 'password|p=s',
79 help => "Password (default: %s)",
80 default => "guest"
81 );
82
83 $p->add_arg(spec => 'ssl|ssl!',
84 help => "Use SSL (default: false)",
85 default => 0
86 );
87
88 $p->add_arg(spec => 'ssl_strict|ssl_strict!',
89 help => "Verify SSL certificate (default: true)",
90 default => 1
91 );
92
93 $p->add_arg(spec => 'proxy|proxy!',
94 help => "Use environment proxy (default: true)",
95 default => 1
96 );
97 $p->add_arg(spec => 'proxyurl=s',
98 help => "Use proxy url like http://proxy.domain.com:8080",
99 );
100
101 $p->add_arg(spec => 'rname|r=s',
102 help => "RabbitName (default: %s)",
103 default => "rabbit",
104 );
105
106 # Parse arguments and process standard ones (e.g. usage, help, version)
107 $p->getopts;
108
109
110 # Check we have four values for warning and critical thresholds
111 my @warning = split(',', $p->opts->warning);
112 $p->nagios_die("You should specify four ranges for --warning argument") unless $#warning == 3;
113
114 my @critical = split(',', $p->opts->critical);
115 $p->nagios_die("You should specify four ranges for --critical argument") unless $#critical == 3;
116
117 ##############################################################################
118 # check stuff.
119
120 my $hostname = $p->opts->hostname;
121
122 my $nodename = $p->opts->node;
123
124 my $rname = $p->opts->rname;
125
126 if (!$nodename) {
127 $hostname =~ /^([a-zA-Z0-9-]*)/;
128 $nodename = $1;
129 }
130
131 my $port = $p->opts->port;
132
133 my $path = "nodes/$rname\@$nodename";
134
135 my $url = sprintf("http%s://%s:%d/api/%s", ($p->opts->ssl ? "s" : ""), $hostname, $port, $path);
136
137 my $ua = LWP::UserAgent->new;
138 if (defined $p->opts->proxyurl)
139 {
140 $ua->proxy('http', $p->opts->proxyurl);
141 }
142 elsif($p->opts->proxy == 1 )
143 {
144 $ua->env_proxy;
145 }
146 $ua->agent($PROGNAME.' ');
147 $ua->timeout($p->opts->timeout);
148 if ($p->opts->ssl and $ua->can('ssl_opts')) {
149 $ua->ssl_opts(verify_hostname => $p->opts->ssl_strict);
150 }
151 my $req = HTTP::Request->new(GET => $url);
152 $req->authorization_basic($p->opts->username, $p->opts->password);
153 my $res = $ua->request($req);
154
155 if (!$res->is_success) {
156 # Deal with standard error conditions - make the messages more sensible
157 if ($res->code == 400) {
158 my $bodyref = decode_json $res->content;
159 $p->nagios_exit(CRITICAL, $bodyref->{'reason'});
160 }
161 $res->code == 404 and $p->nagios_die("Not found: ".$path);
162 $res->code == 401 and $p->nagios_die("Access refused: ".$path);
163 if ($res->code < 200 or $res->code > 400 ) {
164 $p->nagios_exit(CRITICAL, "Received ".$res->status_line." for path: ".$path);
165 }
166 }
167
168 my $bodyref = decode_json $res->content;
169
170 if (!$bodyref->{'running'}) {
171 $p->nagios_exit(CRITICAL, "Not running: ".$path);
172 }
173
174 check($p, "Memory", $bodyref->{'mem_used'}, $bodyref->{'mem_limit'}, $warning[0], $critical[0]);
175 check($p, "Process", $bodyref->{'proc_used'}, $bodyref->{'proc_total'}, $warning[1], $critical[1]);
176 check($p, "FD", $bodyref->{'fd_used'}, $bodyref->{'fd_total'}, $warning[2], $critical[2]);
177 check($p, "Sockets", $bodyref->{'sockets_used'}, $bodyref->{'sockets_total'}, $warning[3], $critical[3]);
178
179 ($code, $message) = $p->check_messages(join_all=>', ');
180 $p->nagios_exit( return_code=>$code, message=>$message);
181
182
183 sub check {
184 my $p = shift;
185 my $label = shift;
186 my $used = shift;
187 my $limit = shift;
188 my $warning = shift;
189 my $critical = shift;
190
•191 if (!$p || !defined($used)) {
192 $p->nagios_exit( return_code=>CRITICAL, message=>'Unable to get values for '.$label);
193 } else {
194 my $value = percent($used, $limit);
195 my $code = $p->check_threshold(check => $value, warning => $warning, critical => $critical);
196 $p->add_message($code, sprintf("$label ".$STATUS_TEXT{$code}." (%.2f%%)", $value)) ;
197 $p->add_perfdata(label=>$label, value => $value, uom=>"%", warning=>$warning, critical=>$critical);
198 }
199 }
200
201 sub percent {
202 my $num = shift;
203 my $denom = shift;
204 $num = 0 if $num eq "unknown";
205 my $value = ($num/ $denom)*100;
206 return sprintf("%.2f", $value)
207 }
208
209 sub structured {
210 my $content = shift;
211 $Data::Dumper::Terse = 1; # don't output names where feasible
212 $Data::Dumper::Indent = 2;
213 return Dumper($content);
214 }
215
216 =head1 NAME
217
218 check_rabbitmq_server - Nagios plugin using RabbitMQ management API to
219 check the server resource usage (processes, memory, file descriptors, and sock descriptors)
220
221 =head1 SYNOPSIS
222
223 check_rabbitmq_server [options] -H hostname
224
225 =head1 DESCRIPTION
226
227 Use the management interface of RabbitMQ to check the resource usage of the
228 server. This check looks at the node statistics for the rabbit node on
229 the host and examines the erlang memory, process and file descriptor usage.
230
231 It provides performance data for each of these variables and allows for
232 warning and criticality levels to be specified for each.
233
234 It uses Monitoring::Plugin and accepts all standard Nagios options.
235
236 =head1 OPTIONS
237
238 =over
239
240 =item -h | --help
241
242 Display help text
243
244 =item -v | --verbose
245
246 Verbose output
247
248 =item -t | --timeout
249
250 Set a timeout for the check in seconds
251
252 =item -H | --hostname | --host
253
254 The host to connect to
255
256 =item --port
257
258 The port to connect to (default: 15672)
259
260 =item --ssl
261
262 Use SSL when connecting (default: false)
263
264 =item -w | --warning
265
266 The warning levels, expressed as a percentage for each of memory, process,
267 file descriptor and sockets usage. This field consists of four comma-separated
268 integers. (default: 80,80,80,80)
269
270 =item -c | --critical
271
272 The critical levels, expressed as a percentage for each of memory, process,
273 file descriptor and sockets usage. This field consists of four comma-separated
274 integers. (default: 90,90,90,90)
275
276 =item -n | --node
277
278 The node name (default is hostname)
279
280 =item -r | --rname
281
282 The rabbit name (default is rabbit)
283
284 =item --username | --user
285
286 The user to connect as (default: guest)
287
288 =item -p | --password
289
290 The password for the user (default: guest)
291
292 =back
293
294 =head1 EXAMPLES
295
296 The defaults all work with a standard fresh install of RabbitMQ, and all that
297 is needed is to specify the host to connect to:
298
299 check_rabbitmq_server -H rabbit.example.com
300
301 This returns a standard Nagios result:
302
303 RABBITMQ_SERVER OK - Memory OK (6.19%) Process OK (0.01%)
304 FD OK (2.53%) Sockets OK (0.86%) | Memory=6.19%;80;90 Process=0.01%;80;90 FD=2.53%;80;90 Sockets=0.86%;1;2
305
306 You can specify different warning and criticality levels. B<-w> and B<-c>
307 both accept four comma-separated percentages which represent the
308 thresholds for memory, process and file descriptor usage respectively. For
309 example, to specify warnings for memory at 60%, process at 80%, file
310 descriptors at 95%, and socket descriptors at 90%:
311
312 check_rabbitmq_server -H rabbit.example.com -w 60,80,95,90
313
314 =head1 ERRORS
315
316 The check tries to provide useful error messages on the status line for
317 standard error conditions.
318
319 Otherwise it returns the HTTP Error message returned by the management
320 interface.
321
322 =head1 EXIT STATUS
323
324 Returns zero if check is OK otherwise returns standard Nagios exit codes to
325 signify WARNING, UNKNOWN or CRITICAL state.
326
327 =head1 SEE ALSO
328
329 See Monitoring::Plugin(3)
330
331 The RabbitMQ management plugin is described at
332 http://www.rabbitmq.com/management.html
333
334 =head1 LICENSE
335
336 This file is part of nagios-plugins-rabbitmq.
337
338 Copyright 2010, Platform 14.
339
340 Licensed under the Apache License, Version 2.0 (the "License");
341 you may not use this file except in compliance with the License.
342 You may obtain a copy of the License at
343
344 http://www.apache.org/licenses/LICENSE-2.0
345
346 Unless required by applicable law or agreed to in writing, software
347 distributed under the License is distributed on an "AS IS" BASIS,
348 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
349 See the License for the specific language governing permissions and
350 limitations under the License.
351
352 =head1 AUTHOR
353
354 James Casey <[email protected]>
355
356 =cut
357
358 1;