package com.nms.server.thread.monitor;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.nms.server.bean.NodeModel;
import com.nms.server.bean.SetInfo;
import com.nms.server.common.Common;
import com.nms.server.common.Constants;
import com.nms.server.dao.CommonDao;
import com.nms.server.service.CommonService;
import com.nms.server.service.ThreadService;
import com.nms.server.thread.InitServerThread;
import com.nms.server.util.BoneCPPool;

public class MonitorManagerThread implements Runnable{
	private Logger logger = Logger.getLogger(MonitorManagerThread.class);
	private String name;
	private final int socketPort = Constants.SSL_CLIENT_PORT;
	private long startTime = Calendar.getInstance().getTimeInMillis();
	private SetInfo setInfo = null;
	
	public MonitorManagerThread(String name,SetInfo setInfo) {
		this.name = name;
		this.setInfo = setInfo;
	}
	
	public static void main(String[] args) {
		//把所有的监测都插入一遍？？只是没有初始化本DC的IP管理范围
		CommonDao dao = null;
		try {
			BoneCPPool.initPool();
			dao = new CommonDao();
			CommonService commonService = new CommonService(dao);
			ThreadService service = new ThreadService();
			
			new InitServerThread().initCommonInfo(dao);
			
			//--获取switchPort 监测配置并启动监测任务
			List<SetInfo> setList =  commonService.getDrivingSetInfo(Constants.DETEC_SYSTEMDATE_STR);//时钟同步
			//-- Ping 监测任务启动 和 客户端启动监测任务启动
			if(setList!=null && setList.size()>0){
				for (Iterator setIte = setList.iterator(); setIte.hasNext();) {
					
					SetInfo si = (SetInfo) setIte.next();
//					if(si.getCheckTypeName().equalsIgnoreCase("checktype422")){
//						si.setCheckTypeName("system");
//						MonitorManagerThread thread = new MonitorManagerThread(si.getCheckTypeName()+"_"+si.getProcessIden(),si);
//						Common.service.submit(thread);
//					}
					
						//- 创建线程
						Runnable runnable = new MonitorManagerThread(si.getCheckTypeName()+"_"+si.getProcessIden(),si);
						
						//- 添加到 定时线程池
						ScheduledFuture<?> future = Common.scheduled.scheduleAtFixedRate(runnable, 0, 2, TimeUnit.MINUTES);
				}
				
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(dao!=null){
				dao.close();
				dao=null;
			}
		}
//		ScheduledFuture<?> fcFuture = Common.scheduled.scheduleWithFixedDelay(new DetecDataResoveManagerThread(), 15, Constants.DETEC_DATA_RESOLVE_PERIOD, TimeUnit.SECONDS);
		//注册 管理线程
	}
	
	@Override
	public void run() {
		Thread.currentThread().setName("Monitor:"+name+"");
		List<NodeModel> nodeList = null;
		CommonDao dao = null;
		try {
			dao = new CommonDao();
			CommonService service = new CommonService(dao);
			nodeList = service.getNodeModelListBySetInfo(setInfo);
			/*nodeList.clear();
			nodeList.add(new NodeModel("10.0.9.183",0l));*/
			logger.info("本次监测节点数："+nodeList.size());
		} catch (Exception e) {
			logger.error("Initializing query monitoring node failure", e);
			return;
		}finally{
			if(dao!=null){
				dao.close();
				dao=null;
			}
		}
		
		try {
			
			
			//- 将设置加入计划任务
			if(Constants.DETEC_PING_STR.equalsIgnoreCase(setInfo.getCheckTypeName())){	//Ping 监测线程创建
				
				if(nodeList!= null && nodeList.size()>0){
					for(NodeModel model: nodeList){
						Common.runMonitorRunnable(new PingThread(name,model.getNodeIp(),setInfo,startTime));
					}
				}
			
			}else if(Constants.DETEC_NMSC_STR.equalsIgnoreCase(setInfo.getCheckTypeName())){	//NMSC 监测线程创建
				if(nodeList!= null && nodeList.size()>0){
					if(Constants.NETTY_SERVER_FLAG == 1 ){	//当启用 nc 主动上传 监测及 任务文件时开启
						//NMSC
						Common.runMonitorRunnable(new ServerNMSCThread(name,setInfo,startTime,nodeList));
					}
					for(NodeModel model: nodeList){
						if(model.getNodeType() != null && model.getNodeType().longValue() == 1l){//非服务器节点
							//SNMP校验监测
							Common.runMonitorRunnable(new NMSClientForSNMPThread(name,model.getNodeIp(),setInfo,startTime));
						}
					}
				}
			}else if(Constants.DETEC_SYSTEMDATE_STR.equalsIgnoreCase(setInfo.getCheckTypeName())){	//系统时间
				
				if(nodeList!= null && nodeList.size()>0){
					if(Constants.NETTY_SERVER_FLAG == 1 ){	//当启用 nc 主动上传 监测及 任务文件时开启,根据心跳计算时间同步
						Common.runMonitorRunnable(new ServerDateThread(name, setInfo, nodeList, startTime));
					}else{
						for(NodeModel model: nodeList){
							//NMSClient 校验修正	针对于计算机 进行NMSClient校验，失败后进行PING校验
							//					针对于非计算机 进行SNMP监测是否启动校验
							if(model.getNodeType() != null && model.getNodeType().longValue() == 0l){	//服务器节点
								//NMSC
								Common.runMonitorRunnable(new SystemDateThread(name,model.getNodeIp(),socketPort,setInfo,startTime));
							}else{		//非服务器节点
								logger.info("监测目标:"+model.getNodeIp()+" 是非服务器节点，不进行"+Constants.DETEC_SYSTEMDATE_STR+"监测");
							}
						}
					}
				}
				
			}else if(Constants.DETEC_SWITCH_STR.equalsIgnoreCase(setInfo.getCheckTypeName())){	//SWITCH 监测线程创建
				if(nodeList!= null && nodeList.size()>0){
					for(NodeModel model: nodeList){
						if(model.getNodeType() != null && model.getNodeType().longValue() == 1l){//交换机节点
//							if(!model.getNodeIp() .equals("10.0.6.254")){
//								continue;
//							}
							//交换机类型节点监测 SNMP校验监测
							Common.runMonitorRunnable(new SwitchPortThread(name,model.getNodeIp(),setInfo,startTime));
						}else{		//计算机类型校验
							logger.info("监测目标:"+model.getNodeIp()+" 不是交换机，无法进行监测");
						}
					}
				}
			 
			}else if(Constants.DETEC_SYSTEM_STR.equalsIgnoreCase(setInfo.getCheckTypeName())){	//system 监测线程创建--预置的，属于snmp监测
				if(nodeList!= null && nodeList.size()>0){
					for(NodeModel model: nodeList){
						if(model.getNodeType() != null && model.getNodeType().longValue() != 0l){	//非服务器节点
							Common.runMonitorRunnable(new SNMP4JThread(name,model.getNodeIp(),setInfo,startTime));
						}else{		//计算机类型校验
							logger.info("监测目标:"+model.getNodeIp()+" 是服务器节点，不进行该类监测");
						}
					}
				}
			 
			}
			else if(setInfo.getIsSNMP()!= null && setInfo.getIsSNMP().longValue()==1){	//SNMP4j 监测线程创建--针对的是非预置的监测
				if(nodeList!= null && nodeList.size()>0){
					for(NodeModel model: nodeList){
						Common.runMonitorRunnable(new SNMP4JThread(name,model.getNodeIp(),setInfo,startTime));
					}
				}
			}
			else if(setInfo.getIsSNMP()!= null && setInfo.getIsSNMP().longValue()==0){	//ObjectSNMP 监测线程创建
				if(nodeList!= null && nodeList.size()>0){
					for(NodeModel model: nodeList){
						Common.runMonitorRunnable(new SNMP4JThread(name,model.getNodeIp(),setInfo,startTime));
					}
				}
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}
	
}
