PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/gmonitor/gmonitor-core/src/main/java/com/zdingke/gmonitor/MetricsJson.java

https://gitlab.com/zhengdingke/gmonitor
Java | 162 lines | 129 code | 29 blank | 4 comment | 5 complexity | 2b3a23c97d48faeb55b3bb42fd58e4e2 MD5 | raw file
  1. package com.zdingke.gmonitor;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.List;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.apache.commons.logging.Log;
  7. import org.apache.commons.logging.LogFactory;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONArray;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.google.common.collect.Lists;
  12. import com.zdingke.gmonitor.common.JMXPollUtil;
  13. import com.zdingke.gmonitor.common.StringFileTransUtil;
  14. import com.zdingke.gmonitor.model.ComponentInfo;
  15. import com.zdingke.gmonitor.model.ComponentMetricInfo;
  16. import com.zdingke.gmonitor.model.JVMInfo;
  17. import com.zdingke.gmonitor.model.MetricInfo;
  18. import com.zdingke.gmonitor.model.ModuleInfo;
  19. public abstract class MetricsJson {
  20. private static final Log LOG = LogFactory.getLog(MetricsJson.class);
  21. private String dir;
  22. public MetricsJson(String dir) {
  23. this.dir = dir;
  24. }
  25. public String getDir() {
  26. return dir;
  27. }
  28. public void setDir(String dir) {
  29. this.dir = dir;
  30. }
  31. public ComponentInfo includeJVMMetric(ComponentInfo ci) {
  32. ci.getModule().stream().filter(f -> f.isJvmInclude()).forEach(module -> {
  33. JVMInfo jvmInfo = JSON.parseObject(StringFileTransUtil.getJvmMetricInfo(getServiceType(), dir), JVMInfo.class);
  34. jvmInfo.getMetrics().stream().forEach(j -> {
  35. j.setResultAlias(j.getResultAlias().replace("${name}", module.getName()));
  36. });
  37. if (module.getMetrics() == null) {
  38. module.setMetrics(jvmInfo.getMetrics());
  39. } else {
  40. module.getMetrics().addAll(jvmInfo.getMetrics());
  41. }
  42. });
  43. return ci;
  44. }
  45. public ComponentInfo includeComponentMetric(ComponentInfo ci, List<MetricInfo> mList) throws IOException {
  46. ci.getModule().stream().forEach(module -> {
  47. List<MetricInfo> metricList = generateAllMetrics(mList, module.getHost(), module.getJmxport());
  48. metricList.stream().forEach(m -> {
  49. m.setResultAlias(m.getResultAlias().replace("${name}", module.getName()));
  50. });
  51. module.setMetrics(metricList);
  52. });
  53. return ci;
  54. }
  55. private List<MetricInfo> generateAllMetrics(List<MetricInfo> mList, String host, String port) {
  56. List<MetricInfo> ml = Lists.newArrayList();
  57. mList.stream().forEach(m -> {
  58. List<String> objList = Lists.newArrayList();
  59. try {
  60. objList = JMXPollUtil.getAllObjByType(host, port, getJmxType());
  61. } catch (Exception e) {
  62. LOG.error(e.getMessage(), e);
  63. }
  64. if (!objList.isEmpty()) {
  65. ml.addAll(objMatch(objList, m, host));
  66. }
  67. });
  68. return ml;
  69. }
  70. public JSONObject createJsonObject(String gmondConn, String serviceType, ModuleInfo minfo) {
  71. String[] gmonds = gmondConn.split(":");
  72. String gmondhost = gmonds[0];
  73. String gmondport = gmonds[1];
  74. JSONArray queries = new JSONArray();
  75. minfo.getMetrics().stream().forEach(m -> {
  76. JSONObject settings = new JSONObject();
  77. settings.put("groupName", serviceType);
  78. settings.put("host", gmondhost);
  79. settings.put("port", gmondport);
  80. JSONObject write = new JSONObject();
  81. write.put("@class", "com.googlecode.jmxtrans.model.output.GangliaWriter");
  82. write.put("settings", settings);
  83. JSONArray outputWriters = new JSONArray();
  84. outputWriters.add(write);
  85. JSONObject querie = new JSONObject();
  86. querie.put("outputWriters", outputWriters);
  87. querie.put("obj", m.getObj());
  88. querie.put("resultAlias", m.getResultAlias());
  89. JSONArray attr = new JSONArray();
  90. attr.addAll(m.getAttr());
  91. querie.put("attr", attr);
  92. queries.add(querie);
  93. });
  94. JSONObject server = new JSONObject();
  95. server.put("port", minfo.getJmxport());
  96. server.put("host", minfo.getHost());
  97. server.put("numQueryThreads", "1");
  98. server.put("queries", queries);
  99. JSONArray servers = new JSONArray();
  100. servers.add(server);
  101. JSONObject obj = new JSONObject();
  102. obj.put("servers", servers);
  103. return obj;
  104. }
  105. public void createJsonFile() throws IOException {
  106. String dirpath = dir.endsWith("/") ? dir.substring(0, dir.length() - 1) : dir;
  107. String serviceType = getServiceType();
  108. ComponentInfo cinfo = combineInfoAndMetric(dirpath, serviceType);
  109. cinfo.getModule().stream().forEach(m -> {
  110. JSONObject moduleObj = createJsonObject(cinfo.getGmondConn(), serviceType, m);
  111. String jsonFileName = m.getName() + ".json";
  112. boolean over = StringFileTransUtil.string2File(JSON.toJSONString(moduleObj, true), new File(dir + "/test/" + jsonFileName));
  113. LOG.info("createFile:" + dir + ":" + jsonFileName + ":" + (over ? "success" : "false"));
  114. });
  115. };
  116. public ComponentInfo combineInfoAndMetric(String dir, String serviceType) throws IOException {
  117. String comstr = StringFileTransUtil.getComponentInfo(serviceType, dir);
  118. String mestr = StringFileTransUtil.getMetricInfo(serviceType, dir);
  119. ComponentInfo cinfo = JSON.parseObject(comstr, ComponentInfo.class);
  120. if (StringUtils.isNotBlank(mestr)) {
  121. List<MetricInfo> mList = JSON.parseObject(mestr, ComponentMetricInfo.class).getMetrics();
  122. includeComponentMetric(cinfo, mList);
  123. }
  124. return includeJVMMetric(cinfo);
  125. }
  126. /*
  127. * objList:表示jmx过滤掉不符合前缀(比如org.apache.zookeepr)之后的obj集合
  128. * matchObj:json中需要进行匹配的obj
  129. */
  130. public abstract List<MetricInfo> objMatch(List<String> objList, MetricInfo m, String host);
  131. public abstract String getServiceType();
  132. public abstract String getJmxType();
  133. }