Merge branch 'devel' of h1dev:/srv/APKPM/
[APKPM.git] / lib / H1 / ZTEDSLAM.pm
old mode 100755 (executable)
new mode 100644 (file)
index 3b6c6c2..c00a4a7
@@ -1,36 +1,15 @@
 package H1::ZTEDSLAM;
-use warnings;
-use strict;
-
 use Moose;
 
-use Net::Telnet;
-use Data::Dump qw(dump);
-
-use Moose::Util::TypeConstraints;
-use Regexp::Common qw(net);
-
-subtype IPAddr
-=> as Str
-=> where {/^$RE{net}{IPv4}$/}
-=> message { 'invalid IP address'};
-
-has 'ip'   => ( is => 'rw', isa => 'IPAddr' );
-
-our $telnet;
+with 'H1::ZTE';
 
-sub telnet {
-       my $self = shift;
-
-       my $ip = $self->ip;
-
-       return $telnet->{$ip} if exists $telnet->{$ip};
+sub connect {
+       my ( $self, $ip ) = @_;
 
        my $t = Net::Telnet->new( Timeout => 10, Prompt => '/#/' );
 
        $t->dump_log('/tmp/log') if $ENV{DEBUG};
 
-       warn "open $ip";
        $t->open( $ip );
 
        $t->print("");
@@ -46,33 +25,17 @@ sub telnet {
 
        warn "login OK";
 
-       return $telnet->{$ip} = $t;
+       return $t;
 }
 
-sub command {
-       my ($self,$command) = @_;
-
-       $command .= ' ' . $self->{port};
-
-       my $t = $self->telnet;
 
-       warn "# $command\n";
-       $t->print($command);
-
-       my $out;
-       while (1) {
-               my($prematch, $match) = $t->waitfor('/(Press any key to continue \(Q to quit\)|#)/');
-               $out .= $prematch;
-               last if $match eq '#';
-               $t->print('');
-       }
-
-       warn "## out = [$out]" if $ENV{DEBUG};
+sub parse {
+       my ( $self, $out ) = @_;
 
        my $hash;
        foreach my $line ( split(/[\n\r]+/, $out) ) {
                warn "# $line\n" if $ENV{DEBUG};
-               if ( $line =~ m/^(\S+.*?)\s+:\s+(\S+.*)$/ ) {
+               if ( $line =~ m/^(\S+.*?)\s*:\s+(\S+.*)$/ ) {
                        my ($n,$v) = ($1,$2);
                        $n =~ s/\(.+\)//;
                        $hash->{$n} = $v;
@@ -81,42 +44,19 @@ sub command {
 
        }
 
-       warn "## ", $self->ip, " $command ",dump $hash;
-
        return $hash;
 }
 
-sub hash {
-       my ($self,$port) = @_;
-
-       my $ip   = $self->ip;
-       $self->{port} = $port;
-
-       warn "# hash $ip $port";
-
-our ( $row, $hash );
-
-sub copy {
-       foreach my $name (@_) {
-#              warn "# copy $name ", dump( $row ),$/;
-               $row->{$name} = $hash->{$name};
-       }
-}
-
-$hash = $self->command('show interface');
-copy qw(
+sub commands {{
+       'show interface %s' => [ qw(
 AdminStatus
 LinkStatus
 LastLinkUpTime
-);
-
-$hash = $self->command('show adsl status');
-copy qw(
+       )],
+       'show adsl status %s' => [ qw(
 LineConfProfile
-);
-
-$hash = $self->command('show adsl physical');
-copy qw(
+       )],
+       'show adsl physical %s' => [ qw(
 AtucCurrSnrMgn
 AtucCurrAtn
 AtucCurrStatus
@@ -129,31 +69,57 @@ AturCurrStatus
 AturCurrOutputPwr
 AturAttainableRate
 AturDMTState
-);
-
-warn "# row = ",dump $row if $ENV{DEBUG};
+       )],
+}}
 
-return $row;
-
-} # sub
+sub custom {
+       my ($self,$port) = @_;
 
+       my $out = $self->command_out("show interface $port adsl-status");
+       my @lines = split(/\s*[\n\r]+/,$out);
 
-sub logout {
-       my $self = shift;
-       my $ip = $self->ip;
-       my $t = $telnet->{$ip} || die "no $ip telnet in ",dump($telnet);
+       shift @lines if $lines[0] =~ m/adsl-status/; # skip command
+       if ( $lines[2] !~ m/^-+$/ ) {
+               warn "Can't parse into table - missing header separator at index 2: $lines[2]";
+               return;
+       }
+       my @h1 = split(/\s+/, $lines[0]);
+       my @h2 = split(/\s+/, $lines[1]);
+       my @v  = split(/\s+/, $lines[3]);
 
-       warn "logout $ip";
-       $t->print('logout');
-       $t->waitfor('/:/');
-       $t->print('y');
+       my $hash;
+       foreach my $i ( 0 .. $#v ) {
+               my $k = $h1[$i];
+
+               $h2[$i] =~ s/\(\w\)\s*$//; # remove unit
+
+               if ( $h2[$i] =~ m/\// && $v[$i] =~ m/\// ) {
+                       my ($ln,$rn) = split(/\//, $h2[$i]);
+                       my ($lv,$rv) = split(/\//, $v[$i]);
+                       $hash->{ join('_',$k,$ln) } = $lv;
+                       $hash->{ join('_',$k,$rn) } = $rv;
+               } else {
+                       $hash->{ $h2[$i] ? $h1[$i] .'_'. $h2[$i] : $h1[$i] } = $v[$i];
+               }
+       }
 
-}
+       my $out = $self->command_out("show adsl perf $port");
+       my @lines = split(/\s*[\n\r]+/,$out);
+
+       my $table;
+       foreach my $line ( @lines ) {
+               next if ! $line;
+               if ( $line =~ m/ADSL\s+(\w+)\s+Table/i ) {
+                       $table = $1;
+                       next;
+               } elsif ( $line =~ m/^(\S+)\s+:\s+(\S)*$/ ) {
+                       $hash->{ $table . '_' . $1 } = $2 if defined $2;
+               } else {
+                       warn "# IGNORED: $line\n";
+               }
+       }
 
-sub DESTROY {
-       my $self = shift;
-       warn "# DESTROY telnet = ",dump( keys %$telnet );
-       $self->logout($_) foreach keys %$telnet;
+       return $hash;
 }
 
 1;