CmtsInterfaceDescriptionCommand.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 CmtsInterfaceDescriptionCommand extends BaseCmtsCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('cmts:interface:description')
  16. ->setDescription('Obtener Detalles de las 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. new InputOption('save-historic', null, InputOption::VALUE_OPTIONAL, "Send data to StatsD",1)
  25. ))
  26. ;
  27. }
  28. /**
  29. * @param InputInterface $input
  30. * @param OutputInterface $output
  31. */
  32. protected function execute(InputInterface $input, OutputInterface $output)
  33. {
  34. parent::execute($input, $output);
  35. $saveHistoric = (int) $input->getOption('save-historic');
  36. $inicio = microtime(true);
  37. $SNMP = new SNMP($this->cmtsIp, $this->cmtsCommunity);
  38. $library = "use".$this->cmtsSnmpLibrary;
  39. $this->apiSNMP = $SNMP->$library();
  40. /*
  41. ----------------------------------
  42. Columns cmts_interface_description
  43. ----------------------------------
  44. id
  45. server_id
  46. cmts_device_id
  47. updated
  48. index
  49. name
  50. if_type
  51. if_mtu
  52. if_speed
  53. if_phys_address
  54. if_admin_status
  55. if_oper_status
  56. if_last_change
  57. if_in_octets
  58. if_in_ucast_pkts
  59. if_in_nucast_pkts
  60. if_in_discards
  61. if_in_errors
  62. if_in_unknown_protos
  63. if_out_octets
  64. if_out_ucast_pkts
  65. if_out_nucast_pkts
  66. if_out_discards
  67. if_out_errors
  68. if_out_qlen
  69. if_specific
  70. docs_if_sig_qincludes_contention
  71. docs_if_sig_qunerroreds
  72. docs_if_sig_qcorrecteds
  73. docs_if_sig_quncorrectables
  74. docs_if_sig_qext_unerroreds
  75. docs_if_sig_qext_correcteds
  76. docs_if_sig_qext_uncorrectables
  77. cer
  78. extra_data
  79. */
  80. $sigOids = array(1 => 'includes_contention', 2 => 'unerroreds', 3 => 'correcteds', 4 => 'uncorrectables', 8 => 'ext_unerroreds', 9 => 'ext_correcteds', 10 => 'ext_uncorrectables');
  81. $ifEntry = $this->getSNMP("ifEntry","cmtsIfEntry");
  82. /* http://cric.grenoble.cnrs.fr/Administrateurs/Outils/MIBS/?oid=1.3.6.1.2.1.2.2.1 */
  83. $dataIfEntry = array();
  84. foreach($ifEntry as $index => $value) {
  85. list($oid, $ifIndex) = explode(".",$index);
  86. if(!isset($dataIfEntry[$ifIndex])) $dataIfEntry[$ifIndex] = array();
  87. $dataIfEntry[$ifIndex][$oid] = $value;
  88. }
  89. $docsIfSignalQualityTable = $this->getSNMP("docsIfSignalQualityTable","cmtsDocsIfSignalQualityTable");
  90. /* http://oidref.com/1.3.6.1.2.1.10.127.1.1.4.1 */
  91. $dataIfSignal = array();
  92. foreach($docsIfSignalQualityTable as $index => $value) {
  93. list($oid, $ifIndex) = explode(".",$index);
  94. if(!isset($dataIfSignal[$ifIndex])) $dataIfSignal[$ifIndex] = array();
  95. $dataIfSignal[$ifIndex][$oid] = $value;
  96. }
  97. $sendData = $rows = array();
  98. $countIfs = 0;
  99. foreach($dataIfEntry as $ifIndex => $values) {
  100. $countIfs++;
  101. $row = array();
  102. $row[] = "NULL";
  103. $row[] = $this->cmtsServerId;
  104. $row[] = $this->cmtsDeviceId;
  105. $row[] = "'".date("Y-m-d H:i:s")."'";
  106. $row[] = $ifIndex;
  107. (isset($values[2]) && !empty($values[2]))? $row[] = "'{$values[2]}'" : $row[] = "NULL";
  108. for($i = 3; $i <= 22; $i++) {
  109. if($i == 6) {
  110. (isset($values[$i]) && !empty($values[$i]))? $row[] = "'{$values[$i]}'" : $row[] = "NULL";
  111. } else {
  112. (isset($values[$i]) && !empty($values[$i]))? $row[] = $values[$i] : $row[] = "NULL";
  113. }
  114. }
  115. for($i = 1; $i <= 4; $i++) {
  116. if(isset($dataIfSignal[$ifIndex][$i]) && !empty($dataIfSignal[$ifIndex][$i])) {
  117. $v = $row[] = $dataIfSignal[$ifIndex][$i];
  118. $name = $sigOids[$i];
  119. $sendData["{$this->d_s}_if_{$name}_{$ifIndex}"] = "{$v}|g";
  120. } else {
  121. $row[] = "NULL";
  122. }
  123. }
  124. $aux = array();
  125. for($i = 8; $i <= 10; $i++) {
  126. if(isset($dataIfSignal[$ifIndex][$i]) && !empty($dataIfSignal[$ifIndex][$i])) {
  127. $v = $aux[$i] = $row[] = $dataIfSignal[$ifIndex][$i];
  128. $name = $sigOids[$i];
  129. $sendData["{$this->d_s}_if_{$name}_{$ifIndex}"] = "{$v}|g";
  130. } else {
  131. $row[] = "NULL";
  132. }
  133. }
  134. if(isset($aux[8]) && isset($aux[9]) && isset($aux[10])) {
  135. $sum = $aux[8] + $aux[9];
  136. $prom = $aux[10] / $sum;
  137. $v = $row[] = round($prom,6);
  138. $sendData["{$this->d_s}_if_cer_{$ifIndex}"] = "{$v}|g";
  139. } else {
  140. $row[] = "NULL";
  141. }
  142. $row[] = "NULL";
  143. $rows[] = "(".implode(",",$row).")";
  144. }
  145. if($rows) {
  146. $doctrine = $this->getContainer()->get('doctrine.orm.entity_manager');
  147. $conn = $doctrine->getConnection();
  148. $sql = "DELETE FROM `cmts_interface_description` WHERE server_id = {$this->cmtsServerId} AND cmts_device_id = {$this->cmtsDeviceId};";
  149. $conn->query($sql);
  150. $conn->close();
  151. $conn = $doctrine->getConnection();
  152. $sql = "INSERT LOW_PRIORITY IGNORE INTO `cmts_interface_description` () VALUES ". implode(",", $rows).";";
  153. $conn->query($sql);
  154. $conn->close();
  155. }
  156. if($sendData && $saveHistoric) {
  157. $t_start_script = microtime(true);
  158. $statsdService = $this->getContainer()->get('statsd');
  159. $statsdService->send($sendData);
  160. $t_end_script = microtime(true);
  161. $time = $t_end_script - $t_start_script;
  162. print_r("Tiempo de envío al StatsD: {$time} ms / Cantidad: ".count($sendData).PHP_EOL);
  163. }
  164. $this->removeLock($this->flag);
  165. $fin = microtime(true);
  166. $time = $fin - $inicio;
  167. $this->output->writeln("Tiempo: $time segundos / Cantidad Interfaces: {$countIfs}");
  168. }
  169. }