mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 01:00:29 +08:00
pikvm/pikvm#1347: added CPU and MEM to /api/info
This commit is contained in:
parent
01fff2c7a9
commit
eb1eb527f8
@ -384,7 +384,7 @@ def _get_config_scheme() -> dict:
|
|||||||
"hw": {
|
"hw": {
|
||||||
"vcgencmd_cmd": Option(["/usr/bin/vcgencmd"], type=valid_command),
|
"vcgencmd_cmd": Option(["/usr/bin/vcgencmd"], type=valid_command),
|
||||||
"ignore_past": Option(False, type=valid_bool),
|
"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": {
|
"fan": {
|
||||||
"daemon": Option("kvmd-fan", type=valid_stripped_string),
|
"daemon": Option("kvmd-fan", type=valid_stripped_string),
|
||||||
|
|||||||
@ -27,6 +27,8 @@ from typing import Callable
|
|||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
from typing import TypeVar
|
from typing import TypeVar
|
||||||
|
|
||||||
|
import psutil
|
||||||
|
|
||||||
from ....logging import get_logger
|
from ....logging import get_logger
|
||||||
|
|
||||||
from .... import env
|
from .... import env
|
||||||
@ -57,11 +59,17 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
|||||||
self.__dt_cache: dict[str, str] = {}
|
self.__dt_cache: dict[str, str] = {}
|
||||||
|
|
||||||
async def get_state(self) -> dict:
|
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("model"),
|
||||||
self.__read_dt_file("serial-number"),
|
self.__read_dt_file("serial-number"),
|
||||||
self.__get_cpu_temp(),
|
|
||||||
self.__get_throttling(),
|
self.__get_throttling(),
|
||||||
|
self.__get_cpu_percent(),
|
||||||
|
self.__get_cpu_temp(),
|
||||||
|
self.__get_mem(),
|
||||||
)
|
)
|
||||||
return {
|
return {
|
||||||
"platform": {
|
"platform": {
|
||||||
@ -73,6 +81,14 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
|||||||
"temp": {
|
"temp": {
|
||||||
"cpu": cpu_temp,
|
"cpu": cpu_temp,
|
||||||
},
|
},
|
||||||
|
"cpu": {
|
||||||
|
"percent": cpu_percent,
|
||||||
|
},
|
||||||
|
"mem": {
|
||||||
|
"percent": mem_percent,
|
||||||
|
"total": mem_total,
|
||||||
|
"available": mem_available,
|
||||||
|
},
|
||||||
"throttling": throttling,
|
"throttling": throttling,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -106,6 +122,33 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
|||||||
get_logger(0).error("Can't read CPU temp from %s: %s", temp_path, err)
|
get_logger(0).error("Can't read CPU temp from %s: %s", temp_path, err)
|
||||||
return None
|
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):
|
async def __get_throttling(self) -> (dict | None):
|
||||||
# https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=147781&start=50#p972790
|
# https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=147781&start=50#p972790
|
||||||
flags = await self.__parse_vcgencmd(
|
flags = await self.__parse_vcgencmd(
|
||||||
|
|||||||
@ -132,7 +132,7 @@ export function Session() {
|
|||||||
if (__info_hw_state !== null) {
|
if (__info_hw_state !== null) {
|
||||||
html += `
|
html += `
|
||||||
Platform:
|
Platform:
|
||||||
${__formatPlatform(__info_hw_state.platform)}
|
${__formatMisc(__info_hw_state)}
|
||||||
<hr>
|
<hr>
|
||||||
Temperature:
|
Temperature:
|
||||||
${__formatTemp(__info_hw_state.health.temp)}
|
${__formatTemp(__info_hw_state.health.temp)}
|
||||||
@ -153,8 +153,13 @@ export function Session() {
|
|||||||
$("about-hardware").innerHTML = html;
|
$("about-hardware").innerHTML = html;
|
||||||
};
|
};
|
||||||
|
|
||||||
var __formatPlatform = function(state) {
|
var __formatMisc = function(state) {
|
||||||
return __formatUl([["Base", state.base], ["Serial", state.serial]]);
|
return __formatUl([
|
||||||
|
["Base", state.platform.base],
|
||||||
|
["Serial", state.platform.serial],
|
||||||
|
["CPU", `${state.health.cpu.percent}%`],
|
||||||
|
["MEM", `${state.health.mem.percent}%`],
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
var __formatFan = function(state) {
|
var __formatFan = function(state) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user