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

« back to all changes in this revision

Viewing changes to scripts/check_rabbitmq_overview

  • 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_overview
 
4
#
 
5
# Use the management APIs to count pending messages in broker
 
6
#
 
7
use strict;
 
8
use warnings;
 
9
 
 
10
use Nagios::Plugin qw(OK CRITICAL WARNING UNKNOWN);
 
11
use Nagios::Plugin::Functions qw(%STATUS_TEXT);
 
12
use LWP::UserAgent;
 
13
use URI::Escape;
 
14
use JSON;
 
15
 
 
16
use vars qw($VERSION $PROGNAME  $verbose $timeout);
 
17
$VERSION = '1.0';
 
18
 
 
19
# get the base name of this script for use in the examples
 
20
use File::Basename;
 
21
$PROGNAME = basename($0);
 
22
 
 
23
my $p = Nagios::Plugin->new(
 
24
    usage => "Usage: %s [options] -H hostname",
 
25
    license => "",
 
26
    version => $VERSION,
 
27
    blurb => 'This plugin uses the RabbitMQ management to count messages in server.',
 
28
);
 
29
 
 
30
$p->add_arg(spec => 'hostname|host|H=s',
 
31
    help => "Specify the host to connect to",
 
32
    required => 1
 
33
);
 
34
$p->add_arg(spec => 'port=i',
 
35
    help => "Specify the port to connect to (default: %s)",
 
36
    default => 55672
 
37
);
 
38
 
 
39
$p->add_arg(spec => 'username|user|u=s',
 
40
    help => "Username (default: %s)",
 
41
    default => "guest",
 
42
);
 
43
$p->add_arg(spec => 'password|p=s',
 
44
    help => "Password (default: %s)",
 
45
    default => "guest"
 
46
);
 
47
 
 
48
$p->add_arg(
 
49
    spec => 'warning|w=s',
 
50
    help =>
 
51
qq{-w, --warning=INTEGER,INTEGER,INTEGER
 
52
   Warning thresholds specified in order that the metrics are returned.
 
53
   Specify -1 if no warning threshold.},
 
54
 
 
55
);
 
56
 
 
57
$p->add_arg(
 
58
    spec => 'critical|c=s',
 
59
    help =>
 
60
qq{-c, --critical=INTEGER,INTEGER,INTEGER
 
61
   Critical thresholds specified in order that the metrics are returned.
 
62
   Specify -1 if no critical threshold.},
 
63
);
 
64
 
 
65
$p->add_arg(spec => 'ssl|ssl!',
 
66
    help => "Use SSL (default: false)",
 
67
    default => 0
 
68
);
 
69
 
 
70
$p->add_arg(spec => 'proxy|proxy!',
 
71
    help => "Use environment proxy (default: true)",
 
72
    default => 1
 
73
);
 
74
 
 
75
# Parse arguments and process standard ones (e.g. usage, help, version)
 
76
$p->getopts;
 
77
 
 
78
# perform sanity checking on command line options
 
79
my %warning;
 
80
if (defined $p->opts->warning) {
 
81
    my @warning = split(',', $p->opts->warning);
 
82
    $p->nagios_die("You should specify three ranges for --warning argument") unless $#warning == 2;
 
83
 
 
84
    $warning{'messages'} = shift @warning;
 
85
    $warning{'messages_ready'} = shift @warning;
 
86
    $warning{'messages_unacknowledged'} = shift @warning;
 
87
}
 
88
 
 
89
my %critical;
 
90
if (defined $p->opts->critical) {
 
91
    my @critical = split(',', $p->opts->critical);
 
92
    $p->nagios_die("You should specify three ranges for --critical argument") unless $#critical == 2;
 
93
 
 
94
    $critical{'messages'} = shift @critical;
 
95
    $critical{'messages_ready'} = shift @critical;
 
96
    $critical{'messages_unacknowledged'} = shift @critical;
 
97
}
 
98
 
 
99
 
 
100
##############################################################################
 
101
# check stuff.
 
102
 
 
103
my $hostname=$p->opts->hostname;
 
104
my $port=$p->opts->port;
 
105
 
 
106
my $ua = LWP::UserAgent->new(env_proxy => $p->opts->proxy);
 
107
$ua->agent($PROGNAME.' ');
 
108
$ua->timeout($p->opts->timeout);
 
109
# Different security domains in 2.5 and 2.6
 
110
$ua->credentials("$hostname:$port",
 
111
    "RabbitMQ Management", $p->opts->username, $p->opts->password);
 
112
$ua->credentials("$hostname:$port",
 
113
    "Management: Web UI", $p->opts->username, $p->opts->password);
 
114
 
 
115
 
 
116
my $url = sprintf("http%s://%s:%d/api/overview", ($p->opts->ssl ? "s" : ""), $hostname, $port);
 
117
my ($retcode, $result) = request($url);
 
118
if ($retcode != 200) {
 
119
    $p->nagios_exit(CRITICAL, "$result : /api/overview");
 
120
}
 
121
 
 
122
my @metrics = ( "messages", "messages_ready", "messages_unacknowledged");
 
123
for my $metric (@metrics) {
 
124
    my $warning = undef;
 
125
    $warning = $warning{$metric} if (defined $warning{$metric} and $warning{$metric} != -1);
 
126
    my $critical = undef;
 
127
    $critical = $critical{$metric} if (defined $critical{$metric} and $critical{$metric} != -1);
 
128
 
 
129
    my $value = $result->{'queue_totals'}->{$metric};
 
130
    my $code = $p->check_threshold(check => $value, warning => $warning, critical=> $critical);
 
131
    $p->add_message($code, sprintf("$metric ".$STATUS_TEXT{$code}." (%d)", $value)) ;
 
132
    $p->add_perfdata(label=>$metric, value => $value, warning=>$warning, critical=> $critical);
 
133
}
 
134
 
 
135
my ($code, $message) = $p->check_messages(join_all=>', ');
 
136
$p->nagios_exit(return_code => $code, message => $message);
 
137
 
 
138
 
 
139
sub request {
 
140
    my ($url) = @_;
 
141
    my $req = HTTP::Request->new(GET => $url);
 
142
    my $res = $ua->request($req);
 
143
 
 
144
    if (!$res->is_success) {
 
145
        # Deal with standard error conditions - make the messages more sensible
 
146
        if ($res->code == 400) {
 
147
            my $bodyref = decode_json $res->content;
 
148
            return (400, $bodyref->{'reason'});
 
149
 
 
150
        }
 
151
        $res->code == 404 and return (404, "Not Found");
 
152
        $res->code == 401 and return (401, "Access Refused");
 
153
        $res->status_line =~ /Can\'t connect/ and return (500, "Connection Refused : $url");
 
154
        if ($res->code < 200 or $res->code > 400 ) {
 
155
            return ($res->code, "Received ".$res->status_line);
 
156
        }
 
157
    }
 
158
    my $bodyref = decode_json $res->content;
 
159
    return($res->code, $bodyref);
 
160
}
 
161
 
 
162
=head1 NAME
 
163
 
 
164
check_rabbitmq_overview - Nagios plugin using RabbitMQ management API to
 
165
count the messages pending on the broker
 
166
 
 
167
=head1 SYNOPSIS
 
168
 
 
169
check_rabbitmq_overview [options] -H hostname
 
170
 
 
171
=head1 DESCRIPTION
 
172
 
 
173
Use the management interface of RabbitMQ to count the number of pending,
 
174
ready and unacknowledged messages.  These are published as performance
 
175
metrics for the check.
 
176
 
 
177
Critical and warning thresholds can be set for each of the metrics.
 
178
 
 
179
It uses Nagios::Plugin and accepts all standard Nagios options.
 
180
 
 
181
=head1 OPTIONS
 
182
 
 
183
=over
 
184
 
 
185
=item -h | --help
 
186
 
 
187
Display help text
 
188
 
 
189
=item -v | --verbose
 
190
 
 
191
Verbose output
 
192
 
 
193
=item -t | --timeout
 
194
 
 
195
Set a timeout for the check in seconds
 
196
 
 
197
=item -H | --hostname | --host
 
198
 
 
199
The host to connect to
 
200
 
 
201
=item --port
 
202
 
 
203
The port to connect to (default: 55672)
 
204
 
 
205
=item --ssl
 
206
 
 
207
Use SSL when connecting (default: false)
 
208
 
 
209
=item --username | --user
 
210
 
 
211
The user to connect as (default: guest)
 
212
 
 
213
=item --pass
 
214
 
 
215
The password for the user (default: guest)
 
216
 
 
217
=item -w | --warning
 
218
 
 
219
The warning levels for each count of messages, messages_ready and
 
220
messages_unacknowledged.  This field consists of three comma-separated
 
221
integers.  Specify -1 if no threshold for a particular count.
 
222
 
 
223
=item -c | --critical
 
224
 
 
225
The critical levels for each count of messages, messages_ready and
 
226
messages_unacknowledged.  This field consists of three comma-separated
 
227
integers.  Specify -1 if no threshold for a particular count
 
228
 
 
229
=back
 
230
 
 
231
=head1 EXAMPLES
 
232
 
 
233
The defaults all work with a standard fresh install of RabbitMQ, and all that
 
234
is needed is to specify the host to connect to:
 
235
 
 
236
    check_rabbitmq_overview -H rabbit.example.com
 
237
 
 
238
This returns a standard Nagios result:
 
239
 
 
240
    RABBITMQ_OVERVIEW OK - messages OK (25794) messages_ready OK (22971)
 
241
      messages_unacknowledged OK (2823) | messages=25794;;
 
242
      messages_ready=22971;; messages_unacknowledged=2823;;
 
243
 
 
244
=head1 ERRORS
 
245
 
 
246
The check tries to provide useful error messages on the status line for
 
247
standard error conditions.
 
248
 
 
249
Otherwise it returns the HTTP Error message returned by the management
 
250
interface.
 
251
 
 
252
=head1 EXIT STATUS
 
253
 
 
254
Returns zero if check is OK otherwise returns standard Nagios exit codes to
 
255
signify WARNING, UNKNOWN or CRITICAL state.
 
256
 
 
257
=head1 SEE ALSO
 
258
 
 
259
See Nagios::Plugin(3)
 
260
 
 
261
The RabbitMQ management plugin is described at
 
262
http://www.rabbitmq.com/management.html
 
263
 
 
264
=head1 LICENSE
 
265
 
 
266
This file is part of nagios-plugins-rabbitmq.
 
267
 
 
268
Copyright 2010, Platform 14.
 
269
 
 
270
Licensed under the Apache License, Version 2.0 (the "License");
 
271
you may not use this file except in compliance with the License.
 
272
You may obtain a copy of the License at
 
273
 
 
274
   http://www.apache.org/licenses/LICENSE-2.0
 
275
 
 
276
Unless required by applicable law or agreed to in writing, software
 
277
distributed under the License is distributed on an "AS IS" BASIS,
 
278
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
279
See the License for the specific language governing permissions and
 
280
limitations under the License.
 
281
 
 
282
=head1 AUTHOR
 
283
 
 
284
James Casey <jamesc.000@gmail.com>
 
285
 
 
286
=cut
 
287
 
 
288
1;