diff options
Diffstat (limited to '')
-rwxr-xr-x | apt-panopticon-report-web.lua | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/apt-panopticon-report-web.lua b/apt-panopticon-report-web.lua new file mode 100755 index 0000000..ece9e09 --- /dev/null +++ b/apt-panopticon-report-web.lua | |||
@@ -0,0 +1,200 @@ | |||
1 | #!/usr/bin/env luajit | ||
2 | |||
3 | local args = {...} | ||
4 | |||
5 | verbosity = -1 | ||
6 | local logFile | ||
7 | |||
8 | |||
9 | --[[ Ordered table iterator, allow to iterate on the natural order of the keys of a table. | ||
10 | From http://lua-users.org/wiki/SortedIteration | ||
11 | ]] | ||
12 | function __genOrderedIndex( t ) | ||
13 | local orderedIndex = {} | ||
14 | for key in pairs(t) do | ||
15 | table.insert( orderedIndex, key ) | ||
16 | end | ||
17 | table.sort( orderedIndex ) | ||
18 | return orderedIndex | ||
19 | end | ||
20 | function orderedNext(t, state) | ||
21 | -- Equivalent of the next function, but returns the keys in the alphabetic | ||
22 | -- order. We use a temporary ordered key table that is stored in the | ||
23 | -- table being iterated. | ||
24 | |||
25 | local key = nil | ||
26 | --print("orderedNext: state = "..tostring(state) ) | ||
27 | if state == nil then | ||
28 | -- the first time, generate the index | ||
29 | t.__orderedIndex = __genOrderedIndex( t ) | ||
30 | key = t.__orderedIndex[1] | ||
31 | else | ||
32 | -- fetch the next value | ||
33 | for i = 1,table.getn(t.__orderedIndex) do | ||
34 | if t.__orderedIndex[i] == state then | ||
35 | key = t.__orderedIndex[i+1] | ||
36 | end | ||
37 | end | ||
38 | end | ||
39 | |||
40 | if key then | ||
41 | return key, t[key] | ||
42 | end | ||
43 | |||
44 | -- no more value to return, cleanup | ||
45 | t.__orderedIndex = nil | ||
46 | return | ||
47 | end | ||
48 | function orderedPairs(t) | ||
49 | -- Equivalent of the pairs() function on tables. Allows to iterate | ||
50 | -- in order | ||
51 | return orderedNext, t, nil | ||
52 | end | ||
53 | |||
54 | |||
55 | local results = {} | ||
56 | |||
57 | local log = function(v, t, s, prot, test, host) | ||
58 | local x = "" | ||
59 | if nil == prot then prot = "" end | ||
60 | if nil ~= test then x = x .. test else test = "" end | ||
61 | if nil ~= host then | ||
62 | if #x > 0 then x = x .. " " end | ||
63 | x = x .. host | ||
64 | end | ||
65 | if #x > 0 then | ||
66 | t = t .. "(" .. x .. ")" | ||
67 | end | ||
68 | if v <= verbosity then | ||
69 | if 3 <= verbosity then t = os.date() .. " " .. t end | ||
70 | print(t .. ": " .. s) | ||
71 | end | ||
72 | if nil ~= logFile then | ||
73 | logFile:write(os.date() .. " " .. t .. ": " .. s .. "\n") | ||
74 | logFile:flush() | ||
75 | end | ||
76 | end | ||
77 | local D = function(s) log(3, "DEBUG ", s) end | ||
78 | local I = function(s) log(2, "INFO ", s) end | ||
79 | local W = function(s, p, t, h) log(1, "WARNING ", s, p, t, h) end | ||
80 | local E = function(s, p, t, h) log(0, "ERROR ", s, p, t, h) end | ||
81 | local C = function(s) log(-1, "CRITICAL", s) end | ||
82 | |||
83 | local faulty = "" | ||
84 | local status = function(host, results, typ) | ||
85 | local result = "" | ||
86 | local e = 0 | ||
87 | local w = 0 | ||
88 | if nil ~= results[typ] then | ||
89 | e = results[typ].errors | ||
90 | w = results[typ].warnings | ||
91 | for k, v in pairs(results[typ]) do | ||
92 | if "table" == type(v) then | ||
93 | e = e + v.errors | ||
94 | w = w + v.warnings | ||
95 | end | ||
96 | end | ||
97 | else | ||
98 | for k, v in pairs(results) do | ||
99 | if "table" == type(v) then | ||
100 | for i, u in pairs(v) do | ||
101 | if "table" == type(u) then | ||
102 | if typ == i then | ||
103 | e = e + u.errors | ||
104 | w = w + u.warnings | ||
105 | end | ||
106 | end | ||
107 | end | ||
108 | end | ||
109 | end | ||
110 | end | ||
111 | |||
112 | if 0 < e then result = e .. " <font color='red'><b>error</b></font>" end | ||
113 | if 1 == e then result = e .. " <font color='red'><b>error</b></font>" end | ||
114 | if 0 < w then | ||
115 | if 0 < e then result = result .. ", " end | ||
116 | if 1 == w then | ||
117 | result = result .. w .. " <font color='blue'><b>warning</b></font>" | ||
118 | else | ||
119 | result = result .. w .. " <font color='blue'><b>warnings</b></font>" | ||
120 | end | ||
121 | end | ||
122 | if "[OK]" ~= result then | ||
123 | if 0 < e then | ||
124 | result = "[<font color='red'><b>FAILED</b></font>] (" .. result .. ")" | ||
125 | faulty = faulty .. host .. " (" .. typ .. ")<br>\n" | ||
126 | elseif 0 < w then | ||
127 | result = "[<font color='green'><b>OK</b></font>] (" .. result .. ")" | ||
128 | else | ||
129 | result = "[<font color='green'><b>OK</b></font>]" | ||
130 | end | ||
131 | end | ||
132 | return result | ||
133 | end | ||
134 | |||
135 | local collate = function(host, ip, results) | ||
136 | local f = "results/" .. host .. "_" .. ip .. ".lua" | ||
137 | local rfile, e = io.open(f, "r") | ||
138 | if nil == rfile then I("opening " .. f .. " file - " .. e) else | ||
139 | rfile:close() | ||
140 | local rs = loadfile(f)() | ||
141 | for k, v in pairs(rs) do | ||
142 | if "table" == type(v) then | ||
143 | for i, u in pairs(v) do | ||
144 | if "table" == type(u) then | ||
145 | for h, t in pairs(u) do | ||
146 | local a = results[k][h] | ||
147 | if nil == a then a = 0 end | ||
148 | results[k][h] = a + t | ||
149 | end | ||
150 | else | ||
151 | local a = results[k][i] | ||
152 | if nil == a then a = 0 end | ||
153 | results[k][i] = a + u | ||
154 | end | ||
155 | end | ||
156 | else | ||
157 | local a = results[k] | ||
158 | if nil == a then a = 0 end | ||
159 | results[k] = a + v | ||
160 | end | ||
161 | end | ||
162 | end | ||
163 | return results | ||
164 | end | ||
165 | |||
166 | |||
167 | local mirrors = loadfile("results/mirrors.lua")() | ||
168 | |||
169 | local file, e = io.open("results/Report-web.html", "w+") | ||
170 | if nil == file then C("opening mirrors file - " .. e) else | ||
171 | file:write( "<html><head>\n" .. | ||
172 | "</head><body>" .. | ||
173 | "Check date: " .. os.date("!%a %b %d %T %Z %Y") .. "\n<br>\n") | ||
174 | for k, v in orderedPairs(mirrors) do | ||
175 | local results = loadfile("results/" .. k .. ".lua")() | ||
176 | file:write(" " .. k .. ".... ") | ||
177 | local IPs = v.IPs | ||
178 | for i, u in pairs(IPs) do | ||
179 | if "table" == type(u) then | ||
180 | for h, t in pairs(u) do | ||
181 | results = collate(k, h, results) | ||
182 | end | ||
183 | else | ||
184 | results = collate(k, i, results) | ||
185 | end | ||
186 | end | ||
187 | -- local http = status(k, results, "http") | ||
188 | -- local https = status(k, results, "https") | ||
189 | local dns = "[<font color='yellow'><b>skip</b></font>]" | ||
190 | local integrity = "[<font color='yellow'><b>skip</b></font>]" | ||
191 | local protocol = status(k, results, "Protocol") | ||
192 | local sanity = "[<font color='yellow'><b>skip</b></font>]" | ||
193 | local updated = "[<font color='yellow'><b>skip</b></font>]" | ||
194 | file:write(" DNS-RR: " .. dns .. " Integrity: " .. integrity .. " Protocol: " .. protocol .. " URL-sanity: " .. sanity .. " Updated: " .. updated .. "\n<br>\n") | ||
195 | end | ||
196 | file:write( "==== faulty mirrors: ====<br>\n<br>\n" .. faulty) | ||
197 | file:write( "\n<br>\nLast Failure: NOT WRITTEN YET<br>\n<br>") | ||
198 | file:write( "\n<br>\n\n</body></html>\n") | ||
199 | file:close() | ||
200 | end | ||