Keepalived-企业微信通知
1. 登录企业微信获取相关信息
1.1 创建第三方应用
1.2 企业号中的部门id
1.3 获取刚刚创建的企业应用ID
1.4 获取CorpID是企业号的标识
2.创建python脚本
对应上面获取的企业微信信息更改脚本中企业号中的部门id、#企业号中的应用id、 #CorpID是企业号的标识、#corpsecretSecret应用管理组凭证密钥
#master和backup节点都操作。
[root@nginx_proxy_01 ]# vim /etc/keepalived/wecaht.py
#_*_coding:utf-8 _*_
import urllib,urllib2
import json
import sys
import simplejson
reload(sys)
sys.setdefaultencoding('utf-8')
def gettoken(corpid,corpsecret):
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
# print gettoken_url
try:
token_file = urllib2.urlopen(gettoken_url)
except urllib2.HTTPError as e:
print e.code
print e.read().decode("utf8")
sys.exit()
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
token_json.keys()
token = token_json['access_token']
return token
def senddata(access_token,user,subject,content):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
send_values = {
"touser":'yanmb', #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
"toparty":"2", #企业号中的部门id。 ##填写
"msgtype":"text", #消息类型。
"agentid":"1000006", #企业号中的应用id。 ##填写
"text":{
"content":subject + '\n' + content
},
"safe":"0"
}
# send_data = json.dumps(send_values, ensure_ascii=False)
send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
print(send_data)
send_request = urllib2.Request(send_url, send_data)
response = json.loads(urllib2.urlopen(send_request).read())
print str(response)
if __name__ == '__main__':
user = str(sys.argv[1]) #zabbix传过来的第一个参数
subject = str(sys.argv[2]) #zabbix传过来的第二个参数
content = str(sys.argv[3]) #zabbix传过来的第三个参数
corpid = 'wXXXXXXXX' #CorpID是企业号的标识 ##填写
corpsecret = 'XXXXXXXXX' #corpsecretSecret应用管理组凭证密钥 ##填写
accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,user,subject,content)
以上脚本必须安装simplejson才能执行
wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2
python setup.py build
python setup.py install
测试
[root@lb01 keepalived]# ./wecaht.py 1 2 3
{"text": {"content": "2\n3"}, "safe": "0", "msgtype": "text", "touser": "yanmb", "agentid": "1000006", "toparty": "2"}
{u'invalidparty': u'2', u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}
3. 添加keepalived告警内容
3.1 master节点操作
[root@nginx_proxy_01]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
1124740487@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id master
}
nginx_check.sh
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 51
priority 100
advert_int 1
#添加以下三条 内容可更改
notify_master "/etc/keepalived/wecaht.py nginx_proxy01告警 nginx_proxy01主机keepalived状态切换告警: master状态被激活,请确认keepalived服务器运行状态"
notify_backup "/etc/keepalived/wecaht.py nginx_proxy01告警 nginx_proxy01主机keepalived状态切换告警: backup状态被激活,请确认keepalived服务器运行状态"
notify_fault "/etc/keepalived/wecaht.py nginx_proxy01告警 nginx_proxy01主机keepalived状态切换告警: 主机故障"
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.21.117
}
}
#重启keepalived服务
[root@nginx_proxy_02 keepalived]# systemctl restart keepalived
3.2 backup节点配置
[root@nginx_proxy_02 keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id 50
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
priority 60
advert_int 1
notify_master "/etc/keepalived/wecaht.py nginx_proxy01告警 nginx_proxy02主机keepalived状态切换告警: master状态被激活,请确认keepalived服务器运行状态"
notify_backup "/etc/keepalived/wecaht.py nginx_proxy01告警 nginx_proxy02主机keepalived状态切换告警: backup状态被激活,请确认keepalived服务器运行状态"
notify_fault "/etc/keepalived/wecaht.py nginx_proxy01告警 nginx_proxy02主机keepalived状态切换告警: 主机故障"
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.21.117
}
}
#重启keepalived服务生效
[root@nginx_proxy_02 keepalived]# systemctl restart keepalived
4. 验证
4.1 关闭keepalived01 (master)节点keepalived
systemctl stop keepalived
4.2 再次启动keepalived01(backup)的keepalived服务
systemctl restart keepalived