mirror of
https://codeberg.org/icewind/palantir.git
synced 2026-06-03 10:14:09 +02:00
memory source
This commit is contained in:
parent
aea5a65371
commit
822e0feee5
2 changed files with 39 additions and 21 deletions
|
|
@ -50,6 +50,7 @@ pub struct Sensors {
|
|||
cpu: Mutex<CpuTimeSource>,
|
||||
temp: Mutex<TemperatureSource>,
|
||||
net: Mutex<NetworkSource>,
|
||||
mem: Mutex<MemorySource>,
|
||||
}
|
||||
|
||||
impl Sensors {
|
||||
|
|
@ -59,6 +60,7 @@ impl Sensors {
|
|||
cpu: Mutex::new(CpuTimeSource::new()?),
|
||||
temp: Mutex::new(TemperatureSource::new()?),
|
||||
net: Mutex::new(NetworkSource::new()?),
|
||||
mem: Mutex::new(MemorySource::new()?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -68,7 +70,7 @@ pub fn get_metrics(sensors: &Sensors) -> Result<String> {
|
|||
let disk_usage = disk_usage()?;
|
||||
let disks = disk_stats()?;
|
||||
let cpu = sensors.cpu.lock().unwrap().read()?;
|
||||
let memory = memory()?;
|
||||
let memory = sensors.mem.lock().unwrap().read()?;
|
||||
let temperatures = sensors.temp.lock().unwrap().read()?;
|
||||
let mut net = sensors.net.lock().unwrap();
|
||||
let networks = net.read()?;
|
||||
|
|
|
|||
|
|
@ -125,17 +125,31 @@ impl SensorSource for TemperatureSource {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn memory() -> Result<Memory> {
|
||||
let mut meminfo = BufReader::new(File::open("/proc/meminfo")?);
|
||||
let mut mem = Memory::default();
|
||||
let mut line = String::new();
|
||||
loop {
|
||||
line.clear();
|
||||
meminfo.read_line(&mut line)?;
|
||||
if line.is_empty() {
|
||||
break;
|
||||
pub struct MemorySource {
|
||||
source: File,
|
||||
buff: String,
|
||||
}
|
||||
if let Some(line) = line.strip_suffix(" kB\n") {
|
||||
|
||||
impl MemorySource {
|
||||
pub fn new() -> Result<MemorySource> {
|
||||
Ok(MemorySource {
|
||||
source: File::open("/proc/meminfo")?,
|
||||
buff: String::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl SensorSource for MemorySource {
|
||||
type Data = Memory;
|
||||
|
||||
fn read(&mut self) -> Result<Self::Data> {
|
||||
self.buff.clear();
|
||||
self.source.rewind()?;
|
||||
self.source.read_to_string(&mut self.buff)?;
|
||||
|
||||
let mut mem = Memory::default();
|
||||
for line in self.buff.lines() {
|
||||
if let Some(line) = line.strip_suffix(" kB") {
|
||||
if let Some(line_total) = line.strip_prefix("MemTotal: ") {
|
||||
mem.total = line_total.trim().parse::<u64>()? * 1000;
|
||||
}
|
||||
|
|
@ -147,8 +161,10 @@ pub fn memory() -> Result<Memory> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(mem)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CpuTime(f32);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue