All Products
Search
Document Center

CloudMonitor:CLI integration example

Last Updated:Sep 23, 2024

Alibaba Cloud Command Line Interface (CLI) is a general-purpose command-line tool that is developed based on APIs. You can use Alibaba Cloud CLI to implement automated management and maintenance of CloudMonitor. This topic describes how to use Alibaba Cloud CLI to call CloudMonitor API operations.

Before you start

Get yourself familiar with Alibaba Cloud CLI. For more information, see What is Alibaba Cloud CLI?

Install Alibaba Cloud CLI

You must install Alibaba Cloud CLI before you can use Alibaba Cloud CLI. You can install Alibaba Cloud CLI on the Windows, Linux, and macOS operating systems. Select an installation package of Alibaba Cloud CLI based on the operating system of your device. For more information, see the following topics:

You can also use Cloud Shell provided by Alibaba Cloud to debug the commands that you want to run in Alibaba Cloud CLI. For more information about Cloud Shell, see What is Cloud Shell?

Configure Alibaba Cloud CLI

Important

An Alibaba Cloud account has the permissions to manage and access the APIs of all Alibaba Cloud services. If you use an Alibaba Cloud account to call API operations, security risks may arise. We recommend that you create and use a Resource Access Management (RAM) user to call API operations or perform routine O&M operations. You must attach the AliyunCloudMonitorFullAccess policy to the RAM user.

Before you use Alibaba Cloud CLI, you must configure credential information such as identity credentials and a region ID in Alibaba Cloud CLI. Alibaba Cloud CLI supports various types of identity credentials. For more information, see Credential types. In this example, AccessKey credentials are used. Perform the following steps:

  1. Create a RAM user and grant the permissions to manage Alibaba Cloud services based on your business requirements. For more information, see Create a RAM user and Grant permissions to a RAM user.

  2. Create an AccessKey pair for the RAM user and record the AccessKey ID and AccessKey secret for the subsequent configuration of identity credentials. For more information, see Create an AccessKey pair.

  3. Specify a region ID. Alibaba Cloud CLI calls API operations in the specified region. For more information about available regions, see Endpoints.

    Note

    When you use Alibaba Cloud CLI, you can use the --region option to specify the region from which you want to initiate a call. If you specify this option, Alibaba Cloud CLI ignores the region information in the default credential configurations and environment variable settings. For more information, see Command line options for API calls.

  4. Use the AccessKey pair of the RAM user to configure identity credentials in the configuration file named AkProfile. For more information, see Configuration examples.

Generate a CLI command

  1. Log on to OpenAPI Portal and click Cloud Monitor.

  2. In the left-side search box of the API debugging page, search for the API operation that you want to call. On the Parameters tab of the API operation, specify the parameters based on the API reference. Click the CLI Example tab on the right side of the Parameters tab to view the generated sample command that contains the specified parameters.

    image

    • Click the Run Commandimage icon to start Cloud Shell and debug the command in Cloud Shell.

    • Click the Copyimage icon to copy the sample command to the clipboard. You can paste the sample command into your shell tool to run the sample command.

      • Pay attention to the parameter formats when you copy and paste the CLI command to the local shell. For more information about the parameter formats of Alibaba Cloud CLI, see Parameter formats.

      • By default, OpenAPI Explorer adds the --region option to the generated CLI command. When you copy the command to your shell tool, Alibaba Cloud CLI ignores the region information in the default credential configurations and environment variable settings, and preferentially runs the command in the region specified by the option. You can delete or retain the option based on your business requirements.

Call API operations of the service

Syntax

In Alibaba Cloud CLI, you can use the following syntax to run commands. For more information, see Syntax.

aliyun <command> <subcommand> [options and parameters]

Command options

When you use Alibaba Cloud CLI, you can specify command options to change the behaviors of commands or implement the extended features of commands. The following command options are frequently used:

  • --profile <profileName>: You can use the --profile option and the profileName parameter to specify a configuration profile. After you specify a valid configuration profile, Alibaba Cloud CLI ignores the information in the default credential configurations and environment variable settings and preferentially uses the configurations that you specify to run commands.

  • --help: You can specify the --help option to obtain the help information about a command. For more information, see Use the help command.

For more information, see Command line options for API calls.

Examples

Example 1: The following sample code describes how to use the --help option to obtain the list of Cloud Monitor API operations supported by Alibaba Cloud CLI. You can also view the API operations that can be called in List of operations by function.

  1. Run the following command:

    aliyun cms --help
  2. View the command output.

    image

Example 2: The following sample code describes how to use Alibaba Cloud CLI to call the CreateMonitorGroup operation of Cloud Monitor to create an application group named ECS_Group.

  1. Run the following command:

    aliyun cms CreateMonitorGroup --GroupName ECS_Group --ContactGroups ECS_Alert_Group
  2. View the command output.

    {
        "Code": 200,
        "GroupId": 238918447,
        "RequestId": "6512EB11-5BF6-5504-A1Ad-B4451E2E15D2",
        "Success": true
    }
    Note

    If an error is returned after you call an API operation of CloudMonitor, you must check whether the request parameters and values are valid based on the returned error code.

    You can also perform self-service diagnostics based on the returned request ID or SDK error information on the OpenAPI problem diagnosis page.