modules/admin-full: resolve scope issue in previous commit
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_system / system.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
15 require("luci.sys")
16 require("luci.sys.zoneinfo")
17 require("luci.tools.webadmin")
18 require("luci.fs")
19
20 m = Map("system", translate("System"), translate("Here you can configure the basic aspects of your device like its hostname or the timezone."))
21
22 local has_rdate = false
23
24 m.uci:foreach("system", "rdate",
25         function()
26                 has_rdate = true
27                 return false
28         end)
29
30
31 s = m:section(TypedSection, "system", "")
32 s.anonymous = true
33 s.addremove = false
34
35 local system, model, memtotal, memcached, membuffers, memfree = luci.sys.sysinfo()
36 local uptime = luci.sys.uptime()
37
38 s:option(DummyValue, "_system", translate("System")).value = system
39 s:option(DummyValue, "_cpu", translate("Processor")).value = model
40
41 local load1, load5, load15 = luci.sys.loadavg()
42 s:option(DummyValue, "_la", translate("Load")).value =
43  string.format("%.2f, %.2f, %.2f", load1, load5, load15)
44
45 s:option(DummyValue, "_memtotal", translate("Memory")).value =
46  string.format("%.2f MB (%.0f%% %s, %.0f%% %s, %.0f%% %s)",
47   tonumber(memtotal) / 1024,
48   100 * memcached / memtotal,
49   tostring(translate("cached")),
50   100 * membuffers / memtotal,
51   tostring(translate("buffered")),
52   100 * memfree / memtotal,
53   tostring(translate("free"))
54 )
55
56 s:option(DummyValue, "_systime", translate("Local Time")).value =
57  os.date("%c")
58
59 s:option(DummyValue, "_uptime", translate("Uptime")).value =
60  luci.tools.webadmin.date_format(tonumber(uptime))
61
62 hn = s:option(Value, "hostname", translate("Hostname"))
63
64 function hn.write(self, section, value)
65         Value.write(self, section, value)
66         luci.sys.hostname(value)
67 end
68
69
70 tz = s:option(ListValue, "zonename", translate("Timezone"))
71 tz:value("UTC")
72
73 for i, zone in ipairs(luci.sys.zoneinfo.TZ) do
74         tz:value(zone[1])
75 end
76
77 function tz.write(self, section, value)
78         local function lookup_zone(title)
79                 for _, zone in ipairs(luci.sys.zoneinfo.TZ) do
80                         if zone[1] == title then return zone[2] end
81                 end
82         end
83
84         AbstractValue.write(self, section, value)
85         local timezone = lookup_zone(value) or "GMT0"
86         self.map.uci:set("system", section, "timezone", timezone)
87         luci.fs.writefile("/etc/TZ", timezone .. "\n")
88 end
89
90 s:option(Value, "log_size", translate("System log buffer size"), "kiB").optional = true
91 s:option(Value, "log_ip", translate("External system log server")).optional = true
92 s:option(Value, "log_port", translate("External system log server port")).optional = true
93 s:option(Value, "conloglevel", translate("Log output level")).optional = true
94 s:option(Value, "cronloglevel", translate("Cron Log Level")).optional = true
95
96 if has_rdate then
97         s2 = m:section(TypedSection, "rdate", translate("Time Server (rdate)"))
98         s2.anonymous = true
99         s2.addremove = false
100
101         s2:option(DynamicList, "server", translate("Server"))
102 end
103
104 return m