forked from kabalin/check_http_redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_http_redirect.pl
executable file
·138 lines (117 loc) · 4.33 KB
/
check_http_redirect.pl
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
#!/usr/bin/perl -w
#------------------------------------------------------------------------------
# Nagios check_http_redirect
# retrieve an http/s url and checks its header for a given redirects
# if the redirect exists and equal to the redirect you entered then exits with OK,
# otherwise exits with WARNING (if not equal) or CRITICAL ( if doesn't exist)
#
# Copyright 2009, Eugene L Kovalenja, http://www.purple.org.ua/
# Copyright 2012, Ruslan Kabalin, Lancaster University, UK
# Licensed under GPLv2
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Opsview; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# -----------------------------------------------------------------------------
use strict;
use Getopt::Std;
use LWP::UserAgent;
use HTTP::Request;
my $plugin_name = 'Nagios check_http_redirect';
my $VERSION = '1.02';
# getopt module config
$Getopt::Std::STANDARD_HELP_VERSION = 1;
# nagios exit codes
use constant EXIT_OK => 0;
use constant EXIT_WARNING => 1;
use constant EXIT_CRITICAL => 2;
use constant EXIT_UNKNOWN => 3;
# parse cmd opts
my %opts;
getopts('vU:R:t:H:', \%opts);
$opts{t} = 5 unless (defined $opts{t});
if (not (defined $opts{U} ) or not (defined $opts{R} )) {
print "ERROR: INVALID USAGE\n";
HELP_MESSAGE();
exit EXIT_CRITICAL;
}
my $status = EXIT_OK;
my $ua = LWP::UserAgent->new;
$ua->agent('check_http_redirect' . $VERSION);
$ua->protocols_allowed( [ 'http', 'https'] );
$ua->parse_head(0);
$ua->timeout($opts{t});
$ua->max_redirect(0);
my $request = new HTTP::Request('GET', $opts{U});
if (defined $opts{H})
{
$request->header('Host', $opts{H});
}
#DEBUG: print $request->as_string;
my $response = $ua->request($request);
#DEBUG: print $response->as_string;
if ($response->is_success or $response->is_error)
{
print "REDIRECT ERROR: Current HTTP response status line: ", $response->status_line, ". Check this page: $opts{U}\n";
$status = EXIT_CRITICAL;
}
else
{
if ($response->is_redirect)
{
if ( $response->header("Location") =~ $opts{R} || $response->header("Location") eq $opts{R} )
{
print "REDIRECT OK: ", $response->status_line, " ", $response->header("Location"), "\n";
$status = EXIT_OK;
}
else
{
print "REDIRECT WARNING: Location is invalid: ",$response->status_line, " ", $response->header("Location"), "\n";
$status = EXIT_WARNING;
}
}
else
{
print "REDIRECT ERROR: cannot retrieve the url: ", $response->status_line, "\n";
$status = EXIT_UNKNOWN;
}
}
exit $status;
sub HELP_MESSAGE
{
print <<EOHELP
Retrieve an http/s url and checks its header for a given redirects.
If the redirect exists and equal to the redirect you entered then exits with OK, otherwise exits with WARNING (if not equal) or CRITICAL ( if doesn't exist)
--help shows this message
--version shows version information
-U URL to retrieve (http or https)
-R URL that must be equal to Header Location Redirect URL
Supports exact string or regular expression.
-H Optional host attribute, useful if you are querying
virtual host. If using, URL to retrieve should contain the real host
name or IP of the webserver.
-t Timeout in seconds to wait for the URL to load. If the page fails to load,
$plugin_name will exit with UNKNOWN state (default 60)
EOHELP
;
}
sub VERSION_MESSAGE
{
print <<EOVM
$plugin_name v. $VERSION
Copyright 2009, Eugene L Kovalenja, http://www.purple.org.ua/ - Licensed under GPLv2
Copyright 2012, Ruslan Kabalin, Lancaster University, UK - Licensed under GPLv2
EOVM
;
}
# vi: tabstop=4 shiftwidth=4 expandtab