/
pingplot.py
77 lines (68 loc) · 1.98 KB
/
pingplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty
from threading import Thread
from subprocess import Popen, PIPE
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from datetime import datetime
import sys
def enqueue_output(out, err, queue):
try:
for line in iter(out.readline, b''):
try:
l = float(line.decode('utf-8').split(' ')[6].split('=')[1])
data = (datetime.now(), l)
queue.put(data)
print(repr(data))
except Exception, e:
print(line)
print(e)
except:
print("Error reading from ping", err.readlines())
finally:
out.close()
err.close()
if __name__ == "__main__":
ping_args = sys.argv[1:]
p = Popen(['ping']+ping_args, stdout=PIPE,
stderr=PIPE, bufsize=1, close_fds=True)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, p.stderr, q))
t.daemon = True # thread dies with the program
t.start()
xdata = np.array([])
ydata = np.array([])
def get_data():
global xdata
global ydata
while True:
try:
x, y = q.get_nowait()
xdata = np.append(xdata, [x,])
ydata = np.append(ydata, [y,])
except Empty:
return (xdata, ydata)
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
xa, = ax1.plot([], [], 'ro')
plt.title('Ping times')
plt.xlabel('Time')
plt.ylabel('ICMP Response (ms)')
plt.show()
l = None
while True:
x, y = get_data()
print(len(x))
#ax1.clear()
ax1.plot(x, y, 'r+')
if l:
l.remove()
mean = y.mean()
l = ax1.axhline(mean, color='b', linestyle='dashed', linewidth=2)
fig.autofmt_xdate()
ax1.fmt_xdata = mdates.DateFormatter('%H-%m-%S')
plt.pause(1)