Compare commits
85 Commits
main
...
2a4cc4b2d5
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a4cc4b2d5 | |||
| c36b17fa05 | |||
| 375b4450f0 | |||
| b134be4c88 | |||
| 6afd5d0fcd | |||
| e02914516d | |||
| bf90d3ceb9 | |||
| a8ccb0521a | |||
| c90a276dca | |||
| dc4c23f9d9 | |||
| 3182d57539 | |||
| 8c1ef7f9f6 | |||
| 16020eea50 | |||
| 432a798210 | |||
| a095444222 | |||
| 5e7bc3df54 | |||
| 1c7a169956 | |||
| c7bce926e9 | |||
| 711083daa0 | |||
| 06cec6ff9f | |||
| a7cae5e93f | |||
| 66428863e6 | |||
| b35cac0dbe | |||
| bb55b46c34 | |||
| 76d54cb433 | |||
| 4dc8c56a5c | |||
| c81040b16b | |||
| 68c307e258 | |||
| 5d00f072d8 | |||
| 9072e253ec | |||
| 063ad113d7 | |||
| 097be0f555 | |||
| c7b8e24a54 | |||
| fb1d016b36 | |||
| 89d58e9696 | |||
| 45c6e8180c | |||
| c395885bbd | |||
| 75494e2a71 | |||
| 8e6e291f6a | |||
| bfeb43f38a | |||
| 2f5e2391f7 | |||
| 820952089a | |||
| c745125f20 | |||
| 758fa7608f | |||
| ee6b947f29 | |||
| 18dd1ef528 | |||
| 8fa7866cc2 | |||
| 238ad87119 | |||
| d584e21fd9 | |||
| ac79a2e0b7 | |||
| 1798c1270b | |||
| 97d1019b69 | |||
| e30ceb75d9 | |||
| 4681e0c694 | |||
| 49f1af392d | |||
| f78e48900a | |||
| 8c49a63a50 | |||
| d994be757e | |||
| d7a58e00da | |||
| d2efc64487 | |||
| 1f23c303c1 | |||
| 1cc85bfa14 | |||
| 8bac357dc6 | |||
| 7154c01f7a | |||
| 813bf4e407 | |||
| bf6f89c954 | |||
| dbe87fedb6 | |||
| 67b24b33aa | |||
| 67ebbdaa19 | |||
| 6fd275802c | |||
| 9018adf998 | |||
| 3124697f10 | |||
| 30382fedef | |||
| 8910155524 | |||
| 7a68df41ac | |||
| 60ce51cd82 | |||
| 54fca8b1d3 | |||
| aa876d9e5d | |||
| 88625ff986 | |||
| 428be53fff | |||
| 83cb815e76 | |||
| 755617c86f | |||
| 314bf8c327 | |||
|
|
0fb3ccd506 | ||
|
|
a6ef23fd69 |
@@ -3,17 +3,12 @@ name: Rust Cross-Platform Build
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches: [ "development", "main", "feature/*", "bugfix/*", "enhancement/*" ]
|
||||
branches: [ "development", "main", "staging" ]
|
||||
tags: [ "v*.*.*" ]
|
||||
pull_request:
|
||||
branches: [ "development", "main" ]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
RUST_BACKTRACE: 1
|
||||
REGISTRY: git.triggermeelmo.com
|
||||
IMAGE_NAME: donpat1to/watcher-agent
|
||||
TAG: ${{ github.ref == 'refs/heads/main' && 'latest' || github.ref == 'refs/heads/development' && 'development' || github.ref_type == 'tag' && github.ref_name || 'pr' }}
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
@@ -54,24 +49,9 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
setup-rust:
|
||||
name: Setup Rust Toolchain
|
||||
needs: detect-project
|
||||
if: ${{ !failure() && !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: stable
|
||||
targets: x86_64-unknown-linux-gnu, x86_64-pc-windows-gnu
|
||||
components: rustfmt, clippy
|
||||
|
||||
test:
|
||||
name: Run Tests
|
||||
needs: [detect-project, setup-rust]
|
||||
needs: [detect-project]
|
||||
if: ${{ !failure() && !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
@@ -94,45 +74,9 @@ jobs:
|
||||
# working-directory: ${{ needs.detect-project.outputs.project-dir }}
|
||||
# run: cargo clippy -- -D warnings
|
||||
|
||||
set-tag:
|
||||
name: Set Tag Name
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
tag_name: ${{ steps.set_tag.outputs.tag_name }}
|
||||
steps:
|
||||
- name: Determine next semantic version tag
|
||||
id: set_tag
|
||||
run: |
|
||||
git fetch --tags
|
||||
|
||||
# Find latest tag matching vX.Y.Z
|
||||
latest_tag=$(git tag --list 'v*.*.*' --sort=-v:refname | head -n 1)
|
||||
if [[ -z "$latest_tag" ]]; then
|
||||
major=0
|
||||
minor=0
|
||||
patch=0
|
||||
else
|
||||
version="${latest_tag#v}"
|
||||
IFS='.' read -r major minor patch <<< "$version"
|
||||
fi
|
||||
|
||||
if [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then
|
||||
major=$((major + 1))
|
||||
minor=0
|
||||
patch=0
|
||||
elif [[ "${GITHUB_REF}" == "refs/heads/development" ]]; then
|
||||
minor=$((minor + 1))
|
||||
patch=0
|
||||
else
|
||||
patch=$((patch + 1))
|
||||
fi
|
||||
|
||||
new_tag="v${major}.${minor}.${patch}"
|
||||
echo "tag_name=${new_tag}" >> $GITHUB_OUTPUT
|
||||
|
||||
# audit:
|
||||
# name: Security Audit
|
||||
# needs: [detect-project, setup-rust]
|
||||
# needs: [detect-project]
|
||||
# if: ${{ !failure() && !cancelled() }}
|
||||
# runs-on: ubuntu-latest
|
||||
# steps:
|
||||
@@ -152,7 +96,7 @@ jobs:
|
||||
|
||||
build:
|
||||
name: Build (${{ matrix.target }})
|
||||
needs: [detect-project, setup-rust, test, audit]
|
||||
needs: [detect-project, test]
|
||||
if: ${{ !failure() && !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
@@ -205,13 +149,65 @@ jobs:
|
||||
path: |
|
||||
${{ needs.detect-project.outputs.project-dir }}/target/${{ matrix.target }}/release/${{ needs.detect-project.outputs.project-name }}${{ matrix.os == 'windows' && '.exe' || '' }}
|
||||
|
||||
set-tag:
|
||||
name: Set Tag Name
|
||||
needs: [detect-project, build]
|
||||
#if: ${{ !failure() && !cancelled() && github.event_name != 'pull_request' }}
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
tag_name: ${{ steps.set_tag.outputs.tag_name }}
|
||||
should_tag: ${{ steps.set_tag.outputs.should_tag }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Determine next semantic version tag
|
||||
id: set_tag
|
||||
run: |
|
||||
git fetch --tags
|
||||
|
||||
# Find latest tag matching vX.Y.Z
|
||||
latest_tag=$(git tag --list 'v*.*.*' --sort=-v:refname | head -n 1)
|
||||
if [[ -z "$latest_tag" ]]; then
|
||||
major=0
|
||||
minor=0
|
||||
patch=0
|
||||
else
|
||||
version="${latest_tag#v}"
|
||||
IFS='.' read -r major minor patch <<< "$version"
|
||||
fi
|
||||
|
||||
if [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then
|
||||
major=$((major + 1))
|
||||
minor=0
|
||||
patch=0
|
||||
new_tag="v${major}.${minor}.${patch}"
|
||||
echo "tag_name=${new_tag}" >> $GITHUB_OUTPUT
|
||||
echo "should_tag=true" >> $GITHUB_OUTPUT
|
||||
echo "Creating new major version tag: ${new_tag}"
|
||||
|
||||
elif [[ "${GITHUB_REF}" == "refs/heads/development" ]]; then
|
||||
minor=$((minor + 1))
|
||||
patch=0
|
||||
new_tag="v${major}.${minor}.${patch}"
|
||||
echo "tag_name=${new_tag}" >> $GITHUB_OUTPUT
|
||||
echo "should_tag=true" >> $GITHUB_OUTPUT
|
||||
echo "Creating new minor version tag: ${new_tag}"
|
||||
|
||||
elif [[ "${GITHUB_REF}" == "refs/heads/staging" ]]; then
|
||||
patch=$((patch + 1))
|
||||
new_tag="v${major}.${minor}.${patch}"
|
||||
echo "tag_name=${new_tag}" >> $GITHUB_OUTPUT
|
||||
echo "should_tag=true" >> $GITHUB_OUTPUT
|
||||
echo "Creating new patch version tag: ${new_tag}"
|
||||
fi
|
||||
|
||||
docker-build:
|
||||
name: Build and Push Docker Image
|
||||
needs: [detect-project, build, set-tag]
|
||||
if: |
|
||||
always() &&
|
||||
needs.detect-project.result == 'success' &&
|
||||
needs.build.result == 'success' &&
|
||||
needs.set-tag.outputs.should_tag == 'true' &&
|
||||
github.event_name != 'pull_request'
|
||||
runs-on: ubuntu-latest
|
||||
environment: production
|
||||
@@ -225,14 +221,14 @@ jobs:
|
||||
path: dist/
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Login to Docker Registry
|
||||
uses: docker/login-action@v2
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
username: ${{ secrets.AUTOMATION_USERNAME }}
|
||||
password: ${{ secrets.AUTOMATION_PASSWORD }}
|
||||
|
||||
- name: Build Docker image
|
||||
uses: docker/build-push-action@v4
|
||||
@@ -248,8 +244,7 @@ jobs:
|
||||
|
||||
tag:
|
||||
name: Create Tag
|
||||
needs: [build, set-tag]
|
||||
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||||
needs: [docker-build, build, set-tag]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
@@ -265,13 +260,13 @@ jobs:
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
echo "Creating new tag: ${{ needs.set-tag.outputs.tag_name }}"
|
||||
git tag ${{ needs.set-tag.outputs.tag_name }}
|
||||
git push origin ${{ needs.set-tag.outputs.tag_name }}
|
||||
|
||||
|
||||
summary:
|
||||
name: Workflow Summary
|
||||
needs: [test, audit, build, docker-build]
|
||||
needs: [test, build, docker-build]
|
||||
if: always()
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -17,6 +17,9 @@ Cargo.lock
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
.env
|
||||
watcher-volumes
|
||||
|
||||
# RustRover
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
|
||||
578
WatcherAgent/-
Normal file
578
WatcherAgent/-
Normal file
@@ -0,0 +1,578 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# This script should be run via curl:
|
||||
# sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
||||
# or via wget:
|
||||
# sh -c "$(wget -qO- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
||||
# or via fetch:
|
||||
# sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
||||
#
|
||||
# As an alternative, you can first download the install script and run it afterwards:
|
||||
# wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
|
||||
# sh install.sh
|
||||
#
|
||||
# You can tweak the install behavior by setting variables when running the script. For
|
||||
# example, to change the path to the Oh My Zsh repository:
|
||||
# ZSH=~/.zsh sh install.sh
|
||||
#
|
||||
# Respects the following environment variables:
|
||||
# ZDOTDIR - path to Zsh dotfiles directory (default: unset). See [1][2]
|
||||
# [1] https://zsh.sourceforge.io/Doc/Release/Parameters.html#index-ZDOTDIR
|
||||
# [2] https://zsh.sourceforge.io/Doc/Release/Files.html#index-ZDOTDIR_002c-use-of
|
||||
# ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
|
||||
# REPO - name of the GitHub repo to install from (default: ohmyzsh/ohmyzsh)
|
||||
# REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
|
||||
# BRANCH - branch to check out immediately after install (default: master)
|
||||
#
|
||||
# Other options:
|
||||
# CHSH - 'no' means the installer will not change the default shell (default: yes)
|
||||
# RUNZSH - 'no' means the installer will not run zsh after the install (default: yes)
|
||||
# KEEP_ZSHRC - 'yes' means the installer will not replace an existing .zshrc (default: no)
|
||||
# OVERWRITE_CONFIRMATION - 'no' means the installer will not ask for confirmation to overwrite the existing .zshrc (default: yes)
|
||||
#
|
||||
# You can also pass some arguments to the install script to set some these options:
|
||||
# --skip-chsh: has the same behavior as setting CHSH to 'no'
|
||||
# --unattended: sets both CHSH and RUNZSH to 'no'
|
||||
# --keep-zshrc: sets KEEP_ZSHRC to 'yes'
|
||||
# For example:
|
||||
# sh install.sh --unattended
|
||||
# or:
|
||||
# sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
|
||||
#
|
||||
set -e
|
||||
|
||||
# Make sure important variables exist if not already defined
|
||||
#
|
||||
# $USER is defined by login(1) which is not always executed (e.g. containers)
|
||||
# POSIX: https://pubs.opengroup.org/onlinepubs/009695299/utilities/id.html
|
||||
USER=${USER:-$(id -u -n)}
|
||||
# $HOME is defined at the time of login, but it could be unset. If it is unset,
|
||||
# a tilde by itself (~) will not be expanded to the current user's home directory.
|
||||
# POSIX: https://pubs.opengroup.org/onlinepubs/009696899/basedefs/xbd_chap08.html#tag_08_03
|
||||
HOME="${HOME:-$(getent passwd $USER 2>/dev/null | cut -d: -f6)}"
|
||||
# macOS does not have getent, but this works even if $HOME is unset
|
||||
HOME="${HOME:-$(eval echo ~$USER)}"
|
||||
|
||||
|
||||
# Track if $ZSH was provided
|
||||
custom_zsh=${ZSH:+yes}
|
||||
|
||||
# Use $zdot to keep track of where the directory is for zsh dotfiles
|
||||
# To check if $ZDOTDIR was provided, explicitly check for $ZDOTDIR
|
||||
zdot="${ZDOTDIR:-$HOME}"
|
||||
|
||||
# Default value for $ZSH
|
||||
# a) if $ZDOTDIR is supplied and not $HOME: $ZDOTDIR/ohmyzsh
|
||||
# b) otherwise, $HOME/.oh-my-zsh
|
||||
if [ -n "$ZDOTDIR" ] && [ "$ZDOTDIR" != "$HOME" ]; then
|
||||
ZSH="${ZSH:-$ZDOTDIR/ohmyzsh}"
|
||||
fi
|
||||
ZSH="${ZSH:-$HOME/.oh-my-zsh}"
|
||||
|
||||
# Default settings
|
||||
REPO=${REPO:-ohmyzsh/ohmyzsh}
|
||||
REMOTE=${REMOTE:-https://github.com/${REPO}.git}
|
||||
BRANCH=${BRANCH:-master}
|
||||
|
||||
# Other options
|
||||
CHSH=${CHSH:-yes}
|
||||
RUNZSH=${RUNZSH:-yes}
|
||||
KEEP_ZSHRC=${KEEP_ZSHRC:-no}
|
||||
OVERWRITE_CONFIRMATION=${OVERWRITE_CONFIRMATION:-yes}
|
||||
|
||||
|
||||
command_exists() {
|
||||
command -v "$@" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
user_can_sudo() {
|
||||
# Check if sudo is installed
|
||||
command_exists sudo || return 1
|
||||
# Termux can't run sudo, so we can detect it and exit the function early.
|
||||
case "$PREFIX" in
|
||||
*com.termux*) return 1 ;;
|
||||
esac
|
||||
# The following command has 3 parts:
|
||||
#
|
||||
# 1. Run `sudo` with `-v`. Does the following:
|
||||
# • with privilege: asks for a password immediately.
|
||||
# • without privilege: exits with error code 1 and prints the message:
|
||||
# Sorry, user <username> may not run sudo on <hostname>
|
||||
#
|
||||
# 2. Pass `-n` to `sudo` to tell it to not ask for a password. If the
|
||||
# password is not required, the command will finish with exit code 0.
|
||||
# If one is required, sudo will exit with error code 1 and print the
|
||||
# message:
|
||||
# sudo: a password is required
|
||||
#
|
||||
# 3. Check for the words "may not run sudo" in the output to really tell
|
||||
# whether the user has privileges or not. For that we have to make sure
|
||||
# to run `sudo` in the default locale (with `LANG=`) so that the message
|
||||
# stays consistent regardless of the user's locale.
|
||||
#
|
||||
! LANG= sudo -n -v 2>&1 | grep -q "may not run sudo"
|
||||
}
|
||||
|
||||
# The [ -t 1 ] check only works when the function is not called from
|
||||
# a subshell (like in `$(...)` or `(...)`, so this hack redefines the
|
||||
# function at the top level to always return false when stdout is not
|
||||
# a tty.
|
||||
if [ -t 1 ]; then
|
||||
is_tty() {
|
||||
true
|
||||
}
|
||||
else
|
||||
is_tty() {
|
||||
false
|
||||
}
|
||||
fi
|
||||
|
||||
# This function uses the logic from supports-hyperlinks[1][2], which is
|
||||
# made by Kat Marchán (@zkat) and licensed under the Apache License 2.0.
|
||||
# [1] https://github.com/zkat/supports-hyperlinks
|
||||
# [2] https://crates.io/crates/supports-hyperlinks
|
||||
#
|
||||
# Copyright (c) 2021 Kat Marchán
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
supports_hyperlinks() {
|
||||
# $FORCE_HYPERLINK must be set and be non-zero (this acts as a logic bypass)
|
||||
if [ -n "$FORCE_HYPERLINK" ]; then
|
||||
[ "$FORCE_HYPERLINK" != 0 ]
|
||||
return $?
|
||||
fi
|
||||
|
||||
# If stdout is not a tty, it doesn't support hyperlinks
|
||||
is_tty || return 1
|
||||
|
||||
# DomTerm terminal emulator (domterm.org)
|
||||
if [ -n "$DOMTERM" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc)
|
||||
if [ -n "$VTE_VERSION" ]; then
|
||||
[ $VTE_VERSION -ge 5000 ]
|
||||
return $?
|
||||
fi
|
||||
|
||||
# If $TERM_PROGRAM is set, these terminals support hyperlinks
|
||||
case "$TERM_PROGRAM" in
|
||||
Hyper|iTerm.app|terminology|WezTerm|vscode) return 0 ;;
|
||||
esac
|
||||
|
||||
# These termcap entries support hyperlinks
|
||||
case "$TERM" in
|
||||
xterm-kitty|alacritty|alacritty-direct) return 0 ;;
|
||||
esac
|
||||
|
||||
# xfce4-terminal supports hyperlinks
|
||||
if [ "$COLORTERM" = "xfce4-terminal" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Windows Terminal also supports hyperlinks
|
||||
if [ -n "$WT_SESSION" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Konsole supports hyperlinks, but it's an opt-in setting that can't be detected
|
||||
# https://github.com/ohmyzsh/ohmyzsh/issues/10964
|
||||
# if [ -n "$KONSOLE_VERSION" ]; then
|
||||
# return 0
|
||||
# fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
# Adapted from code and information by Anton Kochkov (@XVilka)
|
||||
# Source: https://gist.github.com/XVilka/8346728
|
||||
supports_truecolor() {
|
||||
case "$COLORTERM" in
|
||||
truecolor|24bit) return 0 ;;
|
||||
esac
|
||||
|
||||
case "$TERM" in
|
||||
iterm |\
|
||||
tmux-truecolor |\
|
||||
linux-truecolor |\
|
||||
xterm-truecolor |\
|
||||
screen-truecolor) return 0 ;;
|
||||
esac
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
fmt_link() {
|
||||
# $1: text, $2: url, $3: fallback mode
|
||||
if supports_hyperlinks; then
|
||||
printf '\033]8;;%s\033\\%s\033]8;;\033\\\n' "$2" "$1"
|
||||
return
|
||||
fi
|
||||
|
||||
case "$3" in
|
||||
--text) printf '%s\n' "$1" ;;
|
||||
--url|*) fmt_underline "$2" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
fmt_underline() {
|
||||
is_tty && printf '\033[4m%s\033[24m\n' "$*" || printf '%s\n' "$*"
|
||||
}
|
||||
|
||||
# shellcheck disable=SC2016 # backtick in single-quote
|
||||
fmt_code() {
|
||||
is_tty && printf '`\033[2m%s\033[22m`\n' "$*" || printf '`%s`\n' "$*"
|
||||
}
|
||||
|
||||
fmt_error() {
|
||||
printf '%sError: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "$FMT_RESET" >&2
|
||||
}
|
||||
|
||||
setup_color() {
|
||||
# Only use colors if connected to a terminal
|
||||
if ! is_tty; then
|
||||
FMT_RAINBOW=""
|
||||
FMT_RED=""
|
||||
FMT_GREEN=""
|
||||
FMT_YELLOW=""
|
||||
FMT_BLUE=""
|
||||
FMT_BOLD=""
|
||||
FMT_RESET=""
|
||||
return
|
||||
fi
|
||||
|
||||
if supports_truecolor; then
|
||||
FMT_RAINBOW="
|
||||
$(printf '\033[38;2;255;0;0m')
|
||||
$(printf '\033[38;2;255;97;0m')
|
||||
$(printf '\033[38;2;247;255;0m')
|
||||
$(printf '\033[38;2;0;255;30m')
|
||||
$(printf '\033[38;2;77;0;255m')
|
||||
$(printf '\033[38;2;168;0;255m')
|
||||
$(printf '\033[38;2;245;0;172m')
|
||||
"
|
||||
else
|
||||
FMT_RAINBOW="
|
||||
$(printf '\033[38;5;196m')
|
||||
$(printf '\033[38;5;202m')
|
||||
$(printf '\033[38;5;226m')
|
||||
$(printf '\033[38;5;082m')
|
||||
$(printf '\033[38;5;021m')
|
||||
$(printf '\033[38;5;093m')
|
||||
$(printf '\033[38;5;163m')
|
||||
"
|
||||
fi
|
||||
|
||||
FMT_RED=$(printf '\033[31m')
|
||||
FMT_GREEN=$(printf '\033[32m')
|
||||
FMT_YELLOW=$(printf '\033[33m')
|
||||
FMT_BLUE=$(printf '\033[34m')
|
||||
FMT_BOLD=$(printf '\033[1m')
|
||||
FMT_RESET=$(printf '\033[0m')
|
||||
}
|
||||
|
||||
setup_ohmyzsh() {
|
||||
# Prevent the cloned repository from having insecure permissions. Failing to do
|
||||
# so causes compinit() calls to fail with "command not found: compdef" errors
|
||||
# for users with insecure umasks (e.g., "002", allowing group writability). Note
|
||||
# that this will be ignored under Cygwin by default, as Windows ACLs take
|
||||
# precedence over umasks except for filesystems mounted with option "noacl".
|
||||
umask g-w,o-w
|
||||
|
||||
echo "${FMT_BLUE}Cloning Oh My Zsh...${FMT_RESET}"
|
||||
|
||||
command_exists git || {
|
||||
fmt_error "git is not installed"
|
||||
exit 1
|
||||
}
|
||||
|
||||
ostype=$(uname)
|
||||
if [ -z "${ostype%CYGWIN*}" ] && git --version | grep -Eq 'msysgit|windows'; then
|
||||
fmt_error "Windows/MSYS Git is not supported on Cygwin"
|
||||
fmt_error "Make sure the Cygwin git package is installed and is first on the \$PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Manual clone with git config options to support git < v1.7.2
|
||||
git init --quiet "$ZSH" && cd "$ZSH" \
|
||||
&& git config core.eol lf \
|
||||
&& git config core.autocrlf false \
|
||||
&& git config fsck.zeroPaddedFilemode ignore \
|
||||
&& git config fetch.fsck.zeroPaddedFilemode ignore \
|
||||
&& git config receive.fsck.zeroPaddedFilemode ignore \
|
||||
&& git config oh-my-zsh.remote origin \
|
||||
&& git config oh-my-zsh.branch "$BRANCH" \
|
||||
&& git remote add origin "$REMOTE" \
|
||||
&& git fetch --depth=1 origin \
|
||||
&& git checkout -b "$BRANCH" "origin/$BRANCH" || {
|
||||
[ ! -d "$ZSH" ] || {
|
||||
cd -
|
||||
rm -rf "$ZSH" 2>/dev/null
|
||||
}
|
||||
fmt_error "git clone of oh-my-zsh repo failed"
|
||||
exit 1
|
||||
}
|
||||
# Exit installation directory
|
||||
cd -
|
||||
|
||||
echo
|
||||
}
|
||||
|
||||
setup_zshrc() {
|
||||
# Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
|
||||
# with datestamp of installation that moved them aside, so we never actually
|
||||
# destroy a user's original zshrc
|
||||
echo "${FMT_BLUE}Looking for an existing zsh config...${FMT_RESET}"
|
||||
|
||||
# Must use this exact name so uninstall.sh can find it
|
||||
OLD_ZSHRC="$zdot/.zshrc.pre-oh-my-zsh"
|
||||
if [ -f "$zdot/.zshrc" ] || [ -h "$zdot/.zshrc" ]; then
|
||||
# Skip this if the user doesn't want to replace an existing .zshrc
|
||||
if [ "$KEEP_ZSHRC" = yes ]; then
|
||||
echo "${FMT_YELLOW}Found ${zdot}/.zshrc.${FMT_RESET} ${FMT_GREEN}Keeping...${FMT_RESET}"
|
||||
return
|
||||
fi
|
||||
|
||||
if [ $OVERWRITE_CONFIRMATION != "no" ]; then
|
||||
# Ask user for confirmation before backing up and overwriting
|
||||
echo "${FMT_YELLOW}Found ${zdot}/.zshrc."
|
||||
echo "The existing .zshrc will be backed up to .zshrc.pre-oh-my-zsh if overwritten."
|
||||
echo "Make sure your .zshrc contains the following minimal configuration if you choose not to overwrite it:${FMT_RESET}"
|
||||
echo "----------------------------------------"
|
||||
cat "$ZSH/templates/minimal.zshrc"
|
||||
echo "----------------------------------------"
|
||||
printf '%sDo you want to overwrite it with the Oh My Zsh template? [Y/n]%s ' \
|
||||
"$FMT_YELLOW" "$FMT_RESET"
|
||||
read -r opt
|
||||
case $opt in
|
||||
[Yy]*|"") ;;
|
||||
[Nn]*) echo "Overwrite skipped. Existing .zshrc will be kept."; return ;;
|
||||
*) echo "Invalid choice. Overwrite skipped. Existing .zshrc will be kept."; return ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
if [ -e "$OLD_ZSHRC" ]; then
|
||||
OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
|
||||
if [ -e "$OLD_OLD_ZSHRC" ]; then
|
||||
fmt_error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
|
||||
fmt_error "re-run the installer again in a couple of seconds"
|
||||
exit 1
|
||||
fi
|
||||
mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
|
||||
|
||||
echo "${FMT_YELLOW}Found old .zshrc.pre-oh-my-zsh." \
|
||||
"${FMT_GREEN}Backing up to ${OLD_OLD_ZSHRC}${FMT_RESET}"
|
||||
fi
|
||||
echo "${FMT_GREEN}Backing up to ${OLD_ZSHRC}${FMT_RESET}"
|
||||
mv "$zdot/.zshrc" "$OLD_ZSHRC"
|
||||
fi
|
||||
|
||||
echo "${FMT_GREEN}Using the Oh My Zsh template file and adding it to $zdot/.zshrc.${FMT_RESET}"
|
||||
|
||||
# Modify $ZSH variable in .zshrc directory to use the literal $ZDOTDIR or $HOME
|
||||
omz="$ZSH"
|
||||
if [ -n "$ZDOTDIR" ] && [ "$ZDOTDIR" != "$HOME" ]; then
|
||||
omz=$(echo "$omz" | sed "s|^$ZDOTDIR/|\$ZDOTDIR/|")
|
||||
fi
|
||||
omz=$(echo "$omz" | sed "s|^$HOME/|\$HOME/|")
|
||||
|
||||
sed "s|^export ZSH=.*$|export ZSH=\"${omz}\"|" "$ZSH/templates/zshrc.zsh-template" > "$zdot/.zshrc-omztemp"
|
||||
mv -f "$zdot/.zshrc-omztemp" "$zdot/.zshrc"
|
||||
|
||||
echo
|
||||
}
|
||||
|
||||
setup_shell() {
|
||||
# Skip setup if the user wants or stdin is closed (not running interactively).
|
||||
if [ "$CHSH" = no ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
# If this user's login shell is already "zsh", do not attempt to switch.
|
||||
if [ "$(basename -- "$SHELL")" = "zsh" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
# If this platform doesn't provide a "chsh" command, bail out.
|
||||
if ! command_exists chsh; then
|
||||
cat <<EOF
|
||||
I can't change your shell automatically because this system does not have chsh.
|
||||
${FMT_BLUE}Please manually change your default shell to zsh${FMT_RESET}
|
||||
EOF
|
||||
return
|
||||
fi
|
||||
|
||||
echo "${FMT_BLUE}Time to change your default shell to zsh:${FMT_RESET}"
|
||||
|
||||
# Prompt for user choice on changing the default login shell
|
||||
printf '%sDo you want to change your default shell to zsh? [Y/n]%s ' \
|
||||
"$FMT_YELLOW" "$FMT_RESET"
|
||||
read -r opt
|
||||
case $opt in
|
||||
[Yy]*|"") ;;
|
||||
[Nn]*) echo "Shell change skipped."; return ;;
|
||||
*) echo "Invalid choice. Shell change skipped."; return ;;
|
||||
esac
|
||||
|
||||
# Check if we're running on Termux
|
||||
case "$PREFIX" in
|
||||
*com.termux*) termux=true; zsh=zsh ;;
|
||||
*) termux=false ;;
|
||||
esac
|
||||
|
||||
if [ "$termux" != true ]; then
|
||||
# Test for the right location of the "shells" file
|
||||
if [ -f /etc/shells ]; then
|
||||
shells_file=/etc/shells
|
||||
elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
|
||||
shells_file=/usr/share/defaults/etc/shells
|
||||
else
|
||||
fmt_error "could not find /etc/shells file. Change your default shell manually."
|
||||
return
|
||||
fi
|
||||
|
||||
# Get the path to the right zsh binary
|
||||
# 1. Use the most preceding one based on $PATH, then check that it's in the shells file
|
||||
# 2. If that fails, get a zsh path from the shells file, then check it actually exists
|
||||
if ! zsh=$(command -v zsh) || ! grep -qx "$zsh" "$shells_file"; then
|
||||
if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -n 1) || [ ! -f "$zsh" ]; then
|
||||
fmt_error "no zsh binary found or not present in '$shells_file'"
|
||||
fmt_error "change your default shell manually."
|
||||
return
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# We're going to change the default shell, so back up the current one
|
||||
if [ -n "$SHELL" ]; then
|
||||
echo "$SHELL" > "$zdot/.shell.pre-oh-my-zsh"
|
||||
else
|
||||
grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > "$zdot/.shell.pre-oh-my-zsh"
|
||||
fi
|
||||
|
||||
echo "Changing your shell to $zsh..."
|
||||
|
||||
# Check if user has sudo privileges to run `chsh` with or without `sudo`
|
||||
#
|
||||
# This allows the call to succeed without password on systems where the
|
||||
# user does not have a password but does have sudo privileges, like in
|
||||
# Google Cloud Shell.
|
||||
#
|
||||
# On systems that don't have a user with passwordless sudo, the user will
|
||||
# be prompted for the password either way, so this shouldn't cause any issues.
|
||||
#
|
||||
if user_can_sudo; then
|
||||
sudo -k chsh -s "$zsh" "$USER" # -k forces the password prompt
|
||||
else
|
||||
chsh -s "$zsh" "$USER" # run chsh normally
|
||||
fi
|
||||
|
||||
# Check if the shell change was successful
|
||||
if [ $? -ne 0 ]; then
|
||||
fmt_error "chsh command unsuccessful. Change your default shell manually."
|
||||
else
|
||||
export SHELL="$zsh"
|
||||
echo "${FMT_GREEN}Shell successfully changed to '$zsh'.${FMT_RESET}"
|
||||
fi
|
||||
|
||||
echo
|
||||
}
|
||||
|
||||
# shellcheck disable=SC2183 # printf string has more %s than arguments ($FMT_RAINBOW expands to multiple arguments)
|
||||
print_success() {
|
||||
printf '%s %s__ %s %s %s %s %s__ %s\n' $FMT_RAINBOW $FMT_RESET
|
||||
printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $FMT_RAINBOW $FMT_RESET
|
||||
printf '%s / __ \\%s/ __ \\ %s / __ `__ \\%s/ / / / %s /_ / %s/ ___/%s __ \\ %s\n' $FMT_RAINBOW $FMT_RESET
|
||||
printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $FMT_RAINBOW $FMT_RESET
|
||||
printf '%s\\____/%s_/ /_/ %s /_/ /_/ /_/%s\\__, / %s /___/%s____/%s_/ /_/ %s\n' $FMT_RAINBOW $FMT_RESET
|
||||
printf '%s %s %s %s /____/ %s %s %s %s....is now installed!%s\n' $FMT_RAINBOW $FMT_GREEN $FMT_RESET
|
||||
printf '\n'
|
||||
printf '\n'
|
||||
printf "%s %s %s\n" "Before you scream ${FMT_BOLD}${FMT_YELLOW}Oh My Zsh!${FMT_RESET} look over the" \
|
||||
"$(fmt_code "$(fmt_link ".zshrc" "file://$zdot/.zshrc" --text)")" \
|
||||
"file to select plugins, themes, and options."
|
||||
printf '\n'
|
||||
printf '%s\n' "• Follow us on X: $(fmt_link @ohmyzsh https://x.com/ohmyzsh)"
|
||||
printf '%s\n' "• Join our Discord community: $(fmt_link "Discord server" https://discord.gg/ohmyzsh)"
|
||||
printf '%s\n' "• Get stickers, t-shirts, coffee mugs and more: $(fmt_link "Planet Argon Shop" https://shop.planetargon.com/collections/oh-my-zsh)"
|
||||
printf '%s\n' $FMT_RESET
|
||||
}
|
||||
|
||||
main() {
|
||||
# Run as unattended if stdin is not a tty
|
||||
if [ ! -t 0 ]; then
|
||||
RUNZSH=no
|
||||
CHSH=no
|
||||
OVERWRITE_CONFIRMATION=no
|
||||
fi
|
||||
|
||||
# Parse arguments
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
--unattended) RUNZSH=no; CHSH=no; OVERWRITE_CONFIRMATION=no ;;
|
||||
--skip-chsh) CHSH=no ;;
|
||||
--keep-zshrc) KEEP_ZSHRC=yes ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
setup_color
|
||||
|
||||
if ! command_exists zsh; then
|
||||
echo "${FMT_YELLOW}Zsh is not installed.${FMT_RESET} Please install zsh first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -d "$ZSH" ]; then
|
||||
echo "${FMT_YELLOW}The \$ZSH folder already exists ($ZSH).${FMT_RESET}"
|
||||
if [ "$custom_zsh" = yes ]; then
|
||||
cat <<EOF
|
||||
|
||||
You ran the installer with the \$ZSH setting or the \$ZSH variable is
|
||||
exported. You have 3 options:
|
||||
|
||||
1. Unset the ZSH variable when calling the installer:
|
||||
$(fmt_code "ZSH= sh install.sh")
|
||||
2. Install Oh My Zsh to a directory that doesn't exist yet:
|
||||
$(fmt_code "ZSH=path/to/new/ohmyzsh/folder sh install.sh")
|
||||
3. (Caution) If the folder doesn't contain important information,
|
||||
you can just remove it with $(fmt_code "rm -r $ZSH")
|
||||
|
||||
EOF
|
||||
else
|
||||
echo "You'll need to remove it if you want to reinstall."
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create ZDOTDIR folder structure if it doesn't exist
|
||||
if [ -n "$ZDOTDIR" ]; then
|
||||
mkdir -p "$ZDOTDIR"
|
||||
fi
|
||||
|
||||
setup_ohmyzsh
|
||||
setup_zshrc
|
||||
setup_shell
|
||||
|
||||
print_success
|
||||
|
||||
if [ $RUNZSH = no ]; then
|
||||
echo "${FMT_YELLOW}Run zsh to try it out.${FMT_RESET}"
|
||||
exit
|
||||
fi
|
||||
|
||||
exec zsh -l
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@@ -19,9 +19,11 @@ nvml-wrapper = "0.11"
|
||||
nvml-wrapper-sys = "0.9.0"
|
||||
anyhow = "1.0.98"
|
||||
|
||||
# Docker .env loading
|
||||
config = "0.13"
|
||||
dotenvy = "0.15"
|
||||
regex = "1.11.3"
|
||||
|
||||
# Docker API access
|
||||
bollard = "0.19"
|
||||
futures-util = "0.3"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = { version = "0.3", features = ["winuser", "pdh", "ifmib", "iphlpapi", "winerror" ,"wbemcli", "combaseapi"] }
|
||||
|
||||
@@ -1,15 +1,46 @@
|
||||
/// # API Module
|
||||
///
|
||||
/// This module provides all HTTP communication between WatcherAgent and the backend server.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **Registration:** Registers the agent with the backend and retrieves its server ID and IP address.
|
||||
/// - **Heartbeat:** Periodically sends heartbeat signals to indicate liveness.
|
||||
/// - **Metrics Reporting:** Sends collected hardware and network metrics to the backend.
|
||||
/// - **Command Listening:** Polls for and executes remote commands from the backend (e.g., update image, restart container).
|
||||
///
|
||||
/// ## Usage
|
||||
/// 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::serverclientcomm::handle_server_message;
|
||||
use crate::hardware::HardwareInfo;
|
||||
use crate::models::{HeartbeatDto, IdResponse, MetricDto, RegistrationDto};
|
||||
use crate::models::{
|
||||
Acknowledgment, DockerMetricDto, DockerRegistrationDto, HeartbeatDto,
|
||||
IdResponse, MetricDto, RegistrationDto, ServerMessage,
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use reqwest::{Client, StatusCode};
|
||||
use std::error::Error;
|
||||
use tokio::time::sleep;
|
||||
|
||||
use bollard::Docker;
|
||||
|
||||
/// Registers this agent with the backend server and retrieves its server ID and IP address.
|
||||
///
|
||||
/// This function collects local hardware information, prepares a registration payload, and sends it to the backend. It will retry registration until successful, handling network errors and server-side failures gracefully.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `base_url` - The base URL of the backend server (e.g., `https://server.example.com`).
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(i32, String), Box<dyn Error + Send + Sync>>` - Tuple of server ID and registered IP address on success.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns an error if unable to register after repeated attempts.
|
||||
pub async fn register_with_server(
|
||||
base_url: &str,
|
||||
) -> Result<(i32, String), Box<dyn Error + Send + Sync>> {
|
||||
) -> Result<(u16, String), Box<dyn Error + Send + Sync>> {
|
||||
// First get local IP
|
||||
let ip = local_ip_address::local_ip()?.to_string();
|
||||
println!("Local IP address detected: {}", ip);
|
||||
@@ -27,18 +58,18 @@ pub async fn register_with_server(
|
||||
|
||||
// Prepare registration data
|
||||
let registration = RegistrationDto {
|
||||
id: server_id,
|
||||
server_id: server_id,
|
||||
ip_address: registered_ip.clone(),
|
||||
cpu_type: hardware.cpu.name.clone().unwrap_or_default(),
|
||||
cpu_cores: (hardware.cpu.cores).unwrap_or_default(),
|
||||
gpu_type: hardware.gpu.name.clone().unwrap_or_default(),
|
||||
ram_size: hardware.memory.total.unwrap_or_default(),
|
||||
ram_size: hardware.memory.total_size.unwrap_or_default(),
|
||||
};
|
||||
|
||||
// Try to register (will retry on failure)
|
||||
loop {
|
||||
println!("Attempting to register with server...");
|
||||
let url = format!("{}/monitoring/register-agent-by-id", base_url);
|
||||
let url = format!("{}/monitoring/hardware-info", base_url);
|
||||
match client.post(&url).json(®istration).send().await {
|
||||
Ok(resp) if resp.status().is_success() => {
|
||||
println!("✅ Successfully registered with server.");
|
||||
@@ -60,15 +91,25 @@ pub async fn register_with_server(
|
||||
}
|
||||
}
|
||||
|
||||
/// Looks up the server ID for the given IP address from the backend server.
|
||||
///
|
||||
/// This function will retry until a valid response is received, handling network errors and server-side failures.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `base_url` - The base URL of the backend server.
|
||||
/// * `ip` - The local IP address to look up.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(i32, String), Box<dyn Error + Send + Sync>>` - Tuple of server ID and registered IP address.
|
||||
async fn get_server_id_by_ip(
|
||||
base_url: &str,
|
||||
ip: &str,
|
||||
) -> Result<(i32, String), Box<dyn Error + Send + Sync>> {
|
||||
) -> Result<(u16, String), Box<dyn Error + Send + Sync>> {
|
||||
let client = Client::builder()
|
||||
.danger_accept_invalid_certs(true)
|
||||
.build()?;
|
||||
|
||||
let url = format!("{}/monitoring/server-id-by-ip?ipAddress={}", base_url, ip);
|
||||
let url = format!("{}/monitoring/register?ipAddress={}", base_url, ip);
|
||||
|
||||
loop {
|
||||
println!("Attempting to fetch server ID for IP {}...", ip);
|
||||
@@ -111,6 +152,99 @@ 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(
|
||||
base_url: &str,
|
||||
server_id: u16,
|
||||
container_dto: &DockerRegistrationDto,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// First get local IP
|
||||
println!("Preparing to broadcast docker containers...");
|
||||
// Create HTTP client for registration
|
||||
let client = Client::builder()
|
||||
.danger_accept_invalid_certs(true)
|
||||
.build()?;
|
||||
|
||||
// Prepare registration data
|
||||
let mut broadcast_data = container_dto.clone();
|
||||
broadcast_data.server_id = server_id;
|
||||
|
||||
// Try to register (will retry on failure)
|
||||
loop {
|
||||
println!("Attempting to broadcast containers...");
|
||||
|
||||
let json_body = serde_json::to_string_pretty(&broadcast_data)?;
|
||||
println!("📤 JSON being posted:\n{}", json_body);
|
||||
|
||||
let url = format!("{}/monitoring/service-discovery", base_url);
|
||||
match client.post(&url).json(&container_dto).send().await {
|
||||
Ok(resp) if resp.status().is_success() => {
|
||||
println!(
|
||||
"✅ Successfully broadcasted following docker container: {:?}",
|
||||
container_dto
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
Ok(resp) => {
|
||||
let status = resp.status();
|
||||
let text = resp.text().await.unwrap_or_default();
|
||||
println!(
|
||||
"⚠️ Broadcasting failed ({}): {} (will retry in 10 seconds)",
|
||||
status, text
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
println!("⚠️ Broadcasting failed: {} (will retry in 10 seconds)", err);
|
||||
}
|
||||
}
|
||||
sleep(Duration::from_secs(10)).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Periodically sends heartbeat signals to the backend server to indicate agent liveness.
|
||||
///
|
||||
/// This function runs in a background task and will retry on network errors.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `base_url` - The base URL of the backend server.
|
||||
/// * `ip` - The IP address of the agent.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if heartbeats are sent successfully.
|
||||
pub async fn heartbeat_loop(base_url: &str, ip: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let client = Client::builder()
|
||||
.danger_accept_invalid_certs(true)
|
||||
@@ -134,6 +268,16 @@ pub async fn heartbeat_loop(base_url: &str, ip: &str) -> Result<(), Box<dyn Erro
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends collected hardware and network metrics to the backend server.
|
||||
///
|
||||
/// This function is called periodically from the metrics collection loop. It logs the result and retries on network errors.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `base_url` - The base URL of the backend server.
|
||||
/// * `metrics` - The metrics data to send (see [`MetricDto`]).
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if metrics are sent successfully.
|
||||
pub async fn send_metrics(
|
||||
base_url: &str,
|
||||
metrics: &MetricDto,
|
||||
@@ -153,3 +297,150 @@ pub async fn send_metrics(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Polls the backend server for remote commands and executes them.
|
||||
///
|
||||
/// This function runs in a background task, polling the server for new messages. It acknowledges receipt and execution of each command, and handles errors gracefully.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `docker` - Reference to a Bollard Docker client.
|
||||
/// * `base_url` - The base URL of the backend server.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if commands are handled successfully.
|
||||
pub async fn listening_to_server(
|
||||
docker: &Docker,
|
||||
base_url: &str,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let url = format!("{}/api/message", base_url);
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
loop {
|
||||
// Get message from server
|
||||
let resp = client.get(&url).send().await;
|
||||
|
||||
match resp {
|
||||
Ok(response) => {
|
||||
if response.status().is_success() {
|
||||
match response.json::<ServerMessage>().await {
|
||||
Ok(msg) => {
|
||||
// Acknowledge receipt immediately
|
||||
if let Err(e) = send_acknowledgment(
|
||||
&client,
|
||||
base_url,
|
||||
&msg.message_id,
|
||||
"received",
|
||||
"Message received successfully",
|
||||
)
|
||||
.await
|
||||
{
|
||||
eprintln!("Failed to send receipt acknowledgment: {}", e);
|
||||
}
|
||||
|
||||
// Handle the message
|
||||
let result = handle_server_message(docker, msg.clone()).await;
|
||||
|
||||
// Send execution result acknowledgment
|
||||
let (status, details) = match result {
|
||||
Ok(_) => ("success", "Message executed successfully".to_string()),
|
||||
Err(e) => ("error", format!("Execution failed: {}", e)),
|
||||
};
|
||||
|
||||
if let Err(e) = send_acknowledgment(
|
||||
&client,
|
||||
base_url,
|
||||
&msg.message_id,
|
||||
status,
|
||||
&details,
|
||||
)
|
||||
.await
|
||||
{
|
||||
eprintln!("Failed to send execution acknowledgment: {}", e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to parse message: {}", e);
|
||||
}
|
||||
}
|
||||
} else if response.status() == reqwest::StatusCode::NO_CONTENT {
|
||||
// No new messages, continue polling
|
||||
println!("No new messages from server");
|
||||
} else {
|
||||
eprintln!("Server returned error status: {}", response.status());
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to reach server: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Poll every 5 seconds (or use WebSocket for real-time)
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends an acknowledgment to the backend server for a received or executed command message.
|
||||
///
|
||||
/// This function is used internally by [`listening_to_server`] to confirm receipt and execution status of commands.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `client` - Reference to a reqwest HTTP client.
|
||||
/// * `base_url` - The base URL of the backend server.
|
||||
/// * `message_id` - The ID of the message being acknowledged.
|
||||
/// * `status` - Status string (e.g., "received", "success", "error").
|
||||
/// * `details` - Additional details about the acknowledgment.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if acknowledgment is sent successfully.
|
||||
pub async fn send_acknowledgment(
|
||||
client: &reqwest::Client,
|
||||
base_url: &str,
|
||||
message_id: &str,
|
||||
status: &str,
|
||||
details: &str,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let ack_url = format!("{}/api/acknowledge", base_url);
|
||||
|
||||
let acknowledgment = Acknowledgment {
|
||||
message_id: message_id.to_string(),
|
||||
status: status.to_string(),
|
||||
details: details.to_string(),
|
||||
};
|
||||
|
||||
let response = client.post(&ack_url).json(&acknowledgment).send().await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
println!(
|
||||
"Acknowledgment sent successfully for message {}",
|
||||
message_id
|
||||
);
|
||||
} else {
|
||||
eprintln!(
|
||||
"Server returned error for acknowledgment: {}",
|
||||
response.status()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_docker_metrics(
|
||||
base_url: &str,
|
||||
docker_metrics: &DockerMetricDto,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let client = Client::new();
|
||||
let url = format!("{}/monitoring/docker-metric", base_url);
|
||||
|
||||
println!("Docker Metrics: {}", serde_json::to_string_pretty(&docker_metrics)?);
|
||||
|
||||
match client.post(&url).json(&docker_metrics).send().await {
|
||||
Ok(res) => println!(
|
||||
"✅ Sent docker metrics for server {} | Status: {}",
|
||||
docker_metrics.server_id,
|
||||
res.status()
|
||||
),
|
||||
Err(err) => eprintln!("❌ Failed to send docker metrics: {}", err),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
214
WatcherAgent/src/docker/container.rs
Normal file
214
WatcherAgent/src/docker/container.rs
Normal file
@@ -0,0 +1,214 @@
|
||||
//! Docker container utilities for WatcherAgent
|
||||
//!
|
||||
//! Provides functions to list and process Docker containers using the Bollard library.
|
||||
//!
|
||||
use crate::docker::stats;
|
||||
use crate::docker::stats::{ContainerCpuInfo, ContainerNetworkInfo};
|
||||
use crate::models::DockerContainer;
|
||||
|
||||
use bollard::query_parameters::{
|
||||
CreateImageOptions, ListContainersOptions, RestartContainerOptions,
|
||||
};
|
||||
use bollard::Docker;
|
||||
use futures_util::StreamExt;
|
||||
use std::error::Error;
|
||||
|
||||
/// Returns a list of available Docker containers.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `docker` - Reference to a Bollard Docker client.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Vec<DockerContainer>` - Vector of Docker container info.
|
||||
pub async fn get_available_containers(docker: &Docker) -> Vec<DockerContainer> {
|
||||
println!("=== DOCKER CONTAINER LIST ===");
|
||||
|
||||
let options = Some(ListContainersOptions {
|
||||
all: true,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let containers_list = match docker.list_containers(options).await {
|
||||
Ok(containers) => {
|
||||
println!("Available containers ({}):", containers.len());
|
||||
containers
|
||||
.into_iter()
|
||||
.filter_map(|container| {
|
||||
container.id.as_ref()?; // Skip if no ID
|
||||
|
||||
let id = container.id?;
|
||||
let short_id = if id.len() > 12 { &id[..12] } else { &id };
|
||||
|
||||
let name = container
|
||||
.names
|
||||
.and_then(|names| names.into_iter().next())
|
||||
.map(|name| name.trim_start_matches('/').to_string())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
let image = container
|
||||
.image
|
||||
.as_ref()
|
||||
.map(|img| img.to_string())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
Some(DockerContainer {
|
||||
id: short_id.to_string(),
|
||||
image: Some(image),
|
||||
name: Some(name),
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to list containers: {}", e);
|
||||
Vec::new()
|
||||
}
|
||||
};
|
||||
|
||||
containers_list
|
||||
}
|
||||
|
||||
/// Pulls a new Docker image and restarts the current container.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `docker` - Reference to a Bollard Docker client.
|
||||
/// * `image` - The name of the Docker image to pull.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if updated successfully, error otherwise.
|
||||
pub async fn update_docker_image(
|
||||
docker: &Docker,
|
||||
image: &str,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
println!("Updating to {}", image);
|
||||
|
||||
// 1. Pull new image
|
||||
let mut stream = docker.create_image(
|
||||
Some(CreateImageOptions {
|
||||
from_image: Some(image.to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
|
||||
// Use the stream with proper trait bounds
|
||||
while let Some(result) = StreamExt::next(&mut stream).await {
|
||||
match result {
|
||||
Ok(progress) => {
|
||||
if let Some(status) = progress.status {
|
||||
println!("Pull status: {}", status);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error pulling image: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Restart the current container
|
||||
let options = Some(ListContainersOptions {
|
||||
all: true,
|
||||
..Default::default()
|
||||
});
|
||||
let container_id = docker
|
||||
.list_containers(options)
|
||||
.await?
|
||||
.into_iter()
|
||||
.find_map(|c| {
|
||||
c.image
|
||||
.as_ref()
|
||||
.and_then(|img| if img == image { c.id } else { None })
|
||||
});
|
||||
let _ = restart_container(docker, &container_id.unwrap()).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Restarts the agent's own Docker container.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `docker` - Reference to a Bollard Docker client.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if restarted successfully, error otherwise.
|
||||
pub async fn restart_container(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
println!("Restarting container {}", container_id);
|
||||
if let Err(e) = docker
|
||||
.restart_container(
|
||||
&container_id.to_string(),
|
||||
Some(RestartContainerOptions {
|
||||
signal: None,
|
||||
t: Some(0),
|
||||
}),
|
||||
)
|
||||
.await
|
||||
{
|
||||
eprintln!("Failed to restart container: {}", e);
|
||||
}
|
||||
|
||||
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
|
||||
pub async fn get_network_stats(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<ContainerNetworkInfo, Box<dyn Error + Send + Sync>> {
|
||||
let (_, net_info, _, _) = stats::get_single_container_stats(docker, container_id).await?;
|
||||
|
||||
if let Some(net_info) = net_info {
|
||||
Ok(net_info)
|
||||
} else {
|
||||
// Return default network info if not found
|
||||
println!("No network info found for container {}", container_id);
|
||||
Ok(ContainerNetworkInfo {
|
||||
container_id: Some(container_id.to_string()),
|
||||
rx_bytes: None,
|
||||
tx_bytes: None,
|
||||
rx_packets: None,
|
||||
tx_packets: None,
|
||||
rx_errors: None,
|
||||
tx_errors: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets CPU statistics for a specific container
|
||||
pub async fn get_cpu_stats(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<ContainerCpuInfo, Box<dyn Error + Send + Sync>> {
|
||||
let (cpu_info, _, _, _) = stats::get_single_container_stats(docker, container_id).await?;
|
||||
|
||||
if let Some(cpu_info) = cpu_info {
|
||||
Ok(cpu_info)
|
||||
} else {
|
||||
// Return default CPU info if not found
|
||||
println!("No CPU info found for container {}", container_id);
|
||||
Ok(ContainerCpuInfo {
|
||||
container_id: Some(container_id.to_string()),
|
||||
cpu_usage_percent: None,
|
||||
system_cpu_usage: None,
|
||||
container_cpu_usage: None,
|
||||
online_cpus: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
338
WatcherAgent/src/docker/mod.rs
Normal file
338
WatcherAgent/src/docker/mod.rs
Normal file
@@ -0,0 +1,338 @@
|
||||
/// # Docker Module
|
||||
///
|
||||
/// This module provides Docker integration for WatcherAgent, including container enumeration, statistics, and lifecycle management.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **Container Management:** Lists, inspects, and manages Docker containers relevant to the agent.
|
||||
/// - **Statistics Aggregation:** Collects network and CPU statistics for all managed containers.
|
||||
/// - **Lifecycle Operations:** Supports container restart and ID lookup for agent self-management.
|
||||
///
|
||||
pub mod container;
|
||||
pub mod serverclientcomm;
|
||||
pub mod stats;
|
||||
|
||||
use crate::models::{
|
||||
DockerCollectMetricDto, DockerContainer, DockerContainerCpuDto, DockerContainerInfo,
|
||||
DockerContainerNetworkDto, DockerContainerRamDto, DockerMetricDto, DockerRegistrationDto,
|
||||
DockerContainerStatusDto
|
||||
};
|
||||
use bollard::Docker;
|
||||
use std::error::Error;
|
||||
|
||||
/// Main Docker manager that holds the Docker client and provides all operations
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DockerManager {
|
||||
pub docker: Docker,
|
||||
}
|
||||
|
||||
impl Default for DockerManager {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
docker: Docker::connect_with_local_defaults()
|
||||
.unwrap_or_else(|e| panic!("Failed to create default Docker connection: {}", e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DockerManager {
|
||||
/// Creates a new DockerManager instance
|
||||
pub fn new() -> Result<Self, Box<dyn Error + Send + Sync>> {
|
||||
let docker = Docker::connect_with_local_defaults()
|
||||
.map_err(|e| format!("Failed to connect to Docker: {}", e))?;
|
||||
|
||||
Ok(Self { docker })
|
||||
}
|
||||
|
||||
/// Creates a DockerManager instance with optional Docker connection
|
||||
pub fn new_optional() -> Option<Self> {
|
||||
Docker::connect_with_local_defaults()
|
||||
.map(|docker| Self { docker })
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Finds the Docker container running the agent by image name
|
||||
pub async fn get_client_container(
|
||||
&self,
|
||||
) -> Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
||||
let containers = container::get_available_containers(&self.docker).await;
|
||||
let client_image = "watcher-agent";
|
||||
|
||||
Ok(containers
|
||||
.into_iter()
|
||||
.find(|c| c.clone().image.unwrap().contains(client_image))
|
||||
.map(|container| DockerContainer {
|
||||
id: container.id,
|
||||
image: container.image,
|
||||
name: container.name,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Gets the current client version (image name) if running in Docker
|
||||
pub async fn get_client_version(&self) -> String {
|
||||
match self.get_client_container().await {
|
||||
Ok(Some(container)) => container
|
||||
.image
|
||||
.clone()
|
||||
.unwrap()
|
||||
.split(':')
|
||||
.next()
|
||||
.unwrap_or("unknown")
|
||||
.to_string(),
|
||||
Ok(None) => {
|
||||
println!("Warning: No WatcherAgent container found");
|
||||
"unknown".to_string()
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Warning: Could not get current image version: {}", e);
|
||||
"unknown".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if Docker is available and the agent is running in a container
|
||||
pub async fn is_dockerized(&self) -> bool {
|
||||
self.get_client_container()
|
||||
.await
|
||||
.map(|c| c.is_some())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Gets all available containers as DTOs for registration
|
||||
pub async fn get_containers(
|
||||
&self,
|
||||
) -> Result<Vec<DockerContainer>, Box<dyn Error + Send + Sync>> {
|
||||
let containers = container::get_available_containers(&self.docker).await;
|
||||
|
||||
Ok(containers
|
||||
.into_iter()
|
||||
.map(|container| DockerContainer {
|
||||
id: container.id,
|
||||
image: container.image,
|
||||
name: container.name,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Gets the number of running containers
|
||||
pub async fn get_container_count(&self) -> Result<usize, Box<dyn Error + Send + Sync>> {
|
||||
let containers = container::get_available_containers(&self.docker).await;
|
||||
Ok(containers.len())
|
||||
}
|
||||
|
||||
/// Restarts a specific container by ID
|
||||
pub async fn restart_container(
|
||||
&self,
|
||||
container_id: &str,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
container::restart_container(&self.docker, container_id).await
|
||||
}
|
||||
|
||||
/// Collects Docker metrics for all containers
|
||||
pub async fn collect_metrics(&self) -> Result<DockerMetricDto, Box<dyn Error + Send + Sync>> {
|
||||
let containers = self.get_containers().await?;
|
||||
|
||||
// Get stats with status information
|
||||
let stats_result = stats::get_container_stats(&self.docker).await;
|
||||
let (cpu_stats, net_stats, mem_stats, status_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(), Vec::new())
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
.into_iter()
|
||||
.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
|
||||
.iter()
|
||||
.find(|c| {
|
||||
c.container_id
|
||||
.as_ref()
|
||||
.map(|id| id.starts_with(container_short_id))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.cloned();
|
||||
|
||||
let network = net_stats
|
||||
.iter()
|
||||
.find(|n| {
|
||||
n.container_id
|
||||
.as_ref()
|
||||
.map(|id| id.starts_with(container_short_id))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.cloned();
|
||||
|
||||
let ram = mem_stats
|
||||
.iter()
|
||||
.find(|m| {
|
||||
m.container_id
|
||||
.as_ref()
|
||||
.map(|id| id.starts_with(container_short_id))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.cloned();
|
||||
|
||||
let status = status_stats
|
||||
.iter()
|
||||
.find(|s| {
|
||||
s.container_id
|
||||
.as_ref()
|
||||
.map(|id| id.starts_with(container_short_id))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.cloned(); // Clone the entire ContainerStatusInfo
|
||||
|
||||
// Debug output for this container
|
||||
if cpu.is_none() || network.is_none() || ram.is_none() {
|
||||
println!(
|
||||
"Debug: Container {} - CPU: {:?}, Network: {:?}, RAM: {:?}, Status {:?}",
|
||||
container_short_id,
|
||||
cpu.is_some(),
|
||||
network.is_some(),
|
||||
ram.is_some(),
|
||||
status.is_some()
|
||||
);
|
||||
}
|
||||
|
||||
// Debug output for this container
|
||||
if cpu.is_none() || network.is_none() || ram.is_none() || status.is_none() {
|
||||
println!(
|
||||
"Debug: Container {} - CPU: {:?}, Network: {:?}, RAM: {:?}, Status: {:?}",
|
||||
container_short_id,
|
||||
cpu.is_some(),
|
||||
network.is_some(),
|
||||
ram.is_some(),
|
||||
status.is_some()
|
||||
);
|
||||
}
|
||||
|
||||
DockerContainerInfo {
|
||||
container: Some(container),
|
||||
status,
|
||||
cpu,
|
||||
network,
|
||||
ram,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let container_infos: Vec<DockerCollectMetricDto> = container_infos_total
|
||||
.into_iter()
|
||||
.filter_map(|info| {
|
||||
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),
|
||||
net_out: net.tx_bytes.map(|bytes| bytes as f64),
|
||||
}
|
||||
} else {
|
||||
DockerContainerNetworkDto {
|
||||
net_in: None,
|
||||
net_out: None,
|
||||
}
|
||||
};
|
||||
|
||||
let status_dto = if let Some(status_info) = info.status {
|
||||
DockerContainerStatusDto {
|
||||
status: status_info.status, // Extract the status string
|
||||
}
|
||||
} else {
|
||||
DockerContainerStatusDto { status: None }
|
||||
};
|
||||
|
||||
Some(DockerCollectMetricDto {
|
||||
id: container.id,
|
||||
status: status_dto,
|
||||
cpu: cpu_dto,
|
||||
ram: ram_dto,
|
||||
network: network_dto,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
let dto = DockerMetricDto {
|
||||
server_id: 0, // This should be set by the caller
|
||||
containers: serde_json::to_value(&container_infos)?,
|
||||
};
|
||||
|
||||
Ok(dto)
|
||||
}
|
||||
|
||||
pub async fn create_registration_dto(
|
||||
&self,
|
||||
) -> Result<DockerRegistrationDto, Box<dyn Error + Send + Sync>> {
|
||||
let containers = self.get_containers().await?;
|
||||
|
||||
let container_string = serde_json::to_value(&containers)?;
|
||||
|
||||
let dto = DockerRegistrationDto {
|
||||
server_id: 0, // This will be set by the caller
|
||||
containers: container_string,
|
||||
};
|
||||
|
||||
Ok(dto)
|
||||
}
|
||||
}
|
||||
|
||||
// Keep these as utility functions if needed, but they should use DockerManager internally
|
||||
impl DockerContainer {
|
||||
/// Returns the container ID
|
||||
pub fn id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
|
||||
/// Returns the image name
|
||||
pub fn image(&self) -> &str {
|
||||
&self.image.as_deref().unwrap_or("unknown")
|
||||
}
|
||||
|
||||
/// Returns the container name
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name.as_deref().unwrap_or("unknown")
|
||||
}
|
||||
}
|
||||
59
WatcherAgent/src/docker/serverclientcomm.rs
Normal file
59
WatcherAgent/src/docker/serverclientcomm.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
//! Server-client communication utilities for WatcherAgent
|
||||
//!
|
||||
//! Handles server commands, Docker image updates, and container management using the Bollard library.
|
||||
//!
|
||||
use crate::models::ServerMessage;
|
||||
|
||||
use super::container::{restart_container, update_docker_image};
|
||||
//use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions};
|
||||
use bollard::Docker;
|
||||
use std::error::Error;
|
||||
|
||||
/// Handles a message from the backend server and dispatches the appropriate action.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `docker` - Reference to a Bollard Docker client.
|
||||
/// * `msg` - The server message to handle.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if handled successfully, error otherwise.
|
||||
pub async fn handle_server_message(
|
||||
docker: &Docker,
|
||||
msg: ServerMessage,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let msg = msg.clone();
|
||||
println!("Handling server message: {:?}", msg);
|
||||
|
||||
// Handle different message types
|
||||
match msg.message_type.as_str() {
|
||||
"update_image" => {
|
||||
if let Some(image_name) = msg.data.get("image").and_then(|v| v.as_str()) {
|
||||
println!("Received update command for image: {}", image_name);
|
||||
// Call your update_docker_image function here
|
||||
update_docker_image(docker, image_name).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Missing image name in update message".into())
|
||||
}
|
||||
}
|
||||
"restart_container" => {
|
||||
if let Some(image_name) = msg.data.get("image").and_then(|v| v.as_str()) {
|
||||
println!("Received restart command for image: {}", image_name);
|
||||
// Call your update_docker_image function here
|
||||
restart_container(docker, image_name).await?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Missing image name in update message".into())
|
||||
}
|
||||
}
|
||||
"stop_agent" => {
|
||||
println!("Received stop agent command");
|
||||
// Implement graceful shutdown
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Unknown message type: {}", msg.message_type);
|
||||
Err(format!("Unknown message type: {}", msg.message_type).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
99
WatcherAgent/src/docker/stats/cpu.rs
Normal file
99
WatcherAgent/src/docker/stats/cpu.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
use super::ContainerCpuInfo;
|
||||
use bollard::query_parameters::{ListContainersOptions, StatsOptions};
|
||||
use bollard::Docker;
|
||||
use futures_util::stream::TryStreamExt;
|
||||
use std::error::Error;
|
||||
|
||||
/// Get CPU statistics for all containers
|
||||
pub async fn get_all_containers_cpu_stats(
|
||||
docker: &Docker,
|
||||
) -> Result<Vec<ContainerCpuInfo>, Box<dyn Error + Send + Sync>> {
|
||||
let containers = docker
|
||||
.list_containers(Some(ListContainersOptions {
|
||||
all: true,
|
||||
..Default::default()
|
||||
}))
|
||||
.await?;
|
||||
|
||||
let mut cpu_infos = Vec::new();
|
||||
|
||||
for container in containers {
|
||||
let id = container.id.unwrap_or_default();
|
||||
|
||||
// Skip if no ID
|
||||
if id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(cpu_info) = get_single_container_cpu_stats(docker, &id).await? {
|
||||
cpu_infos.push(cpu_info);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(cpu_infos)
|
||||
}
|
||||
|
||||
/// Get CPU statistics for a specific container
|
||||
pub async fn get_single_container_cpu_stats(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<Option<ContainerCpuInfo>, Box<dyn Error + Send + Sync>> {
|
||||
let mut stats_stream = docker.stats(
|
||||
container_id,
|
||||
Some(StatsOptions {
|
||||
stream: false,
|
||||
one_shot: true,
|
||||
}),
|
||||
);
|
||||
|
||||
if let Some(stats) = stats_stream.try_next().await? {
|
||||
if let (Some(cpu_stats), Some(precpu_stats)) = (&stats.cpu_stats, &stats.precpu_stats) {
|
||||
if let (Some(cpu_usage), Some(pre_cpu_usage)) =
|
||||
(&cpu_stats.cpu_usage, &precpu_stats.cpu_usage)
|
||||
{
|
||||
let cpu_delta = cpu_usage
|
||||
.total_usage
|
||||
.unwrap_or(0)
|
||||
.saturating_sub(pre_cpu_usage.total_usage.unwrap_or(0));
|
||||
|
||||
let system_delta = cpu_stats
|
||||
.system_cpu_usage
|
||||
.unwrap_or(0)
|
||||
.saturating_sub(precpu_stats.system_cpu_usage.unwrap_or(0));
|
||||
|
||||
let online_cpus = cpu_stats.online_cpus.unwrap_or(1);
|
||||
|
||||
let cpu_percent = if system_delta > 0 && online_cpus > 0 {
|
||||
(cpu_delta as f64 / system_delta as f64) * online_cpus as f64 * 100.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
return Ok(Some(ContainerCpuInfo {
|
||||
container_id: Some(container_id.to_string()),
|
||||
cpu_usage_percent: Some(cpu_percent),
|
||||
system_cpu_usage: Some(cpu_stats.system_cpu_usage.unwrap_or(0)),
|
||||
container_cpu_usage: Some(cpu_usage.total_usage.unwrap_or(0)),
|
||||
online_cpus: Some(online_cpus),
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Get average CPU usage across all containers
|
||||
pub async fn get_average_cpu_usage(docker: &Docker) -> Result<f64, Box<dyn Error + Send + Sync>> {
|
||||
let cpu_infos = get_all_containers_cpu_stats(docker).await?;
|
||||
|
||||
if cpu_infos.is_empty() {
|
||||
return Ok(0.0);
|
||||
}
|
||||
|
||||
let total_cpu: f64 = cpu_infos
|
||||
.iter()
|
||||
.map(|cpu| cpu.cpu_usage_percent.unwrap())
|
||||
.sum();
|
||||
Ok(total_cpu / cpu_infos.len() as f64)
|
||||
}
|
||||
101
WatcherAgent/src/docker/stats/mod.rs
Normal file
101
WatcherAgent/src/docker/stats/mod.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
pub mod cpu;
|
||||
pub mod network;
|
||||
pub mod ram;
|
||||
pub mod status;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct ContainerStatusInfo {
|
||||
pub container_id: Option<String>,
|
||||
pub status: Option<String>, // "running", "stopped", "paused", "exited", etc.
|
||||
pub state: Option<String>, // More detailed state information
|
||||
pub started_at: Option<String>,
|
||||
pub finished_at: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct ContainerCpuInfo {
|
||||
pub container_id: Option<String>,
|
||||
pub cpu_usage_percent: Option<f64>,
|
||||
pub system_cpu_usage: Option<u64>,
|
||||
pub container_cpu_usage: Option<u64>,
|
||||
pub online_cpus: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct ContainerNetworkInfo {
|
||||
pub container_id: Option<String>,
|
||||
pub rx_bytes: Option<u64>,
|
||||
pub tx_bytes: Option<u64>,
|
||||
pub rx_packets: Option<u64>,
|
||||
pub tx_packets: Option<u64>,
|
||||
pub rx_errors: Option<u64>,
|
||||
pub tx_errors: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct ContainerMemoryInfo {
|
||||
pub container_id: Option<String>,
|
||||
pub memory_usage: Option<u64>,
|
||||
pub memory_limit: Option<u64>,
|
||||
pub memory_usage_percent: Option<f64>,
|
||||
}
|
||||
|
||||
use bollard::Docker;
|
||||
use std::error::Error;
|
||||
|
||||
/// Get container statistics for all containers using an existing Docker client
|
||||
pub async fn get_container_stats(
|
||||
docker: &Docker,
|
||||
) -> Result<
|
||||
(
|
||||
Vec<ContainerCpuInfo>,
|
||||
Vec<ContainerNetworkInfo>,
|
||||
Vec<ContainerMemoryInfo>,
|
||||
Vec<ContainerStatusInfo>,
|
||||
),
|
||||
Box<dyn Error + Send + Sync>,
|
||||
> {
|
||||
let cpu_infos = cpu::get_all_containers_cpu_stats(docker).await?;
|
||||
let net_infos = network::get_all_containers_network_stats(docker).await?;
|
||||
let mem_infos = ram::get_all_containers_memory_stats(docker).await?;
|
||||
let status_infos = status::get_all_containers_status(docker).await?;
|
||||
|
||||
Ok((cpu_infos, net_infos, mem_infos, status_infos))
|
||||
}
|
||||
|
||||
/// Get container statistics for a specific container
|
||||
pub async fn get_single_container_stats(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<(
|
||||
Option<ContainerCpuInfo>,
|
||||
Option<ContainerNetworkInfo>,
|
||||
Option<ContainerMemoryInfo>,
|
||||
Option<ContainerStatusInfo>,
|
||||
), Box<dyn Error + Send + Sync>> {
|
||||
let cpu_info = cpu::get_single_container_cpu_stats(docker, container_id).await?;
|
||||
let net_info = network::get_single_container_network_stats(docker, container_id).await?;
|
||||
let mem_info = ram::get_single_container_memory_stats(docker, container_id).await?;
|
||||
let status_info = status::get_single_container_status(docker, container_id).await?;
|
||||
|
||||
Ok((cpu_info, net_info, mem_info, status_info))
|
||||
}
|
||||
|
||||
/// Get total network statistics across all containers
|
||||
pub async fn get_total_network_stats(
|
||||
docker: &Docker,
|
||||
) -> Result<(u64, u64), Box<dyn Error + Send + Sync>> {
|
||||
network::get_total_network_stats(docker).await
|
||||
}
|
||||
|
||||
/// Get average CPU usage across all containers
|
||||
pub async fn get_average_cpu_usage(docker: &Docker) -> Result<f64, Box<dyn Error + Send + Sync>> {
|
||||
cpu::get_average_cpu_usage(docker).await
|
||||
}
|
||||
|
||||
/// Get total memory usage across all containers
|
||||
pub async fn get_total_memory_usage(docker: &Docker) -> Result<u64, Box<dyn Error + Send + Sync>> {
|
||||
ram::get_total_memory_usage(docker).await
|
||||
}
|
||||
79
WatcherAgent/src/docker/stats/network.rs
Normal file
79
WatcherAgent/src/docker/stats/network.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use super::ContainerNetworkInfo;
|
||||
use bollard::query_parameters::{ListContainersOptions, StatsOptions};
|
||||
use bollard::Docker;
|
||||
use futures_util::stream::TryStreamExt;
|
||||
use std::error::Error;
|
||||
|
||||
/// Get network statistics for all containers
|
||||
pub async fn get_all_containers_network_stats(
|
||||
docker: &Docker,
|
||||
) -> Result<Vec<ContainerNetworkInfo>, Box<dyn Error + Send + Sync>> {
|
||||
let containers = docker
|
||||
.list_containers(Some(ListContainersOptions {
|
||||
all: true,
|
||||
..Default::default()
|
||||
}))
|
||||
.await?;
|
||||
|
||||
let mut net_infos = Vec::new();
|
||||
|
||||
for container in containers {
|
||||
let id = container.id.unwrap_or_default();
|
||||
|
||||
// Skip if no ID
|
||||
if id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(net_info) = get_single_container_network_stats(docker, &id).await? {
|
||||
net_infos.push(net_info);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(net_infos)
|
||||
}
|
||||
|
||||
/// Get network statistics for a specific container
|
||||
pub async fn get_single_container_network_stats(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<Option<ContainerNetworkInfo>, Box<dyn Error + Send + Sync>> {
|
||||
let mut stats_stream = docker.stats(
|
||||
container_id,
|
||||
Some(StatsOptions {
|
||||
stream: false,
|
||||
one_shot: true,
|
||||
}),
|
||||
);
|
||||
|
||||
if let Some(stats) = stats_stream.try_next().await? {
|
||||
if let Some(networks) = stats.networks {
|
||||
// Take the first network interface (usually eth0)
|
||||
if let Some((_name, net)) = networks.into_iter().next() {
|
||||
return Ok(Some(ContainerNetworkInfo {
|
||||
container_id: Some(container_id.to_string()),
|
||||
rx_bytes: net.rx_bytes,
|
||||
tx_bytes: net.tx_bytes,
|
||||
rx_packets: net.rx_packets,
|
||||
tx_packets: net.tx_packets,
|
||||
rx_errors: net.rx_errors,
|
||||
tx_errors: net.tx_errors,
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Get total network statistics across all containers
|
||||
pub async fn get_total_network_stats(
|
||||
docker: &Docker,
|
||||
) -> Result<(u64, u64), Box<dyn Error + Send + Sync>> {
|
||||
let net_infos = get_all_containers_network_stats(docker).await?;
|
||||
|
||||
let total_rx: u64 = net_infos.iter().map(|net| net.rx_bytes.unwrap()).sum();
|
||||
let total_tx: u64 = net_infos.iter().map(|net| net.tx_bytes.unwrap()).sum();
|
||||
|
||||
Ok((total_rx, total_tx))
|
||||
}
|
||||
77
WatcherAgent/src/docker/stats/ram.rs
Normal file
77
WatcherAgent/src/docker/stats/ram.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use super::ContainerMemoryInfo;
|
||||
use bollard::query_parameters::{ListContainersOptions, StatsOptions};
|
||||
use bollard::Docker;
|
||||
use futures_util::stream::TryStreamExt;
|
||||
use std::error::Error;
|
||||
|
||||
/// Get memory statistics for all containers
|
||||
pub async fn get_all_containers_memory_stats(
|
||||
docker: &Docker,
|
||||
) -> Result<Vec<ContainerMemoryInfo>, Box<dyn Error + Send + Sync>> {
|
||||
let containers = docker
|
||||
.list_containers(Some(ListContainersOptions {
|
||||
all: true,
|
||||
..Default::default()
|
||||
}))
|
||||
.await?;
|
||||
|
||||
let mut mem_infos = Vec::new();
|
||||
|
||||
for container in containers {
|
||||
let id = container.id.unwrap_or_default();
|
||||
|
||||
// Skip if no ID
|
||||
if id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(mem_info) = get_single_container_memory_stats(docker, &id).await? {
|
||||
mem_infos.push(mem_info);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(mem_infos)
|
||||
}
|
||||
|
||||
/// Get memory statistics for a specific container
|
||||
pub async fn get_single_container_memory_stats(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<Option<ContainerMemoryInfo>, Box<dyn Error + Send + Sync>> {
|
||||
let mut stats_stream = docker.stats(
|
||||
container_id,
|
||||
Some(StatsOptions {
|
||||
stream: false,
|
||||
one_shot: true,
|
||||
}),
|
||||
);
|
||||
|
||||
if let Some(stats) = stats_stream.try_next().await? {
|
||||
if let Some(memory_stats) = &stats.memory_stats {
|
||||
let memory_usage = memory_stats.usage.unwrap_or(0);
|
||||
let memory_limit = memory_stats.limit.unwrap_or(1); // Avoid division by zero
|
||||
|
||||
let memory_usage_percent = if memory_limit > 0 {
|
||||
(memory_usage as f64 / memory_limit as f64) * 100.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
return Ok(Some(ContainerMemoryInfo {
|
||||
container_id: Some(container_id.to_string()),
|
||||
memory_usage: Some(memory_usage),
|
||||
memory_limit: Some(memory_limit),
|
||||
memory_usage_percent: Some(memory_usage_percent),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Get total memory usage across all containers
|
||||
pub async fn get_total_memory_usage(docker: &Docker) -> Result<u64, Box<dyn Error + Send + Sync>> {
|
||||
let mem_infos = get_all_containers_memory_stats(docker).await?;
|
||||
let total_memory: u64 = mem_infos.iter().map(|mem| mem.memory_usage.unwrap()).sum();
|
||||
Ok(total_memory)
|
||||
}
|
||||
126
WatcherAgent/src/docker/stats/status.rs
Normal file
126
WatcherAgent/src/docker/stats/status.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use super::ContainerStatusInfo;
|
||||
use std::error::Error;
|
||||
use bollard::Docker;
|
||||
use bollard::query_parameters::{ListContainersOptions, InspectContainerOptions};
|
||||
use bollard::models::{ContainerSummaryStateEnum, ContainerStateStatusEnum};
|
||||
|
||||
/// Get status information for all containers
|
||||
pub async fn get_all_containers_status(
|
||||
docker: &Docker,
|
||||
) -> Result<Vec<ContainerStatusInfo>, Box<dyn Error + Send + Sync>> {
|
||||
|
||||
let containers = docker
|
||||
.list_containers(Some(ListContainersOptions {
|
||||
all: true, // Include stopped containers
|
||||
..Default::default()
|
||||
}))
|
||||
.await?;
|
||||
|
||||
let mut status_infos = Vec::new();
|
||||
|
||||
for container in containers {
|
||||
let id = container.id.unwrap_or_default();
|
||||
|
||||
if id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Convert ContainerSummaryStateEnum to String
|
||||
let status = container.state.map(|state| match state {
|
||||
ContainerSummaryStateEnum::CREATED => "created".to_string(),
|
||||
ContainerSummaryStateEnum::RUNNING => "running".to_string(),
|
||||
ContainerSummaryStateEnum::PAUSED => "paused".to_string(),
|
||||
ContainerSummaryStateEnum::RESTARTING => "restarting".to_string(),
|
||||
ContainerSummaryStateEnum::REMOVING => "removing".to_string(),
|
||||
ContainerSummaryStateEnum::EXITED => "exited".to_string(),
|
||||
ContainerSummaryStateEnum::DEAD => "dead".to_string(),
|
||||
_ => "unknown".to_string(),
|
||||
});
|
||||
|
||||
// Convert timestamp from i64 to String
|
||||
let started_at = container.created.map(|timestamp| timestamp.to_string());
|
||||
|
||||
status_infos.push(ContainerStatusInfo {
|
||||
container_id: Some(id.clone()),
|
||||
status,
|
||||
state: container.status,
|
||||
started_at,
|
||||
finished_at: None, // Docker API doesn't provide finished_at in list
|
||||
});
|
||||
}
|
||||
|
||||
Ok(status_infos)
|
||||
}
|
||||
|
||||
/// Get status information for a specific container
|
||||
pub async fn get_single_container_status(
|
||||
docker: &Docker,
|
||||
container_id: &str,
|
||||
) -> Result<Option<ContainerStatusInfo>, Box<dyn Error + Send + Sync>> {
|
||||
// First try to get from list (faster)
|
||||
let containers = docker
|
||||
.list_containers(Some(ListContainersOptions {
|
||||
all: true,
|
||||
..Default::default()
|
||||
}))
|
||||
.await?;
|
||||
|
||||
if let Some(container) = containers.into_iter().find(|c| {
|
||||
c.id.as_ref().map(|id| id == container_id).unwrap_or(false)
|
||||
}) {
|
||||
// Convert ContainerSummaryStateEnum to String
|
||||
let status = container.state.map(|state| match state {
|
||||
ContainerSummaryStateEnum::CREATED => "created".to_string(),
|
||||
ContainerSummaryStateEnum::RUNNING => "running".to_string(),
|
||||
ContainerSummaryStateEnum::PAUSED => "paused".to_string(),
|
||||
ContainerSummaryStateEnum::RESTARTING => "restarting".to_string(),
|
||||
ContainerSummaryStateEnum::REMOVING => "removing".to_string(),
|
||||
ContainerSummaryStateEnum::EXITED => "exited".to_string(),
|
||||
ContainerSummaryStateEnum::DEAD => "dead".to_string(),
|
||||
_ => "unknown".to_string(),
|
||||
});
|
||||
|
||||
// Convert timestamp from i64 to String
|
||||
let started_at = container.created.map(|timestamp| timestamp.to_string());
|
||||
|
||||
return Ok(Some(ContainerStatusInfo {
|
||||
container_id: Some(container_id.to_string()),
|
||||
status,
|
||||
state: container.status,
|
||||
started_at,
|
||||
finished_at: None,
|
||||
}));
|
||||
}
|
||||
|
||||
// Fallback to inspect for more detailed info
|
||||
match docker.inspect_container(container_id, None::<InspectContainerOptions>).await {
|
||||
Ok(container_details) => {
|
||||
let state = container_details.state.unwrap_or_default();
|
||||
|
||||
// Convert ContainerStateStatusEnum to String
|
||||
let status = state.status.map(|status_enum| match status_enum {
|
||||
ContainerStateStatusEnum::CREATED => "created".to_string(),
|
||||
ContainerStateStatusEnum::RUNNING => "running".to_string(),
|
||||
ContainerStateStatusEnum::PAUSED => "paused".to_string(),
|
||||
ContainerStateStatusEnum::RESTARTING => "restarting".to_string(),
|
||||
ContainerStateStatusEnum::REMOVING => "removing".to_string(),
|
||||
ContainerStateStatusEnum::EXITED => "exited".to_string(),
|
||||
ContainerStateStatusEnum::DEAD => "dead".to_string(),
|
||||
_ => "unknown".to_string(),
|
||||
});
|
||||
|
||||
// These are already Option<String> from the Docker API
|
||||
let started_at = state.clone().started_at;
|
||||
let finished_at = state.clone().finished_at;
|
||||
|
||||
Ok(Some(ContainerStatusInfo {
|
||||
container_id: Some(container_id.to_string()),
|
||||
status,
|
||||
state: Some(format!("{:?}", state)), // Convert state to string
|
||||
started_at,
|
||||
finished_at,
|
||||
}))
|
||||
}
|
||||
Err(_) => Ok(None), // Container not found
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,29 @@ use anyhow::Result;
|
||||
use std::error::Error;
|
||||
use sysinfo::System;
|
||||
|
||||
/// # CPU Hardware Module
|
||||
///
|
||||
/// This module provides CPU information collection for WatcherAgent, including load, temperature, and system uptime.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **CPU Detection:** Identifies CPU model and core count.
|
||||
/// - **Metric Collection:** Queries CPU load, temperature, and uptime.
|
||||
/// - **Error Handling:** Graceful fallback if metrics are unavailable.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `current_load`: CPU usage as a percentage (**0.0–100.0**)
|
||||
/// - `current_temp`: CPU temperature in **degrees Celsius (°C)**
|
||||
/// - `uptime`: System uptime in **seconds (s)**
|
||||
///
|
||||
/// CPU statistics for the host system.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `name`: CPU model name (string)
|
||||
/// - `cores`: Number of physical CPU cores (integer)
|
||||
/// - `current_load`: CPU usage as a percentage (**0.0–100.0**)
|
||||
/// - `current_temp`: CPU temperature in **degrees Celsius (°C)**
|
||||
/// - `uptime`: System uptime in **seconds (s)**
|
||||
/// - `host_name`: Hostname of the system (string)
|
||||
#[derive(Debug)]
|
||||
pub struct CpuInfo {
|
||||
pub name: Option<String>,
|
||||
@@ -12,6 +35,10 @@ pub struct CpuInfo {
|
||||
pub host_name: Option<String>,
|
||||
}
|
||||
|
||||
/// Collects CPU information (model, cores, load, temperature, uptime).
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<CpuInfo, Box<dyn Error + Send + Sync>>` - CPU statistics or error if unavailable.
|
||||
pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
|
||||
let mut sys = System::new_all();
|
||||
|
||||
@@ -33,12 +60,23 @@ pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Queries system for current CPU load (percentage).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `sys` - Mutable reference to sysinfo::System
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - CPU load as percentage.
|
||||
pub async fn get_cpu_load(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
|
||||
sys.refresh_cpu_all();
|
||||
tokio::task::yield_now().await; // Allow other tasks to run
|
||||
Ok(sys.global_cpu_usage() as f64)
|
||||
}
|
||||
|
||||
/// Attempts to read CPU temperature from system sensors (Linux only).
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - CPU temperature in degrees Celsius (°C).
|
||||
pub async fn get_cpu_temp() -> Result<f64, Box<dyn Error + Send + Sync>> {
|
||||
println!("Attempting to get CPU temperature...");
|
||||
|
||||
|
||||
@@ -1,130 +1,165 @@
|
||||
use std::error::Error;
|
||||
use crate::models::DiskInfoDetailed;
|
||||
|
||||
use std::error::Error;
|
||||
use anyhow::Result;
|
||||
use sysinfo::DiskUsage;
|
||||
use sysinfo::{Component, Components, Disk, Disks, System};
|
||||
use sysinfo::{Component, Components, Disk, Disks};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
/// # Disk Hardware Module
|
||||
///
|
||||
/// This module provides disk information collection for WatcherAgent, including total and per-disk statistics and temperature data.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **Disk Enumeration:** Lists all physical disks and their properties.
|
||||
/// - **Usage Calculation:** Computes total and per-disk usage, available space, and usage percentage.
|
||||
/// - **Temperature Monitoring:** Associates disk components with temperature sensors if available.
|
||||
///
|
||||
/// ## Units
|
||||
/// - All sizes are in **bytes** unless otherwise noted.
|
||||
/// - Temperatures are in **degrees Celsius (°C)**.
|
||||
///
|
||||
/// Summary of disk statistics for the system.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `total_size`: Total disk size in bytes (all disks > 100MB)
|
||||
/// - `total_used`: Total used disk space in bytes
|
||||
/// - `total_available`: Total available disk space in bytes
|
||||
/// - `total_usage`: Usage percentage (0.0–100.0)
|
||||
/// - `detailed_info`: Vector of [`DiskInfoDetailed`] for each disk
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct DiskInfo {
|
||||
pub total: Option<f64>,
|
||||
pub used: Option<f64>,
|
||||
pub free: Option<f64>,
|
||||
pub total_size: Option<f64>,
|
||||
pub total_used: Option<f64>,
|
||||
pub total_available: Option<f64>,
|
||||
pub total_usage: Option<f64>,
|
||||
pub detailed_info: Vec<DiskInfoDetailed>,
|
||||
}
|
||||
|
||||
pub async fn get_disk_info() -> Result<DiskInfo> {
|
||||
/// Collects disk information for all detected disks, including usage and temperature.
|
||||
///
|
||||
/// This function enumerates all disks, calculates usage statistics, and attempts to associate temperature sensors with disk components.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<DiskInfo, Box<dyn std::error::Error + Send + Sync>>` - Disk statistics and details, or error if collection fails.
|
||||
pub async fn get_disk_info() -> Result<DiskInfo, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let disks = Disks::new_with_refreshed_list();
|
||||
let _disk_types = [
|
||||
sysinfo::DiskKind::HDD,
|
||||
sysinfo::DiskKind::SSD,
|
||||
sysinfo::DiskKind::Unknown(0),
|
||||
];
|
||||
|
||||
let (_, _, _, _) = get_disk_utitlization().unwrap();
|
||||
|
||||
let mut total = 0;
|
||||
let mut used = 0;
|
||||
let mut detailed_info = Vec::new();
|
||||
|
||||
// Collect detailed disk information
|
||||
for disk in disks.list() {
|
||||
if disk.total_space() > 100 * 1024 * 1024 {
|
||||
// > 100MB
|
||||
total += disk.total_space();
|
||||
used += disk.total_space() - disk.available_space();
|
||||
if disk.kind() == sysinfo::DiskKind::Unknown(0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let disk_used = disk.total_space() - disk.available_space();
|
||||
detailed_info.push(DiskInfoDetailed {
|
||||
disk_name: disk.name().to_string_lossy().into_owned(),
|
||||
disk_kind: format!("{:?}", disk.kind()),
|
||||
disk_total_space: disk.total_space() as f64,
|
||||
disk_available_space: disk.available_space() as f64,
|
||||
disk_used_space: disk_used as f64,
|
||||
disk_mount_point: disk.mount_point().to_string_lossy().into_owned(),
|
||||
component_disk_label: String::new(),
|
||||
component_disk_temperature: 0.0,
|
||||
});
|
||||
}
|
||||
|
||||
// Get component temperatures
|
||||
let components = Components::new_with_refreshed_list();
|
||||
for component in &components {
|
||||
if let Some(temperature) = component.temperature() {
|
||||
// Update detailed info with temperature data if it matches a disk component
|
||||
for disk_info in &mut detailed_info {
|
||||
if component.label().contains(&disk_info.disk_name) {
|
||||
disk_info.component_disk_label = component.label().to_string();
|
||||
disk_info.component_disk_temperature = temperature;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate totals (only disks > 100MB)
|
||||
let (total_size, total_used, total_available) = calculate_disk_totals(&disks);
|
||||
|
||||
let (total_size, total_used, total_available, total_usage) = if total_size > 0.0 {
|
||||
(total_size, total_used, total_available, (total_used / total_size) * 100.0)
|
||||
} else {
|
||||
match get_disk_info_fallback() {
|
||||
Ok(fallback_data) => fallback_data,
|
||||
Err(_) => (0.0, 0.0, 0.0, 0.0), // Default values if fallback fails
|
||||
}
|
||||
};
|
||||
|
||||
Ok(DiskInfo {
|
||||
total: Some(total as f64),
|
||||
used: Some(used as f64),
|
||||
free: Some((total - used) as f64),
|
||||
total_size: if total_size > 0.0 { Some(total_size) } else { None },
|
||||
total_used: if total_used > 0.0 { Some(total_used) } else { None },
|
||||
total_available: if total_available > 0.0 { Some(total_available) } else { None },
|
||||
total_usage: if total_usage > 0.0 { Some(total_usage) } else { None },
|
||||
detailed_info,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_disk_utitlization() -> Result<(f64, f64, f64, f64), Box<dyn Error>> {
|
||||
let mut sys = System::new();
|
||||
sys.refresh_all();
|
||||
let mut count = 0;
|
||||
|
||||
fn calculate_disk_totals(disks: &Disks) -> (f64, f64, f64) {
|
||||
let mut total_size = 0u64;
|
||||
let mut total_used = 0u64;
|
||||
let mut total_available = 0u64;
|
||||
|
||||
let disks = Disks::new_with_refreshed_list();
|
||||
for disk in disks.list() {
|
||||
// Only print disks with known kind
|
||||
if disk.kind() == sysinfo::DiskKind::Unknown(0) {
|
||||
continue;
|
||||
}
|
||||
println!(
|
||||
"Disk_Name: {:?}:\n---- Disk_Kind: {},\n---- Total: {},\n---- Available: {},\n---- Used: {}, \n---- Mount_Point: {:?}",
|
||||
disk.name(),
|
||||
disk.kind(),
|
||||
disk.total_space(),
|
||||
disk.available_space(),
|
||||
disk.total_space() - disk.available_space(),
|
||||
disk.mount_point()
|
||||
);
|
||||
}
|
||||
let components = Components::new_with_refreshed_list();
|
||||
for component in &components {
|
||||
if let Some(temperature) = component.temperature() {
|
||||
println!(
|
||||
"Component_Label: {}, Temperature: {}°C",
|
||||
component.label(),
|
||||
temperature
|
||||
);
|
||||
if disk.total_space() > 100 * 1024 * 1024 { // > 100MB
|
||||
total_size += disk.total_space();
|
||||
total_available += disk.available_space();
|
||||
total_used += disk.total_space() - disk.available_space();
|
||||
}
|
||||
}
|
||||
|
||||
// Berechnungen
|
||||
let total_size = if count > 0 {
|
||||
total_size as f64 // in Bytes
|
||||
} else {
|
||||
// Fallback: Versuche df unter Linux
|
||||
println!("Fallback: Using 'df' command to get disk info.");
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
use std::process::Command;
|
||||
if let Ok(output) = Command::new("df")
|
||||
.arg("-B1")
|
||||
.arg("--output=size,used")
|
||||
.output()
|
||||
{
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
for line in stdout.lines().skip(1) {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() == 2 {
|
||||
if let (Ok(size), Ok(used)) =
|
||||
(parts[0].parse::<u64>(), parts[1].parse::<u64>())
|
||||
{
|
||||
total_size += size;
|
||||
total_used += used;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
total_size as f64 // in Bytes
|
||||
} else {
|
||||
0.0
|
||||
(total_size as f64, total_used as f64, total_available as f64)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn get_disk_info_fallback() -> Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>> {
|
||||
use std::process::Command;
|
||||
|
||||
let output = Command::new("df")
|
||||
.arg("-B1")
|
||||
.arg("--output=size,used,avail")
|
||||
.output()?;
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let mut total_size = 0u64;
|
||||
let mut total_used = 0u64;
|
||||
let mut total_available = 0u64;
|
||||
let mut count = 0;
|
||||
|
||||
for line in stdout.lines().skip(1) {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 3 {
|
||||
if let (Ok(size), Ok(used), Ok(avail)) = (
|
||||
parts[0].parse::<u64>(),
|
||||
parts[1].parse::<u64>(),
|
||||
parts[2].parse::<u64>(),
|
||||
) {
|
||||
total_size += size;
|
||||
total_used += used;
|
||||
total_available += avail;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
{
|
||||
0.0
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let usage = if total_size > 0.0 {
|
||||
let usage = if total_size > 0 {
|
||||
(total_used as f64 / total_size as f64) * 100.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
Ok((
|
||||
total_size,
|
||||
total_used as f64,
|
||||
total_available as f64,
|
||||
usage as f64,
|
||||
)) // Disk-Temp bleibt 0.0 ohne spezielle Hardware
|
||||
Ok((total_size as f64, total_used as f64, total_available as f64, usage))
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn get_disk_info_fallback() -> Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>> {
|
||||
Ok((0.0, 0.0, 0.0, 0.0))
|
||||
}
|
||||
|
||||
pub fn _get_disk_temp_for_component(component: &Component) -> Option<f64> {
|
||||
|
||||
@@ -2,6 +2,29 @@ use anyhow::Result;
|
||||
use nvml_wrapper::Nvml;
|
||||
use std::error::Error;
|
||||
|
||||
/// # GPU Hardware Module
|
||||
///
|
||||
/// This module provides GPU information collection for WatcherAgent, including load, temperature, and VRAM statistics.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **GPU Detection:** Identifies GPU model and capabilities.
|
||||
/// - **Metric Collection:** Queries GPU load, temperature, and VRAM usage using NVML (NVIDIA only).
|
||||
/// - **Error Handling:** Graceful fallback if GPU or NVML is unavailable.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `current_load`: GPU usage as a percentage (**0.0–100.0**)
|
||||
/// - `current_temp`: GPU temperature in **degrees Celsius (°C)**
|
||||
/// - `vram_total`: Total VRAM in **bytes**
|
||||
/// - `vram_used`: Used VRAM in **bytes**
|
||||
///
|
||||
/// GPU statistics for the host system.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `name`: GPU model name (string)
|
||||
/// - `current_load`: GPU usage as a percentage (**0.0–100.0**)
|
||||
/// - `current_temp`: GPU temperature in **degrees Celsius (°C)**
|
||||
/// - `vram_total`: Total VRAM in **bytes**
|
||||
/// - `vram_used`: Used VRAM in **bytes**
|
||||
#[derive(Debug)]
|
||||
pub struct GpuInfo {
|
||||
pub name: Option<String>,
|
||||
@@ -11,6 +34,12 @@ pub struct GpuInfo {
|
||||
pub vram_used: Option<f64>,
|
||||
}
|
||||
|
||||
/// Collects GPU information (load, temperature, VRAM) using NVML.
|
||||
///
|
||||
/// This function attempts to query the first NVIDIA GPU using NVML. If unavailable, it returns a fallback with only the detected GPU name.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<GpuInfo, Box<dyn Error + Send + Sync>>` - GPU statistics or fallback if unavailable.
|
||||
pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error + Send + Sync>> {
|
||||
match get_gpu_metrics() {
|
||||
Ok((gpu_temp, gpu_load, vram_used, vram_total)) => {
|
||||
@@ -37,6 +66,10 @@ pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error + Send + Sync>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Queries NVML for GPU metrics: temperature, load, VRAM used/total.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>>` - Tuple of (temperature °C, load %, VRAM used bytes, VRAM total bytes).
|
||||
pub fn get_gpu_metrics() -> Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>> {
|
||||
let nvml = Nvml::init();
|
||||
if let Ok(nvml) = nvml {
|
||||
|
||||
@@ -3,25 +3,56 @@ use std::error::Error;
|
||||
use anyhow::Result;
|
||||
use sysinfo::System;
|
||||
|
||||
/// # Memory Hardware Module
|
||||
///
|
||||
/// This module provides memory information collection for WatcherAgent, including total, used, and free RAM.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **Memory Detection:** Queries system for total, used, and free RAM.
|
||||
/// - **Usage Calculation:** Computes memory usage percentage.
|
||||
/// - **Error Handling:** Graceful fallback if metrics are unavailable.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `total`, `used`, `free`: RAM in **bytes**
|
||||
///
|
||||
/// Memory statistics for the host system.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `total`: Total RAM in **bytes**
|
||||
/// - `used`: Used RAM in **bytes**
|
||||
/// - `free`: Free RAM in **bytes**
|
||||
#[derive(Debug)]
|
||||
pub struct MemoryInfo {
|
||||
pub total: Option<f64>,
|
||||
pub total_size: Option<f64>,
|
||||
pub used: Option<f64>,
|
||||
pub free: Option<f64>,
|
||||
pub current_load: Option<f64>,
|
||||
}
|
||||
|
||||
pub async fn get_memory_info() -> Result<MemoryInfo> {
|
||||
/// Collects memory information (total, used, free RAM).
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<MemoryInfo>` - Memory statistics or error if unavailable.
|
||||
pub async fn get_memory_info() -> Result<MemoryInfo, Box<dyn Error + Send + Sync>> {
|
||||
let mut sys = System::new();
|
||||
sys.refresh_memory();
|
||||
|
||||
Ok(MemoryInfo {
|
||||
total: Some(sys.total_memory() as f64),
|
||||
total_size: Some(sys.total_memory() as f64),
|
||||
used: Some(sys.used_memory() as f64),
|
||||
free: Some(sys.free_memory() as f64),
|
||||
current_load: Some(get_memory_usage(&mut sys).unwrap() as f64)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn _get_memory_usage(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
|
||||
/// Computes memory usage percentage from sysinfo::System.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `sys` - Mutable reference to sysinfo::System
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - Memory usage as percentage.
|
||||
pub fn get_memory_usage(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
|
||||
sys.refresh_memory();
|
||||
Ok((sys.used_memory() as f64 / sys.total_memory() as f64) * 100.0)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,23 @@ pub use memory::get_memory_info;
|
||||
pub use network::get_network_info;
|
||||
pub use network::NetworkMonitor;
|
||||
|
||||
/// # Hardware Module
|
||||
///
|
||||
/// This module aggregates all hardware subsystems for WatcherAgent, providing unified collection and access to CPU, GPU, memory, disk, and network statistics.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **Subsystem Aggregation:** Combines all hardware modules into a single struct for easy access.
|
||||
/// - **Unified Collection:** Provides a single async method to collect all hardware metrics at once.
|
||||
///
|
||||
/// Aggregated hardware statistics for the host system.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `cpu`: CPU statistics (see [`CpuInfo`])
|
||||
/// - `gpu`: GPU statistics (see [`GpuInfo`])
|
||||
/// - `memory`: Memory statistics (see [`MemoryInfo`])
|
||||
/// - `disk`: Disk statistics (see [`DiskInfo`])
|
||||
/// - `network`: Network statistics (see [`NetworkInfo`])
|
||||
/// - `network_monitor`: Rolling monitor for network bandwidth
|
||||
#[derive(Debug)]
|
||||
pub struct HardwareInfo {
|
||||
pub cpu: cpu::CpuInfo,
|
||||
@@ -25,6 +42,10 @@ pub struct HardwareInfo {
|
||||
}
|
||||
|
||||
impl HardwareInfo {
|
||||
/// Collects all hardware statistics asynchronously.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<HardwareInfo, Box<dyn Error + Send + Sync>>` - Aggregated hardware statistics or error if any subsystem fails.
|
||||
pub async fn collect() -> Result<Self, Box<dyn Error + Send + Sync>> {
|
||||
let mut network_monitor = network::NetworkMonitor::new();
|
||||
Ok(Self {
|
||||
|
||||
@@ -2,6 +2,24 @@ use std::error::Error;
|
||||
use std::result::Result;
|
||||
use std::time::Instant;
|
||||
|
||||
/// # Network Hardware Module
|
||||
///
|
||||
/// This module provides network information collection for WatcherAgent, including interface enumeration and bandwidth statistics.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **Interface Detection:** Lists all network interfaces.
|
||||
/// - **Bandwidth Monitoring:** Tracks receive/transmit rates using a rolling monitor.
|
||||
/// - **Error Handling:** Graceful fallback if metrics are unavailable.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `rx_rate`, `tx_rate`: Network bandwidth in **bytes per second (B/s)**
|
||||
///
|
||||
/// Network statistics for the host system.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `interfaces`: List of network interface names (strings)
|
||||
/// - `rx_rate`: Receive bandwidth in **bytes per second (B/s)**
|
||||
/// - `tx_rate`: Transmit bandwidth in **bytes per second (B/s)**
|
||||
#[derive(Debug)]
|
||||
pub struct NetworkInfo {
|
||||
pub interfaces: Option<Vec<String>>,
|
||||
@@ -9,6 +27,13 @@ pub struct NetworkInfo {
|
||||
pub tx_rate: Option<f64>,
|
||||
}
|
||||
|
||||
|
||||
/// Rolling monitor for network bandwidth statistics.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `prev_rx`: Previous received bytes
|
||||
/// - `prev_tx`: Previous transmitted bytes
|
||||
/// - `last_update`: Timestamp of last update
|
||||
#[derive(Debug)]
|
||||
pub struct NetworkMonitor {
|
||||
prev_rx: u64,
|
||||
@@ -23,6 +48,7 @@ impl Default for NetworkMonitor {
|
||||
}
|
||||
|
||||
impl NetworkMonitor {
|
||||
/// Creates a new `NetworkMonitor` for bandwidth tracking.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
prev_rx: 0,
|
||||
@@ -31,6 +57,10 @@ impl NetworkMonitor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates the network usage statistics and returns current rx/tx rates.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(f64, f64), Box<dyn Error>>` - Tuple of (rx_rate, tx_rate) in bytes per second.
|
||||
pub fn update_usage(&mut self) -> Result<(f64, f64), Box<dyn Error>> {
|
||||
let (current_rx, current_tx) = get_network_bytes()?;
|
||||
let elapsed = self.last_update.elapsed().as_secs_f64();
|
||||
@@ -55,6 +85,13 @@ impl NetworkMonitor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects network information (interfaces, rx/tx rates) using a monitor.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `monitor` - Mutable reference to a `NetworkMonitor`
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<NetworkInfo, Box<dyn Error>>` - Network statistics or error if unavailable.
|
||||
pub async fn get_network_info(monitor: &mut NetworkMonitor) -> Result<NetworkInfo, Box<dyn Error>> {
|
||||
let (rx_rate, tx_rate) = monitor.update_usage()?;
|
||||
Ok(NetworkInfo {
|
||||
|
||||
@@ -1,18 +1,53 @@
|
||||
/// WatcherAgent - A Rust-based system monitoring agent
|
||||
/// This agent collects hardware metrics and sends them to a backend server.
|
||||
/// It supports CPU, GPU, RAM, disk, and network metrics.
|
||||
/// # WatcherAgent
|
||||
///
|
||||
/// **WatcherAgent** is a cross-platform system monitoring agent written in Rust.
|
||||
///
|
||||
/// ## Overview
|
||||
/// This agent collects real-time hardware metrics (CPU, GPU, RAM, disk, network) and communicates with a backend server for registration, reporting, and remote control. It is designed for deployment in environments where automated monitoring and remote management of system resources is required.
|
||||
///
|
||||
/// ## Features
|
||||
/// - **Hardware Metrics:** Collects CPU, GPU, RAM, disk, and network statistics using platform-specific APIs.
|
||||
/// - **Docker Integration:** Detects and manages its own Docker container, supports image updates and container restarts.
|
||||
/// - **Server Communication:** Registers with a backend server, sends periodic heartbeats, and reports metrics securely.
|
||||
/// - **Remote Commands:** Listens for and executes commands from the backend (e.g., update image, restart container, stop agent).
|
||||
///
|
||||
/// ## Modules
|
||||
/// - [`api`]: Handles HTTP communication with the backend server (registration, heartbeat, metrics, commands).
|
||||
/// - [`hardware`]: Collects hardware metrics from the host system (CPU, GPU, RAM, disk, network).
|
||||
/// - [`metrics`]: Orchestrates metric collection and reporting.
|
||||
/// - [`models`]: Defines data structures for server communication and metrics.
|
||||
/// - [`docker`]: Integrates with Docker for container management and agent lifecycle.
|
||||
///
|
||||
/// ## Usage
|
||||
/// Run the agent with the backend server URL as an argument:
|
||||
/// ```sh
|
||||
/// watcheragent <server-url>
|
||||
/// ```
|
||||
///
|
||||
/// The agent will register itself, start collecting metrics, and listen for remote commands.
|
||||
pub mod api;
|
||||
pub mod docker;
|
||||
pub mod hardware;
|
||||
pub mod metrics;
|
||||
pub mod models;
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::marker::Send;
|
||||
use std::marker::Sync;
|
||||
use std::result::Result;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
/// Awaits a spawned asynchronous task and flattens its nested `Result` type.
|
||||
///
|
||||
/// This utility is used to handle the result of a `tokio::spawn`ed task that itself returns a `Result`,
|
||||
/// propagating any errors from both the task and its execution.
|
||||
///
|
||||
/// # Type Parameters
|
||||
/// * `T` - The type returned by the task on success.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `handle` - The `JoinHandle` of the spawned task.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<T, Box<dyn Error + Send + Sync>>` - The result of the task, or an error if the task failed or panicked.
|
||||
async fn flatten<T>(
|
||||
handle: JoinHandle<Result<T, Box<dyn Error + Send + Sync>>>,
|
||||
) -> Result<T, Box<dyn Error + Send + Sync>> {
|
||||
@@ -23,29 +58,85 @@ async fn flatten<T>(
|
||||
}
|
||||
}
|
||||
|
||||
/// Main entry point for the WatcherAgent application.
|
||||
///
|
||||
/// This function performs the following steps:
|
||||
/// 1. Initializes the Docker client for container management.
|
||||
/// 2. Detects the current running image version.
|
||||
/// 3. Parses command-line arguments to obtain the backend server URL.
|
||||
/// 4. Registers the agent with the backend server and retrieves its server ID and IP address.
|
||||
/// 5. Spawns background tasks for:
|
||||
/// - Listening for remote commands from the server
|
||||
/// - Sending periodic heartbeat signals
|
||||
/// - Collecting and reporting hardware metrics
|
||||
/// 6. Waits for all background tasks to complete and logs their results.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `server-url` - The URL of the backend server to register and report metrics to (passed as a command-line argument).
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns an error if registration or any background task fails, or if required arguments are missing.
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
// Parse command-line arguments
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
// args[0] is the binary name, args[1] is the first actual argument
|
||||
if args.len() < 2 {
|
||||
eprintln!("Usage: {} <server-url>", args[0]);
|
||||
return Err("Missing server URL argument".into());
|
||||
}
|
||||
|
||||
let server_url = &args[1];
|
||||
println!("Server URL: {:?}", server_url);
|
||||
|
||||
// Registration
|
||||
// Registration with backend server
|
||||
let (server_id, ip) = match api::register_with_server(&server_url).await {
|
||||
Ok((id, ip)) => (id, ip),
|
||||
Ok((id, ip)) => {
|
||||
println!("Registered with server. ID: {}, IP: {}", id, ip);
|
||||
(id, ip)
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Fehler bei der Registrierung am Server: {e}");
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize Docker (optional - agent can run without Docker)
|
||||
let docker_manager = docker::DockerManager::new_optional();
|
||||
|
||||
// Get current image version
|
||||
let client_version = if let Some(ref docker_manager) = docker_manager {
|
||||
docker_manager.get_client_version().await
|
||||
} else {
|
||||
"unknown".to_string()
|
||||
};
|
||||
println!("Client Version: {}", client_version);
|
||||
|
||||
// Prepare Docker registration DTO
|
||||
let container_dto = if let Some(ref docker_manager) = docker_manager {
|
||||
docker_manager.create_registration_dto().await?
|
||||
} else {
|
||||
println!("Fallback for failing registration");
|
||||
models::DockerRegistrationDto {
|
||||
server_id: 0,
|
||||
//container_count: 0, --- IGNORE ---
|
||||
containers: serde_json::to_value(&"")?,
|
||||
}
|
||||
};
|
||||
let _ =
|
||||
api::broadcast_docker_containers(server_url, server_id, &mut container_dto.clone()).await?;
|
||||
|
||||
// Start background tasks
|
||||
// Start server listening for commands (only if Docker is available)
|
||||
let listening_handle = if let Some(ref docker_manager) = docker_manager {
|
||||
tokio::spawn({
|
||||
let docker = docker_manager.docker.clone();
|
||||
let server_url = server_url.to_string();
|
||||
async move { api::listening_to_server(&docker, &server_url).await }
|
||||
})
|
||||
} else {
|
||||
println!("Docker not available, skipping server command listener.");
|
||||
tokio::spawn(async { Ok(()) }) // Dummy task
|
||||
};
|
||||
|
||||
// Start heartbeat in background
|
||||
let heartbeat_handle = tokio::spawn({
|
||||
let ip = ip.clone();
|
||||
@@ -58,20 +149,29 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let metrics_handle = tokio::spawn({
|
||||
let ip = ip.clone();
|
||||
let server_url = server_url.to_string();
|
||||
let docker_manager = docker_manager.as_ref().cloned().unwrap();
|
||||
async move {
|
||||
let mut collector = metrics::Collector::new(server_id, ip);
|
||||
collector.run(&server_url).await
|
||||
let mut collector = metrics::Collector::new(server_id, ip, docker_manager);
|
||||
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(())
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Warte auf beide Tasks und prüfe explizit auf Fehler
|
||||
let (heartbeat_handle, metrics_handle) =
|
||||
tokio::try_join!(flatten(heartbeat_handle), flatten(metrics_handle))?;
|
||||
// Wait for all tasks and check for errors
|
||||
let (listening_result, heartbeat_result, metrics_result) = tokio::try_join!(
|
||||
flatten(listening_handle),
|
||||
flatten(heartbeat_handle),
|
||||
flatten(metrics_handle)
|
||||
)?;
|
||||
|
||||
let (heartbeat, metrics) = (heartbeat_handle, metrics_handle);
|
||||
println!(
|
||||
"All tasks completed successfully: {:?}, {:?}.",
|
||||
heartbeat, metrics
|
||||
"All tasks completed: listening={:?}, heartbeat={:?}, metrics={:?}",
|
||||
listening_result, heartbeat_result, metrics_result
|
||||
);
|
||||
|
||||
println!("All tasks completed successfully.");
|
||||
|
||||
@@ -1,26 +1,66 @@
|
||||
/// # Metrics Module
|
||||
///
|
||||
/// This module orchestrates the collection and reporting of hardware and network metrics for WatcherAgent.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **Metric Collection:** Gathers real-time statistics from all hardware subsystems (CPU, GPU, RAM, disk, network).
|
||||
/// - **Reporting:** Periodically sends metrics to the backend server using the API module.
|
||||
/// - **Error Handling:** Robust to hardware failures and network errors, with retry logic and logging.
|
||||
///
|
||||
/// ## Usage
|
||||
/// The [`Collector`] struct is instantiated in the main loop and runs as a background task, continuously collecting and reporting metrics.
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::api;
|
||||
use crate::docker::DockerManager;
|
||||
//use crate::docker::DockerInfo;
|
||||
use crate::hardware::network::NetworkMonitor;
|
||||
use crate::hardware::HardwareInfo;
|
||||
use crate::models::MetricDto;
|
||||
use crate::models::{DockerMetricDto, MetricDto};
|
||||
|
||||
/// Main orchestrator for hardware and network metric collection and reporting.
|
||||
///
|
||||
/// The `Collector` struct manages the state required to collect metrics and send them to the backend server. It maintains a network monitor for bandwidth tracking, the agent's server ID, and its IP address.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `network_monitor`: Tracks network usage rates (rx/tx).
|
||||
/// - `server_id`: Unique server ID assigned by the backend.
|
||||
/// - `ip_address`: IP address of the agent.
|
||||
pub struct Collector {
|
||||
docker_manager: DockerManager,
|
||||
network_monitor: NetworkMonitor,
|
||||
server_id: i32,
|
||||
server_id: u16,
|
||||
ip_address: String,
|
||||
}
|
||||
|
||||
impl Collector {
|
||||
pub fn new(server_id: i32, ip_address: String) -> Self {
|
||||
/// Creates a new `Collector` instance for metric collection and reporting.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `server_id` - The server ID assigned by the backend.
|
||||
/// * `ip_address` - The IP address of the agent.
|
||||
///
|
||||
/// # Returns
|
||||
/// A new `Collector` ready to collect and report metrics.
|
||||
pub fn new(server_id: u16, ip_address: String, docker_manager: DockerManager) -> Self {
|
||||
Self {
|
||||
docker_manager,
|
||||
network_monitor: NetworkMonitor::new(),
|
||||
server_id,
|
||||
ip_address,
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs the main metrics collection loop, periodically sending metrics to the backend server.
|
||||
///
|
||||
/// This function continuously collects hardware and network metrics, sends them to the backend, and handles errors gracefully. It uses a configurable interval and retries on failures.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `base_url` - The base URL of the backend server.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if metrics are sent successfully.
|
||||
pub async fn run(&mut self, base_url: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
loop {
|
||||
println!(
|
||||
@@ -35,11 +75,26 @@ impl Collector {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let docker_metrics = match self.docker_collect().await {
|
||||
Ok(metrics) => metrics,
|
||||
Err(e) => {
|
||||
eprintln!("Error collecting docker metrics: {}", e);
|
||||
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
api::send_metrics(base_url, &metrics).await?;
|
||||
api::send_docker_metrics(base_url, &docker_metrics).await?;
|
||||
tokio::time::sleep(Duration::from_secs(20)).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects hardware and network metrics from all subsystems.
|
||||
///
|
||||
/// This function queries the hardware module for CPU, GPU, RAM, disk, and network statistics, and packages them into a [`MetricDto`] for reporting.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<MetricDto, Box<dyn Error + Send + Sync>>` - The collected metrics or an error if hardware info is unavailable.
|
||||
pub async fn collect(&mut self) -> Result<MetricDto, Box<dyn Error + Send + Sync>> {
|
||||
let hardware = match HardwareInfo::collect().await {
|
||||
Ok(hw) => hw,
|
||||
@@ -59,14 +114,24 @@ impl Collector {
|
||||
gpu_load: hardware.gpu.current_load.unwrap_or_default(),
|
||||
gpu_temp: hardware.gpu.current_temp.unwrap_or_default(),
|
||||
gpu_vram_size: hardware.gpu.vram_total.unwrap_or_default(),
|
||||
gpu_vram_usage: hardware.gpu.vram_used.unwrap_or_default(),
|
||||
ram_load: hardware.memory.used.unwrap_or_default(),
|
||||
ram_size: hardware.memory.total.unwrap_or_default(),
|
||||
disk_size: hardware.disk.total.unwrap_or_default(),
|
||||
disk_usage: hardware.disk.used.unwrap_or_default(),
|
||||
gpu_vram_load: hardware.gpu.current_load.unwrap_or_default(),
|
||||
ram_load: hardware.memory.current_load.unwrap_or_default(),
|
||||
ram_size: hardware.memory.total_size.unwrap_or_default(),
|
||||
disk_size: hardware.disk.total_size.unwrap_or_default(),
|
||||
disk_usage: hardware.disk.total_usage.unwrap_or_default(),
|
||||
disk_temp: 0.0, // not supported
|
||||
net_rx: hardware.network.rx_rate.unwrap_or_default(),
|
||||
net_tx: hardware.network.tx_rate.unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
|
||||
/// NOTE: This is a compilation-safe stub. Implement the Docker collection using your
|
||||
/// DockerManager API and container helpers when available.
|
||||
pub async fn docker_collect(&self) -> Result<DockerMetricDto, Box<dyn Error + Send + Sync>> {
|
||||
let metrics = self.docker_manager.collect_metrics().await?;
|
||||
Ok(DockerMetricDto {
|
||||
server_id: self.server_id,
|
||||
containers: metrics.containers,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,32 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
/// # Models Module
|
||||
///
|
||||
/// This module defines all data structures (DTOs) used for communication between WatcherAgent and the backend server, as well as hardware metrics and Docker container info.
|
||||
///
|
||||
/// ## Responsibilities
|
||||
/// - **DTOs:** Define payloads for registration, metrics, heartbeat, and server commands.
|
||||
/// - **Units:** All struct fields are documented with their units for clarity and API compatibility.
|
||||
/// - **Docker Info:** Structures for representing Docker container state and statistics.
|
||||
///
|
||||
/// ## Usage
|
||||
/// These types are serialized/deserialized for HTTP communication and used throughout the agent for data exchange.
|
||||
use crate::docker::stats;
|
||||
|
||||
// Data structures matching the C# DTOs
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
/// Registration data sent to the backend server.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `id`: Unique server identifier (integer)
|
||||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||||
/// - `cpu_type`: CPU model name (string)
|
||||
/// - `cpu_cores`: Number of physical CPU cores (integer)
|
||||
/// - `gpu_type`: GPU model name (string)
|
||||
/// - `ram_size`: Total RAM size in **megabytes (MB)**
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct RegistrationDto {
|
||||
#[serde(rename = "id")]
|
||||
pub id: i32,
|
||||
pub server_id: u16,
|
||||
#[serde(rename = "ipAddress")]
|
||||
pub ip_address: String,
|
||||
#[serde(rename = "cpuType")]
|
||||
@@ -17,10 +39,28 @@ pub struct RegistrationDto {
|
||||
pub ram_size: f64,
|
||||
}
|
||||
|
||||
/// Hardware and network metrics data sent to the backend server.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `server_id`: Unique server identifier (integer)
|
||||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||||
/// - `cpu_load`: CPU usage as a percentage (**0.0–100.0**)
|
||||
/// - `cpu_temp`: CPU temperature in **degrees Celsius (°C)**
|
||||
/// - `gpu_load`: GPU usage as a percentage (**0.0–100.0**)
|
||||
/// - `gpu_temp`: GPU temperature in **degrees Celsius (°C)**
|
||||
/// - `gpu_vram_size`: Total GPU VRAM in **bytes**
|
||||
/// - `gpu_vram_load`: GPU Usage of VRAM as a percentage (**0.0–100.0**)
|
||||
/// - `ram_load`: RAM usage as a percentage (**0.0–100.0**)
|
||||
/// - `ram_size`: Total RAM in **bytes**
|
||||
/// - `disk_size`: Total disk size in **bytes**
|
||||
/// - `disk_usage`: Used disk space in **bytes**
|
||||
/// - `disk_temp`: Disk temperature in **degrees Celsius (°C)** (if available)
|
||||
/// - `net_rx`: Network receive rate in **bytes per second (B/s)**
|
||||
/// - `net_tx`: Network transmit rate in **bytes per second (B/s)**
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct MetricDto {
|
||||
#[serde(rename = "serverId")]
|
||||
pub server_id: i32,
|
||||
pub server_id: u16,
|
||||
#[serde(rename = "ipAddress")]
|
||||
pub ip_address: String,
|
||||
#[serde(rename = "cpu_Load")]
|
||||
@@ -33,8 +73,8 @@ pub struct MetricDto {
|
||||
pub gpu_temp: f64,
|
||||
#[serde(rename = "gpu_Vram_Size")]
|
||||
pub gpu_vram_size: f64,
|
||||
#[serde(rename = "gpu_Vram_Usage")]
|
||||
pub gpu_vram_usage: f64,
|
||||
#[serde(rename = "gpu_Vram_Load")]
|
||||
pub gpu_vram_load: f64,
|
||||
#[serde(rename = "ram_Load")]
|
||||
pub ram_load: f64,
|
||||
#[serde(rename = "ram_Size")]
|
||||
@@ -51,19 +91,55 @@ pub struct MetricDto {
|
||||
pub net_tx: f64,
|
||||
}
|
||||
|
||||
/// Detailed disk information for each detected disk.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `disk_total_space`: Total disk space in **bytes**
|
||||
/// - `disk_available_space`: Available disk space in **bytes**
|
||||
/// - `disk_used_space`: Used disk space in **bytes**
|
||||
/// - `component_disk_temperature`: Disk temperature in **degrees Celsius (°C)**
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct DiskInfoDetailed {
|
||||
pub disk_name: String,
|
||||
pub disk_kind: String,
|
||||
pub disk_total_space: f64,
|
||||
pub disk_available_space: f64,
|
||||
pub disk_used_space: f64,
|
||||
pub disk_mount_point: String,
|
||||
pub component_disk_label: String,
|
||||
pub component_disk_temperature: f32,
|
||||
}
|
||||
|
||||
/// Response containing server ID and IP address.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `id`: Unique server identifier (integer)
|
||||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||||
#[derive(Deserialize)]
|
||||
pub struct IdResponse {
|
||||
pub id: i32,
|
||||
pub id: u16,
|
||||
#[serde(rename = "ipAddress")]
|
||||
pub ip_address: String,
|
||||
}
|
||||
|
||||
/// Heartbeat message data sent to the backend server.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||||
#[derive(Serialize)]
|
||||
pub struct HeartbeatDto {
|
||||
#[serde(rename = "IpAddress")]
|
||||
pub ip_address: String,
|
||||
}
|
||||
|
||||
/// Hardware summary data for diagnostics and registration.
|
||||
///
|
||||
/// ## Units
|
||||
/// - `cpu_type`: CPU model name (string)
|
||||
/// - `cpu_cores`: Number of physical CPU cores (integer)
|
||||
/// - `gpu_type`: GPU model name (string)
|
||||
/// - `ram_size`: Total RAM size in **megabytes (MB)**
|
||||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct HardwareDto {
|
||||
pub cpu_type: String,
|
||||
@@ -72,3 +148,126 @@ pub struct HardwareDto {
|
||||
pub ram_size: f64,
|
||||
pub ip_address: String,
|
||||
}
|
||||
|
||||
/// Command message received from the backend server.
|
||||
///
|
||||
/// ## Fields
|
||||
/// - `message_type`: Type of command (e.g., "update_image", "restart_container", "stop_agent")
|
||||
/// - `data`: Command payload (arbitrary JSON)
|
||||
/// - `message_id`: Unique identifier for acknowledgment
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ServerMessage {
|
||||
// Define your message structure here
|
||||
pub message_type: String,
|
||||
pub data: serde_json::Value,
|
||||
pub message_id: String, // Add an ID for acknowledgment
|
||||
}
|
||||
|
||||
/// Acknowledgment payload sent to the backend server for command messages.
|
||||
///
|
||||
/// ## Fields
|
||||
/// - `message_id`: Unique identifier of the acknowledged message
|
||||
/// - `status`: Status string ("success", "error", etc.)
|
||||
/// - `details`: Additional details or error messages
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct Acknowledgment {
|
||||
pub message_id: String,
|
||||
pub status: String, // "success" or "error"
|
||||
pub details: String,
|
||||
}
|
||||
|
||||
/// Docker container information for agent and managed containers.
|
||||
///
|
||||
/// ## Fields
|
||||
/// - `ID`: Container ID (first 12 hex digits, integer)
|
||||
/// - `image`: Docker image name (string)
|
||||
/// - `Name`: Container name (string)
|
||||
/// - `Status`: Container status ("running", "stopped", etc.)
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerRegistrationDto {
|
||||
/// Unique server identifier (integer)
|
||||
#[serde(rename = "Server_id")]
|
||||
pub server_id: u16,
|
||||
/// Number of currently running containers
|
||||
// pub container_count: usize, --- IGNORE ---
|
||||
/// 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
|
||||
#[serde(rename = "Containers")]
|
||||
pub containers: Value, // Vec<DockerContainer>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerMetricDto {
|
||||
pub server_id: u16,
|
||||
/// 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: Value, // Vec<DockerContainerInfo>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
|
||||
pub struct DockerCollectMetricDto {
|
||||
pub id: String,
|
||||
pub status: DockerContainerStatusDto,
|
||||
pub cpu: DockerContainerCpuDto,
|
||||
pub ram: DockerContainerRamDto,
|
||||
pub network: DockerContainerNetworkDto,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerContainerStatusDto {
|
||||
pub status: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerContainerCpuDto {
|
||||
pub cpu_load: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerContainerRamDto {
|
||||
pub ram_load: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
|
||||
pub struct DockerContainerNetworkDto {
|
||||
pub net_in: Option<f64>,
|
||||
pub net_out: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerContainerInfo {
|
||||
pub container: Option<DockerContainer>,
|
||||
pub status: Option<stats::ContainerStatusInfo>, // "running";"stopped";others
|
||||
pub network: Option<stats::ContainerNetworkInfo>,
|
||||
pub cpu: Option<stats::ContainerCpuInfo>,
|
||||
pub ram: Option<stats::ContainerMemoryInfo>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct DockerContainer {
|
||||
pub id: String,
|
||||
#[serde(default)]
|
||||
pub image: Option<String>,
|
||||
#[serde(default)]
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
44
docker-compose.example.yaml
Normal file
44
docker-compose.example.yaml
Normal file
@@ -0,0 +1,44 @@
|
||||
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
|
||||
|
||||
watcher-agent:
|
||||
image: git.triggermeelmo.com/donpat1to/watcher-agent:v0.1.28
|
||||
container_name: watcher-agent
|
||||
restart: always
|
||||
privileged: true # Grants full hardware access (use with caution)
|
||||
env_file: .env
|
||||
pid: "host"
|
||||
volumes:
|
||||
# Mount critical system paths for hardware monitoring
|
||||
- /sys:/sys:ro # CPU/GPU temps, sensors
|
||||
- /proc:/proc # Process/CPU stats
|
||||
- /dev:/dev:ro # Disk/GPU device access
|
||||
- /var/run/docker.sock:/var/run/docker.sock # Docker API access
|
||||
- /:/root:ro # Access to for df-command
|
||||
# Application volumes
|
||||
- ./config:/app/config:ro
|
||||
- ./logs:/app/logs
|
||||
network_mode: host # Uses host network (for correct IP/interface detection)
|
||||
healthcheck:
|
||||
test: [ "CMD", "/usr/local/bin/WatcherAgent", "healthcheck" ]
|
||||
interval: 30s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
20
docker-compose.yaml
Normal file
20
docker-compose.yaml
Normal file
@@ -0,0 +1,20 @@
|
||||
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