-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_http_multiips
executable file
·231 lines (186 loc) · 7.28 KB
/
check_http_multiips
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.0;
use Carp;
use Getopt::Long;
use Data::Dumper;
use IO::Select;
use Pod::Usage;
use Net::DNS::Resolver;
use Readonly;
Readonly my $OK => 0;
Readonly my $WARNING => 1;
Readonly my $CRITICAL => 2;
Readonly my $UNKNOWN => 3;
my $debug = 0;
exit main();
sub main {
my $help = 0;
my $hostname;
my $type = "A";
my $check_http;
my $ssl;
my ($warning, $critical) = (1, 1);
my $options = GetOptions(
"hostname=s" => \$hostname,
"type=s" => \$type,
"check-http=s" => \$check_http,
"warning=i" => \$warning,
"critical=i" => \$critical,
"ssl" => \$ssl,
"debug" => \$debug,
"help" => \$help,
);
if ($help) {
pod2usage({ -verbose => 2 });
}
if (!defined $hostname or !defined $check_http) {
warn "You must specify -hostname and -check_http";
pod2usage({ -verbose => 2 });
}
if (!-x $check_http) {
warn "Option check_http must be executable";
pod2usage({ -verbose => 1 });
}
if ($debug) {
print STDERR "Parameters :\n";
print STDERR "hostname: $hostname\n";
print STDERR "type: $type\n";
print STDERR "warning/critical: $warning/$critical\n";
print STDERR "check-http: $check_http\n";
}
my $resolver = Net::DNS::Resolver->new;
my $res = $resolver->query($hostname, $type);
if (!defined $res) {
print "NG: Name not resolved\n";
return $CRITICAL;
}
my @answer = grep { $_->type eq $type } $res->answer;
if ($debug) {
#print Dumper [$resolver, $res, \@answer];
for my $k (@answer) {
print $k->rdatastr, $/;
}
}
# Hash
my %info = ();
for my $k (@answer) {
my $ipaddr = $k->rdatastr;
my $cmd = "$check_http";
my @args = ("-I", $ipaddr, @ARGV);
unshift @args, "-H $hostname";
unshift @args, "--ssl" if $ssl;
if ($debug) {
print STDERR "CMD: $cmd @args\n";
}
open my $fh, "-|", $cmd, @args or croak "Cannot fork process: $!";
$info{$k} = { pipe => $fh, ipaddr => $ipaddr, };
}
for my $k (keys %info) {
my $pipe = $info{$k}->{pipe};
my @output = ();
while (my $line = <$pipe>) {
push @output, $line;
}
$info{$k}->{output} = \@output;
close $pipe;
delete $info{$k}->{pipe};
}
if ($debug) {
print STDERR Dumper \%info;
}
map {
my $key = $_;
my $output = $info{$key}->{output};
my $output_firstline = $output->[0];
chomp $output_firstline;
my $output_firstline_copy = $output_firstline;
$output_firstline =~ s/HTTP\/\d+\.\d+\s(\d+)\s/$1/;
my $match = $output_firstline_copy =~ /HTTP OK:/;
#print $output_firstline, $/;
if (!$match) {
$info{$key}->{result} = $output_firstline_copy;
} else {
$info{$key}->{result} = "*** OK ***";
}
} (keys %info);
my %final_status = ( failure_count => 0, status => $OK, statusline => "*** OK ***", ipaddress => [], );
for my $key (keys %info) {
my $output = $info{$key};
my $result = $output->{result};
my $ipaddress = $info{$key}->{ipaddr};
if ($result ne "*** OK ***") {
push @{$final_status{ipaddress}}, "$ipaddress:$result";
if($final_status{failure_count} == 0) {
$final_status{statusline} = $result;
} else {
$final_status{statusline} .= $result;
}
$final_status{failure_count}++;
} else {
push @{$final_status{ipaddress}}, "$ipaddress:OK";
}
}
# Finish
if ($debug) {
print STDERR Dumper(\%final_status);
}
# Set status
if ($final_status{failure_count} >= $warning) {
$final_status{status} = $WARNING;
}
if ($final_status{failure_count} >= $critical) {
$final_status{status} = $CRITICAL;
}
# Pretty statusline
my $total = scalar(keys %info);
my $failures = $final_status{failure_count};
if ($final_status{status} == $OK) {
$final_status{statusline} = sprintf "HTTP OK: %d/%d", $total - $failures, $total;
} else {
$final_status{statusline} = sprintf "NOT OK: %d/%d", $total - $failures, $total;
}
print $final_status{statusline} . " " . join(", ", @{$final_status{ipaddress}}) . "\n";
return $final_status{status};
}
__END__
=head1 check_http_multiips
Check HTTP service with multiple IP addresses.
=head2 SYNOPSIS
check_http_multiips -hostname HOSTNAME -check-http CHECK_HTTP_PATH -warning WARN -critical CRIT -- -A USERAGENT
=head2 OPTINOS
=over 8
=item B<-hostname>
Specifies the target hostname (FQDN).
=item B<-check-http>
Specifies the path of the check_http (nagios standard plugin).
=item B<-warning>
Specifies the criteria of warning.
If equal to or more than WARN IP addresses are unavailable it considers WARNING.
=item B<-critical>
Specifies the criteria of critical.
If equal to or more than CRIT IP addresses are unavailable it considers CRITICAL.
=item B<-type>
Request type of DNS. Default is "A" and you need not specify the option in most cases.
=item B<-help>
Prints this message and exits.
=item B<-->
If you place C<--> inside the command-line options, options before it will be passed to this script, and options after it will be passed to C<check_http> command.
=back
=head2 EXAMPLE
If you want to check www.google.com's A records with P06C UserAgent:
check_http_multiips -hostname www.google.co.jp -check-http /usr/local/nagios/libexec/check_http -warning 2 -critical 4 -- -A 'DoCoMo/2.0 P06C(c500;TB;W24H16)'
=head3 EXAMPLE of perlbrew and nagios
Nagios does not seem to evaluate `source perlbrew-bashrc`.
If you set up Perl environment with perlbrew, you can use a command definition like the following:
define command{
command_name check_http_multi
command_line PATH=/home/nagios/perl5/perlbrew/bin:/home/nagios/perl5/perlbrew/perls/perl-5.15.9/bin /home/nagios/git/nagios-check-http-multi-ips/check_http_multiips -hostname $HOSTADDRESS$ -check-http $USER1$/check_http -warning 1 -critical 4 -- $ARG1$
}
=head1 COPYRIGHT
Copyright (c) 2012 limitusus <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=pod