PingCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace CmtsBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class PingCommand extends ContainerAwareCommand
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('ping')
  14. ->setDescription('Ping to host and return SLA metrics')
  15. ->setHelp('Se requieren parámetros para poder realizar la correcta consulta.')
  16. ->setDefinition(array(
  17. new InputArgument('ip', InputArgument::OPTIONAL,"IP"),
  18. new InputOption('timeout', null, InputOption::VALUE_NONE,"Timeout", null),
  19. new InputOption('interval', null, InputOption::VALUE_NONE,"Interval", null),
  20. new InputOption('count', null, InputOption::VALUE_NONE,"Count", null)
  21. ))
  22. ;
  23. }
  24. /**
  25. * @param InputInterface $input
  26. * @param OutputInterface $output
  27. */
  28. protected function execute(InputInterface $input, OutputInterface $output)
  29. {
  30. $ip = $input->getArgument('ip');
  31. if(!$ip) return $output->writeln(json_encode(array('message' => 'IP is required')));
  32. $timeout = $input->getOption('timeout');
  33. $interval = $input->getOption('interval');
  34. $count = $input->getOption('count');
  35. $data = $this->ping($ip, $timeout, $interval, $count);
  36. if(!is_array($data)) {
  37. $data = array();
  38. }
  39. $output->writeln(json_encode($data));
  40. }
  41. function ping($ip, $timeout = 1, $int = 0.5, $i = 5) {
  42. if(! $timeout ) $timeout = 1;
  43. if(! $int ) $int = 0.5;
  44. if(! $i ) $i = 5;
  45. /* ICMP ping packet with a pre-calculated checksum */
  46. $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
  47. $result = array();
  48. $socket = socket_create(AF_INET, SOCK_RAW, 1);
  49. socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
  50. socket_connect($socket, $ip, null);
  51. $n = $c = $total = 0;
  52. $jitter = array();
  53. $last = null;
  54. for($a = 1; $a <= $i; $a++) {
  55. $ts = microtime(true);
  56. socket_send($socket, $package, strLen($package), 0);
  57. if (socket_read($socket, 255)) {
  58. $r = ((microtime(true) - $ts) / 2) * 1000;
  59. if(is_null($last)) {
  60. $last = $r;
  61. } else {
  62. $jitter[] = abs(($last - $r));
  63. }
  64. $total += $r;
  65. $result[] = $r;
  66. $c++;
  67. } else {
  68. $result[] = false;
  69. $n++;
  70. }
  71. if ($int< 1) {usleep($int * 1000000);}else{sleep($int);}
  72. }
  73. socket_close($socket);
  74. $data = array();
  75. if($c > 0) {
  76. $data['latency'] = round(($total) / $c,2);
  77. }
  78. if($jitter) {
  79. $data['jitter'] = round(array_sum($jitter) / count($jitter),2);
  80. }
  81. if($i > 0) {
  82. $data['loss'] = round(($n / $i) * 100,2);
  83. }
  84. return $data;
  85. }
  86. }