forked from PeachCloud/peach-workspace
91 lines
2.4 KiB
Rust
91 lines
2.4 KiB
Rust
use std::result::Result;
|
|
|
|
use miniserde::json;
|
|
use probes::{cpu, disk_usage, load, memory};
|
|
use systemstat::{Platform, System};
|
|
|
|
use crate::error::StatError;
|
|
use crate::structs::{CpuStat, CpuStatPercentages, DiskUsage, LoadAverage, MemStat};
|
|
|
|
pub fn cpu_stats() -> Result<String, StatError> {
|
|
let cpu_stats = cpu::proc::read().map_err(|source| StatError::CpuStat { source })?;
|
|
let s = cpu_stats.stat;
|
|
let cpu = CpuStat {
|
|
user: s.user,
|
|
system: s.system,
|
|
nice: s.nice,
|
|
idle: s.idle,
|
|
};
|
|
let json_cpu = json::to_string(&cpu);
|
|
|
|
Ok(json_cpu)
|
|
}
|
|
|
|
pub fn cpu_stats_percent() -> Result<String, StatError> {
|
|
let cpu_stats = cpu::proc::read().map_err(|source| StatError::CpuStat { source })?;
|
|
let s = cpu_stats.stat.in_percentages();
|
|
let cpu = CpuStatPercentages {
|
|
user: s.user,
|
|
system: s.system,
|
|
nice: s.nice,
|
|
idle: s.idle,
|
|
};
|
|
let json_cpu = json::to_string(&cpu);
|
|
|
|
Ok(json_cpu)
|
|
}
|
|
|
|
pub fn disk_usage() -> Result<String, StatError> {
|
|
let disks = disk_usage::read().map_err(|source| StatError::DiskUsage { source })?;
|
|
let mut disk_usages = Vec::new();
|
|
for d in disks {
|
|
let disk = DiskUsage {
|
|
filesystem: d.filesystem,
|
|
one_k_blocks: d.one_k_blocks,
|
|
one_k_blocks_used: d.one_k_blocks_used,
|
|
one_k_blocks_free: d.one_k_blocks_free,
|
|
used_percentage: d.used_percentage,
|
|
mountpoint: d.mountpoint,
|
|
};
|
|
disk_usages.push(disk);
|
|
}
|
|
let json_disks = json::to_string(&disk_usages);
|
|
|
|
Ok(json_disks)
|
|
}
|
|
|
|
pub fn load_average() -> Result<String, StatError> {
|
|
let l = load::read().map_err(|source| StatError::LoadAvg { source })?;
|
|
let load_avg = LoadAverage {
|
|
one: l.one,
|
|
five: l.five,
|
|
fifteen: l.fifteen,
|
|
};
|
|
let json_load_avg = json::to_string(&load_avg);
|
|
|
|
Ok(json_load_avg)
|
|
}
|
|
|
|
pub fn mem_stats() -> Result<String, StatError> {
|
|
let m = memory::read().map_err(|source| StatError::MemStat { source })?;
|
|
let mem = MemStat {
|
|
total: m.total(),
|
|
free: m.free(),
|
|
used: m.used(),
|
|
};
|
|
let json_mem = json::to_string(&mem);
|
|
|
|
Ok(json_mem)
|
|
}
|
|
|
|
pub fn uptime() -> Result<String, StatError> {
|
|
let sys = System::new();
|
|
let uptime = sys
|
|
.uptime()
|
|
.map_err(|source| StatError::Uptime { source })?;
|
|
let uptime_secs = uptime.as_secs();
|
|
let json_uptime = json::to_string(&uptime_secs);
|
|
|
|
Ok(json_uptime)
|
|
}
|