mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
health event instead of hw
This commit is contained in:
@@ -57,7 +57,7 @@ class ExportApi:
|
||||
async def __get_prometheus_metrics(self) -> str:
|
||||
(atx_state, info_state, gpio_state) = await asyncio.gather(*[
|
||||
self.__atx.get_state(),
|
||||
self.__info_manager.get_state(["hw", "fan"]),
|
||||
self.__info_manager.get_state(["health", "fan"]),
|
||||
self.__user_gpio.get_state(),
|
||||
])
|
||||
rows: list[str] = []
|
||||
@@ -71,7 +71,7 @@ class ExportApi:
|
||||
for key in ["online", "state"]:
|
||||
self.__append_prometheus_rows(rows, ch_state["state"], f"pikvm_gpio_{mode}_{key}_{channel}")
|
||||
|
||||
self.__append_prometheus_rows(rows, info_state["hw"]["health"], "pikvm_hw") # type: ignore
|
||||
self.__append_prometheus_rows(rows, info_state["health"], "pikvm_hw") # type: ignore
|
||||
self.__append_prometheus_rows(rows, info_state["fan"], "pikvm_fan")
|
||||
|
||||
return "\n".join(rows)
|
||||
|
||||
@@ -45,7 +45,10 @@ class InfoApi:
|
||||
|
||||
def __valid_info_fields(self, req: Request) -> list[str]:
|
||||
available = self.__info_manager.get_subs()
|
||||
available.add("hw")
|
||||
default = set(available)
|
||||
default.remove("health")
|
||||
return sorted(valid_info_fields(
|
||||
arg=req.query.get("fields", ",".join(available)),
|
||||
variants=available,
|
||||
arg=req.query.get("fields", ",".join(default)),
|
||||
variants=(available),
|
||||
) or available)
|
||||
|
||||
@@ -31,7 +31,7 @@ from .auth import AuthInfoSubmanager
|
||||
from .system import SystemInfoSubmanager
|
||||
from .meta import MetaInfoSubmanager
|
||||
from .extras import ExtrasInfoSubmanager
|
||||
from .hw import HwInfoSubmanager
|
||||
from .health import HealthInfoSubmanager
|
||||
from .fan import FanInfoSubmanager
|
||||
|
||||
|
||||
@@ -39,11 +39,11 @@ from .fan import FanInfoSubmanager
|
||||
class InfoManager:
|
||||
def __init__(self, config: Section) -> None:
|
||||
self.__subs: dict[str, BaseInfoSubmanager] = {
|
||||
"system": SystemInfoSubmanager(config.kvmd.streamer.cmd),
|
||||
"system": SystemInfoSubmanager(config.kvmd.info.hw.platform, config.kvmd.streamer.cmd),
|
||||
"auth": AuthInfoSubmanager(config.kvmd.auth.enabled),
|
||||
"meta": MetaInfoSubmanager(config.kvmd.info.meta),
|
||||
"extras": ExtrasInfoSubmanager(config),
|
||||
"hw": HwInfoSubmanager(**config.kvmd.info.hw._unpack()),
|
||||
"health": HealthInfoSubmanager(**config.kvmd.info.hw._unpack(ignore="platform")),
|
||||
"fan": FanInfoSubmanager(**config.kvmd.info.fan._unpack()),
|
||||
}
|
||||
self.__queue: "asyncio.Queue[tuple[str, (dict | None)]]" = asyncio.Queue()
|
||||
@@ -52,12 +52,29 @@ class InfoManager:
|
||||
return set(self.__subs)
|
||||
|
||||
async def get_state(self, fields: (list[str] | None)=None) -> dict:
|
||||
fields = (fields or list(self.__subs))
|
||||
return dict(zip(fields, await asyncio.gather(*[
|
||||
fields = set(fields or list(self.__subs))
|
||||
|
||||
hw = ("hw" in fields) # Old for compatible
|
||||
system = ("system" in fields)
|
||||
if hw:
|
||||
fields.remove("hw")
|
||||
fields.add("health")
|
||||
fields.add("system")
|
||||
|
||||
state = dict(zip(fields, await asyncio.gather(*[
|
||||
self.__subs[field].get_state()
|
||||
for field in fields
|
||||
])))
|
||||
|
||||
if hw:
|
||||
state["hw"] = {
|
||||
"health": state.pop("health"),
|
||||
"platform": state["system"].pop("platform"),
|
||||
}
|
||||
if not system:
|
||||
state.pop("system")
|
||||
return state
|
||||
|
||||
async def trigger_state(self) -> None:
|
||||
await asyncio.gather(*[
|
||||
sub.trigger_state()
|
||||
@@ -70,7 +87,7 @@ class InfoManager:
|
||||
# - auth -- Partial
|
||||
# - meta -- Partial, nullable
|
||||
# - extras -- Partial, nullable
|
||||
# - hw -- Partial
|
||||
# - health -- Partial
|
||||
# - fan -- Partial
|
||||
# ===========================
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
import os
|
||||
import asyncio
|
||||
import copy
|
||||
|
||||
@@ -45,59 +44,41 @@ _RetvalT = TypeVar("_RetvalT")
|
||||
|
||||
|
||||
# =====
|
||||
class HwInfoSubmanager(BaseInfoSubmanager):
|
||||
class HealthInfoSubmanager(BaseInfoSubmanager):
|
||||
def __init__(
|
||||
self,
|
||||
platform_path: str,
|
||||
vcgencmd_cmd: list[str],
|
||||
ignore_past: bool,
|
||||
state_poll: float,
|
||||
) -> None:
|
||||
|
||||
self.__platform_path = platform_path
|
||||
self.__vcgencmd_cmd = vcgencmd_cmd
|
||||
self.__ignore_past = ignore_past
|
||||
self.__state_poll = state_poll
|
||||
|
||||
self.__dt_cache: dict[str, str] = {}
|
||||
|
||||
self.__notifier = aiotools.AioNotifier()
|
||||
|
||||
async def get_state(self) -> dict:
|
||||
(
|
||||
base,
|
||||
serial,
|
||||
platform,
|
||||
throttling,
|
||||
cpu_percent,
|
||||
cpu_temp,
|
||||
mem,
|
||||
) = await asyncio.gather(
|
||||
self.__read_dt_file("model", upper=False),
|
||||
self.__read_dt_file("serial-number", upper=True),
|
||||
self.__read_platform_file(),
|
||||
self.__get_throttling(),
|
||||
self.__get_cpu_percent(),
|
||||
self.__get_cpu_temp(),
|
||||
self.__get_mem(),
|
||||
)
|
||||
return {
|
||||
"platform": {
|
||||
"type": "rpi",
|
||||
"base": base,
|
||||
"serial": serial,
|
||||
**platform, # type: ignore
|
||||
"temp": {
|
||||
"cpu": cpu_temp,
|
||||
},
|
||||
"health": {
|
||||
"temp": {
|
||||
"cpu": cpu_temp,
|
||||
},
|
||||
"cpu": {
|
||||
"percent": cpu_percent,
|
||||
},
|
||||
"mem": mem,
|
||||
"throttling": throttling,
|
||||
"cpu": {
|
||||
"percent": cpu_percent,
|
||||
},
|
||||
"mem": mem,
|
||||
"throttling": throttling,
|
||||
}
|
||||
|
||||
async def trigger_state(self) -> None:
|
||||
@@ -115,35 +96,6 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
||||
|
||||
# =====
|
||||
|
||||
async def __read_dt_file(self, name: str, upper: bool) -> (str | None):
|
||||
if name not in self.__dt_cache:
|
||||
path = os.path.join(f"{env.PROCFS_PREFIX}/proc/device-tree", name)
|
||||
try:
|
||||
value = (await aiotools.read_file(path)).strip(" \t\r\n\0")
|
||||
self.__dt_cache[name] = (value.upper() if upper else value)
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't read DT %s from %s: %s", name, path, ex)
|
||||
return None
|
||||
return self.__dt_cache[name]
|
||||
|
||||
async def __read_platform_file(self) -> dict:
|
||||
try:
|
||||
text = await aiotools.read_file(self.__platform_path)
|
||||
parsed: dict[str, str] = {}
|
||||
for row in text.split("\n"):
|
||||
row = row.strip()
|
||||
if row:
|
||||
(key, value) = row.split("=", 1)
|
||||
parsed[key.strip()] = value.strip()
|
||||
return {
|
||||
"model": parsed["PIKVM_MODEL"],
|
||||
"video": parsed["PIKVM_VIDEO"],
|
||||
"board": parsed["PIKVM_BOARD"],
|
||||
}
|
||||
except Exception:
|
||||
get_logger(0).exception("Can't read device model")
|
||||
return {"model": None, "video": None, "board": None}
|
||||
|
||||
async def __get_cpu_temp(self) -> (float | None):
|
||||
temp_path = f"{env.SYSFS_PREFIX}/sys/class/thermal/thermal_zone0/temp"
|
||||
try:
|
||||
@@ -28,6 +28,7 @@ from typing import AsyncGenerator
|
||||
|
||||
from ....logging import get_logger
|
||||
|
||||
from .... import env
|
||||
from .... import aiotools
|
||||
from .... import aioproc
|
||||
|
||||
@@ -38,12 +39,30 @@ from .base import BaseInfoSubmanager
|
||||
|
||||
# =====
|
||||
class SystemInfoSubmanager(BaseInfoSubmanager):
|
||||
def __init__(self, streamer_cmd: list[str]) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
platform_path: str,
|
||||
streamer_cmd: list[str],
|
||||
) -> None:
|
||||
|
||||
self.__platform_path = platform_path
|
||||
self.__streamer_cmd = streamer_cmd
|
||||
|
||||
self.__dt_cache: dict[str, str] = {}
|
||||
self.__notifier = aiotools.AioNotifier()
|
||||
|
||||
async def get_state(self) -> dict:
|
||||
streamer_info = await self.__get_streamer_info()
|
||||
(
|
||||
base,
|
||||
serial,
|
||||
pl,
|
||||
streamer_info,
|
||||
) = await asyncio.gather(
|
||||
self.__read_dt_file("model", upper=False),
|
||||
self.__read_dt_file("serial-number", upper=True),
|
||||
self.__read_platform_file(),
|
||||
self.__get_streamer_info(),
|
||||
)
|
||||
uname_info = platform.uname() # Uname using the internal cache
|
||||
return {
|
||||
"kvmd": {"version": __version__},
|
||||
@@ -52,6 +71,12 @@ class SystemInfoSubmanager(BaseInfoSubmanager):
|
||||
field: getattr(uname_info, field)
|
||||
for field in ["system", "release", "version", "machine"]
|
||||
},
|
||||
"platform": {
|
||||
"type": "rpi",
|
||||
"base": base,
|
||||
"serial": serial,
|
||||
**pl, # type: ignore
|
||||
},
|
||||
}
|
||||
|
||||
async def trigger_state(self) -> None:
|
||||
@@ -64,6 +89,35 @@ class SystemInfoSubmanager(BaseInfoSubmanager):
|
||||
|
||||
# =====
|
||||
|
||||
async def __read_dt_file(self, name: str, upper: bool) -> (str | None):
|
||||
if name not in self.__dt_cache:
|
||||
path = os.path.join(f"{env.PROCFS_PREFIX}/proc/device-tree", name)
|
||||
try:
|
||||
value = (await aiotools.read_file(path)).strip(" \t\r\n\0")
|
||||
self.__dt_cache[name] = (value.upper() if upper else value)
|
||||
except Exception as ex:
|
||||
get_logger(0).error("Can't read DT %s from %s: %s", name, path, ex)
|
||||
return None
|
||||
return self.__dt_cache[name]
|
||||
|
||||
async def __read_platform_file(self) -> dict:
|
||||
try:
|
||||
text = await aiotools.read_file(self.__platform_path)
|
||||
parsed: dict[str, str] = {}
|
||||
for row in text.split("\n"):
|
||||
row = row.strip()
|
||||
if row:
|
||||
(key, value) = row.split("=", 1)
|
||||
parsed[key.strip()] = value.strip()
|
||||
return {
|
||||
"model": parsed["PIKVM_MODEL"],
|
||||
"video": parsed["PIKVM_VIDEO"],
|
||||
"board": parsed["PIKVM_BOARD"],
|
||||
}
|
||||
except Exception:
|
||||
get_logger(0).exception("Can't read device model")
|
||||
return {"model": None, "video": None, "board": None}
|
||||
|
||||
async def __get_streamer_info(self) -> dict:
|
||||
version = ""
|
||||
features: dict[str, bool] = {}
|
||||
|
||||
Reference in New Issue
Block a user