Skip to content

Commit

Permalink
Add support for Glances v4 network sensors (#40)
Browse files Browse the repository at this point in the history
* Add support for Glances v4 network sensors

* Add unit test

* Add support for sensor type Enum in v4

* Revert "Add support for sensor type Enum in v4"

This reverts commit 2d70977.
  • Loading branch information
wittypluck committed May 21, 2024
1 parent 3b6e557 commit 0612bb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
12 changes: 10 additions & 2 deletions glances_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,18 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
sensor_data["network"] = {}
for network in networks:
time_since_update = network["time_since_update"]
# New name of network sensors in Glances v4
rx = network.get("bytes_recv_rate_per_sec")
tx = network.get("bytes_sent_rate_per_sec")
# Compatibility with Glances v3
if rx is None and (rx_bytes := network.get("rx")) is not None:
rx = round(rx_bytes / time_since_update)
if tx is None and (tx_bytes := network.get("tx")) is not None:
tx = round(tx_bytes / time_since_update)
sensor_data["network"][network["interface_name"]] = {
"is_up": network.get("is_up"),
"rx": round(network["rx"] / time_since_update),
"tx": round(network["tx"] / time_since_update),
"rx": rx,
"tx": tx,
"speed": round(network["speed"] / 1024**3, 1),
}
data = self.data.get("dockers") or self.data.get("containers")
Expand Down
16 changes: 16 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@
"time_since_update": 1.55433297157288,
"tx": 0,
},
{
"bytes_sent": 1070106,
"bytes_recv": 163781155,
"speed": 1048576000,
"key": "interface_name",
"interface_name": "eth0_v4",
"bytes_all": 164851261,
"time_since_update": 25.680001497268677,
"bytes_recv_gauge": 5939087689,
"bytes_recv_rate_per_sec": 6377770.0,
"bytes_sent_gauge": 82538934,
"bytes_sent_rate_per_sec": 41670.0,
"bytes_all_gauge": 6021626623,
"bytes_all_rate_per_sec": 6419441.0,
},
],
"sensors": [
{
Expand Down Expand Up @@ -292,6 +307,7 @@
"eth0": {"is_up": True, "rx": 3953, "tx": 5995, "speed": 9.8},
"tunl0": {"is_up": False, "rx": 0.0, "tx": 0.0, "speed": 0.0},
"sit0": {"is_up": False, "rx": 0.0, "tx": 0.0, "speed": 0.0},
"eth0_v4": {"is_up": None, "rx": 6377770.0, "speed": 1.0, "tx": 41670.0},
},
"docker": {"docker_active": 2, "docker_cpu_use": 77.2, "docker_memory_use": 1149.6},
"uptime": "3 days, 10:25:20",
Expand Down

0 comments on commit 0612bb3

Please sign in to comment.