Debian Codegraph @

scripts/check_rabbitmq_shovels

raw
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4 use JSON -support_by_pp;
5 use LWP::UserAgent;
6 use Monitoring::Plugin;
7
8
9 $::PROGRAM = "check_rabbitmq_shovels";
10 $::VERSION = "1.0";
11
12
13 #
14 # main
15 #
16 MAIN: {
17 run() unless caller();
18 }
19
20
21 #
22 # run()
23 # ---
24 sub run {
25 my $np = Monitoring::Plugin->new(
26 plugin => $::PROGRAM,
27 version => $::VERSION,
28 blurb => "check that all the shovels of the given RabbitMQ host "
29 . "are running",
30 usage => "Usage: %s [-H host]",
31 timeout => 5,
32 );
33
34 # declare options
35 $np->add_arg(
36 spec => "hostname|host|H=s",
37 help => "Specify the RabbitMQ host to query. Defaults to '%s'.",
38 default => "localhost",
39 );
40 $np->add_arg(
41 spec => "port|P=s",
42 help => "Specify the port number to query RabbitMQ on. Defaults to %s.",
43 default => 15672,
44 );
45 $np->add_arg(
46 spec => "username|user|u=s",
47 help => "Specify the username. Defaults to '%s'.",
48 default => "guest",
49 );
50 $np->add_arg(
51 spec => "password|pass|p=s",
52 help => "Specify the password. Defaults to '%s'.",
53 default => "guest",
54 );
55
56 # parse options
57 $np->getopts;
58
59 # --------------------------------------
60
61 # create and configure the JSON parser
62 my $json = JSON->new;
63 $json->allow_nonref->utf8->relaxed->escape_slash->loose;
64 $json->allow_singlequote->allow_barekey;
65
66 # construct the URL authority
67 my $url_authority = $np->opts->username . ":" . $np->opts->password
68 . '@' . $np->opts->hostname . ":" . $np->opts->port;
69
70 # fetch the list of shovels
71 alarm $np->opts->timeout;
72 my $agent = LWP::UserAgent->new;
73 my $response = $agent->get("http://$url_authority/api/shovels");
74
75 $np->nagios_exit(
76 return_code => CRITICAL,
77 message => "API error: ".$response->status_line
78 ) unless $response->is_success;
79
80 # decode the JSON
81 my $shovels = eval { $json->decode($response->decoded_content || "[]") };
82
83 $np->nagios_exit(
84 return_code => CRITICAL,
85 message => "API error: could not parse JSON: $@"
86 ) if $@;
87
88 $np->nagios_exit(
89 return_code => CRITICAL,
90 message => "API error: empty JSON"
91 ) unless @$shovels;
92
93 # process the shovels
94 my (@running, @blocked, @terminated);
95
96 for my $shovel (@$shovels) {
97 push @running, $shovel->{name} and next
98 if $shovel->{state} eq "running";
99 push @blocked, $shovel->{name} and next
100 if $shovel->{state} eq "blocked";
101 push @terminated, $shovel->{name} and next
102 if $shovel->{state} eq "terminated";
103 }
104
105 # construct the status message
106 my ($status, @message);
107
108 if (@running) {
109 my $s = (my $n = @running) > 1 ? "s" : "";
110 unshift @message, "$n running shovel$s: @running";
111 $status = OK;
112 }
113 if (@blocked) {
114 my $s = (my $n = @blocked) > 1 ? "s" : "";
115 unshift @message, "$n blocked shovel$s: @blocked";
116 $status = WARNING;
117 }
118 if (@terminated) {
119 my $s = (my $n = @terminated) > 1 ? "s" : "";
120 unshift @message, "$n terminated shovel$s: @terminated";
121 $status = CRITICAL;
122 }
123
124 # return the appropriate status code and message
125 $np->nagios_exit(
126 return_code => $status,
127 message => join ", ", @message,
128 );
129 }
130
131
132 1
133
134 __END__
135
136 =pod
137
138 =head1 NAME
139
140 check_rabbitmq_shovels - Nagios plugin to check that all shovels of the
141 given RabbitMQ host are running
142
143
144 =head1 SYNOPSIS
145
146 check_rabbitmq_shovels --hostname <host>
147 check_rabbitmq_shovels { --help | --man | --version }
148
149
150 =head1 OPTIONS
151
152 =head2 Program options
153
154 =over
155
156 =item B<-H>, B<--hostname> I<string>
157
158 Specify the RabbitMQ host to query. Defaults to C<localhost>.
159
160 =item B<-P>, B<--port> I<number>
161
162 Specify the port number to query RabbitMQ on. Defaults to 15672.
163
164 =item B<-u>, B<--user>, B<--username> I<string>
165
166 Specify the username. Defaults to C<guest>.
167
168 =item B<-p>, B<--pass>, B<--password> I<string>
169
170 Specify the password. Defaults to C<guest>.
171
172 =item B<-t>, B<--timeout> I<duration>
173
174 Specify the timeout period in seconds. Defaults to 5.
175
176 =back
177
178 =head2 Help options
179
180 =over
181
182 =item B<-?>, B<--usage>
183
184 Print a short usage description, then exit.
185
186 =item B<-h>, B<--help>
187
188 Print a more detailed help screen, then exit.
189
190 =item B<-V>, B<--version>
191
192 Print the program name and version, then exit.
193
194 =back
195
196
197 =head1 DESCRIPTION
198
199 This program is a Nagios plugin that uses the JSON over HTTP API
200 to check that all shovels of the given RabbitMQ host are running.
201 The default settings match the standard installation of RabbitMQ.
202
203 The check returns C<OK> if all shovels are running, C<WARNING> if
204 one or more shovels are blocked, C<CRITICAL> if one or more shovels
205 are terminated.
206
207
208 =head1 EXAMPLES
209
210 With default settings, only the hostname need to be specified:
211
212 check_rabbitmq_shovels -H <host>
213
214 It prints the global status and the details of the shovels, by
215 decreasing order or criticity:
216
217 RABBITMQ_SHOVELS OK - 3 running shovels: klonk zlott swish
218
219 RABBITMQ_SHOVELS WARNING - 1 blocked shovel: klonk, 2 running shovels: zlott swish
220
221 RABBITMQ_SHOVELS CRITICAL - 1 terminated shovel: zlott, 1 blocked shovel: klonk, 1 running shovel: swish
222
223 In the case of an API error, the program will give an appropriate
224 diagnostic:
225
226 RABBITMQ_SHOVELS CRITICAL - API error: 401 Unauthorized
227
228 RABBITMQ_SHOVELS CRITICAL - API error: 500 Can't connect to <host>:<port>
229
230 RABBITMQ_SHOVELS CRITICAL - API error: could not parse JSON: malformed JSON string
231
232 RABBITMQ_SHOVELS CRITICAL - API error: empty JSON
233
234
235 =head1 SEE ALSO
236
237 See L<Monitoring::Plugin>
238
239 The RabbitMQ management plugin is described at
240 L<http://www.rabbitmq.com/management.html>
241
242
243 =head1 LICENSE
244
245 Licensed under the Apache License, Version 2.0 (the "License");
246 you may not use this file except in compliance with the License.
247 You may obtain a copy of the License at
248
249 http://www.apache.org/licenses/LICENSE-2.0
250
251 Unless required by applicable law or agreed to in writing, software
252 distributed under the License is distributed on an "AS IS" BASIS,
253 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
254 See the License for the specific language governing permissions and
255 limitations under the License.
256
257
258 =head1 AUTHOR
259
260 Sebastien Aperghis-Tramoni ([email protected])
261