Compare commits
2 Commits
v0.1.0
..
347ad5c744
| Author | SHA1 | Date | |
|---|---|---|---|
| 347ad5c744 | |||
| df01cf614d |
@@ -1,3 +1,4 @@
|
|||||||
|
address,name
|
||||||
10.3.100.1,Belleville Router
|
10.3.100.1,Belleville Router
|
||||||
10.11.31.3,Belleville VPN 11-31
|
10.11.31.3,Belleville VPN 11-31
|
||||||
10.12.32.1,Belleville VPN 12-32
|
10.12.32.1,Belleville VPN 12-32
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
address,name
|
||||||
10.3.100.1,Belleville Router
|
10.3.100.1,Belleville Router
|
||||||
10.11.31.3,Belleville VPN 11-31
|
10.11.31.3,Belleville VPN 11-31
|
||||||
10.12.32.1,Belleville VPN 12-32
|
10.12.32.1,Belleville VPN 12-32
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
address,name
|
||||||
10.3.100.1,Belleville Gateway
|
10.3.100.1,Belleville Gateway
|
||||||
10.11.31.3,Belleville VPN 11-31
|
10.11.31.3,Belleville VPN 11-31
|
||||||
10.12.32.1,Belleville VPN 12-32
|
10.12.32.1,Belleville VPN 12-32
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
pub struct AppSettings {
|
||||||
|
/// File of list of hosts
|
||||||
|
#[arg(short, long)]
|
||||||
|
pub ping_host_file: Option<PathBuf>,
|
||||||
|
}
|
||||||
+13
-9
@@ -17,6 +17,8 @@ use std::{env, error::Error, ffi::OsString, process};
|
|||||||
use color_eyre::owo_colors::OwoColorize;
|
use color_eyre::owo_colors::OwoColorize;
|
||||||
use crossterm::style::Stylize;
|
use crossterm::style::Stylize;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
use pp::app_settings::AppSettings;
|
||||||
|
|
||||||
const SECONDS_IN_MINUTE: u32 = 60;
|
const SECONDS_IN_MINUTE: u32 = 60;
|
||||||
const SECONDS_IN_HOUR: u32 = SECONDS_IN_MINUTE * 60;
|
const SECONDS_IN_HOUR: u32 = SECONDS_IN_MINUTE * 60;
|
||||||
const SECONDS_IN_DAY: u32 = SECONDS_IN_HOUR * 24;
|
const SECONDS_IN_DAY: u32 = SECONDS_IN_HOUR * 24;
|
||||||
@@ -118,7 +120,7 @@ impl PPState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_targets_from_file(filename: Option<PathBuf>) -> BTreeMap<String, TargetState> {
|
pub fn build_targets_from_file(filename: Option<PathBuf>) -> BTreeMap<String, TargetState> {
|
||||||
PPState::get_default_targets();
|
// PPState::get_default_targets();
|
||||||
if let Some(file) = filename {
|
if let Some(file) = filename {
|
||||||
let mut working = BTreeMap::new();
|
let mut working = BTreeMap::new();
|
||||||
if !&file.exists() {
|
if !&file.exists() {
|
||||||
@@ -156,20 +158,22 @@ impl PPState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Simple program to greet a person
|
/// Simple program to greet a person
|
||||||
#[derive(Parser, Debug)]
|
|
||||||
#[command(version, about, long_about = None)]
|
|
||||||
pub struct AppSettings {
|
|
||||||
/// File of list of hosts
|
|
||||||
#[arg(short, long)]
|
|
||||||
ping_host_file: Option<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Get App Settings
|
// Get App Settings
|
||||||
let settings = AppSettings::parse();
|
let settings = AppSettings::parse();
|
||||||
|
|
||||||
print!("Prep to load targets...");
|
print!("Prep to load targets...");
|
||||||
let mut targets = PPState::build_targets_from_file(settings.ping_host_file);
|
let file_to_check = match settings.ping_host_file {
|
||||||
|
None => {
|
||||||
|
PathBuf::from("./hosts.txt")
|
||||||
|
}
|
||||||
|
Some(actual) => {
|
||||||
|
actual
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut targets = PPState::build_targets_from_file(Some(file_to_check));
|
||||||
|
|
||||||
// channel to send requests to ping
|
// channel to send requests to ping
|
||||||
let (ping_response_sender, ping_response_listener) = mpsc::channel::<PingResult>();
|
let (ping_response_sender, ping_response_listener) = mpsc::channel::<PingResult>();
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use clap::Parser;
|
||||||
|
use pp::app_settings::AppSettings;
|
||||||
|
use pp::tui::ratatui_app::RatatuiApp;
|
||||||
|
fn main() -> color_eyre::Result<()> {
|
||||||
|
// find out what file we are using to get our hosts
|
||||||
|
let settings = AppSettings::parse();
|
||||||
|
|
||||||
|
|
||||||
|
color_eyre::install()?;
|
||||||
|
let terminal = ratatui::init();
|
||||||
|
let result = RatatuiApp::new(settings.ping_host_file).run(terminal);
|
||||||
|
ratatui::restore();
|
||||||
|
result
|
||||||
|
}
|
||||||
+2
-1
@@ -2,7 +2,8 @@ pub mod manager;
|
|||||||
pub mod ping_request;
|
pub mod ping_request;
|
||||||
pub mod ping_result;
|
pub mod ping_result;
|
||||||
pub mod target_state;
|
pub mod target_state;
|
||||||
mod tui;
|
pub mod tui;
|
||||||
|
pub mod app_settings;
|
||||||
|
|
||||||
pub const SECONDS_BETWEEN_DISPLAY: u32 = 1;
|
pub const SECONDS_BETWEEN_DISPLAY: u32 = 1;
|
||||||
pub const SECONDS_BETWEEN_PING: u32 = 2;
|
pub const SECONDS_BETWEEN_PING: u32 = 2;
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod target_state_widget;
|
||||||
|
pub mod ratatui_app;
|
||||||
@@ -0,0 +1,223 @@
|
|||||||
|
use color_eyre::Result;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::net::Ipv4Addr;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::sync::mpsc::Receiver;
|
||||||
|
use std::time::{Duration, SystemTime};
|
||||||
|
use crossterm::event;
|
||||||
|
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||||
|
use ratatui::{DefaultTerminal, Frame};
|
||||||
|
use ratatui::prelude::*;
|
||||||
|
use ratatui::widgets::{Block, Paragraph};
|
||||||
|
use crate::manager::Manager;
|
||||||
|
use crate::ping_result::PingResult;
|
||||||
|
use crate::target_state::TargetState;
|
||||||
|
|
||||||
|
|
||||||
|
const SECONDS_IN_MINUTE: u32 = 60;
|
||||||
|
const SECONDS_IN_HOUR: u32 = SECONDS_IN_MINUTE * 60;
|
||||||
|
const SECONDS_IN_DAY: u32 = SECONDS_IN_HOUR * 24;
|
||||||
|
|
||||||
|
|
||||||
|
pub fn duration_to_string(to_convert: Duration) -> String {
|
||||||
|
let mut total_seconds = to_convert.as_secs() as u32;
|
||||||
|
let mut working_string = String::new();
|
||||||
|
|
||||||
|
if total_seconds > 86400 {
|
||||||
|
// days
|
||||||
|
let num_days = (total_seconds / SECONDS_IN_DAY) as u32;
|
||||||
|
working_string = format!("{} days", num_days);
|
||||||
|
total_seconds = total_seconds - (num_days * SECONDS_IN_DAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
if total_seconds > 3600 {
|
||||||
|
// hours
|
||||||
|
let num_hours = (total_seconds / SECONDS_IN_HOUR) as u32;
|
||||||
|
if num_hours > 0 {
|
||||||
|
working_string = format!("{} {} hours", working_string, num_hours);
|
||||||
|
total_seconds = total_seconds - (num_hours * SECONDS_IN_HOUR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if total_seconds > 60 {
|
||||||
|
let num_minutes = (total_seconds / SECONDS_IN_MINUTE) as u32;
|
||||||
|
if num_minutes > 0 {
|
||||||
|
working_string = format!("{} {} minutes", working_string, num_minutes);
|
||||||
|
total_seconds = total_seconds - (num_minutes * SECONDS_IN_MINUTE);
|
||||||
|
}
|
||||||
|
// minutes
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
working_string = format!("{} {} seconds", working_string, total_seconds);
|
||||||
|
|
||||||
|
working_string
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct RatatuiApp {
|
||||||
|
running: bool,
|
||||||
|
state: BTreeMap<String, TargetState>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RatatuiApp {
|
||||||
|
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||||
|
self.running = true;
|
||||||
|
// start the 'manager' thread that spawns its ping threads as needed
|
||||||
|
let (sender, receiver) = mpsc::channel::<PingResult>();
|
||||||
|
Manager::spawn_manager_thread(self.targets_as_vec(), sender);
|
||||||
|
while self.running {
|
||||||
|
// check for any waiting ping results...
|
||||||
|
self.consume_waiting_results(&receiver);
|
||||||
|
|
||||||
|
terminal.draw(|frame| self.render(frame)).expect("Unable to draw to screen");
|
||||||
|
self.handle_crossterm_events()?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn consume_waiting_results(&mut self, receiver: &Receiver<PingResult>) {
|
||||||
|
let mut local_state = self.state.clone();
|
||||||
|
if let Ok(new_message) = receiver.recv_timeout(Duration::from_millis(10)) {
|
||||||
|
// find the right TargetState
|
||||||
|
for (name, mut state) in local_state.clone() {
|
||||||
|
if state.target == new_message.target {
|
||||||
|
let last_alive_change = if new_message.success == state.alive {
|
||||||
|
state.last_alive_change
|
||||||
|
} else {
|
||||||
|
SystemTime::now()
|
||||||
|
};
|
||||||
|
|
||||||
|
let new_state = TargetState {
|
||||||
|
name: state.name.clone(),
|
||||||
|
target: state.target,
|
||||||
|
alive: new_message.success,
|
||||||
|
last_rtt: new_message.rtt,
|
||||||
|
last_alive_change,
|
||||||
|
};
|
||||||
|
local_state.insert(name.clone(), new_state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.state = local_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn targets_as_vec(&self) -> Vec<Ipv4Addr> {
|
||||||
|
let mut working = vec![];
|
||||||
|
for (_, current) in &self.state {
|
||||||
|
working.push(current.target);
|
||||||
|
}
|
||||||
|
working
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(&mut self, frame: &mut Frame) {
|
||||||
|
let title = Line::from("PP Sample App")
|
||||||
|
.bold()
|
||||||
|
.blue()
|
||||||
|
.centered();
|
||||||
|
let mut working = String::new();
|
||||||
|
for (title, current) in &self.state {
|
||||||
|
let color_name = if current.alive {
|
||||||
|
current.name.clone().green()
|
||||||
|
} else {
|
||||||
|
current.name.clone().red()
|
||||||
|
};
|
||||||
|
working = format!("{}\n{} ({}) - {} / {} / {}",
|
||||||
|
working,
|
||||||
|
color_name,
|
||||||
|
current.target,
|
||||||
|
current.alive,
|
||||||
|
current.last_rtt,
|
||||||
|
duration_to_string(
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(current.last_alive_change)
|
||||||
|
.unwrap()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.render_widget(
|
||||||
|
Paragraph::new(working)
|
||||||
|
.block(Block::bordered().title(title))
|
||||||
|
.centered(),
|
||||||
|
frame.area(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_crossterm_events(&mut self) -> Result<()> {
|
||||||
|
if event::poll(Duration::from_millis(100))? {
|
||||||
|
match event::read()? {
|
||||||
|
// it's important to check KeyEventKind::Press to avoid handling key release events
|
||||||
|
Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key),
|
||||||
|
Event::Mouse(_) => {}
|
||||||
|
Event::Resize(_, _) => {}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn quit(&mut self) { self.running = false; }
|
||||||
|
|
||||||
|
/// Handles the key events and updates the state of [`App`].
|
||||||
|
fn on_key_event(&mut self, key: KeyEvent) {
|
||||||
|
match (key.modifiers, key.code) {
|
||||||
|
(_, KeyCode::Esc | KeyCode::Char('q'))
|
||||||
|
| (KeyModifiers::CONTROL, KeyCode::Char('c') |
|
||||||
|
KeyCode::Char('C')) => { self.quit() }
|
||||||
|
// Add other key handlers here.
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(option: Option<PathBuf>) -> Self {
|
||||||
|
let targets = if let Some(file) = option {
|
||||||
|
let mut working = BTreeMap::new();
|
||||||
|
if !&file.exists() {
|
||||||
|
RatatuiApp::get_default_targets()
|
||||||
|
} else {
|
||||||
|
let real_file = File::open(file);
|
||||||
|
let mut rdr = csv::Reader::from_reader(real_file.unwrap());
|
||||||
|
for result in rdr.records() {
|
||||||
|
let record = result.unwrap();
|
||||||
|
working.insert(record[1].to_string(),
|
||||||
|
TargetState {
|
||||||
|
name: record[1].to_string(),
|
||||||
|
target: Ipv4Addr::from_str(&record[0]).unwrap(),
|
||||||
|
alive: false,
|
||||||
|
last_alive_change: SystemTime::now(),
|
||||||
|
last_rtt: 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
working
|
||||||
|
}
|
||||||
|
} else { RatatuiApp::get_default_targets() };
|
||||||
|
let mut working = Self::default();
|
||||||
|
working.state = targets;
|
||||||
|
working
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_default_targets() -> BTreeMap<String, TargetState> {
|
||||||
|
let mut working = BTreeMap::new();
|
||||||
|
|
||||||
|
working.insert("1111 DNS".to_string(), TargetState {
|
||||||
|
name: "1111 DNS".to_string(),
|
||||||
|
target: Ipv4Addr::new(1,1,1,1),
|
||||||
|
..TargetState::default()
|
||||||
|
});
|
||||||
|
working.insert("Google DNS".to_string(), TargetState {
|
||||||
|
name: "Google DNS".to_string(),
|
||||||
|
target: Ipv4Addr::new(8,8,8,8),
|
||||||
|
..TargetState::default()
|
||||||
|
});
|
||||||
|
working.insert("Test Site 1".to_string(), TargetState {
|
||||||
|
name: "Test Site 1".to_string(),
|
||||||
|
target: Ipv4Addr::new(216,234,202,122),
|
||||||
|
..TargetState::default()
|
||||||
|
});
|
||||||
|
working
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
use ratatui::buffer::Cell;
|
||||||
|
use ratatui::prelude::*;
|
||||||
|
|
||||||
|
pub struct TargetStateWidget;
|
||||||
|
|
||||||
|
impl Widget for TargetStateWidget {
|
||||||
|
fn render(self, area: Rect, buf: &mut Buffer)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user