Amazon LinuxにMQTTブローカーMosquittoをインストールしてログ出力させる

2015.11.09

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

MQTT ブローカー Mosquitto(1.4.4) を Amazon Linux(2015.09) にインストールし

  • syslog 経由で /var/log/messages
  • カスタムログ /var/log/mosquitto/mosquitto.log

に出力させる手順を紹介します。

インストール

Mosquitto や Amazon Linux のパッケージレポジトリに存在しません。

CentOS 6 向けの Mosquitto のレポジトリを追加し、サーバ(mosquitto とクライアント(mosquitto-clientsをインストールします。

$ sudo curl http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-6/home:oojah:mqtt.repo -o /etc/yum.repos.d/mqtt.repo
$ sudo yum install -y mosquitto-clients mosquitto

mosquitto サーバの起動設定

mosquitto を自動起動するよう設定します。

  1. rc スクリプト(chkconfig 経由)で制御
  2. monit デーモンで制御

の2種類を説明します。片方だけで対応すればよく、後者は mosquitto プロセスが停止した時の自動起動も備えています。

(1) rc スクリプト(chkconfig 経由)で制御

$ sudo chkconfig --add mosquitto
$ sudo chkconfig mosquitto on
$ sudo chkconfig mosquitto --list
mosquitto       0:off   1:off   2:on    3:on    4:on    5:on    6:off

mosquitto サーバを起動します。

$ sudo service mosquitto start
Starting Mosquitto MQTT broker                             [  OK  ]
1446908874: mosquitto version 1.4.4 (build date 2015-09-24 08:08:55+0000) starting
1446908874: Config loaded from /etc/mosquitto/mosquitto.conf.
1446908874: Opening ipv4 listen socket on port 1883.
1446908874: Opening ipv6 listen socket on port 1883.

(2)monit デーモンで制御

ユーザーバイザーの monit を利用します。

まずは monit をインストールし、monit 自体に自動起動設定を実施します。

$ sudo yum install -y monit
$ sudo chkconfig --add monit
$ sudo chkconfig monit on
$ sudo chkconfig monit --list
monit           0:off 1:off 2:on 3:on 4:on 5:on 6:off

次に monit の設定で、mosquitto プロセスの自動起動や、異常終了時の自動復旧設定を追加します。

ファイル /etc/monit.d/mosquitto.conf を追加します。

check process mosquitto with pidfile /var/run/mosquitto.pid
start = "/etc/init.d/mosquitto start"
stop = "/etc/init.d/mosquitto stop"

最後に /etc/monit.conf を編集し、プロセスの自動復旧パラメーターを調整します。

1分間隔で監視対象のプロセスをチェックし、停止している場合は起動させます。
set daemon  60           # check services at 1-minute intervals
     with start delay 240  # optional: delay the first check by 4-minutes (by
# set daemon  120           # check services at 2-minute intervals
#     with start delay 240  # optional: delay the first check by 4-minutes (by

ログ出力させる

mosquitto サーバはデフォルト設定ではログ出力が有効になっていません。

デフォルトの設定ファイル(/etc/mosquitto/mosquitto.conf)はそのままにし、ログ出力用の設定を /etc/mosquitto/conf.d/logging.conf に追加します。

ログ出力先を指定する

ログ出力先は log_dest で指定します。

今回は syslog とファイルに出力するため、次のように指定します。

log_dest syslog
log_dest file /var/log/mosquitto/mosquitto.log

出力先ごとに複数行で指定します。

mosquitto サーバは mosquitto ユーザで実行されるため、 /var/log/mosquitto 以下に出力できるように、権限設定します。

$ sudo mkdir /var/log/mosquitto
$ sudo chown mosquitto /var/log/mosquitto

ログタイプを指定する

出力するログタイプは log_type で指定します。

#log_type debug
log_type error
log_type warning
log_type notice
log_type information
#log_type none
log_type subscribe
log_type unsubscribe
#log_type websockets
#log_type all

出力タイプごとに複数行で指定します。

その他の細かい設定

クライアントからの接続・切断のログ出力は connection_messages で制御します。

connection_messages true

ログへのタイムスタンプ出力は log_timestamp で制御します。

log_timestamp true

最終的なログ出力設定

最終的に、ログ出力設定(/etc/mosquitto/conf.d/logging.conf)は以下のようになりました。

# =================================================================
# Logging
# =================================================================

# Places to log to. Use multiple log_dest lines for multiple
# logging destinations.
# Possible destinations are: stdout stderr syslog topic file
log_dest syslog
log_dest file /var/log/mosquitto/mosquitto.log


# Types of messages to log. Use multiple log_type lines for logging
# multiple types of messages.
#log_type debug
log_type error
log_type warning
log_type notice
log_type information
#log_type none
log_type subscribe
log_type unsubscribe
#log_type websockets
#log_type all

# If set to true, client connection and disconnection messages will be included
# in the log.
connection_messages true

# If set to true, add a timestamp value to each log message.
log_timestamp true

ログ出力を確認する

サーバ設定を再読込する

新しい設定ファイルを読み込みましょう。

$ sudo service mosquitto reload
Reloading mosquitto:                                       [  OK  ]

ログ出力を確認する

$ mosquitto_pub -d -t test -m "hello world" のように、 mosquitto にメッセージ送信し、ログを確認してみます。

まずは syslog からです。

$ sudo tail /var/log/messages
Nov  7 15:30:28 ip-172-31-29-251 mosquitto[23351]: New client connected from 127.0.0.1 as mosqsub/23499-ip-172-31 (c1, k60).
Nov  7 15:30:28 ip-172-31-29-251 mosquitto[23351]: mosqsub/23499-ip-172-31 1 test
Nov  7 15:30:41 ip-172-31-29-251 mosquitto[23351]: New connection from 127.0.0.1 on port 1883.
Nov  7 15:30:41 ip-172-31-29-251 mosquitto[23351]: New client connected from 127.0.0.1 as mosqpub/23500-ip-172-31 (c1, k60).
Nov  7 15:30:41 ip-172-31-29-251 mosquitto[23351]: Client mosqpub/23500-ip-172-31 disconnected.
Nov  7 15:31:37 ip-172-31-29-251 mosquitto[23351]: Socket error on client mosqsub/23499-ip-172-31, disconnecting.

次にカスタムログファイルを確認します。

$ cat /var/log/mosquitto/mosquitto.log
1446911011: New connection from 127.0.0.1 on port 1883.
1446911011: New client connected from 127.0.0.1 as mosqsub/23600-ip-172-31 (c1, k60).
1446911011: mosqsub/23600-ip-172-31 0 test
1446911013: New connection from 127.0.0.1 on port 1883.

logrotate 設定

今回は新規に /var/log/mosquitto/mosquitto.log にログ出力させています。

/etc/logrotate.d/mosquitto を追加し、日次でこのログファイルがローテートされるようにします。

/var/log/mosquitto/mosquitto.log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    daily
    postrotate
        /sbin/service mosquitto reload > /dev/null 2>/dev/null || true
    endscript
}

まとめ

今回は MQTT サーバ mosquitto に基本的なログ設定を行いました。

/etc/mosquitto/mosquitto.conf.example には設定例がコメント付きで書かれていますので、設定をカスタマイズしたい方はこのファイルを参考にすると良いでしょう。

参考