...
|
...
|
@@ -17,35 +17,46 @@ DING = "https://oapi.dingtalk.com/robot/send?access_token=31cf1a526c98862b7945e |
|
|
ansible_url = "http://ansible.yohops.com/api/v2"
|
|
|
ansible_user = "chunhua.zhang"
|
|
|
ansible_password = "Zch@2019"
|
|
|
|
|
|
JENKINS_UPDATE_VAR_URL = "http://jenkins.yohops.com/job/yoho-ansible-roles/buildWithParameters"
|
|
|
JENKINS_TOKEN = "607f13cd5f7ff5bdf5813f9745d7da49"
|
|
|
|
|
|
def main_handler(event, context):
|
|
|
if not event['queryString']
|
|
|
return
|
|
|
print("start to process mysql alarm: %s" % event['queryString'])
|
|
|
return_str = "OK"
|
|
|
if event['queryStringParameters']['method'] != 'master_fail_over' :
|
|
|
return_str = "Method Not Supported"
|
|
|
elif event['queryStringParameters']['auth'] != 'yohomysqlfailover123456887':
|
|
|
elif event['queryString']['auth'] != 'yohomysqlfailover123456887':
|
|
|
return_str = "Auth Failed"
|
|
|
else:
|
|
|
fail_master = event['queryStringParameters']['failmaster']
|
|
|
new_master = event['queryStringParameters']['newmaster']
|
|
|
return_str = " Mysql Failover Proccess Successfully!"
|
|
|
ret = ansible_tower_process(new_master, fail_master)
|
|
|
alert_to_dingding(new_master, fail_master, ret)
|
|
|
fail_host = event['queryString']['host']
|
|
|
alarm = event['queryString']['alarm'] # maybe: MySQL status on 3306 port is Down (0)'
|
|
|
return_str = " Mysql host error Proccess Successfully!"
|
|
|
ret = ansible_tower_process(fail_host, alarm)
|
|
|
alert_to_dingding(fail_host, alarm, ret)
|
|
|
return { "isBase64Encoded": False, "statusCode": 200, "headers": {} , "body": "%s" %return_str }
|
|
|
|
|
|
def ansible_tower_process(down_host):
|
|
|
def ansible_tower_process(down_host, alarm):
|
|
|
if not down_host:
|
|
|
return
|
|
|
|
|
|
if "mysql" not in alarm.lower() or "is down" not in alarm.lower()
|
|
|
print("ignore this alarm :%s" % alarm)
|
|
|
return
|
|
|
## az1, az2, az3
|
|
|
|
|
|
for inventory in [ 6, 5, 4]:
|
|
|
extra_var = {}
|
|
|
## for update git
|
|
|
keys = []
|
|
|
values = []
|
|
|
r = requests.get(ansible_url + "/inventories/%i/" %inventory, auth=(ansible_user, ansible_password))
|
|
|
inventory_vars = json.loads( r.json()['variables'])
|
|
|
for key in inventory_vars:
|
|
|
# find db read key which value == fail-mysql-slave-node
|
|
|
if key.startswith("db") and key.endswith("_read") and inventory_vars[key] == down_host:
|
|
|
extra_var[key] = inventory_vars[ key.replace("_read", "_write") ]
|
|
|
new_read_host = inventory_vars[ key.replace("_read", "_write") ] # change read to write host
|
|
|
extra_var[key] = new_read_host
|
|
|
keys.append(key)
|
|
|
values.append(new_read_host)
|
|
|
|
|
|
if not extra_var:
|
|
|
print("can not find any variables in inventory:%i with read mysql is %s" %(inventory, down_host))
|
...
|
...
|
@@ -72,6 +83,10 @@ def ansible_tower_process(down_host): |
|
|
# 2.1. fetch job status, make sure job success
|
|
|
job_id = r_cobar.json()['job']
|
|
|
make_sure_job_success(job_id)
|
|
|
|
|
|
#update git
|
|
|
jenkins_param = {"token": JENKINS_TOKEN, "MODIFY_ENV": INVENTORY_ENV[inventory], "key": ",".join(keys), "value": ",".join(values)}
|
|
|
requests.get(JENKINS_UPDATE_VAR_URL, params = jenkins_param)
|
|
|
return 0
|
|
|
|
|
|
|
...
|
...
|
@@ -94,13 +109,13 @@ def make_sure_job_success(job_id): |
|
|
retry = retry + 1
|
|
|
return 0
|
|
|
|
|
|
def alert_to_dingding(down_host, ret):
|
|
|
def alert_to_dingding(down_host, alarm, ret):
|
|
|
# send text dingding message. see:https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1
|
|
|
ding_req = {}
|
|
|
ding_req["msgtype"]="markdown"
|
|
|
ding_req["markdown"] = {}
|
|
|
ding_req["markdown"]['title']="Mysql Failover"
|
|
|
ding_req["markdown"]["text"]= "### Mysql Down. Down Host: %s , ret: %i" % (down_host, ret)
|
|
|
ding_req["markdown"]["text"]= "### Mysql Down. Down Host: %s , alarm: %s, ret: %i" % (down_host, alarm, ret)
|
|
|
|
|
|
r = requests.post(url = DING, json = ding_req)
|
|
|
print("Send DingDing message result:%s, request: %s " %(r, ding_req))
|
...
|
...
|
|