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