Do you know how often Vera does a Luup Restart/Reload? You may not care too much but it can be an early warning of problems.
The fine EventWatcher plugin will tell you when Vera was last rebooted and the time of the last Luup Restart and can also log this along with your other events in a designated file and/or on a SysLog server. Sometimes, though, a dedicated log can be useful.
The following code, placed in Startup Lua, maintains a simple text log of each Luup restart/reload. Along with the date and time of the restart, each entry shows the time since the previous restart (uptime) and the state of the network connection (LAN + Internet). This last part can indicate if Vera did a Luup restart because it could not access the network (an issue I have had). To avoid an ever-growing file, the log is purged of entries older than 30 days (adjustable).
The log can be accessed from a browser with the following URL. Change to the IP address of your Vera (without the <>):
http://<veraip>/restarts.txt
Log entries look like this:
[b]17 Mar 2015 10:27:51.891 Restart. Uptime: 0 Days 12 Hrs 15 Mins. Network: OK[/b]
The code to be added to the end of Startup Lua:
[code]-- Log each restart.
local function restartLog(message, keepdays)
local lfs = require “lfs”
local socket = require(“socket”)
local time = socket.gettime() or os.time()
local tms = string.format(“.%03d “,math.floor (1000 * (time % 1)))
local stamp = os.date(”%d %b %Y %T”,math.floor(time)) … tms
local baseFile = “/www/restarts”
local logFile = baseFile … “.txt”
local lastLog = lfs.attributes(logFile,“modification”) or time
– Log this restart
local uptime = time - lastLog
local uptimeStr = string.format("Uptime: %d Days %d Hrs %d Mins. “,math.floor(uptime / 86400),
math.floor((uptime % 86400) / 3600), math.floor((uptime % 3600) / 60))
local file = io.open(logFile, “a+”)
file:write(stamp … “Restart. " … uptimeStr … (message or “”) … “\n”)
file:close()
– Remove old entries
local prevDays = keepdays or 30 – Days to keep in addition to today.
local tmpFile = baseFile … “.tmp”
local mTxt={Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12}
local dt = {}
local today = time - (time % 86400)
local cut = today - ( prevDays * 86400)
local tmpF,tmpE = io.open(tmpFile,“w+”)
if tmpF ~= nil then
for line in io.lines(logFile) do
dStr,mStr,yStr = string.match(line,”^(%d+)%s(%a+)%s(%d+)%s”)
dt.month = mTxt[mStr]
dt.year = tonumber(yStr)
dt.day =tonumber(dStr)
date = os.time(dt)
if (date >= cut) then tmpF:write(line … “\n”) end
end
tmpF:close()
local retn,err = os.rename(tmpFile, logFile)
end
lfs.touch(logFile,time)
end
local netOK = “OK”
if os.execute(“ping -c 1 8.8.8.8”) ~= 0 then netOK = “Offline” end
restartLog("Network: " … netOK, 30) – Last argument is log history in days.
[/code]
Edit: 17/03/2015 23:08 Code updated to prevent error when first run.
Edit: 19/03/2015 14:48 Changed terminology.