Update my email addresses in the license headers
[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 = "/var/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 }
125 end
126
127 rv[#rv+1] = rd
128 end
129
130 return rv
131 end
132
133 function wifi_network(id)
134 local ntm = require "luci.model.network".init()
135 local net = ntm:get_wifinet(id)
136 if net then
137 local dev = net:get_device()
138 if dev then
139 return {
140 id = id,
141 name = net:shortname(),
142 link = net:adminlink(),
143 up = net:is_up(),
144 mode = net:active_mode(),
145 ssid = net:active_ssid(),
146 bssid = net:active_bssid(),
147 encryption = net:active_encryption(),
148 frequency = net:frequency(),
149 channel = net:channel(),
150 signal = net:signal(),
151 quality = net:signal_percent(),
152 noise = net:noise(),
153 bitrate = net:bitrate(),
154 ifname = net:ifname(),
155 assoclist = net:assoclist(),
156 country = net:country(),
157 txpower = net:txpower(),
158 txpoweroff = net:txpower_offset(),
159 disabled = (dev:get("disabled") == "1" or
160 net:get("disabled") == "1"),
161 device = {
162 up = dev:is_up(),
163 device = dev:name(),
164 name = dev:get_i18n()
165 }
166 }
167 end
168 end
169 return { }
170 end
171
172 function switch_status(devs)
173 local dev
174 local switches = { }
175 for dev in devs:gmatch("[^%s,]+") do
176 local ports = { }
177 local swc = io.popen("swconfig dev %q show" % dev, "r")
178 if swc then
179 local l
180 repeat
181 l = swc:read("*l")
182 if l then
183 local port, up = l:match("port:(%d+) link:(%w+)")
184 if port then
185 local speed = l:match(" speed:(%d+)")
186 local duplex = l:match(" (%w+)-duplex")
187 local txflow = l:match(" (txflow)")
188 local rxflow = l:match(" (rxflow)")
189 local auto = l:match(" (auto)")
190
191 ports[#ports+1] = {
192 port = tonumber(port) or 0,
193 speed = tonumber(speed) or 0,
194 link = (up == "up"),
195 duplex = (duplex == "full"),
196 rxflow = (not not rxflow),
197 txflow = (not not txflow),
198 auto = (not not auto)
199 }
200 end
201 end
202 until not l
203 swc:close()
204 end
205 switches[dev] = ports
206 end
207 return switches
208 end