modules/admin-{mini,full}: display client hostnames in lease overview
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / dhcpleases.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local uci = require "luci.model.uci".cursor()
16 local sys = require "luci.sys"
17 local wa = require "luci.tools.webadmin"
18 local fs = require "nixio.fs"
19
20 m2 = Map("dhcp", translate("DHCP Leases"),
21 translate("Static leases are used to assign fixed IP addresses and symbolic hostnames to " ..
22 "DHCP clients. They are also required for non-dynamic interface configurations where " ..
23 "only hosts with a corresponding lease are served."))
24
25 local leasefn, leasefp, leases
26 uci:foreach("dhcp", "dnsmasq",
27 function(section)
28 leasefn = section.leasefile
29 end
30 )
31 local leasefp = leasefn and fs.access(leasefn) and io.lines(leasefn)
32 if leasefp then
33 leases = {}
34 for lease in leasefp do
35 table.insert(leases, luci.util.split(lease, " "))
36 end
37 end
38
39 if leases then
40 v = m2:section(Table, leases, translate("Active Leases"))
41
42 name = v:option(DummyValue, 4, translate("Hostname"))
43 function name.cfgvalue(self, ...)
44 local value = DummyValue.cfgvalue(self, ...)
45 return (value == "*") and "?" or value
46 end
47
48 ip = v:option(DummyValue, 3, translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
49
50 mac = v:option(DummyValue, 2, translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
51
52 ltime = v:option(DummyValue, 1, translate("Leasetime remaining"))
53 function ltime.cfgvalue(self, ...)
54 local value = DummyValue.cfgvalue(self, ...)
55 return wa.date_format(os.difftime(tonumber(value), os.time()))
56 end
57 end
58
59 s = m2:section(TypedSection, "host", translate("Static Leases"),
60 translate("Use the <em>Add</em> Button to add a new lease entry. The <em>MAC-Address</em> " ..
61 "indentifies the host, the <em>IPv4-Address</em> specifies to the fixed address to " ..
62 "use and the <em>Hostname</em> is assigned as symbolic name to the requesting host."))
63
64 s.addremove = true
65 s.anonymous = true
66 s.template = "cbi/tblsection"
67
68 name = s:option(Value, "name", translate("Hostname"))
69 mac = s:option(Value, "mac", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
70 ip = s:option(Value, "ip", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
71 sys.net.arptable(function(entry)
72 ip:value(entry["IP address"])
73 mac:value(
74 entry["HW address"],
75 entry["HW address"] .. " (" .. entry["IP address"] .. ")"
76 )
77 end)
78
79
80 return m2