Skip to content

Instantly share code, notes, and snippets.

@emileaben
Created September 30, 2015 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emileaben/6cc2ef65db3ffdc82d98 to your computer and use it in GitHub Desktop.
Save emileaben/6cc2ef65db3ffdc82d98 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import fileinput
from ripe.atlas.sagan import DnsResult
cache={}
latest={}
BIN = 3600
def process(line):
result = DnsResult(line)
try:
nsid = result.responses[0].abuf.answers[0].data[0]
ts_bin = int( float( result.created.strftime('%s') ) / BIN ) * BIN
cache.setdefault( nsid, {} )
cache[ nsid ].setdefault( ts_bin , {'ok': 0, 'timeout': 0} )
cache[ nsid ][ts_bin]['ok'] += 1
#print("{}\t{}\t{}\t{}".format(
# result.created.strftime('%s'),
# result.probe_id,
# result.responses[0].response_time,
# result.responses[0].abuf.answers[0].data[0]
# )
#)
latest[ result.probe_id ] = result.responses[0].abuf.answers[0].data[0]
except:
if 'error' in result.raw_data and 'timeout' in result.raw_data['error']:
try:
ts_bin = int( float( result.created.strftime('%s') ) / BIN ) * BIN
except:
print >>sys.stderr, "giving up on" % result.raw_data
try:
nsid = latest[ result.probe_id ]
except:
nsid = 'unknown'
cache.setdefault( nsid, {} )
cache[ nsid ].setdefault( ts_bin , {'ok': 0, 'timeout': 0} )
cache[ nsid ][ts_bin]['timeout'] += 1
pass # brutal
'''
u'error': {u'timeout': 5000}
'''
for line in fileinput.input():
process(line)
for nsid in cache:
for ts_bin in sorted( cache[ nsid ] ):
try:
count = cache[nsid][ts_bin]['timeout'] + cache[nsid][ts_bin]['ok']
err_pct = cache[nsid][ts_bin]['timeout'] * 100.0 / count
print "%s\t%s\t%s\t%s" % ( ts_bin, nsid, err_pct, count )
except:
print "#ERROR %s" % ( cache[ nsid ][ ts_bin ] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment