Page 1 of 1
DHCP Lease script - execution order wrong?
Posted: Sun Apr 21, 2019 10:54 pm
by chbla
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?
Re: DHCP Lease script - execution order wrong?
Posted: Mon Apr 22, 2019 12:39 am
by Jotne
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.
Re: DHCP Lease script - execution order wrong?
Posted: Mon Apr 22, 2019 1:59 am
by JJCinAZ
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.
Re: DHCP Lease script - execution order wrong?
Posted: Mon Apr 22, 2019 9:25 am
by chbla
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?
Re: DHCP Lease script - execution order wrong?
Posted: Mon Apr 22, 2019 7:23 pm
by JJCinAZ
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)
}
Re: DHCP Lease script - execution order wrong?
Posted: Mon Apr 22, 2019 8:43 pm
by chbla
Thanks a lot! I will try this within the next days and report back
![Smile :)](./images/smilies/icon_smile.gif)
Re: DHCP Lease script - execution order wrong?
Posted: Sat Apr 27, 2019 11:06 am
by chbla
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
Re: DHCP Lease script - execution order wrong?
Posted: Sat Apr 27, 2019 11:33 am
by chbla
Nevermind, it works if I change it to find where active-mac-address.
Thanks!
![Smile :)](./images/smilies/icon_smile.gif)