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 sysinfo::{Disks, Networks, System};
|
||||||
|
|
||||||
use crate::cpu::CpuStatReader;
|
use crate::cpu::CpuStatReader;
|
||||||
@@ -54,7 +54,8 @@ impl Collector {
|
|||||||
CPU {
|
CPU {
|
||||||
usage: self.sys.global_cpu_usage(),
|
usage: self.sys.global_cpu_usage(),
|
||||||
threads: self.sys.cpus().len(),
|
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 {
|
pub struct CpuStatReader {
|
||||||
previous_stats: HashMap<String, CpuValues>,
|
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 {
|
impl CpuStatReader {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -42,8 +46,9 @@ impl CpuStatReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_global_cpu_stats(&mut self) -> anyhow::Result<CpuBreakdown> {
|
#[cfg(target_os = "linux")]
|
||||||
let content = std::fs::read_to_string("/proc/stat").unwrap();
|
pub fn cpu_breakdown(&mut self) -> anyhow::Result<CpuBreakdown> {
|
||||||
|
let content = std::fs::read_to_string(CPU_LINE_PATH)?;
|
||||||
|
|
||||||
let cpu_line = content
|
let cpu_line = content
|
||||||
.lines()
|
.lines()
|
||||||
@@ -53,6 +58,16 @@ impl CpuStatReader {
|
|||||||
self.parse_cpu_line(cpu_line)
|
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> {
|
fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result<CpuBreakdown> {
|
||||||
let mut parts = line.split_whitespace();
|
let mut parts = line.split_whitespace();
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ pub struct CPU {
|
|||||||
pub usage: f32,
|
pub usage: f32,
|
||||||
pub threads: usize,
|
pub threads: usize,
|
||||||
pub breakdown: CpuBreakdown,
|
pub breakdown: CpuBreakdown,
|
||||||
|
pub temperature: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ export function LineChart({ data, height = 200 }: LineChartProps) {
|
|||||||
},
|
},
|
||||||
plugins: {
|
plugins: {
|
||||||
legend: {
|
legend: {
|
||||||
display: data.datasets.length > 1,
|
display: data.datasets.length > 0,
|
||||||
position: "top" as const,
|
position: "top" as const,
|
||||||
labels: {
|
labels: {
|
||||||
color: "rgba(156, 163, 175, 0.8)",
|
color: "rgba(156, 163, 175, 0.8)",
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export type ChartData = {
|
|||||||
|
|
||||||
type ChartDataReturns = {
|
type ChartDataReturns = {
|
||||||
cpuData: ChartData;
|
cpuData: ChartData;
|
||||||
|
cpuTempData: ChartData;
|
||||||
memoryData: ChartData;
|
memoryData: ChartData;
|
||||||
networkData: ChartData;
|
networkData: ChartData;
|
||||||
};
|
};
|
||||||
@@ -29,9 +30,11 @@ export function useChartData(
|
|||||||
addDataPoint(data);
|
addDataPoint(data);
|
||||||
}, [data]);
|
}, [data]);
|
||||||
|
|
||||||
|
const labels = currentData.map((p) => formatTimestamp(p.timestamp));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cpuData: {
|
cpuData: {
|
||||||
labels: currentData.map((p) => formatTimestamp(p.timestamp)),
|
labels,
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
label: "Total CPU (%)",
|
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: {
|
memoryData: {
|
||||||
labels: currentData.map((p) => formatTimestamp(p.timestamp)),
|
labels,
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
label: "Memory Usage (%)",
|
label: "Memory Usage (%)",
|
||||||
@@ -101,7 +116,7 @@ export function useChartData(
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
networkData: {
|
networkData: {
|
||||||
labels: currentData.map((p) => formatTimestamp(p.timestamp)),
|
labels,
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
label: "Upload (bps)",
|
label: "Upload (bps)",
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export function AgentPage() {
|
|||||||
}, [agent, period]);
|
}, [agent, period]);
|
||||||
|
|
||||||
const { metrics } = message ?? {};
|
const { metrics } = message ?? {};
|
||||||
const { cpuData, memoryData, networkData } = useChartData(
|
const { cpuData, cpuTempData, memoryData, networkData } = useChartData(
|
||||||
period === "realtime" ? message! : history,
|
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">
|
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6 my-8">
|
||||||
<LineChartCard title="CPU Usage" data={cpuData} />
|
<LineChartCard title="CPU Usage" data={cpuData} />
|
||||||
|
<LineChartCard
|
||||||
|
title="CPU Temperature"
|
||||||
|
data={cpuTempData}
|
||||||
|
/>
|
||||||
<LineChartCard
|
<LineChartCard
|
||||||
title="Memory Usage"
|
title="Memory Usage"
|
||||||
data={memoryData}
|
data={memoryData}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ export interface CPUMetrics {
|
|||||||
usage: number;
|
usage: number;
|
||||||
threads: number;
|
threads: number;
|
||||||
breakdown: CPUBreakdown;
|
breakdown: CPUBreakdown;
|
||||||
|
temperature: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CPUBreakdown {
|
export interface CPUBreakdown {
|
||||||
|
|||||||
Reference in New Issue
Block a user