From 2c3ab22565fe8601ab111d35006d3882970f1fea Mon Sep 17 00:00:00 2001 From: perco Date: Tue, 30 Jun 2026 10:39:19 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20trafic=20r=C3=A9seau=20=E2=80=94=20ne=20?= =?UTF-8?q?compter=20que=20les=20interfaces=20physiques?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit psutil.net_io_counters() sans filtre additionne br-*, veth*, docker0 ce qui triplait le trafic réel. On ne garde maintenant que les interfaces physiques (enp*, eth*, wlan*, bond*) pour DL/UL et total. Co-Authored-By: Claude Sonnet 4.6 --- app/main.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index bdcc9fe..08469cb 100644 --- a/app/main.py +++ b/app/main.py @@ -10,6 +10,7 @@ app = FastAPI() HOST_ROOT = os.environ.get("HOST_ROOT", "") _net_last = {"t": 0.0, "bytes_sent": 0, "bytes_recv": 0} +_PHYS_IFACE = re.compile(r"^(eth|en[ps]|wlan|wlp|bond|ens)\d") _hw_cache: dict = {} _hw_cache_ts = 0.0 HW_TTL = 60.0 @@ -296,18 +297,22 @@ def get_live() -> dict: swap = psutil.swap_memory() disks = _read_host_disks() - net_now = psutil.net_io_counters() + # Trafic réseau : ne sommer que les interfaces physiques (exclut br-, veth, docker0…) + per_nic = psutil.net_io_counters(pernic=True) + phys = {k: v for k, v in per_nic.items() if _PHYS_IFACE.match(k)} + recv_total = sum(v.bytes_recv for v in phys.values()) + sent_total = sum(v.bytes_sent for v in phys.values()) now = time.time() dt = now - _net_last["t"] if _net_last["t"] else 1.0 - dl = (net_now.bytes_recv - _net_last["bytes_recv"]) / dt if _net_last["t"] else 0 - ul = (net_now.bytes_sent - _net_last["bytes_sent"]) / dt if _net_last["t"] else 0 - _net_last = {"t": now, "bytes_sent": net_now.bytes_sent, "bytes_recv": net_now.bytes_recv} + dl = (recv_total - _net_last["bytes_recv"]) / dt if _net_last["t"] else 0 + ul = (sent_total - _net_last["bytes_sent"]) / dt if _net_last["t"] else 0 + _net_last = {"t": now, "bytes_sent": sent_total, "bytes_recv": recv_total} - # Interfaces : filtrer loopback et bridges Docker + # Interfaces physiques uniquement pour l'affichage net_ifaces = {} stats = psutil.net_if_stats() for iface, addrs in psutil.net_if_addrs().items(): - if iface == "lo" or iface.startswith("br-") or iface == "docker0": + if not _PHYS_IFACE.match(iface): continue for a in addrs: if a.family == 2: @@ -349,8 +354,8 @@ def get_live() -> dict: "network": { "dl_bps": max(0.0, dl), "ul_bps": max(0.0, ul), - "total_recv": net_now.bytes_recv, - "total_sent": net_now.bytes_sent, + "total_recv": recv_total, + "total_sent": sent_total, "interfaces": net_ifaces, }, "temperatures": temps,