mirror of
https://github.com/gSpotx2f/luci-app-internet-detector.git
synced 2025-12-12 14:37:01 +03:00
v1.5. Some new options. New package structure.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
return require("internet-detector.main")
|
||||
@@ -45,7 +45,7 @@ local InternetDetector = {
|
||||
}
|
||||
InternetDetector.configDir = string.format("/etc/%s", InternetDetector.appName)
|
||||
InternetDetector.modulesDir = string.format(
|
||||
"%s/%s", InternetDetector.libDir, InternetDetector.appName)
|
||||
"%s/%s/modules", InternetDetector.libDir, InternetDetector.appName)
|
||||
|
||||
-- Loading settings from UCI
|
||||
|
||||
@@ -175,9 +175,9 @@ function InternetDetector:loadModules()
|
||||
if modConfig.enabled == 1 then
|
||||
local m
|
||||
if self.debug then
|
||||
m = require(string.format("%s.%s", self.appName, modName))
|
||||
m = require(string.format("%s.modules.%s", self.appName, modName))
|
||||
else
|
||||
m = self:prequire(string.format("%s.%s", self.appName, modName))
|
||||
m = self:prequire(string.format("%s.modules.%s", self.appName, modName))
|
||||
end
|
||||
if m then
|
||||
m.config = self
|
||||
@@ -244,6 +244,7 @@ function InternetDetector:TCPConnectionToHost(host, port)
|
||||
end
|
||||
else
|
||||
local family = saTable[1].family
|
||||
|
||||
if family then
|
||||
local sock, errMsg, errNum = socket.socket(family, socket.SOCK_STREAM, 0)
|
||||
|
||||
@@ -351,7 +352,6 @@ function InternetDetector:mainLoop()
|
||||
local interval = self.serviceConfig.interval_up
|
||||
local modulesStatus = {}
|
||||
local counter = 0
|
||||
local onStart = true
|
||||
local inetChecked = false
|
||||
_RUNNING = true
|
||||
while _RUNNING do
|
||||
@@ -366,27 +366,27 @@ function InternetDetector:mainLoop()
|
||||
self:writeLogMessage("err", "Unknown error while checking host!")
|
||||
end
|
||||
end
|
||||
if onStart or not stat.stat(self.statusFile) then
|
||||
if not stat.stat(self.statusFile) then
|
||||
self:writeValueToFile(self.statusFile, self:statusJson(
|
||||
currentStatus, self.serviceConfig.instance))
|
||||
onStart = false
|
||||
end
|
||||
|
||||
if currentStatus == 0 then
|
||||
interval = self.serviceConfig.interval_up
|
||||
if lastStatus ~= nil and currentStatus ~= lastStatus then
|
||||
if currentStatus ~= lastStatus then
|
||||
self:writeValueToFile(self.statusFile, self:statusJson(
|
||||
currentStatus, self.serviceConfig.instance))
|
||||
self:writeLogMessage("notice", "Connected")
|
||||
end
|
||||
else
|
||||
interval = self.serviceConfig.interval_down
|
||||
if lastStatus ~= nil and currentStatus ~= lastStatus then
|
||||
if currentStatus ~= lastStatus then
|
||||
self:writeValueToFile(self.statusFile, self:statusJson(
|
||||
currentStatus, self.serviceConfig.instance))
|
||||
self:writeLogMessage("notice", "Disconnected")
|
||||
end
|
||||
end
|
||||
|
||||
counter = 0
|
||||
end
|
||||
|
||||
@@ -423,6 +423,7 @@ function InternetDetector:mainLoop()
|
||||
end
|
||||
|
||||
lastStatus = currentStatus
|
||||
|
||||
unistd.sleep(1)
|
||||
counter = counter + 1
|
||||
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
|
||||
local unistd = require("posix.unistd")
|
||||
|
||||
local Module = {
|
||||
name = "mod_user_scripts",
|
||||
runPrio = 80,
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
deadPeriod = 0,
|
||||
alivePeriod = 0,
|
||||
upScript = "",
|
||||
downScript = "",
|
||||
status = nil,
|
||||
_deadCounter = 0,
|
||||
_aliveCounter = 0,
|
||||
_downScriptExecuted = true,
|
||||
_upScriptExecuted = true,
|
||||
}
|
||||
|
||||
function Module:runExternalScript(scriptPath)
|
||||
if unistd.access(scriptPath, "r") then
|
||||
os.execute(string.format('/bin/sh "%s" &', scriptPath))
|
||||
end
|
||||
end
|
||||
|
||||
function Module:init(t)
|
||||
if t.dead_period ~= nil then
|
||||
self.deadPeriod = tonumber(t.dead_period)
|
||||
end
|
||||
if t.alive_period ~= nil then
|
||||
self.alivePeriod = tonumber(t.alive_period)
|
||||
end
|
||||
if self.config.configDir then
|
||||
self.upScript = string.format(
|
||||
"%s/up-script.%s", self.config.configDir, self.config.serviceConfig.instance)
|
||||
self.downScript = string.format(
|
||||
"%s/down-script.%s", self.config.configDir, self.config.serviceConfig.instance)
|
||||
end
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
|
||||
if currentStatus == 1 then
|
||||
self._aliveCounter = 0
|
||||
self._upScriptExecuted = false
|
||||
if not self._downScriptExecuted then
|
||||
if self._deadCounter >= self.deadPeriod then
|
||||
self:runExternalScript(self.downScript)
|
||||
self._downScriptExecuted = true
|
||||
else
|
||||
self._deadCounter = self._deadCounter + timeDiff
|
||||
end
|
||||
end
|
||||
else
|
||||
self._deadCounter = 0
|
||||
self._downScriptExecuted = false
|
||||
if not self._upScriptExecuted then
|
||||
if self._aliveCounter >= self.alivePeriod then
|
||||
self:runExternalScript(self.upScript)
|
||||
self._upScriptExecuted = true
|
||||
else
|
||||
self._aliveCounter = self._aliveCounter + timeDiff
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Module:onExit()
|
||||
return true
|
||||
end
|
||||
|
||||
return Module
|
||||
@@ -2,24 +2,25 @@
|
||||
local unistd = require("posix.unistd")
|
||||
|
||||
local Module = {
|
||||
name = "mod_network_restart",
|
||||
runPrio = 30,
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
deadPeriod = 900,
|
||||
attempts = 1,
|
||||
restartTimeout = 0,
|
||||
status = nil,
|
||||
_attemptsCounter = 0,
|
||||
_deadCounter = 0,
|
||||
_networkRestarted = false,
|
||||
_ifaceRestarting = false,
|
||||
_ifaceRestartCounter = 0,
|
||||
_netIfaces = {},
|
||||
_netDevices = {},
|
||||
_netItemsNum = 0,
|
||||
name = "mod_network_restart",
|
||||
runPrio = 30,
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
deadPeriod = 900,
|
||||
attempts = 1,
|
||||
restartTimeout = 0,
|
||||
status = nil,
|
||||
_attemptsCounter = 0,
|
||||
_deadCounter = 0,
|
||||
_networkRestarted = false,
|
||||
_ifaceRestarting = false,
|
||||
_ifaceRestartCounter = 0,
|
||||
_netIfaces = {},
|
||||
_netDevices = {},
|
||||
_netItemsNum = 0,
|
||||
_disconnectedAtStartup = false,
|
||||
}
|
||||
|
||||
function Module:toggleDevices(flag)
|
||||
@@ -81,7 +82,9 @@ function Module:init(t)
|
||||
if t.restart_timeout ~= nil then
|
||||
self.restartTimeout = tonumber(t.restart_timeout)
|
||||
end
|
||||
self._attemptsCounter = self.attempts
|
||||
if tonumber(t.disconnected_at_startup) == 1 then
|
||||
self._disconnectedAtStartup = true
|
||||
end
|
||||
end
|
||||
|
||||
function Module:networkRestartFunc()
|
||||
@@ -105,7 +108,9 @@ function Module:networkRestartFunc()
|
||||
"%s: restarting network", self.name))
|
||||
self:restartNetworkService()
|
||||
end
|
||||
self._attemptsCounter = self._attemptsCounter + 1
|
||||
if self.attempts > 0 then
|
||||
self._attemptsCounter = self._attemptsCounter + 1
|
||||
end
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
|
||||
@@ -120,7 +125,7 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
|
||||
else
|
||||
if currentStatus == 1 then
|
||||
if not self._networkRestarted then
|
||||
if self._attemptsCounter < self.attempts then
|
||||
if self._disconnectedAtStartup and (self.attempts == 0 or self._attemptsCounter < self.attempts) then
|
||||
if self._deadCounter >= self.deadPeriod then
|
||||
self:networkRestartFunc()
|
||||
self._networkRestarted = true
|
||||
@@ -129,13 +134,14 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
|
||||
self._deadCounter = self._deadCounter + timeDiff
|
||||
end
|
||||
end
|
||||
elseif inetChecked and self._attemptsCounter < self.attempts then
|
||||
elseif inetChecked and (self.attempts == 0 or self._attemptsCounter < self.attempts) then
|
||||
self:networkRestartFunc()
|
||||
end
|
||||
else
|
||||
self._attemptsCounter = 0
|
||||
self._deadCounter = 0
|
||||
self._networkRestarted = false
|
||||
self._attemptsCounter = 0
|
||||
self._deadCounter = 0
|
||||
self._disconnectedAtStartup = true
|
||||
self._networkRestarted = false
|
||||
end
|
||||
self._ifaceRestartCounter = 0
|
||||
end
|
||||
@@ -34,6 +34,9 @@ function Module:init(t)
|
||||
if t.force_reboot_delay ~= nil then
|
||||
self.forceRebootDelay = tonumber(t.force_reboot_delay)
|
||||
end
|
||||
if tonumber(t.disconnected_at_startup) == 1 then
|
||||
self._rebooted = false
|
||||
end
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
|
||||
@@ -0,0 +1,120 @@
|
||||
|
||||
local unistd = require("posix.unistd")
|
||||
|
||||
local Module = {
|
||||
name = "mod_user_scripts",
|
||||
runPrio = 80,
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
deadPeriod = 0,
|
||||
alivePeriod = 0,
|
||||
upScript = "",
|
||||
downScript = "",
|
||||
upScriptAttempts = 1,
|
||||
downScriptAttempts = 1,
|
||||
status = nil,
|
||||
_deadCounter = 0,
|
||||
_aliveCounter = 0,
|
||||
_upScriptAttemptsCounter = 0,
|
||||
_downScriptAttemptsCounter = 0,
|
||||
_upScriptExecuted = false,
|
||||
_downScriptExecuted = false,
|
||||
_disconnectedAtStartup = false,
|
||||
_connectedAtStartup = false,
|
||||
}
|
||||
|
||||
function Module:runExternalScript(scriptPath)
|
||||
if unistd.access(scriptPath, "r") then
|
||||
os.execute(string.format('/bin/sh "%s" &', scriptPath))
|
||||
end
|
||||
end
|
||||
|
||||
function Module:init(t)
|
||||
if t.dead_period ~= nil then
|
||||
self.deadPeriod = tonumber(t.dead_period)
|
||||
end
|
||||
if t.alive_period ~= nil then
|
||||
self.alivePeriod = tonumber(t.alive_period)
|
||||
end
|
||||
if t.up_script_attempts ~= nil then
|
||||
self.upScriptAttempts = tonumber(t.up_script_attempts)
|
||||
end
|
||||
if t.down_script_attempts ~= nil then
|
||||
self.downScriptAttempts = tonumber(t.down_script_attempts)
|
||||
end
|
||||
if self.config.configDir then
|
||||
self.upScript = string.format(
|
||||
"%s/up-script.%s", self.config.configDir, self.config.serviceConfig.instance)
|
||||
self.downScript = string.format(
|
||||
"%s/down-script.%s", self.config.configDir, self.config.serviceConfig.instance)
|
||||
end
|
||||
if tonumber(t.connected_at_startup) == 1 then
|
||||
self._connectedAtStartup = true
|
||||
end
|
||||
if tonumber(t.disconnected_at_startup) == 1 then
|
||||
self._disconnectedAtStartup = true
|
||||
end
|
||||
end
|
||||
|
||||
function Module:runUpScriptFunc()
|
||||
self:runExternalScript(self.upScript)
|
||||
if self.upScriptAttempts > 0 then
|
||||
self._upScriptAttemptsCounter = self._upScriptAttemptsCounter + 1
|
||||
end
|
||||
end
|
||||
|
||||
function Module:runDownScriptFunc()
|
||||
self:runExternalScript(self.downScript)
|
||||
if self.downScriptAttempts > 0 then
|
||||
self._downScriptAttemptsCounter = self._downScriptAttemptsCounter + 1
|
||||
end
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
|
||||
if currentStatus == 1 then
|
||||
self._upScriptAttemptsCounter = 0
|
||||
self._aliveCounter = 0
|
||||
self._connectedAtStartup = true
|
||||
self._upScriptExecuted = false
|
||||
if not self._downScriptExecuted then
|
||||
if self._disconnectedAtStartup and (self.downScriptAttempts == 0 or self._downScriptAttemptsCounter < self.downScriptAttempts) then
|
||||
if self._deadCounter >= self.deadPeriod then
|
||||
self:runDownScriptFunc()
|
||||
self._downScriptExecuted = true
|
||||
self._deadCounter = 0
|
||||
else
|
||||
self._deadCounter = self._deadCounter + timeDiff
|
||||
end
|
||||
end
|
||||
|
||||
elseif inetChecked and (self.downScriptAttempts == 0 or self._downScriptAttemptsCounter < self.downScriptAttempts) then
|
||||
self:runDownScriptFunc()
|
||||
end
|
||||
elseif currentStatus == 0 then
|
||||
self._downScriptAttemptsCounter = 0
|
||||
self._deadCounter = 0
|
||||
self._disconnectedAtStartup = true
|
||||
self._downScriptExecuted = false
|
||||
if not self._upScriptExecuted then
|
||||
if self._connectedAtStartup and (self.upScriptAttempts == 0 or self._upScriptAttemptsCounter < self.upScriptAttempts) then
|
||||
if self._aliveCounter >= self.alivePeriod then
|
||||
self:runUpScriptFunc()
|
||||
self._upScriptExecuted = true
|
||||
self._aliveCounter = 0
|
||||
else
|
||||
self._aliveCounter = self._aliveCounter + timeDiff
|
||||
end
|
||||
end
|
||||
elseif inetChecked and (self.upScriptAttempts == 0 or self._upScriptAttemptsCounter < self.upScriptAttempts) then
|
||||
self:runUpScriptFunc()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Module:onExit()
|
||||
return true
|
||||
end
|
||||
|
||||
return Module
|
||||
Reference in New Issue
Block a user