api code for traffic monitoring not error

Dear Boen robot /Mikrotik team
Long time to see; Hope you are doing good.
I try to writing code interface monitor-traffic but found a difficult problem
Due to your advice previous time I can execute this command and get the result.
I am sure this code can run perfectly.
$rate = $client->sendSync(new RouterOS\Request(‘/interface/ethernet/monitor numbers=ether1 once’))->getArgument(‘rate’);
echo ‘RATE:’,$rate;now I try to restructure and ease to my understanding. It has a few problem.
1.<?php use PEAR2\Net\RouterOS; require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar'; $client = new RouterOS\Client('192.168.88.100', 'admin', ''); $srequest = new RouterOS\Request('/interface/ethernet/monitor numbers=ether1 once'); $results= $client->sendSync($srequest->getArgument('rate')); echo 'bps:',$rate; ?>It doesn’t work the error picture in speed_not.jpg
What happen please advise the correct coding?
2. In fact , I need to use the same way with interface monitor-traffic=ether1 command
like this but It doesn’t work again. The error picture is in tx-rx-notwork.jpg
please advise the correct coding.

<?php use PEAR2\Net\RouterOS; require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar'; $client = new RouterOS\Client('192.168.88.100', 'admin', ''); $trequest = new RouterOS\Request('/interface monitor-traffic=ether1 once'); $results= $client->sendSync($trequest->getArgument('rx-bits-per-second',tx-bits-per-second)); echo 'RXbps:',$rx-bits-per-second, 'TXbps:',$tx-bits-per-second; ?>3. In item 2 , I wish to receive the value of rx-bits-per-second' and tx-bits-per-second continuously.

I try to make a graph for long term monitoring. please advise how to get the value continuously in PHP.
It’s possible to request the correct coding.
As my understand , PHP web will be idle timeout after 2-3 minute .
I should start background process such as AJAX or not.
Please advise;
Thank you very Much.
Banatus S
tx-rx-notwork.jpg
speed_not.jpg

Hi.

It’s an order of operations thing. You’re getting the request’s argument, not the responses’.

Perhaps it may make more sense if we rewrite the working example a little bit to include more variables:

$srequest = new RouterOS\Request('/interface/ethernet/monitor numbers=ether1 once');
$responses = $client->sendSync($srequest);
$rate = $responses->getArgument('rate');
echo 'RATE:',$rate;

What happens:

  1. Prepare a request.
  2. Send a request.
  3. Out of the first response, get the attribute “rate”.
  4. Write out its value.

The code you’re trying to run is instead equivalent to:

$srequest = new RouterOS\Request('/interface/ethernet/monitor numbers=ether1 once');
$rateArg = $srequest->getArgument('rate');
$results = $client->sendSync($rateArg);
echo 'bps:',$rate;

i.e.

  1. Prepare a request.
  2. Get the “rate” argument of the request. There’s none, so you get NULL.
  3. Send the value of the “rate” argument, i.e. send NULL… Which is not allowed.
  4. Write out the variable $rate, which BTW is not existent at all (not that it matters, since the script has already terminated due to 3).


    In your second code, you’re doing a similar thing wrong, except it’s even worse, in that you’re trying to get two arguments with one getArgument() call, which is not allowed. Plus, the second one is written out as a series of non-existent constants.
$client->sendSync($trequest->getArgument('rx-bits-per-second',tx-bits-per-second));



  1. In item 2 , I wish to receive the value of rx-bits-per-second’ and tx-bits-per-second continuously.
    I try to make a graph for long term monitoring. please advise how to get the value continuously in PHP.
    It’s possible to request the correct coding.
    As my understand , PHP web will be idle timeout after 2-3 minute .

In that case, you need to EITHER remove the “once” argument, and use sendAsync() instead of sendSync() and write out values continuously OR call the page many times with ajax, with each having a “once” argument.

The former is more efficient, but requires a lot of things on both client side and server side to come together for it to work properly, so I’ll just demonstrate the simpler option.

$trequest = new RouterOS\Request('/interface monitor-traffic=ether1 once');
$result = $client->sendSync($trequest)->current();
echo json_encode(
    array(
        'rx' => $result('rx-bits-per-second'),
        'tx' => $result('tx-bits-per-second')
    )
);

You call the PHP file with that code once every second or however often you need to refresh the value, and parse it using the browser’s JSON.parse() method, and then do what you will with it.



To avoid similar sorts of confusion in the future, I’ve been meaning to make a change in the API client, which I’ve so far been a little hesitant to do, since it’s a breaking change (i.e. some people will need to change their code). I’ve adjusted the code above so that you’re still OK in both the current and new versions. The change in question - rename Response::getArgument() to getProperty(), and only keep getArgument() on requests, making it clear whether you’re dealing with the request or response.

Thank you GURU/Mikrotik
1.About rate checking command,I m sure I can run this command"interface ethernet monitor number= ether1 once" and get the response correnctly.
even I use the old code"
$rate = $client->sendSync(new RouterOS\Request(‘/interface/ethernet/monitor numbers=ether1 once’))->getArgument(‘rate’);

echo ‘RATE:’,$rate;"
It’s working properly.
After I change to your recommendation. It seems a bit problem please see attachment error_rate.jpg
2. About bandwidth checking at port. Im sure that I can run this command " Interface monitor-traffic ether1 once" and get the result. However ,we I use your code"

<?php use PEAR2\Net\RouterOS; require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar'; $client = new RouterOS\Client('192.168.88.100', 'admin', ''); $trequest = new RouterOS\Request('/interface monitor-traffic ether1 once'); $result = $client->sendSync($trequest)->current(); echo json_encode( array( 'rx' => $result('rx-bits-per-second'), 'tx' => $result('tx-bits-per-second') ) ); ?>

The results are {“rx”:null,“tx”:null}.
Please advise;
error_rate.jpg

Thank you GURU/Mikrotik
1.About rate checking command,I m sure I can run this command"interface ethernet monitor number= ether1 once" and get the response correnctly.
even I use the old code"
$rate = $client->sendSync(new RouterOS\Request(‘/interface/ethernet/monitor numbers=ether1 once’))->getArgument(‘rate’);

echo ‘RATE:’,$rate;"
It’s working properly.
After I change to your recommendation. It seems a bit problem please see attachment error_rate.jpg
2. About bandwidth checking at port. Im sure that I can run this command " Interface monitor-traffic ether1 once" and get the result. However ,when I use your code"

<?php use PEAR2\Net\RouterOS; require_once 'PEAR2_Net_RouterOS-1.0.0b4.phar'; $client = new RouterOS\Client('192.168.88.100', 'admin', ''); $trequest = new RouterOS\Request('/interface monitor-traffic ether1 once'); $result = $client->sendSync($trequest)->current(); echo json_encode( array( 'rx' => $result('rx-bits-per-second'), 'tx' => $result('tx-bits-per-second') ) ); ?>

The results are {“rx”:null,“tx”:null}.
Please advise;
error_rate.jpg

Opps. My mistake.

I used “monitor-traffic” as an argument, and you then “fixed” it to a nameless argument, which is not correct either… Serves me right for not copy&pate-ing the previously working code.

Make the request

$trequest = new RouterOS\Request('/interface monitor-traffic interface=ether1 once');