Skip to content

Commit 0612bb3

Browse files
authored
Add support for Glances v4 network sensors (#40)
* 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.
1 parent 3b6e557 commit 0612bb3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

glances_api/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,18 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
155155
sensor_data["network"] = {}
156156
for network in networks:
157157
time_since_update = network["time_since_update"]
158+
# New name of network sensors in Glances v4
159+
rx = network.get("bytes_recv_rate_per_sec")
160+
tx = network.get("bytes_sent_rate_per_sec")
161+
# Compatibility with Glances v3
162+
if rx is None and (rx_bytes := network.get("rx")) is not None:
163+
rx = round(rx_bytes / time_since_update)
164+
if tx is None and (tx_bytes := network.get("tx")) is not None:
165+
tx = round(tx_bytes / time_since_update)
158166
sensor_data["network"][network["interface_name"]] = {
159167
"is_up": network.get("is_up"),
160-
"rx": round(network["rx"] / time_since_update),
161-
"tx": round(network["tx"] / time_since_update),
168+
"rx": rx,
169+
"tx": tx,
162170
"speed": round(network["speed"] / 1024**3, 1),
163171
}
164172
data = self.data.get("dockers") or self.data.get("containers")

tests/test_responses.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@
251251
"time_since_update": 1.55433297157288,
252252
"tx": 0,
253253
},
254+
{
255+
"bytes_sent": 1070106,
256+
"bytes_recv": 163781155,
257+
"speed": 1048576000,
258+
"key": "interface_name",
259+
"interface_name": "eth0_v4",
260+
"bytes_all": 164851261,
261+
"time_since_update": 25.680001497268677,
262+
"bytes_recv_gauge": 5939087689,
263+
"bytes_recv_rate_per_sec": 6377770.0,
264+
"bytes_sent_gauge": 82538934,
265+
"bytes_sent_rate_per_sec": 41670.0,
266+
"bytes_all_gauge": 6021626623,
267+
"bytes_all_rate_per_sec": 6419441.0,
268+
},
254269
],
255270
"sensors": [
256271
{
@@ -292,6 +307,7 @@
292307
"eth0": {"is_up": True, "rx": 3953, "tx": 5995, "speed": 9.8},
293308
"tunl0": {"is_up": False, "rx": 0.0, "tx": 0.0, "speed": 0.0},
294309
"sit0": {"is_up": False, "rx": 0.0, "tx": 0.0, "speed": 0.0},
310+
"eth0_v4": {"is_up": None, "rx": 6377770.0, "speed": 1.0, "tx": 41670.0},
295311
},
296312
"docker": {"docker_active": 2, "docker_cpu_use": 77.2, "docker_memory_use": 1149.6},
297313
"uptime": "3 days, 10:25:20",

0 commit comments

Comments
 (0)