当您需要导出云上监控数据时,可以使用云监控提供的实时数据导出功能。

前提条件

请确保您已开通企业云监控。具体操作,请参见开通企业云监控

背景信息

开通企业云监控不会产生任何费用,后续将按照实际使用量进行计费。关于实时数据导出的计费标准,请参见实时数据导出

操作步骤

  1. 调用DescribeMetricMetaList接口获取各云产品的监控指标列表。
    关于DescribeMetricMetaList接口中各参数的设置方法,请参见DescribeMetricMetaList
    说明 您可以从云产品监控项中获取各云产品监控指标的NamespaceMetricNameDimensionsPeriodStatistics
    1. 通过Maven引入云监控的SDK。
      <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>cms20190101</artifactId>
        <version><SDK包的最新版本></version>
      </dependency>
      请您获取云监控SDK包的最新版本,具体操作如下:
      1. 单击DescribeMetricMetaList接口。
      2. OpenAPI页面的SDK示例页签,将鼠标移至SDK依赖信息
        SDK依赖信息对话框,您可以查看SDK包名称和SDK最新版本。SDK_2019
    2. 调用DescribeMetricMetaList接口获取各云产品的监控指标列表。
      SDK请求示例如下:
      private static Client getClient() throws Exception {
          Config config = new Config()
            .setAccessKeyId("LTAI4Fd8LkD2DeS1k79t****")
            .setAccessKeySecret("9y4i7i8ToE9KMjm6Uni57zaMyO****");
          config.endpoint = "metrics.cn-hangzhou.aliyuncs.com";
          return new Client(config);
      }
      public static void main(String[] args) throws Exception {
          Client client = getClient();
          DescribeMetricMetaListRequest describeMetricMetaListRequest = new DescribeMetricMetaListRequest();
          describeMetricMetaListRequest.setNamespace("acs_ecs_dashboard");
          DescribeMetricMetaListResponse describeMetricMetaListResponse = client.describeMetricMetaList(describeMetricMetaListRequest);
          String json = JSON.toJSONString(describeMetricMetaListResponse.getBody());
          System.out.println(json);
      }
  2. 获取监控指标。
    1. 通过Pom引入云监控获取监控数据的SDK。
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>cms_export20211101</artifactId>
          <version><SDK包的最新版本></version>
      </dependency>
      请您获取云监控SDK包的最新版本,具体操作如下:
      1. 单击Cursor接口。
      2. OpenAPI页面的SDK示例页签,将鼠标移至SDK依赖信息
        SDK依赖信息对话框,您可以查看SDK包名称和SDK最新版本。SDK包
    2. 调用Cursor接口定义导出监控数据的范围,并返回用于初始调用BatchGet接口的Cursor值。

      关于Cursor接口中各参数的设置方法,请参见Cursor

      SDK请求示例如下:
      private static Config config = null;
      private static Config getConfig(Config config){
        config = new Config();
        config.setAccessKeyId("LTAI4Fd8LkD2DeS1k79t****");
        config.setAccessKeySecret("9y4i7i8ToE9KMjm6Uni57zaMyO****");
        config.setRegionId("cn-shanghai");
        config.setEndpoint("cms-export.cn-shanghai.aliyuncs.com");
        return config;
      }
      private static String getCursor() throws Exception {
          Client client = new Client(getConfig(config));
          CursorRequest cursorRequest = new CursorRequest();
          cursorRequest.setNamespace("acs_ecs_dashboard");
          cursorRequest.setMetric("CPUUtilization");
          cursorRequest.setPeriod(60);
          cursorRequest.setStartTime(System.currentTimeMillis() - 60 * 60 * 1000);
          cursorRequest.setEndTime(Long.MAX_VALUE);
          CursorRequest.CursorRequestMatchers matcher = new CursorRequest.CursorRequestMatchers();
          matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
          List<CursorRequest.CursorRequestMatchers> chain = new ArrayList<>();
          chain.add(matcher);
          cursorRequest.setMatchers(chain);
          CursorResponse cursorResponse = client.cursor(cursorRequest);
          if (cursorResponse == null || cursorResponse.getBody() == null || cursorResponse.getBody().getData() == null || cursorResponse.getBody().getData().cursor == null) {
            return null;
          }
          return cursorResponse.getBody().getData().cursor;
      }
      说明 regionId仅支持cn-shanghaiendpoint仅支持cms-export.cn-shanghai.aliyuncs.com
      SDK返回结果如下:
      eyJidWNrZXRzIjo0LCJlbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHBvcnRFbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHByZXNzUmFuZ2UiOmZhbHNlLCJoYXNOZXh0Ijp0cnVlLCJsaW1pdCI6NTAwMCwibG9nVGltZU1vZGUiOnRydWUsIm1hdGNoZXJzIjp7ImNoYWluIjpbeyJsYWJlbCI6InVzZXJJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIxNzM2NTExMTM0Mzg5MTEwIn0seyJsYWJlbCI6InRhc2tJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIqIn1dfSwibWV0cmljIjoiQXZhaWxhYmlsaXR5X29yaWdpbmFsIiwibWV0cmljVHlwZSI6Ik1FVFJJQyIsIm5hbWVzcGFjZSI6ImFjc19uZXR3b3JrbW9uaXRvciIsIm5leHRQa0FkYXB0ZXIiOnt9LCJvZmZzZXQiOjAsInN0YXJ0VGltZSI6MTYzODQwNTA1MDcxNywic3RlcCI6LTEsInRpbWVvdXQiOjEyMCwid2luZG93Ijo2****
    3. 调用BatchGet接口导出在Cursor接口中定义的监控数据。

      关于BatchGet接口中各参数的设置方法,请参见BatchGet

      SDK请求示例如下:
      private static String BatchGet(String cursor) throws Exception {
          Client client = new Client(getConfig(config));
          BatchGetRequest batchGetRequest = new BatchGetRequest();
          batchGetRequest.setNamespace("acs_ecs_dashboard");
          batchGetRequest.setMetric("CPUUtilization");
          batchGetRequest.setCursor(cursor);
          batchGetRequest.setLength(1000);
          BatchGetResponse batchGetResponse = client.batchGet(batchGetRequest);
          if (batchGetResponse == null || batchGetResponse.getBody() == null
              || batchGetResponse.getBody().getData() == null) {
            return null;
          }else {
            String json = JSON.toJSONString(batchGetResponse.getBody().getData().records);
            return json;
          }
      }
      SDK返回结果如下:
      {
        "cursor":"eyJidWNrZXRzIjo0LCJjdXJzb3IiOiIxNjM5NDQ0NjgwMDAwMWUxY2YxNWZiNWMwNjY5NjFiZThmNTU4N2RmMzQ4YjRkNDRjOTQ5OTMxMzczMzM2MzUzMTMxMzEzMzM0MzMzODM5MzEzMTMwMjQyYzY5MmQzMDZhNmMzMDdhMzY3NzYyNmE2ODM4NzczNDc4NjM3NjZlMzI2MjY1MjQyYyIsImN1cnNvclZlcnNpb24iOiJxdWVyeSIsImVuZFRpbWUiOjE2Mzk0NDgzMjA4OTMsImV4cG9ydEVuZFRpbWUiOjkyMjMzNzIwMzY4NTQ3NzU4MDcsImV4cG9ydFN0YXJ0VGltZSI6MTYzOTQ0NDY0Mjc0OCwiZXhwcmVzc1JhbmdlIjpmYWxzZSwiaGFzTmV4dCI6dHJ1ZSwiaW5wdXRNZXRyaWMiOiJDUFVVdGlsaXphdGlvbiIsImlucHV0TmFtZXNwYWNlIjoiYWNzX2Vjc19kYXNoYm9hcmQiLCJsaW1pdCI6MSwibG9nVGltZU1vZGUiOnRydWUsIm1hdGNoZXJzIjp7ImNoYWluIjpbeyJsYWJlbCI6InVzZXJJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIxNzM2NTExMTM0Mzg5MTEwIn1dfSwibWV0cmljIjoiQ1BVVXRpbGl6YXRpb24iLCJtZXRyaWNUeXBlIjoiTUVUUklDIiwibmFtZXNwYWNlIjoiYWNzX2Vjc19kYXNoYm9hcmQiLCJuZXh0UGtBZGFwdGVyIjp7fSwib2Zmc2V0IjowLCJwYXJlbnRVaWQiOjEyNzA2NzY2Nzk1NDY3MDQsInN0YXJ0VGltZSI6MTYzOTQ0NDY0Mjc0OCwic3RlcCI6LTEsInRpbWVvdXQiOjEyMCwid2luZG93Ijo2****",
        "length":1,
        "records":[{
          "labelValues":["i-0jl0z6wbjh8w4xcv****","173651113438****"],
          "labels":["instanceId","userId"],
          "measureLabels":["Minimum","Maximum","Average"],
          "measureValues":["0.82","0.82","0.82"],
          "metric":"CPUUtilization",
          "namespace":"acs_ecs_dashboard",
          "period":-1,
          "tagValues":[],
          "tags":[],
          "timestamp":1639444680000
        }]
      }

实时导出监控数据示例

  • 示例1:连续导出云监控中站点监控acs_networkmonitor的监控指标Availability的监控数据。
    1. 获取Cursor。
      public class GetDataUtil {
          private static Config config = null;
      
          private static Config getConfig(Config config) {
              config = new Config();
              config.setAccessKeyId("LTAI4Fd8LkD2DeS1k79t****");
              config.setAccessKeySecret("9y4i7i8ToE9KMjm6Uni57zaMyO****");
              config.setRegionId("cn-shanghai");
              config.setEndpoint("cms-export.cn-shanghai.aliyuncs.com");
              return config;
          }
      
          public static String getCursor(String namespace,String metric,Integer period,Long start,Long end,List<CursorRequest.CursorRequestMatchers> chain) throws Exception {
              Client client = new Client(getConfig(config));
              CursorRequest cursorRequest = new CursorRequest();
              cursorRequest.setNamespace(namespace);
              cursorRequest.setMetric(metric);
              cursorRequest.setPeriod(period);
              cursorRequest.setStartTime(start);
              cursorRequest.setEndTime(end);
              CursorRequest.CursorRequestMatchers matcher = new CursorRequest.CursorRequestMatchers();
              matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
              cursorRequest.setMatchers(chain);
              CursorResponse cursorResponse = client.cursor(cursorRequest);
              if (cursorResponse == null || cursorResponse.getBody() == null || cursorResponse.getBody().getData() == null
                  || cursorResponse.getBody().getData().cursor == null) {
              return null;
              }
              return cursorResponse.getBody().getData().cursor;
          }
      
          // ...
      }
    2. 导出监控数据。
      public class GetDataUtil {
          // ...
      
          public static String BatchGet(String cursor,String namespace,String metric,Integer length) throws Exception {
              Client client = new Client(getConfig(config));
              BatchGetRequest batchGetRequest = new BatchGetRequest();
              batchGetRequest.setNamespace(namespace);
              batchGetRequest.setMetric(metric);
              batchGetRequest.setCursor(cursor);
              batchGetRequest.setLength(length);
              BatchGetResponse batchGetResponse = client.batchGet(batchGetRequest);
              if (batchGetResponse == null || batchGetResponse.getBody() == null
                  || batchGetResponse.getBody().getData() == null) {
              return null;
              }else {
              String json = JSON.toJSONString(batchGetResponse.getBody().getData());
              return json;
              }
          }
      }
    3. 连续导出监控指标Availability的监控数据。
      例如:设置定时器为3秒,即每3秒导出一次监控数据。
      public class Demo1 {
          private String cursor;
          @Scheduled(cron = "*/3 * * * * ?")
          public void getCpuData() throws Exception {
              System.out.println("开始获取数据。");
              if(cursor == null){
                  CursorRequest.CursorRequestMatchers matcher = new CursorRequest.CursorRequestMatchers();
                  matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
                  List<CursorRequest.CursorRequestMatchers> chain  = new ArrayList<>();
                  chain.add(matcher);
                  cursor = GetDataUtil.getCursor("acs_networkmonitor","Availability",60,System.currentTimeMillis() - 60 * 60 * 1000,Long.MAX_VALUE,chain);
              }
              String sth = GetDataUtil.BatchGet(cursor,"acs_networkmonitor","Availability",10);
              JSONObject jsonObject = JSONObject.parseObject(sth);
              if(!StringUtils.isEmpty(jsonObject.getString("cursor"))){
                  cursor = jsonObject.getString("cursor");
              }
              System.out.println("结束获取数据。");
          }
      }
  • 示例2:连续导出云监控中站点监控acs_networkmonitor的监控指标AvailabilityAvailability_new的监控数据。
    1. 获取Cursor。
      public class GetDataUtil {
          private static Config config = null;
      
          private static Config getConfig(Config config) {
              config = new Config();
              config.setAccessKeyId("LTAI4Fd8LkD2DeS1k79t****");
              config.setAccessKeySecret("9y4i7i8ToE9KMjm6Uni57zaMyO****");
              config.setRegionId("cn-shanghai");
              config.setEndpoint("cms-export.cn-shanghai.aliyuncs.com");
              return config;
          }
      
          public static String getCursor(String namespace,String metric,Integer period,Long start,Long end,List<CursorRequest.CursorRequestMatchers> chain) throws Exception {
              Client client = new Client(getConfig(config));
              CursorRequest cursorRequest = new CursorRequest();
              cursorRequest.setNamespace(namespace);
              cursorRequest.setMetric(metric);
              cursorRequest.setPeriod(period);
              cursorRequest.setStartTime(start);
              cursorRequest.setEndTime(end);
              CursorRequest.CursorRequestMatchers matcher = new CursorRequest.CursorRequestMatchers();
              matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
              cursorRequest.setMatchers(chain);
              CursorResponse cursorResponse = client.cursor(cursorRequest);
              if (cursorResponse == null || cursorResponse.getBody() == null || cursorResponse.getBody().getData() == null
                  || cursorResponse.getBody().getData().cursor == null) {
              return null;
              }
              return cursorResponse.getBody().getData().cursor;
          }
      
          // ...
      }
    2. 导出监控数据。
      public class GetDataUtil {
          // ...
      
          public static String BatchGet(String cursor,String namespace,String metric,Integer length) throws Exception {
              Client client = new Client(getConfig(config));
              BatchGetRequest batchGetRequest = new BatchGetRequest();
              batchGetRequest.setNamespace(namespace);
              batchGetRequest.setMetric(metric);
              batchGetRequest.setCursor(cursor);
              batchGetRequest.setLength(length);
              BatchGetResponse batchGetResponse = client.batchGet(batchGetRequest);
              if (batchGetResponse == null || batchGetResponse.getBody() == null
                  || batchGetResponse.getBody().getData() == null) {
              return null;
              }else {
              String json = JSON.toJSONString(batchGetResponse.getBody().getData());
              return json;
              }
          }
      }
    3. 连续导出监控指标AvailabilityAvailability_new的监控数据。
      例如:设置定时器为3秒,即每3秒导出一次监控数据。
      public class Demo2 {
          private String availabilityCursor;
          private String availability_newCursor;
          @Scheduled(cron = "*/3 * * * * ?")
          public void getCpuData() throws Exception {
              System.out.println("开始获取数据。");
              if(availabilityCursor == null){
                  CursorRequest.CursorRequestMatchers matcher = new CursorRequest.CursorRequestMatchers();
                  matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
                  List<CursorRequest.CursorRequestMatchers> chain  = new ArrayList<>();
                  chain.add(matcher);
                  availabilityCursor = GetDataUtil.getCursor("acs_networkmonitor","Availability",60,System.currentTimeMillis() - 60 * 60 * 1000,Long.MAX_VALUE,chain);
              }
              if(availability_newCursor == null){
                  CursorRequest.CursorRequestMatchers matcher = new CursorRequest.CursorRequestMatchers();
                  matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
                  List<CursorRequest.CursorRequestMatchers> chain  = new ArrayList<>();
                  chain.add(matcher);
                  availability_newCursor = GetDataUtil.getCursor("acs_networkmonitor","Availability_new",60,System.currentTimeMillis() - 60 * 60 * 1000,Long.MAX_VALUE,chain);
              }
              String sth1 = GetDataUtil.BatchGet(availabilityCursor,"acs_networkmonitor","Availability",10);
              String sth2 = GetDataUtil.BatchGet(availability_newCursor,"acs_networkmonitor","Availability_new",10);
              JSONObject jsonObject1 = JSONObject.parseObject(sth1);
              if(!StringUtils.isEmpty(jsonObject1.getString("cursor"))){
                  availabilityCursor = jsonObject1.getString("cursor");
              }
              System.out.println("availability");
              System.out.println(sth1);
              JSONObject jsonObject2 = JSONObject.parseObject(sth2);
              if(!StringUtils.isEmpty(jsonObject2.getString("cursor"))){
                  availability_newCursor = jsonObject2.getString("cursor");
              }
              System.out.println("availability_new");
              System.out.println(sth2);
              System.out.println("结束获取数据。");
          }
      }