mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
implement cpu temperature
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use common::metrics::{CPU, Disk, Memory, Metrics, Network, SystemInfo};
|
||||
use common::metrics::{Disk, Memory, Metrics, Network, SystemInfo, CPU};
|
||||
use sysinfo::{Disks, Networks, System};
|
||||
|
||||
use crate::cpu::CpuStatReader;
|
||||
@@ -54,7 +54,8 @@ impl Collector {
|
||||
CPU {
|
||||
usage: self.sys.global_cpu_usage(),
|
||||
threads: self.sys.cpus().len(),
|
||||
breakdown: self.cpu_reader.read_global_cpu_stats().unwrap_or_default(),
|
||||
breakdown: self.cpu_reader.cpu_breakdown().unwrap_or_default(),
|
||||
temperature: self.cpu_reader.cpu_temperature().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,10 +31,14 @@ impl CpuValues {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_os = "linux"), derive(Default))]
|
||||
pub struct CpuStatReader {
|
||||
previous_stats: HashMap<String, CpuValues>,
|
||||
}
|
||||
|
||||
const CPU_LINE_PATH: &str = "/proc/stat";
|
||||
const CPU_TEMP_PATH: &str = "/sys/class/thermal/thermal_zone0/temp";
|
||||
|
||||
impl CpuStatReader {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
@@ -42,8 +46,9 @@ impl CpuStatReader {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_global_cpu_stats(&mut self) -> anyhow::Result<CpuBreakdown> {
|
||||
let content = std::fs::read_to_string("/proc/stat").unwrap();
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn cpu_breakdown(&mut self) -> anyhow::Result<CpuBreakdown> {
|
||||
let content = std::fs::read_to_string(CPU_LINE_PATH)?;
|
||||
|
||||
let cpu_line = content
|
||||
.lines()
|
||||
@@ -53,6 +58,16 @@ impl CpuStatReader {
|
||||
self.parse_cpu_line(cpu_line)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn cpu_temperature(&mut self) -> anyhow::Result<f32> {
|
||||
let content = std::fs::read_to_string(CPU_TEMP_PATH)?;
|
||||
let temp_millicelsius = content.trim().parse::<f32>()?;
|
||||
|
||||
let temp_celsius = temp_millicelsius / 1000.0;
|
||||
|
||||
Ok(temp_celsius)
|
||||
}
|
||||
|
||||
fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result<CpuBreakdown> {
|
||||
let mut parts = line.split_whitespace();
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ pub struct CPU {
|
||||
pub usage: f32,
|
||||
pub threads: usize,
|
||||
pub breakdown: CpuBreakdown,
|
||||
pub temperature: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
|
||||
@@ -73,7 +73,7 @@ export function LineChart({ data, height = 200 }: LineChartProps) {
|
||||
},
|
||||
plugins: {
|
||||
legend: {
|
||||
display: data.datasets.length > 1,
|
||||
display: data.datasets.length > 0,
|
||||
position: "top" as const,
|
||||
labels: {
|
||||
color: "rgba(156, 163, 175, 0.8)",
|
||||
|
||||
@@ -14,6 +14,7 @@ export type ChartData = {
|
||||
|
||||
type ChartDataReturns = {
|
||||
cpuData: ChartData;
|
||||
cpuTempData: ChartData;
|
||||
memoryData: ChartData;
|
||||
networkData: ChartData;
|
||||
};
|
||||
@@ -29,9 +30,11 @@ export function useChartData(
|
||||
addDataPoint(data);
|
||||
}, [data]);
|
||||
|
||||
const labels = currentData.map((p) => formatTimestamp(p.timestamp));
|
||||
|
||||
return {
|
||||
cpuData: {
|
||||
labels: currentData.map((p) => formatTimestamp(p.timestamp)),
|
||||
labels,
|
||||
datasets: [
|
||||
{
|
||||
label: "Total CPU (%)",
|
||||
@@ -75,8 +78,20 @@ export function useChartData(
|
||||
},
|
||||
],
|
||||
},
|
||||
cpuTempData: {
|
||||
labels,
|
||||
datasets: [
|
||||
{
|
||||
label: "CPU Temp (°C)",
|
||||
data: currentData.map(
|
||||
({ metrics }) => metrics.cpu.temperature,
|
||||
),
|
||||
color: "#17abad",
|
||||
},
|
||||
],
|
||||
},
|
||||
memoryData: {
|
||||
labels: currentData.map((p) => formatTimestamp(p.timestamp)),
|
||||
labels,
|
||||
datasets: [
|
||||
{
|
||||
label: "Memory Usage (%)",
|
||||
@@ -101,7 +116,7 @@ export function useChartData(
|
||||
],
|
||||
},
|
||||
networkData: {
|
||||
labels: currentData.map((p) => formatTimestamp(p.timestamp)),
|
||||
labels,
|
||||
datasets: [
|
||||
{
|
||||
label: "Upload (bps)",
|
||||
|
||||
@@ -37,7 +37,7 @@ export function AgentPage() {
|
||||
}, [agent, period]);
|
||||
|
||||
const { metrics } = message ?? {};
|
||||
const { cpuData, memoryData, networkData } = useChartData(
|
||||
const { cpuData, cpuTempData, memoryData, networkData } = useChartData(
|
||||
period === "realtime" ? message! : history,
|
||||
);
|
||||
|
||||
@@ -177,6 +177,10 @@ export function AgentPage() {
|
||||
|
||||
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6 my-8">
|
||||
<LineChartCard title="CPU Usage" data={cpuData} />
|
||||
<LineChartCard
|
||||
title="CPU Temperature"
|
||||
data={cpuTempData}
|
||||
/>
|
||||
<LineChartCard
|
||||
title="Memory Usage"
|
||||
data={memoryData}
|
||||
|
||||
@@ -18,6 +18,7 @@ export interface CPUMetrics {
|
||||
usage: number;
|
||||
threads: number;
|
||||
breakdown: CPUBreakdown;
|
||||
temperature: number;
|
||||
}
|
||||
|
||||
export interface CPUBreakdown {
|
||||
|
||||
Reference in New Issue
Block a user