Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / package / luci / modules / luci-mod-status / luasrc / controller / admin / status.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.admin.status", package.seeall)
6
7 function index()
8         entry({"admin", "status", "overview"}, template("admin_status/index"), _("Overview"), 1)
9
10         entry({"admin", "status", "iptables"}, template("admin_status/iptables"), _("Firewall"), 2).leaf = true
11         entry({"admin", "status", "iptables_dump"}, call("dump_iptables")).leaf = true
12         entry({"admin", "status", "iptables_action"}, post("action_iptables")).leaf = true
13
14         entry({"admin", "status", "routes"}, template("admin_status/routes"), _("Routes"), 3)
15         entry({"admin", "status", "syslog"}, call("action_syslog"), _("System Log"), 4)
16         entry({"admin", "status", "dmesg"}, call("action_dmesg"), _("Kernel Log"), 5)
17         entry({"admin", "status", "processes"}, form("admin_status/processes"), _("Processes"), 6)
18
19         entry({"admin", "status", "realtime"}, alias("admin", "status", "realtime", "load"), _("Realtime Graphs"), 7)
20
21         entry({"admin", "status", "realtime", "load"}, template("admin_status/load"), _("Load"), 1).leaf = true
22         entry({"admin", "status", "realtime", "load_status"}, call("action_load")).leaf = true
23
24         entry({"admin", "status", "realtime", "bandwidth"}, template("admin_status/bandwidth"), _("Traffic"), 2).leaf = true
25         entry({"admin", "status", "realtime", "bandwidth_status"}, call("action_bandwidth")).leaf = true
26
27         if nixio.fs.access("/etc/config/wireless") then
28                 entry({"admin", "status", "realtime", "wireless"}, template("admin_status/wireless"), _("Wireless"), 3).leaf = true
29                 entry({"admin", "status", "realtime", "wireless_status"}, call("action_wireless")).leaf = true
30         end
31
32         entry({"admin", "status", "realtime", "connections"}, template("admin_status/connections"), _("Connections"), 4).leaf = true
33         entry({"admin", "status", "realtime", "connections_status"}, call("action_connections")).leaf = true
34
35         entry({"admin", "status", "nameinfo"}, call("action_nameinfo")).leaf = true
36 end
37
38 function action_syslog()
39         local syslog = luci.sys.syslog()
40         luci.template.render("admin_status/syslog", {syslog=syslog})
41 end
42
43 function action_dmesg()
44         local dmesg = luci.sys.dmesg()
45         luci.template.render("admin_status/dmesg", {dmesg=dmesg})
46 end
47
48 function dump_iptables(family, table)
49         local prefix = (family == "6") and "ip6" or "ip"
50         local ok, lines = pcall(io.lines, "/proc/net/%s_tables_names" % prefix)
51         if ok and lines then
52                 local s
53                 for s in lines do
54                         if s == table then
55                                 luci.http.prepare_content("text/plain")
56                                 luci.sys.process.exec({
57                                         "/usr/sbin/%stables" % prefix, "-w", "-t", table,
58                                         "--line-numbers", "-nxvL"
59                                 }, luci.http.write)
60                                 return
61                         end
62                 end
63         end
64
65         luci.http.status(404, "No such table")
66         luci.http.prepare_content("text/plain")
67 end
68
69 function action_iptables()
70         if luci.http.formvalue("zero") then
71                 if luci.http.formvalue("family") == "6" then
72                         luci.util.exec("/usr/sbin/ip6tables -Z")
73                 else
74                         luci.util.exec("/usr/sbin/iptables -Z")
75                 end
76         elseif luci.http.formvalue("restart") then
77                 luci.util.exec("/etc/init.d/firewall restart")
78         end
79
80         luci.http.redirect(luci.dispatcher.build_url("admin/status/iptables"))
81 end
82
83 function action_bandwidth(iface)
84         luci.http.prepare_content("application/json")
85
86         local bwc = io.popen("luci-bwc -i %s 2>/dev/null"
87                 % luci.util.shellquote(iface))
88
89         if bwc then
90                 luci.http.write("[")
91
92                 while true do
93                         local ln = bwc:read("*l")
94                         if not ln then break end
95                         luci.http.write(ln)
96                 end
97
98                 luci.http.write("]")
99                 bwc:close()
100         end
101 end
102
103 function action_wireless(iface)
104         luci.http.prepare_content("application/json")
105
106         local bwc = io.popen("luci-bwc -r %s 2>/dev/null"
107                 % luci.util.shellquote(iface))
108
109         if bwc then
110                 luci.http.write("[")
111
112                 while true do
113                         local ln = bwc:read("*l")
114                         if not ln then break end
115                         luci.http.write(ln)
116                 end
117
118                 luci.http.write("]")
119                 bwc:close()
120         end
121 end
122
123 function action_load()
124         luci.http.prepare_content("application/json")
125
126         local bwc = io.popen("luci-bwc -l 2>/dev/null")
127         if bwc then
128                 luci.http.write("[")
129
130                 while true do
131                         local ln = bwc:read("*l")
132                         if not ln then break end
133                         luci.http.write(ln)
134                 end
135
136                 luci.http.write("]")
137                 bwc:close()
138         end
139 end
140
141 function action_connections()
142         local sys = require "luci.sys"
143
144         luci.http.prepare_content("application/json")
145
146         luci.http.write('{ "connections": ')
147         luci.http.write_json(sys.net.conntrack())
148
149         local bwc = io.popen("luci-bwc -c 2>/dev/null")
150         if bwc then
151                 luci.http.write(', "statistics": [')
152
153                 while true do
154                         local ln = bwc:read("*l")
155                         if not ln then break end
156                         luci.http.write(ln)
157                 end
158
159                 luci.http.write("]")
160                 bwc:close()
161         end
162
163         luci.http.write(" }")
164 end
165
166 function action_nameinfo(...)
167         local util = require "luci.util"
168
169         luci.http.prepare_content("application/json")
170         luci.http.write_json(util.ubus("network.rrdns", "lookup", {
171                 addrs = { ... },
172                 timeout = 5000,
173                 limit = 1000
174         }) or { })
175 end