当您需要导出云上监控数据时,可以使用云监控提供的实时数据导出功能。
前提条件
背景信息
开通企业云监控不会产生任何费用,后续将按照实际使用量进行计费。关于实时数据导出的计费标准,请参见实时数据导出。
操作步骤
实时导出监控数据示例
- 示例1:连续导出云监控中站点监控
acs_networkmonitor
的监控指标Availability
的监控数据。- 获取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; } // ... }
- 导出监控数据。
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; } } }
- 连续导出监控指标
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("结束获取数据。"); } }
- 获取Cursor。
- 示例2:连续导出云监控中站点监控
acs_networkmonitor
的监控指标Availability
和Availability_new
的监控数据。- 获取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; } // ... }
- 导出监控数据。
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; } } }
- 连续导出监控指标
Availability
和Availability_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("结束获取数据。"); } }
- 获取Cursor。