#!/usr/bin/perl 

use warnings;
use strict;

use Getopt::Long;
use Pod::Usage;

use Netgear::WGT624;

# print_element - starts the router query object and passes results to user.
sub print_element($$$$) {
    my $username = shift;
    my $password = shift;
    my $address  = shift;
    my $element = shift;

    my $rtr = Netgear::WGT624->new();

    $rtr->username($username);
    $rtr->password($password);
    $rtr->address($address);
    
    my $retries = 2;     # The number of times to try querying the router.
    my $retval = undef;

    # The first try seems to not return correct value in some cases,
    # at least in early versions of this program with the command-line
    # lwp client.  This retry code may be unecessary now, but I'm
    # uneasy to get rid of it without thorough testing.
    do {
	$retval = $rtr->getStatistic($element);
	$retries--;
    } while (!defined($retval) && $retries > 0);

    if (defined($retval)) {
	print $retval;
    }
}

sub driver() {

    # Values that we need from user.
    my $username = undef;
    my $password = undef;
    my $address  = undef;
    my $element  = undef;
    
    # Monitors if the user requests a man or help
    # page on command line.
    my $man = 0;
    my $help = 0;

    my $result = GetOptions ("username=s" => \$username,
			     "password=s" => \$password,
			     "address=s"  => \$address,
			     "element=s"  => \$element,
			     "help|?"     => \$help,
			     "man"        => \$man) or pod2usage(2);

    pod2usage(1) if $help;
    pod2usage(-exitstatus => 0, -verbose => 2) if $man;

    # Fail if these elements are not specified on command
    # line -- print usage message instead.
    if (!defined($username) ||
	!defined($password) ||
	!defined($address)  ||
	!defined($element)) {
	
	# Print the SYNOPSIS section of perldoc if sufficient
	# options are not specified.
	pod2usage(2);
    } else {
	# We have necessary parameters passed to us,
	# start querying the router and print results.
	print_element($username, $password, $address, $element);
    }
}

driver();

=head1 NAME

get-wgt624-statistics - Gets a statistic from a Netgear WGT624 router
by parsing the HTML statistics page.

=head1 SYNOPSIS

The following will print out a statistic from the router:

C<get-wgt624-statistics --username username --password password \
    --address router_address --element element>

Use the following to print this usage summary:

C<get-wgt624-statistics --help>

Use the following to print the full program documentation:

C<get-wgt624-statistics --man>

=head1 DESCRIPTION

This program grew out of my frustration with the lack of SNMP support
on the Netgear WGT624 router.  Even though it obviously a cheaper product
than other Netgear devices, there should be some way of quickly getting 
statistics out of it, for diagnostic purposes, and for using something like
MRTG or Cacti with the device.

Unfortunately, all that I was able to find was a single HTML page that
listed statistics for the various devices on the router, including the
WAN, WLAN and LAN interfaces.  This script parses the statistics HTML
page from the Netgear WGT624 router and prints out any of the values
on this HTML page.  The output can then be used in a
program like Cacti for graph generation.

=head1 LISTABLE ELEMENTS FROM WGT624

The following may be listed in the element field for output to the console:

=over 4

=item *

WAN_Status

=item *

WAN_TxPkts

=item *

WAN_RxPkts

=item *

WAN_Collisions

=item *

WAN_TxRate

=item *

WAN_RxRate

=item *

WAN_UpTime

=item *

LAN_Status

=item *

LAN_TxPkts

=item *

LAN_RxPkts

=item *

LAN_Collisions

=item *

LAN_TxRate    

=item *

LAN_RxRate    

=item *

LAN_UpTime    

=item *

WLAN_Status   

=item *

WLAN_TxPkts   

=item *

WLAN_RxPkts   

=item *

WLAN_Collisions

=item *

WLAN_TxRate    

=item *

WLAN_RxRate    

=item *

WLAN_UpTime    

=back

Only one element may be specified per command, so if you want the WLAN
transmit rate and the WLAN receive rate, you will have to type two
separate commands to get this information.

=head1 EXAMPLES

If your router is at address 192.168.0.1, your username for the router is 
"admin" and the password is "p4ssword", and you wanted to see the WLAN_RxRate,
you could use the following command:

C<get-wgt624-statistics --username admin --password p4ssword \
    --address 192.168.0.1 --element WLAN_RxRate>

Of course, the "\" character indicates a continuous line and is only shown above
for formatting reasons -- only do this if the shell in which you're using 
get-wgt624-statistics supports it.

=head1 AUTHOR INFORMATION

get-wgt624-statistics was written by Justin S. Leitgeb
<justin@phq.org>.  The home page for this software is
http://justin.phq.org/netgear/.

=head1 COPYRIGHT

get-wgt624-statistics and related libraries are copyright 2006 Justin
S. Leitgeb.

All rights reserved.  This program is free software; you may
redistribute it and/or modify it under the same terms as Perl itself.

=cut
