# coding: utf-8
import json
import requests
import logging
import os,subprocess
#################public_sentinel_idc_a#############
# '10.0.0.1'','10.0.0.2,'10.0.0.3'
################public_sentinel_idc_b###############
# '10.1.0.1'','10.1.0.2,'10.1.0.3'
################public_sentinel_idc_c###############
# '10.2.0.1'','10.2.0.2,'10.2.0.3'
public_sentinels = (''10.0.0.1'','10.0.0.2,'10.0.0.3', '10.1.0.1'','10.1.0.2,'10.1.0.3', '10.2.0.1'','10.2.0.2,'10.2.0.3')
webhook_test = 'https://sd.a.cn/api/v1/webhook/send?key=xxxxxx'
webhook_redis = 'https://sd.a.cn/api/v1/webhook/send?key=ccccccc'
def run_shell(cmd, debug=False):
if debug:
logging.info('Print command but not actually run: %s' % cmd)
return None, None
result = os.popen(cmd).read()
return result.strip()
def render_webhook_msg(msg):
res_msg2 = {
"msgtype":"markdown",
"markdown": {
"text": " ### sre主从redis公共sentinel巡检异常 ### \n\n 列表如下:"+str(msg)+"<at user_id=\"fhsjfhksfs\">张三</at>"
}
}
return res_msg2
def send_webhook(msg):
#req=requests.post(webhook_test,data=json.dumps(msg))
req=requests.post(webhook_redis,data=json.dumps(msg))
if req.status_code == 200:
return req.json()
else:
"go back to response"
if __name__ == "__main__":
err_list = []
for node in public_sentinels:
cmd = "/usr/bin/redis-cli --no-auth-warning -h {0} -p 26379 ping".format(node)
res = run_shell(cmd)
if res != "PONG":
print(res)
err_list.append(node)
if len(err_list) >0 :
msg = render_webhook_msg(err_list)
send_webhook(msg)
else:
print("ksre主从redis公共sentinel 巡检正常")
暂无评论