e3276800d4e8a2aecacf72e83f19cefacb426397
[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 function dhcp_leases()
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 = ln:match("^(%d+) (%S+) (%S+) (%S+)")
40                                 if ts and mac and ip and name 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                                 end
48                         end
49                 end
50                 fd:close()
51         end
52
53         return rv
54 end
55
56 function wifi_networks()
57         local rv = { }
58         local ntm = require "luci.model.network".init()
59
60         local dev
61         for _, dev in ipairs(ntm:get_wifidevs()) do
62                 local rd = {
63                         up       = dev:is_up(),
64                         device   = dev:name(),
65                         name     = dev:get_i18n(),
66                         networks = { }
67                 }
68
69                 local net
70                 for _, net in ipairs(dev:get_wifinets()) do
71                         rd.networks[#rd.networks+1] = {
72                                 name       = net:shortname(),
73                                 link       = net:adminlink(),
74                                 up         = net:is_up(),
75                                 mode       = net:active_mode(),
76                                 ssid       = net:active_ssid(),
77                                 bssid      = net:active_bssid(),
78                                 encryption = net:active_encryption(),
79                                 frequency  = net:frequency(),
80                                 channel    = net:channel(),
81                                 signal     = net:signal(),
82                                 quality    = net:signal_percent(),
83                                 noise      = net:noise(),
84                                 bitrate    = net:bitrate(),
85                                 ifname     = net:ifname(),
86                                 assoclist  = net:assoclist()
87                         }
88                 end
89
90                 rv[#rv+1] = rd
91         end
92
93         return rv
94 end