⌚
Mosquittoでタイムスタンプを表示
課題
mosquitto_sub
でイベントにタイムスタンプがないのが困る時がある。
mosquitto_sub
でメッセージを受け取った時間を出す場合は、-F '%j'
とやっておくと良い。メッセージを受信したらタイムスタンプがわかる。
{"tst":"2024-12-04T13:59:41.439232+0900","topic":"t1","qos":0,"retain":0,"payloadlen":35,"payload":"{\"time\":\"2024-12-04T13:59:41+0900\"}"}
ただ、この方法だとイベントは時間がわからない。たとえばPINGはこんな感じ。
Client s33b07786 sending CONNECT
Client s33b07786 received CONNACK (0)
Client s33b07786 sending SUBSCRIBE (Mid: 1, Topic: t1, QoS: 0, Options: 0x00)
Client s33b07786 received SUBACK
Subscribed (mid: 1): 0
Client s33b07786 sending PINGREQ
Client s33b07786 received PINGRESP
Client s33b07786 sending PINGREQ
Client s33b07786 received PINGRESP
Client s33b07786 sending PINGREQ
方法
これにタイムスタンプをつける。ts
は brew install moreutils
stdbuf -oL \
` --cafile AmazonRootCA1.pem \
--cert device.pem.crt \
--key private.pem.key \
-h <CODE>.iot.ap-northeast-1.amazonaws.com \
-p 8883 \
-t t1 \
-i sf6b19c86 \
-k 1200 \
-F '%j' \
-d | ts
こんな感じになる。
Dec 04 12:18:21 Client sf6b19c86 sending CONNECT
Dec 04 12:18:21 Client sf6b19c86 received CONNACK (0)
Dec 04 12:18:21 Client sf6b19c86 sending SUBSCRIBE (Mid: 1, Topic: t1, QoS: 0, Options: 0x00)
Dec 04 12:18:21 Client sf6b19c86 received SUBACK
Dec 04 12:18:21 Subscribed (mid: 1): 0
Dec 04 12:38:21 Client sf6b19c86 sending PINGREQ
Dec 04 12:38:21 Client sf6b19c86 received PINGRESP
Dec 04 13:00:03 Client sf6b19c86 sending PINGREQ
Dec 04 13:00:03 Client sf6b19c86 received PINGRESP
Dec 04 13:20:03 Client sf6b19c86 sending PINGREQ
Dec 04 13:20:03 Client sf6b19c86 received PINGRESP
Dec 04 13:40:03 Client sf6b19c86 sending PINGREQ
Dec 04 13:40:03 Client sf6b19c86 received PINGRESP
Dec 04 13:59:41 Client sf6b19c86 received PUBLISH (d0, q0, r0, m0, 't1', ... (35 bytes))
Dec 04 13:59:41 {"tst":"2024-12-04T13:59:41.439232+0900","topic":"t1","qos":0,"retain":0,"payloadlen":35,"payload":"{\"time\":\"2024-12-04T13:59:41+0900\"}"}
Discussion