diff --git a/WatcherAgent/src/api.rs b/WatcherAgent/src/api.rs index 970401a..156df86 100644 --- a/WatcherAgent/src/api.rs +++ b/WatcherAgent/src/api.rs @@ -308,7 +308,7 @@ pub async fn listening_to_server( /// /// # Returns /// * `Result<(), Box>` - Ok if acknowledgment is sent successfully. -async fn send_acknowledgment( +pub async fn send_acknowledgment( client: &reqwest::Client, base_url: &str, message_id: &str, @@ -339,3 +339,27 @@ async fn send_acknowledgment( Ok(()) } + +pub async fn send_docker_metrics( + base_url: &str, + docker_metrics: &MetricDto, +) -> Result<(), Box> { + let client = Client::new(); + let url = format!("{}/monitoring/docker-metric", base_url); + println!("Metrics: {:?}", docker_metrics); + + match client.post(&url).json(&docker_metrics).send().await { + Ok(res) => println!( + "✅ Sent metrics for server {} | Status: {}", + docker_metrics.server_id, + res.status() + ), + Err(err) => eprintln!("❌ Failed to send metrics: {}", err), + } + + Ok(()) +} + +pub async fn broadcast_docker_containers() { + // Placeholder for future implementation +} \ No newline at end of file diff --git a/WatcherAgent/src/docker/container.rs b/WatcherAgent/src/docker/container.rs index 80eab2f..7356ebf 100644 --- a/WatcherAgent/src/docker/container.rs +++ b/WatcherAgent/src/docker/container.rs @@ -4,7 +4,7 @@ //! use crate::docker::stats; use crate::docker::stats::{ContainerCpuInfo, ContainerNetworkInfo}; -use crate::models::{DockerContainerDto, DockerContainerRegistrationDto}; +use crate::models::{DockerRegistrationDto, DockerMetricDto, DockerContainer}; use bollard::query_parameters::{ CreateImageOptions, ListContainersOptions, RestartContainerOptions, @@ -20,7 +20,7 @@ use std::error::Error; /// /// # Returns /// * `Vec` - Vector of Docker container info. -pub async fn get_available_containers(docker: &Docker) -> Vec { +pub async fn get_available_containers(docker: &Docker) -> Vec { println!("=== DOCKER CONTAINER LIST ==="); let options = Some(ListContainersOptions { @@ -51,26 +51,7 @@ pub async fn get_available_containers(docker: &Docker) -> Vec "running".to_string(), - s if s.contains("exited") || s.contains("stopped") => { - "stopped".to_string() - } - _ => s.to_string(), - }) - .unwrap_or_else(|| "unknown".to_string()); - - println!( - " - ID: {}, Image: {}, Name: {}", - short_id, - container.image.unwrap(), - name - );*/ - - Some(DockerContainerDto { + Some(DockerContainer { id: short_id.to_string(), image, name: name, diff --git a/WatcherAgent/src/docker/mod.rs b/WatcherAgent/src/docker/mod.rs index 44c5abf..dad86c1 100644 --- a/WatcherAgent/src/docker/mod.rs +++ b/WatcherAgent/src/docker/mod.rs @@ -11,7 +11,7 @@ pub mod container; pub mod serverclientcomm; pub mod stats; -use crate::models::{DockerContainerDto, DockerContainerMetricDto}; +use crate::models::{DockerRegistrationDto, DockerMetricDto, DockerContainer}; use bollard::{query_parameters::InspectContainerOptions, Docker}; use std::error::Error; @@ -49,14 +49,14 @@ impl DockerManager { /// Finds the Docker container running the agent by image name pub async fn get_client_container( &self, - ) -> Result, Box> { + ) -> Result, Box> { let containers = container::get_available_containers(&self.docker).await; let client_image = "watcher-agent"; Ok(containers .into_iter() .find(|c| c.image.contains(client_image)) - .map(|container| DockerContainerDto { + .map(|container| DockerContainer { id: container.id, image: container.image, name: container.name, @@ -89,12 +89,12 @@ impl DockerManager { /// Gets all available containers as DTOs for registration pub async fn get_containers_for_registration( &self, - ) -> Result, Box> { + ) -> Result, Box> { let containers = container::get_available_containers(&self.docker).await; Ok(containers .into_iter() - .map(|container| DockerContainerDto { + .map(|container| DockerContainer { id: container.id, image: container.image, name: container.name, @@ -105,7 +105,7 @@ impl DockerManager { /// Gets container metrics for all containers pub async fn get_container_metrics( &self, - ) -> Result, Box> { + ) -> Result, Box> { let containers = container::get_available_containers(&self.docker).await; let mut metrics = Vec::new(); @@ -147,7 +147,7 @@ impl DockerManager { }; metrics.push(DockerContainerMetricDto { - id: container.id, + server_id: container.id, status: status, network: network_stats, cpu: cpu_stats, @@ -185,7 +185,7 @@ impl DockerManager { } // Keep these as utility functions if needed, but they should use DockerManager internally -impl DockerContainerDto { +impl DockerContainer { /// Returns the container ID pub fn id(&self) -> &str { &self.id diff --git a/WatcherAgent/src/docker/stats/cpu.rs b/WatcherAgent/src/docker/stats/cpu.rs new file mode 100644 index 0000000..e69de29 diff --git a/WatcherAgent/src/docker/stats/mod.rs b/WatcherAgent/src/docker/stats/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/WatcherAgent/src/docker/stats/network.rs b/WatcherAgent/src/docker/stats/network.rs new file mode 100644 index 0000000..e69de29 diff --git a/WatcherAgent/src/docker/stats/ram.rs b/WatcherAgent/src/docker/stats/ram.rs new file mode 100644 index 0000000..e69de29 diff --git a/WatcherAgent/src/models.rs b/WatcherAgent/src/models.rs index 34e92f5..516a10a 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -186,22 +186,52 @@ pub struct Acknowledgment { /// - `_net_out`: Network transmit rate in **bytes per second (B/s)** /// - `_cpu_load`: CPU usage as a percentage (**0.0–100.0**) #[derive(Debug, Serialize, Clone)] -pub struct DockerContainerRegistrationDto { +pub struct DockerRegistrationDto { + /// Unique server identifier (integer) pub server_id: u32, - pub containers: Vec, + /// json stringified array of DockerContainer + /// + /// ## Json Example + /// json format: [{"id":"234dsf234","image":"nginx:latest","name":"webserver"},...] + /// + /// ## Fields + /// id: unique container ID (first 12 hex digits) + /// image: docker image name + /// name: container name + pub containers: String, } #[derive(Debug, Serialize, Clone)] -pub struct DockerContainerDto { - pub id: String, - pub image: String, - pub name: String, +pub struct DockerMetricDto { + pub server_id: String, + /// json stringified array of DockerContainer + /// + /// ## Json Example + /// json format: [{"id":"234dsf234","status":"running","image":"nginx:latest","name":"webserver","network":{"net_in":1024,"net_out":2048},"cpu":{"cpu_load":12.5},"ram":{"ram_load":10.0}},...] + /// + /// ## Fields + /// id: unique container ID (first 12 hex digits) + /// status: "running";"stopped";others + /// image: docker image name + /// name: container name + /// network: network stats + /// cpu: cpu stats + /// ram: ram stats + pub containers: String, } #[derive(Debug, Serialize, Clone)] -pub struct DockerContainerMetricDto { +pub struct DockerContainer { pub id: String, pub status: String, // "running";"stopped";others + pub image: String, + pub name: String, pub network: stats::ContainerNetworkInfo, pub cpu: stats::ContainerCpuInfo, + //pub ram: stats::ContainerRamInfo, } + +#[derive(Debug, Serialize, Clone)] +pub struct Docker { + pub containers: Vec, +} \ No newline at end of file