Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d37f08075 | |||
| 4d8b5c39e5 | |||
| 4e28d57460 | |||
| 6405ab16e4 |
@@ -207,16 +207,12 @@ pub async fn broadcast_docker_containers(
|
|||||||
loop {
|
loop {
|
||||||
println!("Attempting to broadcast containers...");
|
println!("Attempting to broadcast containers...");
|
||||||
|
|
||||||
let json_body = serde_json::to_string_pretty(&broadcast_data)?;
|
println!("📤 Docker-Services-json being posted:\n{}", serde_json::to_string_pretty(&broadcast_data)?);
|
||||||
println!("📤 JSON being posted:\n{}", json_body);
|
|
||||||
|
|
||||||
let url = format!("{}/monitoring/service-discovery", base_url);
|
let url = format!("{}/monitoring/service-discovery", base_url);
|
||||||
match client.post(&url).json(&container_dto).send().await {
|
match client.post(&url).json(&container_dto).send().await {
|
||||||
Ok(resp) if resp.status().is_success() => {
|
Ok(resp) if resp.status().is_success() => {
|
||||||
println!(
|
println!("✅ Successfully broadcasted docker services");
|
||||||
"✅ Successfully broadcasted following docker container: {:?}",
|
|
||||||
container_dto
|
|
||||||
);
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
@@ -284,7 +280,7 @@ pub async fn send_metrics(
|
|||||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let url = format!("{}/monitoring/metric", base_url);
|
let url = format!("{}/monitoring/metric", base_url);
|
||||||
println!("Metrics: {:?}", metrics);
|
println!("📤 System-Metrics-json being posted:\n{}", serde_json::to_string_pretty(&metrics)?);
|
||||||
|
|
||||||
match client.post(&url).json(&metrics).send().await {
|
match client.post(&url).json(&metrics).send().await {
|
||||||
Ok(res) => println!(
|
Ok(res) => println!(
|
||||||
@@ -424,6 +420,29 @@ pub async fn send_acknowledgment(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sends Docker container metrics to the backend monitoring endpoint.
|
||||||
|
///
|
||||||
|
/// This function asynchronously transmits Docker container statistics including
|
||||||
|
/// CPU usage, memory consumption, network I/O, and container status to the
|
||||||
|
/// backend server for monitoring and analysis.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `base_url` - The base URL of the backend server (e.g., "http://localhost:8080")
|
||||||
|
/// * `docker_metrics` - Reference to a [`DockerMetricDto`] containing container metrics data
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(())` - If the HTTP request was successfully sent (regardless of HTTP status code)
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If JSON serialization fails or other errors occur
|
||||||
|
///
|
||||||
|
/// # Behavior
|
||||||
|
///
|
||||||
|
/// - Constructs the full endpoint URL: `{base_url}/monitoring/docker-metric`
|
||||||
|
/// - Serializes the metrics data to pretty JSON for debugging output
|
||||||
|
/// - Sends a POST request with JSON content-type
|
||||||
|
/// - Logs success/failure messages to stdout/stderr
|
||||||
|
/// - Always returns `Ok(())` after request attempt (does not validate HTTP response status)
|
||||||
pub async fn send_docker_metrics(
|
pub async fn send_docker_metrics(
|
||||||
base_url: &str,
|
base_url: &str,
|
||||||
docker_metrics: &DockerMetricDto,
|
docker_metrics: &DockerMetricDto,
|
||||||
@@ -431,7 +450,7 @@ pub async fn send_docker_metrics(
|
|||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let url = format!("{}/monitoring/docker-metric", base_url);
|
let url = format!("{}/monitoring/docker-metric", base_url);
|
||||||
|
|
||||||
println!("Docker Metrics: {}", serde_json::to_string_pretty(&docker_metrics)?);
|
println!("📤 Docker-Metrics-json being posted:\n{}", serde_json::to_string_pretty(&docker_metrics)?);
|
||||||
|
|
||||||
match client.post(&url).json(&docker_metrics).send().await {
|
match client.post(&url).json(&docker_metrics).send().await {
|
||||||
Ok(res) => println!(
|
Ok(res) => println!(
|
||||||
|
|||||||
@@ -154,19 +154,6 @@ pub async fn restart_container(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
/// Extracts a Docker container ID from a string line.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
/// * `line` - The input string containing a container ID or related info.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// * `Option<String>` - The extracted container ID if found.
|
|
||||||
pub fn extract_client_container_id(line: &str) -> Option<String> {
|
|
||||||
// ...existing code...
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/// Gets network statistics for a specific container
|
/// Gets network statistics for a specific container
|
||||||
pub async fn get_network_stats(
|
pub async fn get_network_stats(
|
||||||
docker: &Docker,
|
docker: &Docker,
|
||||||
|
|||||||
@@ -35,7 +35,15 @@ impl Default for DockerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DockerManager {
|
impl DockerManager {
|
||||||
/// Creates a new DockerManager instance
|
/// Creates a new DockerManager instance with Docker connection
|
||||||
|
///
|
||||||
|
/// Establishes a connection to the local Docker daemon using default connection settings.
|
||||||
|
/// This is the primary constructor for the Docker manager.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(Self)` - Successfully connected Docker manager instance
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If Docker daemon is not running or connection fails
|
||||||
pub fn new() -> Result<Self, Box<dyn Error + Send + Sync>> {
|
pub fn new() -> Result<Self, Box<dyn Error + Send + Sync>> {
|
||||||
let docker = Docker::connect_with_local_defaults()
|
let docker = Docker::connect_with_local_defaults()
|
||||||
.map_err(|e| format!("Failed to connect to Docker: {}", e))?;
|
.map_err(|e| format!("Failed to connect to Docker: {}", e))?;
|
||||||
@@ -44,13 +52,31 @@ impl DockerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a DockerManager instance with optional Docker connection
|
/// Creates a DockerManager instance with optional Docker connection
|
||||||
|
///
|
||||||
|
/// Attempts to connect to Docker but returns None instead of an error if connection fails.
|
||||||
|
/// Useful for scenarios where Docker availability is optional.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Some(Self)` - If Docker connection is successful
|
||||||
|
/// * `None` - If Docker connection fails or Docker daemon is not available
|
||||||
pub fn new_optional() -> Option<Self> {
|
pub fn new_optional() -> Option<Self> {
|
||||||
Docker::connect_with_local_defaults()
|
Docker::connect_with_local_defaults()
|
||||||
.map(|docker| Self { docker })
|
.map(|docker| Self { docker })
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds the Docker container running the agent by image name
|
|
||||||
|
/// Finds the Docker container running the WatcherAgent by image name
|
||||||
|
///
|
||||||
|
/// Searches through all available containers to find the one running the agent.
|
||||||
|
/// Matches containers whose image name contains "watcher-agent".
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(Some(DockerContainer))` - Agent container found with full container details
|
||||||
|
/// * `Ok(None)` - No agent container found (agent may not be running in Docker)
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If container enumeration fails
|
||||||
pub async fn get_client_container(
|
pub async fn get_client_container(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
) -> Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
||||||
@@ -67,7 +93,14 @@ impl DockerManager {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the current client version (image name) if running in Docker
|
/// Gets the current client version from Docker image name
|
||||||
|
///
|
||||||
|
/// Extracts the image name (without tag) from the agent's container.
|
||||||
|
/// Returns "unknown" if the agent is not running in Docker or if version cannot be determined.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `String` - Image name portion before ':' tag, or "unknown" if not determinable
|
||||||
pub async fn get_client_version(&self) -> String {
|
pub async fn get_client_version(&self) -> String {
|
||||||
match self.get_client_container().await {
|
match self.get_client_container().await {
|
||||||
Ok(Some(container)) => container
|
Ok(Some(container)) => container
|
||||||
@@ -90,6 +123,13 @@ impl DockerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if Docker is available and the agent is running in a container
|
/// Checks if Docker is available and the agent is running in a container
|
||||||
|
///
|
||||||
|
/// Determines whether the agent is operating within a Docker container environment.
|
||||||
|
/// This is used to enable/disable Docker-specific functionality.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `bool` - True if agent is running in Docker container, false otherwise
|
||||||
pub async fn is_dockerized(&self) -> bool {
|
pub async fn is_dockerized(&self) -> bool {
|
||||||
self.get_client_container()
|
self.get_client_container()
|
||||||
.await
|
.await
|
||||||
@@ -97,7 +137,15 @@ impl DockerManager {
|
|||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets all available containers as DTOs for registration
|
/// Gets all available Docker containers as DTOs
|
||||||
|
///
|
||||||
|
/// Retrieves a list of all containers (running and stopped) and converts them
|
||||||
|
/// to simplified DTOs suitable for registration with the backend server.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(Vec<DockerContainer>)` - List of all containers with id, image, and name
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If container enumeration fails
|
||||||
pub async fn get_containers(
|
pub async fn get_containers(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<Vec<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
) -> Result<Vec<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
||||||
@@ -114,12 +162,32 @@ impl DockerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the number of running containers
|
/// Gets the number of running containers
|
||||||
|
///
|
||||||
|
/// Counts all containers that are currently available (both running and stopped).
|
||||||
|
/// This provides a quick overview of container density on the host.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(usize)` - Total number of containers
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If container counting fails
|
||||||
pub async fn get_container_count(&self) -> Result<usize, Box<dyn Error + Send + Sync>> {
|
pub async fn get_container_count(&self) -> Result<usize, 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.len())
|
Ok(containers.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Restarts a specific container by ID
|
/// Restarts a specific container by ID
|
||||||
|
///
|
||||||
|
/// Initiates a restart of the specified container. This is typically used
|
||||||
|
/// in response to backend commands for container management.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `container_id` - The full or partial container ID to restart
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(())` - Container restart successfully initiated
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If container not found or restart fails
|
||||||
pub async fn restart_container(
|
pub async fn restart_container(
|
||||||
&self,
|
&self,
|
||||||
container_id: &str,
|
container_id: &str,
|
||||||
@@ -127,7 +195,16 @@ impl DockerManager {
|
|||||||
container::restart_container(&self.docker, container_id).await
|
container::restart_container(&self.docker, container_id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Collects Docker metrics for all containers
|
/// Collects comprehensive Docker metrics for all containers
|
||||||
|
///
|
||||||
|
/// Gathers CPU usage, memory consumption, network I/O, and status information
|
||||||
|
/// for all containers. Matches statistics to containers using container ID prefixes.
|
||||||
|
/// Returns partial data even if some statistics collection fails.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(DockerMetricDto)` - Complete metrics data for all containers
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If critical failures occur
|
||||||
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?;
|
||||||
|
|
||||||
@@ -142,15 +219,6 @@ impl DockerManager {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
println!(
|
|
||||||
"Debug: Found {} containers, {} CPU stats, {} network stats, {} memory stats, {} status stats",
|
|
||||||
containers.len(),
|
|
||||||
cpu_stats.len(),
|
|
||||||
net_stats.len(),
|
|
||||||
mem_stats.len(),
|
|
||||||
status_stats.len(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let container_infos_total: Vec<_> = containers
|
let container_infos_total: Vec<_> = containers
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|container| {
|
.map(|container| {
|
||||||
@@ -238,7 +306,7 @@ 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| {
|
||||||
let _container = match info.container {
|
let container = match info.container {
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
None => {
|
None => {
|
||||||
eprintln!("Warning: Container info missing container data, skipping");
|
eprintln!("Warning: Container info missing container data, skipping");
|
||||||
@@ -286,7 +354,7 @@ impl DockerManager {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Some(DockerCollectMetricDto {
|
Some(DockerCollectMetricDto {
|
||||||
server_id: 0,
|
id: container.id,
|
||||||
status: status_dto,
|
status: status_dto,
|
||||||
cpu: cpu_dto,
|
cpu: cpu_dto,
|
||||||
ram: ram_dto,
|
ram: ram_dto,
|
||||||
@@ -303,6 +371,15 @@ impl DockerManager {
|
|||||||
Ok(dto)
|
Ok(dto)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates registration DTO with container information
|
||||||
|
///
|
||||||
|
/// Prepares container data for initial registration with the backend server.
|
||||||
|
/// This includes basic container information without detailed metrics.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(DockerServiceDto)` - Container information ready for registration
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If container enumeration fails
|
||||||
pub async fn create_registration_dto(
|
pub async fn create_registration_dto(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<DockerServiceDto, Box<dyn Error + Send + Sync>> {
|
) -> Result<DockerServiceDto, Box<dyn Error + Send + Sync>> {
|
||||||
@@ -322,16 +399,33 @@ 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 DockerContainer {
|
impl DockerContainer {
|
||||||
/// Returns the container ID
|
/// Returns the container ID
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `&str` - Full container ID string
|
||||||
pub fn id(&self) -> &str {
|
pub fn id(&self) -> &str {
|
||||||
&self.id
|
&self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the image name
|
/// Returns the container image name
|
||||||
|
///
|
||||||
|
/// Returns "unknown" if the image name is not available.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `&str` - Image name or "unknown" if not available
|
||||||
pub fn image(&self) -> &str {
|
pub fn image(&self) -> &str {
|
||||||
&self.image.as_deref().unwrap_or("unknown")
|
&self.image.as_deref().unwrap_or("unknown")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the container name
|
/// Returns the container name
|
||||||
|
///
|
||||||
|
/// Returns "unknown" if the container name is not available.
|
||||||
|
/// Container names typically start with '/' in Docker.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `&str` - Container name or "unknown" if not available
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &str {
|
||||||
&self.name.as_deref().unwrap_or("unknown")
|
&self.name.as_deref().unwrap_or("unknown")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,19 +51,19 @@ pub async fn get_single_container_cpu_stats(
|
|||||||
if let (Some(cpu_usage), Some(pre_cpu_usage)) =
|
if let (Some(cpu_usage), Some(pre_cpu_usage)) =
|
||||||
(&cpu_stats.cpu_usage, &precpu_stats.cpu_usage)
|
(&cpu_stats.cpu_usage, &precpu_stats.cpu_usage)
|
||||||
{
|
{
|
||||||
let cpu_delta = cpu_usage
|
let cpu_delta: f64 = cpu_usage
|
||||||
.total_usage
|
.total_usage
|
||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
.saturating_sub(pre_cpu_usage.total_usage.unwrap_or(0));
|
.saturating_sub(pre_cpu_usage.total_usage.unwrap_or(0)) as f64;
|
||||||
|
|
||||||
let system_delta = cpu_stats
|
let system_delta: f64 = cpu_stats
|
||||||
.system_cpu_usage
|
.system_cpu_usage
|
||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
.saturating_sub(precpu_stats.system_cpu_usage.unwrap_or(0));
|
.saturating_sub(precpu_stats.system_cpu_usage.unwrap_or(0)) as f64;
|
||||||
|
|
||||||
let online_cpus = cpu_stats.online_cpus.unwrap_or(1);
|
let online_cpus = cpu_stats.online_cpus.unwrap_or(1);
|
||||||
|
|
||||||
let cpu_percent = if system_delta > 0 && online_cpus > 0 {
|
let cpu_percent = if system_delta > 0.0 && online_cpus > 0 {
|
||||||
(cpu_delta as f64 / system_delta as f64) * online_cpus as f64 * 100.0
|
(cpu_delta as f64 / system_delta as f64) * online_cpus as f64 * 100.0
|
||||||
} else {
|
} else {
|
||||||
0.0
|
0.0
|
||||||
|
|||||||
@@ -46,6 +46,20 @@ use bollard::Docker;
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
/// Get container statistics for all containers using an existing Docker client
|
/// Get container statistics for all containers using an existing Docker client
|
||||||
|
///
|
||||||
|
/// Collects comprehensive statistics for all containers including CPU usage, network I/O,
|
||||||
|
/// memory consumption, and container status. This is the primary function for gathering
|
||||||
|
/// complete container metrics in a single operation.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `docker` - Reference to connected Docker client
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok((Vec<ContainerCpuInfo>, Vec<ContainerNetworkInfo>, Vec<ContainerMemoryInfo>, Vec<ContainerStatusInfo>))` -
|
||||||
|
/// Tuple containing vectors of CPU, network, memory, and status information for all containers
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If any statistics collection fails
|
||||||
pub async fn get_container_stats(
|
pub async fn get_container_stats(
|
||||||
docker: &Docker,
|
docker: &Docker,
|
||||||
) -> Result<
|
) -> Result<
|
||||||
@@ -66,6 +80,20 @@ pub async fn get_container_stats(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get container statistics for a specific container
|
/// Get container statistics for a specific container
|
||||||
|
///
|
||||||
|
/// Retrieves detailed metrics for a single container identified by its ID.
|
||||||
|
/// Useful for targeted monitoring or when responding to container-specific commands.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `docker` - Reference to connected Docker client
|
||||||
|
/// * `container_id` - The ID of the container to inspect
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok((Option<ContainerCpuInfo>, Option<ContainerNetworkInfo>, Option<ContainerMemoryInfo>, Option<ContainerStatusInfo>))` -
|
||||||
|
/// Tuple containing optional CPU, network, memory, and status information for the specified container
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If statistics collection fails
|
||||||
pub async fn get_single_container_stats(
|
pub async fn get_single_container_stats(
|
||||||
docker: &Docker,
|
docker: &Docker,
|
||||||
container_id: &str,
|
container_id: &str,
|
||||||
@@ -84,6 +112,18 @@ pub async fn get_single_container_stats(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get total network statistics across all containers
|
/// Get total network statistics across all containers
|
||||||
|
///
|
||||||
|
/// Calculates the sum of network receive and transmit bytes across all containers.
|
||||||
|
/// This provides an overview of total network traffic generated by all containers.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `docker` - Reference to connected Docker client
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok((u64, u64))` - Tuple containing total received bytes and total transmitted bytes
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If network statistics collection fails
|
||||||
pub async fn get_total_network_stats(
|
pub async fn get_total_network_stats(
|
||||||
docker: &Docker,
|
docker: &Docker,
|
||||||
) -> Result<(u64, u64), Box<dyn Error + Send + Sync>> {
|
) -> Result<(u64, u64), Box<dyn Error + Send + Sync>> {
|
||||||
@@ -91,11 +131,35 @@ pub async fn get_total_network_stats(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get average CPU usage across all containers
|
/// Get average CPU usage across all containers
|
||||||
|
///
|
||||||
|
/// Calculates the average CPU usage percentage across all running containers.
|
||||||
|
/// This provides a high-level overview of container CPU utilization.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `docker` - Reference to connected Docker client
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(f64)` - Average CPU usage percentage across all containers (0.0-100.0)
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If CPU statistics collection fails
|
||||||
pub async fn get_average_cpu_usage(docker: &Docker) -> Result<f64, Box<dyn Error + Send + Sync>> {
|
pub async fn get_average_cpu_usage(docker: &Docker) -> Result<f64, Box<dyn Error + Send + Sync>> {
|
||||||
cpu::get_average_cpu_usage(docker).await
|
cpu::get_average_cpu_usage(docker).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get total memory usage across all containers
|
/// Get total memory usage across all containers
|
||||||
|
///
|
||||||
|
/// Calculates the sum of memory usage across all containers.
|
||||||
|
/// This provides an overview of total memory consumption by all containers.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `docker` - Reference to connected Docker client
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(u64)` - Total memory usage in bytes across all containers
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If memory statistics collection fails
|
||||||
pub async fn get_total_memory_usage(docker: &Docker) -> Result<u64, Box<dyn Error + Send + Sync>> {
|
pub async fn get_total_memory_usage(docker: &Docker) -> Result<u64, Box<dyn Error + Send + Sync>> {
|
||||||
ram::get_total_memory_usage(docker).await
|
ram::get_total_memory_usage(docker).await
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,22 @@ use bollard::query_parameters::{ListContainersOptions, InspectContainerOptions};
|
|||||||
use bollard::models::{ContainerSummaryStateEnum, ContainerStateStatusEnum};
|
use bollard::models::{ContainerSummaryStateEnum, ContainerStateStatusEnum};
|
||||||
|
|
||||||
/// Get status information for all containers
|
/// Get status information for all containers
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `docker` - Reference to Docker client
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(Vec<ContainerStatusInfo>)` - Vector of container status information
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If Docker API call fails
|
||||||
|
///
|
||||||
|
/// # Behavior
|
||||||
|
///
|
||||||
|
/// - Lists all containers (including stopped ones)
|
||||||
|
/// - Converts container state enum to string representation
|
||||||
|
/// - Converts timestamp from i64 to string
|
||||||
|
/// - Returns basic status info (finished_at is not available in list view)
|
||||||
pub async fn get_all_containers_status(
|
pub async fn get_all_containers_status(
|
||||||
docker: &Docker,
|
docker: &Docker,
|
||||||
) -> Result<Vec<ContainerStatusInfo>, Box<dyn Error + Send + Sync>> {
|
) -> Result<Vec<ContainerStatusInfo>, Box<dyn Error + Send + Sync>> {
|
||||||
@@ -52,7 +68,26 @@ pub async fn get_all_containers_status(
|
|||||||
Ok(status_infos)
|
Ok(status_infos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Get status information for a specific container
|
/// Get status information for a specific container
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `docker` - Reference to Docker client
|
||||||
|
/// * `container_id` - ID of the container to inspect
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(Some(ContainerStatusInfo))` - Status info if container found
|
||||||
|
/// * `Ok(None)` - If container not found
|
||||||
|
/// * `Err(Box<dyn Error + Send + Sync>)` - If Docker API call fails
|
||||||
|
///
|
||||||
|
/// # Behavior
|
||||||
|
///
|
||||||
|
/// - First tries to find container in list (faster)
|
||||||
|
/// - Falls back to container inspect for detailed info
|
||||||
|
/// - Provides more detailed information including finished_at timestamp
|
||||||
|
/// - Handles container not found case gracefully
|
||||||
pub async fn get_single_container_status(
|
pub async fn get_single_container_status(
|
||||||
docker: &Docker,
|
docker: &Docker,
|
||||||
container_id: &str,
|
container_id: &str,
|
||||||
|
|||||||
@@ -63,31 +63,31 @@ pub struct MetricDto {
|
|||||||
pub server_id: u16,
|
pub server_id: u16,
|
||||||
#[serde(rename = "ipAddress")]
|
#[serde(rename = "ipAddress")]
|
||||||
pub ip_address: String,
|
pub ip_address: String,
|
||||||
#[serde(rename = "cpu_Load")]
|
#[serde(rename = "cpuLoad")]
|
||||||
pub cpu_load: f64,
|
pub cpu_load: f64,
|
||||||
#[serde(rename = "cpu_Temp")]
|
#[serde(rename = "cpuTemp")]
|
||||||
pub cpu_temp: f64,
|
pub cpu_temp: f64,
|
||||||
#[serde(rename = "gpu_Load")]
|
#[serde(rename = "gpuLoad")]
|
||||||
pub gpu_load: f64,
|
pub gpu_load: f64,
|
||||||
#[serde(rename = "gpu_Temp")]
|
#[serde(rename = "gpuTemp")]
|
||||||
pub gpu_temp: f64,
|
pub gpu_temp: f64,
|
||||||
#[serde(rename = "gpu_Vram_Size")]
|
#[serde(rename = "gpuVramSize")]
|
||||||
pub gpu_vram_size: f64,
|
pub gpu_vram_size: f64,
|
||||||
#[serde(rename = "gpu_Vram_Load")]
|
#[serde(rename = "gpuVramLoad")]
|
||||||
pub gpu_vram_load: f64,
|
pub gpu_vram_load: f64,
|
||||||
#[serde(rename = "ram_Load")]
|
#[serde(rename = "ramLoad")]
|
||||||
pub ram_load: f64,
|
pub ram_load: f64,
|
||||||
#[serde(rename = "ram_Size")]
|
#[serde(rename = "ramSize")]
|
||||||
pub ram_size: f64,
|
pub ram_size: f64,
|
||||||
#[serde(rename = "disk_Size")]
|
#[serde(rename = "diskSize")]
|
||||||
pub disk_size: f64,
|
pub disk_size: f64,
|
||||||
#[serde(rename = "disk_Usage")]
|
#[serde(rename = "diskUsage")]
|
||||||
pub disk_usage: f64,
|
pub disk_usage: f64,
|
||||||
#[serde(rename = "disk_Temp")]
|
#[serde(rename = "diskTemp")]
|
||||||
pub disk_temp: f64,
|
pub disk_temp: f64,
|
||||||
#[serde(rename = "net_In")]
|
#[serde(rename = "netIn")]
|
||||||
pub net_rx: f64,
|
pub net_rx: f64,
|
||||||
#[serde(rename = "net_Out")]
|
#[serde(rename = "netOut")]
|
||||||
pub net_tx: f64,
|
pub net_tx: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ pub struct IdResponse {
|
|||||||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct HeartbeatDto {
|
pub struct HeartbeatDto {
|
||||||
#[serde(rename = "IpAddress")]
|
#[serde(rename = "ipAddress")]
|
||||||
pub ip_address: String,
|
pub ip_address: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,10 +142,15 @@ pub struct HeartbeatDto {
|
|||||||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
pub struct HardwareDto {
|
pub struct HardwareDto {
|
||||||
|
#[serde(rename = "cpuType")]
|
||||||
pub cpu_type: String,
|
pub cpu_type: String,
|
||||||
|
#[serde(rename = "cpuCore")]
|
||||||
pub cpu_cores: i32,
|
pub cpu_cores: i32,
|
||||||
|
#[serde(rename = "gpuType")]
|
||||||
pub gpu_type: String,
|
pub gpu_type: String,
|
||||||
|
#[serde(rename = "ramSize")]
|
||||||
pub ram_size: f64,
|
pub ram_size: f64,
|
||||||
|
#[serde(rename = "ipAddress")]
|
||||||
pub ip_address: String,
|
pub ip_address: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +191,7 @@ pub struct Acknowledgment {
|
|||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct DockerServiceDto {
|
pub struct DockerServiceDto {
|
||||||
/// Unique server identifier (integer)
|
/// Unique server identifier (integer)
|
||||||
#[serde(rename = "Server_id")]
|
#[serde(rename = "serverId")]
|
||||||
pub server_id: u16,
|
pub server_id: u16,
|
||||||
/// Number of currently running containers
|
/// Number of currently running containers
|
||||||
// pub container_count: usize, --- IGNORE ---
|
// pub container_count: usize, --- IGNORE ---
|
||||||
@@ -199,12 +204,13 @@ pub struct DockerServiceDto {
|
|||||||
/// id: unique container ID (first 12 hex digits)
|
/// id: unique container ID (first 12 hex digits)
|
||||||
/// image: docker image name
|
/// image: docker image name
|
||||||
/// name: container name
|
/// name: container name
|
||||||
#[serde(rename = "Containers")]
|
#[serde(rename = "containers")]
|
||||||
pub containers: Value, // Vec<DockerContainer>,
|
pub containers: Value, // Vec<DockerContainer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct DockerMetricDto {
|
pub struct DockerMetricDto {
|
||||||
|
#[serde(rename = "serverId")]
|
||||||
pub server_id: u16,
|
pub server_id: u16,
|
||||||
/// json stringified array of DockerContainer
|
/// json stringified array of DockerContainer
|
||||||
///
|
///
|
||||||
@@ -219,13 +225,14 @@ pub struct DockerMetricDto {
|
|||||||
/// network: network stats
|
/// network: network stats
|
||||||
/// cpu: cpu stats
|
/// cpu: cpu stats
|
||||||
/// ram: ram stats
|
/// ram: ram stats
|
||||||
|
#[serde(rename = "containers")]
|
||||||
pub containers: Value, // Vec<DockerContainerInfo>,
|
pub containers: Value, // Vec<DockerContainerInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
|
|
||||||
pub struct DockerCollectMetricDto {
|
pub struct DockerCollectMetricDto {
|
||||||
pub server_id: u16,
|
pub id: String,
|
||||||
pub status: DockerContainerStatusDto,
|
pub status: DockerContainerStatusDto,
|
||||||
pub cpu: DockerContainerCpuDto,
|
pub cpu: DockerContainerCpuDto,
|
||||||
pub ram: DockerContainerRamDto,
|
pub ram: DockerContainerRamDto,
|
||||||
@@ -239,18 +246,22 @@ pub struct DockerContainerStatusDto {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct DockerContainerCpuDto {
|
pub struct DockerContainerCpuDto {
|
||||||
|
#[serde(rename = "cpuLoad")]
|
||||||
pub cpu_load: Option<f64>,
|
pub cpu_load: Option<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
pub struct DockerContainerRamDto {
|
pub struct DockerContainerRamDto {
|
||||||
|
#[serde(rename = "ramLoad")]
|
||||||
pub ram_load: Option<f64>,
|
pub ram_load: Option<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Clone)]
|
#[derive(Debug, Serialize, Clone)]
|
||||||
|
|
||||||
pub struct DockerContainerNetworkDto {
|
pub struct DockerContainerNetworkDto {
|
||||||
|
#[serde(rename = "netIn")]
|
||||||
pub net_in: Option<f64>,
|
pub net_in: Option<f64>,
|
||||||
|
#[serde(rename = "netOut")]
|
||||||
pub net_out: Option<f64>,
|
pub net_out: Option<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
networks:
|
|
||||||
watcher-network:
|
|
||||||
driver: bridge
|
|
||||||
|
|
||||||
services:
|
|
||||||
watcher:
|
|
||||||
image: git.triggermeelmo.com/watcher/watcher-server:v0.1.11
|
|
||||||
container_name: watcher
|
|
||||||
deploy:
|
|
||||||
resources:
|
|
||||||
limits:
|
|
||||||
memory: 200M
|
|
||||||
restart: unless-stopped
|
|
||||||
env_file: .env
|
|
||||||
ports:
|
|
||||||
- "5000:5000"
|
|
||||||
volumes:
|
|
||||||
- ./watcher-volumes/data:/app/persistence
|
|
||||||
- ./watcher-volumes/dumps:/app/wwwroot/downloads/sqlite
|
|
||||||
- ./watcher-volumes/logs:/app/logs
|
|
||||||
Reference in New Issue
Block a user