This topic describes how to install CloudMonitor SDK for Python and provides sample code.

Background information

  • To use CloudMonitor SDK for Python, you must install the core library of Alibaba Cloud SDK for Python (aliyun-python-sdk-core) and CloudMonitor SDK for Python (aliyun-python-sdk-cms).
  • Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
  • For more information about the CloudMonitor API, see List of API operations by function.

Install CloudMonitor SDK for Python

For more information about how to install CloudMonitor SDK for Python, see Quick start.

You can download the installation packages of the SDKs for Python from the following links:

Sample code

The following sample code provides an example on how to use CloudMonitor SDK for Python to call the CloudMonitor API operation DescribeMetricList. For other operations, you can use OpenAPI Explorer to debug the operations and obtain sample code.

For more information about the request parameters of the DescribeMetricList operation, see DescribeMetricList.

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcms.request.v20190101.DescribeMetricListRequest import DescribeMetricListRequest

// Construct an Alibaba Cloud client that is used to initiate requests. 
// When you construct the client, specify your AccessKey ID and AccessKey secret. 
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')
# Construct a request. 
request = DescribeMetricListRequest()
request.set_accept_format('json')
# Specify the request parameters. 
request.set_MetricName("cpu_total")
request.set_Namespace("acs_ecs_dashboard")
request.set_StartTime("1628055731050")
request.set_EndTime("1628062931050")
request.set_Dimensions("{\"instanceId\":\"i-0xii2bvf42iqvxbp****\"}")
request.set_Length("10")
# Initiate the request and obtain a response. 
response = client.do_action_with_exception(request)
# Python 2:  print(response)
print(str(response, encoding='utf-8'))