Compare commits

..

2 Commits

Author SHA1 Message Date
prplV 7677fa1f55 fixed hagent work 2024-11-12 12:28:46 +03:00
prplV 4b897c0063 fixed metrics struct 2024-11-12 12:11:04 +03:00
3 changed files with 74 additions and 18 deletions

View File

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

View File

@ -1,27 +1,69 @@
// 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<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)
}
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 log::info;
use crate::utils::metrics;
use super::*;
#[tokio::test]
// 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<maybe Response>
// --Result<maybe Response>
// one-shot func
async fn send_metrics_to_hagent() {
assert!(true);
let procm = crate::options::structs::ProcessMetrics::new("test-prc", 15.0, 5.0);
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]
async fn open_unixsocket_test() {

View File

@ -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<System>, prcs: Arc<Vec<TrackingProce
get_ram_metrics_container(sys.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 {
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_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 {
proc.cpu_usage()