CmtsInterfaceScanCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace CmtsBundle\Command;
  3. use BaseStatsBundle\Command\BaseCmtsCommand;
  4. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use CmtsBundle\SNMP\SNMP as SNMP;
  9. use Symfony\Component\Yaml\Parser;
  10. class CmtsInterfaceScanCommand extends BaseCmtsCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('cmts:interface:scan')
  16. ->setDescription('Escanear CMTS para obtener Interfaces')
  17. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta. El comando requiere Redis.')
  18. ->setDefinition(array(
  19. new InputOption('cmts-device-id', null, InputOption::VALUE_OPTIONAL, "DeviceId del CMTS",1),
  20. new InputOption('cmts-server-id', null, InputOption::VALUE_OPTIONAL, "ServerDevice del CMTS",1),
  21. new InputOption('cmts-ip', false, InputOption::VALUE_OPTIONAL, "IP del CMTS"),
  22. new InputOption('cmts-community', false, InputOption::VALUE_OPTIONAL, "Community del CMTS"),
  23. new InputOption('cmts-snmp-library', false, InputOption::VALUE_OPTIONAL, "Versión de librería SNMP")
  24. ))
  25. ;
  26. }
  27. /**
  28. * @param InputInterface $input
  29. * @param OutputInterface $output
  30. */
  31. protected function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. parent::execute($input, $output);
  34. $key_cmts_scan = "cmts_scan_ifs_{$this->d_s}";
  35. $inicio = microtime(true);
  36. $SNMP = new SNMP($this->cmtsIp, $this->cmtsCommunity);
  37. $library = "use".$this->cmtsSnmpLibrary;
  38. $this->apiSNMP = $SNMP->$library();
  39. $dataCached = array();
  40. $countIfs = 0;
  41. $interfaces = $this->getSNMP("docsIfDescription","cmtsIf");
  42. $sysDesc = $this->getSNMP("systemDescription","cmtsDescription");
  43. $yaml = new Parser();
  44. $value = $yaml->parse(file_get_contents($cfg_file = dirname(__FILE__).'/../Resources/config/cmts.yml'));
  45. $search = false;
  46. if(isset($sysDesc[0])) {
  47. $sysDesc = $sysDesc[0];
  48. foreach($value['interfaces'] as $key => $ifSearch){
  49. if(preg_match("|$key|", $sysDesc)){
  50. $search = $ifSearch;
  51. break;
  52. }
  53. }
  54. if(!$search) $this->output->writeln("Can't find any rule on $cfg_file matching the CMTS sysDesc : $sysDesc");
  55. } else {
  56. $this->output->writeln("Can't read OID_system_description of CMTS");
  57. }
  58. foreach($interfaces as $index => $name) {
  59. $countIfs++;
  60. $data = array('name' => $name, 'if_index' => $index, 'tr' => false,'rf' => false, 'cm' => false, 'upstream' => false, 'downstream' => false);
  61. $match = false;
  62. if($search) {
  63. foreach($search as $skey => $values){
  64. if(preg_match("|$skey|", $name)){
  65. foreach($values as $k){ $data[$k] = true; }
  66. $match = true;
  67. break;
  68. }
  69. }
  70. }
  71. if(!$match) $data['tr'] = true;
  72. $dataCached[$index] = $data;
  73. }
  74. $this->setData($key_cmts_scan, $dataCached, true);
  75. /* Fin de bloqueo */
  76. $this->removeLock($this->flag);
  77. $fin = microtime(true);
  78. $time = $fin - $inicio;
  79. $this->output->writeln("Tiempo: $time segundos / Cantidad Interfaces: {$countIfs}");
  80. }
  81. }