Cursor API と BatchExport API を使用して、CloudMonitor のメトリクスをリアルタイムでエクスポートします。
前提条件
ハイブリッドクラウド監視が有効化されています。 詳細については、「ハイブリッドクラウド監視の有効化」をご参照ください。
背景情報
ハイブリッドクラウド監視の有効化は無料です。 料金は、使用量に基づいて請求されます。 詳細については、「リアルタイムデータのエクスポート」をご参照ください。
手順
-
DescribeMetricMetaList 操作を呼び出して、各 Alibaba Cloud サービスのメトリックリストを取得します。
パラメーターの詳細については、「DescribeMetricMetaList」をご参照ください。
説明各サービスの
Namespace、MetricName、Dimensions、Period、およびStatisticsの値については、Alibaba Cloud サービスメトリックをご参照ください。-
Maven を使用して CloudMonitor SDK の依存関係を追加します。
<dependency> <groupId>com.aliyun</groupId> <artifactId>cms20190101</artifactId> <version><LATEST_SDK_VERSION></version> </dependency> -
DescribeMetricMetaList API を呼び出して、各 Alibaba Cloud サービスのメトリックリストを取得します。
SDK リクエストの例:
private static Client getClient() throws Exception { Config config = new Config() 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); }
-
-
メトリックの取得
-
SDK 依存関係を pom.xml ファイルに追加します。
<dependency> <groupId>com.aliyun</groupId> <artifactId>cms20190101</artifactId> <version><LATEST_SDK_VERSION></version> </dependency> -
Cursor 操作を呼び出し、データエクスポート範囲を定義します。この操作は、初回の
BatchExport呼び出しに必要なcursorを返します。パラメーターの詳細については、「Cursor」をご参照ください。
Cursor API と BatchExport API は、すべてのリージョンで利用可能です。 詳細については、「エンドポイント」をご参照ください。
SDK リクエストの例:
private static Config config = null; private static Config getConfig(Config config){ config = new Config(); 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 レスポンスの例:
eyJidWNrZXRzIjo0LCJlbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHBvcnRFbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHByZXNzUmFuZ2UiOmZhbHNlLCJoYXNOZXh0Ijp0cnVlLCJsaW1pdCI6NTAwMCwibG9nVGltZU1vZGUiOnRydWUsIm1hdGNoZXJzIjp7ImNoYWluIjpbeyJsYWJlbCI6InVzZXJJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIxNzM2NTExMTM0MzgqKioqIn1dfSwibWV0cmljIjoiQ1BVVXRpbGl6YXRpb24iLCJtZXRyaWNUeXBlIjoiTUVUUklDIiwibmFtZXNwYWNlIjoiYWNzX2Vjc19kYXNoYm9hcmQiLCJuZXh0UGtBZGFwdGVyIjp7fSwib2Zmc2V0IjowLCJzdGFydFRpbWUiOjE2Mzg0MDUwNTA3MTcsInN0ZXAiOi0xLCJ0aW1hb3V0IjoxMjAsIndpbmRvdyI6Nj**** -
Cursor 操作によって定義されたモニタリングデータをエクスポートするには、BatchExport 操作を呼び出します。
パラメーターの詳細については、「BatchExport」をご参照ください。
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":"eyJuYW1lc3BhY2UiOiJhY3NfZWNzX2Rhc2hib2FyZCIsICJtZXRyaWMiOiJDrivitXRpbGl6YXRpb24iLCAic3RhcnRUaW1lIjoxNjM4NDA1MDUwNzE3LCAiZW5kVGltZSI6OTIyMzM3MjAzNjg1NDc3NTgwNywgIm1hdGNoZXJzIjp7ImNoYWluIjpbeyJsYWJlbCI6InVzZXJJZCIsInZhbHVlIjoiMTczNjUxMTEzNDM4KioqKiJ9XX0sICJvZmZzZXQiOjEwMDB9****", "dataResults":[ { "associated":{ }, "dimensions":[ { "label":"userId", "value":"173651113438****" }, { "label":"instanceId", "value":"i-bp1gprn2a56g3b****" } ], "measurements":{ "Maximum":15.5, "Average":10.2, "Minimum":5.1 }, "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:
Availabilityメトリクスの監視データを、サイト監視のacs_networkmonitor名前空間で継続的にエクスポートします。-
カーソルを取得します。
public class GetDataUtil { private static Config config = null; private static Config getConfig(Config config){ config = new Config(); 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(); } // ... } -
モニタリングデータをエクスポートします。
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 秒タイマーを使用します:
public class Demo1 { private String cursor; @Scheduled(cron = "*/3 * * * * ?") public void getCpuData() throws Exception { System.out.println("Start retrieving data."); 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("Finished retrieving data."); } }
-
-
例 2: サイト監視の
acs_networkmonitor名前空間にある、AvailabilityおよびAvailability_newメトリックのモニタリングデータを継続的にエクスポートします。-
カーソルを取得します。
public class GetDataUtil { private static Config config = null; private static Config getConfig(Config config){ config = new Config(); 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(); } // ... } -
モニタリングデータをエクスポートします。
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; } } } -
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("Start retrieving data."); 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("Finished retrieving data."); } }
-