本教程详细介绍如何使用Alibaba Cloud SDK for Java查询云监控插件信息。

前提条件

在使用本教程前,请确保已完成以下操作:

  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见阿里云开发工具包(SDK)
    <dependencies>     <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core --&gt;     <dependency&gt;         <groupId>com.aliyun</groupId>         <artifactId>aliyun-java-sdk-core</artifactId>         <version>4.4.3</version>     </dependency>     <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-ecs -->     <dependency>         <groupId>com.aliyun</groupId>         <artifactId>aliyun-java-sdk-ecs</artifactId>         <version>4.17.4</version>     </dependency>     <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-cms -->     <dependency>         <groupId>com.aliyun</groupId>         <artifactId>aliyun-java-sdk-cms</artifactId>         <version>7.0.4</version>     </dependency> </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.cms.model.v20190101.*; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.google.gson.Gson; import java.util.List;  /**  * 查询云监控插件信息。  */ public class DescribeMonitoringAgentInfoDemo {       /**      * 查询所有的已安装和未安装云监控插件的主机列表。      */     private DescribeMonitoringAgentHostsResponse describeMonitoringAgentHosts(DescribeMonitoringAgentHostsRequest request, IAcsClient client) {         try {             // 调用SDK发送请求             return client.getAcsResponse(request);         } catch (ClientException e) {             e.printStackTrace();             // 发生调用错误,抛出运行时异常             throw new RuntimeException();         }     }      /**      * 查询云监控插件运行状态。      */     private DescribeMonitoringAgentStatusesResponse describeMonitoringAgentStatuses(DescribeMonitoringAgentStatusesRequest request, IAcsClient client) {         try {             // 调用SDK发送请求             return client.getAcsResponse(request);         } catch (ClientException e) {             e.printStackTrace();             // 发生调用错误,抛出运行时异常             throw new RuntimeException();         }     }      /**      * 查询云监控插件的配置信息。      */     private DescribeMonitoringAgentConfigResponse describeMonitoringAgentConfig(DescribeMonitoringAgentConfigRequest request, IAcsClient client){         try {             // 调用SDK发送请求             return client.getAcsResponse(request);         } catch (ClientException e) {             e.printStackTrace();             // 发生调用错误,抛出运行时异常             throw new RuntimeException();         }     }      private static void log_print(String methodName, String result) {         System.out.println("-------------------------------" + methodName + "-------------------------------");         System.out.println(result);     }      public static void main(String[] args) {         // 设置鉴权参数,初始化客户端         DefaultProfile profile = DefaultProfile.getProfile(                 "cn-zhangjiakou",// 地域ID                 "your-access-key-id",// 您的AccessKey ID                 "your-access-key-secret");// 您的AccessKey Secret         IAcsClient client = new DefaultAcsClient(profile);         Gson gson = new Gson();          DescribeMonitoringAgentInfoDemo describeMonitoringAgentInfoDemo = new DescribeMonitoringAgentInfoDemo();          // 获取安装云监控插件的主机列表         DescribeMonitoringAgentHostsRequest describeMonitoringAgentHostsRequest = new DescribeMonitoringAgentHostsRequest();         DescribeMonitoringAgentHostsResponse describeMonitoringAgentHostsResponse = describeMonitoringAgentInfoDemo.describeMonitoringAgentHosts(describeMonitoringAgentHostsRequest, client);         log_print("describeMonitoringAgentHosts",gson.toJson(describeMonitoringAgentHostsResponse));          // 将返回的主机列表拼接成一个逗号分隔的字符串         List<DescribeMonitoringAgentHostsResponse.Host> hosts = describeMonitoringAgentHostsResponse.getHosts();         StringBuilder instanceIdsb =new StringBuilder();         for (DescribeMonitoringAgentHostsResponse.Host host : hosts) {             String instanceId = host.getInstanceId();             instanceIdsb.append(instanceId);             instanceIdsb.append(",");         }         String instanceIdsStr = instanceIdsb.toString();         // 去掉最后一个逗号         String instanceIds = instanceIdsStr.substring(0, instanceIdsStr.length() - 1);          // 查询云监控插件运行状态         DescribeMonitoringAgentStatusesRequest describeMonitoringAgentStatusesRequest = new DescribeMonitoringAgentStatusesRequest();         describeMonitoringAgentStatusesRequest.setInstanceIds(instanceIds);         DescribeMonitoringAgentStatusesResponse describeMonitoringAgentStatusesResponse = describeMonitoringAgentInfoDemo.describeMonitoringAgentStatuses(describeMonitoringAgentStatusesRequest, client);         log_print("describeMonitoringAgentStatuses",gson.toJson(describeMonitoringAgentStatusesResponse));          // 查询云监控插件的配置信息         DescribeMonitoringAgentConfigRequest describeMonitoringAgentConfigRequest = new DescribeMonitoringAgentConfigRequest();         DescribeMonitoringAgentConfigResponse describeMonitoringAgentConfigResponse = describeMonitoringAgentInfoDemo.describeMonitoringAgentConfig(describeMonitoringAgentConfigRequest, client);         log_print("describeMonitoringAgentConfig",gson.toJson(describeMonitoringAgentConfigResponse));     } }

运行结果

正确运行结果类似如下:

-------------------------------describeMonitoringAgentHosts------------------------------- {     "code": "200",     "success": true,     "pageTotal": 1,     "total": 2,     "hosts": [         {             "instanceId": "i-wz9a5tlmvl3pydvcxxxx",             "serialNumber": "ec8274bc-be51-46ca-9f8d-3394c2afxxxx",             "hostName": "iZwz9a5tlmvl3pydvcxxxx",             "aliUid": 1231579085529123,             "operatingSystem": "Linux",             "ipGroup": "119.xx.xx.180,192.168.1.79",             "region": "cn-shenzhen",             "isAliyunHost": true,             "networkType": "vpc",             "instanceTypeFamily": "ecs.n1"         },         {             "instanceId": "i-uf615433jwf1y83gxxxx",             "serialNumber": "5d8c9b31-ee1e-4f8d-8163-9cf97e1exxxx",             "hostName": "i-uf615433jwf1y83gxxxx",             "aliUid": 1231579085529123,             "operatingSystem": "Linux",             "ipGroup": "172.19.93.243",             "region": "cn-shanghai",             "isAliyunHost": true,             "networkType": "vpc",             "instanceTypeFamily": "ecs.n1"         }     ] } -------------------------------describeMonitoringAgentStatuses------------------------------- {     "code": "200",     "success": true,     "nodeStatusList": [         {             "instanceId": "i-wz9a5tlmvl3pydvcxxxx",             "autoInstall": true,             "status": "running"         },         {             "instanceId": "i-uf615433jwf1y83gxxxx",             "autoInstall": true,             "status": "running"         }     ] } -------------------------------describeMonitoringAgentConfig------------------------------- {     "code": "200",     "success": true,     "autoInstall": true,     "enableInstallAgentNewECS": true,     "enableActiveAlert": "" }