Compare commits

..

No commits in common. "7677fa1f55d63f421cbed6a6cba4fe4fe67b26ee" and "6a9491d3b7aa0557eb59fd6e85bd570aa52e1f53" have entirely different histories.

3 changed files with 18 additions and 74 deletions

View File

@ -84,7 +84,7 @@ pub struct FIleTriggers {
/// ///
/// ///
#[derive(Debug, Clone, Serialize,)] #[derive(Debug, Clone)]
pub struct Metrics { pub struct Metrics {
pub container_metrics : ContainerMetrics, pub container_metrics : ContainerMetrics,
pub processes_metrics : Vec<ProcessMetrics>, pub processes_metrics : Vec<ProcessMetrics>,
@ -103,19 +103,17 @@ impl Metrics {
/// ///
/// ///
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone)]
pub struct ContainerMetrics { pub struct ContainerMetrics {
container_id : String, pub cpu_load : f32,
cpu_load : f32, pub ram_load : f32,
ram_load : f32,
// pub net_activity : ??? // pub net_activity : ???
processes : Vec<String>, pub processes : Vec<String>,
} }
impl ContainerMetrics { impl ContainerMetrics {
pub fn new(container_id : &str, cpu: f32, ram: f32, subsystems: Vec<String>,) -> Self{ pub fn new(cpu: f32, ram: f32, subsystems: Vec<String>,) -> Self{
ContainerMetrics { ContainerMetrics {
container_id : String::from(container_id),
cpu_load : cpu, cpu_load : cpu,
ram_load : ram, ram_load : ram,
processes : subsystems, processes : subsystems,
@ -125,24 +123,22 @@ impl ContainerMetrics {
/// ///
/// ///
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone)]
pub struct ProcessMetrics { pub struct ProcessMetrics {
pub process_name : String, pub cpu_load : f32,
cpu_load : f32, pub ram_load : f32,
ram_load : f32,
} }
impl ProcessMetrics { impl ProcessMetrics {
pub fn new(process_name :&str, cpu: f32, ram: f32) -> Self { pub fn new(cpu: f32, ram: f32) -> Self {
ProcessMetrics { ProcessMetrics {
process_name : String::from(process_name),
cpu_load : cpu, cpu_load : cpu,
ram_load : ram, ram_load : ram,
} }
} }
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone)]
pub struct PacketInfo { pub struct PacketInfo {
protocol : String, protocol : String,
dst_ip : Ipv4Addr, dst_ip : Ipv4Addr,

View File

@ -1,69 +1,27 @@
// module needed to check host-agent health condition and to communicate with it // module needed to check host-agent health condition and to communicate with it
use tokio::{io::Interest, net::UnixStream}; use tokio::net::UnixStream;
// //
// code will be here // code will be here
// //
async fn open_unix_socket() -> Result<UnixStream, std::io::Error> { async fn open_unix_socket() -> Result<UnixStream, std::io::Error> {
let socket = UnixStream::connect("/var/run/enode/hostagent.sock").await?; let socket = UnixStream::connect("/var/run/enode/hostagent.sock=").await?;
Ok(socket) 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)] #[cfg(test)]
mod hagent_unittets { mod hagent_unittets {
use log::info;
use crate::utils::metrics;
use super::*; use super::*;
#[tokio::test] #[tokio::test]
// maybe bool : true -> alive, false -> dead // maybe bool : true -> alive, false -> dead
// simple request on api // simple request on api
async fn hagent_healthcheck() { async fn hagent_healthcheck() {
let sock = open_unix_socket().await; assert!(true);
assert!(sock.is_ok());
let sock = sock.unwrap();
assert!(ha_healthcheck(&sock).await.is_ok());
} }
#[tokio::test] #[tokio::test]
// --Result<maybe Response> // Result<maybe Response>
// one-shot func // one-shot func
async fn send_metrics_to_hagent() { async fn send_metrics_to_hagent() {
let procm = crate::options::structs::ProcessMetrics::new("test-prc", 15.0, 5.0); assert!(true);
let contm = crate::options::structs::ContainerMetrics::new("test", 32.0, 12.0, vec![procm.process_name.clone()]);
let metrics = crate::options::structs::Metrics::new(contm, vec![procm]);
let metrics = &serde_json::to_string_pretty(&metrics).unwrap();
let sock = open_unix_socket().await;
assert!(sock.is_ok());
let sock = sock.unwrap();
assert!(ha_healthcheck(&sock).await.is_ok());
assert!(ha_send_data(&sock, &metrics).await.is_ok());
} }
#[tokio::test] #[tokio::test]
async fn open_unixsocket_test() { async fn open_unixsocket_test() {

View File

@ -7,7 +7,6 @@ use crate::options::structs::TrackingProcess;
use sysinfo::{Process, System}; use sysinfo::{Process, System};
use tokio::join; use tokio::join;
use crate::options::structs::{ProcessMetrics, ContainerMetrics, PacketInfo}; use crate::options::structs::{ProcessMetrics, ContainerMetrics, PacketInfo};
use crate::utils::get_container_id;
// use pcap::{Device, Capture, Active}; // use pcap::{Device, Capture, Active};
// use std::net::Ipv4Addr; // use std::net::Ipv4Addr;
// use anyhow::{Result, Ok}; // use anyhow::{Result, Ok};
@ -84,12 +83,7 @@ async fn get_all_container_metrics(sys: Arc<System>, prcs: Arc<Vec<TrackingProce
get_ram_metrics_container(sys.clone()), get_ram_metrics_container(sys.clone()),
get_subsystems(prcs.clone()) get_subsystems(prcs.clone())
); );
ContainerMetrics::new( ContainerMetrics::new(metrics.0, metrics.1, metrics.2)
&get_container_id().unwrap_or(String::from("unknown")),
metrics.0,
metrics.1,
metrics.2
)
} }
async fn get_cpu_metrics_container(sys: Arc<System>) -> f32 { async fn get_cpu_metrics_container(sys: Arc<System>) -> f32 {
sys.global_cpu_usage() sys.global_cpu_usage()
@ -112,11 +106,7 @@ async fn get_all_metrics_process(proc: Arc<Process>, sys: Arc<System>) -> Proces
get_cpu_metrics_process(proc.clone()), get_cpu_metrics_process(proc.clone()),
get_ram_metrics_process(proc.clone(), sys.clone()) get_ram_metrics_process(proc.clone(), sys.clone())
); );
ProcessMetrics::new( ProcessMetrics::new(metrics.0, metrics.1)
proc.name().to_str().unwrap_or("unknown"),
metrics.0,
metrics.1
)
} }
async fn get_cpu_metrics_process(proc: Arc<Process>) -> f32 { async fn get_cpu_metrics_process(proc: Arc<Process>) -> f32 {
proc.cpu_usage() proc.cpu_usage()