修改lb权重,通知钉钉

前提需要安装阿里的核心库

#!/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":"在修改权重为100的时候,发生了意外"}
        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)

  

set_lb的更多相关文章

随机推荐

  1. Kinect一代学习(一):开发环境搭建

    https://blog.csdn.net/hongbin_xu/article/details/80722749 我用的是kinect一代(Xbox 360)的所以选择了v1.x的SDK,如果是ki ...

  2. UEditor在开发环境中正常运作,但是部署到Tomcat中却无法使用

    背景 ​ SpringBoot项目,在 JSP 中使用 UEditor 问题 ​ UEditor 在开发环境中正常运作,但是导致部署到 Tomcat 中却无法使用 原因 在开发环境中,路径不够严谨,多 ...

  3. ESA2GJK1DH1K开发教程: 来看看最新整理的整个开发板的教程大纲吧

    项目开发:教程大纲(测试板型号:ESA2GJK1DH1K) 购买链接: https://item.taobao.com/item.htm?spm=a2oq0.12575281.0.0.25911deb ...

  4. Web中线程与IIS线程池自动回收机制

    开发Web项目后,部署到 IIS上 ,运行一直稳定,当Web程序中加入了定时任务,或者线程之类的机制后,第二天发现悲催了,定时任务并没有执行,此时重新登录一下网站,定时任务又重新执行.原来IIS默认有 ...

  5. Apollo配置中心--安装使用-docker

    官网:https://github.com/ctripcorp/apollo Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推 ...

  6. kafka shell

    停止 ./bin/kafka-server-stop.sh  启动 nohup sh kafka-server-start.sh ../config/server.properties & 创 ...

  7. 【RabbitMQ学习之二】RabbitMQ四种交换机模式应用

    环境 win7 rabbitmq-server-3.7.17 Erlang 22.1 一.概念1.队列队列用于临时存储消息和转发消息.队列类型有两种,即时队列和延时队列. 即时队列:队列中的消息会被立 ...

  8. 不能随便用get和set

    有些对象呢,保存一半.如果你只提供get和set,那么备份不了数据. previousState的get和set还是最新的 wtforms InputRequired: DataRequired: i ...

  9. Django框架之DRF 认证组件源码分析、权限组件源码分析、频率组件源码分析

    认证组件 权限组件 频率组件

  10. 安装Windows10出现无法识别磁盘时的解决方案

    由于前些日子对deepin系统比较感兴趣,一时兴起把备用机刷成了deepin,奈何还是过分依赖windows下的软件,又不得不再刷回Win10. 但由于Linux支持的磁盘格式与Windows不同,在 ...