Page 1 of 1

ADSL stats perl script (attached). Useful?

Posted: Mon Jun 16, 2014 5:18 pm
by clashcityrocker
Don't know if this is useful to anyone else, here is a perl script that obtains the ADSL stats from a billion router. I'm no perl expert, but I was hoping to tweak it to monitor my line over time, and creating a CSV file with some stats that I can graph with regards to error rates over time.

If anyone wants to take it and improve on it, feel free, but please share the love...

You will need the perl libexpect-perl library and you will need to edit the file and put your router password in.

Tested on 7800DXL.

Output from mine:

Code: Select all

 [1402934652] PROCESS_SERVICE_CHECK_RESULT;adslmodem;ADSL sync;0;ADSL2+  No Defect Showtime |'snr-down'=6.0dB 'snr-up'=5.8dB 'atten-down'=30.5dB 'atten-up'=15.7dB 'power-down'=0.0dB 'power-up'=12.8dB 'RS(Downstream):'=124745701 'RS(Upstream):'=1013355 'RSCorr(Downstream):'=18994 'RSCorr(Upstream):'=0 'RSUnCorr(Downstream):'=0 'RSUnCorr(Upstream):'=0

The Perl script

Code: Select all

#!/usr/bin/perl -w
use Expect;

$e = Expect->new();

$e->debug(0);
$e->log_stdout(0);

# Start Telnet session.
$e->spawn("telnet 192.168.1.254");

# Log in.
$e->expect(
  5,
  [ 'ogin',
    sub {$self = shift; $self->send("admin\n"); exp_continue;} ],
  [ 'Password:',
    sub {$self = shift; $self->send("PASSWORD_CHANGEME\n"); exp_continue;} ],
  '> '
);

$e->send("adsl info --show\n");
$e->expect(5, "> ");
my $result = $e->before();
print $result;
if ($result)
{
    adslinfoshow($result);
}

$e->send("logout\n");

sub adslinfoshow
{
  my $status = "";
  my $performance = "";
  my $returncode = 3;
  foreach (split(/\n/, $_[0]))
  {
    s/\r$//;
    /^Status: (G\.994 Training)/i and do { $status .= "$1 "; $returncode = 1; next; };
    /^Status: (G\.992 Started)/i and do { $status .= "$1 "; $returncode = 1; next; };
    /^Status: (G\.992 Channel Analysis)/i and do { $status .= "$1 "; $returncode = 1; next; };
    /^Status: (G\.992 Message Exchange)/i and do { $status .= "$1 "; $returncode = 1; next; };
    /^Status: (Idle)/i and do { $status .= "$1 "; $returncode = 2; next; };
    /^Mode:\s+(.*)$/i and do { $status .= "$1 "; next; };
    /^Channel:\s+(.*)$/i and do { $status .= "$1 "; next; };
    /^Line Status:\s+(.*)$/i and
      do { $status .= "$1 "; next; };
    /^Training Status:\s+(.*)$/i and
      do { $status .= "$1 ";
           $1 eq "Showtime" and $returncode = 0;
           next; };
    /^SNR\s?\(dB\):\s+(.+?)\s+(.+?)$/ and
      do { $performance .= "'snr-down'=$1dB 'snr-up'=$2dB "; next; };
    /^Attn\s?\(dB\):\s+(.+?)\s+(.+?)$/ and
      do { $performance .= "'atten-down'=$1dB 'atten-up'=$2dB "; next; };
    /^Pwr\s?\(dBm\):\s+(.+?)\s+(.+?)$/ and
      do { $performance .= "'power-down'=$1dB 'power-up'=$2dB "; next; };
    /^Max\s?\(Kbps\):\s+(.+?)\s+(.+?)$/ and
      do { $performance .= "'maxrate-down'=$1kbps 'maxrate-up'=$2kbps "; next; };
    /^Rate\s?\(Kbps\):\s+(.+?)\s+(.+?)$/ and
      do { $status .= "$1/$2kbps ";
           $performance .= "'rate-down'=$1kbps 'rate-up'=$2kbps "; next; };

 /^RS:\s+(.+?)\s+(.+?)$/ and
      do { $performance .= "'RS(Downstream):'=$1 'RS(Upstream):'=$2 "; next; };

 /^RSCorr:\s+(.+?)\s+(.+?)$/ and
      do { $performance .= "'RSCorr(Downstream):'=$1 'RSCorr(Upstream):'=$2 "; next; };

 /^RSUnCorr:\s+(.+?)\s+(.+?)$/ and
      do { $performance .= "'RSUnCorr(Downstream):'=$1 'RSUnCorr(Upstream):'=$2 "; next; };

  }
  my $timestamp = time;
  print "[$timestamp] PROCESS_SERVICE_CHECK_RESULT;adslmodem;ADSL sync;$returncode;$status|$performance\n";
}


Re: ADSL stats perl script (attached). Useful?

Posted: Wed Jun 25, 2014 12:04 am
by davidwf
sounds like it might be useful - thanks.....
could you offer some guidance to someone who doesn't know what perl is or how to use it :? ?

Re: ADSL stats perl script (attached). Useful?

Posted: Sat Jul 19, 2014 6:28 pm
by clashcityrocker
Had some more time today, hacked something better in Python. This one logs ADSL router stats to Google Docs online....

Excuse the hackery, I'm not a python coder, I hate the language (what idiot decided to use indentation for code blocks), and some my baulk at the cheap way I use the string tokenize, but it does work.. :-)

Image

Code: Select all

import gspread
import datetime
import getpass
import sys
import telnetlib
import shelve

HOST = "192.168.1.254"
user = "admin"
password = "PASSWORD_CHANGEME" #Router Password

tn = telnetlib.Telnet(HOST)

tn.read_until("Login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("adsl info --show\n")
tn.write("exit\n")
rawtext = tn.read_all()
#print rawtext
values = str.split( rawtext )
#print (values)

d = shelve.open('routerstats.db')

if not d.has_key('LastRSUncorr'):
   LastUncorrected = 0
else:
   LastUncorrected = d['LastRSUncorr']



Status = values[values.index("Status:")+1]
SyncDown = values[values.index("Downstream")+3]
RS = values[values.index("RS:")+1]
RSCorr = values[values.index("RSCorr:")+1]
RSUnCorr = values[values.index("RSUnCorr:")+1]
SNRDown = values[values.index("SNR")+2]
AttenDown = values[values.index("Attn(dB):")+1]
PercentCorrectable = str(round ( float(RSCorr) / float(RS) * 100, 3 )) + "%"
PercentUncorrectable = str(round ( float(RSUnCorr) / float(RS) * 100 , 3 )) + "%"

d['LastRSUncorr'] = RSUnCorr
d.close()

# Login with your Google account
gc = gspread.login('MY_USERNAME@gmail.com', 'MYPASSWORD_CHANGEME')  #Google Docs Account - Might want to use App-Specific sign on here.

# Open a worksheet from spreadsheet with one shot
wks = gc.open("Router Stats").sheet1
values = [datetime.datetime.now(), Status , SyncDown, RS, RSCorr, RSUnCorr, SNRDown, AttenDown, int( RSUnCorr) - int(LastUncorrected), PercentCorrectable, PercentUncorrectable ]
wks.append_row(values)

Re: ADSL stats perl script (attached). Useful?

Posted: Sun Jul 20, 2014 12:38 pm
by clashcityrocker
Just be aware with my script, there is a bug in the Billion firmware, when RS count rolls over (i.e. it exceeds the maximum size of it's storage variable), the RS-Corrected and RS-Uncorrected aren't reset too, so you get weird stats like 130% of packets had correctable errors. The firmware should perhaps reset all 3 when one rolls around.