+#!/usr/bin/lua
+
+json = require("json")
+
+function getConfig(configname)
+
+ local uci=require("uci")
+ local cur=uci.cursor()
+ local config
+ if configname then
+ config=configname
+ else
+ config="beacon"
+ end
+
+ logging = cur.get(config,"logging","enabled")
+
+ mqtt_host = cur.get(config,"mqtt","host")
+ mqtt_port = cur.get(config,"mqtt","port")
+ mqtt_id = cur.get(config,"mqtt","id")
+ mqtt_topic = cur.get(config,"mqtt","topic")
+
+ mqtt_user = cur.get(config,"mqtt","user")
+ mqtt_passwd = cur.get(config,"mqtt","password")
+
+ if mqtt_host and not mqtt_id then
+ socket = require("socket")
+ posix = require("posix")
+ hostname = socket.dns.gethostname()
+ pid = posix.getpid()
+ mqtt_id="beaconmon-"..hostname.."-"..pid
+ end
+
+ if mqtt_host and not mqtt_port then
+ mqtt_port = 1883
+ end
+
+ if mqtt_host and not mqtt_topic then
+ mqtt_topic = 'beaconmon/{type}/{details}'
+ end
+
+end
+
+function capture(cmd, raw)
+ local f = assert(io.popen(cmd, 'r'))
+ local s = assert(f:read('*a'))
+ f:close()
+ if raw then return s end
+ s = string.gsub(s, '^%s+', '')
+ s = string.gsub(s, '%s+$', '')
+ s = string.gsub(s, '[\n\r]+', ' ')
+ return s
+end
+
+function mqtt_encode(str)
+ if (str) then
+ str = string.gsub (str, "\n", "")
+ str = string.gsub (str, ":", "-")
+ str = string.gsub (str, "/", "-")
+ str = string.gsub (str, " ", "_")
+ end
+ return str
+end
+
+function printLog(str)
+ if logging=="yes" then
+ capture("logger -t beaconmon \""..str.."\"")
+ print(str)
+ elseif logging=="syslog" then
+ capture("logger -t beaconmon \""..str.."\"")
+ elseif logging=="stdout" then
+ print(str)
+ end
+end