In OpenWRT's cli, where can I find/grep WAN remaining lease time?

From OpenWRT's cli, where can I find/grep WAN remaining lease time?

It doesn’t seem to be in the output of ubus call network.interface.wan status , although the iface uptime is there.

In the webif/LUCI, it is shown as “Expires in “ if I understand correctly.

thanks in advance,

davy

LuCI calculates the Expiry based on the interface uptime and leasetime.

2 Likes

And how do you access that from the CLI again?

Perhaps it's me, but I couldn't extrapolate that information from a posting of LuCI code.

1 Like

Ifstatus wan6?

1 Like

I think you mean wan?

But unfortunately, it only shows the length of the initial issued lease, and not the time remaining.

1 Like

LuCI calculates the Expiry based on the interface uptime and leasetime.

If that is the only way to know it, it’s not very reliable, I think. My wan uptime can easily exeed 100 days, and my leasetime is 2 hours. If I loose 30 seconds on each lease renew, the remaining leasetime is off by 5 hours, after 100 days.

2 Likes

Looking in htop at the commandline for ohhcpc, it seems it calls /etc/netifd/dhcpv6.script on renew. That script calls the scripts in /etc/odhcp6c.user.d/ (or /etc/udhcpc.user.d/ for dhcpv4). So you could put a script there, which at least creates a timestamp file for the last renew. Combined with the leasetime you should be done.

2 Likes

That is true. My lease time has fluctuated lately between 1 hour and 2 hours, so it isn’t going to be accurate. My point was to show that it isn’t stored anywhere. It’s guessed at on-the-fly by LuCI.

# ifstatus wan | grep -E "uptime|leasetime"
        "uptime": 596521,
                "leasetime": 7200
root@router:~# echo $(( 7200 - ( 596521 % 7200 ) ))
1079
1 Like

My understanding:

Wan Lease Time

  • DHCP client (BusyBox UDHCPC is Default client)
  • At RENEW, client knows the total lease time
  • Client requests Renew at half-life of initial lease time
  • If not honoured, client will request Renew again at half-life of remaining lease time and will repeat this cycle until Renewal Lease is issued (exponential backoff), or lease expires.

If Renewal fails

  • Client broadcasts a DHCPDISCOVER message.
  • Client will wait for DHCPOFFER and will establish new renewal from DHCPOFFER Server .
  • If client cannot reach a DHCP server ¯\_(ツ)_/¯(can't remember ever encountering this scenario). Maybe link-local addressing?

So answer should be REMAINING_LEASE_Time=(CURRENT_time - RENEW_time)

2 Likes

I created this script in /etc/udhcpc.user.d/01-getexpiry

case "$1" in
        renew|bound)
                [ -n "$lease" ] && echo "$(( $(date +%s) + $lease ))" >/tmp/wanleaseexpiry
        ;;
esac
2 Likes

I’m marking this as the solution - thanks @dave14305 - even though it may not be conceptually 100% accurate - it appears that it is as good/accurate as it (LUCI/OpenWRT/Linux/my-ISP-provider) gets.

I just wanted to aggregate a few things and make them available through my Home Assistant instance. At this point its easy to SNMP-extend this … and graph it over time.

My original/simplest-case thought was that Remaining Time would decrease and at t=0, it would renew… pretty clear that this may not be the case.

This script that you suggest is interesting and instructive - I will try it out,@dave14305 . Also thanks @RuralRoots @_bernd @Mijzelf @lleachii .

Of note, my provider is Charter/Spectrum Cable and my lease time is 86400 sec, 1 day.

This will vary by what your particular ISP sets for their customers. In my case I get a 5 min (300 sec) lease, so my syslog gets spammed every 2.5 min (150 secs) with a Request/Renew (my ISP honours every Request without fail). On the other hand, an ISP I worked for used a server that at certain heavy volume periods would ignore Requests until the load decreased before issuing a Renew and another ISP ignored Requests until the last possible instance before issuing a Renew

You can use logread -e udhcpc over your 24 hr. Lease time to see how your ISP handles it.

1 Like