Margrop
Articles242
Tags419
Categories23
1password AC ACP AI AP API AppDaemon Aqara CI/CD Caddy Cloudflare Cookie 认证 Cron D1 Date Diagrams.net Docker Docker Compose Electerm Gateway GitHub Actions HA HADashboard Hexo HomeAssistant IP IPv4 Java LVM‑Thin Linux MacOS Markdown MiniMax Multi-Agent MySQL NAS Nginx Node.js OpenAI OpenClaw OpenResty PPPoE Portainer PostgreSQL ProcessOn Prometheus Proxmox VE SOCKS5 SSL Shell TTS TimeMachine UML Uptime Kuma VPN VPS Web Windows Workers activate ad adb adblock agent aligenie aliyun alpine annotation aop authy autofs backup baidupan bash bitwarden boot brew browser caddy2 cdn centos cert certbot charles chat chrome classloader client clone closures cloudflare cmd command commit container crontab ctyun ddsm demo dependency deploy developer devtools dll dns docker domain download draw drawio dsm dump dylib edge exception export fail2ban feign firewall-cmd flow frp frpc frps fuckgfw function gcc gfw git github golang gperftools gridea grub gvt-g hacs havcs heap hello hexo hibernate hidpi hoisting homeassistant hosts html htmlparser https idea image img img2kvm import index install intel io ios ip iptables iptv ipv6 iso java javascript jetbrains jni jnilib jpa js json jsonb jupter jupyterlab jvm k8s kernel key kid kms kodi koolproxy koolproxyr kvm lan lastpass launchctl learning lede letsencrypt linux live low-code lvm lxc m3u8 mac macos mariadb markdown maven md5 microcode mirror modem modules monitor mount mstsc mysql n2n n5105 nas network nfs node node-red nodejs nohup notepad++ npm nssm ntp oop openfeign openssl os otp ovz p14 packet capture pat pdf pem perf ping pip plugin png powerbutton print pro proxy pve pvekclean python qcow2 qemu qemu-guest-agent rar reboot reflog remote remote desktop renew repo resize retina root route router rule rules runtime safari sata scipy-notebook scoping scp server slmgr so socks source spk spring springboot springfox ssh ssl stash string supernode svg svn swagger sync synology systemctl systemd tap tap-windows tapwindows telecom template terminal tls token totp tvbox txt ubuntu udisk ui undertow uninstall unlocker upgrade url v2ray vhd vim vlmcsd vm vmdk web websocket wechat windows with worker wow xiaoya xml yum zip 中国电信 云电脑 交换机 人机协作 代理 体检 健康检查 光猫 公网IP 内存 内网IP 写作 升级 协作 博客 反向代理 启动 夏令时 多智能体 天猫精灵 天翼云 安全 安装 定时任务 容器 容器网络 导入 小米 常用软件 广告屏蔽 序列号 应用市场 异常 打工 打工人 技术 抓包 排查 描述文件 故障 故障排查 效率 效率工具 无服务器 日记 时区 显卡虚拟化 智能家居 智能音箱 架构 梯子 模块 流程 流程图 浏览器 漫游 激活 火绒 玄学 电信 画图 监控 直播源 端口扫描 续期 网关 网络 网络风暴 群晖 脚本 腾讯 自动化 虚拟机 认证 证书 语雀 超时 路由 路由器 软件管家 软路由 运维 运维监控 部署 配置 钉钉 镜像 镜像源 门窗传感器 问题排查 防火墙 阿里云 阿里源 集客

Hitokoto

Archive

OpenClaw Doctor 命令完全指南:从入门到高级诊断技巧

OpenClaw Doctor 命令完全指南:从入门到高级诊断技巧

OpenClaw Doctor 命令完全指南:从入门到高级诊断技巧

前言

在运维 OpenClaw 系统的过程中,你是否遇到过这样的困惑:系统明明在运行,但总觉得哪里不对劲?或者想给系统做一个全面体检,却不知道从哪里下手?

今天要介绍的工具——openclaw doctor——就是来解决这个问题的。这是一个被很多人忽略但非常有价值的诊断命令。本文将详细介绍这个命令的使用方法、各类诊断项的含义,以及如何根据建议进行修复。

什么是 openclaw doctor

openclaw doctor 是 OpenClaw Gateway 内置的一个诊断工具,顾名思义,它就像是服务器的”医生”,能够对系统进行全面的健康检查,并给出优化建议。

基本用法

1
2
3
4
5
6
7
8
# 在安装了 OpenClaw 的服务器上直接执行
openclaw doctor

# 如果路径不在 PATH 中,可以指定完整路径
/opt/openclaw/bin/openclaw doctor

# 查看帮助信息
openclaw doctor --help

执行后,命令会依次检查多个维度的配置和状态,并以清晰的格式输出检测结果。

诊断项详解

1. 版本信息检查

1
[gateway] update available (latest): v2026.4.1 (current v2026.3.28)

这个检查会对比当前版本和最新版本,提醒管理员及时升级。这是一个容易被忽略但很重要的检查项

为什么重要?

  • 新版本通常包含安全修复
  • 新版本可能修复了已知的 bug
  • 及时升级可以减少被攻击的风险

如何处理?

1
2
3
4
5
6
7
8
# 查看当前版本
openclaw version

# 更新到最新版本
openclaw update

# 或者指定版本
openclaw update --version v2026.4.1

2. 端口暴露检查

1
[gateway] 18789/TCP should probably be behind a reverse proxy instead of exposed directly

这是一个安全相关的检查。18789 端口是 OpenClaw Gateway 的管理接口,直接暴露会有安全风险。

安全风险分析:

  • 如果端口直接暴露在公网,任何人都可以尝试访问
  • 即使有密码保护,仍然存在暴力破解的风险
  • 暴露的管理端口可能成为攻击者的入口

推荐方案:

方案一:通过反向代理访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Nginx 配置示例
server {
listen 443 ssl;
server_name gateway.example.com;

ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;

location / {
proxy_pass http://127.0.0.1:18789;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

方案二:配置防火墙限制

1
2
3
# 仅允许内网段访问 18789
iptables -A INPUT -p tcp --dport 18789 -s 192.168.0.0/16 -j ACCEPT
iptables -A INPUT -p tcp --dport 18789 -j DROP

3. Systemd 服务检查

1
2
[node] systemd unit file not found at ~/.config/systemd/user or /etc/systemd
[node] service not installed, cannot start on boot

这个检查发现 Gateway 没有配置开机自启动。这是一个常见但容易被忽略的问题。

问题场景:

  • 服务器重启后,Gateway 需要手动启动
  • 如果远程维护时遇到重启,需要等待有人物理接触服务器
  • 在凌晨或节假日重启,可能导致长时间服务不可用

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 安装 systemd 服务
openclaw gateway install

# 2. 启用开机自启动
systemctl --user enable openclaw-gateway.service

# 3. 立即启动服务
systemctl --user start openclaw-gateway.service

# 4. 验证服务状态
systemctl --user status openclaw-gateway.service

# 5. 验证服务正在运行
systemctl --user is-active openclaw-gateway.service

验证开机自启动是否生效:

1
2
3
4
# 检查服务是否 enabled
systemctl --user is-enabled openclaw-gateway.service

# 应该返回 "enabled"

4. 消息通道验证

1
[gateway] 钉钉 channel token is set, but we didn't verify that the bot is still active

这个检查发现钉钉的 token 已配置,但系统没有自动验证机器人的活跃状态。

潜在风险:

  • 机器人可能被平台禁用,但管理员不知情
  • Token 可能过期,但系统不会主动发现
  • 需要等到用户反馈才能发现问题

解决方案:

方案一:手动验证

1
2
3
4
5
# 登录钉钉开放平台后台
# 检查机器人应用状态

# 或者通过 API 验证
curl -X GET "https://api.dingtalk.com/robot/getRobotInfo?access_token=YOUR_TOKEN"

方案二:添加自动验证脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# check_dingtalk_bot.sh

TOKEN=$(openclaw config get channels.dingtalk.config.token)

# 调用钉钉 API 验证机器人状态
RESPONSE=$(curl -s "https://api.dingtalk.com/robot/getRobotInfo?access_token=$TOKEN")

if echo "$RESPONSE" | grep -q '"errcode":0'; then
echo "钉钉机器人状态正常"
else
echo "警告:钉钉机器人可能异常"
# 发送告警通知
curl -X POST "https://oapi.dingtalk.com/robot/send?access_token=YOUR_ALERT_TOKEN" \
-H "Content-Type: application/json" \
-d '{"msgtype":"text","text":{"content":"[警告] 钉钉机器人状态异常,请检查"}}'
fi

将脚本加入 cron 定时执行:

1
2
# 每天早上9点检查一次
0 9 * * * /opt/scripts/check_dingtalk_bot.sh >> /var/log/bot_check.log 2>&1

5. Docker 环境检查(如果有容器化部署)

如果 OpenClaw 运行在 Docker 容器中,openclaw doctor 还会检查一些容器特有的配置:

1
2
[docker] container should restart automatically
[docker] healthcheck is not configured

自动重启配置:

1
2
3
4
5
6
7
8
9
10
11
# docker-compose.yml
services:
openclaw:
image: openclaw/openclaw:latest
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:18789/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s

完整修复流程

以下是一个完整的修复脚本,可以一键修复 openclaw doctor 报告的所有常见问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
# openclaw-fix.sh - 一键修复 openclaw doctor 发现的问题

set -e

echo "开始修复 OpenClaw 配置..."

# 1. 更新到最新版本
echo "[1/5] 检查并更新 OpenClaw 版本..."
if command -v openclaw &> /dev/null; then
openclaw update || echo "更新失败,请手动检查"
fi

# 2. 安装 systemd 服务并启用开机自启动
echo "[2/5] 配置 systemd 服务..."
if ! systemctl --user status openclaw-gateway.service &> /dev/null; then
openclaw gateway install
systemctl --user enable openclaw-gateway.service
systemctl --user start openclaw-gateway.service
echo "systemd 服务已配置并启用"
else
echo "systemd 服务已存在"
fi

# 3. 验证服务状态
echo "[3/5] 验证服务状态..."
if systemctl --user is-active openclaw-gateway.service; then
echo "✅ Gateway 服务运行正常"
else
echo "❌ Gateway 服务未运行,尝试启动..."
systemctl --user start openclaw-gateway.service
fi

# 4. 配置防火墙(仅内网访问 18789)
echo "[4/5] 配置防火墙规则..."
if command -v ufw &> /dev/null; then
ufw allow from 192.168.0.0/16 to any port 18789
ufw deny 18789
echo "防火墙规则已配置"
elif command -v iptables &> /dev/null; then
iptables -A INPUT -p tcp --dport 18789 -s 192.168.0.0/16 -j ACCEPT
iptables -A INPUT -p tcp --dport 18789 -j DROP
echo "iptables 规则已配置"
fi

# 5. 验证钉钉连接
echo "[5/5] 验证钉钉连接..."
curl -f -s http://localhost:18789/health > /dev/null && echo "✅ Gateway 健康检查通过" || echo "❌ Gateway 健康检查失败"

echo ""
echo "修复完成!请再次运行 'openclaw doctor' 验证修复结果"

高级诊断技巧

查看详细诊断信息

1
2
3
4
5
6
7
8
# 输出 JSON 格式的诊断结果,便于程序解析
openclaw doctor --format json

# 输出详细日志
openclaw doctor --verbose

# 仅显示警告级别的问题
openclaw doctor --level warning

集成到监控脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# health-monitor.sh - 将诊断结果集成到监控系统

RESULT=$(openclaw doctor 2>&1)

# 检查是否有问题
if echo "$RESULT" | grep -q "update available"; then
echo "版本需要更新"
# 发送告警
fi

if echo "$RESULT" | grep -q "should probably"; then
echo "存在安全建议"
# 记录但不告警
fi

# 将结果写入 Prometheus 格式
cat << EOF > /var/lib/node_exporter/textfile_collector/openclaw_diagnosis.prom
# HELP openclaw_issues_total Total number of issues found by openclaw doctor
# TYPE openclaw_issues_total gauge
openclaw_issues_total{level="warning"} $(echo "$RESULT" | grep -c "warning")
openclaw_issues_total{level="info"} $(echo "$RESULT" | grep -c "info")
EOF

自动修复脚本

1
2
3
4
5
6
7
8
#!/bin/bash
# auto-fix.sh - 自动修复常见问题

# 只修复已知的安全问题,不修改版本配置
openclaw doctor --fix || true

# 记录修复历史
echo "$(date): 自动修复完成" >> /var/log/openclaw-fix.log

常见问题解答

Q1:执行 openclaw doctor 报错 “command not found”

A:说明 openclaw 不在 PATH 中,或者 Gateway 没有正确安装。尝试:

1
2
3
4
5
6
# 查找 openclaw 位置
which openclaw
find / -name "openclaw" -type f 2>/dev/null | head -5

# 如果是 Docker 部署,在容器内执行
docker exec -it openclaw-gateway openclaw doctor

Q2:systemd 服务安装失败

A:常见原因是权限问题。确保以有 sudo 权限的用户执行:

1
2
3
4
5
# 使用 sudo
sudo openclaw gateway install

# 或者启用用户 systemd 模式
loginctl enable-linger $USER

Q3:防火墙规则配置后不生效

A:可能需要保存规则并重启防火墙服务:

1
2
3
4
5
6
7
8
# iptables
iptables-save > /etc/iptables/rules.v4

# ufw
ufw reload

# 然后验证规则
iptables -L -n | grep 18789

Q4:更新版本后服务启动失败

A:可能是配置不兼容。查看日志:

1
2
3
4
5
# 查看 Gateway 日志
journalctl --user -u openclaw-gateway.service -n 100

# 如果是 Docker
docker logs openclaw-gateway

Q5:能否禁用某些检查项?

A:目前不支持禁用特定检查项,但可以忽略某些输出:

1
2
3
4
5
# 仅显示错误
openclaw doctor 2>&1 | grep -v "info:" | grep -v "warning:"

# 保存到文件慢慢看
openclaw doctor > /tmp/doctor-report.txt

最佳实践总结

  1. 定期执行诊断

    • 建议每周至少执行一次 openclaw doctor
    • 可以加入定时任务自动执行
  2. 及时处理警告级别的问题

    • 版本更新、安全建议等应尽快处理
    • 不要忽略警告,它们往往是潜在风险的信号
  3. 记录诊断历史

    • 定期保存诊断报告,便于对比分析
    • 可以发现配置变化的趋势
  4. 自动化修复流程

    • 对于常见问题,编写自动化修复脚本
    • 减少手动操作,避免遗漏
  5. 多节点统一管理

    • 如果有多个 Gateway 节点,建议统一执行诊断
    • 可以用 Ansible 或 Salt 等工具批量操作

延伸阅读

结语

openclaw doctor 是一个非常实用的诊断工具,但它的价值往往被低估。通过定期执行诊断并及时处理发现的问题,可以显著提高系统的稳定性和安全性。

建议所有 OpenClaw 用户都将 openclaw doctor 作为日常运维的一部分,让服务器保持”健康状态”。


作者:小六,一个在上海努力搬砖的程序员

Author:Margrop
Link:http://blog.margrop.com/post/2026-04-04-openclaw-doctor-complete-guide/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可