Skip to content

Commit b10f7dd

Browse files
authored
Add containers data (#46)
Now each container will provide its own memory / cpu use
1 parent d4f54c9 commit b10f7dd

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changes
22
=======
33

4+
0.9.0 - 2024-11-15
5+
------------------
6+
7+
- Add support for Glances containers
8+
49
0.8.0 - 2024-06-09
510
------------------
611

glances_api/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
183183
# Glances v4 provides a list of containers
184184
containers_data = self.data.get("containers")
185185
if containers_data:
186+
sensor_data["containers"] = {}
186187
active_containers = [
187188
container
188189
for container in containers_data
@@ -199,6 +200,13 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
199200
for container in active_containers:
200201
mem_use += container["memory"].get("usage", 0)
201202
sensor_data["docker"]["docker_memory_use"] = round(mem_use / 1024**2, 1)
203+
for container in active_containers:
204+
sensor_data["containers"][container["name"]] = {
205+
"container_cpu_use": round(container["cpu"].get("total", 0), 1),
206+
"container_memory_use": round(
207+
container["memory"].get("usage", 0) / 1024**2, 1
208+
),
209+
}
202210
if data := self.data.get("raid"):
203211
sensor_data["raid"] = data
204212
if data := self.data.get("uptime"):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "glances_api"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
description = "Python API for interacting with Glances"
55
authors = ["Fabian Affolter <[email protected]>"]
66
homepage = "https://github.com/home-assistant-ecosystem/python-glances-api"

tests/test_responses.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@
314314
"fan_speed": 0,
315315
},
316316
},
317+
"containers": {
318+
"container1": {
319+
"container_cpu_use": 50.9,
320+
"container_memory_use": 1068.4,
321+
},
322+
"container2": {
323+
"container_cpu_use": 26.2,
324+
"container_memory_use": 81.2,
325+
},
326+
},
317327
}
318328

319329
RESPONSE_V4: dict[str, Any] = {
@@ -377,6 +387,16 @@
377387
"network": {
378388
"eth0": {"is_up": None, "rx": 6377770.0, "speed": 1.0, "tx": 41670.0},
379389
},
390+
"containers": {
391+
"container1": {
392+
"container_cpu_use": 0.4,
393+
"container_memory_use": 0.0,
394+
},
395+
"container2": {
396+
"container_cpu_use": 0.0,
397+
"container_memory_use": 0.0,
398+
},
399+
},
380400
}
381401

382402

@@ -447,6 +467,10 @@ async def test_ha_sensor_data_with_incomplete_container_information(
447467
ha_sensor_data = HA_SENSOR_DATA.copy()
448468
ha_sensor_data["docker"]["docker_memory_use"] = 0
449469
ha_sensor_data["docker"]["docker_cpu_use"] = 0
470+
ha_sensor_data["containers"]["container1"]["container_memory_use"] = 0
471+
ha_sensor_data["containers"]["container1"]["container_cpu_use"] = 0
472+
ha_sensor_data["containers"]["container2"]["container_memory_use"] = 0
473+
ha_sensor_data["containers"]["container2"]["container_cpu_use"] = 0
450474

451475
httpx_mock.add_response(json=response)
452476

0 commit comments

Comments
 (0)