#!/usr/bin/perl -w
########################################################################
#
# File:  check_wrt54g_ip.pl
#
# Author: Peter L. Berghold <salty.cowdawg@gmail.com>
#
# Description:  
#        This nagios plugin was written to detect when a WRT-54G router 
#        connected to a cable modem had its primary IP interface change 
#        address due to DHCP.  It would then notify the sysadmin accordingly
# 
#        This is meant to be run from Nagios and NOT NRPE. If the IP address
#        of the router changes, what good is NRPE? :-)
#
# Integration with Nagios:
#
#     
# define command {
#        command_name    check_my_router_ip
#        command_line    $USER1$/check_wrt54g_ip.pl --host 111.222.234.234 --realm myroutername --ipaddress 234.321.123.321 --password 's3cr3t'
#  }
#
#  define service {
#        use generic-service
#        host_name       my_router_inside_ip
#        service_description     Assigned DHCP Address
#        is_volatile     0
#        check_period    24x7
#        max_check_attempts      10
#        normal_check_interval   20
#        retry_check_interval    5
#        contact_groups          boss
#        notification_period     24x7
#        notification_interval   960
#        check_command           check_my_router_ip
#}
#  Substitute appropriately for your installation.
#
###############################################################################

use strict;
use warnings;
use LWP::UserAgent;
use Data::Dumper;
use HTML::TableContentParser;
use Getopt::Long;

my $host="";
my $realm="";
my $password="";
my $addr = "";
my $port=80;

my $result=GetOptions(
                      "host=s" => \$host,
                      "realm=s" => \$realm,
                      "password=s" => \$password,
                      "ipaddress=s" => \$addr,
                      "port=i" => \$port
                   );
my $ua=LWP::UserAgent -> new();

barf_and_complain()
    unless (
            $host and $realm and $password and $addr

            );
my $host_settings=sprintf("%s:%d",$host,$port);
my $url = sprintf("http://%s:%d/Status_Router.asp",$host,$port);

$ua -> credentials ($host_settings,$realm,"",$password);

my $response = $ua->get($url);

my $content = $response->content();


my $parser=HTML::TableContentParser->new();
my $tables = $parser->parse($content);

my $capture_ip = 0;
my $ip_addr="";
foreach my $table(@$tables){
    foreach my $row(@{$table->{rows}}){
        foreach my $cell(@{$row->{cells}}){
            next unless $cell->{data};
            if ( ! $capture_ip ) {
                $capture_ip++ if
                    $cell->{data} =~ m/\<script\>Capture\(share.ipaddr\)/;
                next;
            }
            if ( $capture_ip) {
                $cell->{data} =~ m@\<B\>(\d+\.\d+\.\d+\.\d+)\<\/B\>@;
                $ip_addr=$1;
                $capture_ip=0;
            }
        }
    }
}


if ( $ip_addr eq $addr ){
    printf "OK: My IP Address is: %s\n",$ip_addr;
    exit 0;
} else {
    printf "CRITICAL: My IP ADDESS HAS CHANGED to %s\n",$ip_addr;
    exit 2;
}

sub barf_and_complain {

printf "%s\n",qq(
You must specify all options for this plugin to work:
                 --host hostname or ip of router
                 --realm security realm of router
                 --ipaddress expected ip address
                 --password  login password for router

                 the --port option is just that, an option and defaults
                 to 80
                 );
exit(1);

}
