mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-25 00:37:31 +02:00
complete linux coverage including summary
This commit is contained in:
@ -16,12 +16,32 @@ Usage (module):
|
|||||||
Schema:
|
Schema:
|
||||||
|
|
||||||
{
|
{
|
||||||
"ping": string,
|
"type": string, # 'reply', 'timeout', 'summary'
|
||||||
"_meta": # This object only exists if using -q or quiet=True
|
"source_ip": string,
|
||||||
|
"destination_ip": string,
|
||||||
|
"data_bytes": integer,
|
||||||
|
"pattern": string, # (null if not set)
|
||||||
|
"destination": string,
|
||||||
|
"timestamp": float,
|
||||||
|
"bytes": integer,
|
||||||
|
"response_ip": string,
|
||||||
|
"icmp_seq": integer,
|
||||||
|
"ttl": integer,
|
||||||
|
"time_ms": float,
|
||||||
|
"duplicate": boolean,
|
||||||
|
"packets_transmitted": integer,
|
||||||
|
"packets_received": integer,
|
||||||
|
"packet_loss_percent": float,
|
||||||
|
"duplicates": integer,
|
||||||
|
"round_trip_ms_min": float,
|
||||||
|
"round_trip_ms_avg": float,
|
||||||
|
"round_trip_ms_max": float,
|
||||||
|
"round_trip_ms_stddev": float,
|
||||||
|
"_meta": # This object only exists if using -q or quiet=True
|
||||||
{
|
{
|
||||||
"success": booean, # true if successfully parsed, false if error
|
"success": booean, # true if successfully parsed, false if error
|
||||||
"error": string, # exists if "success" is false
|
"error": string, # exists if "success" is false
|
||||||
"line": string # exists if "success" is false
|
"line": string # exists if "success" is false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +87,17 @@ def _process(proc_data):
|
|||||||
|
|
||||||
Dictionary. Structured data to conform to the schema.
|
Dictionary. Structured data to conform to the schema.
|
||||||
"""
|
"""
|
||||||
# process the data
|
int_list = ['data_bytes', 'packets_transmitted', 'packets_received', 'bytes', 'icmp_seq', 'ttl',
|
||||||
|
'duplicates', 'vr', 'hl', 'tos', 'len', 'id', 'flg', 'off', 'pro', 'cks']
|
||||||
|
float_list = ['packet_loss_percent', 'round_trip_ms_min', 'round_trip_ms_avg', 'round_trip_ms_max',
|
||||||
|
'round_trip_ms_stddev', 'timestamp', 'time_ms']
|
||||||
|
|
||||||
|
for key in proc_data:
|
||||||
|
if key in int_list:
|
||||||
|
proc_data[key] = jc.utils.convert_to_int(proc_data[key])
|
||||||
|
|
||||||
|
if key in float_list:
|
||||||
|
proc_data[key] = jc.utils.convert_to_float(proc_data[key])
|
||||||
|
|
||||||
return proc_data
|
return proc_data
|
||||||
|
|
||||||
@ -89,13 +119,19 @@ def parse(data, raw=False, quiet=False):
|
|||||||
if not quiet:
|
if not quiet:
|
||||||
jc.utils.compatibility(__name__, info.compatible)
|
jc.utils.compatibility(__name__, info.compatible)
|
||||||
|
|
||||||
|
destination_ip = None
|
||||||
|
data_bytes = None
|
||||||
pattern = None
|
pattern = None
|
||||||
footer = False
|
footer = False
|
||||||
|
packets_transmitted = None
|
||||||
|
packets_received = None
|
||||||
|
packet_loss_percent = None
|
||||||
|
time_ms = None
|
||||||
|
duplicates = None
|
||||||
|
|
||||||
for line in data:
|
for line in data:
|
||||||
try:
|
try:
|
||||||
output_line = {}
|
output_line = {}
|
||||||
# ping_responses = []
|
|
||||||
|
|
||||||
# check for PATTERN
|
# check for PATTERN
|
||||||
if line.startswith('PATTERN: '):
|
if line.startswith('PATTERN: '):
|
||||||
@ -124,13 +160,9 @@ def parse(data, raw=False, quiet=False):
|
|||||||
dst_ip, dta_byts = (3, 4)
|
dst_ip, dta_byts = (3, 4)
|
||||||
|
|
||||||
line = line.replace('(', ' ').replace(')', ' ')
|
line = line.replace('(', ' ').replace(')', ' ')
|
||||||
output_line.update(
|
destination_ip = line.split()[dst_ip].lstrip('(').rstrip(')')
|
||||||
{
|
data_bytes = line.split()[dta_byts]
|
||||||
'destination_ip': line.split()[dst_ip].lstrip('(').rstrip(')'),
|
|
||||||
'data_bytes': line.split()[dta_byts],
|
|
||||||
'pattern': pattern
|
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if line.startswith('---'):
|
if line.startswith('---'):
|
||||||
@ -141,39 +173,44 @@ def parse(data, raw=False, quiet=False):
|
|||||||
if footer:
|
if footer:
|
||||||
if 'packets transmitted' in line:
|
if 'packets transmitted' in line:
|
||||||
if ' duplicates,' in line:
|
if ' duplicates,' in line:
|
||||||
output_line.update(
|
packets_transmitted = line.split()[0]
|
||||||
{
|
packets_received = line.split()[3]
|
||||||
'packets_transmitted': line.split()[0],
|
packet_loss_percent = line.split()[7].rstrip('%')
|
||||||
'packets_received': line.split()[3],
|
duplicates = line.split()[5].lstrip('+')
|
||||||
'packet_loss_percent': line.split()[7].rstrip('%'),
|
time_ms = line.split()[11].replace('ms', '')
|
||||||
'duplicates': line.split()[5].lstrip('+'),
|
|
||||||
'time_ms': line.split()[11].replace('ms', '')
|
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
else:
|
else:
|
||||||
output_line.update(
|
packets_transmitted = line.split()[0]
|
||||||
{
|
packets_received = line.split()[3]
|
||||||
'packets_transmitted': line.split()[0],
|
packet_loss_percent = line.split()[5].rstrip('%')
|
||||||
'packets_received': line.split()[3],
|
duplicates = '0'
|
||||||
'packet_loss_percent': line.split()[5].rstrip('%'),
|
time_ms = line.split()[9].replace('ms', '')
|
||||||
'duplicates': '0',
|
|
||||||
'time_ms': line.split()[9].replace('ms', '')
|
|
||||||
}
|
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
else:
|
else:
|
||||||
split_line = line.split(' = ')[1]
|
split_line = line.split(' = ')[1]
|
||||||
split_line = split_line.split('/')
|
split_line = split_line.split('/')
|
||||||
output_line.update(
|
output_line = {
|
||||||
{
|
'type': 'summary',
|
||||||
'round_trip_ms_min': split_line[0],
|
'destination_ip': destination_ip or None,
|
||||||
'round_trip_ms_avg': split_line[1],
|
'data_bytes': data_bytes or None,
|
||||||
'round_trip_ms_max': split_line[2],
|
'pattern': pattern or None,
|
||||||
'round_trip_ms_stddev': split_line[3].split()[0]
|
'packets_transmitted': packets_transmitted or None,
|
||||||
}
|
'packets_received': packets_received or None,
|
||||||
)
|
'packet_loss_percent': packet_loss_percent or None,
|
||||||
|
'duplicates': duplicates or None,
|
||||||
|
'time_ms': time_ms or None,
|
||||||
|
'round_trip_ms_min': split_line[0],
|
||||||
|
'round_trip_ms_avg': split_line[1],
|
||||||
|
'round_trip_ms_max': split_line[2],
|
||||||
|
'round_trip_ms_stddev': split_line[3].split()[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
yield stream_success(output_line, quiet) if raw else stream_success(_process(output_line), quiet)
|
||||||
|
continue
|
||||||
|
|
||||||
# ping response lines
|
# ping response lines
|
||||||
else:
|
else:
|
||||||
@ -189,6 +226,8 @@ def parse(data, raw=False, quiet=False):
|
|||||||
|
|
||||||
output_line = {
|
output_line = {
|
||||||
'type': 'timeout',
|
'type': 'timeout',
|
||||||
|
'destination_ip': destination_ip or None,
|
||||||
|
'data_bytes': data_bytes or None,
|
||||||
'pattern': pattern or None,
|
'pattern': pattern or None,
|
||||||
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
|
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
|
||||||
'icmp_seq': line.replace('=', ' ').split()[isequence]
|
'icmp_seq': line.replace('=', ' ').split()[isequence]
|
||||||
@ -220,6 +259,8 @@ def parse(data, raw=False, quiet=False):
|
|||||||
|
|
||||||
output_line = {
|
output_line = {
|
||||||
'type': 'reply',
|
'type': 'reply',
|
||||||
|
'destination_ip': destination_ip or None,
|
||||||
|
'data_bytes': data_bytes or None,
|
||||||
'pattern': pattern or None,
|
'pattern': pattern or None,
|
||||||
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
|
'timestamp': line.split()[0].lstrip('[').rstrip(']') if timestamp else None,
|
||||||
'bytes': line.split()[bts],
|
'bytes': line.split()[bts],
|
||||||
|
Reference in New Issue
Block a user