parse show interface $port adsl-status
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 22 Nov 2011 18:31:57 +0000 (19:31 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 22 Nov 2011 18:32:01 +0000 (19:32 +0100)
This change also adds custom callback which (if implemented) allows
sub-modules to call (also new) command_out to just return command
output without parsing it.

This was implemented to parse one particular table in this change.

lib/H1/ZTE.pm
lib/H1/ZTEDSLAM.pm

index e0f3f6c..9b423b9 100644 (file)
@@ -35,7 +35,7 @@ sub telnet {
        return $telnet->{$ip} = $t;
 }
 
-sub command {
+sub command_out {
        my ($self,$command) = @_;
 
        my $t = $self->telnet;
@@ -53,6 +53,12 @@ sub command {
 
        warn "## out = [$out]" if $ENV{DEBUG};
 
+       return $out;
+}
+
+sub command {
+       my ($self,$command) = @_;
+       my $out  = $self->command_out( $command );
        my $hash = $self->parse( $out );
 
        warn "## ", $self->ip, " $command ",dump $hash;
@@ -84,6 +90,11 @@ sub hash {
                copy ref $commands->{$command} eq 'ARRAY' ? @{$commands->{$command}} : undef;
        }
 
+       if ( $self->can('custom') ) {
+               $hash = $self->custom( $port );
+               copy keys %$hash;
+       }
+
        warn "# row = ",dump $row if $ENV{DEBUG};
 
        $row = $self->fixup_row( $row ) if $self->can('fixup_row');
index 8f9e856..f304345 100644 (file)
@@ -72,5 +72,39 @@ AturDMTState
        )],
 }}
 
+sub custom {
+       my ($self,$port) = @_;
+
+       my $out = $self->command_out("show interface $port adsl-status");
+       my @lines = split(/\s*[\n\r]+/,$out);
+
+       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]);
+
+       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];
+               }
+       }
+
+       return $hash;
+}
+
 1;