BaseCommand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. abstract class BaseCommand extends ContainerAwareCommand
  8. {
  9. /**
  10. * @var CollectorInterface
  11. */
  12. protected $collector;
  13. /**
  14. * @var string
  15. */
  16. protected $pathLock = "/var/run/flowdat3/stats";
  17. protected function execute(InputInterface $input, OutputInterface $output)
  18. {
  19. $this->output = $output;
  20. $this->setCollector();
  21. return;
  22. }
  23. protected function setCollector()
  24. {
  25. $this->collector = $this->getContainer()->get("redis");
  26. }
  27. /**
  28. * @param string $key
  29. * @return array
  30. */
  31. protected function getData($key, $output = false)
  32. {
  33. return $this->collector->get($key, $output);
  34. }
  35. /**
  36. * @param string $key
  37. * @param array $data
  38. */
  39. protected function setData($key, $data, $output = false)
  40. {
  41. $this->collector->set($key, $data, $output);
  42. }
  43. /**
  44. * @param string $key
  45. * @param string $data
  46. */
  47. protected function getString($key, $output = false)
  48. {
  49. return $this->collector->getString($key, $output);
  50. }
  51. /**
  52. * @param array $queries
  53. * @return
  54. */
  55. protected function executeQueries($queries)
  56. {
  57. $result = null;
  58. $conn = $this->getContainer()->get('database_connection');
  59. foreach ($queries as $query)
  60. {
  61. $result = $conn->query($query);
  62. }
  63. $conn->close();
  64. return $result;
  65. }
  66. /**
  67. *
  68. * @param string $flag
  69. * @return boolean
  70. *
  71. * Description: Retorna true si existe el bloqueo. False si no existe bloqueo o no puede determinar la existencia.
  72. */
  73. protected function lock($flag) {
  74. $mypid = getmypid();
  75. $file = $this->pathLock. "/" . $flag;
  76. if (!is_dir($this->pathLock)) {mkdir($this->pathLock, 0777, true);}
  77. if (is_null($flag)) return false;
  78. if (file_exists($file)) {
  79. $handle = fopen($file, "r");
  80. $contenido = fread($handle, filesize($file));
  81. fclose($handle);
  82. $data = explode("\n", $contenido);
  83. if(count($data) == 2) {
  84. if (isset($data[0]) && isset($data[1])) {
  85. $pid = explode("=",$data[0]);
  86. $time = explode("=",$data[1]);
  87. $pid_flag = (int) $pid[1];
  88. $time_flag = (int) $time[1];
  89. if (posix_kill($pid_flag, 0)) {
  90. $now = time();
  91. $lapse = $now - $time_flag;
  92. $limit_time = 60 * 15;
  93. if($lapse > $limit_time) {
  94. print_r("Superado 15 min de ejecución, finalizamos proceso anterior(pid: {$pid_flag}).".PHP_EOL);
  95. if (posix_kill($pid_flag, 9)) {
  96. print_r("Finalizado con éxito".PHP_EOL);
  97. } else {
  98. print_r("Ocurrió un problema, no pudo finalizarse el proceso".PHP_EOL);
  99. }
  100. } else {
  101. print_r("Esperamos el proceso anterior(pid: {$pid_flag}) a que finalice. Tiempo actual de ejecución: {$lapse} segundos.".PHP_EOL);
  102. return true;
  103. }
  104. }
  105. }
  106. }
  107. @unlink($file);
  108. }
  109. $time = time();
  110. $data = "pid={$mypid}\ntime={$time}";
  111. $handle = fopen($file, "w+");
  112. fwrite($handle, $data);
  113. fclose($handle);
  114. return false;
  115. }
  116. /**
  117. *
  118. * @param string $flag
  119. * @return boolean
  120. *
  121. * Description: Retorna true si pudo borrarse el archivo de bloqueo. False en cualquier otro caso.
  122. */
  123. protected function removeLock($flag) {
  124. if (is_null($flag)) return false;
  125. $file = $this->pathLock . "/" . $flag;
  126. unlink($file);
  127. if(!file_exists($file)) return true;
  128. return false;
  129. }
  130. /**
  131. *
  132. * @param string $v
  133. * @return array
  134. *
  135. * Description: Retorna el tipo de valor y el valor mismo de una respuesta SNMP para Octets
  136. */
  137. protected function parseSnmpOctetsValue($v) {
  138. if( $v == '""' || $v == '' ) return array();
  139. $type = substr( $v, 0, strpos( $v, ':' ) );
  140. $value = trim( substr( $v, strpos( $v, ':' ) + 1 ) );
  141. return array(0=>$type,1=>(int)$value);
  142. }
  143. }