mirror of
https://github.com/gSpotx2f/luci-app-internet-detector.git
synced 2025-12-07 12:06:54 +03:00
Fixed syslog
This commit is contained in:
18
README.md
18
README.md
@@ -17,22 +17,22 @@ Internet-detector is an application for checking the availability of the Interne
|
|||||||
|
|
||||||
**OpenWrt >= 21.02:**
|
**OpenWrt >= 21.02:**
|
||||||
|
|
||||||
wget --no-check-certificate -O /tmp/internet-detector_0.4-1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector_0.4-1_all.ipk
|
wget --no-check-certificate -O /tmp/internet-detector_0.4-2_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/internet-detector_0.4-2_all.ipk
|
||||||
opkg install /tmp/internet-detector_0.4-1_all.ipk
|
opkg install /tmp/internet-detector_0.4-2_all.ipk
|
||||||
rm /tmp/internet-detector_0.4-1_all.ipk
|
rm /tmp/internet-detector_0.4-2_all.ipk
|
||||||
/etc/init.d/internet-detector start
|
/etc/init.d/internet-detector start
|
||||||
/etc/init.d/internet-detector enable
|
/etc/init.d/internet-detector enable
|
||||||
|
|
||||||
wget --no-check-certificate -O /tmp/luci-app-internet-detector_0.4-1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-app-internet-detector_0.4-1_all.ipk
|
wget --no-check-certificate -O /tmp/luci-app-internet-detector_0.4-2_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-app-internet-detector_0.4-2_all.ipk
|
||||||
opkg install /tmp/luci-app-internet-detector_0.4-1_all.ipk
|
opkg install /tmp/luci-app-internet-detector_0.4-2_all.ipk
|
||||||
rm /tmp/luci-app-internet-detector_0.4-1_all.ipk
|
rm /tmp/luci-app-internet-detector_0.4-2_all.ipk
|
||||||
/etc/init.d/rpcd restart
|
/etc/init.d/rpcd restart
|
||||||
|
|
||||||
i18n-ru:
|
i18n-ru:
|
||||||
|
|
||||||
wget --no-check-certificate -O /tmp/luci-i18n-internet-detector-ru_0.4-1_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-i18n-internet-detector-ru_0.4-1_all.ipk
|
wget --no-check-certificate -O /tmp/luci-i18n-internet-detector-ru_0.4-2_all.ipk https://github.com/gSpotx2f/packages-openwrt/raw/master/current/luci-i18n-internet-detector-ru_0.4-2_all.ipk
|
||||||
opkg install /tmp/luci-i18n-internet-detector-ru_0.4-1_all.ipk
|
opkg install /tmp/luci-i18n-internet-detector-ru_0.4-2_all.ipk
|
||||||
rm /tmp/luci-i18n-internet-detector-ru_0.4-1_all.ipk
|
rm /tmp/luci-i18n-internet-detector-ru_0.4-2_all.ipk
|
||||||
|
|
||||||
**OpenWrt 19.07:**
|
**OpenWrt 19.07:**
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk
|
|||||||
|
|
||||||
PKG_NAME:=internet-detector
|
PKG_NAME:=internet-detector
|
||||||
PKG_VERSION:=0.4
|
PKG_VERSION:=0.4
|
||||||
PKG_RELEASE:=1
|
PKG_RELEASE:=2
|
||||||
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>
|
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>
|
||||||
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
|
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,10 @@
|
|||||||
|
|
||||||
local Config = {
|
local Config = {
|
||||||
["mode"] = 2,
|
["mode"] = 2,
|
||||||
["enableLogger"] = 1,
|
["enableLogger"] = true,
|
||||||
["enableUpScript"] = 0,
|
["enableUpScript"] = false,
|
||||||
["enableDownScript"] = 0,
|
["enableDownScript"] = false,
|
||||||
["enableRunScript"] = 0,
|
["enableRunScript"] = false,
|
||||||
["intervalUp"] = 30,
|
["intervalUp"] = 30,
|
||||||
["intervalDown"] = 5,
|
["intervalDown"] = 5,
|
||||||
["connectionAttempts"] = 1,
|
["connectionAttempts"] = 1,
|
||||||
@@ -37,7 +37,6 @@ local Config = {
|
|||||||
["tcpPort"] = 53,
|
["tcpPort"] = 53,
|
||||||
["checkType"] = 0, -- 0: TCP, 1: ping
|
["checkType"] = 0, -- 0: TCP, 1: ping
|
||||||
["loggerLevel"] = "info",
|
["loggerLevel"] = "info",
|
||||||
--["loggerCmd"] = "logger",
|
|
||||||
["modules"] = {},
|
["modules"] = {},
|
||||||
}
|
}
|
||||||
Config.configDir = "/etc/" .. Config.appName
|
Config.configDir = "/etc/" .. Config.appName
|
||||||
@@ -48,7 +47,7 @@ Config.runScript = Config.configDir .. "/" .. "run-script"
|
|||||||
Config.pidFile = Config.commonDir .. "/" .. Config.appName .. ".pid"
|
Config.pidFile = Config.commonDir .. "/" .. Config.appName .. ".pid"
|
||||||
Config.statusFile = Config.commonDir .. "/" .. Config.appName .. ".status"
|
Config.statusFile = Config.commonDir .. "/" .. Config.appName .. ".status"
|
||||||
|
|
||||||
-- Import packages
|
-- Importing packages
|
||||||
|
|
||||||
local function prequire(package)
|
local function prequire(package)
|
||||||
local retVal, pkg = pcall(require, package)
|
local retVal, pkg = pcall(require, package)
|
||||||
@@ -65,23 +64,35 @@ if not uci then
|
|||||||
error("You need to install libuci-lua...")
|
error("You need to install libuci-lua...")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Load settings from UCI
|
-- Loading settings from UCI
|
||||||
|
|
||||||
local uciCursor = uci.cursor()
|
local uciCursor = uci.cursor()
|
||||||
Config.mode = uciCursor:get(Config.appName, "config", "mode")
|
Config.mode = uciCursor:get(Config.appName, "config", "mode")
|
||||||
Config.hosts = uciCursor:get(Config.appName, "config", "hosts")
|
Config.hosts = uciCursor:get(Config.appName, "config", "hosts")
|
||||||
Config.checkType = tonumber(uciCursor:get(Config.appName, "config", "check_type"))
|
Config.checkType = tonumber(uciCursor:get(
|
||||||
Config.tcpPort = tonumber(uciCursor:get(Config.appName, "config", "tcp_port"))
|
Config.appName, "config", "check_type"))
|
||||||
Config.UIConnectionAttempts = tonumber(uciCursor:get(Config.appName, "ui_config", "connection_attempts"))
|
Config.tcpPort = tonumber(uciCursor:get(
|
||||||
Config.UIConnectionTimeout = tonumber(uciCursor:get(Config.appName, "ui_config", "connection_timeout"))
|
Config.appName, "config", "tcp_port"))
|
||||||
Config.enableLogger = uciCursor:get(Config.appName, "service_config", "enable_logger")
|
Config.UIConnectionAttempts = tonumber(uciCursor:get(
|
||||||
Config.enableUpScript = uciCursor:get(Config.appName, "service_config", "enable_up_script")
|
Config.appName, "ui_config", "connection_attempts"))
|
||||||
Config.enableDownScript = uciCursor:get(Config.appName, "service_config", "enable_down_script")
|
Config.UIConnectionTimeout = tonumber(uciCursor:get(
|
||||||
Config.enableRunScript = uciCursor:get(Config.appName, "service_config", "enable_run_script")
|
Config.appName, "ui_config", "connection_timeout"))
|
||||||
Config.intervalUp = tonumber(uciCursor:get(Config.appName, "service_config", "interval_up"))
|
Config.enableLogger = (tonumber(uciCursor:get(
|
||||||
Config.intervalDown = tonumber(uciCursor:get(Config.appName, "service_config", "interval_down"))
|
Config.appName, "service_config", "enable_logger")) ~= 0)
|
||||||
Config.connectionAttempts = tonumber(uciCursor:get(Config.appName, "service_config", "connection_attempts"))
|
Config.enableUpScript = (tonumber(uciCursor:get(
|
||||||
Config.connectionTimeout = tonumber(uciCursor:get(Config.appName, "service_config", "connection_timeout"))
|
Config.appName, "service_config", "enable_up_script")) ~= 0)
|
||||||
|
Config.enableDownScript = (tonumber(uciCursor:get(
|
||||||
|
Config.appName, "service_config", "enable_down_script")) ~= 0)
|
||||||
|
Config.enableRunScript = (tonumber(uciCursor:get(
|
||||||
|
Config.appName, "service_config", "enable_run_script")) ~= 0)
|
||||||
|
Config.intervalUp = tonumber(uciCursor:get(
|
||||||
|
Config.appName, "service_config", "interval_up"))
|
||||||
|
Config.intervalDown = tonumber(uciCursor:get(
|
||||||
|
Config.appName, "service_config", "interval_down"))
|
||||||
|
Config.connectionAttempts = tonumber(uciCursor:get(
|
||||||
|
Config.appName, "service_config", "connection_attempts"))
|
||||||
|
Config.connectionTimeout = tonumber(uciCursor:get(
|
||||||
|
Config.appName, "service_config", "connection_timeout"))
|
||||||
|
|
||||||
local function writeValueToFile(filePath, str)
|
local function writeValueToFile(filePath, str)
|
||||||
local retValue = false
|
local retValue = false
|
||||||
@@ -105,15 +116,6 @@ local function readValueFromFile(filePath)
|
|||||||
return retValue
|
return retValue
|
||||||
end
|
end
|
||||||
|
|
||||||
local function writeLogMessage(level, msg)
|
|
||||||
if Config.enableLogger == "1" then
|
|
||||||
local pidValue = readValueFromFile(Config.pidFile)
|
|
||||||
--local fh = io.popen(string.format('%s -t "%s[%d]" -p daemon.%s "%s"', Config.loggerCmd, Config.appName, (pidValue or ""), level, msg), 'r')
|
|
||||||
--fh:close()
|
|
||||||
nixio.syslog(level, string.format("%s[%d]: %s", Config.appName, (pidValue or ""), msg))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function loadModules()
|
local function loadModules()
|
||||||
package.path = string.format("%s;%s/?.lua", package.path, Config.modulesDir)
|
package.path = string.format("%s;%s/?.lua", package.path, Config.modulesDir)
|
||||||
Config.modules = {}
|
Config.modules = {}
|
||||||
@@ -125,7 +127,7 @@ local function loadModules()
|
|||||||
if mod_name and s.enabled == "1" then
|
if mod_name and s.enabled == "1" then
|
||||||
local m = prequire(mod_name)
|
local m = prequire(mod_name)
|
||||||
if m then
|
if m then
|
||||||
m.syslog = writeLogMessage
|
m.syslog = nixio.syslog
|
||||||
m.writeValue = writeValueToFile
|
m.writeValue = writeValueToFile
|
||||||
m.readValue = readValueFromFile
|
m.readValue = readValueFromFile
|
||||||
m:init(s)
|
m:init(s)
|
||||||
@@ -141,7 +143,8 @@ local function runExternalScript(scriptPath, inetStat)
|
|||||||
inetStat = ""
|
inetStat = ""
|
||||||
end
|
end
|
||||||
if nixio.fs.access(scriptPath, "x") then
|
if nixio.fs.access(scriptPath, "x") then
|
||||||
local fh = io.popen(string.format('/bin/sh -c "%s %s" &', scriptPath, inetStat), "r")
|
local fh = io.popen(
|
||||||
|
string.format('/bin/sh -c "%s %s" &', scriptPath, inetStat), "r")
|
||||||
fh:close()
|
fh:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -217,8 +220,8 @@ local function main()
|
|||||||
interval = Config.intervalUp
|
interval = Config.intervalUp
|
||||||
if lastStatus ~= nil and currentStatus ~= lastStatus then
|
if lastStatus ~= nil and currentStatus ~= lastStatus then
|
||||||
writeValueToFile(Config.statusFile, currentStatus)
|
writeValueToFile(Config.statusFile, currentStatus)
|
||||||
writeLogMessage("notice", "internet connected")
|
nixio.syslog("notice", "internet connected")
|
||||||
if Config.enableUpScript == "1" then
|
if Config.enableUpScript then
|
||||||
runExternalScript(Config.upScript)
|
runExternalScript(Config.upScript)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -226,8 +229,8 @@ local function main()
|
|||||||
interval = Config.intervalDown
|
interval = Config.intervalDown
|
||||||
if lastStatus ~= nil and currentStatus ~= lastStatus then
|
if lastStatus ~= nil and currentStatus ~= lastStatus then
|
||||||
writeValueToFile(Config.statusFile, currentStatus)
|
writeValueToFile(Config.statusFile, currentStatus)
|
||||||
writeLogMessage("notice", "internet disconnected")
|
nixio.syslog("notice", "internet disconnected")
|
||||||
if Config.enableDownScript == "1" then
|
if Config.enableDownScript then
|
||||||
runExternalScript(Config.downScript)
|
runExternalScript(Config.downScript)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -237,7 +240,7 @@ local function main()
|
|||||||
e:run(currentStatus, lastStatus)
|
e:run(currentStatus, lastStatus)
|
||||||
end
|
end
|
||||||
|
|
||||||
if Config.enableRunScript == "1" then
|
if Config.enableRunScript then
|
||||||
runExternalScript(Config.runScript, currentStatus)
|
runExternalScript(Config.runScript, currentStatus)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -294,6 +297,7 @@ end
|
|||||||
|
|
||||||
local function stop()
|
local function stop()
|
||||||
local pidValue
|
local pidValue
|
||||||
|
nixio.openlog(Config.appName)
|
||||||
if nixio.fs.access(Config.pidFile, "r") then
|
if nixio.fs.access(Config.pidFile, "r") then
|
||||||
pidValue = readValueFromFile(Config.pidFile)
|
pidValue = readValueFromFile(Config.pidFile)
|
||||||
if pidValue then
|
if pidValue then
|
||||||
@@ -307,20 +311,20 @@ local function stop()
|
|||||||
if not success then
|
if not success then
|
||||||
io.stderr:write(string.format('No such process: "%s"\n', pidValue))
|
io.stderr:write(string.format('No such process: "%s"\n', pidValue))
|
||||||
end
|
end
|
||||||
writeLogMessage("info", "stoped")
|
nixio.syslog("info", string.format("[%s] stoped", pidValue))
|
||||||
removeProcessFiles()
|
removeProcessFiles()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not pidValue then
|
if not pidValue then
|
||||||
io.stderr:write(
|
io.stderr:write(
|
||||||
string.format('PID file "%s" does not exist. %s not running?\n',
|
string.format('PID file "%s" does not exist. %s not running?\n',
|
||||||
Config.pidFile, Config.appName))
|
Config.pidFile, Config.appName))
|
||||||
end
|
end
|
||||||
|
nixio.closelog()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function preRun()
|
local function preRun()
|
||||||
-- Exit if internet detector mode != 2(Service)
|
-- Exit if internet-detector mode != 2(Service)
|
||||||
if Config.mode ~= "2" then
|
if Config.mode ~= "2" then
|
||||||
io.stderr:write(string.format('Start failed, mode != "2"\n', Config.appName))
|
io.stderr:write(string.format('Start failed, mode != "2"\n', Config.appName))
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
@@ -337,7 +341,8 @@ end
|
|||||||
local function run()
|
local function run()
|
||||||
local pidValue = nixio.getpid()
|
local pidValue = nixio.getpid()
|
||||||
writeValueToFile(Config.pidFile, pidValue)
|
writeValueToFile(Config.pidFile, pidValue)
|
||||||
writeLogMessage("info", "started")
|
nixio.openlog(Config.appName, "pid")
|
||||||
|
nixio.syslog("info", "started")
|
||||||
loadModules()
|
loadModules()
|
||||||
|
|
||||||
-- Loaded modules
|
-- Loaded modules
|
||||||
@@ -346,12 +351,13 @@ local function run()
|
|||||||
modules[#modules + 1] = string.format("%s", v.name)
|
modules[#modules + 1] = string.format("%s", v.name)
|
||||||
end
|
end
|
||||||
if #modules > 0 then
|
if #modules > 0 then
|
||||||
writeLogMessage(
|
nixio.syslog(
|
||||||
"info", string.format("Loaded modules: %s", table.concat(modules, ", "))
|
"info", string.format("Loaded modules: %s", table.concat(modules, ", "))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
nixio.closelog()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function noDaemon()
|
local function noDaemon()
|
||||||
@@ -394,7 +400,10 @@ end
|
|||||||
parseHosts()
|
parseHosts()
|
||||||
|
|
||||||
local function help()
|
local function help()
|
||||||
return string.format("Usage: %s [start|no-daemon|stop|restart|status|inet-status|poll [<attempts num>] [<timeout sec>]|--help]", arg[0])
|
return string.format(
|
||||||
|
"Usage: %s [start|no-daemon|stop|restart|status|inet-status|poll [<attempts num>] [<timeout sec>]|--help]",
|
||||||
|
arg[0]
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
local helpArgs = {["-h"] = true, ["--help"] = true, ["help"] = true}
|
local helpArgs = {["-h"] = true, ["--help"] = true, ["help"] = true}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
include $(TOPDIR)/rules.mk
|
include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
PKG_VERSION:=0.4-1
|
PKG_VERSION:=0.4-2
|
||||||
LUCI_TITLE:=LuCI support for internet-detector
|
LUCI_TITLE:=LuCI support for internet-detector
|
||||||
LUCI_DEPENDS:=+internet-detector
|
LUCI_DEPENDS:=+internet-detector
|
||||||
LUCI_PKGARCH:=all
|
LUCI_PKGARCH:=all
|
||||||
|
|||||||
Reference in New Issue
Block a user