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

相关推荐
诸神缄默不语7 小时前
小春学习日刊 2026年第4期:2026.10.16
人工智能
keyipatience7 小时前
JavaScript学习(1)书写位置,变量、数据类型、运算符
开发语言·前端·javascript·css·学习
draracle7 小时前
为什么 AI 无法胜任系统架构
人工智能·系统架构
朱涛的自习室7 小时前
我给 Gemini 4 和 DeepSeek 搞了套「末位淘汰」,AI 干活会更猛吗?(Day 3)
android·前端·后端
未来和明天7 小时前
iLeadE-3403边缘计算网关10.4 TOPS数据采集、视频接入与外设扩展等多元化应用需求
人工智能·边缘计算
youngfengying7 小时前
集成学习及置信度
人工智能·机器学习
火山引擎开发者社区8 小时前
1000次专业数据集调用,这次我们免费送!
人工智能
愚公移码8 小时前
蓝凌aiBPM:从“人找系统“到“意图自驱动“——AI 智能体重构企业报销与审批
人工智能·流程引擎·蓝凌
数商思语行8 小时前
企微、Agent平台与业务系统各管什么:读懂系统图
人工智能·ai·供应链·商业分析·ontology·本体·fde