This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Syslog differences between Resident and Event Scripts
#1
I have the exact same Syslog set up in my Resident and Event Scripts - the Resident Scripts Syslog works, however the Event scripts doesnt

The syslog set up that works in Resident is 

local socket = require("socket")

-------------------------------------------------
-- SYSLOG CONFIG
-------------------------------------------------

local SYSLOG_HOST = "192.168.1.51"
local SYSLOG_PORT = 1514

local HOSTNAME = "logicmachine"
local APPNAME  = "solar-master"

local LOG_INTERVAL = 60  -- seconds

-------------------------------------------------
-- PERSISTENT UDP SOCKET
-------------------------------------------------

storage.syslog_socket = storage.syslog_socket or socket.udp()

local udp = storage.syslog_socket
udp:settimeout(0)

-------------------------------------------------
-- RFC5424 TIMESTAMP
-------------------------------------------------

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

-------------------------------------------------
-- SYSLOG SENDER
-------------------------------------------------

local function slog(fields)

  local timestamp = rfc5424_time()

  local msg = "<14>1 "..timestamp.." "..HOSTNAME.." "..APPNAME.." - - - "..fields

  udp:sendto(msg, SYSLOG_HOST, SYSLOG_PORT)

end

What needs to change for an Event Script to behave the same way and allow for logging to an external syslog server?

Thanks
Reply
#2
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.
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
Reply


Forum Jump: