Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25e8386
commit b1cf744
Showing
17 changed files
with
389 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
extern crate sysinfo; | ||
|
||
use std::collections::HashMap; | ||
use self::sysinfo::{System, SystemExt, Processor, ProcessorExt}; | ||
|
||
use rtop::datastreams::datastream::DataStream; | ||
|
||
|
||
pub struct CPUMonitor { | ||
pub cpu_usage: f32, | ||
pub cpu_core_info: Vec<(String, f32)>, //Name, Usage | ||
pub cpu_usage_history: HashMap<String, Vec<f32>>, //Name, Usage | ||
pub cpu_temp: Option<f32>, | ||
system_info: System, | ||
max_history_len: usize, | ||
} | ||
|
||
impl DataStream for CPUMonitor { | ||
fn new(max_hist_len: usize) -> Self { | ||
Self { | ||
cpu_usage: 0.0, | ||
cpu_core_info: Vec::new(), | ||
cpu_usage_history: HashMap::new(), | ||
cpu_temp: None, | ||
system_info: System::new(), | ||
max_history_len: max_hist_len, | ||
} | ||
} | ||
|
||
fn poll(&mut self, system_info: &System) { | ||
let cpus = system_info.get_processor_list(); | ||
|
||
self.cpu_core_info.clear(); | ||
for cpu in &cpus[1..cpus.len()] { | ||
let info = CPUMonitor::parse_cpu_info(cpu); | ||
self.cpu_core_info.push(info); | ||
match self.cpu_core_info.last() { | ||
Some(entry) => { | ||
let history = self.cpu_usage_history.entry(entry.0.clone()).or_insert(vec![0.0; self.max_history_len]); | ||
while history.len() >= self.max_history_len { | ||
history.remove(0); | ||
} | ||
history.push(entry.1); | ||
}, | ||
None => {}, | ||
}; | ||
} | ||
self.cpu_usage = cpus[0].get_cpu_usage(); | ||
} | ||
} | ||
|
||
impl CPUMonitor { | ||
fn parse_cpu_info(cpu: &Processor) -> (String, f32) { | ||
(String::from(cpu.get_name()), cpu.get_cpu_usage()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
extern crate sysinfo; | ||
|
||
use self::sysinfo::System; | ||
|
||
pub trait DataStream { | ||
fn new(max_hist_len: usize) -> Self; | ||
fn poll(&mut self); | ||
fn poll(&mut self, system_info: &System); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
extern crate sysinfo; | ||
|
||
use std::str; | ||
use std::collections::HashMap; | ||
use self::sysinfo::{Pid, Disk, Processor, Process, System, ProcessExt, | ||
SystemExt, DiskExt, ProcessorExt, NetworkExt, AsU32}; | ||
|
||
use rtop::datastreams::datastream::DataStream; | ||
|
||
pub struct DiskMonitor { | ||
pub disk_usage: Vec<(String, String, u64, u64)>, //Mount, type, used, total | ||
max_history_len: usize, | ||
} | ||
|
||
impl DataStream for DiskMonitor { | ||
fn new(max_hist_len: usize) -> Self { | ||
Self { | ||
disk_usage: Vec::new(), | ||
max_history_len: max_hist_len, | ||
} | ||
} | ||
|
||
fn poll(&mut self, system_info: &System) { | ||
let disks = system_info.get_disks(); | ||
|
||
self.disk_usage.clear(); | ||
for disk in disks { | ||
self.disk_usage.push(DiskMonitor::parse_disk_info(disk)); | ||
} | ||
} | ||
} | ||
|
||
impl DiskMonitor { | ||
fn parse_disk_info(disk: &Disk) -> (String, String, u64, u64) { | ||
let name = disk.get_mount_point().to_str().expect("Optional Disk name returned None"); | ||
let fs = str::from_utf8(disk.get_file_system()).unwrap(); | ||
(String::from(name), String::from(fs), disk.get_total_space() - disk.get_available_space(), disk.get_total_space()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
extern crate sysinfo; | ||
|
||
use self::sysinfo::{System, SystemExt}; | ||
|
||
use rtop::datastreams::datastream::DataStream; | ||
|
||
pub struct MemoryMonitor { | ||
pub memory_usage: u64, | ||
pub memory_usage_history: Vec<f64>, //Name, Usage | ||
pub total_memory: u64, | ||
pub swap_usage: u64, | ||
pub swap_usage_history: Vec<f64>, | ||
pub total_swap: u64, | ||
max_history_len: usize, | ||
} | ||
|
||
impl DataStream for MemoryMonitor { | ||
fn new(max_hist_len: usize) -> Self { | ||
Self { | ||
memory_usage: 0, | ||
total_memory: 10, | ||
memory_usage_history: vec![0.0; max_hist_len], | ||
swap_usage: 0, | ||
total_swap: 10, | ||
swap_usage_history: vec![0.0; max_hist_len], | ||
max_history_len: max_hist_len, | ||
} | ||
} | ||
|
||
fn poll(&mut self, system_info: &System) { | ||
self.memory_usage = system_info.get_used_memory(); | ||
self.total_memory = system_info.get_total_memory(); | ||
self.swap_usage = system_info.get_used_swap(); | ||
self.total_swap = system_info.get_total_swap(); | ||
if self.total_swap == 0 { | ||
self.total_swap = 10; | ||
} | ||
|
||
while self.memory_usage_history.len() >= self.max_history_len { | ||
self.memory_usage_history.remove(0); | ||
} | ||
self.memory_usage_history.push(self.memory_usage as f64 / self.total_memory as f64); | ||
|
||
while self.swap_usage_history.len() >= self.max_history_len { | ||
self.swap_usage_history.remove(0); | ||
} | ||
self.swap_usage_history.push(self.swap_usage as f64 / self.total_swap as f64); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
pub mod datastream; | ||
mod datastream; | ||
pub mod servers; | ||
pub mod systemmonitor; | ||
mod diskmonitor; | ||
mod cpumonitor; | ||
mod networkmonitor; | ||
mod memorymonitor; | ||
mod processmonitor; | ||
|
||
pub use self::datastream::DataStream as DataStream; | ||
pub use self::diskmonitor::DiskMonitor as DiskMonitor; | ||
pub use self::cpumonitor::CPUMonitor as CPUMonitor; | ||
pub use self::networkmonitor::NetworkMonitor as NetworkMonitor; | ||
pub use self::memorymonitor::MemoryMonitor as MemoryMonitor; | ||
pub use self::processmonitor::ProcessMonitor as ProcessMonitor; |
Oops, something went wrong.