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