Browse Source

refactor cdata

alakiza 2 years ago
parent
commit
d9972c73a3
3 changed files with 226 additions and 11 deletions
  1. 2 0
      modules/bdcom.pm
  2. 214 0
      modules/cdata.pm
  3. 10 11
      modules/commands.pm

+ 2 - 0
modules/bdcom.pm

@@ -1,6 +1,8 @@
 use Modern::Perl;
 use utf8;
 
+use telnet;
+
 use Mojo::AsyncAwait;
 use Data::Dumper;
 

+ 214 - 0
modules/cdata.pm

@@ -0,0 +1,214 @@
+use Modern::Perl;
+use utf8;
+
+use telnet;
+
+use Mojo::AsyncAwait;
+use Data::Dumper;
+
+sub cdata_extract_onu_info {
+    my @lines = @_;
+
+    my @onu_info = @lines[6..@lines-5];
+
+    return map([ split(' ', $_) ], @onu_info );
+};
+
+async cdata_inspect => sub
+{
+    my $ip = shift;
+    my $login = shift;
+    my $password = shift;
+    my $tree = shift;
+
+    my $t = new telnet($ip);
+#    $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");
+
+    my $onu_count_line = @lines[@lines-3];
+
+
+    $onu_count_line =~ m|Total: (\d+), online (\d+)|;
+    my $total_onu_cnt   = $1;
+    my $online_onu_cnt  = $2;
+    my $offline_onu_cnt = $total_onu_cnt - $online_onu_cnt;
+
+    $t->close;
+
+    if ($lines[2] =~ /Error/) {
+        $total_onu_cnt   = 0;
+        $online_onu_cnt  = 0;
+        $offline_onu_cnt = 0;
+    }
+
+    my @onu_info = cdata_extract_onu_info(@lines);
+
+    my @online_onu = grep { @$_[5] =~ "online" }  @onu_info;
+    @online_onu = map join(' ', @$_[2,3]), @online_onu;
+
+    my @offline_onu = grep { @$_[5] =~ "offline|powerdown" }  @onu_info;
+    @offline_onu = map join(' ', @$_[2,3]), @offline_onu;
+
+    my $res = "Всего: $total_onu_cnt
+Количество активных ONU: $online_onu_cnt
+Количество неактивных ONU: $offline_onu_cnt
+\n";
+
+    $res = $res . "Неактивные ONU:\n" . join("\n", @offline_onu) . "\n\n" if $offline_onu_cnt;
+
+    $res = $res . "Активные ONU:\n" . join("\n", @online_onu) if $online_onu_cnt;
+
+    return $res;
+};
+
+async cdata_onu => sub
+{
+    my $ip = shift;
+    my $login = shift;
+    my $password = shift;
+    my $tree = shift;
+
+    my $t = new telnet($ip);
+#    $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");
+
+    my $onu_count_line = @lines[@lines-3];
+
+    $onu_count_line =~ m|Total: (\d+), online (\d+)|;
+    my $total_onu_cnt   = $1;
+    my $online_onu_cnt  = $2;
+    my $offline_onu_cnt = $total_onu_cnt - $online_onu_cnt;
+
+    $t->close;
+
+    if ($lines[2] =~ /Error/) {
+        $total_onu_cnt   = 0;
+        $online_onu_cnt  = 0;
+        $offline_onu_cnt = 0;
+    }
+
+    my $res = "Всего: $total_onu_cnt
+Количество активных ONU: $online_onu_cnt
+Количество неактивных ONU: $offline_onu_cnt
+\n";
+
+    return $res;
+};
+
+
+async cdata_purge => sub
+{
+    my $ip = shift;
+    my $login = shift;
+    my $password = shift;
+    my $tree = shift;
+
+    my $t = new telnet($ip);
+#    $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");
+
+    my $onu_count_line = @lines[@lines-3];
+
+    $onu_count_line =~ m|Total: (\d+), online (\d+)|;
+    my $total_onu_cnt   = $1;
+    my $online_onu_cnt  = $2;
+    my $offline_onu_cnt = $total_onu_cnt - $online_onu_cnt;
+
+    if ($lines[2] =~ /Error/) {
+        $total_onu_cnt   = 0;
+        $online_onu_cnt  = 0;
+        $offline_onu_cnt = 0;
+    }
+
+    if ($offline_onu_cnt == 0) {
+        $t->close;
+        return "Нечего чистить.";
+    }
+
+    my @onu_info = cdata_extract_onu_info(@lines);
+
+    my @offline_onu = grep { @$_[5] =~ "offline|powerdown" }  @onu_info;
+
+    foreach my $i (@offline_onu) {
+        my $onu_num = @$i[2];
+        my @tmp = await $t->cmd("ont delete $tree $onu_num");
+    }
+
+    my @deleted_onu = map join(' ', @$_[2,3]), @offline_onu;
+
+    @lines = await $t->cmd("show ont info $tree all");
+    $onu_count_line = @lines[@lines-3];
+
+    $onu_count_line =~ m|Total: (\d+), online (\d+)|;
+    $total_onu_cnt   = $1;
+    $online_onu_cnt  = $2;
+    $offline_onu_cnt = $total_onu_cnt - $online_onu_cnt;
+
+    if ($lines[2] =~ /Error/) {
+        $total_onu_cnt   = 0;
+        $online_onu_cnt  = 0;
+        $offline_onu_cnt = 0;
+    }
+
+    my $res = "После чистки осталось ONU: $total_onu_cnt
+Количество активных ONU: $online_onu_cnt
+Количество неактивных ONU: $offline_onu_cnt
+\n";
+
+    $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
+
+    await $t->cmd("exit");
+
+    await $t->cmd("save");
+
+    $t->close;
+
+    return $res;
+};
+
+1;

+ 10 - 11
modules/commands.pm

@@ -3,14 +3,15 @@ use utf8;
 
 use Mojo::AsyncAwait;
 use Data::Dumper;
-use purge_cdata qw(purge_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 inspect_cdata qw(inspect_cdata);
 #use onu_bdcom qw(onu_bdcom);
-use onu_cdata qw(onu_cdata);
+#use onu_cdata qw(onu_cdata);
 
 use bdcom;
+use cdata;
 
 our $client;
 
@@ -25,6 +26,7 @@ sub command_help
   
   notify($chatid, 
 "<b>/purge </b> <i>ip-адрес-устройства номер-дерева</i> - Очистить дерево PON
+<b>/purge-all-trees</b> <i>ip-адрес-устройства</i> - Очистить все деревья PON
 <b>/inspect</b> <i>ip-адрес-устройства номер-дерева</i> - Информация об ONU на дереве
 <b>/onu</b> <i>ip-адрес-устройства номер-дерева</i> - Краткая информация ONU на дереве", $rest);
 }
@@ -62,7 +64,7 @@ async command_inspect => sub
   }
   elsif ($dev->{type} =~ /^C-DATA/)
   {
-    $sub = reference("inspect_cdata");
+    $sub = reference("cdata_inspect");
   }
 
   return notify($chatid, "Неправильный номер дерева: $tree") unless is_valid_tree($dev->{type}, $tree);
@@ -115,7 +117,7 @@ async command_onu => sub
   }
   elsif ($dev->{type} =~ /^C-DATA/)
   {
-    $sub = reference("onu_cdata");
+    $sub = reference("cdata_onu");
   }
 
   return notify($chatid, "Неправильный номер дерева: $tree") unless is_valid_tree($dev->{type}, $tree);
@@ -136,9 +138,6 @@ async command_onu => sub
 
 };
 
-
-
-
 # purge 172.1.1.1 2 - почистить дерево 2 на ПОН
 async command_purge => sub
 {
@@ -171,7 +170,7 @@ async command_purge => sub
   }
   elsif ($dev->{type} =~ /^C-DATA/)
   {
-    $sub = reference("purge_cdata");
+    $sub = reference("cdata_purge");
   }
 
   return notify($chatid, "Неправильный номер дерева: $tree") unless is_valid_tree($dev->{type}, $tree);
@@ -182,7 +181,7 @@ async command_purge => sub
 
   my $text;
   eval {
-    $text = await $sub->($ip, $dev->{login}, $dev->{password}, $tree, 1);
+      $text = await $sub->($ip, $dev->{login}, $dev->{password}, $tree, 1);
   };
   
   return notify($chatid, "$ip '$dev->{name}': $@") if $@;
@@ -226,7 +225,7 @@ sub is_valid_tree
 
     my $tree_count = get_tree_count($devtype);
 
-    return ($tree_num <= $tree_count)&&($tree_num > 0);
+    return ($tree_num <= $tree_count) && ($tree_num > 0);
 }
 
 sub parse_error