cd543f71e8bba3219a91ab744912e8bab73c0f63
[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 $Id$
13 ]]--
14
15 module("luci.tools.status", package.seeall)
16
17 local uci = require "luci.model.uci".cursor()
18
19 local function dhcp_leases_common(family)
20         local rv = { }
21         local nfs = require "nixio.fs"
22         local leasefile = "/var/dhcp.leases"
23
24         uci:foreach("dhcp", "dnsmasq",
25                 function(s)
26                         if s.leasefile and nfs.access(s.leasefile) then
27                                 leasefile = s.leasefile
28                                 return false
29                         end
30                 end)
31
32         local fd = io.open(leasefile, "r")
33         if fd then
34                 while true do
35                         local ln = fd:read("*l")
36                         if not ln then
37                                 break
38                         else
39                                 local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
40                                 if ts and mac and ip and name and duid then
41                                         if family == 4 and not ip:match(":") then
42                                                 rv[#rv+1] = {
43                                                         expires  = os.difftime(tonumber(ts) or 0, os.time()),
44                                                         macaddr  = mac,
45                                                         ipaddr   = ip,
46                                                         hostname = (name ~= "*") and name
47                                                 }
48                                         elseif family == 6 and ip:match(":") then
49                                                 rv[#rv+1] = {
50                                                         expires  = os.difftime(tonumber(ts) or 0, os.time()),
51                                                         ip6addr  = ip,
52                                                         duid     = (duid ~= "*") and duid,
53                                                         hostname = (name ~= "*") and name
54                                                 }
55                                         end
56                                 end
57                         end
58                 end
59                 fd:close()
60         end
61
62         return rv
63 end
64
65 function dhcp_leases()
66         return dhcp_leases_common(4)
67 end
68
69 function dhcp6_leases()
70         if luci.sys.call("dnsmasq --version 2>/dev/null | grep -q ' DHCPv6 '") == 0 then
71                 return dhcp_leases_common(6)
72         else
73                 return nil
74         end
75 end
76
77 function wifi_networks()
78         local rv = { }
79         local ntm = require "luci.model.network".init()
80
81         local dev
82         for _, dev in ipairs(ntm:get_wifidevs()) do
83                 local rd = {
84                         up       = dev:is_up(),
85                         device   = dev:name(),
86                         name     = dev:get_i18n(),
87                         networks = { }
88                 }
89
90                 local net
91                 for _, net in ipairs(dev:get_wifinets()) do
92                         rd.networks[#rd.networks+1] = {
93                                 name       = net:shortname(),
94                                 link       = net:adminlink(),
95                                 up         = net:is_up(),
96                                 mode       = net:active_mode(),
97                                 ssid       = net:active_ssid(),
98                                 bssid      = net:active_bssid(),
99                                 encryption = net:active_encryption(),
100                                 frequency  = net:frequency(),
101                                 channel    = net:channel(),
102                                 signal     = net:signal(),
103                                 quality    = net:signal_percent(),
104                                 noise      = net:noise(),
105                                 bitrate    = net:bitrate(),
106                                 ifname     = net:ifname(),
107                                 assoclist  = net:assoclist(),
108                                 country    = net:country(),
109                                 txpower    = net:txpower(),
110                                 txpoweroff = net:txpower_offset()
111                         }
112                 end
113
114                 rv[#rv+1] = rd
115         end
116
117         return rv
118 end
119
120 function wifi_network(id)
121         local ntm = require "luci.model.network".init()
122         local net = ntm:get_wifinet(id)
123         if net then
124                 local dev = net:get_device()
125                 if dev then
126                         return {
127                                 id         = id,
128                                 name       = net:shortname(),
129                                 link       = net:adminlink(),
130                                 up         = net:is_up(),
131                                 mode       = net:active_mode(),
132                                 ssid       = net:active_ssid(),
133                                 bssid      = net:active_bssid(),
134                                 encryption = net:active_encryption(),
135                                 frequency  = net:frequency(),
136                                 channel    = net:channel(),
137                                 signal     = net:signal(),
138                                 quality    = net:signal_percent(),
139                                 noise      = net:noise(),
140                                 bitrate    = net:bitrate(),
141                                 ifname     = net:ifname(),
142                                 assoclist  = net:assoclist(),
143                                 country    = net:country(),
144                                 txpower    = net:txpower(),
145                                 txpoweroff = net:txpower_offset(),
146                                 device     = {
147                                         up     = dev:is_up(),
148                                         device = dev:name(),
149                                         name   = dev:get_i18n()
150                                 }
151                         }
152                 end
153         end
154         return { }
155 end