MQTT のトピックを subscribe して一時的に保持し、prometheus 向けに exporter として働くデーモンを書いた。要は pushgateway の MQTT 版のようなもの。
うちではセンサーデータの一部を MQTT サーバに送りつけるようにしてあるので、それを面倒なことなしに prometheus に登録していきたい。
使いかたと挙動
mqtt_topic_exporter --mqtt.server=mqtts://user:pass@mqttserver:8883 --mqtt.topic="/home/sensor/+"
みたいに引数を指定する。mqtt.topic はそのまま subscribe に渡される。ワイルドカードも使える。
mqtt_topic_exporter は MQTT サーバに接続して値を待つ。値がきたらメッセージ内容を数値として扱い gauge のメトリクスとして登録する。
/metrics にアクセスすると以下のようにメトリクスが登録される。
mqtt_topic{topic="/home/sensor/temp"} 30.0
また、topic がしばらく受信できないと、該当メトリクスは一定時間で削除される。
./mqtt_topic_exporter --help
usage: mqtt_topic_exporter --mqtt.server=MQTT.SERVER --mqtt.topic=MQTT.TOPIC [<flags>]
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--web.listen-address=":9981"
Address on which to expose metrics and web interface.
--web.telemetry-path="/metrics"
Path under which to expose metrics.
--mqtt.retain-time="1m" Retain duration for a topic
--mqtt.server=MQTT.SERVER MQTT Server address URI mqtts://user:pass@host:port
--mqtt.topic=MQTT.TOPIC ...
Watch MQTT topic
--log.level="info" Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]
--log.format="logger:stderr"
Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true"
--version Show application version.
- トップ
-
tech
-
MQTT のトピックを prometheus に登録する