Skip to content

Commit

Permalink
fix blocking channel in internet check, use arp flag as value
Browse files Browse the repository at this point in the history
  • Loading branch information
blesswinsamuel committed Jan 29, 2024
1 parent be84797 commit d1e052c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
15 changes: 7 additions & 8 deletions src/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ struct ArpDeviceLabels {
ip_addr: String,
hw_addr: String,
device: String,
flags: String,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
Expand Down Expand Up @@ -63,6 +62,7 @@ impl Arp {
// let router_hostname = router_hostname.to_str().unwrap();
// log::info!("router_hostname: {}", router_hostname);
// let domain_name = router_hostname.split('.').collect::<Vec<_>>()[1..].join(".");
log::debug!("Updating arp metrics");
let domain_name = ".home.local";
let f = File::open("/proc/net/arp").await?;
let mut scanner = BufReader::new(f).lines();
Expand All @@ -83,21 +83,20 @@ impl Arp {
let host = match host_cache.get(&ip_addr) {
Some(v) => v.clone(),
None => {
log::debug!("Looking up hostname of {}", ip_addr);
let host = dns_lookup::lookup_addr(&ip_parsed).unwrap_or("unknown".to_string());
let host = host.strip_suffix(&domain_name).unwrap_or(&host).to_string();
host_cache.insert(ip_addr.clone(), host.clone(), Duration::from_secs(60 * 60));
host
}
};
let flags = arr[2].to_string();
let flags = format!("{}f", flags.trim_start_matches("0x"));
let flags = i64::from_str_radix(&flags, 16).unwrap_or(-1);
self.registry
.arp_devices
.get_or_create(&ArpDeviceLabels {
ip_addr: ip_addr.to_string(),
hw_addr: hw_addr.to_string(),
flags: arr[2].to_string(),
device: arr[5].to_string(),
})
.set(1);
.get_or_create(&ArpDeviceLabels { ip_addr: ip_addr.to_string(), hw_addr: hw_addr.to_string(), device: arr[5].to_string() })
.set(flags);
self.registry
.hostnames
.get_or_create(&ArpDeviceHostnameLabels { ip_addr: ip_addr.to_string(), hostname: host.to_string() })
Expand Down
6 changes: 3 additions & 3 deletions src/internet_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ impl InternetCheck {
}

pub async fn start(&self) {
let ticker = crossbeam::channel::tick(Duration::from_secs(10));
self.is_internet_connection_up().await;
let mut ticker = tokio::time::interval(Duration::from_secs(10));
loop {
let _ = ticker.recv();
let _ = ticker.tick().await;
self.is_internet_connection_up().await;
}
}
Expand All @@ -55,6 +54,7 @@ impl InternetCheck {
"208.67.222.222:53",
"9.9.9.9:53",
];
log::debug!("Checking if internet connection is up.");
for addr in addrs.iter() {
let labels = &InternetUpLabels { addr: addr.to_string() };
match tokio::time::timeout(Duration::from_secs(2), TcpStream::connect(addr)).await {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async fn main() {

tracing::debug!("listening on {}", args.bind_addr.to_string());
let listener = tokio::net::TcpListener::bind(args.bind_addr.clone()).await.unwrap();
axum::serve(listener, app.into_make_service()).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
}

Expand Down

0 comments on commit d1e052c

Please sign in to comment.