ServerMonitorService.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.yihu.hos.broker.services;
  2. import com.mongodb.*;
  3. import com.yihu.hos.broker.common.constants.MonitorConstant;
  4. import com.yihu.hos.broker.util.SigarUtil;
  5. import com.yihu.hos.core.datatype.DateUtil;
  6. import net.sf.json.JSONArray;
  7. import net.sf.json.JSONObject;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.cloud.context.config.annotation.RefreshScope;
  11. import org.springframework.stereotype.Service;
  12. import java.util.List;
  13. //@RefreshScope
  14. //@Service("serverMonitorService")
  15. public class ServerMonitorService {
  16. @Value("${hos.tenant.name}")
  17. private String tenant;
  18. private static String host = SigarUtil.getHost();
  19. @Autowired
  20. private Mongo mongo;
  21. /**
  22. * 服务器健康指标采集
  23. *
  24. * @return
  25. */
  26. public void collectEnvHealth() {
  27. try {
  28. BasicDBObject result = new BasicDBObject();
  29. DBCollection terminal = mongo.getDB(MonitorConstant.DATABASE).getCollection(MonitorConstant.SERVER);
  30. result.put("tenant", tenant);
  31. result.put("create_date", DateUtil.getCurrentString(DateUtil.DEFAULT_YMDHMSDATE_FORMAT));
  32. result.put("create_time", DateUtil.getSysDateTime());
  33. result.put("host", host);
  34. //cpu
  35. net.sf.json.JSONObject cpu = net.sf.json.JSONObject.fromObject(SigarUtil.cpu());
  36. result.put("data", cpu);
  37. result.put("type", MonitorConstant.CPU);
  38. terminal.insert(result);
  39. //内存
  40. net.sf.json.JSONObject memory = net.sf.json.JSONObject.fromObject(SigarUtil.memory());
  41. result.put("data", memory);
  42. result.put("type", MonitorConstant.MEMORY);
  43. result.remove("_id");
  44. terminal.insert(result);
  45. //硬盘
  46. List<net.sf.json.JSONObject> files = JSONArray.fromObject(SigarUtil.file());
  47. result.put("data", files);
  48. result.put("type", MonitorConstant.FILES);
  49. result.remove("_id");
  50. terminal.insert(result);
  51. //网络
  52. JSONObject net = JSONObject.fromObject(SigarUtil.net());
  53. result.put("data", net);
  54. result.put("type", MonitorConstant.NET);
  55. result.remove("_id");
  56. terminal.insert(result);
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. public void checkHost() {
  62. DBCollection terminal = mongo.getDB(MonitorConstant.DATABASE).getCollection(MonitorConstant.HOST);
  63. BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
  64. new BasicDBObject[]{
  65. new BasicDBObject().append("host", host),
  66. new BasicDBObject().append("tenant", tenant)});
  67. DBCursor cursor = terminal.find(queryObject);
  68. if (cursor.size() < 1) {
  69. try {
  70. BasicDBObject result = new BasicDBObject();
  71. String host = SigarUtil.getHost();
  72. String hostName = SigarUtil.getHostName();
  73. result.put("tenant", tenant);
  74. result.put("name", hostName);
  75. result.put("host", host);
  76. terminal.insert(result);
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. }