c55ec0497e72d4a12cdea2590c982da3f56d3a04
[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 function m.on_parse()
23 local has_rdate = false
24
25 m.uci:foreach("system", "rdate",
26 function()
27 has_rdate = true
28 return false
29 end)
30
31 if not has_rdate then
32 m.uci:section("system", "rdate", nil, { })
33 m.uci:save("system")
34 end
35 end
36
37
38 s = m:section(TypedSection, "system", "")
39 s.anonymous = true
40 s.addremove = false
41
42 local system, model, memtotal, memcached, membuffers, memfree = luci.sys.sysinfo()
43 local uptime = luci.sys.uptime()
44
45 s:option(DummyValue, "_system", translate("System")).value = system
46 s:option(DummyValue, "_cpu", translate("Processor")).value = model
47
48 local load1, load5, load15 = luci.sys.loadavg()
49 s:option(DummyValue, "_la", translate("Load")).value =
50 string.format("%.2f, %.2f, %.2f", load1, load5, load15)
51
52 s:option(DummyValue, "_memtotal", translate("Memory")).value =
53 string.format("%.2f MB (%.0f%% %s, %.0f%% %s, %.0f%% %s)",
54 tonumber(memtotal) / 1024,
55 100 * memcached / memtotal,
56 tostring(translate("cached")),
57 100 * membuffers / memtotal,
58 tostring(translate("buffered")),
59 100 * memfree / memtotal,
60 tostring(translate("free"))
61 )
62
63 s:option(DummyValue, "_systime", translate("Local Time")).value =
64 os.date("%c")
65
66 s:option(DummyValue, "_uptime", translate("Uptime")).value =
67 luci.tools.webadmin.date_format(tonumber(uptime))
68
69 hn = s:option(Value, "hostname", translate("Hostname"))
70
71 function hn.write(self, section, value)
72 Value.write(self, section, value)
73 luci.sys.hostname(value)
74 end
75
76
77 tz = s:option(ListValue, "zonename", translate("Timezone"))
78 tz:value("UTC")
79
80 for i, zone in ipairs(luci.sys.zoneinfo.TZ) do
81 tz:value(zone[1])
82 end
83
84 function tz.write(self, section, value)
85 local function lookup_zone(title)
86 for _, zone in ipairs(luci.sys.zoneinfo.TZ) do
87 if zone[1] == title then return zone[2] end
88 end
89 end
90
91 AbstractValue.write(self, section, value)
92 local timezone = lookup_zone(value) or "GMT0"
93 self.map.uci:set("system", section, "timezone", timezone)
94 luci.fs.writefile("/etc/TZ", timezone .. "\n")
95 end
96
97 s:option(Value, "log_size", nil, "kiB").optional = true
98 s:option(Value, "log_ip").optional = true
99 s:option(Value, "conloglevel").optional = true
100 s:option(Value, "cronloglevel").optional = true
101
102 s2 = m:section(TypedSection, "rdate", translate("Time Server (rdate)"))
103 s2.anonymous = true
104 s2.addremove = false
105
106 s2:option(DynamicList, "server", translate("Server"))
107
108 return m