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

相关推荐
程序员黑豆2 小时前
Java类型推断完全指南:从var到菱形运算符,掌握使用限制与最佳实践
java·前端·ai编程
To_OC3 小时前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
fthux3 小时前
装闭 RenoPit 源码解析(09):AnalysisEngine装修闭坑分析主流程
人工智能·ai·开源·github·open source·renopit
敏编程3 小时前
开源项目 NextBand:探索融合健康传感、语音交互与 AI Agent 的下一代可穿戴设备
人工智能
GreenTea3 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
ovO3 小时前
我按下“发送”之后:DeepSeek Harness 如何把一次请求变成 1,177 个增量片段
人工智能·开源·deepseek
Gem_S_6083 小时前
从 Chonkie 到 RAGFlow:主流开源知识库 Chunking 工具横向测评
开源
浮生望3 小时前
前端API工程化:用 Mock 数据与 Axios 配置实现独立于后端的并行开发
前端
昨日之日20063 小时前
LTX-2.5:更清晰、更可控、同步音画、多镜头连贯的AI视频神器
人工智能·音视频
万少4 小时前
给 DeepSeek Harness 装个"应用商店":一条命令,595 个插件随你逛
前端·javascript·后端