|  | @ -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();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |