#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;
use MIME::Base64;

# simple script to get stats from a NTL/VirginMedia cable modem.
# $Id: ntl_virgin_media.pl 67 2007-10-26 22:20:28Z ed $
# $URL: svn+ssh://fw/repos/code/perl/cablemodem/ntl_virgin_media.pl $

# Use at your own risk. Author not responsible for use.
# Updates at http://www.s5h.net
#
# 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA


my %config;
my %status;

sub set_config {
	my $ua = LWP::UserAgent->new();
	$config{'ua'} = $ua;
	$config{'ip'} = "192.168.100.1";
	$config{'user'} = "root";
	$config{'pass'} = "root";
	$config{'auth'} = encode_base64( "${config{'user'}}:" .
		"${config{'pass'}}", "" );
	$config{'ds_page'} = "http://${config{'ip'}}/CmDnstream.asp";
	$config{'us_page'} = "http://${config{'ip'}}/CmUpstream.asp";
	$config{'stat_page'} = "http://${config{'ip'}}/CmStatus.asp";
}

sub get_downstream {
	my $req = HTTP::Request->new( GET => $config{'ds_page'} );
	
	$req->header( "Authorization" => "Basic ${config{'auth'}}" );
	$req->header( "Referer" => "http://192.168.100.1/login.html" );

	$config{'ua'}->request($req);
	
	my $res = $config{'ua'}->request($req);
	if( !$res->is_success ) {
		return undef;
	}

	my @lines = split( /\n/, $res->content );
	foreach( @lines ) {
		if( $_ =~ /Downstream Lock : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'downstream_lock'} = $1;
		}
		if( $_ =~ /Downstream Channel Id : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'downstream_channel_id'} = $1;
		}
		if( $_ =~ /Downstream Frequency : <\/font><\/td><td>(.*?) Hz<\/td><\/tr>/ ) {
			$status{'downstream_frequency'} = $1;
		}

		if( $_ =~ /Downstream Modulation : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'downstream_modulation'} = $1;
		}

		if( $_ =~ /Downstream Symbol Rate : <\/font><\/td><td>(.*?) Ksym\/sec<\/td><\/tr>/ ) {
			$status{'downstream_symbol_rate'} = $1;
		}

		if( $_ =~ /Downstream Interleave Depth : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'downstream_interleave_depth'} = $1;
		}

		if( $_ =~ /Downstream Receive Power Level : <\/font><\/td><td>(.*?) dBmV<\/td><\/tr>/ ) {
			$status{'downstream_receive_power_leavel'} = $1;
		}

		if( $_ =~ /Downstream SNR : <\/font><\/td><td>(.*?) dB<\/td><\/tr>/ ) {
			$status{'downstream_snr'} = $1;
		}
	}
}

sub get_upstream {
	my $req = HTTP::Request->new( GET => $config{'us_page'} );
	
	$req->header( "Authorization" => "Basic ${config{'auth'}}" );
	$req->header( "Referer" => "http://192.168.100.1/login.html" );

	$config{'ua'}->request($req);
	
	my $res = $config{'ua'}->request($req);
	if( !$res->is_success ) {
		return undef;
	}

	my @lines = split( /\n/, $res->content );
	foreach( @lines ) {
		if( $_ =~ /Upstream Lock : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'upstream_lock'} = $1;
		}
		if( $_ =~ /Upstream Channel ID : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'upstream_channel_id'} = $1;
		}
		if( $_ =~ /pstream Frequency : <\/font><\/td><td>(.*?) Hz<\/td><\/tr>/ ) {
			$status{'upstream_frequency'} = $1;
		}
		if( $_ =~ /Upstream Modulation : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'upstream_modulation'} = $1;
		}
		if( $_ =~ /Upstream Symbol Rate : <\/font><\/td><td>(.*?) Ksym\/sec<\/td><\/tr>/ ) {
			$status{'upstream_symbol_rate'} = $1;
		}
		if( $_ =~ /Upstream transmit Power Level : <\/font><\/td><td>(.*?) dBmV<\/td><\/tr>/ ) {
			$status{'upstream_transmit_power_level'} = $1;
		}
		if( $_ =~ /Upstream Mini-Slot Size : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'upstream_mini_slot_size'} = $1;
		}
		if( $_ =~ /Upstream Lock : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'upstream_lock'} = $1;
		}
		if( $_ =~ /Upstream Lock : <\/font><\/td><td>(.*?)<\/td><\/tr>/ ) {
			$status{'upstream_lock'} = $1;
		}
	}
}

sub print_status {
	foreach( sort keys( %status ) ) {
		print( "$_: ${status{$_}}\n" );
	}
}

set_config();
get_downstream();
get_upstream();

if( !defined( $ARGV[0] ) ) {
	print_status();
	exit(0);
}

if( defined( $status{ $ARGV[0] } ) ) {
	print( $status{ $ARGV[0] } . "\n0\n" );
}


