aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/apt-panopticommon.lua
blob: 0862b776ec396d7cfaa469ba2512650e1fa92b77 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
local APT = {}

-- https://oss.oetiker.ch/rrdtool/prog/rrdlua.en.html
APT.rrd = require 'rrd'

APT.protocols = {"ftp", "http", "https", "rsync"}
APT.tests = {'raw', 'Integrity', 'Protocol', 'Redirects', 'Updated', 'URLSanity', 'Speed'}
APT.releases = {"ascii", "beowulf", "chimaera", "ceres"}
APT.subRels = {'backports', 'proposed-updates', 'security', 'updates'}
APT.notExist =
{
    'chimaera-backports',
    'chimaera-security',
    'ceres-backports',		-- These will never exist, it's our code name for the testing suite.
    'ceres-proposed-updates',
    'ceres-updates',
    'ceres-security',
}

APT.verbosity = -1
APT.origin = false
APT.redir = false
APT.keep = false
APT.IPv46 = ''

APT.options =
{
    bandwidth =
	{
	    typ = "number",
	    help =	'\n   0 = low    - HTTP tests for all IPs of all mirrors (HEAD tests), but not URL sanity tests.\n' ..
			'   1 = medium - Also HTTP(S) redirect tests, other protocols, download Release files over HTTP, and check them.\n' ..
			'   2 = high   - Also download Packages.xz files that changed, unpack and check them.\n' ..
			'                Also download and check InRelease files.\n' .. 
			'                Pick a few small packages, download them, check their SHA512.\n' ..
			'   3 = more   - Also pick more files and packages, some for each release / arch.\n' ..
			'   4 = all    - Do absolutely everything.\n' ..
			'                Actually download some Contents files, and some more Packages.xz, and package files.',
	    value = 2,
	},
    referenceSite =
	{
	    typ = "string",
	    help = "The mirror that is used as a reference, coz the others sync to it.",
	    value = "pkgmaster.devuan.org",
	},
    roundRobin =
	{
	    typ = "string",
	    help = "The round robin DNS name.",
	    value = "deb.devuan.org",
	},
    tests =
	{
	    typ = "table",
	    help = "A list of the tests, see the documentation for details.",
	    value =
	    {
	        "IPv4",
	        "IPv6",
--	        "ftp",
	        "http",
	        "https",
--	        "rsync",
	        "DNSRR",
	        "Protocol",
	        "Redirects",
	        "URLSanity",
	        "Integrity",
	        "Updated",
	    },
	},
    maxtime =
	{
	    typ = "number",
	    help = "",
	    value = 240,
	},
    timeout =
	{
	    typ = "number",
	    help = "",
	    value = 5,
	},
    timeouts =
	{
	    typ = "number",
	    help = "",
	    value = 3,
	},
    refresh =
	{
	    typ = "number",
	    help = "",
	    value = 300,
	},
    retries =
	{
	    typ = "number",
	    help = "",
	    value = 2,
	},
    reports =
	{
	    typ = "table",
	    help = "A list of the reports generated.",
	    value =
	    {
		"RRD",		-- RRD has to be before web, coz web creates a graph from the RRD data.
		"email-web",
--		"Prometheus",
	    },
	},
    cgi =
	{
	    typ = "boolean",
	    help = "",
	    value = false,
	},
}

APT.args = {}
APT.parseArgs = function(args)
    APT.args = args
    local arg = {}
    local sendArgs = ""
    -- A special test to disable IPv6 tests if IPv6 isn't available.
    if 1 == APT.exe('ip -6 addr | grep inet6 | grep " global"'):Do().status then
	table.insert(args, '--tests=-IPv6')
    end
    if 0 ~= #(args) then
	local option = ""
	for i, a in pairs(args) do
	    if ("--help" == a) or ("-h" == a) then
		print("Check the health of Devuan Linux package mirrors.")
		for k, v in pairs(APT.options) do
		    print("")
		    print('--' .. k .. '   ' .. v.help)
		end
		os.exit()
	    elseif "--version" == a then
		print("apt-panopticon version 0.2 alpha")
		os.exit()
	    elseif "-v" == a then
		APT.verbosity = APT.verbosity + 1
		sendArgs = sendArgs .. a .. " "
	    elseif "-q" == a then
		APT.verbosity = -1
		sendArgs = sendArgs .. a .. " "
	    elseif ("-4" == a) or ("-6" == a) then
		APT.IPv46 = a
		sendArgs = sendArgs .. a .. " "
	    elseif "-k" == a then
		APT.keep = true
		sendArgs = sendArgs .. a .. " "
	    elseif "-o" == a then
		APT.origin = true
		-- Not adding to sendArgs.
	    elseif "-r" == a then
		APT.redir = true
		sendArgs = sendArgs .. a .. " "
	    elseif "--cgi" == a then
		APT.options.cgi.value = true
		sendArgs = sendArgs .. a .. " "
	    elseif "--low" == a then
		APT.options.bandwidth.value = 0
		APT.options.timeout.value = 2
		APT.options.timeouts.value = 1
		APT.options.retries.value = 1
		sendArgs = sendArgs .. a .. " "
	    elseif "--medium" == a then
		APT.options.bandwidth.value = 1
		sendArgs = sendArgs .. a .. " "
	    elseif "--high" == a then
		APT.options.bandwidth.value = 2
		sendArgs = sendArgs .. a .. " "
	    elseif "--more" == a then
		APT.options.bandwidth.value = 3
		sendArgs = sendArgs .. a .. " "
	    elseif "--all" == a then
		APT.options.bandwidth.value = 4
		sendArgs = sendArgs .. a .. " "
	    elseif "--" == a:sub(1, 2) then
		local s, e = a:find("=")
		if nil == s then e = -1 end
		option = a:sub(3, e - 1)
		local o = APT.options[option]
		if nil == o then
		    print("Unknown option --" .. option)
		    option = ""
		else
		    option = a
		    sendArgs = sendArgs .. a .. " "
		    local s, e = a:find("=")
		    if nil == s then e = 0 end
		    option = a:sub(3, e - 1)
		    if "table" == APT.options[option].typ then
			local result = {}
			for t in (a:sub(e + 1) .. ","):gmatch("([+%-]?%w*),") do
			    local f = t:sub(1, 1)
			    local n = t:sub(2, -1)
			    if ("+" ~= f) and ("-" ~= f) then
				table.insert(result, t)
			    end
			end
			if 0 ~= #result then
			    APT.options[option].value = result
			else
			    for t in (a:sub(e + 1) .. ","):gmatch("([+%-]?%w*),") do
				local f = t:sub(1, 1)
				local n = t:sub(2, -1)
				if "+" == f then
				    table.insert(APT.options[option].value, n)
				elseif "-" == f then
				    local r = {}
				    for i, k in pairs(APT.options[option].value) do
					if k ~= n then table.insert(r, k) end
				    end
				    APT.options[option].value = r
				end
			    end
			end
		    else
			APT.options[option].value = a:sub(e + 1, -1)
		    end
		    option = ""
		end
	    elseif "-" == a:sub(1, 1) then
		print("Unknown option " .. a)
	    else
		table.insert(arg, a)
	    end
	end
    end
    return arg, sendArgs
end



--[[ 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 APT.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.
APT.dumpTableHTML = function (table, name, space)
    if nil == space then space = '' end
    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 APT.orderedPairs(table) do 
	if type(v) == "table" then
	    if " " == space then
		r = r ..           space .. APT.dumpTableHTML(v, k .. "<ul>", space) .. "</ul>\n"
	    else
		r = r .. "<li>" .. space .. APT.dumpTableHTML(v, k .. "<ul>", space) .. "</ul></li>\n"
	    end
	else
	    r = r .. space .. "<li>" .. k .. "</li>\n"
	end
    end
    return r
end

-- Use this to dump a table to a string.
APT.dumpTable = function (table, name, space)
    if nil == space then space = '' end
    local r = ""
    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
    return r
end
dumpTableSub = function (table, space)
    local r = ""
    for k, v in pairs(table) do 
        if type(k) == "string" then k = '"' .. k .. '"' end
	if type(v) == "table" then
	    r = r .. APT.dumpTable(v, k, space)
	elseif type(v) == "string" then
	    local bq = '"'
	    local eq = '"'
	    if nil ~= v:match(bq) then
		bq, eq = '"', '"'
		if nil ~= v:match(bq) then
		    bq, eq = '[[', ']]'
		    mbq, meq = '%[%[', '%]%]'
		    while (nil ~= v:match(mbq)) or (nil ~= v:match(meq)) do
			bq = '[' .. '=' .. bq:sub(2, -1)
			eq = ']' .. '=' .. eq:sub(2, -1)
			mbq = '%[' .. '=' .. bq:sub(3, -1)
			meq = '%]' .. '=' .. eq:sub(3, -1)
		    end
		end
	    end
	    r = r .. space .. "[" .. k .. "] = " .. bq .. v .. eq .. ";\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"
	end
    end
    return r
end


APT.allpairs = function(tbl, func)
    for k, v in pairs(tbl) do
	if 'table' == type(v) then
	    for i, w in pairs(v) do
		func(i, w, k, v)
	    end
	else
	    func(k, v)
	end
    end
end

APT.lnk = function(URL)
    return '<a href="' .. URL .. '">' .. URL .. '</a>'
end


APT.search = function(t, s)
    for i, v in pairs(t) do
	if v == s then return true end
    end
    return false
end

APT.results = {}
APT.logFile = nil
APT.html = false

APT.logName = function(host, a2, a3)
    local name = host
    if (nil ~= a2) and ('' ~= a2) then name = name .. "_" .. a2 end
    if (nil ~= a3) and ('' ~= a3) then name = name .. "_" .. a3 end
    name = 'LOG_' .. name .. '.html'
    return {'results/' .. name, '<a href="' .. name:gsub("/", "_") .. '">' .. name:gsub("/", "_") .. '</a>'}
end

APT.logOpen = function(host, a2, a3)
    local name = APT.logName(host, a2, a3)[1]
    if APT.checkFile(name) then return false end
    APT.logFile, e = io.open(name, "a+")
    if nil == APT.logFile then C('opening log file (' .. name .. ') - ' .. e); return false end
    if nil ~= APT.logFile then
	APT.logFile:write("<html><head>\n")
	APT.logFile:write("</head><body bgcolor='black' text='white' alink='red' link='aqua' vlink='fuchsia'>\n")
	APT.logFile:write("<pre>\n")
	APT.logFile:write(APT.dumpTable(APT.args, 'Arguments'))
	APT.logFile:write("</pre>\n")
    else
	return false
    end
    return true
end

APT.logPost = function()
    if nil ~= APT.logFile then
	APT.logFile:write("</body></html> \n")
	APT.logFile:close()
    end
end

local log = function(v, t, s, prot, test, host)
    local x = ""
    if nil == prot then prot = "" end
    if nil == test then test = "" end
    x = x .. prot
    if "" ~= test then
	if #x > 0 then x = x .. " " end
	x = x .. 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 "" ~= prot then
	    if "" == test then
		if nil == APT.results[prot] then APT.results[prot] = {tested = 0; errors = 0; warnings = 0; timeouts = 0} end
		if v == 0 then APT.results[prot].errors = APT.results[prot].errors + 1 end
		if v == 1 then APT.results[prot].warnings = APT.results[prot].warnings + 1 end
		if v == 2 then APT.results[prot].timeouts = APT.results[prot].timeouts + 1 end
	    else
		if nil == APT.results[prot] then APT.results[prot] = {tested = 0; errors = 0; warnings = 0; timeouts = 0} end
		if nil == APT.results[prot][test] then APT.results[prot][test] = {tested = 0; errors = 0; warnings = 0; timeouts = 0} end
		if v == 0 then APT.results[prot][test].errors = APT.results[prot][test].errors + 1 end
		if v == 1 then APT.results[prot][test].warnings = APT.results[prot][test].warnings + 1 end
		if v == 2 then APT.results[prot][test].timeouts = APT.results[prot][test].timeouts + 1 end
	    end
	end
    end
    if v <= APT.verbosity then
	if 3 <= APT.verbosity then t = os.date('!%F %T') .. " " .. t end
	print(t .. ": " .. s)
    end
    if nil ~= APT.logFile then
	if APT.html then
	    local colour = "white"
	    if -1 == v then colour = "fuchsia" end	-- CRITICAL
	    if  0 == v then colour = "red    " end	-- ERROR
	    if  1 == v then colour = "yellow " end	-- WARNING
	    if  2 == v then colour = "blue   " end	-- TIMEOUT
	    if  3 == v then colour = "white  " end	-- INFO
	    if  4 == v then colour = "gray   " end	-- DEBUG
	    APT.logFile:write(os.date('!%F %T') .. " <font color='" .. colour .. "'><b>" .. t .. "</b></font>: " .. s .. "</br>\n")
	else
	    APT.logFile:write(os.date('!%F %T') .. " " .. t .. ": " .. s .. "\n")
	end
	APT.logFile:flush()
    end
end
APT.D = function(s, h)		log(4,  "DEBUG   ", s, nil, nil, h) end
APT.I = function(s, h)		log(3,  "INFO    ", s, nil, nil, h) end
APT.T = function(s, p, t, h)	log(2,  "TIMEOUT ", s, p, t, h) end
APT.W = function(s, p, t, h)	log(1,  "WARNING ", s, p, t, h) end
APT.E = function(s, p, t, h)	log(0,  "ERROR   ", s, p, t, h) end
APT.C = function(s)		log(-1, "CRITICAL", s) end
local D = APT.D
local I = APT.I
local T = APT.T
local W = APT.W
local E = APT.E
local C = APT.C


APT.debians = {}
APT.mirrors = {}

APT.testing = function(t, host)
    for i, v in pairs(APT.options.tests.value) do
	if t == v then
	    local h = APT.mirrors[host]
	    if nil == h then return true end
	    if true == h["Protocols"][t] then return true else I("Skipping " .. t .. " checks for " .. host) end
	end
    end
    return false
end

APT.tested = function(prot, test, host)
    if "" == test then
	if nil == APT.results[prot] then APT.results[prot] = {tested = 0; errors = 0; warnings = 0; timeouts = 0} end
	APT.results[prot].tested = APT.results[prot].tested + 1;
    else
	if nil == APT.results[prot] then APT.results[prot] = {tested = 0; errors = 0; warnings = 0; timeouts = 0} end
	if nil == APT.results[prot][test] then APT.results[prot][test] = {tested = 0; errors = 0; warnings = 0; timeouts = 0} end
	APT.results[prot][test].tested = APT.results[prot][test].tested + 1;
    end
end

APT.exe = function(c)
    local exe = {status = 0, result = '', log = true, cmd = c .. ' '}

    function exe:log()
	self.log = true
	return self
    end
    function exe:Nice(c)
	if nil == c then
	    self.cmd = 'ionice -c3 nice -n 19 ' .. self.cmd
	else
	    self.cmd = self.cmd .. 'ionice -c3 nice -n 19 ' .. c .. ' '
	end
	return self
    end
    function exe:also(c)
	if nil == c then c = '' else c = ' ' .. c end
	self.cmd = self.cmd .. ';' .. c .. ' '
	return self
    end
    function exe:And(c)
	if nil == c then c = '' else c = ' ' .. c end
	self.cmd = self.cmd .. '&&' .. c .. ' '
	return self
    end
    function exe:Or(c)
	if nil == c then c = '' end
	self.cmd = self.cmd .. '|| ' .. c .. ' '
	return self
    end
    function exe:noErr()
	self.cmd = self.cmd .. '2>/dev/null '
	return self
    end
    function exe:wait(w)
	self.cmd = self.cmd .. '&& touch ' .. w .. ' '
	return self
    end
    function exe:Do()
    --[[    "The condition expression of a control structure can return any
	    value. Both false and nil are considered false. All values different
	    from nil and false are considered true (in particular, the number 0
	    and the empty string are also true)."
	says the docs, I beg to differ.]]
	if true == self.log then D("  executing - &nbsp; <code>" .. self.cmd .. "</code>") end
	--[[ Damn os.execute()
	    Lua 5.1 says it returns "a status code, which is system-dependent"
	    Lua 5.2 says it returns true/nil, "exit"/"signal", the status code.
	    I'm getting 7168 or 0.  No idea what the fuck that is.
	local ok, rslt, status = os.execute(s)
	]]
	local f = io.popen(self.cmd .. ' ; echo "$?"', 'r')
	-- The last line will be the command's returned status, collect everything else in result.
	self.status = ''	-- Otherwise the result starts with 0.
	for l in f:lines() do
	    self.result = self.result .. self.status .. "\n"
	    self.status = l
	end
	self.status = tonumber(self.status)
	return self
    end
    function exe:fork()
	self.cmd = '{ ' .. self.cmd .. '; } &'
	if true == self.log then D("  forking - &nbsp; <code>" .. self.cmd .. "</code>") end
	os.execute(self.cmd)
	return self
    end
    return exe
end

APT.checkExes = function (exe)
    local count = io.popen('ps x | grep "' .. exe .. '" | grep -v " grep " | grep -v "flock -n apt-panopticon.lock " | wc -l'):read("*l")
    D(count .. " " .. exe .. " commands still running.")
    return tonumber(count)
end

APT.checkFile = function(f)
    local h, e = io.open(f, "r")
    if nil == h then return false else h:close();  return true end
end

APT.plurals = function(e, w, t)
    local result = ""
    if 1 == e then
	result = e .. " error"
    elseif e ~= 0 then
	result = e .. " errors"
    end
    if ("" ~= result) and APT.html then result = "<font color='red'><b>" .. result .. "</b></font>" end
    if 0 < w then
	if 0 < e then result = result .. ", " end
	if 1 == w then 
	    result = result .. w .. " warning"
	else
	    result = result .. w .. " warnings"
	end
	if ("" ~= result) and APT.html then result = "<font color='yellow'><b>" .. result .. "</b></font>" end
    end
    if 0 < t then
	if (0 < e) or (0 < w) then result = result .. ", " end
	if 1 == t then 
	    result = result .. t .. " timeout"
	else
	    result = result .. t .. " timeouts"
	end
	if ("" ~= result) and APT.html then result = "<font color='blue'><b>" .. result .. "</b></font>" end
    end
    if "" ~= result then result = "  " .. result end
    return result
end

local typs = {'tested', 'errors', 'warnings', 'timeouts'}
APT.padResults = function(results)
    local c = 0
    if nil == results then results = {}; c = 999 end
    for k, v in pairs(APT.protocols) do
	tests = results[v]
	if nil == tests then tests = {} end
	for m, x in pairs(typs) do
	    if nil == tests[x] then tests[x] = c end
	end
	for l, w in pairs(APT.tests) do
	    if ('raw' ~= w) and ('Speed' ~= w) then
		if nil == tests[w] then tests[w] = {} end
		for m, x in pairs(typs) do
		    if nil == tests[w][x] then tests[w][x] = c end
		end
	    end
	end
	if nil == tests.redirects then
	    tests.redirects = {}
	end
	results[v] = tests
    end
    if nil == results.timeout then results.timeout = false end
    if nil == results.speed   then results.speed   = {min = 999999999999; max = 0} end
    return results
end

APT.collate = function(l, host, ip, results)
    results = APT.padResults(results)
    local f = l .. "/" .. host .. "_" .. ip .. ".lua"
    if APT.checkFile(f) then
	local rs = loadfile(f)()
	rs = APT.padResults(rs)
	for k, v in pairs(rs) do
	    if "table" == type(v) then
		if ("speed" == k) and (nil ~= results.speed) then
		    if v.min < results.speed.min then results.speed.min = v.min end
		    if v.max > results.speed.max then results.speed.max = v.max end
		elseif 'IPs' ~= k then
		    for i, u in pairs(v) do
			if 'redirects' ~= i then
			    if "table" == type(u) then
				for h, t in pairs(u) do
				        local a = results[k]
				    if nil == a then results[k]    = {i = {}}; a = results[k]    end
				    a = a[i]
				    if nil == a then results[k][i] = {h = {}}; a = results[k][i] end
				    a = a[h]
				    if nil == a then a = 0 end
				    results[k][i][h] = a + t
				end
			    else
				local a = results[k]
				if nil == a then a = 0; results[k] = {} else a = a[i] end
				if nil == a then a = 0 end
				results[k][i] = a + u
			    end
			end
		    end
		end
	    elseif "timeout" ~= k then
		local a = results[k]
		if nil == a then a = 0 end
		results[k] = a + v
	    end
	end
    end
    return results
end
APT.collateAll = function(hosts, l, host, func)
    results = {}
    local f = l .. "/" .. host .. ".lua"
    if APT.checkFile(f) then
	results = loadfile(f)()
	results = APT.padResults(results)
	if nil ~= func then func(results) end
	results = APT.collate(l, host, 'R', results)
	local v = hosts[host]
	if nil ~= v then
	    local IPs = results.IPs
	    if nil == IPs then W('No IPs for ' .. host .. ' in ' .. l) else
		for i, u in pairs(IPs) do
		    if "table" == type(u) then
			for h, t in pairs(u) do
			    results = APT.collate(l, host, h, results)
			    results = APT.collate(l, host, h .. '_R', results)
			    if nil ~= func then func(results, h) end
			end
		    else
			results = APT.collate(l, host, i .. '_R', results)
			if nil ~= func then func(results, i) end
		    end
		end
	    end
	end
    end
    results = APT.padResults(results)
    return results
end


APT.now = 0
local status
APT.now = tonumber(APT.exe('TZ="GMT" ls -l --time-style="+%s" results/stamp | cut -d " " -f 6-6'):Do().result)
local start = 'now-1month'
local step = '10min'
local hb = '150min'
local DSIe = 'DS:IntegrityErrors:GAUGE:' .. hb .. ':0:U'
local DSIw = 'DS:IntegrityWarnings:GAUGE:' .. hb .. ':0:U'
local DSIt = 'DS:IntegrityTimeouts:GAUGE:' .. hb .. ':0:U'
local DSPe = 'DS:ProtocolErrors:GAUGE:' .. hb .. ':0:U'
local DSPw = 'DS:ProtocolWarnings:GAUGE:' .. hb .. ':0:U'
local DSPt = 'DS:ProtocolTimeouts:GAUGE:' .. hb .. ':0:U'
local DSRe = 'DS:RedirectsErrors:GAUGE:' .. hb .. ':0:U'
local DSRw = 'DS:RedirectsWarnings:GAUGE:' .. hb .. ':0:U'
local DSRt = 'DS:RedirectsTimeouts:GAUGE:' .. hb .. ':0:U'
local DSUe = 'DS:UpdatedErrors:GAUGE:' .. hb .. ':0:U'
local DSUw = 'DS:UpdatedWarnings:GAUGE:' .. hb .. ':0:U'
local DSUt = 'DS:UpdatedTimeouts:GAUGE:' .. hb .. ':0:U'
local DSSe = 'DS:URLSanityErrors:GAUGE:' .. hb .. ':0:U'
local DSSw = 'DS:URLSanityWarnings:GAUGE:' .. hb .. ':0:U'
local DSSt = 'DS:URLSanityTimeouts:GAUGE:' .. hb .. ':0:U'
local DSx = 'DS:max:GAUGE:' .. hb .. ':0:U'
local DSn = 'DS:min:GAUGE:' .. hb .. ':0:U'
-- What Collectd uses.
local RRAc0  = 'RRA:AVERAGE:0.9:1:1200'
local RRAc1  = 'RRA:MIN:0.9:1:1200'
local RRAc2  = 'RRA:MAX:0.9:1:1200'
local RRAc3  = 'RRA:AVERAGE:0.9:7:1235'
local RRAc4  = 'RRA:MIN:0.9:7:1235'
local RRAc5  = 'RRA:MAX:0.9:7:1235'
local RRAc6  = 'RRA:AVERAGE:0.9:50:1210'
local RRAc7  = 'RRA:MIN:0.9:50:1210'
local RRAc8  = 'RRA:MAX:0.9:50:1210'
local RRAc9  = 'RRA:AVERAGE:0.9:223:1202'
local RRAc10 = 'RRA:MIN:0.9:223:1202'
local RRAc11 = 'RRA:MAX:0.9:223:1202'
local RRAc12 = 'RRA:AVERAGE:0.9:2635:1201'
local RRAc13 = 'RRA:MIN:0.9:2635:1201'
local RRAc14 = 'RRA:MAX:0.9:2635:1201'
-- Try LAST.
local RRAl0  = 'RRA:LAST:0.9:1:1200'
local RRAl1  = 'RRA:LAST:0.9:7:1235'
local RRAl2  = 'RRA:LAST:0.9:50:1210'
local RRAl3  = 'RRA:LAST:0.9:223:1202'
local RRAl4  = 'RRA:LAST:0.9:2635:1201'

APT.createRRD = function(host, ip, o)
    if nil ~= o then start = o end
    if nil ~= ip then host = host .. '_' .. ip end
    for i, p in pairs(APT.protocols) do
	os.execute( 'mkdir -p rrd/' .. host .. '/' .. p:upper())
	if not APT.checkFile('rrd/' .. host .. '/' .. p:upper() .. '/Tests.rrd') then
	    D('Creating ' .. 'rrd/' .. host .. '/' .. p:upper() .. '/Tests.rrd')
	    APT.rrd.create(  'rrd/' .. host .. '/' .. p:upper() .. '/Tests.rrd', '--start', start, '--step', step,
			    DSIe, DSIw, DSIt, DSPe, DSPw, DSPt, DSRe, DSRw, DSRt, DSUe, DSUw, DSUt, DSSe, DSSw, DSSt,
			    RRAc0, RRAc1, RRAc2, RRAl0, RRAc3, RRAc4, RRAc5, RRAl1, RRAc6, RRAc7, RRAc8, RRAl2, RRAc9, RRAc10, RRAc11, RRAl3, RRAc12, RRAc13, RRAc14, RRAl4)
	    -- Start them at 0 so the average has something to work on.
	    APT.rrd.update(      'rrd/' .. host .. '/' .. p:upper() .. '/Tests.rrd', (APT.now - 600) .. ':0:0:0:0:0:0:0:0:0:0:0:0:0:0:0')
	end
    end
    os.execute( 'mkdir -p rrd/' .. host .. '/Speed')
    if not APT.checkFile('rrd/' .. host .. '/Speed/Speed.rrd') then
	D('Creating ' .. 'rrd/' .. host .. '/Speed/Speed.rrd')
	APT.rrd.create(  'rrd/' .. host .. '/Speed/Speed.rrd', '--start', start, '--step', step, DSx, DSn,
			RRAc0, RRAc1, RRAc2, RRAl0, RRAc3, RRAc4, RRAc5, RRAl1, RRAc6, RRAc7, RRAc8, RRAl2, RRAc9, RRAc10, RRAc11, RRAl3, RRAc12, RRAc13, RRAc14, RRAl4)
	APT.rrd.update(      'rrd/' .. host .. '/Speed/Speed.rrd', (APT.now - 600) .. ':0:0')
    end
end

APT.updateRRD = function(results, host, ip)
    if nil ~= ip then host = host .. '_' .. ip end
    for i, p in pairs(APT.protocols) do
	if nil ~= results[p] then
	    APT.rrd.update('rrd/' .. host .. '/' .. p:upper() .. '/Tests.rrd', APT.now .. ':' ..
	    results[p]['Integrity'].errors .. ':'  .. results[p]['Integrity'].warnings .. ':' .. results[p]['Integrity'].timeouts .. ':' ..
	    results[p]['Protocol'].errors  .. ':'  .. results[p]['Protocol'].warnings  .. ':' .. results[p]['Protocol'].timeouts  .. ':' ..
	    results[p]['Redirects'].errors .. ':'  .. results[p]['Redirects'].warnings .. ':' .. results[p]['Redirects'].timeouts   .. ':' ..
	    results[p]['Updated'].errors   .. ':'  .. results[p]['Updated'].warnings   .. ':' .. results[p]['Updated'].timeouts   .. ':' ..
	    results[p]['URLSanity'].errors .. ':'  .. results[p]['URLSanity'].warnings .. ':' .. results[p]['URLSanity'].timeouts)
	else
	    APT.rrd.update('rrd/' .. host .. '/' .. p:upper() .. '/Tests.rrd', APT.now .. ':U:U:U:U:U:U:U:U:U:U:U:U:U:U:U')
	end
    end
    if nil ~= results.speed then
	if 0 ~= results.speed.max then
	    APT.rrd.update('rrd/' .. host .. '/Speed/Speed.rrd', APT.now .. ':' .. results.speed.max .. ':'  .. results.speed.min)
	else
	    APT.rrd.update('rrd/' .. host .. '/Speed/Speed.rrd', APT.now .. ':0:0')
	end
    else
	APT.rrd.update(    'rrd/' .. host .. '/Speed/Speed.rrd', APT.now .. ':U:U')
    end
end

APT.doRRD = function(l, k, v, o)
-- TODO - Quick hack, fix it later.
if k == APT.options.roundRobin.value then return end
    APT.collateAll(APT.mirrors, l, k,
	function(results, ip)
	    APT.createRRD(k, ip, o)
	    APT.updateRRD(results, k, ip)
	end)
end


return APT