本教程详细介绍如何使用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 -->
        <dependency>
            <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-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.DescribeSiteMonitorDataRequest;
import com.aliyuncs.cms.model.v20190101.DescribeSiteMonitorDataResponse;
import com.aliyuncs.cms.model.v20190101.DescribeSiteMonitorStatisticsRequest;
import com.aliyuncs.cms.model.v20190101.DescribeSiteMonitorStatisticsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

public class DescribeSiteMonitorDataDemo {

    /**
     * 查询任务的细粒度监控数据
     */
    private DescribeSiteMonitorDataResponse describeSiteMonitorData(DescribeSiteMonitorDataRequest request, IAcsClient client) throws ClientException {
        return client.getAcsResponse(request);
    }

    /**
     * 查询指定任务一段时间内的平均统计数据
     */
    private DescribeSiteMonitorStatisticsResponse describeSiteMonitorStatistics(DescribeSiteMonitorStatisticsRequest request, IAcsClient client) throws ClientException {
        return client.getAcsResponse(request);
    }

    private static void log_print(String functionName, Object result) {
        Gson gson = new Gson();
        System.out.println("-------------------------------" + functionName + "-------------------------------");
        System.out.println(gson.toJson(result));
    }

    public static void main(String[] args) {
        // 设置鉴权参数,初始化客户端
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-qingdao",// 地域ID
                "your-access-key-id",// 您的AccessKey ID
                "your-access-key-secret");// 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        DescribeSiteMonitorDataDemo describeSiteMonitorDataDemo = new DescribeSiteMonitorDataDemo();

        try {
            // 查询任务的细粒度监控数据
            DescribeSiteMonitorDataRequest describeSiteMonitorDataRequest = new DescribeSiteMonitorDataRequest();
            // 监控项的名称,可选为:Availability(可用率)、ResponseTime(响应时间)
            describeSiteMonitorDataRequest.setMetricName("Availability");
            // 任务ID
            describeSiteMonitorDataRequest.setTaskId("test");
            // 开始时间,可以传入时间戳,也可以传入format时间格式数据,如2019-10-10 00:00:00
            describeSiteMonitorDataRequest.setStartTime("2019-10-01 00:00:00");
            // 结束时间
            describeSiteMonitorDataRequest.setEndTime("2019-10-20 00:00:00");
            DescribeSiteMonitorDataResponse describeSiteMonitorDataResponse = describeSiteMonitorDataDemo.describeSiteMonitorData(describeSiteMonitorDataRequest, client);
            log_print("describeSiteMonitorData",describeSiteMonitorDataResponse);

            // 查询指定任务一段时间内的平均统计数据
            DescribeSiteMonitorStatisticsRequest describeSiteMonitorStatisticsRequest = new DescribeSiteMonitorStatisticsRequest();
            // 监控项的名称,可选为:Availability(可用率)、ResponseTime(响应时间)
            describeSiteMonitorStatisticsRequest.setMetricName("Availability");
            // 任务ID
            describeSiteMonitorStatisticsRequest.setTaskId("test");
            // 统计的时间周期, 单位是分钟,最大1440分钟(1天)
            describeSiteMonitorStatisticsRequest.setTimeRange("5");
            DescribeSiteMonitorStatisticsResponse describeSiteMonitorStatisticsResponse = describeSiteMonitorDataDemo.describeSiteMonitorStatistics(describeSiteMonitorStatisticsRequest, client);
            log_print("describeSiteMonitorStatistics",describeSiteMonitorStatisticsResponse);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

运行结果

正确的运行结果类似如下:
-------------------------------describeSiteMonitorData-------------------------------
{
  "Data": [
    {
      "Over5totalTime": 0,
      "Error4XXRate": 0,
      "UnavailableNumber": 0,
      "Over2totalTime": 0,
      "Over100FailureRate": 0,
      "timestamp": 1551581040000,
      "Over10FailureRate": 0,
      "Availability": 100,
      "userId": "12345",
      "Over80FailureRate": 0,
      "AvailableNumber": 2,
      "Unavailability": 0,
      "ErrorCodeMaximum": 0,
      "Over10totalTime": 0,
      "taskId": "49f7b317-7645-4cc9-94fd-1****",
      "Over30FailureRate": 0,
      "Error6XXRate": 0,
      "Over500NumberRate": 0,
      "Over3totalTime": 0,
      "Over400NumberRate": 0,
      "Error5XXRate": 0,
      "Error5XXNumber": 0,
      "Over90FailureRate": 0,
      "Error4XXNumber": 0,
      "Over50FailureRate": 0,
      "ErrorCodeMinimum": 0,
      "Error6XXNumber": 0
    },
    {
      "Over5totalTime": 0,
      "Error4XXRate": 0,
      "UnavailableNumber": 0,
      "Over2totalTime": 0,
      "Over100FailureRate": 0,
      "timestamp": 1551581100000,
      "Over10FailureRate": 0,
      "Availability": 100,
      "userId": "12345",
      "Over80FailureRate": 0,
      "AvailableNumber": 2,
      "Unavailability": 0,
      "ErrorCodeMaximum": 0,
      "Over10totalTime": 0,
      "taskId": "49f7b317-7645-4cc9-94fd-1****",
      "Over30FailureRate": 0,
      "Error6XXRate": 0,
      "Over500NumberRate": 0,
      "Over3totalTime": 0,
      "Over400NumberRate": 0,
      "Error5XXRate": 0,
      "Error5XXNumber": 0,
      "Over90FailureRate": 0,
      "Error4XXNumber": 0,
      "Over50FailureRate": 0,
      "ErrorCodeMinimum": 0,
      "Error6XXNumber": 0
    }
  ],
  "RequestId": "DBDEAC51-EA3C-4853-8F25-6B48D38A28A9",
  "Success": true,
  "Code": "200"
}
-------------------------------describeSiteMonitorStatistics-------------------------------
{
    "code": "200",
    "message": "successful",
    "success": "true",
    "requestId": "157DB25A-2AB4-4907-9306-05DD1434B0BF",
    "data": ""
}