44acab4721b659723bfc3063bba842b7cffd6952
[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
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 module("luci.controller.admin.status", package.seeall)
15
16 function index()
17         luci.i18n.loadc("base")
18         local i18n = luci.i18n.translate
19
20         entry({"admin", "status"}, template("admin_status/index"), i18n("Status"), 20).index = true
21         entry({"admin", "status", "interfaces"}, template("admin_status/interfaces"), i18n("Interfaces"), 1)
22         entry({"admin", "status", "iptables"}, call("action_iptables"), i18n("Firewall"), 2)
23         entry({"admin", "status", "conntrack"}, template("admin_status/conntrack"), i18n("Active Connections"), 3)
24         entry({"admin", "status", "routes"}, template("admin_status/routes"), i18n("Routes"), 4)
25         entry({"admin", "status", "syslog"}, call("action_syslog"), i18n("System Log"), 5)
26         entry({"admin", "status", "dmesg"}, call("action_dmesg"), i18n("Kernel Log"), 6)
27
28         entry({"admin", "status", "load"}, template("admin_status/load"), i18n("Realtime Load"), 7).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"), 8).leaf = true
32         entry({"admin", "status", "bandwidth_status"}, call("action_bandwidth")).leaf = true
33
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") == "1" then
48                 luci.util.exec("iptables -Z")
49                 luci.http.redirect(
50                         luci.dispatcher.build_url("admin", "status", "iptables")
51                 )
52         elseif luci.http.formvalue("restart") == "1" then
53                 luci.util.exec("/etc/init.d/firewall restart")
54                 luci.http.redirect(
55                         luci.dispatcher.build_url("admin", "status", "iptables")
56                 )
57         else
58                 luci.template.render("admin_status/iptables")
59         end
60 end
61
62 function action_bandwidth()
63         local path  = luci.dispatcher.context.requestpath
64         local iface = path[#path]
65
66         local fs = require "luci.fs"
67         if fs.access("/var/lib/luci-bwc/if/%s" % iface) then
68                 luci.http.prepare_content("application/json")
69
70                 local bwc = io.popen("luci-bwc -i %q 2>/dev/null" % iface)
71                 if bwc then
72                         luci.http.write("[")
73
74                         while true do
75                                 local ln = bwc:read("*l")
76                                 if not ln then break end
77                                 luci.http.write(ln)
78                         end
79
80                         luci.http.write("]")
81                         bwc:close()
82                 end
83
84                 return
85         end
86
87         luci.http.status(404, "No data available")
88 end
89
90 function action_load()
91         local fs = require "luci.fs"
92         if fs.access("/var/lib/luci-bwc/load") then
93                 luci.http.prepare_content("application/json")
94
95                 local bwc = io.popen("luci-bwc -l 2>/dev/null")
96                 if bwc then
97                         luci.http.write("[")
98
99                         while true do
100                                 local ln = bwc:read("*l")
101                                 if not ln then break end
102                                 luci.http.write(ln)
103                         end
104
105                         luci.http.write("]")
106                         bwc:close()
107                 end
108
109                 return
110         end
111
112         luci.http.status(404, "No data available")
113 end