keepalived 日志设置 、邮箱设置和通知

独立的日志设置
vim /etc/sysconfig/keepalived 或/usr/local/keepalived/etc/sysconfig/keepalived
	KEEPALIVED_OPTIONS="-D -S 6"
vim /etc/rsyslog.conf
	local6.*    /var/log/keepalived.log
	

 配置发送邮件的邮箱设置:

配置发送邮件的邮箱设置:   yum install mailx -y
 vim ~/.mailrc 或 /etc/mail.rc
set from=348987564@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=348987564@qq.com 
set smtp-auth-password=univdqnrmunjcafh  #邮箱需要开启POP3/SMTP服务并设置密钥
set smtp-auth=login
set ssl-verify=ignore

测试:echo test mail | mail -s testa 收件人id@qq.com

  

 邮件通知和脚本的调用方法:keepalived服务器都要设置。

! Configuration File for keepalived

global_defs {
   notification_email {
    348987564@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id KA1
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance zzhz {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    #nopreempt
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    192.168.80.222/24 dev eth0 label eth0:1
    }


}

  

vim  /etc/keepalived/notify.sh   
#!/bin/bash
#
contact='348987564@qq.com'  #可以结合邮件设置向QQ发送邮件,在全局中的notification_email也要设置
notify() {
mailsubject="$(hostname) to be $1, vip floating"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)

echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac

chmod 777 /etc/keepalived/notify.sh

  

测试
bash /etc/keepalived/notify.sh master  先测试能不能收到邮件

测试说明:当为抢占模式

[root@localhost7B sysconfig]# systemctl  stop  keepalived.service

[root@localhost7B sysconfig]# systemctl restart keepalived.service

 

 

测试说明:非抢占模式  

[root@localhost7B sysconfig]# systemctl  stop  keepalived.service

[root@localhost7B sysconfig]# systemctl restart keepalived.service

 

 

 

posted @ 2022-07-29 11:19  yuanbangchen  阅读(827)  评论(0编辑  收藏  举报