From 114b0ee55d3ad45cd95ece647fe7705545faf1b6 Mon Sep 17 00:00:00 2001 From: csehviktor Date: Tue, 22 Jul 2025 19:55:59 +0200 Subject: [PATCH] implement cpu temperature --- agent/src/collector.rs | 5 +++-- agent/src/cpu.rs | 19 +++++++++++++++++-- common/src/metrics.rs | 1 + ui/src/components/LineChart.tsx | 2 +- ui/src/hooks/useChartData.tsx | 21 ++++++++++++++++++--- ui/src/pages/agent.tsx | 6 +++++- ui/src/services/types.ts | 1 + 7 files changed, 46 insertions(+), 9 deletions(-) diff --git a/agent/src/collector.rs b/agent/src/collector.rs index 974388c..2d9b045 100644 --- a/agent/src/collector.rs +++ b/agent/src/collector.rs @@ -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(), } } diff --git a/agent/src/cpu.rs b/agent/src/cpu.rs index dc1c35b..9eb1a22 100644 --- a/agent/src/cpu.rs +++ b/agent/src/cpu.rs @@ -31,10 +31,14 @@ impl CpuValues { } } +#[cfg_attr(not(target_os = "linux"), derive(Default))] pub struct CpuStatReader { previous_stats: HashMap, } +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 { - let content = std::fs::read_to_string("/proc/stat").unwrap(); + #[cfg(target_os = "linux")] + pub fn cpu_breakdown(&mut self) -> anyhow::Result { + 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 { + let content = std::fs::read_to_string(CPU_TEMP_PATH)?; + let temp_millicelsius = content.trim().parse::()?; + + let temp_celsius = temp_millicelsius / 1000.0; + + Ok(temp_celsius) + } + fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result { let mut parts = line.split_whitespace(); diff --git a/common/src/metrics.rs b/common/src/metrics.rs index b1107d2..4327bb7 100644 --- a/common/src/metrics.rs +++ b/common/src/metrics.rs @@ -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)] diff --git a/ui/src/components/LineChart.tsx b/ui/src/components/LineChart.tsx index c41c5a8..4144b0d 100644 --- a/ui/src/components/LineChart.tsx +++ b/ui/src/components/LineChart.tsx @@ -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)", diff --git a/ui/src/hooks/useChartData.tsx b/ui/src/hooks/useChartData.tsx index 82e6193..38d42f9 100644 --- a/ui/src/hooks/useChartData.tsx +++ b/ui/src/hooks/useChartData.tsx @@ -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)", diff --git a/ui/src/pages/agent.tsx b/ui/src/pages/agent.tsx index c2b111c..39ced9c 100644 --- a/ui/src/pages/agent.tsx +++ b/ui/src/pages/agent.tsx @@ -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() {
+