modules/admin-full: live status, validation for dhcp leases
[project/luci.git] / modules / admin-full / luasrc / controller / admin / network.lua
index 896b4be..a6324f4 100644 (file)
@@ -87,6 +87,9 @@ function index()
                page.target = cbi("admin_network/dhcpleases")
                page.title  = i18n("DHCP Leases")
                page.order  = 30
+
+               page = entry({"admin", "network", "dhcplease_status"}, call("lease_status"), nil)
+               page.leaf = true
        end
 
        page  = node("admin", "network", "hosts")
@@ -156,45 +159,12 @@ end
 function wifi_delete(network)
        local ntm = require "luci.model.network".init()
 
-       ntm:del_network(network)
+       ntm:del_wifinet(network)
        ntm:save("wireless")
 
        luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
 end
 
-function jsondump(x)
-       if x == nil then
-               luci.http.write("null")
-       elseif type(x) == "table" then
-               local k, v
-               if type(next(x)) == "number" then
-                       luci.http.write("[ ")
-                       for k, v in ipairs(x) do
-                               jsondump(v)
-                               if next(x, k) then
-                                       luci.http.write(", ")
-                               end
-                       end
-                       luci.http.write(" ]")
-               else
-                       luci.http.write("{ ")
-                       for k, v in pairs(x) do
-                       luci.http.write("%q: " % k)
-                               jsondump(v)
-                               if next(x, k) then
-                                       luci.http.write(", ")
-                               end
-                       end
-                       luci.http.write(" }")
-               end
-       elseif type(x) == "number" or type(x) == "boolean" then
-               luci.http.write(tostring(x))
-       elseif type(x) == "string" then
-               luci.http.write("%q" % tostring(x))
-       end
-end
-
-
 function iface_status()
        local path = luci.dispatcher.context.requestpath
        local x    = luci.model.uci.cursor_state()
@@ -245,7 +215,7 @@ function iface_status()
 
        if #rv > 0 then
                luci.http.prepare_content("application/json")
-               jsondump(rv)
+               luci.http.write_json(rv)
                return
        end
 
@@ -276,9 +246,49 @@ function wifi_status()
 
        if #rv > 0 then
                luci.http.prepare_content("application/json")
-               jsondump(rv)
+               luci.http.write_json(rv)
                return
        end
 
        luci.http.status(404, "No such device")
 end
+
+function lease_status()
+       local rv = { }
+       local leasefile = "/var/dhcp.leases"
+
+       local uci = require "luci.model.uci".cursor()
+       local nfs = require "nixio.fs"
+
+       uci:foreach("dhcp", "dnsmasq",
+               function(s)
+                       if s.leasefile and nfs.access(s.leasefile) then
+                               leasefile = s.leasefile
+                               return false
+                       end
+               end)
+
+       local fd = io.open(leasefile, "r")
+       if fd then
+               while true do
+                       local ln = fd:read("*l")
+                       if not ln then
+                               break
+                       else
+                               local ts, mac, ip, name = ln:match("^(%d+) (%S+) (%S+) (%S+)")
+                               if ts and mac and ip and name then
+                                       rv[#rv+1] = {
+                                               expires  = os.difftime(tonumber(ts) or 0, os.time()),
+                                               macaddr  = mac,
+                                               ipaddr   = ip,
+                                               hostname = (name ~= "*") and name
+                                       }
+                               end
+                       end
+               end
+               fd:close()
+       end
+
+       luci.http.prepare_content("application/json")
+       luci.http.write_json(rv)
+end