Merge pull request #663 from NeoRaider/styleable-checkboxes
[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                                         rv[#rv+1] = {
77                                                 expires  = (expire >= 0) and os.difftime(expire, os.time()),
78                                                 macaddr  = duid,
79                                                 ipaddr   = ip,
80                                                 hostname = (name ~= "-") and name
81                                         }
82                                 end
83                         end
84                 end
85                 fd:close()
86         end
87
88         return rv
89 end
90
91 function dhcp_leases()
92         return dhcp_leases_common(4)
93 end
94
95 function dhcp6_leases()
96         return dhcp_leases_common(6)
97 end
98
99 function wifi_networks()
100         local rv = { }
101         local ntm = require "luci.model.network".init()
102
103         local dev
104         for _, dev in ipairs(ntm:get_wifidevs()) do
105                 local rd = {
106                         up       = dev:is_up(),
107                         device   = dev:name(),
108                         name     = dev:get_i18n(),
109                         networks = { }
110                 }
111
112                 local net
113                 for _, net in ipairs(dev:get_wifinets()) do
114                         rd.networks[#rd.networks+1] = {
115                                 name       = net:shortname(),
116                                 link       = net:adminlink(),
117                                 up         = net:is_up(),
118                                 mode       = net:active_mode(),
119                                 ssid       = net:active_ssid(),
120                                 bssid      = net:active_bssid(),
121                                 encryption = net:active_encryption(),
122                                 frequency  = net:frequency(),
123                                 channel    = net:channel(),
124                                 signal     = net:signal(),
125                                 quality    = net:signal_percent(),
126                                 noise      = net:noise(),
127                                 bitrate    = net:bitrate(),
128                                 ifname     = net:ifname(),
129                                 assoclist  = net:assoclist(),
130                                 country    = net:country(),
131                                 txpower    = net:txpower(),
132                                 txpoweroff = net:txpower_offset(),
133                                 disabled   = (dev:get("disabled") == "1" or
134                                              net:get("disabled") == "1")
135                         }
136                 end
137
138                 rv[#rv+1] = rd
139         end
140
141         return rv
142 end
143
144 function wifi_network(id)
145         local ntm = require "luci.model.network".init()
146         local net = ntm:get_wifinet(id)
147         if net then
148                 local dev = net:get_device()
149                 if dev then
150                         return {
151                                 id         = id,
152                                 name       = net:shortname(),
153                                 link       = net:adminlink(),
154                                 up         = net:is_up(),
155                                 mode       = net:active_mode(),
156                                 ssid       = net:active_ssid(),
157                                 bssid      = net:active_bssid(),
158                                 encryption = net:active_encryption(),
159                                 frequency  = net:frequency(),
160                                 channel    = net:channel(),
161                                 signal     = net:signal(),
162                                 quality    = net:signal_percent(),
163                                 noise      = net:noise(),
164                                 bitrate    = net:bitrate(),
165                                 ifname     = net:ifname(),
166                                 assoclist  = net:assoclist(),
167                                 country    = net:country(),
168                                 txpower    = net:txpower(),
169                                 txpoweroff = net:txpower_offset(),
170                                 disabled   = (dev:get("disabled") == "1" or
171                                               net:get("disabled") == "1"),
172                                 device     = {
173                                         up     = dev:is_up(),
174                                         device = dev:name(),
175                                         name   = dev:get_i18n()
176                                 }
177                         }
178                 end
179         end
180         return { }
181 end
182
183 function switch_status(devs)
184         local dev
185         local switches = { }
186         for dev in devs:gmatch("[^%s,]+") do
187                 local ports = { }
188                 local swc = io.popen("swconfig dev %q show" % dev, "r")
189                 if swc then
190                         local l
191                         repeat
192                                 l = swc:read("*l")
193                                 if l then
194                                         local port, up = l:match("port:(%d+) link:(%w+)")
195                                         if port then
196                                                 local speed  = l:match(" speed:(%d+)")
197                                                 local duplex = l:match(" (%w+)-duplex")
198                                                 local txflow = l:match(" (txflow)")
199                                                 local rxflow = l:match(" (rxflow)")
200                                                 local auto   = l:match(" (auto)")
201
202                                                 ports[#ports+1] = {
203                                                         port   = tonumber(port) or 0,
204                                                         speed  = tonumber(speed) or 0,
205                                                         link   = (up == "up"),
206                                                         duplex = (duplex == "full"),
207                                                         rxflow = (not not rxflow),
208                                                         txflow = (not not txflow),
209                                                         auto   = (not not auto)
210                                                 }
211                                         end
212                                 end
213                         until not l
214                         swc:close()
215                 end
216                 switches[dev] = ports
217         end
218         return switches
219 end