Update host list format to be
<ip_address>,<name> to make end user display and interpretation easier
This commit is contained in:
parent
11618c0d0a
commit
da10ec72f8
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
target/*
|
||||||
|
.idea/*
|
||||||
34
Cargo.lock
generated
34
Cargo.lock
generated
@ -116,6 +116,27 @@ version = "1.0.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "csv"
|
||||||
|
version = "1.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf"
|
||||||
|
dependencies = [
|
||||||
|
"csv-core",
|
||||||
|
"itoa",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "csv-core"
|
||||||
|
version = "0.1.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7d02f3b0da4c6504f86e9cd789d8dbafab48c2321be74e9987593de5a894d93d"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "env_filter"
|
name = "env_filter"
|
||||||
version = "0.1.3"
|
version = "0.1.3"
|
||||||
@ -151,6 +172,12 @@ version = "1.70.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jiff"
|
name = "jiff"
|
||||||
version = "0.2.6"
|
version = "0.2.6"
|
||||||
@ -214,6 +241,7 @@ version = "0.1.1"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi_term",
|
"ansi_term",
|
||||||
"clap",
|
"clap",
|
||||||
|
"csv",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"log",
|
"log",
|
||||||
]
|
]
|
||||||
@ -265,6 +293,12 @@ version = "0.8.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.219"
|
version = "1.0.219"
|
||||||
|
|||||||
@ -8,6 +8,10 @@ env_logger = "0.11"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
ansi_term = "0.12"
|
ansi_term = "0.12"
|
||||||
clap = { version = "4.5", features = ["derive"] }
|
clap = { version = "4.5", features = ["derive"] }
|
||||||
|
csv = "1.3"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "duration_to_string"
|
name = "duration_to_string"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "pp"
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
10.3.100.1
|
10.3.100.1,Belleville Router
|
||||||
10.11.31.3
|
10.11.31.3,Belleville VPN 11-31
|
||||||
10.12.32.1
|
10.12.32.1,Belleville VPN 12-32
|
||||||
10.11.21.1
|
10.11.21.1,Lindsay VPN 11-21
|
||||||
10.12.22.1
|
10.12.22.1,Lindsay VPN 11-22
|
||||||
8.8.8.8
|
8.8.8.8,Google DNS
|
||||||
1.1.1.1
|
1.1.1.1,1111 DNS
|
||||||
24.51.235.162
|
24.51.235.162,Peterborough Cogeco
|
||||||
99.214.0.92
|
99.214.0.92,Peterborough Rogers
|
||||||
216.234.202.122
|
216.234.202.122,Lindsay Starlink
|
||||||
24.143.184.98
|
24.143.184.98,Lindsay Cogeco
|
||||||
192.186.110.6
|
192.186.110.6,Belleville Cogeco
|
||||||
129.222.197.36
|
129.222.197.36,Belleville Starlink
|
||||||
|
|||||||
26
lindsay.txt
26
lindsay.txt
@ -1,13 +1,13 @@
|
|||||||
10.2.100.1
|
10.3.100.1,Belleville Router
|
||||||
10.11.21.1
|
10.11.31.3,Belleville VPN 11-31
|
||||||
10.12.22.1
|
10.12.32.1,Belleville VPN 12-32
|
||||||
10.11.31.3
|
10.11.21.1,Lindsay VPN 11-21
|
||||||
10.12.32.1
|
10.12.22.1,Lindsay VPN 11-22
|
||||||
8.8.8.8
|
8.8.8.8,Google DNS
|
||||||
1.1.1.1
|
1.1.1.1,1111 DNS
|
||||||
24.51.235.162
|
24.51.235.162,Peterborough Cogeco
|
||||||
99.214.0.92
|
99.214.0.92,Peterborough Rogers
|
||||||
216.234.202.122
|
216.234.202.122,Lindsay Starlink
|
||||||
24.143.184.98
|
24.143.184.98,Lindsay Cogeco
|
||||||
192.186.110.6
|
192.186.110.6,Belleville Cogeco
|
||||||
129.222.197.36
|
129.222.197.36,Belleville Starlink
|
||||||
|
|||||||
135
src/bin/pp.rs
135
src/bin/pp.rs
@ -1,3 +1,4 @@
|
|||||||
|
use std::collections::BTreeMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
@ -11,39 +12,94 @@ use pp::ping_result::PingResult;
|
|||||||
use pp::ping_request::PingRequest;
|
use pp::ping_request::PingRequest;
|
||||||
use pp::manager::Manager;
|
use pp::manager::Manager;
|
||||||
use pp::SECONDS_BETWEEN_DISPLAY;
|
use pp::SECONDS_BETWEEN_DISPLAY;
|
||||||
use pp::state::State;
|
|
||||||
use pp::target_state::TargetState;
|
use pp::target_state::TargetState;
|
||||||
|
use std::{env, error::Error, ffi::OsString, process};
|
||||||
|
|
||||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
fn get_default_targets() -> BTreeMap<String, TargetState> {
|
||||||
where P: AsRef<Path>, {
|
let mut working = BTreeMap::new();
|
||||||
let file = File::open(filename)?;
|
working.insert("Localhost".to_string(),
|
||||||
Ok(io::BufReader::new(file).lines())
|
TargetState {
|
||||||
|
name: "Localhost".to_string(),
|
||||||
|
target: Ipv4Addr::new(127,0,0,1),
|
||||||
|
..TargetState::default()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
working.insert("Home Gateway".to_string(),
|
||||||
|
TargetState {
|
||||||
|
name: "Home Gateway".to_string(),
|
||||||
|
target: Ipv4Addr::new(172,24,0,1),
|
||||||
|
..TargetState::default()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
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("Site IP 1".to_string(),
|
||||||
|
TargetState {
|
||||||
|
name: "Site IP 1".to_string(),
|
||||||
|
target: Ipv4Addr::new(216,121,247,231),
|
||||||
|
..TargetState::default()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
working.insert("Site IP 2".to_string(),
|
||||||
|
TargetState {
|
||||||
|
name: "Site IP 2".to_string(),
|
||||||
|
target: Ipv4Addr::new(216,234,202,122),
|
||||||
|
..TargetState::default()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
working.insert("Site IP 3".to_string(),
|
||||||
|
TargetState {
|
||||||
|
name: "Site IP 3".to_string(),
|
||||||
|
target: Ipv4Addr::new(24,143,184,98),
|
||||||
|
..TargetState::default()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
working
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_targets_from_file(filename: Option<PathBuf>) -> Vec<Ipv4Addr>{
|
fn build_targets_from_file(filename: Option<PathBuf>) -> BTreeMap<String, TargetState>{
|
||||||
let mut targets = vec![Ipv4Addr::new(127, 0, 0, 1),
|
let mut hosts: BTreeMap<String, TargetState> = get_default_targets();
|
||||||
Ipv4Addr::new(172, 24, 10, 1),
|
|
||||||
Ipv4Addr::new(1,1,1,1),
|
|
||||||
Ipv4Addr::new(8,8,8,8),
|
|
||||||
Ipv4Addr::new(216,121,247,231),
|
|
||||||
Ipv4Addr::new(216,234,202,122),
|
|
||||||
Ipv4Addr::new(24,143,184,98),
|
|
||||||
];
|
|
||||||
|
|
||||||
if let Some(file) = filename {
|
if let Some(file) = filename {
|
||||||
println!("File is {:?}", file.to_str());
|
println!("File is {:?}", file.to_str());
|
||||||
if let Ok(lines) = read_lines(file) {
|
let file = File::open(file);
|
||||||
targets.clear();
|
let mut rdr = csv::Reader::from_reader(file.unwrap());
|
||||||
// Consumes the iterator, returns an (Optional) String
|
for result in rdr.records() {
|
||||||
for line in lines.map_while(Result::ok) {
|
let record = result.unwrap();
|
||||||
targets.push(
|
hosts.insert(record[1].to_string(),
|
||||||
Ipv4Addr::from_str(&*line).unwrap()
|
TargetState {
|
||||||
);
|
name: record[1].to_string(),
|
||||||
|
target: Ipv4Addr::from_str(&record[0]).unwrap(),
|
||||||
|
alive: false,
|
||||||
|
last_alive_change: SystemTime::now(),
|
||||||
|
last_rtt: 0
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// targets = vec![];
|
|
||||||
};
|
};
|
||||||
targets
|
hosts
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ips_from_state(to_read_from: BTreeMap<String, TargetState>) -> Vec<Ipv4Addr> {
|
||||||
|
let mut working: Vec<Ipv4Addr> = vec![];
|
||||||
|
for current in to_read_from {
|
||||||
|
working.push(current.1.target);
|
||||||
|
}
|
||||||
|
working
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Simple program to greet a person
|
/// Simple program to greet a person
|
||||||
@ -64,21 +120,16 @@ fn main() {
|
|||||||
|
|
||||||
let file_to_read = settings.ping_host_file;
|
let file_to_read = settings.ping_host_file;
|
||||||
|
|
||||||
let targets = build_targets_from_file(file_to_read);
|
let mut targets = build_targets_from_file(file_to_read);
|
||||||
let mut state = State::default();
|
|
||||||
|
|
||||||
for current in &targets {
|
|
||||||
state.add_target(*current);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("Setting up requests for {} hosts.", targets.len());
|
println!("Setting up requests for {} hosts.", targets.len());
|
||||||
Manager::spawn_manager_thread(state.targets_ipv4().clone(), ping_response_sender.clone());
|
Manager::spawn_manager_thread(ips_from_state(targets.clone()), ping_response_sender.clone());
|
||||||
|
|
||||||
let mut display_loop_start = SystemTime::now();
|
let mut display_loop_start = SystemTime::now();
|
||||||
let mut duration_since_last_loop = SystemTime::now().duration_since(display_loop_start).unwrap();
|
let mut duration_since_last_loop = SystemTime::now().duration_since(display_loop_start).unwrap();
|
||||||
loop {
|
loop {
|
||||||
if let Ok(response) = ping_response_listener.recv_timeout(Duration::from_millis(100)) {
|
if let Ok(response) = ping_response_listener.recv_timeout(Duration::from_millis(100)) {
|
||||||
for (index, current_state) in state.targets.clone().iter().enumerate() {
|
for (index, (name, current_state)) in targets.clone().iter().enumerate() {
|
||||||
if current_state.target == response.target {
|
if current_state.target == response.target {
|
||||||
let last_alive_change = if response.success == current_state.alive {
|
let last_alive_change = if response.success == current_state.alive {
|
||||||
current_state.last_alive_change
|
current_state.last_alive_change
|
||||||
@ -87,12 +138,13 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let new_state = TargetState {
|
let new_state = TargetState {
|
||||||
|
name: current_state.name.clone(),
|
||||||
target: current_state.target,
|
target: current_state.target,
|
||||||
alive: response.success,
|
alive: response.success,
|
||||||
last_rtt: response.rtt,
|
last_rtt: response.rtt,
|
||||||
last_alive_change,
|
last_alive_change,
|
||||||
};
|
};
|
||||||
state.targets[index] = new_state.clone();
|
targets.insert(current_state.name.clone(), new_state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,15 +153,16 @@ fn main() {
|
|||||||
.expect("unable to figure out how long ago we displayed stuff");
|
.expect("unable to figure out how long ago we displayed stuff");
|
||||||
if duration_since_last_loop.as_secs() > SECONDS_BETWEEN_DISPLAY as u64 {
|
if duration_since_last_loop.as_secs() > SECONDS_BETWEEN_DISPLAY as u64 {
|
||||||
println!("DISPLAY LOOP");
|
println!("DISPLAY LOOP");
|
||||||
println!("Host \t\t | Alive \t | RTT \t\t");
|
println!("Host \t\t\t\t\t | Alive \t | RTT \t\t");
|
||||||
for current_result in state.targets.clone() {
|
for (name, current_result) in targets.clone() {
|
||||||
|
let mut target_string = String::new();
|
||||||
let time_since_last_change = SystemTime::now().duration_since(current_result.last_alive_change).unwrap();
|
let time_since_last_change = SystemTime::now().duration_since(current_result.last_alive_change).unwrap();
|
||||||
let target_string = if current_result.target.clone().to_string().len() > 8 {
|
target_string = format!("{} ({})", name, current_result.target);
|
||||||
format!("{}", current_result.target)
|
while target_string.len() < 34 {
|
||||||
} else {
|
target_string = format!("{} ", target_string);
|
||||||
format!("{}\t", current_result.target)
|
}
|
||||||
};
|
|
||||||
println!("{}\t | {} \t | {}\t | Changed {}s ago",
|
println!("{} \t | {} \t | {}\t | Changed {}s ago",
|
||||||
target_string,
|
target_string,
|
||||||
current_result.alive,
|
current_result.alive,
|
||||||
current_result.last_rtt,time_since_last_change.as_secs()
|
current_result.last_rtt,time_since_last_change.as_secs()
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
pub mod manager;
|
pub mod manager;
|
||||||
pub mod ping_request;
|
pub mod ping_request;
|
||||||
pub mod ping_result;
|
pub mod ping_result;
|
||||||
pub mod state;
|
|
||||||
pub mod target_state;
|
pub mod target_state;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -37,7 +37,7 @@ impl Manager {
|
|||||||
#[cfg(any(target_os="windows"))] {
|
#[cfg(any(target_os="windows"))] {
|
||||||
result = Command::new("c:/windows/system32/ping.exe").arg(request.target.to_string())
|
result = Command::new("c:/windows/system32/ping.exe").arg(request.target.to_string())
|
||||||
.arg("-n 1")
|
.arg("-n 1")
|
||||||
.arg("-w 10")
|
.arg(format!("-w {}", crate::SECONDS_BETWEEN_PING))
|
||||||
.arg("-4")
|
.arg("-4")
|
||||||
.output().unwrap().status.code().unwrap_or(255);
|
.output().unwrap().status.code().unwrap_or(255);
|
||||||
success = result == WIN_PING_SUCCESS as i32;;
|
success = result == WIN_PING_SUCCESS as i32;;
|
||||||
@ -46,7 +46,7 @@ impl Manager {
|
|||||||
result = Command::new("/usr/bin/ping").arg(request.target.to_string())
|
result = Command::new("/usr/bin/ping").arg(request.target.to_string())
|
||||||
.arg("-c 1")
|
.arg("-c 1")
|
||||||
.arg("-4")
|
.arg("-4")
|
||||||
.arg("-w 10")
|
.arg(format!("-w {}", SECONDS_BETWEEN_PING))
|
||||||
.arg("-W 1")
|
.arg("-W 1")
|
||||||
.output().unwrap().status.code().unwrap_or(255);
|
.output().unwrap().status.code().unwrap_or(255);
|
||||||
success = result == LIN_PING_SUCCESS as i32;
|
success = result == LIN_PING_SUCCESS as i32;
|
||||||
@ -64,5 +64,4 @@ impl Manager {
|
|||||||
// println!("Attempt for for {}", local_request.target);
|
// println!("Attempt for for {}", local_request.target);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
26
src/state.rs
26
src/state.rs
@ -1,26 +0,0 @@
|
|||||||
use std::net::Ipv4Addr;
|
|
||||||
use crate::target_state::TargetState;
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct State {
|
|
||||||
pub targets: Vec<TargetState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl State {
|
|
||||||
pub fn add_target(&mut self, to_add: Ipv4Addr) {
|
|
||||||
self.targets.push(TargetState {
|
|
||||||
target: to_add,
|
|
||||||
..TargetState::default()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn targets_ipv4(&self) -> Vec<Ipv4Addr> {
|
|
||||||
let mut working = vec![];
|
|
||||||
|
|
||||||
for current in &self.targets {
|
|
||||||
working.push(current.target);
|
|
||||||
}
|
|
||||||
|
|
||||||
working
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,8 +1,9 @@
|
|||||||
use std::net::Ipv4Addr;
|
use std::net::Ipv4Addr;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct TargetState {
|
pub struct TargetState {
|
||||||
|
pub name: String,
|
||||||
pub target: Ipv4Addr,
|
pub target: Ipv4Addr,
|
||||||
pub alive: bool,
|
pub alive: bool,
|
||||||
pub last_rtt: u32,
|
pub last_rtt: u32,
|
||||||
@ -12,6 +13,7 @@ pub struct TargetState {
|
|||||||
impl Default for TargetState {
|
impl Default for TargetState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
TargetState {
|
TargetState {
|
||||||
|
name: "Unknown Host".to_string(),
|
||||||
target: Ipv4Addr::new(127, 0, 0, 1),
|
target: Ipv4Addr::new(127, 0, 0, 1),
|
||||||
alive: false,
|
alive: false,
|
||||||
last_rtt: 0,
|
last_rtt: 0,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user