notify.sh
#!/bin/sh
#########DESC##########
#keepalive切换告警提示#
#######################
## 配置文件和手机号码配置
config_file="/etc/keepalived/hostinfo.cfg"
alert_user_file="/etc/keepalived/alert_user.cfg"
keepalive_config_file="/etc/keepalived/keepalived.conf"
df_network_name=`grep -v '#' ${keepalive_config_file} | grep 'interface' | awk -F" " '{print $2}'`
phone="188****1234"
alert_url="http://192.168.10.101:10165/mcoms/clogin.do?method=sendMsgDailyAsForm&"
## 获取对应配置value信息
GetValueInfo() {
project_name=`echo ${basic_info} | cut -d '[' -f 2 | cut -d ']' -f 1`
vip=`echo ${basic_info} | awk -F 'vip=' '{print $2}' | cut -d ' ' -f 1`
master_host=`echo ${basic_info} | awk -F 'master=' '{print $2}' | cut -d ' ' -f 1`
backup_host=`echo ${basic_info} | awk -F 'backup=' '{print $2}' | cut -d ' ' -f 1`
}
## 获取本机IP对应配置文件中的配置信息
GetConfigInfo() {
host_ip=`ip a |grep -E "brd.*${df_network_name}" |awk '{print $2}' |awk -F '/' '{print $1}'`
host_count=`grep -w ${host_ip} ${config_file} | wc -l`
if [[ ${host_count} -eq 0 ]];then
warningbody="【客户端】${host_ip}: 主机信息未添加"
elif [[ ${host_count} -gt 1 ]];then
warningbody="【客户端】${host_ip}: 主机信息存在重复"
else
#basic_info=`awk -vRS="[" '/'''${host_ip}'''/{print RT$0}' ${config_file} | grep -v "^$"`
basic_info=`grep -C 3 -w "${host_ip}" ${config_file} | awk -vRS="[" '/'''${host_ip}'''/{print RT$0}' | grep -v "^$"`
GetValueInfo
fi
}
## 发送告警信息
SendMsgAlert() {
curl -i --data "receivers=${phone}&msgContent=${warningbody}" "$alert_url"
}
## keepalive告警
notify() {
GetConfigInfo
if [ "${warningbody}" != "" ]; then
SendMsgAlert
# echo "error" >> /tmp/keepalive_output.txt
exit
else
warningbody="【客户端】【${project_name}】_RS:${host_ip}_vip:${vip} ChangeTo $1"
# echo ${warningbody} >> /tmp/keepalive_output.txt
SendMsgAlert
fi
}
case "$1" in
master)
notify master
exit 0
;;
backup)
notify backup
exit 0
;;
fault)
notify fault
exit 0
;;
stop)
notify stop
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault|stop}'
exit 1
;;
esac
hostinfo.cfg
[ha_sftp]
vip=192.168.10.188
master=192.168.10.101
backup=192.168.10.102
标签:ip,keepalived,echo,host,sh,awk,notify,print
From: https://www.cnblogs.com/whiteY/p/17385658.html