全部产品
存储与CDN 数据库 域名与网站(万网) 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网 钉钉智能硬件
云监控

JavaSDK使用手册

更新时间:2017-08-23 11:33:21

SDK安装

通过maven进行安装,需要添加的依赖如下:

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-core</artifactId>
  5. <version>3.2.6</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.aliyun</groupId>
  9. <artifactId>aliyun-java-sdk-cms</artifactId>
  10. <version>5.0.4</version>
  11. </dependency>
  12. </dependencies>

SDK下载

测试代码

查询监控数据

  • 请求示例

使用时请将如下示例中的accessKey和accessSecret替换成您的Access Key

  1. package com.aliyuncs.cms.test;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.cms.model.v20170301.QueryMetricListRequest;
  6. import com.aliyuncs.cms.model.v20170301.QueryMetricListResponse;
  7. import com.aliyuncs.exceptions.ClientException;
  8. import com.aliyuncs.exceptions.ServerException;
  9. import com.aliyuncs.http.FormatType;
  10. import com.aliyuncs.profile.DefaultProfile;
  11. import com.aliyuncs.profile.IClientProfile;
  12. public class QueryMetricListDemo {
  13. public static void main(String[] args) {
  14. QueryMetricListRequest request = new QueryMetricListRequest();
  15. request.setProject("acs_ecs_dashboard");
  16. request.setMetric("cpu_idle");
  17. request.setPeriod("60");
  18. request.setStartTime("2017-03-14 14:20:27");
  19. request.setEndTime("2017-03-14 14:30:27");
  20. JSONObject dim = new JSONObject();
  21. dim.put("instanceId", "<your_instanceId>");
  22. request.setDimensions(dim.toJSONString());
  23. request.setAcceptFormat(FormatType.JSON);
  24. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
  25. "<your_access_key_id>", "<your_access_key_secret>");
  26. IAcsClient client = new DefaultAcsClient(profile);
  27. try {
  28. QueryMetricListResponse response = client.getAcsResponse(request);
  29. System.out.println(response.getCode());
  30. System.out.println(response.getMessage());
  31. System.out.println(response.getRequestId());
  32. System.out.println(response.getDatapoints());
  33. } catch (ServerException e) {
  34. e.printStackTrace();
  35. } catch (ClientException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  • 返回示例
  1. Code:200
  2. Message:null
  3. RequestId:727B48BD-55E3-4E5D-BEF4-1D092055F7A4
  4. Datapoints:[
  5. {
  6. "Maximum":100,
  7. "Minimum":25,
  8. "instanceId":"i-bp18abl200xk9599ck7c",
  9. "Average":94.62,
  10. "userId":"1270676679546704",
  11. "timestamp":1489472460000
  12. },
  13. {
  14. "Maximum":100,
  15. "Minimum":25,
  16. "instanceId":"i-bp18abl200xk9599ck7c",
  17. "Average":95.11,
  18. "userId":"1270676679546704",
  19. "timestamp":1489472520000
  20. },
  21. {
  22. "Maximum":100,
  23. "Minimum":25,
  24. "instanceId":"i-bp18abl200xk9599ck7c",
  25. "Average":95.16,
  26. "userId":"1270676679546704",
  27. "timestamp":1489472580000
  28. },
  29. {
  30. "Maximum":100,
  31. "Minimum":25,
  32. "instanceId":"i-bp18abl200xk9599ck7c",
  33. "Average":94.69,
  34. "userId":"1270676679546704",
  35. "timestamp":1489472640000
  36. },
  37. {
  38. "Maximum":100,
  39. "Minimum":20,
  40. "instanceId":"i-bp18abl200xk9599ck7c",
  41. "Average":95.82,
  42. "userId":"1270676679546704",
  43. "timestamp":1489472700000
  44. },
  45. {
  46. "Maximum":100,
  47. "Minimum":25,
  48. "instanceId":"i-bp18abl200xk9599ck7c",
  49. "Average":94.77,
  50. "userId":"1270676679546704",
  51. "timestamp":1489472760000
  52. },
  53. {
  54. "Maximum":100,
  55. "Minimum":33.33,
  56. "instanceId":"i-bp18abl200xk9599ck7c",
  57. "Average":95.18,
  58. "userId":"1270676679546704",
  59. "timestamp":1489472820000
  60. },
  61. {
  62. "Maximum":100,
  63. "Minimum":25,
  64. "instanceId":"i-bp18abl200xk9599ck7c",
  65. "Average":95.11,
  66. "userId":"1270676679546704",
  67. "timestamp":1489472880000
  68. },
  69. {
  70. "Maximum":100,
  71. "Minimum":20,
  72. "instanceId":"i-bp18abl200xk9599ck7c",
  73. "Average":94.5,
  74. "userId":"1270676679546704",
  75. "timestamp":1489472940000
  76. },
  77. {
  78. "Maximum":100,
  79. "Minimum":20,
  80. "instanceId":"i-bp18abl200xk9599ck7c",
  81. "Average":94.99,
  82. "userId":"1270676679546704",
  83. "timestamp":1489473000000
  84. }
  85. ]
  86. }

创建报警规则

  • 请求示例
  1. package com.aliyuncs.cms.test;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.aliyuncs.DefaultAcsClient;
  5. import com.aliyuncs.IAcsClient;
  6. import com.aliyuncs.cms.model.v20170301.CreateAlarmRequest;
  7. import com.aliyuncs.cms.model.v20170301.CreateAlarmResponse;
  8. import com.aliyuncs.exceptions.ClientException;
  9. import com.aliyuncs.exceptions.ServerException;
  10. import com.aliyuncs.http.FormatType;
  11. import com.aliyuncs.profile.DefaultProfile;
  12. import com.aliyuncs.profile.IClientProfile;
  13. public class CreateAlarmDemo {
  14. public static void main(String[] args) {
  15. CreateAlarmRequest request = new CreateAlarmRequest();
  16. request.setName("create_alarm_test");
  17. request.setNamespace("acs_ecs_dashboard");
  18. request.setMetricName("vm.MemoryUtilization");
  19. JSONArray dimensions = new JSONArray();
  20. JSONObject dim = new JSONObject();
  21. dim.put("userId", "<your_userId>");
  22. dim.put("instanceId", "<your_instanceId>");
  23. dimensions.add(dim);
  24. request.setDimensions(dimensions.toJSONString());
  25. request.setPeriod(900);
  26. request.setStatistics("Average");
  27. request.setComparisonOperator(">=");
  28. request.setThreshold("35");
  29. request.setEvaluationCount(2);
  30. JSONArray contactGroups = new JSONArray();
  31. contactGroups.add("<your_group1>");
  32. contactGroups.add("<your_group2>");
  33. request.setContactGroups(contactGroups.toJSONString());
  34. request.setStartTime(6);
  35. request.setEndTime(20);
  36. request.setNotifyType(1);
  37. request.setAcceptFormat(FormatType.JSON);
  38. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
  39. "<your_access_key_id>", "<your_access_key_secret>");
  40. IAcsClient client = new DefaultAcsClient(profile);
  41. try {
  42. CreateAlarmResponse response = client.getAcsResponse(request);
  43. System.out.println(response.getCode());
  44. System.out.println(response.getMessage());
  45. System.out.println(response.getRequestId());
  46. System.out.println(response.getData());
  47. } catch (ServerException e) {
  48. e.printStackTrace();
  49. } catch (ClientException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  • 返回示例
  1. Code:200
  2. Message:null
  3. RequestId:B8E6E26A-9435-4D52-8796-68793935CA74
  4. Data:81b412bf-fbbb-4a40-8d74-b68ec849a091
本文导读目录