1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

use try/except to make parser more resilient against unknown error types

This commit is contained in:
Kelly Brazil
2021-05-04 15:34:45 -07:00
parent 5abe095beb
commit ff034e401d

View File

@ -35,7 +35,7 @@ Schema:
"round_trip_ms_stddev": float, "round_trip_ms_stddev": float,
"responses": [ "responses": [
{ {
"type": string, # 'reply', 'timeout', etc. See type_map for all options "type": string, # 'reply', 'timeout', 'unparsable_line', etc. See `_error_type.type_map` for all options
"timestamp": float, "timestamp": float,
"bytes": integer, "bytes": integer,
"response_ip": string, "response_ip": string,
@ -364,35 +364,39 @@ def _linux_parse(data):
# normal responses # normal responses
else: else:
try:
line = line.replace('(', ' ').replace(')', ' ').replace('=', ' ')
line = line.replace('(', ' ').replace(')', ' ').replace('=', ' ') # positions of items depend on whether ipv4/ipv6 and/or ip/hostname is used
if ipv4 and not hostname:
bts, rip, iseq, t2l, tms = (0, 3, 5, 7, 9)
elif ipv4 and hostname:
bts, rip, iseq, t2l, tms = (0, 4, 7, 9, 11)
elif not ipv4 and not hostname:
bts, rip, iseq, t2l, tms = (0, 3, 5, 7, 9)
elif not ipv4 and hostname:
bts, rip, iseq, t2l, tms = (0, 4, 7, 9, 11)
# positions of items depend on whether ipv4/ipv6 and/or ip/hostname is used # if timestamp option is specified, then shift everything right by one
if ipv4 and not hostname: timestamp = False
bts, rip, iseq, t2l, tms = (0, 3, 5, 7, 9) if line[0] == '[':
elif ipv4 and hostname: timestamp = True
bts, rip, iseq, t2l, tms = (0, 4, 7, 9, 11) bts, rip, iseq, t2l, tms = (bts + 1, rip + 1, iseq + 1, t2l + 1, tms + 1)
elif not ipv4 and not hostname:
bts, rip, iseq, t2l, tms = (0, 3, 5, 7, 9)
elif not ipv4 and hostname:
bts, rip, iseq, t2l, tms = (0, 4, 7, 9, 11)
# if timestamp option is specified, then shift everything right by one response = {
timestamp = False 'type': 'reply',
if line[0] == '[': 'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
timestamp = True 'bytes': line.split()[bts],
bts, rip, iseq, t2l, tms = (bts + 1, rip + 1, iseq + 1, t2l + 1, tms + 1) 'response_ip': line.split()[rip].rstrip(':'),
'icmp_seq': line.split()[iseq],
response = { 'ttl': line.split()[t2l],
'type': 'reply', 'time_ms': line.split()[tms],
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None, 'duplicate': True if 'DUP!' in line else False
'bytes': line.split()[bts], }
'response_ip': line.split()[rip].rstrip(':'), except Exception:
'icmp_seq': line.split()[iseq], response = {
'ttl': line.split()[t2l], 'type': 'unparsable_line'
'time_ms': line.split()[tms], }
'duplicate': True if 'DUP!' in line else False
}
ping_responses.append(response) ping_responses.append(response)
continue continue
@ -542,7 +546,28 @@ def _bsd_parse(data):
# normal response # normal response
else: else:
line = line.replace(':', ' ').replace('=', ' ') try:
line = line.replace(':', ' ').replace('=', ' ')
response = {
'type': 'reply',
'bytes': line.split()[0],
'response_ip': line.split()[3],
'icmp_seq': line.split()[5],
'ttl': line.split()[7],
'time_ms': line.split()[9]
}
except Exception:
response = {
'type': 'unparsable_line'
}
ping_responses.append(response)
continue
# ipv6 lines
else:
try:
line = line.replace(',', ' ').replace('=', ' ')
response = { response = {
'type': 'reply', 'type': 'reply',
'bytes': line.split()[0], 'bytes': line.split()[0],
@ -551,20 +576,11 @@ def _bsd_parse(data):
'ttl': line.split()[7], 'ttl': line.split()[7],
'time_ms': line.split()[9] 'time_ms': line.split()[9]
} }
ping_responses.append(response) except Exception:
continue response = {
'type': 'unparsable_line'
}
# ipv6 lines
else:
line = line.replace(',', ' ').replace('=', ' ')
response = {
'type': 'reply',
'bytes': line.split()[0],
'response_ip': line.split()[3],
'icmp_seq': line.split()[5],
'ttl': line.split()[7],
'time_ms': line.split()[9]
}
ping_responses.append(response) ping_responses.append(response)
continue continue