sys.lua _nethints(): read location of dhcp.lease file from uci
[project/luci.git] / modules / luci-base / luasrc / tools / status.lua
1 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.tools.status", package.seeall)
5
6 local uci = require "luci.model.uci".cursor()
7
8 local function dhcp_leases_common(family)
9         local rv = { }
10         local nfs = require "nixio.fs"
11         local leasefile = "/tmp/dhcp.leases"
12
13         uci:foreach("dhcp", "dnsmasq",
14                 function(s)
15                         if s.leasefile and nfs.access(s.leasefile) then
16                                 leasefile = s.leasefile
17                                 return false
18                         end
19                 end)
20
21         local fd = io.open(leasefile, "r")
22         if fd then
23                 while true do
24                         local ln = fd:read("*l")
25                         if not ln then
26                                 break
27                         else
28                                 local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
29                                 if ts and mac and ip and name and duid then
30                                         if family == 4 and not ip:match(":") then
31                                                 rv[#rv+1] = {
32                                                         expires  = os.difftime(tonumber(ts) or 0, os.time()),
33                                                         macaddr  = mac,
34                                                         ipaddr   = ip,
35                                                         hostname = (name ~= "*") and name
36                                                 }
37                                         elseif family == 6 and ip:match(":") then
38                                                 rv[#rv+1] = {
39                                                         expires  = os.difftime(tonumber(ts) or 0, os.time()),
40                                                         ip6addr  = ip,
41                                                         duid     = (duid ~= "*") and duid,
42                                                         hostname = (name ~= "*") and name
43                                                 }
44                                         end
45                                 end
46                         end
47                 end
48                 fd:close()
49         end
50
51         local fd = io.open("/tmp/hosts/odhcpd", "r")
52         if fd then
53                 while true do
54                         local ln = fd:read("*l")
55                         if not ln then
56                                 break
57                         else
58                                 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (%d+) (%S+) (%S+) (.*)")
59                                 if ip and iaid ~= "ipv4" and family == 6 then
60                                         rv[#rv+1] = {
61                                                 expires  = os.difftime(tonumber(ts) or 0, os.time()),
62                                                 duid     = duid,
63                                                 ip6addr  = ip,
64                                                 hostname = (name ~= "-") and name
65                                         }
66                                 elseif ip and iaid == "ipv4" and family == 4 then
67                                         rv[#rv+1] = {
68                                                 expires  = os.difftime(tonumber(ts) or 0, os.time()),
69                                                 macaddr  = duid,
70                                                 ipaddr   = ip,
71                                                 hostname = (name ~= "-") and name
72                                         }
73                                 end
74                         end
75                 end
76                 fd:close()
77         end
78
79         return rv
80 end
81
82 function dhcp_leases()
83         return dhcp_leases_common(4)
84 end
85
86 function dhcp6_leases()
87         return dhcp_leases_common(6)
88 end
89
90 function wifi_networks()
91         local rv = { }
92         local ntm = require "luci.model.network".init()
93
94         local dev
95         for _, dev in ipairs(ntm:get_wifidevs()) do
96                 local rd = {
97                         up       = dev:is_up(),
98                         device   = dev:name(),
99                         name     = dev:get_i18n(),
100                         networks = { }
101                 }
102
103                 local net
104                 for _, net in ipairs(dev:get_wifinets()) do
105                         rd.networks[#rd.networks+1] = {
106                                 name       = net:shortname(),
107                                 link       = net:adminlink(),
108                                 up         = net:is_up(),
109                                 mode       = net:active_mode(),
110                                 ssid       = net:active_ssid(),
111                                 bssid      = net:active_bssid(),
112                                 encryption = net:active_encryption(),
113                                 frequency  = net:frequency(),
114                                 channel    = net:channel(),
115                                 signal     = net:signal(),
116                                 quality    = net:signal_percent(),
117                                 noise      = net:noise(),
118                                 bitrate    = net:bitrate(),
119                                 ifname     = net:ifname(),
120                                 assoclist  = net:assoclist(),
121                                 country    = net:country(),
122                                 txpower    = net:txpower(),
123                                 txpoweroff = net:txpower_offset(),
124                                 disabled   = (dev:get("disabled") == "1" or
125                                              net:get("disabled") == "1")
126                         }
127                 end
128
129                 rv[#rv+1] = rd
130         end
131
132         return rv
133 end
134
135 function wifi_network(id)
136         local ntm = require "luci.model.network".init()
137         local net = ntm:get_wifinet(id)
138         if net then
139                 local dev = net:get_device()
140                 if dev then
141                         return {
142                                 id         = id,
143                                 name       = net:shortname(),
144                                 link       = net:adminlink(),
145                                 up         = net:is_up(),
146                                 mode       = net:active_mode(),
147                                 ssid       = net:active_ssid(),
148                                 bssid      = net:active_bssid(),
149                                 encryption = net:active_encryption(),
150                                 frequency  = net:frequency(),
151                                 channel    = net:channel(),
152                                 signal     = net:signal(),
153                                 quality    = net:signal_percent(),
154                                 noise      = net:noise(),
155                                 bitrate    = net:bitrate(),
156                                 ifname     = net:ifname(),
157                                 assoclist  = net:assoclist(),
158                                 country    = net:country(),
159                                 txpower    = net:txpower(),
160                                 txpoweroff = net:txpower_offset(),
161                                 disabled   = (dev:get("disabled") == "1" or
162                                               net:get("disabled") == "1"),
163                                 device     = {
164                                         up     = dev:is_up(),
165                                         device = dev:name(),
166                                         name   = dev:get_i18n()
167                                 }
168                         }
169                 end
170         end
171         return { }
172 end
173
174 function switch_status(devs)
175         local dev
176         local switches = { }
177         for dev in devs:gmatch("[^%s,]+") do
178                 local ports = { }
179                 local swc = io.popen("swconfig dev %q show" % dev, "r")
180                 if swc then
181                         local l
182                         repeat
183                                 l = swc:read("*l")
184                                 if l then
185                                         local port, up = l:match("port:(%d+) link:(%w+)")
186                                         if port then
187                                                 local speed  = l:match(" speed:(%d+)")
188                                                 local duplex = l:match(" (%w+)-duplex")
189                                                 local txflow = l:match(" (txflow)")
190                                                 local rxflow = l:match(" (rxflow)")
191                                                 local auto   = l:match(" (auto)")
192
193                                                 ports[#ports+1] = {
194                                                         port   = tonumber(port) or 0,
195                                                         speed  = tonumber(speed) or 0,
196                                                         link   = (up == "up"),
197                                                         duplex = (duplex == "full"),
198                                                         rxflow = (not not rxflow),
199                                                         txflow = (not not txflow),
200                                                         auto   = (not not auto)
201                                                 }
202                                         end
203                                 end
204                         until not l
205                         swc:close()
206                 end
207                 switches[dev] = ports
208         end
209         return switches
210 end