为嵌入式设备高并发监控场景设计的极简网页资源占用显示系统 https://crates.io/crates/swb-sys-monitor

PJ568 416ef56323 【修复,CI】修复工作流启动失败与依赖审计漏洞 2 ay önce
.github dbc4ce66a8 【修复,CI】修复复用工作流权限导致的启动失败 2 ay önce
assets 283afe9e8c 【新增】添加 CPU 和内存进度条的 title 属性以提升 UI 可读性 1 yıl önce
benches d7137bd2e4 【更改】缓存 TTL 支持动态配置 1 yıl önce
docs 6a1e401e27 【修复,缓存】同步锁文件并修正文档示例 2 ay önce
scripts cb0a59cb44 【修复】修改服务配置中的用户组设置和只读路径配置格式 1 yıl önce
src 09ff0be22c 【修复,缓存】修复缓存并发读取内存安全问题 2 ay önce
templates d7137bd2e4 【更改】缓存 TTL 支持动态配置 1 yıl önce
.dockerignore f5f0998188 【初始】初始化项目基础结构与核心功能 1 yıl önce
.gitignore f5f0998188 【初始】初始化项目基础结构与核心功能 1 yıl önce
CONTRIBUTING.md f5f0998188 【初始】初始化项目基础结构与核心功能 1 yıl önce
Cargo.lock 0c2f839499 【修复,依赖】修复审计发现的依赖漏洞 2 ay önce
Cargo.toml 0c2f839499 【修复,依赖】修复审计发现的依赖漏洞 2 ay önce
Dockerfile f5f0998188 【初始】初始化项目基础结构与核心功能 1 yıl önce
LICENSE f5f0998188 【初始】初始化项目基础结构与核心功能 1 yıl önce
README.en.md 353d1785cb 【修复,缓存】修复缓存并发读取内存安全问题 3 ay önce
README.md 353d1785cb 【修复,缓存】修复缓存并发读取内存安全问题 3 ay önce
docker-compose.yml f5f0998188 【初始】初始化项目基础结构与核心功能 1 yıl önce

README.en.md

Resource Usage Monitor System

English | 简体中文

A minimal resource usage monitoring system designed specifically for embedded device high-concurrency monitoring scenarios. Supports 200+ high-concurrency client connections with lock-free algorithms for high performance.

Features

  • Minimal code implementation: Pursuing code simplicity and maintainability
  • High performance, low memory usage: Optimized for embedded devices
  • High concurrency access, lock-free algorithms: Supports 200+ concurrent connections
  • Few dependencies, prefer system commands: Reduces external dependencies
  • Data expires after 10 seconds: No need to fetch data again when not expired
  • No fetching when no users access: On-demand update strategy
  • No CSS, no JS: Pure HTML implementation

Quick Start

Installation

Install pre-compiled binary package

cargo install swb-sys-monitor

Compile from source

# Clone repository
git clone https://github.com/swaybien/swb-sys-monitor.git
cd swb-sys-monitor

# Build release version
cargo build --release

# Or run directly
cargo run -- --help

Usage

# Start with default configuration (listen on [::]:8080, cache TTL 10 seconds)
./target/release/swb-sys-monitor

# Custom configuration
./target/release/swb-sys-monitor --address 127.0.0.1 --port 3000 --ttl 5

# Set log level
./target/release/swb-sys-monitor --log-level debug

Access

After starting, visit http://localhost:8080 in your browser to view system resource usage.

The page automatically refreshes every 10 seconds and displays the following information:

  • CPU usage
  • Memory usage (used, available, cached, free)
  • Data acquisition timestamp

Health Check Endpoint

The system provides a health check endpoint at http://localhost:8080/health for monitoring service status:

curl http://localhost:8080/health
# Returns: OK

Command Line Arguments

Parameter Short Parameter Default Description
--address -a :: Server binding address (supports IPv4 and IPv6)
--port -p 8080 Server port
--ttl -t 10 Cache TTL in seconds
--log-level -l info Log level (trace, debug, info, warn, error)
--help -h - Show help information

Technical Architecture

Core Components

  1. System Resource Acquisition Module: Directly reads /proc/stat and /proc/meminfo to obtain system information
  2. Lock-free Data Caching Mechanism: Uses arc-swap (ArcSwap) and AtomicU64 to implement thread-safe caching
  3. High-concurrency Web Server: High-performance HTTP server based on tokio + hyper
  4. Server-side HTML Rendering: Pure HTML implementation, no CSS, no JS

Performance Optimization

  • Lock-free algorithms: Cache read/write uses atomic operations, supporting high-concurrency access
  • On-demand updates: System information is updated only when data is expired and there are requests
  • Memory optimization: Uses String::with_capacity to pre-allocate capacity, reducing reallocation
  • Function inlining: Small functions use #[inline] attribute for performance optimization

Development

Build Requirements

  • Rust 2024 Edition
  • Linux system (currently only supports Linux)

Development Commands

# Compile check
cargo check --all-targets

# Code style check
cargo clippy

# Run tests
cargo test

# Run benchmark tests
cargo bench

# Build release version
cargo build --release

Test Coverage

The project includes comprehensive test coverage:

  • Unit tests: caching mechanism, system information acquisition, HTTP server
  • Integration tests: end-to-end functionality tests
  • Performance benchmark tests: performance tests for key operations

Deployment

Automatic Installation (Recommended)

Use the provided installation script for automatic deployment to systemd systems:

# Clone repository
git clone https://github.com/swaybien/swb-sys-monitor.git
cd swb-sys-monitor

# Run installation script
sudo ./scripts/install.sh

# To uninstall
sudo ./scripts/uninstall.sh

For detailed deployment script usage instructions, please refer to Deployment Guide.

Manual Installation

System Service Configuration

Create a systemd service file /etc/systemd/system/swb-sys-monitor.service:

[Unit]
Description=Resource Status Monitor
After=network.target

[Service]
Type=simple
User=nobody
ExecStart=/usr/local/bin/swb-sys-monitor
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl enable swb-sys-monitor
sudo systemctl start swb-sys-monitor

Docker Deployment

Using Docker Compose (Recommended)

# Clone repository
git clone https://github.com/swaybien/swb-sys-monitor.git
cd swb-sys-monitor

# Start service
docker-compose up -d

# View logs
docker-compose logs -f swb-sys-monitor

# Stop service
docker-compose down

Access URL: http://localhost:18273

Using Docker Commands

# Build image
docker build -t swb-sys-monitor .

# Run container
docker run -d \
  --name swb-sys-monitor \
  --restart unless-stopped \
  -p 18273:8080 \
  --read-only \
  --tmpfs /tmp \
  --cap-drop ALL \
  --cap-add DAC_OVERRIDE \
  --security-opt no-new-privileges:true \
  swb-sys-monitor

# View logs
docker logs -f swb-sys-monitor

# Stop container
docker stop swb-sys-monitor
docker rm swb-sys-monitor

Docker Configuration Description

Docker deployment includes the following security features:

  • Read-only file system: Prevents runtime modifications
  • Minimal privileges: Removes all Linux capabilities, only adds necessary ones
  • Security options: Enables no-new-privileges to prevent privilege escalation
  • Health check: Automatically monitors service status
  • Non-root user: Runs with unprivileged user inside the container

Custom Configuration

You can customize configuration through environment variables:

# docker-compose.yml
services:
  swb-sys-monitor:
    build: .
    environment:
      - RUST_LOG=debug
    command:
      [
        "./swb-sys-monitor",
        "--address",
        "0.0.0.0",
        "--port",
        "8080",
        "--ttl",
        "5",
      ]

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Issues and Pull Requests are welcome! Please read the Contributing Guide.

Related Links