Add threaded mt fetching, initial db tuff
This commit is contained in:
@@ -15,6 +15,7 @@ use std::{
|
||||
};
|
||||
|
||||
use anyhow::bail;
|
||||
use futures::future::join_all;
|
||||
use mikrotik_rs::{Command, CommandBuilder, Event, MikrotikDevice};
|
||||
use regex::Regex;
|
||||
|
||||
@@ -25,6 +26,7 @@ lazy_static::lazy_static! {
|
||||
}
|
||||
|
||||
pub struct MtCommander;
|
||||
#[derive(Debug, Clone)]
|
||||
struct MtCommanderInternal {
|
||||
cfg: Option<Config>,
|
||||
}
|
||||
@@ -39,19 +41,26 @@ impl MtCommander {
|
||||
pub async fn run_command_on_hosts(
|
||||
hosts: &[ConfigHost],
|
||||
command: &str,
|
||||
) -> anyhow::Result<HashMap<ConfigHost, Vec<HashMap<String, Option<String>>>>> {
|
||||
let mut res_map = HashMap::new();
|
||||
for host in hosts {
|
||||
let res = Self::run_command_on_host(host, command).await?;
|
||||
res_map.insert(host.clone(), res);
|
||||
}
|
||||
) -> anyhow::Result<HashMap<ConfigHost, anyhow::Result<Vec<HashMap<String, Option<String>>>>>>
|
||||
{
|
||||
let results = join_all(hosts.iter().map(|host| async move {
|
||||
let res = Self::run_command_on_host(host, command).await;
|
||||
(host.clone(), res)
|
||||
}))
|
||||
.await;
|
||||
|
||||
let res_map: HashMap<_, _> = results.into_iter().collect();
|
||||
Ok(res_map)
|
||||
}
|
||||
pub async fn run_command_on_host(
|
||||
host: &ConfigHost,
|
||||
command: &str,
|
||||
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
|
||||
let mtc = MTC.lock().map_err(|e| anyhow::anyhow!(e.to_string()))?;
|
||||
let mtc = {
|
||||
MTC.lock()
|
||||
.map_err(|e| anyhow::anyhow!(e.to_string()))?
|
||||
.clone()
|
||||
};
|
||||
mtc.run_command(host, &command).await
|
||||
}
|
||||
|
||||
@@ -114,18 +123,16 @@ impl MtCommanderInternal {
|
||||
cmd: &str,
|
||||
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
|
||||
let addr = format!("{}:{}", &host.ip, host.port(&self.cfg()));
|
||||
let dev_res = MikrotikDevice::connect(
|
||||
&addr,
|
||||
&host.username(self.cfg()),
|
||||
Some(&host.password(self.cfg())),
|
||||
)
|
||||
.await;
|
||||
let dev_res = MikrotikDevice::builder(&addr)
|
||||
.credentials(&host.username(self.cfg()), Some(&host.password(self.cfg())))
|
||||
.connect()
|
||||
.await;
|
||||
|
||||
let dev = match dev_res {
|
||||
Ok(dev) => {
|
||||
log::info!("({addr}) Connected successfully!");
|
||||
dev
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Failed to connect to {addr}: {e}");
|
||||
bail!(e);
|
||||
@@ -142,7 +149,12 @@ impl MtCommanderInternal {
|
||||
match event {
|
||||
Event::Reply { response, tag: _ } => {
|
||||
log::debug!("({}) {:?}", &host.ip, response.attributes);
|
||||
size += response.attributes.values().flatten().map(|v| v.len()).sum::<usize>();
|
||||
size += response
|
||||
.attributes
|
||||
.values()
|
||||
.flatten()
|
||||
.map(|v| v.len())
|
||||
.sum::<usize>();
|
||||
res.push(response.attributes);
|
||||
}
|
||||
Event::Done { .. } => {
|
||||
|
||||
Reference in New Issue
Block a user