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
#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


Messages In This Thread
RE: Syslog differences between Resident and Event Scripts - by admin - Today, 07:49

Forum Jump: