MariaDB(MySQL) で synchronous multi-master replication を行うことのできる Codership Galera Clusterでは、クラスタのステータスに変化が生じた(1ノードがダウンした等)際に、my.cnf等でwsrep_notify_cmdパラメータに設定したスクリプト等を呼ぶことができるようになっています。
GitHub でgaleranotify - Python E-Mail script for use with Galera wsrep_notify_cmd という Pythonスクリプトを見つけたので、これを用いてメールで通知を行ってみます。
/etc/my.cnf.d/server.cnf 設定
各 Galera ノードの/etc/my.cnf.d/server.cnfのmysqld セクションに wsrep_notify_cmd を追加します。
[mysqld]
wsrep_notify_cmd = /usr/local/bin/galeranotify.py
MariaDBサーバを再起動します。
systemctl restart mariadb
failover時の通知テスト
データベースプロキシMaxScaleを稼働させている場合は、maxadmin list servers で各ノードのステータス確認ができます。
# maxadmin list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
galera1 | 172.24.119.235 | 3306 | 0 | Master, Synced, Running
galera2 | 172.24.119.236 | 3306 | 0 | Slave, Synced, Running
galera3 | 172.24.119.237 | 3306 | 0 | Slave, Synced, Running
-------------------+-----------------+-------+-------------+--------------------
ここで、便宜上のマスターのgalera1ノードをリブートしてみます(multi-master なのですべてのノードがマスタですが)。
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
galera1 | 172.24.119.235 | 3306 | 0 | Down
galera2 | 172.24.119.236 | 3306 | 0 | Slave, Synced, Running
galera3 | 172.24.119.237 | 3306 | 0 | Master, Synced, Running
-------------------+-----------------+-------+-------------+--------------------
galera1 が Down になりました。galeranotify.py が正しく設定されていれば、メールで以下のような通知が届くはずです。
Sub: Galera Notification: galera1
Galera running on galera1 has reported the following cluster membership changes:
Status of this node: Undefined
Cluster state UUID: 28c469ce-da4f-11e7-8697-dedd385653fd
Current cluster component is primary: No
Current members of the component:
-> d1887400-dd58-11e7-9f06-9eb7242291de/galera1/172.24.119.235:3306
Index of this node in the member list: 0
galera1 が再起動し、MariaDBサーバが復旧すると、以下のような通知メールが届きます。
Sub: Galera Notification: galera1
Galera running on galera1 has reported the following cluster membership changes:
Status of this node: Joined
Cluster state UUID: 28c469ce-da4f-11e7-8697-dedd385653fd
Current cluster component is primary: Yes
Current members of the component:
-> 35a3f041-dd59-11e7-876b-4b5eecd5beff/galera1/172.24.119.235:3306
-- 563d0d2a-dd58-11e7-8ae6-67c18a5d82eb/galera3/172.24.119.237:3306
-- b9f3da31-dd60-11e7-bc8b-a3ddf4c8e1a6/galera2/172.24.119.236:3306
Index of this node in the member list: 0
ダウンした場合も復旧した場合も件名が同一でわかりにくく感じますので、以下のように変更するといいかもしれません。
81: send_notification(MAIL_FROM, MAIL_TO, 'Galera Notification: ' + THIS_SERVER + ' - ' + message_obj._status, DATE,
参考リンク:
https://mariadb.com/kb/en/library/what-is-mariadb-galera-cluster/