Adding a LuCI Status Page Widget

Hello,
I would like to add a small section to my router's main status page, to display the WAN interface's PTR record if connected and resolvable.

Proper documentation on LuCI seems hard to come by, my understanding so far is:-
I can create a dir called "index" within "/usr/lib/lua/luci/view/admin_status/", place HTML files in there and they will be appended to the view upon access;
These HTML files can break into Lua scripting with percent tags (<% %>);
Certain LuCI libraries can be used within the scripts.

Based upon this, I would like to use the API call luci.sys.net.routes() to obtain the default IPV4 route gateway, which should be the WAN IP address. But my attempts keep failing because either the library or the function doesn't exist.
Am I looking at old API docs, or is something else wrong?

I think it would be quite helpful if you posted your current code as well :slight_smile:

Well I managed to track down a git commit from 2017 that indicated that luci.sys.net.routes() was deprecated, so I have to use luci.ip.routes() or luci.ip.route() instead.
Of course, nothing written in the docs to suggest a problem :roll_eyes:

So, having changed the call I've since made progress, in that I get the default route and the device that it would send from (the WAN side of the router).
Now, how do I get IP addresses for a device name e.g. eth1?

Use the src attribute of the returned route object. An interface might have multiple IPs and you usually want to utilize the one that would be used as source IP for outbound traffic.

To obtain the reverse DNS entry you can utilize nixio.getnameinfo().

<%
function get_ptr()
  local ip = require("luci.ip")
  local nx = require("nixio")

  local default_route = ip.route("8.8.8.8")

  if default_route and default_route.src then
    local egress_ip = default_route.src:host():string()

    return nx.getnameinfo(egress_ip)
  end
end
%>

<div>
  <strong>Current PTR record:</strong>
  <%= get_ptr() or "<em>unknown</em>" %>
</div>

As a command-line oneliner for testing:

lua -lluci.ip -lnixio -e 'print(nixio.getnameinfo(luci.ip.route("8.8.8.8").src:ho
st():string()))'

Relevant API docs:
https://openwrt.github.io/luci/api/modules/luci.ip.html#route
https://openwrt.github.io/luci/api/modules/nixio.html#nixio.getnameinfo

2 Likes

Ah. The docs for "luci.ip.route()" indicate that the "src" field is optional as a return value, so I was thrown. :sleepy:
Thanks, this would make a good basic tutorial.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.