This topic describes how to use Server Load Balancer (SLB) SDK for Python to collect monitoring metrics of an SLB instance and set alert rules.

Prerequisites

Two Elastic Compute Service (ECS) instances are created in the China (Zhangjiakou) region. Both ECS instances are in the Running state.
Before you run the following code in script mode, make sure that the following requirements are met:
  1. Your account has a balance of at least USD 100 so that you can pay the instance fee that is incurred when you use the Alibaba Cloud SDK for Python to create an SLB instance.
  2. An Access Key is obtained.

    Make sure that you have obtained an AccessKeyID and AccessKeySecret pair (that is, an Access Key), which is used for authentication. If your account does not have an Access Key, see Obtain an AccessKey pair.

  3. Download the SLB Python Example library, which is a library of Alibaba Cloud SLB SDK samples for Python.
  4. Go to the setup.py directory and run the following command to initialize environment configurations:
    python setup.py install

Background information

In this topic, the following steps are performed:
  • Create an SLB instance in the China (Zhangjiakou) region. Set the name of the SLB instance to SLB1, set the primary zone to cn-zhangjiakou-a, and set the secondary zone to cn-zhangjiakou-b.
  • Create a TCP listener for the SLB instance. Set the frontend port that the SLB instance uses to port 80 and set the backend port that receives requests to port 80. Then, set the health check protocol to TCP and add the ECS instances that are deployed in the China (Zhangjiakou) region to the default server group. Set the weights of both ECS instances to 100. Network traffic is then forwarded by the SLB instance to the ECS instances.
  • Query the queries per second (QPS) usage of the new SLB instance.
  • Create an alert rule. If the average QPS usage of the new SLB instance is at least 35, the alert is triggered.
  • Delete the SLB instance.

Procedure

  1. In the directory to which the SDK is downloaded, open the folder $aliyun-openapi-python-sdk-examples\sdk_examples\examples\slb.
  2. Open slb_monitor.py in the editor. Set the ACS_CLIENT parameter to configure user identity verification and set other parameters based on your actual needs. Then, save the file and exit.
    Note The SDK verifies the identity of every user. You can set the AcsClient parameter in a constants file and invoke the constants file when you need it.
  3. Switch to the directory where the slb_monitor.py file is stored and run the following command to view the monitoring data:
    python slb_monitor.py
    The following output is returned:
    ---------------------------create_load_balancer---------------------------
    {
      "VpcId": "",
      "AddressIPVersion": "ipv4",
      "LoadBalancerName": "SLB1",
      "ResourceGroupId": "rg-axxxxxxxxxx",
      "VSwitchId": "",
      "RequestId": "661DDF49-FD2B-46C1-8E38-FDE5E62C8448",
      "Address": "39.xx.xx.xx4",
      "NetworkType": "classic",
      "LoadBalancerId": "lb-8vbtxxxxxxxxxxx1hn"
    }
    
    ---------------------------create_tcp_listener---------------------------
    {
      "RequestId": "7ED453FF-7DF6-4F4F-81A0-670EED9EB997"
    }
    
    ---------------------------add_backend_servers---------------------------
    {
      "RequestId": "4B730606-BD44-4A6B-BE9E-118E9606B064",
      "BackendServers": {
        "BackendServer": [
          {
            "ServerId": "i-8xxxxxxxxxxf4z",
            "Type": "ecs",
            "Weight": 100
          },
          {
            "ServerId": "i-8xxxxxxxxxxxxx",
            "Type": "ecs",
            "Weight": 100
          }
        ]
      },
      "LoadBalancerId": "lb-8xxxxxxxxxxxxxx"
    }
    
    ---------------------------describe_metric_last---------------------------
    {
      "Code": "200",
      "Period": "60",
      "RequestId": "15FD2461-FD94-451F-ABE3-D281DB453161",
      "Datapoints": "[]"
    }
    
    ---------------------------put_resource_metric_rule---------------------------
    {
      "Code": "200",
      "Data": "53547DF14168E368073BE8E34E1522DE25854224",
      "RequestId": "8652E53F-74AD-413B-B2D9-103BFE557FAC",
      "Success": true
    }
    
    ---------------------------delete_load_balancer---------------------------
    {
      "RequestId": "D6CCD259-41C2-4D5C-A644-19A0E96F1D4C"
    }