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

« back to all changes in this revision

Viewing changes to scripts/check_rabbitmq_watermark

  • 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_watermark.pl
 
4
 
 
5
# Use the management API to check if mem_alarm or disk_free_alarm has been triggered. 
 
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 API to check if the mem_alarm or disk_free_alarm has been triggered',
 
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
$p->add_arg(spec => 'node|n=s',
 
51
    help => "Specify the node name (default is hostname)"
 
52
);
 
53
$p->add_arg(spec => 'username|user|u=s',
 
54
    help => "Username (default: %s)",
 
55
    default => "guest",
 
56
);
 
57
$p->add_arg(spec => 'password|p=s',
 
58
    help => "Password (default: %s)",
 
59
    default => "guest"
 
60
);
 
61
 
 
62
$p->add_arg(spec => 'vhost=s',
 
63
    help => "Specify the vhost to test (default: %s)",
 
64
    default => "/"
 
65
);
 
66
 
 
67
$p->add_arg(spec => 'ssl|ssl!',
 
68
    help => "Use SSL (default: false)",
 
69
    default => 0
 
70
);
 
71
 
 
72
$p->add_arg(spec => 'proxy|proxy!',
 
73
    help => "Use environment proxy (default: true)",
 
74
    default => 1
 
75
);
 
76
 
 
77
# Parse arguments and process standard ones (e.g. usage, help, version)
 
78
$p->getopts;
 
79
 
 
80
 
 
81
# perform sanity checking on command line options
 
82
 
 
83
 
 
84
##############################################################################
 
85
# check stuff.
 
86
 
 
87
my $hostname=$p->opts->hostname;
 
88
my $port=$p->opts->port;
 
89
my $vhost=uri_escape($p->opts->vhost);
 
90
 
 
91
my $nodename = $p->opts->node;
 
92
 
 
93
if (!$nodename) {
 
94
    $hostname =~ /^([a-zA-Z0-9-]*)/;
 
95
    $nodename = $1;
 
96
}
 
97
 
 
98
my $url = sprintf("http%s://%s:%d/api/nodes/rabbit\@%s", ($p->opts->ssl ? "s" : ""), $hostname, $port, $nodename);
 
99
 
 
100
my $ua = LWP::UserAgent->new(env_proxy => $p->opts->proxy);
 
101
$ua->agent($PROGNAME.' ');
 
102
$ua->timeout($p->opts->timeout);
 
103
# Different security domains in 2.5 and 2.6
 
104
$ua->credentials("$hostname:$port",
 
105
    "RabbitMQ Management", $p->opts->username, $p->opts->password);
 
106
$ua->credentials("$hostname:$port",
 
107
    "Management: Web UI", $p->opts->username, $p->opts->password);
 
108
my $req = HTTP::Request->new(GET => $url);
 
109
my $res = $ua->request($req);
 
110
 
 
111
if (!$res->is_success) {
 
112
    # Deal with standard error conditions - make the messages more sensible
 
113
    if ($res->code == 400) {
 
114
        my $bodyref = decode_json $res->content;
 
115
        $p->nagios_exit(CRITICAL, $bodyref->{'reason'});
 
116
    }
 
117
    $res->code == 404 and $p->nagios_die("Not found");
 
118
    $res->code == 401 and $p->nagios_die("Access refused");
 
119
    if ($res->code < 200 or $res->code > 400 ) {
 
120
        $p->nagios_exit(CRITICAL, "Received ".$res->status_line);
 
121
    }
 
122
}
 
123
 
 
124
my $bodyref = decode_json $res->content;
 
125
my @checks = ("mem_alarm", "disk_free_alarm");
 
126
 
 
127
for my $check (@checks) {
 
128
    if ($bodyref->{$check} eq 0) {
 
129
        $p->add_message(OK, "$check");
 
130
    } else {
 
131
        $p->add_message(CRITICAL, "$check");
 
132
    }
 
133
}
 
134
 
 
135
my($code, $message) = $p->check_messages(join_all=>', ');
 
136
$p->nagios_exit(
 
137
    return_code => $code,
 
138
    message => $message
 
139
);
 
140
 
 
141
=head1 NAME
 
142
 
 
143
check_rabbitmq_watermark - Nagios plugin using RabbitMQ management API to check if the mem_alarm or disk_free_alarm has been triggered 
 
144
 
 
145
=head1 SYNOPSIS
 
146
 
 
147
check_rabbitmq_watermark [options] -H hostname
 
148
 
 
149
=head1 DESCRIPTION
 
150
 
 
151
Use the management interface of RabbitMQ to check if the mem_alarm or disk_free_alarm has been triggered.
 
152
 
 
153
It uses Nagios::Plugin and accepts all standard Nagios options.
 
154
 
 
155
=head1 OPTIONS
 
156
 
 
157
=over
 
158
 
 
159
=item -h | --help
 
160
 
 
161
Display help text
 
162
 
 
163
=item -v | --verbose
 
164
 
 
165
Verbose output
 
166
 
 
167
=item -t | --timeout
 
168
 
 
169
Set a timeout for the check in seconds
 
170
 
 
171
=item -H | --hostname | --host
 
172
 
 
173
The host to connect to
 
174
 
 
175
=item --port
 
176
 
 
177
The port to connect to (default: 55672)
 
178
 
 
179
=item --ssl
 
180
 
 
181
Use SSL when connecting (default: false)
 
182
 
 
183
=item --vhost
 
184
 
 
185
The vhost to create the test queue within (default: /)
 
186
 
 
187
=item -n | --node
 
188
 
 
189
The node name (default is hostname)
 
190
 
 
191
=item --username | --user
 
192
 
 
193
The user to connect as (default: guest)
 
194
 
 
195
=item --pass
 
196
 
 
197
The password for the user (default: guest)
 
198
 
 
199
=back
 
200
 
 
201
=head1 EXAMPLES
 
202
 
 
203
The defaults all work with a standard fresh install of RabbitMQ, and all that
 
204
is needed is to specify the host to connect to:
 
205
 
 
206
    check_rabbitmq_watermark -H rabbit.example.com
 
207
 
 
208
This returns a standard Nagios result:
 
209
 
 
210
    RABBITMQ_ALIVENESS OK - vhost: /
 
211
 
 
212
You can choose a different vhost to use for the check also:
 
213
 
 
214
    check_rabbitmq_watermark -H rabbit.example.com --vhost /foo
 
215
 
 
216
=head1 ERRORS
 
217
 
 
218
The check tries to provide useful error messages on the status line for
 
219
standard error conditions.
 
220
 
 
221
Otherwise it returns the HTTP Error message returned by the management
 
222
interface.
 
223
 
 
224
=head1 EXIT STATUS
 
225
 
 
226
Returns zero if check is OK otherwise returns standard Nagios exit codes to
 
227
signify WARNING, UNKNOWN or CRITICAL state.
 
228
 
 
229
=head1 SEE ALSO
 
230
 
 
231
See Nagios::Plugin(3)
 
232
 
 
233
The RabbitMQ management plugin is described at
 
234
http://www.rabbitmq.com/management.html
 
235
 
 
236
=head1 LICENSE
 
237
 
 
238
This file is part of nagios-plugins-rabbitmq.
 
239
 
 
240
Copyright 2010, Platform 14.
 
241
 
 
242
Licensed under the Apache License, Version 2.0 (the "License");
 
243
you may not use this file except in compliance with the License.
 
244
You may obtain a copy of the License at
 
245
 
 
246
   http://www.apache.org/licenses/LICENSE-2.0
 
247
 
 
248
Unless required by applicable law or agreed to in writing, software
 
249
distributed under the License is distributed on an "AS IS" BASIS,
 
250
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
251
See the License for the specific language governing permissions and
 
252
limitations under the License.
 
253
 
 
254
=head1 AUTHOR
 
255
 
 
256
James Casey <jamesc.000@gmail.com>
 
257
 
 
258
=cut
 
259
 
 
260
1;