CmtsInterfaceStatsCommand.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 CmtsInterfaceStatsCommand extends BaseCmtsCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('cmts:interface:stats')
  16. ->setDescription('Obtener Stats de 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. $key_cm_scan = "cmts_scan_{$this->d_s}";
  36. $inicio = microtime(true);
  37. $SNMP = new SNMP($this->cmtsIp, $this->cmtsCommunity);
  38. $library = "use".$this->cmtsSnmpLibrary;
  39. $this->apiSNMP = $SNMP->$library();
  40. $dataCached = $this->getData($key_cmts_scan, true);
  41. if(empty($dataCached)) {
  42. $this->output->writeln("Se requiere {$key_cmts_scan}.");
  43. $this->removeLock($this->flag);
  44. return true;
  45. }
  46. $_utilization = $this->getSNMP("docsIfCmtsChannelUtilization","cmtsIfUtilization");
  47. $utilization = array();
  48. foreach($_utilization as $compIndex => $value) {
  49. $params = explode(".",$compIndex);
  50. if($params > 1) {
  51. $index = $params[0];
  52. $aux = implode(".", array_slice($params,1));
  53. } else {
  54. $index = $compIndex;
  55. $aux = 0;
  56. }
  57. $utilization[$index] = array($aux => $value);
  58. }
  59. $microreflection = $this->getSNMP("docsIfSigQMicroreflections","cmtsIfMicroreflection");
  60. $signal = $this->getSNMP("docsIfSigQSignalNoise","cmtsIfSignal");
  61. print_r($utilization);
  62. print_r($microreflection);
  63. print_r($signal);
  64. $cmCached = $this->getData($key_cm_scan, true);
  65. $states = array();
  66. if(empty($cmCached)) {
  67. $this->output->writeln("Se requiere {$key_cm_scan} para obtener cantidad de CM por Interface.");
  68. } else {
  69. foreach($cmCached as $index => $value) {
  70. $up = $value['upInterface'];
  71. $down = $value['downInterface'];
  72. $state = $value['status'];
  73. if(!isset($states[$up])) $states[$up] = array(0 => 0, 1 => 0);
  74. if(!isset($states[$down])) $states[$down] = array(0 => 0, 1 => 0);
  75. $states[$up][$state]++;
  76. $states[$down][$state]++;
  77. }
  78. }
  79. $ifStatsCached = $sendData = array();
  80. $subId = $this->d_s;
  81. $metrics = array("utilization" => "{$subId}_if_utilization_", "microreflection" => "{$subId}_if_microreflection_", "signal" => "{$subId}_if_signal_", "states" => "{$subId}_if_state_");
  82. foreach($dataCached as $index => $interface) {
  83. $stats = array();
  84. foreach($metrics as $data => $metric) {
  85. if(!isset($$data[$index])) continue;
  86. $m = "{$metric}{$index}";
  87. if($data == "signal") {
  88. $v = $$data[$index] * 0.1;
  89. $sendData[$m] = "{$v}|g";
  90. } elseif($data == "utilization") {
  91. foreach($$data[$index] as $aux => $v) {
  92. $sendData["{$m}.{$aux}"] = "{$v}|g";
  93. }
  94. $v = $$data[$index];
  95. } elseif($data == "states") {
  96. $v = $$data[$index];
  97. $on = "{$subId}_if_state_on_{$index}";
  98. $off = "{$subId}_if_state_off_{$index}";
  99. $sendData[$on] = "{$v[1]}|g";
  100. $sendData[$off] = "{$v[0]}|g";
  101. } else {
  102. $v = $$data[$index];
  103. }
  104. $stats[$data] = $v;
  105. }
  106. $ifStatsCached[$index] = $stats;
  107. }
  108. print_r($ifStatsCached);
  109. print_r($sendData);
  110. die;
  111. $dataCached = array();
  112. $countIfs = 0;
  113. $interfaces = $this->getSNMP("docsIfDescription","cmtsIf");
  114. $this->setData($key_cmts_scan, $dataCached, true);
  115. /* Fin de bloqueo */
  116. $this->removeLock($this->flag);
  117. $fin = microtime(true);
  118. $time = $fin - $inicio;
  119. $this->output->writeln("Tiempo: $time segundos / Cantidad Interfaces: {$countIfs}");
  120. }
  121. }