def health_status(slbid, port): '''创建 request,并设置参数 slbid:是负载均衡ID ecsid_list:['slb_id_1','slb_id_2','slb_id_3'] ''' request = DescribeHealthStatusRequest.DescribeHealthStatusRequest() request.set_accept_format('json') request.set_LoadBalancerId(slbid) request.set_ListenerPort(port) response = client.do_action_with_exception(request) print response
def getbackstatus(LoadBalancerId): #获取后端ECS健康状态 #ListenerPort Integer 是 负载均衡实例前端使用的端口。 # 取值:1-65535。默认值:无。 # 不设置该参数表示获取所有端口的健康检查状态。 # LoadBalancerId String 是 负载均衡实例的唯一标识。 # 后端服务器的健康状况,normal,abnormal或unavailable。normal表示状态为健康;abnormal表示状态为不健康;unavailable表示未能完成健康检查,具体原因可能是:未开启健康检查、 dhs = DescribeHealthStatusRequest.DescribeHealthStatusRequest() dhs.set_LoadBalancerId(LoadBalancerId) response = client.do_action_with_exception(dhs) status = json.loads(response)['BackendServers']['BackendServer'] return status
def elb(request): '''查看ELB监控状态''' from aliyunsdkslb.request.v20140515 import DescribeHealthStatusRequest obj = AliyunAPI() LoadBalancerId = 'lb-wz9w9oue3lh0n5ie8wlu6' ListenerPort = 80 res = DescribeHealthStatusRequest.DescribeHealthStatusRequest() res.set_ListenerPort(ListenerPort) res.set_LoadBalancerId(LoadBalancerId) response = obj.client.do_action_with_exception(res) result = response.decode() return HttpResponse(result)
def get_unhealthy_backends(self, slb_id, listener_port): """ :params: SLB实例ID 前端监听端口号 :return: 后端不健康实例ID列表 """ request = DescribeHealthStatusRequest.DescribeHealthStatusRequest() request.set_accept_format('json') request.set_LoadBalancerId(slb_id) request.set_ListenerPort(listener_port) result = self.client \ .do_action(request)['BackendServers']['BackendServer'] return [backend['ServerId'] for backend in result \ if backend['ServerHealthStatus'] == 'abnormal']
def query_slb_health(LoadBalancerId): # 设置参数 request = DescribeHealthStatusRequest.DescribeHealthStatusRequest() request.set_accept_format('json') request.add_query_param('RegionId', 'cn-hangzhou') request.add_query_param('LoadBalancerId', LoadBalancerId) # 发起请求 response = clt.do_action(request) if 'Message' in response: return json.loads(response) else: try: json.loads(response)['BackendServers'] except: return {'success', 'False'} else: BackendServers = json.loads(response)['BackendServers'] BackendServer = BackendServers['BackendServer'] return BackendServer
def DescribeHealthStatus(self,**kwargs): self.request = None self.request =DescribeHealthStatusRequest.DescribeHealthStatusRequest() self.request.add_query_param('LoadBalancerId', kwargs['LoadBalancerId'])