aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/apt-panopticon-report-DNS.lua
blob: c571c3def6688970d3cd2ad29667ce381f86f9c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env luajit

local args = {...}

local logFile


--[[ 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.
dumpTable = function (table, space, name)
    local r = ""
--    if "" == space then r = r .. space .. name .. " =\n" else r = r .. space .. "[" .. name .. "] =\n" end
    if "" == space then r = r .. space .. name .. " \n" else r = r .. space .. name .. "\n" end
--    r = r .. space .. "{\n"
    r = r .. dumpTableSub(table, space .. "  ")
--    if "" == space then r = r .. space .. "}\n" else r = r .. space .. "},\n" end
    if "" == space then r = r .. space .. "\n" end
    return r
end
dumpTableSub = function (table, space)
    local r = ""
--    for k, v in pairs(table) do 
    for k, v in orderedPairs(table) do 
--        if type(k) == "string" then k = '"' .. k .. '"' end
	if type(v) == "table" then
	    r = r .. dumpTable(v, space, k)
--	elseif type(v) == "string" then
--	    r = r .. space .. "[" .. k .. "] = '" .. v .. "';\n"
--	elseif type(v) == "function" then
--	    r = r .. space .. "[" .. k .. "] = function ();\n"
--	elseif type(v) == "userdata" then
--	    r = r .. space .. "userdata " .. "[" .. k .. "];\n"
--	elseif type(v) == "boolean" then
--	    if (v) then
--		r = r .. space .. "[" .. k .. "] = true;\n"
--	    else
--		r = r .. space .. "[" .. k .. "] = false;\n"
--	    end
	else
--	    r = r .. space .. "[" .. k .. "] = " .. v .. ";\n"
	    r = r .. space .. k .. "\n"
	end
    end
    return r
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 ..  ")"
	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)
    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")()
local m = {}

for k, v in pairs(mirrors) do
    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
    m["\n" .. k .. " DNS entries -"] = mirrors[k].IPs
end


local file, e = io.open("results/Report-DNS.txt", "w+")
if nil == file then C("opening mirrors file - " .. e) else
    file:write("This DNS report lists each mirror, and the DNS entries for that \nmirror.  " .. 
		"If a mirror has a CNAME, that CNAME is listed along with that \nCNAMEs DNS entries.  " .. 
		"deb.devuan.org is the DNS round robin, which points \nto the mirrors that are part of the DNS-RR.  " ..
		"pkgmaster.devuan.org is the \nmaster mirror, all the others sync to it.  ")
    file:write(dumpTable(m, "", ""))
    file:close()
end