すべてのプロダクト
Search
ドキュメントセンター

Cloud Monitor:リアルタイムモニタリングデータのエクスポート

最終更新日:Jun 03, 2026

Cursor API と BatchExport API を使用して、CloudMonitor の監視メトリクスをリアルタイムでエクスポートします。

前提条件

ハイブリッドクラウド監視が有効化されている必要があります。詳細については、「ハイブリッドクラウド監視の有効化」をご参照ください。

背景情報

ハイブリッドクラウド監視の有効化は無料です。料金は使用量に基づいて課金されます。詳細については、「リアルタイムデータのエクスポート」をご参照ください。

手順

  1. DescribeMetricMetaList API を呼び出して、各 Alibaba Cloud サービスのメトリックリストを取得します。

    パラメーターの詳細については、「DescribeMetricMetaList」をご参照ください。

    説明

    各サービスの NamespaceMetricNameDimensionsPeriod、および Statistics の値は、Alibaba Cloud サービスのメトリックに記載されています。

    1. Maven を使用して CloudMonitor SDK の依存関係を追加します。

      <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>cms20190101</artifactId>
        <version><LATEST_SDK_VERSION></version>
      </dependency>
    2. DescribeMetricMetaList API を呼び出して、各 Alibaba Cloud サービスのメトリックリストを取得します。

      SDK のリクエストサンプル:

      private static Client getClient() throws Exception {
          Config config = new Config()
          // アクセスキー ID を設定します。
          config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
          // アクセスキーシークレットを設定します。
        	config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
          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.xml ファイルに SDK の依存関係を追加します。

      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>cms20190101</artifactId>
          <version><LATEST_SDK_VERSION></version>
      </dependency>
    2. Cursor API を呼び出して、データエクスポートの範囲を定義します。この API は、最初の BatchExport 呼び出しに必要なカーソルを返します。

      パラメーターの詳細については、「Cursor API」をご参照ください。

      Cursor API と BatchExport API はすべてのリージョンで利用可能です。詳細については、「エンドポイント」をご参照ください。

      SDK のリクエストサンプル:

      private static Config config = null;
      private static Config getConfig(Config config){
        config = new Config();
        // アクセスキー ID を設定します。
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        // アクセスキーシークレットを設定します。
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.setEndpoint("metrics.cn-hangzhou.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);
        	List<Matcher> matchers = new ArrayList<>();
        	Matcher matcher = new Matcher();
        	matcher.setLabel("userId");
          matcher.setValue("173651113438****");
        	matcher.setOperator("EQUALS");
        	matchers.add(matcher);
          cursorRequest.setMatchers(matchers);
          CursorResponse cursorResponse = client.cursor(cursorRequest);
          if (cursorResponse == null || cursorResponse.getBody() == null || cursorResponse.getBody().getCursor() == null) {
            return null;
          }
          return cursorResponse.getBody().getCursor();
      }

      SDK のレスポンスサンプル:

      eyJidWNrZXRzIjo0LCJlbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHBvcnRFbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHByZXNzUmFuZ2UiOmZhbHNlLCJoYXNOZXh0Ijp0cnVlLCJsaW1pdCI6NTAwMCwibG9nVGltZU1vZGUiOnRydWUsIm1hdGNoZXJzIjp7ImNoYWluIjpbeyJsYWJlbCI6InVzZXJJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIxNzM2NTExMTM0Mzg5MTEwIn0seyJsYWJlbCI6Imluc3RhbmNlSWQiLCJvcGVyYXRvciI6IkVRVUFJUyIsInZhbHVlIjoiKiJ9XX0sIm1ldHJpYyI6IkNQVVRpbGl6YXRpb24iLCJtZXRyaWNUeXBlIjoiTUVUUklDIiwibmFtZXNwYWNlIjoiYWNzX2Vjc19kYXNoYm9hcmQiLCJuZXh0UGtBZGFwdGVyIjp7fSwib2Zmc2V0IjowLCJzdGFydFRpbWUiOjE2Mzg0MDUwNTA3MTcsInN0ZXAiOi0xLCJ0aW1lb3V0IjoxMjAsIndpbmRvdyI6Nj****
    3. BatchExport API を呼び出して、Cursor API で定義された監視データをエクスポートします。

      パラメーターの詳細については、「BatchExport API」をご参照ください。

      SDK のリクエストサンプル:

      private static String BatchExport(String cursor) throws Exception {
          Client client = new Client(getConfig(config));
          BatchExportRequest request = new BatchExportRequest();
          request.setNamespace("acs_ecs_dashboard");
          request.setMetric("CPUUtilization");
          request.setCursor(cursor);
          request.setLength(1000);
          BatchExportResponse response = client.batchExport(request);
          if (response == null || response.getBody() == null) {
            return null;
          }else {
            String json = JSON.toJSONString(response.getBody());
            return json;
          }
      }

      SDK のレスポンスサンプル:

      {
          "anchor":"168801465****",
          "code":200,
          "cursor":"eyJidWNrZXRzIjo0LCJlbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHBvcnRFbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHByZXNzUmFuZ2UiOmZhbHNlLCJoYXNOZXh0Ijp0cnVlLCJsaW1pdCI6NTAwMCwibG9nVGltZU1vZGUiOnRydWUsIm1hdGNoZXJzIjp7ImNoYWluIjpbeyJsYWJlbCI6InVzZXJJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIxNzM2NTExMTM0Mzg5MTEwIn0seyJsYWJlbCI6Imluc3RhbmNlSWQiLCJvcGVyYXRvciI6IkVRVUFJUyIsInZhbHVlIjoiKiJ9XX0sIm1ldHJpYyI6IkNQVVRpbGl6YXRpb24iLCJtZXRyaWNUeXBlIjoiTUVUUklDIiwibmFtZXNwYWNlIjoiYWNzX2Vjc19kYXNoYm9hcmQiLCJuZXh0UGtBZGFwdGVyIjp7fSwib2Zmc2V0IjowLCJzdGFydFRpbWUiOjE2Mzg0MDUwNTA3MTcsInN0ZXAiOi0xLCJ0aW1lb3V0IjoxMjAsIndpbmRvdyI6Nj****",
          "dataResults":[ 
              {
                  "associated":{
      
                  },
                  "dimensions":[
                      {
                          "label":"userId",
                          "value":"173651113438****"
                      },
                      {
                          "label":"instanceId",
                          "value":"i-bp12meeb25mb7d****"
                      }
                  ],
                  "measurements":{
                      "Maximum":15.25,
                      "Average":12.5
                  },
                  "metric":"CPUUtilization",
                  "namespace":"acs_ecs_dashboard",
                  "period":60,
                  "timestamp":1688014560000
              }
          ],
          "hasNext":true,
          "length":1, 
          "message":"success",  
          "requestId":"8D26C6ED-991C-533E-B601-B020EDF7****",
          "success":true
      }

  • 例 1: サイトモニタリングの acs_networkmonitor 名前空間にある Availability メトリックのモニタリングデータを継続的にエクスポートします。

    1. カーソルを取得します。

      public class GetDataUtil {
          private static Config config = null;
      		private static Config getConfig(Config config){
        	config = new Config();
        	// アクセスキー ID を設定します。
        	config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        	// アクセスキーシークレットを設定します。
        	config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        	config.setEndpoint("metrics.cn-hangzhou.aliyuncs.com");
        	return config;
      	}
      	private static String getCursor(String namespace, String metric, int period, long startTime, long endTime, List<Matcher> matchers) throws Exception {
      		Client client = new Client(getConfig(config));
       	  CursorRequest cursorRequest = new CursorRequest();
       	  cursorRequest.setNamespace(namespace);
          cursorRequest.setMetric(metric);
          cursorRequest.setPeriod(period);
          cursorRequest.setStartTime(startTime);
          cursorRequest.setEndTime(endTime);
          cursorRequest.setMatchers(matchers);
          CursorResponse cursorResponse = client.cursor(cursorRequest);
          if (cursorResponse == null || cursorResponse.getBody() == null || cursorResponse.getBody().getCursor() == null) {
            return null;
          }
          return cursorResponse.getBody().getCursor();
      	}
      
          // ...
      }
    2. 監視データをエクスポートします。

      public class GetDataUtil {
          // ...
      
          private static String BatchExport(String cursor, String namespace, String metric, int length) throws Exception {
          	Client client = new Client(getConfig(config));
          	BatchExportRequest request = new BatchExportRequest();
          	request.setNamespace(namespace);
          	request.setMetric(metric);
          	request.setCursor(cursor);
          	request.setLength(length);
          	BatchExportResponse response = client.batchExport(request);
          	if (response == null || response.getBody() == null) {
            	return null;
          	}else {
            	String json = JSON.toJSONString(response.getBody());
            	return json;
          	}
      	}
      }
    3. Availability メトリクスの監視データを継続的にエクスポートします。

      次の例では、3秒のタイマーを使用しています。

      public class Demo1 {
          private String cursor;
          @Scheduled(cron = "*/3 * * * * ?")
          public void getCpuData() throws Exception {
              System.out.println("データの取得を開始します。");
              if(cursor == null){
                  Matcher matcher = new Matcher();
                  matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
                  List<Matcher> 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.BatchExport(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 名前空間にある、Availability および Availability_new メトリクスの監視データを継続的にエクスポートします。

    1. カーソルを取得します。

      public class GetDataUtil {
          private static Config config = null;
      		private static Config getConfig(Config config){
        	config = new Config();
        	// アクセスキー ID を設定します。
        	config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        	// アクセスキーシークレットを設定します。
        	config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        	config.setEndpoint("metrics.cn-hangzhou.aliyuncs.com");
        	return config;
      	}
      	private static String getCursor(String namespace, String metric, int period, long startTime, long endTime, List<Matcher> matchers) throws Exception {
      		Client client = new Client(getConfig(config));
       	  CursorRequest cursorRequest = new CursorRequest();
       	  cursorRequest.setNamespace(namespace);
          cursorRequest.setMetric(metric);
          cursorRequest.setPeriod(period);
          cursorRequest.setStartTime(startTime);
          cursorRequest.setEndTime(endTime);
          cursorRequest.setMatchers(matchers);
          CursorResponse cursorResponse = client.cursor(cursorRequest);
          if (cursorResponse == null || cursorResponse.getBody() == null || cursorResponse.getBody().getCursor() == null) {
            return null;
          }
          return cursorResponse.getBody().getCursor();
      	}
      
          // ...
      }
    2. 監視データをエクスポートします。

      public class GetDataUtil {
          // ...
      
          private static String BatchExport(String cursor, String namespace, String metric, int length) throws Exception {
          	Client client = new Client(getConfig(config));
          	BatchExportRequest request = new BatchExportRequest();
          	request.setNamespace(namespace);
          	request.setMetric(metric);
          	request.setCursor(cursor);
          	request.setLength(length);
          	BatchExportResponse response = client.batchExport(request);
          	if (response == null || response.getBody() == null) {
            	return null;
          	}else {
            	String json = JSON.toJSONString(response.getBody());
            	return json;
          	}
      	}
      }
    3. Availability メトリクスと Availability_new メトリクスの監視データを継続的にエクスポートします。

      次の例では、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){
                  Matcher matcher = new Matcher();
                  matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
                  List<Matcher> 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){
                  Matcher matcher = new Matcher();
      						matcher.setLabel("userId").setOperator("EQUALS").setValue("173651113438****");
                  List<Matcher> 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.BatchExport(availabilityCursor,"acs_networkmonitor","Availability",10);
              String sth2 = GetDataUtil.BatchExport(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("データの取得が完了しました。");
          }
      }