PageRenderTime 85ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/htest/flumetest/src/main/java/com/zdingke/flume/ganglia/metric/GangliaWriter.java

https://gitlab.com/zhengdingke/htest
Java | 302 lines | 244 code | 55 blank | 3 comment | 6 complexity | 06b8f3815f669e5992693cb356a91488 MD5 | raw file
  1. package com.zdingke.flume.ganglia.metric;
  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 = 6000;
  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 = null;
  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) {
  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. }
  63. private UDPAddressingMode computeAddressingMode(String mode, String host) {
  64. // Parse and validate addressing mode setting
  65. try {
  66. return UDPAddressingMode.valueOf(mode);
  67. } catch (IllegalArgumentException iae) {
  68. log.debug("Non existing UDP addressing mode {}.", host, iae);
  69. try {
  70. return UDPAddressingMode.getModeForAddress(host);
  71. } catch (UnknownHostException uhe) {
  72. log.debug("Unknown host {}, falling back to default addressing mode.", host, uhe);
  73. return DEFAULT_ADDRESSING_MODE;
  74. } catch (IOException ioe) {
  75. log.debug("Could not resolve host {}, falling back to default addressing mode.", host, ioe);
  76. return DEFAULT_ADDRESSING_MODE;
  77. }
  78. }
  79. }
  80. private static GMetricType getType(final Object obj) {
  81. if (obj instanceof Long || obj instanceof Integer || obj instanceof Byte || obj instanceof Short) {
  82. return GMetricType.INT32;
  83. }
  84. if (obj instanceof Float) {
  85. return GMetricType.FLOAT;
  86. }
  87. if (obj instanceof Double) {
  88. return GMetricType.DOUBLE;
  89. }
  90. try {
  91. Double.parseDouble(obj.toString());
  92. return GMetricType.DOUBLE;
  93. } catch (NumberFormatException e) {
  94. }
  95. try {
  96. Integer.parseInt(obj.toString());
  97. return GMetricType.UINT32;
  98. } catch (NumberFormatException e) {
  99. }
  100. return GMetricType.STRING;
  101. }
  102. public void doWrite(Map<String, String> map) {
  103. map.entrySet().stream().forEach(entry -> {
  104. String metricName = entry.getKey();
  105. String metricValue = entry.getValue();
  106. GMetricType dataType = getType(metricValue);
  107. log.info("Sending Ganglia metric {}={} [type={}]", entry.getKey(), entry.getValue(), dataType);
  108. GMetric metric;
  109. try {
  110. metric = new GMetric(host, port, addressingMode, ttl);
  111. metric.announce(metricName, metricValue.toString(), dataType, units, slope, tmax, dmax, groupName);
  112. } catch (Exception e) {
  113. log.error("metric announce error,the host {}:{}={}", host, metricName, metricValue, e);
  114. }
  115. });
  116. }
  117. public String getHost() {
  118. return host;
  119. }
  120. public void setHost(String host) {
  121. this.host = host;
  122. }
  123. public Integer getPort() {
  124. return port;
  125. }
  126. public void setPort(Integer port) {
  127. this.port = port;
  128. }
  129. public UDPAddressingMode getAddressingMode() {
  130. return addressingMode;
  131. }
  132. public void setAddressingMode(UDPAddressingMode addressingMode) {
  133. this.addressingMode = addressingMode;
  134. }
  135. public Integer getTtl() {
  136. return ttl;
  137. }
  138. public void setTtl(Integer ttl) {
  139. this.ttl = ttl;
  140. }
  141. public Boolean getV31() {
  142. return v31;
  143. }
  144. public void setV31(Boolean v31) {
  145. this.v31 = v31;
  146. }
  147. public String getUnits() {
  148. return units;
  149. }
  150. public void setUnits(String units) {
  151. this.units = units;
  152. }
  153. public GMetricSlope getSlope() {
  154. return slope;
  155. }
  156. public void setSlope(GMetricSlope slope) {
  157. this.slope = slope;
  158. }
  159. public Integer getTmax() {
  160. return tmax;
  161. }
  162. public void setTmax(Integer tmax) {
  163. this.tmax = tmax;
  164. }
  165. public Integer getDmax() {
  166. return dmax;
  167. }
  168. public void setDmax(Integer dmax) {
  169. this.dmax = dmax;
  170. }
  171. public String getGroupName() {
  172. return groupName;
  173. }
  174. public void setGroupName(String groupName) {
  175. this.groupName = groupName;
  176. }
  177. public String getSpoofedHostName() {
  178. return spoofedHostName;
  179. }
  180. public void setSpoofedHostName(String spoofedHostName) {
  181. this.spoofedHostName = spoofedHostName;
  182. }
  183. @Override
  184. public String toString() {
  185. return JSON.toJSONString(this, true);
  186. }
  187. public static Builder builder() {
  188. return new Builder();
  189. }
  190. public static final class Builder {
  191. private String host;
  192. private Integer port;
  193. private String addressingMode;
  194. private Integer ttl;
  195. private Boolean v31;
  196. private String units;
  197. private String slope;
  198. private Integer tmax;
  199. private Integer dmax;
  200. private String groupName;
  201. private Builder() {
  202. }
  203. public Builder setHost(String host) {
  204. this.host = host;
  205. return this;
  206. }
  207. public Builder setPort(int port) {
  208. this.port = port;
  209. return this;
  210. }
  211. public Builder setAddressingMode(String addressingMode) {
  212. this.addressingMode = addressingMode;
  213. return this;
  214. }
  215. public Builder setTtl(Integer ttl) {
  216. this.ttl = ttl;
  217. return this;
  218. }
  219. public Builder setV31(Boolean v31) {
  220. this.v31 = v31;
  221. return this;
  222. }
  223. public Builder setUnits(String units) {
  224. this.units = units;
  225. return this;
  226. }
  227. public Builder setSlope(String slope) {
  228. this.slope = slope;
  229. return this;
  230. }
  231. public Builder setTmax(Integer tmax) {
  232. this.tmax = tmax;
  233. return this;
  234. }
  235. public Builder setDmax(Integer dmax) {
  236. this.dmax = dmax;
  237. return this;
  238. }
  239. public Builder setGroupName(String groupName) {
  240. this.groupName = groupName;
  241. return this;
  242. }
  243. public GangliaWriter build() {
  244. return new GangliaWriter(host, port, addressingMode, ttl, v31, units, slope, tmax, dmax, groupName);
  245. }
  246. }
  247. }