Compare commits
3 Commits
v0.1.25
...
8c1ef7f9f6
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c1ef7f9f6 | |||
| 16020eea50 | |||
| 432a798210 |
@@ -12,7 +12,6 @@
|
||||
/// These functions are called from the main agent loop and background tasks. All network operations are asynchronous and robust to transient failures.
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::docker::container;
|
||||
use crate::docker::serverclientcomm::handle_server_message;
|
||||
use crate::hardware::HardwareInfo;
|
||||
use crate::models::{
|
||||
|
||||
@@ -159,43 +159,58 @@ impl DockerManager {
|
||||
|
||||
let container_infos: Vec<DockerCollectMetricDto> = container_infos_total
|
||||
.into_iter()
|
||||
.map(|info| DockerCollectMetricDto {
|
||||
id: Some(info.container.unwrap().id).unwrap_or("".to_string()),
|
||||
cpu: info
|
||||
.cpu
|
||||
.unwrap()
|
||||
.cpu_usage_percent
|
||||
.map(|load| DockerContainerCpuDto {
|
||||
cpu_load: Some(load),
|
||||
.filter_map(|info| {
|
||||
// Safely handle container extraction
|
||||
let container = match info.container {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
eprintln!("Warning: Container info missing container data, skipping");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
// Safely handle CPU data with defaults
|
||||
let cpu_dto = if let Some(cpu) = info.cpu {
|
||||
DockerContainerCpuDto {
|
||||
cpu_load: cpu.cpu_usage_percent,
|
||||
}
|
||||
} else {
|
||||
DockerContainerCpuDto { cpu_load: None }
|
||||
};
|
||||
|
||||
// Safely handle RAM data with defaults
|
||||
let ram_dto = if let Some(ram) = info.ram {
|
||||
DockerContainerRamDto {
|
||||
ram_load: ram.memory_usage_percent,
|
||||
}
|
||||
} else {
|
||||
DockerContainerRamDto { ram_load: None }
|
||||
};
|
||||
|
||||
// Safely handle network data with defaults
|
||||
let network_dto = if let Some(net) = info.network {
|
||||
DockerContainerNetworkDto {
|
||||
net_in: net.rx_bytes.map(|bytes| bytes as f64).or(Some(0.0)),
|
||||
net_out: net.tx_bytes.map(|bytes| bytes as f64).or(Some(0.0)),
|
||||
}
|
||||
} else {
|
||||
DockerContainerNetworkDto {
|
||||
net_in: Some(0.0),
|
||||
net_out: Some(0.0),
|
||||
}
|
||||
};
|
||||
|
||||
Some(DockerCollectMetricDto {
|
||||
id: container.id,
|
||||
cpu: cpu_dto,
|
||||
ram: ram_dto,
|
||||
network: network_dto,
|
||||
})
|
||||
.unwrap_or(DockerContainerCpuDto { cpu_load: None }),
|
||||
ram: info
|
||||
.ram
|
||||
.unwrap()
|
||||
.memory_usage_percent
|
||||
.map(|load| DockerContainerRamDto {
|
||||
cpu_load: Some(load),
|
||||
})
|
||||
.unwrap_or(DockerContainerRamDto { cpu_load: None }),
|
||||
network: DockerContainerNetworkDto {
|
||||
net_in: info
|
||||
.network
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.rx_bytes
|
||||
.map(|bytes| bytes as f64)
|
||||
.or(Some(0.0)),
|
||||
net_out: info
|
||||
.network
|
||||
.unwrap()
|
||||
.tx_bytes
|
||||
.map(|bytes| bytes as f64)
|
||||
.or(Some(0.0)),
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
|
||||
let dto = DockerMetricDto {
|
||||
server_id: 0,
|
||||
server_id: 0, // This should be set by the caller
|
||||
containers: serde_json::to_string(&container_infos)?,
|
||||
};
|
||||
|
||||
@@ -207,9 +222,9 @@ impl DockerManager {
|
||||
) -> Result<DockerRegistrationDto, Box<dyn Error + Send + Sync>> {
|
||||
let containers = self.get_containers().await?;
|
||||
let dto = DockerRegistrationDto {
|
||||
server_id: 0,
|
||||
//container_count,
|
||||
containers: serde_json::to_string(&containers)?,
|
||||
server_id: 0, // This will be set by the caller
|
||||
containers: serde_json::to_string(&containers)
|
||||
.unwrap_or_else(|_| "[]".to_string()), // Fallback to empty array
|
||||
};
|
||||
|
||||
Ok(dto)
|
||||
|
||||
@@ -31,10 +31,8 @@ pub mod hardware;
|
||||
pub mod metrics;
|
||||
pub mod models;
|
||||
|
||||
use bollard::Docker;
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
/// Awaits a spawned asynchronous task and flattens its nested `Result` type.
|
||||
@@ -153,7 +151,13 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let docker_manager = docker_manager.as_ref().cloned().unwrap();
|
||||
async move {
|
||||
let mut collector = metrics::Collector::new(server_id, ip, docker_manager);
|
||||
collector.run(&server_url).await
|
||||
if let Err(e) = collector.run(&server_url).await {
|
||||
eprintln!("Metrics collection error: {}", e);
|
||||
// Don't panic, just return the error
|
||||
Err(e)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -182,9 +182,6 @@ pub struct Acknowledgment {
|
||||
/// - `image`: Docker image name (string)
|
||||
/// - `Name`: Container name (string)
|
||||
/// - `Status`: Container status ("running", "stopped", etc.)
|
||||
/// - `_net_in`: Network receive 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**)
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerRegistrationDto {
|
||||
/// Unique server identifier (integer)
|
||||
@@ -238,7 +235,7 @@ pub struct DockerContainerCpuDto {
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerContainerRamDto {
|
||||
pub cpu_load: Option<f64>,
|
||||
pub ram_load: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
|
||||
Reference in New Issue
Block a user