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

« back to all changes in this revision

Viewing changes to scripts/check_rabbitmq_aliveness

  • 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_aliveness.pl
 
4
 
 
5
# Use the management aliveness-check to send/receive a message through a vhost
 
6
 
 
7
# Originally by Nathan Vonnahme, n8v at users dot sourceforge
 
8
# dot net, July 19 2006
 
9
 
 
10
##############################################################################
 
11
# prologue
 
12
use strict;
 
13
use warnings;
 
14
 
 
15
use Nagios::Plugin ;
 
16
use LWP::UserAgent;
 
17
use URI::Escape;
 
18
use JSON;
 
19
 
 
20
use vars qw($VERSION $PROGNAME  $verbose $timeout);
 
21
$VERSION = '1.0';
 
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 Nagios::Plugin object (the 'usage' parameter is mandatory)
 
35
my $p = Nagios::Plugin->new(
 
36
    usage => "Usage: %s [options] -H hostname",
 
37
    license => "",
 
38
    version => $VERSION,
 
39
    blurb => 'This plugin uses the RabbitMQ management aliveness-check to send/receive a message through a vhost.',
 
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 => 55672
 
49
);
 
50
 
 
51
$p->add_arg(spec => 'username|user|u=s',
 
52
    help => "Username (default: %s)",
 
53
    default => "guest",
 
54
);
 
55
$p->add_arg(spec => 'password|p=s',
 
56
    help => "Password (default: %s)",
 
57
    default => "guest"
 
58
);
 
59
 
 
60
$p->add_arg(spec => 'vhost=s',
 
61
    help => "Specify the vhost to test (default: %s)",
 
62
    default => "/"
 
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
 
 
79
# perform sanity checking on command line options
 
80
 
 
81
 
 
82
##############################################################################
 
83
# check stuff.
 
84
 
 
85
my $hostname=$p->opts->hostname;
 
86
my $port=$p->opts->port;
 
87
my $vhost=uri_escape($p->opts->vhost);
 
88
 
 
89
my $url = sprintf("http%s://%s:%d/api/aliveness-test/%s", ($p->opts->ssl ? "s" : ""), $hostname, $port, $vhost);
 
90
 
 
91
my $ua = LWP::UserAgent->new(env_proxy => $p->opts->proxy);
 
92
$ua->agent($PROGNAME.' ');
 
93
$ua->timeout($p->opts->timeout);
 
94
# Different security domains in 2.5 and 2.6
 
95
$ua->credentials("$hostname:$port",
 
96
    "RabbitMQ Management", $p->opts->username, $p->opts->password);
 
97
$ua->credentials("$hostname:$port",
 
98
    "Management: Web UI", $p->opts->username, $p->opts->password);
 
99
my $req = HTTP::Request->new(GET => $url);
 
100
my $res = $ua->request($req);
 
101
 
 
102
if (!$res->is_success) {
 
103
    # Deal with standard error conditions - make the messages more sensible
 
104
    if ($res->code == 400) {
 
105
        my $bodyref = decode_json $res->content;
 
106
        $p->nagios_exit(CRITICAL, $bodyref->{'reason'});
 
107
    }
 
108
    $res->code == 404 and $p->nagios_die("Not found: ".$p->opts->vhost);
 
109
    $res->code == 401 and $p->nagios_die("Access refused: ".$p->opts->vhost);
 
110
    if ($res->code < 200 or $res->code > 400 ) {
 
111
        $p->nagios_exit(CRITICAL, "Received ".$res->status_line." for vhost: ".$p->opts->vhost);
 
112
    }
 
113
}
 
114
 
 
115
my $bodyref = decode_json $res->content;
 
116
$bodyref->{'status'} eq "ok" or $p->nagios_exit(CRITICAL, $res->content);
 
117
my($code, $message) = (OK, "vhost: ".$p->opts->vhost);
 
118
$p->nagios_exit(
 
119
    return_code => $code,
 
120
    message => $message
 
121
);
 
122
 
 
123
=head1 NAME
 
124
 
 
125
check_rabbitmq_aliveness - Nagios plugin using RabbitMQ management API to
 
126
check liveness by send/receive a message through a vhost
 
127
 
 
128
=head1 SYNOPSIS
 
129
 
 
130
check_rabbitmq_aliveness [options] -H hostname
 
131
 
 
132
=head1 DESCRIPTION
 
133
 
 
134
Use the management interface of RabbitMQ to check that the server is alive.
 
135
It declares a test queue, then publishes and consumes a message.
 
136
 
 
137
It uses Nagios::Plugin and accepts all standard Nagios options.
 
138
 
 
139
=head1 OPTIONS
 
140
 
 
141
=over
 
142
 
 
143
=item -h | --help
 
144
 
 
145
Display help text
 
146
 
 
147
=item -v | --verbose
 
148
 
 
149
Verbose output
 
150
 
 
151
=item -t | --timeout
 
152
 
 
153
Set a timeout for the check in seconds
 
154
 
 
155
=item -H | --hostname | --host
 
156
 
 
157
The host to connect to
 
158
 
 
159
=item --port
 
160
 
 
161
The port to connect to (default: 55672)
 
162
 
 
163
=item --ssl
 
164
 
 
165
Use SSL when connecting (default: false)
 
166
 
 
167
=item --vhost
 
168
 
 
169
The vhost to create the test queue within (default: /)
 
170
 
 
171
=item --username | --user
 
172
 
 
173
The user to connect as (default: guest)
 
174
 
 
175
=item --pass
 
176
 
 
177
The password for the user (default: guest)
 
178
 
 
179
=back
 
180
 
 
181
=head1 EXAMPLES
 
182
 
 
183
The defaults all work with a standard fresh install of RabbitMQ, and all that
 
184
is needed is to specify the host to connect to:
 
185
 
 
186
    check_rabbitmq_aliveness -H rabbit.example.com
 
187
 
 
188
This returns a standard Nagios result:
 
189
 
 
190
    RABBITMQ_ALIVENESS OK - vhost: /
 
191
 
 
192
You can choose a different vhost to use for the check also:
 
193
 
 
194
    check_rabbitmq_aliveness -H rabbit.example.com --vhost /foo
 
195
 
 
196
=head1 ERRORS
 
197
 
 
198
The check tries to provide useful error messages on the status line for
 
199
standard error conditions.
 
200
 
 
201
Otherwise it returns the HTTP Error message returned by the management
 
202
interface.
 
203
 
 
204
=head1 EXIT STATUS
 
205
 
 
206
Returns zero if check is OK otherwise returns standard Nagios exit codes to
 
207
signify WARNING, UNKNOWN or CRITICAL state.
 
208
 
 
209
=head1 SEE ALSO
 
210
 
 
211
See Nagios::Plugin(3)
 
212
 
 
213
The RabbitMQ management plugin is described at
 
214
http://www.rabbitmq.com/management.html
 
215
 
 
216
=head1 LICENSE
 
217
 
 
218
This file is part of nagios-plugins-rabbitmq.
 
219
 
 
220
Copyright 2010, Platform 14.
 
221
 
 
222
Licensed under the Apache License, Version 2.0 (the "License");
 
223
you may not use this file except in compliance with the License.
 
224
You may obtain a copy of the License at
 
225
 
 
226
   http://www.apache.org/licenses/LICENSE-2.0
 
227
 
 
228
Unless required by applicable law or agreed to in writing, software
 
229
distributed under the License is distributed on an "AS IS" BASIS,
 
230
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
231
See the License for the specific language governing permissions and
 
232
limitations under the License.
 
233
 
 
234
=head1 AUTHOR
 
235
 
 
236
James Casey <jamesc.000@gmail.com>
 
237
 
 
238
=cut
 
239
 
 
240
1;