set_lb

 

Modify lb weights, nail notice

Ali premise that you need to install the core library

#!/usr/local/python-3.6.4/bin/python3
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515.SetBackendServersRequest import SetBackendServersRequest
from urllib import request as ddrequest
import json
from sys import argv

client = AcsClient('LTAI42e74K2hrtmu', 'f4S7rx9KEFv9Js3AR5KmU6V7g9D4xd', 'cn-beijing')
access_token = "dingding"

def set_lb(request,ecs,weight,loadslb):
    serverid = ecs.get("serverid")
    request.set_accept_format('json')
    slb_list = '[{"ServerId":"%s","Weight":"%s"},]' % (str(serverid),str(weight))
    print(slb_list)
    request.set_BackendServers(slb_list) 
    request.set_LoadBalancerId(loadslb)
    try:  
        response = client.do_action_with_exception(request)
    except Exception as e:
        response = { "hostname": ecs.get ( "hostname"), "ip": ecs.get ( "ip"), "message": "modifying a weight of 100 when the accident occurred"}
        send_msg(response)
    else:
        response = {"hostname":ecs.get("hostname"),"ip":ecs.get("ip"),"message":"修改权重为100成功"}
        send_msg(response)

def send_msg(response):
    url = "https://oapi.dingtalk.com/robot/send?access_token=" + access_token
    data = {
        "msgtype": "text",
        "text": {
            "content": response
        },
        "at": {
            "isAtAll": "false"
        }
    }
    json_data= json.dumps(data).encode(encoding='utf-8')
    print(json_data)
    header_encoding = {"Content-Type": "application/json"}
    req = ddrequest.Request(url=url, data=json_data, headers=header_encoding)
    res = ddrequest.urlopen(req)
    res = res.read()
    print(res.decode(encoding='utf-8'))

if __name__ == "__main__":
    request = SetBackendServersRequest()
    ecs = {"hostname":"pc-prod-1","ip":"ip","serverid":"id"}
    weight = "100"
    loadslb = "lb-id"
    set_lb(request,ecs,weight,loadslb)

  

 

Guess you like

Origin www.cnblogs.com/jokerbj/p/11101935.html