Skip to content

Add ability to plot from file #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
See the example `.log` files for examples of logging output for various operating systems.

* [Windows](example_logfile_windows.log)
* [Mac OS](example_logfile_macos.log)
* [*nix / Mac OS](example_logfile_macos.log)

See [`example_plot.png`](example_plot.png) for an example of plotted ping data.
35 changes: 17 additions & 18 deletions pingplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,34 @@
ping_flag = 'c'


# ping
def pinger(host, n):
def do_ping(host, n):
"""Executes the PCs ping command"""
proc = os.popen(f"ping -{ping_flag} {n} {host}")
return ''.join(proc.readlines())


# wrapper for ping
def call_pinger(host, n, ping, loss, t):
"""Calls the pinger function and returns results as arrays"""
out = pinger(host, n)
def extract_ping_data(raw_ping_output, pings_arr, loss_arr, t_arr, cur_time=time.time(), platform=sys.platform):
"""Extracts ping results from the raw ping process output"""
try:
if sys.platform == 'win32':
loss_idx = float(re.search("\d+(?=% loss)", out).group(0))
ping_idx = float(re.search("(?<=Average =) \d+", out).group(0))
if platform == 'win32':
packet_loss = float(re.search("\d+(?=% loss)", raw_ping_output).group(0))
ping_ms = float(re.search("(?<=Average =) \d+", raw_ping_output).group(0))
else:
# the next two lines assume this format:
# 4 packets transmitted, 4 received, 0% packet loss, time 3002ms
# rtt min/avg/max/mdev = 24.146/63.155/128.436/42.823 ms
loss_idx = float(re.search("\d+(?=% packet loss)", out).group(0))
ping_idx = float(out.split('/')[-3])
packet_loss = float(re.search("\d+(?=% packet loss)", raw_ping_output).group(0))
ping_ms = float(raw_ping_output.split('/')[-3])
except:
ping_idx = np.nan # bad connection
loss_idx = 100.
ping_ms = np.nan # bad connection
packet_loss = 100.

# append data
ping = np.append(ping, ping_idx)
loss = np.append(loss, loss_idx)
t = np.append(t, time.time())
return ping, loss, t, out
pings_arr = np.append(pings_arr, ping_ms)
loss_arr = np.append(loss_arr, packet_loss)
t_arr = np.append(t_arr, cur_time)
return pings_arr, loss_arr, t_arr


# writes out to the log file
Expand Down Expand Up @@ -77,7 +75,7 @@ def plot_gen(ping, now, nans, host, interactive=False, size="1280x640"):
start = []
finish = []
for i in range(len(nans)):
if nans[i] == True:
if nans[i]:
if i == 0:
start.append(i)
elif nans[i] != nans[i - 1]:
Expand Down Expand Up @@ -148,7 +146,8 @@ def main(argv=None):
while True:
# quit on ctrl+c
try:
ping, loss, t, out = call_pinger(opts.host, opts.n, ping, loss, t)
out = do_ping(opts.host, opts.n)
ping, loss, t = extract_ping_data(out, ping, loss, t)
now = np.append(now, datetime.datetime.now())
cnt += 1

Expand Down
53 changes: 53 additions & 0 deletions pingplot_readlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pingplot
import numpy as np
import datetime as dt
import re

filename = "example/example_logfile_macos_nix.log"
filename = "example/example_logfile_windows.log"

TIME_FORMAT = "%a %b %d %H:%M:%S %Y"
TIME_MARKER = "TIME:"
file_format = "windows"

ping = np.array([])
loss = np.array([])
t = np.array([])
hostname = ""


def extract_hostname(content, format):
if format == "nix":
return re.search("(?i)PING (.*) \(", content).group(1)
return re.search("(?i)Pinging (.*) \[", content).group(1)


with open(filename) as file_in:
# Skip the header lines
line = file_in.readline()
while not line.startswith(TIME_MARKER):
line = file_in.readline()

while line:
if line.con
if line.startswith(TIME_MARKER):
cur_time = dt.datetime.strptime(line[6:].strip(), TIME_FORMAT)
line = file_in.readline()
else:
out = []
while line and not line.startswith(TIME_MARKER):
out.append(line.strip())
line = file_in.readline()
raw_output = "\n".join(out)
if not hostname:
hostname = extract_hostname(raw_output, file_format)
ping, loss, t = pingplot.extract_ping_data(raw_output, ping, loss, t, cur_time=cur_time)

nans = np.isnan(ping)

plt = pingplot.plot_gen(ping, t, nans, hostname)

plt.savefig("foo.png")

if __name__ == '__main__':
print("Done")