Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
c6722645d2
|
|||
|
ac94ffc30f
|
|||
|
68f6f02ec5
|
|||
|
fd373d10c8
|
|||
|
5147f76336
|
12
.cargo/config.toml
Normal file
12
.cargo/config.toml
Normal 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
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/target
|
||||
/release
|
||||
|
||||
111
Cargo.lock
generated
111
Cargo.lock
generated
@ -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"
|
||||
|
||||
@ -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"] }
|
||||
|
||||
120
Makefile
Normal file
120
Makefile
Normal file
@ -0,0 +1,120 @@
|
||||
# Makefile for pt_system_checking
|
||||
# 支持 amd64 和 arm64 架构的交叉编译
|
||||
|
||||
# 项目名称
|
||||
PROJECT_NAME = pt_system_checking
|
||||
VERSION = 0.1.0
|
||||
|
||||
# 构建目录
|
||||
BUILD_DIR = target
|
||||
RELEASE_DIR = release
|
||||
|
||||
# 目标架构
|
||||
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 create-release
|
||||
|
||||
# 默认目标
|
||||
all: build-all
|
||||
|
||||
# 帮助信息
|
||||
help:
|
||||
@echo "可用的构建目标:"
|
||||
@echo " make install-targets - 安装交叉编译工具链"
|
||||
@echo " make build-amd64 - 构建 amd64 版本"
|
||||
@echo " make build-arm64 - 构建 arm64 版本(适用于香橙派 Zero3)"
|
||||
@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 权限)"
|
||||
|
||||
# 安装交叉编译目标
|
||||
install-targets:
|
||||
@echo "安装交叉编译工具链..."
|
||||
rustup target add $(ARCH_AMD64)
|
||||
rustup target add $(ARCH_ARM64)
|
||||
@echo ""
|
||||
@echo "请确保已安装 ARM64 交叉编译工具:"
|
||||
@echo " Ubuntu/Debian: sudo apt-get install gcc-aarch64-linux-gnu"
|
||||
@echo " Fedora/RHEL: sudo dnf install gcc-aarch64-linux-gnu"
|
||||
@echo " Arch Linux: sudo pacman -S aarch64-linux-gnu-gcc"
|
||||
@echo ""
|
||||
@if ! command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then \
|
||||
echo "警告: 未检测到 aarch64-linux-gnu-gcc,ARM64 交叉编译可能失败"; \
|
||||
echo "请运行: sudo apt-get install gcc-aarch64-linux-gnu"; \
|
||||
else \
|
||||
echo "✓ 检测到 aarch64-linux-gnu-gcc"; \
|
||||
fi
|
||||
@echo "工具链安装完成"
|
||||
|
||||
# 构建 amd64 版本
|
||||
build-amd64:
|
||||
@echo "构建 amd64 版本..."
|
||||
cargo build --target $(ARCH_AMD64)
|
||||
@mkdir -p $(RELEASE_DIR)
|
||||
@cp $(BUILD_DIR)/$(ARCH_AMD64)/debug/$(PROJECT_NAME) $(RELEASE_DIR)/$(BINARY_AMD64)
|
||||
@echo "amd64 版本构建完成: $(RELEASE_DIR)/$(BINARY_AMD64)"
|
||||
|
||||
# 构建 arm64 版本(适用于香橙派 Zero3)
|
||||
build-arm64:
|
||||
@echo "构建 arm64 版本(适用于香橙派 Zero3)..."
|
||||
cargo build --target $(ARCH_ARM64)
|
||||
@mkdir -p $(RELEASE_DIR)
|
||||
@cp $(BUILD_DIR)/$(ARCH_ARM64)/debug/$(PROJECT_NAME) $(RELEASE_DIR)/$(BINARY_ARM64)
|
||||
@echo "arm64 版本构建完成: $(RELEASE_DIR)/$(BINARY_ARM64)"
|
||||
|
||||
# 构建所有架构
|
||||
build-all: build-amd64 build-arm64
|
||||
@echo "所有架构构建完成"
|
||||
|
||||
# 发布版本(优化编译)
|
||||
release:
|
||||
@echo "构建优化版本..."
|
||||
@mkdir -p $(RELEASE_DIR)
|
||||
cargo build --release --target $(ARCH_AMD64)
|
||||
cargo build --release --target $(ARCH_ARM64)
|
||||
@cp $(BUILD_DIR)/$(ARCH_AMD64)/release/$(PROJECT_NAME) $(RELEASE_DIR)/$(BINARY_AMD64)
|
||||
@cp $(BUILD_DIR)/$(ARCH_ARM64)/release/$(PROJECT_NAME) $(RELEASE_DIR)/$(BINARY_ARM64)
|
||||
@echo "发布版本构建完成:"
|
||||
@ls -lh $(RELEASE_DIR)/
|
||||
|
||||
# 运行测试
|
||||
test:
|
||||
cargo test
|
||||
|
||||
# 清理构建文件
|
||||
clean:
|
||||
cargo clean
|
||||
rm -rf $(RELEASE_DIR)
|
||||
@echo "清理完成"
|
||||
|
||||
# 准备发布文件(包含二进制文件和安装脚本)
|
||||
prepare-release: release
|
||||
@echo "准备发布文件..."
|
||||
@cp install.sh $(RELEASE_DIR)/
|
||||
@chmod +x $(RELEASE_DIR)/install.sh
|
||||
@echo "发布文件已准备完成,位于 $(RELEASE_DIR)/ 目录"
|
||||
@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 .
|
||||
@echo "安装完成"
|
||||
128
README.md
Normal file
128
README.md
Normal file
@ -0,0 +1,128 @@
|
||||
# PT System Checking
|
||||
|
||||
上报硬盘剩余空间到 Uptime Kuma 实例,以 ping 作为空间剩余
|
||||
|
||||
## 部署
|
||||
|
||||
### 一键部署(推荐)
|
||||
|
||||
下载后运行(因为需要交互脚本):
|
||||
|
||||
```bash
|
||||
wget https://gitea.service.jazzwhom.top/Passthem/pt-disk-report/raw/branch/main/install.sh
|
||||
sudo bash install.sh
|
||||
```
|
||||
|
||||
安装脚本会:
|
||||
1. 自动检测系统架构(amd64/arm64)
|
||||
2. 从 Gitea Releases 下载对应的二进制文件
|
||||
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
|
||||
```
|
||||
|
||||
## 本地构建与发布
|
||||
|
||||
### 安装交叉编译工具链
|
||||
|
||||
```bash
|
||||
make install-targets
|
||||
```
|
||||
|
||||
### 构建所有架构
|
||||
|
||||
```bash
|
||||
make release
|
||||
```
|
||||
|
||||
### 准备发布文件
|
||||
|
||||
```bash
|
||||
make prepare-release
|
||||
```
|
||||
|
||||
这会在 `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
|
||||
```
|
||||
|
||||
## 配置说明
|
||||
|
||||
服务支持以下配置项(通过环境变量):
|
||||
|
||||
- `PT__SERVER_URL` - 服务器 URL(用于上报磁盘状态)
|
||||
- `PT__INTERVAL` - 检查间隔(秒,默认 300)
|
||||
- `PT__LOG_LEVEL` - 日志级别(trace/debug/info/warn/error,默认 info)
|
||||
|
||||
## 服务管理
|
||||
|
||||
安装后可使用以下命令管理服务:
|
||||
|
||||
```bash
|
||||
# 查看服务状态
|
||||
systemctl status pt_system_checking
|
||||
|
||||
# 查看实时日志
|
||||
journalctl -u pt_system_checking -f
|
||||
|
||||
# 停止服务
|
||||
systemctl stop pt_system_checking
|
||||
|
||||
# 启动服务
|
||||
systemctl start pt_system_checking
|
||||
|
||||
# 重启服务
|
||||
systemctl restart pt_system_checking
|
||||
|
||||
# 禁用开机自启
|
||||
systemctl disable pt_system_checking
|
||||
```
|
||||
|
||||
## 手动安装
|
||||
|
||||
如果不使用一键脚本,可以手动安装:
|
||||
|
||||
1. 下载对应架构的二进制文件
|
||||
2. 复制到 `/opt/pt_system_checking/`
|
||||
3. 创建 systemd 服务文件 `/etc/systemd/system/pt_system_checking.service`
|
||||
4. 配置环境变量
|
||||
5. 启动服务:`systemctl enable --now pt_system_checking`
|
||||
|
||||
## 开发
|
||||
|
||||
### 运行测试
|
||||
|
||||
```bash
|
||||
make test
|
||||
```
|
||||
|
||||
### 清理构建文件
|
||||
|
||||
```bash
|
||||
make clean
|
||||
```
|
||||
|
||||
279
install.sh
Executable file
279
install.sh
Executable file
@ -0,0 +1,279 @@
|
||||
#!/bin/bash
|
||||
# PT System Checking 一键部署脚本
|
||||
# 支持从 Gitea 下载并配置为 systemd 服务
|
||||
|
||||
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 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_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 "下载完成"
|
||||
}
|
||||
|
||||
# 交互式配置
|
||||
interactive_config() {
|
||||
echo ""
|
||||
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:-""}
|
||||
|
||||
# 检查间隔
|
||||
read -p "请输入检查间隔(秒,默认 300): " INTERVAL
|
||||
INTERVAL=${INTERVAL:-300}
|
||||
|
||||
# 日志级别
|
||||
read -p "请输入日志级别(trace/debug/info/warn/error,默认 info): " LOG_LEVEL
|
||||
LOG_LEVEL=${LOG_LEVEL:-"info"}
|
||||
|
||||
echo ""
|
||||
print_info "配置摘要:"
|
||||
echo " 服务器 URL: ${SERVER_URL:-未配置}"
|
||||
echo " 检查间隔: ${INTERVAL} 秒"
|
||||
echo " 日志级别: ${LOG_LEVEL}"
|
||||
echo ""
|
||||
|
||||
read -p "确认以上配置?(y/n): " confirm
|
||||
if [[ ! $confirm =~ ^[Yy]$ ]]; then
|
||||
print_warn "取消安装"
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
|
||||
# 安装服务
|
||||
install_service() {
|
||||
local arch=$1
|
||||
local binary_name="${SERVICE_NAME}-${arch}"
|
||||
|
||||
print_info "安装服务..."
|
||||
|
||||
# 创建安装目录
|
||||
mkdir -p "${INSTALL_DIR}"
|
||||
|
||||
# 复制二进制文件
|
||||
cp "/tmp/${binary_name}" "${INSTALL_DIR}/${SERVICE_NAME}"
|
||||
chmod +x "${INSTALL_DIR}/${SERVICE_NAME}"
|
||||
|
||||
# 创建 systemd 服务文件
|
||||
cat > "${SYSTEMD_SERVICE}" <<EOF
|
||||
[Unit]
|
||||
Description=PT System Disk Checking Service
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
Group=root
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
|
||||
# 工作目录
|
||||
WorkingDirectory=${INSTALL_DIR}
|
||||
|
||||
# 二进制文件路径
|
||||
ExecStart=${INSTALL_DIR}/${SERVICE_NAME}
|
||||
|
||||
# 环境变量配置
|
||||
Environment="PT__SERVER_URL=${SERVER_URL}"
|
||||
Environment="PT__INTERVAL=${INTERVAL}"
|
||||
Environment="PT__LOG_LEVEL=${LOG_LEVEL}"
|
||||
|
||||
# 安全设置
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
|
||||
# 日志配置
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
SyslogIdentifier=${SERVICE_NAME}
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
# 重载 systemd
|
||||
systemctl daemon-reload
|
||||
|
||||
print_info "服务安装完成"
|
||||
}
|
||||
|
||||
# 启动服务
|
||||
start_service() {
|
||||
print_info "启动服务..."
|
||||
|
||||
systemctl enable "${SERVICE_NAME}"
|
||||
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 "服务启动失败,请查看日志:"
|
||||
echo " journalctl -u ${SERVICE_NAME} -n 50"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 卸载服务
|
||||
uninstall_service() {
|
||||
print_info "卸载服务..."
|
||||
|
||||
if systemctl is-active --quiet "${SERVICE_NAME}"; then
|
||||
systemctl stop "${SERVICE_NAME}"
|
||||
fi
|
||||
|
||||
systemctl disable "${SERVICE_NAME}" 2>/dev/null || true
|
||||
|
||||
rm -f "${SYSTEMD_SERVICE}"
|
||||
rm -rf "${INSTALL_DIR}"
|
||||
|
||||
systemctl daemon-reload
|
||||
|
||||
print_info "服务已卸载"
|
||||
}
|
||||
|
||||
# 主函数
|
||||
main() {
|
||||
echo ""
|
||||
print_info "=== PT System Checking 一键部署脚本 ==="
|
||||
echo ""
|
||||
|
||||
# 检查参数
|
||||
if [ "$1" == "uninstall" ]; then
|
||||
check_root
|
||||
uninstall_service
|
||||
exit 0
|
||||
fi
|
||||
|
||||
check_root
|
||||
|
||||
# 检测架构
|
||||
ARCH=$(detect_arch)
|
||||
print_info "检测到系统架构: ${ARCH}"
|
||||
|
||||
# 获取版本
|
||||
print_info "获取最新版本信息..."
|
||||
VERSION=$(get_latest_version)
|
||||
print_info "使用版本: v${VERSION}"
|
||||
|
||||
# 下载二进制文件
|
||||
download_binary "${ARCH}" "${VERSION}"
|
||||
|
||||
# 交互式配置
|
||||
interactive_config
|
||||
|
||||
# 安装服务
|
||||
install_service "${ARCH}"
|
||||
|
||||
# 启动服务
|
||||
start_service
|
||||
|
||||
# 清理临时文件
|
||||
rm -f "/tmp/${SERVICE_NAME}-${ARCH}"
|
||||
|
||||
echo ""
|
||||
print_info "部署完成!"
|
||||
}
|
||||
|
||||
# 运行主函数
|
||||
main "$@"
|
||||
31
src/main.rs
31
src/main.rs
@ -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, ¶ms)?;
|
||||
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
248
update.sh
Executable 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 "$@"
|
||||
Reference in New Issue
Block a user