Spaces:
Paused
Paused
File size: 2,736 Bytes
8c74a95 |
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 |
#!/usr/bin/env python3
import time
from datetime import datetime
from nicegui import ui, app
import os
from ping3 import ping, verbose_ping
if 'PINGER_IPS' not in os.environ:
print('define PINGER_IPS > 1.1.1.1,8.8.8.8')
quit()
ips = os.getenv('PINGER_IPS').split(',')
maxTimeSec = 600
pingAlertLimitMs = 100
maxPingResponseTimeS = 0.3
chartRefreshS = 1
chart = []
pingTimer = []
pingIntervalS = 1
for ip in ips:
chart.append(ui.chart({'title': { 'text': ip},
'chart': { 'type': 'area', 'zoomType': 'x' },
'xAxis': { 'type': 'datetime' },
'yAxis': { 'title': {'text':'ms'}},
'time': { 'timezoneOffset': 7200},
'legend': { 'enabled': False },
'series': [{'name' : ip, 'data': [], 'color': '#32a84c'}],
}
).classes('w-full h-64'))
log = ui.log(max_lines=30).classes('w-full h-96 bg-black text-white')
def clear():
i = -1
for ip in ips:
i+= 1
chart[i].options['series'][0]['data'].clear()
log.clear()
log.push("Auto refresh time: " + str(chartRefreshS) + "sec")
ui.button('Clear all', on_click=clear)
def ping_internal(i):
global ips, conn, c, maxTimeSec, maxResponseTimeMs
ip = ips[i]
response_time = ping(ip, timeout=maxPingResponseTimeS, unit='ms')
if response_time is None:
print(datetime.now().strftime('%H:%M:%S') + " no ping reply from " + ip)
log.push(datetime.now().strftime('%H:%M:%S') + " no ping reply from " + ip)
ui.notify(datetime.now().strftime('%H:%M:%S') + " no ping reply from " + ip, type='negative')
chart[i].options['series'][0]['data'].append({'x': int(time.time()*1000), 'y': 0, 'marker': { 'radius': 3, 'fillColor': '#eb0909' }})
else:
chart[i].options['series'][0]['data'].append({'x': int(time.time()*1000), 'y': round(response_time,2), 'marker': { 'radius': 0 }})
if len(chart[i].options['series'][0]['data']) > maxTimeSec:
chart[i].options['series'][0]['data'].pop(0)
if response_time > pingAlertLimitMs:
log.push(datetime.now().strftime('%H:%M:%S') + " high ping reply time from " + ip + " > " + str(response_time) + " ms")
def updateCharts():
global ips, conn, c, maxTimeSec, maxResponseTimeMs
i = -1
for ip in ips:
i+= 1
chart[i].update()
i = -1
for ip in ips:
i += 1
pingTimer.append(ui.timer(pingIntervalS, lambda iter=i: ping_internal(iter)))
# ui.timer(round(len(ips)*maxPingResponseTimeS+1), lambda: ping_internals())
chartTimer = ui.timer(chartRefreshS, lambda: updateCharts())
log.push("Auto refresh time: " + str(chartRefreshS) + "sec")
ui.run(title="pinger", show="False", favicon="📶") |