mirror of
https://github.com/gSpotx2f/luci-app-internet-detector.git
synced 2025-12-11 05:56:54 +03:00
v0.5. New modules, refactoring
This commit is contained in:
138
internet-detector/files/usr/lib/internet-detector/mod_email.lua
Normal file
138
internet-detector/files/usr/lib/internet-detector/mod_email.lua
Normal file
@@ -0,0 +1,138 @@
|
||||
--[[
|
||||
Dependences:
|
||||
mailsend
|
||||
--]]
|
||||
local nixio = require("nixio")
|
||||
|
||||
local Module = {
|
||||
name = "mod_email",
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
alivePeriod = 0,
|
||||
hostAlias = "OpenWrt",
|
||||
mta = "/usr/bin/mailsend",
|
||||
mailRecipient = "email@gmail.com",
|
||||
mailSender = "email@gmail.com",
|
||||
mailUser = "email@gmail.com",
|
||||
mailPassword = "password",
|
||||
mailSmtp = "smtp.gmail.com",
|
||||
mailSmtpPort = '587',
|
||||
mailSecurity = "tls",
|
||||
_enabled = false,
|
||||
_aliveCounter = 0,
|
||||
_msgSent = true,
|
||||
_disconnected = true,
|
||||
_lastDisconnection = nil,
|
||||
_lastConnection = nil,
|
||||
}
|
||||
|
||||
function Module:init(t)
|
||||
self.alivePeriod = tonumber(t.alive_period)
|
||||
if t.host_alias then
|
||||
self.hostAlias = t.host_alias
|
||||
else
|
||||
self.hostAlias = self.config.hostname
|
||||
end
|
||||
|
||||
self.mailRecipient = t.mail_recipient
|
||||
self.mailSender = t.mail_sender
|
||||
self.mailUser = t.mail_user
|
||||
self.mailPassword = t.mail_password
|
||||
self.mailSmtp = t.mail_smtp
|
||||
self.mailSmtpPort = t.mail_smtp_port
|
||||
self.mailSecurity = t.mail_security
|
||||
|
||||
if nixio.fs.access(self.mta, "x") then
|
||||
self._enabled = true
|
||||
else
|
||||
self._enabled = false
|
||||
self.syslog("warning", string.format("%s: %s is not available", self.name, self.mta))
|
||||
end
|
||||
|
||||
if (not self.mailRecipient or
|
||||
not self.mailSender or
|
||||
not self.mailUser or
|
||||
not self.mailPassword or
|
||||
not self.mailSmtp or
|
||||
not self.mailSmtpPort) then
|
||||
self._enabled = false
|
||||
self.syslog("warning", string.format(
|
||||
"%s: Insufficient data to connect to the SMTP server", self.name))
|
||||
end
|
||||
end
|
||||
|
||||
function Module:sendMessage(msg)
|
||||
local verboseArg = ""
|
||||
-- Debug
|
||||
if self.config.debug then
|
||||
verboseArg = " -v"
|
||||
io.stdout:write("--- mod_email ---\n")
|
||||
io.stdout:flush()
|
||||
end
|
||||
local securityArgs = "-starttls -auth-login"
|
||||
if self.mailSecurity == "ssl" then
|
||||
securityArgs = "-ssl -auth"
|
||||
end
|
||||
local mtaCmd = string.format(
|
||||
'%s%s %s -smtp "%s" -port %s -cs utf-8 -user "%s" -pass "%s" -f "%s" -t "%s" -sub "%s" -M "%s"',
|
||||
self.mta, verboseArg, securityArgs, self.mailSmtp, self.mailSmtpPort,
|
||||
self.mailUser, self.mailPassword, self.mailSender, self.mailRecipient,
|
||||
string.format("%s notification", self.hostAlias),
|
||||
string.format("%s:\n%s", self.hostAlias, msg))
|
||||
|
||||
if os.execute(mtaCmd) ~= 0 then
|
||||
self.syslog("err", string.format(
|
||||
"%s: An error occured while sending message", self.name))
|
||||
else
|
||||
self.syslog("info", string.format(
|
||||
"%s: Message sent to %s", self.name, self.mailRecipient))
|
||||
end
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff)
|
||||
if not self._enabled then
|
||||
return
|
||||
end
|
||||
|
||||
if currentStatus == 1 then
|
||||
self._aliveCounter = 0
|
||||
self._msgSent = false
|
||||
self._lastConnection = nil
|
||||
if not self._disconnected then
|
||||
self._disconnected = true
|
||||
if not self._lastDisconnection then
|
||||
self._lastDisconnection = os.date("%Y.%m.%d %H:%M:%S", os.time())
|
||||
end
|
||||
end
|
||||
else
|
||||
if not self._msgSent then
|
||||
|
||||
if not self._lastConnection then
|
||||
self._lastConnection = os.date("%Y.%m.%d %H:%M:%S", os.time())
|
||||
end
|
||||
|
||||
if self._aliveCounter >= self.alivePeriod then
|
||||
local message = {}
|
||||
if self._lastDisconnection then
|
||||
message[#message + 1] = string.format(
|
||||
"Internet disconnected: %s", self._lastDisconnection)
|
||||
self._lastDisconnection = nil
|
||||
end
|
||||
if self._lastConnection then
|
||||
message[#message + 1] = string.format(
|
||||
"Internet connected: %s", self._lastConnection)
|
||||
self:sendMessage(table.concat(message, ", "))
|
||||
self._msgSent = true
|
||||
end
|
||||
else
|
||||
self._aliveCounter = self._aliveCounter + timeDiff
|
||||
end
|
||||
end
|
||||
|
||||
self._disconnected = false
|
||||
end
|
||||
end
|
||||
|
||||
return Module
|
||||
@@ -3,16 +3,19 @@ local nixio = require("nixio")
|
||||
|
||||
local Module = {
|
||||
name = "mod_led_control",
|
||||
sysLedsDir = "/sys/class/leds",
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
runInterval = 5,
|
||||
sysLedsDir = "/sys/class/leds",
|
||||
ledName = nil,
|
||||
_enabled = false,
|
||||
_ledDir = nil,
|
||||
_ledMaxBrightnessFile = nil,
|
||||
_ledBrightnessFile = nil,
|
||||
_ledMaxBrightness = nil,
|
||||
_counter = 0,
|
||||
}
|
||||
|
||||
function Module:resetLeds()
|
||||
@@ -33,14 +36,14 @@ function Module:init(t)
|
||||
if not self.ledName then
|
||||
return
|
||||
end
|
||||
self._ledDir = string.format("%s/%s", self.sysLedsDir, self.ledName)
|
||||
self._ledMaxBrightnessFile = self._ledDir .. "/max_brightness"
|
||||
self._ledBrightnessFile = self._ledDir .. "/brightness"
|
||||
self._ledDir = string.format("%s/%s", self.sysLedsDir, self.ledName)
|
||||
self._ledMaxBrightnessFile = string.format("%s/max_brightness", self._ledDir)
|
||||
self._ledBrightnessFile = string.format("%s/brightness", self._ledDir)
|
||||
self._ledMaxBrightness = self.readValue(self._ledMaxBrightnessFile) or 1
|
||||
if (not nixio.fs.access(self._ledDir, "r") or
|
||||
not nixio.fs.access(self._ledBrightnessFile, "r", "w")) then
|
||||
self._enabled = false
|
||||
self.syslog("warning", string.format('%s: "%s" is not available', self.name, self.ledName))
|
||||
self.syslog("warning", string.format("%s: LED '%s' is not available", self.name, self.ledName))
|
||||
else
|
||||
self._enabled = true
|
||||
-- Reset all LEDs
|
||||
@@ -63,19 +66,25 @@ function Module:off()
|
||||
self.writeValue(self._ledBrightnessFile, 0)
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus)
|
||||
function Module:run(currentStatus, lastStatus, timeDiff)
|
||||
if not self._enabled then
|
||||
return
|
||||
end
|
||||
if currentStatus == 0 then
|
||||
if not self:getCurrentState() then
|
||||
self:on()
|
||||
end
|
||||
else
|
||||
if self:getCurrentState() then
|
||||
self:off()
|
||||
if self._counter == 0 or self._counter >= self.runInterval or currentStatus ~= lastStatus then
|
||||
|
||||
if currentStatus == 0 then
|
||||
if not self:getCurrentState() then
|
||||
self:on()
|
||||
end
|
||||
else
|
||||
if self:getCurrentState() then
|
||||
self:off()
|
||||
end
|
||||
end
|
||||
|
||||
self._counter = 0
|
||||
end
|
||||
self._counter = self._counter + timeDiff
|
||||
end
|
||||
|
||||
return Module
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
--[[
|
||||
Dependences:
|
||||
modemmanager
|
||||
--]]
|
||||
local nixio = require("nixio")
|
||||
|
||||
local Module = {
|
||||
name = "mod_modem_restart",
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
mmcli = "/usr/bin/mmcli",
|
||||
mmInit = "/etc/init.d/modemmanager",
|
||||
deadPeriod = 0,
|
||||
iface = nil,
|
||||
anyBand = false,
|
||||
_enabled = false,
|
||||
_deadCounter = 0,
|
||||
_restarted = false,
|
||||
}
|
||||
|
||||
function Module:toggleIface(flag)
|
||||
return os.execute(
|
||||
string.format("%s %s", (flag and "/sbin/ifup" or "/sbin/ifdown"), self.iface)
|
||||
)
|
||||
end
|
||||
|
||||
function Module:restartMM()
|
||||
if self.anyBand then
|
||||
self.syslog("info", string.format(
|
||||
"%s: resetting current-bands to 'any'", self.name))
|
||||
os.execute(string.format("%s -m any --set-current-bands=any", self.mmcli))
|
||||
end
|
||||
|
||||
self.syslog("info", string.format("%s: reconnecting modem", self.name))
|
||||
os.execute(string.format("%s restart", self.mmInit))
|
||||
|
||||
if self.iface then
|
||||
self.syslog("info", string.format(
|
||||
"%s: restarting network interface '%s'", self.name, self.iface))
|
||||
self:toggleIface(false)
|
||||
self:toggleIface(true)
|
||||
end
|
||||
end
|
||||
|
||||
function Module:init(t)
|
||||
self.deadPeriod = tonumber(t.dead_period)
|
||||
self.iface = t.iface
|
||||
self.anyBand = (tonumber(t.any_band) ~= 0)
|
||||
|
||||
if not nixio.fs.access(self.mmcli, "x") then
|
||||
self.anyBand = false
|
||||
end
|
||||
|
||||
if nixio.fs.access(self.mmInit, "x") then
|
||||
self._enabled = true
|
||||
else
|
||||
self._enabled = false
|
||||
self.syslog("warning", string.format(
|
||||
"%s: modemmanager service is not available", self.name))
|
||||
end
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff)
|
||||
if not self._enabled then
|
||||
return
|
||||
end
|
||||
if currentStatus == 1 then
|
||||
if not self._restarted then
|
||||
if self._deadCounter >= self.deadPeriod then
|
||||
self:restartMM()
|
||||
self._restarted = true
|
||||
else
|
||||
self._deadCounter = self._deadCounter + timeDiff
|
||||
end
|
||||
end
|
||||
else
|
||||
self._deadCounter = 0
|
||||
self._restarted = false
|
||||
end
|
||||
end
|
||||
|
||||
return Module
|
||||
@@ -0,0 +1,91 @@
|
||||
|
||||
local nixio = require("nixio")
|
||||
|
||||
local Module = {
|
||||
name = "mod_network_restart",
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
iface = false,
|
||||
attempts = 0,
|
||||
deadPeriod = 0,
|
||||
restartTimeout = 0,
|
||||
_attemptsCounter = 0,
|
||||
_deadCounter = 0,
|
||||
}
|
||||
|
||||
function Module:toggleFunc(flag)
|
||||
return
|
||||
end
|
||||
|
||||
function Module:toggleDevice(flag)
|
||||
local ip = "/sbin/ip"
|
||||
if nixio.fs.access(ip, "x") then
|
||||
return os.execute(
|
||||
string.format("%s link set dev %s %s", ip, self.iface, (flag and "up" or "down"))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
function Module:toggleIface(flag)
|
||||
return os.execute(
|
||||
string.format("%s %s", (flag and "/sbin/ifup" or "/sbin/ifdown"), self.iface)
|
||||
)
|
||||
end
|
||||
|
||||
function Module:ifaceUp()
|
||||
self:toggleFunc(true)
|
||||
end
|
||||
|
||||
function Module:ifaceDown()
|
||||
self:toggleFunc(false)
|
||||
end
|
||||
|
||||
function Module:networkRestart()
|
||||
return os.execute("/etc/init.d/network restart")
|
||||
end
|
||||
|
||||
function Module:init(t)
|
||||
local iface = t.iface
|
||||
if iface then
|
||||
self.iface = iface
|
||||
if self.iface:match("^@") then
|
||||
self.iface = self.iface:gsub("^@", "")
|
||||
self.toggleFunc = self.toggleIface
|
||||
else
|
||||
self.toggleFunc = self.toggleDevice
|
||||
end
|
||||
end
|
||||
self.attempts = tonumber(t.attempts)
|
||||
self.deadPeriod = tonumber(t.dead_period)
|
||||
self.restartTimeout = tonumber(t.restart_timeout)
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff)
|
||||
if currentStatus == 1 then
|
||||
if self.attempts == 0 or self._attemptsCounter < self.attempts then
|
||||
if self._deadCounter >= self.deadPeriod then
|
||||
if self.iface then
|
||||
self.syslog("info", string.format(
|
||||
"%s: restarting network interface '%s'", self.name, self.iface))
|
||||
self:ifaceDown()
|
||||
nixio.nanosleep(self.restartTimeout)
|
||||
self:ifaceUp()
|
||||
else
|
||||
self.syslog("info", string.format("%s: restarting network", self.name))
|
||||
self:networkRestart()
|
||||
end
|
||||
self._deadCounter = 0
|
||||
self._attemptsCounter = self._attemptsCounter + 1
|
||||
else
|
||||
self._deadCounter = self._deadCounter + timeDiff
|
||||
end
|
||||
end
|
||||
else
|
||||
self._attemptsCounter = 0
|
||||
self._deadCounter = 0
|
||||
end
|
||||
end
|
||||
|
||||
return Module
|
||||
@@ -0,0 +1,46 @@
|
||||
|
||||
local nixio = require("nixio")
|
||||
|
||||
local Module = {
|
||||
name = "mod_reboot",
|
||||
config = {},
|
||||
syslog = function(level, msg) return true end,
|
||||
writeValue = function(filePath, str) return false end,
|
||||
readValue = function(filePath) return nil end,
|
||||
deadPeriod = 0,
|
||||
forceRebootDelay = 0,
|
||||
_deadCounter = 0,
|
||||
}
|
||||
|
||||
function Module:rebootDevice()
|
||||
self.syslog("warning", string.format("%s: reboot", self.name))
|
||||
os.execute("/sbin/reboot &")
|
||||
|
||||
if self.forceRebootDelay > 0 then
|
||||
nixio.nanosleep(self.forceRebootDelay)
|
||||
self.syslog("warning", string.format("%s: force reboot", self.name))
|
||||
self.writeValue("/proc/sys/kernel/sysrq", "1")
|
||||
self.writeValue("/proc/sysrq-trigger", "b")
|
||||
end
|
||||
end
|
||||
|
||||
function Module:init(t)
|
||||
self.deadPeriod = tonumber(t.dead_period)
|
||||
self.forceRebootDelay = tonumber(t.force_reboot_delay)
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff)
|
||||
if currentStatus == 1 then
|
||||
if self._deadCounter >= self.deadPeriod then
|
||||
self:rebootDevice()
|
||||
self._deadCounter = 0
|
||||
else
|
||||
self._deadCounter = self._deadCounter + timeDiff
|
||||
end
|
||||
|
||||
else
|
||||
self._deadCounter = 0
|
||||
end
|
||||
end
|
||||
|
||||
return Module
|
||||
@@ -0,0 +1,63 @@
|
||||
|
||||
local nixio = require("nixio")
|
||||
|
||||
local Module = {
|
||||
name = "mod_user_scripts",
|
||||
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 = "",
|
||||
_deadCounter = 0,
|
||||
_aliveCounter = 0,
|
||||
_upScriptExecuted = true,
|
||||
_downScriptExecuted = true,
|
||||
}
|
||||
|
||||
function Module:runExternalScript(scriptPath)
|
||||
if nixio.fs.access(scriptPath, "x") then
|
||||
os.execute(string.format('/bin/sh -c "%s" &', scriptPath))
|
||||
end
|
||||
end
|
||||
|
||||
function Module:init(t)
|
||||
self.deadPeriod = tonumber(t.dead_period)
|
||||
self.alivePeriod = tonumber(t.alive_period)
|
||||
if self.config.configDir then
|
||||
self.upScript = string.format("%s/up-script", self.config.configDir)
|
||||
self.downScript = string.format("%s/down-script", self.config.configDir)
|
||||
end
|
||||
end
|
||||
|
||||
function Module:run(currentStatus, lastStatus, timeDiff)
|
||||
if currentStatus == 1 then
|
||||
self._aliveCounter = 0
|
||||
self._downScriptExecuted = false
|
||||
|
||||
if not self._upScriptExecuted then
|
||||
if self._deadCounter >= self.deadPeriod then
|
||||
self:runExternalScript(self.downScript)
|
||||
self._upScriptExecuted = true
|
||||
else
|
||||
self._deadCounter = self._deadCounter + timeDiff
|
||||
end
|
||||
end
|
||||
else
|
||||
self._deadCounter = 0
|
||||
self._upScriptExecuted = false
|
||||
|
||||
if not self._downScriptExecuted then
|
||||
if self._aliveCounter >= self.alivePeriod then
|
||||
self:runExternalScript(self.upScript)
|
||||
self._downScriptExecuted = true
|
||||
else
|
||||
self._aliveCounter = self._aliveCounter + timeDiff
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Module
|
||||
Reference in New Issue
Block a user