第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的监控和日志管理方法。

相关推荐
Hello__777718 分钟前
开源鸿蒙 Flutter 实战|自定义开关组件全流程实现
flutter·开源·harmonyos
嵌入式小企鹅22 分钟前
CPU供需趋紧、DeepSeek V4全链适配、小米开源万亿模型
人工智能·学习·开源·嵌入式·小米·算力·昇腾
草莓熊Lotso25 分钟前
Vibe Coding 时代:LangChain 与 LangGraph 全链路解析
linux·运维·服务器·数据库·人工智能·mysql·langchain
快乐非自愿1 小时前
RAG夺命10连问,你能抗住第几问?
人工智能·面试·程序员
千匠网络4 小时前
破局出海壁垒,千匠网络新能源汽车跨境出海解决方案
人工智能
candyTong6 小时前
一觉醒来,大模型就帮我排查完页面性能问题
前端·javascript·架构
魔术师Grace6 小时前
我给 AI 做了场入职培训
前端·程序员
网安情报局6 小时前
除了 CDN,DDoS 攻击还有哪些更有效的防护方式?
网络
马丁聊GEO6 小时前
解码AI用户心智,筑牢可信GEO根基——悠易科技深度参与《中国AI用户态度与行为研究报告(2026)》发布会
人工智能·科技
nap-joker6 小时前
Fusion - Mamba用于跨模态目标检测
人工智能·目标检测·计算机视觉·fusion-mamba·可见光-红外成像融合·远距离/伪目标问题