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