Hi?
Could some one show how using /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
Hi?
Could some one show how using /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
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
//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
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);
}
});
}
}
When you say “nothing happens”, what do you mean? It doesn’t compile, or it compiles, but it doesn’t output anything?
nothing output
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:
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
Opps. My fault then.
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)
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
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.
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/mikrotik-java/blob/master/src/main/java/me/legrange/mikrotik/ResponseListener.java
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?
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.
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.
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.
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.
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”.
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
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.