From ba91934c30d2c8d5f83c80ec08a15ffd7e526216 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Wed, 5 Dec 2007 16:48:44 +0000 Subject: Renamed some variables for better understanding. Splitted up first page from settings and advanced. git-svn-id: svn://svn.alpinelinux.org/acf/openntpd/trunk@411 ab2d0c66-481e-0410-8bed-d214d4d58bed --- openntpd-advanced-html.lsp | 28 ++++++++++-------- openntpd-controller.lua | 37 +++++++++++++++++------- openntpd-model.lua | 49 ++++++++++++++++++++++--------- openntpd-read-html.lsp | 72 ++++++++-------------------------------------- 4 files changed, 91 insertions(+), 95 deletions(-) diff --git a/openntpd-advanced-html.lsp b/openntpd-advanced-html.lsp index 5cb8104..33f59e5 100644 --- a/openntpd-advanced-html.lsp +++ b/openntpd-advanced-html.lsp @@ -1,18 +1,15 @@ -

System time

-

MENU

+

SYSTEM INFO

+ +
Process status
+
-
-
Go back to start page
+
Start or stop process
+

Details

@@ -25,15 +22,22 @@ io.write(debugs.variables(view))
Last modified
- +
Error message
-
+

Content

- +
+ + diff --git a/openntpd-controller.lua b/openntpd-controller.lua index 06b53b2..8b1fe04 100644 --- a/openntpd-controller.lua +++ b/openntpd-controller.lua @@ -3,6 +3,7 @@ module (..., package.seeall) -- Cause an http redirect to our "read" action -- We use the self.conf table because it already has prefix,controller,etc -- The redir code is defined in the application error handler (acf-controller) +require("posix") local list_redir = function (self) self.conf.action = "read" self.conf.type = "redir" @@ -17,25 +18,41 @@ mvc.on_load = function(self, parent) end -read = function (self) +settings = function (self) local cmd = self.clientdata.cmd - if ( cmd == "start" ) then - return ( {filecontent = self.model:get(), startup = self.model:startstop_service( cmd ), shutdown = "", url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller } ) - elseif ( cmd == "stop" ) then - return ( {filecontent = self.model:get(), shutdown = self.model:startstop_service( cmd ), startup = "", url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller } ) + local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller + if ( cmd ~= nil ) then + local startstop = self.model:startstop_service( cmd ) + posix.sleep(1) -- Wait for the process to start|stop + local statusinfo = self.model:get() + return ( {startstop = startstop, statusinfo = statusinfo, url = url } ) else - return ( {filecontent = self.model:get(), shutdown = "" , startup = "", url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller } ) + return ( {statusinfo = self.model:get(), startstop = "", url = url } ) end end +read = function (self) + local cmd = self.clientdata.cmd + local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller + return ( {statusinfo = self.model:get(), url = url } ) +end + advanced = function (self) local filecontent = self.clientdata.modifications or "" + local cmd = self.clientdata.cmd + local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller + if ( filecontent ~= "") then - local me = ( {filecontent = self.model:update_filecontent(filecontent), url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller } ) - list_redir(self) + local me = ( {filecontent = self.model:update_filecontent(filecontent), url = url } ) + return ( {startstop = "", statusinfo = self.model:get(), filecontent = self.model:get_filecontent(), url = url } ) else - local me = ( {filecontent = self.model:get_filecontent(), url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller } ) - return me + if ( cmd ~= nil ) then + local startstop = self.model:startstop_service( cmd ) + posix.sleep(1) -- Wait for the process to start|stop + return ( {startstop = startstop, statusinfo = self.model:get(), filecontent = self.model:get_filecontent(), url = url } ) + else + return ( {startstop = "", statusinfo = self.model:get(), filecontent = self.model:get_filecontent(), url = url } ) + end end end diff --git a/openntpd-model.lua b/openntpd-model.lua index 2db000a..14aad55 100644 --- a/openntpd-model.lua +++ b/openntpd-model.lua @@ -3,6 +3,7 @@ module (..., package.seeall) require("format") require("date") require("fs") +require("posix") local ntpdconfig = "/etc/ntpd.conf" local progname = "openntpd" @@ -61,58 +62,80 @@ local function get_version () end local is_running = function( process ) - local statusreport = "" - local pidofsx, error = io.popen("pidof " .. process ,r) - local pidofs = format.string_to_table(" ", string.gsub(pidofsx:read("*a"), "\n", "")) - pidofsx:close() - if (table.maxn(pidofs) > 1) then + local statusreport = nil + local cmdoutput = {} + local cmd, error = io.popen("pidof " .. process ,r) + local cmdoutput = string.gsub(cmd:read("*a"), "%s", "") + cmd:close() + if (cmdoutput ~= "") then statusreport = "Running" - elseif (table.maxn(pidofs) == 1) then - statusreport = "Unknown (Only 1 process running, this might be wrong)" else statusreport = "Stopped" end return statusreport end +local last_time_change = function() + local cmdoutput = {} + local cmd, error = io.popen("cat /var/log/messages | grep ntpd | grep adjusting | tail -1" ,r) + local cmdoutput1,cmdoutput2 = string.match(cmd:read("*a"), "^%s*(%S+%s+%S+%s+%S+%s+).*: (.*)$") + cmd:close() +-- if not (cmdoutput1) then cmdouput + cmdoutput1 = cmdoutput1 or "(Have no data on updates)" + cmdoutput2 = cmdoutput2 or "" + return cmdoutput1 .. cmdoutput2 +end + -- ################################################################################ -- PUBLIC FUNCTIONS function startstop_service ( self, state ) local status = {} - local f,err = io.popen("/etc/init.d/ntpd " .. state) + -- This is a strange hack to get the init.d script working + local f,err = io.popen("PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin /etc/init.d/ntpd " .. state) local config = f:read("*a") f:close() if (config == "") then config = "OK!" end - status.status = "Command:" .. state .." Result:" ..config + status.status = config return status end function get () local path = ntpdconfig + local config = {} if not (fs.is_file(path)) then local file_result,err = fs.write_file(path, "") end - config = config_content ( path ) + local startstop = is_running ("ntpd") + local config = config_content ( path ) + if (config["listen"]) then + server = "Yes" + else + server = "No" + end config["version"] = string.match(get_version(), "^(%S*)" ) - config["status"] = is_running ("ntpd") config["date"] = os.date() + config["status"] = startstop + config["listenstate"] = server + config["timechanged"] = last_time_change() config["timezone"] = date.what_tz() return config end function get_filecontent (self) local path = ntpdconfig - file_content = nil + local file_content = get() if (fs.is_file(path)) then local filedetails = file_info(path) + local configstatus = get() local file = io.open( path ) local file_result = file:read("*a") or "unknown" + file_content["filedetails"]=filedetails + file_content["value"]=file_result file:close() - file_content = {name=name, value=file_result, filedetails=filedetails} else file_content = {value="", errtxt="File is missing, but will be created when you save your new settings",filedetails={longname=path, size="0", mtimelong=""}} end diff --git a/openntpd-read-html.lsp b/openntpd-read-html.lsp index 08c5336..4a3a9d9 100644 --- a/openntpd-read-html.lsp +++ b/openntpd-read-html.lsp @@ -5,76 +5,28 @@

SYSTEM INFO

Program version
-
+
Process status
-
+
+ +
Configured as server (listens)
+
Current time
-
+
+ +
Latest time adjustment
+
- +
TimeZone
-
+
-

NTPD SETTINGS

- -
Set time immediately at startup
-
- -

'SET TIME' OPTIONS

-
Timeserver hosts...
-
-
-In most cases you could use pool.ntp.org or [countryname].pool.ntp.org (if listed in http://www.pool.ntp.org/). -
- -
Delete selected host
-
(see above)
- -
Add new host
-
- -

'PRESENT TIME' OPTIONS (ACT AS TIME SERVER)

-
Listen on address...
-
-
Empty list = Not listening or acting as server; "*" = All local addresses -
- -
Delete selected address
-
(see above)
- - -
Add new listen address
-
- - -

MANAGEMENT

- -
Start NTPD
-
- -
Stop NTPD
-
-