Advertisement
Guest User

Untitled

a guest
Sep 21st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. <?php
  2. namespace Your\Namespace;
  3.  
  4. use Exception;
  5. use PEAR2\Net\RouterOS\Client as rosClient;
  6. use PEAR2\Net\RouterOS\Request;
  7. use PEAR2\Net\RouterOS\Response;
  8. use Symfony\Component\Console\Command\Command;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11.  
  12.  
  13. class RouterCommand extends Command
  14. {
  15. /** @var rosClient */
  16. private $RouterOSclient;
  17. private $keys;
  18. private $counter;
  19. private $interfaces;
  20.  
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('router:run')
  25. ->setDescription('Collect and dump router interface stats');
  26. }
  27.  
  28. private function setup()
  29. {
  30. $host = 'your host';
  31. $user = 'your user';
  32. $pass = 'your password';
  33.  
  34. try {
  35. $this->RouterOSclient = new rosClient($host, $user, $pass);
  36. } catch (Exception $e) {
  37. die('/');
  38. }
  39.  
  40. $this->keys = array(
  41. 'rx-bits-per-second',
  42. 'tx-bits-per-second',
  43. 'rx-packets-per-second',
  44. 'tx-packets-per-second',
  45. );
  46.  
  47. $this->interfaces = [
  48. 'ether1',
  49. 'ether2',
  50. 'ether3',
  51. 'ether4',
  52. 'ether5',
  53. 'wlan1',
  54. 'wlan2',
  55. 'bridge',
  56. ];
  57.  
  58. $this->counter = 0;
  59. }
  60.  
  61. protected function execute(InputInterface $input, OutputInterface $output)
  62. {
  63. $this->setup();
  64.  
  65. $request = new Request('/interface/monitor-traffic interface=' . implode(',', $this->interfaces));
  66.  
  67. $request->setTag('stats');
  68.  
  69. $this->RouterOSclient->sendAsync($request, array($this, 'responseHandler'));
  70.  
  71. $this->RouterOSclient->loop(30);
  72.  
  73. }
  74.  
  75.  
  76. public function responseHandler(Response $response)
  77. {
  78. foreach ($response as $key => $value) {
  79. if (!in_array($key, $this->keys)) {
  80. continue;
  81. }
  82.  
  83. var_dump($key, $value);
  84.  
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement