aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authoronefang2019-11-05 17:30:08 +1000
committeronefang2019-11-05 17:30:08 +1000
commit99a7271471c1fb01df5dc7d6bb8ddf1f42aefffd (patch)
tree10ad826455349e705288f07ade4bd61329dae32b
parentWhitespace clean up. (diff)
downloadapt-panopticon-99a7271471c1fb01df5dc7d6bb8ddf1f42aefffd.zip
apt-panopticon-99a7271471c1fb01df5dc7d6bb8ddf1f42aefffd.tar.gz
apt-panopticon-99a7271471c1fb01df5dc7d6bb8ddf1f42aefffd.tar.bz2
apt-panopticon-99a7271471c1fb01df5dc7d6bb8ddf1f42aefffd.tar.xz
Generic reports, including a dumb example.
-rwxr-xr-xapt-panopticon-report-DNS.lua144
-rwxr-xr-xapt-panopticon.lua26
2 files changed, 170 insertions, 0 deletions
diff --git a/apt-panopticon-report-DNS.lua b/apt-panopticon-report-DNS.lua
new file mode 100755
index 0000000..b26bde5
--- /dev/null
+++ b/apt-panopticon-report-DNS.lua
@@ -0,0 +1,144 @@
1#!/usr/bin/env luajit
2
3local args = {...}
4
5local logFile
6
7
8--[[ Ordered table iterator, allow to iterate on the natural order of the keys of a table.
9 From http://lua-users.org/wiki/SortedIteration
10 ]]
11function __genOrderedIndex( t )
12 local orderedIndex = {}
13 for key in pairs(t) do
14 table.insert( orderedIndex, key )
15 end
16 table.sort( orderedIndex )
17 return orderedIndex
18end
19function orderedNext(t, state)
20 -- Equivalent of the next function, but returns the keys in the alphabetic
21 -- order. We use a temporary ordered key table that is stored in the
22 -- table being iterated.
23
24 local key = nil
25 --print("orderedNext: state = "..tostring(state) )
26 if state == nil then
27 -- the first time, generate the index
28 t.__orderedIndex = __genOrderedIndex( t )
29 key = t.__orderedIndex[1]
30 else
31 -- fetch the next value
32 for i = 1,table.getn(t.__orderedIndex) do
33 if t.__orderedIndex[i] == state then
34 key = t.__orderedIndex[i+1]
35 end
36 end
37 end
38
39 if key then
40 return key, t[key]
41 end
42
43 -- no more value to return, cleanup
44 t.__orderedIndex = nil
45 return
46end
47function orderedPairs(t)
48 -- Equivalent of the pairs() function on tables. Allows to iterate
49 -- in order
50 return orderedNext, t, nil
51end
52
53
54-- Use this to dump a table to a string.
55dumpTable = function (table, space, name)
56 local r = ""
57 if "" == space then r = r .. space .. name .. " =\n" else r = r .. space .. "[" .. name .. "] =\n" end
58 r = r .. space .. "{\n"
59 r = r .. dumpTableSub(table, space .. " ")
60 if "" == space then r = r .. space .. "}\n" else r = r .. space .. "},\n" end
61 return r
62end
63dumpTableSub = function (table, space)
64 local r = ""
65-- for k, v in pairs(table) do
66 for k, v in orderedPairs(table) do
67 if type(k) == "string" then k = '"' .. k .. '"' end
68 if type(v) == "table" then
69 r = r .. dumpTable(v, space, k)
70 elseif type(v) == "string" then
71 r = r .. space .. "[" .. k .. "] = '" .. v .. "';\n"
72 elseif type(v) == "function" then
73 r = r .. space .. "[" .. k .. "] = function ();\n"
74 elseif type(v) == "userdata" then
75 r = r .. space .. "userdata " .. "[" .. k .. "];\n"
76 elseif type(v) == "boolean" then
77 if (v) then
78 r = r .. space .. "[" .. k .. "] = true;\n"
79 else
80 r = r .. space .. "[" .. k .. "] = false;\n"
81 end
82 else
83 r = r .. space .. "[" .. k .. "] = " .. v .. ";\n"
84 end
85 end
86 return r
87end
88
89local results = {}
90
91local log = function(v, t, s, prot, test, host)
92 local x = ""
93 if nil == prot then prot = "" end
94 if nil ~= test then x = x .. test else test = "" end
95 if nil ~= host then
96 if #x > 0 then x = x .. " " end
97 x = x .. host
98 end
99 if #x > 0 then
100 t = t .. "(" .. x .. ")"
101 if "" == test then
102 if v == 0 then results[prot].errors = results[prot].errors + 1 end
103 if v == 1 then results[prot].warnings = results[prot].warnings + 1 end
104 else
105 if v == 0 then results[prot][test].errors = results[prot][test].errors + 1 end
106 if v == 1 then results[prot][test].warnings = results[prot][test].warnings + 1 end
107 end
108 end
109 if v <= verbosity then
110 if 3 <= verbosity then t = os.date() .. " " .. t end
111 print(t .. ": " .. s)
112 end
113 if nil ~= logFile then
114 logFile:write(os.date() .. " " .. t .. ": " .. s .. "\n")
115 logFile:flush()
116 end
117end
118local D = function(s) log(3, "DEBUG ", s) end
119local I = function(s) log(2, "INFO ", s) end
120local W = function(s, p, t, h) log(1, "WARNING ", s, p, t, h) end
121local E = function(s, p, t, h) log(0, "ERROR ", s, p, t, h) end
122local C = function(s) log(-1, "CRITICAL", s) end
123
124local mirrors = {}
125
126mirrors = loadfile("results/mirrors.lua")()
127
128for k, v in pairs(mirrors) do
129 mirrors[k].Protocols = nil
130 mirrors[k].FQDN = nil
131 mirrors[k].Active = nil
132 mirrors[k].Rate = nil
133 mirrors[k].BaseURL = nil
134 mirrors[k].Country = nil
135 mirrors[k].Bandwidth = nil
136end
137
138
139local file, e = io.open("results/REPORT-DNS.txt", "w+")
140if nil == file then C("opening mirrors file - " .. e) else
141 file:write(dumpTable(mirrors, "", "DNS report"))
142 file:close()
143end
144
diff --git a/apt-panopticon.lua b/apt-panopticon.lua
index 2a9631b..788d72c 100755
--- a/apt-panopticon.lua
+++ b/apt-panopticon.lua
@@ -48,6 +48,20 @@ options =
48-- "Updated", 48-- "Updated",
49 }, 49 },
50 }, 50 },
51 reports =
52 {
53 typ = "table",
54 help = "",
55 value =
56 {
57 "DNS",
58 "email",
59-- "Nagios",
60 "Prometheus",
61-- "RRD",
62 "web",
63 },
64 },
51} 65}
52 66
53local defaultURL = {scheme = "http"} 67local defaultURL = {scheme = "http"}
@@ -630,5 +644,17 @@ else
630 while 0 < checkExes(downloadLock) do os.execute("sleep 10") end 644 while 0 < checkExes(downloadLock) do os.execute("sleep 10") end
631 end 645 end
632 os.execute("rm -f results/*.check") 646 os.execute("rm -f results/*.check")
647
648 -- Create the reports.
649 for n, r in pairs(options.reports.value) do
650 local report = "apt-panopticon-report-" .. r .. ".lua"
651 local rfile, e = io.open(report, "r")
652 if nil == rfile then C("opening " .. report .. " file - " .. e) else
653 rfile:close()
654 I("Creating " .. report .. " report.")
655 execute("./" .. report .. " &")
656 end
657 end
658
633 logFile:close() 659 logFile:close()
634end 660end