Skip to content

Instantly share code, notes, and snippets.

@AlexanderS
Created November 27, 2015 17:02
Show Gist options
  • Save AlexanderS/3ae1280425314edcc3aa to your computer and use it in GitHub Desktop.
Save AlexanderS/3ae1280425314edcc3aa to your computer and use it in GitHub Desktop.
#!/bin/luavar
-- Last message to remember
last = { count=0 }
--last = { count=1 }
-- Filter settings
saveset = {}
table.insert(saveset,{["name"]="logger:settings/filter",["value"]="0"})
-- Hack (lua does not have a sleep build-in
function sleep(n)
os.execute("sleep " .. tonumber(n))
end
-- Parse date and time strings into timestamps
-- so that we can compare it easily
function parse_timestamp(date, time)
day,month,year=date:match("(%d+).(%d+).(%d+)")
hour,min,sec=time:match("(%d+):(%d+):(%d+)")
return os.time({day=day,month=month,year=2000 + year,
hour=hour,min=min,sec=sec})
end
-- Store the last handled message
function update_last(new, count)
date, time, message, id, type = unpack(new)
timestamp = parse_timestamp(date, time)
last = {timestamp=timestamp, message=message, id=id, type=type,
count=count}
end
function entry_equal(a, b)
if a.id ~= b.id then
return false
end
if a.message ~= b.message then
return false
end
time_min, time_max = a.timestamp - 2, a.timestamp + 2
if (time_min > b.timestamp) or (time_max < b.timestamp) then
return false
end
if a.type ~= b.type then
return false
end
return true
end
function format_event(event)
date, time, message, id, type = unpack(event)
return (date..' '..time..': '..message)
end
function send_logger(msg)
os.execute("logger -t avm '" .. msg .. "'")
end
while true do
-- get all event logs
box.set_config(saveset)
g_log = box.multiquery("logger:status/log_separate") or {}
-- if no last message is saved, use the current
if last.count == 0 then
update_last(g_log[1], 0)
end
new = 0
count = 0
for i,t in ipairs(g_log) do
msg = {timestamp=parse_timestamp(t[1], t[2]), message=t[3], id=t[4], type=t[5]}
if entry_equal(last, msg) then
count = count + 1
elseif count > 0 then
-- this is the first message, after some matching messages
break
end
new = i
end
if last.count == 0 then
last.count = count
else
if last.count < count then
new = new - last.count
else
new = new - count
end
if new >= 1 then
for i=new,1,-1 do
output = format_event(g_log[i])
--print(output)
send_logger(output)
end
if count == 0 then
count = 1
end
update_last(g_log[1], count)
end
end
sleep(60)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment