Merge pull request #965 from cshore-firmware/pull-request-odhcpd-mac
[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                                 local expire = tonumber(ts) or 0
30                                 if ts and mac and ip and name and duid then
31                                         if family == 4 and not ip:match(":") then
32                                                 rv[#rv+1] = {
33                                                         expires  = (expire ~= 0) and os.difftime(expire, os.time()),
34                                                         macaddr  = mac,
35                                                         ipaddr   = ip,
36                                                         hostname = (name ~= "*") and name
37                                                 }
38                                         elseif family == 6 and ip:match(":") then
39                                                 rv[#rv+1] = {
40                                                         expires  = (expire ~= 0) and os.difftime(expire, os.time()),
41                                                         ip6addr  = ip,
42                                                         duid     = (duid ~= "*") and duid,
43                                                         hostname = (name ~= "*") and name
44                                                 }
45                                         end
46                                 end
47                         end
48                 end
49                 fd:close()
50         end
51
52         local lease6file = "/tmp/hosts/odhcpd"
53         uci:foreach("dhcp", "odhcpd",
54                 function(t)
55                         if t.leasefile and nfs.access(t.leasefile) then
56                                 lease6file = t.leasefile
57                                 return false
58                         end
59                 end)
60         local fd = io.open(lease6file, "r")
61         if fd then
62                 while true do
63                         local ln = fd:read("*l")
64                         if not ln then
65                                 break
66                         else
67                                 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (-?%d+) (%S+) (%S+) (.*)")
68                                 local expire = tonumber(ts) or 0
69                                 if ip and iaid ~= "ipv4" and family == 6 then
70                                         rv[#rv+1] = {
71                                                 expires  = (expire >= 0) and os.difftime(expire, os.time()),
72                                                 duid     = duid,
73                                                 ip6addr  = ip,
74                                                 hostname = (name ~= "-") and name
75                                         }
76                                 elseif ip and iaid == "ipv4" and family == 4 then
77                                         local mac, mac1, mac2, mac3, mac4, mac5, mac6
78                                         if duid and type(duid) == "string" then
79                                                  mac1, mac2, mac3, mac4, mac5, mac6 = duid:match("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
80                                         end
81                                         if not (mac1 and mac2 and mac3 and mac4 and mac5 and mac6) then
82                                                 mac = "FF:FF:FF:FF:FF:FF"
83                                         else
84                                                 mac = mac1..":"..mac2..":"..mac3..":"..mac4..":"..mac5..":"..mac6
85                                         end
86                                         rv[#rv+1] = {
87                                                 expires  = (expire >= 0) and os.difftime(expire, os.time()),
88                                                 macaddr  = duid,
89                                                 macaddr  = mac:lower(),
90                                                 ipaddr   = ip,
91                                                 hostname = (name ~= "-") and name
92                                         }
93                                 end
94                         end
95                 end
96                 fd:close()
97         end
98
99         return rv
100 end
101
102 function dhcp_leases()
103         return dhcp_leases_common(4)
104 end
105
106 function dhcp6_leases()
107         return dhcp_leases_common(6)
108 end
109
110 function wifi_networks()
111         local rv = { }
112         local ntm = require "luci.model.network".init()
113
114         local dev
115         for _, dev in ipairs(ntm:get_wifidevs()) do
116                 local rd = {
117                         up       = dev:is_up(),
118                         device   = dev:name(),
119                         name     = dev:get_i18n(),
120                         networks = { }
121                 }
122
123                 local net
124                 for _, net in ipairs(dev:get_wifinets()) do
125                         rd.networks[#rd.networks+1] = {
126                                 name       = net:shortname(),
127                                 link       = net:adminlink(),
128                                 up         = net:is_up(),
129                                 mode       = net:active_mode(),
130                                 ssid       = net:active_ssid(),
131                                 bssid      = net:active_bssid(),
132                                 encryption = net:active_encryption(),
133                                 frequency  = net:frequency(),
134                                 channel    = net:channel(),
135                                 signal     = net:signal(),
136                                 quality    = net:signal_percent(),
137                                 noise      = net:noise(),
138                                 bitrate    = net:bitrate(),
139                                 ifname     = net:ifname(),
140                                 assoclist  = net:assoclist(),
141                                 country    = net:country(),
142                                 txpower    = net:txpower(),
143                                 txpoweroff = net:txpower_offset(),
144                                 disabled   = (dev:get("disabled") == "1" or
145                                              net:get("disabled") == "1")
146                         }
147                 end
148
149                 rv[#rv+1] = rd
150         end
151
152         return rv
153 end
154
155 function wifi_network(id)
156         local ntm = require "luci.model.network".init()
157         local net = ntm:get_wifinet(id)
158         if net then
159                 local dev = net:get_device()
160                 if dev then
161                         return {
162                                 id         = id,
163                                 name       = net:shortname(),
164                                 link       = net:adminlink(),
165                                 up         = net:is_up(),
166                                 mode       = net:active_mode(),
167                                 ssid       = net:active_ssid(),
168                                 bssid      = net:active_bssid(),
169                                 encryption = net:active_encryption(),
170                                 frequency  = net:frequency(),
171                                 channel    = net:channel(),
172                                 signal     = net:signal(),
173                                 quality    = net:signal_percent(),
174                                 noise      = net:noise(),
175                                 bitrate    = net:bitrate(),
176                                 ifname     = net:ifname(),
177                                 assoclist  = net:assoclist(),
178                                 country    = net:country(),
179                                 txpower    = net:txpower(),
180                                 txpoweroff = net:txpower_offset(),
181                                 disabled   = (dev:get("disabled") == "1" or
182                                               net:get("disabled") == "1"),
183                                 device     = {
184                                         up     = dev:is_up(),
185                                         device = dev:name(),
186                                         name   = dev:get_i18n()
187                                 }
188                         }
189                 end
190         end
191         return { }
192 end
193
194 function switch_status(devs)
195         local dev
196         local switches = { }
197         for dev in devs:gmatch("[^%s,]+") do
198                 local ports = { }
199                 local swc = io.popen("swconfig dev %q show" % dev, "r")
200                 if swc then
201                         local l
202                         repeat
203                                 l = swc:read("*l")
204                                 if l then
205                                         local port, up = l:match("port:(%d+) link:(%w+)")
206                                         if port then
207                                                 local speed  = l:match(" speed:(%d+)")
208                                                 local duplex = l:match(" (%w+)-duplex")
209                                                 local txflow = l:match(" (txflow)")
210                                                 local rxflow = l:match(" (rxflow)")
211                                                 local auto   = l:match(" (auto)")
212
213                                                 ports[#ports+1] = {
214                                                         port   = tonumber(port) or 0,
215                                                         speed  = tonumber(speed) or 0,
216                                                         link   = (up == "up"),
217                                                         duplex = (duplex == "full"),
218                                                         rxflow = (not not rxflow),
219                                                         txflow = (not not txflow),
220                                                         auto   = (not not auto)
221                                                 }
222                                         end
223                                 end
224                         until not l
225                         swc:close()
226                 end
227                 switches[dev] = ports
228         end
229         return switches
230 end