Community discussions

MUM Europe 2020
 
kaplunalex
just joined
Topic Author
Posts: 6
Joined: Mon May 12, 2014 12:04 pm

API Java monitor-traffic

Tue May 13, 2014 3:53 pm

Hi?

Could some one show how using

ros code

/interface/monitor-traffic
=interface=ether3-master-local
write out data, I'm looking in WiKi and don't understand it. I want to get rx-bits-per-second and tx-bits-per-second
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Java monitor-traffic

Tue May 13, 2014 8:07 pm

Each time a sample is gathered, it's given to the client as a reply in the form of
!re
=rx-bits-per-second=<some number>
=tx-bits-per-second=<some number>
<more attributes>
How exactly do you extract the values you want depends on the client you're using.

With this client for example, you can access it the same way you'd access any other map object. Since this is a continuous command, you'd need to eventually cancel it if you want it to stop, or just process responses asynchronously (check out the Asynchronous commands in the readme).
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
kaplunalex
just joined
Topic Author
Posts: 6
Joined: Mon May 12, 2014 12:04 pm

Re: API Java monitor-traffic

Wed May 14, 2014 5:35 pm

Each time a sample is gathered, it's given to the client as a reply in the form of
!re
=rx-bits-per-second=<some number>
=tx-bits-per-second=<some number>
<more attributes>
How exactly do you extract the values you want depends on the client you're using.

With this client for example, you can access it the same way you'd access any other map object. Since this is a continuous command, you'd need to eventually cancel it if you want it to stop, or just process responses asynchronously (check out the Asynchronous commands in the readme).
I'm only start learning Java and couldn't understand how this working. Could you give me working example with using =rx-bits-per-second= pls
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Java monitor-traffic

Wed May 14, 2014 5:53 pm

//Near the top
import me.legrange.mikrotik;

//Within a method in your class
ApiConnection con = ApiConnection.connect("10.0.1.1"); // connect to router
con.login("admin","password"); // log in to router
String tag = con.execute("/interface/monitor-traffic numbers=ether3-master-local .proplist=tx-bits-per-second,rx-bits-per-second", 
      new ResultListener() {

            public void receive(Map<String, String> result) {
                System.out.println(result);
            }

        }
  );
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
kaplunalex
just joined
Topic Author
Posts: 6
Joined: Mon May 12, 2014 12:04 pm

Re: API Java monitor-traffic

Wed May 14, 2014 6:03 pm

//Near the top
import me.legrange.mikrotik;

//Within a method in your class
ApiConnection con = ApiConnection.connect("10.0.1.1"); // connect to router
con.login("admin","password"); // log in to router
String tag = con.execute("/interface/monitor-traffic numbers=ether3-master-local .proplist=tx-bits-per-second,rx-bits-per-second", 
      new ResultListener() {

            public void receive(Map<String, String> result) {
                System.out.println(result);
            }

        }
  );
I try to use it but nothing happens

ros code

import java.util.Map;

import me.legrange.mikrotik.MikrotikApiException;
import me.legrange.mikrotik.ResultListener;

public class Ether3 extends Connect {

	public static void main(String... args) throws Exception {
		Ether3 ex = new Ether3();
		ex.connect();
		ex.test();
		ex.disconnect();
	}

	private void test() throws MikrotikApiException, InterruptedException {
		String tag = con.execute("/interface/monitor-traffic numbers=ether6-5th-floor .proplist=tx-bits-per-second,rx-bits-per-second",
						new ResultListener() {

							public void receive(Map<String, String> result) {
								System.out.println(result);
							}

						});
	}
}
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Java monitor-traffic

Wed May 14, 2014 6:18 pm

When you say "nothing happens", what do you mean? It doesn't compile, or it compiles, but it doesn't output anything?
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
kaplunalex
just joined
Topic Author
Posts: 6
Joined: Mon May 12, 2014 12:04 pm

Re: API Java monitor-traffic

Wed May 14, 2014 6:22 pm

When you say "nothing happens", what do you mean? It doesn't compile, or it compiles, but it doesn't output anything?
nothing output
 
legrang
just joined
Posts: 22
Joined: Wed Nov 03, 2010 4:05 pm
Location: South Africa
Contact:

Re: API Java monitor-traffic

Fri May 16, 2014 12:34 pm

The following code works for me:
String id = con.execute("/interface/monitor-traffic interface=wlan1 return tx-bits-per-second,rx-bits-per-second", new ResultListener() {

           public void receive(Map<String, String> result) {
               System.out.println(result);
           }


        });

Some notes:
* I specify the interface with "interface=". I'm not sure that "numbers=" will work.
* I specify the fields I want to receive with a "return" statement. The reason for this is a bit intricate and is to do with how the RouterOS API works.

You can take a look at the API examples on the GitHub page for an examples of the return statement and for asynchronous API use. If you still struggle, please PM me or log an issue on the GitHub page.

Gideon
Last edited by legrang on Fri May 16, 2014 12:40 pm, edited 1 time in total.
Gideon le Grange
RouterOS Java API: https://github.com/GideonLeGrange/mikrotik-java
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Java monitor-traffic

Fri May 16, 2014 12:39 pm

* I specify the interface with interface=. I'm not sure if numbers= will work.
Opps. My fault then.
* I specify the fields I want to receive with a "return" statement. The reason for this is a bit intricate and is to do with how the RouterOS API works.
Yeah, about that... you're already accepting ".id", and ".proplist" is just another argument name... Why introduce a new keyword? Seems unnecessary.
(I think I know enough about Java and the API protocol, so hit me with the non-dumbed down version of the explanation)
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
legrang
just joined
Posts: 22
Joined: Wed Nov 03, 2010 4:05 pm
Location: South Africa
Contact:

Re: API Java monitor-traffic

Fri May 16, 2014 6:16 pm

Yeah, about that... you're already accepting ".id", and ".proplist" is just another argument name... Why introduce a new keyword? Seems unnecessary.
(I think I know enough about Java and the API protocol, so hit me with the non-dumbed down version of the explanation)
Clearly you know the API protocol, but a big point to making an API library is to isolate developers from unnecessary details.

My idea was to make the Java API as easy to use as possible to somebody who understands the Mikrotik command line, without needing them to understand the Mikrotik API syntax. So if a user for example knows he would do a "/ip address print" command on the command line, it is a very small jump to get to con.execute("/ip/address/print"). The "where" syntax already in the command line fit into this naturally, but there is no ".proplist" parameter in the command line syntax, so I needed to added a keyword that looked "natural". This may be a mistake, but I think it is a small one. Somebody who knows Mikrotik command line syntax and Java can read the README on GitHub and get going without digging into API internals.

Adding ".proplist=" to the command passed to the API does work, I checked this after your post, so you don't have to use "return". I brought it up because I was unsure if it would be passed correctly, but it does.

What your post did make me realise is that there is definitely an issue with error handling when using the API asynchronously (your "numbers=" should cause an error), and I probably need to improve the documentation a lot.

Gideon
Gideon le Grange
RouterOS Java API: https://github.com/GideonLeGrange/mikrotik-java
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Java monitor-traffic

Fri May 16, 2014 6:25 pm

Adding ".proplist=" to the command passed to the API does work, I checked this after your post, so you don't have to use "return". I brought it up because I was unsure if it would be passed correctly, but it does.
Oh. So it's just syntax sugar? That's... OK... I guess... as long as MikroTik don't decide to introduce a "return" argument with its own semantics. I thought maybe you were doing some additional work behind the scenes (which would be a legitimate reason to add a keyword or a dedicated method), but couldn't figure out what that might be.
What your post did make me realise is that there is definitely an issue with error handling when using the API asynchronously (your "numbers=" should cause an error), and I probably need to improve the documentation a lot.
Maybe add a new method to the listener that will get error replies? If mandatory, it would make users more cautious with their error handling... which tends to be a good thing. That, or perhaps adjust the listener to accept a Response, and adjust the Response to contain the type.

(And most importantly - send all kinds of replies to the listener)
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
legrang
just joined
Posts: 22
Joined: Wed Nov 03, 2010 4:05 pm
Location: South Africa
Contact:

Re: API Java monitor-traffic

Fri May 16, 2014 8:44 pm

Maybe add a new method to the listener that will get error replies? If mandatory, it would make users more cautious with their error handling... which tends to be a good thing. That, or perhaps adjust the listener to accept a Response, and adjust the Response to contain the type.

(And most importantly - send all kinds of replies to the listener)
That already exists. You can pass an implementation of ResponseListener (subclass of ResultsListener) and will receive errors and command completion information. It isn't mandatory, but most likely should be though, or at least documented :( https://github.com/GideonLeGrange/mikro ... tener.java
Gideon le Grange
RouterOS Java API: https://github.com/GideonLeGrange/mikrotik-java
 
hugojammal
just joined
Posts: 9
Joined: Thu Jun 05, 2014 1:35 am

Re: API Java monitor-traffic

Sun Jun 29, 2014 11:24 pm

Hello Legrand.

Your API for JAVA works very well conglatulations.

I'm developing one JAVA application to manage users in mikrotik.

when I read for example (/ip/hotspot/user/print), your API returns one string containing all informations about the users.

I want to get this string and make java objects with the attributes.

Can you help?
 
legrang
just joined
Posts: 22
Joined: Wed Nov 03, 2010 4:05 pm
Location: South Africa
Contact:

Re: API Java monitor-traffic

Mon Jun 30, 2014 1:34 pm

when I read for example (/ip/hotspot/user/print), your API returns one string containing all informations about the users.

I want to get this string and make java objects with the attributes.

Can you help?
The API does not return one String. It returns a List containing Maps for the individual results. So if you have two users, then /ip/hotspot/user/print will return a List<Map<String, String>> containing two maps, one per user. The individual attributes for each user are accessible using the specific map instance's accessor methods.

If you want the data in Java objects, you need to construct the objects to you liking, extract the data from the Maps for the specific results, and set the data in your objects. You need to implement this the way you want it.
Gideon le Grange
RouterOS Java API: https://github.com/GideonLeGrange/mikrotik-java
 
hugojammal
just joined
Posts: 9
Joined: Thu Jun 05, 2014 1:35 am

Re: API Java monitor-traffic

Thu Jul 10, 2014 4:45 am

Legrand.

Tanks for your support.

i'm trying to implement your responselistener to monitor the currents actives hotspot users.

based on your example 5 i got the sucessfull with MONITOR commands in mikrotik.

do you know a way to continuous check the active hotspot user like the responselistener?

the simple solution is make a "infinite" loop to execute the con.execute("/ip/hotspot/active/print").
but i believe that there are one more intelligent way to do this job.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Java monitor-traffic

Thu Jul 10, 2014 1:58 pm

@hugojammal

Just to make sure we're on the same page... This is not LeGrange's API client... This one is.


If you need to run a continuous command, see the asynchronous commands example. In particular, to monitor new users as they log in, you can use the "print" with a "follow" argument. That way, as soon as someone logs in, you'll get a new !re reply. The command will never finish on its own.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
hugojammal
just joined
Posts: 9
Joined: Thu Jun 05, 2014 1:35 am

Re: API Java monitor-traffic

Thu Jul 10, 2014 6:53 pm

@boen_robot

We are on the same page :)

I will try. Because is a little confuse to me, Actually the API returns a more consistent java object than the !re from mikrotik.

I want to compare the list of active users with the inserted users.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Java monitor-traffic

Thu Jul 10, 2014 7:04 pm

I want to compare the list of active users with the inserted users.
Inserted where?


Whatever the case... within the listener, you'll get the new logged in user, and you can search it within the "inserted users".
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
hugojammal
just joined
Posts: 9
Joined: Thu Jun 05, 2014 1:35 am

Re: API Java monitor-traffic

Fri Jul 11, 2014 3:19 am

lets starts from the beginner.
Actually I use my mikrotik as a hotspot in my family's hotel.
We have contracted one hotspot supplier that installed one mikrotik and started to charge us every month.
When I understood that what this supplier "sells" its only a API based software to control the mikrotik i decided to search freeware softwares.
I found some mk user manager, but always a complete solution and dedicated to the ISP companies. So I decided to develop my own user manager software.
Basically it has to ADD user, Modify user, show the current inserted users and inform who is active (based on the current inserted users). The second step is implement one check-out date that automatically remove the user.

Answering your question. I ran the ip/hotspot/user/print and save it one database, the ADD insert the user both in Mk and the database. now i need to compare who is active based on my user database.

the listener works good to inform new active users, but when one logoff is made the listener doesn't recognize it.

Do you have some tricks to work with this issue?

tankz
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API Java monitor-traffic

Fri Jul 11, 2014 11:15 am

the listener works good to inform new active users, but when one logoff is made the listener doesn't recognize it.

Do you have some tricks to work with this issue?
Oh, so detecting a log off is needed too... Then try using the command "/ip/hotspot/user/active/listen" with no arguments. This should tell you not only when a user first logs into the hotspot, but also when they log off, either explicitly, or because their session has expired. A logged off user should be distinguishable from its logged in counterpart via the property ".dead", which will be set (and have a value of "yes") when the user is not active anymore.
Basically it has to ADD user, Modify user, show the current inserted users and inform who is active (based on the current inserted users).
But... you can get all hotspot users from "/ip/hotspot/user/print", and all active ones with a (sync) "/ip/hotspot/user/active/print". No need to look up a database, which may theoretically be out of date with the router.

There's an easy way to also avoid the DB lookup when removing users too (=> make your management less error prone). Upon creating the hotspot user, also create a scheduler script that will remove it, and set said scheduler script to run when the guest leaves, and remove itself at the end. That way, even if your DB goes down, the only problem that will create is that no new hotspot users would be made (easily), but the current ones will work and finish as usual.

If the hotel guest extends their visit? Modify the time of the scheduler script (which you'll previously name after the hotspot username, in order to easily target it), and you're set.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6283
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: API Java monitor-traffic

Fri Jul 11, 2014 12:17 pm

user-manager is RADIUS server implementation. You are free to get any RADIUS implementation that will do all of this for you, for example FreeRADIUS server.

just my 2cents, so you do not re-invent the wheel again.
 
hugojammal
just joined
Posts: 9
Joined: Thu Jun 05, 2014 1:35 am

Re: API Java monitor-traffic

Sun Aug 03, 2014 9:25 pm

Scanning for projects...

Some problems were encountered while building the effective model for me.legrange:mikrotik:jar:1.1.7-SNAPSHOT
'build.plugins.plugin.version' for org.apache.maven.plugins:maven-gpg-plugin is missing. @ line 72, column 21

It is highly recommended to fix these problems because they threaten the stability of your build.

For this reason, future Maven versions might no longer support building such malformed projects.


How can I solve this problem?
 
legrang
just joined
Posts: 22
Joined: Wed Nov 03, 2010 4:05 pm
Location: South Africa
Contact:

Re: API Java monitor-traffic

Wed Oct 08, 2014 7:27 am

Scanning for projects...

Some problems were encountered while building the effective model for me.legrange:mikrotik:jar:1.1.7-SNAPSHOT
'build.plugins.plugin.version' for org.apache.maven.plugins:maven-gpg-plugin is missing. @ line 72, column 21

It is highly recommended to fix these problems because they threaten the stability of your build.

For this reason, future Maven versions might no longer support building such malformed projects.


How can I solve this problem?
Pull the latest code from GitHub, it is fixed.
Gideon le Grange
RouterOS Java API: https://github.com/GideonLeGrange/mikrotik-java
 
hachemx6
just joined
Posts: 1
Joined: Mon Jan 22, 2018 5:51 pm

Re: API Java monitor-traffic

Mon Jan 22, 2018 6:11 pm

how to get uptime for registered users api java and thank you
 
jharahuld
just joined
Posts: 1
Joined: Mon Jul 16, 2018 9:22 am

Re: API Java monitor-traffic

Mon Jul 16, 2018 9:27 am

When you say "nothing happens", what do you mean? It doesn't compile, or it compiles, but it doesn't output anything?
nothing output
Its an interactive command u have to use once keyword to execute that command command will be :
/interface/monitor-traffic interface="10th Floor" once return tx-bits-per-second,rx-bits-per-second
working in mikrotik v3 java api
 
dieler
just joined
Posts: 2
Joined: Mon Dec 04, 2017 1:48 pm

Re: API Java monitor-traffic

Tue Oct 16, 2018 4:59 am

im new in java and i have the same issue
	public void Monitors() throws Exception
	{
		ApiConnection con = ApiConnection.connect(DatosRouter.ip);
		try 
		{
			con.login(DatosRouter.user, DatosRouter.password);
			String id = con.execute("/interface/monitor-traffic interface=ether1 return tx-bits-per-second,rx-bits-per-second",
					new 
		        public void receive(Map<String, String> result) {
		        	
		            System.out.println(result);
		        }

		           public void error(MikrotikApiException e) {
		               System.out.println("An error occurred: " + e.getMessage());
		           }

		           public void completed() {
		                System.out.println("Asynchronous command has finished"); 
		           }
		     });
			System.out.println(id);
		} 
		catch (Exception e) 
		{
		 e.printStackTrace();
		}
	
	
	}
executing the method and nothing happens
what im doing wrong? help... :(
public abstract class test {

	public static void main(String[] args) throws Exception {
		BeanMonitor beanMonitor = new BeanMonitor();
		beanMonitor.Monitors();

	}

}
 
dieler
just joined
Posts: 2
Joined: Mon Dec 04, 2017 1:48 pm

Re: API Java monitor-traffic

Tue Oct 16, 2018 5:24 am

When you say "nothing happens", what do you mean? It doesn't compile, or it compiles, but it doesn't output anything?
nothing output
Try this.. this works for me


package examples;

import java.util.Map;
import me.legrange.mikrotik.MikrotikApiException;
import me.legrange.mikrotik.ResultListener;

/**
 * Example 4: Asynchronous results. Run a command and receive results for it asynchronously with a ResultListener
 *
 * @author gideon
 */
public class AsyncCommand extends Example {

    public static void main(String... args) throws Exception {
        AsyncCommand ex = new AsyncCommand();
        ex.connect();
        ex.test();
        ex.disconnect();
    }
  
    private void test() throws MikrotikApiException, InterruptedException {
       String id = con.execute("/interface/monitor-traffic interface=ether1", new ResultListener() {
           private int prev = 0;

           public void receive(Map<String, String> result) {
               System.out.println(result);
/*               int val = Integer.parseInt(result.get("signal-strength"));
               String sym = (val == prev) ? " " : ((val < prev) ? "-" : "+");
               System.out.printf("%d %s\n", val, sym);
               prev = val;
  */          }

           @Override
           public void error(MikrotikApiException ex) {
               throw new RuntimeException(ex.getMessage(), ex);
           }

           @Override
           public void completed() {
           }


        });
       // let it run for 60 seconds 
       Thread.sleep(60000);
       con.cancel(id);
    }
}


Who is online

Users browsing this forum: No registered users and 8 guests