aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/apt-panopticon-report-web.lua
blob: 75f289578f96c1ce3659be371e3e5777238fbed4 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env luajit

local args = {...}

verbosity = -1
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, 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 .. "<ul>") .. "</ul>\n"
	    else
		r = r .. "<li>" .. space .. dumpTableHTML(v, space, k .. "<ul>") .. "</ul></li>\n"
	    end
	else
	    r = r .. space .. "<li>" .. k .. "</li>\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 ..  ")"
    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 faulty = ""
local status = function(host, results, typ)
    local result = ""
    local e = 0
    local w = 0
    if nil ~= results[typ] then
	e = results[typ].errors
	w = results[typ].warnings
	for k, v in pairs(results[typ]) do
	    if "table" == type(v) then
		e = e + v.errors
		w = w + v.warnings
	    end
	end
    else
	for k, v in pairs(results) do
	    if "table" == type(v) then
		for i, u in pairs(v) do
		    if "table" == type(u) then
			if typ == i then
			    e = e + u.errors
			    w = w + u.warnings
			end
		    end
		end
	    end
	end
    end

    if 0 < e then result = e .. " <font color='red'><b>errors</b></font>" end
    if 1 == e then result = e .. " <font color='red'><b>error</b></font>" end
    if 0 < w then
	if 0 < e then result = result .. ", " end
	if 1 == w then 
	    result = result .. w .. " <font color='yellow'><b>warning</b></font>"
	else
	    result = result .. w .. " <font color='yellow'><b>warnings</b></font>"
	end
    end
    if "[OK]" ~= result then
	if 0 < e then
	    result = "[<font color='red'><b>FAILED</b></font>] (" .. result .. ")"
	    faulty = faulty .. host .. " (" .. typ .. ")<br>\n"
	elseif 0 < w then
	    result = "[<font color='green'><b>OK</b></font>] (" .. result .. ")"
	else
	    result = "[<font color='green'><b>OK</b></font>]"
	end
    end
    return result
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
		for i, u in pairs(v) do
		    if "table" == type(u) then
			for h, t in pairs(u) do
			    local a = results[k][h]
			    if nil == a then a = 0 end
			    results[k][h] = a + t
			end
		    else
			local a = results[k][i]
			if nil == a then a = 0 end
			results[k][i] = a + u
		    end
		end
	    else
		local a = results[k]
		if nil == a then a = 0 end
		results[k] = a + v
	    end
	end
    end
    return results
end

local mirrors = loadfile("results/mirrors.lua")()
local m = {}

local file, e = io.open("results/Report-web.html", "w+")
if nil == file then C("opening mirrors file - " .. e) else
    file:write( "<html><head>\n" ..
		'</head><body bgcolor="black" text="white" alink="red" link="blue" vlink="purple">' ..
		"<p>Check time: " .. os.date("!%a %b %d %T %Z %Y") .. "</p>\n" ..
		"<p>The full list of Devuan package mirrors is available at the URL: " ..
		"<a href='https://pkgmaster.devuan.org/mirror_list.txt'>https://pkgmaster.devuan.org/mirror_list.txt</a><br>\n" ..
		"Please see below the current status of the Devuan Package Mirror network:</p>\n" ..
		"<h2>==== mirror status: ====</h2>\n<table>\n"
		)
    for k, v in orderedPairs(mirrors) do
	local results = loadfile("results/" .. k .. ".lua")()
	file:write("     <tr><th>" .. k .. "</th> ")
	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)
		end
	    else
		results = collate(k, i, results)
	    end
	end
	local http	= status(k, results, "http")
	local https	= status(k, results, "https")
	local dns	= "[<font color='gray'><b>skip</b></font>]"
	local protocol	= status(k, results, "Protocol")
	local sanity	= "[<font color='gray'><b>skip</b></font>]"
	local integrity = "[<font color='gray'><b>skip</b></font>]"
	local updated	= "[<font color='gray'><b>skip</b></font>]"
	file:write("<td>http: " .. http .. "&nbsp;</td><td>https: " .. https .. "&nbsp;</td><td>DNS-RR: " ..
		    dns .. "&nbsp;</td><td>Protocol: " .. protocol .. "&nbsp;</td><td>URL-sanity: " .. sanity ..
		    "&nbsp;</td><td>Integrity: " .. integrity .. "&nbsp;</td><td>Updated: " .. updated .. "</td></tr>\n")
    end
    file:write( "</table>\n<br>\n<h2>==== faulty mirrors: ====</h2>\n" .. faulty)
    file:write( "<br>\n<br>\n<h2>==== DNS and logs: ====</h2>\n")

    for k, v in pairs(mirrors) do
	local n = {}
	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
	    if type(w) == "table" then
		n[l] = {}
		for i, u in pairs(w) do
		    local nm = "LOG_" .. k .. "_" .. i .. ".html"
		    local rfile, e = io.open("results/" .. nm, "r")
		    if nil ~= rfile then
			rfile:close()
			n[l]["<a href='" .. nm .. "'>" .. i .. "</a>"] = u
		    else
			n[l][i] = u
		    end
		end
	    else
		local nm = "LOG_" .. k .. "_" .. l .. ".html"
		local rfile, e = io.open("results/" .. nm, "r")
		if nil ~= rfile then
		    rfile:close()
		    n["<a href='" .. nm .. "'>" .. l .. "</a>"] = w
		else
		    n[l] = w
		end
	    end
	end
	m[k .. " DNS entries -"] = n
    end
    file:write(	"<p>This lists each mirror, and the DNS entries for that mirror.  " ..
		"The links point to the log files for each FDQN / IP combination that was checked.  " ..
		"If a mirror has a CNAME, that CNAME is listed along with that CNAMEs DNS entries.  " ..
		"deb.devuan.org is the DNS round robin, which points to the mirrors that are part of the DNS-RR.  " ..
		"pkgmaster.devuan.org is the master mirror, all the others sync to it.  " ..
		"</p>\n"
		)
    file:write(dumpTableHTML(m, "", ""))
    file:write( "\n<br>\n\n</body></html>\n")
    file:close()
end