cdata.pm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. use Modern::Perl;
  2. use utf8;
  3. use telnet;
  4. use Mojo::AsyncAwait;
  5. use Data::Dumper;
  6. sub cdata_extract_onu_info {
  7. my @lines = @_;
  8. my @onu_info = @lines[6..@lines-5];
  9. return map([ split(' ', $_) ], @onu_info );
  10. };
  11. async cdata_login => sub
  12. {
  13. my $t = shift;
  14. my $login = shift;
  15. my $password = shift;
  16. await $t->connect;
  17. await $t->reply(qr/User name:/, $login);
  18. await $t->reply(qr/User password:/, $password);
  19. $t->print("enable");
  20. await $t->waitfor("#");
  21. $t->prompt(qr/#\s?$/);
  22. await $t->cmd("config");
  23. await $t->cmd("vty output show-all");
  24. };
  25. async cdata_get_onu_info => sub
  26. {
  27. my $t = shift;
  28. my $tree = shift;
  29. await $t->cmd("interface epon 0/0");
  30. my @lines = await $t->cmd("show ont info $tree all");
  31. my $onu_count_line = @lines[@lines-3];
  32. $onu_count_line =~ m|Total: (\d+), online (\d+)|;
  33. my $total_onu_cnt = $1;
  34. my $online_onu_cnt = $2;
  35. my $offline_onu_cnt = $total_onu_cnt - $online_onu_cnt;
  36. if ($lines[2] =~ /Error/) {
  37. $total_onu_cnt = 0;
  38. $online_onu_cnt = 0;
  39. $offline_onu_cnt = 0;
  40. }
  41. await $t->cmd("exit");
  42. my @onu_info = cdata_extract_onu_info(@lines);
  43. my @online_onu = grep { @$_[5] =~ "online" } @onu_info;
  44. @online_onu = map [@$_[2,3]], @online_onu;
  45. my @offline_onu = grep { @$_[5] =~ "offline|powerdown" } @onu_info;
  46. @offline_onu = map [@$_[2,3]], @offline_onu;
  47. my %res = (
  48. 'total_cnt' => $total_onu_cnt,
  49. 'active_cnt' => $online_onu_cnt,
  50. 'inactive_cnt' => $offline_onu_cnt,
  51. 'active' => [ @online_onu ],
  52. 'inactive' => [ @offline_onu ],
  53. );
  54. return %res;
  55. };
  56. async cdata_inspect => sub
  57. {
  58. my $ip = shift;
  59. my $login = shift;
  60. my $password = shift;
  61. my $tree = shift;
  62. my $t = new telnet($ip);
  63. # $t->debug(1);
  64. await cdata_login($t, $login, $password);
  65. my %onu_info = await cdata_get_onu_info($t, $tree);
  66. # say Dumper \%onu_info;
  67. $t->close;
  68. my $res = "Всего: $onu_info{'total_cnt'}
  69. Количество активных ONU: $onu_info{'active_cnt'}
  70. Количество неактивных ONU: $onu_info{'inactive_cnt'}
  71. \n";
  72. $res = $res . "Неактивные ONU:\n" . join("\n", map( join(" ", @$_), @{$onu_info{'inactive'}} ) ) . "\n\n";
  73. $res = $res . "Активные ONU:\n" . join("\n", map( join(" ", @$_), @{$onu_info{'active'}} ) );
  74. return $res;
  75. };
  76. async cdata_onu => sub
  77. {
  78. my $ip = shift;
  79. my $login = shift;
  80. my $password = shift;
  81. my $tree = shift;
  82. my $t = new telnet($ip);
  83. # $t->debug(1);
  84. await cdata_login($t, $login, $password);
  85. my %onu_info = await cdata_get_onu_info($t, $tree);
  86. $t->close;
  87. my $res = "Всего: $onu_info{'total_cnt'}
  88. Количество активных ONU: $onu_info{'active_cnt'}
  89. Количество неактивных ONU: $onu_info{'inactive_cnt'}
  90. \n";
  91. return $res;
  92. };
  93. async cdata_purge => sub
  94. {
  95. my $ip = shift;
  96. my $login = shift;
  97. my $password = shift;
  98. my $tree = shift;
  99. my $save = shift;
  100. my $t = new telnet($ip);
  101. # $t->debug(1);
  102. await cdata_login($t, $login, $password);
  103. my %onu_info = await cdata_get_onu_info($t, $tree);
  104. if ($onu_info{'inactive_cnt'} == 0) {
  105. $t->close;
  106. return "Нечего чистить.";
  107. }
  108. #say Dumper \%onu_info;
  109. await $t->cmd("interface epon 0/0");
  110. foreach my $i ( @{$onu_info{'inactive'}} ) {
  111. #say Dumper @$i[0];
  112. my $onu_num = @$i[0];
  113. my @tmp = await $t->cmd("ont delete $tree $onu_num");
  114. #say Dumper @tmp;
  115. }
  116. await $t->cmd("exit");
  117. my @deleted_onu = map { sprintf("%2d %s", $_[0], $_[1]) } @{$onu_info{inactive}};
  118. %onu_info = await cdata_get_onu_info($t, $tree);
  119. if ($save) {
  120. await $t->cmd("save");
  121. }
  122. $t->close;
  123. my $res = "Осталось ONU: $onu_info{'total_cnt'}\n";
  124. if ( $onu_info{'active_cnt'} != $onu_info{'total_cnt'} ) {
  125. $res = $res . "Общее количество ONU и количество активных ONU не совпадает.
  126. Лучше обратиться к Вашему системному администратору\n";
  127. }
  128. $res = $res . "Удалённые ONU:\n" . join("\n", @deleted_onu) if @deleted_onu > 0;
  129. return $res;
  130. };
  131. 1;