Browse Source

implement purge for cdata

alakiza 2 years ago
parent
commit
8d47c906b1
3 changed files with 103 additions and 25 deletions
  1. 5 3
      modules/commands.pm
  2. 1 1
      modules/inspect_cdata.pm
  3. 97 21
      modules/purge_cdata.pm

+ 5 - 3
modules/commands.pm

@@ -177,14 +177,16 @@ async command_purge => sub
   
   notify($chatid, "Очищаю дерево $tree на устройстве $ip '$dev->{name}'");
 
-  my $count;
+  my $text;
   eval {
-    $count = await $sub->($ip, $dev->{login}, $dev->{password}, $tree);
+    $text = await $sub->($ip, $dev->{login}, $dev->{password}, $tree);
   };
   
   return notify($chatid, "$ip '$dev->{name}': $@") if $@;
   
-  notify($chatid, "$ip '$dev->{name}': работает $count ONU");
+  notify($chatid, "$ip '$dev->{name}' дерево $tree
+<code>$text</code>");
+
 };
 
 ###################################

+ 1 - 1
modules/inspect_cdata.pm

@@ -62,7 +62,7 @@ async inspect_cdata => sub
     my @online_onu = grep { @$_[5] =~ "online" }  @onu_info;
     @online_onu = map join(' ', @$_[2,3]), @online_onu;
 
-    my @offline_onu = grep { @$_[5] =~ "offline" }  @onu_info;
+    my @offline_onu = grep { @$_[5] =~ "offline|powerdown" }  @onu_info;
     @offline_onu = map join(' ', @$_[2,3]), @offline_onu;
 
     my $res = "Всего: $total_onu_cnt

+ 97 - 21
modules/purge_cdata.pm

@@ -3,36 +3,112 @@ use utf8;
 
 use telnet;
 
+sub extract_onu_info {
+    my @lines = @_;
+
+    my @onu_info = @lines[6..@lines-5];
+
+    return map([ split(' ', $_) ], @onu_info );
+};
+
 async purge_cdata => 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/User name:/, $login);
-  await $t->reply(qr/User password:/, $password);
+    await $t->reply(qr/User name:/, $login);
+    await $t->reply(qr/User password:/, $password);
   
-  $t->print("enable");
-  await $t->waitfor("#");
-  $t->prompt(qr/#\s?$/);
+    $t->print("enable");
+    await $t->waitfor("#");
+    $t->prompt(qr/#\s?$/);
   
-  await $t->cmd("config");
-  await $t->cmd("vty output show-all");
+    await $t->cmd("config");
+    await $t->cmd("vty output show-all");
   
-  $t->print("Interface epon 0/$tree");
-  my @lines = await $t->cmd("show ont info $tree  all");
+    $t->print("interface epon 0/0");
+    $t->prompt(qr/#\s?$/);
+    my @lines = await $t->cmd("show ont info $tree all");
   
-  say Dumper @lines;
-  
-  $t->close;
-  
-  return 5;
+#    say Dumper @lines;
+#    say Dumper @lines[2];
+
+    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 = 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 @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];
+
+    $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;
+
+    $t->close;
+
+    #$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;
 };
 
 1;