返回社区案例

修复Prometheus监控配置YAML缩进错误导致服务反复重启失败

发布时间:2026/7/9浏览量:409

【最近实战】

背景

我在一台服务器上部署了Prometheus进行监控,之前配置了监控Prometheus自身(localhost:)。现在需要新增监控本机(...***),于是在配置文件中新增了一个job_name: "localhost_monitoring"。修改后执行systemctl restart prometheus,服务未能正常启动。

目标

解决Prometheus因YAML缩进错误导致服务反复重启失败的问题,正确添加对本机的监控配置。

操作过程

步骤1:确认问题

我先通过systemctl status prometheus查看服务状态,发现服务处于activating (auto-restart)状态,主进程退出码为2:

● prometheus.service - Prometheus Monitoring System
   Active: activating (auto-restart) (Result: exit-code)
   Main PID: *** (code=exited, status=2)

接着我提出查看日志,通过Wisdom SSH执行journalctl -u prometheus命令查看错误详情,日志反复显示:

parsing YAML file /usr/local/prometheus/prometheus.yml: yaml: line 29: did not find expected key

错误分析:这表明配置文件的第29行存在YAML语法问题,Prometheus在加载配置时解析失败。

步骤2:排查配置文件

通过Wisdom SSH读取配置文件/usr/local/prometheus/prometheus.yml,我看到了新增的配置片段:

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:***"]
    - job_name: "localhost_monitoring"
     static_configs:
      - targets: ["***.***.***.***:***"]
       labels:
        app: "localhost_monitoring"

问题根源:- job_name: "localhost_monitoring"的缩进不对——它与static_configs在同一层级,导致它被解析为第一个job的内部元素,而不是scrape_configs下的独立列表项。此外,static_configs、labels等行的缩进也不一致,导致YAML解析器在第29行找不到预期的键。

步骤3:尝试通过编辑工具修复缩进

AI助手多次通过Wisdom SSH的文件编辑功能尝试修正缩进:

------- SEARCH
    - job_name: "localhost_monitoring"
     static_configs:
      - targets: ["***.***.***.***:***"]
       labels:
=======
    - job_name: "localhost_monitoring"
     static_configs:
      - targets: ["***.***.***.***:***"]
       labels:
        app: "localhost_monitoring"
+++++++ REPLACE

但每次修改后重启,仍然报第29行的YAML错误。多次尝试后,配置文件还出现了重复的app: "localhost_monitoring"标签等问题。Wisdom SSH每次编辑后都会自动创建备份文件(如_usr_local_prometheus_prometheus.yml.bak.***),这些备份保存在Wisdom SSH本地客户端的存储中,而非服务器上。

步骤4:处理node_exporter问题

在操作过程中,我还注意到node_exporter服务也启动失败了。通过日志发现,其systemd服务文件的ExecStart行末尾多了一个反斜杠\:

ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=***.***.***.***:*** \

这个多余的反斜杠导致命令解析错误,出现error: unexpected \。通过Wisdom SSH的编辑功能去掉末尾反斜杠后,重新加载并启动,问题解决。

步骤5:彻底重写配置文件

在多次编辑仍未解决Prometheus配置问题后,AI助手决定直接用正确的YAML内容重写整个配置文件。通过Wisdom SSH执行cat >命令覆盖文件,使用Heredoc语法写入正确格式的配置:

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:***"]

  - job_name: "localhost_monitoring"
    static_configs:
      - targets: ["***.***.***.***:***"]
        labels:
          app: "localhost_monitoring"

关键点:- job_name: "localhost_monitoring"与- job_name: "prometheus"在同一缩进层级(两个空格),两者都是scrape_configs下的列表元素。static_configs、labels等子属性使用正确的缩进层级。

步骤6:验证并重启

文件重写后,通过Wisdom SSH执行语法验证:

/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml

输出:SUCCESS: /usr/local/prometheus/prometheus.yml is valid prometheus config file syntax

然后重启服务并查看状态:

● prometheus.service - Prometheus Monitoring System
   Active: active (running)

Prometheus成功启动,Server is ready to serve,问题解决。

技术总结

  1. YAML缩进敏感:Prometheus配置文件使用YAML格式,缩进必须一致。同一个列表中的元素必须使用相同的缩进层级,属性的缩进必须比父级多至少一个空格。

  2. 错误定位:错误信息中的行号可能存在偏差,因为YAML解析器在遇到缩进问题时可能无法准确报告错误位置,需要结合上下文分析。

  3. systemd中的反斜杠:在systemd服务文件的ExecStart行中,末尾的反斜杠会被当作续行符处理。如果不是多行命令,多余的\会导致命令解析失败。

  4. 操作效率:整个排查和修复过程中,我通过与AI助手的自然对话完成所有操作——查看日志、读取文件、编辑配置、验证语法、重启服务,无需手动SSH登录和执行命令。Wisdom SSH的AI辅助功能提升了运维排障的效率,尤其是遇到反复编辑仍无法解决的问题时,直接重写文件这种方式能避免累积的错误。

本文由用户分享,展示了Wisdom SSH在AI运维中的实际应用

推荐社区案例

点击阅读真实AI运维案例