Files

102 lines
2.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# LinuxAwesomeBot
轻量级 OneBot 11 Python 机器人:查看 Linux 功耗/CPU/内存/温度/网络,并在 CPU 高负载时主动提醒并列出高负载进程。
基于 `aiohttp` + `psutil`,机器人作为**反向 WebSocket 服务端**运行,NapCat / Lagrange / go-cqhttp 等 OneBot 实现端主动连入。
## 安装
使用 [uv](https://docs.astral.sh/uv/) 管理依赖(首次运行会自动创建虚拟环境并安装依赖):
```bash
uv sync
```
## 配置
```bash
cp config.example.toml config.toml
# 编辑 config.toml:端口、access_token、告警群号/好友号、CPU 阈值
```
## 启动机器人
```bash
uv run linuxawesomebot
```
也可以 `uv run python bot.py`,效果相同。
## 测试
```bash
uv run pytest
```
## OneBot 实现端配置(以 NapCat 为例)
在 NapCat 的 `onebot11` 网络配置中添加**反向 WebSocket**:
```json
{
"ws": {
"enable": true,
"url": "ws://127.0.0.1:8765/ws",
"token": ""
}
}
```
`token` 需与 `config.toml` 的 `access_token` 一致(都留空也可)。
## 聊天命令
群聊需 @机器人 或使用 `/`、`!` 前缀;私聊直接发。
| 命令 | 说明 |
|---|---|
| `/status` 或 `状态` | 总览:CPU、负载、内存、最高温度、网速、运行时间 |
| `/top` 或 `进程` | CPU 占用 TOP10 进程 |
| `/temp` 或 `温度` | 各传感器温度 |
| `/net` 或 `网络` | 网络上下行速率 |
| `/help` 或 `帮助` | 命令帮助 |
## 高负载告警
后台每 10 秒采样,CPU 连续 2 次超过阈值(默认 85%)即向配置的群/好友推送告警,
消息包含当前 CPU%、负载和 TOP5 高负载进程(PID/名称/CPU/内存)。同一轮高负载期间
冷却 10 分钟不重复告警;回落到阈值以下会发送恢复通知。以上均可在 `config.toml` 的 `[alert]` 中调整。
## 平台兼容性
| 功能 | Linux | Windows |
|---|---|---|
| CPU/内存/进程/磁盘/网络 | 完整支持 | 完整支持 |
| 负载 (load average) | 内核提供 | psutil 模拟值 |
| 温度 | lm-sensors / sysfs | WMI 热区(部分主板支持,可能需管理员权限) |
机器人本体(OneBot 反向 WS、命令、告警)在两个平台行为一致。
## 本地测试(Windows 也可)
## systemd 部署(Linux)
```ini
# /etc/systemd/system/linuxawesomebot.service
[Unit]
Description=LinuxAwesomeBot
After=network.target
[Service]
WorkingDirectory=/opt/LinuxAwesomeBot
ExecStart=/usr/local/bin/uv run linuxawesomebot
Restart=always
[Install]
WantedBy=multi-user.target
```
```bash
systemctl enable --now linuxawesomebot
```