Last active
April 15, 2021 22:48
-
-
Save rometsch/d0ddca60730334d7f8643b4e9c698ad8 to your computer and use it in GitHub Desktop.
Show timeseries and histogram of output from ping -D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import re | |
from datetime import datetime | |
import numpy as np | |
import plotly.graph_objects as go | |
from plotly.subplots import make_subplots | |
def main(): | |
argparser = argparse.ArgumentParser() | |
argparser.add_argument("ping_logfile", | |
help="Output of the command ping -D your.remote.hosts.ip") | |
args = argparser.parse_args() | |
log_file = args.ping_logfile | |
data = parse_ping_log(log_file) | |
data["datetime"] = [datetime.fromtimestamp( | |
ut) for ut in data["unixtimestamp"]] | |
fig = make_subplots(rows=2, cols=1) | |
fig.add_trace(go.Scatter(x=data["datetime"], y=data["ping [ms]"], | |
mode="markers", showlegend=False, name="ping [ms]"), | |
row=1, col=1) | |
fig.add_trace(go.Histogram(x=data["ping [ms]"], showlegend=False, name="ping [ms]"), | |
row=2, col=1) | |
fig.update_layout( | |
yaxis_title="ping [ms]", | |
yaxis2_title="Count", | |
yaxis2_type="log", | |
xaxis2_type="log", | |
xaxis2_title="ping [ms]" | |
) | |
fig.show() | |
def parse_ping_log(filename): | |
""" Parse the output of a ping -D command. | |
The ping -D option includes the a unix timestamp in the output. | |
Paramters | |
--------- | |
filename : str | |
Path of the stored output log. | |
Returns | |
------- | |
dict | |
Dict containing the parsed info. | |
""" | |
ptrn = re.compile( | |
r"\[([0-9\.]+)\] 64 bytes from ([0-9\.]+): icmp_seq=([0-9]+) ttl=([0-9]+) time=([0-9\.]+) ms") | |
data = {"unixtimestamp": [], "ip": "", | |
"icmp_seq": [], "ttl": [], "ping [ms]": []} | |
with open(filename, "r") as in_file: | |
for line in in_file: | |
m = re.match(ptrn, line.strip()) | |
if m is not None: | |
ts, ip, icmp_seq, ttl, time = m.groups() | |
data["unixtimestamp"].append(int(float(ts))) | |
data["ip"] = ip | |
data["icmp_seq"].append(int(icmp_seq)) | |
data["ttl"].append(int(ttl)) | |
data["ping [ms]"].append(float(time)) | |
return data | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment