From 4bb51520521d9ab612d50dd2bcf904d70cf15735 Mon Sep 17 00:00:00 2001 From: onefang Date: Tue, 10 Dec 2019 15:45:11 +1000 Subject: Move common code to it's own module. Some minor clean ups and test tweaks likely came for the ride. --- apt-panopticon-report-email-web.lua | 250 +++++++----------------------------- 1 file changed, 47 insertions(+), 203 deletions(-) (limited to 'apt-panopticon-report-email-web.lua') diff --git a/apt-panopticon-report-email-web.lua b/apt-panopticon-report-email-web.lua index 1144550..ef61183 100755 --- a/apt-panopticon-report-email-web.lua +++ b/apt-panopticon-report-email-web.lua @@ -1,143 +1,22 @@ #!/usr/bin/env luajit -local args = {...} +local APT = require 'apt-panopticommon' +local D = APT.D +local I = APT.I +local W = APT.W +local E = APT.E +local C = APT.C +local arg, sendArgs = APT.parseArgs({...}) -verbosity = -1 -local logFile -local html = false - - ---[[ Ordered table iterator, allow to iterate on the natural order of the keys of a table. - From http://lua-users.org/wiki/SortedIteration - ]] -function __genOrderedIndex( t ) - local orderedIndex = {} - for key in pairs(t) do - table.insert( orderedIndex, key ) - end - table.sort( orderedIndex ) - return orderedIndex -end -function orderedNext(t, state) - -- Equivalent of the next function, but returns the keys in the alphabetic - -- order. We use a temporary ordered key table that is stored in the - -- table being iterated. - - local key = nil - --print("orderedNext: state = "..tostring(state) ) - if state == nil then - -- the first time, generate the index - t.__orderedIndex = __genOrderedIndex( t ) - key = t.__orderedIndex[1] - else - -- fetch the next value - for i = 1,table.getn(t.__orderedIndex) do - if t.__orderedIndex[i] == state then - key = t.__orderedIndex[i+1] - end - end - end - - if key then - return key, t[key] - end - - -- no more value to return, cleanup - t.__orderedIndex = nil - return -end -function orderedPairs(t) - -- Equivalent of the pairs() function on tables. Allows to iterate - -- in order - return orderedNext, t, nil -end - --- Use this to dump a table to a string, with HTML. -dumpTableHTML = function (table, space, name) - local r = name .. "\n" - r = r .. dumpTableHTMLSub(table, space .. " ") - r = r .. space .. "" - return r -end -dumpTableHTMLSub = function (table, space) - local r = "" - for k, v in orderedPairs(table) do - if type(v) == "table" then - if " " == space then - r = r .. space .. dumpTableHTML(v, space, k .. "\n" - else - r = r .. "
  • " .. space .. dumpTableHTML(v, space, k .. "
  • \n" - end - else - r = r .. space .. "
  • " .. k .. "
  • \n" - end - end - return r -end - -local checkFile = function(f) - local h, e = io.open(f, "r") - if nil == h then return false else h:close(); return true end -end - -local plurals = function(e, w) - local result = "" - if 1 == e then - result = e .. " error" - elseif e ~= 0 then - result = e .. " errors" - end - if ("" ~= result) and html then result = "" .. result .. "" end --- result = " " .. result - if 0 < w then - if 0 < e then result = result .. ", " end - if 1 == w then - result = result .. w .. " warning" - else - result = result .. w .. " warnings" - end - if ("" ~= result) and html then result = "" .. result .. "" end --- result = " " .. result - end - if "" ~= result then result = " (" .. result .. ")" end - return result -end local results = {} - -local log = function(v, t, s, prot, test, host) - local x = "" - if nil == prot then prot = "" end - if nil ~= test then x = x .. test else test = "" end - if nil ~= host then - if #x > 0 then x = x .. " " end - x = x .. host - end - if #x > 0 then - t = t .. "(" .. x .. ")" - end - if v <= verbosity then - if 3 <= verbosity then t = os.date() .. " " .. t end - print(t .. ": " .. s) - end - if nil ~= logFile then - logFile:write(os.date() .. " " .. t .. ": " .. s .. "\n") - logFile:flush() - end -end -local D = function(s) log(3, "DEBUG ", s) end -local I = function(s) log(2, "INFO ", s) end -local W = function(s, p, t, h) log(1, "WARNING ", s, p, t, h) end -local E = function(s, p, t, h) log(0, "ERROR ", s, p, t, h) end -local C = function(s) log(-1, "CRITICAL", s) end - -local mirrors = loadfile("results/mirrors.lua")() +APT.mirrors = loadfile("results/mirrors.lua")() local revDNS = function(dom, IP) if "deb.devuan.org" ~= dom then - if nil ~= mirrors["deb.devuan.org"] then - if nil ~= mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][IP] then - if html then + if nil ~= APT.mirrors["deb.devuan.org"] then + if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][IP] then + if APT.html then return "DNS-RR" else return "DNS-RR" @@ -145,7 +24,7 @@ local revDNS = function(dom, IP) end end else - for k, v in pairs(mirrors) do + for k, v in pairs(APT.mirrors) do if "deb.devuan.org" ~= k then local IPs = v.IPs for i, u in pairs(IPs) do @@ -168,7 +47,7 @@ local status = function(host, results, typ) local result = "" local e = 0 local w = 0 - local s = nil ~= mirrors[host].Protocols[typ] + local s = nil ~= APT.mirrors[host].Protocols[typ] local to = results.timeout if ('http' ~= typ) and ('https' ~= typ) and ('ftp' ~= typ) and ('rsync' ~= typ) then s = true end if nil ~= results[typ] then @@ -198,7 +77,7 @@ local status = function(host, results, typ) if to then result = "[TIMEOUT" if not s then result = result .. "*" end - if html then + if APT.html then if s then result = "[TIMEOUT" else @@ -208,14 +87,14 @@ local status = function(host, results, typ) elseif 0 < e then result = "[FAILED" if not s then result = result .. "*" end - if html then + if APT.html then if s then result = "[FAILED" else result = "[FAILED*" end end - if html then + if APT.html then faulty = faulty .. host .. " (" .. typ .. ")
    \n" else faulty = faulty .. host .. " (" .. typ .. ")\n" @@ -223,7 +102,7 @@ local status = function(host, results, typ) else result = "[OK" if not s then result = result .. "*" end - if html then + if APT.html then if s then result = "[OK" else @@ -231,43 +110,7 @@ local status = function(host, results, typ) end end end - return result .. plurals(e, w) .. "]" -end - -local collate = function(host, ip, results) - local f = "results/" .. host .. "_" .. ip .. ".lua" - local rfile, e = io.open(f, "r") - if nil == rfile then I("opening " .. f .. " file - " .. e) else - rfile:close() - local rs = loadfile(f)() - for k, v in pairs(rs) do - if "table" == type(v) then - if "speed" == k then - if v.min < results.speed.min then results.speed.min = v.min end - if v.max > results.speed.max then results.speed.max = v.max end - else - for i, u in pairs(v) do - if "table" == type(u) then - for h, t in pairs(u) do - local a = results[k][i][h] - if nil == a then a = 0 end - results[k][i][h] = a + t - end - else - local a = results[k][i] - if nil == a then a = 0 end - results[k][i] = a + u - end - end - end - elseif "timeout" ~= k then - local a = results[k] - if nil == a then a = 0 end - results[k] = a + v - end - end - end - return results + return result .. APT.plurals(e, w) .. "]" end local m = {} @@ -287,20 +130,20 @@ local logCount = function(domain, ip) if nil ~= l:match(">WARNING ") then warnings = warnings + 1 end end rfile:close() - if html then + if APT.html then if nil == ip then log = "" .. domain .. "" else log = "" .. ip .. "" end end - log = log .. plurals(errors, warnings) + log = log .. APT.plurals(errors, warnings) end return log end -html = false +APT.html = false local email, e = io.open("results/Report-email.txt", "w+") if nil == email then C("opening mirrors file - " .. e) else email:write( "Dear Mirror Admins,\n\n" .. @@ -316,17 +159,17 @@ if nil == email then C("opening mirrors file - " .. e) else "Please see below the current status of the Devuan Package Mirror \nnetwork:\n\n" .. "==== package mirror status " .. os.date("!%Y-%m-%d %H:%M") .. " GMT ====\n" .. "[skip] means that the test hasn't been written yet.\n\n") - for k, v in orderedPairs(mirrors) do + for k, v in APT.orderedPairs(APT.mirrors) do local results = loadfile("results/" .. k .. ".lua")() email:write(k .. "....\n") local IPs = v.IPs for i, u in pairs(IPs) do if "table" == type(u) then for h, t in pairs(u) do - results = collate(k, h, results) + results = APT.collate(k, h, results) end else - results = collate(k, i, results) + results = APT.collate(k, i, results) end end local ftp = "[skip]" @@ -340,11 +183,11 @@ if nil == email then C("opening mirrors file - " .. e) else local updated = status(k, results, "Updated") -- DNS-RR test. - if ("deb.devuan.org" ~= k) and (nil ~= mirrors["deb.devuan.org"]) then - for l, w in pairs(mirrors[k].IPs) do + if ("deb.devuan.org" ~= k) and (nil ~= APT.mirrors["deb.devuan.org"]) then + for l, w in pairs(APT.mirrors[k].IPs) do if type(w) == "table" then for i, u in pairs(w) do - if nil ~= mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then + if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then local log = logCount("deb.devuan.org", i) if "" ~= log then if "" == dns then dns = " " else dns = dns .. " " end @@ -356,7 +199,7 @@ if nil == email then C("opening mirrors file - " .. e) else end end else - if nil ~= mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][l] then + if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][l] then local log = logCount("deb.devuan.org", l) if "" ~= log then if "" == dns then dns = " " else dns = dns .. " " end @@ -391,7 +234,7 @@ end results = {} m = {} faulty = "" -html = true +APT.html = true local web, e = io.open("results/Report-web.html", "w+") if nil == web then C("opening mirrors file - " .. e) else web:write( "apt-panopticon results\n" .. @@ -420,7 +263,7 @@ if nil == web then C("opening mirrors file - " .. e) else "\n" .. "\n" ) - for k, v in orderedPairs(mirrors) do + for k, v in APT.orderedPairs(APT.mirrors) do local results = loadfile("results/" .. k .. ".lua")() local active = "" if "yes" == v.Active then @@ -433,10 +276,10 @@ if nil == web then C("opening mirrors file - " .. e) else for i, u in pairs(IPs) do if "table" == type(u) then for h, t in pairs(u) do - results = collate(k, h, results) + results = APT.collate(k, h, results) end else - results = collate(k, i, results) + results = APT.collate(k, i, results) end end local ftp = "[skip]" @@ -455,11 +298,11 @@ if nil == web then C("opening mirrors file - " .. e) else local spd = '' -- DNS-RR test. - if ("deb.devuan.org" ~= k) and (nil ~= mirrors["deb.devuan.org"]) then - for l, w in pairs(mirrors[k].IPs) do + if ("deb.devuan.org" ~= k) and (nil ~= APT.mirrors["deb.devuan.org"]) then + for l, w in pairs(APT.mirrors[k].IPs) do if type(w) == "table" then for i, u in pairs(w) do - if nil ~= mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then + if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][i] then local log = logCount("deb.devuan.org", i) if "" ~= log then if "" == dns then dns = " " else dns = dns .. "   " end @@ -471,7 +314,7 @@ if nil == web then C("opening mirrors file - " .. e) else end end else - if nil ~= mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][l] then + if nil ~= APT.mirrors["deb.devuan.org"].IPs["deb.roundr.devuan.org"][l] then local log = logCount("deb.devuan.org", l) if "" ~= log then if "" == dns then dns = " " else dns = dns .. "   " end @@ -502,18 +345,18 @@ if nil == web then C("opening mirrors file - " .. e) else web:write( "
    FTPHTTPHTTPSRSYNCDNS round robinProtocolURL sanityIntegrityUpdatedSpeed range
    \n
    \n

    ==== faulty mirrors: ====

    \n" .. faulty) web:write( "
    \n
    \n

    ==== DNS and logs: ====

    \n") - for k, v in pairs(mirrors) do + for k, v in pairs(APT.mirrors) do local log = k local n = {} log = logCount(k) - mirrors[k].Protocols = nil - mirrors[k].FQDN = nil - mirrors[k].Active = nil - mirrors[k].Rate = nil - mirrors[k].BaseURL = nil - mirrors[k].Country = nil - mirrors[k].Bandwidth = nil - for l, w in pairs(mirrors[k].IPs) do + APT.mirrors[k].Protocols = nil + APT.mirrors[k].FQDN = nil + APT.mirrors[k].Active = nil + APT.mirrors[k].Rate = nil + APT.mirrors[k].BaseURL = nil + APT.mirrors[k].Country = nil + APT.mirrors[k].Bandwidth = nil + for l, w in pairs(APT.mirrors[k].IPs) do if type(w) == "table" then n[l] = {} for i, u in pairs(w) do @@ -535,11 +378,12 @@ if nil == web then C("opening mirrors file - " .. e) else "pkgmaster.devuan.org is the master mirror, all the others sync to it.   " .. "

    \n" ) - web:write(dumpTableHTML(m, "", "")) web:write( "\n
    \n
    \n\n" .. + web:write(APT.dumpTableHTML(m, "", "")) "

    The email report.   " .. "All the logs and other output.   " .. "You can get the source code here.

    " .. "\n") web:close() end + -- cgit v1.1