1
0

system.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. local sys = require "luci.sys"
  5. local zones = require "luci.sys.zoneinfo"
  6. local fs = require "nixio.fs"
  7. local conf = require "luci.config"
  8. local m, s, o
  9. local has_ntpd = fs.access("/usr/sbin/ntpd")
  10. local has_zram = fs.access("/etc/init.d/zram")
  11. m = Map("system", translate("System"), translate("Here you can configure the basic aspects of your device like its hostname or the timezone."))
  12. m:chain("luci")
  13. s = m:section(TypedSection, "system", translate("System Properties"))
  14. s.anonymous = true
  15. s.addremove = false
  16. s:tab("general", translate("General Settings"))
  17. s:tab("logging", translate("Logging"))
  18. s:tab("language", translate("Language and Style"))
  19. s:tab("advanced", translate("Advanced"))
  20. if has_zram then s:tab("zram", translate("ZRam Settings")) end
  21. --
  22. -- System Properties
  23. --
  24. o = s:taboption("general", DummyValue, "_systime", translate("Local Time"))
  25. o.template = "admin_system/clock_status"
  26. o = s:taboption("general", Value, "hostname", translate("Hostname"))
  27. o.datatype = "hostname"
  28. function o.write(self, section, value)
  29. Value.write(self, section, value)
  30. sys.hostname(value)
  31. end
  32. o = s:taboption("general", ListValue, "zonename", translate("Timezone"))
  33. o:value("UTC")
  34. for i, zone in ipairs(zones.TZ) do
  35. o:value(zone[1])
  36. end
  37. function o.write(self, section, value)
  38. local function lookup_zone(title)
  39. for _, zone in ipairs(zones.TZ) do
  40. if zone[1] == title then return zone[2] end
  41. end
  42. end
  43. AbstractValue.write(self, section, value)
  44. local timezone = lookup_zone(value) or "GMT0"
  45. self.map.uci:set("system", section, "timezone", timezone)
  46. fs.writefile("/etc/TZ", timezone .. "\n")
  47. end
  48. --
  49. -- Logging
  50. --
  51. o = s:taboption("logging", Value, "log_size", translate("System log buffer size"), translate("KiB"))
  52. o.optional = true
  53. o.placeholder = 16
  54. o.datatype = "uinteger"
  55. o = s:taboption("logging", Value, "log_ip", translate("External system log server"))
  56. o.optional = true
  57. o.placeholder = "0.0.0.0"
  58. o.datatype = "ip4addr"
  59. o = s:taboption("logging", Value, "log_port", translate("External system log server port"))
  60. o.optional = true
  61. o.placeholder = 514
  62. o.datatype = "port"
  63. o = s:taboption("logging", ListValue, "log_proto", translate("External system log server protocol"))
  64. o:value("udp", "UDP")
  65. o:value("tcp", "TCP")
  66. o = s:taboption("logging", Value, "log_file", translate("Write system log to file"))
  67. o.optional = true
  68. o.placeholder = "/tmp/system.log"
  69. o = s:taboption("logging", ListValue, "conloglevel", translate("Log output level"))
  70. o:value(8, translate("Debug"))
  71. o:value(7, translate("Info"))
  72. o:value(6, translate("Notice"))
  73. o:value(5, translate("Warning"))
  74. o:value(4, translate("Error"))
  75. o:value(3, translate("Critical"))
  76. o:value(2, translate("Alert"))
  77. o:value(1, translate("Emergency"))
  78. o = s:taboption("logging", ListValue, "cronloglevel", translate("Cron Log Level"))
  79. o.default = 8
  80. o:value(5, translate("Debug"))
  81. o:value(8, translate("Normal"))
  82. o:value(9, translate("Warning"))
  83. --
  84. -- Zram Properties
  85. --
  86. if has_zram then
  87. o = s:taboption("zram", Value, "zram_size_mb", translate("ZRam Size"), translate("Size of the ZRam device in megabytes"))
  88. o.optional = true
  89. o.placeholder = 16
  90. o.datatype = "uinteger"
  91. o = s:taboption("zram", ListValue, "zram_comp_algo", translate("ZRam Compression Algorithm"))
  92. o.optional = true
  93. o.placeholder = lzo
  94. o:value("lzo", "lzo")
  95. o:value("lz4", "lz4")
  96. o:value("deflate", "deflate")
  97. o = s:taboption("zram", Value, "zram_comp_streams", translate("ZRam Compression Streams"), translate("Number of parallel threads used for compression"))
  98. o.optional = true
  99. o.placeholder = 1
  100. o.datatype = "uinteger"
  101. end
  102. --
  103. -- Language & Style
  104. --
  105. o = s:taboption("language", ListValue, "_lang", translate("Language"))
  106. o:value("auto")
  107. local i18ndir = luci.i18n.i18ndir .. "base."
  108. for k, v in luci.util.kspairs(conf.languages) do
  109. local file = i18ndir .. k:gsub("_", "-")
  110. if k:sub(1, 1) ~= "." and fs.access(file .. ".lmo") then
  111. o:value(k, v)
  112. end
  113. end
  114. function o.cfgvalue(...)
  115. return m.uci:get("luci", "main", "lang")
  116. end
  117. function o.write(self, section, value)
  118. m.uci:set("luci", "main", "lang", value)
  119. end
  120. o = s:taboption("language", ListValue, "_mediaurlbase", translate("Theme"))
  121. for k, v in pairs(conf.themes) do
  122. if k:sub(1, 1) ~= "." then
  123. o:value(v, k)
  124. end
  125. end
  126. function o.cfgvalue(...)
  127. return m.uci:get("luci", "main", "mediaurlbase")
  128. end
  129. function o.write(self, section, value)
  130. m.uci:set("luci", "main", "mediaurlbase", value)
  131. end
  132. --
  133. -- Advanced
  134. --
  135. o = s:taboption("advanced", Value, "_pollinterval",
  136. translate("Polling interval"),
  137. translate("Polling interval for status queries in seconds"))
  138. o.datatype = "range(3, 20)"
  139. o.default = 5
  140. o:value("3")
  141. o:value("5")
  142. o:value("10")
  143. function o.cfgvalue(...)
  144. return m.uci:get("luci", "main", "pollinterval")
  145. end
  146. function o.write(self, section, value)
  147. m.uci:set("luci", "main", "pollinterval", value)
  148. end
  149. --
  150. -- NTP
  151. --
  152. if has_ntpd then
  153. -- timeserver setup was requested, create section and reload page
  154. if m:formvalue("cbid.system._timeserver._enable") then
  155. m.uci:section("system", "timeserver", "ntp",
  156. {
  157. server = { "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", "2.openwrt.pool.ntp.org", "3.openwrt.pool.ntp.org" }
  158. }
  159. )
  160. m.uci:save("system")
  161. luci.http.redirect(luci.dispatcher.build_url("admin/system", arg[1]))
  162. return
  163. end
  164. local has_section = false
  165. m.uci:foreach("system", "timeserver",
  166. function(s)
  167. has_section = true
  168. return false
  169. end)
  170. if not has_section then
  171. s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
  172. s.anonymous = true
  173. s.cfgsections = function() return { "_timeserver" } end
  174. x = s:option(Button, "_enable")
  175. x.title = translate("Time Synchronization is not configured yet.")
  176. x.inputtitle = translate("Set up Time Synchronization")
  177. x.inputstyle = "apply"
  178. else
  179. s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
  180. s.anonymous = true
  181. s.addremove = false
  182. o = s:option(Flag, "enable", translate("Enable NTP client"))
  183. o.rmempty = false
  184. function o.cfgvalue(self)
  185. return sys.init.enabled("sysntpd")
  186. and self.enabled or self.disabled
  187. end
  188. function o.write(self, section, value)
  189. if value == self.enabled then
  190. sys.init.enable("sysntpd")
  191. sys.call("env -i /etc/init.d/sysntpd start >/dev/null")
  192. else
  193. sys.call("env -i /etc/init.d/sysntpd stop >/dev/null")
  194. sys.init.disable("sysntpd")
  195. end
  196. end
  197. o = s:option(Flag, "enable_server", translate("Provide NTP server"))
  198. o:depends("enable", "1")
  199. o = s:option(DynamicList, "server", translate("NTP server candidates"))
  200. o.datatype = "host(0)"
  201. o:depends("enable", "1")
  202. -- retain server list even if disabled
  203. function o.remove() end
  204. end
  205. end
  206. return m