mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
formatter + implement status history
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use common::metrics::{Disk, Memory, Metrics, Network, SystemInfo, CPU};
|
||||
use common::metrics::{CPU, Disk, Memory, Metrics, Network, SystemInfo};
|
||||
use sysinfo::{Disks, Networks, System};
|
||||
|
||||
use crate::cpu::CPUStatReader;
|
||||
@@ -54,12 +54,16 @@ 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.read_global_cpu_stats().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_disk(&self) -> Disk {
|
||||
let disk = self.disks.iter().max_by_key(|disk| disk.total_space()).unwrap();
|
||||
let disk = self
|
||||
.disks
|
||||
.iter()
|
||||
.max_by_key(|disk| disk.total_space())
|
||||
.unwrap();
|
||||
|
||||
Disk {
|
||||
free: disk.available_space(),
|
||||
@@ -77,17 +81,14 @@ impl Collector {
|
||||
}
|
||||
|
||||
fn collect_network(&self) -> Network {
|
||||
let (down, up): (u64, u64) = self.networks
|
||||
let (down, up): (u64, u64) = self
|
||||
.networks
|
||||
.values()
|
||||
.map(|data| (data.received(), data.transmitted()))
|
||||
.fold((0, 0), |(acc_down, acc_up), (down, up)| {
|
||||
(acc_down + down, acc_up + up)
|
||||
});
|
||||
|
||||
Network {
|
||||
down,
|
||||
up,
|
||||
}
|
||||
Network { down, up }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
use anyhow::anyhow;
|
||||
use common::metrics::CPUBreakdown;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy)]
|
||||
struct CPUValues {
|
||||
@@ -18,8 +18,16 @@ struct CPUValues {
|
||||
|
||||
impl CPUValues {
|
||||
pub fn total(&self) -> u64 {
|
||||
self.user + self.nice + self.system + self.idle + self.iowait +
|
||||
self.irq + self.softirq + self.steal + self.guest + self.guest_nice
|
||||
self.user
|
||||
+ self.nice
|
||||
+ self.system
|
||||
+ self.idle
|
||||
+ self.iowait
|
||||
+ self.irq
|
||||
+ self.softirq
|
||||
+ self.steal
|
||||
+ self.guest
|
||||
+ self.guest_nice
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +37,9 @@ pub struct CPUStatReader {
|
||||
|
||||
impl CPUStatReader {
|
||||
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> {
|
||||
@@ -46,7 +56,8 @@ impl CPUStatReader {
|
||||
fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result<CPUBreakdown> {
|
||||
let mut parts = line.split_whitespace();
|
||||
|
||||
let cpu_name = parts.next()
|
||||
let cpu_name = parts
|
||||
.next()
|
||||
.ok_or_else(|| anyhow!("missing cpu name"))?
|
||||
.to_string();
|
||||
|
||||
@@ -80,7 +91,11 @@ impl CPUStatReader {
|
||||
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 {
|
||||
return CPUBreakdown::default();
|
||||
};
|
||||
@@ -90,9 +105,8 @@ impl CPUStatReader {
|
||||
return CPUBreakdown::default();
|
||||
}
|
||||
|
||||
let calculate_pct = |current: u64, prev: u64| {
|
||||
(current.saturating_sub(prev) as f32 / total_delta) * 100.0
|
||||
};
|
||||
let calculate_pct =
|
||||
|current: u64, prev: u64| (current.saturating_sub(prev) as f32 / total_delta) * 100.0;
|
||||
|
||||
CPUBreakdown {
|
||||
system: calculate_pct(current.system, prev.system),
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use common::MQTT_SEND_INTERVAL;
|
||||
use collector::Collector;
|
||||
use std::time::Duration;
|
||||
use mqtt::MqttHandle;
|
||||
use clap::Parser;
|
||||
use collector::Collector;
|
||||
use common::MQTT_SEND_INTERVAL;
|
||||
use mqtt::MqttHandle;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
pub mod collector;
|
||||
pub mod mqtt;
|
||||
pub mod cpu;
|
||||
pub mod mqtt;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[arg(long, short, default_value = "unknown-agent")]
|
||||
#[arg(long, short, default_value = "agent")]
|
||||
name: String,
|
||||
|
||||
#[arg(long, default_value = "0.0.0.0")]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use common::{metrics::Metrics, StatusMessage, MQTT_TOPIC};
|
||||
use common::{MQTT_TOPIC, StatusMessage, metrics::Metrics};
|
||||
use rumqttc::{AsyncClient, MqttOptions, QoS};
|
||||
use tokio::task::JoinHandle;
|
||||
use std::time::Duration;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
pub struct MqttHandle {
|
||||
pub agent: String,
|
||||
@@ -28,7 +28,7 @@ impl MqttHandle {
|
||||
Self {
|
||||
agent,
|
||||
client,
|
||||
eventloop_handle
|
||||
eventloop_handle,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user