PageRenderTime 41ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/gmonitor/gmonitor-core/src/main/java/com/zdingke/gmonitor/utils/MetricJsonCreateUtil.java

https://gitlab.com/zhengdingke/gmonitor
Java | 116 lines | 104 code | 12 blank | 0 comment | 5 complexity | 47322fda843703f9b6e2e0b436f08c7c MD5 | raw file
  1. package com.zdingke.gmonitor.utils;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.stream.Collectors;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import com.alibaba.fastjson.JSON;
  12. import com.alibaba.fastjson.JSONObject;
  13. import com.google.common.collect.Lists;
  14. import com.zdingke.gmonitor.FlumeMetricsJson;
  15. import com.zdingke.gmonitor.KafkaMetricsJson;
  16. import com.zdingke.gmonitor.MetricsJson;
  17. import com.zdingke.gmonitor.StormMetricsJson;
  18. import com.zdingke.gmonitor.ZookeeperMetricsJson;
  19. import com.zdingke.gmonitor.common.StringUtil;
  20. import com.zdingke.gmonitor.model.ComponentInfo;
  21. public class MetricJsonCreateUtil {
  22. private static final Log LOG = LogFactory.getLog(MetricJsonCreateUtil.class);
  23. public static void main(String[] args) throws IOException {
  24. if (args.length != 2) {
  25. LOG.info("parm include <serviceType,jsonDir>");
  26. } else {
  27. String serviceType = args[0];
  28. String dir = args[1];
  29. if (serviceType.equals("all")) {
  30. getAllModuleJsonObj(dir).stream().forEach(m -> {
  31. System.out.println(JSON.toJSONString(m, true));
  32. });
  33. } else {
  34. getModuleJsonObjByServiceType(serviceType, dir).stream().forEach(m -> {
  35. System.out.println(JSON.toJSONString(m, true));
  36. });
  37. ;
  38. }
  39. }
  40. }
  41. public static void storm(String dir) throws IOException {
  42. MetricsJson create = new StormMetricsJson(dir);
  43. create.createJsonFile();
  44. }
  45. public static void kafka(String dir) throws IOException {
  46. MetricsJson create = new KafkaMetricsJson(dir);
  47. create.createJsonFile();
  48. }
  49. public static void flume(String dir) throws IOException {
  50. MetricsJson create = new FlumeMetricsJson(dir);
  51. create.createJsonFile();
  52. }
  53. public static void zookeeper(String dir) throws IOException {
  54. MetricsJson create = new ZookeeperMetricsJson(dir);
  55. create.createJsonFile();
  56. }
  57. public static List<JSONObject> getAllModuleJsonObj(String dir) {
  58. Map<Boolean,List<File>> map = StringUtil.getJsonFiles(dir).stream().collect(Collectors.partitioningBy(j -> j.getName().contains("info")));
  59. String dirpath = dir.endsWith("/") ? dir.substring(dir.length() - 1) : dir;
  60. List<JSONObject> jsonObj = Lists.newArrayList();
  61. map.get(true).stream().forEach(info->{
  62. String serviceType = info.getName().replace("-info.json", "");
  63. MetricsJson metricJson = getMetricsJsonByServiceType(serviceType, dirpath);
  64. ComponentInfo cinfo = null;
  65. try {
  66. cinfo = metricJson.combineInfoAndMetric(dirpath, serviceType);
  67. String gmondConn = cinfo.getGmondConn();
  68. cinfo.getModule().stream().forEach(m -> {
  69. jsonObj.add(metricJson.createJsonObject(gmondConn, serviceType, m));
  70. });
  71. } catch (Exception e) {
  72. LOG.error(e.getMessage(), e);
  73. }
  74. });
  75. return jsonObj;
  76. }
  77. public static List<JSONObject> getModuleJsonObjByServiceType(String serviceType, String dir) {
  78. String dirpath = dir.endsWith("/") ? dir.substring(dir.length() - 1) : dir;
  79. List<JSONObject> jsonObj = Lists.newArrayList();
  80. MetricsJson metricJson = getMetricsJsonByServiceType(serviceType, dirpath);
  81. ComponentInfo cinfo = null;
  82. try {
  83. cinfo = metricJson.combineInfoAndMetric(dirpath, serviceType);
  84. String gmondConn = cinfo.getGmondConn();
  85. cinfo.getModule().stream().forEach(m -> {
  86. jsonObj.add(metricJson.createJsonObject(gmondConn, serviceType, m));
  87. });
  88. } catch (Exception e) {
  89. LOG.error(e.getMessage(), e);
  90. }
  91. return jsonObj;
  92. }
  93. public static MetricsJson getMetricsJsonByServiceType(String serviceType, String dirpath) {
  94. Class claz = null;
  95. try {
  96. claz = Class.forName("com.kingdeehit.bigdata.metric." + StringUtil.tranFirstCharUpper(serviceType) + "MetricsJson");
  97. Constructor<?> cons[] = claz.getConstructors();
  98. return (MetricsJson) cons[0].newInstance(dirpath);
  99. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  100. LOG.error(serviceType + "MetricsJson class is not exist!!!!", e);
  101. }
  102. return null;
  103. }
  104. }