温馨提示×

如何监控Debian上Filebeat的状态

小樊
34
2025-11-18 04:42:47
栏目: 智能运维

Debian上监控Filebeat的实用方案

一 基础运行状态检查

  • 使用 systemd 查看服务状态与自启情况:
    • 查看状态:sudo systemctl status filebeat
    • 启动/重启/停止:sudo systemctl start|restart|stop filebeat
    • 设置/取消开机自启:sudo systemctl enable|disable filebeat
  • 查看服务日志(实时与历史):
    • 实时跟踪:sudo journalctl -u filebeat -f
    • 最近一段时间的日志:sudo journalctl -u filebeat --since “2025-11-18 00:00:00”
  • 进程与端口连通性快速确认:
    • 进程是否存在:ps aux | grep filebeat
    • 若作为输出到 Logstash 的收集器,检查端口(默认 5044):sudo ss -tulpen | grep 5044
  • 说明:Filebeat 的日志默认由 systemd 管理,常见日志路径为 /var/log/filebeat/filebeat;也可直接用 journalctl 查看。

二 内置监控与指标导出

  • 启用 Filebeat 自身监控并上报到 Elasticsearch(示例):
    • /etc/filebeat/filebeat.yml 中开启:
      • monitoring.enabled: true
      • monitoring.elasticsearch.hosts: [“http://<es_host>:9200”]
    • 重启生效:sudo systemctl restart filebeat
    • Kibana 中查看 Stack MonitoringBeats 仪表盘,可观察 events 速率、harvester 数量、队列与错误 等关键指标。
  • 使用 Filebeat 的 HTTP API 获取运行时统计(需先启用 API):
    • 本地获取统计:curl -s http://localhost:5066/stats?pretty
    • 远程获取(示例):curl -s http://<filebeat_host>:5066/stats?pretty
    • 常用端点:/stats(统计)、/state(运行时状态)
  • 提示:API 端口与是否启用需在配置中显式开启,生产环境建议配合 TLS访问控制

三 第三方监控与可视化

  • Prometheus + Grafana(推荐用于指标化监控与告警)
    • 部署 PrometheusGrafana,在 Prometheus 配置中添加 Filebeat 的抓取任务(示例):
      • scrape_configs:
        • job_name: ‘filebeat’ static_configs:
          • targets: [‘<filebeat_host>:5066’]
    • Grafana 添加 Prometheus 数据源,导入或创建 Filebeat/Beats 仪表盘,监控 events published、harvester、queue、errors 等。
  • ELK 方案联动
    • 若已使用 Elasticsearch + Logstash + Kibana,可直接在 Kibana Stack Monitoring 中查看 Beats 的运行状态与指标,无需额外部署 Prometheus。

四 日志与告警实践

  • 日志轮转与保留(避免磁盘被占满):
    • /etc/logrotate.d/filebeat 中配置(示例):
      • /var/log/filebeat/*.log {
        • daily
        • missingok
        • rotate 7
        • compress
        • notifempty
        • create 640 root adm
      • }
  • 关键告警建议
    • 服务异常:systemctl is-active filebeat != active
    • 近期错误日志激增:journalctl -u filebeat --since “5m” | grep -i error | wc -l
    • 输出目标不可达(示例 Logstash 5044):ss -tulpen | grep 5044 为空或连接失败
    • 队列积压(通过 API):/statsharvester.finished / events.active 异常增长
  • 快速健康检查脚本(示例)
    • 运行:bash check_filebeat.sh
    • 内容:
      • #!/usr/bin/env bash
      • set -e
      • systemctl is-active --quiet filebeat || { echo “CRIT: filebeat not running”; exit 2; }
      • ss -tulpen | grep -q :5044 || { echo “WARN: port 5044 not listening”; exit 1; }
      • echo “OK: filebeat running and 5044 listening”

0