Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c90a276dca | |||
| dc4c23f9d9 | |||
| 3182d57539 |
@@ -152,10 +152,45 @@ async fn get_server_id_by_ip(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Broadcasts Docker container information to the monitoring server for service discovery.
|
||||||
|
///
|
||||||
|
/// This function sends the current Docker container configuration to the server
|
||||||
|
/// to register available containers and enable service monitoring. It will
|
||||||
|
/// continuously retry until successful, making it suitable for initial
|
||||||
|
/// registration scenarios.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `base_url` - The base URL of the monitoring server API (e.g., "https://monitoring.example.com")
|
||||||
|
/// * `server_id` - The ID of the server to associate the containers with
|
||||||
|
/// * `container_dto` - Mutable reference to Docker container information for broadcast
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(())` - When container information is successfully broadcasted to the server
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If an unrecoverable error occurs (though the function typically retries on transient failures)
|
||||||
|
///
|
||||||
|
/// # Behavior
|
||||||
|
///
|
||||||
|
/// This function operates in a retry loop with the following characteristics:
|
||||||
|
///
|
||||||
|
/// - **Retry Logic**: Attempts broadcast every 10 seconds until successful
|
||||||
|
/// - **Mutation**: Modifies the `container_dto` to set the `server_id` before sending
|
||||||
|
/// - **TLS**: Accepts invalid TLS certificates for development environments
|
||||||
|
/// - **Logging**: Provides detailed console output about broadcast attempts and results
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// This function may return an error in the following cases:
|
||||||
|
///
|
||||||
|
/// * **HTTP Client Creation**: Failed to create HTTP client with TLS configuration
|
||||||
|
/// * **Network Issues**: Persistent connection failures to the backend server
|
||||||
|
/// * **Server Errors**: Backend returns non-success HTTP status codes repeatedly
|
||||||
|
/// * **JSON Serialization**: Cannot serialize container data (should be rare with proper DTOs)
|
||||||
pub async fn broadcast_docker_containers(
|
pub async fn broadcast_docker_containers(
|
||||||
base_url: &str,
|
base_url: &str,
|
||||||
server_id: u16,
|
server_id: u16,
|
||||||
container_dto: &mut DockerRegistrationDto,
|
container_dto: &DockerRegistrationDto,
|
||||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
// First get local IP
|
// First get local IP
|
||||||
println!("Preparing to broadcast docker containers...");
|
println!("Preparing to broadcast docker containers...");
|
||||||
@@ -165,8 +200,8 @@ pub async fn broadcast_docker_containers(
|
|||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
// Prepare registration data
|
// Prepare registration data
|
||||||
let container_dto = container_dto;
|
let mut broadcast_data = container_dto.clone();
|
||||||
container_dto.server_id = server_id;
|
broadcast_data.server_id = server_id;
|
||||||
|
|
||||||
// Try to register (will retry on failure)
|
// Try to register (will retry on failure)
|
||||||
loop {
|
loop {
|
||||||
|
|||||||
@@ -129,27 +129,72 @@ impl DockerManager {
|
|||||||
/// Collects Docker metrics for all containers
|
/// Collects Docker metrics for all containers
|
||||||
pub async fn collect_metrics(&self) -> Result<DockerMetricDto, Box<dyn Error + Send + Sync>> {
|
pub async fn collect_metrics(&self) -> Result<DockerMetricDto, Box<dyn Error + Send + Sync>> {
|
||||||
let containers = self.get_containers().await?;
|
let containers = self.get_containers().await?;
|
||||||
let (cpu_stats, net_stats, mem_stats) = stats::get_container_stats(&self.docker).await?;
|
if let Some(first_container) = containers.first() {
|
||||||
|
println!("Debug: Testing stats for container {}", first_container.id);
|
||||||
|
let _ = self.debug_container_stats(&first_container.id).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Get stats with proper error handling
|
||||||
|
let stats_result = stats::get_container_stats(&self.docker).await;
|
||||||
|
let (cpu_stats, net_stats, mem_stats) = match stats_result {
|
||||||
|
Ok(stats) => stats,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Warning: Failed to get container stats: {}", e);
|
||||||
|
// Return empty stats instead of failing completely
|
||||||
|
(Vec::new(), Vec::new(), Vec::new())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Debug: Found {} containers, {} CPU stats, {} network stats, {} memory stats",
|
||||||
|
containers.len(), cpu_stats.len(), net_stats.len(), mem_stats.len());
|
||||||
|
|
||||||
let container_infos_total: Vec<_> = containers
|
let container_infos_total: Vec<_> = containers
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|container| {
|
.map(|container| {
|
||||||
|
// Use short ID for matching (first 12 chars)
|
||||||
|
let container_short_id = if container.id.len() > 12 {
|
||||||
|
&container.id[..12]
|
||||||
|
} else {
|
||||||
|
&container.id
|
||||||
|
};
|
||||||
|
|
||||||
let cpu = cpu_stats
|
let cpu = cpu_stats
|
||||||
.iter()
|
.iter()
|
||||||
.find(|c| c.container_id == Some(container.id.clone()))
|
.find(|c| {
|
||||||
|
c.container_id.as_ref()
|
||||||
|
.map(|id| id.starts_with(container_short_id))
|
||||||
|
.unwrap_or(false)
|
||||||
|
})
|
||||||
.cloned();
|
.cloned();
|
||||||
|
|
||||||
let network = net_stats
|
let network = net_stats
|
||||||
.iter()
|
.iter()
|
||||||
.find(|n| n.container_id == Some(container.id.clone()))
|
.find(|n| {
|
||||||
|
n.container_id.as_ref()
|
||||||
|
.map(|id| id.starts_with(container_short_id))
|
||||||
|
.unwrap_or(false)
|
||||||
|
})
|
||||||
.cloned();
|
.cloned();
|
||||||
|
|
||||||
let ram = mem_stats
|
let ram = mem_stats
|
||||||
.iter()
|
.iter()
|
||||||
.find(|m| m.container_id == Some(container.id.clone()))
|
.find(|m| {
|
||||||
|
m.container_id.as_ref()
|
||||||
|
.map(|id| id.starts_with(container_short_id))
|
||||||
|
.unwrap_or(false)
|
||||||
|
})
|
||||||
.cloned();
|
.cloned();
|
||||||
|
|
||||||
|
// Debug output for this container
|
||||||
|
if cpu.is_none() || network.is_none() || ram.is_none() {
|
||||||
|
println!("Debug: Container {} - CPU: {:?}, Network: {:?}, RAM: {:?}",
|
||||||
|
container_short_id, cpu.is_some(), network.is_some(), ram.is_some());
|
||||||
|
}
|
||||||
|
|
||||||
DockerContainerInfo {
|
DockerContainerInfo {
|
||||||
container: Some(container),
|
container: Some(container),
|
||||||
status: None, // Status can be fetched if needed
|
status: None,
|
||||||
cpu,
|
cpu,
|
||||||
network,
|
network,
|
||||||
ram,
|
ram,
|
||||||
@@ -160,7 +205,6 @@ impl DockerManager {
|
|||||||
let container_infos: Vec<DockerCollectMetricDto> = container_infos_total
|
let container_infos: Vec<DockerCollectMetricDto> = container_infos_total
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|info| {
|
.filter_map(|info| {
|
||||||
// Safely handle container extraction
|
|
||||||
let container = match info.container {
|
let container = match info.container {
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
None => {
|
None => {
|
||||||
@@ -190,13 +234,13 @@ impl DockerManager {
|
|||||||
// Safely handle network data with defaults
|
// Safely handle network data with defaults
|
||||||
let network_dto = if let Some(net) = info.network {
|
let network_dto = if let Some(net) = info.network {
|
||||||
DockerContainerNetworkDto {
|
DockerContainerNetworkDto {
|
||||||
net_in: net.rx_bytes.map(|bytes| bytes as f64).or(Some(0.0)),
|
net_in: net.rx_bytes.map(|bytes| bytes as f64),
|
||||||
net_out: net.tx_bytes.map(|bytes| bytes as f64).or(Some(0.0)),
|
net_out: net.tx_bytes.map(|bytes| bytes as f64),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
DockerContainerNetworkDto {
|
DockerContainerNetworkDto {
|
||||||
net_in: Some(0.0),
|
net_in: None,
|
||||||
net_out: Some(0.0),
|
net_out: None,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -229,6 +273,41 @@ impl DockerManager {
|
|||||||
|
|
||||||
Ok(dto)
|
Ok(dto)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Debug function to check stats collection for a specific container
|
||||||
|
pub async fn debug_container_stats(
|
||||||
|
&self,
|
||||||
|
container_id: &str
|
||||||
|
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
|
println!("=== DEBUG STATS FOR CONTAINER {} ===", container_id);
|
||||||
|
|
||||||
|
let (cpu_info, net_info, mem_info) = stats::get_single_container_stats(&self.docker, container_id).await?;
|
||||||
|
|
||||||
|
println!("CPU Info: {:?}", cpu_info);
|
||||||
|
println!("Network Info: {:?}", net_info);
|
||||||
|
println!("Memory Info: {:?}", mem_info);
|
||||||
|
|
||||||
|
// Also try the individual stats functions
|
||||||
|
println!("--- Individual CPU Stats ---");
|
||||||
|
match stats::cpu::get_single_container_cpu_stats(&self.docker, container_id).await {
|
||||||
|
Ok(cpu) => println!("CPU: {:?}", cpu),
|
||||||
|
Err(e) => println!("CPU Error: {}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("--- Individual Network Stats ---");
|
||||||
|
match stats::network::get_single_container_network_stats(&self.docker, container_id).await {
|
||||||
|
Ok(net) => println!("Network: {:?}", net),
|
||||||
|
Err(e) => println!("Network Error: {}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("--- Individual Memory Stats ---");
|
||||||
|
match stats::ram::get_single_container_memory_stats(&self.docker, container_id).await {
|
||||||
|
Ok(mem) => println!("Memory: {:?}", mem),
|
||||||
|
Err(e) => println!("Memory Error: {}", e),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
Reference in New Issue
Block a user