SatCat commited on
Commit
8c74a95
·
verified ·
1 Parent(s): ec56c1f

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +88 -0
main.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import time
3
+ from datetime import datetime
4
+ from nicegui import ui, app
5
+ import os
6
+ from ping3 import ping, verbose_ping
7
+
8
+
9
+ if 'PINGER_IPS' not in os.environ:
10
+ print('define PINGER_IPS > 1.1.1.1,8.8.8.8')
11
+ quit()
12
+
13
+ ips = os.getenv('PINGER_IPS').split(',')
14
+
15
+ maxTimeSec = 600
16
+ pingAlertLimitMs = 100
17
+ maxPingResponseTimeS = 0.3
18
+ chartRefreshS = 1
19
+ chart = []
20
+ pingTimer = []
21
+ pingIntervalS = 1
22
+
23
+ for ip in ips:
24
+ chart.append(ui.chart({'title': { 'text': ip},
25
+ 'chart': { 'type': 'area', 'zoomType': 'x' },
26
+ 'xAxis': { 'type': 'datetime' },
27
+ 'yAxis': { 'title': {'text':'ms'}},
28
+ 'time': { 'timezoneOffset': 7200},
29
+ 'legend': { 'enabled': False },
30
+ 'series': [{'name' : ip, 'data': [], 'color': '#32a84c'}],
31
+ }
32
+ ).classes('w-full h-64'))
33
+
34
+ log = ui.log(max_lines=30).classes('w-full h-96 bg-black text-white')
35
+
36
+ def clear():
37
+ i = -1
38
+
39
+ for ip in ips:
40
+ i+= 1
41
+ chart[i].options['series'][0]['data'].clear()
42
+
43
+ log.clear()
44
+ log.push("Auto refresh time: " + str(chartRefreshS) + "sec")
45
+
46
+ ui.button('Clear all', on_click=clear)
47
+
48
+ def ping_internal(i):
49
+ global ips, conn, c, maxTimeSec, maxResponseTimeMs
50
+ ip = ips[i]
51
+
52
+ response_time = ping(ip, timeout=maxPingResponseTimeS, unit='ms')
53
+ if response_time is None:
54
+ print(datetime.now().strftime('%H:%M:%S') + " no ping reply from " + ip)
55
+ log.push(datetime.now().strftime('%H:%M:%S') + " no ping reply from " + ip)
56
+ ui.notify(datetime.now().strftime('%H:%M:%S') + " no ping reply from " + ip, type='negative')
57
+
58
+ chart[i].options['series'][0]['data'].append({'x': int(time.time()*1000), 'y': 0, 'marker': { 'radius': 3, 'fillColor': '#eb0909' }})
59
+
60
+ else:
61
+ chart[i].options['series'][0]['data'].append({'x': int(time.time()*1000), 'y': round(response_time,2), 'marker': { 'radius': 0 }})
62
+
63
+ if len(chart[i].options['series'][0]['data']) > maxTimeSec:
64
+ chart[i].options['series'][0]['data'].pop(0)
65
+
66
+ if response_time > pingAlertLimitMs:
67
+ log.push(datetime.now().strftime('%H:%M:%S') + " high ping reply time from " + ip + " > " + str(response_time) + " ms")
68
+
69
+
70
+ def updateCharts():
71
+ global ips, conn, c, maxTimeSec, maxResponseTimeMs
72
+ i = -1
73
+
74
+ for ip in ips:
75
+ i+= 1
76
+ chart[i].update()
77
+
78
+
79
+ i = -1
80
+ for ip in ips:
81
+ i += 1
82
+ pingTimer.append(ui.timer(pingIntervalS, lambda iter=i: ping_internal(iter)))
83
+
84
+ # ui.timer(round(len(ips)*maxPingResponseTimeS+1), lambda: ping_internals())
85
+ chartTimer = ui.timer(chartRefreshS, lambda: updateCharts())
86
+
87
+ log.push("Auto refresh time: " + str(chartRefreshS) + "sec")
88
+ ui.run(title="pinger", show="False", favicon="📶")