第13章 Mosquitto监控与日志管理

张开发
2026/4/8 21:23:06 15 分钟阅读

分享文章

第13章 Mosquitto监控与日志管理
第13章 监控与日志管理13.1 监控指标监控指标连接指标消息指标性能指标系统指标客户端连接数连接速率断开速率消息接收消息发送消息存储消息延迟吞吐量队列深度CPU使用率内存使用磁盘IO13.2 系统主题监控# 实时监控脚本#!/bin/bashmosquitto_sub-v-t$SYS/broker/#|whilereadline;doecho$(date%Y-%m-%d %H:%M:%S)-$linedone监控仪表板$SYS主题TelegrafInfluxDBGrafana可视化仪表板13.3 日志管理日志级别日志级别debuginfonoticewarningerror最详细最少日志配置# /etc/mosquitto/mosquitto.conf# 日志类型log_destfile/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_timestamptruelog_timestamp_format %Y-%m-%dT%H:%M:%S13.4 日志轮转# /etc/logrotate.d/mosquitto/var/log/mosquitto/*.log{daily rotate14compress delaycompress missingok notifempty create 0640 mosquitto mosquitto sharedscripts postrotate systemctl reload mosquitto/dev/null21||trueendscript}日志轮转流程是否是否当前日志每天检查超过1天?重命名压缩继续写入保留14天超过14天?删除旧日志保留13.5 Prometheus监控Exporter配置# 使用mosquitto-exporterdockerrun-d\--namemosquitto-exporter\-p9234:9234\sapcc/mosquitto-exporter\--brokermqtt://localhost:1883Prometheus配置# prometheus.ymlscrape_configs:-job_name:mosquittostatic_configs:-targets:[localhost:9234]Grafana仪表板Mosquitto$SYS主题ExporterPrometheusGrafana监控面板13.6 告警配置# alertmanager.ymlroutes:-match:alertname:MosquittoDownreceiver:emailreceivers:-name:emailemail_configs:-to:adminexample.comfrom:alertexample.comsmarthost:smtp.example.com:587告警规则# mosquitto-alerts.ymlgroups:-name:mosquittorules:-alert:MosquittoDownexpr:up{jobmosquitto} 0for:1mlabels:severity:criticalannotations:summary:Mosquitto is down-alert:TooManyConnectionsexpr:mosquitto_clients_connected10000for:5mlabels:severity:warning13.7 本章小结掌握了Mosquitto的监控和日志管理方法。

更多文章