Skip to content

Commit

Permalink
adding disks panel
Browse files Browse the repository at this point in the history
  • Loading branch information
narendasan committed Apr 2, 2018
1 parent 25e8386 commit b1cf744
Show file tree
Hide file tree
Showing 17 changed files with 389 additions and 230 deletions.
13 changes: 6 additions & 7 deletions src/main.rs
Expand Up @@ -25,15 +25,14 @@ use rtop::event::Event;
use rtop::ui::renderer::render::render;

fn main() {
stderrlog::new()
.module(module_path!())
.verbosity(4)
.init()
.unwrap();
stderrlog::new().module(module_path!())
.verbosity(4)
.init()
.unwrap();

info!("Start");
//Program
let mut app = App::new(10000);
let mut app = App::new(150);
let (tx, rx) = mpsc::channel();
let input_tx = tx.clone();

Expand Down Expand Up @@ -68,7 +67,6 @@ fn main() {
terminal.resize(size).unwrap();
term_size = size;
}
render(&mut terminal, &app, &term_size).unwrap();
let evt = rx.recv().unwrap();
{
match evt {
Expand All @@ -89,6 +87,7 @@ fn main() {
}
}
}
render(&mut terminal, &app, &term_size).unwrap();
}
terminal.show_cursor().unwrap();
terminal.clear().unwrap();
Expand Down
72 changes: 43 additions & 29 deletions src/rtop/app.rs
@@ -1,13 +1,16 @@
extern crate sysinfo;

use std::collections::HashMap;

use tui::style::Color;
use termion::event::Key;
use self::sysinfo::{System, SystemExt};

use rtop::cmd::Cmd;
use rtop::ui::tabs::Tabs;
use rtop::datastreams::datastream::DataStream;
use rtop::datastreams::{DataStream, DiskMonitor, MemoryMonitor,
CPUMonitor, NetworkMonitor, ProcessMonitor};
use rtop::datastreams::servers::Servers;
use rtop::datastreams::systemmonitor::SystemMonitor;

pub struct App<'a> {
pub items: Vec<&'a str>,
Expand All @@ -28,7 +31,12 @@ pub struct App<'a> {
pub swap_usage_str: String,
pub net_in_str: String,
pub net_out_str: String,
pub sys_info: SystemMonitor,
pub disk_info: DiskMonitor,
pub cpu_info: CPUMonitor,
pub net_info: NetworkMonitor,
pub mem_info: MemoryMonitor,
pub process_info: ProcessMonitor,
pub sys_monitor: System,
}

impl <'a> App<'a> {
Expand Down Expand Up @@ -67,7 +75,12 @@ impl <'a> App<'a> {
swap_usage_str: String::new(),
net_in_str: String::new(),
net_out_str: String::new(),
sys_info: DataStream::new(history_len),
disk_info: DataStream::new(history_len),
cpu_info: DataStream::new(history_len),
net_info: DataStream::new(history_len),
mem_info: DataStream::new(history_len),
process_info: DataStream::new(history_len),
sys_monitor: System::new(),
}
}
pub fn input_handler(&mut self, input: Key) -> Option<Cmd>{
Expand All @@ -80,7 +93,7 @@ impl <'a> App<'a> {
self.selected_proc -= 1
};
}
Key::Down => if self.selected_proc < self.sys_info.process_info.len() - 1 {
Key::Down => if self.selected_proc < self.process_info.process_info.len() - 1 {
self.selected_proc += 1;
},
Key::Left => {
Expand Down Expand Up @@ -110,9 +123,15 @@ impl <'a> App<'a> {
if self.color_index >= self.colors.len() {
self.color_index = 0;
}
self.sys_monitor.refresh_all();
self.disk_info.poll(&self.sys_monitor);
self.cpu_info.poll(&self.sys_monitor);
self.net_info.poll(&self.sys_monitor);
self.mem_info.poll(&self.sys_monitor);
self.process_info.poll(&self.sys_monitor);
//CPU History Parsing
{
for (name, usage) in &self.sys_info.cpu_usage_history {
for (name, usage) in &self.cpu_info.cpu_usage_history {
let pairwise_data = usage.iter()
.enumerate()
.map(|x| (x.0 as f64, x.1.clone() as f64))
Expand All @@ -135,56 +154,51 @@ impl <'a> App<'a> {
}
let core_label = core_num.to_string();
core_name = format!("Core: {} ({:.2}%)", core_label,
(self.sys_info.cpu_core_info[(core_num) as usize].1 * 100.0).to_string());
(self.cpu_info.cpu_core_info[(core_num) as usize].1 * 100.0).to_string());
self.cpu_panel_memory.insert(core_num, (core_name, pairwise_data));
}
}
//println!("{:?},{:?}", 100.0 * self.sys_info.memory_usage as f64 / self.sys_info.total_memory as f64, 100.0 * self.sys_info.swap_usage as f64 / self.sys_info.total_swap as f64);

//Memory History Parsing
{
self.mem_panel_memory = self.sys_info.memory_usage_history.iter()
self.mem_panel_memory = self.mem_info.memory_usage_history.iter()
.enumerate()
.map(|(i, u)| (i as f64, u.clone()))
.collect::<Vec<(f64, f64)>>();
self.mem_usage_str = format!("Memory ({:.2}%)", 100.0 * self.sys_info.memory_usage as f64 / self.sys_info.total_memory as f64);
println!("{:?}", self.mem_usage_str);
self.mem_usage_str = format!("Memory ({:.2}%)", 100.0 * self.mem_info.memory_usage as f64 / self.mem_info.total_memory as f64);
}
//Swap History Parsing
{
self.swap_panel_memory = self.sys_info.swap_usage_history.iter()
self.swap_panel_memory = self.mem_info.swap_usage_history.iter()
.enumerate()
.map(|(i, u)| (i as f64, u.clone()))
.collect::<Vec<(f64, f64)>>();
self.swap_usage_str = format!("Swap ({:.2}%)", 100.0 * self.sys_info.swap_usage as f64 / self.sys_info.total_swap as f64);
self.swap_usage_str = format!("Swap ({:.2}%)", 100.0 * self.mem_info.swap_usage as f64 / self.mem_info.total_swap as f64);
}
//Network Parsing
{

let (scalar, unit) = App::si_prefix(self.sys_info.net_in);
self.net_in_str = format!("Current Incoming Traffic: {} {}/s", self.sys_info.net_in / scalar, unit);
let (scalar, unit) = App::si_prefix(self.net_info.net_in);
self.net_in_str = format!("Current Incoming Traffic: {} {}/s", (self.net_info.net_in) / scalar, unit);


let (scalar, unit) = App::si_prefix(self.sys_info.net_out);
self.net_out_str = format!("Current Outgoing Network Traffic: {} {}/s", self.sys_info.net_out / scalar, unit);
let (scalar, unit) = App::si_prefix(self.net_info.net_out);
self.net_out_str = format!("Current Outgoing Network Traffic: {} {}/s", (self.net_info.net_out) / scalar, unit);
}

self.sys_info.poll();
}

fn si_prefix(bytes: u64) -> (u64, String) {
let b = bytes as f64;
fn si_prefix(bits: u64) -> (u64, String) {
let b = bits as f64;
if b == 0.0 {
return (1 as u64, String::from("B"));
}
match b.log(10.0) as u64 {
0 | 1 | 2 => ((10 as u64).pow(0), String::from("B")),
3 | 4 | 5 => ((10 as u64).pow(3), String::from("KB")),
6 | 7 | 8 => ((10 as u64).pow(6), String::from("MB")),
9 | 10 | 11 => ((10 as u64).pow(9), String::from("GB")),
12 | 13 | 14 => ((10 as u64).pow(12), String::from("TB")),
15 | 16 | 17 => ((10 as u64).pow(15), String::from("PB")),
_ => ((10 as u64).pow(18), String::from("EB")),
0 | 1 | 2 => ((10 as u64).pow(0), String::from("b")),
3 | 4 | 5 => ((10 as u64).pow(3), String::from("Kb")),
6 | 7 | 8 => ((10 as u64).pow(6), String::from("Mb")),
9 | 10 | 11 => ((10 as u64).pow(9), String::from("Gb")),
12 | 13 | 14 => ((10 as u64).pow(12), String::from("Tb")),
15 | 16 | 17 => ((10 as u64).pow(15), String::from("Pb")),
_ => ((10 as u64).pow(18), String::from("Eb")),
}
}
}
56 changes: 56 additions & 0 deletions src/rtop/datastreams/cpumonitor.rs
@@ -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())
}
}
6 changes: 5 additions & 1 deletion src/rtop/datastreams/datastream.rs
@@ -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);
}
39 changes: 39 additions & 0 deletions src/rtop/datastreams/diskmonitor.rs
@@ -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())
}
}
49 changes: 49 additions & 0 deletions src/rtop/datastreams/memorymonitor.rs
@@ -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);
}
}
15 changes: 13 additions & 2 deletions src/rtop/datastreams/mod.rs
@@ -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;

0 comments on commit b1cf744

Please sign in to comment.