aredn/files/etc/cron.hourly/check-services

135 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2022-2023 Tim Wilkinson
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version
--]]
require("uci")
require("aredn.services")
local cursor = uci.cursor()
-- Current nameserver state
local ns = cursor:get_all("olsrd", "nameservice")
if not ns then
print("Missing nameservice")
return
end
local cnames = {}
for _, name in ipairs(ns.name)
do
cnames[name] = true
end
local chosts = {}
for k, v in pairs(ns)
do
if k:match("^%d+_%d+_%d+_%d+$") then
chosts[k:gsub("_", ".")] = v
end
end
local cservices = {}
for _, service in ipairs(ns.service or {})
do
cservices[service] = true
end
-- Work out the differences between the validated state and the current state
local names, hosts, services = aredn.services.get(true)
local nnames = false
for _, name in ipairs(names)
do
if cnames[name] then
cnames[name] = nil
else
nnames = true
end
end
local nhosts = {}
for _, host in ipairs(hosts)
do
if chosts[host.ip] then
chosts[host.ip] = nil
else
nhosts[host.ip] = host.host
end
end
local nservices = false
for _, service in ipairs(services)
do
if cservices[service] then
cservices[service] = nil
else
nservices = true
end
end
function not_empty(h)
for _, _ in pairs(h)
do
return true
end
return false
end
local change = false
-- Apply the changes
if nnames or not_empty(cnames) then
cursor:set("olsrd", "nameserver", "name", names)
change = true
end
if not_empty(chosts) or not_empty(nhosts) then
for k, _ in pairs(chosts)
do
cursor:delete("olsrd", "nameserver", k)
end
for k, v in pairs(nhosts)
do
cursor:set("olsrd", "nameserver", k, v)
end
change = true
end
if nservices or not_empty(cservices) then
cursor:set("olsrd", "nameserver", "service", services)
change = true
end
-- If services have changed we need to restart olsrd
if change then
cursor:commit("olsrd")
print("Change")
os.execute("/etc/init.d/olsrd restart > /dev/null 2>&1")
else
print("Unchange")
end