Add function to get physical interfaces for bandwidth calculation (#6618)

* Add function to get physical interfaces for bandwidth calculation in get_bandwidth_stats() function

* Add telemetry configuration option for enabled network interfaces, with default values for monitoring bandwidth stats for camera ffmpeg processes, go2rtc, and object detectors. Also add support for FrigateConfig in set_bandwidth_stats function to get bandwidth stats for specified network interfaces
This commit is contained in:
Sergey Krashevich 2023-06-11 15:34:03 +03:00 committed by GitHub
parent 8bc76d19db
commit 8d941e5e26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 5 deletions

View File

@ -558,6 +558,14 @@ ui:
# Optional: Telemetry configuration # Optional: Telemetry configuration
telemetry: telemetry:
# Optional: Enabled network interfaces for bandwidth stats monitoring (default: shown below)
network_interfaces:
- eth
- enp
- eno
- ens
- wl
- lo
# Optional: Enable the latest version outbound check (default: shown below) # Optional: Enable the latest version outbound check (default: shown below)
# NOTE: If you use the HomeAssistant integration, disabling this will prevent it from reporting new versions # NOTE: If you use the HomeAssistant integration, disabling this will prevent it from reporting new versions
version_check: True version_check: True

View File

@ -90,6 +90,10 @@ class UIConfig(FrigateBaseModel):
class TelemetryConfig(FrigateBaseModel): class TelemetryConfig(FrigateBaseModel):
network_interfaces: List[str] = Field(
default=["eth", "enp", "eno", "ens", "wl", "lo"],
title="Enabled network interfaces for bandwidth calculation.",
)
version_check: bool = Field(default=True, title="Enable latest version check.") version_check: bool = Field(default=True, title="Enable latest version check.")

View File

@ -108,7 +108,7 @@ def get_processing_stats(
[ [
asyncio.create_task(set_gpu_stats(config, stats, hwaccel_errors)), asyncio.create_task(set_gpu_stats(config, stats, hwaccel_errors)),
asyncio.create_task(set_cpu_stats(stats)), asyncio.create_task(set_cpu_stats(stats)),
asyncio.create_task(set_bandwidth_stats(stats)), asyncio.create_task(set_bandwidth_stats(config, stats)),
] ]
) )
@ -126,9 +126,9 @@ async def set_cpu_stats(all_stats: dict[str, Any]) -> None:
all_stats["cpu_usages"] = cpu_stats all_stats["cpu_usages"] = cpu_stats
async def set_bandwidth_stats(all_stats: dict[str, Any]) -> None: async def set_bandwidth_stats(config: FrigateConfig, all_stats: dict[str, Any]) -> None:
"""Set bandwidth from nethogs.""" """Set bandwidth from nethogs."""
bandwidth_stats = get_bandwidth_stats() bandwidth_stats = get_bandwidth_stats(config)
if bandwidth_stats: if bandwidth_stats:
all_stats["bandwidth_usages"] = bandwidth_stats all_stats["bandwidth_usages"] = bandwidth_stats

View File

@ -844,10 +844,27 @@ def get_cpu_stats() -> dict[str, dict]:
return usages return usages
def get_bandwidth_stats() -> dict[str, dict]: def get_physical_interfaces(interfaces) -> list:
with open("/proc/net/dev", "r") as file:
lines = file.readlines()
physical_interfaces = []
for line in lines:
if ":" in line:
interface = line.split(":")[0].strip()
for int in interfaces:
if interface.startswith(int):
physical_interfaces.append(interface)
return physical_interfaces
def get_bandwidth_stats(config) -> dict[str, dict]:
"""Get bandwidth usages for each ffmpeg process id""" """Get bandwidth usages for each ffmpeg process id"""
usages = {} usages = {}
top_command = ["nethogs", "-t", "-v0", "-c5", "-d1"] top_command = ["nethogs", "-t", "-v0", "-c5", "-d1"] + get_physical_interfaces(
config.telemetry.network_interfaces
)
p = sp.run( p = sp.run(
top_command, top_command,