第13章 Mosquitto监控与日志管理

第13章 监控与日志管理

13.1 监控指标

监控指标
连接指标
消息指标
性能指标
系统指标
客户端连接数
连接速率
断开速率
消息接收
消息发送
消息存储
消息延迟
吞吐量
队列深度
CPU使用率
内存使用
磁盘IO

13.2 系统主题监控

bash 复制代码
# 实时监控脚本
#!/bin/bash
mosquitto_sub -v -t "$SYS/broker/#" | while read line; do
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $line"
done

监控仪表板

$SYS主题
Telegraf
InfluxDB
Grafana
可视化仪表板

13.3 日志管理

日志级别

日志级别
debug
info
notice
warning
error
最详细
最少

日志配置

bash 复制代码
# /etc/mosquitto/mosquitto.conf

# 日志类型
log_dest file /var/log/mosquitto/mosquitto.log
log_dest syslog
log_dest stdout
log_dest topic

# 日志级别
log_type error
log_type warning
log_type notice
log_type information
# log_type debug

# 日志格式
log_timestamp true
log_timestamp_format %Y-%m-%dT%H:%M:%S

13.4 日志轮转

bash 复制代码
# /etc/logrotate.d/mosquitto

/var/log/mosquitto/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 mosquitto mosquitto
    sharedscripts
    postrotate
        systemctl reload mosquitto > /dev/null 2>&1 || true
    endscript
}

日志轮转流程

是
否
是
否
当前日志
每天检查
超过1天?
重命名+压缩
继续写入
保留14天
超过14天?
删除旧日志
保留

13.5 Prometheus监控

Exporter配置

bash 复制代码
# 使用mosquitto-exporter
docker run -d \
  --name mosquitto-exporter \
  -p 9234:9234 \
  sapcc/mosquitto-exporter \
  --broker=mqtt://localhost:1883

Prometheus配置

yaml 复制代码
# prometheus.yml
scrape_configs:
  - job_name: 'mosquitto'
    static_configs:
      - targets: ['localhost:9234']

Grafana仪表板

Mosquitto
$SYS主题
Exporter
Prometheus
Grafana
监控面板

13.6 告警配置

yaml 复制代码
# alertmanager.yml
routes:
  - match:
      alertname: MosquittoDown
    receiver: 'email'

receivers:
  - name: 'email'
    email_configs:
      - to: admin@example.com
        from: alert@example.com
        smarthost: smtp.example.com:587

告警规则

yaml 复制代码
# mosquitto-alerts.yml
groups:
  - name: mosquitto
    rules:
      - alert: MosquittoDown
        expr: up{job="mosquitto"} == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "Mosquitto is down"

      - alert: TooManyConnections
        expr: mosquitto_clients_connected > 10000
        for: 5m
        labels:
          severity: warning

13.7 本章小结

掌握了Mosquitto的监控和日志管理方法。

相关推荐
子兮曰3 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
回眸&啤酒鸭3 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
子兮曰3 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
一隅论数智3 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
不悔哥3 天前
开源OV-Watch:怎么做一个智能手表
单片机·开源·嵌入式
AI的探索之旅3 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein3 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
家和803 天前
Carla仿真系列:4_在 Carla 的 Tesla 上装 4 路摄像头,并创建网格为环视拼接做准备
开源
前端小万3 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序