0efc51be23a5145f5586074f1ce4fbf6ef214421
[project/luci.git] / modules / base / 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         local fd = io.open("/tmp/hosts/odhcpd", "r")
62         if fd then
63                 while true do
64                         local ln = fd:read("*l")
65                         if not ln then
66                                 break
67                         else
68                                 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (%d+) (%S+) (%S+) (.*)")
69                                 if ip and iaid ~= "ipv4" and family == 6 then
70                                         rv[#rv+1] = {
71                                                 expires  = os.difftime(tonumber(ts) or 0, os.time()),
72                                                 duid     = duid,
73                                                 ip6addr  = ip,
74                                                 hostname = (name ~= "-") and name
75                                         }
76                                 elseif ip and iaid == "ipv4" and family == 4 then
77                                         rv[#rv+1] = {
78                                                 expires  = os.difftime(tonumber(ts) or 0, os.time()),
79                                                 macaddr  = duid,
80                                                 ipaddr   = ip,
81                                                 hostname = (name ~= "-") and name
82                                         }
83                                 end
84                         end
85                 end
86                 fd:close()
87         end
88
89         return rv
90 end
91
92 function dhcp_leases()
93         return dhcp_leases_common(4)
94 end
95
96 function dhcp6_leases()
97         return dhcp_leases_common(6)
98 end
99
100 function wifi_networks()
101         local rv = { }
102         local ntm = require "luci.model.network".init()
103
104         local dev
105         for _, dev in ipairs(ntm:get_wifidevs()) do
106                 local rd = {
107                         up       = dev:is_up(),
108                         device   = dev:name(),
109                         name     = dev:get_i18n(),
110                         networks = { }
111                 }
112
113                 local net
114                 for _, net in ipairs(dev:get_wifinets()) do
115                         rd.networks[#rd.networks+1] = {
116                                 name       = net:shortname(),
117                                 link       = net:adminlink(),
118                                 up         = net:is_up(),
119                                 mode       = net:active_mode(),
120                                 ssid       = net:active_ssid(),
121                                 bssid      = net:active_bssid(),
122                                 encryption = net:active_encryption(),
123                                 frequency  = net:frequency(),
124                                 channel    = net:channel(),
125                                 signal     = net:signal(),
126                                 quality    = net:signal_percent(),
127                                 noise      = net:noise(),
128                                 bitrate    = net:bitrate(),
129                                 ifname     = net:ifname(),
130                                 assoclist  = net:assoclist(),
131                                 country    = net:country(),
132                                 txpower    = net:txpower(),
133                                 txpoweroff = net:txpower_offset()
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