All Products
Search
Document Center

CloudMonitor:Example of calling SDK for Python

Last Updated:May 30, 2024

This topic uses SDK for Python as an example to describe how to call the DescribeMetricList operation to query the metric data of cloud services.

Step 1: View the API documentation

In the List of operations by function topic, you can find that the DescribeMetricList operation is used to query the metric data of a cloud service. For more information about the data and permissions required to call this operation, see the documentation.

Step 2: Create a RAM user and grant permissions to the RAM user

Identities

You can call this operation by using an Alibaba Cloud account, a Resource Access Management (RAM) user, or a RAM role. For more information about the differences between identities, see Identity. In this topic, a RAM user is used.

Log on to the RAM console, create a RAM user that has the permissions to only call API operations, and record the AccessKey pair. For more information, see Create a RAM user.

Credentials

By default, an AccessKey pair is generated when you create a RAM user. You can also go to the details page of the RAM user. On the Authentication tab, click Create AccessKey to create an AccessKey pair. For more information, see Create an AccessKey pair.

Note

The AccessKey secret of a RAM user is displayed only when the RAM user is created. You cannot view the AccessKey secret after the RAM user is created. Store the AccessKey secret in a secure location.

Authorization

  1. On the Users page in the RAM console, find the RAM user that you want to manage and click Add Permissions in the Actions column.

  2. In the Select Policy section, enter CloudMonitor in the search box, and select the AliyunCloudMonitorFullAccess policy.

    The AliyunCloudMonitorMetricDataReadOnlyAccess system policy is selected in this topic.

    Note
    • CloudMonitor provides the following system policies:

      • AliyunCloudMonitorFullAccess: grants the permissions to manage all CloudMonitor resources.

      • AliyunCloudMonitorReadOnlyAccess: grants the read-only permissions on all CloudMonitor resources.

      • AliyunCloudMonitorMetricDataReadOnlyAccess: grants the permissions to access time series metric data in CloudMonitor.

    • For more information about how to create a custom policy, see Create custom policies.

  3. Click OK.

Step 3: Call an API operation

This topic describes how to use SDK for Python to call an operation. You can use SDKs for other programming languages in a similar way. For more information, see

CloudMonitor SDKs. You can use other methods to call operations based on your business requirements. For more information, see API call methods.

Prepare a Python environment

  1. Download and install Python.

    For more information about how to download Python 3, visit the official website of Python.

  2. Check the version of Python.

    • Linux and macOS

      Open the terminal and run the python --version command.

    • Windows

      • In the Python installation directory, double-click python.exe.

      • Open the Command Prompt.

        Press the Win+R keys to open the Run dialog box, enter python, and then click OK.

Configure environment variables

Configure environment variables on Linux, macOS, and Windows. For more information, see Configure environment variables in Linux, macOS, and Windows.

Install dependencies

pip install alibabacloud_credentials
pip install alibabacloud_cms20190101==2.0.8
pip install alibabacloud_tea_console

Download the sample code

  1. In OpenAPI Explorer, visit the

    endpoint of the DescribeMetricList operation.

  2. Set Namespace to acs_ecs_dashboard, set MetricName to cpu_idle, and then click Initiate Call. You do not need to specify other request parameters.

  3. Click the SDK Sample Code tab. In the Languages section, click Python.

    The system provides the sample code of CloudMonitor SDK for Python.

  4. Click Download Project to download the sample code package.

  5. Decompress the sample code package on your computer and go to the alibabacloud_sample directory.

Run the sample code

Run the following command:

python sample.py

The following output is returned:

{
  "NextToken": "eyJiYXRjaGVzIjoxLCJidWNrZXRCeXRlcyI6Ik9BPT0iLCJidWNrZXRzIjozMiwiY2hyb25vbG9naWNhbCI6IkRFRkFVTFQiLCJjdXJyZW50QnVja2V0Ijo1NiwiY3VycmVudFRhcmdldFRpbWVTbG90IjoxNjkxMDIwODAwLCJlbmRUaW1lIjoxNjkxMDQxMDU5NzYwLCJleHBvcnRFbmRUaW1lIjowLCJleHBvcnRSZXRyeSI6MiwiZXhwb3J0U2Vuc2l0aXZpdHkiOjE1LCJleHBvcnRTdGFydFRpbWUiOjAsImV4cHJlc3NSYW5nZSI6dHJ1ZSwiZXh0cmFSb3VnaFJlZ0V4cyI6W10sImZhaWxGYXN0IjpmYWxzZSwiaGFzTmV4dCI6dHJ1ZSwiaW5jbHVkZUVuZE1hdGNoZXJzNFJhbmdlIjpmYWxzZSwiaW5kZXgiOjAsImxhdGVzdExvZ1RpbWUiOjAsImxpbWl0IjowLCJsb2dUaW1lTW9kZSI6ZmFsc2UsIm1hdGNoZXJzIjp7ImNoYWluIjpbeyJsYWJlbCI6InVzZXJJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIxMjA4ODYzMTc4NjEyOTUzIn1dLCJsb2NrZWQiOnRydWV9LCJtYXhEZXZpYXRpb24iOi0xLCJtZXRyaWMiOiJzeXN0ZW0uY3B1IiwibWV0cmljVHlwZSI6Ik1FVFJJQyIsIm5hbWVzcGFjZSI6ImFjc19ob3N0IiwibmV4dFBrQWRhcHRlciI6e30sIm9mZnNldCI6NSwicmVxdWVzdE1vZHVsZSI6IkFQSSIsInJvdWdoRGltUmVnRXhzIjpbXSwic3RhcnRUaW1lIjoxNjkxMDQwNDY1MDAwLCJzdGVwIjotMSwidGFnTWF0Y2hlcnMiOnsiY2hhaW4iOltdLCJsb2NrZWQiOnRydWV9LCJ0YXJnZXRUaW1lU2xvdHMiOlsxNjkxMDIwODAwXSwidGltZW91dCI6MzAsInV1aWQiOiJhYzVhZGY5NS05MDhlLTRkYjItYjY5OC0yZjg4MjdjZmE5YjEiLCJ3aW5kb3ciOj****",
  "RequestId": "E0168B8A-723E-5528-9FB7-D5B6B68A3222",
  "Period": "15",
  "Datapoints": "[{\"timestamp\":1691040465000,\"instanceId\":\"i-2ze3jvsy7620giru****\",\"userId\":\"120886317861****\",\"Minimum\":99.0,\"Maximum\":100.0,\"Average\":99.67}]",
  "Code": "200",
  "Success": true
}