123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package com.yihu.hos.broker.services;
- import com.mongodb.*;
- import com.yihu.hos.broker.common.constants.MonitorConstant;
- import com.yihu.hos.broker.util.SigarUtil;
- import com.yihu.hos.core.datatype.DateUtil;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.context.config.annotation.RefreshScope;
- import org.springframework.stereotype.Service;
- import java.util.List;
- //@RefreshScope
- //@Service("serverMonitorService")
- public class ServerMonitorService {
- @Value("${hos.tenant.name}")
- private String tenant;
- private static String host = SigarUtil.getHost();
- @Autowired
- private Mongo mongo;
- /**
- * 服务器健康指标采集
- *
- * @return
- */
- public void collectEnvHealth() {
- try {
- BasicDBObject result = new BasicDBObject();
- DBCollection terminal = mongo.getDB(MonitorConstant.DATABASE).getCollection(MonitorConstant.SERVER);
- result.put("tenant", tenant);
- result.put("create_date", DateUtil.getCurrentString(DateUtil.DEFAULT_YMDHMSDATE_FORMAT));
- result.put("create_time", DateUtil.getSysDateTime());
- result.put("host", host);
- //cpu
- net.sf.json.JSONObject cpu = net.sf.json.JSONObject.fromObject(SigarUtil.cpu());
- result.put("data", cpu);
- result.put("type", MonitorConstant.CPU);
- terminal.insert(result);
- //内存
- net.sf.json.JSONObject memory = net.sf.json.JSONObject.fromObject(SigarUtil.memory());
- result.put("data", memory);
- result.put("type", MonitorConstant.MEMORY);
- result.remove("_id");
- terminal.insert(result);
- //硬盘
- List<net.sf.json.JSONObject> files = JSONArray.fromObject(SigarUtil.file());
- result.put("data", files);
- result.put("type", MonitorConstant.FILES);
- result.remove("_id");
- terminal.insert(result);
- //网络
- JSONObject net = JSONObject.fromObject(SigarUtil.net());
- result.put("data", net);
- result.put("type", MonitorConstant.NET);
- result.remove("_id");
- terminal.insert(result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void checkHost() {
- DBCollection terminal = mongo.getDB(MonitorConstant.DATABASE).getCollection(MonitorConstant.HOST);
- BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
- new BasicDBObject[]{
- new BasicDBObject().append("host", host),
- new BasicDBObject().append("tenant", tenant)});
- DBCursor cursor = terminal.find(queryObject);
- if (cursor.size() < 1) {
- try {
- BasicDBObject result = new BasicDBObject();
- String host = SigarUtil.getHost();
- String hostName = SigarUtil.getHostName();
- result.put("tenant", tenant);
- result.put("name", hostName);
- result.put("host", host);
- terminal.insert(result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
|