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