HuaweiOnuScanCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace HuaweiBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use HuaweiBundle\SNMP\SNMP as SNMP;
  8. //use RedisBundle\Services\RedisService;
  9. class HuaweiOnuScanCommand extends BaseCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('huawei:onu:scan')
  15. ->setDescription('Escanear OLT para obtener ONUs')
  16. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  17. ->setDefinition(array(
  18. new InputOption('olt-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId de la OLT",1),
  19. new InputOption('olt-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice de la OLT",1),
  20. new InputOption('olt-ip', false, InputOption::VALUE_OPTIONAL, "IP de la OLT"),
  21. new InputOption('olt-community', false, InputOption::VALUE_OPTIONAL, "COMMUNITY de la OLT"),
  22. new InputOption('olt-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP")
  23. ))
  24. ;
  25. }
  26. /**
  27. * @param InputInterface $input
  28. * @param OutputInterface $output
  29. */
  30. protected function execute(InputInterface $input, OutputInterface $output)
  31. {
  32. parent::execute($input, $output);
  33. $this->output->writeln("INICIO");
  34. $inicio = microtime(true);
  35. $oltDeviceId = (int) $input->getOption('olt-device-id');
  36. $oltServerId = (int) $input->getOption('olt-server-id');
  37. $oltIp = $input->getOption('olt-ip');
  38. $oltCommunity = $input->getOption('olt-community');
  39. $oltSnmpLibrary = $input->getOption('olt-snmp-library');
  40. $flag = "olt_scan_d_{$oltDeviceId}_s_{$oltServerId}.lock";
  41. $key_olt_scan = "olt_scan_d_{$oltDeviceId}_s_{$oltServerId}";
  42. $key_olt_pons = "olt_scan_pons_d_{$oltDeviceId}_s_{$oltServerId}";
  43. /* Control de bloqueo */
  44. if($this->lock($flag)) {return;}
  45. $SNMP = new SNMP($oltIp, $oltCommunity);
  46. $library = "use".$oltSnmpLibrary;
  47. $portCached = $this->getData($key_olt_pons, true);
  48. if(empty($portCached)) {
  49. $this->output->writeln("Se requiere {$key_olt_pons}.");
  50. $this->removeLock($flag);
  51. return true;
  52. }
  53. $dataCached = array();
  54. $serialNumbers = $SNMP->$library()->onuSerialNumber();
  55. // array [4194312192.1] => 485754430011D168
  56. // portIndex.onuIndex => serialNumber Hexa [48 57 54 43] 0011D168 => HWTC0011D168 (parecido al PonSerialNumber)
  57. //print_r($portCached);
  58. //print_r($serialNumbers);
  59. $countOnus = 0;
  60. foreach($serialNumbers as $index => $hexSerialNumber) {
  61. $vendoId = $this->hex2str(substr($hexSerialNumber,0,8));
  62. $rest = substr($hexSerialNumber,8);
  63. $sn = strtolower($vendoId.$rest);
  64. $portOnu = explode(".",$index);
  65. if(count($portOnu) != 2) continue;
  66. $portIndex = $portOnu[0];
  67. $onuId = $portOnu[1];
  68. $countOnus++;
  69. if(isset($portCached[$portIndex])) {
  70. $p = $portCached[$portIndex];
  71. $data = $p;
  72. $data['onuId'] = $onuId;
  73. $data['serialNumber'] = $sn;
  74. $data['hexaSerialNumber'] = $hexSerialNumber;
  75. $data['ponport'] = "{$p['ponPort']}/{$onuId}";
  76. $dataCached[$index] = $data;
  77. }
  78. }
  79. $this->setData($key_olt_scan, $dataCached, true);
  80. /* Fin de bloqueo */
  81. $this->removeLock($flag);
  82. $fin = microtime(true);
  83. $time = $fin - $inicio;
  84. $this->output->writeln("Tiempo: $time segundos / Cantidad ONUs: {$countOnus}");
  85. }
  86. }