diff --git a/src/options/structs.rs b/src/options/structs.rs index 55020c1..1aedb94 100644 --- a/src/options/structs.rs +++ b/src/options/structs.rs @@ -105,15 +105,17 @@ impl Metrics { /// #[derive(Debug, Clone)] pub struct ContainerMetrics { - pub cpu_load : f32, - pub ram_load : f32, + container_id : String, + cpu_load : f32, + ram_load : f32, // pub net_activity : ??? - pub processes : Vec, + processes : Vec, } impl ContainerMetrics { - pub fn new(cpu: f32, ram: f32, subsystems: Vec,) -> Self{ + pub fn new(container_id : &str, cpu: f32, ram: f32, subsystems: Vec,) -> Self{ ContainerMetrics { + container_id : String::from(container_id), cpu_load : cpu, ram_load : ram, processes : subsystems, @@ -125,13 +127,15 @@ impl ContainerMetrics { /// #[derive(Debug, Clone)] pub struct ProcessMetrics { - pub cpu_load : f32, - pub ram_load : f32, + process_name : String, + cpu_load : f32, + ram_load : f32, } impl ProcessMetrics { - pub fn new(cpu: f32, ram: f32) -> Self { + pub fn new(process_name :&str, cpu: f32, ram: f32) -> Self { ProcessMetrics { + process_name : String::from(process_name), cpu_load : cpu, ram_load : ram, } diff --git a/src/utils/hagent.rs b/src/utils/hagent.rs index 39c464c..4fd8d75 100644 --- a/src/utils/hagent.rs +++ b/src/utils/hagent.rs @@ -1,13 +1,38 @@ // module needed to check host-agent health condition and to communicate with it -use tokio::net::UnixStream; +use tokio::{io::Interest, net::UnixStream}; // // code will be here // async fn open_unix_socket() -> Result { - let socket = UnixStream::connect("/var/run/enode/hostagent.sock=").await?; + let socket = UnixStream::connect("/var/run/enode/hostagent.sock").await?; Ok(socket) } +async fn ha_healthcheck(socket: &UnixStream) -> Result<(), std::io::Error >{ + socket.ready(Interest::WRITABLE).await?; + if socket.writable().await.is_ok() { + if let Err(er) = socket.try_write(b"Hello HAgent") { + return Err(er); + } + } else { + return Err(std::io::ErrorKind::WouldBlock.into()); + } + Ok(()) +} + + +async fn ha_send_data(socket: &UnixStream, data: &str) -> Result<(), std::io::Error > { + socket.ready(Interest::WRITABLE).await?; + if socket.writable().await.is_ok() { + if let Err(er) = socket.try_write(data.as_bytes()) { + return Err(er); + } + } else { + return Err(std::io::ErrorKind::WouldBlock.into()); + } + Ok(()) +} + #[cfg(test)] mod hagent_unittets { use super::*; @@ -15,7 +40,10 @@ mod hagent_unittets { // maybe bool : true -> alive, false -> dead // simple request on api async fn hagent_healthcheck() { - assert!(true); + let sock = open_unix_socket().await; + assert!(sock.is_ok()); + let sock = sock.unwrap(); + assert!(ha_healthcheck(&sock).await.is_ok()); } #[tokio::test] // Result diff --git a/src/utils/metrics.rs b/src/utils/metrics.rs index 4de54ba..7e8e663 100644 --- a/src/utils/metrics.rs +++ b/src/utils/metrics.rs @@ -7,6 +7,7 @@ use crate::options::structs::TrackingProcess; use sysinfo::{Process, System}; use tokio::join; use crate::options::structs::{ProcessMetrics, ContainerMetrics, PacketInfo}; +use crate::utils::get_container_id; // use pcap::{Device, Capture, Active}; // use std::net::Ipv4Addr; // use anyhow::{Result, Ok}; @@ -83,7 +84,12 @@ async fn get_all_container_metrics(sys: Arc, prcs: Arc) -> f32 { sys.global_cpu_usage() @@ -106,7 +112,11 @@ async fn get_all_metrics_process(proc: Arc, sys: Arc) -> Proces get_cpu_metrics_process(proc.clone()), get_ram_metrics_process(proc.clone(), sys.clone()) ); - ProcessMetrics::new(metrics.0, metrics.1) + ProcessMetrics::new( + proc.name().to_str().unwrap_or("unknown"), + metrics.0, + metrics.1 + ) } async fn get_cpu_metrics_process(proc: Arc) -> f32 { proc.cpu_usage()