Skip to content

Commit

Permalink
Add containers data (#46)
Browse files Browse the repository at this point in the history
Now each container will provide its own memory / cpu use
  • Loading branch information
pszypowicz authored Nov 29, 2024
1 parent d4f54c9 commit b10f7dd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changes
=======

0.9.0 - 2024-11-15
------------------

- Add support for Glances containers

0.8.0 - 2024-06-09
------------------

Expand Down
8 changes: 8 additions & 0 deletions glances_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
# Glances v4 provides a list of containers
containers_data = self.data.get("containers")
if containers_data:
sensor_data["containers"] = {}
active_containers = [
container
for container in containers_data
Expand All @@ -199,6 +200,13 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
for container in active_containers:
mem_use += container["memory"].get("usage", 0)
sensor_data["docker"]["docker_memory_use"] = round(mem_use / 1024**2, 1)
for container in active_containers:
sensor_data["containers"][container["name"]] = {
"container_cpu_use": round(container["cpu"].get("total", 0), 1),
"container_memory_use": round(
container["memory"].get("usage", 0) / 1024**2, 1
),
}
if data := self.data.get("raid"):
sensor_data["raid"] = data
if data := self.data.get("uptime"):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "glances_api"
version = "0.8.0"
version = "0.9.0"
description = "Python API for interacting with Glances"
authors = ["Fabian Affolter <fabian@affolter-engineering.ch>"]
homepage = "https://github.com/home-assistant-ecosystem/python-glances-api"
Expand Down
24 changes: 24 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,16 @@
"fan_speed": 0,
},
},
"containers": {
"container1": {
"container_cpu_use": 50.9,
"container_memory_use": 1068.4,
},
"container2": {
"container_cpu_use": 26.2,
"container_memory_use": 81.2,
},
},
}

RESPONSE_V4: dict[str, Any] = {
Expand Down Expand Up @@ -377,6 +387,16 @@
"network": {
"eth0": {"is_up": None, "rx": 6377770.0, "speed": 1.0, "tx": 41670.0},
},
"containers": {
"container1": {
"container_cpu_use": 0.4,
"container_memory_use": 0.0,
},
"container2": {
"container_cpu_use": 0.0,
"container_memory_use": 0.0,
},
},
}


Expand Down Expand Up @@ -447,6 +467,10 @@ async def test_ha_sensor_data_with_incomplete_container_information(
ha_sensor_data = HA_SENSOR_DATA.copy()
ha_sensor_data["docker"]["docker_memory_use"] = 0
ha_sensor_data["docker"]["docker_cpu_use"] = 0
ha_sensor_data["containers"]["container1"]["container_memory_use"] = 0
ha_sensor_data["containers"]["container1"]["container_cpu_use"] = 0
ha_sensor_data["containers"]["container2"]["container_memory_use"] = 0
ha_sensor_data["containers"]["container2"]["container_cpu_use"] = 0

httpx_mock.add_response(json=response)

Expand Down

0 comments on commit b10f7dd

Please sign in to comment.