From 2c86739c4bd465525b9181b96628542ed0de5601 Mon Sep 17 00:00:00 2001 From: onefang Date: Tue, 5 Nov 2019 02:53:00 +1000 Subject: Create a persistant table with the results. --- mirror-checker.lua | 61 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 15 deletions(-) (limited to 'mirror-checker.lua') diff --git a/mirror-checker.lua b/mirror-checker.lua index aea816e..01082d9 100755 --- a/mirror-checker.lua +++ b/mirror-checker.lua @@ -130,14 +130,28 @@ dumpTableSub = function (table, space) end return r end -local log = function(v, t, s, test, host) + +local ip = "" +local results = {} + +local log = function(v, t, s, prot, test, host) local x = "" - if nil ~= test then x = x .. test end + 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 #x > 0 then + t = t .. "(" .. x .. ")" + if "" == test then + if v == 0 then results[prot].errors = results[prot].errors + 1 end + if v == 1 then results[prot].warnings = results[prot].warnings + 1 end + else + if v == 0 then results[prot][test].errors = results[prot][test].errors + 1 end + if v == 1 then results[prot][test].warnings = results[prot][test].warnings + 1 end + end + end if v <= verbosity then if 3 <= verbosity then t = os.date() .. " " .. t end print(t .. ": " .. s) @@ -149,8 +163,8 @@ local log = function(v, t, s, test, host) end local D = function(s) log(3, "DEBUG ", s) end local I = function(s) log(2, "INFO ", s) end -local W = function(s, t, h) log(1, "WARNING ", s, t, h) end -local E = function(s, t, h) log(0, "ERROR ", s, t, h) 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 = {} @@ -231,15 +245,15 @@ checkHEAD = function (host, URL, r, retry) check = "Retry " .. retry .. " " .. check end if 3 < timeouts then - E("too many timeouts! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, host) + E("too many timeouts! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, "", host) return end if 20 < r then - E("too many redirects! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, host) + E("too many redirects! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, "", host) return end if 4 < retry then - E("too many retries! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, host) + E("too many retries! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, "", host) return end D(PU.scheme .. " :// " .. check .. " " .. host .. " -> " .. URL) @@ -256,13 +270,13 @@ checkHEAD = function (host, URL, r, retry) local p, c, h, s = htp.request{method = "HEAD", redirect = false, url = URL, headers = hd, protocol = "any", options = "all"} if nil == s then s = "" end if nil == p then - E(" " .. c .. " " .. s .. "! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, host) + E(" " .. c .. " " .. s .. "! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, "", host) -- So far the only errors are "timeout", "Network is unreachable", and "closed", and I suspect "closed" is due to saturating my bandwidth. if "timeout" == c then timeouts = timeouts + 1 end if ("closed" == c) or ("Network is unreachable" == c) or ("timeout" == c) then checkHEAD(host, URL, r, retry + 1, timeouts) end else if ("4" == tostring(c):sub(1, 1)) or ("5" == tostring(c):sub(1, 1)) then - E(" " .. c .. " " .. s .. ". " .. check .. " " .. host .. " -> " .. URL, PU.scheme, host) + E(" " .. c .. " " .. s .. ". " .. check .. " " .. host .. " -> " .. URL, PU.scheme, "", host) else I(" " .. c .. " " .. s .. ". " .. check .. " " .. host .. " -> " .. URL) timeouts = timeouts - 1 -- Backoff the timeouts count if we managed to get through. @@ -271,12 +285,12 @@ checkHEAD = function (host, URL, r, retry) if nil ~= l then pu = url.parse(l, defaultURL) if (pu.scheme ~= PU.scheme) then - if testing("Protocol") then W(" protocol changed during redirect! " .. check .. " " .. host .. " -> " .. URL .. " -> " .. l, PU.scheme, host) end + if testing("Protocol") then W(" protocol changed during redirect! " .. check .. " " .. host .. " -> " .. URL .. " -> " .. l, PU.scheme, "Protocol", host) end if (pu.host == host) and pu.path == PU.path then D("Not testing protocol change " .. URL .. " -> " .. l); return end end if l == URL then - E(" redirect loop! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, host) + E(" redirect loop! " .. check .. " " .. host .. " -> " .. URL, PU.scheme, PU.scheme, host) elseif nil == pu.host then I(" relative redirect. " .. check .. " " .. host .. " -> " .. URL .. " -> " .. l) checkHEAD(host, PU.scheme .. "://" .. PU.host .. l, r + 1) @@ -541,15 +555,27 @@ if 0 < #arg then end local pu = url.parse("http://" .. arg[1]) if nil ~= arg[2] then - logFile, e = io.open("results/mirror-checker-lua_" .. pu.host .. "_" .. arg[2] .. ".log", "a+") + logFile, e = io.open("results/" .. pu.host .. "_" .. arg[2] .. ".log", "a+") else - logFile, e = io.open("results/mirror-checker-lua_" .. pu.host .. ".log", "a+") + logFile, e = io.open("results/" .. pu.host .. ".log", "a+") end if nil == logFile then C("opening log file - " .. e); return end I("Starting tests for " ..arg[1] .. " with these tests - " .. table.concat(options.tests.value, ", ")) mirrors = loadfile("results/mirrors.lua")() - if nil ~= arg[2] then I(" Using IP " .. arg[2]) end + if nil ~= arg[2] then I(" Using IP " .. arg[2]); ip = arg[2] end if nil ~= arg[3] then I(" Using file " .. arg[3]); end + + for k, v in pairs{"ftp", "http", "https", "rsync"} do + if testing(v) then + local tests = {errors = 0; warnings = 0} + if testing("Integrity") then tests.Integrity = {errors = 0; warnings = 0} end + if testing("Protocol") then tests.Protocol = {errors = 0; warnings = 0} end + if testing("Updated") then tests.Updated = {errors = 0; warnings = 0} end + if testing("URL-Sanity") then tests.URL_Sanity = {errors = 0; warnings = 0} end + results[v] = tests + end + end + if testing("Integrity") or testing("Updated") then if nil == arg[3] then if not keep then execute("rm -fr results/" .. pu.host) end @@ -568,6 +594,11 @@ if 0 < #arg then checkHost(pu.host, pu.host, pu.path, arg[2], arg[3]) end logFile:close() + local rfile, e = io.open("results/" .. pu.host .. "_" .. ip .. ".lua", "w+") + if nil == rfile then C("opening results file - " .. e) else + rfile:write(dumpTable(results, "", "results") .. "\nreturn results\n") + rfile:close() + end else if not keep then os.execute("rm -f results/*.log") end os.execute("rm -f results/*.check") -- cgit v1.1