summaryrefslogtreecommitdiff
path: root/kvmd/apps
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2024-07-08 04:34:32 +0300
committerMaxim Devaev <[email protected]>2024-07-08 04:34:32 +0300
commiteb1eb527f8b7d0e3156f80564d8798fffacdd3ed (patch)
tree9a9f71c2a1cf35540da0fe86f56a15085df0db24 /kvmd/apps
parent01fff2c7a9404c963800b2df43debb816ad89874 (diff)
pikvm/pikvm#1347: added CPU and MEM to /api/info
Diffstat (limited to 'kvmd/apps')
-rw-r--r--kvmd/apps/__init__.py2
-rw-r--r--kvmd/apps/kvmd/info/hw.py47
2 files changed, 46 insertions, 3 deletions
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py
index b8c4fe5b..25279be5 100644
--- a/kvmd/apps/__init__.py
+++ b/kvmd/apps/__init__.py
@@ -384,7 +384,7 @@ def _get_config_scheme() -> dict:
"hw": {
"vcgencmd_cmd": Option(["/usr/bin/vcgencmd"], type=valid_command),
"ignore_past": Option(False, type=valid_bool),
- "state_poll": Option(10.0, type=valid_float_f01),
+ "state_poll": Option(5.0, type=valid_float_f01),
},
"fan": {
"daemon": Option("kvmd-fan", type=valid_stripped_string),
diff --git a/kvmd/apps/kvmd/info/hw.py b/kvmd/apps/kvmd/info/hw.py
index a436a01f..33cedf70 100644
--- a/kvmd/apps/kvmd/info/hw.py
+++ b/kvmd/apps/kvmd/info/hw.py
@@ -27,6 +27,8 @@ from typing import Callable
from typing import AsyncGenerator
from typing import TypeVar
+import psutil
+
from ....logging import get_logger
from .... import env
@@ -57,11 +59,17 @@ class HwInfoSubmanager(BaseInfoSubmanager):
self.__dt_cache: dict[str, str] = {}
async def get_state(self) -> dict:
- (model, serial, cpu_temp, throttling) = await asyncio.gather(
+ (
+ model, serial, throttling,
+ cpu_percent, cpu_temp,
+ (mem_percent, mem_total, mem_available),
+ ) = await asyncio.gather(
self.__read_dt_file("model"),
self.__read_dt_file("serial-number"),
- self.__get_cpu_temp(),
self.__get_throttling(),
+ self.__get_cpu_percent(),
+ self.__get_cpu_temp(),
+ self.__get_mem(),
)
return {
"platform": {
@@ -73,6 +81,14 @@ class HwInfoSubmanager(BaseInfoSubmanager):
"temp": {
"cpu": cpu_temp,
},
+ "cpu": {
+ "percent": cpu_percent,
+ },
+ "mem": {
+ "percent": mem_percent,
+ "total": mem_total,
+ "available": mem_available,
+ },
"throttling": throttling,
},
}
@@ -106,6 +122,33 @@ class HwInfoSubmanager(BaseInfoSubmanager):
get_logger(0).error("Can't read CPU temp from %s: %s", temp_path, err)
return None
+ async def __get_cpu_percent(self) -> (float | None):
+ try:
+ st = psutil.cpu_times_percent()
+ user = st.user - st.guest
+ nice = st.nice - st.guest_nice
+ idle_all = st.idle + st.iowait
+ system_all = st.system + st.irq + st.softirq
+ virtual = st.guest + st.guest_nice
+ total = max(1, user + nice + system_all + idle_all + st.steal + virtual)
+ return int(
+ st.nice / total * 100
+ + st.user / total * 100
+ + system_all / total * 100
+ + (st.steal + st.guest) / total * 100
+ )
+ except Exception as err:
+ get_logger(0).error("Can't get CPU percent: %s", err)
+ return None
+
+ async def __get_mem(self) -> (tuple[float, int, int] | tuple[None, None, None]):
+ try:
+ st = psutil.virtual_memory()
+ return (st.percent, st.total, st.available)
+ except Exception as err:
+ get_logger(0).error("Can't get memory info: %s", err)
+ return (None, None, None)
+
async def __get_throttling(self) -> (dict | None):
# https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=147781&start=50#p972790
flags = await self.__parse_vcgencmd(