PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/gmonitor/gmonitor-app/src/main/java/com/kingdeehit/bigdata/appmetric/ganglia/GangliaWriter.java

https://gitlab.com/zhengdingke/gmonitor
Java | 309 lines | 250 code | 55 blank | 4 comment | 6 complexity | d25a305a49b202d6e18fd3af44022088 MD5 | raw file
  1. package com.kingdeehit.bigdata.appmetric.ganglia;
  2. import info.ganglia.gmetric4j.gmetric.GMetric;
  3. import info.ganglia.gmetric4j.gmetric.GMetric.UDPAddressingMode;
  4. import info.ganglia.gmetric4j.gmetric.GMetricSlope;
  5. import info.ganglia.gmetric4j.gmetric.GMetricType;
  6. import java.io.IOException;
  7. import java.net.UnknownHostException;
  8. import java.util.Map;
  9. import java.util.regex.Pattern;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import com.alibaba.fastjson.JSON;
  13. import com.alibaba.fastjson.annotation.JSONCreator;
  14. import com.google.common.base.MoreObjects;
  15. public class GangliaWriter {
  16. private static final Logger log = LoggerFactory.getLogger(GangliaWriter.class);
  17. private static final Pattern PATTERN_HOST_IP = Pattern.compile("(.+):([^:]+)$");
  18. // gmetric鍛戒护鐨勯厤锟??
  19. public static final String ADDRESSING_MODE = "addressingMode";
  20. public static final String TTL = "ttl";
  21. public static final String V31 = "v3.1";
  22. public static final String UNITS = "units";
  23. public static final String SLOPE = "slope";
  24. public static final String TMAX = "tmax";
  25. public static final String DMAX = "dmax";
  26. public static final String GROUP_NAME = "groupName";
  27. public static final String SPOOF_NAME = "spoofedHostName";
  28. // 榛樿閰嶇疆
  29. public static final int DEFAULT_PORT = 8649;
  30. public static final UDPAddressingMode DEFAULT_ADDRESSING_MODE = UDPAddressingMode.UNICAST;
  31. public static final int DEFAULT_TTL = 1;
  32. public static final boolean DEFAULT_V31 = false;
  33. public static final String DEFAULT_UNITS = "default";
  34. public static final GMetricSlope DEFAULT_SLOPE = GMetricSlope.BOTH;
  35. public static final int DEFAULT_DMAX = 604800;
  36. public static final int DEFAULT_TMAX = 60;
  37. public static final String DEFAULT_GROUP_NAME = "JMX";
  38. private String host;
  39. private Integer port;
  40. private UDPAddressingMode addressingMode;
  41. private Integer ttl;
  42. private Boolean v31;
  43. private String units;
  44. private GMetricSlope slope;
  45. private Integer tmax;
  46. private Integer dmax;
  47. private String groupName;
  48. private String spoofedHostName;
  49. @JSONCreator
  50. public GangliaWriter(String host, Integer port, String addressingMode, Integer ttl, Boolean v31, String units, String slope, Integer tmax, Integer dmax,
  51. String groupName, String spoofedHostName) {
  52. this.host = MoreObjects.firstNonNull(host, null);
  53. this.port = MoreObjects.firstNonNull(port, DEFAULT_PORT);
  54. this.addressingMode = computeAddressingMode(MoreObjects.firstNonNull(addressingMode, ""), this.host);
  55. this.ttl = MoreObjects.firstNonNull(ttl, DEFAULT_TTL);
  56. this.v31 = MoreObjects.firstNonNull(v31, DEFAULT_V31);
  57. this.units = MoreObjects.firstNonNull(units, DEFAULT_UNITS);
  58. this.slope = GMetricSlope.valueOf(MoreObjects.firstNonNull(slope, DEFAULT_SLOPE.name()));
  59. this.tmax = MoreObjects.firstNonNull(tmax, DEFAULT_TMAX);
  60. this.dmax = MoreObjects.firstNonNull(dmax, DEFAULT_DMAX);
  61. this.groupName = MoreObjects.firstNonNull(groupName, DEFAULT_GROUP_NAME);
  62. this.spoofedHostName = spoofedHostName;
  63. }
  64. private UDPAddressingMode computeAddressingMode(String mode, String host) {
  65. // Parse and validate addressing mode setting
  66. try {
  67. return UDPAddressingMode.valueOf(mode);
  68. } catch (IllegalArgumentException iae) {
  69. log.debug("Non existing UDP addressing mode {}.", host, iae);
  70. try {
  71. return UDPAddressingMode.getModeForAddress(host);
  72. } catch (UnknownHostException uhe) {
  73. log.debug("Unknown host {}, falling back to default addressing mode.", host, uhe);
  74. return DEFAULT_ADDRESSING_MODE;
  75. } catch (IOException ioe) {
  76. log.debug("Could not resolve host {}, falling back to default addressing mode.", host, ioe);
  77. return DEFAULT_ADDRESSING_MODE;
  78. }
  79. }
  80. }
  81. private static GMetricType getType(final Object obj) {
  82. if (obj instanceof Long || obj instanceof Integer || obj instanceof Byte || obj instanceof Short) {
  83. return GMetricType.INT32;
  84. }
  85. if (obj instanceof Float) {
  86. return GMetricType.FLOAT;
  87. }
  88. if (obj instanceof Double) {
  89. return GMetricType.DOUBLE;
  90. }
  91. try {
  92. Double.parseDouble(obj.toString());
  93. return GMetricType.DOUBLE;
  94. } catch (NumberFormatException e) {
  95. }
  96. try {
  97. Integer.parseInt(obj.toString());
  98. return GMetricType.UINT32;
  99. } catch (NumberFormatException e) {
  100. }
  101. return GMetricType.STRING;
  102. }
  103. public void doWrite(Map<String, String> map) {
  104. map.entrySet().stream().forEach(entry -> {
  105. String metricName = entry.getKey();
  106. String metricValue = entry.getValue();
  107. GMetricType dataType = getType(metricValue);
  108. log.info("Sending Ganglia metric {}={} [type={}]", entry.getKey(), entry.getValue(), dataType);
  109. GMetric metric;
  110. try {
  111. // metric = new GMetric(host, port, addressingMode, ttl);
  112. metric = new GMetric(host, port, addressingMode, ttl, true, null, spoofedHostName);
  113. metric.announce(metricName, metricValue.toString(), dataType, units, slope, tmax, dmax, groupName);
  114. } catch (Exception e) {
  115. log.error("metric announce error,the host {}:{}={}", host, metricName, metricValue, e);
  116. }
  117. });
  118. }
  119. public String getHost() {
  120. return host;
  121. }
  122. public void setHost(String host) {
  123. this.host = host;
  124. }
  125. public Integer getPort() {
  126. return port;
  127. }
  128. public void setPort(Integer port) {
  129. this.port = port;
  130. }
  131. public UDPAddressingMode getAddressingMode() {
  132. return addressingMode;
  133. }
  134. public void setAddressingMode(UDPAddressingMode addressingMode) {
  135. this.addressingMode = addressingMode;
  136. }
  137. public Integer getTtl() {
  138. return ttl;
  139. }
  140. public void setTtl(Integer ttl) {
  141. this.ttl = ttl;
  142. }
  143. public Boolean getV31() {
  144. return v31;
  145. }
  146. public void setV31(Boolean v31) {
  147. this.v31 = v31;
  148. }
  149. public String getUnits() {
  150. return units;
  151. }
  152. public void setUnits(String units) {
  153. this.units = units;
  154. }
  155. public GMetricSlope getSlope() {
  156. return slope;
  157. }
  158. public void setSlope(GMetricSlope slope) {
  159. this.slope = slope;
  160. }
  161. public Integer getTmax() {
  162. return tmax;
  163. }
  164. public void setTmax(Integer tmax) {
  165. this.tmax = tmax;
  166. }
  167. public Integer getDmax() {
  168. return dmax;
  169. }
  170. public void setDmax(Integer dmax) {
  171. this.dmax = dmax;
  172. }
  173. public String getGroupName() {
  174. return groupName;
  175. }
  176. public void setGroupName(String groupName) {
  177. this.groupName = groupName;
  178. }
  179. public String getSpoofedHostName() {
  180. return spoofedHostName;
  181. }
  182. public void setSpoofedHostName(String spoofedHostName) {
  183. this.spoofedHostName = spoofedHostName;
  184. }
  185. @Override
  186. public String toString() {
  187. return JSON.toJSONString(this, true);
  188. }
  189. public static Builder builder() {
  190. return new Builder();
  191. }
  192. public static final class Builder {
  193. private String host;
  194. private Integer port;
  195. private String addressingMode;
  196. private Integer ttl;
  197. private Boolean v31;
  198. private String units;
  199. private String slope;
  200. private Integer tmax;
  201. private Integer dmax;
  202. private String groupName;
  203. private String spoofedHostName;
  204. private Builder() {
  205. }
  206. public Builder setHost(String host) {
  207. this.host = host;
  208. return this;
  209. }
  210. public Builder setPort(int port) {
  211. this.port = port;
  212. return this;
  213. }
  214. public Builder setAddressingMode(String addressingMode) {
  215. this.addressingMode = addressingMode;
  216. return this;
  217. }
  218. public Builder setTtl(Integer ttl) {
  219. this.ttl = ttl;
  220. return this;
  221. }
  222. public Builder setV31(Boolean v31) {
  223. this.v31 = v31;
  224. return this;
  225. }
  226. public Builder setUnits(String units) {
  227. this.units = units;
  228. return this;
  229. }
  230. public Builder setSlope(String slope) {
  231. this.slope = slope;
  232. return this;
  233. }
  234. public Builder setTmax(Integer tmax) {
  235. this.tmax = tmax;
  236. return this;
  237. }
  238. public Builder setDmax(Integer dmax) {
  239. this.dmax = dmax;
  240. return this;
  241. }
  242. public Builder setGroupName(String groupName) {
  243. this.groupName = groupName;
  244. return this;
  245. }
  246. public Builder setSpoofedHostName(String spoofedHostName) {
  247. this.spoofedHostName = spoofedHostName;
  248. return this;
  249. }
  250. public GangliaWriter build() {
  251. return new GangliaWriter(host, port, addressingMode, ttl, v31, units, slope, tmax, dmax, groupName, spoofedHostName);
  252. }
  253. }
  254. }