Python SDK call example
Use the Python SDK to call the DescribeMetricList operation and query metric data for a cloud service.
Step 1: Review API documentation
The API overview covers the DescribeMetricList operation for querying cloud service metric data, including required parameters and permissions.
Step 2: Create a RAM user and grant permissions
Identity
You can call this operation with an Alibaba Cloud account, a RAM user, or a RAM role. Differences are explained in Identity. This example uses a RAM user.
Log on to the RAM console and Create a RAM user with API-only access. Save the AccessKey pair.
Credentials
An AccessKey pair is generated by default when you create a RAM user. To create additional pairs, go to the RAM user details page, click the Certification management tab, then click Create AccessKey. Create an AccessKey pair.
The AccessKey Secret displays only at creation and cannot be retrieved later. Save it securely.
Authorization
-
Go to the Users page. Find the target RAM user and click Add Permissions in the Actions column.
-
In the Policy section, search for
CloudMonitor. Select the AliyunCloudMonitorMetricDataReadOnlyAccess policy from the results.This example selects the AliyunCloudMonitorMetricDataReadOnlyAccess system policy.
Note-
Cloud Monitor provides the following system policies:
-
AliyunCloudMonitorFullAccess: Grants full management permissions for Cloud Monitor.
-
AliyunCloudMonitorReadOnlyAccess: Grants read-only permissions for Cloud Monitor.
-
AliyunCloudMonitorMetricDataReadOnlyAccess: Grants permissions to access time-series metric data in Cloud Monitor.
-
-
Create custom permissions using Create a custom policy and Authorization.
-
-
Click OK to complete the authorization.
Step 3: Call the API
This example uses the Python SDK. Other languages are available in Cloud Monitor SDKs. Alternative call methods are listed in API call methods.
Prepare the Python environment
-
Download and install Python.
Download Python 3 from the official Python website.
-
Check your Python version.
-
Linux and macOS
Open the terminal and run the
python --versioncommand. -
Windows
-
In the Python installation directory, double-click python.exe.
-
Open Command Prompt.
Press Win+R to open the Run dialog box. Enter python and click OK.
-
-
Configure environment variables
Configure your AccessKey pair as environment variables following Configure environment variables on Linux, macOS, and Windows.
Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_cms20190101==2.0.8
pip install alibabacloud_tea_console
Download sample code
-
In OpenAPI Explorer, go to the DescribeMetricList operation's debugging page: Debug address.
-
Set Namespace to acs_ecs_dashboard and MetricName to cpu_idle. Leave other parameters empty and click Initiate Call.
-
Click the SDK Sample Code tab, then click the Python tab.
The Python SDK sample code appears.
-
Click Download Project.
-
Decompress the package and navigate to the alibabacloud_sample directory.
Run code
Run the following command:
python sample.py
Expected output:
{
"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
}