All Products
Search
Document Center

CloudMonitor:Example of calling an API operation

Last Updated:Sep 01, 2023

This topic describes how to call a CloudMonitor API operation to query the monitoring data of a cloud service.

View the API documentation

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

Create a RAM user and grant permissions to the RAM user

Identities

You can call this operation by using an Alibaba Cloud account, a RAM user, or a RAM role. For more information about the differences between identities, see Identity, credential, and authorization. In this example, a RAM user is used.

Log on to the RAM console, create a RAM user that is only authorized to call API operations, and record the AccessKey information. For information about how to create a RAM user, see Create a RAM user.

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.

    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 RAM authorization.

  3. Click OK to complete the authorization.

Credentials

By default, an AccessKey pair is generated when you create a RAM user. You can directly use the AccessKey pair. 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.

Call an API operation

The following example shows how to call an API operation in Python.

Prepare a Python environment

  1. Download and install Python.

    For more information about how to download Python 3, visit Python 3.

  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.

      • Start the Command Prompt.

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

Configure environment variables

In this example, environment variables are configured to manage the AccessKey pair. This helps prevent security risks that are caused by hard coding the AccessKey pair into your business code.

  • Linux and macOS

    Replace <access_key_id> and <access_key_secret> in the following commands with the actual AccessKey ID and AccessKey secret.

  • export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then specify your AccessKey ID for ALIBABA_CLOUD_ACCESS_KEY_ID and your AccessKey secret for ALIBABA_CLOUD_ACCESS_KEY_SECRET. Then, restart the Windows operating system.

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 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
}