mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
fix naming consistency
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
use common::metrics::{Disk, Memory, Metrics, Network, SystemInfo, CPU};
|
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;
|
||||||
|
|
||||||
pub struct Collector {
|
pub struct Collector {
|
||||||
sys: System,
|
sys: System,
|
||||||
disks: Disks,
|
disks: Disks,
|
||||||
networks: Networks,
|
networks: Networks,
|
||||||
cpu_reader: CpuStatReader,
|
cpu_reader: CPUStatReader,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Collector {
|
impl Collector {
|
||||||
@@ -16,7 +16,7 @@ impl Collector {
|
|||||||
sys: System::new_all(),
|
sys: System::new_all(),
|
||||||
disks: Disks::new_with_refreshed_list(),
|
disks: Disks::new_with_refreshed_list(),
|
||||||
networks: Networks::new_with_refreshed_list(),
|
networks: Networks::new_with_refreshed_list(),
|
||||||
cpu_reader: CpuStatReader::new(),
|
cpu_reader: CPUStatReader::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use common::metrics::CpuBreakdown;
|
use common::metrics::CPUBreakdown;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
struct CpuValues {
|
struct CPUValues {
|
||||||
pub user: u64,
|
pub user: u64,
|
||||||
pub nice: u64,
|
pub nice: u64,
|
||||||
pub system: u64,
|
pub system: u64,
|
||||||
@@ -15,23 +15,23 @@ struct CpuValues {
|
|||||||
pub guest_nice: u64,
|
pub guest_nice: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CpuValues {
|
impl CPUValues {
|
||||||
pub fn total(&self) -> u64 {
|
pub fn total(&self) -> u64 {
|
||||||
self.user + self.nice + self.system + self.idle + self.iowait +
|
self.user + self.nice + self.system + self.idle + self.iowait +
|
||||||
self.irq + self.softirq + self.steal + self.guest + self.guest_nice
|
self.irq + self.softirq + self.steal + self.guest + self.guest_nice
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CpuStatReader {
|
pub struct CPUStatReader {
|
||||||
previous_stats: HashMap<String, CpuValues>,
|
previous_stats: HashMap<String, CPUValues>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CpuStatReader {
|
impl CPUStatReader {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { previous_stats: HashMap::new() }
|
Self { previous_stats: HashMap::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_global_cpu_stats(&mut self) -> anyhow::Result<CpuBreakdown> {
|
pub fn read_global_cpu_stats(&mut self) -> anyhow::Result<CPUBreakdown> {
|
||||||
let content = std::fs::read_to_string("/proc/stat").unwrap();
|
let content = std::fs::read_to_string("/proc/stat").unwrap();
|
||||||
|
|
||||||
let cpu_line = content
|
let cpu_line = content
|
||||||
@@ -42,7 +42,7 @@ impl CpuStatReader {
|
|||||||
self.parse_cpu_line(cpu_line)
|
self.parse_cpu_line(cpu_line)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result<CpuBreakdown> {
|
fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result<CPUBreakdown> {
|
||||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||||
|
|
||||||
let cpu_name = parts[0].to_string();
|
let cpu_name = parts[0].to_string();
|
||||||
@@ -51,7 +51,7 @@ impl CpuStatReader {
|
|||||||
parts[num].parse().ok().unwrap()
|
parts[num].parse().ok().unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
let values = CpuValues {
|
let values = CPUValues {
|
||||||
user: parse(1),
|
user: parse(1),
|
||||||
nice: parse(2),
|
nice: parse(2),
|
||||||
system: parse(3),
|
system: parse(3),
|
||||||
@@ -72,21 +72,21 @@ impl CpuStatReader {
|
|||||||
Ok(percentages)
|
Ok(percentages)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_percentages(&self, current: &CpuValues, previous: Option<CpuValues>) -> CpuBreakdown {
|
fn calculate_percentages(&self, current: &CPUValues, previous: Option<CPUValues>) -> CPUBreakdown {
|
||||||
let Some(prev) = previous else {
|
let Some(prev) = previous else {
|
||||||
return CpuBreakdown::default();
|
return CPUBreakdown::default();
|
||||||
};
|
};
|
||||||
|
|
||||||
let total_delta = current.total().saturating_sub(prev.total()) as f32;
|
let total_delta = current.total().saturating_sub(prev.total()) as f32;
|
||||||
if total_delta <= 0.0 {
|
if total_delta <= 0.0 {
|
||||||
return CpuBreakdown::default();
|
return CPUBreakdown::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
let calculate_pct = |current: u64, prev: u64| {
|
let calculate_pct = |current: u64, prev: u64| {
|
||||||
(current.saturating_sub(prev) as f32 / total_delta) * 100.0
|
(current.saturating_sub(prev) as f32 / total_delta) * 100.0
|
||||||
};
|
};
|
||||||
|
|
||||||
CpuBreakdown {
|
CPUBreakdown {
|
||||||
system: calculate_pct(current.system, prev.system),
|
system: calculate_pct(current.system, prev.system),
|
||||||
user: calculate_pct(current.user, prev.user),
|
user: calculate_pct(current.user, prev.user),
|
||||||
idle: calculate_pct(current.idle, prev.idle),
|
idle: calculate_pct(current.idle, prev.idle),
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ pub struct SystemInfo {
|
|||||||
pub struct CPU {
|
pub struct CPU {
|
||||||
pub usage: f32,
|
pub usage: f32,
|
||||||
pub threads: usize,
|
pub threads: usize,
|
||||||
pub breakdown: CpuBreakdown,
|
pub breakdown: CPUBreakdown,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||||
pub struct CpuBreakdown {
|
pub struct CPUBreakdown {
|
||||||
pub system: f32,
|
pub system: f32,
|
||||||
pub user: f32,
|
pub user: f32,
|
||||||
pub idle: f32,
|
pub idle: f32,
|
||||||
|
|||||||
Reference in New Issue
Block a user