PingableSystem: filter out the ping from the command output instead of showing the whole output on the dashboard, changed default ui refresh interval to 5 seconds

This commit is contained in:
ChaoticByte 2025-02-24 22:49:50 +01:00
parent c4a71ac0dc
commit fa2d10198f
No known key found for this signature in database
2 changed files with 16 additions and 5 deletions

View file

@ -4,13 +4,14 @@
# additional libraries # additional libraries
import platform import platform
import requests import re
import subprocess import subprocess
import time import time
from enum import Enum from enum import Enum
from typing import Tuple from typing import Tuple
import requests
# base classes and types and stuff # base classes and types and stuff
@ -46,6 +47,8 @@ class System:
# some basic systems # some basic systems
ping_time_regex = re.compile(r".*ttl=\d+ time=((?:\d+\.)?\d+ ms).*")
class PingableSystem(System): class PingableSystem(System):
def __init__(self, name, description, host: str): def __init__(self, name, description, host: str):
@ -55,7 +58,11 @@ class PingableSystem(System):
def ping(self) -> Tuple[bool, str, str]: def ping(self) -> Tuple[bool, str, str]:
if platform.system().lower() == "windows": p = "-n" if platform.system().lower() == "windows": p = "-n"
else: p = "-c" else: p = "-c"
s = subprocess.run(["ping", p, '1', self.host], stdout=subprocess.PIPE, stderr=subprocess.PIPE) s = subprocess.run(
["ping", p, '1', self.host],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env={"LC_ALL": "C"} # don't translate
)
return s.returncode == 0, s.stdout.decode(), s.stderr.decode() return s.returncode == 0, s.stdout.decode(), s.stderr.decode()
def update_state(self): def update_state(self):
@ -63,10 +70,14 @@ class PingableSystem(System):
ok, stdout, stderr = self.ping() ok, stdout, stderr = self.ping()
if ok: if ok:
self.state = SystemState.OK self.state = SystemState.OK
self.state_verbose = stdout p_matches = ping_time_regex.findall(stdout)
if len(p_matches) > 0:
self.state_verbose = f"Ping: {p_matches[0]}"
else:
self.state_verbose = stdout.strip("\n\r ")
else: else:
self.state = SystemState.FAILED self.state = SystemState.FAILED
self.state_verbose = stdout + "\n" + stderr self.state_verbose = (stdout + "\n" + stderr).strip("\n\r ")
class HTTPServer(System): class HTTPServer(System):

View file

@ -11,7 +11,7 @@ from nicegui import ui, html, run
def init_ui( def init_ui(
systems: List[System], systems: List[System],
ui_refresh_interval: float = 2, # in seconds ui_refresh_interval: float = 5, # in seconds
system_state_update_interval: float = 15 # in seconds system_state_update_interval: float = 15 # in seconds
): ):