keepAlived故障切换VIP邮件通知

这是KeepAlived故障切换VIP邮件通知方案二
方案一见: https://blog.csdn.net/m0_38102561/article/details/100699264

一、安装mailx

yum install mailx

二、编辑/etc/mail.rc

mailx安装完成后会生成/etc/mail.rc文件,打开mail.rc文件,在末尾添加以下内容

#发送邮件的邮箱
set from=913011***@qq.com
#smtp邮件服务器
set smtp=smtp.qq.com
#发送邮件的邮箱用户
set smtp-auth-user=913011***@qq.com
#邮箱授权码
set smtp-auth-password=cozc********bceb
set smtp-auth=login
set ssl-verify=ignore

三、编写邮件通知脚本

创建邮件通知脚本文件notify.sh,编辑内容如下

#!/bin/bash
#接收者邮箱,多个以空格分隔
contact=(913011***@qq.com 15773241***@163.com )
#本机ip
HOST_IP=192.169.1.71
notify() {
    #邮件主题
    mailsubject="vip转移,$HOST_IP keepalived to be $1"
    #邮件正文
    mailbody="$(date +'%F %T'): vrrp transition, $HOST_IP keepalived changed to be $1"
    for receiver in ${contact[*]}
    do
        #发送邮件
        echo "$mailbody" | mail -s "$mailsubject" $receiver
    done
}
case $1 in
master)
    notify master
    ;;
backup)
    notify backup
    ;;
fault)
    notify fault
    ;;
*)
    echo "Usage:{master|backup|fault}"
    exit 1
    ;;
esac

编辑完成后,给脚本授予执行权限

chmod +755 notify.sh

四、在keepalived中调用邮件通知脚本

在keepalived的配置文件keepalived.conf中配置notify.sh脚本被触发执行的时机

#当前节点成为主节点时触发的脚本
notify_master "/etc/keepalived/notify.sh master"
#当前节点转为备节点时触发的脚本
notify_backup "/etc/keepalived/notify.sh backup"
#当前节点转为失败状态时触发的脚本
notify_fault "/etc/keepalived/notify.sh fault"

即:
keepAlived故障切换VIP邮件通知_第1张图片

参考地址
https://blog.51cto.com/11886307/2406629
https://blog.csdn.net/lazyRabbitLLL/article/details/79718450
http://blog.rekfan.com/articles/419.html

你可能感兴趣的:(keepalived)