fixed metrics struct

pull/6/head
prplV 2024-11-12 12:11:04 +03:00
parent 6a9491d3b7
commit 4b897c0063
3 changed files with 54 additions and 12 deletions

View File

@ -105,15 +105,17 @@ impl Metrics {
/// ///
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ContainerMetrics { pub struct ContainerMetrics {
pub cpu_load : f32, container_id : String,
pub ram_load : f32, cpu_load : f32,
ram_load : f32,
// pub net_activity : ??? // pub net_activity : ???
pub processes : Vec<String>, processes : Vec<String>,
} }
impl ContainerMetrics { impl ContainerMetrics {
pub fn new(cpu: f32, ram: f32, subsystems: Vec<String>,) -> Self{ pub fn new(container_id : &str, 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,13 +127,15 @@ impl ContainerMetrics {
/// ///
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ProcessMetrics { pub struct ProcessMetrics {
pub cpu_load : f32, process_name : String,
pub ram_load : f32, cpu_load : f32,
ram_load : f32,
} }
impl ProcessMetrics { impl ProcessMetrics {
pub fn new(cpu: f32, ram: f32) -> Self { pub fn new(process_name :&str, 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,
} }

View File

@ -1,13 +1,38 @@
// 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::net::UnixStream; use tokio::{io::Interest, 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 super::*; use super::*;
@ -15,7 +40,10 @@ mod hagent_unittets {
// 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() {
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] #[tokio::test]
// Result<maybe Response> // Result<maybe Response>

View File

@ -7,6 +7,7 @@ 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};
@ -83,7 +84,12 @@ 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(metrics.0, metrics.1, metrics.2) ContainerMetrics::new(
&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()
@ -106,7 +112,11 @@ 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(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<Process>) -> f32 { async fn get_cpu_metrics_process(proc: Arc<Process>) -> f32 {
proc.cpu_usage() proc.cpu_usage()