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

/gmonitor/gmonitor-app/src/main/java/com/kingdeehit/bigdata/appmetric/flume/GangliaSink.java

https://gitlab.com/zhengdingke/gmonitor
Java | 60 lines | 50 code | 9 blank | 1 comment | 2 complexity | 8ea605891fb95806a91b7f744ccc5b1c MD5 | raw file
  1. package com.kingdeehit.bigdata.appmetric.flume;
  2. import org.apache.commons.logging.Log;
  3. import org.apache.commons.logging.LogFactory;
  4. import org.apache.flume.Channel;
  5. import org.apache.flume.Context;
  6. import org.apache.flume.Event;
  7. import org.apache.flume.EventDeliveryException;
  8. import org.apache.flume.Transaction;
  9. import org.apache.flume.conf.Configurable;
  10. import org.apache.flume.sink.AbstractSink;
  11. import com.alibaba.fastjson.JSON;
  12. import com.kingdeehit.bigdata.appmetric.ganglia.GangliaWriter;
  13. import com.kingdeehit.bigdata.appmetric.model.MetricInfo;
  14. public class GangliaSink extends AbstractSink implements Configurable {
  15. private static final Log log = LogFactory.getLog(GangliaSink.class);
  16. @Override
  17. public Status process() throws EventDeliveryException {
  18. Channel ch = getChannel();
  19. Transaction txn = ch.getTransaction();
  20. try {
  21. txn.begin();
  22. Event e = getChannel().take();
  23. if (e == null) {
  24. txn.rollback();
  25. return Status.BACKOFF;
  26. }
  27. String data = new String(e.getBody());
  28. sendDataToGanglia(data);
  29. txn.commit();
  30. return Status.READY;
  31. } catch (Exception ex) {
  32. txn.rollback();
  33. log.error(ex.getMessage(), ex);
  34. return Status.BACKOFF;
  35. } finally {
  36. txn.close();
  37. }
  38. }
  39. private void sendDataToGanglia(String data) {
  40. MetricInfo info = JSON.parseObject(data,MetricInfo.class);
  41. String []strs= info.getGmetadInfo().split(":");
  42. GangliaWriter writer = GangliaWriter.builder().setHost(strs[0]).setPort(Integer.parseInt(strs[1])).setAddressingMode(null).setDmax(null).setTmax(null)
  43. .setUnits(null).setSlope(null).setTtl(null).setV31(null).setGroupName(info.getGroupName()).setSpoofedHostName(info.getIpHost())
  44. .build();
  45. // log.info("DATA:" + data);
  46. writer.doWrite(info.getMap());
  47. }
  48. @Override
  49. public void configure(Context context) {
  50. }
  51. }