changed Dtos and Docker structs
This commit is contained in:
@@ -308,7 +308,7 @@ pub async fn listening_to_server(
|
|||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if acknowledgment is sent successfully.
|
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if acknowledgment is sent successfully.
|
||||||
async fn send_acknowledgment(
|
pub async fn send_acknowledgment(
|
||||||
client: &reqwest::Client,
|
client: &reqwest::Client,
|
||||||
base_url: &str,
|
base_url: &str,
|
||||||
message_id: &str,
|
message_id: &str,
|
||||||
@@ -339,3 +339,27 @@ async fn send_acknowledgment(
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn send_docker_metrics(
|
||||||
|
base_url: &str,
|
||||||
|
docker_metrics: &MetricDto,
|
||||||
|
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
|
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
|
||||||
|
}
|
@@ -4,7 +4,7 @@
|
|||||||
//!
|
//!
|
||||||
use crate::docker::stats;
|
use crate::docker::stats;
|
||||||
use crate::docker::stats::{ContainerCpuInfo, ContainerNetworkInfo};
|
use crate::docker::stats::{ContainerCpuInfo, ContainerNetworkInfo};
|
||||||
use crate::models::{DockerContainerDto, DockerContainerRegistrationDto};
|
use crate::models::{DockerRegistrationDto, DockerMetricDto, DockerContainer};
|
||||||
|
|
||||||
use bollard::query_parameters::{
|
use bollard::query_parameters::{
|
||||||
CreateImageOptions, ListContainersOptions, RestartContainerOptions,
|
CreateImageOptions, ListContainersOptions, RestartContainerOptions,
|
||||||
@@ -20,7 +20,7 @@ use std::error::Error;
|
|||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// * `Vec<DockerContainer>` - Vector of Docker container info.
|
/// * `Vec<DockerContainer>` - Vector of Docker container info.
|
||||||
pub async fn get_available_containers(docker: &Docker) -> Vec<DockerContainerDto> {
|
pub async fn get_available_containers(docker: &Docker) -> Vec<DockerContainer> {
|
||||||
println!("=== DOCKER CONTAINER LIST ===");
|
println!("=== DOCKER CONTAINER LIST ===");
|
||||||
|
|
||||||
let options = Some(ListContainersOptions {
|
let options = Some(ListContainersOptions {
|
||||||
@@ -51,26 +51,7 @@ pub async fn get_available_containers(docker: &Docker) -> Vec<DockerContainerDto
|
|||||||
.map(|img| img.to_string())
|
.map(|img| img.to_string())
|
||||||
.unwrap_or_else(|| "unknown".to_string());
|
.unwrap_or_else(|| "unknown".to_string());
|
||||||
|
|
||||||
/*let status = container
|
Some(DockerContainer {
|
||||||
.status
|
|
||||||
.as_ref()
|
|
||||||
.map(|s| match s.to_lowercase().as_str() {
|
|
||||||
s if s.contains("up") || s.contains("running") => "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 {
|
|
||||||
id: short_id.to_string(),
|
id: short_id.to_string(),
|
||||||
image,
|
image,
|
||||||
name: name,
|
name: name,
|
||||||
|
@@ -11,7 +11,7 @@ pub mod container;
|
|||||||
pub mod serverclientcomm;
|
pub mod serverclientcomm;
|
||||||
pub mod stats;
|
pub mod stats;
|
||||||
|
|
||||||
use crate::models::{DockerContainerDto, DockerContainerMetricDto};
|
use crate::models::{DockerRegistrationDto, DockerMetricDto, DockerContainer};
|
||||||
use bollard::{query_parameters::InspectContainerOptions, Docker};
|
use bollard::{query_parameters::InspectContainerOptions, Docker};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
@@ -49,14 +49,14 @@ impl DockerManager {
|
|||||||
/// Finds the Docker container running the agent by image name
|
/// Finds the Docker container running the agent by image name
|
||||||
pub async fn get_client_container(
|
pub async fn get_client_container(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<Option<DockerContainerDto>, Box<dyn Error + Send + Sync>> {
|
) -> Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
||||||
let containers = container::get_available_containers(&self.docker).await;
|
let containers = container::get_available_containers(&self.docker).await;
|
||||||
let client_image = "watcher-agent";
|
let client_image = "watcher-agent";
|
||||||
|
|
||||||
Ok(containers
|
Ok(containers
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|c| c.image.contains(client_image))
|
.find(|c| c.image.contains(client_image))
|
||||||
.map(|container| DockerContainerDto {
|
.map(|container| DockerContainer {
|
||||||
id: container.id,
|
id: container.id,
|
||||||
image: container.image,
|
image: container.image,
|
||||||
name: container.name,
|
name: container.name,
|
||||||
@@ -89,12 +89,12 @@ impl DockerManager {
|
|||||||
/// Gets all available containers as DTOs for registration
|
/// Gets all available containers as DTOs for registration
|
||||||
pub async fn get_containers_for_registration(
|
pub async fn get_containers_for_registration(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<Vec<DockerContainerDto>, Box<dyn Error + Send + Sync>> {
|
) -> Result<Vec<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
||||||
let containers = container::get_available_containers(&self.docker).await;
|
let containers = container::get_available_containers(&self.docker).await;
|
||||||
|
|
||||||
Ok(containers
|
Ok(containers
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|container| DockerContainerDto {
|
.map(|container| DockerContainer {
|
||||||
id: container.id,
|
id: container.id,
|
||||||
image: container.image,
|
image: container.image,
|
||||||
name: container.name,
|
name: container.name,
|
||||||
@@ -105,7 +105,7 @@ impl DockerManager {
|
|||||||
/// Gets container metrics for all containers
|
/// Gets container metrics for all containers
|
||||||
pub async fn get_container_metrics(
|
pub async fn get_container_metrics(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<Vec<DockerContainerMetricDto>, Box<dyn Error + Send + Sync>> {
|
) -> Result<Vec<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
||||||
let containers = container::get_available_containers(&self.docker).await;
|
let containers = container::get_available_containers(&self.docker).await;
|
||||||
let mut metrics = Vec::new();
|
let mut metrics = Vec::new();
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@ impl DockerManager {
|
|||||||
};
|
};
|
||||||
|
|
||||||
metrics.push(DockerContainerMetricDto {
|
metrics.push(DockerContainerMetricDto {
|
||||||
id: container.id,
|
server_id: container.id,
|
||||||
status: status,
|
status: status,
|
||||||
network: network_stats,
|
network: network_stats,
|
||||||
cpu: cpu_stats,
|
cpu: cpu_stats,
|
||||||
@@ -185,7 +185,7 @@ impl DockerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Keep these as utility functions if needed, but they should use DockerManager internally
|
// Keep these as utility functions if needed, but they should use DockerManager internally
|
||||||
impl DockerContainerDto {
|
impl DockerContainer {
|
||||||
/// Returns the container ID
|
/// Returns the container ID
|
||||||
pub fn id(&self) -> &str {
|
pub fn id(&self) -> &str {
|
||||||
&self.id
|
&self.id
|
||||||
|
0
WatcherAgent/src/docker/stats/cpu.rs
Normal file
0
WatcherAgent/src/docker/stats/cpu.rs
Normal file
0
WatcherAgent/src/docker/stats/mod.rs
Normal file
0
WatcherAgent/src/docker/stats/mod.rs
Normal file
0
WatcherAgent/src/docker/stats/network.rs
Normal file
0
WatcherAgent/src/docker/stats/network.rs
Normal file
0
WatcherAgent/src/docker/stats/ram.rs
Normal file
0
WatcherAgent/src/docker/stats/ram.rs
Normal file
@@ -186,22 +186,52 @@ pub struct Acknowledgment {
|
|||||||
/// - `_net_out`: Network transmit rate in **bytes per second (B/s)**
|
/// - `_net_out`: Network transmit rate in **bytes per second (B/s)**
|
||||||
/// - `_cpu_load`: CPU usage as a percentage (**0.0–100.0**)
|
/// - `_cpu_load`: CPU usage as a percentage (**0.0–100.0**)
|
||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct DockerContainerRegistrationDto {
|
pub struct DockerRegistrationDto {
|
||||||
|
/// Unique server identifier (integer)
|
||||||
pub server_id: u32,
|
pub server_id: u32,
|
||||||
pub containers: Vec<DockerContainerDto>,
|
/// 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)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct DockerContainerDto {
|
pub struct DockerMetricDto {
|
||||||
pub id: String,
|
pub server_id: String,
|
||||||
pub image: String,
|
/// json stringified array of DockerContainer
|
||||||
pub name: String,
|
///
|
||||||
|
/// ## 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)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct DockerContainerMetricDto {
|
pub struct DockerContainer {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub status: String, // "running";"stopped";others
|
pub status: String, // "running";"stopped";others
|
||||||
|
pub image: String,
|
||||||
|
pub name: String,
|
||||||
pub network: stats::ContainerNetworkInfo,
|
pub network: stats::ContainerNetworkInfo,
|
||||||
pub cpu: stats::ContainerCpuInfo,
|
pub cpu: stats::ContainerCpuInfo,
|
||||||
|
//pub ram: stats::ContainerRamInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Clone)]
|
||||||
|
pub struct Docker {
|
||||||
|
pub containers: Vec<DockerContainer>,
|
||||||
}
|
}
|
Reference in New Issue
Block a user