Skip to content

Instantly share code, notes, and snippets.

@AlexanderS
Created November 27, 2015 18:27
Show Gist options
  • Save AlexanderS/22e15dd18db1196f8e27 to your computer and use it in GitHub Desktop.
Save AlexanderS/22e15dd18db1196f8e27 to your computer and use it in GitHub Desktop.
#!/bin/luavar
statefile = '/var/media/ftp/UStor01/log.state'
debug = false
-- 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
-- Compare two events
function event_equal(a, b)
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.id ~= b.id) or (a.message ~= b.message) or (a.type ~= b.type) then
return false
end
return true
end
-- Format the event for output
function format_event(event)
date, time, message, id, type = unpack(event)
return (date..' '..time..': '..message)
end
-- Send a message to syslog
function send_syslog(msg)
os.execute("logger -t avm '" .. msg .. "'")
end
-- Read state from file (can fail)
function read_statefile(statefile)
file = io.open(statefile, "r")
timestamp = file:read("*l")
message = file:read("*l")
id = file:read("*l")
type = file:read("*l")
count = tonumber(file:read("*l"))
file:close()
return { timestamp = timestamp
, message = message
, id = id
, type = type
, count = count }
end
-- Read state from file or get an initial state
function get_state(statefile, event)
success, state = pcall(read_statefile, statefile)
if success then
return state
end
return { timestamp = parse_timestamp(event[1], event[2])
, message = event[3]
, id = event[4]
, type = event[5]
, count = 0 }
end
-- Write a new state to the file
function write_state(event, count, statefile)
date, time, message, id, type = unpack(event)
timestamp = parse_timestamp(date, time)
file = io.open(statefile, "w")
file:write(timestamp .. "\n")
file:write(message .. "\n")
file:write(id .. "\n")
file:write(type .. "\n")
file:write(count .. "\n")
file:close()
end
function main()
-- get all event logs
saveset = {}
table.insert(saveset,{["name"]="logger:settings/filter",["value"]="0"})
box.set_config(saveset)
log = box.multiquery("logger:status/log_separate") or {}
state = get_state(statefile, log[1])
new = 0
count = 0
for i,event in ipairs(log) do
msg = { timestamp = parse_timestamp(event[1], event[2])
, message = event[3]
, id = event[4]
, type = event[5] }
if event_equal(state, msg) then
-- count equal matching messages
count = count + 1
elseif count > 0 then
-- this is the first message, after some matching messages
break
end
new = i
end
if state.count == 0 then
write_state(log[1], count, statefile)
else
if state.count < count then
new = new - state.count
else
new = new - count
end
if new >= 1 then
for i = new,1,-1 do
output = format_event(log[i])
if debug then
print(output)
else
send_syslog(output)
end
end
write_state(log[1], count, statefile)
end
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment