實(shí)時(shí)導(dǎo)出監(jiān)控?cái)?shù)據(jù)
當(dāng)您需要導(dǎo)出云上監(jiān)控?cái)?shù)據(jù)時(shí),可以使用云監(jiān)控提供的實(shí)時(shí)數(shù)據(jù)導(dǎo)出功能。
前提條件
請(qǐng)確保您已開通企業(yè)云監(jiān)控。具體操作,請(qǐng)參見開通企業(yè)云監(jiān)控。
背景信息
開通企業(yè)云監(jiān)控不會(huì)產(chǎn)生任何費(fèi)用,后續(xù)將按照實(shí)際使用量進(jìn)行計(jì)費(fèi)。關(guān)于實(shí)時(shí)數(shù)據(jù)導(dǎo)出的計(jì)費(fèi)標(biāo)準(zhǔn),請(qǐng)參見實(shí)時(shí)數(shù)據(jù)導(dǎo)出。
操作步驟
調(diào)用DescribeMetricMetaList接口獲取各云產(chǎn)品的監(jiān)控指標(biāo)列表。
關(guān)于DescribeMetricMetaList接口中各參數(shù)的設(shè)置方法,請(qǐng)參見DescribeMetricMetaList。
說明您可以從云產(chǎn)品監(jiān)控項(xiàng)中獲取各云產(chǎn)品監(jiān)控指標(biāo)的
Namespace
、MetricName
、Dimensions
、Period
和Statistics
。通過Maven引入云監(jiān)控的SDK。
<dependency> <groupId>com.aliyun</groupId> <artifactId>cms20190101</artifactId> <version><SDK包的最新版本></version> </dependency>
請(qǐng)您獲取云監(jiān)控SDK包的最新版本,具體操作如下:
在OpenAPI頁(yè)面的SDK示例頁(yè)簽,將鼠標(biāo)移至SDK信息。
在SDK信息對(duì)話框,您可以查看SDK包名稱和SDK最新版本。
調(diào)用DescribeMetricMetaList接口獲取各云產(chǎn)品的監(jiān)控指標(biāo)列表。
SDK請(qǐng)求示例如下:
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); }
獲取監(jiān)控指標(biāo)。
通過Pom引入云監(jiān)控獲取監(jiān)控?cái)?shù)據(jù)的SDK。
<dependency> <groupId>com.aliyun</groupId> <artifactId>cms20190101</artifactId> <version><SDK包的最新版本></version> </dependency>
請(qǐng)您獲取云監(jiān)控SDK包的最新版本,具體操作如下:
單擊Cursor接口。
在OpenAPI頁(yè)面的SDK示例頁(yè)簽,將鼠標(biāo)移至SDK信息。
在SDK信息對(duì)話框,您可以查看SDK包名稱和SDK最新版本。
調(diào)用Cursor接口定義導(dǎo)出監(jiān)控?cái)?shù)據(jù)的范圍,并返回用于初始調(diào)用BatchExport接口的Cursor值。
關(guān)于Cursor接口中各參數(shù)的設(shè)置方法,請(qǐng)參見Cursor。
Cursor和BatchExport支持所有地域,請(qǐng)參見服務(wù)接入點(diǎn)。
SDK請(qǐng)求示例如下:
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返回結(jié)果如下:
eyJidWNrZXRzIjo0LCJlbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHBvcnRFbmRUaW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3LCJleHByZXNzUmFuZ2UiOmZhbHNlLCJoYXNOZXh0Ijp0cnVlLCJsaW1pdCI6NTAwMCwibG9nVGltZU1vZGUiOnRydWUsIm1hdGNoZXJzIjp7ImNoYWluIjpbeyJsYWJlbCI6InVzZXJJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIxNzM2NTExMTM0Mzg5MTEwIn0seyJsYWJlbCI6InRhc2tJZCIsIm9wZXJhdG9yIjoiRVFVQUxTIiwidmFsdWUiOiIqIn1dfSwibWV0cmljIjoiQXZhaWxhYmlsaXR5X29yaWdpbmFsIiwibWV0cmljVHlwZSI6Ik1FVFJJQyIsIm5hbWVzcGFjZSI6ImFjc19uZXR3b3JrbW9uaXRvciIsIm5leHRQa0FkYXB0ZXIiOnt9LCJvZmZzZXQiOjAsInN0YXJ0VGltZSI6MTYzODQwNTA1MDcxNywic3RlcCI6LTEsInRpbWVvdXQiOjEyMCwid2luZG93Ijo2****
調(diào)用BatchExport接口導(dǎo)出在Cursor接口中定義的監(jiān)控?cái)?shù)據(jù)。
關(guān)于BatchExport接口中各參數(shù)的設(shè)置方法,請(qǐng)參見BatchExport。
SDK請(qǐng)求示例如下:
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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Results":[ { "associated":{ }, "dimensions":[ { "label":"userId", "value":"173651113438****" }, { "label":"instanceId", "value":"r-bp12meeb25mb7d****" }, { "label":"nodeId", "value":"r-bp12meeb25mb7d****-proxy-3" } ], "measurements":{ "Maximum":565.916, "Average":565.916 }, "metric":"SplitrwProxyMaxResponseSize", "namespace":"acs_kvstore", "period":60, "timestamp":1688014560000 } ], "hasNext":true, "length":1, "message":"success", "requestId":"8D26C6ED-991C-533E-B601-B020EDF7****", "success":true }
實(shí)時(shí)導(dǎo)出監(jiān)控?cái)?shù)據(jù)示例
示例1:連續(xù)導(dǎo)出云監(jiān)控中站點(diǎn)監(jiān)控
acs_networkmonitor
的監(jiān)控指標(biāo)Availability
的監(jiān)控?cái)?shù)據(jù)。獲取Cursor。
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() 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(); } // ... }
導(dǎo)出監(jiān)控?cái)?shù)據(jù)。
public class GetDataUtil { // ... 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; } } }
連續(xù)導(dǎo)出監(jiān)控指標(biāo)
Availability
的監(jiān)控?cái)?shù)據(jù)。例如:設(shè)置定時(shí)器為3秒,即每3秒導(dǎo)出一次監(jiān)控?cái)?shù)據(jù)。
public class Demo1 { private String cursor; @Scheduled(cron = "*/3 * * * * ?") public void getCpuData() throws Exception { System.out.println("開始獲取數(shù)據(jù)。"); 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("結(jié)束獲取數(shù)據(jù)。"); } }
示例2:連續(xù)導(dǎo)出云監(jiān)控中站點(diǎn)監(jiān)控
acs_networkmonitor
的監(jiān)控指標(biāo)Availability
和Availability_new
的監(jiān)控?cái)?shù)據(jù)。獲取Cursor。
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() 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(); } // ... }
導(dǎo)出監(jiān)控?cái)?shù)據(jù)。
public class GetDataUtil { // ... 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; } } }
連續(xù)導(dǎo)出監(jiān)控指標(biāo)
Availability
和Availability_new
的監(jiān)控?cái)?shù)據(jù)。例如:設(shè)置定時(shí)器為3秒,即每3秒導(dǎo)出一次監(jiān)控?cái)?shù)據(jù)。
public class Demo2 { private String availabilityCursor; private String availability_newCursor; @Scheduled(cron = "*/3 * * * * ?") public void getCpuData() throws Exception { System.out.println("開始獲取數(shù)據(jù)。"); 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("結(jié)束獲取數(shù)據(jù)。"); } }