Community discussions

MikroTik App
 
chbla
just joined
Topic Author
Posts: 16
Joined: Mon Dec 31, 2018 2:52 pm

DHCP Lease script - execution order wrong?

Sun Apr 21, 2019 10:54 pm

Hi there,

I've got a basic lease script to detect if my phone is present:

:global chMAC "F4:xxx";
:global leaseBound
:global leaseServerName
:global leaseActMAC
:global leaseActIP

:if ($leaseBound =1) do={
 :if ([$leaseActMAC] = $chMAC)  do={
    :log info "assign CH";
    /tool fetch http-method=put url="http://192.168.1.25:8080/rest/items/pres_ch/state" mode=http http-header-field="content-type: text/plain" http-data="ON"
 }
}

:if ($leaseBound =0) do={
 :if ([$leaseActMAC] = $chMAC) do={
   :log info "deassign CH";
   /tool fetch http-method=put url="http://192.168.1.25:8080/rest/items/pres_ch/state" mode=http http-header-field="content-type: text/plain" http-data="OFF"
 }
}

However, I noticed the following entries in my log:

653 20:57:42 dhcp,info defconf deassigned 192.168.1.52 from F4:xxx
654 20:57:42 dhcp,info defconf assigned 192.168.1.52 to F4:xxx
655 20:57:42 script,info assign CH 
656 20:57:42 script,info deassign CH

As you can see, the script is executed in reverse order, for the statements, rendering my result
useless.

Is there any way to serialize this?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3297
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: DHCP Lease script - execution order wrong?

Mon Apr 22, 2019 12:39 am

Not a direct respond to your problem, but I du use Splunk to monitor this.
To see If a device is online one of the Splunk view looks at the DNS request. Nearly all devices request many DNS every minute. So if a device as at least on request last 5 min I mark it as online.

See more in link in my signatur.
 
JJCinAZ
Member
Member
Posts: 475
Joined: Fri Oct 22, 2004 8:03 am
Location: Tucson, AZ

Re: DHCP Lease script - execution order wrong?

Mon Apr 22, 2019 1:59 am

Yeah, not surprising there's a race condition there and it would be difficult for the DHCP server to serialize since it would have to have a single pipeline or a pipeline per-mac address which would be a lot of complication.

You could try writing the script so that it doesn't care if it's an assignment or deassignment. Instead, every time the script runs, just query the lease table and see if your target MAC is present or not and then call your rest API with the results; and, maybe, the last-seen property. Worst case, you get two calls that your device is present. With the last-seen value, you can debounce the two calls.
 
chbla
just joined
Topic Author
Posts: 16
Joined: Mon Dec 31, 2018 2:52 pm

Re: DHCP Lease script - execution order wrong?

Mon Apr 22, 2019 9:25 am

I would prefer to not have a separate app like Splunk to do this, I guess the leases are accurate enough to tell if someone is there or not.
There is an additional problem with timeouts though, the phones switch into powersave after a certain amount of time. They keep coming back though.

@JJCinAz thanks for the pointers! Do you mean querying the lease table in this script or from the outside?
Can you tell me how to do that? I don't yet have an overview of all the functions, or is there an API as well?
 
JJCinAZ
Member
Member
Posts: 475
Joined: Fri Oct 22, 2004 8:03 am
Location: Tucson, AZ

Re: DHCP Lease script - execution order wrong?

Mon Apr 22, 2019 7:23 pm

Here's a quick try at it:
# mac-address is case sensitive -- use upper only
:local x [/ip dhcp-server lease find where mac-address="20:3A:07:F2:B6:3F"]
:if ([:len $x] = 0) do={
    # not found
    /tool fetch http-method=put url="http://192.168.1.25:8080/rest/items/pres_ch/state" \
      mode=http http-header-field="content-type: text/plain" http-data="OFF"
} else={
    # found -- assume one only
    :local ls [/ip dhcp-server lease get $x last-seen]
    /tool fetch http-method=put url="http://192.168.1.25:8080/rest/items/pres_ch/state" \
      mode=http http-header-field="content-type: text/plain" http-data=("ON:".$ls)
}
 
chbla
just joined
Topic Author
Posts: 16
Joined: Mon Dec 31, 2018 2:52 pm

Re: DHCP Lease script - execution order wrong?

Mon Apr 22, 2019 8:43 pm

Thanks a lot! I will try this within the next days and report back :)
 
chbla
just joined
Topic Author
Posts: 16
Joined: Mon Dec 31, 2018 2:52 pm

Re: DHCP Lease script - execution order wrong?

Sat Apr 27, 2019 11:06 am

Here's a quick try at it:
# mac-address is case sensitive -- use upper only
:local x [/ip dhcp-server lease find where mac-address="20:3A:07:F2:B6:3F"]
:if ([:len $x] = 0) do={
    # not found
    /tool fetch http-method=put url="http://192.168.1.25:8080/rest/items/pres_ch/state" \
      mode=http http-header-field="content-type: text/plain" http-data="OFF"
} else={
    # found -- assume one only
    :local ls [/ip dhcp-server lease get $x last-seen]
    /tool fetch http-method=put url="http://192.168.1.25:8080/rest/items/pres_ch/state" \
      mode=http http-header-field="content-type: text/plain" http-data=("ON:".$ls)
}
It seems as if this always returns the found clause - probably because the entry exists but there is no assignment?
I'm yet learning how to properly script
 
chbla
just joined
Topic Author
Posts: 16
Joined: Mon Dec 31, 2018 2:52 pm

Re: DHCP Lease script - execution order wrong?

Sat Apr 27, 2019 11:33 am

Nevermind, it works if I change it to find where active-mac-address.
Thanks! :)

Who is online

Users browsing this forum: Bing [Bot] and 46 guests