「(続)NVR500でDynDNSの登録更新をしてみた」で見つかった実行時間誤差を確認した結果「NVR500のsyslogwatch関数timeout値誤差」が原因でした。
毎日指定した時刻にDynDNSに登録更新ができるようにsyslogwatchのタイムアウト時間の誤差修正を行い指定するように変更してみた。
8日間実行した結果、+-1秒以内で指定時間に実行させることが出来た。
実行させたLUAスクリプトは、
の通り
毎日指定した時刻にDynDNSに登録更新ができるようにsyslogwatchのタイムアウト時間の誤差修正を行い指定するように変更してみた。
2012/02/23 21:58:17: [LUA] dyndns-update.lua Starting up(if=PP[01]:121.xxx.yyy.130)
2012/02/23 21:58:17: [LUA] dyndns-update.lua dTime= 11803
2012/02/23 21:58:18: [LUA] dyndns-update.lua corTime= -13
2012/02/23 21:58:18: [LUA] dyndns-update.lua Next checking after 11790sec
2012/02/24 01:15:00: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/02/24 01:15:00: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/02/24 01:15:01: [LUA] dyndns-update.lua dTime= 86400
2012/02/24 01:15:01: [LUA] dyndns-update.lua corTime= -90
2012/02/24 01:15:01: [LUA] dyndns-update.lua Next checking after 86310sec
2012/02/25 01:15:00: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/02/25 01:15:00: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/02/25 01:15:00: [LUA] dyndns-update.lua dTime= 86400
2012/02/25 01:15:00: [LUA] dyndns-update.lua corTime= -90
2012/02/25 01:15:00: [LUA] dyndns-update.lua Next checking after 86310sec
2012/02/26 01:15:01: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/02/26 01:15:01: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/02/26 01:15:01: [LUA] dyndns-update.lua dTime= 86399
2012/02/26 01:15:01: [LUA] dyndns-update.lua corTime= -90
2012/02/26 01:15:01: [LUA] dyndns-update.lua Next checking after 86309sec
2012/02/27 01:15:00: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/02/27 01:15:01: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/02/27 01:15:01: [LUA] dyndns-update.lua dTime= 86399
2012/02/27 01:15:01: [LUA] dyndns-update.lua corTime= -90
2012/02/27 01:15:01: [LUA] dyndns-update.lua Next checking after 86309sec
2012/02/28 01:14:59: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/02/28 01:14:59: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/02/28 01:14:59: [LUA] dyndns-update.lua dTime= 86401
2012/02/28 01:14:59: [LUA] dyndns-update.lua corTime= -91
2012/02/28 01:14:59: [LUA] dyndns-update.lua Next checking after 86310sec
2012/02/29 01:15:00: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/02/29 01:15:02: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/02/29 01:15:02: [LUA] dyndns-update.lua dTime= 86398
2012/02/29 01:15:02: [LUA] dyndns-update.lua corTime= -90
2012/02/29 01:15:02: [LUA] dyndns-update.lua Next checking after 86308sec
2012/03/01 01:14:59: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/03/01 01:15:00: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/03/01 01:15:00: [LUA] dyndns-update.lua dTime= 86400
2012/03/01 01:15:00: [LUA] dyndns-update.lua corTime= -90
2012/03/01 01:15:00: [LUA] dyndns-update.lua Next checking after 86310sec
2012/03/02 01:14:59: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/03/02 01:15:00: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/03/02 01:15:00: [LUA] dyndns-update.lua dTime= 86400
2012/03/02 01:15:00: [LUA] dyndns-update.lua corTime= -90
2012/03/02 01:15:00: [LUA] dyndns-update.lua Next checking after 86310sec
2012/03/03 01:15:00: [LUA] dyndns-update.lua DynDNS force update IP=121.xxx.yyy.130
2012/03/03 01:15:00: [LUA] dyndns-update.lua DynDNS Updated IP= 121.xxx.yyy.130
2012/03/03 01:15:01: [LUA] dyndns-update.lua dTime= 86400
2012/03/03 01:15:01: [LUA] dyndns-update.lua corTime= -90
2012/03/03 01:15:01: [LUA] dyndns-update.lua Next checking after 86310sec
8日間実行した結果、+-1秒以内で指定時間に実行させることが出来た。
実行させたLUAスクリプトは、
--[[
Feb.23 '2012 Rabbit51
]]
--------------------------## 設定値 ##--------------------------------
-- debug
dbg = false
-- DynDNS user account
username="your dyndns account id"
password="your dyndns account password"
UpFQDN="registerd fqdn"
-- IP取得するPP番号
PPnum=1
-- 実行指定時間(hh:mm)
execTime="01:15"
-- syslogwatch time 86400/day, max 864000(10days)
-- WatchInterval * UpdateInterval
WatchInterval=86400
CorectionTime = -90
UpdateInterval=1
WaitToStart=WatchInterval+CorectionTime
-- DynDNS server address
UpHost="members.dyndns.org"
UpPort="80"
UpIP=""
UpUrl="http://" .. UpHost .. "/nic/update?hostname=" .. UpFQDN .. "&myip=" .. UpIP .. "&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
----------------------## 設定値ここまで ##----------------------------
------------------------------------------------------------
-- 指定された時間までの秒数を返す関数(1/960秒補正) --
------------------------------------------------------------
function diffTimeToStart(sTime)
local dTime, corTime
local x={string.split(sTime,/:/)}
local nt = os.time()
local setTt=os.date("*t",nt)
setTt.hour=x[1]
setTt.min=x[2]
setTt.sec=0
if os.difftime(os.time(setTt),nt) <= 60 then
setTt=os.date("*t",os.time(setTt)+86400)
end
dTime = os.difftime(os.time(setTt),nt)
if ( dbg ) then rt.syslog("info","[LUA] dyndns-update.lua dTime= " .. dTime) end
corTime = (dTime*CorectionTime)/WatchInterval
if ( dbg ) then rt.syslog("info","[LUA] dyndns-update.lua corTime= " .. corTime) end
return ( dTime + corTime )
end
------------------------------------------------------------
-- 指定されたPPのPPPで取得したlocal IPを返す関数 --
------------------------------------------------------------
function get_local_ip(id)
local rtn, str, ipadr
local cmd = "show status pp " .. tostring(id)
local ptn = "PP IP Address Local:%s+(%d+%.%d+%.%d+%.%d+)"
rtn, str = rt.command(cmd)
if (rtn) and (str) then
ipadr = str:match(ptn)
if (ipadr == nil) then
rtn =false
ipadr = "接続されていません"
end
else
rtn = false
ipadr = cmd .. "コマンド実行失敗rn"
end
return rtn, ipadr
end
------------------------------------------------------------
-- IPアドレスが変化したか確認する関数 --
------------------------------------------------------------
function is_new(ip)
local blip
blip=os.getenv("GLOBALIP")
if (blip) then
if ( blip == ip ) then
return false
else
rt.command("set GLOBALIP=" .. ip)
return true
end
else
rt.command("set GLOBALIP=" .. ip)
return true
end
end
------------------------------------------------------------
-- HttpでDynDNSにUpdateする関数 --
------------------------------------------------------------
function Update_DynDNS(ip)
local rtn, resp
UpIP = ip
req_table = {
url = UpUrl,
server_port = UpPort,
method = "GET",
auth_type = "basic",
auth_name = username,
auth_pass = password
}
resp_table = rt.httprequest(req_table)
if ( resp_table.rtn1) then
rt.syslog("info","[LUA] dyndns-update.lua DynDNS Updated IP= " .. ip )
else
rt.syslog("info","[LUA] dyndns-update.lua DynDNS Failed - " .. "nrtn1-> " .. tostring(resp_table.rtn1) .. "\nrtn2-> " .. tostring(resp_table.rtn2) .. "\nerr-> " .. tostring(resp_table.err) .. "\ncode-> " .. tostring(resp_table.code) .. "\nheader-> " .. tostring(resp_table.header) .. "\nbody-> " .. tostring(resp_table.body) )
end
end
------------------------------------------------------------
-- main --
------------------------------------------------------------
local rtn,str,lip,cnt
cnt=0
rtn,str=get_local_ip(PPnum)
rt.syslog("info","[LUA] dyndns-update.lua Starting up(if=" .. string.format("PP[%02d]:",PPnum) .. str .. ")" )
if (rtn) then
lip=str
if (is_new(lip)) then
if (dbg) then print("-- Update_DynDNS(lip)") else Update_DynDNS(lip) end
end
end
WaitToStart=diffTimeToStart(execTime)
rt.syslog("info","[LUA] dyndns-update.lua Next checking after " .. WaitToStart .. "sec" )
while (true) do
local rtn,str,lip
rtn,str = rt.syslogwatch(string.format("PP%%[%02d%%]",PPnum) .. " PPP/IPCP up%s+%(Local:%s+(%d+%.%d+%.%d+%.%d+)",1,WaitToStart)
if (rtn == 0) then
cnt = cnt + 1
if (dbg) then print("Count= " .. tostring(cnt)) end
if ( cnt == UpdateInterval ) then
cnt = 0
rt.syslog("info","[LUA] dyndns-update.lua DynDNS force update IP=" .. os.getenv("GLOBALIP") )
if (dbg) then
print("Count reach to " .. tostring(UpdateInterval) .. " DynUpdate IP=" .. os.getenv("GLOBALIP"))
else
Update_DynDNS(os.getenv("GLOBALIP"))
end
end
WaitToStart=diffTimeToStart(execTime)
rt.syslog("info","[LUA] dyndns-update.lua Next checking after " .. WaitToStart .. "sec" )
else
lip=string.match(str[rtn],"Local:%s+(%d+%.%d+%.%d+%.%d+)")
rt.syslog("info","[LUA] dyndns-update.lua New Connection(old IP/new IP):(" .. os.getenv("GLOBALIP") .. "/" .. lip .. ")" )
if (is_new(lip)) then
if (dbg) then print("DynDNS Update call in while loop\n") else Update_DynDNS(lip) end
cnt = 0
end
WaitToStart=diffTimeToStart(execTime)
rt.syslog("info","[LUA] dyndns-update.lua Next checking after " .. WaitToStart .. "sec" )
end
end
の通り