Browse Source

implement purge commands

alakiza 2 years ago
parent
commit
b592beda46
7 changed files with 195 additions and 103 deletions
  1. 6 6
      modules/commands.pm
  2. 26 14
      modules/inspect_bdcom.pm
  3. 12 13
      modules/inspect_cdata.pm
  4. 16 5
      modules/onu_bdcom.pm
  5. 10 8
      modules/onu_cdata.pm
  6. 109 31
      modules/purge_bdcom.pm
  7. 16 26
      modules/purge_cdata.pm

+ 6 - 6
modules/commands.pm

@@ -3,12 +3,12 @@ use utf8;
 
 use Mojo::AsyncAwait;
 use Data::Dumper;
-use purge_cdata;
-use purge_bdcom;
-use inspect_bdcom;
-use inspect_cdata;
-use onu_bdcom;
-use onu_cdata;
+use purge_cdata qw(purge_cdata);
+use purge_bdcom qw(purge_bdcom);
+use inspect_bdcom qw(inspect_bdcom);
+use inspect_cdata qw(inspect_cdata);
+use onu_bdcom qw(onu_bdcom);
+use onu_cdata qw(onu_cdata);
 
 our $client;
 

+ 26 - 14
modules/inspect_bdcom.pm

@@ -1,9 +1,15 @@
+#package inspect_bdcom;
+
 use Modern::Perl;
 use utf8;
 
 use telnet;
 
-sub extract_onu_num {
+use Exporter;
+our @ISA= qw( Exporter );
+our @EXPORT = qw( inspect_bdcom );
+
+sub inspect_bdcom::_extract_onu_num {
     my $onu_name = shift;
 
     $onu_name =~ m|EPON0/\d+:(\d+)|;
@@ -11,10 +17,10 @@ sub extract_onu_num {
     return $1;
 };
 
-sub extract_onu_info {
+sub inspect_bdcom::_extract_onu_info {
     my @lines = @_;
 
-    return map([substr($_, 0, 11), split(' ', substr($_, 12, -1))], @lines[3..@lines-3]);
+    return map([substr($_, 0, 11), split(' ', substr($_, 11, -1))], @lines[3..@lines-3]);
 };
 
 async inspect_bdcom => sub
@@ -23,9 +29,9 @@ async inspect_bdcom => sub
     my $login = shift;
     my $password = shift;
     my $tree = shift;
-  
+
   ############################
-  
+
     my $t = new telnet($ip);
 #  $t->debug(1);
 
@@ -53,21 +59,25 @@ async inspect_bdcom => sub
     }
 
     $t->prompt(qr/#/);
-  
+
     await $t->cmd("terminal length 0");
     await $t->cmd("terminal width 200");
-  
+
   #######################
-  
+
     my @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
     $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
     my $inactive_count = $1 || "?";
-
-    $t->prompt(qr/#/);
+    if (@inactive_onu <= 1) {
+        $inactive_count = 0;
+    }
 
     my @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
     $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
     my $active_count = $1 || "?";
+    if (@active_onu <= 1) {
+        $active_count = 0;
+    }
 
     my $total_count = $active_count + $inactive_count;
 
@@ -78,12 +88,14 @@ async inspect_bdcom => sub
 Количество неактивных ONU: $inactive_count
 \n";
 
-    my @in_onu_info = extract_onu_info(@inactive_onu);
-    my @compact_in_onu_info = map( [extract_onu_num(@$_[0]), @$_[1]], @in_onu_info );
+    my @in_onu_info = inspect_bdcom::_extract_onu_info(@inactive_onu);
+    my @compact_in_onu_info = map( [inspect_bdcom::_extract_onu_num(@$_[0]), @$_[1]], @in_onu_info );
     @compact_in_onu_info = map( join(" ", @$_), @compact_in_onu_info);
 
-    my @a_onu_info = extract_onu_info(@active_onu);
-    my @compact_a_onu_info = map( [extract_onu_num(@$_[0]), @$_[1]], @a_onu_info );
+    say Dumper @active_onu;
+    my @a_onu_info = inspect_bdcom::_extract_onu_info(@active_onu);
+    say "test2";
+    my @compact_a_onu_info = map( [inspect_bdcom::_extract_onu_num(@$_[0]), @$_[1]], @a_onu_info );
     @compact_a_onu_info = map( join(" ", @$_), @compact_a_onu_info);
 
     $res = $res . "Неактивные ONU:\n" . join("\n", @compact_in_onu_info) . "\n\n";

+ 12 - 13
modules/inspect_cdata.pm

@@ -3,7 +3,11 @@ use utf8;
 
 use telnet;
 
-sub extract_onu_info {
+use Exporter;
+our @ISA= qw( Exporter );
+our @EXPORT = qw( inspect_cdata );
+
+sub inspect_cdata::_extract_onu_info {
     my @lines = @_;
 
     my @onu_info = @lines[6..@lines-5];
@@ -17,28 +21,25 @@ async inspect_cdata => sub
     my $login = shift;
     my $password = shift;
     my $tree = shift;
-  
+
     my $t = new telnet($ip);
-    $t->debug(1);
+#    $t->debug(1);
 
     await $t->connect;
 
     await $t->reply(qr/User name:/, $login);
     await $t->reply(qr/User password:/, $password);
-  
+
     $t->print("enable");
     await $t->waitfor("#");
     $t->prompt(qr/#\s?$/);
-  
+
     await $t->cmd("config");
     await $t->cmd("vty output show-all");
-  
+
     $t->print("interface epon 0/0");
     $t->prompt(qr/#\s?$/);
     my @lines = await $t->cmd("show ont info $tree all");
-  
-#    say Dumper @lines;
-#    say Dumper @lines[2];
 
     my $onu_count_line = @lines[@lines-3];
 
@@ -49,15 +50,13 @@ async inspect_cdata => sub
 
     $t->close;
 
-    if (@lines[2] =~ /Error/) {
+    if ($lines[2] =~ /Error/) {
         $total_onu_cnt   = 0;
         $online_onu_cnt  = 0;
         $offline_onu_cnt = 0;
     }
 
-    my @onu_info = extract_onu_info(@lines);
-
-#    say Dumper @onu_info;
+    my @onu_info = inspect_cdata::_extract_onu_info(@lines);
 
     my @online_onu = grep { @$_[5] =~ "online" }  @onu_info;
     @online_onu = map join(' ', @$_[2,3]), @online_onu;

+ 16 - 5
modules/onu_bdcom.pm

@@ -3,15 +3,19 @@ use utf8;
 
 use telnet;
 
+use Exporter;
+our @ISA= qw( Exporter );
+our @EXPORT = qw( onu_bdcom );
+
 async onu_bdcom => sub
 {
     my $ip = shift;
     my $login = shift;
     my $password = shift;
     my $tree = shift;
-  
+
   ############################
-  
+
     my $t = new telnet($ip);
 #  $t->debug(1);
 
@@ -39,21 +43,28 @@ async onu_bdcom => sub
     }
 
     $t->prompt(qr/#/);
-  
+
     await $t->cmd("terminal length 0");
     await $t->cmd("terminal width 200");
-  
+
   #######################
-  
+
     my @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
     $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
     my $inactive_count = $1 || "?";
+    if (@inactive_onu <= 1) {
+        $inactive_count = 0;
+    }
 
     $t->prompt(qr/#/);
 
     my @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
     $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
     my $active_count = $1 || "?";
+    if (@active_onu <= 1) {
+        $active_count = 0;
+    }
+
 
     my $total_count = $active_count + $inactive_count;
 

+ 10 - 8
modules/onu_cdata.pm

@@ -3,33 +3,35 @@ use utf8;
 
 use telnet;
 
+use Exporter;
+our @ISA= qw( Exporter );
+our @EXPORT = qw( onu_cdata );
+
 async onu_cdata => sub
 {
     my $ip = shift;
     my $login = shift;
     my $password = shift;
     my $tree = shift;
-  
+
     my $t = new telnet($ip);
-    $t->debug(1);
+#    $t->debug(1);
 
     await $t->connect;
 
     await $t->reply(qr/User name:/, $login);
     await $t->reply(qr/User password:/, $password);
-  
+
     $t->print("enable");
     await $t->waitfor("#");
     $t->prompt(qr/#\s?$/);
-  
+
     await $t->cmd("config");
     await $t->cmd("vty output show-all");
-  
+
     $t->print("interface epon 0/0");
     $t->prompt(qr/#\s?$/);
     my @lines = await $t->cmd("show ont info $tree all");
-  
-    #say Dumper @lines;
 
     my $onu_count_line = @lines[@lines-3];
 
@@ -40,7 +42,7 @@ async onu_cdata => sub
 
     $t->close;
 
-    if (@lines[2] =~ /Error/) {
+    if ($lines[2] =~ /Error/) {
         $total_onu_cnt   = 0;
         $online_onu_cnt  = 0;
         $offline_onu_cnt = 0;

+ 109 - 31
modules/purge_bdcom.pm

@@ -2,55 +2,133 @@ use Modern::Perl;
 use utf8;
 
 use telnet;
+
+use Exporter;
+our @ISA= qw( Exporter );
+our @EXPORT = qw( purge_bdcom );
+
+sub purge_bdcom::_extract_onu_num {
+    my $onu_name = shift;
+
+    $onu_name =~ m|EPON0/\d+:(\d+)|;
+
+    return $1;
+};
+
+sub purge_bdcom::_extract_onu_info {
+    my @lines = @_;
+
+    return map([substr($_, 0, 11), split(' ', substr($_, 11, -1))], @lines[3..@lines-3]);
+};
+
 async purge_bdcom => sub
 {
-  my $ip = shift;
-  my $login = shift;
-  my $password = shift;
-  my $tree = shift;
+    my $ip = shift;
+    my $login = shift;
+    my $password = shift;
+    my $tree = shift;
   
   ############################
   
-  my $t = new telnet($ip);
-#  $t->debug(1);
+    my $t = new telnet($ip);
+#    $t->debug(1);
 
-  await $t->connect;
+    await $t->connect;
 
-  await $t->reply(qr/Username:/, $login);
-  await $t->reply(qr/Password:/, $password);
+    await $t->reply(qr/Username:/, $login);
+    await $t->reply(qr/Password:/, $password);
 
-  my @greeting = await $t->waitfor(qr/>/);
-  my $version = "C";
+    my @greeting = await $t->waitfor(qr/>/);
+    my $version = "C";
 
-  for (@greeting)
-  {
-    $version = $1 if /Welcome to BDCOM P3310(\w)/;
-  }
+    for (@greeting)
+    {
+        $version = $1 if /Welcome to BDCOM P3310(\w)/;
+    }
 
-  $t->print("enable");
+    $t->print("enable");
 
-  my @next = await $t->waitfor(qr/#|password:/);
+    my @next = await $t->waitfor(qr/#|password:/);
 
-  if (grep {$_ =~ /password:/} @next)
-  {
-    $t->print($password);
-    await $t->waitfor(qr/#/);
-  }
+    if (grep {$_ =~ /password:/} @next)
+    {
+        $t->print($password);
+        await $t->waitfor(qr/#/);
+    }
 
-  $t->prompt(qr/#/);
+    $t->prompt(qr/#/);
   
-  await $t->cmd("terminal length 0");
-  await $t->cmd("terminal width 200");
+    await $t->cmd("terminal length 0");
+    await $t->cmd("terminal width 200");
   
   #######################
-  
-  my @lines = await $t->cmd("show epon active-onu interface ePON 0/$tree");
-  $lines[0] =~ m|Interface EPON0/\d+ has bound (\d+) active ONUs|;
-  my $count = $1 || "?";
 
-  $t->close;
+    my @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
+    $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    my $inactive_count = $1 || "?";
+    if (@inactive_onu <= 1) {
+        $inactive_count = 0;
+    }
+
+    my @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
+    $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    my $active_count = $1 || "?";
+    if (@active_onu <= 1) {
+        $active_count = 0;
+    }
+
+    my $total_count = $active_count + $inactive_count;
   
-  return $count;
+    if ($inactive_count == 0) {
+        $t->close;
+        return "Нечего чистить";
+    }
+
+    await $t->cmd("config");
+    await $t->cmd("interface EPON0/$tree");
+
+    my @in_onu_info = purge_bdcom::_extract_onu_info(@inactive_onu);
+    my @compact_in_onu_info = map( [purge_bdcom::_extract_onu_num(@$_[0]), @$_[1]], @in_onu_info );
+
+    foreach my $i ( @compact_in_onu_info ) {
+        say Dumper @$i[1];
+        my $onu_mac = @$i[1];
+        my @tmp = await $t->cmd("no epon bind-onu mac $onu_mac");
+        say Dumper @tmp;
+    }
+
+    my @deleted_onu = map( join(" ", @$_), @compact_in_onu_info);
+
+    await $t->cmd("exit");
+
+    @inactive_onu = await $t->cmd("show epon inactive-onu interface ePON 0/$tree");
+    $inactive_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    $inactive_count = $1 || "?";
+    if (@inactive_onu <= 1) {
+        $inactive_count = 0;
+    }
+
+    @active_onu = await $t->cmd("show epon active-onu interface ePON 0/$tree");
+    $active_onu[0] =~ m|Interface EPON0/\d+ has bound (\d+)|;
+    $active_count = $1 || "?";
+    if (@active_onu <= 1) {
+        $active_count = 0;
+    }
+
+    $total_count = $active_count + $inactive_count;
+
+    await $t->cmd("write all");
+
+    $t->close;
+
+    my $res = "После чистки осталось ONU: $total_count
+Количество активных ONU: $active_count
+Количество неактивных ONU: $inactive_count
+\n";
+
+    $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
+
+    return $res;
 };
 
 1;

+ 16 - 26
modules/purge_cdata.pm

@@ -3,7 +3,11 @@ use utf8;
 
 use telnet;
 
-sub extract_onu_info {
+use Exporter;
+our @ISA= qw( Exporter );
+our @EXPORT = qw( purge_cdata );
+
+sub purge_cdata::_extract_onu_info {
     my @lines = @_;
 
     my @onu_info = @lines[6..@lines-5];
@@ -17,28 +21,25 @@ async purge_cdata => sub
     my $login = shift;
     my $password = shift;
     my $tree = shift;
-  
+
     my $t = new telnet($ip);
-    $t->debug(1);
+#    $t->debug(1);
 
     await $t->connect;
 
     await $t->reply(qr/User name:/, $login);
     await $t->reply(qr/User password:/, $password);
-  
+
     $t->print("enable");
     await $t->waitfor("#");
     $t->prompt(qr/#\s?$/);
-  
+
     await $t->cmd("config");
     await $t->cmd("vty output show-all");
-  
+
     $t->print("interface epon 0/0");
     $t->prompt(qr/#\s?$/);
     my @lines = await $t->cmd("show ont info $tree all");
-  
-#    say Dumper @lines;
-#    say Dumper @lines[2];
 
     my $onu_count_line = @lines[@lines-3];
 
@@ -47,7 +48,7 @@ async purge_cdata => sub
     my $online_onu_cnt  = $2;
     my $offline_onu_cnt = $total_onu_cnt - $online_onu_cnt;
 
-    if (@lines[2] =~ /Error/) {
+    if ($lines[2] =~ /Error/) {
         $total_onu_cnt   = 0;
         $online_onu_cnt  = 0;
         $offline_onu_cnt = 0;
@@ -58,28 +59,17 @@ async purge_cdata => sub
         return "Нечего чистить.";
     }
 
-    my @onu_info = extract_onu_info(@lines);
-
-#    say Dumper @onu_info;
-
-    #my @online_onu = grep { @$_[5] =~ "online" }  @onu_info;
-    #@online_onu = map join(' ', @$_[2,3]), @online_onu;
+    my @onu_info = purge_cdata::_extract_onu_info(@lines);
 
     my @offline_onu = grep { @$_[5] =~ "offline|powerdown" }  @onu_info;
 
-#    say Dumper @offline_onu;
-
     foreach my $i (@offline_onu) {
-#        say Dumper @$i[2];
         my $onu_num = @$i[2];
         my @tmp = await $t->cmd("ont delete $tree $onu_num");
-#        say Dumper @tmp;
     }
 
     my @deleted_onu = map join(' ', @$_[2,3]), @offline_onu;
 
-#    say Dumper @deleted_onu;
-
     @lines = await $t->cmd("show ont info $tree all");
     $onu_count_line = @lines[@lines-3];
 
@@ -88,7 +78,7 @@ async purge_cdata => sub
     $online_onu_cnt  = $2;
     $offline_onu_cnt = $total_onu_cnt - $online_onu_cnt;
 
-    if (@lines[2] =~ /Error/) {
+    if ($lines[2] =~ /Error/) {
         $total_onu_cnt   = 0;
         $online_onu_cnt  = 0;
         $offline_onu_cnt = 0;
@@ -102,11 +92,11 @@ async purge_cdata => sub
 
     $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
 
-    $t->close;
+    await $t->cmd("exit");
 
-    #$res = $res . "Неактивные ONU:\n" . join("\n", @offline_onu) . "\n\n" if $offline_onu_cnt;
+    await $t->cmd("save");
 
-    #$res = $res . "Активные ONU:\n" . join("\n", @online_onu) if $online_onu_cnt;
+    $t->close;
 
     return $res;
 };