/
check_wan_ip_ddwrt
executable file
·186 lines (132 loc) · 4.83 KB
/
check_wan_ip_ddwrt
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
#!/usr/bin/env perl
use strict;
use warnings;
# load the libwww module
use LWP::Simple;
# load the Getopt::Long module for cli arguments
use Getopt::Long;
# load the Pod::Usage module for automatic documentation
use Pod::Usage;
#-------------------------------------------------------------------------------
# GLOBAL VARIABLES
#-------------------------------------------------------------------------------
my $hostname = undef; # this has to be a cli argument
my $response = undef;
my $help = undef;
my $wan_ip = undef;
my $expected_ip = undef;
#-------------------------------------------------------------------------------
# process cli arguments
#-------------------------------------------------------------------------------
Getopt::Long::Configure( "no_ignore_case", "bundling" );
GetOptions(
'H|hostname=s' => \$hostname,
'h|help|?' => \$help,
'i|ipaddress=s' => \$expected_ip,
);
# if help chosen, display it
pod2usage( -verbose => 2, -noperldoc => 1, ) if $help;
# if no -H specified, show usage
pod2usage() unless $hostname and $expected_ip;
# fetch the site
my $html = get "http://$hostname";
# if fetch is successful, process html
if ( defined $html ) {
_get_wan_ip();
if ( $wan_ip eq $expected_ip ) {
print "OK: our WAN ip is $wan_ip and we expected $expected_ip\n";
exit 0;
}
else {
print
"WARNING: our WAN ip has changed. We expected $expected_ip but got $wan_ip\n";
exit 1;
}
}
else {
print "WARNING: Something went wrong fetching it.\n";
exit 1;
}
#=== FUNCTION ================================================================
# NAME: _get_wan_ip
# PURPOSE: get WAN ip of dd-wrt router
# PARAMETERS: none
# RETURNS: wan ip address
# DESCRIPTION:
# THROWS:
# COMMENTS: the wan ip is in this html code:
# <span id="wan_ipaddr">ipv4 address</span>
# so we we need the span tag with an wan_ipaddr id attribute
# SEE ALSO: n/a
#===============================================================================
sub _get_wan_ip {
# parse the html $response file
use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new_from_content($html);
if ( my $div = $tree->look_down( _tag => "span", id => "wan_ipaddr" ) ) {
$wan_ip = $div->as_text();
return $wan_ip;
}
# if we do not find the wan_ipaddr attr end with unknown
else {
print "UNKNOWN: something went wrong while parsing the html code\n";
exit 3;
}
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Application Documentation
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
=head1 NAME
check_wan_ip_ddwrt - check WAN ip address of dd-wrt broadband routers
=head1 VERSION
This documentation refers to <check_wan_ip_ddwrt> version 0.1
=head1 USAGE
check_wan_ip_ddwrt -H host -i ip.add.re.ss
=head1 REQUIRED ARGUMENTS
-H|--hostname
host name or LAN ip address of our dd-wrt router;
-i|--ipaddress
expected ip address we want to check.
=head1 OPTIONS
-H|--hostname
host name or LAN ip address of our dd-wrt router;
-i|--ipaddress
expected ip address we want to check.
-h|--help
this text;
=head1 DESCRIPTION
Nagios check to monitor if the WAN ip address our dd-wrt router gets from our
internet provider is the one we expect it to be. I need to know this to modify
a DNS A record accordingly because I do not have a fixed ip address.
The check retrieves the info web site from the router and parses it to
retrieve its WAN ip.
=head1 DIAGNOSTICS
This check can fail because of network problems (no connectivity to the
router) or because the router does not have a WAN ip address (internet
provider problem).
=head1 CONFIGURATION AND ENVIRONMENT
Place the plugin in the plugin directory of the nagios server (usually,
/usr/local/nagios/libexec/), make the nagios user its owner and change the
permissions to 775.
=head1 DEPENDENCIES
This plugin requires the LWP::Simple and HTML::TreeBuilder modules.
On a recent Redhat based system you can install it like this:
# yum install perl-HTML-Tree
On a debian based system the package is called libhtml-tree-perl and you
use apt-get so:
# apt-get install libhtml-tree-perl
On other flavours of unix, check your package manager documentation or use
something like perlbrew with cpanminus.
=head1 INCOMPATIBILITIES
None known yet.
=head1 BUGS AND LIMITATIONS
Plenty, patches welcome :-)
=head1 AUTHOR
<Natxo Asenjo> (<natxo@asenjo.nl_removethis>)
=head1 LICENCE AND COPYRIGHT
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See perldoc perlartistic.
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.
=cut