Keepalivedの通知ではメールが設定できるが,Slackの方が便利なのでSlack通知を設定する.
手順
ここでは,HAProxyの起動状態をチェック基準にする.
keepalived.conf
! Configuration File for keepalived
vrrp_script chk_haproxy {
script "systemctl is-active haproxy"
}
vrrp_instance VI_1 {
state BACKUP
priority 50
interface ens160
garp_master_delay 10
virtual_router_id 31
advert_int 1
authentication {
auth_type PASS
auth_pass XXXXXXXX
}
virtual_ipaddress {
192.168.100.10
}
track_script {
chk_haproxy
}
notify /etc/keepalived/notify.sh
}
新たに以下のSlack通知のスクリプトを作成した.
notify.sh
#!/bin/bash
TYPE=$1
NAME=$2
STATE=$3
URL='https://hooks.slack.com/services/XXXXXX'
CHANNEL=${CHANNEL:-'#_alert'}
BOTNAME=${BOTNAME:-'keepalived-notice'}
HOSTNAME=`hostname`
MESSAGE="\`${TYPE:-'type'}\` on \`${HOSTNAME:-'host'}\` :: ${STATE:-'state'}"
payload="payload={
\"channel\": \"${CHANNEL}\",TYPE=$1
\"username\": \"${BOTNAME}\",
\"text\": \"${MESSAGE}\"
}"
curl -s -S -X POST --data-urlencode "${payload}" ${URL} > /dev/null
実際にSlack通知された例を以下に示す.