Explorar el Código

服务器指标采集保存模式修改

demon hace 8 años
padre
commit
d540b2d034

+ 3 - 0
hos-broker/src/main/java/com/yihu/hos/common/constants/MonitorConstant.java

@ -7,6 +7,9 @@ package com.yihu.hos.common.constants;
 */
public class MonitorConstant {
    public static String MONITOR = "monitor";
    public static String SERVER = "server";
    public static String HOST = "host";

+ 64 - 17
hos-broker/src/main/java/com/yihu/hos/common/scheduler/MonitorScheduler.java

@ -1,5 +1,6 @@
package com.yihu.hos.common.scheduler;
import com.mongodb.*;
import com.yihu.hos.common.constants.MonitorConstant;
import com.yihu.hos.common.dao.BrokerDao;
import com.yihu.hos.common.util.MongodbUtil;
@ -14,16 +15,21 @@ import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 *  服务器性能数据采集定时器
 * 服务器性能数据采集定时器
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/10/11.
@ -31,17 +37,24 @@ import java.util.List;
@Component
public class MonitorScheduler {
    @Autowired
    private MongoOperations mongoOperations;
    @Autowired
    private Mongo mongo;
    static private final Logger logger = LoggerFactory.getLogger(MonitorScheduler.class);
    private static String host = SigarUtil.getHost();
    @Resource(name = ServiceMonitorService.BEAN_ID)
    private ServiceMonitorService serviceMonitorService;
    @Autowired
    private BrokerDao brokerDao;
    @Scheduled(cron="0 0/1 * * * ?") //每分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?") //每分钟执行一次
    public void statusCheck() {
        System.out.println("每分钟执行一次。开始============================================");
        //TODO 采集服务器健康监控指标数据 statusTask.healthCheck();
//        collectEnvHealth();
        collectEnvHealth();
        try {
            collectServiceHealth();
        } catch (Exception e) {
@ -50,37 +63,42 @@ public class MonitorScheduler {
        System.out.println("每分钟执行一次。结束。");
    }
    public String collectEnvHealth(){
    /**
     * 服务器健康指标采集
     * @return
     */
    public String collectEnvHealth() {
        try {
            MongodbUtil monoEnv = new MongodbUtil("envHealth");
            MongodbUtil monoEnv = new MongodbUtil(MonitorConstant.MONITOR);
            Document result = null;
            result = new Document();
            result.put("create_date", DateUtil.getCurrentString(DateUtil.DEFAULT_YMDHMSDATE_FORMAT));
            result.put("create_time", new Date());
            result.put("host",host);
            //cpu
            JSONObject cpu = JSONObject.fromObject( SigarUtil.cpu());
            result.put("data",cpu);
            JSONObject cpu = JSONObject.fromObject(SigarUtil.cpu());
            result.put("data", cpu);
            result.put("type", MonitorConstant.CPU);
            monoEnv.insert(host,result);
            monoEnv.insert(MonitorConstant.SERVER, result);
            //内存
            JSONObject memory = JSONObject.fromObject( SigarUtil.memory());
            result.put("data",memory);
            JSONObject memory = JSONObject.fromObject(SigarUtil.memory());
            result.put("data", memory);
            result.put("type", MonitorConstant.MEMORY);
            result.remove("_id");
            monoEnv.insert(host,result);
            monoEnv.insert(MonitorConstant.SERVER, result);
            //硬盘
            List<JSONObject> files = JSONArray.fromObject( SigarUtil.file());
            result.put("data",files);
            List<JSONObject> files = JSONArray.fromObject(SigarUtil.file());
            result.put("data", files);
            result.put("type", MonitorConstant.FILES);
            result.remove("_id");
            monoEnv.insert(host, result);
            monoEnv.insert(MonitorConstant.SERVER, result);
            //网络
            JSONObject net = JSONObject.fromObject( SigarUtil.net());
            result.put("data",net);
            JSONObject net = JSONObject.fromObject(SigarUtil.net());
            result.put("data", net);
            result.put("type", MonitorConstant.NET);
            result.remove("_id");
            monoEnv.insert(host,result);
            monoEnv.insert(MonitorConstant.SERVER, result);
        } catch (Exception e) {
            e.printStackTrace();
@ -117,4 +135,33 @@ public class MonitorScheduler {
//        logger.info("每10秒执行一次。结束。");
//    }
    /**
     * 服务器列表保存
     */
    @Scheduled(initialDelay=3000,cron = "0 0 12 * * ?") //每天中午12点触发
    public void checkHost() {
        MongodbUtil monoEnv = new MongodbUtil("monitor");
        mongoOperations = new MongoTemplate(mongo, "monitor");
        DBCollection envCollection = mongoOperations.getCollection(MonitorConstant.HOST);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("host", host)});
        DBCursor cursor = envCollection.find(queryObject);
        if (cursor.size() < 1) {
            try {
                org.json.JSONObject result = new org.json.JSONObject();
                InetAddress addr = null;
                addr = InetAddress.getLocalHost();
                result.put("name",  addr.getHostName());
                result.put("host",  addr.getHostAddress());
                monoEnv.insert(MonitorConstant.HOST, result);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }
}

+ 32 - 18
src/main/java/com/yihu/hos/monitor/service/ServerMonitorService.java

@ -21,7 +21,9 @@ import java.util.*;
@Service("ServerMonitorService")
public class ServerMonitorService {
    public static final String BEAN_ID = "ServerMonitorService";
    public static final String envHealth = "envHealth";
    public static final String MONITOR = "monitor";
    public static final String SERVER = "server";
    public static final String HOST = "host";
    @Autowired
    private MongoOperations mongoOperations;
@ -29,11 +31,12 @@ public class ServerMonitorService {
    @Autowired
    private Mongo mongo;
    public Result getMonitorList(String table, String type, String beginTime, String endTime) {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        DBCollection envCollection = mongoOperations.getCollection(table);
    public Result getMonitorList(String host, String type, String beginTime, String endTime) {
        mongoOperations = new MongoTemplate(mongo, MONITOR);
        DBCollection envCollection = mongoOperations.getCollection(SERVER);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("host",host),
                        new BasicDBObject().append("type",type),
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
@ -54,10 +57,11 @@ public class ServerMonitorService {
    }
    public Result getMonitorDetail(String host, String type, String date) {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        DBCollection envCollection = mongoOperations.getCollection(host);
        mongoOperations = new MongoTemplate(mongo, MONITOR);
        DBCollection envCollection = mongoOperations.getCollection(SERVER);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("host", host),
                        new BasicDBObject().append("type", type),
                        new BasicDBObject().append("create_time",new BasicDBObject().append(QueryOperators.LTE, DateUtil.toTimestamp(date)))});
@ -86,34 +90,44 @@ public class ServerMonitorService {
    }
    public Result getHosts() {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        Set<String> collections = mongoOperations.getCollectionNames();
        List<Map<String,Object>> result = new ArrayList<>();
        Iterator<String> iterator = collections.iterator();
        while (iterator.hasNext()){
            String host = iterator.next();
        mongoOperations = new MongoTemplate(mongo, MONITOR);
        DBCollection envCollection = mongoOperations.getCollection(HOST);
        DBCursor cursor = envCollection.find();
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            Map<String,Object> hostMap = new HashMap<>();
            hostMap.put("host",host);
            hostMap.put("host",dbObject.get("host"));
            hostMap.put("name",dbObject.get("name"));
            result.add(hostMap);
            hostMap=null;
        }
        ActionResult actionResult = new ActionResult();
        actionResult.setData(result);
        return actionResult;
    }
    /**
     * 獲取服務器列表 树
     * @return
     * @throws Exception
     */
    public Result getServerTreeList() throws Exception {
        List<TreeView> treeList = new ArrayList<>();
        mongoOperations = new MongoTemplate(mongo, envHealth);
        Set<String> collections = mongoOperations.getCollectionNames();
        for (String host : collections) {
        mongoOperations = new MongoTemplate(mongo, MONITOR);
        DBCollection envCollection = mongoOperations.getCollection(HOST);
        DBCursor cursor = envCollection.find();
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            TreeView rootTree = new TreeView();
            rootTree.setId("host" + host);
            rootTree.setId("host" + dbObject.get("host"));
            rootTree.setPid("-1");
            rootTree.setText(host);
            rootTree.setText(dbObject.get("host").toString());
            treeList.add(rootTree);
        }
        JSONArray jsonArray = new JSONArray(treeList);
        JSONArray jsonArray = new JSONArray(treeList);
        return Result.success(jsonArray.toString());
    }