Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Your\Namespace;
- use Exception;
- use PEAR2\Net\RouterOS\Client as rosClient;
- use PEAR2\Net\RouterOS\Request;
- use PEAR2\Net\RouterOS\Response;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- class RouterCommand extends Command
- {
- /** @var rosClient */
- private $RouterOSclient;
- private $keys;
- private $counter;
- private $interfaces;
- protected function configure()
- {
- $this
- ->setName('router:run')
- ->setDescription('Collect and dump router interface stats');
- }
- private function setup()
- {
- $host = 'your host';
- $user = 'your user';
- $pass = 'your password';
- try {
- $this->RouterOSclient = new rosClient($host, $user, $pass);
- } catch (Exception $e) {
- die('/');
- }
- $this->keys = array(
- 'rx-bits-per-second',
- 'tx-bits-per-second',
- 'rx-packets-per-second',
- 'tx-packets-per-second',
- );
- $this->interfaces = [
- 'ether1',
- 'ether2',
- 'ether3',
- 'ether4',
- 'ether5',
- 'wlan1',
- 'wlan2',
- 'bridge',
- ];
- $this->counter = 0;
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $this->setup();
- $request = new Request('/interface/monitor-traffic interface=' . implode(',', $this->interfaces));
- $request->setTag('stats');
- $this->RouterOSclient->sendAsync($request, array($this, 'responseHandler'));
- $this->RouterOSclient->loop(30);
- }
- public function responseHandler(Response $response)
- {
- foreach ($response as $key => $value) {
- if (!in_array($key, $this->keys)) {
- continue;
- }
- var_dump($key, $value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement