Today, 07:49
Sharing socket between scripts is not possible. You can simply create a new socket for each slog() call.
Add the following code to Common functions to make slog() function available for all scripts.
Add the following code to Common functions to make slog() function available for all scripts.
Code:
local function rfc5424_time()
local now = os.time()
local utc = os.time(os.date("!*t", now))
local offset = os.difftime(now, utc)
local sign = "+"
if offset < 0 then sign = "-" end
offset = math.abs(offset)
local hours = math.floor(offset / 3600)
local mins = math.floor((offset % 3600) / 60)
return os.date("%Y-%m-%dT%H:%M:%S") ..
string.format("%s%02d:%02d", sign, hours, mins)
end
function slog(fields)
local SYSLOG_HOST = "192.168.0.2"
local SYSLOG_PORT = 1514
local HOSTNAME = "logicmachine"
local APPNAME = "solar-master"
local timestamp = rfc5424_time()
local msg = "<14>1 "..timestamp.." "..HOSTNAME.." "..APPNAME.." - - - "..fields
local sock = require('socket').udp()
sock:sendto(msg, SYSLOG_HOST, SYSLOG_PORT)
sock:close()
end