-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsejson.py
executable file
·119 lines (105 loc) · 3.38 KB
/
parsejson.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# parsejson.py: parse JSON data from RIPE Atlas measurements
#
# Copyright (C) 2017 by Maciej Andziński <[email protected]>
# Copyright (C) 2017 by Paweł Foremski <[email protected]>
#
# Licensed under GNU GPL v3, <https://www.gnu.org/licenses/gpl-3.0.html>
#
import json
import argparse
import gzip
import string
from collections import defaultdict
import dsfail
import nxdomain
import dnskey
import whoami
import whoami2
import ipv6
import ping
import traceroute
import qname
import tcp
import hostname
import version
import serverid
db = defaultdict(str)
db_counts = defaultdict(int)
db_count_max = 0
# call handler.main() for each JSON array element in given file path
def process(handler, path):
global db_count_max
if path.endswith(".gz"):
fh = gzip.open(path)
else:
fh = open(path)
jsobj = json.load(fh)
db_count_max += 1
handler.init(jsobj)
for el in jsobj:
if "prb_id" not in el: continue
pid = el['prb_id']
# a resultset? take first
if "resultset" in el:
ok = [x for x in el["resultset"] if "result" in x]
if len(ok) > 0: el = ok[0]
# is error?
if "result" not in el:
if "fail" in dir(handler):
db[pid] += "," + handler.fail()
db_counts[pid] += 1
continue
res = el["result"]
try:
out = handler.each(pid, el, res)
if out:
db[pid] += "," + out
db_counts[pid] += 1
except Exception, e:
print "%% error for probe_id %d: %s" % (pid, e)
continue
def main():
prs = argparse.ArgumentParser(description='Parse the Atlas results')
prs.add_argument('--ping', help='path to ping results')
prs.add_argument('--traceroute', help='path to traceroute results')
prs.add_argument('--ipv6', help='path to ipv6 results')
prs.add_argument('--qname', help='path to qname case results')
prs.add_argument('--tcp', help='path to TCP results')
prs.add_argument('--hostname', help='path to CHAOS hostname.bind results')
prs.add_argument('--version', help='path to CHAOS version.bind results')
prs.add_argument('--serverid', help='path to CHAOS id.server results')
prs.add_argument('--dsfail', help='path to dnssec-failed.org results')
prs.add_argument('--dnskey', help='path to DNSKEY results')
prs.add_argument('--nxd', help='path to NXDOMAIN results')
prs.add_argument('--whoami', help='path to whoami results')
prs.add_argument('--whoami2', help='path to whoami2 results')
args = prs.parse_args()
# print file header
print "@relation 'parsejson'"
print "@attribute probe_id numeric"
# process the input files
if args.ping: process(ping, args.ping)
if args.traceroute: process(traceroute, args.traceroute)
if args.ipv6: process(ipv6, args.ipv6)
if args.qname: process(qname, args.qname)
if args.tcp: process(tcp, args.tcp)
if args.hostname: process(hostname, args.hostname)
if args.version: process(version, args.version)
if args.serverid: process(serverid, args.serverid)
if args.dsfail: process(dsfail, args.dsfail)
if args.dnskey: process(dnskey, args.dnskey)
if args.nxd: process(nxdomain, args.nxd)
if args.whoami: process(whoami, args.whoami)
if args.whoami2: process(whoami2, args.whoami2)
# print the results
printable = set(string.printable)
print "@data"
for pid,out in db.iteritems():
if db_counts[pid] == db_count_max:
print "%d%s" % (pid, filter(lambda x: x in printable, out))
# else:
# print "%% %d skipped - not present in all files" % (pid)
if __name__ == "__main__": main()