3 Commits
v0.1.4 ... main

Author SHA1 Message Date
c6722645d2 更新重试器 2026-02-23 21:26:48 +08:00
ac94ffc30f 调整硬盘筛选策略 2026-02-22 21:02:27 +08:00
68f6f02ec5 更新各种脚本 2026-02-22 20:55:05 +08:00
9 changed files with 451 additions and 32 deletions

12
.cargo/config.toml Normal file
View File

@ -0,0 +1,12 @@
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
[target.x86_64-unknown-linux-gnu]
linker = "gcc"
[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=+crt-static", "-C", "link-self-contained=yes"]
[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-gnu-gcc"
rustflags = ["-C", "target-feature=+crt-static", "-C", "link-self-contained=yes"]

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
/target
/release
/.cargo

111
Cargo.lock generated
View File

@ -466,6 +466,21 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
[[package]]
name = "futures"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.32"
@ -473,6 +488,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@ -481,6 +497,34 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
[[package]]
name = "futures-executor"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718"
[[package]]
name = "futures-macro"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.32"
@ -499,8 +543,13 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"slab",
]
@ -1180,13 +1229,15 @@ dependencies = [
[[package]]
name = "pt_system_checking"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"anyhow",
"chrono",
"clap",
"config",
"reqwest",
"reqwest-middleware",
"reqwest-retry",
"serde",
"sysinfo",
"tokio",
@ -1358,6 +1409,50 @@ dependencies = [
"web-sys",
]
[[package]]
name = "reqwest-middleware"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "199dda04a536b532d0cc04d7979e39b1c763ea749bf91507017069c00b96056f"
dependencies = [
"anyhow",
"async-trait",
"http",
"reqwest",
"thiserror 2.0.18",
"tower-service",
]
[[package]]
name = "reqwest-retry"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe2412db2af7d2268e7a5406be0431f37d9eb67ff390f35b395716f5f06c2eaa"
dependencies = [
"anyhow",
"async-trait",
"futures",
"getrandom 0.2.17",
"http",
"hyper",
"reqwest",
"reqwest-middleware",
"retry-policies",
"thiserror 2.0.18",
"tokio",
"tracing",
"wasmtimer",
]
[[package]]
name = "retry-policies"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46a4bd6027df676bcb752d3724db0ea3c0c5fc1dd0376fec51ac7dcaf9cc69be"
dependencies = [
"rand",
]
[[package]]
name = "ring"
version = "0.17.14"
@ -2177,6 +2272,20 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "wasmtimer"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c598d6b99ea013e35844697fc4670d08339d5cda15588f193c6beedd12f644b"
dependencies = [
"futures",
"js-sys",
"parking_lot",
"pin-utils",
"slab",
"wasm-bindgen",
]
[[package]]
name = "web-sys"
version = "0.3.88"

View File

@ -1,6 +1,6 @@
[package]
name = "pt_system_checking"
version = "0.1.0"
version = "0.2.0"
edition = "2024"
[dependencies]
@ -9,6 +9,8 @@ chrono = "0.4.43"
clap = { version = "4.5.60", features = ["derive"] }
config = "0.15.19"
reqwest = "0.13.2"
reqwest-middleware = "0.5.1"
reqwest-retry = "0.9.1"
serde = { version = "1.0.228", features = ["derive"] }
sysinfo = "0.38.2"
tokio = { version = "1.49.0", features = ["full"] }

View File

@ -10,14 +10,14 @@ BUILD_DIR = target
RELEASE_DIR = release
# 目标架构
ARCH_AMD64 = x86_64-unknown-linux-gnu
ARCH_AMD64 = x86_64-unknown-linux-musl
ARCH_ARM64 = aarch64-unknown-linux-gnu
# 输出二进制文件名
BINARY_AMD64 = $(PROJECT_NAME)-amd64
BINARY_ARM64 = $(PROJECT_NAME)-arm64
.PHONY: all help install-targets build-amd64 build-arm64 build-all clean release test prepare-release
.PHONY: all help install-targets build-amd64 build-arm64 build-all clean release test prepare-release create-release
# 默认目标
all: build-all
@ -31,6 +31,7 @@ help:
@echo " make build-all - 构建所有架构版本"
@echo " make release - 创建发布版本(优化编译)"
@echo " make prepare-release - 准备发布文件(包含安装脚本)"
@echo " make create-release - 创建 Gitea Release需要设置 VERSION 变量)"
@echo " make test - 运行测试"
@echo " make clean - 清理构建文件"
@echo " make install - 安装到系统(需要 root 权限)"
@ -104,6 +105,15 @@ prepare-release: release
@echo "请将以下文件上传到 Gitea Releases:"
@ls -lh $(RELEASE_DIR)/
# 创建 Gitea Release
create-release: prepare-release
@echo "创建 Gitea Release v$(VERSION)..."
tea releases create --tag v$(VERSION) --title "V$(VERSION) 版本发布" \
--asset "$(RELEASE_DIR)/install.sh" \
--asset "$(RELEASE_DIR)/$(BINARY_AMD64)" \
--asset "$(RELEASE_DIR)/$(BINARY_ARM64)"
@echo "Release 创建完成"
# 安装到系统(默认安装当前架构)
install:
cargo install --path .

View File

@ -1,26 +1,12 @@
# PT System Checking - 磁盘状态监控服务
# PT System Checking
一个轻量级的系统磁盘监控服务,定期检查磁盘可用空间并上报到指定服务器。
上报硬盘剩余空间到 Uptime Kuma 实例,以 ping 作为空间剩余
## 功能特性
- 自动检测所有非可移动磁盘的可用空间
- 定期上报磁盘状态到远程服务器
- 支持通过环境变量或命令行参数配置
- 支持 amd64 和 arm64 架构(适用于香橙派 Zero3
- 一键部署脚本,自动配置 systemd 服务
## 快速开始
## 部署
### 一键部署(推荐)
在目标设备上运行以下命令
```bash
curl -fsSL https://gitea.service.jazzwhom.top/Passthem/pt-disk-report/raw/branch/main/install.sh | sudo bash
```
或者下载后运行:
下载后运行(因为需要交互脚本)
```bash
wget https://gitea.service.jazzwhom.top/Passthem/pt-disk-report/raw/branch/main/install.sh
@ -33,13 +19,26 @@ sudo bash install.sh
3. 交互式配置服务参数(服务器 URL、检查间隔、日志级别
4. 安装并启动 systemd 服务
### 更新服务
```bash
curl -fsSL https://gitea.service.jazzwhom.top/Passthem/pt-disk-report/raw/branch/main/update.sh | sudo bash
```
或者下载后运行:
```bash
wget https://gitea.service.jazzwhom.top/Passthem/pt-disk-report/raw/branch/main/update.sh
sudo bash update.sh
```
### 卸载服务
```bash
sudo bash install.sh uninstall
```
## 本地构建
## 本地构建与发布
### 安装交叉编译工具链
@ -63,6 +62,13 @@ make prepare-release
- `pt_system_checking-amd64` - x86_64 架构二进制文件
- `pt_system_checking-arm64` - ARM64 架构二进制文件(适用于香橙派 Zero3
- `install.sh` - 一键部署脚本
- `update.sh` - 一键更新脚本
### 发布 Gitea Release
```bash
make create-release VERSION=0.1.5
```
## 配置说明
@ -120,6 +126,3 @@ make test
make clean
```
## 许可证
根据项目需要添加许可证信息。

View File

@ -91,6 +91,21 @@ interactive_config() {
print_info "=== 配置服务参数 ==="
echo ""
# 检查是否为非交互模式(通过管道执行)
if [ ! -t 0 ]; then
print_info "检测到非交互模式,使用默认配置"
SERVER_URL=""
INTERVAL=300
LOG_LEVEL="info"
print_info "配置摘要:"
echo " 服务器 URL: ${SERVER_URL:-未配置}"
echo " 检查间隔: ${INTERVAL}"
echo " 日志级别: ${LOG_LEVEL}"
echo ""
return
fi
# 服务器 URL
read -p "请输入服务器 URL用于上报磁盘状态留空则不上报: " SERVER_URL
SERVER_URL=${SERVER_URL:-""}

View File

@ -1,7 +1,9 @@
use std::time::Duration;
use std::{collections::HashSet, time::Duration};
use clap::{Parser};
use config::{Config, ConfigError, Environment};
use reqwest_middleware::ClientBuilder;
use reqwest_retry::{RetryTransientMiddleware, policies::ExponentialBackoff};
use serde::Deserialize;
use tokio::time::interval;
@ -40,13 +42,27 @@ async fn task(settings: &Settings) -> anyhow::Result<()> {
let disks = sysinfo::Disks::new_with_refreshed_list();
let mut available = 0;
let mut seen_device = HashSet::new();
for disk in disks.list().iter().filter(|d| !d.is_removable()) {
available += disk.available_space();
let mount_path = disk.mount_point().to_string_lossy();
if mount_path.contains("/snap") || mount_path.contains("/docker") {
continue;
}
let dname = disk.name().to_string_lossy().to_string();
if !seen_device.insert(dname.clone()) {
continue;
}
if dname == "drivers" || dname == "none" {
continue;
}
let davailable = disk.available_space();
tracing::info!(disk=dname, available=davailable, "检查一块硬盘");
available += davailable;
}
let report = (available >> 20) / 100;
tracing::info!(report = report, "获取剩余硬盘空间(单位 .1GB");
let report = ((available >> 20) * 100) >> 10;
tracing::info!(report = report, "获取剩余硬盘空间(单位 .01GB");
if settings.server_url.is_empty() {
tracing::warn!("没有配置服务器地址,不会上报给服务端");
@ -58,9 +74,14 @@ async fn task(settings: &Settings) -> anyhow::Result<()> {
("ping", &_rep),
];
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let client = ClientBuilder::new(reqwest::Client::new())
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();
let url = reqwest::Url::parse_with_params(&settings.server_url, &params)?;
let urls: String = url.clone().into();
let resp = reqwest::get(url).await?.text().await?;
let resp = client.get(url).send().await?.text().await?;
tracing::info!(url=urls, resp=resp, "上报了硬盘可用空间");
}

248
update.sh Executable file
View File

@ -0,0 +1,248 @@
#!/bin/bash
# PT System Checking 一键更新脚本
# 从 Gitea 下载最新版本并更新服务
set -e
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 配置变量
GITEA_REPO="https://gitea.service.jazzwhom.top/Passthem/pt-disk-report"
SERVICE_NAME="pt_system_checking"
INSTALL_DIR="/opt/${SERVICE_NAME}"
SYSTEMD_SERVICE="/etc/systemd/system/${SERVICE_NAME}.service"
# 打印信息函数
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查是否以 root 运行
check_root() {
if [ "$EUID" -ne 0 ]; then
print_error "请使用 root 权限运行此脚本"
echo "使用方法: sudo bash update.sh"
exit 1
fi
}
# 检查服务是否已安装
check_installed() {
if [ ! -f "${INSTALL_DIR}/${SERVICE_NAME}" ]; then
print_error "服务未安装,请先运行 install.sh 进行安装"
exit 1
fi
if [ ! -f "${SYSTEMD_SERVICE}" ]; then
print_error "systemd 服务文件不存在,请先运行 install.sh 进行安装"
exit 1
fi
}
# 检测系统架构
detect_arch() {
local arch=$(uname -m)
case $arch in
x86_64)
echo "amd64"
;;
aarch64|arm64)
echo "arm64"
;;
*)
print_error "不支持的架构: $arch"
exit 1
;;
esac
}
# 获取当前版本
get_current_version() {
if [ -f "${INSTALL_DIR}/${SERVICE_NAME}" ]; then
local version=$("${INSTALL_DIR}/${SERVICE_NAME}" --version 2>/dev/null | grep -oP 'v\K[0-9.]+' || echo "未知")
echo "$version"
else
echo "未安装"
fi
}
# 获取最新版本
get_latest_version() {
local version=$(curl -s "${GITEA_REPO}/releases" | grep -oP 'releases/tag/v\K[0-9.]+' | head -1)
if [ -z "$version" ]; then
version="0.1.0"
print_warn "无法自动获取版本,使用默认版本 ${version}" >&2
fi
echo "$version"
}
# 下载二进制文件
download_binary() {
local arch=$1
local version=$2
local binary_name="${SERVICE_NAME}-${arch}"
local download_url="${GITEA_REPO}/releases/download/v${version}/${binary_name}"
print_info "下载 ${arch} 架构的二进制文件..."
print_info "下载地址: ${download_url}"
if ! curl -L -f -o "/tmp/${binary_name}" "${download_url}"; then
print_error "下载失败,请检查版本号和网络连接"
print_info "尝试的 URL: ${download_url}"
exit 1
fi
chmod +x "/tmp/${binary_name}"
print_info "下载完成"
}
# 备份当前版本
backup_current() {
local backup_file="${INSTALL_DIR}/${SERVICE_NAME}.backup"
if [ -f "${INSTALL_DIR}/${SERVICE_NAME}" ]; then
print_info "备份当前版本..."
cp "${INSTALL_DIR}/${SERVICE_NAME}" "${backup_file}"
print_info "备份保存至: ${backup_file}"
fi
}
# 恢复备份
restore_backup() {
local backup_file="${INSTALL_DIR}/${SERVICE_NAME}.backup"
if [ -f "${backup_file}" ]; then
print_warn "恢复备份版本..."
cp "${backup_file}" "${INSTALL_DIR}/${SERVICE_NAME}"
chmod +x "${INSTALL_DIR}/${SERVICE_NAME}"
print_info "已恢复到备份版本"
fi
}
# 更新服务
update_service() {
local arch=$1
local binary_name="${SERVICE_NAME}-${arch}"
print_info "更新服务..."
# 停止服务
if systemctl is-active --quiet "${SERVICE_NAME}"; then
print_info "停止服务..."
systemctl stop "${SERVICE_NAME}"
fi
# 替换二进制文件
cp "/tmp/${binary_name}" "${INSTALL_DIR}/${SERVICE_NAME}"
chmod +x "${INSTALL_DIR}/${SERVICE_NAME}"
print_info "服务更新完成"
}
# 启动服务
start_service() {
print_info "启动服务..."
systemctl start "${SERVICE_NAME}"
sleep 2
if systemctl is-active --quiet "${SERVICE_NAME}"; then
print_info "服务启动成功!"
echo ""
print_info "常用命令:"
echo " 查看状态: systemctl status ${SERVICE_NAME}"
echo " 查看日志: journalctl -u ${SERVICE_NAME} -f"
echo " 停止服务: systemctl stop ${SERVICE_NAME}"
echo " 重启服务: systemctl restart ${SERVICE_NAME}"
else
print_error "服务启动失败,正在恢复备份..."
restore_backup
systemctl start "${SERVICE_NAME}"
if systemctl is-active --quiet "${SERVICE_NAME}"; then
print_info "已恢复到备份版本"
else
print_error "恢复失败,请查看日志:"
echo " journalctl -u ${SERVICE_NAME} -n 50"
fi
exit 1
fi
}
# 主函数
main() {
echo ""
print_info "=== PT System Checking 一键更新脚本 ==="
echo ""
check_root
check_installed
# 检测架构
ARCH=$(detect_arch)
print_info "检测到系统架构: ${ARCH}"
# 获取版本信息
CURRENT_VERSION=$(get_current_version)
print_info "当前版本: ${CURRENT_VERSION}"
print_info "获取最新版本信息..."
LATEST_VERSION=$(get_latest_version)
print_info "最新版本: v${LATEST_VERSION}"
# 版本比较
if [ "$CURRENT_VERSION" == "$LATEST_VERSION" ]; then
print_info "已是最新版本,无需更新"
exit 0
fi
echo ""
print_warn "即将从 v${CURRENT_VERSION} 更新到 v${LATEST_VERSION}"
# 检查是否为非交互模式
if [ -t 0 ]; then
read -p "是否继续?(y/n): " confirm
if [[ ! $confirm =~ ^[Yy]$ ]]; then
print_warn "取消更新"
exit 0
fi
else
print_info "检测到非交互模式,自动继续更新"
fi
echo ""
# 备份当前版本
backup_current
# 下载新版本
download_binary "${ARCH}" "${LATEST_VERSION}"
# 更新服务
update_service "${ARCH}"
# 启动服务
start_service
# 清理临时文件
rm -f "/tmp/${SERVICE_NAME}-${ARCH}"
echo ""
print_info "更新完成!版本: v${LATEST_VERSION}"
}
# 运行主函数
main "$@"