Merge pull request #464 from remakeelectric/pulls/list-operations
[project/luci.git] / modules / luci-mod-admin-full / 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"}, alias("admin", "status", "overview"), _("Status"), 20).index = true
9         entry({"admin", "status", "overview"}, template("admin_status/index"), _("Overview"), 1)
10
11         entry({"admin", "status", "iptables"}, template("admin_status/iptables"), _("Firewall"), 2).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"}, cbi("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         entry({"admin", "status", "realtime", "wireless"}, template("admin_status/wireless"), _("Wireless"), 3).leaf = true
28         entry({"admin", "status", "realtime", "wireless_status"}, call("action_wireless")).leaf = true
29
30         entry({"admin", "status", "realtime", "connections"}, template("admin_status/connections"), _("Connections"), 4).leaf = true
31         entry({"admin", "status", "realtime", "connections_status"}, call("action_connections")).leaf = true
32
33         entry({"admin", "status", "nameinfo"}, call("action_nameinfo")).leaf = true
34 end
35
36 function action_syslog()
37         local syslog = luci.sys.syslog()
38         luci.template.render("admin_status/syslog", {syslog=syslog})
39 end
40
41 function action_dmesg()
42         local dmesg = luci.sys.dmesg()
43         luci.template.render("admin_status/dmesg", {dmesg=dmesg})
44 end
45
46 function action_iptables()
47         if luci.http.formvalue("zero") then
48                 if luci.http.formvalue("family") == "6" then
49                         luci.util.exec("/usr/sbin/ip6tables -Z")
50                 else
51                         luci.util.exec("/usr/sbin/iptables -Z")
52                 end
53         elseif luci.http.formvalue("restart") then
54                 luci.util.exec("/etc/init.d/firewall restart")
55         end
56
57         luci.http.redirect(luci.dispatcher.build_url("admin/status/iptables"))
58 end
59
60 function action_bandwidth(iface)
61         luci.http.prepare_content("application/json")
62
63         local bwc = io.popen("luci-bwc -i %q 2>/dev/null" % iface)
64         if bwc then
65                 luci.http.write("[")
66
67                 while true do
68                         local ln = bwc:read("*l")
69                         if not ln then break end
70                         luci.http.write(ln)
71                 end
72
73                 luci.http.write("]")
74                 bwc:close()
75         end
76 end
77
78 function action_wireless(iface)
79         luci.http.prepare_content("application/json")
80
81         local bwc = io.popen("luci-bwc -r %q 2>/dev/null" % iface)
82         if bwc then
83                 luci.http.write("[")
84
85                 while true do
86                         local ln = bwc:read("*l")
87                         if not ln then break end
88                         luci.http.write(ln)
89                 end
90
91                 luci.http.write("]")
92                 bwc:close()
93         end
94 end
95
96 function action_load()
97         luci.http.prepare_content("application/json")
98
99         local bwc = io.popen("luci-bwc -l 2>/dev/null")
100         if bwc then
101                 luci.http.write("[")
102
103                 while true do
104                         local ln = bwc:read("*l")
105                         if not ln then break end
106                         luci.http.write(ln)
107                 end
108
109                 luci.http.write("]")
110                 bwc:close()
111         end
112 end
113
114 function action_connections()
115         local sys = require "luci.sys"
116
117         luci.http.prepare_content("application/json")
118
119         luci.http.write("{ connections: ")
120         luci.http.write_json(sys.net.conntrack())
121
122         local bwc = io.popen("luci-bwc -c 2>/dev/null")
123         if bwc then
124                 luci.http.write(", statistics: [")
125
126                 while true do
127                         local ln = bwc:read("*l")
128                         if not ln then break end
129                         luci.http.write(ln)
130                 end
131
132                 luci.http.write("]")
133                 bwc:close()
134         end
135
136         luci.http.write(" }")
137 end
138
139 function action_nameinfo(...)
140         local i
141         local rv = { }
142         for i = 1, select('#', ...) do
143                 local addr = select(i, ...)
144                 local fqdn = nixio.getnameinfo(addr)
145                 rv[addr] = fqdn or (addr:match(":") and "[%s]" % addr or addr)
146         end
147
148         luci.http.prepare_content("application/json")
149         luci.http.write_json(rv)
150 end