ef6089f62323fd63e5763af637fe374345671296
[project/luci.git] / modules / admin-full / luasrc / controller / admin / status.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15 module("luci.controller.admin.status", package.seeall)
16
17 function index()
18         luci.i18n.loadc("base")
19         local i18n = luci.i18n.translate
20
21         entry({"admin", "status"}, alias("admin", "status", "overview"), i18n("Status"), 20).index = true
22         entry({"admin", "status", "overview"}, template("admin_status/index"), i18n("Overview"), 1)
23         entry({"admin", "status", "iptables"}, call("action_iptables"), i18n("Firewall"), 2).leaf = true
24         entry({"admin", "status", "routes"}, template("admin_status/routes"), i18n("Routes"), 3)
25         entry({"admin", "status", "syslog"}, call("action_syslog"), i18n("System Log"), 4)
26         entry({"admin", "status", "dmesg"}, call("action_dmesg"), i18n("Kernel Log"), 5)
27
28         entry({"admin", "status", "load"}, template("admin_status/load"), i18n("Realtime Load"), 6).leaf = true
29         entry({"admin", "status", "load_status"}, call("action_load")).leaf = true
30
31         entry({"admin", "status", "bandwidth"}, template("admin_status/bandwidth"), i18n("Realtime Traffic"), 7).leaf = true
32         entry({"admin", "status", "bandwidth_status"}, call("action_bandwidth")).leaf = true
33
34         entry({"admin", "status", "connections"}, template("admin_status/connections"), i18n("Realtime Connections"), 8).leaf = true
35         entry({"admin", "status", "connections_status"}, call("action_connections")).leaf = true
36
37         entry({"admin", "status", "processes"}, cbi("admin_status/processes"), i18n("Processes"), 20)
38 end
39
40 function action_syslog()
41         local syslog = luci.sys.syslog()
42         luci.template.render("admin_status/syslog", {syslog=syslog})
43 end
44
45 function action_dmesg()
46         local dmesg = luci.sys.dmesg()
47         luci.template.render("admin_status/dmesg", {dmesg=dmesg})
48 end
49
50 function action_iptables()
51         if luci.http.formvalue("zero") then
52                 if luci.http.formvalue("zero") == "6" then
53                         luci.util.exec("ip6tables -Z")
54                 else
55                         luci.util.exec("iptables -Z")
56                 end
57                 luci.http.redirect(
58                         luci.dispatcher.build_url("admin", "status", "iptables")
59                 )
60         elseif luci.http.formvalue("restart") == "1" then
61                 luci.util.exec("/etc/init.d/firewall restart")
62                 luci.http.redirect(
63                         luci.dispatcher.build_url("admin", "status", "iptables")
64                 )
65         else
66                 luci.template.render("admin_status/iptables")
67         end
68 end
69
70 function action_bandwidth()
71         local path  = luci.dispatcher.context.requestpath
72         local iface = path[#path]
73
74         local fs = require "luci.fs"
75         if fs.access("/var/lib/luci-bwc/if/%s" % iface) then
76                 luci.http.prepare_content("application/json")
77
78                 local bwc = io.popen("luci-bwc -i %q 2>/dev/null" % iface)
79                 if bwc then
80                         luci.http.write("[")
81
82                         while true do
83                                 local ln = bwc:read("*l")
84                                 if not ln then break end
85                                 luci.http.write(ln)
86                         end
87
88                         luci.http.write("]")
89                         bwc:close()
90                 end
91
92                 return
93         end
94
95         luci.http.status(404, "No data available")
96 end
97
98 function action_load()
99         local fs = require "luci.fs"
100         if fs.access("/var/lib/luci-bwc/load") then
101                 luci.http.prepare_content("application/json")
102
103                 local bwc = io.popen("luci-bwc -l 2>/dev/null")
104                 if bwc then
105                         luci.http.write("[")
106
107                         while true do
108                                 local ln = bwc:read("*l")
109                                 if not ln then break end
110                                 luci.http.write(ln)
111                         end
112
113                         luci.http.write("]")
114                         bwc:close()
115                 end
116
117                 return
118         end
119
120         luci.http.status(404, "No data available")
121 end
122
123 function action_connections()
124         local fs  = require "luci.fs"
125         local sys = require "luci.sys"
126
127         luci.http.prepare_content("application/json")
128
129         luci.http.write("{ connections: ")
130         luci.http.write_json(sys.net.conntrack())
131
132         if fs.access("/var/lib/luci-bwc/connections") then
133                 local bwc = io.popen("luci-bwc -c 2>/dev/null")
134                 if bwc then
135                         luci.http.write(", statistics: [")
136
137                         while true do
138                                 local ln = bwc:read("*l")
139                                 if not ln then break end
140                                 luci.http.write(ln)
141                         end
142
143                         luci.http.write("]")
144                         bwc:close()
145                 end
146         end
147
148         luci.http.write(" }")
149 end