Skip to content

Instantly share code, notes, and snippets.

@davidbirdsong
Last active June 24, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidbirdsong/8ca702499658bf93bf594bbb7a4946fc to your computer and use it in GitHub Desktop.
Save davidbirdsong/8ca702499658bf93bf594bbb7a4946fc to your computer and use it in GitHub Desktop.
tcp syslog test
local coroutine = require('coroutine')
local socket = require('socket')
local string = require('string')
local table = require('table')
local io = require('io')
local write = io.write
local syslog_grammar, address, port
if read_config then
if read_config('syslog_format') then
local syslog = require("syslog")
syslog_grammar = syslog.build_rsyslog_grammar(
read_config('syslog_format')
)
end
address = read_config("address") or "127.0.0.1"
port = read_config("port") or 5565
end
local server = assert(socket.bind(address, port))
server:settimeout(0)
local threads = {}
local sockets = {server}
local logger = read_config("Logger")
local function rsyslog_msg(buf)
local msg = {
Type = "syslog",
Payload = nil,
}
msg.fields = syslog_grammar:match(buf)
if not msg.fields then return nil end
-- short circuit
msg.Fields = msg.fields
msg.fields = nil
if true then return msg end
msg.Payload = msg.fields.msg
msg.fields.msg = nil
if msg.fields.timestamp then msg.Timestamp = msg.fields.timestamp end
msg.fields.timestamp = nil
if msg.fields.syslogtag then
msg.Logger = msg.fields.syslogtag.programname
msg.Pid = msg.fields.syslogtag.pid
end
msg.Fields = msg.fields
msg.fields = nil
return msg
end
local function handle_client(client, caddr, cport)
local found, consumed, need = false, 0, 8192 * 4
client:settimeout(0)
write('in handle client', "\n")
local m
while client do
write("client receive", "\n")
local buf, err= client:receive('*l')
write("buf:", buf, "\n")
if err == "closed" then
write("connection closed", "\n")
return
end
if not buf then
write("connection return emptey buf", "\n")
end
m = rsyslog_msg(buf)
if m then
write("injecting", m.Payload, "\n")
--local l, e = pcall(inject_message, m)
inject_message( m)
write("injected !!!!!","\n")
write("injected !!!!!", l, e, "\n")
end
coroutine.yield()
end
write("falling off", "\n")
end
function process_message()
while true do
local ready = socket.select(sockets, nil, 1)
if ready then
for _, s in ipairs(ready) do
if s == server then
local client = s:accept()
if client then
local caddr, cport = client:getpeername()
write('new conn [', caddr, ':', cport, "]\n")
if not caddr then
caddr = "unknown"
cport = 0
end
sockets[#sockets + 1] = client
threads[client] = coroutine.create(
function()
handle_client(client, caddr, cport)
end
)
write('new coroutine', "\n")
end
else
if threads[s] then
write('coroutine resume', "\n")
local status = coroutine.resume(threads[s])
write('coroutine resume result: ', tostring(status), "\n")
if not status then
s:close()
write('closing conn', "\n")
for i = #sockets, 2, -1 do
if s == sockets[i] then
table.remove(sockets, i)
break
end
end
threads[s] = nil
end
else
write('s not in threads', "\n")
end
end
end
end
end
return 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment