feat: add structured logging, process watchdog, and systemd hardening

- Add lib/logger.js: timestamped structured logging with 5MB x 5 file rotation
- Add lib/watchdog.js: generic child process supervisor with rate-limited restarts
- Enhance client.js: WS ping/pong liveness detection, uncaughtException/unhandledRejection handlers, systemd sd-notify integration
- Refactor frpc.js: FrpcManager now delegates to Watchdog instead of manual spawn/exit
- Enhance install.sh: environment file, log directory, systemd resource limits, security hardening, WatchdogSec=60
- Replace all console.log/warn/error with structured logger across modules

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-16 07:31:19 +08:00
parent 42d1d361dc
commit b3770d21d4
9 changed files with 545 additions and 149 deletions

View File

@@ -8,7 +8,11 @@ Claw Box 守护进程,将本地 Linux 设备通过 WebSocket 长连接接入 [
- 首次连接自动注册,获取 `claw_id` + `token` 并持久化
- 每 30 秒上报系统指标CPU、内存、磁盘、温度、负载、运行时间
- 断线自动重连(指数退避,最大 60 秒)
- systemd 管理,开机自启
- WS 层 Ping/Pong 活性检测,连接假死自动重连
- frpc / ttyd 子进程 Watchdog 守护,崩溃自动重启(速率限制)
- 结构化日志 + 文件轮转5MB × 5 份)
- systemd 集成Watchdog、资源限制、优雅停止
- 全局异常兜底uncaughtException / unhandledRejection
## 快速安装Linux需要 root
@@ -32,18 +36,17 @@ node bin/clawd.js
## 首次启动输出示例
```
[clawd] 启动中...
[clawd] box_id = a1b2c3d4e5f6...
[clawd] 服务器 = wss://claw.cutos.ai/ws
[clawd] WebSocket 已连接
[clawd] 注册成功claw_id = 1000
╔══════════════════════════════════╗
║ 激活 PIN 码: 779413
║ 请在管理后台或前台输入此 PIN 码 ║
╚══════════════════════════════════╝
[clawd] 等待激活中,心跳正常运行...
2026-03-16T10:00:00.000Z INFO [clawd] 启动中... 服务器 = wss://claw.cutos.ai/ws
2026-03-16T10:00:01.000Z INFO [clawd] WebSocket 已连接
2026-03-16T10:00:01.100Z INFO [clawd] 注册成功claw_id = 1000
2026-03-16T10:00:01.100Z INFO [clawd]
2026-03-16T10:00:01.100Z INFO [clawd] ╔════════════════════════════════════╗
2026-03-16T10:00:01.100Z INFO [clawd] ║ Claw ID : 1000 ║
2026-03-16T10:00:01.100Z INFO [clawd] ║ PIN 码 : 779413 ║
2026-03-16T10:00:01.100Z INFO [clawd] ║ 请在网页前台「添加设备」中输入
2026-03-16T10:00:01.100Z INFO [clawd] ╚════════════════════════════════════╝
2026-03-16T10:00:01.100Z INFO [clawd]
2026-03-16T10:00:01.100Z INFO [clawd] 等待激活,心跳正常运行...
```
## 配置文件
@@ -59,27 +62,60 @@ node bin/clawd.js
}
```
## 环境变量
| 变量 | 默认值 | 说明 |
|------|--------|------|
| `CLAWD_LOG_LEVEL` | `info` | 日志级别debug / info / warn / error |
| `CLAWD_LOG_FILE` | `1` | 是否写日志文件(`0` = 仅 stdout/journald |
| `CLAWD_LOG_DIR` | `~/.clawd/logs` | 日志文件目录 |
| `CLAWD_CONFIG_DIR` | `~/.clawd` | 配置目录 |
systemd 安装后环境变量文件位于 `/etc/clawd/env`
## 服务管理
```bash
systemctl status clawd # 查看状态
journalctl -u clawd -f # 实时日志
systemctl restart clawd # 重启
systemctl stop clawd # 停止
systemctl disable clawd # 取消开机自启
systemctl restart clawd # 重启
systemctl stop clawd # 停止
systemctl disable clawd # 取消开机自启
```
## 日志
- **stdout/journald**所有日志同时输出到标准输出systemd 自动采集到 journald
- **文件日志**`/etc/clawd/logs/clawd.log`,单文件 5MB保留 5 份轮转
## 心跳上报字段
| 字段 | 说明 | 单位 |
|------|------|------|
| `cpu` | CPU 使用率 | % |
| `mem_total` / `mem_used` | 内存总量 / 已用 | KB |
| `disk_total` / `disk_used` | 根分区总量 / 已用 | KB |
| `disk_total` / `disk_used` | 磁盘总量 / 已用 | KB |
| `temperature` | CPU 温度 | °C |
| `load_1m` / `load_5m` / `load_15m` | 系统负载 | — |
| `uptime` | 运行时间 | 秒 |
## 架构
```
clawd/
├── bin/clawd.js ← 入口,优雅停止
├── lib/
│ ├── client.js ← 核心WS 连接、心跳、Ping/Pong、sd-notify
│ ├── config.js ← 配置读写
│ ├── fingerprint.js ← 硬件指纹生成
│ ├── frpc.js ← frpc/ttyd/dashboard 管理Watchdog 守护)
│ ├── logger.js ← 结构化日志 + 文件轮转
│ ├── metrics.js ← 系统指标采集
│ └── watchdog.js ← 通用子进程守护(速率限制重启)
├── install.sh ← 一键安装(含 systemd
└── package.json
```
## License
MIT