-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathping_monitoring.py
More file actions
136 lines (123 loc) · 3.71 KB
/
Copy pathping_monitoring.py
File metadata and controls
136 lines (123 loc) · 3.71 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import sys
import matplotlib.pyplot as plt
from datetime import datetime
global ip
#close app
condition = True
fig, ax = plt.subplots()
def on_close(evt=None):
global condition
condition = False
#######
#stop app
def on_press(event):
print('press', event.key)
sys.stdout.flush()
global condition
global ip
if event.key == 'x':
condition = False
plt.title(f'Ping Monitor to {ip} (Stop)')
##########
def if_linux(ipin, interin, db):
file = open(db, 'a')
interin = int(interin)
y = []
x = []
parameter = '-c'
while condition:
ping = os.popen(f'ping {ipin} {parameter} 1')
result = ping.readlines()
if result == []:
ms_result = float(0)
c = str(ms_result)
now = datetime.now()
file.write(now.strftime('%H:%M:%S') + ' ' + c + '\n')
y.insert(0, ms_result)
x.insert(0, now.strftime('%H:%M:%S'))
plt.title(f'Ping Monitor to {ipin}')
plt.xlabel('Time')
plt.ylabel('ms')
plt.xticks(rotation=90)
plt.yticks(rotation=45)
plt.scatter(x, y, c='green')
plt.pause(interin)
else:
msLine = result[-1].strip()
split = msLine.split(' = ')[-1]
ms_result = split.split('ms')[0]
ms_result = ms_result.split('/')[0]
if ms_result == '':
ms_result = float(0)
print('error')
else:
ms_result = float(ms_result)
c = str(ms_result)
now = datetime.now()
file.write(now.strftime('%H:%M:%S') + ' ' + c + '\n')
y.insert(0, ms_result)
x.insert(0, now.strftime('%H:%M:%S'))
plt.title(f'Ping Monitor to {ipin}')
plt.xlabel('Time')
plt.ylabel('ms')
plt.xticks(rotation=90)
plt.yticks(rotation=45)
plt.scatter(x, y, c='green')
plt.pause(interin)
file.close()
def if_windows(ipin, interin, db):
file = open(db, 'a')
interin = int(interin)
y = []
x = []
parameter = '-n'
while condition:
ping = os.popen(f'ping {ipin} {parameter} 1')
result = ping.readlines()
msLine = result[-1].strip()
split = msLine.split(' = ')[-1]
ms_result = split.split('ms')[0]
if ms_result == '1 (100% loss),':
ms_result = 0
if ms_result == '0 (0% loss),':
ms_result = 0
ms_result = float(ms_result)
c = str(ms_result)
now = datetime.now()
file.write(now.strftime('%H:%M:%S') + ' ' + c + '\n')
y.insert(1, ms_result)
x.insert(1, now.strftime('%H:%M:%S'))
plt.xlabel('Time')
plt.ylabel('ms')
plt.title(f'Ping Monitor to {ipin}')
plt.xticks(rotation=90)
plt.yticks(rotation=45)
plt.scatter(x, y, c='green')
plt.pause(interin)
file.close()
def main():
db = input("Enter Database name:")
db = db + ".txt"
global ip
fig.canvas.mpl_connect('key_press_event', on_press)
fig.canvas.mpl_connect('close_event', on_close)
file_path = str(db)
if os.path.exists(file_path):
os.remove(file_path)
else:
print('Build a DataBase: ' + db)
print('The computer`s operating system is:', sys.platform)
print('#### For stop pinging press X ####')
ip = input('Enter ip address to ping:')
inter = input('time to interval:')
if sys.platform == 'linux':
if_linux(ip, inter, db)
if sys.platform == 'windows' or 'win32':
if_windows(ip, inter, db)
#close app
plt.ioff()
######
plt.show()
if __name__ == "__main__":
main()