From 9b3c3ac5d7828b348558012f6167527459310f21 Mon Sep 17 00:00:00 2001 From: chenjinsong Date: Thu, 27 Sep 2018 16:17:06 +0800 Subject: initial commit --- src/com/nms/server/DataController.java | 132 + src/com/nms/server/bean/AlarmInfo.java | 197 + src/com/nms/server/bean/ContactSetInfo.java | 127 + src/com/nms/server/bean/DetecAllInfo.java | 328 + src/com/nms/server/bean/DetectInfo.java | 403 ++ src/com/nms/server/bean/DownloadFileModel.java | 43 + src/com/nms/server/bean/EmailInfo.java | 148 + src/com/nms/server/bean/EmailModel.java | 36 + src/com/nms/server/bean/EventRecordLibrary.java | 128 + src/com/nms/server/bean/LoopMissionRoundInfo.java | 53 + src/com/nms/server/bean/MissionResult.java | 41 + src/com/nms/server/bean/MissionResult2.java | 118 + src/com/nms/server/bean/MissionStateTable.java | 152 + src/com/nms/server/bean/NmsErrorCode.java | 86 + src/com/nms/server/bean/NmsErrorInfo.java | 135 + src/com/nms/server/bean/NodeModel.java | 131 + src/com/nms/server/bean/NodegroupModel.java | 66 + src/com/nms/server/bean/ReturnFilePO.java | 106 + src/com/nms/server/bean/ServerIpSegment.java | 32 + src/com/nms/server/bean/ServerTable.java | 121 + src/com/nms/server/bean/SetInfo.java | 277 + src/com/nms/server/bean/SimpleNode.java | 29 + src/com/nms/server/bean/TableColumnsModel.java | 57 + src/com/nms/server/bean/TableModel.java | 39 + src/com/nms/server/bean/Task1.java | 70 + src/com/nms/server/bean/Task4.java | 165 + src/com/nms/server/bean/Task6.java | 181 + src/com/nms/server/bean/TrapMessageInfo.java | 162 + src/com/nms/server/common/Common.java | 2798 ++++++++ src/com/nms/server/common/CommonResources.java | 68 + src/com/nms/server/common/Config.java | 159 + src/com/nms/server/common/ConfigUpdate.java | 264 + src/com/nms/server/common/Constants.java | 844 +++ src/com/nms/server/common/DetectionConstants.java | 5 + src/com/nms/server/common/EmailTypeConstants.java | 74 + src/com/nms/server/common/MissionConstants.java | 33 + src/com/nms/server/common/UpdateParams.java | 40 + src/com/nms/server/dao/CommonDao.java | 254 + src/com/nms/server/dao/OracleToMysql.java | 473 ++ src/com/nms/server/service/ChangeService.java | 290 + src/com/nms/server/service/CommonService.java | 6656 ++++++++++++++++++++ src/com/nms/server/service/EmailService.java | 707 +++ src/com/nms/server/service/ThreadService.java | 68 + src/com/nms/server/service/UpgradeService.java | 3470 ++++++++++ src/com/nms/server/snmp/trap/TrapServer.java | 375 ++ src/com/nms/server/thread/DBTestThread.java | 40 + .../nms/server/thread/DetecDataCollectThread.java | 148 + src/com/nms/server/thread/DetecSelfThread.java | 115 + src/com/nms/server/thread/GetLocalIpFromWeb.java | 25 + src/com/nms/server/thread/InitServerThread.java | 491 ++ src/com/nms/server/thread/ResetServerThread.java | 106 + src/com/nms/server/thread/WritePidThread.java | 68 + .../alarmData/AlarmDataResoveManagerThread.java | 167 + .../thread/alarmData/AlarmDataResoveThread.java | 211 + .../thread/change/ChangeForActiveAlarmInfo.java | 127 + .../server/thread/change/ChangeForCheckType.java | 74 + .../server/thread/change/ChangeForNodeGroup.java | 207 + .../nms/server/thread/change/ChangeForSetInfo.java | 196 + .../server/thread/change/ChangeManagerThread.java | 45 + .../thread/change/ChangePluginScriptFile.java | 534 ++ src/com/nms/server/thread/change/ChangeThread.java | 173 + .../thread/change/socket/ChangeSocketThread.java | 108 + .../dataCollect/DataCollectManagerThread.java | 274 + .../thread/dataCollect/DataCollectThread.java | 613 ++ .../NoDetectDataCollectManagerThread.java | 251 + .../NonRltTaskResultCollectManagerThread.java | 107 + .../dataCollect/NonRltTaskResultCollectThread.java | 156 + .../deleteFiles/DeleteFilesManagerThread.java | 38 + .../thread/deleteFiles/DeleteFilesThread.java | 148 + .../detecData/DetecDataResoveManagerThread.java | 170 + .../thread/detecData/DetecDataResoveThread.java | 1217 ++++ .../thread/detecData/NewDetecDataResoveThread.java | 1264 ++++ .../detectDataHandler/DataInsertManagerThread.java | 51 + .../detectDataHandler/DataResolveThread.java | 1263 ++++ .../DetectDetailInsertThread.java | 220 + .../detectDataHandler/DetectInfoInsertThread.java | 1000 +++ .../thread/email/RefreshEmailFlagThread.java | 49 + .../errorinfo/ErrorInfoResoveManagerThread.java | 162 + .../thread/errorinfo/ErrorInfoResoveThread.java | 89 + .../server/thread/file/DetectFileReaderThread.java | 145 + .../server/thread/file/ResultFileReaderThread.java | 105 + .../server/thread/file/ReturnFileReaderThread.java | 155 + .../nms/server/thread/file/UnZipManagerThread.java | 63 + src/com/nms/server/thread/file/UnZipThread.java | 124 + .../file/upload/FileUploadManagerThread.java | 131 + .../thread/file/upload/FileUploadThread3.java | 87 + .../thread/mission/InitRunningMissionThread.java | 285 + .../thread/mission/LoadNewMissionThread.java | 660 ++ .../thread/mission/MissionPollingThread.java | 456 ++ .../thread/mission/MissionResultManagerThread.java | 208 + .../server/thread/mission/MissionResultThread.java | 228 + .../nms/server/thread/mission/NMSClientTask1.java | 74 + .../nms/server/thread/mission/NMSClientTask4.java | 72 + .../thread/mission/NMSClientTask4Cancel.java | 35 + .../nms/server/thread/mission/NMSClientTask6.java | 82 + .../server/thread/mission/NMSClientTask6Cover.java | 57 + .../thread/monitor/MonitorManagerThread.java | 190 + .../thread/monitor/NMSClientForSNMPThread.java | 439 ++ .../thread/monitor/NMSClientForSocketThread.java | 757 +++ src/com/nms/server/thread/monitor/PingThread.java | 354 ++ .../nms/server/thread/monitor/SNMP4JThread.java | 229 + .../server/thread/monitor/ServerDateThread.java | 84 + .../server/thread/monitor/ServerNMSCThread.java | 181 + .../server/thread/monitor/SwitchPortThread.java | 1048 +++ .../server/thread/monitor/SystemDateThread.java | 163 + .../thread/monitor/test/DPSwitchProt64Data.java | 981 +++ .../server/thread/monitor/util/MonitorUtil.java | 351 ++ src/com/nms/server/thread/netty/BusinessType.java | 40 + src/com/nms/server/thread/netty/Message.java | 84 + .../nms/server/thread/netty/MessageDecoder.java | 197 + .../nms/server/thread/netty/MessageHandler.java | 248 + src/com/nms/server/thread/netty/NettyServer.java | 93 + .../overrunData/DataFileReaderManagerThread.java | 396 ++ .../overrunData/MissionResultFileResoveThread.java | 230 + .../nms/server/thread/socket/NMSWebBPDownload.java | 136 + .../nms/server/thread/socket/SSLCertManager.java | 92 + src/com/nms/server/thread/socket/SSLCertOper.java | 214 + src/com/nms/server/thread/socket/SSLClient.java | 62 + src/com/nms/server/thread/socket/SSLServer.java | 563 ++ .../nms/server/thread/socket/SSLServerManager.java | 100 + src/com/nms/server/thread/socket/SocketCMD.java | 147 + src/com/nms/server/util/BoneCPPool.java | 217 + src/com/nms/server/util/BoneCPTest.java | 72 + src/com/nms/server/util/CSVUtils.java | 455 ++ src/com/nms/server/util/CommandExecUtil.java | 335 + src/com/nms/server/util/ConnectionOracle.java | 984 +++ src/com/nms/server/util/DateUtil.java | 203 + src/com/nms/server/util/DbPool.java | 47 + src/com/nms/server/util/DruidPool.java | 78 + src/com/nms/server/util/ExceptionPrintUtils.java | 13 + src/com/nms/server/util/FileUtils.java | 448 ++ src/com/nms/server/util/FileWrUtil.java | 284 + src/com/nms/server/util/IpCovert.java | 95 + src/com/nms/server/util/LocalAddress.java | 58 + src/com/nms/server/util/MD5Util.java | 88 + src/com/nms/server/util/MathUtil.java | 10 + src/com/nms/server/util/SNMP4JUtils.java | 541 ++ src/com/nms/server/util/SNMPObjectUtils.java | 374 ++ .../server/util/SQLExecuteTimeoutException.java | 330 + src/com/nms/server/util/SendMail.java | 160 + src/com/nms/server/util/StringUtil.java | 218 + src/com/nms/server/util/URLClassLoaderUtil.java | 238 + src/com/nms/server/util/UnicodeInputStream.java | 102 + src/com/nms/server/util/UnicodeReader.java | 106 + src/com/nms/server/util/ZipUtil.java | 255 + .../server/util/file/BufferedRandomAccessFile.java | 308 + src/com/nms/server/util/file/Res.java | 17 + .../util/log4j/MyDailyRollingFileAppender.java | 14 + .../server/util/log4j/MyRollingFileAppender.java | 14 + .../nms/server/util/socket/SSLSocketCallable.java | 101 + .../socket/SSLSocketCallableForDateCollection.java | 121 + src/com/nms/server/util/socket/SocketUtils.java | 1604 +++++ .../util/socket/limiter/BandwidthLimiter.java | 88 + .../util/socket/limiter/InputStreamLimiter.java | 98 + .../util/socket/limiter/OutputStreamLimiter.java | 66 + src/druid.properties | 38 + src/log4j.properties | 28 + src/myconfig.properties | 283 + src/old/interruptThread/model/SimpleModel1.java | 63 + src/old/interruptThread/model/SimpleModel2.java | 63 + 160 files changed, 49011 insertions(+) create mode 100644 src/com/nms/server/DataController.java create mode 100644 src/com/nms/server/bean/AlarmInfo.java create mode 100644 src/com/nms/server/bean/ContactSetInfo.java create mode 100644 src/com/nms/server/bean/DetecAllInfo.java create mode 100644 src/com/nms/server/bean/DetectInfo.java create mode 100644 src/com/nms/server/bean/DownloadFileModel.java create mode 100644 src/com/nms/server/bean/EmailInfo.java create mode 100644 src/com/nms/server/bean/EmailModel.java create mode 100644 src/com/nms/server/bean/EventRecordLibrary.java create mode 100644 src/com/nms/server/bean/LoopMissionRoundInfo.java create mode 100644 src/com/nms/server/bean/MissionResult.java create mode 100644 src/com/nms/server/bean/MissionResult2.java create mode 100644 src/com/nms/server/bean/MissionStateTable.java create mode 100644 src/com/nms/server/bean/NmsErrorCode.java create mode 100644 src/com/nms/server/bean/NmsErrorInfo.java create mode 100644 src/com/nms/server/bean/NodeModel.java create mode 100644 src/com/nms/server/bean/NodegroupModel.java create mode 100644 src/com/nms/server/bean/ReturnFilePO.java create mode 100644 src/com/nms/server/bean/ServerIpSegment.java create mode 100644 src/com/nms/server/bean/ServerTable.java create mode 100644 src/com/nms/server/bean/SetInfo.java create mode 100644 src/com/nms/server/bean/SimpleNode.java create mode 100644 src/com/nms/server/bean/TableColumnsModel.java create mode 100644 src/com/nms/server/bean/TableModel.java create mode 100644 src/com/nms/server/bean/Task1.java create mode 100644 src/com/nms/server/bean/Task4.java create mode 100644 src/com/nms/server/bean/Task6.java create mode 100644 src/com/nms/server/bean/TrapMessageInfo.java create mode 100644 src/com/nms/server/common/Common.java create mode 100644 src/com/nms/server/common/CommonResources.java create mode 100644 src/com/nms/server/common/Config.java create mode 100644 src/com/nms/server/common/ConfigUpdate.java create mode 100644 src/com/nms/server/common/Constants.java create mode 100644 src/com/nms/server/common/DetectionConstants.java create mode 100644 src/com/nms/server/common/EmailTypeConstants.java create mode 100644 src/com/nms/server/common/MissionConstants.java create mode 100644 src/com/nms/server/common/UpdateParams.java create mode 100644 src/com/nms/server/dao/CommonDao.java create mode 100644 src/com/nms/server/dao/OracleToMysql.java create mode 100644 src/com/nms/server/service/ChangeService.java create mode 100644 src/com/nms/server/service/CommonService.java create mode 100644 src/com/nms/server/service/EmailService.java create mode 100644 src/com/nms/server/service/ThreadService.java create mode 100644 src/com/nms/server/service/UpgradeService.java create mode 100644 src/com/nms/server/snmp/trap/TrapServer.java create mode 100644 src/com/nms/server/thread/DBTestThread.java create mode 100644 src/com/nms/server/thread/DetecDataCollectThread.java create mode 100644 src/com/nms/server/thread/DetecSelfThread.java create mode 100644 src/com/nms/server/thread/GetLocalIpFromWeb.java create mode 100644 src/com/nms/server/thread/InitServerThread.java create mode 100644 src/com/nms/server/thread/ResetServerThread.java create mode 100644 src/com/nms/server/thread/WritePidThread.java create mode 100644 src/com/nms/server/thread/alarmData/AlarmDataResoveManagerThread.java create mode 100644 src/com/nms/server/thread/alarmData/AlarmDataResoveThread.java create mode 100644 src/com/nms/server/thread/change/ChangeForActiveAlarmInfo.java create mode 100644 src/com/nms/server/thread/change/ChangeForCheckType.java create mode 100644 src/com/nms/server/thread/change/ChangeForNodeGroup.java create mode 100644 src/com/nms/server/thread/change/ChangeForSetInfo.java create mode 100644 src/com/nms/server/thread/change/ChangeManagerThread.java create mode 100644 src/com/nms/server/thread/change/ChangePluginScriptFile.java create mode 100644 src/com/nms/server/thread/change/ChangeThread.java create mode 100644 src/com/nms/server/thread/change/socket/ChangeSocketThread.java create mode 100644 src/com/nms/server/thread/dataCollect/DataCollectManagerThread.java create mode 100644 src/com/nms/server/thread/dataCollect/DataCollectThread.java create mode 100644 src/com/nms/server/thread/dataCollect/NoDetectDataCollectManagerThread.java create mode 100644 src/com/nms/server/thread/dataCollect/NonRltTaskResultCollectManagerThread.java create mode 100644 src/com/nms/server/thread/dataCollect/NonRltTaskResultCollectThread.java create mode 100644 src/com/nms/server/thread/deleteFiles/DeleteFilesManagerThread.java create mode 100644 src/com/nms/server/thread/deleteFiles/DeleteFilesThread.java create mode 100644 src/com/nms/server/thread/detecData/DetecDataResoveManagerThread.java create mode 100644 src/com/nms/server/thread/detecData/DetecDataResoveThread.java create mode 100644 src/com/nms/server/thread/detecData/NewDetecDataResoveThread.java create mode 100644 src/com/nms/server/thread/detectDataHandler/DataInsertManagerThread.java create mode 100644 src/com/nms/server/thread/detectDataHandler/DataResolveThread.java create mode 100644 src/com/nms/server/thread/detectDataHandler/DetectDetailInsertThread.java create mode 100644 src/com/nms/server/thread/detectDataHandler/DetectInfoInsertThread.java create mode 100644 src/com/nms/server/thread/email/RefreshEmailFlagThread.java create mode 100644 src/com/nms/server/thread/errorinfo/ErrorInfoResoveManagerThread.java create mode 100644 src/com/nms/server/thread/errorinfo/ErrorInfoResoveThread.java create mode 100644 src/com/nms/server/thread/file/DetectFileReaderThread.java create mode 100644 src/com/nms/server/thread/file/ResultFileReaderThread.java create mode 100644 src/com/nms/server/thread/file/ReturnFileReaderThread.java create mode 100644 src/com/nms/server/thread/file/UnZipManagerThread.java create mode 100644 src/com/nms/server/thread/file/UnZipThread.java create mode 100644 src/com/nms/server/thread/file/upload/FileUploadManagerThread.java create mode 100644 src/com/nms/server/thread/file/upload/FileUploadThread3.java create mode 100644 src/com/nms/server/thread/mission/InitRunningMissionThread.java create mode 100644 src/com/nms/server/thread/mission/LoadNewMissionThread.java create mode 100644 src/com/nms/server/thread/mission/MissionPollingThread.java create mode 100644 src/com/nms/server/thread/mission/MissionResultManagerThread.java create mode 100644 src/com/nms/server/thread/mission/MissionResultThread.java create mode 100644 src/com/nms/server/thread/mission/NMSClientTask1.java create mode 100644 src/com/nms/server/thread/mission/NMSClientTask4.java create mode 100644 src/com/nms/server/thread/mission/NMSClientTask4Cancel.java create mode 100644 src/com/nms/server/thread/mission/NMSClientTask6.java create mode 100644 src/com/nms/server/thread/mission/NMSClientTask6Cover.java create mode 100644 src/com/nms/server/thread/monitor/MonitorManagerThread.java create mode 100644 src/com/nms/server/thread/monitor/NMSClientForSNMPThread.java create mode 100644 src/com/nms/server/thread/monitor/NMSClientForSocketThread.java create mode 100644 src/com/nms/server/thread/monitor/PingThread.java create mode 100644 src/com/nms/server/thread/monitor/SNMP4JThread.java create mode 100644 src/com/nms/server/thread/monitor/ServerDateThread.java create mode 100644 src/com/nms/server/thread/monitor/ServerNMSCThread.java create mode 100644 src/com/nms/server/thread/monitor/SwitchPortThread.java create mode 100644 src/com/nms/server/thread/monitor/SystemDateThread.java create mode 100644 src/com/nms/server/thread/monitor/test/DPSwitchProt64Data.java create mode 100644 src/com/nms/server/thread/monitor/util/MonitorUtil.java create mode 100644 src/com/nms/server/thread/netty/BusinessType.java create mode 100644 src/com/nms/server/thread/netty/Message.java create mode 100644 src/com/nms/server/thread/netty/MessageDecoder.java create mode 100644 src/com/nms/server/thread/netty/MessageHandler.java create mode 100644 src/com/nms/server/thread/netty/NettyServer.java create mode 100644 src/com/nms/server/thread/overrunData/DataFileReaderManagerThread.java create mode 100644 src/com/nms/server/thread/overrunData/MissionResultFileResoveThread.java create mode 100644 src/com/nms/server/thread/socket/NMSWebBPDownload.java create mode 100644 src/com/nms/server/thread/socket/SSLCertManager.java create mode 100644 src/com/nms/server/thread/socket/SSLCertOper.java create mode 100644 src/com/nms/server/thread/socket/SSLClient.java create mode 100644 src/com/nms/server/thread/socket/SSLServer.java create mode 100644 src/com/nms/server/thread/socket/SSLServerManager.java create mode 100644 src/com/nms/server/thread/socket/SocketCMD.java create mode 100644 src/com/nms/server/util/BoneCPPool.java create mode 100644 src/com/nms/server/util/BoneCPTest.java create mode 100644 src/com/nms/server/util/CSVUtils.java create mode 100644 src/com/nms/server/util/CommandExecUtil.java create mode 100644 src/com/nms/server/util/ConnectionOracle.java create mode 100644 src/com/nms/server/util/DateUtil.java create mode 100644 src/com/nms/server/util/DbPool.java create mode 100644 src/com/nms/server/util/DruidPool.java create mode 100644 src/com/nms/server/util/ExceptionPrintUtils.java create mode 100644 src/com/nms/server/util/FileUtils.java create mode 100644 src/com/nms/server/util/FileWrUtil.java create mode 100644 src/com/nms/server/util/IpCovert.java create mode 100644 src/com/nms/server/util/LocalAddress.java create mode 100644 src/com/nms/server/util/MD5Util.java create mode 100644 src/com/nms/server/util/MathUtil.java create mode 100644 src/com/nms/server/util/SNMP4JUtils.java create mode 100644 src/com/nms/server/util/SNMPObjectUtils.java create mode 100644 src/com/nms/server/util/SQLExecuteTimeoutException.java create mode 100644 src/com/nms/server/util/SendMail.java create mode 100644 src/com/nms/server/util/StringUtil.java create mode 100644 src/com/nms/server/util/URLClassLoaderUtil.java create mode 100644 src/com/nms/server/util/UnicodeInputStream.java create mode 100644 src/com/nms/server/util/UnicodeReader.java create mode 100644 src/com/nms/server/util/ZipUtil.java create mode 100644 src/com/nms/server/util/file/BufferedRandomAccessFile.java create mode 100644 src/com/nms/server/util/file/Res.java create mode 100644 src/com/nms/server/util/log4j/MyDailyRollingFileAppender.java create mode 100644 src/com/nms/server/util/log4j/MyRollingFileAppender.java create mode 100644 src/com/nms/server/util/socket/SSLSocketCallable.java create mode 100644 src/com/nms/server/util/socket/SSLSocketCallableForDateCollection.java create mode 100644 src/com/nms/server/util/socket/SocketUtils.java create mode 100644 src/com/nms/server/util/socket/limiter/BandwidthLimiter.java create mode 100644 src/com/nms/server/util/socket/limiter/InputStreamLimiter.java create mode 100644 src/com/nms/server/util/socket/limiter/OutputStreamLimiter.java create mode 100644 src/druid.properties create mode 100644 src/log4j.properties create mode 100644 src/myconfig.properties create mode 100644 src/old/interruptThread/model/SimpleModel1.java create mode 100644 src/old/interruptThread/model/SimpleModel2.java (limited to 'src') diff --git a/src/com/nms/server/DataController.java b/src/com/nms/server/DataController.java new file mode 100644 index 0000000..b539737 --- /dev/null +++ b/src/com/nms/server/DataController.java @@ -0,0 +1,132 @@ +package com.nms.server; + +import java.sql.Connection; + +import javax.swing.JOptionPane; + +import org.apache.log4j.Logger; + +import com.fang.lang.Db; +import com.nms.server.common.Common; +import com.nms.server.common.Config; +import com.nms.server.common.Constants; +import com.nms.server.thread.InitServerThread; +import com.nms.server.thread.WritePidThread; +import com.nms.server.thread.netty.NettyServer; +import com.nms.server.thread.socket.SSLServerManager; +import com.nms.server.util.DbPool; + + +/** + * DataController 程序启动主方法类 + * @date Aug 10, 2011 1:55:25 PM + * @author ZhangGang + * + */ +public class DataController { + private static Logger logger = Logger.getLogger(DataController.class); + + public static void doShutDownWork() { + logger.info("注册程序退出事件"); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + try { +// Thread.currentThread().setName("退出DC,缓存数据清理线程"); + Thread.currentThread().setName("Exit DC, Caching Data Cleaning Thread"); + Common.stop();//程序停止运行 + logger.info("停止DataController,处理缓存数据。。。"); + Common.clearDataInMem(); + logger.info("停止DataController,处理缓存数据 完成"); + } catch (Exception ex) { + logger.error("Stop DataController, cache data entry anomalies, cache data to disk", ex);//1.全部入库,入库异常时存盘 或 2.全部存盘,下次启动时入库 + Common.saveDataInMemToDisk(); + logger.error("Stop DataController, cache data entry anomalies, cache data stored in hard disk to complete", ex);//1.全部入库,入库异常时存盘 或 2.全部存盘,下次启动时入库 + } + } + }); + } + /** + * DataController程序启动入口 + * @time Jan 5, 2012-11:37:59 AM + * @param args + */ + public static void main(String [] args) { +// Thread.currentThread().setName("主程序"); + Thread.currentThread().setName("Main Program"); + logger.info("------- DataController 启动开始------------"); + + doShutDownWork();//注册shutdown事件 + + //书写PID + Common.service.submit(new WritePidThread()); +// WritePidThread pidth = new WritePidThread(); +// pidth.run(); + + //- 初始化数据库连接池(在优化方向:将数据库连接池创建及维护通过 其专有维护线程实现独立管理) + try { + Thread.sleep(3000); + DbPool.initPool(); + //初始化Common.Db + Common.initDb(new Db(new Db.DataSource() { + @Override + public Connection getConnection() { + try { + return DbPool.getConnection(); + } catch (Exception e) { + logger.error("",e); + } + return null; + } + })); + Thread.sleep(3000); + String sequenceTable=Config.getString("sequence.table", "detection_info"); + if(!sequenceTable.equals("0")){ + String[] split=sequenceTable.split(","); + Common.initSequence(Common.getDb().getConnection(),split); + } + + } catch (ClassNotFoundException e) { + logger.error("Load database driver failure"); + } catch (Exception e) { + logger.error("Initialization of connection pool exception:"+e.getMessage()); + } + + //- 启动加密通讯端口监听 + try { + Common.service.submit(new SSLServerManager(Constants.SSL_SERVER_PORT,true)); //与web和Agent端通讯线程启动 + if(Constants.NETTY_SERVER_FLAG == 1){ + //启动netty server + Common.service.submit(new NettyServer(Constants.NETTY_SERVER_PORT, Constants.NETTY_SERVER_SSL)); + } + } catch (Exception e) { +// String info = "通讯服务启动失败!\n请确定程序是否未启动或检查服务端口["+Constants.SSL_SERVER_PORT+"]未被占用!\n"+e.getMessage(); + String info = "The communication service failed to start! \n please confirm whether the program has not started or checked the service port["+Constants.SSL_SERVER_PORT+"]is not occupied!!\n"+e.getMessage(); +// String info = "i18n_server.DataController.showMsg_n81i["+Constants.SSL_SERVER_PORT+"]i18n_server.DataController.isExists_n81i!\n"+e.getMessage(); + logger.error(info);//i18nlog +// JOptionPane.showMessageDialog(null, info, "错误", JOptionPane.ERROR_MESSAGE); +// JOptionPane.showMessageDialog(null, info, "Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, info, "Error", JOptionPane.ERROR_MESSAGE); + System.exit(0); + } + + //- 获取本机IP + /*try { + String localIp = Common.getLocalIp(); + + if(StringUtils.isBlank(localIp)){ + String info = "读取本地IP失败 程序无法启动 请检查配置文件"; + logger.error(info); + JOptionPane.showMessageDialog(null, info, "错误", JOptionPane.ERROR_MESSAGE); + System.exit(0); + } + } catch (Exception e) { + String info = "主线程执行异常,被迫中止\n"+e.getMessage(); + logger.error(info); + JOptionPane.showMessageDialog(null, info, "错误", JOptionPane.ERROR_MESSAGE); + System.exit(0); + }*/ + + //- 执行加载启动操作 + Common.service.submit(new InitServerThread()); + } +} diff --git a/src/com/nms/server/bean/AlarmInfo.java b/src/com/nms/server/bean/AlarmInfo.java new file mode 100644 index 0000000..5d03c25 --- /dev/null +++ b/src/com/nms/server/bean/AlarmInfo.java @@ -0,0 +1,197 @@ +package com.nms.server.bean; + +/** + * 告警信息对象 + * + */ +public class AlarmInfo { + /** + * 告警设置id + */ + private Long id; + /** + * 监测设置ID + */ + private Long setInfoId; + /** + * 监测类型名称(cpu...) + */ + private String checkType; + /** + * 进程标识 + */ + private String processIden; + + /** + * 字段表字段Id + */ + private Long metadataId; + /** + * 字段序号 + */ + private Integer showNum; + /** + * 字段描述 + */ + private String filedCommonts; + /** + * 报警状态 + */ + private String policeState; + /** + * 报警值 + */ + private String policeValue; + /** + * 报警单位 + */ + private String policeUnit; + /** + * 报警比较符:针对number型数据>、<、>=、<=、= 针对字符串类型数据equal、 include、exclude + */ + private String policeSysmbols; + /** + * 报警等级 + */ + private Integer policeLevel; + /** + * 报警紧急标示告警 (是否紧急 0:紧急 1:非紧急 默认为1) + */ + private Integer policeEmergent; + /** + * 设置告警时,指定多个标识符(如多个盘符、多个CPU、多个网卡),如硬盘使用率,空:所有盘存在一个盘使用率超过告警值,则告警;all:所有盘总的使用率超过告警值,则告警;指定多个盘符:指定盘存在一个盘使用率超过告警值,则告警 + */ + private String marker; + /** + * 标识符对应字段在metadata表中的id + */ + private Integer markerFiledId;; + /** + * 标识符对应字段在metadata表中的showNum + */ + private Integer markerFiledShowNum; + + + public Long getSetInfoId() { + return setInfoId; + } + public void setSetInfoId(Long setInfoId) { + this.setInfoId = setInfoId; + } + public Long getMetadataId() { + return metadataId; + } + public void setMetadataId(Long metadataId) { + this.metadataId = metadataId; + } + public Integer getShowNum() { + return showNum; + } + public void setShowNum(Integer showNum) { + this.showNum = showNum; + } + public String getPoliceState() { + return policeState; + } + public void setPoliceState(String policeState) { + this.policeState = policeState; + } + + public String getPoliceValue() { + return policeValue; + } + public void setPoliceValue(String policeValue) { + this.policeValue = policeValue; + } + public String getPoliceUnit() { + return policeUnit; + } + public void setPoliceUnit(String policeUnit) { + this.policeUnit = policeUnit; + } + public String getPoliceSysmbols() { + return policeSysmbols; + } + public void setPoliceSysmbols(String policeSysmbols) { + this.policeSysmbols = policeSysmbols; + } + + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public Integer getPoliceLevel() { + return policeLevel; + } + public void setPoliceLevel(Integer policeLevel) { + this.policeLevel = policeLevel; + } + public String getCheckType() { + return checkType; + } + public void setCheckType(String checkType) { + this.checkType = checkType; + } + public String getProcessIden() { + return processIden; + } + public void setProcessIden(String processIden) { + this.processIden = processIden; + } + public String getFiledCommonts() { + return filedCommonts; + } + public void setFiledCommonts(String filedCommonts) { + this.filedCommonts = filedCommonts; + } + public Integer getPoliceEmergent() { + return policeEmergent; + } + public void setPoliceEmergent(Integer policeEmergent) { + if(policeEmergent==null){policeEmergent = 1;} + this.policeEmergent = policeEmergent; + } + + public String toString() { + return "showNum=" + showNum + ",policeSysmbols=" + policeSysmbols + + ",policeValue=" + policeValue + ",policeLevel=" + policeLevel; + } + + public String toStringVal(){ + return showNum + "|" + policeSysmbols + "|" + policeValue + "|" + + policeLevel + "|" + filedCommonts; + } + + public String getMarker() + { + return marker; + } + + public void setMarker(String marker) + { + this.marker = marker; + } + + public Integer getMarkerFiledId() + { + return markerFiledId; + } + + public void setMarkerFiledId(Integer markerFiledId) + { + this.markerFiledId = markerFiledId; + } + + public Integer getMarkerFiledShowNum() + { + return markerFiledShowNum; + } + + public void setMarkerFiledShowNum(Integer markerFiledShowNum) + { + this.markerFiledShowNum = markerFiledShowNum; + } + +} diff --git a/src/com/nms/server/bean/ContactSetInfo.java b/src/com/nms/server/bean/ContactSetInfo.java new file mode 100644 index 0000000..11f4397 --- /dev/null +++ b/src/com/nms/server/bean/ContactSetInfo.java @@ -0,0 +1,127 @@ +package com.nms.server.bean; + +import java.util.Date; + + +/** + * 客户端用到的监测设置信息实体 + * + */ +public class ContactSetInfo { + + /** + * 监测设置信息ID + */ + private Long missionId; + private String missionName; + private String nodeGroupName; + private String nodeGroupsId; + private String nodeIpsId; + private String nodeIpsName; + private Long missionType; + private Long missionState; + private Long isLoop; + private Date startTime; + private Date endTime; + private Long viewLevel; + private Long createUserId; + private Long createUserGroupId; + private Long systemId; + private String contactUserIds; + public Long getViewLevel() { + return viewLevel; + } + public void setViewLevel(Long viewLevel) { + this.viewLevel = viewLevel; + } + public Long getCreateUserId() { + return createUserId; + } + public void setCreateUserId(Long createUserId) { + this.createUserId = createUserId; + } + public Long getCreateUserGroupId() { + return createUserGroupId; + } + public void setCreateUserGroupId(Long createUserGroupId) { + this.createUserGroupId = createUserGroupId; + } + public Long getSystemId() { + return systemId; + } + public void setSystemId(Long systemId) { + this.systemId = systemId; + } + public String getContactUserIds() { + return contactUserIds; + } + public void setContactUserIds(String contactUserIds) { + this.contactUserIds = contactUserIds; + } + public Long getMissionId() { + return missionId; + } + public void setMissionId(Long missionId) { + this.missionId = missionId; + } + public String getMissionName() { + return missionName; + } + public void setMissionName(String missionName) { + this.missionName = missionName; + } + public String getNodeGroupName() { + return nodeGroupName; + } + public void setNodeGroupName(String nodeGroupName) { + this.nodeGroupName = nodeGroupName; + } + public Long getMissionType() { + return missionType; + } + public void setMissionType(Long missionType) { + this.missionType = missionType; + } + public Long getMissionState() { + return missionState; + } + public void setMissionState(Long missionState) { + this.missionState = missionState; + } + public Long getIsLoop() { + return isLoop; + } + public void setIsLoop(Long isLoop) { + this.isLoop = isLoop; + } + public Date getStartTime() { + return startTime; + } + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + public Date getEndTime() { + return endTime; + } + public void setEndTime(Date endTime) { + this.endTime = endTime; + } + public String getNodeGroupsId() { + return nodeGroupsId; + } + public void setNodeGroupsId(String nodeGroupsId) { + this.nodeGroupsId = nodeGroupsId; + } + public String getNodeIpsId() { + return nodeIpsId; + } + public void setNodeIpsId(String nodeIpsId) { + this.nodeIpsId = nodeIpsId; + } + public String getNodeIpsName() { + return nodeIpsName; + } + public void setNodeIpsName(String nodeIpsName) { + this.nodeIpsName = nodeIpsName; + } +} diff --git a/src/com/nms/server/bean/DetecAllInfo.java b/src/com/nms/server/bean/DetecAllInfo.java new file mode 100644 index 0000000..fa36d83 --- /dev/null +++ b/src/com/nms/server/bean/DetecAllInfo.java @@ -0,0 +1,328 @@ +package com.nms.server.bean; + + +import java.util.List; +import java.util.Map; + +/** + * 监测信息 + * @author Nan,Fang + * 2016-2-29 + */ +public class DetecAllInfo { + + + /* 基本信息解析格式变量 */ + private String seqId = null; //seqId + private String setInfoId = null; //监测设置ID + private String checkType = null; //监测类别 + private String process = null; //进程名称(监测类别设置名称) + private Long startTime = null; //监测服务启动时间 + + private Integer delayTime = null; //检测时延(秒) + private Long checkTime = null; //本次检测时间 + private Integer currentTimes = null; //尝试次数 + private Long nextCheckTime = null; //下次计划监测时间 + private Long arriveTime; + + private Integer state = -1; //执行状态是否成功是否正常 + private String showNums = null; //告警序列(数据) + private Integer pLevel = 99; //告警级别(数据) + //private int pl = 99; //告警级别(入库) + private String pValue = null; //告 警 值(数据) + private String dsinfo = null; //状态信息(描述信息) + private String pdata = null; //性能数据 + private List> details;//详细参数 + private boolean delyFlag; //是否解析详细参数标志 + private int urgentLevel ; //紧急级别 + private String alarmInfo;//告警信息 + + private boolean sendEmailFlag;//邮件发送标志 + private boolean stateChangeFlag;//状态变更标志 + private long statusChangTime = -1L;//状态变更时间 + + private EmailInfo emailInfo;//邮件信息 + + private List diskInfoList; // net 特殊格式追加详细信息 + private List netInfoList; // net 特殊格式追加详细信息 + + private DetecAllInfo appendWarningInfo ;//延迟数据,追加告警信息 + + private byte[] dsb;//存放字节数字格式的监测数据,用于解析异常时,存监测数据到硬盘的时候用 + + /** + * 以下属性用于记录原监测数据 + */ + + private String valid; + private String detectionInfoId; + private String waitTime; + /** + * 无参构造方法 + */ + public DetecAllInfo(){ + + } + + public String getSeqId() { + return seqId; + } + + public void setSeqId(String seqId) { + this.seqId = seqId; + } + + public String getSetInfoId() { + return setInfoId; + } + + public void setSetInfoId(String setInfoId) { + this.setInfoId = setInfoId; + } + + public String getCheckType() { + return checkType; + } + + public void setCheckType(String checkType) { + this.checkType = checkType; + } + + public String getProcess() { + return process; + } + + public void setProcess(String process) { + this.process = process; + } + + public Long getStartTime() { + return startTime; + } + + public void setStartTime(Long startTime) { + this.startTime = startTime; + } + + public Integer getDelayTime() { + return delayTime; + } + + public void setDelayTime(Integer delayTime) { + this.delayTime = delayTime; + } + + public Long getCheckTime() { + return checkTime; + } + + public void setCheckTime(Long checkTime) { + this.checkTime = checkTime; + } + + public Integer getCurrentTimes() { + return currentTimes; + } + + public void setCurrentTimes(Integer currentTimes) { + this.currentTimes = currentTimes; + } + + public Long getNextCheckTime() { + return nextCheckTime; + } + + public void setNextCheckTime(Long nextCheckTime) { + this.nextCheckTime = nextCheckTime; + } + + public String getShowNums() { + return showNums; + } + + public void setShowNums(String showNums) { + this.showNums = showNums; + } + + + + public String getpValue() { + return pValue; + } + + public void setpValue(String pValue) { + this.pValue = pValue; + } + + public String getDsinfo() { + return dsinfo; + } + + public void setDsinfo(String dsinfo) { + this.dsinfo = dsinfo; + } + + public String getPdata() { + return pdata; + } + + public void setPdata(String pdata) { + this.pdata = pdata; + } + + public List> getDetails() { + return details; + } + + public void setDetails(List> details) { + this.details = details; + } + + public Long getArriveTime() { + return arriveTime; + } + + public void setArriveTime(Long arriveTime) { + this.arriveTime = arriveTime; + } + + public boolean isDelyFlag() { + return delyFlag; + } + + public void setDelyFlag(boolean delyFlag) { + this.delyFlag = delyFlag; + } + + public int getUrgentLevel() { + return urgentLevel; + } + + public void setUrgentLevel(int urgentLevel) { + this.urgentLevel = urgentLevel; + } + + public String getAlarmInfo() { + return alarmInfo; + } + + public void setAlarmInfo(String alarmInfo) { + this.alarmInfo = alarmInfo; + } + + public boolean isStateChangeFlag() { + return stateChangeFlag; + } + + public void setStateChangeFlag(boolean stateChangeFlag) { + this.stateChangeFlag = stateChangeFlag; + } + + + public boolean isSendEmailFlag() { + return sendEmailFlag; + } + + public void setSendEmailFlag(boolean sendEmailFlag) { + this.sendEmailFlag = sendEmailFlag; + } + + public void setState(Integer state) { + this.state = state; + } + + public Integer getState() { + return state; + } + + public Integer getpLevel() { + return pLevel; + } + + public void setpLevel(Integer pLevel) { + this.pLevel = pLevel; + } + + public EmailInfo getEmailInfo() { + return emailInfo; + } + + public void setEmailInfo(EmailInfo emailInfo) { + this.emailInfo = emailInfo; + } + + public long getStatusChangTime() { + return statusChangTime; + } + + public void setStatusChangTime(long statusChangTime) { + this.statusChangTime = statusChangTime; + } + + + public List getDiskInfoList() { + return diskInfoList; + } + + public void setDiskInfoList(List diskInfoList) { + this.diskInfoList = diskInfoList; + } + + public List getNetInfoList() { + return netInfoList; + } + + public void setNetInfoList(List netInfoList) { + this.netInfoList = netInfoList; + } + + public DetecAllInfo getAppendWarningInfo() { + return appendWarningInfo; + } + + public void setAppendWarningInfo(DetecAllInfo appendWarningInfo) { + this.appendWarningInfo = appendWarningInfo; + } + + + + public String getValid() { + return valid; + } + + public void setValid(String valid) { + this.valid = valid; + } + + public String getDetectionInfoId() { + return detectionInfoId; + } + + public void setDetectionInfoId(String detectionInfoId) { + this.detectionInfoId = detectionInfoId; + } + + public String getWaitTime() { + return waitTime; + } + + public void setWaitTime(String waitTime) { + this.waitTime = waitTime; + } + + + public byte[] getDsb() + { + return dsb; + } + + + public void setDsb(byte[] dsb) + { + this.dsb = dsb; + } + + + + +} diff --git a/src/com/nms/server/bean/DetectInfo.java b/src/com/nms/server/bean/DetectInfo.java new file mode 100644 index 0000000..ff7a7fd --- /dev/null +++ b/src/com/nms/server/bean/DetectInfo.java @@ -0,0 +1,403 @@ +package com.nms.server.bean; + +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import com.fang.U; +import com.nms.server.common.Common; +import com.nms.server.common.Config; +import com.nms.server.dao.OracleToMysql; + +public class DetectInfo { + + public static final int SEQID = 0; //seqId + public static final int SETINFOID = 1; //监测设置ID + public static final int CHECKTYPE = 2; //监测类别 + public static final int PROCESS = 3; //进程名称(监测类别设置名称) + public static final int STARTTIME = 4; //监测服务启动时间 + public static final int DELAYTIME = 5; //检测时延(秒) + public static final int CHECKTIME = 6; //本次检测时间 + public static final int CURRENTTIMES = 7; //尝试次数 + public static final int NEXTCHECKTIME = 8; //下次计划监测时间 + public static final int ARRIVETIME = 9; + public static final int STATE = 10; //执行状态是否成功是否正常 + public static final int SHOWNUMS = 11; //告警序列(数据) + public static final int PLEVEL = 12; //告警级别(数据) + public static final int PVALUE = 13; //告 警 值(数据) + public static final int DSINFO = 14; //状态信息(描述信息) + public static final int PDATA = 15; //性能数据 + public static final int DETAILS = 16;//详细参数 + public static final int DELYFLAG = 17; //是否解析详细参数标志 + public static final int URGENTLEVEL =18 ; //紧急级别 + public static final int ALARMINFO =19 ;//告警信息 + public static final int SENDEMAILFLAG =20;//邮件发送标志 + public static final int STATECHANGEFLAG =21;//状态变更标志 + public static final int STATUSCHANGTIME = 22;//状态变更时间 + public static final int EMAILINFO = 23;//邮件信息 + public static final int DISKINFOLIST = 24; // net 特殊格式追加详细信息 + public static final int NETINFOLIST =25; // net 特殊格式追加详细信息 + public static final int APPENDWARNINGINFO = 26;//延迟数据,追加告警信息 + //public static final int DSB = 27;//存放字节数字格式的监测数据,用于解析异常时,存监测数据到硬盘的时候用 + + + /** + * 以下属性用于记录原监测数据 + */ + public static final int VALID = 27; + public static final int DETECTIONINFOID = 28; + public static final int WAITTIME = 29; + + /** + * 从解析之后数据恢复成原数据时使用 + */ + public static final int SHOWNUMLIST = 30; + public static final int PLEVELLIST = 31; + public static final int ALARMLIST = 32; + + /** + * detect_queue 的 key + */ + public static final String INFO_KEY = "INFO"; + public static final String DISK_STR_KEY = "DISK_STR"; + public static final String NET_STR_KEY = "NET_STR"; + /** + * 详细表中的外键字段名 + */ + public static final String DETECTION_INFO_ID = "DETECTION_INFO_ID"; + + public static final String DETECTION_INFO="detection_info"; + + + + /* + * 该字段是针对,同一时间点多条具体数据(写多个文件),比如:网卡监测(多个网卡问题) + */ + private List detailDatas = new LinkedList(); + + /* + * 针对公共数据的描述信息 + */ + private String descInfo; + + /** + * 尝试次数 (小于最大尝试次数) + */ + private int testTimes; + + /** + * 监测时间 + */ + private long checkTime; + + /* + * 该字段是针对一条数据,关联的其他表的数据;写入到一个文件的: String 是解析标识, List是多条数据(String[]存入表中的数据) + * 如:系统信息监测,数据中有网卡数量,网卡数量关联着其他表(表中又有多个字段,如网卡名称,IP,子网掩码等等) + */ + private Map> relatedDatas; + + public List getDetailDatas() { + return detailDatas; + } + public void setDetailDatas(List detailDatas) { + this.detailDatas = detailDatas; + } + public String getDescInfo() { + return descInfo; + } + public void setDescInfo(String descInfo) { + this.descInfo = descInfo; + } + public Map> getRelatedDatas() { + return relatedDatas; + } + public void setRelatedDatas(Map> relatedDatas) { + this.relatedDatas = relatedDatas; + } + public int getTestTimes() { + return testTimes; + } + public void setTestTimes(int testTimes) { + this.testTimes = testTimes; + } + public long getCheckTime() { + return checkTime; + } + public void setCheckTime(long checkTime) { + this.checkTime = checkTime; + } + + /** + * DETECTION_INFO insert sql + * @return + */ + public static String getInsertInfoSql(){ + String insertInfoSql = ""; + if(Common.SEQUENCE.get(DetectInfo.DETECTION_INFO)!=null){ + insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " + + "PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " + + "POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " + + "DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT,STATUS_CHANGE_TIME,SEQUENCE)" + +" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?)"; + }else{ + insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " + + "PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " + + "POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " + + "DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT,STATUS_CHANGE_TIME)" + +" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))"; + } + + return OracleToMysql.trans(insertInfoSql); + } + + + /** + * 组织detail表的insert语句 + * @param fieldNames + * @param detailTableName + * @param tempMap + * @return + */ + public static String createDetailSql(List fieldNames, + String detailTableName, Map tempMap) { + String insertDetailSql; + StringBuffer preSql = new StringBuffer(); + StringBuffer sufSql = new StringBuffer(); + preSql.append(",DETECTION_INFO_ID"); + sufSql.append(",?"); + fieldNames.add("DETECTION_INFO_ID"); + for(String tempField : tempMap.keySet()){ + fieldNames.add(tempField); + preSql.append("," + tempField); + sufSql.append(",?"); + } + insertDetailSql = "insert into "+detailTableName +" ( "+ preSql.substring(1) +" ) values (" + sufSql.substring(1) +" )"; + return insertDetailSql; + } + + /** + * 计算 detection_info 及详细表的关联主键 + * @param checkTime 监测时间 + * @param seqId 节点id + * @param setId 监测设置id + * @return 10位时间戳 + 5位 seqId + 3位 setId + */ + public static String computeId(String checkTime,String seqId,String setId){ + if(U.Strs.isAllNotBlank(checkTime,seqId,setId)){ + StringBuilder sb = new StringBuilder(); + checkTime = checkTime.substring(checkTime.length() -10);//截取时间戳 后10位 + sb.append("00000"); + sb.append(seqId); + seqId = sb.substring(sb.length()-5);// seqId 占 5位 + sb.delete(0, sb.length());//清空sb + sb.append("000"); + sb.append(setId); + setId = sb.substring(sb.length() - 3); // set info id 占 3位 + sb.setLength(0); + sb.append(checkTime); + sb.append(seqId); + sb.append(setId); + return sb.toString(); + } + return null; + } + + /** + * 监测设置查询权限 为 1 2 3 的邮件列表 sql + * @return + */ + public static String getDetecEmailListOf123Sql(){ + StringBuilder sb = new StringBuilder(); + sb.append(" select distinct c.id, xyj1.email "); + sb.append(" from xt_yh_jbxx xyj1, "); + sb.append(" (select dsi.id, ',' || dsi.contact_user_ids || ',' as ids "); + sb.append(" from detection_set_info dsi "); + sb.append(" where dsi.contact_user_ids is not null) c "); + sb.append(" where c.ids like ('%,' || xyj1.yhid || ',%') "); + sb.append(" and xyj1.email is not null "); + sb.append(" union "); + sb.append(" (select distinct dsi.id,xyj.email "); + sb.append(" from detection_set_info dsi "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on dsi.create_usergroup_id = xjj.jsbh "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join xt_yh_jbxx xyj "); + sb.append(" on xyj.yhid = xyji.yhid "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + sb.append(" and dsi.view_level = 2 "); + sb.append(" and xyj.email is not null "); + sb.append(" and dsi.create_usergroup_id is not null "); + sb.append(" and dsi.contact_user_ids is null) "); + sb.append(" union "); + sb.append(" ( "); + sb.append(" select distinct dsi.id,xyj.email from detection_set_info dsi "); + sb.append(" left join gorup_system_table gst "); + sb.append(" on gst.system_id = dsi.system_id "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on gst.user_group_id = xjj.jsbh "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join xt_yh_jbxx xyj "); + sb.append(" on xyji.yhid = xyj.yhid "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + sb.append(" and dsi.view_level = 3 "); + sb.append(" and xyj.email is not null "); + sb.append(" and dsi.contact_user_ids is null "); + sb.append(" ) "); + return OracleToMysql.trans(sb.toString()); + } + + + /** + * 监测设置查看权限为 4 的 邮件列表 sql + * @return + */ + public static String getDetecEmailListOf4Sql(){ + StringBuilder sb = new StringBuilder(); + sb.append(" select distinct nt.seq_id ,xyj.email"); + sb.append(" from xt_yh_jbxx xyj "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xyji.yhid = xyj.yhid "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join gorup_system_table gst "); + sb.append(" on gst.user_group_id = xjj.jsbh "); + sb.append(" left join system_table st "); + sb.append(" on st.system_id = gst.system_id "); + sb.append(" left join node_table nt "); + sb.append(" on nt.system_id = gst.system_id "); + sb.append(" left join nodegroup_table ngt "); + sb.append(" on ngt.group_id = nt.node_group_id "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and nt.node_state = 0 "); + sb.append(" and ngt.is_valid = 1 "); + sb.append(" and st.system_state = 0 "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + return sb.toString(); + } + + /** + * detection_info 表的 insert 语句 + * @return + */ + public static String getInsertDetectInfoSql(){ + String insertInfoSql=""; + if(Common.SEQUENCE.get(DetectInfo.DETECTION_INFO)==null){ + insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " + + "PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " + + "POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " + + "DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT)" + +" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?)"; + }else{ + insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " + + "PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " + + "POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " + + "DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT,SEQUENCE)" + +" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?,?)"; + } + return OracleToMysql.trans(insertInfoSql); + } + + /** + * detection_warning 表的insert 语句 + * @return + */ + public static String getInsertWarningSql(){ + //插入warning表 + String insertWaringSql = "INSERT INTO DETECTION_INFO_WARNING (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " + + "PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " + + "POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " + + "DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT)" + +" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?)"; + return OracleToMysql.trans(insertWaringSql); + } + + /** + * email_info 表的insert语句 + * @return + */ + public static String getInsertEmailSql(){ + //插入email_table表 + String insertEmailSql = " insert into email_table (to_address,action_type,content,send_flag,action_ip,action_date,action_desc,send_level,CREATE_TIME)" + + " values(?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))"; + return OracleToMysql.trans(insertEmailSql); + } + + /** + * 更新状态变更时间 + * @return + */ + public static String getUpdateStatusSql(){ + String sql = "update detection_info_new din set din.status_change_time =to_date(?,'yyyy-mm-dd hh24:mi:ss') where din.seq_id=?"; + return OracleToMysql.trans(sql); + } + + public static String getSaveToNewSql(){ + String sql=" insert into detection_info_new(detection_set_info_id ,detection_state_info ," + + "performace_data ,current_times ,start_time ,wait_time ,delay_time ,next_check_time ," + + "police_level ,data_check_time ,data_arrive_time ,detectioned_state ," + + "status_change_time ,seq_id ,detection_info_id,data_check_time_digital,data_arrive_time_digital ," + + "POLICE_EMERGENT ) VALUES" + +"(?,?," + + "?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss')," + + "?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),to_date(?,'yyyy-mm-dd hh24:mi:ss'),?," + + "to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,?,?,?)"; + return OracleToMysql.trans(sql); + } + + public static String getUpdateToNewSql(){ + String sql=" update detection_info_new din set " + + +"din.detection_state_info = ?" + + +", din.performace_data = ?" + + +", din.current_times = ?" + + +", din.start_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')" + + +", din.wait_time = ?" + + +" , din.delay_time = ?" + + +" , din.next_check_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')" + + +" , din.police_level = ?" + + +" , din.data_check_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')" + + +" , din.data_arrive_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')" + + +", din.detectioned_state = ?" + + +" , din.status_change_time = to_date(?,'yyyy-mm-dd hh24:mi:ss')" + + +" , detection_info_id = ?" + + +" ,data_check_time_digital = ?" + + +" ,data_arrive_time_digital = ?" + + +" ,POLICE_EMERGENT = ?" + + +" where din.detection_set_info_id = ?" + + +" and din.seq_id = ?"; + return OracleToMysql.trans(sql); + } + + public static String getChangeWarningInfoSql(){ + String sql="update detection_info_warning set valid =0 where SEQ_ID=? and DETECTION_SET_INFO_ID=? and valid=1"; + return OracleToMysql.trans(sql); + } +} diff --git a/src/com/nms/server/bean/DownloadFileModel.java b/src/com/nms/server/bean/DownloadFileModel.java new file mode 100644 index 0000000..f12783c --- /dev/null +++ b/src/com/nms/server/bean/DownloadFileModel.java @@ -0,0 +1,43 @@ +package com.nms.server.bean; + +import java.util.concurrent.Future; + +/** + * 断点下载文件 数据模板 + * @date Mar 8, 2012 4:33:41 PM + * @author ZhangGang + * + */ +public class DownloadFileModel { + private String fileName; + private String MD5Val; + private Future bpDownFuture; + + public DownloadFileModel(){ + + } + + public DownloadFileModel(String fileName,String MD5Val,Future bpDownFuture){ + this.fileName = fileName; + this.MD5Val = MD5Val; + this.bpDownFuture = bpDownFuture; + } + public String getFileName() { + return fileName; + } + public void setFileName(String fileName) { + this.fileName = fileName; + } + public String getMD5Val() { + return MD5Val; + } + public void setMD5Val(String val) { + MD5Val = val; + } + public Future getBpDownFuture() { + return bpDownFuture; + } + public void setBpDownFuture(Future bpDownFuture) { + this.bpDownFuture = bpDownFuture; + } +} diff --git a/src/com/nms/server/bean/EmailInfo.java b/src/com/nms/server/bean/EmailInfo.java new file mode 100644 index 0000000..42d8653 --- /dev/null +++ b/src/com/nms/server/bean/EmailInfo.java @@ -0,0 +1,148 @@ +package com.nms.server.bean; + + +public class EmailInfo { + /** + * 邮件ID + */ + private Long id; + /** + * 目标地址 + */ + private String toAddress; //接收地址 + /** + * 事件类型 + * 10监测恢复,11监测信息异常,12:监测结果超时, + * 20主动告警恢复,21主动告警异常, + * 31结果失败,32任务状态变更, + * 40系统运行异常恢复,41系统运行异常 + */ + private Integer actionType; // + /** + * 事件标题(具体的监测、任务等名称标题) + */ + private String actionDesc; // + /** + * 事件摘要(监测或任务下的,典型描述) + private String actionDesc2; // + */ + /** + * 事件地点 + */ + private String actionIp; // + /** + * 事件时间 + */ + private String actionDate; // + /** + * 事件内容及结果 + */ + private String content; // + /** + * 发送标示 + * 0未发送 1已发送 + */ + private Integer sendFlag; // + /** + * 发送标示 + * 0紧急发送 1 非紧急 + */ + private Integer sendLevel; // + + public EmailInfo (){ + + } + + public EmailInfo (EmailInfo info){ + this.toAddress = info.getToAddress(); + this.actionType = info.getActionType(); + this.actionDesc = info.getActionDesc(); +// this.actionDesc2 = info.getActionDesc2(); + this.actionIp = info.getActionIp(); + this.actionDate = info.getActionDate(); + this.content = info.getContent(); + this.sendFlag = info.getSendFlag(); + this.sendLevel = info.getSendLevel(); + } + + public EmailInfo (Integer actionType,String actionDesc,/*String actionDesc2,*/String actionIp,String actionDate,String content,Integer sendFlag,Integer sendLevel){ + this.actionType = actionType; + this.actionDesc = actionDesc; +// this.actionDesc2 = actionDesc2; + this.actionIp = actionIp; + this.actionDate = actionDate; + this.content = content; + this.sendFlag = sendFlag; + this.sendLevel = sendLevel; + } + + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public Integer getActionType() { + return actionType; + } + public void setActionType(Integer actionType) { + this.actionType = actionType; + } + public String getActionIp() { + return actionIp; + } + public void setActionIp(String actionIp) { + this.actionIp = actionIp; + } + public String getActionDate() { + return actionDate; + } + public void setActionDate(String actionDate) { + this.actionDate = actionDate; + } + public String getContent() { + return content; + } + public void setContent(String content) { + this.content = content; + } + public Integer getSendFlag() { + return sendFlag; + } + public void setSendFlag(Integer sendFlag) { + this.sendFlag = sendFlag; + } + public String getActionDesc() { + return actionDesc; + } + public void setActionDesc(String actionDesc) { + this.actionDesc = actionDesc; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + /*public String getActionDesc2() { + return actionDesc2; + } + + public void setActionDesc2(String actionDesc2) { + this.actionDesc2 = actionDesc2; + } +*/ + public Integer getSendLevel() { + return sendLevel; + } + + public void setSendLevel(Integer sendLevel) { + this.sendLevel = sendLevel; + } + + +} diff --git a/src/com/nms/server/bean/EmailModel.java b/src/com/nms/server/bean/EmailModel.java new file mode 100644 index 0000000..1ca8413 --- /dev/null +++ b/src/com/nms/server/bean/EmailModel.java @@ -0,0 +1,36 @@ +package com.nms.server.bean; + +public class EmailModel { + private String toAddress; //接收地址 + private String subjectDesc; //主题 + private String content; //邮件内容 + private Integer sendFlag; //0未发送 1已发送 2即时发送 + + + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + public String getSubjectDesc() { + return subjectDesc; + } + public void setSubjectDesc(String subjectDesc) { + this.subjectDesc = subjectDesc; + } + public String getContent() { + return content; + } + public void setContent(String content) { + this.content = content; + } + public Integer getSendFlag() { + return sendFlag; + } + public void setSendFlag(Integer sendFlag) { + this.sendFlag = sendFlag; + } + + +} diff --git a/src/com/nms/server/bean/EventRecordLibrary.java b/src/com/nms/server/bean/EventRecordLibrary.java new file mode 100644 index 0000000..929f721 --- /dev/null +++ b/src/com/nms/server/bean/EventRecordLibrary.java @@ -0,0 +1,128 @@ +package com.nms.server.bean; + +import java.util.Date; + +/** + * EventRecordLibrary entity. + * + * @author MyEclipse Persistence Tools + */ + +public class EventRecordLibrary implements java.io.Serializable { + + // Fields + + /** + * + */ + private static final long serialVersionUID = 1L; + + private Long id; + private String recordType; + private String recordContent; + private Long seqId; + private Long state; + private String recordCommand; + private Date createTime; // 创建时间 +// private String tableName; // 操作表 +// private Long tableId; //表主键 + + // Constructors + + /** default constructor */ + public EventRecordLibrary() { + } + + /** full constructor */ + public EventRecordLibrary(String recordType, String recordContent, + Long seqId, Long state, String recordCommand) { + this.recordType = recordType; + this.recordContent = recordContent; + this.seqId = seqId; + this.state = state; + this.recordCommand = recordCommand; + } + + // Property accessors + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getRecordType() { + return this.recordType; + } + + public void setRecordType(String recordType) { + this.recordType = recordType; + } + + public String getRecordContent() { + return this.recordContent; + } + + public void setRecordContent(String recordContent) { + this.recordContent = recordContent; + } + + public Long getSeqId() { + return seqId; + } + + public void setSeqId(Long seqId) { + this.seqId = seqId; + } + + public Long getState() { + return this.state; + } + + public void setState(Long state) { + this.state = state; + } + + public String getRecordCommand() { + return this.recordCommand; + } + + public void setRecordCommand(String recordCommand) { + this.recordCommand = recordCommand; + } + +// public Long getPid() { +// return pid; +// } +// +// public void setPid(Long pid) { +// this.pid = pid; +// } + + public Date getCreateTime() { + return createTime; + } + + public void setCreateTime(Date createTime) { + this.createTime = createTime; + } + +// public String getTableName() { +// return tableName; +// } +// +// public void setTableName(String tableName) { +// this.tableName = tableName; +// } +// +// public Long getTableId() { +// return tableId; +// } +// +// public void setTableId(Long tableId) { +// this.tableId = tableId; +// } + +} \ No newline at end of file diff --git a/src/com/nms/server/bean/LoopMissionRoundInfo.java b/src/com/nms/server/bean/LoopMissionRoundInfo.java new file mode 100644 index 0000000..efe448b --- /dev/null +++ b/src/com/nms/server/bean/LoopMissionRoundInfo.java @@ -0,0 +1,53 @@ +package com.nms.server.bean; + +import java.util.Date; + +/** + * MissionStateTable entity. + * + * @author MyEclipse Persistence Tools + */ + +public class LoopMissionRoundInfo implements java.io.Serializable { + + /** + * + */ + private static final long serialVersionUID = 8184110460944634042L; + private Long missionId; + private Long curMissionId; + private Long missionState; + private Date startTime; + private Date endTime; + + public Long getMissionId() { + return missionId; + } + public void setMissionId(Long missionId) { + this.missionId = missionId; + } + public Long getCurMissionId() { + return curMissionId; + } + public void setCurMissionId(Long curMissionId) { + this.curMissionId = curMissionId; + } + public Long getMissionState() { + return missionState; + } + public void setMissionState(Long missionSate) { + this.missionState = missionSate; + } + public Date getStartTime() { + return startTime; + } + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + public Date getEndTime() { + return endTime; + } + public void setEndTime(Date endTime) { + this.endTime = endTime; + } +} \ No newline at end of file diff --git a/src/com/nms/server/bean/MissionResult.java b/src/com/nms/server/bean/MissionResult.java new file mode 100644 index 0000000..b7a093b --- /dev/null +++ b/src/com/nms/server/bean/MissionResult.java @@ -0,0 +1,41 @@ +package com.nms.server.bean; + + +public class MissionResult { + private Long missionId;// 任务ID + private String nodeIp;// 节点IP + private Long uuid;// 节点IP + private Long result;// 结果 + private String description;// 结果描述 + public Long getMissionId() { + return missionId; + } + public void setMissionId(Long missionId) { + this.missionId = missionId; + } + public String getNodeIp() { + return nodeIp; + } + public void setNodeIp(String nodeIp) { + this.nodeIp = nodeIp; + } + public Long getResult() { + return result; + } + public void setResult(Long result) { + this.result = result; + } + public Long getUuid() { + return uuid; + } + public void setUuid(Long uuid) { + this.uuid = uuid; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/src/com/nms/server/bean/MissionResult2.java b/src/com/nms/server/bean/MissionResult2.java new file mode 100644 index 0000000..d2e0d61 --- /dev/null +++ b/src/com/nms/server/bean/MissionResult2.java @@ -0,0 +1,118 @@ +package com.nms.server.bean; + +import java.io.Serializable; + + +public class MissionResult2 implements Serializable{ + /** + * + */ + + private static final long serialVersionUID = -2036264131238309952L; + private Long missionId;// 任务ID + private Long missionType;// 节点IP + private Long uuid;// 节点IP + private Long startTime;// 配置截取文本 + private Long EndTime;// 配置截取文本 + private String text;// 配置截取文本 + private Long result;// 结果 + private String description;// 结果描述 + private String fileInfo;// 回传文件 + private Long loopFlag;// 周期标识 0 非周期,1 周期 + private Long resultDetail;//执行结果失败的具体原因,目前只有下发失败41 + + public MissionResult2() { + + } + + public MissionResult2(Long missionId) { + this.missionId = missionId; + } + + public MissionResult2(Long missionId,Long missionType,Long uuid,Long startTime,Long EndTime,String text,Long result,String description,String fileInfo,Long loopFlag) { + this.missionId = missionId;// 任务ID + this.missionType = missionType;;// 节点IP + this.uuid = uuid;;// 节点IP + this.startTime = startTime;;// 配置截取文本 + this.EndTime = EndTime;;// 配置截取文本 + this.text = text;;// 配置截取文本 + this.result = result;;// 结果 + this.description = description;;// 结果描述 + this.fileInfo = fileInfo;;// 结果描述 + this.loopFlag = loopFlag;;// 周期标识 0 非周期,1 周期 + } + + public Long getMissionId() { + return missionId; + } + public void setMissionId(Long missionId) { + this.missionId = missionId; + } + public Long getResult() { + return result; + } + public void setResult(Long result) { + this.result = result; + } + public Long getUuid() { + return uuid; + } + public void setUuid(Long uuid) { + this.uuid = uuid; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public Long getMissionType() { + return missionType; + } + public void setMissionType(Long missionType) { + this.missionType = missionType; + } + public Long getStartTime() { + return startTime; + } + public void setStartTime(Long startTime) { + this.startTime = startTime; + } + public Long getEndTime() { + return EndTime; + } + public void setEndTime(Long endTime) { + EndTime = endTime; + } + public String getText() { + return text; + } + public void setText(String text) { + this.text = text; + } + public String getFileInfo() { + return fileInfo; + } + public void setFileInfo(String fileInfo) { + this.fileInfo = fileInfo; + } + public Long getLoopFlag() { + return loopFlag; + } + public void setLoopFlag(Long loopFlag) { + this.loopFlag = loopFlag; + } + + + public Long getResultDetail() + { + return resultDetail; + } + + + public void setResultDetail(Long resultDetail) + { + this.resultDetail = resultDetail; + } + +} diff --git a/src/com/nms/server/bean/MissionStateTable.java b/src/com/nms/server/bean/MissionStateTable.java new file mode 100644 index 0000000..02301c8 --- /dev/null +++ b/src/com/nms/server/bean/MissionStateTable.java @@ -0,0 +1,152 @@ +package com.nms.server.bean; + +import java.util.Date; + +/** + * MissionStateTable entity. + * + * @author MyEclipse Persistence Tools + */ + +public class MissionStateTable implements java.io.Serializable { + + // Fields + + /** + * + */ + private static final long serialVersionUID = -1342600696641355325L; + private Long missionId; +// private String missionName; + private Long missionType; +// private String missionTypeName; + private Long missionState; +// private Long viewLevel; +// private Long userId; +// private String userIdName; + private Long groupId; + private Long nodeListId; + private String nodeIpsId; +// private Long nodeListId1; + private String nodeGroupsId; +// private String groupIdName; +// private Date createTime; + private Date startTime; + private Date endTime; +// private String failDesc; + private Long systemId; +// private String systemIdName; + private Long loopFlag = 0L; + private Long loopDelay; + + // Constructors + + /** default constructor */ + public MissionStateTable() { + } + + public Long getMissionId() { + return missionId; + } + + public void setMissionId(Long missionId) { + this.missionId = missionId; + } + + public Long getMissionType() { + return missionType; + } + + public void setMissionType(Long missionType) { + this.missionType = missionType; + } + + public Long getMissionState() { + return missionState; + } + + public void setMissionState(Long missionState) { + this.missionState = missionState; + } + + public Long getGroupId() { + return groupId; + } + + public void setGroupId(Long groupId) { + this.groupId = groupId; + } + + public Date getStartTime() { + return startTime; + } + + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + + public Date getEndTime() { + return endTime; + } + + public void setEndTime(Date endTime) { + this.endTime = endTime; + } + + public Long getSystemId() { + return systemId; + } + + public void setSystemId(Long systemId) { + this.systemId = systemId; + } + + public Long getLoopFlag() { + return loopFlag; + } + + public void setLoopFlag(Long loopFlag) { + this.loopFlag = loopFlag; + } + + public Long getLoopDelay() { + return loopDelay; + } + + public void setLoopDelay(Long loopDelay) { + this.loopDelay = loopDelay; + } + + public Long getNodeListId() { + return nodeListId; + } + + public void setNodeListId(Long nodeListId) { + this.nodeListId = nodeListId; + } + +// public Long getNodeListId1() { +// return nodeListId1; +// } +// +// public void setNodeListId1(Long nodeListId1) { +// this.nodeListId1 = nodeListId1; +// } + + public String getNodeGroupsId() { + return nodeGroupsId; + } + + public void setNodeGroupsId(String groupsId) { + this.nodeGroupsId = groupsId; + } + + public String getNodeIpsId() { + return nodeIpsId; + } + + public void setNodeIpsId(String nodeIpsId) { + this.nodeIpsId = nodeIpsId; + } + +} \ No newline at end of file diff --git a/src/com/nms/server/bean/NmsErrorCode.java b/src/com/nms/server/bean/NmsErrorCode.java new file mode 100644 index 0000000..b013899 --- /dev/null +++ b/src/com/nms/server/bean/NmsErrorCode.java @@ -0,0 +1,86 @@ +package com.nms.server.bean; + +/** + * NmsErrorCode entity. + * + * @author MyEclipse Persistence Tools + */ + +public class NmsErrorCode implements java.io.Serializable { + + // Fields + + private Long id; + private String errorCode; + private String errorName; + private String errorDes; + private Long errorLevel; + + // Constructors + + /** default constructor */ + public NmsErrorCode() { + } + + /** minimal constructor */ + public NmsErrorCode(Long id, String errorCode, String errorName, + Long errorLevel) { + this.id = id; + this.errorCode = errorCode; + this.errorName = errorName; + this.errorLevel = errorLevel; + } + + /** full constructor */ + public NmsErrorCode(Long id, String errorCode, String errorName, + String errorDes, Long errorLevel) { + this.id = id; + this.errorCode = errorCode; + this.errorName = errorName; + this.errorDes = errorDes; + this.errorLevel = errorLevel; + } + + // Property accessors + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getErrorCode() { + return this.errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + public String getErrorName() { + return this.errorName; + } + + public void setErrorName(String errorName) { + this.errorName = errorName; + } + + public String getErrorDes() { + return this.errorDes; + } + + public void setErrorDes(String errorDes) { + this.errorDes = errorDes; + } + + public Long getErrorLevel() { + return this.errorLevel; + } + + public void setErrorLevel(Long errorLevel) { + this.errorLevel = errorLevel; + } + +} \ No newline at end of file diff --git a/src/com/nms/server/bean/NmsErrorInfo.java b/src/com/nms/server/bean/NmsErrorInfo.java new file mode 100644 index 0000000..9080fb4 --- /dev/null +++ b/src/com/nms/server/bean/NmsErrorInfo.java @@ -0,0 +1,135 @@ +package com.nms.server.bean; + +import java.util.Date; + +/** + * NmsErrorInfo entity. + * + * @author MyEclipse Persistence Tools + */ + +public class NmsErrorInfo implements java.io.Serializable { + + // Fields + + private Long id; + private String errorCode; + private Date errorTime; + private String errortGetip; + private String errortIp; + private String errortDesc; + private Long errorState; + private Date stateUpdateTime; + private Long stateUpdateUserid; + + // Constructors + + /** default constructor */ + public NmsErrorInfo() { + } + + /** minimal constructor */ + public NmsErrorInfo(Long id) { + this.id = id; + } + + /** full constructor */ +// public NmsErrorInfo(Long id, String errorCode, Date errorTime, +// String errortGetip, String errortIp, Long errorState, +// Date stateUpdateTime, Long stateUpdateUserid,String errortDesc) { +// this.id = id; +// this.errorCode = errorCode; +// this.errorTime = errorTime; +// this.errortGetip = errortGetip; +// this.errortIp = errortIp; +// this.errorState = errorState; +// this.errortDesc = errortDesc; +// this.stateUpdateTime = stateUpdateTime; +// this.stateUpdateUserid = stateUpdateUserid; +// } + + public NmsErrorInfo(String errorCode, Date errorTime, + String errortGetip, String errortIp, Long errorState,String errortDesc) { + this.errorCode = errorCode; + this.errorTime = errorTime; + this.errortGetip = errortGetip; + this.errortIp = errortIp; + this.errorState = errorState; + this.errortDesc = errortDesc; + } + + // Property accessors + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getErrorCode() { + return this.errorCode; + } + + public void setErrorCode(String errorCode) { + this.errorCode = errorCode; + } + + public Date getErrorTime() { + return this.errorTime; + } + + public void setErrorTime(Date errorTime) { + this.errorTime = errorTime; + } + + public String getErrortGetip() { + return this.errortGetip; + } + + public void setErrortGetip(String errortGetip) { + this.errortGetip = errortGetip; + } + + public String getErrortIp() { + return this.errortIp; + } + + public void setErrortIp(String errortIp) { + this.errortIp = errortIp; + } + + public Long getErrorState() { + return this.errorState; + } + + public void setErrorState(Long errorState) { + this.errorState = errorState; + } + + public Date getStateUpdateTime() { + return this.stateUpdateTime; + } + + public void setStateUpdateTime(Date stateUpdateTime) { + this.stateUpdateTime = stateUpdateTime; + } + + public Long getStateUpdateUserid() { + return this.stateUpdateUserid; + } + + public void setStateUpdateUserid(Long stateUpdateUserid) { + this.stateUpdateUserid = stateUpdateUserid; + } + + public String getErrortDesc() { + return errortDesc; + } + + public void setErrortDesc(String errortDesc) { + this.errortDesc = errortDesc; + } + +} \ No newline at end of file diff --git a/src/com/nms/server/bean/NodeModel.java b/src/com/nms/server/bean/NodeModel.java new file mode 100644 index 0000000..b7b92ae --- /dev/null +++ b/src/com/nms/server/bean/NodeModel.java @@ -0,0 +1,131 @@ +package com.nms.server.bean; + + +/** + * NodeTable entity. + * + * @author MyEclipse Persistence Tools + */ + +public class NodeModel implements java.io.Serializable { + + // Fields + + /** + * + */ + private static final long serialVersionUID = 1984053252904108229L; + private Long nodeId; + private String nodeIp; + + /* 节点类型 0服务器 1交换机 */ + private Long nodeType; +// private String nodeName; + + /* 节点状态 0有效 1无效*/ + private Long nodeState; + private Long systemId; + private Long seqId; + private Long groupId; + + + private String nodeUserName; + private String nodePassword; + // Constructors + + /** default constructor */ + public NodeModel() { + } + + public NodeModel(String nodeIp,Long nodeType) { + this.nodeIp = nodeIp; + this.nodeType = nodeType; + } + + /** minimal constructor */ + public NodeModel(Long nodeId, Long nodeState, + Long systemId) { + this.nodeId = nodeId; +// this.nodeName = nodeName; + this.nodeState = nodeState; + this.systemId = systemId; + //this.nodeLatticeId=nodeLatticeId; + } + + + // Property accessors + + public Long getNodeId() { + return this.nodeId; + } + + public void setNodeId(Long nodeId) { + this.nodeId = nodeId; + } + + public Long getNodeState() { + return this.nodeState; + } + + public void setNodeState(Long nodeState) { + this.nodeState = nodeState; + } + + public Long getSystemId() { + return this.systemId; + } + + public void setSystemId(Long systemId) { + this.systemId = systemId; + } + + public String getNodeIp() { + return nodeIp; + } + + public void setNodeIp(String nodeIp) { + this.nodeIp = nodeIp; + } + + public Long getNodeType() { + return nodeType; + } + + public void setNodeType(Long nodeType) { + this.nodeType = nodeType; + } + + public Long getSeqId() { + return seqId; + } + + public void setSeqId(Long seqId) { + this.seqId = seqId; + } + + public Long getGroupId() { + return groupId; + } + + public void setGroupId(Long groupId) { + this.groupId = groupId; + } + + public String getNodeUserName() { + return nodeUserName; + } + + public void setNodeUserName(String nodeUserName) { + this.nodeUserName = nodeUserName; + } + + public String getNodePassword() { + return nodePassword; + } + + public void setNodePassword(String nodePassword) { + this.nodePassword = nodePassword; + } + + +} \ No newline at end of file diff --git a/src/com/nms/server/bean/NodegroupModel.java b/src/com/nms/server/bean/NodegroupModel.java new file mode 100644 index 0000000..2beb069 --- /dev/null +++ b/src/com/nms/server/bean/NodegroupModel.java @@ -0,0 +1,66 @@ +package com.nms.server.bean; + + +/** + * NodegroupTable entity. + * + * @author MyEclipse Persistence Tools + */ + +public class NodegroupModel implements java.io.Serializable { + + // Fields + + /** + * + */ + private static final long serialVersionUID = -5562024412959332727L; + private Long groupId; + private String groupName; + private Long groupType; + private Long systemId; + private Long isValid;//0是失效,1是生效 + + // Property accessors + + public Long getGroupId() { + return this.groupId; + } + + public void setGroupId(Long groupId) { + this.groupId = groupId; + } + + public String getGroupName() { + return this.groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + public Long getGroupType() { + return this.groupType; + } + + public void setGroupType(Long groupType) { + this.groupType = groupType; + } + + public void setSystemId(Long systemId) { + this.systemId = systemId; + } + + public Long getIsValid() { + return this.isValid; + } + + public void setIsValid(Long isValid) { + this.isValid = isValid; + } + + public Long getSystemId() { + return systemId; + } + +} \ No newline at end of file diff --git a/src/com/nms/server/bean/ReturnFilePO.java b/src/com/nms/server/bean/ReturnFilePO.java new file mode 100644 index 0000000..6c138cd --- /dev/null +++ b/src/com/nms/server/bean/ReturnFilePO.java @@ -0,0 +1,106 @@ +package com.nms.server.bean; + +import java.util.Date; + +public class ReturnFilePO { + /** + * ID + */ + private Long taskId; + /** + * 类型:4 命令执行,6 升级 + */ + private long taskType; + /** + * 唯一标志一台物理机(类似于IP) + */ + private long uuid; + /** + * 回传路径集: key 文件实际路径 value 文件别名 + */ +// private Map filePathMap; + /** + * 当前要回传的文件 + */ +// private File curRetrunFile; + /** + * 回传文件名(统一处理后一个任务只有一个回传文件) + */ + private String returnFileName; + /** + * 回传状态:0 成功,1 失败 + */ + private long state; + /** + * 开始时间 + */ + private Date startTime; + /** + * 结束时间 + */ + private Date endTime; + /** + * 是否循环任务 + */ + private long isLoop; + /** + * 回传文件的结果描述 + */ + private String resDesc; + + public Long getTaskId() { + return taskId; + } + public void setTaskId(Long taskId) { + this.taskId = taskId; + } + public long getTaskType() { + return taskType; + } + public void setTaskType(long taskType) { + this.taskType = taskType; + } + public long getState() { + return state; + } + public void setState(long state) { + this.state = state; + } + public Date getStartTime() { + return startTime; + } + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + public Date getEndTime() { + return endTime; + } + public void setEndTime(Date endTime) { + this.endTime = endTime; + } + public long getIsLoop() { + return isLoop; + } + public void setIsLoop(long isLoop) { + this.isLoop = isLoop; + } + public String getResDesc() { + return resDesc; + } + public void setResDesc(String resDesc) { + this.resDesc = resDesc; + } + public String getReturnFileName() { + return returnFileName; + } + public void setReturnFileName(String returnFileName) { + this.returnFileName = returnFileName; + } + public long getUuid() { + return uuid; + } + public void setUuid(long uuid) { + this.uuid = uuid; + } + +} diff --git a/src/com/nms/server/bean/ServerIpSegment.java b/src/com/nms/server/bean/ServerIpSegment.java new file mode 100644 index 0000000..053dece --- /dev/null +++ b/src/com/nms/server/bean/ServerIpSegment.java @@ -0,0 +1,32 @@ +package com.nms.server.bean; + +public class ServerIpSegment { + public String startIp; + public Long startIpn; + public String endIp; + public Long endIpn; + public String getStartIp() { + return startIp; + } + public void setStartIp(String startIp) { + this.startIp = startIp; + } + public Long getStartIpn() { + return startIpn; + } + public void setStartIpn(Long startIpn) { + this.startIpn = startIpn; + } + public String getEndIp() { + return endIp; + } + public void setEndIp(String endIp) { + this.endIp = endIp; + } + public Long getEndIpn() { + return endIpn; + } + public void setEndIpn(Long endIpn) { + this.endIpn = endIpn; + } +} diff --git a/src/com/nms/server/bean/ServerTable.java b/src/com/nms/server/bean/ServerTable.java new file mode 100644 index 0000000..de41279 --- /dev/null +++ b/src/com/nms/server/bean/ServerTable.java @@ -0,0 +1,121 @@ +package com.nms.server.bean; + +import java.util.LinkedList; +import java.util.List; + +/** + * ServerTable entity. + * + * @author MyEclipse Persistence Tools + */ + +public class ServerTable implements java.io.Serializable { + + // Fields + + /** + * + */ + private static final long serialVersionUID = 1L; + private Long id; //数据库DC 注册ID + private String serverName; //当前DC名称 + private Long serverState; //当前DC状态 + private String serverIp; //当前DC IP + private Long serverIpn; //当前DC IPN + private int index; //当前dc 在数据库的排序 + private int conut; //当前dc 总数 + //- 当前DC 的IP管理范围(空时 过滤条件为空,即为管理全部 注意) + private List ipSegList = new LinkedList(); + + // Constructors + + /** default constructor */ + public ServerTable() { + } + + /** full constructor */ + public ServerTable(String serverName, Long serverState, String serverIp, + Long serverIpn) { + this.serverName = serverName; + this.serverState = serverState; + this.serverIp = serverIp; + this.serverIpn = serverIpn; + + } + + /** All constructor */ + public ServerTable(Long id,String serverName, Long serverState, String serverIp, + Long serverIpn) { + this.id = id; + this.serverName = serverName; + this.serverState = serverState; + this.serverIp = serverIp; + this.serverIpn = serverIpn; + } + // Property accessors + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getServerName() { + return this.serverName; + } + + public void setServerName(String serverName) { + this.serverName = serverName; + } + + public Long getServerState() { + return this.serverState; + } + + public void setServerState(Long serverState) { + this.serverState = serverState; + } + + public String getServerIp() { + return this.serverIp; + } + + public void setServerIp(String serverIp) { + this.serverIp = serverIp; + } + + public Long getServerIpn() { + return this.serverIpn; + } + + public void setServerIpn(Long serverIpn) { + this.serverIpn = serverIpn; + } + + public List getIpSegList() { + return ipSegList; + } + + public void setIpSegList(List ipSegList) { + this.ipSegList = ipSegList; + } + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } + + public int getConut() { + return conut; + } + + public void setConut(int conut) { + this.conut = conut; + } + +} \ No newline at end of file diff --git a/src/com/nms/server/bean/SetInfo.java b/src/com/nms/server/bean/SetInfo.java new file mode 100644 index 0000000..7acfc8e --- /dev/null +++ b/src/com/nms/server/bean/SetInfo.java @@ -0,0 +1,277 @@ +package com.nms.server.bean; + +import org.apache.commons.lang.builder.ReflectionToStringBuilder; + +/** + * 客户端用到的监测设置信息实体 + * + */ +/** + * @author chuan + * + */ +public class SetInfo { + /** + * 监测设置信息ID + */ + private Long id; + + /** + * 检测类型 + */ + private String checkTypeName;//如:CPU、DISK等 + private Long checkTypeId;//检测类型的ID,预留 + + /** + * 目标IP + */ +// private String nodeIp;// + + /** + * 最大测试次数 + */ + private Long checkMaxTimes; + /** + * 节点组ID + */ +// private Long groupId; + /** + * + */ + private String nodeGroupsId; + /** + * 最大测试次数 + */ + private String nodeIpsId; + /** + * 时间间隔(单位:分钟) + */ + private Long checkGap; + /** + * 超时时间(单位:秒) + */ + private Long checkOutTime; + /** + * 监测状态:0无效;1有效 + */ + private String checkState; + /** + * 监测方式:0主动,1被动 + */ + private String checkWay; + /** + * 进程标志 + */ + private String processIden; + /** + * 进程执行文件 + */ + private String processFile; + /** + * 进程执行文件路径 + */ + private String processPath; + /** + * 是否系统启动(NMSAgent启动/第三方自己启动);默认0自启动;1NMSAgent启动 + */ + private String IsControlStart; +/* *//** + * 是否需下发文件:0需要;1不需要 默认1 + *//* + private String isIssueFile; + *//** + * 文件下发是否成功;0成功;1不成功 默认1 + *//* + private String isIssued; + *//** + * 是否下发设置:0下发;1未下发 默认值为1如新添加或者修改一项设置之后设置为未下发,需要完成其他配置信息后才执行一次下发 + *//* + private String isIssue;*/ + /** + * 控制启动时间 + */ + private Long controlStartTime; + /** + *上传数据时间间隔单位分钟:不能为空,默认15分钟。监测数据上传到NMSServer周期。 + */ + private Long uploadGap; + + /** + * 计划检测时间:针对当前配置信息首次执行时间 + */ + private Long planCheckTime; + + /** + * 是否预置监测 + */ + private String isSchedule; + /** + * 监测级别 + */ + private Long viewLevel; + + /** + * 指定IP操作 + */ + private String nodeIp2; //指定单个IP + + /** + * 进程搜索关键字 + */ + private String processSearchKeyCode; //指定单个IP + + /** + * 是否SNMP检测 0:objectSNMP 1:SNMP4j + */ + private Long isSNMP; + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public String getCheckTypeName() { + return checkTypeName; + } + public void setCheckTypeName(String checkTypeName) { + this.checkTypeName = checkTypeName; + } + public Long getCheckTypeId() { + return checkTypeId; + } + public Long getCheckMaxTimes() { + return checkMaxTimes; + } + public void setCheckMaxTimes(Long checkMaxTimes) { + this.checkMaxTimes = checkMaxTimes; + } + public Long getCheckGap() { + return checkGap; + } + public void setCheckGap(Long checkGap) { + this.checkGap = checkGap; + } + public Long getCheckOutTime() { + return checkOutTime; + } + public void setCheckOutTime(Long checkOutTime) { + this.checkOutTime = checkOutTime; + } + public String getCheckState() { + return checkState; + } + public void setCheckState(String checkState) { + this.checkState = checkState; + } + public String getProcessIden() { + return processIden; + } + public void setProcessIden(String processIden) { + this.processIden = processIden; + } + public String getProcessFile() { + return processFile; + } + public void setProcessFile(String processFile) { + this.processFile = processFile; + } + public String getProcessPath() { + return processPath; + } + public void setProcessPath(String processPath) { + this.processPath = processPath; + } + public void setCheckTypeId(Long checkTypeId) { + this.checkTypeId = checkTypeId; + } + public String getCheckWay() { + return checkWay; + } + public void setCheckWay(String checkWay) { + this.checkWay = checkWay; + } + + +@Override + public String toString() { + return ReflectionToStringBuilder.toString(this); + } + // public String getNodeIp() { +// return nodeIp; +// } +// public void setNodeIp(String nodeIp) { +// this.nodeIp = nodeIp; +// } + public String getIsControlStart() { + return IsControlStart; + } + public void setIsControlStart(String isControlStart) { + IsControlStart = isControlStart; + } + public Long getControlStartTime() { + return controlStartTime; + } + public void setControlStartTime(Long controlStartTime) { + this.controlStartTime = controlStartTime; + } + public Long getPlanCheckTime() { + return planCheckTime; + } + public void setPlanCheckTime(Long planCheckTime) { + this.planCheckTime = planCheckTime; + } + public Long getUploadGap() { + return uploadGap; + } + public void setUploadGap(Long uploadGap) { + this.uploadGap = uploadGap; + } + public String getIsSchedule() { + return isSchedule; + } + public void setIsSchedule(String isSchedule) { + this.isSchedule = isSchedule; + } +// public Long getGroupId() { +// return groupId; +// } +// public void setGroupId(Long groupId) { +// this.groupId = groupId; +// } + public Long getIsSNMP() { + return isSNMP; + } + public void setIsSNMP(Long isSNMP) { + this.isSNMP = isSNMP; + } + public String getNodeGroupsId() { + return nodeGroupsId; + } + public void setNodeGroupsId(String nodeGroupsId) { + this.nodeGroupsId = nodeGroupsId; + } + public String getNodeIpsId() { + return nodeIpsId; + } + public void setNodeIpsId(String nodeIpsId) { + this.nodeIpsId = nodeIpsId; + } + public String getNodeIp2() { + return nodeIp2; + } + public void setNodeIp2(String nodeIp2) { + this.nodeIp2 = nodeIp2; + } + public Long getViewLevel() { + return viewLevel; + } + public void setViewLevel(Long viewLevel) { + this.viewLevel = viewLevel; + } + public String getProcessSearchKeyCode() { + return processSearchKeyCode; + } + public void setProcessSearchKeyCode(String processSearchKeyCode) { + this.processSearchKeyCode = processSearchKeyCode; + } +} diff --git a/src/com/nms/server/bean/SimpleNode.java b/src/com/nms/server/bean/SimpleNode.java new file mode 100644 index 0000000..6d9ea08 --- /dev/null +++ b/src/com/nms/server/bean/SimpleNode.java @@ -0,0 +1,29 @@ +package com.nms.server.bean; + +/** + * + * 简单节点对象 + * @date Jun 18, 2013 3:51:28 PM + * @author ZhangGang + * + */ +public class SimpleNode { + private String nodeIp; //节点IP + private Integer snmpVersion; // 节点SNMP版本 version1 version2c version3 + public SimpleNode(String nodeIp,Integer snmpVersion){ + this.nodeIp = nodeIp; + this.snmpVersion = snmpVersion; + } + public String getNodeIp() { + return nodeIp; + } + public void setNodeIp(String nodeIp) { + this.nodeIp = nodeIp; + } + public Integer getSnmpVersion() { + return snmpVersion; + } + public void setSnmpVersion(Integer snmpVersion) { + this.snmpVersion = snmpVersion; + } +} diff --git a/src/com/nms/server/bean/TableColumnsModel.java b/src/com/nms/server/bean/TableColumnsModel.java new file mode 100644 index 0000000..8fdf485 --- /dev/null +++ b/src/com/nms/server/bean/TableColumnsModel.java @@ -0,0 +1,57 @@ +package com.nms.server.bean; + +/** + * 表字段详细信息 + * @date Jun 18, 2013 4:02:44 PM + * @author ZhangGang + * + */ +public class TableColumnsModel { + + private String filedName; //字段名称 表字段名称 + private String filedComments; //字段描述 中文名称 + private String filedType; //字段类型 + private String oid; //字段OID 用于snmp +// private Integer showNum; + + public TableColumnsModel() { + + } + public TableColumnsModel(String filedName,String filedComments,String filedType,String oid) { + this.filedName = filedName; + this.filedComments = filedComments; + this.filedType = filedType; + this.oid = oid; +// this.showNum = showNum; + } + public String getFiledName() { + return filedName; + } + public void setFiledName(String filedName) { + this.filedName = filedName; + } + public String getFiledComments() { + return filedComments; + } + public void setFiledComments(String filedComments) { + this.filedComments = filedComments; + } + public String getFiledType() { + return filedType; + } + public void setFiledType(String filedType) { + this.filedType = filedType; + } + public String getOid() { + return oid; + } + public void setOid(String oid) { + this.oid = oid; + } +// public Integer getShowNum() { +// return showNum; +// } +// public void setShowNum(Integer showNum) { +// this.showNum = showNum; +// } +} diff --git a/src/com/nms/server/bean/TableModel.java b/src/com/nms/server/bean/TableModel.java new file mode 100644 index 0000000..1e67a7e --- /dev/null +++ b/src/com/nms/server/bean/TableModel.java @@ -0,0 +1,39 @@ +package com.nms.server.bean; + +/** + * 监测类别信息 + * @date Jun 18, 2013 4:04:15 PM + * @author ZhangGang + * + */ +public class TableModel { + private String tableName ; //表名称 库表名称 + private String oid ; // 监测类别对应的OID + private Long isSnmp; // 监测类别是否是snmp + public TableModel (){ + + } + public TableModel (String tableName,String oid,Long isSnmp){ + this.tableName = tableName; + this.oid = oid; + this.isSnmp = isSnmp; + } + public String getTableName() { + return tableName; + } + public void setTableName(String tableName) { + this.tableName = tableName; + } + public String getOid() { + return oid; + } + public void setOid(String oid) { + this.oid = oid; + } + public Long getIsSnmp() { + return isSnmp; + } + public void setIsSnmp(Long isSnmp) { + this.isSnmp = isSnmp; + } +} diff --git a/src/com/nms/server/bean/Task1.java b/src/com/nms/server/bean/Task1.java new file mode 100644 index 0000000..6f52497 --- /dev/null +++ b/src/com/nms/server/bean/Task1.java @@ -0,0 +1,70 @@ +package com.nms.server.bean; + + +/** + * 任务实体类:1 文件推送 + * + */ +public class Task1 { + /** + * 任务ID + */ + private Long taskId; + /** + * 任务类型:1 文件推送,2 非流文本数据获取,3 流文本数据获取,4 命令执行,5 shell注册 + */ + private long taskType; + /** + * 节点组Id + */ + private Long nodeListId; + private String nodeIpsId; +// private Long nodeListId1; + private String nodeGroupsId; + /** + * 节点组Id + */ + private String taskParam; + public Long getTaskId() { + return taskId; + } + public void setTaskId(Long taskId) { + this.taskId = taskId; + } + public long getTaskType() { + return taskType; + } + public void setTaskType(long taskType) { + this.taskType = taskType; + } + public Long getNodeListId() { + return nodeListId; + } + public void setNodeListId(Long nodeListId) { + this.nodeListId = nodeListId; + } + public String getTaskParam() { + return taskParam; + } + public void setTaskParam(String taskParam) { + this.taskParam = taskParam; + } + public String getNodeGroupsId() { + return nodeGroupsId; + } + public void setNodeGroupsId(String groupsId) { + this.nodeGroupsId = groupsId; + } +// public Long getNodeListId1() { +// return nodeListId1; +// } +// public void setNodeListId1(Long nodeListId1) { +// this.nodeListId1 = nodeListId1; +// } + public String getNodeIpsId() { + return nodeIpsId; + } + public void setNodeIpsId(String nodeIpsId) { + this.nodeIpsId = nodeIpsId; + } +} diff --git a/src/com/nms/server/bean/Task4.java b/src/com/nms/server/bean/Task4.java new file mode 100644 index 0000000..aaac376 --- /dev/null +++ b/src/com/nms/server/bean/Task4.java @@ -0,0 +1,165 @@ +package com.nms.server.bean; + + +/** + * 任务实体类:4 命令执行 + * + */ +public class Task4 { + /** + * 任务ID + */ + private Long taskId; + /** + * 任务类型:1 文件推送,2 非流文本数据获取,3 流文本数据获取,4 命令执行,5 shell注册 + */ + private long taskType; + /** + * 命令类型:1 Agent原生支持命令,2 脚本,3 shell命令 + */ + private long commandType; + /** + * 命令名称 + */ + private String commandName; + /** + * 命令参数 + */ + private String commandParam; + /** + * 脚本路径 + */ + private String scriptPath; + /** + * 执行状态:4下发任务(40 下发成功,41下发失败),5杀进程(50成功,51失败)、6备份、7更新(覆盖) 、8启动进程 + */ + private long state; + /** + * 执行状态:0 周期执行,5撤销执行 + */ + private Long missionState; + /** + * 如果是升级,旧的版本 + */ + private Long version; + /** + * 节点组Id + */ + private Long nodeListId; + private String nodeIpsId; + private String nodeGroupsId; + /** + * 执行时间 + */ + private Long startTime; + /** + * 结束时间 + */ + private Long endTime; + /** + * 是否循环任务 + */ + private Long isLoop; + /** + * 如果是循环任务,循环周期 + */ + private Long loopDelay; + public Long getTaskId() { + return taskId; + } + public void setTaskId(Long taskId) { + this.taskId = taskId; + } + public long getTaskType() { + return taskType; + } + public void setTaskType(long taskType) { + this.taskType = taskType; + } + public long getCommandType() { + return commandType; + } + public void setCommandType(long commandType) { + this.commandType = commandType; + } + public String getCommandName() { + return commandName; + } + public void setCommandName(String commandName) { + this.commandName = commandName; + } + public String getCommandParam() { + return commandParam; + } + public void setCommandParam(String commandParam) { + this.commandParam = commandParam; + } + public String getScriptPath() { + return scriptPath; + } + public void setScriptPath(String scriptPath) { + this.scriptPath = scriptPath; + } + public long getState() { + return state; + } + public void setState(long state) { + this.state = state; + } + public Long getMissionState() { + return missionState; + } + public void setMissionState(Long missionState) { + this.missionState = missionState; + } + public Long getVersion() { + return version; + } + public void setVersion(Long version) { + this.version = version; + } + public Long getNodeListId() { + return nodeListId; + } + public void setNodeListId(Long nodeListId) { + this.nodeListId = nodeListId; + } + public String getNodeIpsId() { + return nodeIpsId; + } + public void setNodeIpsId(String nodeIpsId) { + this.nodeIpsId = nodeIpsId; + } + public String getNodeGroupsId() { + return nodeGroupsId; + } + public void setNodeGroupsId(String nodeGroupsId) { + this.nodeGroupsId = nodeGroupsId; + } + public Long getStartTime() { + return startTime; + } + public void setStartTime(Long startTime) { + this.startTime = startTime; + } + public Long getEndTime() { + return endTime; + } + public void setEndTime(Long endTime) { + this.endTime = endTime; + } + public Long getIsLoop() { + return isLoop; + } + public void setIsLoop(Long isLoop) { + this.isLoop = isLoop; + } + public Long getLoopDelay() { + return loopDelay; + } + public void setLoopDelay(Long loopDelay) { + this.loopDelay = loopDelay; + } + + +} diff --git a/src/com/nms/server/bean/Task6.java b/src/com/nms/server/bean/Task6.java new file mode 100644 index 0000000..a7497cb --- /dev/null +++ b/src/com/nms/server/bean/Task6.java @@ -0,0 +1,181 @@ +package com.nms.server.bean; + + +/** + * 任务实体类:6 升级 + * + */ +public class Task6 { + /** + * 任务ID + */ + private Long taskId; + /** + * 任务类型:1 文件推送,4 命令执行,5 shell注册,6 升级 + */ + private long taskType; + /** + * 命令类型:4 Agent原生支持命令 + */ + private long commandType; + /** + * 命令名称 + */ + private String commandName; + /** + * 命令参数 + */ + private String commandParam; + /** + * 执行状态:4下发任务(40 下发成功,41下发失败),5杀进程(50成功,51失败)、6备份、7更新(覆盖) 、8启动进程 + */ + private Long state; + /** + * 当前版本 + */ + private Long version; + /** + * 节点组Id(Agent无用) + */ + private Long nodeListId; + private String nodeIpsId; +// private Long nodeListId1; + private String nodeGroupsId; + /** + * 升级时间 + */ + private Long upgradeTime; + /** + * 推送文件名 + */ + private String fileName; + /** + * 推送目的地 + */ + private String destPath; + /** + * 推送文件的MD5值 + */ + private String md5Value; + /** + * 全局权限用户名(未来) + */ + private String username; + /** + * 全局权限密码(未来 约定加密方式) + */ + private String userpwd; + /** + * 重新执行,原任务ID + */ + private Long oldTaskId; + public Long getTaskId() { + return taskId; + } + public void setTaskId(Long taskId) { + this.taskId = taskId; + } + public long getTaskType() { + return taskType; + } + public void setTaskType(long taskType) { + this.taskType = taskType; + } + public long getCommandType() { + return commandType; + } + public void setCommandType(long commandType) { + this.commandType = commandType; + } + public String getCommandName() { + return commandName; + } + public void setCommandName(String commandName) { + this.commandName = commandName; + } + public String getCommandParam() { + return commandParam; + } + public void setCommandParam(String commandParam) { + this.commandParam = commandParam; + } + public Long getState() { + return state; + } + public void setState(Long state) { + this.state = state; + } + public Long getVersion() { + return version; + } + public void setVersion(Long version) { + this.version = version; + } + public Long getNodeListId() { + return nodeListId; + } + public void setNodeListId(Long nodeListId) { + this.nodeListId = nodeListId; + } + public Long getUpgradeTime() { + return upgradeTime; + } + public void setUpgradeTime(Long upgradeTime) { + this.upgradeTime = upgradeTime; + } + public String getFileName() { + return fileName; + } + public void setFileName(String fileName) { + this.fileName = fileName; + } + public String getDestPath() { + return destPath; + } + public void setDestPath(String destPath) { + this.destPath = destPath; + } + public String getMd5Value() { + return md5Value; + } + public void setMd5Value(String md5Value) { + this.md5Value = md5Value; + } + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + public String getUserpwd() { + return userpwd; + } + public void setUserpwd(String userpwd) { + this.userpwd = userpwd; + } + public Long getOldTaskId() { + return oldTaskId; + } + public void setOldTaskId(Long oldTaskId) { + this.oldTaskId = oldTaskId; + } + public String getNodeGroupsId() { + return nodeGroupsId; + } + public void setNodeGroupsId(String groupsId) { + this.nodeGroupsId = groupsId; + } +// public Long getNodeListId1() { +// return nodeListId1; +// } +// public void setNodeListId1(Long nodeListId1) { +// this.nodeListId1 = nodeListId1; +// } +// + public String getNodeIpsId() { + return nodeIpsId; + } + public void setNodeIpsId(String nodeIpsId) { + this.nodeIpsId = nodeIpsId; + } +} diff --git a/src/com/nms/server/bean/TrapMessageInfo.java b/src/com/nms/server/bean/TrapMessageInfo.java new file mode 100644 index 0000000..f75374d --- /dev/null +++ b/src/com/nms/server/bean/TrapMessageInfo.java @@ -0,0 +1,162 @@ +package com.nms.server.bean; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; + +import org.snmp4j.PDU; +import org.snmp4j.smi.Variable; + +public class TrapMessageInfo +{ + + private String agentSendIP; + private String pduAgentIP; + private Timestamp receiverTime; + private int trapVersion; + private int trapV1GenericType; + private int trapV1SpecificType; + private String trapName; + private String trapOID; + private List trapPDUOIDs; + private List trapPDUOIDValues; + private PDU originalPDU; + public static final int TrapVersionV1 = 1; + public static final int TrapVersionV2 = 2; + + public TrapMessageInfo() + { + agentSendIP = ""; + pduAgentIP = ""; + receiverTime = new Timestamp(System.currentTimeMillis()); + trapVersion = 1; + trapV1GenericType = -1; + trapV1SpecificType = -1; + trapName = ""; + trapOID = ""; + trapPDUOIDs = new ArrayList(); + trapPDUOIDValues = new ArrayList(); + } + + public Variable getOIDValue(String s) + { + for (int i = 0; i < trapPDUOIDs.size(); i++) + { + String s1 = (String)trapPDUOIDs.get(i); + if (s.equals(s1)) + return (Variable)trapPDUOIDValues.get(i); + } + + return null; + } + + public String getAgentSendIP() + { + return agentSendIP; + } + + public void setAgentSendIP(String s) + { + agentSendIP = s; + } + + public Timestamp getReceiverTime() + { + return receiverTime; + } + + public void setReceiverTime(Timestamp timestamp) + { + receiverTime = timestamp; + } + + public int getTrapVersion() + { + return trapVersion; + } + + public void setTrapVersion(int i) + { + trapVersion = i; + } + + public String getTrapName() + { + return trapName; + } + + public void setTrapName(String s) + { + trapName = s; + } + + public String getTrapOID() + { + return trapOID; + } + + public void setTrapOID(String s) + { + trapOID = s; + } + + public String getPduAgentIP() + { + return pduAgentIP; + } + + public void setPduAgentIP(String s) + { + pduAgentIP = s; + } + + public int getTrapV1GenericType() + { + return trapV1GenericType; + } + + public void setTrapV1GenericType(int i) + { + trapV1GenericType = i; + } + + public int getTrapV1SpecificType() + { + return trapV1SpecificType; + } + + public void setTrapV1SpecificType(int i) + { + trapV1SpecificType = i; + } + + public List getTrapPDUOIDs() + { + return trapPDUOIDs; + } + + public void setTrapPDUOIDs(List list) + { + trapPDUOIDs = list; + } + + public List getTrapPDUOIDValues() + { + return trapPDUOIDValues; + } + + public void setTrapPDUOIDValues(List list) + { + trapPDUOIDValues = list; + } + + public void setOriginalPDU(PDU pdu) + { + originalPDU = pdu; + } + + public PDU getOriginalPDU() + { + return originalPDU; + } +} diff --git a/src/com/nms/server/common/Common.java b/src/com/nms/server/common/Common.java new file mode 100644 index 0000000..868c655 --- /dev/null +++ b/src/com/nms/server/common/Common.java @@ -0,0 +1,2798 @@ +package com.nms.server.common; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.nio.charset.Charset; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.snmp4j.Snmp; + +import com.fang.U; +import com.fang.lang.Db; +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.LoopMissionRoundInfo; +import com.nms.server.bean.MissionResult2; +import com.nms.server.bean.MissionStateTable; +import com.nms.server.bean.NmsErrorInfo; +import com.nms.server.bean.ServerIpSegment; +import com.nms.server.bean.ServerTable; +import com.nms.server.bean.SetInfo; +import com.nms.server.bean.SimpleNode; +import com.nms.server.bean.TableColumnsModel; +import com.nms.server.bean.TableModel; +import com.nms.server.dao.CommonDao; +import com.nms.server.thread.alarmData.AlarmDataResoveThread; +import com.nms.server.thread.detecData.NewDetecDataResoveThread; +import com.nms.server.thread.detectDataHandler.DataInsertManagerThread; +import com.nms.server.thread.detectDataHandler.DetectInfoInsertThread; +import com.nms.server.thread.mission.MissionPollingThread; +import com.nms.server.thread.mission.MissionResultThread; +import com.nms.server.thread.monitor.util.MonitorUtil; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.DateUtil; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.FileUtils; +import com.nms.server.util.IpCovert; +import com.nms.server.util.SNMP4JUtils; +import com.nms.server.util.StringUtil; +import com.nms.server.util.socket.SSLSocketCallable; +import com.nms.server.util.socket.SocketUtils; + +import ch.ethz.ssh2.Connection; +import ch.ethz.ssh2.Session; +import ch.ethz.ssh2.StreamGobbler; + +/** + * DataController公共方法类

+ * DataController运行正常必要的

+ * *线程池、锁、信号计数器及公共数据集合的操作

+ * *部分公用工具方法

+ * *不涉及数据库

+ * ****方法数量较多,涉及广泛,部分方法更涉及到业务方面,优化规整**** + * @author ZGGG3 + * + */ +public class Common { + private static final Logger logger = Logger.getLogger(Common.class); + + /** + * 程序运行标识 + */ + private volatile static boolean STOP = false; + + /** + * 数据库操作工具类 + */ + private static Db db; + + /** + * 缓存解析之后的数据 + * key: info,checkType + */ + public static final Hashtable DETECT_QUEUE = new Hashtable(); + + /** + * 保存心跳信息 + */ + public static final Hashtable> HEARTBEAT_IPS = new Hashtable<>(); + + + //类加载器 +// private static URLClassLoader classLoader = null; + + //#--- DataController 启动成功标志 false 未启动 true 启动成功 默认false //-废弃 +// private static boolean SYSTEM_INIT_FLAG = false; + + /* 数据库可连接状态 */ + public static boolean DB_CONNECT_FLAG = true; + + //#--- 当前 DataController 数据库信息 ------- + private static ServerTable serverTable= null; + + //#--- Executor Services ------- + + + /** + * 线程代码编写注意: + * 1、线程run()或 call()方法产生的异常无法捕获,所以需要对run()和call()方法中的所有代码尽量捕获异常; + * 2、不采取捕获的线程,在周期线程池中执行时,如果抛出异常,则该周期任务将被 canceled ,不会再次执行,所以为保证周期任务的稳定运行,尽可能的catch run()的异常 + * */ + //-- 非周期执行线程池 用于单次执行线程的运行 + public static final ExecutorService service = Executors.newFixedThreadPool(Constants.EXECUTOR_SOCKET_THREAD_SIZE); + public static final ExecutorService dataResoveService = Executors.newFixedThreadPool(Constants.EXECUTOR_DATA_RESOVE_THREAD_SIZE); + //-- 定时周期执行线程池 用于周期执行线程的运行 + public static final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(Constants.EXECUTOR_SCHEDULED_THREAD_SIZE); + //-- DataController 程序控制线程池 非守护线程: 独立的执行DC自身监测或优化等操作 + public static final ScheduledExecutorService dcPrivScheduled = Executors.newSingleThreadScheduledExecutor(); + + /** + * 管理 周期线程的集合 Map<线程名称, 线程Future> 用于停用周期线程操作 + * + * 涉及到方法 线程注册和撤销以及判断线程是否已注册 + * */ + private static final Map> futureMap = new HashMap>(); //定时任务变更或取消控制集合 +// private static final Map monitorThreadMap = new HashMap(); //定时任务变更或取消控制集合 + + // #--- Node IP & UUID Map ------- + private static final Map IP_UUIDMap = new HashMap(); //注册IP集合(临时IP信息和已注册IP信息) + private static final Map nodeInfoMape = new HashMap(); //注册IP集合(临时IP信息和已注册IP信息) + //#--- Node IP & userName password + private static final Map IP_USERPWDMap = new HashMap(); //IP及对应用户名密码信息 + + // #--- Detection Resove Map ------- + /** + * Map> =Map<监测设置ID,List<告警设置>> 实现通过监测设置ID 获取对应配置的 告警设置信息,用于数据告警判断和 告警描述 + * Map> =Map<监测类别名称标识, Map<解析字段序号(showNumber), 表字段详细信息>> + * Map = Map<监测类别名称标识,监测类别信息> + * */ + private static final Map> alarmInfoMap = new HashMap>(); //告警设置信息集合 + private static final Map> tableMap = new HashMap>(); //原数据表及Columns结构集合 checktype_name vs cloumns + private static final Map InsertTable = new HashMap(); //check_type VS table_name + // private static final Map alarmSetInfoMap = new HashMap(); // 常驻内存的监测设置信息集合 checkTypeName,SetInfo + + /** + * snmp 监测服务 + * + * */ + private static Snmp snmp ; // 全局SNMP对象 + + /** + * snmp_trap 监测类别 特殊处理 + */ + private static SetInfo snmpTrapSetInfo = null; //snmp trap 监测设置信息 + /** + * Map setInfoNameMape = Map<监测设置ID , 监测设置显示名称> 用于获取监测设置显示名称(邮件 的友好展示) + */ + private static final Map setInfoNameMape = new HashMap(); + /** + * Map checkTypeNameMape = Map<监测类别标识 , 监测类别显示名称> + */ + private static final Map checkTypeNameMape = new HashMap(); // 监测类别 标识名称 与 显示名称映射 + // #--- Detection Data Set ------- + private static boolean deteDataFlag = true; + private static final LinkedList deteDataList1 = new LinkedList(); + private static final LinkedList deteDataList2 = new LinkedList(); + + // #--- Alarm Data Set ------- + private static boolean alarmDataFlag = true; + private static final LinkedList alarmDataList1 = new LinkedList(); + private static final LinkedList alarmDataList2 = new LinkedList(); + + // #--- Mission Thread ------- + private static final LinkedList reserveMissionList = new LinkedList(); //任务预备队列 +// private static final List> assignFailMissionList = new LinkedList>(); //周期任务因节点不通,下发失败队列 + private static final Map missionIdWebIPNMap = new HashMap(); //任务id 下发webIP Map + private static final MissionPollingThread firstStartThread = new MissionPollingThread("First Polling Thread",1,reserveMissionList); +// private static final AssignMissionAgainThread assignAgainThread = new AssignMissionAgainThread("下发失败后,首次下发线程",1,assignFailMissionList); + private static final List assignFailMissIds = new LinkedList(); + + private static final Map> bpDownMissionFilesMap = new HashMap>(); //下载任务文件集合 }>> + private static final Map loopMissionInfoMap = new HashMap(); + private static final Map> loopMissionRoundInfoList = new HashMap>(); + + //-- 已检查属性 Begin + public static boolean SERVER_UN_UPGRADE_FLAG = true; //DataController 升级操作标识 + private static final Semaphore missionSemaphore = new Semaphore(Constants.MISSION_RELEASE_SEMAPHORE_MAX,true); //任务通信线程最大并发数 + private static final Semaphore monitorSemaphore = new Semaphore(Constants.DETEC_RELEASE_SEMAPHORE_MAX,true); //主动监测线程最大并发数 + private static final Semaphore changeSemaphore = new Semaphore(Constants.CHANGE_RELEASE_SEMAPHORE_MAX,true); //主动监测线程最大并发数 + + // #---任务结果数据集合 + private static final LinkedList missionResult2List1 = new LinkedList(); + private static final LinkedList missionResult2List2 = new LinkedList(); + private static final LinkedList missionResult2List3 = new LinkedList(); + private static final LinkedList missionResult2List4 = new LinkedList(); + private static final LinkedList missionResult2List5 = new LinkedList(); + private static final LinkedList missionResult2List6 = new LinkedList(); + + //系统异常信息集合 + private static boolean errorInfoFlag = true; + private static final LinkedList errorInfoList1 = new LinkedList(); + private static final LinkedList errorInfoList2 = new LinkedList(); + + //数据收集线程:限定时间内未获得监测数据节点的存放列表(单独处理) + public static final ArrayList noDetectDataNodeList = new ArrayList(); + + //锁 + private static final Byte[] REGISTER_LOCK = new Byte[0]; //线程注册锁 + private static final Byte[] DETEC_DATA_LOCK = new Byte[0]; //监测数据锁 + private static final Byte[] ALARM_DATA_LOCK = new Byte[0]; //告警数据锁 + private static final Byte[] ERROR_INFO_LOCK = new Byte[0]; //告警数据锁 + + + private static final Byte[] NO_DETEC_DATA_NODE_LOCK = new Byte[0]; // + //-- 已检查属性 End + + /** + * web 最后一次 握手通信时间 + */ + private static long LAST_WEB_HANDSHAKE = System.currentTimeMillis(); + + public static final Map SEQUENCE=new HashMap (); + + static{ + //-- 初始化snmpObject + /*try { + logger.debug("创建 classloader "); + logger.debug("SNMP_CLASS_PATH "+new File(Constants.SNMP_CLASS_PATH).toURI().toURL()); + classLoader = new URLClassLoader(new URL[] {new File(Constants.SNMP_CLASS_PATH).toURI().toURL()},ClassLoader.getSystemClassLoader()); + } catch (Error e) { + logger.error("Class Error", e); + } catch (Exception e) { + logger.error("Class Exception", e); + }*/ + //-- +// service. + } + public static void initSequence(java.sql.Connection conn,String ...tableNames){ + logger.info("table sequence init"); + try { + Statement smt ; + for (String tableName : tableNames) { + String sql="select max(SEQUENCE) from "+tableName; + smt= conn.createStatement(); + ResultSet rs = smt.executeQuery(sql); + while(rs.next()){ + Integer maxSeq = rs.getInt(1); + if(maxSeq==null){ + maxSeq=0; + } + AtomicInteger tableSequence = new AtomicInteger(maxSeq); + SEQUENCE.put(tableName, tableSequence); + } + smt.close(); + smt=null; + } + conn.close(); + conn=null; + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + public static void detecSelf(){ + logger.debug("缓存中告警数据量:"+(alarmDataList1.size()+alarmDataList2.size())); + logger.debug("缓存中监测数据量:"+(DETE_DATA_QUEUE.size())); + Set> entrySet = DETECT_QUEUE.entrySet(); + for(Entry en : entrySet){ + int size = 0; + LinkedBlockingDeque value = en.getValue(); + if(value != null){ + size = value.size(); + } + logger.debug("缓存中监测数据-" + en.getKey() +": " + size); + } + //logger.debug("缓存中监测数据量:"+(deteDataList1.size()+deteDataList2.size())); + logger.debug("缓存中任务结果数信息数:"+(missionResult2List1.size()+missionResult2List4.size()+missionResult2List6.size())); + logger.debug("缓存中错误信息数据量:"+(errorInfoList1.size()+errorInfoList2.size())); + logger.debug("缓存中监测类别数量2:"+(InsertTable.size())); +// logger.debug("缓存中监测设置信息集合:"+(alarmSetInfoMap.size())); + logger.debug("缓存中告警设置信息集合2:"+(alarmInfoMap.size())); + logger.debug("缓存中IP集合数量:"+(IP_UUIDMap.size())); + + + logger.debug("缓存中文件下载线程数:"+(bpDownMissionFilesMap.size())); + logger.debug("缓存中循环任务信息数1:"+(loopMissionInfoMap.size())); + logger.debug("缓存中循环任务信息数2:"+(loopMissionRoundInfoList.size())); + logger.debug("缓存中任务并发剩余许可:"+(missionSemaphore.availablePermits())); + logger.debug("缓存中监测并发剩余许可:"+(monitorSemaphore.availablePermits())); + logger.debug("缓存中变更并发剩余许可:"+(changeSemaphore.availablePermits())); + +// logger.debug("基本线程池用量:"+(service.)); +// logger.debug("定时线程池用量:"+(scheduled)); + + + } + + + /** + * 当前数据库连接状态 + * @time Mar 7, 2013-1:45:25 PM + * @return + */ + public static boolean isDbConnected(){ + return DB_CONNECT_FLAG; + } + + /** + * 设置数据库连接状态 + * @time Mar 7, 2013-1:45:37 PM + * @param flag + */ + public static void setDbConnectedFlag(boolean flag){ + DB_CONNECT_FLAG = flag; + } + + public static String getDateDirName(){ + return new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime()); + } + +/* public static void changeInitFlag(){ + SYSTEM_INIT_FLAG = (SYSTEM_INIT_FLAG==false? true:false); + } + public static boolean getInitFlag(){ + return SYSTEM_INIT_FLAG ; + }*/ + public static String getLocalIp(){ + /* + if(StringUtils.isEmpty(SYSTEM_INET_ADDRESS)){ + //-- 获取并判断本机IP + try { + Future future = Common.service.submit(new GetLocalIpFromWeb(Constants.WEB_SOCKET_IP,Constants.WEB_SOCKET_PORT)); + Object obj = future.get(20,TimeUnit.SECONDS); + SYSTEM_INET_ADDRESS = (obj==null?null:obj.toString()); + } catch (Exception e) { + logger.error("获取Server IP 操作 异常", e); + } + if(StringUtils.isEmpty(SYSTEM_INET_ADDRESS)){ + logger.warn("Server IP使用默认配置信息"); + SYSTEM_INET_ADDRESS = Constants.SYSTEM_INET_ADDRESS; + } + } + */ + return Constants.SYSTEM_INET_ADDRESS; + } + + public static String getWebIPByMissionId(Long missionId) { + return missionIdWebIPNMap.get(missionId); + } + + public static void addMissionIdWebIPMap(Long missionId,String webIp) { + missionIdWebIPNMap.put(missionId, webIp); + logger.debug("添加任务ID与系统IP映射: 任务ID["+missionId+"]来自Web["+webIp+"]"); + } + + public static void removeMissionIdWebIPMap(Long missionId) { + missionIdWebIPNMap.remove(missionId); + logger.debug("删除任务ID与系统IP映射: 任务ID["+missionId+"]"); + } + public static void clearMissionIdWebIPMap() { + missionIdWebIPNMap.clear(); + logger.debug("清空任务ID与系统IP映射"); + } + /** + * 改变存放监测数据的数据集合 + * @time Mar 7, 2012-2:15:38 PM + */ + public static void chengeDeteDataFlag() { + deteDataFlag = deteDataFlag ? false : true; + logger.info("监测数据缓存集合变更为集合"+(deteDataFlag?"1":"2")); + } + + /** + * 获得 非存放状态的数据集合 + * @time Mar 7, 2012-3:18:11 PM + * @return + */ + public static synchronized LinkedList getDeteDataList() { + LinkedList result = U.newLinkedList(); + int i = DETE_DATA_QUEUE.drainTo(result); + return result; + /*int dds1 = deteDataList1.size(); + int dds2 = deteDataList2.size(); + logger.debug("集合1缓存数据:" + dds1 +",集合2缓存数据:" + dds2); + if((dds1 > dds2 && deteDataFlag) || (dds1 getNotResovlingDeteDataList() { + + /*if(deteDataFlag){ + logger.info("取到集合1的缓存监测数据 共"+deteDataList1.size()); + return deteDataList1; + } + else{ + logger.info("取到集合2的缓存监测数据 共"+deteDataList2.size()); + return deteDataList2; + }*/ + LinkedList result = U.newLinkedList(); + int i = DETE_DATA_QUEUE.drainTo(result); + return result; + } + + + /** + * 添加或更新Monitor Runnable 到 集合 + * @time Mar 7, 2012-3:18:11 PM + * @return + */ +// public static void addOrUpdateMonitorRunnableToMap(String runnableName,Runnable runnable) { +// monitorThreadMap.put(runnableName, runnable); +// } + /** + * 根据 key 删除集合中的 Monitor Runnable + * @time Mar 7, 2012-3:18:11 PM + * @return + */ +// public static void removeMonitorRunnableFromMap(String runnableName) { +// monitorThreadMap.remove(runnableName); +// } + /** + * 根据Key 获取Monitor Runnable + * @time Mar 7, 2012-3:18:11 PM + * @return + */ +// public static Runnable getMonitorRunnableFromMap(String runnableName) { +// return monitorThreadMap.get(runnableName); +// } + /** + * 将监测数据存入存放状态的数据集合中 + * @time Mar 7, 2012-3:18:58 PM + * @param dsb + */ + public static void addDeteData(byte[] dsb) { + + /*synchronized (DETEC_DATA_LOCK) { + if(dsb==null || dsb.length==0l){ + logger.debug("监测数据 字节长度0 无需添加到监测数据集合"); + return; + } + if(deteDataFlag){ + logger.debug("监测数据 添加到 集合1"); + deteDataList1.add(dsb);} + else{ + logger.debug("监测数据 添加到 集合2"); + deteDataList2.add(dsb); } + }*/ + logger.debug(new String(dsb)); + DETE_DATA_QUEUE.add(dsb); + } + + //无监测数据列表增加删除的同步操作 + public static void moveNoDeteDataNodeFirst(String ip) { + + synchronized (NO_DETEC_DATA_NODE_LOCK) { + if(StringUtils.isNotBlank(ip)){ + Common.noDetectDataNodeList.remove(ip); + Common.noDetectDataNodeList.add(0,ip); + logger.debug("无监测数据节点列表 移动节点到最前面:"+ip); + } + } + + } + public static void addNoDeteDataNode(String ip) { + + synchronized (NO_DETEC_DATA_NODE_LOCK) { + if(StringUtils.isNotBlank(ip)){ + Common.noDetectDataNodeList.add(ip); + logger.debug("无监测数据节点列表 添加节点:"+ip); + } + } + + } + /** + * 将监测数据存入存放状态的数据集合中 + * @time Mar 7, 2012-3:18:58 PM + * @param seqId + * @param dsb + */ + public static void addAllDeteDataList(List dbsList) { + + /*synchronized (DETEC_DATA_LOCK) { + if(dbsList==null || dbsList.size()==0){ + logger.debug("监测数据 个数0 无需添加到监测数据集合"); + return; + } + if(deteDataFlag){logger.debug("监测数据 添加到 集合1 : 个数"+(dbsList.size())); + deteDataList1.addAll(dbsList);} + else{logger.debug("监测数据 添加到 集合2 : 个数"+dbsList.size()); + deteDataList2.addAll(dbsList);} + + }*/ + DETE_DATA_QUEUE.addAll(dbsList); + } + /** + * 清空非存放状态的数据集合 + * @time Mar 7, 2012-3:19:30 PM + * @param seqId + * @param dsb + */ + public static void clearDeteDataList() { + + synchronized (DETEC_DATA_LOCK) { + + getDeteDataList().clear(); + logger.info("清空该集合"); + } + + } + + /** + * 改变存放监测数据的数据集合 + * @time Mar 7, 2012-2:15:38 PM + */ + public static void changeAlarmDataFlag() { + + alarmDataFlag = alarmDataFlag ? false : true; + logger.info("告警数据缓存集合变更为集合"+(alarmDataFlag?"1":"2")); + } + + /** + * 获得 非存放状态的数据集合 + * @time Mar 7, 2012-3:18:11 PM + * @return + */ + public static LinkedList getAlarmDataList() { + + if(!alarmDataFlag){logger.info("取到集合1的缓存告警数据 共"+alarmDataList1.size()); + return alarmDataList1;} + else{logger.info("取到集合2的缓存告警数据 共"+alarmDataList2.size()); + return alarmDataList2;} + + } + + /** + * 获得 告警数据存放状态的数据集合 + * @time Mar 7, 2012-3:18:11 PM + * @return + */ + public static LinkedList getNotResovlingWarnDataList() { + + if(alarmDataFlag){ + logger.info("取到集合1的缓存告警数据 共"+alarmDataList1.size()); + return alarmDataList1; + } + else{ + logger.info("取到集合2的缓存告警数据 共"+alarmDataList2.size()); + return alarmDataList2; + } + + } + + /** + * 将告警数据存入存放状态的数据集合中 + * @time Mar 7, 2012-3:18:58 PM + * @param dsb + */ + public static void addAlarmData(String[] dsb) { + + synchronized (ALARM_DATA_LOCK) { + if(dsb==null || dsb.length==0l){ + logger.debug("告警数据 字节长度0 无需添加到监测数据集合"); + return; + } + if(alarmDataFlag){logger.debug("告警数据 添加到 集合1"); + alarmDataList1.add(dsb);} + else{logger.debug("告警数据 添加到 集合2"); + alarmDataList2.add(dsb); } + + } + + } + /** + * 将告警数据存入存放状态的数据集合中 + * @time Mar 7, 2012-3:18:58 PM + * @param seqId + * @param dsb + */ + public static void addAllAlarmDataList(List dbsList) { + + synchronized (ALARM_DATA_LOCK) { + if(dbsList==null || dbsList.size()==0l){ + logger.debug("告警数据 个数0 无需添加到监测数据集合"); + return; + } + if(alarmDataFlag){logger.debug("告警数据 添加到 集合1"); + alarmDataList1.addAll(dbsList);} + else{logger.debug("告警数据 添加到 集合2"); + alarmDataList2.addAll(dbsList);} + + } + + } + /** + * 清空非存放状态的数据集合 + * @time Mar 7, 2012-3:19:30 PM + * @param seqId + * @param dsb + */ + public static void clearAlarmDataList() { + + synchronized (ALARM_DATA_LOCK) { + + getAlarmDataList().clear(); + logger.info("清空该集合"); + } + + } + + + public static void changeErrorInfoFlag() { + + errorInfoFlag = errorInfoFlag ? false : true; + logger.info("错误信息集合变更为集合"+(errorInfoFlag?"1":"2")); + } + + /** + * 获得 非存放状态的数据集合 + * @time Mar 7, 2012-3:18:11 PM + * @return + */ + public static LinkedList getErrorInfoList() { + + if(!errorInfoFlag){logger.info("取到集合1的错误信息数据 共"+errorInfoList1.size()); + return errorInfoList1;} + else{logger.info("取到集合2的错误信息数据 共"+errorInfoList2.size()); + return errorInfoList2;} + + } + + /** + * 获得 错误数据存放状态的数据集合 + * @time Mar 7, 2012-3:18:11 PM + * @return + */ + public static LinkedList getNotResovlingErrorInfoList() { + + if(errorInfoFlag){ + logger.info("取到集合1的缓存错误数据 共"+errorInfoList1.size()); + return errorInfoList1; + } + else{ + logger.info("取到集合2的缓存错误数据 共"+errorInfoList2.size()); + return errorInfoList2; + } + + } + + /** + * 将监测数据存入存放状态的数据集合中 + * @time Mar 7, 2012-3:18:58 PM + * @param dsb + */ + public static void addErrorInfo(NmsErrorInfo dsb) { + + synchronized (ERROR_INFO_LOCK) { + if(dsb==null){ + logger.debug("错误信息 为空 无需添加到监测数据集合"); + return; + } + if(errorInfoFlag){logger.debug("错误信息 添加到 集合1"); + errorInfoList1.add(dsb);} + else{logger.debug("错误信息 添加到 集合2"); + errorInfoList2.add(dsb); } + + } + + } + /** + * 将监测数据存入存放状态的数据集合中 + * @time Mar 7, 2012-3:18:58 PM + * @param seqId + * @param dsb + */ + public static void addAllErrorInfoList(List dbsList) { + + synchronized (ERROR_INFO_LOCK) { + if(dbsList==null || dbsList.size()==0l){ + logger.debug("错误信息 个数0 无需添加到监测数据集合"); + return; + } + if(errorInfoFlag){logger.debug("错误信息 添加到 集合1"); + errorInfoList1.addAll(dbsList);} + else{logger.debug("错误信息 添加到 集合2"); + errorInfoList2.addAll(dbsList);} + + } + + } + /** + * 将监测数据存入存放状态的数据集合中 + * default 本机 + * @time Mar 7, 2012-3:18:58 PM + * @param dsb + */ + public static void addErrorInfoForMySelf(String errorCode,Long errorState,String errortDesc) { +// NmsErrorInfo nei = new NmsErrorInfo(errorCode,Calendar.getInstance().getTime(),Common.getLocalIp(),Common.getLocalIp(),errorState,errortDesc); +// nei.setErrortGetip(Common.getLocalIp()); +// nei.setErrortIp(Common.getLocalIp()); +// nei.setErrorCode(errorCode); +// nei.setErrorState(errorState); +// nei.setErrortDesc(errortDesc); +// nei.setErrorTime(Calendar.getInstance().getTime()); + addErrorInfo( new NmsErrorInfo(errorCode,Calendar.getInstance().getTime(),Common.getLocalIp(),Common.getLocalIp(),errorState,errortDesc)); + } + + /** + * 将监测数据存入存放状态的数据集合中 + * default 本机 + * @time Mar 7, 2012-3:18:58 PM + * @param dsb + */ + public static void addErrorInfo(String errorCode,String errortIp,Date errorTime,Long errorState,String errortDesc) { +// NmsErrorInfo nei = new NmsErrorInfo(errorCode,errorTime,Common.getLocalIp(),errortIp,errorState,errortDesc); +// nei.setErrortGetip(Common.getLocalIp()); +// nei.setErrortIp(errortIp); +// nei.setErrorCode(errorCode); +// nei.setErrorState(errorState); +// nei.setErrortDesc(errortDesc); +// nei.setErrorTime(errorTime); + addErrorInfo(new NmsErrorInfo(errorCode,errorTime,Common.getLocalIp(),errortIp,errorState,errortDesc)); + } + /** + * 将系统错误信息存入存放状态的数据集合中 + * @time Mar 7, 2012-3:18:58 PM + * @param dsb + */ + public static void addErrorInfo(String errorCode,String errorGetIp,String errortIp,Date errorTime,Long errorState,String errortDesc) { +// NmsErrorInfo nei = new NmsErrorInfo(errorCode,errorTime,errorGetIp,errortIp,errorState,errortDesc); +// nei.setErrortGetip(errorGetIp); +// nei.setErrortIp(errortIp); +// nei.setErrorCode(errorCode); +// nei.setErrorState(errorState); +// nei.setErrortDesc(errortDesc); +// nei.setErrorTime(errorTime); + addErrorInfo(new NmsErrorInfo(errorCode,errorTime,errorGetIp,errortIp,errorState,errortDesc)); + } + /** + * 清空非存放状态的数据集合 + * @time Mar 7, 2012-3:19:30 PM + * @param seqId + * @param dsb + */ + public static void clearErrorInfoList() { + + synchronized (ERROR_INFO_LOCK) { + + getErrorInfoList().clear(); + logger.info("清空该集合"); + } + + } + + /** + * 计划任务更新 + * @time Sep 5, 2011-3:57:17 PM + * @param key + * @param + */ + public static void registRunnable(String key ,Future future){ + synchronized (REGISTER_LOCK) { + futureMap.put(key, future); + logger.info("线程 "+key+" 注册成功"); + } + } + + public static boolean isRunnableRegist(String key){ + synchronized (REGISTER_LOCK) { + if(futureMap.get(key)!=null){ + logger.info("线程 "+key+" 状态: 已存在"); + return true; + }else{ + logger.info("线程 "+key+" 状态: 不存在"); + return false; + } + } + } + + /** + * 即时注销线程 + * @time Sep 5, 2011-4:08:34 PM + * @param key + */ + public static void cancelRunnableAtOnce(final String key) { + synchronized (REGISTER_LOCK) { + Future future = futureMap.get(key); + if (future != null) { + if(future.isDone()||future.isCancelled()){ + logger.info("线程 "+key+" 状态: 已停止"); + futureMap.remove(key); + }else{ + logger.info("线程 "+key+" 状态: 运行中"); + future.cancel(true);//单次:触发中断标识,这样程序里就可以做判断;周期的就是下一次不再执行 + futureMap.remove(key); + logger.info("线程 "+key+" 注销成功"); + } + } else { + logger.info("线程 "+key+" 状态: 不存在"); + } + } + } + + /** + * 异步注销线程操作 + * 判断线程是否已存在,再进行注销操作 + * @time Sep 5, 2011-4:08:34 PM + * @param key + */ + public static void cancelRunnable(final String key) { + + Future future = futureMap.get(key); + if (future == null) { + logger.info("线程 "+key+" 状态: 不存在"); + }else{ + service.submit(new Runnable() { + public void run() { + synchronized (REGISTER_LOCK) { +// Thread.currentThread().setName("注销线程" + key); + Thread.currentThread().setName("Write Off Thread " + key); + Future future = futureMap.get(key); + if (future != null) { + if(future.isDone()||future.isCancelled()){ + logger.info("线程状态: 已停止"); + futureMap.remove(key); + }else{ + logger.info("线程状态: 运行中"); + future.cancel(true); + futureMap.remove(key); + logger.info("注销成功"); + } + } else { + logger.info("线程状态: 不存在"); + } + } + } + + }); + } + } + + + /** + * 在 snmp_class_dir 中加载指定类 并返回该类 + * 属于临时加载 + * @time Apr 19, 2012-3:18:15 PM + * @param className + * @return + */ + /*public static Class loadClass(String className) { + Class c = null; + try { + c = classLoader.loadClass(className); + logger.debug(" className "+className); + } catch (ClassNotFoundException e) { + logger.error("class 加载失败: "+className, e); + } + return c; + }*/ + + /** + * + *

key: check_type + *

value: table_name + * @time Jul 15, 2011-2:48:00 PM + * @return + */ + public static Map getInsertTable(){ + return InsertTable; + } + + /** + * 主动告警异常信息编写--NC主动发送的给DC的告警(程序异常+程序没运行+(告警值)异常状态连续4次-1级别+(告警值)异常状态1次-设置的告警级别)+DC的一些(例如ping、switchportthread-catch异常) + + * @time Oct 18, 2011-2:34:07 PM + * @param setInfoId + * @param checkIp + * @param checkType + * @param processName + * @param checkTime + * @param level + * @param exInfo + * @return + */ + public static String[] alarmExceptionInfo(long setInfoId,String checkIp,String checkType,String processName,long checkTime,long level,long state,String exInfo) { + String[] datas = new String[9]; + datas[0] = setInfoId+""; + datas[1] = Common.getIpSeqIdMap().get(checkIp)+""; + datas[2] = checkType; + datas[3] = processName;//process_iden + datas[4] = checkTime==0?"":checkTime+""; + datas[5] = System.currentTimeMillis()+""; + datas[6] = level+""; + datas[7] = state+"";//正常、异常 + datas[8] = exInfo==null?"":exInfo.length()>250?exInfo.substring(0, 250):exInfo; + return datas; + } + + /** + * 获取任务下发信号 + * @time Nov 23, 2011-3:49:38 PM + */ + public static void acquireMissionSemaphore() { +// synchronized (missionSemaphore) { +// try { + missionSemaphore.acquireUninterruptibly(); + logger.debug("任务锁 已申请 剩余可用许可:> "+missionSemaphore.availablePermits()); +// } catch (Exception e) { +// logger.warn("任务锁 线程未申请成功已被中断:"+Thread.currentThread().interrupted(),e); +// } +// } + } + + /** + * 释放任务下发信号 + * @time Nov 25, 2011-1:24:08 PM + */ + public static void releaseMissionSemaphore() { + missionSemaphore.release(); + logger.debug("任务锁 已释放 当前可用许可:> "+missionSemaphore.availablePermits()); + } + /** + * 获取监测下发信号 + * @time Nov 23, 2011-3:49:38 PM + */ + public static void acquireMonitorSemaphore() { +// synchronized (monitorSemaphore) { +// try { + logger.debug("监测锁 正在申请 剩余可用许可:> "+monitorSemaphore.availablePermits()); + monitorSemaphore.acquireUninterruptibly(); + logger.debug("监测锁 已申请 剩余可用许可:> "+monitorSemaphore.availablePermits()); +// } catch (Exception e) { +// logger.warn("监测锁 线程未申请成功已被中断:"+Thread.currentThread().interrupted(),e); +// } +// } + } + /** + * 释放监测下发信号 + * @time Nov 25, 2011-1:24:08 PM + */ + public static void releaseMonitorSemaphore() { +// synchronized (monitorSemaphore) { + monitorSemaphore.release(); + logger.debug("监测锁 已释放 当前可用许可:> "+monitorSemaphore.availablePermits()); +// } + } + /** + * 获取变更下发信号 + * @time Nov 23, 2011-3:49:38 PM + */ + public static void acquireChangeSemaphore() { +// synchronized (changeSemaphore) { +// try { + changeSemaphore.acquireUninterruptibly(); + logger.debug("变更锁 已申请 剩余可用许可:> "+changeSemaphore.availablePermits()); +// } catch (Exception e) { +// logger.warn("变更锁 线程未申请成功已被中断:"+Thread.currentThread().interrupted(),e); +// } +// } + } + /** + * 释放变更下发信号 + * @time Nov 25, 2011-1:24:08 PM + */ + public static void releaseChangeSemaphore() { +// synchronized (changeSemaphore) { + changeSemaphore.release(); + logger.debug("变更锁 已释放 当前可用许可:> "+changeSemaphore.availablePermits()); +// } + } + + + /** + * 将任务结果存放到任务结果集合中 + * @time Nov 18, 2011-6:35:48 PM + * @param result2 + */ + public static void addResultToResultList(MissionResult2 result2){ + logger.debug("任务结果加入到 结果队列 "+result2.getMissionType().longValue()+" <> "+result2.getMissionId()+" <> "+result2.getUuid()); +// if(result2 != null && result2.getResult() != null){ + switch (result2.getMissionType().intValue()) { + case 1: + synchronized (missionResult2List1) { + missionResult2List1.add(result2); + } + break; + case 2: + synchronized (missionResult2List2) { + missionResult2List2.add(result2); + } + break; + case 3: + synchronized (missionResult2List3) { + missionResult2List3.add(result2); + } + break; + case 4: + synchronized (missionResult2List4) { + missionResult2List4.add(result2); + } + break; + case 5: + synchronized (missionResult2List5) { + missionResult2List5.add(result2); + } + break; + case 6: + synchronized (missionResult2List6) { + missionResult2List6.add(result2); + } + break; + default: + break; + } +// } + } + /** + * 从指定任务类型的任务结果列表中,删除指定的任务结果集合 + * @time Nov 18, 2011-6:35:48 PM + * @param resultList 指定删除的任务结果集合 + */ + public static void removeResultsFormList(int missionType ,LinkedList resultList){ + if(resultList != null && resultList.size() >0){ + switch (missionType){ + case 1: + synchronized (missionResult2List1) { + logger.debug("从长度为"+missionResult2List1.size()+" 的 任务结果集合1 中删除 "+resultList.size()+" 个"); + missionResult2List1.removeAll(resultList); + } + break; + case 2: + synchronized (missionResult2List2) { + logger.debug("从长度为"+missionResult2List2.size()+" 的 任务结果集合2 中删除 "+resultList.size()+" 个"); + missionResult2List2.removeAll(resultList); + } + break; + case 3: + synchronized (missionResult2List3) { + logger.debug("从长度为"+missionResult2List3.size()+" 的 任务结果集合3 中删除 "+resultList.size()+" 个"); + missionResult2List3.removeAll(resultList); + } + break; + case 4: + synchronized (missionResult2List4) { + logger.debug("从长度为"+missionResult2List4.size()+" 的 任务结果集合4 中删除 "+resultList.size()+" 个"); + missionResult2List4.removeAll(resultList); + } + break; + case 5: + synchronized (missionResult2List5) { + logger.debug("从长度为"+missionResult2List5.size()+" 的 任务结果集合5 中删除 "+resultList.size()+" 个"); + missionResult2List5.removeAll(resultList); + } + break; + case 6: + synchronized (missionResult2List6) { + logger.debug("从长度为"+missionResult2List6.size()+" 的 任务结果集合6 中删除 "+resultList.size()+" 个"); + missionResult2List6.removeAll(resultList); + } + break; + default: + break; + } + } + } + + /** + * 返回指定任务类型的任务结果列表(浅克隆) + * @time Nov 18, 2011-6:35:48 PM + * @param missionType 任务类型 1文件推送 2 3 4脚本命令 5 + * @return + */ + @SuppressWarnings("unchecked") + public static LinkedList getResultListClone(int missionType){ + LinkedList mr2List = null; + switch (missionType) { + case 1: + synchronized (missionResult2List1) { + mr2List = (LinkedList) missionResult2List1.clone(); + } + break; + case 2: + synchronized (missionResult2List2) { + mr2List = (LinkedList) missionResult2List2.clone(); + } + break; + case 3: + synchronized (missionResult2List3) { + mr2List = (LinkedList) missionResult2List3.clone(); + } + break; + case 4: + synchronized (missionResult2List4) { + mr2List = (LinkedList) missionResult2List4.clone(); + } + break; + case 5: + synchronized (missionResult2List5) { + mr2List = (LinkedList) missionResult2List5.clone(); + } + break; + case 6: + synchronized (missionResult2List6) { + mr2List = (LinkedList) missionResult2List6.clone(); + } + break; + default: + break; + } + return mr2List; + } + + /** + * 分配任务 + * 将任务分配到指定线程分类操作 + * 即时线程队列、线程池-计划线程、线程池-周期线程 + * @time Nov 10, 2011-4:28:52 PM + * @param mission + */ + public static void assignMssion(final MissionStateTable mission){ + //刷新即时任务 + try { + logger.info("分配任务 id: "+mission.getMissionId()+" State: "+mission.getMissionState() + " 分配到即时任务队列"); + //线程不存在情况 + //任务队列存在任务 且第一个任务就是变更任务 终止线程 重新执行该线程 + LinkedList missionList = Common.getReserveMissionListClone(); + if(missionList.size()>0 && missionList.get(0).getMissionId().longValue() == mission.getMissionId().longValue()){ //线程重启:新建之后,马上停掉,0:正在执行的,队列之后的表示还没执行 + Common.getFutureMap().get(Constants.MISSION_THREAD).cancel(true); + Common.getFutureMap().remove(Constants.MISSION_THREAD); + Common.removeMissionToReserveMissionList(missionList.get(0)); + Common.addMissionToReserveMissionList(0,mission); + Common.registRunnable(Constants.MISSION_THREAD,Common.scheduled.schedule(Common.getFirstStartThread(), 0, TimeUnit.SECONDS)); + }else{ + //判断线程是否存在情况 不存在创建 + if(Common.getFutureMap().get(Constants.MISSION_THREAD)==null){ + Common.addMissionToReserveMissionList(mission); + Common.registRunnable(Constants.MISSION_THREAD,Common.scheduled.schedule(Common.getFirstStartThread(), 0, TimeUnit.SECONDS)); + }else{//已存在 将任务加入队列即可 + boolean flag = true; + for(int i=0 ; i< missionList.size();i++ ){ + MissionStateTable misTable = missionList.get(i); + if(misTable.getMissionId().longValue() == mission.getMissionId().longValue()){ + Common.addMissionToReserveMissionList(i, mission); + flag = false; + break; + } + } + if(flag){ + Common.addMissionToReserveMissionList(mission); + } + if(Common.getFutureMap().get(Constants.MISSION_THREAD).isCancelled()){ + Common.registRunnable(Constants.MISSION_THREAD,Common.scheduled.schedule(Common.getFirstStartThread(), 0, TimeUnit.SECONDS)); + }else if(Common.getFutureMap().get(Constants.MISSION_THREAD).isDone()){ + Common.registRunnable(Constants.MISSION_THREAD,Common.scheduled.schedule(Common.getFirstStartThread(), 0, TimeUnit.SECONDS)); + } + } + } + logger.debug("当前任务队列中任务数: "+Common.getReserveMissionListClone().size()); + } catch (Exception e) { + logger.error("",e); + } + } + + /** + * 主动监测任务执行操作方法 + * 引入 申请信号,和释放信号 操作 + * @time Sep 12, 2012-4:28:45 PM + * @param runnable + */ + public static void runMonitorRunnable(final Runnable runnable){ + try { + //-- 针对节点 进行文件推送 + Common.acquireMonitorSemaphore(); //申请监测信号 + + //-- 启动新线程 推送文件和任务信息 + final Future future = Common.service.submit(runnable); + final String threadName = Thread.currentThread().getName(); + Common.service.submit(new Runnable() { + + public void run() { + Thread.currentThread().setName(threadName); + try { + future.get(Constants.SINGLE_NODE_DETECT_TIMEOUT, TimeUnit.SECONDS); + } catch (Exception e) { + logger.error("Active Monitoring:"+threadName+" Wait for timeout anomaly",e); + if (future != null) { + if(future.isDone()||future.isCancelled()){ + logger.info("线程 "+threadName+" 状态: 已停止"); + }else{ + logger.info("线程 "+threadName+" 状态: 运行中"); + future.cancel(true);//单次:触发中断标识,这样程序里就可以做判断;周期的就是下一次不再执行 + } + } else { + logger.info("线程 "+threadName+" 状态: 不存在"); + } + }finally{ + Common.releaseMonitorSemaphore(); + } + } + }); + + } catch (Exception e) { + logger.error("Application signal anomaly",e); + Common.releaseMonitorSemaphore(); + }finally{ + + } + } + + /** + * 任务通讯操作线程 + * 引入 申请信号,和释放信号 操作 + * @time Sep 12, 2012-4:31:27 PM + * @param mission + * @param callable + * @param uuid + * @param testNum + * @param startTime + * @param endTime + */ + public static void sendMissionInfo(final MissionStateTable mission,SSLSocketCallable callable,final long uuid,final Integer testNum,final Long startTime,final Long endTime){ + + try { + + //-- 针对节点 进行文件推送 + Common.acquireMissionSemaphore(); //申请通讯信号 + + //-- 启动新线程 推送文件和任务信息 + final Future future = Common.service.submit(callable); + + //-- 异步线程处理 推送线程执行结果 +// receiveResult(future, mission, uuid,testNum,startTime,endTime); + final String threadName = Thread.currentThread().getName(); + Common.service.submit(new Runnable() { + public void run() { + Thread.currentThread().setName(threadName); + try { + MissionResult2 result2 = new MissionResult2(); + result2.setMissionId(mission.getMissionId()); + result2.setMissionType(mission.getMissionType()); + result2.setUuid(uuid); + result2.setLoopFlag(mission.getLoopFlag()); + result2.setStartTime(startTime); + result2.setEndTime(endTime); + //-- 等待推送线程执行完成返回的结果 + /* + * 返回值有:null 与 非null + * */ + logger.debug("result Info: "+future.get()); + if(future.get() instanceof Integer){ + return ; + } + + String resultStr = (String)future.get(); + if(StringUtils.isNotEmpty(resultStr)){ + String [] str = resultStr.split(Constants.COMMON_DATA_SPLIT,2); + if(str.length<=1){ + str = resultStr.split(":",2); + } + if(StringUtils.isNotEmpty(str[0])){ + result2.setResult(Long.parseLong(str[0])); + } + if(str.length>1){ + result2.setDescription(str[1]); + } + //-- 将结果追加到结果集和 + Common.addResultToResultList(result2); + }else{ + if(testNum.intValue() == Constants.MISSION_RELEASE_TIMES.intValue()){ + if(mission.getMissionState().longValue()==6l){ + result2.setResult(6l); +// result2.setDescription("撤销通讯失败,超过最大尝试次数,等待客户端请求"); +// result2.setDescription("Revocation failed, exceeded maximum number of attempts, waiting for client request"); + result2.setDescription("i18n_server.Common.revokeFail_n81i"); + Common.addResultToResultList(result2); + }else{ + result2.setResult(1l); + result2.setResultDetail(41l); +// result2.setDescription("任务下发失败,通讯中断,且超过最大尝试次数"); +// result2.setDescription("The task failed to be delivered, communication was interrupted, and the maximum number of attempts was exceeded"); + result2.setDescription("i18n_server.Common.missionLssueFail_n81i"); + Common.addResultToResultList(result2); + } + } + } + } catch (InterruptedException e) { + logger.error("",e); + } catch (Exception e) { + logger.error("",e); + }finally{ + Common.releaseMissionSemaphore(); + } + } + }); + + } catch (Exception e) { + + logger.warn(nodeInfoMape.get(uuid)+" Task "+mission.getMissionId()+" failure of communication creation"); + if(testNum.intValue() == Constants.MISSION_RELEASE_TIMES.intValue()){ + MissionResult2 result2 = new MissionResult2(); + result2.setMissionId(mission.getMissionId()); + result2.setMissionType(mission.getMissionType()); + result2.setUuid(uuid); + result2.setResult(1l); +// result2.setDescription("任务下发失败,建立通讯失败"); +// result2.setDescription("The task failed to be sent and the communication failed to be established"); + result2.setDescription("i18n_server.Common.createConnectionFail_n81i"); + Common.addResultToResultList(result2); + } + + Common.releaseMissionSemaphore(); + } + } + /** + * 变更操作线程执行方法 + * 引入 申请信号,和释放信号 操作 + * @time Sep 12, 2012-4:28:45 PM + * @param runnable + */ + public static void runChangeRunnable(final Object runnable){ + try { + //-- 针对节点 进行文件推送 + Common.acquireChangeSemaphore(); //申请监测信号 + + //-- 启动新线程 推送文件和任务信息 + final Future future; + if(runnable instanceof Runnable){ + future = Common.service.submit((Runnable)runnable); + }else if(runnable instanceof Callable){ + future = Common.service.submit((Callable)runnable); + }else { + future = null; + } + + final String threadName = Thread.currentThread().getName(); + Common.service.submit(new Runnable() { + + public void run() { + Thread.currentThread().setName(threadName); + try { + future.get(); + } catch (Exception e) { + logger.error("",e); + }finally{ + Common.releaseChangeSemaphore(); + } + } + }); + + } catch (Exception e) { + logger.error("",e); + Common.releaseChangeSemaphore(); + }finally{ + + } + } + public static boolean hasIpInIpSegment(String ip){ + Long ipn = IpCovert.ipToLong(ip); + List list = Common.getServerTable().getIpSegList(); + if(ipn != 0 && list!= null && list.size()>0){ + for(ServerIpSegment segment : list){ + if(segment != null && segment.getStartIpn() <= ipn && ipn <= segment.getEndIpn()){ + logger.debug("ip 存在于 区间["+segment.getStartIp()+","+segment.getEndIp()+"]"); + return true; + } + } + } + logger.debug("ip 不在当前Server的管理区间"); + return false; + } + + public static Long getUUIDByIp(String ip) { + return IP_UUIDMap.get(ip)==null?null:IP_UUIDMap.get(ip); + } + + public static String getNodeIpByUUID(Long uuid) { + return nodeInfoMape.get(uuid)==null?null:nodeInfoMape.get(uuid).getNodeIp(); + } + + public static Integer getSnmpVerByUUID(Long uuid) { + return (nodeInfoMape.get(uuid)==null + || nodeInfoMape.get(uuid).getSnmpVersion()==null)?Constants.COMMON_SNMP_VERSION + :nodeInfoMape.get(uuid).getSnmpVersion(); + } + + public static String getClassNameByCheckTypeName(String checkTypeName){ + String tableName = Common.getInsertTable().get(checkTypeName).getTableName(); + if(StringUtils.isNotEmpty(tableName) && tableName.startsWith("DI_")){ + tableName = tableName.replaceFirst("DI_", ""); + } + return tableName; + } +// /** +// * 线程执行结果处理方法 +// * 线程执行结束返回结果信息为string类型数据 格式为 "0:string" +// * @time Nov 13, 2011-12:17:36 PM +// * @param future +// * @param result +// */ +// private static void receiveResult(final Future future,final MissionStateTable mission,final long uuid,final Integer testNum,final Long startTime,final Long endTime){ +// Common.service.submit(new Runnable() { +// public void run() { +// Thread.currentThread().setName("任务推送结果处理线程"); +// try { +// MissionResult2 result2 = new MissionResult2(); +// result2.setMissionId(mission.getMissionId()); +// result2.setMissionType(mission.getMissionType()); +// result2.setUuid(uuid); +// result2.setLoopFlag(mission.getLoopFlag()); +// result2.setStartTime(startTime); +// result2.setEndTime(endTime); +// //-- 等待推送线程执行完成返回的结果 +// /* +// * 返回值有:null 与 非null +// * */ +// logger.debug("result Info: "+future.get()); +// if(future.get() instanceof Integer){ +// return ; +// } +// +// String resultStr = (String)future.get(); +// if(StringUtils.isNotEmpty(resultStr)){ +// String [] str = resultStr.split(Constants.COMMON_DATA_SPLIT,2); +// if(str.length<=1){ +// str = resultStr.split(":",2); +// } +// if(StringUtils.isNotEmpty(str[0])){ +// result2.setResult(Long.parseLong(str[0])); +// } +// if(str.length>1){ +// result2.setDescription(str[1]); +// } +// //-- 将结果追加到结果集和 +// Common.addResultToResultList(result2); +// }else{ +// if(testNum.intValue() == Constants.MISSION_RELEASE_TIMES.intValue()){ +// if(mission.getMissionState().longValue()==6l){ +// result2.setResult(6l); +// result2.setDescription("撤销通讯失败,超过最大尝试次数,等待客户端请求"); +// Common.addResultToResultList(result2); +// }else{ +// result2.setResult(1l); +// result2.setDescription("任务下发失败,通讯中断,且超过最大尝试次数"); +// Common.addResultToResultList(result2); +// } +// } +// } +// } catch (InterruptedException e) { +// logger.error("",e); +// } catch (Exception e) { +// logger.error("",e); +// }finally{ +// +// } +// } +// }); +// } + //获得当前的报警设置信息,与相应字段值比较 + public static String[] getAlarmState(List alarmInfos, String[] sysData) throws Exception{ + String[] strs = null; +// logger.debug("!!you gaojing o "+(alarmInfos==null?null:alarmInfos.size())); + if(alarmInfos!=null){ + strs = new String[]{"", "", ""}; + for(AlarmInfo alarm : alarmInfos){ +// logger.debug("!!you gaojing o "+alarm.getPoliceValue()); + if(sysData.length>=alarm.getShowNum()){ + if(sysData[alarm.getShowNum()-1]==null){ + logger.error("Monitoring data is empty and can not do alarm verification"); + continue; + } + if("equal".equals(alarm.getPoliceSysmbols())){ + if((sysData[alarm.getShowNum()-1]).equals(alarm.getPoliceValue())){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else if("include".equals(alarm.getPoliceSysmbols())){ + if((sysData[alarm.getShowNum()-1]).indexOf(alarm.getPoliceValue())!=-1){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else if("exclude".equals(alarm.getPoliceSysmbols())){ + if((sysData[alarm.getShowNum()-1]).indexOf(alarm.getPoliceValue())==-1){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else{ + Double data = Math.abs(Double.parseDouble(sysData[alarm.getShowNum()-1])); + double result = data - Double.parseDouble(alarm.getPoliceValue()); + if ((">".equals(alarm.getPoliceSysmbols()) && result > 0) + || ("<".equals(alarm.getPoliceSysmbols()) && result < 0) + || ("=".equals(alarm.getPoliceSysmbols()) && result == 0) + || (">=".equals(alarm.getPoliceSysmbols()) && result >= 0) + || ("<=".equals(alarm.getPoliceSysmbols()) && result <=0) ) { + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + } + } + for(int i=0; i0){ + strs[i] = strs[i].substring(0, strs[i].length()-1); + } + } + } + + if(strs!=null && "".equals(strs[0])){ + strs = null; + } + + return strs; + } + + //获得当前的报警设置信息,与相应字段值比较 + public static String getStateInfo(String checkType, List alarmInfos, String[] sysData) throws Exception{ + StringBuffer strs = null; +// logger.debug("!!you gaojing o "+(alarmInfos==null?null:alarmInfos.size())); + if(alarmInfos!=null){ + strs = new StringBuffer(); + for(AlarmInfo alarm : alarmInfos){ +// logger.debug("!!you gaojing o "+alarm.getPoliceValue()); + if(sysData.length>=alarm.getShowNum()){ + if(sysData[alarm.getShowNum()-1]==null){ + logger.error("Monitoring data is empty and can not do alarm verification"); + continue; + } + boolean alarmFlag =false; + if("equal".equals(alarm.getPoliceSysmbols())){ + if((sysData[alarm.getShowNum()-1]).equals(alarm.getPoliceValue())){ + alarmFlag = true; + } + } + else if("include".equals(alarm.getPoliceSysmbols())){ + if((sysData[alarm.getShowNum()-1]).indexOf(alarm.getPoliceValue())!=-1){ + alarmFlag = true; + } + } + else if("exclude".equals(alarm.getPoliceSysmbols())){ + if((sysData[alarm.getShowNum()-1]).indexOf(alarm.getPoliceValue())==-1){ + alarmFlag = true; + } + } + else{ + Double data = Math.abs(Double.parseDouble(sysData[alarm.getShowNum()-1])); + double result = data - Double.parseDouble(alarm.getPoliceValue()); + if ((">".equals(alarm.getPoliceSysmbols()) && result > 0) + || ("<".equals(alarm.getPoliceSysmbols()) && result < 0) + || ("=".equals(alarm.getPoliceSysmbols()) && result == 0) + || (">=".equals(alarm.getPoliceSysmbols()) && result >= 0) + || ("<=".equals(alarm.getPoliceSysmbols()) && result <=0) ) { + alarmFlag = true; + } + } + + if(alarmFlag){ + strs.append(Common.getTableMap().get(checkType).get(alarm.getShowNum()).getFiledName() + +"("+Common.getTableMap().get(checkType).get(alarm.getShowNum()).getFiledComments()+") "+ + ":> "+sysData[alarm.getShowNum()-1]+" "+alarm.getPoliceSysmbols()+" "+alarm.getPoliceValue()+";\n"); + } + } + } + } + return strs.toString(); + } + + + private static final SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); //Java Date 类型数据格式化格式 + + public static SimpleDateFormat getFormat() { + return format; + } + + + /** + * 任务结果解析 + * @time Nov 12, 2011-8:00:06 PM + * @return + */ + public static MissionResult2 resoveMissionResult(String [] result){ +// 任务ID,任务类型,UUID,周期标识 long,开始执行时候,结束时间,配置截取文本,结果状态,结果描述 + try { + if(result == null && result.length>=9){ + return null; + } + if(StringUtils.isEmpty(result[0]) || StringUtils.isEmpty(result[1])){ + return null; + } +// 任务ID、任务类型、UUID、周期任务标识、开始时间、结束时间、resultConfig(备用的字段)、结果状态、结果描述 + + MissionResult2 result2 = new MissionResult2(); + //- 任务Id long + result2.setMissionId((StringUtils.isNotEmpty(result[0]) && !"null".equalsIgnoreCase(result[0]))?Long.parseLong(result[0]):null); + //-- 任务类型 long + result2.setMissionType((StringUtils.isNotEmpty(result[1]) && !"null".equalsIgnoreCase(result[1]))?Long.parseLong(result[1]):null); + //- UUID long + result2.setUuid((StringUtils.isNotEmpty(result[2]) && !"null".equalsIgnoreCase(result[2]))?Long.parseLong(result[2]):null); + //- 周期标识 long + result2.setLoopFlag((StringUtils.isNotEmpty(result[3]) && !"null".equalsIgnoreCase(result[3]))?Long.parseLong(result[3]):null); + //- 开始执行时间 long + result2.setStartTime((StringUtils.isNotEmpty(result[4]) && !"null".equalsIgnoreCase(result[4]))?Long.parseLong(result[4]):null); + //- 结束时间 long + result2.setEndTime((StringUtils.isNotEmpty(result[5]) && !"null".equalsIgnoreCase(result[5]))?Long.parseLong(result[5]):null); + //- 配置截取文本 string + result2.setText((StringUtils.isNotEmpty(result[6])&& !"null".equalsIgnoreCase(result[6].trim()))?result[6]:null); + //- 结果状态 long + result2.setResult((StringUtils.isNotEmpty(result[7]) && !"null".equalsIgnoreCase(result[7]))?Long.parseLong(result[7]):null); + //- 结果描述 string + if(result.length>8){ + result2.setDescription((StringUtils.isNotEmpty(result[8])&& !"null".equalsIgnoreCase(result[8].trim()))?(format.format(result2.getStartTime())+">> "+result[8].trim()):null); + } + return result2; + } catch (Exception e) { + logger.error("",e); + } + return null; + } + + //保存缓存中的监测数据到硬盘 + public static void saveDetectDataToDisk() { + LinkedList notResolvingDetectDataList = Common.getNotResovlingDeteDataList(); // 获取未在解析的监测数据集合 + try { + //终止正在解析监测数据的线程,并将正在解析的list里未解析的数据存入硬盘 + Future future = Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + future.cancel(true); + } + LinkedList detectDataList = Common.getDeteDataList(); // 获取非存放状态的数据集合(正在解析的list) + saveByteToFile(detectDataList); + + //将未在解析的list存放到硬盘上 + saveByteToFile(notResolvingDetectDataList); + + }catch(IndexOutOfBoundsException e) { + logger.error("When monitoring data is too much, the data is stored in the hard disk",e); + }catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + + } + public static void saveByteToFile(LinkedList dataList) { + String filePath = ""; + int dataSize = dataList.size();//如果不按此时的size来操作,则list里的数据可能会一直在增加,就会一直循环下去? + try { + for(int j=0;j notResolvingWarningDataList = Common.getNotResovlingWarnDataList(); // 获取未在解析的告警数据集合 + try { + + //终止正在解析告警数据的线程,并将正在解析的list里未解析的数据存入硬盘 + Future future = Common.getFutureMap().get(Constants.ALARM_DATA_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + future.cancel(true); + } + LinkedList warningDataList = Common.getAlarmDataList(); // 获取非存放状态的数据集合(正在解析的list) + saveStrToFile(warningDataList); + + //将未在解析的list存放到硬盘上 + saveStrToFile(notResolvingWarningDataList); + + + }catch(IndexOutOfBoundsException e) { + logger.error("When the alarm data is too much, the data is stored in the hard disk",e); + }catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + + } + private static void saveStrToFile(LinkedList dataList) { + String filePath = ""; + int dataSize = dataList.size();//如果不按此时的size来操作,则list里的数据可能会一直在增加,就会一直循环下去? + try { + for(int j=0;j future = Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + future.cancel(true);//停解析任务结果数据入库线程 + } + + for(int i=1 ;i<=6 ; i++){ + //-- 获取结果集合 + LinkedList result = Common.getResultListClone(i); + + //-- 集合不为空则进行解析 存在removeList情况 ,优化方式可参考 监测数据解析的实现方式 + if(result!=null && result.size()>0){ + //将未在解析的list存放到硬盘上 + saveMissObjToFile(result,i); + result.clear(); //清除mr2List + } + } + + }catch(IndexOutOfBoundsException e) { + logger.error("When the result data is too much, the data is stored in the hard disk exception",e); + }catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + + } + + // 收集数据前先进行握手监测通信 + public static DetectInfo doNmsClientDetect(String ip,SetInfo setInfo,long startTime) { + logger.info("开始握手监测>" + ip); + DetectInfo detectInfo = null; + try { + // -- 发送握手命令 + String sendTime = Calendar.getInstance().getTimeInMillis() + ""; + // sendMessage(SocketCMD.CLIENT_REQ_HAND_SHAKE);//DC与NC握手通信后,NC会关闭socket,导致无法收集数据 + // String msg = receiveMessage();//收到回复的信息后,说明握手成功了 + + String backTime = Calendar.getInstance().getTimeInMillis() + ""; + // -- 信息保存到监测信息对象中 + detectInfo = new DetectInfo(); + + StringBuffer nmsClientDetailDetectInfo = new StringBuffer(); + nmsClientDetailDetectInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);// 发送信息 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append(SocketUtils.SUCCESS + ":" + + SocketCMD.CLIENT_REQ_HAND_SHAKE + "|" + sendTime);// 返回信息:DC端伪造的,因为也不需要NC回复信息了 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append("0");// 是否可达,0:可达,1:不可达 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append(sendTime);// 发送时间 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append(backTime);// 返回时间 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append(DateUtil.getSecondsFromBeinToEnd( + new Date(Long.parseLong(sendTime)).getTime(), new Date(Long + .parseLong(backTime)).getTime()) + + "");// 延迟时间 + + detectInfo.getDetailDatas().add( + nmsClientDetailDetectInfo.toString()); + +// detectInfo.setDescInfo("开始" +// + DateUtil.getStringByLongStr(sendTime) +// + " NMSClient握手" +// + DateUtil.getStringByLongStr(backTime) +// + " 成功 "); +// detectInfo.setDescInfo("Started" +// + DateUtil.getStringByLongStr(sendTime) +// + " NMSClient handshake" +// + DateUtil.getStringByLongStr(backTime) +// + " success "); + detectInfo.setDescInfo("i18n_server.Common.nmsShakeHandBegin_n81i" + + DateUtil.getStringByLongStr(sendTime) + + " i18n_server.Common.nmsShakeHand_n81i" + + DateUtil.getStringByLongStr(backTime) + + " i18n_server.Common.nmsShakeHandSuccess_n81i "); + + } catch (Exception e) { + logger.error("Generating node:"+ip+"information anomaly", e); + }finally{ + if (Thread.currentThread().isInterrupted()) { + // 线程被中断,结束收集线程 + logger.info("收集线程 被中断"); + return detectInfo; + }else { + Common.createNmsClientDetectData(setInfo, ip, detectInfo, startTime); + } + } + + return detectInfo; + } + + public static DetectInfo errorNodeToDo(String threadName,boolean nmsclientFlag, SetInfo setInfo,String ip,Long checkTime,long startTime){ +// Thread.currentThread().setName(threadName + "(异常节点)"); + Thread.currentThread().setName(threadName + "(Abnormal node)"); + if (Thread.currentThread().isInterrupted()) { + // 线程被中断,结束收集线程 + logger.info("收集线程 被中断"); + return null; + } + if(!nmsclientFlag){//对于不需要进行握手监测的节点,则不进行握手 + return null; + } + DetectInfo detectInfo = null; + String stateInfo = ""; + try{ + detectInfo = new DetectInfo(); + String failStartTime = Calendar.getInstance().getTimeInMillis() + + ""; + long maxTimes = 1l; + + if (setInfo != null && setInfo.getCheckMaxTimes() != null) { + maxTimes = setInfo.getCheckMaxTimes(); + } + + String[] pingData = pingHandshake(ip, maxTimes); + + String failEndTime = Calendar.getInstance().getTimeInMillis() + ""; + + if ((StringUtils.isEmpty(pingData[1]) || "0".equals(pingData[1]))) {// ping不通,无握手监测详细数据 +// stateInfo = "网络异常"; +// stateInfo = "Network abnormality"; + stateInfo = "i18n_server.Common.netErr_n81i"; + } else { + // 可以ping通进行ssh测试 如果ssh可以通 就产生一条握手失败信息 如果ssh不同就产生一条ssh失败信息 + String sshResult = Constants.SUCCESS; + if(1==Constants.FLAG_HANDWALK_SSH){ + String[] userPwdInfo = Common.getIpUserPwdMap().get(ip); + if (userPwdInfo!=null && + userPwdInfo.length>=3 && "1".equals(userPwdInfo[2])) {//应该判断nc的操作系统,不是dc的,1:linux; 2:windows; 3:other + sshResult = sshConnect(ip,userPwdInfo[0],userPwdInfo[1]); + } + } + + if (Constants.SUCCESS.equals(sshResult)) { +// stateInfo = "开始" + DateUtil.getStringByLong(checkTime) +// + " NMSClient握手" + DateUtil.getCurrentTime() + " 失败 "; +// stateInfo = "Started" + DateUtil.getStringByLong(checkTime) +// + " NMSClient handshake" + DateUtil.getCurrentTime() + " failed "; + stateInfo = "i18n_server.Common.nmsShakeHandBegin_n81i" + DateUtil.getStringByLong(checkTime) + + " i18n_server.Common.nmsShakeHand_n81i" + DateUtil.getCurrentTime() + " i18n_server.Common.nmsShakeHandFail_n81i "; + }else{ + stateInfo = sshResult; + } + StringBuffer detailInfo = new StringBuffer(); + detailInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);// 发送信息 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append("");// 返回信息 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append("1");// 是否可达,0:可达,1:不可达 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(failStartTime);// 发送时间 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(failEndTime);// 返回时间 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(DateUtil.getSecondsFromBeinToEnd(new Date( + Long.parseLong(failStartTime)).getTime(), new Date(Long + .parseLong(failEndTime)).getTime()) + + "");// 延迟时间 + detectInfo.getDetailDatas().add(detailInfo.toString()); + } + }catch (Exception e) { + logger.error("Generating abnormal nodes:"+ip+"information anomaly", e); + }finally{ + detectInfo.setDescInfo(stateInfo); + logger.info("与 " + ip + " 的握手监测发生异常 失败:" + stateInfo); + createNmsClientDetectData(setInfo,ip,detectInfo, startTime);//生成监测数据信息 + } + return detectInfo; + } + + //创建握手监测信息 + public static void createNmsClientDetectData(SetInfo setInfo,String ip,DetectInfo detectInfo,long startTime){ + if(detectInfo!=null){ + int testTimes = 1; + Long checkTime = System.currentTimeMillis(); + detectInfo.setTestTimes(testTimes);//尝试次数 + detectInfo.setCheckTime(checkTime);//监测时间 + try{ + /** + * -- 监测数据描述信息非空(web界面的状态信息),才生成监测数据: + * 1、监测数据为空可能是收集线程超时导致ping或者ssh未执行完毕,未生成描述信息 + * 2、此时web端进行超周期设置时,过滤掉异常的节点,异常节点的异常信息还是采用之前周期的-此功能 20160906 web端还未实现 + */ + if(StringUtils.isNotBlank(detectInfo.getDescInfo())){ + MonitorUtil.createMonitorData(ip, setInfo, startTime, Common.getAlarmInfoMap().get(setInfo.getId()+ ""), detectInfo); + } + } catch (Exception ex) { + logger.error(ExceptionPrintUtils.printExceptionStack(ex)); + String processIden = setInfo.getProcessIden()==null ? "" : setInfo.getProcessIden(); + String[] alarm = Common.alarmExceptionInfo(setInfo.getId(), ip, Constants.DETEC_NMSC_STR,processIden , startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); + Common.addAlarmData(alarm); + } + } + } + + public static List pingHandshake(List nodetectdatanodelist) { + List list = new ArrayList(); + StringBuffer commond = new StringBuffer(); + String system = (String) (System.getProperty("os.name")).toLowerCase(); + Process process = null; + BufferedReader in = null; + long a = new Date().getTime(); + for(String str : nodetectdatanodelist){ + if (system.toLowerCase().indexOf("win") != -1) { + commond.append("ping -n 4 "+ str +" & "); + } else if (system.toLowerCase().indexOf("linux") != -1) { + commond.append("ping -c 4 "+ str +" ; "); + } else { + commond.append("ping -w 4 "+ str +" & "); + } + } + if(commond.length()>1){ + commond=commond.delete(commond.length()-2, commond.length()-1); + } + logger.info("异常节点列表ping commond:"+commond); + + String[] cmd = new String[3] ; + if (system.toLowerCase().indexOf("win") != -1) { + cmd[0] = "cmd"; + cmd[1] = "/C"; + cmd[2] = commond.toString(); + } else if (system.toLowerCase().indexOf("linux") != -1) { + cmd[0] = "/bin/sh"; + cmd[1] = "-c"; + cmd[2] = commond.toString(); + } + try { + process = Runtime.getRuntime().exec(cmd); + in = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line=""; + while ((line = in.readLine()) != null) { + System.out.println(line.toLowerCase()); + if(line.toLowerCase().indexOf("ttl")>0){ + String[] str = line.split(" "); + String ip = ""; + if (system.toLowerCase().indexOf("win") != -1) { + ip = str[1]; + }else if(system.toLowerCase().indexOf("linux") != -1){ + ip = str[3].substring(0,str[3].length()-1); + } + if(!StringUtils.isBlank(ip)&&!list.contains(ip)){ + list.add(ip.trim()); + } + } + } + long b = System.currentTimeMillis(); + logger.info("-----------执行ping命令 节点数量:"+nodetectdatanodelist.size()+" 总耗时:"+(b-a)+"毫秒, "+(b-a)/1000+"秒"); + for(String s : list){logger.info(" ping 成功的ip:"+s); } + } catch (IOException e) { + e.printStackTrace(); + } + return list; + } + + private static String[] pingHandshake(String ip, long maxTimes) { + + String command = ""; // 命令语句 + int minTime = Integer.MAX_VALUE, maxTime = 0, totalTime = 0, curTime; // 最短时间,最长时间,总时间,时间变量 + int snum = 0, fnum = 0; // 发包成功和失败数 + + // 判断系统类型 win or Linux + String system = (String) (System.getProperty("os.name")).toLowerCase(); + + if (system.toLowerCase().indexOf("win") != -1) { + command += "ping -n 4 " + ip;// 尝试次数 -n + } else if (system.toLowerCase().indexOf("linux") != -1) { + command += "ping -c 4 " + ip; + } else { + command += "ping -w 4 " + ip; + } + + String stateInfo = ""; + String[] datas2 = new String[7]; + Process process = null; + BufferedReader in = null; // 读取 Ping命令返回的信息 + StringBuffer buffer = new StringBuffer(); + try { + logger.debug("ping command:" + command); + process = Runtime.getRuntime().exec(command); + in = new BufferedReader(new InputStreamReader(process + .getInputStream())); + String line = null; + long count = maxTimes + 10; + int index; + // 最多多读10行 + while ((line = in.readLine()) != null && count != 0) { + if ("".equals(line)) { + count--; + continue; + } // 空串跳过 + buffer.append(line + "\n"); + line = line.toLowerCase(); + logger.debug("line:" + line); + if (line.indexOf("ttl") > 0) { // 获得成功响应的数据 + count--; // 计数器自减1 + // 截取time 值 + if ((index = line.lastIndexOf("ms")) != -1) { + byte[] buf = line.getBytes(); + int start = 0, end = buf.length, i, j; + // 下标末点 + for (i = index; i >= 0; i--) { + if (Character.isDigit((char) buf[i])) { + end = i; + break; + } + } + if (i == 0) + continue; + // 下标起点 + for (j = end; j >= 0; j--) { + if (!Character.isDigit((char) buf[j])) { + start = j + 1; + break; + } + } + // 时间值截取 + curTime = Integer.parseInt(new String(buf, start, end + + 1 - start)); + snum++; // 成功接收次数加1 + totalTime += curTime; // 计算总时间 + + if (curTime < minTime) { + minTime = curTime; + } // 最小时间 + + if (curTime > maxTime) { + maxTime = curTime; + } // 最大时间 + } + } else if (line.split(" ").length < 4) { + count--; // 计数器自减1 + fnum++; // 失败接收次数加1 + } else { + stateInfo += line + "\n"; + } + } + + if (totalTime == 0) { + minTime = 0; + } + + // 已发送包数 + datas2[0] = "" + (snum + fnum); + datas2[1] = "" + snum; + datas2[2] = "" + fnum; + + BigDecimal fnum0 = new BigDecimal(fnum); + BigDecimal tnum0 = new BigDecimal(snum + fnum); + DecimalFormat df = new DecimalFormat("####0.00"); + if (tnum0.intValue() != 0) { + datas2[3] = "" + + ((new BigDecimal(df.format(fnum0.divide(tnum0, 4, + RoundingMode.HALF_UP)))) + .multiply(new BigDecimal(100))); + } + datas2[4] = "" + minTime; + datas2[5] = "" + maxTime; + datas2[6] = "" + (snum == 0 ? 0 : (totalTime / snum)); + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + logger.error("", e); + } + } + if (process != null) + process.destroy(); + process = null; + } + + /* + * if(state==-1){ isSuccess = false; }else{ isSuccess = true; } + */ + return datas2; + } + + //批量ping方法:方法1:使用nmap命令; 方法2: +// private static List pingHandshakeBatch(List ipList, long maxTimes) { +// +//// List commandList = new ArrayList(); // 命令语句 +// List datasList = new ArrayList(); // 命令语句 +// int minTime = Integer.MAX_VALUE, maxTime = 0, totalTime = 0, curTime; // 最短时间,最长时间,总时间,时间变量 +// int snum = 0, fnum = 0; // 发包成功和失败数 +// +// // 判断系统类型 win or Linux +// String system = (String) (System.getProperty("os.name")).toLowerCase(); +// String os = ""; +// if(system.toLowerCase().indexOf("win") != -1){ +// os = Constants.OS_WIN; +// }else if (system.toLowerCase().indexOf("linux") != -1) { +// os = Constants.OS_LINUX; +// } +// String command = ""; // 命令语句 +// Process process = null; +// BufferedReader in = null; // 读取 Ping命令返回的信息 +// try { +// for(String ip:ipList){ +// if (Constants.OS_WIN.equals(os)) { +// // command += "ping -n 4 " + ip+" & ";// 尝试次数 -n +// command = "ping -n 4 " + ip;// 尝试次数 -n +// } else if (Constants.OS_LINUX.equals(os)) { +// // command += "ping -c 4 " + ip+" ; "; +// command = "ping -c 4 " + ip; +// } else { +// command = "ping -w 4 " + ip;//??? +// } +// // commandList.add(command); +// +// +// String stateInfo = ""; +// String[] datas2 = new String[7]; +// +// StringBuffer buffer = new StringBuffer(); +// +// logger.debug("ping command:" + command); +// // String[] cmds = new String[commandList.size()]; +// +// process = Runtime.getRuntime().exec(command); +// in = new BufferedReader(new InputStreamReader(process +// .getInputStream())); +// String line = null; +// long count = maxTimes + 10; +// int index; +// // 最多多读10行 +// while ((line = in.readLine()) != null && count != 0) { +// if ("".equals(line)) { +// count--; +// continue; +// } // 空串跳过 +// buffer.append(line + "\n"); +// line = line.toLowerCase(); +// logger.debug("line:" + line); +// if (line.indexOf("ttl") > 0) { // 获得成功响应的数据 +// count--; // 计数器自减1 +// // 截取time 值 +// if ((index = line.lastIndexOf("ms")) != -1) { +// byte[] buf = line.getBytes(); +// int start = 0, end = buf.length, i, j; +// // 下标末点 +// for (i = index; i >= 0; i--) { +// if (Character.isDigit((char) buf[i])) { +// end = i; +// break; +// } +// } +// if (i == 0) +// continue; +// // 下标起点 +// for (j = end; j >= 0; j--) { +// if (!Character.isDigit((char) buf[j])) { +// start = j + 1; +// break; +// } +// } +// // 时间值截取 +// curTime = Integer.parseInt(new String(buf, start, end +// + 1 - start)); +// snum++; // 成功接收次数加1 +// totalTime += curTime; // 计算总时间 +// +// if (curTime < minTime) { +// minTime = curTime; +// } // 最小时间 +// +// if (curTime > maxTime) { +// maxTime = curTime; +// } // 最大时间 +// } +// } else if (line.split(" ").length < 4) { +// count--; // 计数器自减1 +// fnum++; // 失败接收次数加1 +// } else { +// stateInfo += line + "\n"; +// } +// } +// +// if (totalTime == 0) { +// minTime = 0; +// } +// +// // 已发送包数 +// datas2[0] = "" + (snum + fnum); +// datas2[1] = "" + snum; +// datas2[2] = "" + fnum; +// +// BigDecimal fnum0 = new BigDecimal(fnum); +// BigDecimal tnum0 = new BigDecimal(snum + fnum); +// DecimalFormat df = new DecimalFormat("####0.00"); +// if (tnum0.intValue() != 0) { +// datas2[3] = "" +// + ((new BigDecimal(df.format(fnum0.divide(tnum0, 4, +// RoundingMode.HALF_UP)))) +// .multiply(new BigDecimal(100))); +// } +// datas2[4] = "" + minTime; +// datas2[5] = "" + maxTime; +// datas2[6] = "" + (snum == 0 ? 0 : (totalTime / snum)); +// +// datasList.add(datas2); +// } +// } catch (Exception e) { +// logger.error(ExceptionPrintUtils.printExceptionStack(e)); +// } finally { +// if (in != null) { +// try { +// in.close(); +// } catch (IOException e) { +// logger.error("", e); +// } +// } +// if (process != null) +// process.destroy(); +// process = null; +// } +// /* +// * if(state==-1){ isSuccess = false; }else{ isSuccess = true; } +// */ +// return datasList; +// } + + public static String sshConnect(String hostname, String username, String password) { + String result = Constants.SUCCESS; + long a = new Date().getTime(); + Connection conn = null; + Session sess = null; + try { + if(StringUtils.isNotBlank(hostname)){ + conn = new Connection(hostname); + logger.debug("SSHConnect----链接ip:"+hostname+" 用户名:"+username+" 密码:"+password); + conn.connect(null,Constants.SSH_CONNECT_TIMEOUT,Constants.SSH_CONNECT_TIMEOUT); + long b = new Date().getTime(); + logger.debug("SSHConnect----链接ip:"+hostname+"耗时:" + (b - a) + "毫秒," + (b - a) + / 1000 + "秒"); + boolean isAuthenticated = conn.authenticateWithPassword(username, + password); + if (isAuthenticated == true) { + sess = conn.openSession(); + sess.execCommand("date"); + InputStream stdout = new StreamGobbler(sess.getStdout()); + BufferedReader stdoutReader = new BufferedReader( + new InputStreamReader(stdout, Charset.forName("utf-8"))); + while (true) { + String line = stdoutReader.readLine(); + if (line == null){ + break; + } + } + }else{ +// result = "SSH 登录用户名或密码错误"; +// result = "SSH login username or password error"; + result = "i18n_server.Common.sshLogin_n81i"; + } + long c = new Date().getTime(); + logger.debug("SSHConnect----测试完成ip:"+hostname+",总耗时:" + (c - a) + "毫秒," + (c - a)/1000 + "秒"); + } + } catch (IOException e) { + long d = new Date().getTime(); + logger.error("SSHConnect----Connection exception IP:"+hostname+",Time consuming:" + (d - a) + "Millisecond," + (d - a) + / 1000 + "Second", e); +// result = "SSH 连接失败"; + result = "i18n_server.Common.sshConnectionFail_n81i"; + } catch (Exception e1) { + long e = new Date().getTime(); + logger.error("SSHConnect----Connection exception IP:"+hostname+",Time consuming:" + (e - a) + "Millisecond," + (e - a) + / 1000 + "Second", e1); +// result = "SSH 连接失败"; +// result = "SSH connection failure"; + result = "i18n_server.Common.sshConnectionFail_n81i"; + }finally { + if(conn!=null){ + conn.close(); + } + if(sess!=null){ + sess.close(); + } + } + return result; + } + + + + //写一个任务对象到一个文件后,清除列表里的这个任务对象 + public static void saveMissObjToFile(LinkedList missObjList,int missionType) { + String filePath = ""; + int missObjSize = missObjList.size();//如果不按此时的size来操作,则list里的数据可能会一直在增加,就会一直循环下去? + try { + String parPath = Constants.OVERRUN_RESULT_FILE_DIR.substring(0,Constants.OVERRUN_RESULT_FILE_DIR.length()-1); + LinkedList tmpList = new LinkedList(); + for(int j=0;j detecFuture = null; + Future detecFuture2 = null; + if(Constants.DETECT_INSERT_THREAD_MODE ==1){//单线程模式 + //-- 关闭线程 清理 监测数据 + if(Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE) != null){ + Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE).cancel(true);//此处可能没结束成功 + } + LinkedList dsbList = Common.getDeteDataList(); +// Future detecFuture = Common.scheduled.submit(new NewDetecDataResoveThread("Server关闭前_缓存监测数据入库1",dsbList)); + detecFuture = Common.scheduled.submit(new NewDetecDataResoveThread("Server Before Closing The Cache Monitoring Data Warehouse 1",dsbList)); +// Common.chengeDeteDataFlag(); +// LinkedList dsbList2 = Common.getDeteDataList(); +// Future detecFuture2 = Common.scheduled.submit(new NewDetecDataResoveThread("Server关闭前_缓存监测数据入库2",dsbList2)); +// Future detecFuture2 = Common.scheduled.submit(new NewDetecDataResoveThread("Server Before Closing The Cache Monitoring Data Warehouse 2",dsbList2)); + }else if(Constants.DETECT_INSERT_THREAD_MODE == 2){//多线程入库 + detecFuture = Common.service.submit(new DataInsertManagerThread()); + detecFuture2 = Common.service.submit(new DetectInfoInsertThread()); + } + logger.info("缓存监测数据 清理中"); + + //-- 关闭线程 清理 告警数据 + if(Common.getFutureMap().get(Constants.ALARM_DATA_RESOVE) != null){ + Common.getFutureMap().get(Constants.ALARM_DATA_RESOVE).cancel(true); + } + LinkedList adList = Common.getAlarmDataList(); +// Future alarmFuture = Common.scheduled.submit(new AlarmDataResoveThread("Server关闭前_缓存告警数据入库1",adList)); + Future alarmFuture = Common.scheduled.submit(new AlarmDataResoveThread("Server Before Closing The Cache Alarm Data Into The Library 1",adList)); + Common.changeAlarmDataFlag(); + LinkedList adList2 = Common.getAlarmDataList(); +// Future alarmFuture2 = Common.scheduled.submit(new AlarmDataResoveThread("Server关闭前_缓存告警数据入库2",adList2)); + Future alarmFuture2 = Common.scheduled.submit(new AlarmDataResoveThread("Server Before Closing The Cache Alarm Data Into The Library 2",adList2)); + logger.info("缓存告警数据 清理中"); + + //-- 关闭线程 清理 任务结果数据 + if(Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE) != null){ + Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE).cancel(true); + } +// Future mrFuture = Common.scheduled.submit(new MissionResultThread("Server关闭前_缓存任务结果入库")); + Future mrFuture = Common.scheduled.submit(new MissionResultThread("Server Before Closing The Result Of Caching Task")); + logger.info("缓存任务结果 清理中"); + + + //超时未处理完 + try { + if(detecFuture != null){ + detecFuture.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + } + if(detecFuture2 != null){ + detecFuture2.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + } + logger.info("缓存监测数据 清理结束"); + + alarmFuture.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + alarmFuture2.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + logger.info("缓存告警数据 清理结束"); + + mrFuture.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + logger.info("缓存任务结果 清理结束"); + + } catch (Exception e) { + logger.warn("DC handles (parse, store) cache data timeout, confirm shutdown"); + } + } + + //将缓存中的数据存至硬盘 + public static void saveDataInMemToDisk(){ + try{ + logger.info("缓存数据存至硬盘..."); + logger.info("缓存 监测数据 存至硬盘..."); + Common.saveDetectDataToDisk();//监测数据 + logger.info("缓存 监测数据 存至硬盘 完成"); + + logger.info("缓存 告警数据 存至硬盘..."); + Common.saveWarningDataToDisk();//告警数据 + logger.info("缓存 告警数据 存至硬盘 完成"); + + logger.info("缓存 结果数据 存至硬盘..."); + Common.saveResultDataToDisk();//结果数据 + logger.info("缓存 结果数据 存至硬盘 完成"); + }catch(Exception e){ + logger.error("将缓存中的数据存至硬盘 异常",e); + } + } + + public static void printMemUse(){ + Runtime.getRuntime().gc(); + long totleByte = Runtime.getRuntime().totalMemory(); + long freeByte = Runtime.getRuntime().freeMemory(); + logger.info("占用内存:目前总内存:"+(totleByte/1024/1024)+"M 空闲内存:"+(freeByte/1024/1024)+"M 占用内存:"+((totleByte-freeByte)/1024/1024)+"M"); + + } + + + public static void main(String [] args){ + + String result = "261$@$4$@$1$@$0$@$1358241982955$@$1358241996202$@$$@$0$@$"; + resoveMissionResult(result.split(Constants.COMMON_DATA_SPLIT)); + List ipList = new ArrayList(); + ipList.add("10.0.6.102"); + ipList.add("10.0.6.100"); +// pingHandshakeBatch(ipList, 20); + } + /** + * 原数据表及Columns结构集合 + * key : CheckTypeName + * value : Columns结构集合 + * @time Apr 9, 2012-3:36:09 PM + * @return + */ + public static Map> getTableMap() { + return tableMap; + } + public static Map> getFutureMap() { + return futureMap; + } + public static Map> getAlarmInfoMap() { + return alarmInfoMap; + } + public static Map getIpSeqIdMap() { + return IP_UUIDMap; + } + public static Map getIpUserPwdMap() { + return IP_USERPWDMap; + } + + public static void putSimpleNode(Long uuid, SimpleNode node) { + nodeInfoMape.put(uuid, node); + } + public static LinkedList getReserveMissionListClone() { + synchronized (reserveMissionList) { + return (LinkedList) reserveMissionList.clone(); + } + } + public static void removeMissionToReserveMissionList(int i) { + synchronized (reserveMissionList) { + reserveMissionList.remove(i); + } + } + public static void removeMissionToReserveMissionList(MissionStateTable mission) { + synchronized (reserveMissionList) { + reserveMissionList.remove(mission); + } + } + public static void addMissionToReserveMissionList(MissionStateTable mission) { + synchronized (reserveMissionList) { + reserveMissionList.add(mission); + } + } + public static void addMissionToReserveMissionList(int i,MissionStateTable mission) { + synchronized (reserveMissionList) { + reserveMissionList.add(i,mission); + } + } + + //下发失败任务 +// public static void removeMissionToAssignFailMissionList(int i) { +// synchronized (assignFailMissionList) { +// assignFailMissionList.remove(i); +// } +// } +// public static void removeMissionToAssignFailMissionList(Map missSeqMap) { +// synchronized (assignFailMissionList) { +// assignFailMissionList.remove(missSeqMap); +// } +// } +// public static void addMissionToAssignFailMissionList(Map missSeqMap) { +// synchronized (assignFailMissionList) { +// assignFailMissionList.add(missSeqMap); +// } +// } +// public static void addMissionToAssignFailMissionList(int i,Map missSeqMap) { +// synchronized (assignFailMissionList) { +// assignFailMissionList.add(i,missSeqMap); +// } +// } + //----- + public static MissionPollingThread getFirstStartThread() { + return firstStartThread; + } + +// public static AssignMissionAgainThread getAssignAgainThread() +// { +// return assignAgainThread; +// } + public static Map getLoopMissionInfoMap() { + return loopMissionInfoMap; + } + public static Map> getLoopMissionRoundInfoList() { + return loopMissionRoundInfoList; + } + + public static Map> getBpDownMissionFilesMap() { + return bpDownMissionFilesMap; + } + +// public static Map getAlarmSetInfoMap() { +// return alarmSetInfoMap; +// } + + public static Snmp getSnmp() { + return snmp==null ? SNMP4JUtils.createSnmp():snmp; + } + public static ServerTable getServerTable() { + return serverTable; + } + public static void setServerTable(ServerTable serverTable) { + Common.serverTable = serverTable; + } + + + public static SetInfo getSnmpTrapSetInfo() { + return snmpTrapSetInfo; + } + + + public static void setSnmpTrapSetInfo(SetInfo snmpTrapSetInfo) { + Common.snmpTrapSetInfo = snmpTrapSetInfo; + } + + + public static Map getSetInfoNameMape() { + return setInfoNameMape; + } + + + public static Map getCheckTypeNameMape() { + return checkTypeNameMape; + } + + + +// public static List> getAssignFailMissionList() +// { +// return assignFailMissionList; +// } + + public static List getAssignFailMissIds() + { + return assignFailMissIds; + } + + public static void addAssignFailMissionIds(String missId) { + synchronized (assignFailMissIds) { + assignFailMissIds.add(missId); + } + } + + public static void addAllAssignFailMissionIds(List missIds) { + synchronized (assignFailMissIds) { + assignFailMissIds.addAll(missIds); + } + } + + public static void removeAllAssignFailMissionIds() { + synchronized (assignFailMissIds) { + assignFailMissIds.clear(); + } + } + + public static long getLastWebHandshake() { + return LAST_WEB_HANDSHAKE; + } + + public static void setLastWebHandshake(long LastWebHandshake) { + LAST_WEB_HANDSHAKE = LastWebHandshake; + } + + + + /** + * 2018年7月30日 修改监测数据存放位置,改为保存到队列 中 + */ + private static final LinkedBlockingDeque DETE_DATA_QUEUE = new LinkedBlockingDeque(); + + /** + * 保存监测数据到缓存队列 + * @param data + */ + public static void addDeteDataQueue(byte[] data){ + DETE_DATA_QUEUE.add(data); + } + + /** + * 保存监测数据到缓存队列 + * @param data + */ + public static void addDeteDataQueue(List data){ + DETE_DATA_QUEUE.addAll(data); + } + + /** + * 从监测数据缓存队列中 获取元素 + * @return + * @throws InterruptedException + */ + public static byte[] takeDeteDataQueue() throws InterruptedException{ + return DETE_DATA_QUEUE.take(); + } + + /** + * 非阻塞 从监测数据缓存队列中 获取元素 + * @return + * @throws InterruptedException + */ + public static byte[] pollDeteDataQueue(){ + return DETE_DATA_QUEUE.poll(); + } + + /** + * 程序是否停止运行 + * fasle:运行 + * true:停止 + * @return + */ + public static boolean isStop(){ + return STOP; + } + + /** + * 程序停止运行 + */ + public static synchronized void stop(){ + STOP = true; + } + + public static void initDb(Db db){ + Common.db = db; + } + + public static Db getDb(){ + return Common.db; + } + + /** + * 将监测数据缓存到队列,等待入库线程入库 + * @param type + * @param obj + */ + public synchronized static void putDetectQueue(String type,Map detail){ + LinkedBlockingDeque queue = DETECT_QUEUE.get(type); + if(queue == null){ + queue = new LinkedBlockingDeque(); + DETECT_QUEUE.put(type, queue); + } + queue.add(detail); + } + + public synchronized static void putDetectInfoQueue(Object[] info){ + LinkedBlockingDeque queue = DETECT_QUEUE.get(DetectInfo.INFO_KEY); + if(queue == null){ + queue = new LinkedBlockingDeque(); + DETECT_QUEUE.put(DetectInfo.INFO_KEY, queue); + } + queue.add(info); + } + + /** + * 添加多条 + * @param type + * @param details + */ + public synchronized static void putAllDetectQueue(String type,List>details){ + LinkedBlockingDeque queue = DETECT_QUEUE.get(type); + if(queue == null){ + queue = new LinkedBlockingDeque(); + DETECT_QUEUE.put(type, queue); + } + queue.addAll(details); + } + + /** + * 保存 心跳信息 + * @param ip + * @param key + * @param value + */ + public static void saveHbInfo(String ip,String key,Object value){ + Hashtable hashtable = HEARTBEAT_IPS.get(ip); + if(hashtable == null){ + hashtable = new Hashtable(); + HEARTBEAT_IPS.put(ip, hashtable); + } + hashtable.put(key, value); + } + + /** + * 获取 心跳信息 + * @param ip + * @param key + * @return + */ + public static T getHbInfo(String ip,String key){ + Hashtable hashtable = HEARTBEAT_IPS.get(ip); + if(hashtable != null){ + return (T) hashtable.get(key); + } + return null; + } + + + public static int byteArrayToInt(byte[] b) { + return b[3] & 0xFF | + (b[2] & 0xFF) << 8 | + (b[1] & 0xFF) << 16 | + (b[0] & 0xFF) << 24; + } + + public static byte[] intToByteArray(int a) { + return new byte[] { + (byte) ((a >> 24) & 0xFF), + (byte) ((a >> 16) & 0xFF), + (byte) ((a >> 8) & 0xFF), + (byte) (a & 0xFF) + }; + } +} diff --git a/src/com/nms/server/common/CommonResources.java b/src/com/nms/server/common/CommonResources.java new file mode 100644 index 0000000..d6c5a08 --- /dev/null +++ b/src/com/nms/server/common/CommonResources.java @@ -0,0 +1,68 @@ +package com.nms.server.common; + +import java.util.ListResourceBundle; + +public class CommonResources extends ListResourceBundle{ + static final Object[][] contents = new String[][]{ +// { "ms_1", "已创建" }, +// { "ms_2", "进行中" }, +// { "ms_3", "已完成" }, +// { "ms_30", "全部成功" }, +// { "ms_31", "全部失败" }, +// { "ms_32", "部分成功" }, +// { "ms_4", "未能执行" }, +// { "ms_5", "撤销准备" }, +// { "ms_6", "撤销开始" }, +// { "ms_7", "撤销完成" }, +// { "mt_1", "推送文件" }, +// { "mt_4", "命令执行" }, +// { "mt_6", "升级部署" }, +// { "loop_0", "非周期任务" }, +// { "loop_1", "周期任务" }, +// { "ec_1", "任务开始下发" }, +// { "ec_4", "任务开始下发" }, +// { "ec_6", "任务开始下发"}}; + +// { "ms_1", "Created" }, +// { "ms_2", "In progress" }, +// { "ms_3", "Completed" }, +// { "ms_30", "All succeeded" }, +// { "ms_31", "All failed" }, +// { "ms_32", "Partially successful" }, +// { "ms_4", "Failed to execute" }, +// { "ms_5", "Undo preparation" }, +// { "ms_6", "Revocation starts" }, +// { "ms_7", "Undo completed" }, +// { "mt_1", "Push File" }, +// { "mt_4", "Command execution" }, +// { "mt_6", "Upgrade deployment" }, +// { "loop_0", "Acyclic tasks" }, +// { "loop_1", "Period tasks" }, +// { "ec_1", "Mission started" }, +// { "ec_4", "Mission started" }, +// { "ec_6", "Mission started"}}; + + + { "ms_1", "i18n_server.CommonResources.ms_1_n81i" }, + { "ms_2", "i18n_server.CommonResources.ms_2_n81i" }, + { "ms_3", "i18n_server.CommonResources.ms_3_n81i" }, + { "ms_30", "i18n_server.CommonResources.ms_30_n81i" }, + { "ms_31", "i18n_server.CommonResources.ms_31_n81i" }, + { "ms_32", "i18n_server.CommonResources.ms_32_n81i" }, + { "ms_4", "i18n_server.CommonResources.ms_4_n81i" }, + { "ms_5", "i18n_server.CommonResources.ms_5_n81i" }, + { "ms_6", "i18n_server.CommonResources.ms_6_n81i" }, + { "ms_7", "i18n_server.CommonResources.ms_7_n81i" }, + { "mt_1", "i18n_server.CommonResources.mt_1_n81i" }, + { "mt_4", "i18n_server.CommonResources.mt_4_n81i" }, + { "mt_6", "Upgrade deployment" }, + { "loop_0", "i18n_server.CommonResources.loop_0_n81i" }, + { "loop_1", "i18n_server.CommonResources.loop_1_n81i" }, + { "ec_1", "i18n_server.CommonResources.ec_1_n81i" }, + { "ec_4", "i18n_server.CommonResources.ec_1_n81i" }, + { "ec_6", "i18n_server.CommonResources.ec_1_n81i"}}; + public Object[][] getContents() { + return contents; + } + +} diff --git a/src/com/nms/server/common/Config.java b/src/com/nms/server/common/Config.java new file mode 100644 index 0000000..3eac2c1 --- /dev/null +++ b/src/com/nms/server/common/Config.java @@ -0,0 +1,159 @@ +package com.nms.server.common; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +import javax.swing.JOptionPane; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +/** + * 获取和保存myconfig信息的类 + * + * @author ZGGG3 + * + */ +public class Config { + private static Logger logger = Logger.getLogger(Config.class); + private static Properties properties; + private static String url = null; + public static Config config = Config.getInstance(); +// private static ResourceBundle resource; + + public static String getSystemDir() { + return System.getProperty("user.dir"); + } + + /** + * 单例 初始化获取实例 + * + * @return + */ + public static Config getInstance() { + if (config == null) + return new Config(); + return config; + } + + /** + * 单例 重新读取文件已获取参数 + * + * @return + */ + public static Config reinitialize() { + config = null; + return new Config(); + } + + /** + * 构造函数私有,用于单例实例 + */ + private Config() { + //执行参数更新及检查 + ConfigUpdate.getInstance(); + //加载配置文件 + URL urlObj = Config.class.getClassLoader().getResource("myconfig.properties"); +// resource = ResourceBundle.getBundle(DefaultConfig.class.getName()); + if(urlObj==null){ +// JOptionPane.showMessageDialog(null, "未找到参数配文件!\n请运行"+Constants.COMMON_CONFIG_EXE_NAME+"初始化参数配置", "错误", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(null, "Parameter configuration file not found! \nPlease run "+Constants.COMMON_CONFIG_EXE_NAME+"initialization parameter configuration", "Error", JOptionPane.ERROR_MESSAGE); +// JOptionPane.showMessageDialog(null, "i18n_server.Config.initConfig_n81i"+Constants.COMMON_CONFIG_EXE_NAME+"i18n_server.Config.initConfig.init_n81i", "i18n_server.Config.error_n81i", JOptionPane.ERROR_MESSAGE); + System.exit(0); + }else{ + url = urlObj.getPath().replaceAll("%20", " "); + } + properties = new Properties(); + try { + properties.load(new FileInputStream(url)); + } catch (IOException e) { + logger.error("Reading properties file error"+"",e); + } + } + /** + * 根据name获取value + * + * @param name + * @return + */ + public static Boolean getBoolan(String name,Boolean defaultValue) { + try { + String val = getString(name,defaultValue==null?null:defaultValue.toString()); + return StringUtils.isEmpty(val)? null : Boolean.valueOf(val); + } catch (Exception e) { + logger.error("Digital formatting error", e); + } + return null; + } + /** + * 根据name获取value + * + * @param name + * @return + */ + public static Integer getInteger(String name,Integer defaultValue) { + try { + String val = getString(name,defaultValue==null?null:defaultValue.toString()); + return StringUtils.isEmpty(val)? null : Integer.parseInt(val); + } catch (Exception e) { + logger.error("Digital formatting error", e); + } + return null; + } + /** + * 根据name获取value + * + * @param name + * @return + */ + public static String getString(String name,String defaultValue) { + String str = null; + /* 获取参数值:先从资源文件取值,如果为空,则从默认参数配置中取值 */ + str = properties.getProperty(name,defaultValue); + + if(StringUtils.isNotEmpty(str)){ + str = str.trim(); + }else{ + logger.warn("Resource configuration file abnormality >> "+name+" Value is empty"); + } + + return str; + } + + /** + * 向资源配置文件中添加或更新一个键值对 + * + * @time Jul 7, 2011-3:52:45 PM + * @param key + * @param value + */ + public static void setValueByName(String key, String value) { + // 添加或更新键值对 + properties.setProperty(key, value); + try { + // 保存到文件 + if (url != null && !"".equals(url)) { + String fileName = url.substring( + url.lastIndexOf(File.separator), url.length()); + properties.store(new FileOutputStream(url), fileName); + } + } catch (FileNotFoundException e) { + logger.error("The properties file Set Value file does not find ERROR "+"",e); + } catch (IOException e) { + logger.error("Properties file Set Value set value ERROR"+"",e); + } + } +/* + // 测试主函数 + public static void main(String[] args) { + Config cfg=new Config(); + String oid="mission.file.download.dir"; + System.out.println("---------"+cfg.getString(oid)); + } + */ +} diff --git a/src/com/nms/server/common/ConfigUpdate.java b/src/com/nms/server/common/ConfigUpdate.java new file mode 100644 index 0000000..2393011 --- /dev/null +++ b/src/com/nms/server/common/ConfigUpdate.java @@ -0,0 +1,264 @@ +package com.nms.server.common; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import java.util.ResourceBundle; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +/** + * 获取和保存myconfig信息的类 + * + * @author ZGGG3 + * + */ +public class ConfigUpdate { + private static Logger logger = Logger.getLogger(ConfigUpdate.class); + private static Properties properties; + private static String url = null; + public static List proList = new LinkedList(); + public static ConfigUpdate config = ConfigUpdate.getInstance(); + private static ResourceBundle resource; + /** + * 单例 初始化获取实例 + * + * @return + */ + public static ConfigUpdate getInstance() { + if (config == null) + return new ConfigUpdate(); + return config; + } + + /** + * 单例 重新读取文件已获取参数 + * + * @return + */ + public static ConfigUpdate reinitialize() { + config = null; + return new ConfigUpdate(); + } + + /** + * 构造函数私有,用于单例实例 + */ + private ConfigUpdate() { +// Thread.currentThread().setName("更新参数"); + resource = ResourceBundle.getBundle(UpdateParams.class.getName()); + + URL urlObj = ConfigUpdate.class.getClassLoader().getResource("myconfig.properties"); + if(urlObj!= null){ + url = urlObj.getPath().replaceAll("%20", " "); + }else{ + url = new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath()+"/conf/myconfig.properties"; + } + logger.info("参数文件:"+url); + BufferedReader reader = null; + FileInputStream fis = null; + properties = new Properties(); + + try { + File file = new File(url).getAbsoluteFile(); + /*外部资源文件是否已存在*/ + if(file.exists()){ // 存在 则读取已存在的文件信息 ,将内部资源文件参数更新到外部资源文件中 + fis = new FileInputStream(file); + properties.load(fis); + + //判断是否更新properties + String updateFlag = properties.getProperty(UpdateParams.CONFIG_UPDATE_FLAG,"-1"); + if(updateFlag.equals(resource.getString(UpdateParams.CONFIG_UPDATE_FLAG))){ //配置文件已经更新,退出操作 + return; + } + }else{ // 不存在无操作 + return ; + } + + //更新参数 + /*Enumeration en = resource.getKeys(); + while (en.hasMoreElements()) { + String elem = (String) en.nextElement(); + String value = properties.getProperty(elem); + if(StringUtils.isBlank(value)){ + properties.setProperty(elem, configRb.getString(elem)); + } + } + */ + fis.close(); + fis = new FileInputStream(file); + reader = new BufferedReader(new InputStreamReader(fis,Charset.forName("utf-8"))); + String str =null; + while((str = reader.readLine() )!=null){ + proList.add(str); + } + save(); + } catch (Exception e) { + logger.error("Reading properties file error"+"",e); + }finally{ + try { + if(reader!= null)reader.close(); + if(fis!= null)fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * 根据name获取value + * + * @param name + * @return + */ + public static Integer getInteger(String name) { + try { + return Integer.parseInt(getString(name)); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + /** + * 根据name获取value + * + * @param name + * @return + */ + public static String getString(String name) { + String str = null; + try { + str = properties.getProperty(name); + if(StringUtils.isNotEmpty(str)){ + str = str.trim(); + }else{ + logger.warn("Resource configuration file abnormality >> "+name+" Value is empty"); + } + } catch (Exception e) { + logger.error("Resource configuration file abnormality", e); + } + return str; + } + + /** + * 向资源配置文件中添加或更新一个键值对 + * + * @time Jul 7, 2011-3:52:45 PM + * @param key + * @param value + */ + public static void setValueByName(String key, String value) { + // 添加或更新键值对 + String oldValue = properties.getProperty(key); + properties.setProperty(key, value); + } + + /** + * 保存到文件 + * @time Jan 6, 2013-1:24:07 PM + */ + public static void save() { + BufferedWriter writer = null; + try { + //将properties中的值更新到proList中 + Enumeration en = resource.getKeys(); + while (en.hasMoreElements()) { + String elem = (String) en.nextElement(); + String value = resource.getString(elem); + boolean addFlag = true; + try { + for (int i = 0; i < proList.size(); i++) { + String str = proList.get(i); + if(StringUtils.isEmpty(str)){continue;} + if(str.split("=", 2)[0].trim().equals(elem)){ +// str = elem+" = "+value; +// proList.set(i, str); + addFlag = false; +// logger.info("参数更新:"+elem+" = "+(StringUtils.isBlank(value)?"":value)); + break ; + } + } + + if(addFlag){ + proList.add(elem+" = "+value); + logger.info("参数新增:"+elem+" = "+(StringUtils.isBlank(value)?"":value)); + } + } catch (Exception e) { + logger.error("Properties file Set Value set value ERROR "+elem,e); + } + } + File file = new File(url).getAbsoluteFile(); + if(!file.exists()){ + if(!file.getParentFile().exists()){ + file.getParentFile().mkdirs(); + } + file.createNewFile(); + } + //将文件信息写入到文件中 + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),Charset.forName("utf-8"))); + Iterator it = proList.iterator(); + while (it.hasNext()) { + String elem = (String) it.next(); + writer.write((elem==null?"":elem)+"\r\n"); + } + writer.flush(); + } catch (Exception e) { + System.err.println("资源配置文件取值异常"); + e.printStackTrace(); + }finally{ + try { + if(writer!= null ){ + writer.close(); + writer = null; + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * 向资源配置文件host_uuid.properties中更新UUID键值 + */ + /*public static void saveResource(String value) { + try { + Properties properties = new Properties(); + if (uuidUrl != null && !"".equals(uuidUrl)) { + properties.load(new FileInputStream(uuidUrl)); + // 添加或更新键值对 + properties.setProperty(AGENT_HOST_UUID_KEY, value); + // 保存到文件 + properties.store(new FileOutputStream(uuidUrl), ""); + } + properties.clear(); + + if(value != null && !"".equals(value)){ + Contants.AGENT_HOST_UUID = Long.parseLong(value); + } + } catch (Exception e) { + logger.error(e); + } + }*/ + +/* public static void pl(Object obj){ + System.out.println(obj==null?null:obj.toString()); + }*/ + +/* public static void main(String [] args){ +// pl(log4jRb.getString("log4j.appender.logfile.File")); +// pl(configRb.getString("email.flag")); + }*/ +} diff --git a/src/com/nms/server/common/Constants.java b/src/com/nms/server/common/Constants.java new file mode 100644 index 0000000..a5fa3e0 --- /dev/null +++ b/src/com/nms/server/common/Constants.java @@ -0,0 +1,844 @@ +package com.nms.server.common; + +import java.io.File; +import java.net.SocketException; +import javax.net.ssl.SSLServerSocket; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.util.LocalAddress; +import com.zhtelecom.common.snmp.SNMPTarget; + + +/** + * DataController 公共常量和配置文件参数信息类 + * @date Jan 5, 2012 11:38:45 AM + * @author ZhangGang + * + */ +public class Constants { + private static final Logger logger = Logger.getLogger(Constants.class); +// private static ResourceBundle resource = ResourceBundle.getBundle(DefaultConfig.class.getName()); + // #---Common------- + /** + * DC初始化标识 + */ + public static boolean flag_init = false; + /** + * 全局 邮件信息保存参数 + * 1保存 0 不保存 DC端默认为0 缺省配置为1 + * PS:当同步web端邮件功能配置时,可以创建发送通讯,变更该变量信息 + */ + public static int flag_email = 0; //Email 信息创建标示 1创建 0 不创建 DC端默认为0 + public static final String ERROR_INFO_RESOVE_MANAGER = "errorInfoResoveManager"; //错误信息解析入库管理线程 标识, futureMap 中的Key值 + public static final String ERROR_INFO_RESOVE = "errorInfoResove"; //错误信息解析入库操作线程 标识, futureMap 中的Key值 + public static final Integer ERROR_INFO_RESOVE_PERIOD; //监测数据解析周期(秒) + + public static final String MAILING_MANAGER = "errorInfoResoveManager"; //错误信息解析入库管理线程 标识, futureMap 中的Key值 + public static final String MAILING_THREAD = "errorInfoResove"; //错误信息解析入库操作线程 标识, futureMap 中的Key值 +// public static final Integer MAILING_PERIOD; //监测数据解析周期(秒) + + public static final String DETEC_DATA_RESOVE_MANAGER = "detecDataResoveManager"; +// public static final String DETEC_DATA_COLLECT_MANAGER = "detecDataCollectManager";//DC端主动收集监测数据 + public static final String DATA_COLLECT_MANAGER = "dataCollectManager";//DC端主动收集数据:监测数据、任务结果、回传文件 + public static final String ERROR_NODE_DATA_COLLECT_MANAGER = "errorNodeDataCollectManager";//异常节点管理线程 + public static final String NO_DETECT_DATA_NODE_DATA_COLLECT_MANAGER = "noDetectDataNodeDataCollectManager";//无监测数据节点管理线程 + public static final String EMAIL_START_THREAD = "emailStartThread";//DC端刷新邮件启动标识线程 + public static final String NONRLTTASK_RESULT_COLLECT_MANAGER = "nonRltTaskResultCollectManager";//DC端主动收集数据:执行完成,但无任务结果的任务的结果信息 + public static final String DETEC_DATA_RESOVE = "detecDataResove"; //监测数据解析线程 标识, futureMap 中的Key值 + public static final String OVERRUN_DATA_RESOVE_MANAGER = "overrunDataResove"; //硬盘数据解析入库管理线程 标识, futureMap 中的Key值 + + public static final String OVRERUN_DETEC_DATA_RESOVE = "overrunDetecDataResove"; //硬盘监测数据解析线程 标识, futureMap 中的Key值 + public static final String OVRERUN_WARNING_DATA_RESOVE = "overrunWarningDataResove"; //硬盘告警数据解析线程 标识, futureMap 中的Key值 + public static final String OVRERUN_RESULT_DATA_RESOVE = "overrunResultDataResove"; //硬盘任务结果数据解析线程 标识, futureMap 中的Key值 + + public static final String DELETE_TMP_FILES_MANAGER = "deteleTmpFilesManager";//删除log等临时文件 + + public static final String DETEC_DATA_COLLECT = "detecDataCollect"; //监测数据主动收集线程 标识, futureMap 中的Key值-暂未使用 + public static final String DATA_COLLECT = "dataCollect"; //监测数据主动收集线程 标识, futureMap 中的Key值 + public static final String NO_DETECT_DATA_COLLECT = "noDetectDataCollect"; //异常监测数据主动收集线程 标识, futureMap 中的Key值 + public static final String NONRLTTASK_RESULT_COLLECT = "nonRltTaskResultCollect"; //无任务结果的任务的结果数据的主动收集线程 标识, futureMap 中的Key值 + public static final String ALARM_DATA_RESOVE_MANAGER = "alarmDataResoveManager"; + public static final String ALARM_DATA_RESOVE = "alarmDataResove"; //告警数据解析线程 标识, futureMap 中的Key值 + public static final String MISSION_THREAD = "missionThread"; + + public static final String DELETE_TMP_FILES = "deleteTmpFiles"; //删除log等文件线程 标识, futureMap 中的Key值 + + public static final String UNZIP_FILE_MANAGER = "unZipFileManager"; + public static final String UNZIP_FILE = "unZipFile"; + + public static final String SUCCESS = "success"; + + + public static final String MISSION_RESULT_RESOVE_MANAGER = "missionResultResoveManager"; + public static final String MISSION_RESULT_RESOVE = "missionResultResove"; + public static final String MISSION_FILE_UPLOAD = "missionFileUpload"; + + public static final String CHANGE_OPERATIONS_MANAGER = "changeOperationsManager"; + public static final String CHANGE_OPERATIONS = "changeOperations"; + + public static final String DETECTION_INFO_TABLE_NAME = "detection_info_new";//监测数据表名 + public static final String DETECTION_WARNING_TABLE_NAME = "detection_info_warning";//告警数据表名 + + public static final Boolean DETECSELFTHREAD_FLAG ;//DetecSelfThread 开关 + public static final String DETECTI_SELF_THREAD_NAME = "DetecSelfThread";//监测数据表名 + public static final Long DETECSELFTHREAD_PERIOD;//监测间隔,单位:秒 + public static final Long DETECSELFTHREAD_INITIALDELAY;//监测延迟启动时间,单位:秒 + public static final String DETECTI_DATA_COLLECT_THREAD_NAME = "DetecDataCollectThread";//监测数据收集线程是否正常运行 + public static final String RESET_SERVER_THREAD_NAME = "ResetServerThread";//告警数据表名 + + public static final String DB_CONNECTION_THREAD_NAME = "DbConnectionThreadName";//数据库连接线程名 + + public static final String EMAIL_TYPE_IDENTITY = "emailflag";//是否发送邮件的参数配置标识 + +// public static final String NO_DETECTDATA_STATUS_INFO = "数据收集超时,未获取监测数据";//DC收集数据,无监测数据时默认的提示信息 +// public static final String NO_DETECTDATA_STATUS_INFO = "Data collection is out of time and no monitoring data is obtained";//DC收集数据,无监测数据时默认的提示信息 + public static final String NO_DETECTDATA_STATUS_INFO = "i18n_server.Constants.NO_DETECTDATA_STATUS_INFO_n81i";//DC收集数据,无监测数据时默认的提示信息 + + + // #---Common------- + public static final String COMMON_CONFIG_EXE_NAME ; //参数配置程序名称 + public static final String COMMON_TEXT_CODING ; //公用字符流编码格式 + public static final String COMMON_DATE_FORMAT; //公用日期格式化标准格式DEFAULT_DATE_FORMAT + public static final String COMMON_DATAS_DIR; + public static final Integer COMMON_SNMP_VERSION = SNMPTarget.VERSION2C ; //公用字符流编码格式 + public static final String COMMON_DATA_SPLIT = "\\$@\\$"; //主动告警信息解析标识串-用于分隔字符 + public static final String COMMON_DATA_POINT = "$@$"; //主动告警信息解析标识串-用于连接字符 + public static final String COMMON_TEMP_DIR ; //安装程序下临时目录 + public static final String COMMON_RUNTIME_PID_FILE ; //程序运行时PID文件 + public static final String SYSTEM_PATH; //DataController应用启动主目录 + public static final String SYSTEM_INET_ADDRESS; + public static final Integer SYSTEM_CLEAR_PERIOD = 24; // [1,99999999]hours 每SYSTEM_CLEAR_PERIOD系统清理一次 + public static final Integer SYSTEM_CLEAR_TIME = 20; // [00:00:00,24:00:00] 每天某时间(精确到小时),执行系统数据清理 + public static final Integer MAX_NETWORK_BANDWIDTH; //证书有效期(天) + + // #---Executor /ThreadPool------- + public static final Integer EXECUTOR_SOCKET_THREAD_SIZE ; //线程池 通讯线程最大数 + public static final Integer EXECUTOR_DATA_RESOVE_THREAD_SIZE ; + public static final Integer EXECUTOR_SCHEDULED_THREAD_SIZE ; //线程池 周期性线程最大数 + // #---Change Operations------- + public static final Integer CHANGE_OPERATIONS_PERIOD ; + public static final Integer CHANGE_RELEASE_SEMAPHORE_MAX ; + + // #---Detection Resolve------- +// public static final Integer DETEC_TIMEOUT_ALARM_PERIOD_TIMES; //监测数据告警周期数 + public static final Integer DETEC_DATA_RESOLVE_PERIOD; //缓存监测数据解析周期(秒) + public static final Integer DISK_DATA_RESOLVE_PERIOD; //硬盘监测数据解析周期(秒) + public static final int CHECK_DETEC_DATA_OVERRUN = 5; //检查 监测数据量是否过大(导致无法及时、完整的入库) 的次数,即5*DETEC_DATA_RESOLVE_PERIOD + public static final int CHECK_WARNING_DATA_OVERRUN = 5; //检查 告警数据量是否过大(导致无法及时、完整的入库) 的次数,即5*告警数据解析周期 + public static final int CHECK_RESULT_DATA_OVERRUN = 5; //检查 任务结果数据量是否过大(导致无法及时、完整的入库) 的次数,即5*任务结果解析周期 + + public static final int REFRESH_EMAIL_FLAG_PEROID = 300; //刷新邮件功能启用标示周期:5分钟 +// public static final Integer DETEC_DATA_COLLECT_PERIOD; //监测数据主动收集周期 + public static final Integer DATA_COLLECT_PERIOD; //DC端主动收集数据(监测数据、任务结果、回传文件)周期 + public static final Integer ERROR_NODE_DATA_COLLECT_PERIOD; //DC端主动收集数据(监测数据、任务结果、回传文件)周期(针对异常节点) + public static final Integer DATA_COLLECT_DAILY; //DC端主动收集数据:等待时间 + public static final Integer ERROR_NODE_DATA_COLLECT_DAILY; //DC端主动收集数据:等待时间(针对问题节点) + public static final Integer SINGLE_NODE_DETECT_TIMEOUT; //DC端主动监测,单节点超时时间 + public static final Integer DETEC_RELEASE_SEMAPHORE_MAX; + public static final String DETEC_PING_STR; //ping配置名称 与数据库监测类别表 ping对应 谨慎一致修改 + public static final String DETEC_NMSC_STR; //NMSClient端通信握手配置名称 与数据库监测类别表 NMSClient对 应谨慎一致修改 + public static final String DETEC_SNMP_STR; + public static final String DETEC_SWITCH_STR; + public static final String DETEC_SYSTEMINFO_STR; + public static final String DETEC_SYSTEM_STR; + public static final String DETEC_SYSTEMDATE_STR; + public static final String DETEC_IFXTABLE_STR; + + public static final String ALARM_SNMP_TRAP_STR; + + public static final Integer DELETE_TMP_FILES_PERIOD; //删除log等临时文件的线程执行间隔时间 + public static final Integer KEEP_FILE_DAYS; //保留log等临时文件的天数 + + // #---Alarm Resolve------- + public static final Integer ALARM_DATA_RESOLVE_PERIOD; //告警数据解析周期 + + public static final Integer NONRLTTASK_RESULT_COLLECT_PERIOD; //DC端主动收集数据(执行完成,但无任务结果的任务的结果信息)周期 + + // #---Web------- +// public static final String WEB_DOWNLOAD_URL; //NMSWeb 文件下载路径 + public static final String WEB_SOCKET_IP; //指定非加密通讯通信IP + public static final Integer WEB_SOCKET_PORT; //指定非加密通讯通信端口 + + // #---Mission Control------- + public static final Integer MISSION_FIRST_START = 0;//10; //任务下发线程启动时间 计算式:(agent任务结果上传周期+MISSION_RESULTS_SAVE_PERIOD)/MISSION_RESULTS_SAVE_PERIOD ,取其进1整数*MISSION_RESULTS_SAVE_PERIOD+MISSION_RESULTS_SAVE_START+1 + public static final Integer MISSION_RESULTS_SAVE_START = 60; //任务结果入库线程启动时间 秒 + public static final Integer MISSION_RESULTS_SAVE_PERIOD = 120; //任务结果入库线程循环周期 秒 + public static final Integer MISSION_CHECK_NEW_START = 10;//10; //新任务检查线程启动在first_start之后 + public static final Integer MISSION_CHECK_NEW_PERIOD = 300; //周期间隔可以稍长 + public static final Integer TASK_RLT_COLLECT_RESERVED_PERIOD = 600; //任务结果收集估计预留时间,单位秒 + public static final Integer MISSION_LOOP_PRESET_NUMBER; //周期预置数 +// public static final String MISSION_LOOP_FINISHING_DAILY; //周期任务 完成状态变更时间(未使用) + public static final Integer MISSION_UPGRADE_DAILY; //DataController应用启动主目录 + + // #---Mission release------- + public static final Integer MISSION_RELEASE_SEMAPHORE_MAX; ////信号计数,每申请一个信号,创建两个线程,一个任务执行线程,一个结果接收线程 + public static final Integer MISSION_RELEASE_TIMES; //下发任务尝试次数 + public static final Integer MISSION_RELEASE_PERIOD; //下发任务间隔周期 + + // #---Mission File------- + public static final String MISSION_FILE_DOWNLOAD_DIR ; //下发任务文件目录(推送文件用) + public static final Integer MISSION_FILE_DOWNLOAD_DELAY; //上传最大等待时间(秒) + public static final String MISSION_FILE_UPLOAD_DIR ; //上传回传文件目录(推送文件用) + public static final Integer MISSION_FILE_UPLOAD_PERIOD; //上传周期(分) + + // #---Email------- +// public static final String EMAIL_ADDRESS ; +// public static final String EMAIL_USERNAME ; +// public static final String EMAIL_PASSWORD; +// public static final String EMAIL_HOST ; + + // #---DataBase------- + public static final String DB_POOL_TYPE;//连接池类型 + public static final String DB_DRIVER; //数据库链接驱动 + public static final String DB_USER_NAME; //数据库链接用户名 + public static final String DB_PASSWORD; //数据库链接密码 + public static final String DB_URL; //数据库链接地址 + public static final String DB_DATE_FORMAT; //数据库日期格式化格式 + public static final Integer DB_EXECUTE_BATCH; //数据库批处理更新操作单次最大处理量 + public static final Integer DB_STATEMENT_EXECUTE_TIMEOUT ; + public static final Integer DB_POOL_PARTITION_COUNT ; //连接池 设置分区 缺省值 3 + public static final Integer DB_POOL_CONNECTIONS_PER_PARTITION_MAX; //设置每个分区中的最大连接数 缺省值 30 + public static final Integer DB_POOL_CONNECTIONS_PER_PARTITION_MIN ; //设置每个分区中的最小连接数 缺省值 10 + public static final Integer DB_POOL_ACQUIRE_INCREMENT; //当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数 缺省值3 + public static final Integer DB_POOL_IDLE_CONNECTION_TEST_PERIOD ; //设置每60秒检查数据库中的空闲连接数 单位 秒 缺省值 60 + public static final Integer DB_POOL_IDLE_MAX_AGE ; //设置连接空闲时间 单位 分钟 缺省值 240 + public static final Integer DB_GET_CONNECTION_TIMEOUT ; //设置获取连接超时时间,默认30秒 + public static final Integer DB_POOL_MAX_CONNECTION_AGE; + public static final Integer DB_POOL_RELEASE_HELPER_THREADS ; //连接释放处理 缺省值3 + public static final Integer DB_POOL_STATEMENT_RELEASE_HELPER_THREADS; + public static final Integer DB_POOL_QUERY_EXECUTE_TIME_LIMIT ; //sql执行超时时间 + public static final boolean DB_POOL_LOG_STATEMENT_ENABLE; + // #---DATA------- + /* + public static final String DATA_DIR; //DataController端未解析数据文件存放目录 + public static final String DATA_BACKUP_RIGHT_DIR; //DataController端解析正确的数据文件备份目录 + public static final String DATA_BACKUP_WRONG_DIR; //DataController端解析错误的数据文件备份目录 + */ + // #---ZIP File------- + public static final String ZIP_FILE_DETECT_DATA_DIR; + public static final String ZIP_FILE_TASK_RESULT_DIR; + public static final String ZIP_FILE_TASK_RETURN_DIR; + + // #---ZIP File Resove------- + public static final String ZIP_RESOVE_DETECT_DATA_DIR; + public static final String ZIP_RESOVE_TASK_RESULT_DIR; + public static final String ZIP_RESOVE_TASK_RETURN_DIR; + public static final String ERROR_DETEC_FILE_DIR; + public static final int ERROR_DETEC_FILE_DIR_FILE_SIZES = 500; + public static final String DEFAULT_NODE_GROUP_NAME; + + //20160909 单个节点默认收集后加入监测数据列表的最大监测数据条数,如果一次收集超过设置的条数,则存入dc_overrun/detect/zip_resove中(避免解析时数据过多导致内存溢出) + public static final int MAX_COLLECT_RESOVE_DETECT_DATA_NUM; + //20160912 内存中监测数据的条数超过MAX_DETECT_DATA_RESOVE_LIMIT_NUM,则不再向内存中添加监测数据,而是暂存硬盘,待之后解析入库,避免内存溢出,默认10万 + public static final int MAX_DETECT_DATA_RESOVE_LIMIT_NUM; + + // #---overrun detect data file path------- + public static final String OVERRUN_DETEC_FILE_DIR;//超出处理能力的监测数据文件存放路径 + // #---overrun warning data file path------- + public static final String OVERRUN_WARNING_FILE_DIR;//超出处理能力的告警数据文件存放路径 + // #---overrun result data file path------- + public static final String OVERRUN_RESULT_FILE_DIR;//超出处理能力的任务结果数据文件存放路径 + + // 第三方监测脚本 + public static final String PLUGIN_SCRIPT_FILE_DIR; // 第三方检测脚本存储路径 + + + // #---SNMP------- + public static final Integer SNMP_TRAP_THREAD_POOL_SIZE; //指定SNMP 共同体名称 + public static final Integer SNMP_CLIENT_PORT; //指定SNMP client get端口 + public static final Integer SNMP_TRAP_PORT; //指定SNMP trap get端口 + public static final String SNMP_COMMUNITY; //指定SNMP 共同体名称 +/* public static final String SNMP_MIB_DIR ; // + public static final String SNMP_CLASS_PATH ; + public static final String SNMP_CLASS_DIR ; + */ + public static final String SNMP_CLASS_PACKAGE ; + public static final Integer SNMP_V3_SECURITY_LEVEL ; // ##--SNMP 加密认证等级 + public static final String SNMP_V3_SECURITY_NAME ; // ##--SNMP 加密认证用户 用户名 + public static final String SNMP_V3_AUTH_PROTOCOL ; // ##--SNMP 认证协议方式 + public static final String SNMP_V3_PRIV_PROTOCOL ; // ##--SNMP 加密协议方式 + public static final String SNMP_V3_AUTH_PASSPHRASE ; // ##--SNMP 认证密码明文 + public static final String SNMP_V3_PRIV_PASSPHRASE ; // ##--SNMP 加密密码明文 + + // #---SSL Init------- + public static final Integer SSL_SO_TIMEOUT; //SSL加密通信类型 + public static final String SSL_TYPE; //SSL加密通信类型 + public static final String SSL_KEYSTORE_TYPE; //SSL加密通信加密密匙库类型 + public static final Integer SSL_SERVER_PORT; //SSL加密通信接收端口 + public static final Integer SSL_CLIENT_PORT; //SSL加密通信请求端口 + public static final String SSL_DIR ; //证书存放目录 + public static final String SSL_SERVER_EXPORT ; //程序初始化服务端证书导出名称 + public static final String SSL_CLIENT_EXPORT ; //程序初始化客户端通用证书导出名称 + +// public static final String SSL_INIT_SERVER_KEY; //程序初始化服务端证书 (安装时用) +// public static final String SSL_INIT_SERVER_KEY_PSW ; //程序初始化服务端证书密码 +// public static final String SSL_INIT_CLIENT_KEY ; //程序初始化客户端通用证书 (安装时用) +// public static final String SSL_INIT_CLIENT_KEY_PSW ; //程序初始化客户端证书密码 + // #---SSL Server------- + public static final String SSL_SERVER_STORE ; //通讯接收端证书密匙库 + public static final String SSL_CLIENT_STORE ; //通讯请求端证书密匙库 + public static final String SSL_SERVER_STORE_PSW ; //通讯接收端证书密匙库密码 + public static final String SSL_SERVER_TRUST; //通讯接收端证书公钥库 + public static final String SSL_CLIENT_TRUST; //通讯请求端证书公钥库 + public static final String SSL_SERVER_TRUST_PSW; //通讯接收端证书公钥库密码 + public static final String SSL_SERVER_KEY_NEW; //通讯接收端新密匙 + public static final String SSL_SERVER_KEY_NEW_PSW; + public static final String SSL_SERVER_KEY_OLD ; //通讯接收端旧密匙 + public static final String SSL_SERVER_KEY_OLD_PSW; //通讯接收端旧密匙密码 + public static final String SSL_CLIENT_KEY ; //通讯请求端密匙 + public static final String SSL_CLIENT_KEY_PSW ; //通讯请求端密匙密码 + public static final Integer SSL_KEY_VALIDITY; //证书有效期(天) + // #---SSL Bat------- +// public static final String SSL_BAT_IMPORT ; +// public static final String SSL_BAT_GENKEY_EXPORT ; +// public static final String SSL_BAT_SERVER_INIT ; + + //监测执行失败时,握手监测-默认的告警级别:0 + public static final int DETECT_FAIL_NMSC_POLICE_LEVEL;//0 + //监测执行失败时,握手监测-默认的紧急状态:0紧急 + public static final int DETECT_FAIL_NMSC_POLICE_EMERGENT;//0 + //监测执行失败时,非握手监测-默认的告警级别:1级 + public static final int DETECT_FAIL_NON_NMSC_POLICE_LEVEL;//1 + //监测执行失败时,非握手监测-默认的紧急状态:非紧急 + public static final int DETECT_FAIL_NON_NMSC_POLICE_EMERGENT;//1 + //NC主动告警(默认为紧急),state=-2时,默认的告警级别:0级, + public static final String NC_ALARM_POLICE_LEVEL;//1 + + /**监测信息状态集合 + * [0] -2 监测线程异常 + * [1] -1 监测任务失败 + * [2] 0 监测信息异常 + * [3] 1 监测信息正常 + * [4] 2 监测线程正常 + * */ + private static final Integer [] DETECTION_INFO_STATE = new Integer[]{-2,-1,0,1,2}; + /**监测线程异常 -2*/ + public static final Integer DETECTION_INFO_ALARM_WRONG = DETECTION_INFO_STATE[1]; + /**监测线程正常 2*/ + public static final Integer DETECTION_INFO_ALARM_RIGHT = DETECTION_INFO_STATE[3]; + /**监测任务失败 -1*/ + public static final Integer DETECTION_INFO_DETEC_ERROR = DETECTION_INFO_STATE[1]; + /**监测信息异常 0*/ + public static final Integer DETECTION_INFO_DETEC_WRONG = DETECTION_INFO_STATE[2]; + /**监测信息正常 1*/ + public static final Integer DETECTION_INFO_DETEC_RIGHT = DETECTION_INFO_STATE[3]; + +// public static final String ERROR_CODE_CREATE_SOCKET_ERROR= "CreateSocketError"; +// public static final String ERROR_CODE_CREATE_SOCKET_ERROR= "CreateSocketError"; +// public static final String ERROR_CODE_CREATE_SOCKET_ERROR= "CreateSocketError"; +// public static final String ERROR_CODE_CREATE_SOCKET_ERROR= "CreateSocketError"; +// public static final String ERROR_CODE_CREATE_SOCKET_ERROR= "CreateSocketError"; +// public static final String ERROR_CODE_CREATE_SOCKET_ERROR= "CreateSocketError"; + public static final String ERROR_CODE_CREATE_SOCKET= "CreateSocketError"; + public static final String ERROR_CODE_SOCKET_RUNTIME= "DCSocketRuntimeError"; + public static final String ERROR_CODE_SOCKET_SERVER_START = "DCSocketServerStartError"; + public static final String ERROR_CODE_SOCKET_SERVER_RUNTIME = "DCSocketServerRuntimeError"; + public static final String ERROR_CODE_DB_CONNECT= "DBConnectError"; + public static final String ERROR_CODE_DATA_COLLECT= "DataCollectError"; +// public static final String ERROR_DESC_DATA_COLLECT= "数据收集管理线程停止运行"; +// public static final String ERROR_DESC_DATA_COLLECT= "The data collection management thread stopped running"; + public static final String ERROR_DESC_DATA_COLLECT= "i18n_server.Constants.ERROR_DESC_DATA_COLLECT_n81i"; +// public static final String ERROR_DESC_DATA_COLLECT_NO_RUN= "数据收集管理线程未启动"; +// public static final String ERROR_DESC_DATA_COLLECT_NO_RUN= "Data Collection Management thread did not start"; + public static final String ERROR_DESC_DATA_COLLECT_NO_RUN= "i18n_server.Constants.ERROR_DESC_DATA_COLLECT_NO_RUN_n81i"; + + public static final long ERROR_INFO_SATAE_ERROR = 1;//异常 + public static final long ERROR_INFO_SATAE_RIGHT = 0;//已解决,手动置为已解决的 + public static final long ERROR_INFO_SATAE_RIGHT_AUTO = 2;//已恢复,自己恢复的 + + public static final int PORT_ALARM_LEVEL;//端口异常,默认的告警级别:5 + public static final int PORT_ALARM_EMERGENT;//端口异常,默认的紧急状态:1,非紧急 + + // #---Test Flag------- + public static final Integer FLAG_ZIP; +// public static final Integer FLAG_DETEC_TIMEOUT; + public static final Integer FLAG_DETEC_RESOVE; + public static final Integer FLAG_ERROR_INFO; + public static final Integer FLAG_FILES_READER_RESOVE; + public static final Integer FLAG_DATA_COLLECT;//数据收集标志(是否启动数据收集管理线程) + public static final Integer FLAG_HANDWALK_SSH;//握手时,是否进行ssh判断 + public static final Integer FLAG_DATA_COLLECT_ONLY_HANDSHAKE;//数据收集线程里只握手,不收集数据 + public static final Integer FLAG_NONRLTTASK_RESULT_COLLECT;//执行完成,但无任务结果的任务的结果信息的收集标志 + public static final Integer FLAG_DELETE_TMP_FILES;//删除日志等文件标识 + public static final Integer FLAG_FILE_UPLOAD; + public static final Integer FLAG_CHANGE_OPERATIONS; + public static final Integer FLAG_PING; + public static final Integer FLAG_NMSC; + public static final Integer FLAG_SNMP; + public static final Integer FLAG_SWITCH; + public static final Integer FLAG_SYSTEMDATE; + public static final Integer FLAG_MISSION; + public static final Integer FLAG_MISSION_RESULT; + /*public static final Integer FLAG_RESOVE_READ_FILE; + public static final Integer FLAG_RESOVE_RESOVE_FILE; + public static final Integer FLAG_MOVE_FILE;*/ + public static final Integer FLAG_RESOVE_COMMIT_DB; + public static final Integer FLAG_ALARM_RESOVE; + public static final Integer FLAG_TRAP; + + public static final Integer FLAG_EMAIL_START;//是否启用邮件发送功能 + + + //20151229 hyx + public static final Integer FLAG_DETECT_DATA_SAVE_DISK_RESOVE;//监测数据过多时是否保存硬盘:1保存 + public static final Integer FLAG_WARN_DATA_SAVE_DISK_RESOVE;//告警数据过多时是否保存硬盘:1保存 + public static final Integer FLAG_ERROR_DATA_SAVE_DISK_RESOVE;//错误数据过多时是否保存硬盘:1保存 + public static final Integer FLAG_TASK_RLT_SAVE_DISK_RESOVE;//任务结果过多时是否保存硬盘:1保存 + + public static final String OS_WIN = "win"; + public static final String OS_LINUX = "linux"; + + public static SSLServerSocket sslServer = null; + + public static int LEVEL_OF_EMERGENCY = 0;//紧急级别对应的level(0:紧急;1-5:非紧急) + + public static int VARCHAR_MAX_LENGTH = 4000-1;//oracle中varchar2类型的最大长度为4000个字节 + + public static Integer DETECTION_INFO_DATA_MAX_ROWS;//监测数据每多少条数据入库一次 + + public static int FETCH_SIZE;//如果不设置自动取的是10,当网络情况不好的时候,一次查询的数据较多的时候(批量监测数据入库就会一次查出new表和端口表等的数据,多达几万条),就会多次从数据库取数据,在网络上浪费时间,因此这个值需要设置的大一些,但是这个值设置的越大,占用的内存就越大,所以一般设置50或者100是比较合适的 + + public static int SSH_CONNECT_TIMEOUT; + + public static boolean IS_HANDEL_EXCEPTION_NODE = false;//正常收集线程是否处理异常节点的具体异常原因 + + + //批量解析数据限制数量 + public static final int BATCH_RESOVE_COUNT; +// public static final Integer FLAG_TEST; +// public static final Integer TEST_COMPUTER; +// public static final Integer TEST_SWITCH; + //static final 自变量的初始化 + + public static final String DB_TYPE; + public static final boolean IS_MYSQL; + //dc监测数据入库模式,1:由web 主控控制入库,2:自己主动入库,3,智能模式,4,数据上传 + public static final Integer DETECT_INSERT_MODE; + public static final Integer WEB_NOTICE_INSERT_DETECT_OVER_TIMES ;// web通知监测入库超时次数,只有 配置 DETECT_INSERT_MODE = 3 有效 + + + + /** + * 监测数据入库线程模式,1:单线程,2:双线程(info,detail) + */ + public static final int DETECT_INSERT_THREAD_MODE; + /** + * INFO入库线程运行模式,1:每隔 N秒 运行一次,2:间隔 N 秒运行一次 + */ + public static final int DETECT_INFO_THREAD_MODE; + /** + * INFO入库线程 每隔 或间隔 时间 ,单位:S + */ + public static final int DETECT_INFO_THREAD_TIME; + public static final int DETECT_INFO_THREAD_DELAY; + /** + * DETAIL入库线程运行模式,1:每隔 N秒 运行一次,2:间隔 N 秒运行一次 + */ + public static final int DETECT_DETAIL_THREAD_MODE; + /** + * DETAIL入库线程 每隔 或间隔 时间 ,单位:S + */ + public static final int DETECT_DETAIL_THREAD_TIME; + public static final int DETECT_DETAIL_THREAD_DELAY; + /** + * future key + */ + public static final String DETECT_INFO_THREAD_KEY = "DETECT_INFO_THREAD_KEY" ; + public static final String DETECT_DETAIL_THREAD_KEY = "DETECT_DETAIL_THREAD_KEY" ; + /** + * 监测数据解析线程 + */ + public static final String DETECT_RESOVE_THREAD_KEY = "DETECT_RESOVE_THREAD_KEY" ; + + public static final int NETTY_SERVER_FLAG; + public static final int NETTY_SERVER_PORT; + public static final boolean NETTY_SERVER_SSL;//是否使用加密通信 + public static final int NETTY_IDLE_TIME; + public static final int NETTY_SO_BACKLOG; + + /** + * 服务器握手监测 周期时间 + */ + public static final int SERVER_NMSC_THREAD_INTERVAL; + + public static final String NEXTVAL_FUNCTION_NAME ; + public static final String CURRVAL_FUNCTION_NAME ; + + public static final Long HANDSHANK_DELAY_TIME; + + + + static { + +// InetAddress inetAddress = null; +// try { +// inetAddress = InetAddress.getLocalHost(); +// } catch (UnknownHostException e) { +// logger.error("LocalHost InetAddress 获取失败",e); +// } + BATCH_RESOVE_COUNT=Config.getInteger("batch.resove.count",500); + // #---Common------- + SYSTEM_PATH = formatPath(Config.getSystemDir()); + COMMON_CONFIG_EXE_NAME = Config.getString("common.config.exe.name","DCConfig.exe"); + COMMON_TEXT_CODING = Config.getString("common.text.coding","UTF-8"); + COMMON_DATE_FORMAT = Config.getString("common.date.format","yyyy-MM-dd HH:mm:ss"); + COMMON_DATAS_DIR = Config.getString("common.datas.dir",SYSTEM_PATH+"/nmsdata"); + + COMMON_TEMP_DIR = Config.getString("common.temp.dir","temp"); + COMMON_RUNTIME_PID_FILE = Config.getString("common.Runtime.pid","DataControllerPid.temp"); + +// if(inetAddress!= null){ +// SYSTEM_INET_ADDRESS = StringUtils.isEmpty(Config.getString("system.inet.address")) +// ?inetAddress.getHostAddress().toString() +// :Config.getString("system.inet.address"); +// }else { + String localIP = Config.getString("system.inet.address",null); + try { + String localIP2 = LocalAddress.getRealIp(); +// if(StringUtils.isEmpty(localIP)){ +// localIP = localIP2; +// logger.warn("配置IP【"+localIP+"】为空 程序获取IP【"+localIP2+"】将使用程序获取IP"); +// } + + if(!localIP.equals(localIP2)){ + logger.warn("Local IP exception configuration IP【"+localIP+"】 Program to get IP【"+localIP2+"】 will use the configuration IP"); + } + } catch (SocketException e1) { + logger.error("Program gets IP failure", e1); + } + SYSTEM_INET_ADDRESS = localIP; +// } + MAX_NETWORK_BANDWIDTH = Config.getInteger("max.network.bandwidth",0); + // #---Executor /ThreadPool------- + EXECUTOR_SOCKET_THREAD_SIZE = Config.getInteger("executor.socket.thread.size",120); + EXECUTOR_SCHEDULED_THREAD_SIZE = Config.getInteger("executor.scheduled.thread.size",30); + EXECUTOR_DATA_RESOVE_THREAD_SIZE = Config.getInteger("executor.data.resove.thread.size",3); + + // #---Change Operations------- + CHANGE_OPERATIONS_PERIOD = Config.getInteger("change.operations.period",120); + CHANGE_RELEASE_SEMAPHORE_MAX = Config.getInteger("change.release.semaphore.max",20); + // #---Detection Resolve------- +// String outTimes = Config.getString("detec.timeout.alarm.period.times"); +// +// int outT = 1; +// if(outTimes != null ){ +// try { +// outT = Integer.parseInt(outTimes); +// } catch (Exception e) { +// logger.error("detec.timeout.alarm.period.times 取值失败",e); +// } +// } +// DETEC_TIMEOUT_ALARM_PERIOD_TIMES = Config.getInteger("detec.timeout.alarm.period.times");; + DETEC_RELEASE_SEMAPHORE_MAX = Config.getInteger("detec.release.semaphore.max",(int)(EXECUTOR_SOCKET_THREAD_SIZE*0.8)); + + DETEC_DATA_RESOLVE_PERIOD = Config.getInteger("detec.data.resovle.period",60); + DISK_DATA_RESOLVE_PERIOD = Config.getInteger("disk.data.resovle.period",600); + ERROR_INFO_RESOVE_PERIOD = Config.getInteger("error.info.resovle.period",60); +// MAILING_PERIOD = Config.getInteger("mailing.period",120); +// DETEC_DATA_COLLECT_PERIOD = Config.getInteger("detec.data.collect.period"); + DATA_COLLECT_PERIOD = Config.getInteger("data.collect.period",300); + ERROR_NODE_DATA_COLLECT_PERIOD = Config.getInteger("error.node.data.collect.period",600); + NONRLTTASK_RESULT_COLLECT_PERIOD = Config.getInteger("nonRltTask.result.collect.period",600); + DETEC_PING_STR = Config.getString("detec.ping.str","ping"); + DETEC_NMSC_STR = Config.getString("detec.nmsc.str","nmsclient"); + DETEC_SNMP_STR = Config.getString("detec.snmp.str","SNMP"); + DETEC_SWITCH_STR = Config.getString("detec.switch.str","switchport"); + DETEC_SYSTEMINFO_STR = Config.getString("detec.systeminfo.str","systeminfo"); + DETEC_SYSTEM_STR = Config.getString("detec.system.str","system"); + DETEC_SYSTEMDATE_STR = Config.getString("detec.systemdate.str","systemdate"); + DETEC_IFXTABLE_STR = Config.getString("detec.ifxtable.str","ifXTable"); + DELETE_TMP_FILES_PERIOD = Config.getInteger("delete.files.period",24); + KEEP_FILE_DAYS = Config.getInteger("keep.file.days",7); + + ALARM_SNMP_TRAP_STR = Config.getString("alarm.snmp.trap.str","SNMP_TRAP"); + + // #---Alarm Resolve------- + ALARM_DATA_RESOLVE_PERIOD = Config.getInteger("alarm.data.resovle.period",60); + + // #---Web------- +// WEB_DOWNLOAD_URL = formatPath(Config.getValue("web.download.url")); //指定IP通信 + WEB_SOCKET_IP = Config.getString("web.socket.ip",null); //指定IP通信 + WEB_SOCKET_PORT = Config.getInteger("web.socket.port",60703) ; //指定IP通信端口 + + // #---Mission Control------- + MISSION_LOOP_PRESET_NUMBER = Config.getInteger("mission.loop.preset.number",10); +// MISSION_LOOP_FINISHING_DAILY = Config.getString("mission.loop.finishing.daily"); + + // #---Mission release------- + MISSION_RELEASE_SEMAPHORE_MAX = Config.getInteger("mission.release.semaphore.max",(int)(EXECUTOR_SOCKET_THREAD_SIZE*0.3)); + MISSION_RELEASE_TIMES = Config.getInteger("mission.release.times",3); + MISSION_RELEASE_PERIOD = Config.getInteger("mission.release.period",180); + + // #---Mission File------- + MISSION_FILE_DOWNLOAD_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("mission.file.download.dir","dc_downLoad")); + +// int downloadDelay = (int)Config.getInteger("mission.file.download.delay"); + MISSION_FILE_DOWNLOAD_DELAY = Config.getInteger("mission.file.download.delay",60); + MISSION_FILE_UPLOAD_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("mission.file.upload.dir","dc_upload")); + MISSION_FILE_UPLOAD_PERIOD = Config.getInteger("mission.file.upload.period",120); + MISSION_UPGRADE_DAILY = Config.getInteger("mission.update.daily",300); + + // #---Email------- + /* + FLAG_EMAIL = Config.getInteger("email.flag",0); + if(FLAG_EMAIL==1){ + EMAIL_ADDRESS = Config.getString("email.address"); + EMAIL_USERNAME = Config.getString("email.userName"); + EMAIL_PASSWORD = Config.getString("email.password"); + EMAIL_HOST = Config.getString("email.host"); + }else{ + EMAIL_ADDRESS = null; + EMAIL_USERNAME = null; + EMAIL_PASSWORD = null; + EMAIL_HOST = null; + }*/ + + // #---DataBase------- + DB_POOL_TYPE = Config.getString("db.pool.type", "bonecp");//默认 bonecp + DB_URL = Config.getString("db.url",null); + DB_USER_NAME = Config.getString("db.username",null); + DB_PASSWORD = Config.getString("db.password",null); + DB_DRIVER = Config.getString("db.driver","oracle.jdbc.driver.OracleDriver"); + DB_DATE_FORMAT = Config.getString("db.date.format","yyyy-MM-dd HH24:mi:ss"); + DB_EXECUTE_BATCH = Config.getInteger("db.execute.batch",1000); + +// Integer partitionCount = (Integer) resource.getObject("db.pool.partitionCount"); + DB_STATEMENT_EXECUTE_TIMEOUT = Config.getInteger("db.statement.execute.timeout",60); + DB_POOL_PARTITION_COUNT = Config.getInteger("db.pool.partitionCount",3); + DB_POOL_CONNECTIONS_PER_PARTITION_MAX = Config.getInteger("db.pool.maxConnectionsPerPartition",5); + DB_POOL_CONNECTIONS_PER_PARTITION_MIN = Config.getInteger("db.pool.minConnectionsPerPartition",3); + DB_POOL_ACQUIRE_INCREMENT = Config.getInteger("db.pool.acquireIncrement",3); + + DB_POOL_IDLE_CONNECTION_TEST_PERIOD = Config.getInteger("db.pool.idleConnectionTestPeriod",15); + DB_POOL_IDLE_MAX_AGE = Config.getInteger("db.pool.idleMaxAge",30); + DB_GET_CONNECTION_TIMEOUT = Config.getInteger("db.get.connection.timeout",30); + DB_POOL_MAX_CONNECTION_AGE = Config.getInteger("db.pool.max.connection.age",DB_POOL_IDLE_MAX_AGE*2); + + DB_POOL_RELEASE_HELPER_THREADS = Config.getInteger("db.pool.releaseHelperThreads",DB_POOL_PARTITION_COUNT); + DB_POOL_STATEMENT_RELEASE_HELPER_THREADS = Config.getInteger("db.pool.statement.releaseHelperThreads",DB_POOL_PARTITION_COUNT); + + DB_POOL_QUERY_EXECUTE_TIME_LIMIT = Config.getInteger("db.pool.queryExecuteTimeLimit",60); + + DB_POOL_LOG_STATEMENT_ENABLE = Config.getBoolan("db.pool.logStatementEnable",true); + + // #---DATA------- + /* + DATA_DIR = formatPath(Config.getString("data.dir")); + DATA_BACKUP_RIGHT_DIR = formatPath(Config.getString("data.backup.right.dir")); + DATA_BACKUP_WRONG_DIR = formatPath(Config.getString("data.backup.wrong.dir")); + */ + // #---ZIP File------- + ZIP_FILE_DETECT_DATA_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("zip.file.detect.data.dir","dc_zip/detect")); + ZIP_FILE_TASK_RESULT_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("zip.file.task.result.dir","dc_zip/result")); + ZIP_FILE_TASK_RETURN_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("zip.file.task.return.dir","dc_zip/return")); + + // #---ZIP File Resove------- +// ZIP_RESOVE_DETECT_DATA_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("zip.resove.detect.data.dir","dc_zip/resove/detect")); + ZIP_RESOVE_DETECT_DATA_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("zip.resove.detect.data.dir","dc_overrun/detect/zip_resove"));//为了避免因长时间未收集数据,导致一次性收集大量的数据(nc端默认1000csv打包zip,所以如果监测数据打包发送,表明数据较多),如果全部进行解析入库,会导致内存溢出,所以存入dc_overrun等待之后慢慢解析 + ZIP_RESOVE_TASK_RESULT_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("zip.resove.task.result.dir","dc_zip/resove/result")); + ZIP_RESOVE_TASK_RETURN_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("zip.resove.task.return.dir","dc_zip/resove/return")); + + ERROR_DETEC_FILE_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("error.detect.file.dir","dc_error/detect")); + + OVERRUN_DETEC_FILE_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("overrun.detect.file.dir","dc_overrun/detect")); + OVERRUN_WARNING_FILE_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("overrun.warning.file.dir","dc_overrun/warning")); + OVERRUN_RESULT_FILE_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("overrun.result.file.dir","dc_overrun/result")); + + // TODO + PLUGIN_SCRIPT_FILE_DIR = formatPath(COMMON_DATAS_DIR+File.separator+Config.getString("detecScript","dc_detecScript")); + + + +// DEFAULT_NODE_GROUP_NAME = Config.getString("default.node.group.name","默认节点组"); +// DEFAULT_NODE_GROUP_NAME = Config.getString("default.node.group.name","Default Node Group"); + DEFAULT_NODE_GROUP_NAME = Config.getString("default.node.group.name","i18n_server.Constants.defaultNodeGroup_n81i"); + MAX_COLLECT_RESOVE_DETECT_DATA_NUM = Config.getInteger("max.collect.resove.detect.data.num",60); + MAX_DETECT_DATA_RESOVE_LIMIT_NUM = Config.getInteger("max.detect.data.resove.limit.num",60000); + + + // #---SNMP------- + SNMP_CLIENT_PORT = Config.getInteger("snmp.client.port",161); + SNMP_TRAP_THREAD_POOL_SIZE = Config.getInteger("snmp.trap.thread.pool.size",10); + SNMP_TRAP_PORT = Config.getInteger("snmp.trap.port",162); + SNMP_COMMUNITY = Config.getString("snmp.community","public"); + /*SNMP_MIB_DIR = formatPath(Config.getString("snmp.mib.dir")); + SNMP_CLASS_PATH = formatPath(Config.getString("snmp.class.path")); + SNMP_CLASS_DIR = SNMP_CLASS_PATH+SNMP_CLASS_PACKAGE.replaceAll("\\.", "/")+"/";*/ + SNMP_CLASS_PACKAGE = Config.getString("snmp.class.package","osDomains"); + + /*##--SNMP 加密认证级别 0 不认证 不加密'zg' 1 认证 不加密'hckings' ,2认证 且 加密'dxy'*/ + SNMP_V3_SECURITY_LEVEL = Config.getInteger("snmp.v3.security.level",0); + SNMP_V3_SECURITY_NAME = Config.getString("snmp.v3.security.name","zg"); + SNMP_V3_AUTH_PROTOCOL = Config.getString("snmp.v3.auth.protocol","MD5"); + SNMP_V3_PRIV_PROTOCOL = Config.getString("snmp.v3.priv.protocol","DES"); + SNMP_V3_AUTH_PASSPHRASE = Config.getString("snmp.v3.auth.passphrase","12345678"); + SNMP_V3_PRIV_PASSPHRASE = Config.getString("snmp.v3.priv.passphrase","12345678"); + + // #---SSL Init------- + SSL_SO_TIMEOUT = Config.getInteger("ssl.so.timeout",600); + SSL_TYPE = Config.getString("ssl.type","TLS"); + SSL_DIR = formatPath(Config.getString("ssl.dir","cer")); + SSL_SERVER_PORT = Config.getInteger("ssl.server.port",60702); + SSL_CLIENT_PORT = Config.getInteger("ssl.client.port",60701); + SSL_KEYSTORE_TYPE = Config.getString("ssl.keystore.type","jceks"); + SSL_SERVER_STORE = Config.getString("ssl.server.store","server_ks"); + SSL_SERVER_STORE_PSW = Config.getString("ssl.server.store.psw","client"); + SSL_SERVER_TRUST = Config.getString("ssl.server.trust","server_ts"); + SSL_SERVER_TRUST_PSW = Config.getString("ssl.server.trust.psw","client"); + SSL_SERVER_EXPORT = Config.getString("ssl.server.export","server.cer"); + SSL_CLIENT_EXPORT = Config.getString("ssl.client.export","client.cer"); + SSL_SERVER_KEY_NEW = Config.getString("ssl.server.key.new","serverks20110828"); + SSL_SERVER_KEY_OLD = Config.getString("ssl.server.key.old","serverks20110828"); + SSL_SERVER_KEY_NEW_PSW = Config.getString("ssl.server.key.new.psw","123456"); + SSL_SERVER_KEY_OLD_PSW = Config.getString("ssl.server.key.old.psw","123456"); + SSL_CLIENT_KEY = Config.getString("ssl.client.key","serverks20110828"); + SSL_CLIENT_KEY_PSW = Config.getString("ssl.client.key.psw","123456"); + SSL_CLIENT_STORE = Config.getString("ssl.client.store","client_ks"); + SSL_CLIENT_TRUST= Config.getString("ssl.client.trust","client_ts"); + SSL_KEY_VALIDITY= Config.getInteger("ssl.key.validity",90); + + + // #---Test Flag------- + FLAG_ZIP = Config.getInteger("zip.flag",1); +// FLAG_DETEC_TIMEOUT = Config.getInteger("detec.timeout.flag"); + FLAG_FILE_UPLOAD= Config.getInteger("file.upload.flag",1); + FLAG_DETEC_RESOVE= Config.getInteger("file.resove.flag",1); + FLAG_ERROR_INFO= Config.getInteger("error.info.flag",1); + FLAG_FILES_READER_RESOVE= Config.getInteger("data.files.reader.flag",1); + FLAG_DATA_COLLECT= Config.getInteger("data.collect.flag",1); + FLAG_HANDWALK_SSH= Config.getInteger("handwalk.ssh.flag",1); + FLAG_DATA_COLLECT_ONLY_HANDSHAKE= Config.getInteger("data.collect.flag.only.handshake",1); + FLAG_EMAIL_START= Config.getInteger("email.start.flag",1); + FLAG_NONRLTTASK_RESULT_COLLECT= Config.getInteger("nonRltTask.result.collect.flag",1); + FLAG_DELETE_TMP_FILES= Config.getInteger("delete.files.flag",1); + FLAG_CHANGE_OPERATIONS= Config.getInteger("change.operations.flag",1); + FLAG_PING= Config.getInteger("ping.flag",1); + FLAG_NMSC= Config.getInteger("nmsc.flag",1); + FLAG_SNMP= Config.getInteger("snmp.flag",1); + FLAG_SWITCH= Config.getInteger("switch.flag",1); + FLAG_SYSTEMDATE = Config.getInteger("systemdate.flag",1); + + FLAG_MISSION= Config.getInteger("mission.flag",1); + FLAG_MISSION_RESULT= Config.getInteger("result.flag",1); + /*FLAG_RESOVE_READ_FILE= Config.getInteger("file.resove.readFile.flag"); + FLAG_RESOVE_RESOVE_FILE= Config.getInteger("file.resove.resoveFile.flag"); + FLAG_MOVE_FILE= Config.getInteger("file.moveFile.flag");*/ + FLAG_RESOVE_COMMIT_DB= Config.getInteger("file.resove.commitDB.flag",1); + FLAG_ALARM_RESOVE = Config.getInteger("alarm.resove.flag",1); + FLAG_TRAP = Config.getInteger("trap.flag",1); +// FLAG_TEST = Config.getInteger("test.flag"); +// TEST_COMPUTER = Config.getInteger("computer.size"); +// TEST_SWITCH = Config.getInteger("switch.size"); +// SSL_INIT_SERVER_KEY = Config.getValue("ssl.init.server.key"); +// SSL_INIT_CLIENT_KEY = Config.getValue("ssl.init.client.key"); +// SSL_INIT_SERVER_KEY_PSW = Config.getValue("ssl.init.server.key.psw"); +// SSL_INIT_CLIENT_KEY_PSW = Config.getValue("ssl.init.client.key.psw"); + + // #---SSL Bat------- +// SSL_BAT_IMPORT = Config.getValue("ssl.bat.import"); +// SSL_BAT_GENKEY_EXPORT = Config.getValue("ssl.bat.genkey.export"); +// SSL_BAT_SERVER_INIT = Config.getValue("ssl.bat.server.init"); +// SYSTEM_INET_ADDRESS = Common.getLocalIp(); + + PORT_ALARM_LEVEL = Config.getInteger("port.alarm.level",5); + PORT_ALARM_EMERGENT = Config.getInteger("port.alarm.level",1); + + DETECT_FAIL_NMSC_POLICE_LEVEL = Config.getInteger("detect.fail.nmsc.police.level",0); + DETECT_FAIL_NMSC_POLICE_EMERGENT = Config.getInteger("detect.fail.nmsc.police.emergent",0); + DETECT_FAIL_NON_NMSC_POLICE_LEVEL = Config.getInteger("detect.fail.non.nmsc.police.level",1); + DETECT_FAIL_NON_NMSC_POLICE_EMERGENT = Config.getInteger("detect.fail.non.nmsc.police.emergent",1); + NC_ALARM_POLICE_LEVEL = Config.getString("nc.alarm.police.level","0"); + + SINGLE_NODE_DETECT_TIMEOUT = Config.getInteger("single.node.detect.timeout", 180); + + VARCHAR_MAX_LENGTH = Config.getInteger("varchar.max.length",4000)-1; + + + FLAG_DETECT_DATA_SAVE_DISK_RESOVE = Config.getInteger("flag.detect.data.save.disk.resove",1); + FLAG_WARN_DATA_SAVE_DISK_RESOVE = Config.getInteger("flag.warn.data.save.disk.resove",1); + FLAG_ERROR_DATA_SAVE_DISK_RESOVE = Config.getInteger("flag.error.data.save.disk.resove",1); + FLAG_TASK_RLT_SAVE_DISK_RESOVE = Config.getInteger("flag.task.rlt.save.disk.resove",1); + + DETECTION_INFO_DATA_MAX_ROWS = Config.getInteger("detection_info_data_max_rows", 500); + FETCH_SIZE = Config.getInteger("fetch.size", 100); + DATA_COLLECT_DAILY = Config.getInteger("data.collect.daily", 120); + ERROR_NODE_DATA_COLLECT_DAILY = Config.getInteger("error.node.data.collect.daily", 540); + + SSH_CONNECT_TIMEOUT = Config.getInteger("ssh.connect.timeout", 2000); + + IS_HANDEL_EXCEPTION_NODE = Config.getBoolan("is.handel.exception.node", false); + /** + * 数据库类型 + */ + DB_TYPE = Config.getString("db.type", "oracle"); + /** + * 是否是 mysql 数据库 + */ + IS_MYSQL = "mysql".equalsIgnoreCase(DB_TYPE); + /** + * //dc监测数据入库模式,1:由web 主控控制入库,2:自己主动入库 + */ + DETECT_INSERT_MODE = Config.getInteger("detect.insert.mode", 2); + WEB_NOTICE_INSERT_DETECT_OVER_TIMES = Config.getInteger("web.notice.insert.detect.over.times", 3); + + DETECSELFTHREAD_FLAG = Config.getBoolan("DETECSELFTHREAD_FLAG", true);//DETECSELFTHREAD 开关 + DETECSELFTHREAD_PERIOD = Long.valueOf(Config.getInteger("DETECSELFTHREAD_PERIOD", 30));//监测间隔,单位:秒 + DETECSELFTHREAD_INITIALDELAY = Long.valueOf(Config.getInteger("DETECSELFTHREAD_PERIOD", 30));//监测延迟启动时间,单位:秒 + + /** + * 监测数据入库线程模式,1:单线程,2:双线程(info,detail) + */ + DETECT_INSERT_THREAD_MODE = Config.getInteger("detect.insert.thread.mode", 1); + /** + * INFO入库线程运行模式,1:每隔 N秒 运行一次,2:间隔 N 秒运行一次 + */ + DETECT_INFO_THREAD_MODE = Config.getInteger("detect.info.thread.mode", 1); + /** + * INFO入库线程 每隔 或间隔 时间 ,单位:S + */ + DETECT_INFO_THREAD_TIME = Config.getInteger("detect.info.thread.time", 60); + DETECT_INFO_THREAD_DELAY = Config.getInteger("detect.INFO.thread.delay", 5); + /** + * DETAIL入库线程运行模式,1:每隔 N秒 运行一次,2:间隔 N 秒运行一次 + */ + DETECT_DETAIL_THREAD_MODE = Config.getInteger("detect.detail.thread.mode", 1); + /** + * DETAIL入库线程 每隔 或间隔 时间 ,单位:S + */ + DETECT_DETAIL_THREAD_TIME = Config.getInteger("detect.detail.thread.time", 60); + DETECT_DETAIL_THREAD_DELAY = Config.getInteger("detect.detail.thread.delay", 5); + + + /** + * netty server + * + */ + //0:启用 + NETTY_SERVER_FLAG = Config.getInteger("netty.server.flag", 1); + NETTY_SERVER_PORT = Config.getInteger("netty.server.port", 9527); + NETTY_SERVER_SSL = Config.getBoolan("netty.server.ssl", true); + NETTY_IDLE_TIME = Config.getInteger("netty.idle.time", 15); + NETTY_SO_BACKLOG = Config.getInteger("netty.so.backlog", 6000); + SERVER_NMSC_THREAD_INTERVAL = Config.getInteger("server.nmsc.thread.interval", 300); + + NEXTVAL_FUNCTION_NAME = Config.getString("nextval_function_name", "nextval"); + CURRVAL_FUNCTION_NAME = Config.getString("currval_function_name", "currval"); + HANDSHANK_DELAY_TIME = Long.parseLong(Config.getString("handshank.delay.time", 15000+"")); + + } + + //文件传输 临时文件命名后缀 + public static final String TEMP_SUFFIX = ".tp"; + + private static String formatPath(String path){ + if(StringUtils.isNotEmpty(path)){ + path = path.replaceAll("\\\\", "/"); + if(!path.endsWith("/") && !path.endsWith("=")){ + path = path+"/"; + } + } + return path; + } +} diff --git a/src/com/nms/server/common/DetectionConstants.java b/src/com/nms/server/common/DetectionConstants.java new file mode 100644 index 0000000..9e45810 --- /dev/null +++ b/src/com/nms/server/common/DetectionConstants.java @@ -0,0 +1,5 @@ +package com.nms.server.common; + +public class DetectionConstants { + +} diff --git a/src/com/nms/server/common/EmailTypeConstants.java b/src/com/nms/server/common/EmailTypeConstants.java new file mode 100644 index 0000000..6d03420 --- /dev/null +++ b/src/com/nms/server/common/EmailTypeConstants.java @@ -0,0 +1,74 @@ +package com.nms.server.common; + +public class EmailTypeConstants { +// private static final Object[][] type =new Object[][]{ +// {10,"监测信息恢复"}, +// {11,"监测信息异常"}, +// {12,"监测信息超时"}, +// {20,"主动告警异常"}, +// {21,"主动告警恢复"}, +// {31,"节点结果失败"}, +// {32,"任务状态变更"}, +// {40,"系统运行异常"}, +// {41,"系统运行恢复"}, +// }; + + public static final String ERROR_PROCESSS_NOT_EXIST = "ProcessNotExist"; //进程不存在 + public static final String ERROR_HANDSHAKE = "HandShakeError"; //握手失败 + public static final String ERROR_THREAD_RUNTIME = "ThreadRuntimeError"; //执行异常 + public static final String ERROR_PROT_LISTENER = "ProtListenerError"; //端口监听失败 + public static final String ERROR_DEAMON_NOT_EXIST = "DeamonNotExist"; //守护进程不存在 + public static final String ERROR_DATA_RESOVE = "DataResoveError"; //数据解析错误 + public static final String ERROR_SOCKET = "SocketError"; //通讯失败 + +// public static final int STATE_OK = 0; //已解决 +// public static final int STATE_FAIL = 1; //未解决 +// public static final int STATE_RECOVER = 2; //已恢复 + + public static final int FLAG_SEND_LATER = 0; + public static final int FLAG_SEND_ALLREADY = 1; + public static final int FLAG_SEND_IMMEDIATELY = 2; + + public static final int URGENT_IMMEDIATELY = 0; + public static final int URGENT_LATER = 1; + + public static final int TYPE_DETECTION_INFO_RECOVER = 10; +// public static final String DESC_DETECTION_INFO_RECOVER = "监测信息恢复"; +// public static final String DESC_DETECTION_INFO_RECOVER = "Monitoring Information Recovery"; + public static final String DESC_DETECTION_INFO_RECOVER = "i18n_server.EmailTypeConstants.DESC_DETECTION_INFO_RECOVER_n81i"; + public static final int TYPE_DETECTION_INFO_EXCEPTION = 11; +// public static final String DESC_DETECTION_INFO_EXCEPTION = "监测信息异常"; +// public static final String DESC_DETECTION_INFO_EXCEPTION = "Monitoring information is abnormal"; + public static final String DESC_DETECTION_INFO_EXCEPTION = "i18n_server.EmailTypeConstants.DESC_DETECTION_INFO_EXCEPTION_n81i"; + public static final int TYPE_DETECTION_INFO_TIMEOUT = 12; +// public static final String DESC_DETECTION_INFO_TIMEOUT = "监测信息超时"; +// public static final String DESC_DETECTION_INFO_TIMEOUT = "Monitoring information timeout"; + public static final String DESC_DETECTION_INFO_TIMEOUT = "i18n_server.EmailTypeConstants.DESC_DETECTION_INFO_TIMEOUT_n81i"; + + public static final int TYPE_ALARM_INFO_EXCEPTION = 20; +// public static final String DESC_ALARM_INFO_EXCEPTION = "主动告警异常"; +// public static final String DESC_ALARM_INFO_EXCEPTION = "Active alarm exception"; + public static final String DESC_ALARM_INFO_EXCEPTION = "i18n_server.EmailTypeConstants.DESC_ALARM_INFO_EXCEPTION_n81i"; + public static final int TYPE_ALARM_INFO_RECOVER = 21; +// public static final String DESC_ALARM_INFO_RECOVER = "主动告警恢复"; +// public static final String DESC_ALARM_INFO_RECOVER = "Active Alarm Recovery"; + public static final String DESC_ALARM_INFO_RECOVER = "i18n_server.EmailTypeConstants.DESC_ALARM_INFO_RECOVER_n81i"; + + public static final int TYPE_TASK_NODE_RESULT_ERROR = 31; +// public static final String DESC_TASK_NODE_RESULT_ERROR = "节点结果失败"; +// public static final String DESC_TASK_NODE_RESULT_ERROR = "Node result failed"; + public static final String DESC_TASK_NODE_RESULT_ERROR = "i18n_server.EmailTypeConstants.DESC_TASK_NODE_RESULT_ERROR_n81i"; + public static final int TYPE_TASK_STATE_CHANGE = 32; +// public static final String DESC_TASK_STATE_CHANGE = "任务状态变更"; +// public static final String DESC_TASK_STATE_CHANGE = "Change of task status"; + public static final String DESC_TASK_STATE_CHANGE = "i18n_server.EmailTypeConstants.DESC_TASK_STATE_CHANGE_n81i"; + + public static final int TYPE_SYSTEM_RUNNING_EXCEPTION = 40; +// public static final String DESC_SYSTEM_RUNNING_EXCEPTION = "系统运行异常"; +// public static final String DESC_SYSTEM_RUNNING_EXCEPTION = "System operation is abnormal"; + public static final String DESC_SYSTEM_RUNNING_EXCEPTION = "i18n_server.EmailTypeConstants.DESC_SYSTEM_RUNNING_EXCEPTION_n81i"; + public static final int TYPE_SYSTEM_RUNNING_RECOVER = 41; +// public static final String DESC_SYSTEM_RUNNING_RECOVER = "系统运行恢复"; +// public static final String DESC_SYSTEM_RUNNING_RECOVER = "System Operation Recovery"; + public static final String DESC_SYSTEM_RUNNING_RECOVER = "i18n_server.EmailTypeConstants.DESC_SYSTEM_RUNNING_RECOVER_n81i"; +} diff --git a/src/com/nms/server/common/MissionConstants.java b/src/com/nms/server/common/MissionConstants.java new file mode 100644 index 0000000..21cd738 --- /dev/null +++ b/src/com/nms/server/common/MissionConstants.java @@ -0,0 +1,33 @@ +package com.nms.server.common; + +public class MissionConstants { + + public final static int MISSION_STATE_CREATE = 1; + public final static int MISSION_STATE_RUNNING_OK = 2; + public final static int MISSION_STATE_FINISHING = 3; + public final static int MISSION_STATE_RUNNING_FAIL = 4; + public final static int MISSION_STATE_CANCEL = 5; + public final static int MISSION_STATE_CANCEL_RUNNING_OK = 6; + public final static int MISSION_STATE_CANCEL_FINISHING = 7; + +// public final static String ERROR_TASK_PARAMS = "参数错误,任务无法执行;"; +// public final static String ERROR_TASK_NO_FILE = "无推送文件,任务无法执行;"; +// public final static String ERROR_TASK_NO_USED_NODE = "无有效服务器节点,任务无法执行;"; +// public final static String ERROR_TASK_RUND_NO_USED_NODE = "无有效服务器节点,当前周期无法执行;"; +// public final static String WARN_TASK_UN_MANAGEMENT_NODE = "存在DC管理外节点;"; +// public final static String NOTICE_TASK_RUNNING = "任务开始执行"; +// public final static String ERROR_RESULT_NOT_SERVER = "该节点不是服务器"; +// public final static String ERROR_RESULT_UN_MANAGEMENT_NODE = "该节点不在DC管理范围内"; +// public final static String ERROR_RESULT_FILE_DOWNLOAD_FAIL = "DC文件下载失败"; +// public final static String ERROR_RESULT_EXEC_TIMEOUT = "周期任务执行超时,结果置为失败";//endTime<当前时间 + public final static String ERROR_TASK_PARAMS = "i18n_server.MissionConstants.ERROR_TASK_PARAMS_n81i;"; + public final static String ERROR_TASK_NO_FILE = "i18n_server.MissionConstants.ERROR_TASK_NO_FILE_n81i;"; + public final static String ERROR_TASK_NO_USED_NODE = "i18n_server.MissionConstants.ERROR_TASK_NO_USED_NODE_n81i;"; + public final static String ERROR_TASK_RUND_NO_USED_NODE = "i18n_server.MissionConstants.ERROR_TASK_RUND_NO_USED_NODE_n81i;"; + public final static String WARN_TASK_UN_MANAGEMENT_NODE = "i18n_server.MissionConstants.WARN_TASK_UN_MANAGEMENT_NODE_n81i;"; + public final static String NOTICE_TASK_RUNNING = "i18n_server.MissionConstants.NOTICE_TASK_RUNNING_n81i"; + public final static String ERROR_RESULT_NOT_SERVER = "i18n_server.MissionConstants.ERROR_RESULT_NOT_SERVER_n81i"; + public final static String ERROR_RESULT_UN_MANAGEMENT_NODE = "i18n_server.MissionConstants.ERROR_RESULT_UN_MANAGEMENT_NODE_n81i"; + public final static String ERROR_RESULT_FILE_DOWNLOAD_FAIL = "i18n_server.MissionConstants.ERROR_RESULT_FILE_DOWNLOAD_FAIL_n81i"; + public final static String ERROR_RESULT_EXEC_TIMEOUT = "i18n_server.MissionConstants.ERROR_RESULT_EXEC_TIMEOUT_n81i";//endTime<当前时间 +} diff --git a/src/com/nms/server/common/UpdateParams.java b/src/com/nms/server/common/UpdateParams.java new file mode 100644 index 0000000..2624859 --- /dev/null +++ b/src/com/nms/server/common/UpdateParams.java @@ -0,0 +1,40 @@ +package com.nms.server.common; + +public class UpdateParams extends java.util.ListResourceBundle { + public static String CONFIG_UPDATE_FLAG = "config.update.flag"; //更新标示 固定 判断配置文件指定值更新 建议自增1操作 + public static String CONFIG_UPDATE_FLAG_VALUE = "12"; //更新标示 该值 缺省值为0 每次修改都要 + static final String[][] contents = new String[][]{ + {CONFIG_UPDATE_FLAG,CONFIG_UPDATE_FLAG_VALUE}, //更新标示 固定 判断配置文件指定值更新 建议自增1操作 + {"detec.switch.str","switchport"}, // 更新switch 监测类别 + {"detec.systeminfo.str","systeminfo"}, // 更新systeminfo 监测类别 + {"detec.systemdate.str","systemdate"}, // 更新systemdate 监测类别 + {"detec.system.str","system"}, // 更新systeminfo 监测类别 + {"detec.ifxtable.str","ifXTable"},// 更新ifXTable 监测类别 + {"error.info.flag","1"}, // 更新systeminfo 监测类别 + {"data.files.reader.flag","1"}, // 数据保存暂存硬盘标示 + {"systemdate.flag","1"}, // systemdate主动监测监测标示 + {"email.start.flag","1"}, // 邮件发送功能启用标识 + + {"delete.files.flag","1"}, // delete log/error/download files flag + {"keep.file.days","7"}, // keep log/error/download files days + {"delete.files.period","24"}, // 删除log等临时文件的时间间隔,unit:hour + {"mailing.period","120"}, // 邮件发送周期,unit:S + + //2014-1-2 hyx add + {"detect.fail.nmsc.police.level","0"},//握手监测失败时(state=-1),默认的告警级别:0级 + {"detect.fail.nmsc.police.emergent","0"},//握手监测失败时(state=-1),默认的紧急状态:0紧急;1非紧急 + {"detect.fail.non.nmsc.police.level","1"},//非握手监测失败时(state=-1),默认的告警级别:1-5级 + {"detect.fail.non.nmsc.police.emergent","1"},//非握手监测失败时(state=-1),默认的紧急状态:0紧急;1非紧急 + + {"port.alarm.level","5"},//端口异常(流量为0,设置状态和实际状态不符)时,默认的告警级别:1-5级 + {"port.alarm.emergent","1"},//端口异常(流量为0,设置状态和实际状态不符)时,默认的紧急状态:0紧急;1非紧急 + + {"nc.alarm.police.level","0"},//NC主动告警(默认为紧急),state=-2时,默认的告警级别:0级 + {"single.node.detect.timeout","180"}//DC端主动监测,单节点超时时间 + + }; + + public Object[][] getContents() { + return contents; + } +} diff --git a/src/com/nms/server/dao/CommonDao.java b/src/com/nms/server/dao/CommonDao.java new file mode 100644 index 0000000..99fbcdf --- /dev/null +++ b/src/com/nms/server/dao/CommonDao.java @@ -0,0 +1,254 @@ +package com.nms.server.dao; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; +import com.nms.server.util.ConnectionOracle; +import com.nms.server.util.DateUtil; +import com.nms.server.util.SQLExecuteTimeoutException; + +public class CommonDao extends ConnectionOracle { + + public CommonDao() throws SQLException { + super(); + } + + private Logger logger = Logger.getLogger(CommonDao.class); + /** + * 插入数据 封装方法 + * + * @time Jul 27, 2011-4:00:44 PM + * @param tableName + * 插入表 表名 + * @param info + * 字段和值集合 + * @return + * @throws Exception + */ + public boolean insertObj(String tableName, Map info) + throws SQLExecuteTimeoutException,SQLException { + StringBuffer values_0 =new StringBuffer(); + StringBuffer infoSQL = new StringBuffer("insert into " + tableName + + " ("); // SQL + StringBuffer values = new StringBuffer(); // values + // SQL拼写 + Iterator> ite = info.entrySet().iterator(); + ArrayList params = new ArrayList(); + + while (ite.hasNext()) { + Entry entry = ite.next(); + //logger.debug(entry.getKey() +": \""+entry.getValue()+"\"" ); + if (StringUtils.isBlank(entry.getValue())) + continue; // 跳过无效数据 + infoSQL.append(entry.getKey() + ","); + params.add(entry.getValue()); + if (DateUtil.isDate(entry.getValue(), Constants.COMMON_DATE_FORMAT)) { // 日期类型 + values.append(" to_date(?,'" + Constants.DB_DATE_FORMAT + + "'),"); + } else { // 其他类型 + values.append("?,"); + } + //-- + if (DateUtil.isDate(entry.getValue(), Constants.COMMON_DATE_FORMAT)) { // 日期类型 + values_0.append(" to_date('"+entry.getValue()+"','" + Constants.DB_DATE_FORMAT + + "'),"); + } else { // 其他类型 + values_0.append("'"+entry.getValue()+"',"); + } + } + + infoSQL = infoSQL.deleteCharAt(infoSQL.length() - 1); + values = values.deleteCharAt(values.length() - 1); + values_0 = values_0.deleteCharAt(values_0.length() - 1); + logger.debug(infoSQL+") values ("+values_0+")"); + infoSQL.append(") values (" + values + ")"); + return this.dbUpdate(infoSQL.toString(), params.toArray()); + } + + /** + * 插入数据 封装方法 + * + * @time Jul 27, 2011-4:00:44 PM + * @param tableName + * 插入表 表名 + * @param info + * 字段和值集合 + * @return + * @throws Exception + */ + public boolean insertObjByBatch(String tableName, List> infoList) + throws SQLExecuteTimeoutException,SQLException { + if(infoList!=null && infoList.size()>0){ + List sqList = new LinkedList(); + for(Map info : infoList){ + StringBuffer values_0 =new StringBuffer(); + StringBuffer infoSQL = new StringBuffer("insert into " + tableName + + " ("); // SQL + // SQL拼写 + Iterator> ite = info.entrySet().iterator(); + ArrayList params = new ArrayList(); + + while (ite.hasNext()) { + Entry entry = ite.next(); + //logger.debug(entry.getKey() +": \""+entry.getValue()+"\"" ); + if (StringUtils.isBlank(entry.getValue())) + continue; // 跳过无效数据 + infoSQL.append(entry.getKey() + ","); + params.add(entry.getValue()); + + //-- + if (DateUtil.isDate(entry.getValue(), Constants.COMMON_DATE_FORMAT)) { // 日期类型 + values_0 + .append(" to_date('"+entry.getValue()+"','" + Constants.DB_DATE_FORMAT + + "'),"); + } else { // 其他类型 + values_0.append("'"+entry.getValue()+"',"); + } + } + + infoSQL = infoSQL.deleteCharAt(infoSQL.length() - 1); + values_0 = values_0.deleteCharAt(values_0.length() - 1); + logger.debug(infoSQL+") values ("+values_0+")"); + infoSQL.append(") values (" + values_0 + ")"); + sqList.add(infoSQL.toString()); + } + return this.dbUpdateByBatch(sqList); + }else{ + return true; + } + } + + + /** + * 插入数据 封装方法 + * + * @time Jul 27, 2011-4:00:44 PM + * @param tableName + * 插入表 表名 + * @param info + * 字段和值集合 + * @return + * @throws Exception + */ + /*public boolean insertSimpleObjByBatch(String tableName,List> infoList) + throws SQLExecuteTimeoutException,SQLException { + + //固定 字段名 + List columnList = null; + //固定 sql + StringBuffer infoSQL = new StringBuffer("insert into " + tableName + + " ("); // SQL + StringBuffer values = new StringBuffer(); // values + if(infoList!=null && infoList.size()>0){ + Map map = infoList.get(0); + + columnList = Arrays.asList(map.keySet().toArray()); + + for(Object obj : columnList){ + + if(obj == null){ + continue; + } + + String key = obj.toString(); + String value = map.get(key); + + logger.debug(key +": \""+value+"\"" ); + infoSQL.append(key + ","); + + if (DateUtil.isDate(value, Constants.COMMON_DATE_FORMAT)) { // 日期类型 + values + .append(" to_date(?,'" + Constants.DB_DATE_FORMAT + + "'),"); + } else { // 其他类型 + values.append("?,"); + } + } + infoSQL = infoSQL.deleteCharAt(infoSQL.length() - 1); + values = values.deleteCharAt(values.length() - 1); + infoSQL.append(") values (" + values + ")"); + } + + + //整理值 + if(infoList!=null && infoList.size()>0){ + List paramsList = new LinkedList(); + + for(Map info : infoList){ + + for(Object obj : columnList){ + if(obj == null){ + continue; + } + + ArrayList params = new ArrayList(); + String value = info.get(obj); + if(StringUtils.isBlank(value)){ + params.add(""); + }else{ + params.add(info.get(obj)); + } + System.out.println(""+Arrays.toString(params.toArray(new String[0]))); + paramsList.add(params.toArray(new String[0])); + } + + } + return this.dbUpdateByBatch(infoSQL.toString(),paramsList); + }else{ + return true; + } + }*/ + + /** + * 查询数据 封装方法 + * + * @time Jul 27, 2011-4:08:38 PM + * @param attributes + * @param fields{ + * + * @param tableName + * @param info + * @return + * @throws Exception + */ + /*private ArrayList> selectObjs(ArrayList fields, + String tableName, Map info) throws Exception { + StringBuffer seachInfoSQL = new StringBuffer(); // 查询SQL + if (fields == null || fields.size() == 0) { // 全部字段查询 + seachInfoSQL.append("select * from " + tableName + " where 1=1 "); + } else { // 指定字段查询 + seachInfoSQL.append("select "); + for (String attribute : fields) { + seachInfoSQL.append(attribute + ","); + } + seachInfoSQL.deleteCharAt(seachInfoSQL.length() - 1); + seachInfoSQL.append(" from " + tableName + " where 1=1 "); + } + // SQL拼写 + Iterator> ite = info.entrySet().iterator(); + ArrayList params = new ArrayList(); + + while (ite.hasNext()) { + Entry entry = ite.next(); + if (StringUtils.isEmpty(entry.getValue())) + continue; + params.add(entry.getValue()); + if (Common.isDate(entry.getValue(), Constants.COMMON_DATE_FORMAT)) { + seachInfoSQL.append(" and to_char(" + entry.getKey() + ",'" + + Constants.DB_DATE_FORMAT + "') = ?"); + } else { + seachInfoSQL.append(" and " + entry.getKey() + "=?"); + } + } + return this.dbSelect(seachInfoSQL.toString(), fields, params.toArray()); + }*/ +} diff --git a/src/com/nms/server/dao/OracleToMysql.java b/src/com/nms/server/dao/OracleToMysql.java new file mode 100644 index 0000000..1ce9956 --- /dev/null +++ b/src/com/nms/server/dao/OracleToMysql.java @@ -0,0 +1,473 @@ +package com.nms.server.dao; + +import java.util.LinkedHashMap; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; + +/** + * oracle 特殊函数 适配 mysql 数据库 sql 转换工具 + * @author fang + * + */ +public class OracleToMysql { + static final Logger logger = Logger.getLogger(OracleToMysql.class); + /** + * decode函数正则匹配,不支持括号嵌套 + */ + static final Pattern DECODE_PATTERN = Pattern.compile("([^\\w]+)decode\\s*\\(([^\\(\\)]*)\\)",Pattern.CASE_INSENSITIVE); + /** + * nvl函数正则匹配 + */ + static final Pattern NVL_PATTERN = Pattern.compile("[^\\w]+(nvl)\\s*\\(",Pattern.CASE_INSENSITIVE); + /** + * sysdate + */ + static final Pattern SYSDATE_PATTERN = Pattern.compile("[^\\w]+(sysdate)[^\\w]+",Pattern.CASE_INSENSITIVE); + /** + * sysdate-1 oracle 日期加减 + * ([^\\w]+)(sysdate\\s*([-+])\\s*(\\w+))([^\\w]+) + */ + static final Pattern DATE_CAL_PATTERN = Pattern.compile("([^\\w]+)(sysdate\\s*([-+])\\s*([0-9/]+))([^\\w]+|$)",Pattern.CASE_INSENSITIVE); + + /** + * nextval + */ + static final Pattern NEXTVAL_PATTERN = Pattern.compile("([^\\w]+)([\\w]+)\\s*\\.\\s*nextval([^\\w]+)",Pattern.CASE_INSENSITIVE); + /** + * currval + */ + static final Pattern CURRVAL_PATTERN = Pattern.compile("([^\\w]+)([\\w]+)\\s*\\.\\s*currval([^\\w]+)",Pattern.CASE_INSENSITIVE); + + /** + * to_date + */ + static final Pattern TO_DATE_PATTERN = Pattern.compile("([\\W])to_date\\s*\\(([^\\)]+),([^\\)]+)",Pattern.CASE_INSENSITIVE); + + /** + * to_char + */ + static final Pattern TO_CHAR_PATTERN = Pattern.compile("([\\W])to_char\\s*\\(([^\\)]+),([^\\)]+)",Pattern.CASE_INSENSITIVE); + /** + * 数字 转字符 + */ + static final Pattern TO_CHAR_CAST_PATTERN = Pattern.compile("([\\W])to_char\\s*\\(([^\\),]+)",Pattern.CASE_INSENSITIVE); + + /** + * trunc 函数截取日期 只匹配 sysdate 转换成 date_add 的语句 + */ + static final Pattern TRUNC_DATE_PATTERN = Pattern.compile("[^\\w]+(trunc)\\s*\\(\\s*date_add",Pattern.CASE_INSENSITIVE); + + /** + * instr函数 + */ + static final Pattern INSTR_PATTERN = Pattern.compile("([^\\w]+)(instr)\\s*\\(([^\\)]+)\\)",Pattern.CASE_INSENSITIVE); + + + /** + * where 条件中的rownum + */ + //static final Pattern WHERE_ROWNUM_PATTERN = Pattern.compile("([^\\w]+)where([^\\)]+)rownum\\s*( DATEFORMAT_MAPPING = new LinkedHashMap(); + static{ + /*DATEFORMAT_MAPPING.put("","%a");//工作日的缩写名称  (Sun..Sat) + DATEFORMAT_MAPPING.put("","%b");//月份的缩写名称  (Jan..Dec) + DATEFORMAT_MAPPING.put("","%c");//月份,数字形式(0..12) + DATEFORMAT_MAPPING.put("","%D");//带有英语后缀的该月日期  (0th, 1st, 2nd, 3rd, ...) + DATEFORMAT_MAPPING.put("","%d");//该月日期, 数字形式 (00..31) + DATEFORMAT_MAPPING.put("","%e");//该月日期, 数字形式(0..31) + DATEFORMAT_MAPPING.put("","%f");//微秒 (000000..999999) + DATEFORMAT_MAPPING.put("","%H");//小时(00..23) + DATEFORMAT_MAPPING.put("","%h");//小时(01..12) + DATEFORMAT_MAPPING.put("","%I");//小时 (01..12) + DATEFORMAT_MAPPING.put("","%i");//分钟,数字形式 (00..59) + DATEFORMAT_MAPPING.put("","%j");//一年中的天数 (001..366) + DATEFORMAT_MAPPING.put("","%k");//小时 (0..23) + DATEFORMAT_MAPPING.put("","%l");//小时 (1..12) + DATEFORMAT_MAPPING.put("","%M");//月份名称 (January..December) + DATEFORMAT_MAPPING.put("","%m");//月份, 数字形式 (00..12) + DATEFORMAT_MAPPING.put("","%p");//上午(AM)或下午( PM) + DATEFORMAT_MAPPING.put("","%r");//时间 , 12小时制 (小时hh:分钟mm:秒数ss 后加 AM或PM) + DATEFORMAT_MAPPING.put("","%S");//秒 (00..59) + DATEFORMAT_MAPPING.put("","%s");//秒 (00..59) + DATEFORMAT_MAPPING.put("","%T");//时间 , 24小时制 (小时hh:分钟mm:秒数ss) + DATEFORMAT_MAPPING.put("","%U");//周 (00..53), 其中周日为每周的第一天 + DATEFORMAT_MAPPING.put("","%u");//周 (00..53), 其中周一为每周的第一天  + DATEFORMAT_MAPPING.put("","%V");//周 (01..53), 其中周日为每周的第一天 ; 和 %X同时使用 + DATEFORMAT_MAPPING.put("","%v");//周 (01..53), 其中周一为每周的第一天 ; 和 %x同时使用 + DATEFORMAT_MAPPING.put("","%W");//工作日名称 (周日..周六) + DATEFORMAT_MAPPING.put("","%w");//一周中的每日 (0=周日..6=周六) + DATEFORMAT_MAPPING.put("","%X");//该周的年份,其中周日为每周的第一天, 数字形式,4位数;和%V同时使用 + DATEFORMAT_MAPPING.put("","%x");//该周的年份,其中周一为每周的第一天, 数字形式,4位数;和%v同时使用 + DATEFORMAT_MAPPING.put("","%Y");//年份, 数字形式,4位数 + DATEFORMAT_MAPPING.put("","%y");//年份, 数字形式 (2位数) + */ + DATEFORMAT_MAPPING.put("YYYY-MM-DD HH24:MI:SS","%Y-%m-%d %H:%i:%s"); + DATEFORMAT_MAPPING.put("YYYY-MM-DD HH24:MI","%Y-%m-%d %H:%i"); + DATEFORMAT_MAPPING.put("YYYY-MM-DD","%Y-%m-%d"); + DATEFORMAT_MAPPING.put("HH24:MI:SS","%H:%i:%s"); + } + + /** + * oracle sql 转换 成 mysql + * @param sql + * @return + */ + public static String trans(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + logger.debug("before sql -> :" + sql); + if(Constants.IS_MYSQL){ + sql = decodeToCaseWhen(sql); + sql = nextvalToFunction(sql); + sql = currvalToFunction(sql); + sql = toCharToDateFormat(sql); + sql = toDateToStrtodate(sql); + sql = nvlToIfnull(sql); + sql = sysdateCal(sql); + sql = sysdateToNow(sql); + sql = truncSysdate(sql); + //sql = instr(sql); //@2018年4月19日15:46:47 直接修改 sql 语句,将 第3,4个参数删除,都是默认 :1 + sql = toChar(sql); + logger.debug("after sql -> :" + sql); + } + return sql; + } + + + /** + * oracle decode函数 转换为 case when 语句 + * @param decode + * @return + */ + public static String decodeToCaseWhen(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = DECODE_PATTERN.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + String group = m.group(2); + String[] split = group.split(","); + int len = split.length; + StringBuilder caseWhen = new StringBuilder(m.group(1)); + caseWhen.append(" ( case "); + caseWhen.append(split[0]); + for(int i = 1;i "+caseWhen.toString()); + m.appendReplacement(sb, caseWhen.toString()); + } + m.appendTail(sb); + return sb.toString(); + } + + /** + * oracle nvl 转成 mysql ifNull + * @param sql + * @return + */ + public static String nvlToIfnull(String sql){ + return regexReplace(sql, NVL_PATTERN, "IFNULL" ,1); + } + + /** + * sysdate 转成 now() + * @param sql + * @return + */ + public static String sysdateToNow(String sql){ + return regexReplace(sql, SYSDATE_PATTERN, "now()" ,1); + } + + /** + * trunc 函数截取日期 只匹配 sysdate 转换成 date_add 的语句 + * @param sql + * @return + */ + public static String truncSysdate(String sql){ + return regexReplace(sql, TRUNC_DATE_PATTERN, "date" ,1); + } + /** + * sysdate 日期加减 + * ([^\\w]+)(sysdate\\s*([-+])\\s*((\\d+/)*\\d+))([^\\w]+) + * @param sql + * @return + */ + public static String sysdateCal(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = DATE_CAL_PATTERN.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + StringBuilder temp = new StringBuilder(); + String signal = m.group(3);//signal - + + String number = m.group(4);// \\d+ + temp.append(m.group(1)); + temp.append("date_add(now(),interval "); + temp.append(signal); + temp.append(number); + temp.append("*24*60*60 second)"); + temp.append(m.group(5)); + logger.debug(m.group() +" --> "+temp.toString()); + m.appendReplacement(sb, temp.toString()); + } + m.appendTail(sb); + return sb.toString(); + } + + /** + * oracle 序列 转成 mysql 自定义函数 nextval('') + * @return + */ + public static String nextvalToFunction(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = NEXTVAL_PATTERN.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + StringBuilder temp = new StringBuilder(); + String seqName = m.group(2);//seq name + temp.append(m.group(1)); + temp.append(Constants.NEXTVAL_FUNCTION_NAME); + temp.append("('"); + temp.append(seqName); + temp.append("')"); + temp.append(m.group(3)); + logger.debug(m.group() +" --> "+temp.toString()); + m.appendReplacement(sb, temp.toString()); + } + m.appendTail(sb); + return sb.toString(); + } + + + /** + * oracle 序列 转成 mysql 自定义函数 currval('') + * @return + */ + public static String currvalToFunction(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = CURRVAL_PATTERN.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + StringBuilder temp = new StringBuilder(); + String seqName = m.group(2);//seq name + temp.append(m.group(1)); + temp.append(Constants.CURRVAL_FUNCTION_NAME); + temp.append("('"); + temp.append(seqName); + temp.append("')"); + temp.append(m.group(3)); + logger.debug(m.group() +" --> "+temp.toString()); + m.appendReplacement(sb, temp.toString()); + } + m.appendTail(sb); + return sb.toString(); + } + + /** + * oracle to_date 函数 转换为 str_to_date + * @param sql + * @return + */ + public static String toDateToStrtodate(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = TO_DATE_PATTERN.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + StringBuilder temp = new StringBuilder(); + String fm = m.group(3);//seq name + String mfm = DATEFORMAT_MAPPING.get(fm.replaceAll("'", "").trim().toUpperCase()); +// Objects.requireNonNull(mfm, fm + " 没有mysql格式化映射"); + Objects.requireNonNull(mfm, fm + " No MySQL formatting mappings"); + temp.append(m.group(1)); + temp.append("str_to_date("); + temp.append(m.group(2)); + temp.append(",'"); + temp.append(mfm); + temp.append("'"); + logger.debug(m.group() +" --> "+temp.toString()); + m.appendReplacement(sb, temp.toString()); + } + m.appendTail(sb); + return sb.toString(); + } + + /** + * oracle to_char 函数 转换为 date_format + * @param sql + * @return + */ + public static String toCharToDateFormat(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = TO_CHAR_PATTERN.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + StringBuilder temp = new StringBuilder(); + String fm = m.group(3);//seq name + String mfm = DATEFORMAT_MAPPING.get(fm.replaceAll("'", "").trim().toUpperCase()); +// Objects.requireNonNull(mfm, m.group() + " 没有mysql格式化映射"); + Objects.requireNonNull(mfm, m.group() + " No MySQL formatting mappings"); + temp.append(m.group(1)); + temp.append("date_format("); + temp.append(m.group(2)); + temp.append(",'"); + temp.append(mfm); + temp.append("'"); + logger.debug(m.group() +" --> "+temp.toString()); + m.appendReplacement(sb, temp.toString()); + } + m.appendTail(sb); + return sb.toString(); + } + + /** + * 数字转字符,只匹配 to_char 有一个参数的 + * ([\\W]+)to_char\\s*\\(([^\\),]+)) + * @param sql + * @return + */ + public static String toChar(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = TO_CHAR_CAST_PATTERN.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + StringBuilder temp = new StringBuilder(); + temp.append(m.group(1)); + temp.append(" convert("); + temp.append(m.group(2)); + temp.append(" , CHAR "); + logger.debug(m.group() +" --> "+temp.toString()); + m.appendReplacement(sb, temp.toString()); + } + m.appendTail(sb); + return sb.toString(); + } + + + /** + * instr mysql 只有两个参数 + * 查遍所有 instr 函数,第三四个参数都为 1可以省略,若不为 1 则不能使用此函数 + * ([^\\w]+)(instr)\\s*\\(([^\\)]+)\\) + * @param sql + * @return + */ + public static String instr(String sql){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = INSTR_PATTERN.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + StringBuilder temp = new StringBuilder(); + String param = m.group(3);//seq name + Matcher dyh = DYH_PATTERN.matcher(param); + StringBuffer ss = new StringBuffer(); + while(dyh.find()){ + String group = dyh.group(1); + group = group.replaceAll(",", "@@@"); + dyh.appendReplacement(ss, group); + } + dyh.appendTail(ss); + String[] params = ss.toString().split(","); + temp.append(m.group(1)); + temp.append("instr("); + temp.append(params[0].replaceAll("@@@", ",")); + temp.append(","); + temp.append(params[1].replaceAll("@@@", ",")); + temp.append(")"); + logger.debug(m.group() +" --> "+temp.toString()); + m.appendReplacement(sb, temp.toString()); + } + m.appendTail(sb); + return sb.toString(); + } + + + /** + * 替换 rownum -> limit + * ([^\\w]+)where([^\\)]+)rownum\\s*( "+temp.toString()); + m.appendReplacement(sb, temp.toString()); + } + m.appendTail(sb); + return sb.toString(); + }*/ + + /** + * 正则替换 + * @param sql + * @param p + * @param replace + * @return + */ + private static String regexReplace(String sql,Pattern p,String replace,Integer groupNum){ +// Objects.requireNonNull(sql, "sql语句不能为空"); + Objects.requireNonNull(sql, "The SQL statement cannot be empty"); + Matcher m = p.matcher(sql); + StringBuffer sb = new StringBuffer(); + while(m.find()){ + String group = m.group(); + if(groupNum != null && groupNum > 0){ + String gn = m.group(groupNum); + group = group.replace(gn, replace); + } + logger.debug(m.group() +" --> "+group); + m.appendReplacement(sb, group); + } + m.appendTail(sb); + return sb.toString(); + } + + public static void main(String[] args) { + String sql = "delete event_record_library where create_time < sysdate-4 and sysdate -4 > now()"; + sql = trans(sql); + System.out.println(sql); + } + +} diff --git a/src/com/nms/server/service/ChangeService.java b/src/com/nms/server/service/ChangeService.java new file mode 100644 index 0000000..4b37af3 --- /dev/null +++ b/src/com/nms/server/service/ChangeService.java @@ -0,0 +1,290 @@ +package com.nms.server.service; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.EventRecordLibrary; +import com.nms.server.bean.ServerIpSegment; +import com.nms.server.bean.ServerTable; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; + +/** + *

变更操作

+ *

针对操作变更未及时下发到指定Client,而进行的后续操作

+ * @date Mar 6, 2012 8:33:40 AM + * @author ZhangGang + * + */ +public class ChangeService extends CommonService{ + private Logger logger = Logger.getLogger(ChangeService.class); + public ChangeService(CommonDao dao){ + super(dao); + } + + public List getNewEventRecordList() { + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + List erlList = new LinkedList(); + + StringBuffer sql = new StringBuffer(); + sql.append("select "); + sql.append("erl.id, "); + sql.append("erl.record_type, "); + sql.append("erl.record_content, "); + sql.append("erl.seq_ids, "); + sql.append("erl.record_command, "); + sql.append("to_char(erl.create_time,'"+Constants.DB_DATE_FORMAT+"') create_time, "); + sql.append("erl.state "); + sql.append("from event_record_library erl "); + sql.append("where erl.state = 1 "); + sql.append("and erl.NMSSERVER_ID = "+Common.getServerTable().getId()); + sql.append(" order by erl.create_time asc"); + + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("state"); + fields.add("record_type"); + fields.add("record_command"); + fields.add("record_content"); + fields.add("seq_ids"); + fields.add("create_time"); + + try { + List> maps = dao.dbSelect(sql.toString(), fields); + if (maps!=null && maps.size()>0) { + for (Map map : maps) { + EventRecordLibrary erl = new EventRecordLibrary(); + erl.setId(StringUtils.isEmpty(map.get("id"))?null:Long.parseLong(map.get("id"))); + erl.setState(StringUtils.isEmpty(map.get("state"))?null:Long.parseLong(map.get("state"))); + erl.setRecordType(map.get("record_type")); + erl.setRecordCommand(map.get("record_command")); + erl.setRecordContent(map.get("record_content")); + erl.setSeqId(StringUtils.isEmpty(map.get("seq_ids"))?null:Long.parseLong(map.get("seq_ids"))); + erl.setCreateTime(StringUtils.isEmpty(map.get("create_time"))?null:format.parse(map.get("create_time"))); + erlList.add(erl); + } + } + } catch (Exception e) { + logger.error("EventRecordLibrary encapsulates ERROR",e); + }finally{ + dao.clearConn(); + } + return erlList; + } + + public void saveEventRecordLibrary(String cmd,String seq_id,String type, String content) { + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + try { + Map library = new HashMap(); + library.put("record_command", cmd); + library.put("record_content", content); + library.put("record_type", type); + library.put("seq_ids", seq_id); + library.put("state", "1"); + library.put("NMSSERVER_ID", ""+Common.getServerTable().getId()); + library.put("create_time", format.format(System.currentTimeMillis())); +// System.out.println("record_command"+ cmd); +// System.out.println("record_content"+ content); +// System.out.println("record_type"+ type); +// System.out.println("seq_ids"+ seq_id); +// System.out.println("state"+ "1"); +// System.out.println("NMSSERVER_ID"+ ""+Common.getServerTable().getId()); +// System.out.println("create_time"+ format.format(System.currentTimeMillis())); + dao.insertObj("event_record_library",library); + } catch (Exception e) { + logger.error("Change record save failure", e); + }finally{ + dao.clearConn(); + } + } + + + + public void updateEventRecordLibrary(EventRecordLibrary event,String createTime,String content) { + if(event.getId() == null){ + return ; + } + String sql = "update event_record_library erl set RECORD_CONTENT='"+content+"' ,CREATE_TIME=to_date('"+createTime+"','yyyy-MM-dd HH24:mi:ss' ) where id="+event.getId(); + try { + dao.dbUpdate(sql); + } catch (Exception e) { + logger.error("Update the event_record_library exception: Record ID="+event.getId(),e); + }finally{ + dao.clearConn(); + } + } + + public void deleteEventRecordBySeqId(Long id) { + if(id == null ){ + return ; + } + String sql = "delete from event_record_library where lower(RECORD_TYPE)=lower('S2C') and SEQ_IDS = '"+id.longValue()+"'"; + try { + dao.dbUpdate(sql); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + public void deleteEventRecordByIds(String ids) { + if(StringUtils.isEmpty(ids)){ + return ; + } + String sql = "delete from event_record_library where id in ("+ids+")"; + try { + dao.dbUpdate(sql); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + public void deleteEventRecordOldInfo() { +// String sql = "delete event_record_library t where sysdate - t.create_time > 4"; + String sql = "delete from event_record_library where create_time < sysdate-4 "; + try { + dao.dbUpdate(sql); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + + public EventRecordLibrary selectEventRecordInfo(String recordType,String recordCommand,String seqId) { + EventRecordLibrary event = null; + try { + if(StringUtils.isBlank(seqId)) { + return event; + } + ArrayList> mapList = null; + + ArrayList fields = new ArrayList(); + fields.clear(); + fields.add("id"); + fields.add("RECORD_TYPE"); + fields.add("RECORD_CONTENT"); + fields.add("SEQ_IDS"); + fields.add("RECORD_COMMAND"); + fields.add("CREATE_TIME"); + fields.add("NMSSERVER_ID"); + + mapList = dao.dbSelect(" select ID,RECORD_TYPE,RECORD_CONTENT,SEQ_IDS,RECORD_COMMAND,CREATE_TIME,NMSSERVER_ID " + + " from event_record_library erl " + + " where erl.RECORD_TYPE='"+recordType+"' and erl.RECORD_COMMAND='"+recordCommand+"' and erl.SEQ_IDS=" + Long.parseLong(seqId.trim()) + + " order by erl.CREATE_TIME desc",fields); + + if(mapList!=null && mapList.size()>0){ + Map map = mapList.get(0); + if(StringUtils.isNotBlank(map.get("id")) ) { + event = new EventRecordLibrary(); + event.setId(Long.valueOf(map.get("id"))); + event.setRecordType(map.get("RECORD_TYPE")); + event.setRecordContent(map.get("RECORD_CONTENT")); + event.setSeqId(StringUtils.isBlank(map.get("SEQ_IDS"))? null:Long.valueOf(map.get("SEQ_IDS"))); + event.setRecordCommand(map.get("RECORD_COMMAND")); + } + } + } catch (Exception e1) { + logger.error("Query the event_record_library table exception",e1); + }finally{ + dao.clearConn(); + } + return event; + } + /*public static void main(String [] args) { + CommonDao dao = null; + ChangeService service = null; + try { + dao = new CommonDao(); + service = new ChangeService(new CommonDao()); + Common.setServerTable(service.getNMSServerParams("10.0.6.242")); + List list = service.getNewEventRecordList(); + for (int i = 0; i < list.size(); i++) { + EventRecordLibrary library = list.get(i); + pl(library.getId()+" "+library.getSeqId()+" "+library.getRecordType()+" "+library.getState()); + } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + if(dao!=null)dao.close(); + } + + + }*/ + + public ServerTable getNMSServerParams(String ip) throws Exception{ + ServerTable serverTable = null; + try { + //查询server 信息 + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("server_name"); + fields.add("server_state"); + fields.add("server_ip"); + fields.add("server_ipn"); + ArrayList> mapsList = dao.dbSelect("select st.id,st.server_name,st.server_state, st.server_ip,st.server_ipn from server_table st where st.server_ip='" + ip + "'", fields); +// pl(""+mapsList.size()); + if(mapsList!=null && mapsList.size()>0){ + serverTable = new ServerTable(); + Map map = mapsList.get(0); + serverTable.setId(StringUtils.isEmpty(map.get(fields.get(0))) ? null : Long.parseLong(map.get(fields.get(0)))); + serverTable.setServerName(StringUtils.isEmpty(map.get(fields.get(1))) ? null : map.get(fields.get(1))); + serverTable.setServerState(StringUtils.isEmpty(map.get(fields.get(2))) ? null : Long.parseLong(map.get(fields.get(2)))); + serverTable.setServerIp(StringUtils.isEmpty(map.get(fields.get(3))) ? null : map.get(fields.get(3))); + serverTable.setServerIpn(StringUtils.isEmpty(map.get(fields.get(4))) ? null : Long.parseLong(map.get(fields.get(4)))); + }else{ +// throw new Exception("IP为 "+ip+" 的DataController信息 数据库中不存在,请检查网络设置或进行人工DataController配置"); + throw new Exception("IP does not exist in the DataController information database for "+ip+" Check the network settings or manual DataController configuration"); + } + + //查询server 的管理IP段信息 + if(serverTable!= null && serverTable.getId()!=null){ + serverTable.setIpSegList(new LinkedList()); + fields.clear(); + fields.add("start_ip"); + fields.add("start_ipn"); + fields.add("end_ip"); + fields.add("end_ipn"); + mapsList = dao.dbSelect("select sis.start_ip,sis.start_ipn,sis.end_ip,sis.end_ipn from server_ip_segment sis where sis.server_id='" + serverTable.getId().longValue() + "'", fields); +// pl(""+mapsList.size()); + if(mapsList!=null && mapsList.size()>0){ + for(Map map : mapsList){ + ServerIpSegment segment = new ServerIpSegment(); + segment.setStartIp(StringUtils.isEmpty(map.get(fields.get(0))) ? null : map.get(fields.get(0))); + segment.setStartIpn(StringUtils.isEmpty(map.get(fields.get(1))) ? null : Long.parseLong(map.get(fields.get(1)))); + segment.setEndIp(StringUtils.isEmpty(map.get(fields.get(2))) ? null : map.get(fields.get(2))); + segment.setEndIpn(StringUtils.isEmpty(map.get(fields.get(3))) ? null : Long.parseLong(map.get(fields.get(3)))); + serverTable.getIpSegList().add(segment); + } + }else{ +// throw new Exception("IP为 "+ip+" 的DataController的IP段信息 数据库中不存在 请进行人工配置"); + throw new Exception("IP does not exist in the "+ip+" segment information database of DataController for IP. Please configure manually"); + } + }else{ +// throw new Exception("IP为 "+ip+" 的DataController信息 查询异常 无法查询出ID"); + throw new Exception("DataController information with IP "+ip+" query exception Unable to query ID"); + } + } catch (Exception e) { + logger.error("By parameter:"+ ip+" to get node information failure",e); + }finally{ + dao.clearConn(); + } + return serverTable; + + } + +} diff --git a/src/com/nms/server/service/CommonService.java b/src/com/nms/server/service/CommonService.java new file mode 100644 index 0000000..e83d5ab --- /dev/null +++ b/src/com/nms/server/service/CommonService.java @@ -0,0 +1,6656 @@ +package com.nms.server.service; + +import java.io.IOException; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import net.sf.json.JSONException; +import net.sf.json.JSONObject; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.apache.log4j.Logger; + +import sun.misc.BASE64Decoder; + +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.EmailInfo; +import com.nms.server.bean.NmsErrorInfo; +import com.nms.server.bean.NodeModel; +import com.nms.server.bean.NodegroupModel; +import com.nms.server.bean.ServerIpSegment; +import com.nms.server.bean.ServerTable; +import com.nms.server.bean.SetInfo; +import com.nms.server.bean.SimpleNode; +import com.nms.server.bean.TableColumnsModel; +import com.nms.server.bean.TableModel; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.common.EmailTypeConstants; +import com.nms.server.dao.CommonDao; +import com.nms.server.util.DateUtil; +import com.nms.server.util.IpCovert; +import com.nms.server.util.SQLExecuteTimeoutException; +import com.nms.server.util.StringUtil; + + +/** + * 公用业务操作类 + * @date Jul 20, 2011 3:16:00 PM + * @author ZhangGang + * + */ +public class CommonService { + private final Logger logger = Logger.getLogger(CommonService.class); + private final SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); //Java Date 类型数据格式化格式 + private final String PING_STR = Constants.DETEC_PING_STR; + private final String NMSC_STR = Constants.DETEC_NMSC_STR; + protected CommonDao dao = null; + + public CommonService(CommonDao dao){ + this.dao = dao; + } + + public int getFlagEmail() { + int emailFlag = 0;//1:启动,0:关闭 + try { + ArrayList fields = new ArrayList(); + fields.add("type_identity"); + + //参数配置中0表示启用,1表示停用 + String selectSql = "select type_identity from type_table t where t.type_state=0 and lower(t.type_identity)=lower('"+Constants.EMAIL_TYPE_IDENTITY+"')"; + List list = dao.dbSelect(selectSql, fields); + if(list!=null && list.size()>0) { + emailFlag = 1; + } + } catch (Exception e) { + logger.error("", e); + } + return emailFlag; + } + /** + * 获取指定ID的SetInfo信息 + * @time Oct 12, 2011-10:45:54 AM + * @param setInfoId SetInfo ID + * @return + */ + public SetInfo getSetInfoById(Long setInfoId) { + SetInfo setInfo = new SetInfo(); + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("node_ips_id"); + fields.add("check_type_id"); + fields.add("check_gap"); + fields.add("check_out_time"); + fields.add("check_max_times"); + fields.add("process_Iden"); + fields.add("process_file"); + fields.add("process_path"); + fields.add("detection_set_state"); + fields.add("check_way"); + fields.add("check_type_name"); + fields.add("is_control_start"); + fields.add("control_start_time"); + fields.add("upload_gap"); + fields.add("plan_check_time"); + fields.add("is_schedule"); + fields.add("is_snmp"); + fields.add("node_groups_id"); + fields.add("process_search_keycode"); + + ArrayList> mapList = dao + .dbSelect( + "select dsi.id,dsi.check_type_id,dsi.node_ips_id,dsi.check_gap,dsi.check_out_time,dsi.check_max_times,check_way,dsi.process_Iden,dsi.process_file,dsi.process_path,cti.check_type_name,cti.is_snmp,dsi.detection_set_state, " + + "to_char(dsi.plan_check_time,'" + Constants.DB_DATE_FORMAT + "') plan_check_time, " + + "dsi.is_control_start,dsi.upload_gap,dsi.control_start_time,is_schedule,dsi.node_groups_id,dsi.process_search_keycode " + + "from detection_set_info dsi " + + "left join check_type_info cti on cti.id = dsi.check_type_id " + + "where dsi.id = '" + setInfoId.longValue() + "' ", + fields); + if (mapList != null && mapList.size() > 0) { + for (Map map : mapList) { + setInfo.setId(setInfoId); + setInfo.setCheckTypeId(StringUtils.isEmpty(map.get("check_type_id"))?null:Long.valueOf(map.get("check_type_id"))); + setInfo.setCheckGap(StringUtils.isEmpty(map.get("check_gap"))?null:Long.valueOf(map.get("check_gap"))); + setInfo.setCheckOutTime( + StringUtils.isEmpty(map.get("check_out_time"))?null:Long.valueOf(map.get("check_out_time"))); + setInfo.setCheckMaxTimes( + StringUtils.isEmpty(map.get("check_max_times"))?null:Long.valueOf(map.get("check_max_times"))); + setInfo.setProcessIden(map.get("process_Iden")); + setInfo.setProcessFile(map.get("process_file")); + setInfo.setProcessPath(map.get("process_path")); + setInfo.setCheckWay(map.get("check_way")); + setInfo.setCheckState(map.get("detection_set_state")); + setInfo.setCheckTypeName(map.get("check_type_name")); + setInfo.setIsControlStart(map.get("is_control_start")); + setInfo.setUploadGap( + StringUtils.isEmpty(map.get("upload_gap"))?null:Long.valueOf(map.get("upload_gap")) + ); + if (!StringUtils.isEmpty(map.get("plan_check_time"))) { + setInfo.setPlanCheckTime(format.parse(map.get("plan_check_time")).getTime()); + } + + if (!StringUtils.isEmpty(map.get("control_start_time"))) { + setInfo.setControlStartTime(format.parse(map.get("control_start_time")).getTime()); + } + setInfo.setIsSchedule(StringUtils.trim(map.get("is_schedule"))); + setInfo.setIsSNMP(StringUtils.isBlank(map.get("is_snmp"))?null:Long.valueOf(StringUtils.trim(map.get("is_snmp")))); + setInfo.setNodeIpsId(StringUtils.trim(map.get("node_ips_id"))); + setInfo.setNodeGroupsId(StringUtils.trim(map.get("node_groups_id"))); + setInfo.setProcessSearchKeyCode(StringUtils.trim(map.get("process_search_keycode"))); +// setInfo.setNodeIp(map.get("node_ip")); +// setInfo.setGroupId(StringUtils.isEmpty(map.get("group_id"))?null:Long.valueOf(map.get("group_id"))); + break; + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return setInfo; + } + + + /** + * 获取指定ID的SetInfo信息 + * @time Oct 12, 2011-10:45:54 AM + * @param setInfoId SetInfo ID + * @return + */ + public List getAllSetInfo(Integer setInfoState,Integer checkWay) { + List setInfoList = new ArrayList(); + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("node_ips_id"); + fields.add("check_type_id"); + fields.add("check_gap"); + fields.add("check_out_time"); + fields.add("check_max_times"); + fields.add("process_Iden"); + fields.add("process_file"); + fields.add("process_path"); + fields.add("detection_set_state"); + fields.add("check_way"); + fields.add("check_type_name"); + fields.add("is_control_start"); + fields.add("control_start_time"); + fields.add("upload_gap"); + fields.add("plan_check_time"); + fields.add("is_schedule"); + fields.add("is_snmp"); + fields.add("node_groups_id"); + fields.add("process_search_keycode"); + + String sql = "select dsi.id,dsi.check_type_id,dsi.node_ips_id,dsi.check_gap,dsi.check_out_time,dsi.check_max_times,check_way,dsi.process_Iden,dsi.process_file,dsi.process_path,cti.check_type_name,cti.is_snmp,dsi.detection_set_state, " + + "to_char(dsi.plan_check_time,'" + Constants.DB_DATE_FORMAT + "') plan_check_time, " + + "dsi.is_control_start,dsi.upload_gap,dsi.control_start_time,is_schedule,dsi.node_groups_id,dsi.process_search_keycode " + + "from detection_set_info dsi " + + "left join check_type_info cti on cti.id = dsi.check_type_id " + + "where 1=1"; + if(setInfoState != null ){ + if(!sql.trim().endsWith("where")){ + sql += " and "; + } + sql += " dsi.DETECTION_SET_STATE ='"+setInfoState+"'"; + } + if(checkWay != null ){ + sql += " and dsi.CHECK_WAY ='"+checkWay+"'"; + } + sql += " and lower(cti.check_type_name) <>lower('"+Constants.ALARM_SNMP_TRAP_STR+"')"; + ArrayList> mapList = dao.dbSelect(sql,fields); + if (mapList != null && mapList.size() > 0) { + for (Map map : mapList) { + SetInfo setInfo = new SetInfo(); + setInfo.setId(StringUtils.isEmpty(map.get("id"))?null:Long.valueOf(map.get("id"))); + setInfo.setCheckTypeId(StringUtils.isEmpty(map.get("check_type_id"))?null:Long.valueOf(map.get("check_type_id"))); + setInfo.setCheckGap(StringUtils.isEmpty(map.get("check_gap"))?null:Long.valueOf(map.get("check_gap"))); + setInfo.setCheckOutTime( + StringUtils.isEmpty(map.get("check_out_time"))?null:Long.valueOf(map.get("check_out_time"))); + setInfo.setCheckMaxTimes( + StringUtils.isEmpty(map.get("check_max_times"))?null:Long.valueOf(map.get("check_max_times"))); + setInfo.setProcessIden(map.get("process_Iden")); + setInfo.setProcessFile(map.get("process_file")); + setInfo.setProcessPath(map.get("process_path")); + setInfo.setCheckWay(map.get("check_way")); + setInfo.setCheckState(map.get("detection_set_state")); + setInfo.setCheckTypeName(map.get("check_type_name")); + setInfo.setIsControlStart(map.get("is_control_start")); + setInfo.setUploadGap( + StringUtils.isEmpty(map.get("upload_gap"))?null:Long.valueOf(map.get("upload_gap")) + ); + if (!StringUtils.isEmpty(map.get("plan_check_time"))) { + setInfo.setPlanCheckTime(format.parse(map.get("plan_check_time")).getTime()); + } + + if (!StringUtils.isEmpty(map.get("control_start_time"))) { + setInfo.setControlStartTime(format.parse(map.get("control_start_time")).getTime()); + } + setInfo.setIsSchedule(StringUtils.trim(map.get("is_schedule"))); + setInfo.setIsSNMP(StringUtils.isBlank(map.get("is_snmp"))?null:Long.valueOf(StringUtils.trim(map.get("is_snmp")))); + setInfo.setNodeIpsId(StringUtils.trim(map.get("node_ips_id"))); + setInfo.setNodeGroupsId(StringUtils.trim(map.get("node_groups_id"))); + setInfo.setProcessSearchKeyCode(StringUtils.trim(map.get("process_search_keycode"))); +// setInfo.setNodeIp(map.get("node_ip")); +// setInfo.setGroupId(StringUtils.isEmpty(map.get("group_id"))?null:Long.valueOf(map.get("group_id"))); + setInfoList.add(setInfo); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return setInfoList; + } + /** + * 获取指定SetInfo ID的告警信息集合 + * @time Oct 12, 2011-10:46:18 AM + * @param setInfoId SetInfo ID + * @return + */ + public List getAlarmInfoListBySetInfoId(Long setInfoId) { + List alarmList = new ArrayList(); + ArrayList fields = new ArrayList(); + ArrayList> mapList = null; + try { + // 告警信息查询 + fields.add("id"); + fields.add("detection_set_info_id"); + fields.add("check_type_name"); + fields.add("process_iden"); + fields.add("metadata_id"); + fields.add("show_num"); + fields.add("filed_comments"); + fields.add("police_state"); + fields.add("police_value"); + fields.add("police_unit"); + fields.add("police_symbols"); + fields.add("police_level"); + fields.add("marker"); + fields.add("MARKER_FILED_ID"); + mapList = dao + .dbSelect( + "select dpr.id,dpr.detection_set_info_id,cti.check_type_name,dsi.process_iden,dpr.metadata_id,md.show_num,md.filed_comments,dpr.police_state,dpr.police_value,dpr.police_unit,dpr.police_symbols,dpr.police_level,dpr.marker,dpr.MARKER_FILED_ID " + + "from data_police_relation dpr " + + "left join metadata md on md.id = dpr.metadata_id " + + "left join detection_set_info dsi on dsi.id = dpr.detection_set_info_id " + + "left join check_type_info cti on cti.id = dsi.check_type_id " + + "where dpr.detection_set_info_id = '" + + setInfoId + + "' " + + "and dpr.police_state=0 " + + "order by md.show_num, DPR.POLICE_LEVEL ASC", + fields); + if (mapList != null && mapList.size() > 0) { + Map metadataIdShownumMap= selectAllMetadata(); + for (Map map : mapList) { + AlarmInfo info = new AlarmInfo(); + info.setId(StringUtils.isEmpty(map.get("id")) ? null : Long + .valueOf(map.get("id"))); + info.setSetInfoId(StringUtils.isEmpty(map + .get("detection_set_info_id")) ? null : Long + .valueOf(map.get("detection_set_info_id"))); + info.setCheckType(map.get("check_type_name")); + info.setProcessIden(map.get("process_iden")); + info.setMetadataId(StringUtils.isEmpty(map + .get("metadata_id")) ? null : Long.valueOf(map + .get("metadata_id"))); + info + .setShowNum(StringUtils + .isEmpty(map.get("show_num")) ? null + : Integer.valueOf(map.get("show_num"))); + info.setPoliceState(map.get("police_state")); + info.setPoliceValue(map.get("police_value")); + info.setPoliceUnit(map.get("police_unit")); + info.setPoliceSysmbols(map.get("police_symbols")); + info.setPoliceLevel(Integer + .valueOf(map.get("police_level"))); + info.setFiledCommonts(map.get("filed_comments")); + + info.setMarker(map.get("marker")); + info.setMarkerFiledId(StringUtils.isEmpty(map.get("MARKER_FILED_ID"))?null:Integer.valueOf(map.get("MARKER_FILED_ID"))); + info.setMarkerFiledShowNum(metadataIdShownumMap.get(StringUtils.isEmpty(map.get("MARKER_FILED_ID"))?null:Integer.valueOf(map.get("MARKER_FILED_ID")))); + + alarmList.add(info); + } + } + } catch (Exception e) { + logger.error("",e); + } finally { + dao.clearConn(); + } + return alarmList; + } + + public ServerTable getNMSServerParams(String ip) throws Exception{ + ServerTable serverTable = null; + try { + //查询server 信息 + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("server_name"); + fields.add("server_state"); + fields.add("server_ip"); + fields.add("server_ipn"); + ArrayList> mapsList = dao.dbSelect("select st.id,st.server_name,st.server_state, st.server_ip,st.server_ipn from server_table st where st.server_state=0 order by st.id", fields); +// pl(""+mapsList.size()); + if(mapsList != null && mapsList.size() > 0){ + int index = 1; + for(Map map :mapsList){ + String sip = map.get("server_ip"); + if(ip.equalsIgnoreCase(sip)){ + serverTable = new ServerTable(); + serverTable.setId(StringUtils.isEmpty(map.get(fields.get(0))) ? null : Long.parseLong(map.get(fields.get(0)))); + serverTable.setServerName(StringUtils.isEmpty(map.get(fields.get(1))) ? null : map.get(fields.get(1))); + serverTable.setServerState(StringUtils.isEmpty(map.get(fields.get(2))) ? null : Long.parseLong(map.get(fields.get(2)))); + serverTable.setServerIp(StringUtils.isEmpty(map.get(fields.get(3))) ? null : map.get(fields.get(3))); + serverTable.setServerIpn(StringUtils.isEmpty(map.get(fields.get(4))) ? null : Long.parseLong(map.get(fields.get(4)))); + serverTable.setIndex(index); + serverTable.setConut(mapsList.size()); + break; + } + index ++; + } + + }else{ + throw new Exception("IP为 "+ip+" 的DataController信息 数据库中不存在,请检查网络设置或进行人工DataController配置"); + } + + //查询server 的管理IP段信息 + if(serverTable != null && serverTable.getId() != null){ + serverTable.setIpSegList(new LinkedList()); + fields.clear(); + fields.add("start_ip"); + fields.add("start_ipn"); + fields.add("end_ip"); + fields.add("end_ipn"); + mapsList = dao.dbSelect("select sis.start_ip,sis.start_ipn,sis.end_ip,sis.end_ipn from server_ip_segment sis where sis.server_id='" + serverTable.getId().longValue() + "'", fields); +// pl(""+mapsList.size()); + if(mapsList!=null && mapsList.size()>0){ + for(Map map : mapsList){ + ServerIpSegment segment = new ServerIpSegment(); + segment.setStartIp(StringUtils.isEmpty(map.get(fields.get(0))) ? null : map.get(fields.get(0))); + segment.setStartIpn(StringUtils.isEmpty(map.get(fields.get(1))) ? null : Long.parseLong(map.get(fields.get(1)))); + segment.setEndIp(StringUtils.isEmpty(map.get(fields.get(2))) ? null : map.get(fields.get(2))); + segment.setEndIpn(StringUtils.isEmpty(map.get(fields.get(3))) ? null : Long.parseLong(map.get(fields.get(3)))); + serverTable.getIpSegList().add(segment); + } + }else{ + throw new Exception("IP为 "+ip+" 的DataController的IP段信息 数据库中不存在 请进行人工配置"); + } + }else{ + throw new Exception("IP为 "+ip+" 的DataController信息 查询异常 无法查询出ID"); + } +// } catch (Exception e) { +// logger.error("由参数:"+ ip+" 获取 DataController 参数失败",e); + }finally{ + dao.clearConn(); + } + return serverTable; + } + + /** + * 获取指定SetInfo ID的所涉及到的节点IP集合 + * @time Oct 12, 2011-10:46:49 AM + * @param checkTypeId checkTypeinfo ID + * @return + */ + public String getCheckTypeNameById(Long checkTypeId) { + ArrayList fields = new ArrayList(); + fields.add("check_type_name"); + String checkTypeName = null; + String searchSql = "select t.check_type_name from check_type_info t where t.id = '"+checkTypeId+"'"; + ArrayList> mapList = null; + try { + mapList = dao.dbSelect(searchSql,fields); + if (mapList != null && mapList.size() > 0) { + for (Map map : mapList) { + checkTypeName = map.get("check_type_name"); + } + } + } catch (Exception e) { + logger.error("By parameter:"+ checkTypeId+" to gain monitoring class name failure",e); + }finally{ + dao.clearConn(); + } + return checkTypeName; + } + /** + * 根据节点ID获取节点信息 + * @time Dec 23, 2011-2:20:22 PM + * @return + */ + public NodeModel getNodeInfoById(Long nodeId) { + NodeModel node = null; + if(nodeId == null){ + return node; + } + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_id"); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); +// fields.add("node_name"); + fields.add("node_state"); + fields.add("system_id"); + fields.add("node_group_id"); + + String sql="select t.node_id,t.node_ip, t.seq_id, t.node_type, t.node_name, t.node_state, t.system_id, t.node_group_id from node_table t where t.node_id = '"+nodeId.longValue()+"' "; + ArrayList> mapList = dao.dbSelect(sql,fields); + if (mapList != null && mapList.size() > 0) { + node = new NodeModel(); + Map map = mapList.get(0); + node.setNodeId(nodeId); + node.setNodeIp(map.get("node_ip")); +// node.setNodeName(map.get("node_name")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + node.setNodeState(StringUtils.isEmpty(map.get("node_state"))?null:Long.valueOf(map.get("node_state"))); + node.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); + node.setGroupId(StringUtils.isEmpty(map.get("node_group_id"))?null:Long.valueOf(map.get("node_group_id"))); + } + } catch (Exception e) { + logger.error("From parameter:"+ nodeId +" to get node information failure",e); + }finally{ + dao.clearConn(); + } + return node; + } + + /** + * 根据节点ID获取节点信息 + * @time Dec 23, 2011-2:20:22 PM + * @return + */ + public List getNodeInfoByGroupId(Long groupId) { + List nmList = null; + if(groupId == null){ + return nmList; + } + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_id"); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); + fields.add("node_name"); + fields.add("node_state"); + fields.add("system_id"); + fields.add("node_group_id"); + + + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 ", " nt.group_id in ('"+groupId.longValue()+"') ", "group_id", "parent_group_id"); +// String sql="select t.node_id,t.node_ip, t.seq_id, t.node_type, t.node_name, t.node_state, t.system_id, t.node_group_id " + +// " from node_table t where t.node_group_id in ( " + +// " select nt.group_id " + +// " from NODEGROUP_TABLE nt " + +// " where nt.leaf_group = 1 start with nt.group_id in ('"+groupId.longValue()+"') connect by prior nt.group_id = nt.parent_group_id ) and t.node_state='0' "+" "+getIpnSelect(); + String sql="select t.node_id,t.node_ip, t.seq_id, t.node_type, t.node_name, t.node_state, t.system_id, t.node_group_id " + + " from node_table t where t.node_group_id in ("+startWithConnectBy+") and t.node_state='0' "+" "+getIpnSelect(); + nmList = new ArrayList(); + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); + node.setNodeId(StringUtils.isEmpty(map.get("node_id"))?null:Long.valueOf(map.get("node_id"))); + node.setNodeIp(map.get("node_ip")); +// node.setNodeName(map.get("node_name")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + node.setNodeState(StringUtils.isEmpty(map.get("node_state"))?null:Long.valueOf(map.get("node_state"))); + node.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); + node.setGroupId(StringUtils.isEmpty(map.get("node_group_id"))?null:Long.valueOf(map.get("node_group_id"))); + nmList.add(node); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + public List getAllEffectiveNodeInfo() { + List nmList = null; + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); +// fields.add("node_id"); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); +// fields.add("node_name"); +// fields.add("node_state"); +// fields.add("system_id"); +// fields.add("node_group_id"); + + + String sql="select distinct t.node_ip, t.seq_id, t.node_type from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' "+" "+getIpnSelect(); + nmList = new ArrayList(); + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); +// node.setNodeId(StringUtils.isEmpty(map.get("node_id"))?null:Long.valueOf(map.get("node_id"))); + node.setNodeIp(map.get("node_ip")); +// node.setNodeName(map.get("node_name")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); +// node.setNodeState(StringUtils.isEmpty(map.get("node_state"))?null:Long.valueOf(map.get("node_state"))); +// node.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); +// node.setGroupId(StringUtils.isEmpty(map.get("node_group_id"))?null:Long.valueOf(map.get("node_group_id"))); + nmList.add(node); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + + public List getAllComputerIp() { + List nmList = null; + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + + String sql="select distinct t.node_ip from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' and t.node_type='0' "+" "+getIpnSelect(); + nmList = new ArrayList(); + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + nmList.add(map.get("node_ip")); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + + /** + * 获取所有detection_info_new 表中当前dc的监测数据 + * @author fang + * 2016年2月26日 10:55:59 + * @return + */ + public Map> getAllDetectionInfoNew(){ + //存放结果 + Map> result = null; + try { + + ArrayList fields = new ArrayList(); + fields.add("ID"); + fields.add("DETECTION_SET_INFO_ID"); + fields.add("CHECK_WAY"); + fields.add("DETECTION_STATE_INFO"); + fields.add("PERFORMACE_DATA"); + fields.add("CURRENT_TIMES"); + fields.add("START_TIME"); + fields.add("WAIT_TIME"); + fields.add("DELAY_TIME"); + fields.add("NEXT_CHECK_TIME"); + fields.add("OFF_LINE"); + fields.add("POLICE_LEVEL"); + fields.add("DATA_CHECK_TIME"); + fields.add("DATA_ARRIVE_TIME"); + fields.add("DETECTIONED_STATE"); + fields.add("NODE_IP"); + fields.add("STATUS_CHANGE_TIME"); + fields.add("DATA_CHECK_TIME_DIGITAL"); + fields.add("DATA_ARRIVE_TIME_DIGITAL"); + fields.add("SEQ_ID"); + fields.add("DETECTION_INFO_ID"); + fields.add("VALID"); + fields.add("POLICE_EMERGENT"); + + //查询dc监控下的节点seq_id + String querySeqidSql="select distinct t.seq_id from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' and t.node_type='0' "+" "+getIpnSelect(); + //查询detection_info_new数据 + String sql = "select din.ID,din.DETECTION_SET_INFO_ID,din.CHECK_WAY,din.DETECTION_STATE_INFO,din.PERFORMACE_DATA,din.CURRENT_TIMES,din.START_TIME,din.WAIT_TIME,din.DELAY_TIME,din.NEXT_CHECK_TIME,din.OFF_LINE,din.POLICE_LEVEL,din.DATA_CHECK_TIME,din.DATA_ARRIVE_TIME,din.DETECTIONED_STATE,din.NODE_IP,din.STATUS_CHANGE_TIME,din.DATA_CHECK_TIME_DIGITAL,din.DATA_ARRIVE_TIME_DIGITAL,din.SEQ_ID,din.DETECTION_INFO_ID,din.VALID,din.POLICE_EMERGENT" + + " from detection_info_new din left join ("+querySeqidSql+") s on din.seq_id = s.seq_id"; + + ArrayList> mapList = dao.dbSelectByFetchSize(sql,fields); + result = new HashMap>(); + //整理查询数据 + if(mapList != null && mapList.size() > 0){ + String detecSetInfoId = null; + String seqId = null; + for(Map temp : mapList){ + //监测设置id + detecSetInfoId = temp.get("DETECTION_SET_INFO_ID"); + //监测节点id + seqId = temp.get("SEQ_ID"); + Object[] obj = new Object[33]; + obj[DetectInfo.SETINFOID] = (detecSetInfoId); + obj[DetectInfo.SEQID] = (seqId); + obj[DetectInfo.STATE] = (temp.get("DETECTIONED_STATE") == null ? null : Integer.parseInt(temp.get("DETECTIONED_STATE"))); + obj[DetectInfo.CHECKTIME] = (temp.get("DATA_CHECK_TIME_DIGITAL") == null ? null :Long.parseLong(temp.get("DATA_CHECK_TIME_DIGITAL"))); + obj[DetectInfo.PLEVEL] = (temp.get("POLICE_LEVEL") == null ? -1 : Integer.parseInt(temp.get("POLICE_LEVEL"))); + obj[DetectInfo.URGENTLEVEL] = (temp.get("POLICE_EMERGENT") == null ? -1 : Integer.parseInt(temp.get("POLICE_EMERGENT"))); + obj[DetectInfo.DETECTIONINFOID] = (temp.get("DETECTION_INFO_ID")); + obj[DetectInfo.STATUSCHANGTIME] = (temp.get("STATUS_CHANGE_TIME")== null ? null : format.parse(temp.get("STATUS_CHANGE_TIME")).getTime()); + obj[DetectInfo.ARRIVETIME] = (temp.get("DATA_ARRIVE_TIME_DIGITAL") == null ? null : Long.parseLong(temp.get("DATA_ARRIVE_TIME_DIGITAL"))); + obj[DetectInfo.DELAYTIME] = (temp.get("DELAY_TIME") == null ? null : Integer.parseInt(temp.get("DELAY_TIME"))); + obj[DetectInfo.NEXTCHECKTIME] = (temp.get("NEXT_CHECK_TIME") == null ? null : format.parse(temp.get("NEXT_CHECK_TIME")).getTime()); + obj[DetectInfo.STARTTIME] = (temp.get("START_TIME") == null ? null :format.parse(temp.get("START_TIME")).getTime()); + obj[DetectInfo.DSINFO] = (temp.get("DETECTION_STATE_INFO")); + obj[DetectInfo.PDATA] = (temp.get("PERFORMACE_DATA")); + obj[DetectInfo.CURRENTTIMES] = (temp.get("CURRENT_TIMES") == null ? -1 : Integer.parseInt(temp.get("CURRENT_TIMES"))); + + + if(result.get(detecSetInfoId) == null){//首次放入 + Map seqIdMap = new HashMap(); + seqIdMap.put(seqId, obj); + result.put(detecSetInfoId, seqIdMap); + }else{ + result.get(detecSetInfoId).put(seqId, obj); + } + detecSetInfoId = null; + seqId = null; + } + } + + }catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + + return result; + } + + /** + * 获取对应监测设置detection_info_new 表中当前dc的监测数据 + */ + public Map getDetectionInfoNewBySet(String setId){ + if(StringUtils.isBlank(setId)){ + return null; + } + //存放结果 + Map result = null; + try { + + ArrayList fields = new ArrayList(); + fields.add("ID"); + fields.add("DETECTION_SET_INFO_ID"); + fields.add("CHECK_WAY"); + fields.add("DETECTION_STATE_INFO"); + fields.add("PERFORMACE_DATA"); + fields.add("CURRENT_TIMES"); + fields.add("START_TIME"); + fields.add("WAIT_TIME"); + fields.add("DELAY_TIME"); + fields.add("NEXT_CHECK_TIME"); + fields.add("OFF_LINE"); + fields.add("POLICE_LEVEL"); + fields.add("DATA_CHECK_TIME"); + fields.add("DATA_ARRIVE_TIME"); + fields.add("DETECTIONED_STATE"); + fields.add("NODE_IP"); + fields.add("STATUS_CHANGE_TIME"); + fields.add("DATA_CHECK_TIME_DIGITAL"); + fields.add("DATA_ARRIVE_TIME_DIGITAL"); + fields.add("SEQ_ID"); + fields.add("DETECTION_INFO_ID"); + fields.add("VALID"); + fields.add("POLICE_EMERGENT"); + + //查询dc监控下的节点seq_id + String querySeqidSql="select distinct t.seq_id from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' and t.node_type='0' "+" "+getIpnSelect(); + //查询detection_info_new数据 + String sql = "select din.ID,din.DETECTION_SET_INFO_ID,din.CHECK_WAY,din.DETECTION_STATE_INFO,din.PERFORMACE_DATA,din.CURRENT_TIMES,din.START_TIME,din.WAIT_TIME,din.DELAY_TIME,din.NEXT_CHECK_TIME,din.OFF_LINE,din.POLICE_LEVEL,din.DATA_CHECK_TIME,din.DATA_ARRIVE_TIME,din.DETECTIONED_STATE,din.NODE_IP,din.STATUS_CHANGE_TIME,din.DATA_CHECK_TIME_DIGITAL,din.DATA_ARRIVE_TIME_DIGITAL,din.SEQ_ID,din.DETECTION_INFO_ID,din.VALID,din.POLICE_EMERGENT" + + " from detection_info_new din left join ("+querySeqidSql+") s on din.seq_id = s.seq_id " + + " where DETECTION_SET_INFO_ID="+setId; + + ArrayList> mapList = dao.dbSelectByFetchSize(sql,fields); + //整理查询数据 + if(mapList != null && mapList.size() > 0){ + result = new HashMap(); + String seqId = null; + for(Map temp : mapList){ + //监测节点id + seqId = temp.get("SEQ_ID"); + Object[] obj = new Object[33]; + + obj[DetectInfo.SETINFOID] = (setId); + obj[DetectInfo.SEQID] = (seqId); + obj[DetectInfo.STATE] = (temp.get("DETECTIONED_STATE") == null ? null : Integer.parseInt(temp.get("DETECTIONED_STATE"))); + obj[DetectInfo.CHECKTIME] = (temp.get("DATA_CHECK_TIME_DIGITAL") == null ? null :Long.parseLong(temp.get("DATA_CHECK_TIME_DIGITAL"))); + obj[DetectInfo.PLEVEL] = (temp.get("POLICE_LEVEL") == null ? -1 : Integer.parseInt(temp.get("POLICE_LEVEL"))); + obj[DetectInfo.URGENTLEVEL] = (temp.get("POLICE_EMERGENT") == null ? -1 : Integer.parseInt(temp.get("POLICE_EMERGENT"))); + obj[DetectInfo.DETECTIONINFOID] = (temp.get("DETECTION_INFO_ID")); + obj[DetectInfo.STATUSCHANGTIME] = (temp.get("STATUS_CHANGE_TIME")== null ? null : format.parse(temp.get("STATUS_CHANGE_TIME")).getTime()); + obj[DetectInfo.ARRIVETIME] = (temp.get("DATA_ARRIVE_TIME_DIGITAL") == null ? null : Long.parseLong(temp.get("DATA_ARRIVE_TIME_DIGITAL"))); + obj[DetectInfo.DELAYTIME] = (temp.get("DELAY_TIME") == null ? null : Integer.parseInt(temp.get("DELAY_TIME"))); + obj[DetectInfo.NEXTCHECKTIME] = (temp.get("NEXT_CHECK_TIME") == null ? null : format.parse(temp.get("NEXT_CHECK_TIME")).getTime()); + obj[DetectInfo.STARTTIME] = (temp.get("START_TIME") == null ? null :format.parse(temp.get("START_TIME")).getTime()); + obj[DetectInfo.DSINFO] = (temp.get("DETECTION_STATE_INFO")); + obj[DetectInfo.PDATA] = (temp.get("PERFORMACE_DATA")); + obj[DetectInfo.CURRENTTIMES] = (temp.get("CURRENT_TIMES") == null ? -1 : Integer.parseInt(temp.get("CURRENT_TIMES"))); + + result.put(seqId,obj); + } + } + + }catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + + return result; + } + + + /** + * 获取对应监测设置detection_info_new 表中当前dc的监测数据 + */ + public List getDetectionInfoNewBySetTest(String setId){ + if(StringUtils.isBlank(setId)){ + return null; + } + //存放结果 + List rltList = new ArrayList(); + try { + + ArrayList fields = new ArrayList(); + fields.add("ID"); + fields.add("DETECTION_SET_INFO_ID"); + fields.add("CHECK_WAY"); + fields.add("DETECTION_STATE_INFO"); + fields.add("PERFORMACE_DATA"); + fields.add("CURRENT_TIMES"); + fields.add("START_TIME"); + fields.add("WAIT_TIME"); + fields.add("DELAY_TIME"); + fields.add("NEXT_CHECK_TIME"); + fields.add("OFF_LINE"); + fields.add("POLICE_LEVEL"); + fields.add("DATA_CHECK_TIME"); + fields.add("DATA_ARRIVE_TIME"); + fields.add("DETECTIONED_STATE"); + fields.add("NODE_IP"); + fields.add("STATUS_CHANGE_TIME"); + fields.add("DATA_CHECK_TIME_DIGITAL"); + fields.add("DATA_ARRIVE_TIME_DIGITAL"); + fields.add("SEQ_ID"); +// fields.add("DETECTION_INFO_ID"); +// fields.add("VALID"); + fields.add("POLICE_EMERGENT"); + + //查询dc监控下的节点seq_id + String querySeqidSql="select distinct t.seq_id from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' and t.node_type='0' "+" "+getIpnSelect(); + //查询detection_info_new数据 + String sql = "select din.ID,din.DETECTION_SET_INFO_ID,din.CHECK_WAY,din.DETECTION_STATE_INFO,din.PERFORMACE_DATA,din.CURRENT_TIMES,din.START_TIME,din.WAIT_TIME,din.DELAY_TIME,din.NEXT_CHECK_TIME,din.OFF_LINE,din.POLICE_LEVEL,din.DATA_CHECK_TIME,din.DATA_ARRIVE_TIME,din.DETECTIONED_STATE,din.NODE_IP,din.STATUS_CHANGE_TIME,din.DATA_CHECK_TIME_DIGITAL,din.DATA_ARRIVE_TIME_DIGITAL,din.SEQ_ID,din.POLICE_EMERGENT" + + " from detection_info din left join ("+querySeqidSql+") s on din.seq_id = s.seq_id " + + " where DETECTION_SET_INFO_ID="+setId +" and rownum<=30000"; + +// String sql = "select din.ID,din.DETECTION_SET_INFO_ID,din.CHECK_WAY,din.DETECTION_STATE_INFO,din.PERFORMACE_DATA,din.CURRENT_TIMES,din.START_TIME,din.WAIT_TIME,din.DELAY_TIME,din.NEXT_CHECK_TIME,din.OFF_LINE,din.POLICE_LEVEL,din.DATA_CHECK_TIME,din.DATA_ARRIVE_TIME,din.DETECTIONED_STATE,din.NODE_IP,din.STATUS_CHANGE_TIME,din.DATA_CHECK_TIME_DIGITAL,din.DATA_ARRIVE_TIME_DIGITAL,din.SEQ_ID,din.DETECTION_INFO_ID,din.VALID,din.POLICE_EMERGENT" + +// " from detection_info_new din left join ("+querySeqidSql+") s on din.seq_id = s.seq_id " + +// " where DETECTION_SET_INFO_ID="+setId; + + rltList = dao.dbSelectList(sql, fields); + //整理查询数据 +// if(mapList != null && mapList.size() > 0){ +// result = new HashMap(); +// String seqId = null; +// for(Map temp : mapList){ +// //监测节点id +// seqId = temp.get("SEQ_ID"); +// DetecAllInfo info = new DetecAllInfo(); +// info.setSetInfoId(setId); +// info.setSeqId(seqId); +// info.setState(temp.get("DETECTIONED_STATE") == null ? null : Integer.parseInt(temp.get("DETECTIONED_STATE"))); +// info.setCheckTime(temp.get("DATA_CHECK_TIME_DIGITAL") == null ? null :Long.parseLong(temp.get("DATA_CHECK_TIME_DIGITAL"))); +// info.setpLevel(temp.get("POLICE_LEVEL") == null ? -1 : Integer.parseInt(temp.get("POLICE_LEVEL"))); +// info.setUrgentLevel(temp.get("POLICE_EMERGENT") == null ? -1 : Integer.parseInt(temp.get("POLICE_EMERGENT"))); +// info.setDetectionInfoId(temp.get("DETECTION_INFO_ID")); +// info.setStatusChangTime(temp.get("STATUS_CHANGE_TIME")== null ? null : format.parse(temp.get("STATUS_CHANGE_TIME")).getTime()); +// info.setArriveTime(temp.get("DATA_ARRIVE_TIME_DIGITAL") == null ? null : Long.parseLong(temp.get("DATA_ARRIVE_TIME_DIGITAL"))); +// info.setDelayTime(temp.get("DELAY_TIME") == null ? null : Integer.parseInt(temp.get("DELAY_TIME"))); +// info.setNextCheckTime(temp.get("NEXT_CHECK_TIME") == null ? null : format.parse(temp.get("NEXT_CHECK_TIME")).getTime()); +// info.setStartTime(temp.get("START_TIME") == null ? null :format.parse(temp.get("START_TIME")).getTime()); +// info.setDsinfo(temp.get("DETECTION_STATE_INFO")); +// info.setPdata(temp.get("PERFORMACE_DATA")); +// info.setCurrentTimes(temp.get("CURRENT_TIMES") == null ? -1 : Integer.parseInt(temp.get("CURRENT_TIMES"))); +// +// result.put(seqId,info); +// } +// } + + }catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + + return rltList; + } + public List getDetectionInfoNewListBySet(String setId,List seqIdList){ + if(StringUtils.isBlank(setId) || seqIdList == null || seqIdList.size() < 1){ + return null; + } + //存放结果 + List rltList = new ArrayList(); + String seqIds = StringUtils.join(seqIdList.iterator(), ","); + seqIdList.clear();//清空seqID信息 + try { + + ArrayList fields = new ArrayList(); + fields.add("ID"); + fields.add("DETECTION_SET_INFO_ID"); + fields.add("CHECK_WAY"); + fields.add("DETECTION_STATE_INFO"); + fields.add("PERFORMACE_DATA"); + fields.add("CURRENT_TIMES"); + fields.add("START_TIME"); + fields.add("WAIT_TIME"); + fields.add("DELAY_TIME"); + fields.add("NEXT_CHECK_TIME"); + fields.add("OFF_LINE"); + fields.add("POLICE_LEVEL"); + fields.add("DATA_CHECK_TIME"); + fields.add("DATA_ARRIVE_TIME"); + fields.add("DETECTIONED_STATE"); + fields.add("NODE_IP"); + fields.add("STATUS_CHANGE_TIME"); + fields.add("DATA_CHECK_TIME_DIGITAL"); + fields.add("DATA_ARRIVE_TIME_DIGITAL"); + fields.add("SEQ_ID"); + fields.add("DETECTION_INFO_ID"); + fields.add("VALID"); + fields.add("POLICE_EMERGENT"); + + /*//查询dc监控下的节点seq_id + String querySeqidSql="select distinct t.seq_id from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' and t.node_type='0' "+" "+getIpnSelect(); + //查询detection_info_new数据 + String sql = "select din.ID,din.DETECTION_SET_INFO_ID,din.CHECK_WAY,din.DETECTION_STATE_INFO,din.PERFORMACE_DATA,din.CURRENT_TIMES,din.START_TIME,din.WAIT_TIME,din.DELAY_TIME,din.NEXT_CHECK_TIME,din.OFF_LINE,din.POLICE_LEVEL,din.DATA_CHECK_TIME,din.DATA_ARRIVE_TIME,din.DETECTIONED_STATE,din.NODE_IP,din.STATUS_CHANGE_TIME,din.DATA_CHECK_TIME_DIGITAL,din.DATA_ARRIVE_TIME_DIGITAL,din.SEQ_ID,din.DETECTION_INFO_ID,din.VALID,din.POLICE_EMERGENT" + + " from ("+querySeqidSql+") s left join detection_info_new din on din.seq_id = s.seq_id " + + " where DETECTION_SET_INFO_ID="+setId;*/ + StringBuilder sbSql = new StringBuilder(); + sbSql.append("select din.ID,din.DETECTION_SET_INFO_ID,din.CHECK_WAY,din.DETECTION_STATE_INFO,din.PERFORMACE_DATA,din.CURRENT_TIMES,din.START_TIME,din.WAIT_TIME,din.DELAY_TIME,din.NEXT_CHECK_TIME,din.OFF_LINE,din.POLICE_LEVEL,din.DATA_CHECK_TIME,din.DATA_ARRIVE_TIME,din.DETECTIONED_STATE,din.NODE_IP,din.STATUS_CHANGE_TIME,din.DATA_CHECK_TIME_DIGITAL,din.DATA_ARRIVE_TIME_DIGITAL,din.SEQ_ID,din.DETECTION_INFO_ID,din.VALID,din.POLICE_EMERGENT"); + sbSql.append(" from detection_info_new din "); + sbSql.append(" where DETECTION_SET_INFO_ID = "); + sbSql.append( setId); + sbSql.append( " and din.seq_id in ( "); + sbSql.append(seqIds); + sbSql.append( " )"); + String sql =sbSql.toString(); +// String sql = "select din.ID,din.DETECTION_SET_INFO_ID,din.CHECK_WAY,din.DETECTION_STATE_INFO,din.PERFORMACE_DATA,din.CURRENT_TIMES,din.START_TIME,din.WAIT_TIME,din.DELAY_TIME,din.NEXT_CHECK_TIME,din.OFF_LINE,din.POLICE_LEVEL,din.DATA_CHECK_TIME,din.DATA_ARRIVE_TIME,din.DETECTIONED_STATE,din.NODE_IP,din.STATUS_CHANGE_TIME,din.DATA_CHECK_TIME_DIGITAL,din.DATA_ARRIVE_TIME_DIGITAL,din.SEQ_ID,din.POLICE_EMERGENT" + +// " from detection_info din left join ("+querySeqidSql+") s on din.seq_id = s.seq_id " + +// " where DETECTION_SET_INFO_ID="+setId +" and rownum<=30000"; + logger.debug("查询new表对应监测设置的最新周期数据,进行状态比较:"+sql); + List rltArrayList = dao.dbSelectList(sql, fields); + //整理查询数据 + if(rltArrayList != null && rltArrayList.size() > 0){ + String seqId = null; + String state = null; + String checkTime = null; + String policeLevel = null; + String policeEmergent = null; + String detectionInfoId = null; + String statusChangeTime = null; + String dataArriveTimeDigital = null; + String delayTime = null; + String nextCheckTime = null; + String startTime = null; + String detectionStateInfo = null; + String performaceData = null; + String currentTimes = null; + for(String[] temp : rltArrayList){ + //监测节点id +// seqId = getIndexContent(fields,"SEQ_ID",temp); +// DetecAllInfo info = new DetecAllInfo(); +// info.setSetInfoId(setId); +// info.setSeqId(seqId); +// info.setState(getIndexContent(fields,"DETECTIONED_STATE",temp) == null ? null : Integer.parseInt(getIndexContent(fields,"DETECTIONED_STATE",temp))); +// info.setCheckTime(getIndexContent(fields,"DATA_CHECK_TIME_DIGITAL",temp) == null ? null :Long.parseLong(temp[getIndex(fields,"DATA_CHECK_TIME_DIGITAL")])); +// info.setpLevel(getIndexContent(fields,"POLICE_LEVEL",temp) == null ? -1 : Integer.parseInt(temp[getIndex(fields,"POLICE_LEVEL")])); +// info.setUrgentLevel(getIndexContent(fields,"POLICE_EMERGENT",temp) == null ? -1 : Integer.parseInt(temp[getIndex(fields,"POLICE_EMERGENT")])); +//// info.setDetectionInfoId(temp[getIndex(fields,"DETECTION_INFO_ID")]); +// info.setStatusChangTime(getIndexContent(fields,"STATUS_CHANGE_TIME",temp)== null ? null : format.parse(temp[getIndex(fields,"STATUS_CHANGE_TIME")]).getTime()); +// info.setArriveTime(getIndexContent(fields,"DATA_ARRIVE_TIME_DIGITAL",temp) == null ? null : Long.parseLong(temp[getIndex(fields,"DATA_ARRIVE_TIME_DIGITAL")])); +// info.setDelayTime(getIndexContent(fields,"DELAY_TIME",temp) == null ? null : Integer.parseInt(temp[getIndex(fields,"DELAY_TIME")])); +// info.setNextCheckTime(getIndexContent(fields,"NEXT_CHECK_TIME",temp) == null ? null : format.parse(temp[getIndex(fields,"NEXT_CHECK_TIME")]).getTime()); +// info.setStartTime(getIndexContent(fields,"START_TIME",temp) == null ? null :format.parse(temp[getIndex(fields,"START_TIME")]).getTime()); +// info.setDsinfo(getIndexContent(fields,"DETECTION_STATE_INFO",temp)); +// info.setPdata(getIndexContent(fields,"PERFORMACE_DATA",temp)); +// info.setCurrentTimes(getIndexContent(fields,"CURRENT_TIMES",temp) == null ? -1 : Integer.parseInt(temp[getIndex(fields,"CURRENT_TIMES")])); + + seqId = getIndexContent(fields,"SEQ_ID",temp); + state = getIndexContent(fields,"DETECTIONED_STATE",temp); + checkTime = getIndexContent(fields,"DATA_CHECK_TIME_DIGITAL",temp); + policeLevel = getIndexContent(fields,"POLICE_LEVEL",temp); + policeEmergent = getIndexContent(fields,"POLICE_EMERGENT",temp); + detectionInfoId = getIndexContent(fields,"DETECTION_INFO_ID",temp); + statusChangeTime = getIndexContent(fields,"STATUS_CHANGE_TIME",temp); + dataArriveTimeDigital = getIndexContent(fields,"DATA_ARRIVE_TIME_DIGITAL",temp); + delayTime = getIndexContent(fields,"DELAY_TIME",temp); + nextCheckTime = getIndexContent(fields,"NEXT_CHECK_TIME",temp); + startTime = getIndexContent(fields,"START_TIME",temp); + detectionStateInfo = getIndexContent(fields,"DETECTION_STATE_INFO",temp); + performaceData = getIndexContent(fields,"PERFORMACE_DATA",temp); + currentTimes = getIndexContent(fields,"CURRENT_TIMES",temp); + Object[] obj = new Object[33]; + + obj[DetectInfo.SETINFOID] = (setId); + obj[DetectInfo.SEQID] = (seqId); + obj[DetectInfo.STATE] = (state == null ? null : Integer.parseInt(state)); + obj[DetectInfo.CHECKTIME] = ( checkTime== null ? null :Long.parseLong(checkTime)); + obj[DetectInfo.PLEVEL] = (policeLevel == null ? -1 : Integer.parseInt(policeLevel)); + obj[DetectInfo.URGENTLEVEL] = (policeEmergent == null ? -1 : Integer.parseInt(policeEmergent)); + obj[DetectInfo.DETECTIONINFOID] = (detectionInfoId); + obj[DetectInfo.STATUSCHANGTIME] = (statusChangeTime== null ? null : format.parse(statusChangeTime).getTime()); + obj[DetectInfo.ARRIVETIME] = (dataArriveTimeDigital == null ? null : Long.parseLong(dataArriveTimeDigital)); + obj[DetectInfo.DELAYTIME] = (delayTime == null ? null : Integer.parseInt(delayTime)); + obj[DetectInfo.NEXTCHECKTIME] = (nextCheckTime == null ? null : format.parse(nextCheckTime).getTime()); + obj[DetectInfo.STARTTIME] = (startTime == null ? null :format.parse(startTime).getTime()); + obj[DetectInfo.DSINFO] = (detectionStateInfo); + obj[DetectInfo.PDATA] = (performaceData); + obj[DetectInfo.CURRENTTIMES] = (currentTimes == null ? -1 : Integer.parseInt(currentTimes)); + + seqIdList.add(seqId); + rltList.add(obj); + } + } + + }catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + logger.debug("查询new表对应监测设置的最新周期数据,进行状态比较,new 表共:"+rltList.size()+"条"); + } + + return rltList; + } + + public String getIndexContent(List fields,String fieldName,String[] temp){ + String indexContent = null; + for(int i=0;i fields,String fieldName,List temp){ + Object[] indexObj = null; + try + { + int fieldsNum = fields.size(); + if(fieldsNum==temp.size()){ + for(int i=0;i> getAllPortName(){ + //结果 + Map> result = new HashMap>(); + try { + ArrayList fields = new ArrayList(); + fields.add("seqid"); + fields.add("name_flag"); + + //查询dc监控下的节点seq_id + String querySeqidSql="select distinct t.seq_id from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' "+getIpnSelect() + " and ngt.is_valid ='1' and t.node_type='0' "; + //查询detection_info_new数据 + StringBuffer sql = new StringBuffer(); + sql.append(" select t.seqid, t.name_flag from node_figure_info t "); + sql.append(" where (t.port_set_state = 1 or t.port_set_state is null) "); + sql.append(" and seqid in (" ); + sql.append(querySeqidSql); + sql.append(" ) and t.name_flag is not null and t.is_leaf = 1 "); + + ArrayList> mapList = dao.dbSelectByFetchSize(sql.toString(),fields); + //整理查询数据 + if(mapList != null && mapList.size() > 0){ + String nameFlag = null; + String seqId = null; + for(Map temp : mapList){ + //端口名称 + nameFlag = temp.get("name_flag"); + //监测节点id + seqId = temp.get("seqid"); + + if(result.get(seqId) == null){//首次放入 + List portNameList = new ArrayList(); + portNameList.add(nameFlag); + result.put(seqId, portNameList); + }else{ + result.get(seqId).add(nameFlag); + } + nameFlag = null; + seqId = null; + } + } + + } catch (Exception e) { + logger.error("Getting node network port name failed",e); + }finally{ + dao.clearConn(); + } + return result; + } + + /** + * 获取 监测设置对应的联系人,包括: + * 1、viewLevel = 1 + * 2、viewLevel = 2 and 指定联系人 + * 3、viewLevel = 3 and 指定联系人 + * 4、viewLevel = 2 and 未指定联系人 + * 5、viewLevel = 3 and 未指定联系人 + * + * 不包含: + * 1、viewLevel = 4 的情况 + * @return Map> + */ + public Map> getDetecSetEmailListOf123(){ + Map> result = new HashMap>(); + StringBuilder sb = new StringBuilder(); + sb.append(" select distinct c.id, xyj1.email "); + sb.append(" from xt_yh_jbxx xyj1, "); + sb.append(" (select dsi.id, ',' || dsi.contact_user_ids || ',' as ids "); + sb.append(" from detection_set_info dsi "); + sb.append(" where dsi.contact_user_ids is not null) c "); + sb.append(" where c.ids like ('%,' || xyj1.yhid || ',%') "); + sb.append(" and xyj1.email is not null "); + sb.append(" union "); + sb.append(" (select distinct dsi.id,xyj.email "); + sb.append(" from detection_set_info dsi "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on dsi.create_usergroup_id = xjj.jsbh "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join xt_yh_jbxx xyj "); + sb.append(" on xyj.yhid = xyji.yhid "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + sb.append(" and dsi.view_level = 2 "); + sb.append(" and xyj.email is not null "); + sb.append(" and dsi.create_usergroup_id is not null "); + sb.append(" and dsi.contact_user_ids is null) "); + sb.append(" union "); + sb.append(" ( "); + sb.append(" select distinct dsi.id,xyj.email from detection_set_info dsi "); + sb.append(" left join gorup_system_table gst "); + sb.append(" on gst.system_id = dsi.system_id "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on gst.user_group_id = xjj.jsbh "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join xt_yh_jbxx xyj "); + sb.append(" on xyji.yhid = xyj.yhid "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + sb.append(" and dsi.view_level = 3 "); + sb.append(" and xyj.email is not null "); + sb.append(" and dsi.contact_user_ids is null "); + sb.append(" ) "); + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("email"); + try { + List> map1 = (List>)dao.dbSelect(sb.toString(), fields); + if(map1 != null && map1.size()>0){ + List tempList = null; + String email = null; + String id = null; + for(Map temp : map1){ + id = temp.get("id"); + email = temp.get("email"); + tempList = result.get(id); + if(tempList == null){//第一次添加 + List emailList = new ArrayList(); + emailList.add(email); + result.put(id, emailList); + }else{ + tempList.add(email); + } + id = null; + email = null; + tempList = null; + } + } + } catch (Exception e) { + logger.debug("查询邮件列表失败",e); + }finally{ + dao.clearConn(); + } + return result; + } + + /** + * 获取viewLevel = 4 的邮件地址 + * @return + */ + public Map> getDetecEmailListOf4(){ + Map> result = new HashMap>(); + try { + //查询dc监控下的节点seq_id + String querySeqidSql="select distinct t.seq_id from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' and t.node_type='0' "+" "+getIpnSelect(); + + StringBuilder sb = new StringBuilder(); + sb.append(" select distinct nt.seq_id ,xyj.email"); + sb.append(" from xt_yh_jbxx xyj "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xyji.yhid = xyj.yhid "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join gorup_system_table gst "); + sb.append(" on gst.user_group_id = xjj.jsbh "); + sb.append(" left join system_table st "); + sb.append(" on st.system_id = gst.system_id "); + sb.append(" left join node_table nt "); + sb.append(" on nt.system_id = gst.system_id "); + sb.append(" left join nodegroup_table ngt "); + sb.append(" on ngt.group_id = nt.node_group_id "); + sb.append(" left join ( "+querySeqidSql+" ) ns "); + sb.append(" on nt.seq_id = ns.seq_id "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and nt.node_state = 0 "); + sb.append(" and ngt.is_valid = 1 "); + sb.append(" and st.system_state = 0 "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + + ArrayList fields = new ArrayList(); + fields.add("seq_id"); + fields.add("email"); + + List> map1 = (List>)dao.dbSelect(sb.toString(), fields); + if(map1 != null && map1.size()>0){ + List tempList = null; + String email = null; + String seqId = null; + for(Map temp : map1){ + seqId = temp.get("seq_id"); + email = temp.get("email"); + tempList = result.get(seqId); + if(tempList == null){//第一次添加 + List emailList = new ArrayList(); + emailList.add(email); + result.put(seqId, emailList); + }else{ + tempList.add(email); + } + seqId = null; + email = null; + tempList = null; + } + } + + } catch (Exception e) { + logger.debug("查询邮件列表失败",e); + }finally{ + dao.clearConn(); + } + return result; + } + + + + + //获得 执行完成但无结果 的节点和多个任务id的map + public Map> getNonRltIpTaskMap(){ + Map> ipTaskMap = new HashMap>(); + try { + //再次收集结果的间隔时间:5+3*3+2*5+10=34分钟 + int timeStandard = Constants.MISSION_CHECK_NEW_PERIOD//DC新任务检查周期 + +Constants.MISSION_RELEASE_PERIOD*Constants.MISSION_RELEASE_TIMES//任务下发间隔周期*尝试次数 + +2*Constants.DATA_COLLECT_PERIOD//2*收集周期 + +Constants.TASK_RLT_COLLECT_RESERVED_PERIOD;//单位:秒 + //任务类型1的map-测试ok + ipTaskMap = addMaps(ipTaskMap,getNonRltIpTaskOneMap(timeStandard)); + + //任务类型4的map(非周期) + ipTaskMap = addMaps(ipTaskMap,getNonRltIpTaskFourNoLoopMap(timeStandard)); + + //任务类型4的map(周期) + ipTaskMap = addMaps(ipTaskMap,getNonRltIpTaskFourLoopMap(timeStandard)); + + //任务类型6的map + ipTaskMap = addMaps(ipTaskMap,getNonRltIpTaskSixMap(timeStandard)); + + logger.info("需要收集结果的任务相关信息"); + Iterator iteAll = ipTaskMap.entrySet().iterator(); + while(iteAll.hasNext()) { + Entry en = (Entry)iteAll.next(); + String ip = en.getKey().toString(); + Map allTaskInfo = (Map)en.getValue();//当前ip上执行成功的任务id + Iterator iteTackAll = allTaskInfo.entrySet().iterator(); + logger.info("ip="+ip); + while(iteTackAll.hasNext()) { + Entry enTask = (Entry)iteTackAll.next(); + String taskId = enTask.getKey().toString(); + String[] tInfo = (String[])enTask.getValue(); + logger.info("taskId="+taskId); + logger.info("taskOtherInfo="+ArrayUtils.toString(tInfo)); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + + return ipTaskMap; + } + + //将两个map合并(将key相同的value合并) + private Map> addMaps(Map> oldMap,Map> newMap) throws Exception { + Iterator ite = newMap.entrySet().iterator(); + while(ite.hasNext()) { + Entry en = (Entry)ite.next(); + String ip = en.getKey().toString(); + Map taskInfoMaps = (Map)en.getValue();//当前ip上执行成功的任务信息 + if(oldMap.containsKey(ip)) { + Map oldTaskInfoMaps = oldMap.get(ip); + oldTaskInfoMaps.putAll(taskInfoMaps); + }else { + oldMap.put(ip, taskInfoMaps); + } + } + return oldMap; + } + + //任务类型一:需要重新收集任务结果的ip:taskIds的map + private Map> getNonRltIpTaskOneMap(int timeStandard) { + String task1AllSql = null; + if(Constants.IS_MYSQL){ + task1AllSql = "select distinct t.mission_id,para.node_groups_id,para.node_ips_id ," + + " 1 missionType,t.is_loop,date_format(t.create_time,'%Y-%m-%d %H:%i:%s') startTime," + + "date_format(ifnull(t.end_time,''),'%Y-%m-%d %H:%i:%s') endTime " + + " from mission_state_table t" + + " left join mission_parameter_table1 para" + + " on para.mission_id=t.mission_id" + + " where t.mission_type = 1" + + " and t.create_time < date_add(now() ,interval -"+timeStandard+ "*24*3600 second)" +" order by t.mission_id desc";//timeStandard是大概估算的,所以不会那么准确 + }else{ + task1AllSql = "select distinct t.mission_id,para.node_groups_id,para.node_ips_id ," + + " 1 missionType,t.is_loop,to_char(t.create_time,'" + Constants.DB_DATE_FORMAT + "') startTime," + + "to_char(nvl(t.end_time,''),'" + Constants.DB_DATE_FORMAT + "') endTime " + + " from mission_state_table t" + + " left join mission_parameter_table1 para" + + " on para.mission_id=t.mission_id" + + " where t.mission_type = 1" + + " and (sysdate-t.create_time)*24*3600>"+timeStandard+" order by t.mission_id desc";//timeStandard是大概估算的,所以不会那么准确 + + } + String task1SucSql = "select info.ip,info.mission_id as taskIds" + + " from (" + + " select distinct node.node_ip as ip,rlt.mission_id as mission_id" + + " from mission_result_table1 rlt" + + " left join node_table node" + + " on node.seq_id=rlt.seq_id " + + " where rlt.result in (1,0,-1)" + + " and rlt.mission_id in( " + + " select distinct t.mission_id " + + " from mission_state_table t " + + " where t.mission_type = 1" + +// " and (sysdate-t.create_time)*24*3600>" +timeStandard+ + " and t.create_time< sysdate- "+timeStandard+"/24/3600 "+ + " )" + + " )info " ; + // " group by info.ip"; + return getNonRltIpTaskMap(task1AllSql,task1SucSql); + } + + //任务类型四(非周期):需要重新收集任务结果的ip:taskIds的map + private Map> getNonRltIpTaskFourNoLoopMap(int timeStandard) { + String task4NoLoopAllSql = null; + if(Constants.IS_MYSQL){//date_format(ifnull(t.end_time,''),'%Y-%m-%d %H:%i:%s') + task4NoLoopAllSql = "select distinct t.mission_id,para.node_groups_id,para.node_ips_id, " + + " 4 missionType,t.is_loop,to_char(t.create_time,'" + Constants.DB_DATE_FORMAT + "') startTime," + + "date_format(ifnull(t.end_time,''),'%Y-%m-%d %H:%i:%s') endTime " + + " from mission_state_table t" + + " left join mission_parameter_table4 para" + + " on para.mission_id=t.mission_id" + + " where t.mission_type = 4" + + " and t.is_loop!=1" + + " and t.create_time < date_add(now() ,interval -"+timeStandard+ "*24*3600 second)"; + }else{ + task4NoLoopAllSql = "select distinct t.mission_id,para.node_groups_id,para.node_ips_id, " + + " 4 missionType,t.is_loop,to_char(t.create_time,'" + Constants.DB_DATE_FORMAT + "') startTime," + + "to_char(nvl(t.end_time,''),'" + Constants.DB_DATE_FORMAT + "') endTime " + + " from mission_state_table t" + + " left join mission_parameter_table4 para" + + " on para.mission_id=t.mission_id" + + " where t.mission_type = 4" + + " and t.is_loop!=1" + + " and (sysdate-t.create_time)*24*3600>"+timeStandard; + } + +// System.out.println(task4NoLoopAllSql); + String task4NoLoopSucSql = "select info.ip,info.mission_id as taskIds" + + " from (" + + " select distinct node.node_ip as ip,rlt.mission_id as mission_id" + + " from mission_result_table4 rlt" + + " left join node_table node" + + " on node.seq_id=rlt.seq_id " + + " where rlt.result in (1,0,-1)" + + " and rlt.mission_id in( " + + " select distinct t.mission_id " + + " from mission_state_table t " + + " where t.mission_type = 4" + + " and t.is_loop!=1" + +// " and (sysdate-t.create_time)*24*3600>" + timeStandard + + " and t.create_time< sysdate- "+timeStandard+"/24/3600 "+ + + " )" + + " )info " ; + // " group by info.ip"; +// System.out.println(task4NoLoopSucSql); + return getNonRltIpTaskMap(task4NoLoopAllSql,task4NoLoopSucSql); + } + + //任务类型四(周期):需要重新收集任务结果的ip:taskIds的map + private Map> getNonRltIpTaskFourLoopMap(int timeStandard) { + String task4LoopAllSql = "select distinct t.mission_id as mission_id,para.node_groups_id,para.node_ips_id," + + " 4 missionType,t.is_loop,to_char(loopMis.Start_Time,'" + Constants.DB_DATE_FORMAT + "') startTime,to_char(loopMis.End_Time,'" + Constants.DB_DATE_FORMAT + "') endTime" + + " from mission_state_table t" + + " left join mission_parameter_table4 para" + + " on para.mission_id=t.mission_id" + + " left join loopmission_state_table loopMis" + + " on loopMis.Mission_Id=t.mission_id" + + " where t.mission_type = 4" + + " and t.is_loop=1" + + " and loopMis.Mission_State=2" + +// " and (sysdate-loopMis.Start_Time)*24*3600>"+timeStandard; + " and loopMis.Start_Time< sysdate- "+timeStandard+"/24/3600 "; + + String task4LoopSucSql = "select info.ip,info.mission_id as taskIds" + + " from (" + + " select distinct node.node_ip as ip,rlt.mission_id as mission_id" + + " from mission_result_table4 rlt" + + " left join node_table node" + + " on node.seq_id=rlt.seq_id " + + " where rlt.result in (1,0,-1)" + + " and rlt.mission_id in(" + + " select distinct t.mission_id as mission_id" + + " from mission_state_table t " + + " left join loopmission_state_table loopMis" + + " on loopMis.Mission_Id=t.mission_id" + + " where t.mission_type = 4" + + " and t.is_loop=1" + + " and loopMis.Mission_State=2" + +// " and (sysdate-loopMis.Start_Time)*24*3600>" + timeStandard + + " and loopMis.Start_Time< sysdate-"+timeStandard+"/24/3600 " + + " )" + + " )info " ; + // " group by info.ip"; + return getNonRltIpTaskMap(task4LoopAllSql,task4LoopSucSql); + } + + //任务类型六:需要重新收集任务结果的ip:taskIds的map + private Map> getNonRltIpTaskSixMap(int timeStandard) { + String task6AllSql = null; + String task6SucSql = null; + if(Constants.IS_MYSQL){ + task6AllSql = "select distinct t.mission_id,para.node_groups_id,para.node_ips_id, " + + "6 missionType,t.is_loop," + + "date_format(greatest(t.create_time, ifnull(para.start_time, t.create_time)),'%Y-%m-%d %H:%i:%s') startTime," + + "date_format(ifnull(t.end_time,''),'%Y-%m-%d %H:%i:%s') endTime" + + " from mission_state_table t" + + " left join mission_parameter_table6 para" + + " on para.mission_id=t.mission_id" + + " where t.mission_type = 6" + + " and greatest(t.create_time,ifnull(para.start_time,t.create_time))>date_add(now() ,interval -"+timeStandard+ "*24*3600 second)"; + task6SucSql = "select info.ip,info.mission_id as taskIds" + + " from (" + + " select distinct node.node_ip as ip,rlt.mission_id as mission_id" + + " from mission_result_table6 rlt" + + " left join node_table node" + + " on node.seq_id=rlt.seq_id " + + " where rlt.result in (1,0,-1)" + + " and rlt.mission_id in( " + + " select distinct t.mission_id " + + " from mission_state_table t " + + " left join mission_parameter_table6 para" + + " on para.mission_id=t.mission_id" + + " where t.mission_type = 6" + + /* " and (sysdate-greatest(t.create_time,nvl(para.start_time,t.create_time)))*24*3600>" + timeStandard + + " )" +*/ + " and greatest(t.create_time,ifnull(para.start_time,t.create_time))>date_add(now() ,interval -"+timeStandard+ "*24*3600 second))"+ + " )info " ; + }else{ + task6AllSql = "select distinct t.mission_id,para.node_groups_id,para.node_ips_id, " + + "6 missionType,t.is_loop,to_char(greatest(t.create_time, nvl(para.start_time, t.create_time)),'" + Constants.DB_DATE_FORMAT + "') startTime," + + "to_char(nvl(t.end_time,''),'" + Constants.DB_DATE_FORMAT + "') endTime" + + " from mission_state_table t" + + " left join mission_parameter_table6 para" + + " on para.mission_id=t.mission_id" + + " where t.mission_type = 6" + + " and (sysdate-greatest(t.create_time,nvl(para.start_time,t.create_time)))*24*3600>"+timeStandard; + task6SucSql = "select info.ip,info.mission_id as taskIds" + + " from (" + + " select distinct node.node_ip as ip,rlt.mission_id as mission_id" + + " from mission_result_table6 rlt" + + " left join node_table node" + + " on node.seq_id=rlt.seq_id " + + " where rlt.result in (1,0,-1)" + + " and rlt.mission_id in( " + + " select distinct t.mission_id " + + " from mission_state_table t " + + " left join mission_parameter_table6 para" + + " on para.mission_id=t.mission_id" + + " where t.mission_type = 6" + + " and (sysdate-greatest(t.create_time,nvl(para.start_time,t.create_time)))*24*3600>" + timeStandard + + " )" + + " )info " ; + } + + return getNonRltIpTaskMap(task6AllSql,task6SucSql); + } + + //获得 执行完成但无结果 的节点和多个任务id的map:1.1.1.1:,123,234,235, + private Map> getNonRltIpTaskMap(String taskAllSql,String taskSucSql) { + Map> ipTaskAllMap = new HashMap>(); + Map ipTaskSucMap = new HashMap(); + Map> ipTaskFailMap = new HashMap>(); + try { + ipTaskAllMap = getNonRltIpTaskAllMap(taskAllSql);//ip(当前DC管理的)和每个ip上对应的任务id(按时间算该执行完毕的) + ipTaskSucMap = getNonRltIpTaskSucMap(taskSucSql);//ip(当前DC管理的)和每个ip上对应的任务id(执行成功的) + //打印所有的该有结果的任务的相关信息 +// System.out.println("所有的"); +// Iterator iteAll = ipTaskAllMap.entrySet().iterator(); +// while(iteAll.hasNext()) { +// Entry en = (Entry)iteAll.next(); +// String ip = en.getKey().toString(); +// Map allTaskInfo = (Map)en.getValue();//当前ip上执行成功的任务id +// Iterator iteTackAll = allTaskInfo.entrySet().iterator(); +// System.out.println("ip="+ip); +// while(iteTackAll.hasNext()) { +// Entry enTask = (Entry)iteTackAll.next(); +// String taskId = enTask.getKey().toString(); +// String[] tInfo = (String[])enTask.getValue(); +// System.out.println("taskId="+taskId); +// System.out.println("taskOtherInfo="+ArrayUtils.toString(tInfo)); +// } +// } + + //已有结果的任务 +// System.out.println("成功的"); +// Iterator iteSucc = ipTaskSucMap.entrySet().iterator(); +// while(iteSucc.hasNext()) { +// Entry en = (Entry)iteSucc.next(); +// String ip = en.getKey().toString(); +// String failTaskIds = en.getValue().toString();//当前ip上执行成功的任务id +// System.out.println("key="+ip+",value="+failTaskIds); +// } + + Iterator iteSuc = ipTaskSucMap.entrySet().iterator(); + while(iteSuc.hasNext()) { + Entry en = (Entry)iteSuc.next(); + if(en!=null && en.getKey()!=null) { + String ip = en.getKey().toString(); + String sucTaskIds = en.getValue().toString();//当前ip上执行成功的任务id +// System.out.println("成功的:key="+ip+",value="+sucTaskIds); + Map taskInfoMaps = ipTaskAllMap.get(en.getKey());//当前ip上应有结果的任务id + if(taskInfoMaps!=null&&taskInfoMaps.size()>0) { + String[] sucTaskIdAry = sucTaskIds.split(","); + for(String sucTaskId:sucTaskIdAry) {//从总任务信息中,去掉有任务结果的信息 + taskInfoMaps.remove(sucTaskId);//总的-执行成功的=失败的 + } + if(taskInfoMaps.size()==0) {//如果所有减去成功的只剩下一个逗号,说明,当前ip上的任务都执行成功了 + ipTaskAllMap.remove(ip); + } +// else { +// ipTaskAllMap.put(ip, taskIds); +// } + } + } + } + + ipTaskFailMap = ipTaskAllMap; + +// System.out.println("失败的"); +// Iterator iteAllFail = ipTaskFailMap.entrySet().iterator(); +// while(iteAllFail.hasNext()) { +// Entry en = (Entry)iteAllFail.next(); +// String ip = en.getKey().toString(); +// Map allTaskInfo = (Map)en.getValue();//当前ip上执行成功的任务id +// Iterator iteTackAll = allTaskInfo.entrySet().iterator(); +// System.out.println("ip="+ip); +// while(iteTackAll.hasNext()) { +// Entry enTask = (Entry)iteTackAll.next(); +// String taskId = enTask.getKey().toString(); +// String[] tInfo = (String[])enTask.getValue(); +// System.out.println("taskId="+taskId); +// System.out.println("taskOtherInfo="+ArrayUtils.toString(tInfo)); +// } +// } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + + return ipTaskFailMap; + } + + //返回当前DC管理的ip和每个ip上对应的任务id(按时间算该执行完毕的) + private Map> getNonRltIpTaskAllMap(String task1AllSql) { + Map> ipTaskAllMap = new HashMap>(); + try { + //任务类型1的map + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("node_groups_id"); + fields.add("node_ips_id"); + fields.add("missionType"); + fields.add("is_loop"); + fields.add("startTime"); + fields.add("endTime"); + ArrayList> mapList = dao.dbSelect(task1AllSql,fields); + Iterator> ite = mapList.iterator(); + List nmList = this.getAllNodeModel();//node_table获取的当前dc管控的所有节点的node信息 + Map> allGroupIDs = this.getAllGroupIdAndParentGroupID();//获取所有子节点组id对应的父节点组id + while (ite.hasNext()) { + Map map = (Map) ite.next(); + String node_ips_id = map.get("node_ips_id"); + String taskId = map.get("mission_id"); + String[] taskInfoTmp = new String[4]; + taskInfoTmp[0] = map.get("missionType"); + taskInfoTmp[1] = map.get("is_loop"); + taskInfoTmp[2] = map.get("startTime"); + taskInfoTmp[3] = map.get("endTime"); + //如果没有选择节点,则根据节点组id查询所有的节点 + List nodeList = new ArrayList(); + if(node_ips_id==null || "".equals(node_ips_id)) { + String node_groups_id = map.get("node_groups_id"); + if(node_groups_id != null && !"".equals(node_groups_id)) { + //1、将字符串型nodeGroupid 转换为 long 并去重 + String[] tempIds = node_groups_id.split(","); + Set tempLongIds = new HashSet(); + for(String temp : tempIds){ + try { + tempLongIds.add(Long.parseLong(temp)); + } catch (Exception e) { + logger.debug("",e); + } + } + //2、获取所有节点组id + Set groupIds = null; + if(tempLongIds.size() > 0 && allGroupIDs != null && allGroupIDs.size() > 0){ + groupIds = this.getNodeGroupId(tempLongIds, allGroupIDs); + } + //3、获取节点 + if(groupIds != null && groupIds.size() > 0){ + nodeList = this.getNodeModelByGroupIds(groupIds, nmList); + } + } + }else {//如果选择了节点,则只根据节点来,不管节点组 + //1、将字符串型nodeid 转换为 long 并去重 + String[] tempIds = node_ips_id.split(","); + Set tempLongIds = new HashSet(); + for(String temp : tempIds){ + try { + tempLongIds.add(Long.parseLong(temp)); + } catch (Exception e) { + logger.debug("",e); + } + } + //2、从缓存中查找 指定nodeid的信息 + if(tempLongIds.size() > 0){ + if(nmList != null && nmList.size() > 0){ + for(NodeModel tempModel : nmList){ + if(tempLongIds.contains(tempModel.getNodeId())){ + nodeList.add(tempModel); + } + } + } + } + } + for(NodeModel node:nodeList) { + String ip = node.getNodeIp(); + if(ipTaskAllMap.containsKey(ip)) { + Map taskInfoMapTmp = ipTaskAllMap.get(ip); + if(taskInfoMapTmp.get(taskId)==null) { + taskInfoMapTmp.put(taskId, taskInfoTmp); + } + }else if(StringUtils.isNotBlank(ip)){ + Map taskInfoMap = new HashMap(); + taskInfoMap.put(taskId, taskInfoTmp); + ipTaskAllMap.put(ip,taskInfoMap); + } + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return ipTaskAllMap; + } + + + //返回当前DC管理的ip和每个ip上对应的任务id(执行成功的) + private Map getNonRltIpTaskSucMap(String task1SucSql) { + Map ipTaskSucMap = new HashMap(); + try { + //任务类型1的map + ArrayList fields = new ArrayList(); + fields.add("ip"); + fields.add("taskIds"); + + ArrayList> mapList = dao.dbSelect(task1SucSql,fields); + Iterator> ite = mapList.iterator(); + + while (ite.hasNext()) { + Map map = (Map) ite.next(); + String ip = map.get("ip"); + String taskId = map.get("taskIds"); + if(StringUtils.isNotBlank(ip)&&StringUtils.isNotBlank(taskId)) { + if(StringUtils.isBlank(ipTaskSucMap.get(ip))){ + ipTaskSucMap.put(ip, ","+taskId.trim()+","); + }else{ + String val = ipTaskSucMap.get(ip); + if(!val.contains(","+taskId+",")){ + val = val + taskId+","; + ipTaskSucMap.put(ip, val); + } + } + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + + return ipTaskSucMap; + } + + /** + * 查询任务下发节点的IPN信息, + * 条件1 节点状态有效 值0 + * 条件2 节点类型为服务器 值0 + * @time Dec 23, 2011-2:20:22 PM + * @param ids 节点组ids [id1,id2..id3] + * @return + */ +// public List getNodeIPNForTaskByNodeIds(String ids) { +// List nmList = new LinkedList(); +// if(StringUtils.isBlank(ids)){ +// return nmList; +// } +// +// try { +// // SetInfo信息查询 +// ArrayList fields = new ArrayList(); +// fields.add("ipn"); +// +// String sql="select t.ipn from node_table t where t.node_id in ("+(StringUtils.isNotBlank(ids)?ids:0)+") and t.node_state='0' and t.node_type ='0' "; +// +// ArrayList> mapList = dao.dbSelect(sql,fields); +// Iterator> ite = mapList.iterator(); +// while (ite.hasNext()) { +// Map map = (Map) ite.next(); +// if(StringUtils.isNotEmpty(map.get("ipn"))){ +// nmList.add(Long.parseLong(map.get("ipn").trim())); +// } +// } +// } catch (Exception e) { +// logger.error("",e); +// }finally{ +// dao.clearConn(); +// } +// return nmList; +// } + + /** + * 查询任务下发节点的IPN信息, + * 条件1 节点状态有效 值0 + * 条件2 节点组状态有效 值1 + * 条件3 节点类型为服务器 值0 + * @time Dec 23, 2011-2:20:22 PM + * @param gidsStr 节点组ids [gid1,gid2..gid3] + * @return + */ +// public List getNodeIPNForTaskByGroupsIds(String gidsStr) { +// List nmList = new LinkedList(); +// if(StringUtils.isBlank(gidsStr)){ +// return nmList; +// } +// +// try { +// // SetInfo信息查询 +// ArrayList fields = new ArrayList(); +// fields.add("ipn"); +// +// String sql="select t.ipn from node_table t left join nodegroup_table ngt on t.group_id=ngt.group_id where t.group_id in ("+(StringUtils.isNotBlank(gidsStr)?gidsStr:0)+") and t.node_state='0' and t.node_type='0' and ngt.is_valid = '1' "; +// ArrayList> mapList = dao.dbSelect(sql,fields); +// Iterator> ite = mapList.iterator(); +// while (ite.hasNext()) { +// Map map = (Map) ite.next(); +// if(StringUtils.isNotEmpty(map.get("ipn"))){ +// nmList.add(Long.parseLong(map.get("ipn").trim())); +// } +// } +// } catch (Exception e) { +// logger.error("",e); +// }finally{ +// dao.clearConn(); +// } +// return nmList; +// } + + /** + * 根据节点组ID 获取有效节点信息 + * @time Dec 23, 2011-2:20:22 PM + * @return + */ + public List getNodeInfoByGroupsIdsWithoutManageSegment(String gids) { + List nmList = null; + if(StringUtils.isEmpty(gids)){ + return nmList; + } + try { + String gidsStr = null; + if(StringUtils.isNotEmpty(gids)){ +// gidsStr = "and t.node_group_id in( select ngt.group_id from NODEGROUP_TABLE ngt where ngt.leaf_group = 1 and ngt.is_valid = '1' start with ngt.group_id in ("+gids+") connect by prior ngt.group_id = ngt.parent_group_id)"; + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 and ngt.is_valid = '1' ", " nt.group_id in ("+gids+") ", "group_id", "parent_group_id"); + gidsStr = "and t.node_group_id in( "+startWithConnectBy+" )"; + } + + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_id"); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); + fields.add("node_name"); + fields.add("node_state"); + fields.add("system_id"); + fields.add("node_group_id"); + + + String sql="select t.node_id,t.node_ip, t.seq_id, t.node_type, t.node_name, t.node_state, t.system_id, t.node_group_id from node_table t where t.node_state='0' "+(StringUtils.isEmpty(gidsStr)?"":gidsStr); + nmList = new ArrayList(); + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); + node.setNodeId(StringUtils.isEmpty(map.get("node_id"))?null:Long.valueOf(map.get("node_id"))); + node.setNodeIp(map.get("node_ip")); +// node.setNodeName(map.get("node_name")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + node.setNodeState(StringUtils.isEmpty(map.get("node_state"))?null:Long.valueOf(map.get("node_state"))); + node.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); + node.setGroupId(StringUtils.isEmpty(map.get("node_group_id"))?null:Long.valueOf(map.get("node_group_id"))); + nmList.add(node); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + + /** + * 根据节点ids 获取有效节点信息 + * @time Dec 23, 2011-2:20:22 PM + * @return + */ + public List getNodeModelByNodeIdsWithoutManageSegment(String ids) { + List nmList = new LinkedList(); + if(StringUtils.isBlank(ids)){ + return nmList; + } + + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_id"); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); + fields.add("node_name"); + fields.add("node_state"); + fields.add("system_id"); + fields.add("node_group_id"); + + String sql="select t.node_id,t.node_ip, t.seq_id, t.node_type, t.node_name, t.node_state, t.system_id, t.node_group_id from node_table t where t.node_id in ("+(StringUtils.isNotBlank(ids)?ids:0)+") and t.node_state='0' "; + + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); + node.setNodeId(StringUtils.isEmpty(map.get("node_id"))?null:Long.valueOf(map.get("node_id"))); + node.setNodeIp(map.get("node_ip")); +// node.setNodeName(map.get("node_name")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + node.setNodeState(StringUtils.isEmpty(map.get("node_state"))?null:Long.valueOf(map.get("node_state"))); + node.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); + node.setGroupId(StringUtils.isEmpty(map.get("node_group_id"))?null:Long.valueOf(map.get("node_group_id"))); + nmList.add(node); +// break; + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + /** + * 根据节点IP 获取节点信息 + * @time Dec 23, 2011-2:20:22 PM + * @return + */ + public List getNodeModelByIds(String ids) { + List nmList = new LinkedList(); + if(StringUtils.isBlank(ids)){ + return nmList; + } + + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_id"); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); + fields.add("node_name"); + fields.add("node_state"); + fields.add("system_id"); + fields.add("node_group_id"); + + String sql="select t.node_id,t.node_ip, t.seq_id, t.node_type, t.node_name, t.node_state, t.system_id, t.node_group_id from node_table t where t.node_id in ("+(StringUtils.isNotBlank(ids)?ids:0)+") and t.node_state='0' "+" "+getIpnSelect(); + + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); + node.setNodeId(StringUtils.isEmpty(map.get("node_id"))?null:Long.valueOf(map.get("node_id"))); + node.setNodeIp(map.get("node_ip")); +// node.setNodeName(map.get("node_name")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + node.setNodeState(StringUtils.isEmpty(map.get("node_state"))?null:Long.valueOf(map.get("node_state"))); + node.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); + node.setGroupId(StringUtils.isEmpty(map.get("node_group_id"))?null:Long.valueOf(map.get("node_group_id"))); + nmList.add(node); +// break; + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + + /** + * 根据节点id 获取节点信息 + * @time 2013-1-30 hyx + * @return + */ + public List getNodesByIds(String ids) { + List nmList = new LinkedList(); + if(StringUtils.isBlank(ids)){ + return nmList; + } + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); + + String sql="select distinct t.node_ip, t.seq_id, t.node_type " + + " from node_table t " + + " where t.node_id in ("+(StringUtils.isNotBlank(ids)?ids:0)+") and t.node_state='0' "+" "+getIpnSelect(); + + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); + node.setNodeIp(map.get("node_ip")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + nmList.add(node); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + + public Map getIpAndNewNmsclientInfo(SetInfo setInfo) { + Map ipAndInfoMap = new HashMap(); + if(setInfo==null){ + return ipAndInfoMap; + } + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + fields.add("detection_state_info"); + + String sql="select nt.node_ip,t.detection_state_info " + + " from detection_info_new t left join node_table nt on nt.seq_id=t.seq_id " + + " where t.detection_set_info_id="+setInfo.getId()+getIpnSelect(); + + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + ipAndInfoMap.put(map.get("node_ip"),map.get("detection_state_info")); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return ipAndInfoMap; + } + + + public Long getNodeTypeByIp(String ip) { + Long nodeType = null; + String sql = "select distinct nt.node_type from node_table nt where nt.node_ip = '"+ip+"' "; + ArrayListfields = new ArrayList(); + fields.add("node_type"); + try { + ArrayList> mapsList = dao.dbSelect(sql, fields); + if(mapsList!=null && mapsList.size()>0){ + for(Map map : mapsList){ + nodeType = StringUtils.isBlank(map.get("node_type"))?null:Long.parseLong(StringUtils.trim(map.get("node_type"))); + break; + } + } + } catch (Exception e) { + logger.error("By parameter:"+ ip+" to get node type failure",e); + } + return nodeType; + } + + /** + * 获取所有节点信息 + * @return + */ + public List getAllNodeModel(){ + List nmList = null; + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_id"); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); + fields.add("node_name"); + fields.add("node_state"); + fields.add("system_id"); + fields.add("node_group_id"); + String sql="select t.node_id,t.node_ip, t.seq_id, t.node_type, t.node_name, t.node_state, t.system_id, t.node_group_id from node_table t where t.node_state='0' "+getIpnSelect(); + nmList = new ArrayList(); + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); + node.setNodeId(StringUtils.isEmpty(map.get("node_id"))?null:Long.valueOf(map.get("node_id"))); + node.setNodeIp(map.get("node_ip")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + node.setNodeState(StringUtils.isEmpty(map.get("node_state"))?null:Long.valueOf(map.get("node_state"))); + node.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); + node.setGroupId(StringUtils.isEmpty(map.get("node_group_id"))?null:Long.valueOf(map.get("node_group_id"))); + nmList.add(node); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + + /** + * 根据节点组id 获取 符合条件的(属于当前dc管理)节点信息 + * @param groupIds + * @return + */ + public List getNodeModelByGroupIds(Set groupIds,List nodeModelList){ + List result = null; + try { + result = new ArrayList(); + Long groupId = null; + for(NodeModel tempNode : nodeModelList){ + groupId = tempNode.getGroupId();//节点组id + if(groupIds.contains(groupId)){ + result.add(tempNode); + } + } + } catch (Exception e) { + logger.debug("",e); + } + return result; + } + + + /** + * 查询多个节点组 对应的所有 子节点组 + * @param groupIds + * @param allGroupIDs + * @return + */ + public Set getNodeGroupId(Set groupIds,Map> allGroupIDs){ + Set result = null; + try { + if(allGroupIDs != null && allGroupIDs.size() >0 ){ + result = new HashSet(); + Set condition = new HashSet();//查询条件 + Set tempResult = new HashSet();//查询结果 + condition.addAll(groupIds);//初始化查询条件 + result.addAll(groupIds); + while(condition.size() > 0){ + for(Map.Entry> tempGroupIds : allGroupIDs.entrySet()){ + Long parentGroupId = tempGroupIds.getKey();//父节点组id + if(condition.contains(parentGroupId)){ + tempResult.addAll(tempGroupIds.getValue());//子节点组ids + } + } + condition.clear();//清空条件 + if(tempResult.size() > 0){//如果有对应的子节点,作为查询条件,准备下一次查询 + result.addAll(tempResult); + condition.addAll(tempResult);//结果作为下一次查询条件 + tempResult.clear();//清空查询结果 + } + } + } + } catch (Exception e) { + logger.debug("",e); + } + return result; + } + + + /** + * 获取所有group_id及对应的parent_group_id + * @return Map> key为 parent_group_id + * 2016年3月21日 11:24:47 + */ + public Map> getAllGroupIdAndParentGroupID(){ + Map> result = new HashMap>(); + try { + String sql = "select ngt.group_id, ngt.parent_group_id " + + " from NODEGROUP_TABLE ngt " + + " where ngt.leaf_group = 1 " + + " and ngt.is_valid = 1 "; + ArrayList fields = new ArrayList(); + fields.add("group_id"); + fields.add("parent_group_id"); + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + String groupId = null; + String parentGroupId = null; + List tempList = null; + while (ite.hasNext()) { + Map map = (Map) ite.next(); + groupId = map.get("group_id"); + parentGroupId = map.get("parent_group_id"); + if(StringUtils.isNotBlank(groupId) &&StringUtils.isNotBlank(parentGroupId)){ + tempList = result.get(Long.parseLong(parentGroupId)); + if(tempList == null){//第一次添加 + tempList = new ArrayList(); + tempList.add(Long.parseLong(groupId)); + result.put(Long.parseLong(parentGroupId), tempList); + }else{ + tempList.add(Long.parseLong(groupId)); + } + } + groupId = null; + parentGroupId = null; + tempList = null; + } + + } catch (Exception e) { + logger.debug("获取节点组信息失败",e); + result = null; + } finally{ + dao.clearConn(); + } + return result; + } + + /** + * 根据节点组ID获取节点信息 + * @time Dec 23, 2011-2:20:22 PM + * @return + */ + public List getNodeModelByGroupIds(String gids) { + List nmList = null; + if(StringUtils.isEmpty(gids)){ + return nmList; + } + +// String sql = "select distinct nt.node_ip from node_table nt "+(StringUtils.isEmpty(gidsStr)?"":gidsStr); + + try { + + String gidsStr = null; + if(StringUtils.isNotEmpty(gids)){ +// gidsStr = "and t.node_group_id in( select ngt.group_id from NODEGROUP_TABLE ngt where ngt.leaf_group = 1 and ngt.is_valid = '1' start with ngt.group_id in ("+gids+") connect by prior ngt.group_id = ngt.parent_group_id)"; + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 and ngt.is_valid = '1' ", " nt.group_id in ("+gids+") ", "group_id", "parent_group_id"); + gidsStr = "and t.node_group_id in( "+startWithConnectBy+" )"; + } + + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_id"); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); + fields.add("node_name"); + fields.add("node_state"); + fields.add("system_id"); + fields.add("node_group_id"); + + + String sql="select t.node_id,t.node_ip, t.seq_id, t.node_type, t.node_name, t.node_state, t.system_id, t.node_group_id from node_table t where t.node_state='0' "+(StringUtils.isEmpty(gidsStr)?"":gidsStr)+" "+getIpnSelect(); + nmList = new ArrayList(); + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); + node.setNodeId(StringUtils.isEmpty(map.get("node_id"))?null:Long.valueOf(map.get("node_id"))); + node.setNodeIp(map.get("node_ip")); +// node.setNodeName(map.get("node_name")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + node.setNodeState(StringUtils.isEmpty(map.get("node_state"))?null:Long.valueOf(map.get("node_state"))); + node.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); + node.setGroupId(StringUtils.isEmpty(map.get("node_group_id"))?null:Long.valueOf(map.get("node_group_id"))); + nmList.add(node); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + /** + * 根据节点组ID获取节点信息 + * @time 2013-1-30 hyx + * @return + */ + public List getNodesByGroupIds(String gids) { + List nmList = null; + if(StringUtils.isEmpty(gids)){ + return nmList; + } + + try { + String gidsStr = null; + if(StringUtils.isNotEmpty(gids)){ +// gidsStr = "and t.node_group_id in( select ngt.group_id from NODEGROUP_TABLE ngt where ngt.leaf_group = 1 and ngt.is_valid = '1' start with ngt.group_id in ("+gids+") connect by prior ngt.group_id = ngt.parent_group_id)"; + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 and nt.is_valid = '1' ", " nt.group_id in ("+gids+") ", "group_id", "parent_group_id"); + gidsStr = "and t.node_group_id in( "+startWithConnectBy+" )"; + } + + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("node_type"); + + String sql="select distinct t.node_ip, t.seq_id, t.node_type" + + " from node_table t" + + " where t.node_state='0' "+(StringUtils.isEmpty(gidsStr)?"":gidsStr)+" "+getIpnSelect(); + nmList = new ArrayList(); + ArrayList> mapList = dao.dbSelect(sql,fields); + Iterator> ite = mapList.iterator(); + while (ite.hasNext()) { + Map map = (Map) ite.next(); + NodeModel node = new NodeModel(); + node.setNodeIp(map.get("node_ip")); + node.setSeqId(StringUtils.isEmpty(map.get("seq_id"))?null:Long.valueOf(map.get("seq_id"))); + node.setNodeType(StringUtils.isEmpty(map.get("node_type"))?null:Long.valueOf(map.get("node_type"))); + nmList.add(node); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nmList; + } + + public boolean checkUpdateNodeIsPhysicalNode(NodeModel node) { + if(node==null || node.getNodeId()==null || node.getSeqId()==null){ + return false; + } + //--第二步,查询节点状态 有效 唯一的Id或者null (已完成) + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("node_id"); +// fields.add("seq_id"); +// fields.add("node_state"); +// fields.add("system_id"); +// fields.add("node_group_id"); + String sql=" select nt.node_id " + + " from node_table nt " + + " left join ( " + + " select count(nt.node_id) node_count,nt.seq_id " + + " from node_table nt " + + " where nt.seq_id = "+node.getSeqId().longValue()+" and nt.node_state=0 " + + " group by nt.seq_id ) ntc " + + " on nt.seq_id =ntc.seq_id " + + " where ntc.node_count=1 and nt.node_group_id="+node.getGroupId().longValue();//一个节点组内不允许有重复的节点 + ArrayList> mapList; + try { + mapList = dao.dbSelect(sql,fields); + //--第三步,当第一步节点有效时:比较第一步的节点id和第二步的节点Id是有相等 或者 当第一步节点无效时:null + //node state 0有效 1无效 + if(new Long(1l).equals(node.getNodeState())){//节点无效 + if (mapList != null && mapList.size() > 0) { //不为空,是逻辑节点 + return false; + }else{ //为空,是物理节点 + return true; + } + }else{//节点有效 + if (mapList != null && mapList.size() > 0) { //不为空,是物理节点 + return true; + }else{ //为空,是逻辑节点 + return false; + } + } + } catch (Exception e) { + e.printStackTrace(); + return false; + }finally{ + dao.clearConn(); + } + } + /** + * 根据节点组ID获取节点组信息 + * @time Dec 23, 2011-2:20:37 PM + * @return + */ + public NodegroupModel getNodeGroupInfoById(Long nodeGroupId) { + NodegroupModel nodeGroup = null; + if(nodeGroupId == null){ + return nodeGroup; + } + try { + // SetInfo信息查询 + ArrayList fields = new ArrayList(); + fields.add("group_id"); + fields.add("group_name"); + fields.add("group_type"); + fields.add("system_id"); + fields.add("is_valid"); + + String sql="select t.group_id,t.group_name, t.group_type, t.system_id, t.is_valid from nodegroup_table t where t.group_id = '"+nodeGroupId.longValue()+"' "; + ArrayList> mapList = dao.dbSelect(sql,fields); + if (mapList != null && mapList.size() > 0) { + nodeGroup = new NodegroupModel(); + Map map = mapList.get(0); + nodeGroup.setGroupId(nodeGroupId); + nodeGroup.setGroupName(map.get("group_name")); + nodeGroup.setGroupType(StringUtils.isEmpty(map.get("group_type"))?null:Long.valueOf(map.get("group_type"))); + nodeGroup.setIsValid(StringUtils.isEmpty(map.get("is_valid"))?null:Long.valueOf(map.get("is_valid"))); + nodeGroup.setSystemId(StringUtils.isEmpty(map.get("system_id"))?null:Long.valueOf(map.get("system_id"))); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return nodeGroup; + } + /** + * 获取指定SetInfo ID的所涉及到的节点IP集合 + * @time Oct 12, 2011-10:46:49 AM + * @param setInfoId SetInfo ID + * @return + */ +// public List getUpdateNodeIpListBySetInfoId(Long setInfoId) { +// List nodeList = new ArrayList(); +// SetInfo setInfo = this.getSetInfoById(setInfoId); +// ArrayList fields = new ArrayList(); +// ArrayList> mapList = null; +// try { +// if (StringUtils.isNotEmpty(setInfo.getNodeIp())) { // 单节点监测任务变更 +// nodeList.add(setInfo.getNodeIp()); +// } else { // setInfo为缺省设置,查询需要更新监测任务的主机 +// fields.add("node_ip"); +// mapList = dao +// .dbSelect( +// "select distinct nt.node_ip from node_table nt where nt.node_ip not in (select dsi.node_ip from detection_set_info dsi where dsi.check_type_id ='" +// + setInfoId +// + "' and dsi.node_ip is not null )", +// fields); +// if (mapList != null && mapList.size() > 0) { +// for (Map map : mapList) { +// nodeList.add(map.get("node_ip")); +// } +// } +// } +// } catch (Exception e) { +// logger.error("",e); +// }finally{ +// dao.clearConn(); +// } +// return nodeList; +// } + /** + * 获取指定SetInfo ID的所涉及到的节点IP集合 + * @time Oct 12, 2011-10:46:49 AM + * @param setInfoId SetInfo ID + * @return + */ +// public List getNodeIpListBySetInfoId(Long setInfoId) { +// List nodeList = new ArrayList(); +// SetInfo setInfo = this.getSetInfoById(setInfoId); +// ArrayList fields = new ArrayList(); +// ArrayList> mapList = null; +// try { +// if (StringUtils.isNotEmpty(setInfo.getNodeIpsId())) { // 单节点监测任务变更 +// nodeList.addAll(getNodeIpListByIds(setInfo.getNodeIpsId())); +// } else { // setInfo为缺省设置,查询需要更新监测任务的主机 +// nodeList.addAll(getNodeIpListByGroupIds(setInfo.getNodeGroupsId())); +// } +// } catch (Exception e) { +// logger.error("",e); +// }finally{ +// dao.clearConn(); +// } +// return nodeList; +// } + + /** + * 根据多个node_id 查询ip + * @time Jun 15, 2011-4:52:41 PM + * @param ids + * @return + */ +// public List getNodeIpListByIds(String ids){ +// List ips = new LinkedList(); +// ArrayList fields = new ArrayList(); +// fields.add("node_ip"); +// String sql = "select distinct nt.node_ip from node_table nt where nt.node_id in("+(StringUtils.isEmpty(ids)?0:ids)+") "+" "+getIpnSelect(); +// ArrayList> mapList = null; +// try { +// mapList = dao.dbSelect(sql,fields); +// if (mapList != null && mapList.size() > 0) { +// for (Map map : mapList) { +// ips.add(map.get("node_ip")); +// } +// } +// } catch (Exception e) { +// logger.error("", e); +// } +// return ips; +// } + + /** + * 根据多个node_id 查询ip + * @time Jun 15, 2011-4:52:41 PM + * @param ids + * @return + */ +// public List getNodeIpListByGroupIds(String gids){ +// List ips = new LinkedList(); +// ArrayList fields = new ArrayList(); +// fields.add("node_ip"); +// String gidsStr = null; +// if(StringUtils.isNotEmpty(gids)){ +// gidsStr = "and nt.node_group_id in( select ngt.group_id from NODEGROUP_TABLE ngt where ngt.leaf_group = 1 start with ngt.group_id in ("+gids+") connect by prior ngt.group_id = ngt.parent_group_id)"; +// } +// String sql = "select distinct nt.node_ip from node_table nt where 1=1 "+(StringUtils.isEmpty(gidsStr)?"":gidsStr)+" "+getIpnSelect(); +// ArrayList> mapList = null; +// try { +// mapList = dao.dbSelect(sql,fields); +// if (mapList != null && mapList.size() > 0) { +// for (Map map : mapList) { +// ips.add(map.get("node_ip")); +// } +// } +// } catch (Exception e) { +// logger.error("", e); +// } +// return ips; +// } + + /** + * 更新监测设置ID_设置名称映射 + * @time Sep 20, 2011-9:07:24 AM + * @param checkTable 监测类型和表名Map集合 + * @param typeName 监测类型(为空即为整体更新) + */ + public void updateSetInfoName(Map setInfoNameMap,Long siId) { + String selectSql = "select dsi.id,dsi.process_iden_name from detection_set_info dsi where dsi.detection_set_state =1 and id is not null" ; + if (siId != null && siId.longValue() != 0) { //更新指定typeName的表信息 + selectSql += "and dsi.id = "+siId.toString(); + }else{ //整体更新--先清空集合 + setInfoNameMap.clear(); + } + selectSql += "" ; + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("process_iden_name"); + try { + ArrayList> datas = dao.dbSelect(selectSql,fields); + if(datas!=null && datas.size()>0){ + for(Map data : datas){ + Common.getSetInfoNameMape().put(Long.parseLong(data.get("id")), data.get("process_iden_name")); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + /** + * 更新监测类型表及表名Map集合 + * @time Sep 20, 2011-9:07:24 AM + * @param checkTable 监测类型和表名Map集合 + * @param typeName 监测类型(为空即为整体更新) + */ + public void updateCheckTableInfo(Map checkTable,String typeName) { + String selectSql = "select cti.check_type_name,cti.table_name,cti.is_snmp,cti.oid,cti.check_type_name1 from check_type_info cti where CRETE_STATE =0 " ; + if (StringUtils.isNotEmpty(typeName)) { //更新指定typeName的表信息 + if(Constants.DETEC_SNMP_STR.equals(typeName)){ + selectSql += "and nvl(cti.IS_SNMP,2) <> '2' "; + }else{ + selectSql += "and lower(cti.check_type_name) = lower('"+typeName+"') "; + } + }else{ //整体更新--先清空集合 + checkTable.clear(); + } + selectSql += "" ; + ArrayList fields = new ArrayList(); + fields.add("check_type_name"); + fields.add("table_name"); + fields.add("is_snmp"); + fields.add("oid"); + fields.add("check_type_name1"); + try { + ArrayList> datas = dao.dbSelect(selectSql,fields); + if(datas!=null && datas.size()>0){ + for(Map data : datas){ + checkTable.put(data.get("check_type_name"),new TableModel(data.get("table_name"),data.get("oid"),StringUtils.isEmpty(data.get("is_snmp"))?null:Long.parseLong(data.get("is_snmp")))); + Common.getCheckTypeNameMape().put(data.get("check_type_name"), data.get("check_type_name1")); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + /** + * 更新表字段结构Map 用于监测数据详细数据解析部分 + * @time Sep 7, 2011-5:39:51 PM + * @param tableColumnsMap 集合 + * @param typeName 类型名称(监测类型与表名一对一可以根据类型名查询) + */ + public void updateTableColumnsMap(Map> tableColumnsMap,String typeName){ + String selectSql = "select cti.check_type_name,md.show_num, md.filed_name,md.filed_comments,md.filed_type,md.oid " + + "from metadata md " + + "left join check_type_info cti on cti.id = md.check_type_id " + + "where md.state = 0 and cti.check_type_name is not null " ; + if (StringUtils.isNotEmpty(typeName)) { //清理指定集合的数据 + selectSql +="and lower(cti.check_type_name) = lower('"+typeName+"') " ; + tableColumnsMap.remove(typeName); + }else{ //整体更新,清空集合 + tableColumnsMap.clear(); + } + selectSql += "order by cti.check_type_name,md.show_num asc"; + ArrayList fields = new ArrayList(); + fields.add("check_type_name"); + fields.add("show_num"); + fields.add("filed_name"); + fields.add("filed_comments"); + fields.add("filed_type"); + fields.add("oid"); + try { + ArrayList> datas = dao.dbSelect(selectSql,fields); + String oldType = ""; + if(datas!=null && datas.size()>0){ + Map map = null; + for(Map data : datas){ + if(!oldType.equals(data.get("check_type_name"))){ + map = tableColumnsMap.get(data.get("check_type_name")); + oldType = data.get("check_type_name")+""; + } + if(map==null){ + map = new HashMap(); + } +// System.out.println("--"+data.get("check_type_name")+" "+data.get("show_num")+" "+data.get("filed_name")+" "+data.get("filed_comments")); + map.put(Integer.parseInt(data.get("show_num")),new TableColumnsModel(data.get("filed_name"),data.get("filed_comments"),data.get("filed_type"),data.get("oid"))); + tableColumnsMap.put(oldType, map); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + protected String getIpnSelect(){ + ServerTable serverTable = Common.getServerTable(); + StringBuffer ipnStr = new StringBuffer(); + if(serverTable!=null && serverTable.getIpSegList()!=null && serverTable.getIpSegList().size()>0){ + for(ServerIpSegment ipSegment : serverTable.getIpSegList()){ + if(ipnStr.length()==0l){ //首 + ipnStr.append("(ipn >= '"+ipSegment.getStartIpn().longValue()+"' and ipn <= '"+ipSegment.getEndIpn().longValue()+"')"); + }else{ + ipnStr.append("or (ipn >= '"+ipSegment.getStartIpn().longValue()+"' and ipn <= '"+ipSegment.getEndIpn().longValue()+"')"); + } + } + ipnStr.insert(0, " and ("); + ipnStr.append(")"); + } + return ipnStr.toString(); + } + + /** + * 更新节点列表 用于升级下发和主动监测 + * @time Sep 20, 2011-9:37:29 AM + * @param map + */ +// private void updateNodeList(Map map,ServerTable serverTable) { +// ArrayList fields = new ArrayList(); +// fields.add("node_ip"); +// fields.add("is_temp"); +// List> list = null; +// try { +// StringBuffer ipnStr = new StringBuffer(); +// if(serverTable.getIpSegList().size()>0){ +// for(ServerIpSegment ipSegment : serverTable.getIpSegList()){ +// if(ipnStr.length()==0l){ //首 +// ipnStr.append("(nt.ipn > '"+ipSegment.getStartIpn().longValue()+"' and nt.ipn < '"+ipSegment.getEndIpn().longValue()+"')"); +// }else{ +// ipnStr.append("or (nt.ipn > '"+ipSegment.getStartIpn().longValue()+"' and nt.ipn < '"+ipSegment.getEndIpn().longValue()+"')"); +// } +// } +// ipnStr.append(" and ("+ipnStr+")"); +// } +// +// list = dao.dbSelect("select nt.node_ip,nvl(nt.is_temp,1) is_temp from node_table nt " + +// "where nvl(nt.node_state,0)='0'"+ipnStr, fields); +// if(list!=null && list.size()>0){ //已存在 +// for(Map map0 : list){ +// map.put(map0.get("node_ip"), map0.get("is_temp")); +// } +// return;//跳过 +// } +// } catch (Exception e) { +// logger.error("",e); +// }finally{ +// dao.clearConn(); +// } +// } + /** + * @time Sep 9, 2011-10:25:01 AM + * @param setInfoId + * @return + */ + public void updateAlarmInfo(Map> alarmMap,Long setId){ + try { + String selectSql = "select dpr.id,dpr.detection_set_info_id, dpr.metadata_id, dpr.police_state, dpr.police_value, dpr.police_unit, dpr.police_symbols, md.show_num, dpr.police_level,dpr.police_emergent,md.filed_comments,dpr.marker,dpr.MARKER_FILED_ID " + + "from data_police_relation dpr left join metadata md on md.id = dpr.metadata_id " + + "left join detection_set_info dsi on dsi.id = dpr.detection_set_info_id " + + "where dpr.police_state = 0 " + + "and dsi.detection_set_state = 1 "; + if(setId!=null){ + selectSql += "and dsi.id = "+setId; + } + selectSql += " order by md.show_num, DPR.POLICE_LEVEL ASC"; + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("detection_set_info_id"); + fields.add("metadata_id"); + fields.add("police_state"); + fields.add("police_value"); + fields.add("police_unit"); + fields.add("police_symbols"); + fields.add("show_num"); + fields.add("police_level"); + fields.add("police_emergent"); + fields.add("filed_comments"); + fields.add("marker"); + fields.add("MARKER_FILED_ID"); + List> mapList = dao.dbSelect(selectSql, fields); + + //清理缓存中告警信息 + if(setId==null){ + alarmMap.clear(); + }else{ + List policeList = alarmMap.get(setId.longValue()+""); + if(policeList!=null){ + policeList.clear(); + } + } + + //填入新告警信息 + if(mapList!=null && mapList.size()>0){ + Map metadataIdShownumMap= selectAllMetadata(); + for(Map map : mapList){ + List policeList = alarmMap.get(map.get("detection_set_info_id")); + if(policeList==null){ + policeList = new ArrayList(); + alarmMap.put(map.get("detection_set_info_id"), policeList); + } + AlarmInfo info = new AlarmInfo(); + info.setId(StringUtils.isEmpty(map.get("id"))?null:Long.valueOf(map.get("id"))); + info.setSetInfoId(StringUtils.isEmpty(map.get("detection_set_info_id"))?null:Long.valueOf(map.get("detection_set_info_id"))); + info.setMetadataId(StringUtils.isEmpty(map.get("metadata_id"))?null:Long.valueOf(map.get("metadata_id"))); + info.setShowNum(StringUtils.isEmpty(map.get("show_num"))?null:Integer.valueOf(map.get("show_num"))); + info.setPoliceLevel(StringUtils.isEmpty(map.get("police_level"))?null:Integer.valueOf(map.get("police_level"))); + info.setPoliceState(map.get("police_state")); + info.setPoliceValue(map.get("police_value")); + info.setPoliceUnit(map.get("police_unit")); + info.setPoliceSysmbols(map.get("police_symbols")); + info.setPoliceEmergent(StringUtils.isEmpty(map.get("police_emergent"))?null:Integer.valueOf(map.get("police_emergent"))); + info.setFiledCommonts(map.get("filed_comments")); + + info.setMarker(map.get("marker")); + info.setMarkerFiledId(StringUtils.isEmpty(map.get("MARKER_FILED_ID"))?null:Integer.valueOf(map.get("MARKER_FILED_ID"))); + info.setMarkerFiledShowNum(metadataIdShownumMap.get(StringUtils.isEmpty(map.get("MARKER_FILED_ID"))?null:Integer.valueOf(map.get("MARKER_FILED_ID")))); + + policeList.add(info); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + /** + * 查询某个监测的告警信息 + * @time Sep 7, 2011-7:13:21 PM + * @param tableMap + * @param typeName + */ + public List> getAlarmInfo(String setInfoId,String showNums,String levels){ + String selectSql = "select md.filed_comments,md.show_num,dpr.police_level,dpr.police_symbols,dpr.police_value,dpr.police_unit " + + "from data_police_relation dpr " + + "left join metadata md on dpr.metadata_id = md.id " + + "where md.state = 0 " + + "and dpr.detection_set_info_id = '"+setInfoId+"' " + + "and md.show_num in ("+showNums+") " + + "and dpr.police_level in ("+levels+") " + + "order by md.show_num,dpr.police_level asc"; + ArrayList fields = new ArrayList(); + fields.add("filed_comments"); + fields.add("show_num"); + fields.add("police_level"); + fields.add("police_symbols"); + fields.add("police_value"); + fields.add("police_unit"); + try { + return dao.dbSelect(selectSql, fields); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return null; + } + //-------------------------------------------------------------------------------------------- + public List getDrivingSetInfo(String checktype,Long seqId) { + if(StringUtils.isEmpty(checktype)){ + return new LinkedList(); + } + //获取主动监测设置 ,最后参数 为false + return selectSetInfoList(null, null,seqId, checktype, null, 1l, null, 0l,false); + } + + public static void p(Object ss){ + System.out.print(ss==null?null:ss.toString()); + } + public static void pl(Object ss){ + System.out.println(ss==null?null:ss.toString()); + } + /** + * 获取主动监测设置信息列表 + * @time Nov 21, 2011-2:56:16 PM + * @param checktype + * @param setInfoId + * @return + */ + public List getDrivingSetInfo(String checktype) { + List setList = new LinkedList(); + if(StringUtils.isEmpty(checktype)){ + return setList; + } + //获取主动监测设置 ,最后参数 为false + return selectSetInfoList(0l,null,null, checktype, null, 1l, null, 0l,false); + } + + /** + * 查询DetectionSetInfo 信息 + * @time Jan 21, 2013-9:51:18 AM + * @param checkWay 监测方式 0主动 1被动 + * @param setInfoId 监测设置ID + * @param seqId 节点物理标示ID + * @param checktype 节点类型 + * @param groupId 节点组ID + * @param groupState 节点组状态 1有效 0无效 + * @param nodeId 节点ID + * @param nodeState 节点状态 0有效 1无效 + * @param agentFlag 是否是agent获取标示 true 是Agent获取,不是DC下发,不增加DC管理范围ID过滤 false 是DC下发,增加DC管理范围ID过滤 + * @return + */ + public List selectSetInfoList(Long checkWay ,Long setInfoId ,Long seqId , String checktype ,String groupId ,Long groupState,String nodeId,Long nodeState,boolean agentFlag) { + List setInfoList = new LinkedList(); + //-- 查询执行Ping的节点 + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("node_ips_id"); + fields.add("check_type_id"); + fields.add("process_Iden"); + fields.add("process_file"); + fields.add("process_path"); + fields.add("check_gap"); + fields.add("check_out_time"); + fields.add("check_max_times"); + fields.add("plan_check_time"); + fields.add("check_type_name"); + fields.add("detection_set_state"); + fields.add("node_groups_id"); + fields.add("check_way"); + fields.add("is_control_start"); + fields.add("control_start_time"); + fields.add("upload_gap"); + fields.add("VIEW_LEVEL"); + fields.add("IS_SNMP"); + fields.add("is_schedule"); + fields.add("process_search_keycode"); + + ArrayList> pingList = null; + try { + String searchSql = this.selectSetInfoSql(checkWay ,setInfoId,seqId,checktype,groupId,groupState,nodeId,nodeState,agentFlag); + pingList = dao.dbSelect(searchSql, fields); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + if(pingList!=null && pingList.size()>0){ + try { + for(Map map : pingList){ + SetInfo info = new SetInfo(); + info.setId(StringUtils.isEmpty(map.get("id"))?null:Long.valueOf(map.get("id"))); + info.setCheckTypeId(StringUtils.isEmpty(map.get("check_type_id"))?null:Long.valueOf(map.get("check_type_id"))); + info.setCheckGap(StringUtils.isEmpty(map.get("check_gap"))?null:Long.valueOf(map.get("check_gap"))); + info.setCheckOutTime(StringUtils.isEmpty(map.get("check_out_time"))?null:Long.valueOf(map.get("check_out_time"))); + info.setCheckMaxTimes(StringUtils.isEmpty(map.get("check_max_times"))?null:Long.valueOf(map.get("check_max_times"))); + info.setProcessIden(map.get("process_Iden")); + info.setProcessFile(map.get("process_file")); + info.setProcessPath(map.get("process_path")); + info.setCheckWay(map.get("check_way")); + info.setCheckState(map.get("detection_set_state")); + info.setCheckTypeName(map.get("check_type_name")); + info.setIsControlStart(map.get("is_control_start")); + info.setUploadGap(StringUtils.isEmpty(map.get("upload_gap"))?null:Long.valueOf(map.get("upload_gap"))); + info.setViewLevel(StringUtils.isEmpty(map.get("VIEW_LEVEL"))?null:Long.valueOf(map.get("VIEW_LEVEL"))); + info.setIsSNMP(StringUtils.isEmpty(map.get("IS_SNMP"))?null:Long.valueOf(map.get("IS_SNMP"))); + if(!StringUtils.isEmpty(map.get("plan_check_time"))){ + info.setPlanCheckTime(format.parse(map.get("plan_check_time")).getTime()); + } + if(!StringUtils.isEmpty(map.get("control_start_time"))){ + info.setControlStartTime(format.parse(map.get("control_start_time")).getTime()); + } + info.setIsSchedule(map.get("is_schedule")); + info.setNodeIpsId(StringUtils.trim(map.get("node_ips_id"))); + info.setNodeGroupsId(StringUtils.trim(map.get("node_groups_id"))); + info.setProcessSearchKeyCode(StringUtils.trim(map.get("process_search_keycode"))); +// info.setGroupId(map.get("group_id")); +// info.setNodeIp(map.get("node_ip")); + setInfoList.add(info); + } + } catch (ParseException e) { + logger.error("",e); + } + } + return setInfoList; + } +// public String getSetInfoByGroupID(Long checkWay ,Long seqId, String checktype,String groupIds,Long groupState,String nodeIds,Long nodeState){ +// +// } + /** + * 查询DetectionSetInfo 信息 + * @time Apr 9, 2012-10:00:16 AM + * @param checkWay + * @param setInfoId + * @param seqId + * @param checktype + * @param groupId + * @param groupState + * @param nodeId + * @param nodeState + * @return + */ + /*public List selectSetInfoListWithoutIPSegment(Long checkWay ,Long setInfoId ,Long seqId , String checktype ,String groupId ,Long groupState,String nodeId,Long nodeState) { + List setInfoList = new LinkedList(); + //-- 查询执行Ping的节点 + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("node_ips_id"); + fields.add("check_type_id"); + fields.add("process_Iden"); + fields.add("process_file"); + fields.add("process_path"); + fields.add("check_gap"); + fields.add("check_out_time"); + fields.add("check_max_times"); + fields.add("plan_check_time"); + fields.add("check_type_name"); + fields.add("detection_set_state"); + fields.add("node_groups_id"); + fields.add("check_way"); + fields.add("is_control_start"); + fields.add("control_start_time"); + fields.add("upload_gap"); + fields.add("VIEW_LEVEL"); + fields.add("IS_SNMP"); + fields.add("is_schedule"); + String searchSql = this.selectSetInfoSqlWithoutIPSegment(checkWay ,setInfoId,seqId,checktype,groupId,groupState,nodeId,nodeState); + ArrayList> pingList = null; + try { + pingList = dao.dbSelect(searchSql, fields); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + if(pingList!=null && pingList.size()>0){ + try { + for(Map map : pingList){ + SetInfo info = new SetInfo(); + info.setId(StringUtils.isEmpty(map.get("id"))?null:Long.valueOf(map.get("id"))); + info.setCheckTypeId(StringUtils.isEmpty(map.get("check_type_id"))?null:Long.valueOf(map.get("check_type_id"))); + info.setCheckGap(StringUtils.isEmpty(map.get("check_gap"))?null:Long.valueOf(map.get("check_gap"))); + info.setCheckOutTime(StringUtils.isEmpty(map.get("check_out_time"))?null:Long.valueOf(map.get("check_out_time"))); + info.setCheckMaxTimes(StringUtils.isEmpty(map.get("check_max_times"))?null:Long.valueOf(map.get("check_max_times"))); + info.setProcessIden(map.get("process_Iden")); + info.setProcessFile(map.get("process_file")); + info.setProcessPath(map.get("process_path")); + info.setCheckWay(map.get("check_way")); + info.setCheckState(map.get("detection_set_state")); + info.setCheckTypeName(map.get("check_type_name")); + info.setIsControlStart(map.get("is_control_start")); + info.setUploadGap(StringUtils.isEmpty(map.get("upload_gap"))?null:Long.valueOf(map.get("upload_gap"))); + info.setViewLevel(StringUtils.isEmpty(map.get("VIEW_LEVEL"))?null:Long.valueOf(map.get("VIEW_LEVEL"))); + info.setIsSNMP(StringUtils.isEmpty(map.get("IS_SNMP"))?null:Long.valueOf(map.get("IS_SNMP"))); + if(!StringUtils.isEmpty(map.get("plan_check_time"))){ + info.setPlanCheckTime(format.parse(map.get("plan_check_time")).getTime()); + } + if(!StringUtils.isEmpty(map.get("control_start_time"))){ + info.setControlStartTime(format.parse(map.get("control_start_time")).getTime()); + } + info.setIsSchedule(map.get("is_schedule")); + info.setNodeIpsId(StringUtils.trim(map.get("node_ips_id"))); + info.setNodeGroupsId(StringUtils.trim(map.get("node_groups_id"))); +// info.setGroupId(map.get("group_id")); +// info.setNodeIp(map.get("node_ip")); + setInfoList.add(info); + } + } catch (ParseException e) { + logger.error("",e); + } + } + return setInfoList; + } + + public String selectSetInfoSqlWithoutIPSegment(Long checkWay ,Long setInfoId ,Long seqId, String checktype,String groupIds,Long groupState,String nodeIds,Long nodeState){ + + + 设置的有效节点范围。 + 如果有NODES_IP有值则不管节点组的有效范围; + 如果NODES_IP无值则使用节点组的范围; + 如果NODES_IP和NODE_GROUPS_ID均为空则有效范围是SYSTEM_ID系统内全部有效节点; + 如果三者均为空则有效范围为NMS系统内全部有效节点。 + + + //--叶子节点组查询节点组SQL + StringBuffer groupsIdsSQL = new StringBuffer(); + + //查询叶子节点sql1 + StringBuffer nodeGroupSQL = new StringBuffer(); + nodeGroupSQL.append("select ngt.group_id from NODEGROUP_TABLE ngt left join node_table nt on nt.node_group_id = ngt.group_id where ngt.leaf_group = 1 "); + + //节点ID不为空 + if(StringUtils.isNotEmpty(nodeIds)){ + nodeGroupSQL.append("and nt.node_id in("+(StringUtils.isEmpty(nodeIds)?"0":nodeIds)+") "); + } + //节点物理标识不为空 + if(seqId != null){ + nodeGroupSQL.append("and nt.seq_id ="+seqId.longValue()+" "); + } + //节点状态不为空 + if(nodeState != null){ + nodeGroupSQL.append("and nt.node_state ="+nodeState.longValue()+" "); + } + //节点组ID不为空 + if(StringUtils.isNotEmpty(groupIds)){ + nodeGroupSQL.append("and nt.node_group_id in ("+(StringUtils.isEmpty(groupIds)?"0":groupIds)+") "); + } + //节点组状态不为空 + if(groupState != null){ + nodeGroupSQL.append("and ngt.is_valid = "+groupState.longValue()+" "); + } +// nodeGroupSQL.append(getIpnSelect()); + //--逆向查询SQL 查询包括叶子节点组在内的父节点组 + groupsIdsSQL.append("select distinct nt.group_id from NODEGROUP_TABLE nt "); + groupsIdsSQL.append("start with nt.group_id in ("); + groupsIdsSQL.append(nodeGroupSQL); + groupsIdsSQL.append(") connect by prior nt.parent_group_id = nt.group_id "); + + + + + + //全局查询SQL 即节点组、节点为空 + StringBuffer allSql = new StringBuffer(); + StringBuffer groupSql = new StringBuffer(); + StringBuffer nodeSql = new StringBuffer(); + allSql.append("select distinct dsi.id, " + + " dsi.node_ips_id, " + + " dsi.check_type_id, " + + " dsi.process_iden, " + + " dsi.process_file, " + + " dsi.process_path, " + + " dsi.check_gap, " + + " check_out_time, " + + " check_max_times, " + + " to_char(dsi.plan_check_time, '"+Constants.DB_DATE_FORMAT+"') plan_check_time, " + + " cti.check_type_name, " + + " detection_set_state, " + + " dsi.node_groups_id, " + + " dsi.check_way, " + + " dsi.is_control_start, " + + " dsi.upload_gap, " + + " dsi.VIEW_LEVEL, " + + " to_char(dsi.control_start_time, '"+Constants.DB_DATE_FORMAT+"') control_start_time, " + + " IS_SNMP, " + + " cti.is_schedule " ); + allSql.append("from v_detection_set_info dsi "); + allSql.append("left join check_type_info cti on cti.id = dsi.check_type_id "); + allSql.append("where 1=1 " ); + allSql.append("and dsi.detection_set_state = '1' "); + + if (checkWay != null) { + allSql.append("and dsi.check_way = '"+checkWay.longValue()+"' "); + } + + if(StringUtils.isNotEmpty(checktype)){ + //- SNMP监测设置获取 + if(Constants.DETEC_SNMP_STR.equals(checktype)){ //IS_SNMP 0:objectSNMP 1:SNMP4J + allSql.append("and nvl(cti.IS_SNMP,2) <> '2' "); + }else{ //IS_SNMP 2或null 通过checktype “NMSClient”“Ping” + allSql.append("and cti.check_type_name = '"+checktype+"' "); + } + } + + if (setInfoId != null) { + allSql.append("and dsi.id = '"+setInfoId.longValue()+"' "); + } + + allSql.append("and dsi.node_groups_id is null "); + allSql.append("and dsi.node_ips_id is null "); + + //-----------------------end + + //-- group +// if(nodeIds == null ){ + groupSql.append("select distinct dsi.id, " + + " dsi.node_ips_id, " + + " dsi.check_type_id, " + + " dsi.process_iden, " + + " dsi.process_file, " + + " dsi.process_path, " + + " dsi.check_gap, " + + " check_out_time, " + + " check_max_times, " + + " to_char(dsi.plan_check_time, '"+Constants.DB_DATE_FORMAT+"') plan_check_time, " + + " cti.check_type_name, " + + " detection_set_state, " + + " dsi.node_groups_id, " + + " dsi.check_way, " + + " dsi.is_control_start, " + + " dsi.upload_gap, " + + " dsi.VIEW_LEVEL, " + + " to_char(dsi.control_start_time, '"+Constants.DB_DATE_FORMAT+"') control_start_time, " + + " IS_SNMP, " + + " cti.is_schedule "); + groupSql.append("from v_detection_set_info dsi "); + groupSql.append("left join check_type_info cti on cti.id = dsi.check_type_id "); + //groupSql.append("left join nodegroup_table ngt on ('0,'||dsi.node_groups_id||',0' like '%,' || ngt.group_id || ',%')"); + //groupSql.append("left join node_table nt on nt.node_group_id = dsi.node_group_id "); + groupSql.append("where 1=1 " ); + groupSql.append("and dsi.detection_set_state = '1' "); + + if (checkWay != null) { //监测方式 0主动 1被动 + groupSql.append("and dsi.check_way = '"+checkWay.longValue()+"' "); + } + + if (groupState != null) { //节点组状态 + groupSql.append("and dsi.is_valid = '"+groupState.longValue()+"' "); //--1有效 0无效 + } + + if (StringUtils.isNotEmpty(groupsIdsSQL.toString())) { //节点组ID + groupSql.append("and dsi.node_group_id in ("+groupsIdsSQL+") " ); + } + + if(StringUtils.isNotEmpty(checktype)){ //监测类别名称 + if(Constants.DETEC_SNMP_STR.equals(checktype)){ //IS_SNMP 0:objectSNMP 1:SNMP4J + groupSql.append("and nvl(cti.IS_SNMP,2) <> '2' "); + }else{ //IS_SNMP 2或null 通过checktype “NMSClient”“Ping” + groupSql.append("and cti.check_type_name = '"+checktype+"' "); + } + } + + if (setInfoId != null) { + groupSql.append("and dsi.id = '"+setInfoId.longValue()+"' "); + } + groupSql.append("and dsi.node_groups_id is not null "); + groupSql.append("and dsi.node_ips_id is null "); +// } + + //存在node的查询sql + nodeSql.append("select distinct dsi.id, " + + " dsi.node_ips_id, " + + " dsi.check_type_id, " + + " dsi.process_iden, " + + " dsi.process_file, " + + " dsi.process_path, " + + " dsi.check_gap, " + + " check_out_time, " + + " check_max_times, " + + " to_char(dsi.plan_check_time, '"+Constants.DB_DATE_FORMAT+"') plan_check_time, " + + " cti.check_type_name, " + + " detection_set_state, " + + " dsi.node_groups_id, " + + " dsi.check_way, " + + " dsi.is_control_start, " + + " dsi.upload_gap, " + + " dsi.VIEW_LEVEL, " + + " to_char(dsi.control_start_time, '"+Constants.DB_DATE_FORMAT+"') control_start_time, " + + " IS_SNMP, " + + " cti.is_schedule "); + nodeSql.append("from v_detection_set_info dsi "); + nodeSql.append("left join check_type_info cti on cti.id = dsi.check_type_id "); + //nodeSql.append("left join nodegroup_table ngt on ('0,'||dsi.node_groups_id||',0' like '%,' || ngt.group_id || ',%')"); + //nodeSql.append("left join node_table nt on ('0,'||dsi.node_ips_id||',0' like '%,' || nt.node_id || ',%') "); + nodeSql.append("where 1=1 " ); + nodeSql.append("and dsi.detection_set_state = '1' "); + if (checkWay != null) { //监测方式 主动 被动 + nodeSql.append("and dsi.check_way = '"+checkWay.longValue()+"' "); + } + if (groupState != null) { //节点组状态 + nodeSql.append("and dsi.is_valid = '"+groupState.longValue()+"' "); //--1有效 0无效 + } + if (StringUtils.isNotEmpty(groupsIdsSQL.toString())) { //节点组ID + nodeSql.append("and dsi.node_group_id in ("+groupsIdsSQL+") " ); + } + if (!StringUtils.isEmpty(nodeIds)) { //节点ID + nodeSql.append("and dsi.node_id in("+nodeIds+")" ); + } + if (nodeState != null) { //节点状态 + nodeSql.append("and dsi.node_state = '"+nodeState.longValue()+"' "); //--0有效 1无效 + } + if(StringUtils.isNotEmpty(checktype)){ //监测类别名称 + if(Constants.DETEC_SNMP_STR.equals(checktype)){ //IS_SNMP 0:objectSNMP 1:SNMP4J + nodeSql.append("and nvl(cti.IS_SNMP,2) <> '2' "); + }else{ //IS_SNMP 2或null 通过checktype “NMSClient”“Ping” + nodeSql.append("and cti.check_type_name = '"+checktype+"' "); + } + if(pingList!=null && pingList.size()>0){ + try { + for(Map map : pingList){ + SetInfo info = new SetInfo(); + info.setId(StringUtils.isEmpty(map.get("id"))?null:Long.valueOf(map.get("id"))); + info.setCheckTypeId(StringUtils.isEmpty(map.get("check_type_id"))?null:Long.valueOf(map.get("check_type_id"))); + info.setCheckGap(StringUtils.isEmpty(map.get("check_gap"))?null:Long.valueOf(map.get("check_gap"))); + info.setCheckOutTime(StringUtils.isEmpty(map.get("check_out_time"))?null:Long.valueOf(map.get("check_out_time"))); + info.setCheckMaxTimes(StringUtils.isEmpty(map.get("check_max_times"))?null:Long.valueOf(map.get("check_max_times"))); + info.setProcessIden(map.get("process_Iden")); + info.setProcessFile(map.get("process_file")); + info.setProcessPath(map.get("process_path")); + info.setCheckWay(map.get("check_way")); + info.setCheckState(map.get("detection_set_state")); + info.setCheckTypeName(map.get("check_type_name")); + info.setIsControlStart(map.get("is_control_start")); + info.setUploadGap(StringUtils.isEmpty(map.get("upload_gap"))?null:Long.valueOf(map.get("upload_gap"))); + info.setViewLevel(StringUtils.isEmpty(map.get("VIEW_LEVEL"))?null:Long.valueOf(map.get("VIEW_LEVEL"))); + info.setIsSNMP(StringUtils.isEmpty(map.get("IS_SNMP"))?null:Long.valueOf(map.get("IS_SNMP"))); + if(!StringUtils.isEmpty(map.get("plan_check_time"))){ + info.setPlanCheckTime(format.parse(map.get("plan_check_time")).getTime()); + } + if(!StringUtils.isEmpty(map.get("control_start_time"))){ + info.setControlStartTime(format.parse(map.get("control_start_time")).getTime()); + } + info.setIsSchedule(map.get("is_schedule")); + info.setNodeIpsId(StringUtils.trim(map.get("node_ips_id"))); + info.setNodeGroupsId(StringUtils.trim(map.get("node_groups_id"))); + info.setProcessSearchKeyCode(StringUtils.trim(map.get("process_search_keycode"))); +// info.setGroupId(map.get("group_id")); +// info.setNodeIp(map.get("node_ip")); + setInfoList.add(info); + } + } catch (ParseException e) { + logger.error("",e); + } + } + return setInfoList; + } + + /* + * 查询DetectionSetInfo 信息 + * @time Jan 21, 2013-9:51:18 AM + * @param checkWay 监测方式 0主动 1被动 + * @param setInfoId 监测设置ID + * @param seqId 节点物理标示ID + * @param checktype 节点类型 + * @param groupId 节点组ID + * @param groupState 节点组状态 1有效 0无效 + * @param nodeId 节点ID + * @param nodeState 节点状态 0有效 1无效 + * @param agentFlag 是否是agent获取标示 true 是Agent获取,不是DC下发,不增加DC管理范围ID过滤 false 是DC下发,增加DC管理范围ID过滤 + * @return + */ + /** + * 查询DetectionSetInfo 信息 + * @time Jan 21, 2013-9:51:18 AM + * @param checkWay 监测方式 0主动 1被动 + * @param setInfoId 监测设置ID + * @param seqId 节点物理标示ID + * @param checktype 节点类型 + * @param groupId 节点组ID + * @param groupState 节点组状态 1有效 0无效 + * @param nodeId 节点ID + * @param nodeState 节点状态 0有效 1无效 + * @param agentFlag 是否是agent获取标示 true 是Agent获取,不是DC下发,不增加DC管理范围ID过滤 false 是DC下发,增加DC管理范围ID过滤 + * @return + * @throws Exception + */ + public String selectSetInfoSql(Long checkWay ,Long setInfoId ,Long seqId, String checktype,String groupIds,Long groupState,String nodeIds,Long nodeState,boolean agentFlag) throws Exception{ + + //--节点组查询节点组SQL(包括叶子节点 和 非叶子节点) + StringBuffer groupsIdsSQL = new StringBuffer(); + + /* 叶子节点组查询节点组SQL */ + StringBuffer nodeGroupSQL = new StringBuffer(); + nodeGroupSQL.append("select ngt.group_id from NODEGROUP_TABLE ngt left join node_table nt on nt.node_group_id = ngt.group_id where ngt.leaf_group = 1 "); + + //节点ID不为空 + if(StringUtils.isNotBlank(nodeIds) && !"null".equals(nodeIds)){ + nodeGroupSQL.append("and nt.node_id in ("+nodeIds+") "); + } + //节点物理标识不为空 + if(seqId != null){ + nodeGroupSQL.append("and nt.seq_id ="+seqId.longValue()+" "); + } + //节点状态不为空 + if(nodeState != null){ + nodeGroupSQL.append("and nt.node_state ="+nodeState.longValue()+" "); + } + //节点组ID不为空 + if(StringUtils.isNotEmpty(groupIds)){ + nodeGroupSQL.append("and nt.node_group_id in ("+(StringUtils.isEmpty(groupIds)?"0":groupIds)+") "); + } + //节点组状态不为空 + if(groupState != null){ //节点组状态 1 有效 0无效 + nodeGroupSQL.append("and ngt.is_valid = "+groupState.longValue()+" "); + } + + //是Agent主动获取监测设置,无需增加主动监测ip管理范围过滤 + if(!agentFlag){ + nodeGroupSQL.append(getIpnSelect()); + } + + //--逆向查询SQL 查询包括叶子节点组在内的父节点组 +// groupsIdsSQL.append("select distinct nt.group_id from NODEGROUP_TABLE nt "); +// groupsIdsSQL.append("start with nt.group_id in ("); +// groupsIdsSQL.append(nodeGroupSQL); +// groupsIdsSQL.append(") connect by prior nt.parent_group_id = nt.group_id "); + + String gIds = dao.startWithConnectByReverse("NODEGROUP_TABLE", null, "group_id in ( " + nodeGroupSQL.toString() +" ) ", "group_id", "parent_group_id"); + groupsIdsSQL.append(gIds); + + StringBuffer allSql = new StringBuffer(); + StringBuffer groupSql = new StringBuffer(); + StringBuffer nodeSql = new StringBuffer(); + /* 查询detec_set_info node_ips_id为空 node_groups_id为空 system_id不作条件 begin */ + //全局查询SQL 即节点组、节点为空 + allSql.append("select distinct dsi.id, " + + " dsi.node_ips_id, " + + " dsi.check_type_id, " + + " dsi.process_iden, " + + " dsi.process_file, " + + " dsi.process_path, " + + " dsi.check_gap, " + + " check_out_time, " + + " check_max_times, " + + " to_char(dsi.plan_check_time, '"+Constants.DB_DATE_FORMAT+"') plan_check_time, " + + " cti.check_type_name, " + + " detection_set_state, " + + " dsi.node_groups_id, " + + " dsi.check_way, " + + " dsi.is_control_start, " + + " dsi.upload_gap, " + + " dsi.VIEW_LEVEL, " + + " to_char(dsi.control_start_time, '"+Constants.DB_DATE_FORMAT+"') control_start_time, " + + " IS_SNMP, " + + " cti.is_schedule,dsi.process_search_keycode " ); + allSql.append("from v_detection_set_info dsi "); + allSql.append("left join check_type_info cti on cti.id = dsi.check_type_id "); + allSql.append("where 1=1 " ); + allSql.append("and dsi.detection_set_state = '1' "); + + if (checkWay != null) { + allSql.append("and dsi.check_way = '"+checkWay.longValue()+"' "); + } + + if(StringUtils.isNotEmpty(checktype)){ + //- SNMP监测设置获取 + if(Constants.DETEC_SNMP_STR.equals(checktype)){ //IS_SNMP 0:objectSNMP 1:SNMP4J 2或null不是snmp监测 + allSql.append("and nvl(cti.IS_SNMP,2) <> '2' and lower(cti.check_type_name) <> lower('"+Constants.DETEC_SWITCH_STR+"') "); + }else{ //IS_SNMP 2或null 通过checktype “NMSClient”“Ping” + allSql.append("and lower(cti.check_type_name) = lower('"+checktype+"') "); + } + } + + if (setInfoId != null) { + allSql.append("and dsi.id = '"+setInfoId.longValue()+"' "); + } + + allSql.append("and dsi.node_groups_id is null "); + allSql.append("and dsi.node_ips_id is null "); + /* 查询detec_set_info node_ips_id为空 node_groups_id为空 end */ + + + //-- group + /* 查询detec_set_info node_ips_id为空 node_groups_id不为空 begin */ +// if(nodeIds == null ){ + groupSql.append("select distinct dsi.id, " + + " dsi.node_ips_id, " + + " dsi.check_type_id, " + + " dsi.process_iden, " + + " dsi.process_file, " + + " dsi.process_path, " + + " dsi.check_gap, " + + " check_out_time, " + + " check_max_times, " + + " to_char(dsi.plan_check_time, '"+Constants.DB_DATE_FORMAT+"') plan_check_time, " + + " cti.check_type_name, " + + " detection_set_state, " + + " dsi.node_groups_id, " + + " dsi.check_way, " + + " dsi.is_control_start, " + + " dsi.upload_gap, " + + " dsi.VIEW_LEVEL, " + + " to_char(dsi.control_start_time, '"+Constants.DB_DATE_FORMAT+"') control_start_time, " + + " IS_SNMP, " + + " cti.is_schedule,dsi.process_search_keycode "); + groupSql.append("from v_detection_set_info dsi "); + groupSql.append("left join check_type_info cti on cti.id = dsi.check_type_id "); + groupSql.append("where 1=1 " ); + groupSql.append("and dsi.detection_set_state = '1' "); + + if (checkWay != null) { //监测方式 0主动 1被动 + groupSql.append("and dsi.check_way = '"+checkWay.longValue()+"' "); + } + + if (groupState != null) { //节点组状态 + groupSql.append("and dsi.is_valid = '"+groupState.longValue()+"' "); //--1有效 0无效 + } + + if (StringUtils.isNotEmpty(groupsIdsSQL.toString())) { //节点组ID + groupSql.append("and dsi.node_group_id in ("+groupsIdsSQL+") " ); + } + + //监测类别名称 + if(StringUtils.isNotEmpty(checktype)){ + if(Constants.DETEC_SNMP_STR.equals(checktype)){ //IS_SNMP 0:objectSNMP 1:SNMP4J 2或null:不是snmp + groupSql.append("and nvl(cti.IS_SNMP,2) <> '2' "); + }else{ //IS_SNMP 2或null 通过checktype “NMSClient”“Ping” 等指定 类型名称的查询 + groupSql.append("and lower(cti.check_type_name) = lower('"+checktype+"') "); + } + } + + //监测设置ID + if (setInfoId != null) { + groupSql.append("and dsi.id = '"+setInfoId.longValue()+"' "); + } + groupSql.append("and dsi.node_groups_id is not null "); + groupSql.append("and dsi.node_ips_id is null "); +// } + /* 查询detec_set_info node_ips_id为空 node_groups_id不为空 end */ + + /* 查询detec_set_info node_ips_id不为空 begin */ + //存在node的查询sql + nodeSql.append("select distinct dsi.id, " + + " dsi.node_ips_id, " + + " dsi.check_type_id, " + + " dsi.process_iden, " + + " dsi.process_file, " + + " dsi.process_path, " + + " dsi.check_gap, " + + " check_out_time, " + + " check_max_times, " + + " to_char(dsi.plan_check_time, '"+Constants.DB_DATE_FORMAT+"') plan_check_time, " + + " cti.check_type_name, " + + " detection_set_state, " + + " dsi.node_groups_id, " + + " dsi.check_way, " + + " dsi.is_control_start, " + + " dsi.upload_gap, " + + " dsi.VIEW_LEVEL, " + + " to_char(dsi.control_start_time, '"+Constants.DB_DATE_FORMAT+"') control_start_time, " + + " IS_SNMP, " + + " cti.is_schedule,dsi.process_search_keycode "); + nodeSql.append("from v_detection_set_info dsi "); + nodeSql.append("left join check_type_info cti on cti.id = dsi.check_type_id "); + nodeSql.append("where 1=1 " ); + nodeSql.append("and dsi.detection_set_state = '1' "); + if (checkWay != null) { //监测方式 主动 被动 + nodeSql.append("and dsi.check_way = '"+checkWay.longValue()+"' "); + } + if (groupState != null) { //节点组状态 + nodeSql.append("and dsi.is_valid = '"+groupState.longValue()+"' "); //--1有效 0无效 + } + if (StringUtils.isNotEmpty(groupsIdsSQL.toString())) { //节点组ID + nodeSql.append("and dsi.node_group_id in ("+groupsIdsSQL+") " ); + } + if (!StringUtils.isEmpty(nodeIds)) { //节点ID + nodeSql.append("and dsi.node_id in("+nodeIds+")" ); + } + if (nodeState != null) { //节点状态 + nodeSql.append("and dsi.node_state = '"+nodeState.longValue()+"' "); //--0有效 1无效 + } + if(StringUtils.isNotEmpty(checktype)){ //监测类别名称 + if(Constants.DETEC_SNMP_STR.equals(checktype)){ //IS_SNMP 0:objectSNMP 1:SNMP4J + nodeSql.append("and nvl(cti.IS_SNMP,2) <> '2' "); + }else{ //IS_SNMP 2或null 通过checktype “NMSClient”“Ping” + nodeSql.append("and lower(cti.check_type_name) = lower('"+checktype+"') "); + } + } + if (setInfoId != null) { + nodeSql.append("and dsi.id = '"+setInfoId.longValue()+"' "); + } + if (seqId != null) { + nodeSql.append("and dsi.seq_id = '"+seqId.longValue()+"' "); + } + nodeSql.append("and dsi.node_groups_id is not null "); + nodeSql.append("and dsi.node_ips_id is not null "); + /* 查询detec_set_info node_ips_id不为空 end */ + + /* 拼写总的sql */ + StringBuffer unionSql = new StringBuffer(); + unionSql.append(""); + if(allSql.length()!=0 || groupSql.length()!=0){ + unionSql.append("("+nodeSql+")"); + if(groupSql.length() != 0){ + unionSql.append(" union all ("+groupSql+")"); + } + if(allSql.length() != 0){ + unionSql.append(" union all ("+allSql+")"); + } + }else{ + unionSql.append(nodeSql); + } + return unionSql.toString(); + } + + /** + * IP & Seq_id one to one + * @time Dec 17, 2012-6:49:08 PM + * @param uuid + * @param newIP + * @param systemType + * @return + */ + public String[] checkAndUpdateNodeParams(Long uuid,String newIP,Integer systemType) { + String[] resultInfo = new String[2]; + try { + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("NODE_SYSTEM_TYPE"); + String selectSql = "select distinct nt.node_ip,nt.seq_id,nt.NODE_SYSTEM_TYPE from node_table nt where nt.seq_id="+uuid+" or nt.node_ip ='"+newIP+"'"; + ArrayList> mapsList = dao.dbSelect(selectSql, fields); + List oldNodeParams = null; + List newNodeParams = null; + if(mapsList !=null && mapsList.size()>0){ + for(Map map : mapsList){ + //old node + if(StringUtils.isNotEmpty(map.get(fields.get(1))) && !map.get(fields.get(1)).equals(uuid.toString())){ + oldNodeParams = new ArrayList(); + oldNodeParams.add(StringUtils.isEmpty(map.get(fields.get(0)))?null:map.get(fields.get(0))); + oldNodeParams.add(StringUtils.isEmpty(map.get(fields.get(1)))?null:map.get(fields.get(1))); + oldNodeParams.add(StringUtils.isEmpty(map.get(fields.get(2)))?null:map.get(fields.get(2))); + } + + //new node + if(StringUtils.isNotEmpty(map.get(fields.get(0))) && !map.get(fields.get(1)).equals(uuid.toString())){ + newNodeParams = new ArrayList(); + newNodeParams.add(StringUtils.isEmpty(map.get(fields.get(0)))?null:map.get(fields.get(0))); + newNodeParams.add(StringUtils.isEmpty(map.get(fields.get(1)))?null:map.get(fields.get(1))); + newNodeParams.add(StringUtils.isEmpty(map.get(fields.get(2)))?null:map.get(fields.get(2))); + } + } + } + + if(oldNodeParams!=null){ + resultInfo[0] = "1"; +// resultInfo[1] = "新IP被占用,更新失败"; +// resultInfo[1] = "New IP is occupied, update failed"; + resultInfo[1] = "i18n_server.CommonService.resultInfo1_n81i"; + }else{ + //新IP未被使用 ,可用于变更操作 + dao.dbUpdate("update node_table nt set nt.node_ip='"+newIP+"' ,nt.NODE_SYSTEM_TYPE='"+systemType+"' where nt.seq_id="+uuid+""); + resultInfo[0] = "0"; + resultInfo[1] = "更新成功"; + } + } catch (Exception e) { + logger.error("校验并更新节点参数失败", e); + }finally{ + dao.clearConn(); + } + return resultInfo; + } + //-------------------------------------------------------------------------------------------- + /** + * @time Sep 20, 2011-2:38:48 PM + * @param alarms + * @return + */ + public boolean resoveAlarms(String[] alarms) throws Exception{ + boolean flag = false; + // alarms长度为9 ip type 进程标识,告警信息 + if (alarms == null || alarms.length < 9) { + return false; + } + + // 整理 + try { + String setInfoId = alarms[0]; + Map publicInfo = new HashMap(); + // seqId, + String seqId = alarms[1]; + publicInfo.put("seq_Id", seqId); +// publicInfo.put("node_ip", Common.getSeqIdMape().get(Long.parseLong(seqId))); + // 监测设置ID, + publicInfo.put("detection_set_info_id", setInfoId); + publicInfo.put("check_way", "1"); + +// publicInfo.put("current_times", "");//hyx 2013-6-20 add:由于以前主动告警只是针对NC,所以有特定的格式:common的alarmExceptionInfo方法,没有current_times + + // 监测类别, + String checkType = alarms[2]; + // 进程名称(检查类别设置名称), + String process = alarms[3]; + // 监测服务启动时间, + String startTime =format.format(new Date(Long.parseLong(alarms[4]))); + publicInfo.put("start_time", startTime); + // 本次检测时间, + String checkTime =format.format(new Date(Long.parseLong(alarms[5]))); + publicInfo.put("DATA_CHECK_TIME".toLowerCase(), checkTime); // 测试时间 + + + // 告警级别 + String pLevel = alarms[6]; + String state = alarms[7]; + if(StringUtils.isEmpty(pLevel)){ + if("-2".equals(state)){ + pLevel = Constants.NC_ALARM_POLICE_LEVEL; + }else if("2".equals(state)){ + pLevel = "99"; + } + } + publicInfo.put("police_level", pLevel); + // 执行状态是否成功,记录的状态是否正常(用于报警 -2主动告警 -1执行失败 0不正常,1正常), + publicInfo.put("detectioned_state", state); + + // 状态信息(描述信息), + //--状态信息(描述信息), + String dsinfo = alarms[8]; + if(StringUtils.isNotEmpty(dsinfo)){ + // add by zg 增加对
的处理 begin + dsinfo = dsinfo.replaceAll("
", "\n"); + // add by zg 增加对
的处理 end + if(dsinfo.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + dsinfo = StringUtil.substring(dsinfo,Constants.VARCHAR_MAX_LENGTH); + } + } + + publicInfo.put("detection_state_info", dsinfo); + //性能数据, + publicInfo.put("performace_data",dsinfo); + + //--获取第十字段 告警 + String showNum = null; + int urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY;//默认为紧急 + if(alarms.length>9){ + showNum = alarms[9]; + } + + + if(StringUtils.isNotBlank(showNum)){ + if(NumberUtils.isNumber(showNum)){ + List alarmList = Common.getAlarmInfoMap().get(setInfoId); + if(alarmList!=null && alarmList.size()>0){ + for(AlarmInfo info : alarmList){ + if(info!=null && info.getPoliceEmergent()!=null){ + if(info.getShowNum().intValue() == Integer.parseInt(showNum)){ + urgentLevel = info.getPoliceEmergent(); + } + } + } + } + } + } + + //- 组织邮件信息 + int actionType = EmailTypeConstants.TYPE_ALARM_INFO_EXCEPTION; + if("99".equals(pLevel)){ + actionType = EmailTypeConstants.TYPE_ALARM_INFO_RECOVER; + urgentLevel = EmailTypeConstants.URGENT_LATER; + } + + if(Integer.parseInt(pLevel)>0){ + urgentLevel = EmailTypeConstants.URGENT_LATER; + } + + publicInfo.put("POLICE_EMERGENT",urgentLevel+""); + + // -- 根据执行状态判断是否需要获取详细数据的信息 及报警信息 + + // 将数据保存到历史数据表,并查询出Id + Calendar arriveTime = Calendar.getInstance(); + publicInfo.put("DATA_ARRIVE_TIME".toLowerCase(), format.format(arriveTime.getTime()));// 获取到数据时间(入库时间) + publicInfo.put("STATUS_CHANGE_TIME".toLowerCase(), checkTime);// 获取到数据时间(入库时间) + publicInfo.put("data_check_time_digital".toLowerCase(), alarms[5]);// 获取到数据时间长整形 + publicInfo.put("data_arrive_time_digital".toLowerCase(), arriveTime.getTimeInMillis()+"");// 获取到数据时间长整形 + + // -- 告警信息整理 + StringBuffer pInfo = new StringBuffer(); + if("99".equals(pLevel)){ +// pInfo.append("告警恢复 监测内容 " + dsinfo + "\n"); +// pInfo.append("Alarm Recovery Monitoring Content " + dsinfo + "\n"); + pInfo.append("i18n_server.CommonService.pInfo1_n81i " + dsinfo + "\n"); + }else{ +// pInfo.append("告警级别 " + alarms[6] + " 监测内容 " + dsinfo + "\n"); +// pInfo.append("Alert level " + alarms[6] + " Monitoring content " + dsinfo + "\n"); + pInfo.append("i18n_server.CommonService.pInfo2_n81i " + alarms[6] + " i18n_server.CommonService.monitorContent_n81i " + dsinfo + "\n"); + } +// publicInfo.put("notice_info".toLowerCase(), pInfo.toString().toCharArray().length>499?StringUtil.getChineseByLength(pInfo.toString(), 0, 499):pInfo.toString()); + + + ArrayList fields = new ArrayList(); + + + //查询detection_info_new 表,判断状态变更,进行报警通知 + String searchSql = "select count(1) counts from detection_info_new din where din.seq_id = ? and din.detection_set_info_id = ? " + + "and (din.detectioned_state <>? or (din.detectioned_state = ? and din.police_level<>?)) and din.data_check_time_digital < ?"; + fields.add("counts"); + List> dta = dao.dbSelect(searchSql, fields, seqId, setInfoId,publicInfo.get("detectioned_state"),publicInfo.get("detectioned_state"),publicInfo.get("police_level"),alarms[5]); + + /* 第五步:状态变更时发送邮件 */ + //-- 拼写邮件头 + boolean sendEmailFlag = false; + if (dta == null || dta.size()==0) { + sendEmailFlag = true; + }else{ + if (dta.get(0) != null) { + String counts = dta.get(0).get("counts"); + if((StringUtils.isNotEmpty(counts) && !"null".equalsIgnoreCase(counts) && !counts.equals("0"))){ //状态变化发送 + sendEmailFlag = true; + } + } + } + + if(sendEmailFlag){ + long seqIdLong = Long.parseLong(seqId); + long setInfoIdLong = Long.parseLong(setInfoId); + String emailContent = pInfo.toString(); + if(emailContent.getBytes().length>290){ + emailContent = StringUtil.substring(emailContent,290); + } + EmailInfo emailInfo = new EmailInfo(actionType,Common.getSetInfoNameMape().get(setInfoIdLong)+"("+Common.getCheckTypeNameMape().get(checkType)+")",Common.getNodeIpByUUID(seqIdLong), checkTime,emailContent,EmailTypeConstants.FLAG_SEND_LATER,urgentLevel); + new EmailService(dao).sendEmailForDetectionBySeqId(setInfoIdLong,seqIdLong,emailInfo); + } + + + + + // 新增保存报警表信息 +// publicInfo.put("notice_flag".toLowerCase(), "1");// 1已发送 0未发送 + + //-- 获取ID + fields.clear(); + + String id = DetectInfo.computeId(checkTime, seqId, setInfoId); + + /*String searchIdSql = "select seq_detection_info.nextval id from dual"; + fields.add("id"); + List> sid = dao.dbSelect(searchIdSql, fields); + String id = null; + if(sid!=null && sid.size()>0){ + id = sid.get(0).get("id"); + }*/ + dao.setAutoCommit(false); + publicInfo.put("id", id); + dao.insertObj("detection_info", publicInfo); + publicInfo.remove("id"); + publicInfo.put("detection_info_id", id); + // 保存到报警表 + flag = dao.insertObj("detection_info_warning".toLowerCase(),publicInfo); + dao.commit(); + } catch (Exception e) { + logger.error("resoveAlarms 告警信息解析异常:"+"",e); + dao.rollback(); + return false; + } finally { + dao.clearConn(); + } + return flag; + } + + + public Object[] resoveNewData(List strsList,Map> allPortName) throws Exception{ + /* 监测数据有效判断*/ + if(strsList==null || strsList.size()==0 + || strsList.get(0).length<12){ + return null; + } + + //监测解析结果 + Object[] detectInfo = null; + Iterator strsIte = strsList.iterator();//一条监测数据:有多行数据 + String [] datas = strsIte.next(); + + /** + * 公共部分解析格式: + * seqId, + * 监测设置ID, + * 监测类别, + * 进程名称(监测类别设置名称), + * 监测服务启动时间, + * 检测时延(秒), + * 本次检测时间, + * 尝试次数, + * 下次计划监测时间, + * 执行状态是否成功是否正常, + * 状态信息(描述信息), + * 性能数据 + **/ + + /* 基本信息解析格式变量 */ + String seqId = null; //seqId + String setInfoId = null; //监测设置ID + String checkType = null; //监测类别 + String process = null; //进程名称(监测类别设置名称) + Long startTime = null; //监测服务启动时间 + + Integer delayTime = null; //检测时延(秒) + Long checkTime = null; //本次检测时间 + Integer currentTimes = null; //尝试次数 + Long nextCheckTime = null; //下次计划监测时间 + int state = -1; //执行状态是否成功是否正常 + String dsinfo = null; //状态信息(描述信息) + String pdata = null; //性能数据 + + + /* 其他变量 */ + StringBuffer errorInfo= new StringBuffer(); //数据文件错误,信息整理 + boolean delyFlag = true; //详细数据获取标识 + Long stateChangeTime = null; //状态变更时间 + + /* 第一步:解析公共数据部分 */ + + try { + + //--seqId, + seqId = datas[0]; +// logger.debug("被监测节点seqId:"+seqId); + if(StringUtils.isEmpty(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不能为空;"); + errorInfo.append("Location"+(0)+"data node seqId (physical host ID) cannot be empty;"); + }else if(!StringUtil.isNumber(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不是正整数:"+seqId); + errorInfo.append("Location"+(0)+"data node seqId (physical host ID) is not a positive integer:"+seqId); + } + + //--监测设置ID, + setInfoId = datas[1]; +// logger.debug("监测项:"+setInfoId); + if(StringUtils.isEmpty(setInfoId)){ + errorInfo.append("Location"+(1)+"data Monitoring Settings ID cannot be emp;"); + }else if(!StringUtil.isNumber(setInfoId) && !"0".equals(setInfoId)){ +// errorInfo.append("位置"+(1)+"数据 监测设置ID不是整数(可以为0,默认的系统基础信息的设置就为0):"+setInfoId); + errorInfo.append("Location"+(1)+"Data Monitoring setting ID is not an integer (can be 0, the default system basic information is set to 0):"+setInfoId); + } + + //--监测类别 + checkType = datas[2]; + if(StringUtils.isEmpty(checkType)){ + errorInfo.append("位置"+(2)+"数据 监测类别不能为空;"); + } + + //--进程名称(检查类别设置名称), + process = datas[3]; + + //--监测服务启动时间, + String startTimeStr = datas[4]; + if(StringUtils.isNotEmpty(startTimeStr)){ + try { + startTime = Long.parseLong(startTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(4)+"数据 “"+startTime+"” 监测服务启动时间须为整形数:"+startTime); + } + }else { + errorInfo.append("位置"+(4)+"数据 监测服务启动时间不能为空;"); + } + + //--检测时延(秒), + String delayTimeStr = datas[5]; + if(StringUtils.isNotEmpty(delayTimeStr)){ + try { + delayTime = Integer.parseInt(delayTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(5)+"数据 “"+delayTime+"” 检测时延(秒)须为整形数:"+delayTime); + } + } + + //--本次检测时间, + String checkTimeStr = datas[6]; + if(StringUtils.isNotEmpty(checkTimeStr)){ + try { + checkTime = Long.parseLong(checkTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(6)+"数据 “"+checkTimeStr+"” 本次检测时间须为整形数:"+checkTime); + } + }else { + errorInfo.append("位置"+(6)+"数据 本次检测时间不能为空;"); + } + + //--尝试次数, + String currentTimesSre = datas[7]; + if(StringUtils.isNotEmpty(currentTimesSre)){ + try { + currentTimes = Integer.parseInt(currentTimesSre); + } catch (Exception e) { + errorInfo.append("位置"+(7)+"数据 “"+currentTimesSre+"” 尝试次数须为整形数:"+currentTimes); + } + }else{ + errorInfo.append("位置"+(7)+"数据 尝试次数不能为空;"); + } + + //--下次计划监测时间, + String nextCheckTimeStr = datas[8]; + if(StringUtils.isNotEmpty(nextCheckTimeStr)){ + try { + nextCheckTime = Long.parseLong(nextCheckTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(8)+"数据 “"+nextCheckTimeStr+"” 下次计划监测时间须为整形数:"+nextCheckTime); + } + }else{ + errorInfo.append("位置"+(8)+"数据 尝试次数不能为空;"); + } + + //--执行状态是否成功,记录的状态是否正常(用于报警 -1执行失败 0不正常,1正常), + String stateStr = datas[9]; + if(StringUtils.isNotEmpty(stateStr)){ + try { + state = Integer.parseInt(stateStr); + } catch (Exception e) { + errorInfo.append("位置"+(9)+"数据 “"+stateStr+"” 执行状态须为整形数:"+state); + } + }else{ + errorInfo.append("位置"+(9)+"数据 执行状态不能为空;"); + } + + //--状态信息(描述信息), + dsinfo = datas[10]; + + + //--性能数据 + pdata = datas[11]; + if(StringUtils.isNotEmpty(pdata)){ + // add by zg 增加对
的处理 begin + pdata = pdata.replaceAll("
", "\n"); + // add by zg 增加对
的处理 end + if(pdata.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + pdata = StringUtil.substring(pdata,Constants.VARCHAR_MAX_LENGTH); + } + }else {//界面异常信息列表的状态信息显示为性能数据,所以如果性能数据为空,则取状态信息 + pdata = dsinfo; + } + + } catch (Exception e) { + String s = "基本数据部分解析失败"+e.getMessage(); + logger.warn(s); + errorInfo.append(s); + } + + //--基本信息解析 存在异常值 无须保存,返回错误信息 用于系统告警 + if(errorInfo.length() != 0){ + logger.error(" 第一步-1: 新格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); + return null; + } + + //-- 根据执行状态判断是否需要获取详细数据的信息 及报警信息,执行失败,则不要获取 + if(state < 0){ //失败 报警only + delyFlag = false;//程序执行失败时,不入库详细监测信息 + } + + + //公共信息集合 + Calendar arriveTime = Calendar.getInstance(); + detectInfo = new Object[33]; + //detectInfo[DetectInfo.null] = (CSVUtils.csvBytesPrinter1(strsList, Constants.COMMON_TEXT_CODING)); + detectInfo[DetectInfo.SEQID] = (seqId); + detectInfo[DetectInfo.SETINFOID] = (setInfoId); + detectInfo[DetectInfo.STARTTIME] = (startTime); + detectInfo[DetectInfo.DELAYTIME] = (delayTime); + detectInfo[DetectInfo.CHECKTIME] = (checkTime); + detectInfo[DetectInfo.CURRENTTIMES] = (currentTimes); + detectInfo[DetectInfo.NEXTCHECKTIME] = (nextCheckTime); + detectInfo[DetectInfo.CHECKTYPE] = (checkType); + detectInfo[DetectInfo.PDATA] = (pdata); + detectInfo[DetectInfo.ARRIVETIME] = (arriveTime.getTimeInMillis()); + detectInfo[DetectInfo.PROCESS] = process; + + /* 第二步:解析详细数据部分 */ + Map tableMap = Common.getTableMap().get(checkType); + if(tableMap ==null || tableMap.size() == 0){ + logger.error(""+checkType+"无法获取表结构"); + delyFlag = false; + } + + List> detaiList = new ArrayList>(); //详细数据集合 + int pLevel =99;//默认恢复正常 + StringBuffer alarmInfo = new StringBuffer(); + int urgentLevel = EmailTypeConstants.URGENT_LATER; + if(delyFlag){ + + details:while(strsIte.hasNext()){ + String [] detailsArr = strsIte.next(); //取出 + if(detailsArr == null || detailsArr.length ==0){ //空行不解析 + continue; + } + if(detailsArr.length != 2){ //不是[details:0]格式无法解析 + break details; + } + if("details".equalsIgnoreCase(detailsArr[0])){ + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + /* + * 如果是checkType='switchport'监测,且有详细信息,才判断每个详细信息的状态,再确定公共信息的状态:人为设置状态、流量大于0 + 一、根据端口状态,修改监测信息(detection_info、di_switchport表)的detection_state: + 公共信息状态:-1执行不成功;0异常;1正常 + 端口信息状态:1:正常,0:异常 + 1.查询当前节点人为设置状态port_set_state为up的端口:1up,2down + 2.对于每个端口 + if(端口state==1&&当前端口的设置状态为up) {//如果state非0才做判断,并修改,如果state已经为0,则没必要再判断端口的状态(只要有一个端口发生以下异常,则detection_state=0异常) + + 若IFADMINSTATUS状态为down,则端口state和公共信息state异常(1up,2down) + if(端口state==1) { + 判断流量(输入速度+输出速度)为0,则端口state和公共信息state异常 + } + } + + */ + List portNameList = null; + if(endNum>0 && Constants.DETEC_SWITCH_STR.equalsIgnoreCase(checkType)) { + //查询当前节点设置状态为up的端口名称,生成list + portNameList = allPortName.get(seqId); + } + + /** + * 储存详细数据前三个字段,恢复数据时使用 + */ + List showNumList = new LinkedList(); + List plevelList = new LinkedList(); + List alarmList = new LinkedList(); + while(strsIte.hasNext()){ + endNum--; + String [] detailsArr0 = strsIte.next(); //取出 + Map detail = new HashMap(); //详细数据集合 + //-- 详细数据解析 + if(detailsArr0!=null && detailsArr0.length>0){ + //告警数据相关信息:0-2 + String showNumStr = detailsArr0[0]; + String pLevelStr = detailsArr0[1]; + String alarmValue = detailsArr0[2]; + + showNumList.add(showNumStr); + plevelList.add(pLevelStr); + alarmList.add(alarmValue); + + int index = 3; //数据集合解析游标 + try { + //-- 详细信息整理 监测具体数据信息(每个字段一列)。 + int i = 0 ; + while(detail.size() < tableMap.size()){//将metadata表中对应的字段,按shouNum进行组合数据 + TableColumnsModel field = tableMap.get(i); + ++i; + if((i-1)>tableMap.size()){ + break; + } + if(field!=null){ + //当监测数据(index == detailsArr0.length)时,及有效监测数据已经读取完成,tableMap页无需增加空字段数据 + if(index == detailsArr0.length){ + detail.put(field.getFiledName(), null); + continue; + } + try { + String fileType = field.getFiledType().toUpperCase(); + if("DATE".equals(fileType)){ + if(StringUtils.isNumeric(detailsArr0[index])){//如果为数字型时间,转换 + detail.put(field.getFiledName(), format.format(new Date(Long.parseLong(detailsArr0[index].trim())))); + }else{//eg:2016-03-06 12:12:12 + detail.put(field.getFiledName(), detailsArr0[index].trim()); + } + }else{ + if("NUMBER".equals(fileType)){ + if (StringUtils.isBlank(detailsArr0[index]) + || StringUtil.isRationalNumber(detailsArr0[index])) { + detail.put(field.getFiledName(), detailsArr0[index].trim()); + } else if (detailsArr0[index].trim().contains(" days, ")) { + detail.put(field.getFiledName(), getLongTimeStr(detailsArr0[index].trim())); + } else { + throw new Exception("NUMBER数据格式错误!"); + } + }else{ // VARCHAR2(x) + String length = fileType.replaceAll("VARCHAR2\\((\\d+)\\)", "$1"); + if(Integer.parseInt(length) < detailsArr0[index].trim().length()) { + throw new Exception("VARCHAR2数据格式错误!"); + } + detail.put(field.getFiledName(), detailsArr0[index].trim()); + } + } + } catch (Exception e) { + errorInfo.append(checkType+" 表字段集合信息解析失败;"); + errorInfo.append(field.getFiledName()).append("字段取值") + .append(detailsArr0[index]).append("发生错误;异常数据行:"); + for (String string : detailsArr0) { // 添加异常数据 + errorInfo.append(string).append(','); + } + errorInfo.deleteCharAt(errorInfo.length()-1); + //监测数据的详细信息如果和指定的字段类型不匹配,则抛弃此条监测数据,避免影响批量入库 + logger.error(" 第一步-1: 新格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); + return null; + } + index++; + } + } + //组合附加的一些信息 + detail.put("SEQ_ID", seqId); + String detailDetectionState = ""; + if(StringUtils.isBlank(pLevelStr)){//没有告警,当前详细监测信息正常 + detailDetectionState = "1";//正常 + }else{ + detailDetectionState = "0";//异常 + } + //如果是switchport监测,有端口详细信息,且端口状态为正常,才需要再根据以下条件判断端口是否异常 + if(portNameList!=null&&portNameList.size()>0&&"1".equals(detailDetectionState)) { + String ifDescr = detailsArr0[4];//ifDescr-showNum:2--+2=4(因为有3个告警的字段) + /** + * 1.人为设置端口状态为up时,若IFADMINSTATUS或者IFOPERSTATUS状态为down2(!=1:up),则state=0异常--1up,2down + if(state==1) { + 2.流量(输入速度+输出速度)为0,则state=0异常 + } + */ + if(portNameList.contains(ifDescr)) {//如果此端口设置的状态为up + String ifAdminStatus = detailsArr0[9];//showNum=7,1:up,2:down + if("2".equals(ifAdminStatus)) { + state = 0;//监测公共信息的状态 + dsinfo += "$@$端口"+ifDescr+"异常(配置状态与实际状态不符)"; + detailDetectionState = "0";//当前端口的状态 + pLevel = Constants.PORT_ALARM_LEVEL;//非界面设置,默认为5级 + } + + if("1".equals(detailDetectionState)) {//如果当前端口的状态正常,才需要判断流量是否为0 + double inOctetsSpeed = detailsArr0[25]==null?0d:("".equals(detailsArr0[25])?0d:Double.parseDouble(detailsArr0[25].trim()));//23+2=25,瞬时输入字节速度 + double outOctetsSpeed = detailsArr0[29]==null?0d:("".equals(detailsArr0[29])?0d:Double.parseDouble(detailsArr0[29].trim()));//27+2=29,瞬时输出字节速度 + logger.info("test:port:inOctetsSpeed="+inOctetsSpeed+";outOctetsSpeed="+outOctetsSpeed); + if((inOctetsSpeed+outOctetsSpeed)<=0) { + state = 0;//监测公共信息的状态 + dsinfo += "$@$端口"+ifDescr+"异常(流量为0),inOctetsSpeed="+inOctetsSpeed+",outOctetsSpeed="+outOctetsSpeed+",sum="+(inOctetsSpeed+outOctetsSpeed); + detailDetectionState = "0";//当前端口的状态 + pLevel = Constants.PORT_ALARM_LEVEL;//非界面设置,默认为5级 + } + } + } + } + + detail.put("DETECTIONED_STATE", detailDetectionState); + detail.put("detection_set_info_id",setInfoId); // 入库时间 + detail.put("data_check_time_digital", checkTime+""); // 测试时间 + detail.put("data_check_time", format.format(new Date(checkTime))); // 测试时间 + detail.put("data_arrive_time_digital", arriveTime.getTimeInMillis()+""); // 入库时间 + detail.put("data_arrive_time",format.format(arriveTime.getTime())); // 入库时间 + detaiList.add(detail); + //-- 整理告警信息 + if(state == 0){ + String [] nums = showNumStr.split("\\|"); + String [] levels = pLevelStr.split("\\|"); + String [] values = alarmValue.split("\\|"); + List aList = Common.getAlarmInfoMap().get(setInfoId); + Iterator ite = null; + for(int m = 0 ; m= Integer.parseInt(levels[m].trim())){ + pLevel = Integer.parseInt(levels[m].trim()); + //0级别:对应紧急,其它对应非紧急 + if(pLevel==Constants.LEVEL_OF_EMERGENCY) { + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + }else { + urgentLevel = EmailTypeConstants.URGENT_LATER; + } + } + } catch (Exception e) + { + logger.error("新格式监测数据-修改正告警级别时,解析告警level:"+levels[m]+" 异常", e); + } + + /** 字段名称 intDate 实际值: 80》60%(临界值) 级别 40 **/ + String filedName = ""; + String filedComments = ""; + if(tableMap !=null){ + TableColumnsModel tm = tableMap.get(elem.getShowNum()); + filedName = tm==null?"":tm.getFiledName(); + filedComments = tm==null?"":tm.getFiledComments(); + } + cpInfo += " "+(StringUtils.isEmpty(elem.getFiledCommonts())?filedName:elem.getFiledCommonts()); + cpInfo += " 为:"+detail.get(filedName); + cpInfo += (elem.getPoliceUnit()==null ? "" : "null".equals(elem.getPoliceUnit()) ? "" : elem.getPoliceUnit()); + cpInfo += " 报警级别:"+pLevel; + cpInfo += " \n"; + } + } + //如果是握手监测,不添加告警设置字段相关信息 + if(!Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + alarmInfo.append(cpInfo); + } + } + } + } catch (Exception e) { + String s = "详细数据部分解析失败"+e.getMessage(); + logger.warn(s); + } + } + + detectInfo[DetectInfo.SHOWNUMLIST] = (showNumList); + detectInfo[DetectInfo.PLEVELLIST] = (plevelList); + detectInfo[DetectInfo.ALARMLIST] = (alarmList); + + if(endNum ==0){ + break details; + } + } + if(dsinfo.startsWith("$@$") && dsinfo.length()>3) { + dsinfo = dsinfo.substring(3, dsinfo.length()); + } + } + } + } + if(state == -1){//监测执行失败,是最高级别的告警 + //NMSC 监测失败状态 程序设定为为紧急 + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + pLevel = Constants.DETECT_FAIL_NMSC_POLICE_LEVEL; + urgentLevel = Constants.DETECT_FAIL_NMSC_POLICE_EMERGENT; + }else { + pLevel = Constants.DETECT_FAIL_NON_NMSC_POLICE_LEVEL; + urgentLevel = Constants.DETECT_FAIL_NON_NMSC_POLICE_EMERGENT; + } + } + + //--状态信息(描述信息), + if(StringUtils.isNotEmpty(dsinfo)){ + dsinfo = dsinfo.replaceAll("
", "\n");// add by zg 增加对
的处理 begin + if(dsinfo.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + dsinfo = StringUtil.substring(dsinfo,Constants.VARCHAR_MAX_LENGTH); + } + } + + detectInfo[DetectInfo.STATE] = (state); + detectInfo[DetectInfo.DSINFO] = (dsinfo); + detectInfo[DetectInfo.PLEVEL] = (pLevel); + detectInfo[DetectInfo.ALARMINFO] = (alarmInfo.toString());//告警信息,发送邮件的时候需要用 + detectInfo[DetectInfo.URGENTLEVEL] = (urgentLevel);//状态是否改变会影响这个告警级别 + detectInfo[DetectInfo.DELYFLAG] = (delyFlag);//根据这个参数,决定是否保存详细信息 + detectInfo[DetectInfo.DETAILS] = (detaiList);//用于批量入库详细信息 + + //特殊追加信息解析--systeminfo-disk、net + //-- 特殊信息保存整理 +// logger.debug("新格式解析 第七步 特殊追加信息解析"+(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType))); + if(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType) ) { + while (strsIte.hasNext()) { + String [] detailsArr = strsIte.next(); //取出 + if(detailsArr == null || detailsArr.length ==0){ //空行不解析 + continue; + } + if(detailsArr.length != 2){ //不是[details:0]格式无法解析 + break; + } + + if("disk".equalsIgnoreCase(detailsArr[0])){ + List infoList = new ArrayList(); + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + disk:while (strsIte.hasNext()) { + endNum--; + String [] detailsArr0 = strsIte.next(); //取出 + int len = detailsArr0.length; + String[] diskInfo = new String[8]; + //diskInfo[0] = id; + diskInfo[1] = len>0 ? detailsArr0[0]:"";//盘符 + diskInfo[2] = len>1 ? detailsArr0[1]:"";//大小 + diskInfo[3] = checkTime+"";//监测时间(long) + diskInfo[4] = format.format(new Date(checkTime));//监测时间(date) + diskInfo[5] = arriveTime.getTimeInMillis()+"";//录入记录时间(当前时间 long) + diskInfo[6] = format.format(arriveTime.getTime());//录入记录时间(当前时间 date) + diskInfo[7] = setInfoId; + infoList.add(diskInfo); + if(endNum==0){ + logger.debug("disk解析完成 跳出循环"); + break disk; + } + } + if(infoList.size()>0) { + detectInfo[DetectInfo.DISKINFOLIST] = (infoList);//插入操作批量执行 + } + }else if("net".equalsIgnoreCase(detailsArr[0])) { + List infoList = new ArrayList(); + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + net:while (strsIte.hasNext()) { + endNum--; + String [] netInfoTmp = strsIte.next(); //取出 + int len = netInfoTmp.length; + String[] netInfo = new String[13]; + //netInfo[0] = id; + netInfo[1] = len>0 ? netInfoTmp[0]:"";//网卡名称 + netInfo[2] = len>1 ? netInfoTmp[1]:"";//网卡状态 + netInfo[3] = len>2 ? netInfoTmp[2]:"";//网卡带宽 + netInfo[4] = len>3 ? netInfoTmp[3]:"";//ip + netInfo[5] = len>4 ? netInfoTmp[4]:"";//子网掩码 + netInfo[6] = len>5 ? netInfoTmp[5]:"";//网关 + netInfo[7] = len>6 ? netInfoTmp[6]:"";//NET_MAC + netInfo[8] = format.format(new Date(checkTime));//监测时间(date) + netInfo[9] = format.format(arriveTime.getTime());//录入记录时间(当前时间 date) + netInfo[10] = checkTime+"";//监测时间(long) + netInfo[11] = arriveTime.getTimeInMillis()+"";//录入记录时间(当前时间 long) + netInfo[12] = setInfoId;// id + infoList.add(netInfo); + if(endNum ==0){ + break net; + } + } + if(infoList.size()>0) { + detectInfo[DetectInfo.NETINFOLIST] = (infoList);//插入操作批量执行 + } + } + } + } + return detectInfo; + } +// public DetecAllInfo resoveNewData(List strsList,Map> allPortName) throws Exception{ +// /* 监测数据有效判断*/ +// if(strsList==null || strsList.size()==0 +// || strsList.get(0).length<12){ +// return null; +// } +// +// //监测解析结果 +// DetecAllInfo detectInfo = null; +// Iterator strsIte = strsList.iterator();//一条监测数据:有多行数据 +// String [] datas = strsIte.next(); +// +// /** +// * 公共部分解析格式: +// * seqId, +// * 监测设置ID, +// * 监测类别, +// * 进程名称(监测类别设置名称), +// * 监测服务启动时间, +// * 检测时延(秒), +// * 本次检测时间, +// * 尝试次数, +// * 下次计划监测时间, +// * 执行状态是否成功是否正常, +// * 状态信息(描述信息), +// * 性能数据 +// **/ +// +// /* 基本信息解析格式变量 */ +// String seqId = null; //seqId +// String setInfoId = null; //监测设置ID +// String checkType = null; //监测类别 +// String process = null; //进程名称(监测类别设置名称) +// Long startTime = null; //监测服务启动时间 +// +// Integer delayTime = null; //检测时延(秒) +// Long checkTime = null; //本次检测时间 +// Integer currentTimes = null; //尝试次数 +// Long nextCheckTime = null; //下次计划监测时间 +// int state = -1; //执行状态是否成功是否正常 +// String dsinfo = null; //状态信息(描述信息) +// String pdata = null; //性能数据 +// +// +// /* 其他变量 */ +// StringBuffer errorInfo= new StringBuffer(); //数据文件错误,信息整理 +// boolean delyFlag = true; //详细数据获取标识 +// Long stateChangeTime = null; //状态变更时间 +// +// /* 第一步:解析公共数据部分 */ +// +// try { +// +// //--seqId, +// seqId = datas[0]; +//// logger.debug("被监测节点seqId:"+seqId); +// if(StringUtils.isEmpty(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不能为空;"); +// }else if(!StringUtil.isNumber(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不是正整数:"+seqId); +// } +// +// //--监测设置ID, +// setInfoId = datas[1]; +//// logger.debug("监测项:"+setInfoId); +// if(StringUtils.isEmpty(setInfoId)){ +// errorInfo.append("位置"+(1)+"数据 监测设置ID不能为空;"); +// }else if(!StringUtil.isNumber(setInfoId) && !"0".equals(setInfoId)){ +// errorInfo.append("位置"+(1)+"数据 监测设置ID不是整数(可以为0,默认的系统基础信息的设置就为0):"+setInfoId); +// } +// +// //--监测类别 +// checkType = datas[2]; +// if(StringUtils.isEmpty(checkType)){ +// errorInfo.append("位置"+(2)+"数据 监测类别不能为空;"); +// } +// +// //--进程名称(检查类别设置名称), +// process = datas[3]; +// +// //--监测服务启动时间, +// String startTimeStr = datas[4]; +// if(StringUtils.isNotEmpty(startTimeStr)){ +// try { +// startTime = Long.parseLong(startTimeStr); +// } catch (Exception e) { +// errorInfo.append("位置"+(4)+"数据 “"+startTime+"” 监测服务启动时间须为整形数:"+startTime); +// } +// }else { +// errorInfo.append("位置"+(4)+"数据 监测服务启动时间不能为空;"); +// } +// +// //--检测时延(秒), +// String delayTimeStr = datas[5]; +// if(StringUtils.isNotEmpty(delayTimeStr)){ +// try { +// delayTime = Integer.parseInt(delayTimeStr); +// } catch (Exception e) { +// errorInfo.append("位置"+(5)+"数据 “"+delayTime+"” 检测时延(秒)须为整形数:"+delayTime); +// } +// } +// +// //--本次检测时间, +// String checkTimeStr = datas[6]; +// if(StringUtils.isNotEmpty(checkTimeStr)){ +// try { +// checkTime = Long.parseLong(checkTimeStr); +// } catch (Exception e) { +// errorInfo.append("位置"+(6)+"数据 “"+checkTimeStr+"” 本次检测时间须为整形数:"+checkTime); +// } +// }else { +// errorInfo.append("位置"+(6)+"数据 本次检测时间不能为空;"); +// } +// +// //--尝试次数, +// String currentTimesSre = datas[7]; +// if(StringUtils.isNotEmpty(currentTimesSre)){ +// try { +// currentTimes = Integer.parseInt(currentTimesSre); +// } catch (Exception e) { +// errorInfo.append("位置"+(7)+"数据 “"+currentTimesSre+"” 尝试次数须为整形数:"+currentTimes); +// } +// }else{ +// errorInfo.append("位置"+(7)+"数据 尝试次数不能为空;"); +// } +// +// //--下次计划监测时间, +// String nextCheckTimeStr = datas[8]; +// if(StringUtils.isNotEmpty(nextCheckTimeStr)){ +// try { +// nextCheckTime = Long.parseLong(nextCheckTimeStr); +// } catch (Exception e) { +// errorInfo.append("位置"+(8)+"数据 “"+nextCheckTimeStr+"” 下次计划监测时间须为整形数:"+nextCheckTime); +// } +// }else{ +// errorInfo.append("位置"+(8)+"数据 尝试次数不能为空;"); +// } +// +// //--执行状态是否成功,记录的状态是否正常(用于报警 -1执行失败 0不正常,1正常), +// String stateStr = datas[9]; +// if(StringUtils.isNotEmpty(stateStr)){ +// try { +// state = Integer.parseInt(stateStr); +// } catch (Exception e) { +// errorInfo.append("位置"+(9)+"数据 “"+stateStr+"” 执行状态须为整形数:"+state); +// } +// }else{ +// errorInfo.append("位置"+(9)+"数据 执行状态不能为空;"); +// } +// +// //--状态信息(描述信息), +// dsinfo = datas[10]; +// +// +// //--性能数据 +// pdata = datas[11]; +// if(StringUtils.isNotEmpty(pdata)){ +// // add by zg 增加对
的处理 begin +// pdata = pdata.replaceAll("
", "\n"); +// // add by zg 增加对
的处理 end +// if(pdata.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ +// pdata = StringUtil.substring(pdata,Constants.VARCHAR_MAX_LENGTH); +// } +// } +// +// } catch (Exception e) { +// String s = "基本数据部分解析失败"+e.getMessage(); +// logger.warn(s); +// errorInfo.append(s); +// } +// +// //--基本信息解析 存在异常值 无须保存,返回错误信息 用于系统告警 +// if(errorInfo.length() != 0){ +// logger.error(" 第一步-1: 新格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); +// return null; +// } +// +// //-- 根据执行状态判断是否需要获取详细数据的信息 及报警信息,执行失败,则不要获取 +// if(state < 0){ //失败 报警only +// delyFlag = false;//程序执行失败时,不入库详细监测信息 +// } +// +// //公共信息集合 +// detectInfo = new DetecAllInfo(); +// detectInfo.setDsb(CSVUtils.csvBytesPrinter1(strsList, Constants.COMMON_TEXT_CODING));//存储一条监测数据的字节数组格式,以备批量保存异常时,将监测数据暂存硬盘,等待下次再入库 +// detectInfo.setSeqId(seqId); +// detectInfo.setSetInfoId(setInfoId); +// detectInfo.setStartTime(startTime); +// detectInfo.setDelayTime(delayTime); +// detectInfo.setCheckTime(checkTime); +// detectInfo.setCurrentTimes(currentTimes); +// detectInfo.setNextCheckTime(nextCheckTime); +// detectInfo.setCheckType(checkType); +// detectInfo.setPdata(pdata); +// Calendar arriveTime = Calendar.getInstance(); +// detectInfo.setArriveTime(arriveTime.getTimeInMillis()); +// +// /* 第二步:解析详细数据部分 */ +// Map tableMap = Common.getTableMap().get(checkType); +// if(tableMap ==null || tableMap.size() == 0){ +// logger.error(""+checkType+"无法获取表结构"); +// delyFlag = false; +// } +// +// List> detaiList = new ArrayList>(); //详细数据集合 +// int pLevel =99;//默认恢复正常 +// StringBuffer alarmInfo = new StringBuffer(); +// int urgentLevel = EmailTypeConstants.URGENT_LATER; +// if(delyFlag){ +// +// details:while(strsIte.hasNext()){ +// String [] detailsArr = strsIte.next(); //取出 +// if(detailsArr == null || detailsArr.length ==0){ //空行不解析 +// continue; +// } +// if(detailsArr.length != 2){ //不是[details:0]格式无法解析 +// break details; +// } +// if("details".equalsIgnoreCase(detailsArr[0])){ +// int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); +// /* +// * 如果是checkType='switchport'监测,且有详细信息,才判断每个详细信息的状态,再确定公共信息的状态:人为设置状态、流量大于0 +// 一、根据端口状态,修改监测信息(detection_info、di_switchport表)的detection_state: +// 公共信息状态:-1执行不成功;0异常;1正常 +// 端口信息状态:1:正常,0:异常 +// 1.查询当前节点人为设置状态port_set_state为up的端口:1up,2down +// 2.对于每个端口 +// if(端口state==1&&当前端口的设置状态为up) {//如果state非0才做判断,并修改,如果state已经为0,则没必要再判断端口的状态(只要有一个端口发生以下异常,则detection_state=0异常) +// +// 若IFADMINSTATUS状态为down,则端口state和公共信息state异常(1up,2down) +// if(端口state==1) { +// 判断流量(输入速度+输出速度)为0,则端口state和公共信息state异常 +// } +// } +// +// */ +// List portNameList = null; +// if(endNum>0 && Constants.DETEC_SWITCH_STR.equalsIgnoreCase(checkType)) { +// //查询当前节点设置状态为up的端口名称,生成list +// portNameList = allPortName.get(seqId); +// } +// +// while(strsIte.hasNext()){ +// endNum--; +// String [] detailsArr0 = strsIte.next(); //取出 +// Map detail = new HashMap(); //详细数据集合 +// //-- 详细数据解析 +// if(detailsArr0!=null && detailsArr0.length>0){ +// //告警数据相关信息:0-2 +// String showNumStr = detailsArr0[0]; +// String pLevelStr = detailsArr0[1]; +// String alarmValue = detailsArr0[2]; +// int index = 3; //数据集合解析游标 +// try { +// //-- 详细信息整理 监测具体数据信息(每个字段一列)。 +// int i = 0 ; +// while(detail.size() < tableMap.size()){//将metadata表中对应的字段,按shouNum进行组合数据 +// TableColumnsModel field = tableMap.get(i); +// ++i; +// if((i-1)>tableMap.size()){ +// break; +// } +// if(field!=null){ +// //当监测数据(index == detailsArr0.length)时,及有效监测数据已经读取完成,tableMap页无需增加空字段数据 +// if(index == detailsArr0.length){ +// detail.put(field.getFiledName(), null); +// continue; +// } +// try { +// if("DATE".equals(field.getFiledType().toUpperCase())){ +// if(StringUtils.isNotBlank(detailsArr0[index])){ +// detail.put(field.getFiledName(), format.format(new Date(Long.parseLong(detailsArr0[index].trim())))); +// }else{ +// detail.put(field.getFiledName(), detailsArr0[index].trim()); +// } +// }else{ +// if("NUMBER".equals(field.getFiledType().toUpperCase())){ +// try { +// if(StringUtils.isNotBlank(detailsArr0[index])){ +// Double.parseDouble(detailsArr0[index].trim()); +// } +// detail.put(field.getFiledName(), detailsArr0[index].trim()); +// } catch (Exception e) { +// detail.put(field.getFiledName(), getLongTimeStr(detailsArr0[index].trim())); +// } +// }else{ +// detail.put(field.getFiledName(), detailsArr0[index].trim()); +// } +// } +// } catch (Exception e) { +// errorInfo.append(checkType+" 表字段集合信息加载失败,请检查该监测类别的相关设置后重启本应用程序!"); +// } +// index++; +// } +// } +// //组合附加的一些信息 +// detail.put("SEQ_ID", seqId); +// String detailDetectionState = ""; +// if(StringUtils.isBlank(pLevelStr)){//没有告警,当前详细监测信息正常 +// detailDetectionState = "1";//正常 +// }else{ +// detailDetectionState = "0";//异常 +// } +// //如果是switchport监测,有端口详细信息,且端口状态为正常,才需要再根据以下条件判断端口是否异常 +// if(portNameList!=null&&portNameList.size()>0&&"1".equals(detailDetectionState)) { +// String ifDescr = detailsArr0[4];//ifDescr-showNum:2--+2=4(因为有3个告警的字段) +// /** +// * 1.人为设置端口状态为up时,若IFADMINSTATUS或者IFOPERSTATUS状态为down2(!=1:up),则state=0异常--1up,2down +// if(state==1) { +// 2.流量(输入速度+输出速度)为0,则state=0异常 +// } +// */ +// if(portNameList.contains(ifDescr)) {//如果此端口设置的状态为up +// String ifAdminStatus = detailsArr0[9];//showNum=7,1:up,2:down +// if("2".equals(ifAdminStatus)) { +// state = 0;//监测公共信息的状态 +// dsinfo += "$@$端口"+ifDescr+"异常(配置状态与实际状态不符)"; +// detailDetectionState = "0";//当前端口的状态 +// pLevel = Constants.PORT_ALARM_LEVEL;//非界面设置,默认为5级 +// } +// +// if("1".equals(detailDetectionState)) {//如果当前端口的状态正常,才需要判断流量是否为0 +// double inOctetsSpeed = detailsArr0[25]==null?0d:("".equals(detailsArr0[25])?0d:Double.parseDouble(detailsArr0[25].trim()));//23+2=25,瞬时输入字节速度 +// double outOctetsSpeed = detailsArr0[29]==null?0d:("".equals(detailsArr0[29])?0d:Double.parseDouble(detailsArr0[29].trim()));//27+2=29,瞬时输出字节速度 +// logger.info("test:port:inOctetsSpeed="+inOctetsSpeed+";outOctetsSpeed="+outOctetsSpeed); +// if((inOctetsSpeed+outOctetsSpeed)<=0) { +// state = 0;//监测公共信息的状态 +// dsinfo += "$@$端口"+ifDescr+"异常(流量为0),inOctetsSpeed="+inOctetsSpeed+",outOctetsSpeed="+outOctetsSpeed+",sum="+(inOctetsSpeed+outOctetsSpeed); +// detailDetectionState = "0";//当前端口的状态 +// pLevel = Constants.PORT_ALARM_LEVEL;//非界面设置,默认为5级 +// } +// } +// } +// } +// +// detail.put("DETECTIONED_STATE", detailDetectionState); +// detail.put("detection_set_info_id",setInfoId); // 入库时间 +// detail.put("data_check_time_digital", checkTime+""); // 测试时间 +// detail.put("data_check_time", format.format(new Date(checkTime))); // 测试时间 +// detail.put("data_arrive_time_digital", arriveTime.getTimeInMillis()+""); // 入库时间 +// detail.put("data_arrive_time",format.format(arriveTime.getTime())); // 入库时间 +// detaiList.add(detail); +// //-- 整理告警信息 +// if(state == 0){ +// String [] nums = showNumStr.split("\\|"); +// String [] levels = pLevelStr.split("\\|"); +// String [] values = alarmValue.split("\\|"); +// List aList = Common.getAlarmInfoMap().get(setInfoId); +// Iterator ite = null; +// for(int m = 0 ; m= Integer.parseInt(levels[m].trim())){ +// pLevel = Integer.parseInt(levels[m].trim()); +// //0级别:对应紧急,其它对应非紧急 +// if(pLevel==Constants.LEVEL_OF_EMERGENCY) { +// urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; +// }else { +// urgentLevel = EmailTypeConstants.URGENT_LATER; +// } +// } +// } catch (Exception e) +// { +// logger.error("新格式监测数据-修改正告警级别时,解析告警level:"+levels[m]+" 异常", e); +// } +// +// /** 字段名称 intDate 实际值: 80》60%(临界值) 级别 40 **/ +// String filedName = ""; +// String filedComments = ""; +// if(tableMap !=null){ +// TableColumnsModel tm = tableMap.get(elem.getShowNum()); +// filedName = tm==null?"":tm.getFiledName(); +// filedComments = tm==null?"":tm.getFiledComments(); +// } +// cpInfo += " "+(StringUtils.isEmpty(elem.getFiledCommonts())?filedName:elem.getFiledCommonts()); +// cpInfo += " 为:"+detail.get(filedName); +// cpInfo += (elem.getPoliceUnit()==null ? "" : "null".equals(elem.getPoliceUnit()) ? "" : elem.getPoliceUnit()); +// cpInfo += " 报警级别:"+pLevel; +// cpInfo += " \n"; +// } +// } +// //如果是握手监测,不添加告警设置字段相关信息 +// if(!Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { +// alarmInfo.append(cpInfo); +// } +// } +// } +// } catch (Exception e) { +// String s = "详细数据部分解析失败"+e.getMessage(); +// logger.warn(s); +// } +// } +// if(endNum ==0){ +// break details; +// } +// } +// if(dsinfo.startsWith("$@$") && dsinfo.length()>3) { +// dsinfo = dsinfo.substring(3, dsinfo.length()); +// } +// } +// } +// } +// if(state == -1){//监测执行失败,是最高级别的告警 +// //NMSC 监测失败状态 程序设定为为紧急 +// if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ +// pLevel = Constants.DETECT_FAIL_NMSC_POLICE_LEVEL; +// urgentLevel = Constants.DETECT_FAIL_NMSC_POLICE_EMERGENT; +// }else { +// pLevel = Constants.DETECT_FAIL_NON_NMSC_POLICE_LEVEL; +// urgentLevel = Constants.DETECT_FAIL_NON_NMSC_POLICE_EMERGENT; +// } +// } +// +// //--状态信息(描述信息), +// if(StringUtils.isNotEmpty(dsinfo)){ +// dsinfo = dsinfo.replaceAll("
", "\n");// add by zg 增加对
的处理 begin +// if(dsinfo.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ +// dsinfo = StringUtil.substring(dsinfo,Constants.VARCHAR_MAX_LENGTH); +// } +// } +// detectInfo.setState(state); +// detectInfo.setDsinfo(dsinfo); +// detectInfo.setpLevel(pLevel); +// detectInfo.setAlarmInfo(alarmInfo.toString());//告警信息,发送邮件的时候需要用 +// detectInfo.setUrgentLevel(urgentLevel);//状态是否改变会影响这个告警级别 +// detectInfo.setDelyFlag(delyFlag);//根据这个参数,决定是否保存详细信息 +// detectInfo.setDetails(detaiList);//用于批量入库详细信息 +// +// //特殊追加信息解析--systeminfo-disk、net +// //-- 特殊信息保存整理 +//// logger.debug("新格式解析 第七步 特殊追加信息解析"+(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType))); +// if(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType) ) { +// while (strsIte.hasNext()) { +// String [] detailsArr = strsIte.next(); //取出 +// if(detailsArr == null || detailsArr.length ==0){ //空行不解析 +// continue; +// } +// if(detailsArr.length != 2){ //不是[details:0]格式无法解析 +// break; +// } +// +// if("disk".equalsIgnoreCase(detailsArr[0])){ +// List infoList = new ArrayList(); +// int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); +// disk:while (strsIte.hasNext()) { +// endNum--; +// String [] detailsArr0 = strsIte.next(); //取出 +// int len = detailsArr0.length; +// String[] diskInfo = new String[8]; +// //diskInfo[0] = id; +// diskInfo[1] = len>0 ? detailsArr0[0]:"";//盘符 +// diskInfo[2] = len>1 ? detailsArr0[1]:"";//大小 +// diskInfo[3] = checkTime+"";//监测时间(long) +// diskInfo[4] = format.format(new Date(checkTime));//监测时间(date) +// diskInfo[5] = arriveTime.getTimeInMillis()+"";//录入记录时间(当前时间 long) +// diskInfo[6] = format.format(arriveTime.getTime());//录入记录时间(当前时间 date) +// diskInfo[7] = setInfoId; +// infoList.add(diskInfo); +// if(endNum==0){ +// logger.debug("disk解析完成 跳出循环"); +// break disk; +// } +// } +// if(infoList.size()>0) { +// detectInfo.setDiskInfoList(infoList);//插入操作批量执行 +// } +// }else if("net".equalsIgnoreCase(detailsArr[0])) { +// List infoList = new ArrayList(); +// int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); +// net:while (strsIte.hasNext()) { +// endNum--; +// String [] netInfoTmp = strsIte.next(); //取出 +// int len = netInfoTmp.length; +// String[] netInfo = new String[13]; +// //netInfo[0] = id; +// netInfo[1] = len>0 ? netInfoTmp[0]:"";//网卡名称 +// netInfo[2] = len>1 ? netInfoTmp[1]:"";//网卡状态 +// netInfo[3] = len>2 ? netInfoTmp[2]:"";//网卡带宽 +// netInfo[4] = len>3 ? netInfoTmp[3]:"";//ip +// netInfo[5] = len>4 ? netInfoTmp[4]:"";//子网掩码 +// netInfo[6] = len>5 ? netInfoTmp[5]:"";//网关 +// netInfo[7] = len>6 ? netInfoTmp[6]:"";//NET_MAC +// netInfo[8] = format.format(new Date(checkTime));//监测时间(date) +// netInfo[9] = format.format(arriveTime.getTime());//录入记录时间(当前时间 date) +// netInfo[10] = checkTime+"";//监测时间(long) +// netInfo[11] = arriveTime.getTimeInMillis()+"";//录入记录时间(当前时间 long) +// netInfo[12] = setInfoId;// id +// infoList.add(netInfo); +// if(endNum ==0){ +// break net; +// } +// } +// if(infoList.size()>0) { +// detectInfo.setNetInfoList(infoList);//插入操作批量执行 +// } +// } +// } +// } +// return detectInfo; +// } + + + + /** + * 详细数据解析 + * 第一步:解析公共数据部分,保存各字段值到Map publicInfo 中 + * 第二步:解析详细数据部分,保存各字段到 Map detail 中 + * 第三步:检查告警信息数据,多字段告警去最高级(Level最小) + * 第四步:查询数据库中最新同类监测记录,并判断状态是否变化(同类判定:seq_id、detection_set_info_id,变化判定:DETECTIONED_STATE 和 POLICE_LEVEL 至少一个变化) + * 第五步:根据告警信息数据,拼写告警信息 + * 第六步:状态变更时发送邮件,将告警信息邮件发送 + * 第七步:保存到基本信息、非正常数据和状态变更数据信息保存到告警表中 + * 完成... + * @time Sep 8, 2011-3:40:45 PM + * @param datas + * @param dao + */ + public Object[] resoveOldData(List strsList,Map> allPortName) throws Exception{ + /* 监测数据有效判断*/ + if(strsList==null || strsList.size()==0){ + logger.debug("旧格式监测数据解析:数据内容为空,不予解析;"); + return null; + } + //监测解析结果 + Object[] detectInfo = null; + Iterator strsIte = strsList.iterator(); + String[] datas = null; + if(strsIte!=null && strsIte.hasNext()) { + datas = strsIte.next();//获得第一行基本信息 + } + if(datas==null){ + logger.debug("旧格式监测数据解析:数据内容为空,不予解析;"); + return null; + }else if(datas.length>0){/* 数据日志尝试输出 */ + String dateTime = null; + if(datas.length>6){ //第7字段 为监测时间字段 作展示标记 + dateTime = format.format(Long.parseLong(datas[6])); + } + logger.debug("监测数据-旧格式 监测时间》"+dateTime+" 内容》"+Arrays.toString(datas)); + /* 监测数据有效判断 */ + if(datas.length<15){ + logger.debug("旧格式监测数据解析:数据解析长度:"+datas.length+" 小于最小解析标准,不予解析;"); + return null; + } + /** + * 公共部分解析格式: + * seqId, + * 监测设置ID, + * 监测类别, + * 进程名称(监测类别设置名称), + * 监测服务启动时间, + * 检测时延(秒), + * 本次检测时间, + * 尝试次数, + * 下次计划监测时间, + * 执行状态是否成功是否正常, + * 告警列序号, + * 告警级别, + * 告警值, + * 状态信息(描述信息), + * 性能数据 + * (,详细数据部分).... + **/ + /* 基本信息解析格式变量 */ + String seqId = null; //seqId + String setInfoId = null; //监测设置ID + String checkType = null; //监测类别 + String process = null; //进程名称(监测类别设置名称) + Long startTime = null; //监测服务启动时间 + Integer delayTime = null; //检测时延(秒) + Long checkTime = null; //本次检测时间 + Integer currentTimes = null; //尝试次数 + Long nextCheckTime = null; //下次计划监测时间 + int state = -1; //执行状态是否成功是否正常 + String showNums = null; //告警序列(数据) + String pLevel = null; //告警级别(数据) + int pl = 99; //告警级别(入库) + String pValue = null; //告 警 值(数据) + String dsinfo = null; //状态信息(描述信息) + String pdata = null; //性能数据 + + + /* 其他变量 */ + StringBuffer errorInfo= new StringBuffer(); //数据文件错误,信息整理 + boolean delyFlag = true; //详细数据获取标识 + //Long stateChangeTime = null;//状态变更时间 + + /* 第一步:解析公共数据部分 */ + try { + //--seqId, + seqId = datas[0]; + if(StringUtils.isEmpty(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不能为空;"); + errorInfo.append("Location"+(0)+"data node seqId (physical host ID) cannot be empty;"); + }else if(!StringUtil.isNumber(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不是正整数:"+seqId); + errorInfo.append("Location"+(0)+"data node seqId (Physical Host ID) is not a positive integer:"+seqId); + } + //--监测设置ID, + setInfoId = datas[1]; + if(StringUtils.isEmpty(setInfoId)){ + errorInfo.append("位置"+(1)+"数据 监测设置ID不能为空;"); + }else if(!StringUtil.isNumber(setInfoId) && !"0".equals(setInfoId)){ + errorInfo.append("位置"+(1)+"数据 监测设置ID不是整数(包括0):"+setInfoId); + } + //--监测类别 + checkType = datas[2]; + if(StringUtils.isEmpty(checkType)){ + errorInfo.append("位置"+(2)+"数据 监测类别不能为空;"); + } + //--进程名称(检查类别设置名称), + process = datas[3]; + //--监测服务启动时间, + String startTimeStr = datas[4]; + if(StringUtils.isNotEmpty(startTimeStr)){ + try { + startTime = Long.parseLong(startTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(4)+"数据 “"+startTime+"” 监测服务启动时间须为整形数:"+startTime); + } + }else { + errorInfo.append("位置"+(4)+"数据 监测服务启动时间不能为空;"); + } + //--检测时延(秒), + String delayTimeStr = datas[5]; + if(StringUtils.isNotEmpty(delayTimeStr)){ + try { + delayTime = Integer.parseInt(delayTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(5)+"数据 “"+delayTime+"” 检测时延(秒)须为整形数:"+delayTime); + } + } + //--本次检测时间, + String checkTimeStr = datas[6]; + if(StringUtils.isNotEmpty(checkTimeStr)){ + try { + checkTime = Long.parseLong(checkTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(6)+"数据 “"+checkTimeStr+"” 本次检测时间须为整形数:"+checkTime); + } + }else { + errorInfo.append("位置"+(6)+"数据 本次检测时间不能为空;"); + } + //--尝试次数, + String currentTimesSre = datas[7]; + if(StringUtils.isNotEmpty(currentTimesSre)){ + try { + currentTimes = Integer.parseInt(currentTimesSre); + } catch (Exception e) { + errorInfo.append("位置"+(7)+"数据 “"+currentTimesSre+"” 尝试次数须为整形数:"+currentTimes); + } + }else{ + errorInfo.append("位置"+(7)+"数据 尝试次数不能为空;"); + } + //--下次计划监测时间, + String nextCheckTimeStr = datas[8]; + if(StringUtils.isNotEmpty(nextCheckTimeStr)){ + try { + nextCheckTime = Long.parseLong(nextCheckTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(8)+"数据 “"+nextCheckTimeStr+"” 下次计划监测时间须为整形数:"+nextCheckTime); + } + }else{ + errorInfo.append("位置"+(8)+"数据 尝试次数不能为空;"); + } + //--执行状态是否成功,记录的状态是否正常(用于报警 -1执行失败 0不正常,1正常), + String stateStr = datas[9]; + if(StringUtils.isNotEmpty(stateStr)){ + try { + state = Integer.parseInt(stateStr); + } catch (Exception e) { + errorInfo.append("位置"+(9)+"数据 “"+stateStr+"” 执行状态须为整形数:"+state); + } + }else{ + errorInfo.append("位置"+(9)+"数据 执行状态不能为空;"); + } + //--告警序列 + showNums = datas[10]; + //--告警级别 + pLevel = datas[11]; + String [] nums0 = pLevel.split("\\|"); + for(String num : nums0){ + if(StringUtils.isEmpty(num)){ + continue; + } + int level = Integer.parseInt(num); + if(pl>level){ + pl = level; + } + } + //失败 最高级 + if(state == -1){ + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + pl = Constants.DETECT_FAIL_NMSC_POLICE_LEVEL; + }else { + pl = Constants.DETECT_FAIL_NON_NMSC_POLICE_LEVEL; + } + } + //-- 告警值 + pValue = datas[12]; + //--状态信息(描述信息), + dsinfo = datas[13]; + if(StringUtils.isNotEmpty(dsinfo)){ + // add by zg 增加对
的处理 begin + dsinfo = dsinfo.replaceAll("
", "\n"); + // add by zg 增加对
的处理 end + if(dsinfo.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + dsinfo = StringUtil.substring(dsinfo,Constants.VARCHAR_MAX_LENGTH); + } + } + //--性能数据 + pdata = datas[14]; + if(StringUtils.isNotEmpty(pdata)){ + // add by zg 增加对
的处理 begin + pdata = pdata.replaceAll("
", "\n"); + // add by zg 增加对
的处理 end + if(pdata.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + pdata = StringUtil.substring(pdata,Constants.VARCHAR_MAX_LENGTH); + } + }else {//界面异常信息列表的状态信息显示为性能数据,所以如果性能数据为空,则取状态信息 + pdata = dsinfo; + } + } catch (Exception e) { + String s = "基本数据部分解析失败"+e.getMessage(); + logger.warn(s); + errorInfo.append(s); + } + //--基本信息解析 存在异常值 无须保存,返回错误信息 用于系统告警 + if(errorInfo.length() != 0){ + logger.error(" 第一步-1: 旧格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); + return null; + } + //-- 根据执行状态判断是否需要获取详细数据的信息 及报警信息 + if(state < 0){ //失败 报警only + delyFlag = false; + } + Calendar arriveTime = Calendar.getInstance(); + detectInfo = new Object[33]; + //detectInfo[DetectInfo.null] = (CSVUtils.csvBytesPrinter1(strsList, Constants.COMMON_TEXT_CODING)); + detectInfo[DetectInfo.SEQID] = (seqId); + detectInfo[DetectInfo.SETINFOID] = (setInfoId); + detectInfo[DetectInfo.STARTTIME] = (startTime); + detectInfo[DetectInfo.DELAYTIME] = (delayTime); + detectInfo[DetectInfo.CHECKTIME] = (checkTime); + detectInfo[DetectInfo.CURRENTTIMES] = (currentTimes); + detectInfo[DetectInfo.NEXTCHECKTIME] = (nextCheckTime); + detectInfo[DetectInfo.CHECKTYPE] = (checkType); + detectInfo[DetectInfo.PDATA] = (pdata); + detectInfo[DetectInfo.ARRIVETIME] = (arriveTime.getTimeInMillis()); + detectInfo[DetectInfo.STATE] = (state); + detectInfo[DetectInfo.PLEVEL] = (pl); + detectInfo[DetectInfo.DSINFO] = (dsinfo); + detectInfo[DetectInfo.PROCESS] = process; + + /** + * 储存详细数据前三个字段,恢复数据时使用 + */ + List showNumList = new LinkedList(); + List plevelList = new LinkedList(); + List alarmList = new LinkedList(); + + showNumList.add(showNums); + plevelList.add(pLevel); + alarmList.add(pValue); + + detectInfo[DetectInfo.SHOWNUMLIST] = (showNumList); + detectInfo[DetectInfo.PLEVELLIST] = (plevelList); + detectInfo[DetectInfo.ALARMLIST] = (alarmList); + + + /* 第二步:解析详细数据部分 */ + Map detail = new HashMap(); //详细数据集合 + Map tableMap = Common.getTableMap().get(checkType); + if(tableMap ==null || tableMap.size() == 0){ + logger.error(""+checkType+"无法获取表结构"); + delyFlag = false; + } + List> detaiList = null; //详细数据集合 + if(delyFlag){ + detaiList = new ArrayList>();//初始化详细数据集合 + int index = 15; //数据集合解析游标 + try { + //-- 详细信息整理 监测具体数据信息(每个字段一列)。 + int i = 0 ; + while(detail.size() < tableMap.size()){ + TableColumnsModel field = tableMap.get(i); + ++i; + if((i-1)>tableMap.size()){ + break; + } + if(field!=null){ + //当监测数据(index == datas.length)时,及有效监测数据已经读取完成,tableMap页无需增加空字段数据 + if(index == datas.length){ + detail.put(field.getFiledName(), null); + continue; + } + try { + String fileType = field.getFiledType().toUpperCase(); + if("DATE".equals(fileType)){ + if(StringUtils.isNumeric(datas[index])){//是数字型时间时格式化 + detail.put(field.getFiledName(), format.format(new Date(Long.parseLong(datas[index].trim())))); + }else{//字符型时间 2016-06-17 08:02:02 + detail.put(field.getFiledName(), datas[index].trim()); + } + }else{ + if("NUMBER".equals(fileType)){ + if (StringUtils.isBlank(datas[index]) + || StringUtil.isRationalNumber(datas[index])) { + detail.put(field.getFiledName(), datas[index].trim()); + } else if (datas[index].trim().contains(" days, ")) { + detail.put(field.getFiledName(), getLongTimeStr(datas[index].trim())); + } else { + throw new Exception("NUMBER数据格式错误!"); + } + }else{ + String length = fileType.replaceAll("VARCHAR2\\((\\d+)\\)", "$1"); + if(Integer.parseInt(length) < datas[index].trim().length()) { + throw new Exception("VARCHAR2数据格式错误!"); + } + detail.put(field.getFiledName(), datas[index].trim()); + } + } + } catch (Exception e) { + errorInfo.append(checkType+" 表字段集合信息解析失败;"); + errorInfo.append(field.getFiledName()).append("字段取值") + .append(datas[index]).append("发生错误;异常数据行:"); + for (String string : datas) { // 添加异常数据 + errorInfo.append(string).append(','); + } + errorInfo.deleteCharAt(errorInfo.length()-1); + //监测数据的详细信息如果和指定的字段类型不匹配,则抛弃此条监测数据,避免影响批量入库 + logger.error(" 第一步-1: 旧格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); + return null; + } + index++; + } + } + + detail.put("SEQ_ID", seqId); + detail.put("DETECTIONED_STATE", state+""); + detail.put("detection_set_info_id", setInfoId+""); + detail.put("data_check_time_digital", checkTime+""); // 测试时间 + detail.put("data_check_time", format.format(new Date(checkTime))); // 测试时间 + detail.put("data_arrive_time_digital", arriveTime.getTimeInMillis()+""); // 入库时间 + detail.put("data_arrive_time",format.format(arriveTime.getTime())); // 入库时间 + detaiList.add(detail); + } catch (Exception e) { + String s = "详细数据部分解析失败"+e.getMessage(); + logger.warn(s); + } + } + + int urgentLevel = EmailTypeConstants.URGENT_LATER; + + //NMSC 监测失败状态 程序设定为为紧急 + if(state == -1 ){ + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + urgentLevel = Constants.DETECT_FAIL_NMSC_POLICE_EMERGENT; + }else { + urgentLevel = Constants.DETECT_FAIL_NON_NMSC_POLICE_EMERGENT; + } + } + /* + *1.整理告警信息 + *2.判断告警级别 + */ + //根据告警设置,获得当前告警紧急状态:由于之前的处理是当监测状态或者告警级别发生改变时,才发送邮件和根据告警设置来确定是否紧急,当第一次入库就异常时,是否紧急的判断就有问题了, + String [] nums = showNums.split("\\|"); + String [] levels = pLevel.split("\\|"); + String [] values = pValue.split("\\|"); + List aList = Common.getAlarmInfoMap().get(setInfoId); + Iterator iteAlarmList = null; + String cpInfoTmp = ""; + StringBuffer alarmInfo = new StringBuffer(); + if(state == 0){ + int pl0 = 99; + for(int i = 0 ; i=Integer.parseInt(levels[i].trim())){ + pl0 = Integer.parseInt(levels[i].trim()); + //告警紧急级别随告警级别走 + if(pl0==Constants.LEVEL_OF_EMERGENCY) { + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + }else { + urgentLevel = EmailTypeConstants.URGENT_LATER; + + } + } + } catch (Exception e) + { + logger.error("旧格式监测数据-修改正告警级别时,解析告警level:"+levels[i]+" 异常", e); + } + /** 字段名称 intDate 实际值: 80》60%(临界值) 级别 40 **/ + String filedName = ""; + String filedComments = ""; + if(tableMap !=null){ + TableColumnsModel tm = tableMap.get(elem.getShowNum()); + filedName = tm==null?"":tm.getFiledName(); + filedComments = tm==null?"":tm.getFiledComments(); + } + + cpInfoTmp += " "+(StringUtils.isEmpty(elem.getFiledCommonts())?filedName:elem.getFiledCommonts()); + cpInfoTmp += " 为:"+detail.get(filedName); + cpInfoTmp += ""+(elem.getPoliceUnit()==null ? "" : "null".equals(elem.getPoliceUnit()) ? "" : elem.getPoliceUnit()); + cpInfoTmp += " 报警级别:"+levels[i]; + cpInfoTmp += " \n"; + } + } + //如果是握手监测,不添加告警设置字段相关信息 + if(!Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + alarmInfo.append(cpInfoTmp); + } + } + } + + detectInfo[DetectInfo.ALARMINFO] = (alarmInfo.toString()); + detectInfo[DetectInfo.URGENTLEVEL] = (urgentLevel); + detectInfo[DetectInfo.DELYFLAG] = (delyFlag);//用于邮件发送 + detectInfo[DetectInfo.DETAILS] = (detaiList);//用于批量入库详细信息 + + //特殊追加信息解析--systeminfo-disk、net + //-- 特殊信息保存整理 + if(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType) ) { + while (strsIte.hasNext()) { + String [] detailsArr = strsIte.next(); //取出 + if(detailsArr == null || detailsArr.length ==0){ //空行不解析 + continue; + } + if(detailsArr.length != 2){ //不是[details:0]格式无法解析 + break; + } + + if("disk".equalsIgnoreCase(detailsArr[0])){ + List infoList = new ArrayList(); + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + disk:while (strsIte.hasNext()) { + endNum--; + String [] detailsArr0 = strsIte.next(); //取出 + int len = detailsArr0.length; + String[] diskInfo = new String[8]; + diskInfo[1] = len>0 ? detailsArr0[0]:"";//盘符 + diskInfo[2] = len>1 ? detailsArr0[1]:"";//大小 + diskInfo[3] = checkTime+"";//监测时间(long) + diskInfo[4] = format.format(new Date(checkTime));//监测时间(date) + diskInfo[5] = arriveTime.getTimeInMillis()+"";//录入记录时间(当前时间 long) + diskInfo[6] = format.format(arriveTime.getTime());//录入记录时间(当前时间 date) + diskInfo[7] = setInfoId; + infoList.add(diskInfo); + if(endNum==0){ + logger.debug("disk解析完成 跳出循环"); + break disk; + } + } + if(infoList.size()>0) { + detectInfo[DetectInfo.DISKINFOLIST] = (infoList); + } + }else if("net".equalsIgnoreCase(detailsArr[0])) { + List infoList = new ArrayList(); + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + net:while (strsIte.hasNext()) { + endNum--; + String [] netInfoTmp = strsIte.next(); //取出 + int len = netInfoTmp.length; + String[] netInfo = new String[13]; + //netInfo[0] = id; + netInfo[1] = len>0 ? netInfoTmp[0]:"";//网卡名称 + netInfo[2] = len>1 ? netInfoTmp[1]:"";//网卡状态 + netInfo[3] = len>2 ? netInfoTmp[2]:"";//网卡带宽 + netInfo[4] = len>3 ? netInfoTmp[3]:"";//ip + netInfo[5] = len>4 ? netInfoTmp[4]:"";//子网掩码 + netInfo[6] = len>5 ? netInfoTmp[5]:"";//网关 + netInfo[7] = len>6 ? netInfoTmp[6]:"";//NET_MAC + netInfo[8] = format.format(new Date(checkTime));//监测时间(date) + netInfo[9] = format.format(arriveTime.getTime());//录入记录时间(当前时间 date) + netInfo[10] = checkTime+"";//监测时间(long) + netInfo[11] = arriveTime.getTimeInMillis()+"";//录入记录时间(当前时间 long) + netInfo[12] = setInfoId;// id + infoList.add(netInfo); + if(endNum ==0){ + break net; + } + } + if(infoList.size()>0) { + detectInfo[DetectInfo.NETINFOLIST] = (infoList); + } + } + } + } + + } + return detectInfo; + } + + public String resoveErrorInfo(List neiList) throws SQLExecuteTimeoutException,SQLException{ + String errorInfo = null; + String insertSql = "insert into nms_error_info (error_code,error_time,errort_getip,errort_ip,error_state,error_des) values (?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,?,?)"; + String updateSql = "UPDATE nms_error_info SET error_state=?,state_update_time=to_date(?,'yyyy-mm-dd hh24:mi:ss') WHERE error_state=1 AND error_code=? AND error_des = ? AND error_time<=to_date(?,'yyyy-mm-dd hh24:mi:ss') and errort_getip=? and errort_ip=?"; + + List paramsList = new LinkedList(); + List paramsListForUpdate = new LinkedList(); + if(neiList != null && neiList.size()>0){ + for(NmsErrorInfo nei : neiList){ +// if(){check info} +// errorInfo=""; + Long stateTmp = nei.getErrorState(); + if(stateTmp!=null && stateTmp==1l) {//2:恢复,1:异常,0:已解决 + String[] params = new String[] { + nei.getErrorCode(), + format.format(nei.getErrorTime()), + nei.getErrortGetip(), + nei.getErrortIp(), + "1", + nei.getErrortDesc()}; + paramsList.add(params); + }else { + String[] params = new String[] { + (nei.getErrorState() == null ? 0 : nei.getErrorState().intValue())+ "", + format.format(nei.getErrorTime()), + nei.getErrorCode(), + nei.getErrortDesc(), + format.format(nei.getErrorTime()), + nei.getErrortGetip(), + nei.getErrortIp()}; + paramsListForUpdate.add(params); + + } + } + + try { + //以下2个批量更新槽位,执行的顺序不可更改(要先插入异常的信息,再插入恢复的信息) + dao.dbUpdateByBatch(insertSql, paramsList);//要先插入异常的信息 + dao.dbUpdateByBatch(updateSql, paramsListForUpdate);//再插入恢复的信息 + } catch (SQLExecuteTimeoutException e) { + dao.rollback(); + throw e; +// } catch (SQLException e) { +// e.printStackTrace(); + }finally { + dao.clearConn(); + } + } + return errorInfo; + } + /** + * "102 days, 8:33:24.19"格式时间转毫秒整形 + * @time Nov 28, 2012-1:55:29 PM + * @param data + * @return + */ + public static String getLongTimeStr (String data) { + String[] da = data.split(" days, "); + if(da.length>1){ + try { + long date = Long.parseLong(da[0].trim())*24*60*60*1000; //天-->毫秒 + //System.out.println(da[0].trim()+" "+date); + String [] da2 = da[1].split(":"); + if(da2.length>=1){ + date += Long.parseLong(da2[0].trim())*60*60*1000; + //System.out.println(da2[0].trim()+" "+(Long.parseLong(da2[0].trim())*60*60*1000)); + } + if(da2.length>=2){ + date += Long.parseLong(da2[1].trim())*60*1000; + //System.out.println(da2[1].trim()+" "+(Long.parseLong(da2[1].trim())*60*1000)); + } + if(da2.length>=3){ + date += Double.parseDouble(da2[2].trim())*1000; + //System.out.println(da2[2].trim()+" "+(Double.parseDouble(da2[2].trim())*1000)); + } + return date+""; + } catch (Exception e) { + e.printStackTrace(); + return data; + } + }else { + return data; + } + } + + public List getNodeModelListBySetInfo(SetInfo setInfo) { + + List ipList = new LinkedList(); + //- 单节点 监测 + if(StringUtils.isNotBlank(setInfo.getNodeIp2())){ + ipList.add(new NodeModel(setInfo.getNodeIp2(),getNodeTypeByIp(setInfo.getNodeIp2()))); + }else + + //- 节点IDs 监测 + if(StringUtils.isNotBlank(setInfo.getNodeIpsId())){ +// ipList.addAll(getNodeModelByIds(setInfo.getNodeIpsId()));//根据ip的id获得节点信息-未对ip去重 + ipList.addAll(getNodesByIds(setInfo.getNodeIpsId()));//根据ip的id获得节点信息 + }else + + //- 节点组 监测 + if(StringUtils.isNotBlank(setInfo.getNodeGroupsId())){ +// ipList = getNodeModelByGroupIds(setInfo.getNodeGroupsId());//根据节点组id获得节点信息-未对ip去重 + ipList = getNodesByGroupIds(setInfo.getNodeGroupsId());//根据节点组id获得节点信息 + } + else {//- 全局监测 + ipList = getAllEffectiveNodeInfo(); + } + + return ipList; + } + + + public JSONObject agentInit(String ip) { + + if(StringUtils.isEmpty(ip)){//无效ip不做操作 + return null; + } + JSONObject jsonObj = new JSONObject(); + try { + //校验上传目录是否存在 + + //1、settings + try { + + boolean agentFlag = true; + + //SetInfo + List setInfoList = selectSetInfoList(1l, null, Common.getIpSeqIdMap().get(ip), null, null, 1l, null, 0l,agentFlag); + + //告警 + List alarmInfoList = selectAlarmInfoBySetIfoList(setInfoList); + + //是否启用主动告警 + Boolean startAutoAlarm = selectIsStartAutoAlarm(); + + //填写Socket数据 + try { + jsonObj.put("number", Calendar.getInstance().getTimeInMillis()); + jsonObj.put("showAutoAlarm", startAutoAlarm); + jsonObj.put("setInfo", setInfoList); + // XXX + System.out.println("*****"); + System.out.println(jsonObj); + System.out.println("*****"); + jsonObj.put("alarmInfo", alarmInfoList); + } catch (JSONException e) { + logger.error("",e); + } + } catch (Exception e1) { + logger.error("",e1); + } + } catch (Exception e) { + logger.error("Socket Agent initialization operation 》"+ip+"appears exceptions:"+"",e); + return null; + }finally{ + logger.debug("Socket Agent初始化操作 结束"); + dao.clearConn(); + } + + //- 同步清理 变更记录表信息 + String sql = "delete from event_record_library where record_type='S2C' and seq_ids=?"; + try { + dao.dbUpdate(sql,Common.getIpSeqIdMap().get(ip)); + } catch (Exception e) { + logger.error("同步清理 变更记录表信息 "+ip+" 出现异常",e); + } + return jsonObj; + } + public List selectAlarmInfoBySetIfoList(List setInfoList) { + if(setInfoList==null || setInfoList.size()==0){//无效ip不做操作 + return null; + } + ArrayList alarmInfoList = new ArrayList(); + try { + try { + ArrayList fields = new ArrayList(); + ArrayList> mapList = null; + Iterator siIte = setInfoList.iterator(); + StringBuffer ipSb = new StringBuffer("0"); + while (siIte.hasNext()) { + SetInfo si = (SetInfo) siIte.next(); + ipSb.append(","+si.getId()); + } + //告警 + fields.clear(); + fields.add("id"); + fields.add("detection_set_info_id"); + fields.add("check_type_name"); + fields.add("process_iden"); + fields.add("metadata_id"); + fields.add("show_num"); + fields.add("filed_comments"); + fields.add("police_state"); + fields.add("police_value"); + fields.add("police_unit"); + fields.add("police_symbols"); + fields.add("police_level"); + fields.add("marker"); + fields.add("MARKER_FILED_ID"); +// mapList.clear(); + mapList = dao + .dbSelect( + "select dpr.id,dpr.detection_set_info_id,cti.check_type_name,dsi.process_iden,dpr.metadata_id,md.show_num,md.filed_comments,dpr.police_state,dpr.police_value,dpr.police_unit,dpr.police_symbols,dpr.police_level,dpr.marker,dpr.MARKER_FILED_ID " + + "from data_police_relation dpr " + + "left join metadata md on md.id = dpr.metadata_id " + + "left join detection_set_info dsi on dsi.id = dpr.detection_set_info_id " + + "left join check_type_info cti on cti.id = dsi.check_type_id " + + "where dpr.detection_set_info_id in ("+ipSb+") " + + "and dpr.police_state=0 " + + "and lower(cti.check_type_name) not in(lower('"+PING_STR+"'),lower('"+NMSC_STR+"'))" + + "order by md.show_num, DPR.POLICE_LEVEL ASC", + fields); + + if(mapList!=null && mapList.size()>0){ + Map metadataIdShownumMap= selectAllMetadata(); + for(Map map : mapList){ + AlarmInfo info = new AlarmInfo(); + info.setId(StringUtils.isEmpty(map.get("id"))?null:Long.valueOf(map.get("id"))); + info.setSetInfoId(StringUtils.isEmpty(map.get("detection_set_info_id"))?null:Long.valueOf(map.get("detection_set_info_id"))); + info.setCheckType(map.get("check_type_name")); + info.setProcessIden(map.get("process_iden")); + info.setMetadataId(StringUtils.isEmpty(map.get("metadata_id"))?null:Long.valueOf(map.get("metadata_id"))); + info.setShowNum(StringUtils.isEmpty(map.get("show_num"))?null:Integer.valueOf(map.get("show_num"))); + info.setFiledCommonts(map.get("filed_comments")); + info.setPoliceState(map.get("police_state")); + info.setPoliceValue(map.get("police_value")); + info.setPoliceUnit(map.get("police_unit")); + info.setPoliceSysmbols(map.get("police_symbols")); + info.setPoliceLevel(StringUtils.isEmpty(map.get("police_level"))?null:Integer.valueOf(map.get("police_level"))); + info.setMarker(map.get("marker")); + info.setMarkerFiledId(StringUtils.isEmpty(map.get("MARKER_FILED_ID"))?null:Integer.valueOf(map.get("MARKER_FILED_ID"))); + info.setMarkerFiledShowNum(metadataIdShownumMap.get(StringUtils.isEmpty(map.get("MARKER_FILED_ID"))?null:Integer.valueOf(map.get("MARKER_FILED_ID")))); + alarmInfoList.add(info); + } + } + } catch (Exception e1) { + logger.error("",e1); + }finally{ + dao.clearConn(); + } + //填写Socket数据 + return alarmInfoList; + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return null; + } + + //false:不显示主动告警;true:显示主动告警 + public Boolean selectIsStartAutoAlarm() { + Boolean startAutoAlarm = false;//默认不显示 + try { + ArrayList fields = new ArrayList(); + ArrayList> mapList = null; + + fields.clear(); + fields.add("type_state"); + mapList = dao.dbSelect( + "select t.type_state from type_table t where t.type_identity='showAutoAlarm'", + fields); + + if(mapList!=null && mapList.size()>0){ + String startAutoAlarmStr = mapList.get(0).get("type_state"); + if("0".equals(startAutoAlarmStr)) {//0:启用;1:停用 + startAutoAlarm = true; + } + } + } catch (Exception e1) { + logger.error("",e1); + }finally{ + dao.clearConn(); + } + return startAutoAlarm; + } + + /** + * 查询metadata表所有记录,返回id:showNum的map + * @author Administrator May 20, 2014 + * @version 1.0 + * @return + */ + public Map selectAllMetadata() { + Map metadataList = new HashMap(); + try { + ArrayList> mapList = null; + + ArrayList fields = new ArrayList(); + fields.clear(); + fields.add("id"); + fields.add("show_num"); + mapList = dao.dbSelect( + " select md.id,md.show_num " + + " from metadata md ", + fields); + + if(mapList!=null && mapList.size()>0){ + for(Map map : mapList){ + if(StringUtils.isNotBlank(map.get("id")) + && StringUtils.isNotBlank(map.get("show_num"))) { + metadataList.put(Integer.valueOf(map.get("id")), Integer.valueOf(map.get("show_num"))); + } + } + } + } catch (Exception e1) { + logger.error("Querying all records in the metadata table, returning the map exception of id:showNum",e1); + }finally{ + dao.clearConn(); + } + return metadataList; + } + /** + * + * @time Aug 31, 2011-3:55:15 PM + */ + public boolean newClientCheck(String ip,Integer snmpVersion){ + + try { + //检查或创建上传目录 + /*File file = new File(Constants.DATA_DIR+File.separator+ip); + if (!file.exists()) { + file.mkdirs(); + }*/ + + //检查或创建临时IP信息 + Long ss = Common.getIpSeqIdMap().get(ip); + if(ss != null){//已存在 + return false;//跳过 + } + + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("snmp_version"); + List> list = dao.dbSelect("select nt.node_ip,nt.seq_id,nt.snmp_version from node_table nt where nt.node_ip='"+ip+"' ", fields); + if(list!=null && list.size()>0){ //已存在 将节点添加到Map + if(StringUtils.isNotEmpty(list.get(0).get("seq_id"))){ + + Common.getIpSeqIdMap().put(ip, Long.parseLong(list.get(0).get("seq_id"))); + Common.putSimpleNode(Long.parseLong(list.get(0).get("seq_id")), new SimpleNode(list.get(0).get("node_ip"),StringUtils.isNotEmpty(list.get(0).get("snmp_version"))?Integer.parseInt(list.get(0).get("snmp_version")):null)); + + return true; + } + } + logger.info("发现新节点:> "+ip+" 临时数据保存"); + Long groupId = getDefaultNodeGroup(); + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + Long seqId = getSeqIdIpByNodeIp(ip); + Map info = new HashMap(); + info.put("node_id", ""); + info.put("node_ip", ip); + info.put("node_type", "0"); +// info.put("node_name", "新节点_"+Calendar.getInstance().getTimeInMillis()); + info.put("node_name", "New node_"+Calendar.getInstance().getTimeInMillis()); + info.put("node_state", "0"); + info.put("NODE_GROUP_ID", groupId+""); + info.put("node_createtime", format.format(Calendar.getInstance().getTime())); + info.put("is_temp", "0"); + info.put("seq_id", seqId+""); + info.put("ipn", IpCovert.ipToLong(ip)+""); + dao.insertObj("node_table", info); + dao.commit(); + Common.getIpSeqIdMap().put(ip, seqId); + Common.putSimpleNode(seqId, new SimpleNode(ip,snmpVersion)); //- 此处业务上是否存在问题 snmp_version 为空 + return true; + } catch (Exception e) { + logger.error("",e); + return false; + }finally{ + dao.clearConn(); + } + } + + /** + * 获取 默认节点组 ID + * 用于新节点注册入库时关联到该节点组 + * @time Jan 7, 2013-3:46:31 PM + * @return + */ + public Long getDefaultNodeGroup(){ + String nodeGroupName = Constants.DEFAULT_NODE_GROUP_NAME; + Long groupId = null; + StringBuffer ngtSql = new StringBuffer("select ngt.group_id from nodegroup_table ngt where ngt.group_name='"+nodeGroupName+"' and ngt.is_valid ='1' and nvl(ngt.group_id,-999)<>-999 order by ngt.group_id asc"); + ArrayList fields = new ArrayList(); + fields.add("group_id"); + try { + ArrayList> mapsList = dao.dbSelect(ngtSql.toString(),fields); + if(mapsList!=null && mapsList.size()>0){ + String groupIdStr = mapsList.get(0).get("group_id"); + groupId = StringUtils.isBlank(groupIdStr)?null:Long.parseLong(groupIdStr); + } + + //节点组ID为空时,生成新的默认节点组, + if(groupId==null){ + Map info = new HashMap(); + info.put("GROUP_NAME", nodeGroupName); + info.put("VIEW_LEVEL", "4"); + info.put("GROUP_TYPE", "0"); +// info.put("GROUP_DESC", "存放自动注册的新节点信息的默认节点组"); +// info.put("GROUP_DESC", "Default node group for automatically registering new node information"); + info.put("GROUP_DESC", "i18n_server.CommonService.saveDefault_n81i"); + info.put("IS_VALID", "1"); + info.put("GROUP_CREATETIME", format.format(Calendar.getInstance().getTime())); + info.put("PARENT_GROUP_ID", "0"); + info.put("LEAF_GROUP", "1"); + info.put("GROUP_LEVEL", "1"); + info.put("SHOW_INDEX", "0"); + dao.insertObj("nodegroup_table", info); + + mapsList = dao.dbSelect(ngtSql.toString(),fields); + if(mapsList!=null && mapsList.size()>0){ + String groupIdStr = mapsList.get(0).get("group_id"); + groupId = StringUtils.isBlank(groupIdStr)?null:Long.parseLong(groupIdStr); + } + } + + } catch (Exception e) { + logger.error("查询 默认节点组异常",e); + } + return groupId; + } + + public void updateSeqIdByNodeIp(Map map,String nodeIp){ + StringBuffer sql = new StringBuffer(); + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + fields.add("seq_id"); + sql.append("select distinct nt.node_ip,nt.seq_id from node_table nt where 1=1 "); + if(StringUtils.isNotEmpty(nodeIp)){ + sql.append("and nt.node_ip='"+nodeIp+"'"); + } + try { + ArrayList> mapsList = this.dao.dbSelect(sql.toString(), fields); + if(mapsList!=null && mapsList.size()>0){ + for (Map obj : mapsList) { + if(StringUtils.isNotEmpty(obj.get("node_ip")) && StringUtils.isNotEmpty(obj.get("seq_id"))){ + map.put(obj.get("node_ip"), Long.parseLong(obj.get("seq_id"))); +// map.put(Long.parseLong(obj.get("seq_id")), new String[]{obj.get("node_ip"),obj.get("snmp_version")}); + } + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + public void updateUserPwdByNodeIp(Map ipNodeMap,String nodeIp) { + logger.debug("更新节点用户名密码信息。。。"); + StringBuffer sql= new StringBuffer(); + sql.append("select distinct NODE_IP,NODE_USERNAME,NODE_PASSWORD,NODE_SYSTEM_TYPE from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' and t.node_type='0' "); + if(StringUtils.isNotBlank(nodeIp)){ + sql.append("and t.node_ip='"+nodeIp+"'"); + }else { + sql.append(getIpnSelect());//不指定IP时,只查询当前DC管理的节点信息 + } + ArrayList fields = new ArrayList(); + fields.add("NODE_IP"); + fields.add("NODE_USERNAME"); + fields.add("NODE_PASSWORD"); + fields.add("NODE_SYSTEM_TYPE"); + try { + List> sid = dao.dbSelect(sql.toString(), fields); + if(sid!=null&&sid.size()>0){ + BASE64Decoder decoder = new BASE64Decoder(); + for(Map map : sid){ + String[] ipInfo = new String[3]; + ipInfo[0] = map.get("NODE_USERNAME"); + String passward = map.get("NODE_PASSWORD"); + if(StringUtils.isNotBlank(passward)){ + passward = new String(decoder.decodeBuffer(passward)); + ipInfo[1] = passward; + } + ipInfo[2] = map.get("NODE_SYSTEM_TYPE"); + ipNodeMap.put(map.get("NODE_IP"), ipInfo); + } + } + } catch (SQLException e) { + e.printStackTrace(); + logger.error("Query node username and password failed",e); + } catch (IOException e) { + e.printStackTrace(); + logger.error("Query node username and password failed",e); + }finally{ + if(dao != null){ + dao.clearConn();//清空连接,但是不关闭 + } + } + + } + public void updateSimpleNodeByUUID(Long seqId){ + StringBuffer sql = new StringBuffer(); + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + fields.add("seq_id"); + fields.add("snmp_version"); + sql.append("select distinct nt.node_ip,nt.seq_id,nt.snmp_version from node_table nt where 1=1 "); + if(seqId != null){ + sql.append("and nt.seq_id='"+seqId.longValue()+"'"); + } + try { + ArrayList> mapsList = this.dao.dbSelect(sql.toString(), fields); + if(mapsList!=null && mapsList.size()>0){ + for (Map obj : mapsList) { + if(StringUtils.isNotEmpty(obj.get("node_ip")) && StringUtils.isNotEmpty(obj.get("seq_id"))){ + Common.putSimpleNode(Long.parseLong(obj.get("seq_id")), new SimpleNode(obj.get("node_ip"),StringUtils.isNotEmpty(obj.get("snmp_version"))?Integer.parseInt(obj.get("snmp_version").trim()):null)); + }else{ + long aa = getSeqIdIpByNodeIp(obj.get("node_ip")); + dao.dbUpdate("update node_table nt set nt.seq_id='"+aa+"' where nt.node_ip='"+obj.get("node_ip")+"'"); + } + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + private Long getSeqIdIpByNodeIp (String nodeIp) { + ArrayList fields = new ArrayList(); + fields.add("node_ip"); + fields.add("seq_id"); + List> list; + try { + list = dao.dbSelect("select nt.node_ip,nt.seq_id from node_table nt where nt.node_ip='"+nodeIp+"' ", fields); + if(list!=null && list.size()>0){ //已存在 + if(StringUtils.isNotEmpty(list.get(0).get("seq_id"))){ + return Long.parseLong(list.get(0).get("seq_id")); + }else{ + fields.remove("node_ip"); + list = dao.dbSelect("select seq_seq_id.nextval seq_id from dual", fields); + return Long.parseLong(list.get(0).get("seq_id"));//跳过 + } + }else{ + fields.remove("node_ip"); + list = dao.dbSelect("select seq_seq_id.nextval seq_id from dual", fields); + return Long.parseLong(list.get(0).get("seq_id"));//跳过 + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return null; + } + + /** + * 初始化DC 自身参数 + * @time Jan 8, 2013-1:11:33 PM + * @param dao + * @return + */ + public boolean initServerParams() { + + String localIp = Common.getLocalIp(); + + //无法获取 + if(StringUtils.isEmpty(localIp)){ + logger.error("Failure to read local IP update DC parameter information"); + return false; + } + + try { + ServerTable serverTable = getNMSServerParams(localIp); + if(serverTable!=null + && serverTable.getId() !=null + && serverTable.getIpSegList() != null + && serverTable.getIpSegList().size() >0){ + Common.setServerTable(serverTable); + logger.info("DC 参数信息更新"); + return true; + }else{ + return false; + } + } catch (Exception e) { + logger.error("DC 参数信息查询失败",e); + return false; + }finally{ + dao.clearConn(); + } + } + + //统计 监测数据 或者 告警数据 表: 监测设置<->已有有效数据的seqid + public Map getDetectSeq(String tableName){// detection_info_new + String querySeqidSql="select distinct t.seq_id from node_table t left join nodegroup_table ngt on ngt.group_id = t.node_group_id where t.node_state='0' and ngt.is_valid ='1' and t.node_type='0' "+" "+getIpnSelect(); + String sql = "select t.detection_set_info_id detSetId ,t.seq_id seqIds" + + " from ("+querySeqidSql+") s left join "+tableName+" t on t.seq_id = s.seq_id " +//只更新本dc管理的监测数据 + " where t.valid=1" ;//"只查询有效的记录 + Map detectSetSeqIdsMap = new HashMap(); + ArrayList fields = new ArrayList(); + fields.add("detSetId"); + fields.add("seqIds"); + try { + ArrayList> mapsList = dao.dbSelectByFetchSize(sql,fields); + if(mapsList!=null && mapsList.size()>0){ + Map tempMap = new HashMap(); + for (Map obj : mapsList) { + String detSetId = obj.get("detSetId"); + String seqId = obj.get("seqIds")==null?"":obj.get("seqIds"); + if(tempMap.get(detSetId)!=null){ + String ids = tempMap.get(detSetId); + if(!ids.contains(","+seqId+",")){ + tempMap.put(detSetId, ids+seqId+","); + } + }else{ + if(!StringUtils.isBlank(detSetId)) { + tempMap.put(detSetId,","+seqId+","); + } + } + } + for (Map.Entry entry : tempMap.entrySet()) { + if(entry.getValue().length()>1){ + String val = entry.getValue().substring(1, entry.getValue().length()-1); + String[] arr = val.split(","); + Arrays.sort(arr); + detectSetSeqIdsMap.put(entry.getKey(),arr); + } + } + } + } catch (Exception e) { + logger.error("In the statistical monitoring data table, monitoring settings: SEQID anomalies with valid data",e); + } + return detectSetSeqIdsMap; + } + + //查询节点组非空的有效监测设置信息 + public Map getDetectSetInfo(){ + String sql = " select dsi.id id,dsi.node_groups_id nodeGroupsId,dsi.node_ips_id nodeIpsId" + + " from detection_set_info dsi" + + " where dsi.node_groups_id is not null " + + " and dsi.detection_set_state='1'"; + Map detectSetSeqIdsMap = new HashMap(); + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("nodeGroupsId"); + fields.add("nodeIpsId"); + try { + ArrayList> mapsList = dao.dbSelect(sql,fields); + if(mapsList!=null && mapsList.size()>0){ + Map allData = this.getAllNodeTableData(); + //获取所有节点组对应的seq_id + Map> groupSeq =(Map>) allData.get("group"); + //获取所有 节点 对应的seq_id + Map nodeSeq = (Map) allData.get("node"); + for (Map obj : mapsList) { + String id = obj.get("id"); + String nodeIpsId = obj.get("nodeIpsId"); + String nodeGroupsId = obj.get("nodeGroupsId"); + String seqIds = this.getDetecSetSeq(nodeGroupsId, nodeIpsId, groupSeq, nodeSeq); + detectSetSeqIdsMap.put(id, seqIds); + } + } + + } catch (Exception e) { + logger.error("Invalid non valid monitoring settings information for query node group",e); + } + return detectSetSeqIdsMap; + } + /** + * 获取 指定的 nodeIds 或 nodeGroupIds 对应的 seq_id eg:21,23,34 + * @param nodeGroupsId + * @param nodeIpsId + * @param groupSeq + * @param nodeSeq + * @return seq_id eg:21,23,34... + */ + public String getDetecSetSeq(String nodeGroupsId,String nodeIpsId,Map> groupSeq,Map nodeSeq){ + StringBuilder result = new StringBuilder(); + if(StringUtils.isNotBlank(nodeIpsId)){//如果指定nodeId ,则查找对应的seqId + String[] nodeIds = nodeIpsId.split(","); + for(String temp :nodeIds){ + if(StringUtils.isNotBlank(nodeSeq.get(temp.trim()))){ + result.append(","+nodeSeq.get(temp.trim())); + } + } + }else if (StringUtils.isNotBlank(nodeGroupsId)) {//如果没有指定nodeId ,指定nodeGroupId则查找节点组下的所有seqId + String[] groupIds = nodeGroupsId.split(","); + for(String temp :groupIds){ + List tempList = groupSeq.get(temp.trim()); + if(tempList != null && tempList.size() > 0){ + for(String tempSeqId : tempList){ + result.append(","+tempSeqId); + } + } + } + }else{//nodeId 和nodegroupId 都没有则 查找所有 seqId + for(Map.Entry temp : nodeSeq.entrySet()){ + result.append("," + temp.getValue()); + } + } + if(result.length()>1){ + result.deleteCharAt(0);//删除第一个"," + } + return result.toString(); + } + + /** + * 查询node_table表的所有数据,组织成nodeid --seqId 和 groupid -- seqId 两个map + * @return + */ + public Map getAllNodeTableData(){ + Map result = null; + Map nodeSeq = null; + Map> groupSeq = null; + /* + * 根据节点id和节点组id,获得有效的seqId,并用逗号连接:1,2,3,5 + * 1. 去掉对节点是否有效的过滤: + * 因为如果当前监测设置包含节点A,而此时将节点A下线,此时DC端录入监测数据时, + * 就会将所有的数据置为无效,而当上线节点后,再去查看,节点仍是无效, + * 因为在节点上线和下线操作时,不对监测数据的有效性进行修改 + * 2. 如果节点下线后,又上线了,此时只会显示新录入的监测数据,之前的旧数据就不会显示了??要想显示之前旧的数据,只能从web端先去掉下线过的节点,再添加上 + */ + String sql = "select nt.node_id nodeId , nt.seq_id seqId ,nt.node_group_id nodeGroupId from node_table nt "; + ArrayList fields = new ArrayList(); + fields.add("nodeId"); + fields.add("seqId"); + fields.add("nodeGroupId"); + try { + List> mapList = dao.dbSelect(sql,fields); + if(mapList != null && mapList.size() > 0){ + nodeSeq = new HashMap(); + groupSeq = new HashMap>(); + for(Map temp : mapList){ + String nodeId = temp.get("nodeId"); + String seqId = temp.get("seqId"); + String nodeGroupId = temp.get("nodeGroupId"); + nodeSeq.put(nodeId, seqId);//nodeId -- seqId + if(StringUtils.isNotBlank(nodeGroupId)){ + if(groupSeq.get(nodeGroupId) == null){//第一次添加 + List tempList = new ArrayList(); + tempList.add(seqId); + groupSeq.put(nodeGroupId, tempList); + }else{ + groupSeq.get(nodeGroupId).add(seqId); + } + } + } + result = new HashMap(); + result.put("node", nodeSeq); + result.put("group", groupSeq); + } + } catch (Exception e) { + logger.error("Query all record exceptions of the node_table table",e); + } + return result; + } + + + /** + * 获取所有节点组id 对应的节点seq_Id + * @return + */ + public Map> getAllNodeGroupSeqId(){ + Map> result = null; + String sql = "select ngt.group_id groupId, nt.seq_id seqId from node_table nt left join nodegroup_table ngt on ngt.group_id = nt.node_group_id order by ngt.group_id"; + ArrayList fields = new ArrayList(); + fields.add("groupId"); + fields.add("seqId"); + try { + ArrayList> mapsList = dao.dbSelect(sql,fields); + String nodeGroupId = null; + String seqId = null; + List temp = null; + if(mapsList != null && mapsList.size() > 0){ + result = new HashMap>(); + for (Map obj : mapsList) { + nodeGroupId = obj.get("groupId"); + seqId = obj.get("seqId"); + temp = result.get(nodeGroupId); + if(temp != null){ + temp.add(seqId); + }else{ + temp = new ArrayList(); + temp.add(seqId); + result.put(nodeGroupId, temp); + } + temp = null; + } + } + } catch (Exception e) { + logger.error("Query the node seqid exception corresponding to all node groups",e); + } + return result; + } + /** + * 获取所有 nodeId 对应的 seqID + * @return + */ + public Map getAllNodeIdSeqId(){ + Map result = null; + String sql = "select nt.node_id nodeId , nt.seq_id seqId from node_table nt order by nt.node_id"; + ArrayList fields = new ArrayList(); + fields.add("nodeId"); + fields.add("seqId"); + try { + ArrayList> mapsList = dao.dbSelect(sql,fields); + String nodeId = null; + String seqId = null; + if(mapsList != null && mapsList.size() > 0){ + result = new HashMap(); + for (Map obj : mapsList) { + nodeId = obj.get("nodeId"); + seqId = obj.get("seqId"); + result.put(nodeId, seqId); + } + } + } catch (Exception e) { + logger.error("Query the node seqid exception corresponding to all node groups",e); + } + return result; + } + + /* + * 根据节点id和节点组id,获得有效的seqId,并用逗号连接:1,2,3,5 + * 1. 去掉对节点是否有效的过滤: + * 因为如果当前监测设置包含节点A,而此时将节点A下线,此时DC端录入监测数据时, + * 就会将所有的数据置为无效,而当上线节点后,再去查看,节点仍是无效, + * 因为在节点上线和下线操作时,不对监测数据的有效性进行修改 + * 2. 如果节点下线后,又上线了,此时只会显示新录入的监测数据,之前的旧数据就不会显示了??要想显示之前旧的数据,只能从web端先去掉下线过的节点,再添加上 + */ + public String getValidSeq(String ipIds,String groupIds) { + String seqIds = ""; + try{ + String sql = ""; + if(ipIds!=null && !"".equals(ipIds.trim())) {//节点非空 + sql=" select distinct t.seq_id seqIds" + + " from node_table t " + + " where t.node_id in ("+ipIds+") "; +// " and t.node_state='0' ";//有效节点 + + }else if(groupIds!=null && !"".equals(groupIds.trim())) {//节点空,节点组非空 + sql=" select distinct t.seq_id seqIds" + + " from node_table t " + + " where t.node_group_id in ("+groupIds+") "; +// " and t.node_state='0' ";//有效节点 + + }else {//全局节点-没有业务系统的限制 + sql=" select distinct t.seq_id seqIds" + + " from node_table t "; +// " where t.node_state='0' ";//有效节点 + } + + ArrayList fields = new ArrayList(); + fields.add("seqIds"); + ArrayList> mapsList = dao.dbSelect(sql,fields); + String str = ","; + for(Map map :mapsList){ + String seqId = map.get("seqIds"); + if(!StringUtils.isBlank(seqId)&&!str.contains(","+seqId+",")){ + str =str+seqId+","; + } + } + if(str.length()>1){ + seqIds = str.substring(1, str.length()-1); + }else{ + seqIds =""; + } + }catch(Exception e) { + logger.error("According to node ID and node group ID, get valid seqId, and use comma to connect exceptions",e); + } + return seqIds; + } + + /** + * 查询库表中所有的节点信息 + * @time Jan 8, 2013-1:11:33 PM + * @param dao + * @return + */ + /*public boolean initServerParams(CommonDao dao) { + CommonService service = new CommonService(dao); + String localIp = Common.getLocalIp(); + + //无法获取 + if(StringUtils.isEmpty(localIp)){ + return false; + } + + try { + ServerTable serverTable = service.getNMSServerParams(localIp); + if(serverTable!=null + && serverTable.getId() !=null + && serverTable.getIpSegList() != null + && serverTable.getIpSegList().size() >0){ + Common.setServerTable(serverTable); + return true; + }else{ + return false; + } + } catch (Exception e) { + logger.error("DC 参数信息查询失败",e); + return false; + } + }*/ +/* public static void main(String [] args) { + CommonDao dao = new CommonDao(); + CommonService service = new CommonService(dao); + service.newClientCheck("192.168.1.189", 3); + }*/ + /** + * detection_info addbatch + * @param id + * @param infoStmt + * @param temInfo + * @throws SQLException + */ + + public void addInfoStmt(long id,Object[] temInfo) + throws SQLException { + boolean useSequnce= Common.SEQUENCE.get(DetectInfo.DETECTION_INFO)==null?true:false; + //插入info表 + String insertInfoSql = ""; + if(useSequnce){ + insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " + + "PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " + + "POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " + + "DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT,SEQUENCE)" + +" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?,?)"; + }else{ + insertInfoSql = "INSERT INTO DETECTION_INFO (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " + + "PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " + + "POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " + + "DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT)" + +" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?)"; + } + + List infoParameter = new ArrayList(); + try{ + String[] params = new String[] { + id+"", + (String) temInfo[DetectInfo.SETINFOID], + (String) temInfo[DetectInfo.DSINFO], + (String) temInfo[DetectInfo.PDATA], + temInfo[DetectInfo.CURRENTTIMES]+"", + temInfo[DetectInfo.STARTTIME] != null ? format.format(new Date((Long)temInfo[DetectInfo.STARTTIME])) : null, + temInfo[DetectInfo.DELAYTIME]+"", + temInfo[DetectInfo.NEXTCHECKTIME] != null ? format.format(new Date((Long)temInfo[DetectInfo.NEXTCHECKTIME])) : null, + temInfo[DetectInfo.PLEVEL]+"", + temInfo[DetectInfo.CHECKTIME] != null ? format.format(new Date((Long)temInfo[DetectInfo.CHECKTIME])) : null, + temInfo[DetectInfo.ARRIVETIME] != null ? format.format(new Date((Long)temInfo[DetectInfo.ARRIVETIME])) : null, + temInfo[DetectInfo.STATE]+"", + temInfo[DetectInfo.CHECKTIME] +"", + temInfo[DetectInfo.ARRIVETIME]+"", + (String) temInfo[DetectInfo.SEQID], + temInfo[DetectInfo.URGENTLEVEL]+"", + Common.SEQUENCE.get(DetectInfo.DETECTION_INFO)==null?null:Common.SEQUENCE.get(DetectInfo.DETECTION_INFO).incrementAndGet()+"" + }; + if(!useSequnce){ + params=Arrays.copyOf(params, params.length-1); + } + infoParameter.add(params); + dao.addRecordToInfo(insertInfoSql,infoParameter); + } catch (Exception e){ + logger.error(" Third step -1: adding alarm information exception:sql:"+insertInfoSql+" ; value:" + +id+", "+temInfo[DetectInfo.SETINFOID]+", "+temInfo[DetectInfo.DSINFO]+", " + +temInfo[DetectInfo.PDATA]+", "+temInfo[DetectInfo.CURRENTTIMES]+", "+temInfo[DetectInfo.STARTTIME]+", " + +temInfo[DetectInfo.DELAYTIME]+", "+temInfo[DetectInfo.NEXTCHECKTIME]+", "+temInfo[DetectInfo.PLEVEL]+", " + +temInfo[DetectInfo.CHECKTIME]+", "+temInfo[DetectInfo.ARRIVETIME]+", "+temInfo[DetectInfo.STATE]+", " + +temInfo[DetectInfo.CHECKTIME]+", "+temInfo[DetectInfo.ARRIVETIME]+", "+temInfo[DetectInfo.SEQID]+", " + +temInfo[DetectInfo.URGENTLEVEL]+", ", e); + + } + + } + + /** + * email信息addbatch + * @param emailStmt + * @param emailInfo + * @param tempAddress + * @throws SQLException + */ + public void addEmailStmt(EmailInfo emailInfo,String tempAddress) throws SQLException { + + //插入email_table表 + String insertEmailSql = " insert into email_table (to_address,action_type,content,send_flag,action_ip,action_date,action_desc,send_level,CREATE_TIME)" + + " values(?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))"; + List infoParameter = new ArrayList(); + String[] params = new String[] { + tempAddress, + emailInfo.getActionType()+"", + emailInfo.getContent()+"", + emailInfo.getSendFlag()+"", + emailInfo.getActionIp(), + emailInfo.getActionDate(), + emailInfo.getActionDesc(), + emailInfo.getSendLevel() +"", + DateUtil.getCurrentTime() + }; + infoParameter.add(params); + dao.addRecordToEmail(insertEmailSql,infoParameter); + } + + public void addStateChangeStmt(long statusChangeTime,String seqId) throws SQLException { + + //插入email_table表 + String sqlUpdateStatusChangeTime = "update detection_info_new din set din.status_change_time =to_date(?,'yyyy-mm-dd hh24:mi:ss') where din.seq_id=?"; + + List infoParameter = new ArrayList(); + + String[] params = new String[] { + format.format(new Date(statusChangeTime)), + seqId + }; + infoParameter.add(params); + dao.addRecordToStatusChange(sqlUpdateStatusChangeTime,infoParameter); + } + + /** + * 特殊追加信息net addbatch + * @param currentId + * @param netStmt + * @param temNetInfo + * @throws SQLException + * @throws ParseException + */ + public void addNetStmt(long currentId, + String[] temNetInfo) throws SQLException, ParseException { + + //特殊格式追加 net + String netSql = " insert into di_systeminfo_net (detection_info_id,net_name,net_state," + + " net_speed,net_ip,net_submask,net_gateway,NET_MAC," + + " data_check_time,data_arrive_time," + + " data_check_time_digital,data_arrive_time_digital,DETECTION_SET_INFO_ID) " + + " values (?,?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,?) "; + List infoParameter = new ArrayList(); + String[] params = new String[] { + currentId+"", + temNetInfo[1],//网卡名称 + temNetInfo[2],//网卡状态 + temNetInfo[3],//网卡带宽 + temNetInfo[4],//ip + temNetInfo[5],//子网掩码 + temNetInfo[6],//网关 + temNetInfo[7],//NET_MAC + temNetInfo[8],//监测时间(date) + temNetInfo[9],//录入记录时间(当前时间 date) + temNetInfo[10],//监测时间(long) + temNetInfo[11],//录入记录时间(当前时间 long) + temNetInfo[12]//setInfoId + }; + infoParameter.add(params); + dao.addRecordToNet(netSql,infoParameter); + } + + /** + * 特殊追加信息disk addbatch + * @param currentId + * @param diskStmt + * @param temDiskInfo + * @throws SQLException + * @throws ParseException + */ + public void addDiskStmt(long currentId, + String[] temDiskInfo) throws SQLException, ParseException { + //特殊格式追加 disk + String diskSql = " insert into di_systeminfo_disk (detection_info_id,disk_rang,disk_size," + + " data_check_time_digital,data_check_time," + + " data_arrive_time_digital,data_arrive_time,DETECTION_SET_INFO_ID) " + + " values (?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?) "; + List infoParameter = new ArrayList(); + String[] params = new String[] { + currentId+"", + temDiskInfo[1],//盘符 + temDiskInfo[2],//大小 + temDiskInfo[3],//监测时间(long) + temDiskInfo[4],//监测时间(date) + temDiskInfo[5],//(当前时间 long) + temDiskInfo[6],//录入记录时间(当前时间 date) + temDiskInfo[7]//setInfoId + }; + infoParameter.add(params); + dao.addRecordToDisk(diskSql,infoParameter); + } + + /** + * 告警信息 addbatch + * @param currentId + * @param warningStmt + * @param temInfo + * @throws SQLException + */ + + public void addWarningStmt(String currentId, + Object[] temInfo){ + //插入warning表 + String insertWaringSql = "INSERT INTO DETECTION_INFO_WARNING (ID, DETECTION_SET_INFO_ID, DETECTION_STATE_INFO, " + + "PERFORMACE_DATA, CURRENT_TIMES, START_TIME, DELAY_TIME, NEXT_CHECK_TIME, " + + "POLICE_LEVEL, DATA_CHECK_TIME, DATA_ARRIVE_TIME, DETECTIONED_STATE, " + + "DATA_CHECK_TIME_DIGITAL, DATA_ARRIVE_TIME_DIGITAL, SEQ_ID, POLICE_EMERGENT)" + +" VALUES (?, ?, ?, ?, ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, to_date(?,'yyyy-mm-dd hh24:mi:ss'), to_date(?,'yyyy-mm-dd hh24:mi:ss'), ?, ?, ?, ?, ?)"; + try + { + List infoParameter = new ArrayList(); + String[] params = new String[] { + currentId+"", + (String) temInfo[DetectInfo.SETINFOID], + (String) temInfo[DetectInfo.DSINFO], + (String) temInfo[DetectInfo.PDATA], + temInfo[DetectInfo.CURRENTTIMES]+"", + temInfo[DetectInfo.STARTTIME] != null ? format.format(new Date((Long)temInfo[DetectInfo.STARTTIME])) : null, + temInfo[DetectInfo.DELAYTIME]+"", + temInfo[DetectInfo.NEXTCHECKTIME] != null ? format.format(new Date((Long)temInfo[DetectInfo.NEXTCHECKTIME])) : null, + temInfo[DetectInfo.PLEVEL]+"", + temInfo[DetectInfo.CHECKTIME] != null ? format.format(new Date((Long)temInfo[DetectInfo.CHECKTIME])) : null, + temInfo[DetectInfo.ARRIVETIME] != null ? format.format(new Date((Long)temInfo[DetectInfo.ARRIVETIME])) : null, + temInfo[DetectInfo.STATE]+"", + temInfo[DetectInfo.CHECKTIME] +"", + temInfo[DetectInfo.ARRIVETIME]+"", + (String) temInfo[DetectInfo.SEQID], + temInfo[DetectInfo.URGENTLEVEL]+"" + }; + + infoParameter.add(params); + dao.addRecordToWarning(insertWaringSql,infoParameter); + } catch (Exception e) + { + logger.error(" Third step -1: adding alarm information exception:sql:"+insertWaringSql+" ; value:" + +currentId+", "+temInfo[DetectInfo.SETINFOID]+", "+temInfo[DetectInfo.DSINFO]+", " + +temInfo[DetectInfo.PDATA]+", "+temInfo[DetectInfo.CURRENTTIMES]+", "+temInfo[DetectInfo.STARTTIME]+", " + +temInfo[DetectInfo.DELAYTIME]+", "+temInfo[DetectInfo.NEXTCHECKTIME]+", "+temInfo[DetectInfo.PLEVEL]+", " + +temInfo[DetectInfo.CHECKTIME]+", "+temInfo[DetectInfo.ARRIVETIME]+", "+temInfo[DetectInfo.STATE]+", " + +temInfo[DetectInfo.CHECKTIME]+", "+temInfo[DetectInfo.ARRIVETIME]+", "+temInfo[DetectInfo.SEQID]+", " + +temInfo[DetectInfo.URGENTLEVEL]+", ", e); + + } + + } + + /** + * 获取detection_info sequenceid 和 increment_by + * @return + */ + public long[] getDetectionInfoSequenceID(){ + long[] result = new long[2]; + try { + String searchIdSql = null; + if(Constants.IS_MYSQL){ + searchIdSql = "select t.INCREMENT ival from sys_sequence t where t.name = 'SEQ_DETECTION_INFO'"; + ArrayList fields = new ArrayList(); + fields.add("ival"); + List> sid = dao.dbSelect(searchIdSql, fields); + if(sid!=null && sid.size()>0){ + result[1] = Long.parseLong(sid.get(0).get("ival")); + } + searchIdSql = "select nextval('seq_detection_info') val from dual"; + fields = new ArrayList(); + fields.add("val"); + sid = dao.dbSelect(searchIdSql, fields); + if(sid!=null && sid.size()>0){ + result[0] = Long.parseLong(sid.get(0).get("val")); + } + }else{ + searchIdSql = "select seq_detection_info.NEXTVAL val, t.increment_by ival from user_sequences t where t.sequence_name = 'SEQ_DETECTION_INFO'"; + ArrayList fields = new ArrayList(); + fields.add("val"); + fields.add("ival"); + List> sid = dao.dbSelect(searchIdSql, fields); + if(sid!=null && sid.size()>0){ + result[0] = Long.parseLong(sid.get(0).get("val")); + result[1] = Long.parseLong(sid.get(0).get("ival")); + } + } + logger.info("获取的detection_info表的sequence已使用完毕,重新获取,间隔为:"+result[1]+",next值为:"+result[0]); + } catch (SQLException e) { + logger.warn("获取detection_info 的sequence id 失败",e); + result[0] = -1; + result[1] = -1; + }finally{ + if(dao != null){ + //dao.clearConn();//清空连接,但是不关闭 + } + } + return result; + } + + /** + * 获取数据库当前时间 + * @return + */ + public Date getDbTime(){ + String sql = "select to_char(sysdate,'"+Constants.DB_DATE_FORMAT+"') now from dual"; + try { + ArrayList fields = new ArrayList(); + fields.add("now"); + String now = dao.dbSelect(sql, fields).get(0).get("now"); + return Common.getFormat().parse(now); + } catch (Exception e) { + logger.error("",e); + }finally{ + try { + dao.clearConn(); + } catch (Exception e2) { + } + } + return null; + } + public void saveToNew(List dataList,int flag ,PreparedStatement stmt ){ + if(dataList==null||dataList.size()<1){ + return ; + } + if(dataList!=null&&dataList.size()>0){ + if(flag==0){//insert + String sql=DetectInfo.getSaveToNewSql(); + try { + if(stmt==null){ + stmt=dao.getConn().prepareStatement(sql); + } + + for (Object[] newData : dataList) { + // 计算 详细表的 外键 时间戳(10) + seqId(5)+ setId (3),主要是为了使用 java 中long 类型 + String checkTime = newData[DetectInfo.CHECKTIME].toString(); + String setId = newData[DetectInfo.SETINFOID].toString(); + String seqId = newData[DetectInfo.SEQID].toString(); + String id = DetectInfo.computeId(checkTime, seqId, setId); + Object []params={ + newData[DetectInfo.SETINFOID], + newData[DetectInfo.DSINFO], + newData[DetectInfo.PDATA], + newData[DetectInfo.CURRENTTIMES], + newData[DetectInfo.STARTTIME] != null ? format.format(new Date((Long) newData[DetectInfo.STARTTIME])) : null, + newData[DetectInfo.WAITTIME], + newData[DetectInfo.DELAYTIME], + newData[DetectInfo.NEXTCHECKTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.NEXTCHECKTIME])) : null, + newData[DetectInfo.PLEVEL], + newData[DetectInfo.CHECKTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.CHECKTIME])) : null, + newData[DetectInfo.ARRIVETIME]!= null ? format.format(new Date((Long) newData[DetectInfo.ARRIVETIME])) : null, + newData[DetectInfo.STATE], + newData[DetectInfo.STATUSCHANGTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.STATUSCHANGTIME])) : null, + newData[DetectInfo.SEQID], + id, + newData[DetectInfo.CHECKTIME], + newData[DetectInfo.ARRIVETIME], + newData[DetectInfo.URGENTLEVEL] + }; + addRecordToStatement(stmt, params); + } + stmt.executeBatch(); + + logger.info("info_new table insert success! size : "+ dataList.size()); + } catch (SQLException e) { + logger.error("save info to info_new table error"+e.getMessage()); + } + }else if(flag==1){//update + String sql=DetectInfo.getUpdateToNewSql(); + try { + if(stmt==null){ + stmt=dao.getConn().prepareStatement(sql); + } + for (Object[] newData : dataList) { + // 计算 详细表的 外键 时间戳(10) + seqId(5)+ setId (3),主要是为了使用 java 中long 类型 + String checkTime = newData[DetectInfo.CHECKTIME].toString(); + String setId = newData[DetectInfo.SETINFOID].toString(); + String seqId = newData[DetectInfo.SEQID].toString(); + String id = DetectInfo.computeId(checkTime, seqId, setId); + Object []params={newData[DetectInfo.DSINFO], + newData[DetectInfo.PDATA], + newData[DetectInfo.CURRENTTIMES], + newData[DetectInfo.STARTTIME] != null ? format.format(new Date((Long) newData[DetectInfo.STARTTIME])) : null, + newData[DetectInfo.WAITTIME], + newData[DetectInfo.DELAYTIME], + newData[DetectInfo.NEXTCHECKTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.NEXTCHECKTIME])) : null, + newData[DetectInfo.PLEVEL], + newData[DetectInfo.CHECKTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.CHECKTIME])) : null, + newData[DetectInfo.ARRIVETIME]!= null ? format.format(new Date((Long) newData[DetectInfo.ARRIVETIME])) : null, + newData[DetectInfo.STATE], + newData[DetectInfo.STATUSCHANGTIME]!= null && (Long)newData[DetectInfo.STATUSCHANGTIME]!= -1? format.format(new Date((Long) newData[DetectInfo.STATUSCHANGTIME])) : null, + id, + newData[DetectInfo.CHECKTIME], + newData[DetectInfo.ARRIVETIME], + newData[DetectInfo.URGENTLEVEL], + newData[DetectInfo.SETINFOID], + newData[DetectInfo.SEQID] + }; + addRecordToStatement(stmt, params); + } + stmt.executeBatch(); + + logger.info("info_new table update success! size : "+ dataList.size()); + } catch (SQLException e) { + logger.error("update info to info_new table error"+e.getMessage()); + } + } + } + dataList.clear(); + } + public void addRecordToStatement(PreparedStatement stmt, Object[] params) throws SQLException { + if (params != null) { + logger.debug(" params " + Arrays.toString(params)); + for (int i = 0; i < params.length; i++) { + stmt.setObject(i + 1, params[i]); + } + stmt.addBatch(); + } + } + public static void main(String[] args) { + System.out.println(StringUtils.isNumeric("1245463")); + System.out.println(StringUtils.isNumeric("1245.463")); + System.out.println(StringUtils.isNumeric("1asd5463")); + } + +// public Map getNodeByIp(List ipList) { +// Map ipNodeMap = new HashMap(); +// if(ipList!=null &&ipList.size()>0){ +// StringBuffer strb = new StringBuffer(); +// for(int i=0;i0){ +// strb.deleteCharAt(strb.length() - 1); +// } +// strb.append(")"); +// String sql = "select NODE_IP,NODE_USERNAME,NODE_PASSWORD from NODE_TABLE where NODE_IP in ("+strb.toString(); +// +// ArrayList fields = new ArrayList(); +// fields.add("NODE_IP"); +// fields.add("NODE_USERNAME"); +// fields.add("NODE_PASSWORD"); +// try { +// List> sid = dao.dbSelect(sql, fields); +// if(sid!=null&&sid.size()>0){ +// BASE64Decoder decoder = new BASE64Decoder(); +// for(Map map : sid){ +// String[] ipInfo = new String[2]; +// ipInfo[0] = map.get("NODE_USERNAME"); +// String passward = map.get("NODE_PASSWORD"); +// if(StringUtils.isNotBlank(passward)){ +// passward = new String(decoder.decodeBuffer(passward)); +// ipInfo[1] = passward; +// } +// ipNodeMap.put(map.get("NODE_IP"), ipInfo); +// } +// } +// } catch (SQLException e) { +// e.printStackTrace(); +// logger.error("查询失败",e); +// } catch (IOException e) { +// e.printStackTrace(); +// }finally{ +// if(dao != null){ +// dao.clearConn();//清空连接,但是不关闭 +// } +// } +// } +// return ipNodeMap; +// } +} diff --git a/src/com/nms/server/service/EmailService.java b/src/com/nms/server/service/EmailService.java new file mode 100644 index 0000000..64abec3 --- /dev/null +++ b/src/com/nms/server/service/EmailService.java @@ -0,0 +1,707 @@ +package com.nms.server.service; + +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.ResourceBundle; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.ContactSetInfo; +import com.nms.server.bean.EmailInfo; +import com.nms.server.common.CommonResources; +import com.nms.server.common.Constants; +import com.nms.server.common.EmailTypeConstants; +import com.nms.server.dao.CommonDao; +import com.nms.server.util.SQLExecuteTimeoutException; + +/** + * 创建并保存邮件信息 + * 查询邮件信息并将邮件信息整理成单一邮件进行查询 + * @date Feb 6, 2012 10:17:56 AM + * @author ZhangGang + * + */ +public class EmailService { + private final Logger logger = Logger.getLogger(EmailService.class); + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + private CommonDao dao = null; + + public EmailService(CommonDao dao){ + this.dao = dao; + } + + public List getEmailInfoList(){ + String selectSql = "select et.id , et.to_address ,et.action_type ,et.action_desc ,et.action_ip ,to_char(et.action_date,'"+Constants.DB_DATE_FORMAT+"') action_date,et.content from email_table et where et.send_flag = ? order by et.to_address asc,et.action_type asc,et.action_desc asc,et.action_ip asc,et.action_date asc"; + ArrayList fields = new ArrayList(); + fields.add("id"); + fields.add("to_address"); + fields.add("action_type"); + fields.add("action_desc"); + fields.add("action_ip"); + fields.add("action_date"); + fields.add("content"); + + Object [] params = new Object[]{EmailTypeConstants.FLAG_SEND_LATER}; + List eiList = new LinkedList(); + try { + ArrayList> mapsList = dao.dbSelect(selectSql, fields, EmailTypeConstants.FLAG_SEND_LATER); + if(mapsList!=null && mapsList.size()>0){ + for(Map maps : mapsList){ + EmailInfo ei = new EmailInfo(); + ei.setId(StringUtils.isEmpty(maps.get("id"))?null:Long.parseLong(maps.get("id"))); + ei.setToAddress(maps.get("to_address")); + ei.setActionType(StringUtils.isEmpty(maps.get("action_type"))?null:Integer.parseInt(maps.get("action_type"))); + ei.setActionDesc(maps.get("action_desc")); + ei.setActionIp(maps.get("action_ip")); + ei.setActionDate(maps.get("action_date")); + ei.setContent(maps.get("content")); + eiList.add(ei); + } + } + } catch (SQLException e) { + logger.error("Failure of mail information query", e); + } + + return eiList; + } + + public String getEmailTypeDescByTypeValue(int type){ + switch (type) { + + case EmailTypeConstants.TYPE_DETECTION_INFO_RECOVER: + return EmailTypeConstants.DESC_DETECTION_INFO_RECOVER; + case EmailTypeConstants.TYPE_DETECTION_INFO_EXCEPTION: + return EmailTypeConstants.DESC_DETECTION_INFO_EXCEPTION; + case EmailTypeConstants.TYPE_DETECTION_INFO_TIMEOUT: + return EmailTypeConstants.DESC_DETECTION_INFO_TIMEOUT; + + case EmailTypeConstants.TYPE_ALARM_INFO_EXCEPTION: + return EmailTypeConstants.DESC_ALARM_INFO_EXCEPTION; + case EmailTypeConstants.TYPE_ALARM_INFO_RECOVER: + return EmailTypeConstants.DESC_ALARM_INFO_RECOVER; + + case EmailTypeConstants.TYPE_TASK_NODE_RESULT_ERROR: + return EmailTypeConstants.DESC_TASK_NODE_RESULT_ERROR; + case EmailTypeConstants.TYPE_TASK_STATE_CHANGE: + return EmailTypeConstants.DESC_TASK_STATE_CHANGE; + + case EmailTypeConstants.TYPE_SYSTEM_RUNNING_EXCEPTION: + return EmailTypeConstants.DESC_SYSTEM_RUNNING_EXCEPTION; + case EmailTypeConstants.TYPE_SYSTEM_RUNNING_RECOVER: + return EmailTypeConstants.DESC_SYSTEM_RUNNING_RECOVER; + + default: + return null; + } + + } + public void sendEmailForDetectionBySeqId(Long dId,Long seqId,EmailInfo emailInfo){ + + //-- 邮件功能是否开启 + if(Constants.flag_email != 1){ + logger.info("邮件功能已关闭"); + return; + } + + //-- 数据校验 无效返回 + if(dId==null || dId.longValue() == 0l || seqId==null || seqId.longValue() == 0l){ + return; + } + + //-- 查询联系人设置信息 + ContactSetInfo contactSetInfo = this.getContactSetInfoByDetecId(dId); + + //-- 获取联系人列表 + List eiList = new LinkedList(); + List emaiList = this.getEmailList(contactSetInfo, seqId); + logger.debug("邮件数量 "+(emaiList==null?null:emaiList.size())); + if(emaiList!=null && emaiList.size()>0){ + + for (Iterator emailIte = emaiList.iterator(); emailIte.hasNext();) { + String addr = (String) emailIte.next(); + if(StringUtils.isEmpty(addr))continue; //邮件地址不为空 + EmailInfo emailInfo2 = new EmailInfo(emailInfo); + emailInfo2.setToAddress(addr); + eiList.add(emailInfo2); + } + + try { + saveEmailInfoList(eiList); + } catch (SQLExecuteTimeoutException e) { + logger.error("",e); + } catch (SQLException e) { + logger.error("",e); + } + } + } + + public void sendEmailForMissionBySeqId(Long mId,int missionType,Long seqId,EmailInfo emailInfo){ + //-- 邮件功能是否开启 + if(Constants.flag_email != 1){ + logger.info("邮件功能已关闭"); + return; + } + + //-- 数据校验 无效返回 + if(mId==null || mId.longValue() == 0l || missionType==0 || seqId==null || seqId.longValue() == 0l){ + return; + } + + //-- 查询联系人设置信息 + ContactSetInfo contactSetInfo = this.getContactSetInfoByMissionId(mId); + + //-- 获取联系人列表 + List eiList = new LinkedList(); + List emaiList = this.getEmailList(contactSetInfo, seqId); + logger.debug("邮件数量 "+(emaiList==null?null:emaiList.size())); + + //-- 发邮件 + if(emaiList!=null && emaiList.size()>0){ + + for (Iterator emailIte = emaiList.iterator(); emailIte.hasNext();) { + String addr = (String) emailIte.next(); + if(StringUtils.isEmpty(addr))continue; + EmailInfo emailInfo2 = new EmailInfo(emailInfo); + emailInfo2.setToAddress(addr); + eiList.add(emailInfo2); + } + } + + try { + saveEmailInfoList(eiList); + } catch (SQLExecuteTimeoutException e) { + logger.error("",e); + } catch (SQLException e) { + logger.error("",e); + } + //-结束 + } + + /** + * 任务执行邮件通知方法 + * 任务类型 1 根据 系统 用户组 指定用户处理 + * 任务类型 4 根据 系统 用户组 指定用户处理 + * 任务类型 6 根据 下发机器 的管理用户发送通知 + * @time Mar 28, 2012-1:53:56 PM + * @param mission 任务信息 + * @param emaiTitle 主题 + * @param emailContext 内容 + */ + public void sendEmailForMission(Long missionId,String emaiTitle,String emailContext){ + //-- 邮件功能是否开启 + if(Constants.flag_email != 1){ + logger.info("邮件功能已关闭"); + return; + } + + //-- 参数校验 + if(missionId==null || StringUtils.isEmpty(emaiTitle) || StringUtils.isEmpty(emailContext)){ + return; + } + + ContactSetInfo info = this.getContactSetInfoByMissionId(missionId); + LinkedList emailList = null; + + ResourceBundle res = ResourceBundle.getBundle(CommonResources.class.getName()); +// ContactSetInfo info = getContactSetInfoByMissionId(mId,mType); + String startTime = ""; + String endTime = ""; + if(info.getStartTime()!=null){ + startTime = format.format(info.getStartTime()); + } + if(info.getEndTime()!=null){ + endTime = format.format(info.getEndTime()); + } + + String sendInfo = ""; + + if(info.getMissionType().longValue()==4l){ +// sendInfo += " \n周期任务:" +(info.getIsLoop()==null?"否":(info.getIsLoop().longValue()==1l?"是":"否")); +// sendInfo += " \n计划执行时间:" + startTime+" -- "+endTime; +// sendInfo += " \nPeriodic Task:" +(info.getIsLoop()==null?"No":(info.getIsLoop().longValue()==1l?"Yes":"No")); +// sendInfo += " \nPlan Execution Time:" + startTime+" -- "+endTime; + sendInfo += " \ni18n_server.EmailService.loopMission_n81i:" +(info.getIsLoop()==null?"i18n_server.EmailService.no_n81i":(info.getIsLoop().longValue()==1l?"i18n_server.EmailService.yes_n81i":"i18n_server.EmailService.no_n81i")); + sendInfo += " \ni18n_server.EmailService.plantime_n81i:" + startTime+" -- "+endTime; + } +// sendInfo += " \n任务状态:" + res.getObject("ms_"+info.getMissionState().intValue()) +// + " \n节点组:" + info.getNodeGroupName() +// + " \n节点:" + info.getNodeIpsName() +// + " \n" + emailContext; +// sendInfo += " \nTask state:" + res.getObject("ms_"+info.getMissionState().intValue()) +// + " \nNode Group:" + info.getNodeGroupName() +// + " \nNode:" + info.getNodeIpsName() +// + " \n" + emailContext; + sendInfo += " \ni18n_server.EmailService.missionState_n81i:" + res.getObject("ms_"+info.getMissionState().intValue()) + + " \ni18n_server.EmailService.nodegroup_n81i:" + info.getNodeGroupName() + + " \ni18n_server.EmailService.node_n81i:" + info.getNodeIpsName() + + " \n" + emailContext; + + EmailInfo emailInfo = new EmailInfo(); + emailInfo.setActionDesc(info.getMissionName()+"("+res.getObject("mt_"+info.getMissionType().longValue())+")"); + emailInfo.setActionDate(new SimpleDateFormat(Constants.COMMON_DATE_FORMAT).format(new Date())); + emailInfo.setActionType(EmailTypeConstants.TYPE_TASK_STATE_CHANGE); + emailInfo.setSendFlag(EmailTypeConstants.FLAG_SEND_LATER); + emailInfo.setContent(sendInfo); + emailInfo.setSendLevel(EmailTypeConstants.URGENT_IMMEDIATELY);//2013-6-24 hyx 任务状态改变由不紧急修改为紧急 + //- 发送邮件业务 + try { + //-- 查询联系人参数过滤 + switch (info.getMissionType().intValue()) { + case 6: + info.setViewLevel(3l); // 升级部署 通知均为系统内通知 + emailList = getEmailList(info, null); + break; + default: //任务1 任务4 均按原有业务发送邮件 + emailList = getEmailList(info, null); + break; + } + logger.debug("邮件数量 "+(emailList==null?null:emailList.size())); + + //-- 发送邮件 + List eiList = new LinkedList(); + if(emailList !=null && emailList.size()>0){ + for (Iterator emailIte = emailList.iterator(); emailIte.hasNext();) { + String addr = (String) emailIte.next(); + if(StringUtils.isEmpty(addr))continue; + EmailInfo emailInfo2 = new EmailInfo(emailInfo); + emailInfo2.setToAddress(addr); + eiList.add(emailInfo2); + + } + + saveEmailInfoList(eiList); + + } + + } catch (Exception e) { + logger.error("",e); + } + logger.info("任务下发 邮件通知 发送完成"); + + //-结束 + } + + public boolean saveEmailInfoList(List eiList) throws SQLExecuteTimeoutException,SQLException { + + String insertSql = " insert into email_table (to_address,action_type,content,send_flag,action_ip,action_date,action_desc,send_level,CREATE_TIME)" + + " values(?,?,?,?,?,to_date(?,'"+Constants.DB_DATE_FORMAT+"'),?,?,to_date(?,'"+Constants.DB_DATE_FORMAT+"'))" ; + List paramsList = new LinkedList(); + if(eiList != null && eiList.size()>0){ + String createTime = format.format(new Date()); + for(EmailInfo ei :eiList){ + paramsList.add(new String[]{ei.getToAddress(),ei.getActionType()+"",ei.getContent(),ei.getSendFlag()+"",ei.getActionIp(),ei.getActionDate(),ei.getActionDesc(),/*ei.getActionDesc2(),*/ei.getSendLevel()==null?EmailTypeConstants.URGENT_LATER+"":ei.getSendLevel()+"",createTime}); + } + return dao.dbUpdateByBatch(insertSql, paramsList); + } + return false; + } + + /*private void sendEmailForDetectionBySeqId(Long dId,Long seqId,String emaiTitle,String emailContext){ + + //-- 数据校验 无效返回 + if(dId==null || dId.longValue() == 0l || seqId==null || seqId.longValue() == 0l){ + return; + } + //-- 查询联系人设置信息 + ContactSetInfo contactSetInfo = this.getContactSetInfoByDetecId(dId); + //-- 获取联系人列表 + List emaiList = this.getEmailList(contactSetInfo, seqId); + logger.debug("邮件数量 "+(emaiList==null?null:emaiList.size())); + //-- 发邮件 + if(emaiList!=null && emaiList.size()>0){ + SendMail cn = new SendMail(); + for (Iterator emailIte = emaiList.iterator(); emailIte.hasNext();) { + String addr = (String) emailIte.next(); + if(StringUtils.isEmpty(addr))continue; + logger.debug("邮件 To "+(addr)); + // 设置发件人地址、收件人地址和邮件标题 + cn.setAddress(Constants.EMAIL_ADDRESS, addr, emaiTitle); + cn.send(emailContext); + } + } + //-结束 + }*/ + + + + /*private void sendEmailForMissionBySeqId(Long mId,int missionType,Long seqId,String emaiTitle,String emailContext){ + + //-- 数据校验 无效返回 + if(mId==null || mId.longValue() == 0l || missionType==0 || seqId==null || seqId.longValue() == 0l){ + return; + } + //-- 查询联系人设置信息 + ContactSetInfo contactSetInfo = this.getContactSetInfoByMissionId(mId); + //-- 获取联系人列表 + List emaiList = this.getEmailList(contactSetInfo, seqId); + logger.debug("邮件数量 "+(emaiList==null?null:emaiList.size())); + //-- 发邮件 + if(emaiList!=null && emaiList.size()>0){ + SendMail cn = new SendMail(); + for (Iterator emailIte = emaiList.iterator(); emailIte.hasNext();) { + String addr = (String) emailIte.next(); + if(StringUtils.isEmpty(addr))continue; + logger.debug("邮件 To "+(addr)); + // 设置发件人地址、收件人地址和邮件标题 + cn.setAddress(Constants.EMAIL_ADDRESS, addr, emaiTitle); + cn.send(emailContext); + } + } + //-结束 + }*/ + + /** + * 任务执行邮件通知方法 + * 任务类型 1 根据 系统 用户组 指定用户处理 + * 任务类型 4 根据 系统 用户组 指定用户处理 + * 任务类型 6 根据 下发机器 的管理用户发送通知 + * @time Mar 28, 2012-1:53:56 PM + * @param mission 任务信息 + * @param emaiTitle 主题 + * @param emailContext 内容 + */ + /*private void sendEmailForMission(Long missionId,String emaiTitle,String emailContext){ + + //-- 参数校验 + if(missionId==null || StringUtils.isEmpty(emaiTitle) || StringUtils.isEmpty(emailContext)){ + return; + } + ContactSetInfo info = this.getContactSetInfoByMissionId(missionId); + LinkedList emailList = null; + + ResourceBundle res = ResourceBundle.getBundle(CommonResources.class.getName()); +// ContactSetInfo info = getContactSetInfoByMissionId(mId,mType); + String startTime = ""; + String endTime = ""; + if(info.getStartTime()!=null){ + startTime = format.format(info.getStartTime()); + } + if(info.getEndTime()!=null){ + endTime = format.format(info.getEndTime()); + } + + String sendInfo = "任务名称:"+info.getMissionName() + + " \n任务类型:" + res.getObject("mt_"+info.getMissionType().longValue()); + + if(info.getMissionType().longValue()==4l){ + sendInfo += " \n周期任务:" +(info.getIsLoop()==null?"否":(info.getIsLoop().longValue()==1l?"是":"否")); + } + sendInfo += " \n任务状态:" + res.getObject("ms_"+info.getMissionState().intValue()); + sendInfo += " \n计划执行时间:" + startTime+" -- "+endTime + + " \n节点组:" + info.getNodeGroupName() + + " \n节点:" + info.getNodeIpsName() + + " \n" + emailContext; + + //- 发送邮件业务 + try { + //-- 查询联系人参数过滤 + switch (info.getMissionType().intValue()) { + case 6: + info.setViewLevel(3l); // 升级部署 通知均为系统内通知 + emailList = getEmailList(info, null); + break; + default: //任务1 任务4 均按原有业务发送邮件 + emailList = getEmailList(info, null); + break; + } + logger.debug("邮件数量 "+(emailList==null?null:emailList.size())); + + //-- 发送邮件 + if(emailList !=null && emailList.size()>0){ + SendMail cn = new SendMail(); + for (Iterator emailIte = emailList.iterator(); emailIte.hasNext();) { + String addr = (String) emailIte.next(); + if(StringUtils.isEmpty(addr))continue; + logger.debug("邮件 To "+(addr)); + // 设置发件人地址、收件人地址和邮件标题 + cn.setAddress(Constants.EMAIL_ADDRESS, addr, emaiTitle); + cn.send(sendInfo); + } + } + } catch (Exception e) { + logger.error("",e); + } + logger.info("任务下发 邮件通知 发送完成"); + + //-结束 + }*/ + + private LinkedList getEmailList(ContactSetInfo contactSetInfo,Long seqId) { + LinkedList emailList = new LinkedList(); + ArrayList fields = new ArrayList(); + fields.add("email"); + if(contactSetInfo!=null && contactSetInfo.getViewLevel()!=null){ + try { + if(contactSetInfo.getViewLevel().intValue()==1){ //个人查看 邮件发送给 创建者 + String sql1 = "select distinct xyj.email from xt_yh_jbxx xyj where xyj.yhid = '"+contactSetInfo.getCreateUserId()+"' and xyj.is_receiveemail='0' "; + List> map1 = (List>)dao.dbSelect(sql1, fields); + if(map1 != null && map1.size()>0){ + emailList.add(map1.get(0).get("email")); + } + }else { //非个人 + //-- 无限制 或 未指定联系人情况 + if(contactSetInfo.getViewLevel().intValue()==4 || (contactSetInfo.getContactUserIds()==null || contactSetInfo.getContactUserIds().length()==0)){ + /** + * 查询seqId所在的系统 + * 系统关联的用户维护组的有效维护人员接收邮件信息 + * */ + StringBuffer selectSql = new StringBuffer(); + selectSql.append("select distinct xyj.email "); + selectSql.append("from xt_yh_jbxx xyj "); + selectSql.append("left join xt_yh_js_index xyji on xyji.yhid = xyj.yhid "); + selectSql.append("left join xt_js_jbxx xjj on xjj.jsbh = xyji.jsbh "); + selectSql.append("left join gorup_system_table gst on gst.user_group_id = xjj.jsbh "); + selectSql.append("left join system_table st on st.system_id = gst.system_id "); + selectSql.append("left join node_table nt on nt.system_id = gst.system_id "); + selectSql.append("left join nodegroup_table ngt on ngt.group_id = nt.node_group_id "); + selectSql.append("where xyj.is_receiveemail='0' "); + + selectSql.append("and nt.node_state = 0 "); // 节点有效状态 0有效 + selectSql.append("and ngt.is_valid = 1 "); // 节点组有效状态 1有效 + selectSql.append("and st.system_state = 0 "); // 系统有效状态 0有效 + selectSql.append("and xjj.zxbz = 0 "); // 用户组有效状态 0有效 + selectSql.append("and xjj.type = 1 "); // 组类别状态 1是用户组 0是角色 + selectSql.append("and xyj.zxbz = 0 "); // 用户有效状态 0 有效 + if(seqId != null && seqId.longValue() != 0){ + selectSql.append("and nt.seq_id = "+seqId); + } + //-- 无限制 情况 + if(contactSetInfo.getViewLevel().intValue()==4){ + + }else + //-- 系统内 情况 + if(contactSetInfo.getViewLevel().intValue()==3){ + selectSql.append("and nt.system_id = '"+contactSetInfo.getSystemId()+"' "); + }else + //-- 组内 情况 + if(contactSetInfo.getViewLevel().intValue()==2){ + selectSql.append("and xjj.jsbh = '"+contactSetInfo.getCreateUserGroupId()+"' "); + } + List> mapList = (List>) dao.dbSelect(selectSql.toString(), fields); + if(mapList != null && mapList.size()>0){ + for (Iterator> mapIte = mapList.iterator(); mapIte.hasNext();) { + Map map = mapIte.next(); + emailList.add(map.get("email")); + } + } + } + //-- 组或系统内指定联系人情况 + else{ + String sql0 = "select distinct xyj.email from xt_yh_jbxx xyj where xyj.yhid in ("+contactSetInfo.getContactUserIds()+") "; + List> map0List = (List>)dao.dbSelect(sql0, fields); + if(map0List != null && map0List.size()>0){ + for (Iterator> map0Ite = map0List.iterator(); map0Ite.hasNext();) { + Map map0 = map0Ite.next(); + emailList.add(map0.get("email")); + } + } + } + } + } catch (Exception e) { + logger.error(" Fail to query the list of email addresses to be sent to failure",e); + }finally{ + dao.clearConn(); + } + } + + return emailList; + } + + private ContactSetInfo getContactSetInfoByDetecId(Long dId){ + ContactSetInfo contactSetInfo = null; + StringBuffer selectSql = new StringBuffer(); + selectSql.append("select "); + selectSql.append("dsi.view_level, "); + selectSql.append("dsi.create_user_id, "); + selectSql.append("dsi.create_usergroup_id, "); + selectSql.append("dsi.system_id, "); + selectSql.append("dsi.contact_user_ids "); + selectSql.append("from detection_set_info dsi where dsi.id= '"+dId+"' "); + ArrayList fields = new ArrayList(); + fields.add("view_level"); + fields.add("create_user_id"); + fields.add("create_usergroup_id"); + fields.add("system_id"); + fields.add("contact_user_ids"); + try { + ArrayList> mapList = dao.dbSelect(selectSql.toString(), fields); + if(mapList!=null && mapList.size()>0){ + Map map0 = mapList.get(0); + contactSetInfo = new ContactSetInfo(); + contactSetInfo.setViewLevel(map0.get("view_level")==null?null:Long.parseLong(map0.get("view_level"))); + contactSetInfo.setCreateUserId(map0.get("create_user_id")==null?null:Long.parseLong(map0.get("create_user_id"))); + contactSetInfo.setCreateUserGroupId(map0.get("create_usergroup_id")==null?null:Long.parseLong(map0.get("create_usergroup_id"))); + contactSetInfo.setSystemId(map0.get("system_id")==null?null:Long.parseLong(map0.get("system_id"))); + contactSetInfo.setContactUserIds(map0.get("contact_user_ids")); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return contactSetInfo; + } + + private ContactSetInfo getContactSetInfoByMissionId(Long mId){ + ContactSetInfo contactSetInfo = null; + StringBuffer selectSql = new StringBuffer(); + selectSql.append("select "); + selectSql.append("mst.mission_id, "); + selectSql.append("mst.mission_name, "); +// selectSql.append("ngt.group_name node_group_name, "); +// selectSql.append("mpt.node_groups_id, "); +// selectSql.append("mpt.node_ips_id, "); + selectSql.append("mst.mission_type, "); + selectSql.append("mst.mission_state, "); + selectSql.append("mst.is_loop , "); + selectSql.append("to_char(mst.start_time,'"+Constants.DB_DATE_FORMAT+"' ) start_time, "); + selectSql.append("to_char(mst.end_time,'"+Constants.DB_DATE_FORMAT+"' ) end_time, "); + selectSql.append("mst.view_level, "); + selectSql.append("mst.user_id, "); + selectSql.append("mst.group_id, "); + selectSql.append("mst.system_id, "); + selectSql.append("mst.contact_user_ids "); + selectSql.append("from mission_state_table mst "); +// selectSql.append("left join mission_parameter_table"+missionType+" mpt on mpt.mission_id = mst.mission_id "); +// selectSql.append("left join nodegroup_table ngt on ngt.group_id = mpt.node_groups_id "); + selectSql.append("where mst.mission_id= '"+mId+"' "); + + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("mission_name"); +// fields.add("node_groups_id"); +// fields.add("node_ips_id"); + fields.add("mission_type"); + fields.add("mission_state"); + fields.add("is_loop"); + fields.add("start_time"); + fields.add("end_time"); + fields.add("view_level"); + fields.add("user_id"); + fields.add("group_id"); + fields.add("system_id"); + fields.add("contact_user_ids"); + try { + ArrayList> mapList = dao.dbSelect(selectSql.toString(), fields); + if(mapList!=null && mapList.size()>0){ + Map map = mapList.get(0); + contactSetInfo = new ContactSetInfo(); + contactSetInfo.setMissionId(map.get("mission_id")==null?null:Long.parseLong(map.get("mission_id"))); + contactSetInfo.setMissionName(map.get("mission_name")); + contactSetInfo.setMissionType(map.get("mission_type")==null?null:Long.parseLong(map.get("mission_type"))); + contactSetInfo.setMissionState(map.get("mission_state")==null?null:Long.parseLong(map.get("mission_state"))); + contactSetInfo.setIsLoop(map.get("is_loop")==null?null:Long.parseLong(map.get("is_loop"))); + contactSetInfo.setStartTime(map.get("start_time")==null?null:format.parse(map.get("start_time"))); + contactSetInfo.setEndTime(map.get("end_time")==null?null:format.parse(map.get("end_time"))); + contactSetInfo.setViewLevel(map.get("view_level")==null?null:Long.parseLong(map.get("view_level"))); + contactSetInfo.setCreateUserId(map.get("user_id")==null?null:Long.parseLong(map.get("user_id"))); + contactSetInfo.setCreateUserGroupId(map.get("group_id")==null?null:Long.parseLong(map.get("group_id"))); + contactSetInfo.setSystemId(map.get("system_id")==null?null:Long.parseLong(map.get("system_id"))); + contactSetInfo.setContactUserIds(map.get("contact_user_ids")); + + ArrayList paramsFields = new ArrayList(); + paramsFields.add("node_groups_id"); + paramsFields.add("node_ips_id"); + String paramsSql = "select mpt.node_groups_id,mpt.node_ips_id from mission_parameter_table"+contactSetInfo.getMissionType().intValue()+" mpt where mpt.mission_id=?"; + ArrayList> paramsMapList = dao.dbSelect(paramsSql.toString(), paramsFields,mId); + + if(paramsMapList!= null && paramsMapList.size()>0){ + Map map0 = paramsMapList.get(0); + contactSetInfo.setNodeGroupsId(map0.get("node_groups_id")); + contactSetInfo.setNodeIpsId(map0.get("node_ips_id")); + } + } + + + + //节点组和节点名称填写 + if(contactSetInfo!= null){ + if(contactSetInfo.getNodeGroupsId()!= null){ + //查询节点组名称 + ArrayList ngnFields = new ArrayList(); + ngnFields.add("group_name"); + String ngNameSql = "select ngt.group_name from nodegroup_table ngt where ngt.group_id in ("+contactSetInfo.getNodeGroupsId()+")"; + ArrayList> ngnMapList = dao.dbSelect(ngNameSql.toString(), ngnFields); + if(ngnMapList != null && ngnMapList.size()>0){ + StringBuffer ngnsb = new StringBuffer(); + for(Map map : ngnMapList){ + ngnsb.append(","+map.get(ngnFields.get(0))); + } + if(ngnsb.length()>0){ + ngnsb.deleteCharAt(0); + } + contactSetInfo.setNodeGroupName(ngnsb.toString()); + } + + //查询节点名称 + ArrayList nnFields = new ArrayList(); + nnFields.add("node_name"); + String nnNameSql = null; + ArrayList> nnMapList = null; + if(contactSetInfo.getNodeIpsId()!= null){ + nnNameSql = "select nt.node_name from node_table nt where nt.node_id in ("+contactSetInfo.getNodeIpsId()+")"; + nnMapList = dao.dbSelect(nnNameSql.toString(), nnFields); + }else{ + nnNameSql = "select nt.node_name from node_table nt where nt.node_group_id in ("+contactSetInfo.getNodeGroupsId()+") "; + if(contactSetInfo.getIsLoop()==null || contactSetInfo.getIsLoop()==0){ //非周期任务 + nnNameSql += "and nt.seq_id in (select mrt.seq_id from mission_result_table"+contactSetInfo.getMissionType()+" mrt where mrt.mission_id=?)"; + }else{ //周期任务 + nnNameSql += "and nt.seq_id in (select distinct mrt.seq_id from mission_result_table4 mrt where mrt.mission_id in (select lmst.cur_mission_id from loopmission_state_table lmst where lmst.mission_id=?))"; + } + nnMapList = dao.dbSelect(nnNameSql.toString(), nnFields,mId); + } + if(nnMapList != null && nnMapList.size()>0){ + StringBuffer nnsb = new StringBuffer(); + for(Map map : nnMapList){ + nnsb.append(","+map.get(nnFields.get(0))); + } + if(nnsb.length()>0){ + nnsb.deleteCharAt(0); + } + contactSetInfo.setNodeIpsName(nnsb.toString()); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + }finally{ + dao.clearConn(); + } + return contactSetInfo; + } + + public static void main(String [] args){ + CommonDao dao = null; + try { + dao = new CommonDao(); + EmailService emailService = new EmailService(dao); + EmailInfo emailInfo = new EmailInfo(11,"测试名称","10.0.6.113",new SimpleDateFormat(Constants.COMMON_DATE_FORMAT).format(new Date()),"这是一条关于监测设置的测试信息",EmailTypeConstants.FLAG_SEND_LATER,EmailTypeConstants.URGENT_LATER); + //113监测邮件测试 + Constants.flag_email = 1; +// emailService.sendEmailForDetectionBySeqId(2570l, 85l, emailInfo); +// EmailInfo emailInfo2 = new EmailInfo(31,"任务XXX","10.0.6.113",new SimpleDateFormat(Constants.COMMON_DATE_FORMAT).format(new Date()),"这是一条关于任务结果的测试信息",0); +// emailService.sendEmailForMissionBySeqId(2570l, 6, 85l, emailInfo2); + emailService.sendEmailForMission(2570l, "任务XXX", "这是一条关于任务状态的测试信息"); + + pl("测试邮件入库完毕"); + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + + } + private static void pl(Object object) { + System.out.println(object==null?null:object.toString()); + } +} diff --git a/src/com/nms/server/service/ThreadService.java b/src/com/nms/server/service/ThreadService.java new file mode 100644 index 0000000..89e9bf0 --- /dev/null +++ b/src/com/nms/server/service/ThreadService.java @@ -0,0 +1,68 @@ +package com.nms.server.service; + +import java.util.Date; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.SetInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.thread.monitor.MonitorManagerThread; + +public class ThreadService{ + private static Logger logger = Logger.getLogger(ThreadService.class); + public ThreadService(){ + + } + + /** + * 更新主动监测线程操作 + * 关闭旧线程,创建新线程 + * @time Sep 12, 2012-4:44:03 PM + * @param nm + * @param info + */ + public void updateThread(SetInfo info) { + + //- 计算启动后第一次检测时间 默认即时启动 + long initialDelay = Constants.HANDSHANK_DELAY_TIME; + if(info.getPlanCheckTime()!=null){ + initialDelay = info.getPlanCheckTime().longValue() - (new Date().getTime()); + initialDelay = initialDelay > 0l ? initialDelay : 0l; + } + + //- 设置名称 + String processIden = StringUtils.isEmpty(info.getProcessIden())?"":info.getProcessIden(); + + //-线程名称 + String runnableName = info.getCheckTypeName()+"_"+processIden; + + //- 撤销旧监测线程 + Common.cancelRunnableAtOnce(runnableName); //撤销 旧任务 + + //- 监测设置状态无效 结束操作--对于变更,如果修改为无效,则不再启动新监测 + if(StringUtils.isEmpty(info.getCheckState()) || "0".equals(info.getCheckState())){//0无效;1有效 + return; + } + + //- 监测方式 非主动 结束操作--为了严谨 + if(StringUtils.isEmpty(info.getCheckWay()) || "1".equals(info.getCheckWay())){//0主动,1被动 + return; + } + logger.info("主动监测设置启动 "+runnableName); + + //- 创建线程 + Runnable runnable = new MonitorManagerThread(runnableName,info); + + //- 添加到 定时线程池 + ScheduledFuture future = Common.scheduled.scheduleAtFixedRate(runnable, initialDelay/1000, info.getCheckGap()*60, TimeUnit.SECONDS); + //- 注册线程 + Common.registRunnable(runnableName, future); + + } + + +} diff --git a/src/com/nms/server/service/UpgradeService.java b/src/com/nms/server/service/UpgradeService.java new file mode 100644 index 0000000..dc47870 --- /dev/null +++ b/src/com/nms/server/service/UpgradeService.java @@ -0,0 +1,3470 @@ +package com.nms.server.service; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.EmailInfo; +import com.nms.server.bean.LoopMissionRoundInfo; +import com.nms.server.bean.MissionResult; +import com.nms.server.bean.MissionResult2; +import com.nms.server.bean.MissionStateTable; +import com.nms.server.bean.Task1; +import com.nms.server.bean.Task4; +import com.nms.server.bean.Task6; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.common.EmailTypeConstants; +import com.nms.server.common.MissionConstants; +import com.nms.server.dao.CommonDao; +import com.nms.server.thread.mission.NMSClientTask4Cancel; +import com.nms.server.util.BoneCPPool; +import com.nms.server.util.DateUtil; +import com.nms.server.util.MD5Util; +import com.nms.server.util.socket.SocketUtils; +import com.socket.utils.FileComment; + + +public class UpgradeService extends CommonService{ + private final Logger logger = Logger.getLogger(UpgradeService.class); + private final SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); //Java Date 类型数据格式化格式 +// private CommonDao dao = null; + public UpgradeService(CommonDao dao){ + super(dao); +// this.dao = dao; + } + /** + * 获取新任务信息,加载到 任务执行操作中 + * @time Mar 23, 2012-3:28:41 PM + * @param missionId 为空时,不指定missionId查询 + * @return + */ + public ArrayList getNewMessionList(Long missionId){ + //-- 查询可执行的任务 + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mst.mission_id,"); + searchSQL.append("mst.mission_type,"); + searchSQL.append("mst.mission_state,"); + searchSQL.append("mst.system_id,"); + searchSQL.append("mst.group_id,"); + searchSQL.append("to_char(mst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(mst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time,"); + searchSQL.append("mst.is_loop,"); + searchSQL.append("mst.loop_delay "); + searchSQL.append("from mission_state_table mst "); + searchSQL.append("where 1=1 "); + //2012-03-20 取消任务时间过滤,将对所有任务进行处理 +// searchSQL.append("and (mst.end_time>sysdate or mst.end_time is null) ");// 周期任务(未过时) or 非周期任务 + if (missionId!=null && missionId.longValue() !=0l) { + searchSQL.append("and mst.mission_id = '"+missionId.longValue()+"' "); + }else{ + searchSQL.append("and mst.mission_state in (1,5) "); //以创建任务 准备撤销任务 均作为新任务周期性载入 + } + searchSQL.append("order by mst.create_time,mst.mission_id asc "); + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("mission_type"); + fields.add("mission_state"); + fields.add("system_id"); + fields.add("group_id"); + fields.add("start_time"); + fields.add("end_time"); + fields.add("is_loop"); + fields.add("loop_delay"); + ArrayList> rs = null; + ArrayList mstList = new ArrayList(); + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + MissionStateTable mission = new MissionStateTable(); +// mission.setMissionName(maps.get("mission_name")); + mission.setMissionId(StringUtils.isEmpty(maps.get("mission_id"))?null:Long.parseLong(maps.get("mission_id"))); + mission.setMissionType(StringUtils.isEmpty(maps.get("mission_type"))?null:Long.parseLong(maps.get("mission_type")) ); + mission.setMissionState(StringUtils.isEmpty(maps.get("mission_state"))?null:Long.parseLong(maps.get("mission_state"))); + mission.setSystemId(StringUtils.isEmpty(maps.get("system_id"))?null:Long.parseLong(maps.get("system_id"))); + mission.setGroupId(StringUtils.isEmpty(maps.get("group_id"))?null:Long.parseLong(maps.get("group_id"))); + mission.setStartTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time"))); + mission.setEndTime(StringUtils.isEmpty(maps.get("end_time"))?null:format.parse(maps.get("end_time"))); + mission.setLoopFlag(StringUtils.isEmpty(maps.get("is_loop"))?null:Long.parseLong(maps.get("is_loop"))); + mission.setLoopDelay(StringUtils.isEmpty(maps.get("loop_delay"))?null:Long.parseLong(maps.get("loop_delay"))); + mstList.add(mission); + } + } + return mstList; + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return mstList; + } + + /** + * 获取正在执行状态的任务 + * 用于任务下发线程初始化操作 + * @time Mar 23, 2012-3:28:05 PM + * @param ids + * @return + */ + public ArrayList getStartMessionList(String ids){ + //-- 查询可执行的任务 + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mst.mission_id,"); + searchSQL.append("mst.mission_type,"); + searchSQL.append("mst.mission_state,"); + searchSQL.append("mst.system_id,"); + searchSQL.append("mst.group_id,"); + searchSQL.append("to_char(mst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(mst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time,"); + searchSQL.append("mst.is_loop,"); + searchSQL.append("mst.loop_delay "); + searchSQL.append("from mission_state_table mst "); + searchSQL.append("where 1=1 "); +// searchSQL.append("and (mst.end_time>sysdate or mst.end_time is null) "); + searchSQL.append("and mst.mission_state in(2,6) "); + if(StringUtils.isNotEmpty(ids)){ + searchSQL.append("and mst.mission_id in ("+ids+") "); + } + searchSQL.append("order by mst.create_time,mst.mission_id asc "); + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("mission_type"); + fields.add("mission_state"); + fields.add("system_id"); + fields.add("group_id"); + fields.add("start_time"); + fields.add("end_time"); + fields.add("is_loop"); + fields.add("loop_delay"); + ArrayList> rs = null; + ArrayList mstList = new ArrayList(); + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + MissionStateTable mission = new MissionStateTable(); + mission.setMissionId(StringUtils.isEmpty(maps.get("mission_id"))?null:Long.parseLong(maps.get("mission_id"))); + mission.setMissionType(StringUtils.isEmpty(maps.get("mission_type"))?null:Long.parseLong(maps.get("mission_type")) ); + mission.setMissionState(StringUtils.isEmpty(maps.get("mission_state"))?null:Long.parseLong(maps.get("mission_state"))); + mission.setSystemId(StringUtils.isEmpty(maps.get("system_id"))?null:Long.parseLong(maps.get("system_id"))); + mission.setGroupId(StringUtils.isEmpty(maps.get("group_id"))?null:Long.parseLong(maps.get("group_id"))); + mission.setStartTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time"))); + mission.setEndTime(StringUtils.isEmpty(maps.get("end_time"))?null:format.parse(maps.get("end_time"))); + mission.setLoopFlag(StringUtils.isEmpty(maps.get("is_loop"))?null:Long.parseLong(maps.get("is_loop"))); + mission.setLoopDelay(StringUtils.isEmpty(maps.get("loop_delay"))?null:Long.parseLong(maps.get("loop_delay"))); + mstList.add(mission); + } + } + return mstList; + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return mstList; + } + + + public JSONArray getMissionForAgentStart(Long uuid){ + try{ + if(uuid==null || uuid.longValue() == 0l){ + return null; + } + StringBuffer groupsIdsSQL = new StringBuffer(); + //当节点组不为空时,查询该节点组的叶子节点组及其父系节点组ID + if(uuid != null ){ + //--正向查询SQL 查询相关叶子节点组 + StringBuffer groupsIdsSQLN = new StringBuffer(); + groupsIdsSQLN.append("select nt.node_group_id from NODE_TABLE nt where 1=1 "); + groupsIdsSQLN.append("and nt.seq_id = "+uuid+" and nt.node_state=0 "); + + //--逆向查询SQL 查询包括叶子节点组在内的父节点组 +// StringBuffer groupsIdsSQLW = new StringBuffer(); +// groupsIdsSQLW.append("select distinct ngt.group_id from NODEGROUP_TABLE ngt "); +// groupsIdsSQLW.append("start with ngt.group_id in ( "); +// groupsIdsSQLW.append(groupsIdsSQLN); +// groupsIdsSQLW.append(") connect by prior ngt.parent_group_id = ngt.group_id "); +// groupsIdsSQL.append(groupsIdsSQLW); + //@2018年4月26日15:56:55 适配mysql 数据库 + String groupIds = dao.startWithConnectByReverse("NODEGROUP_TABLE", null, "group_id in ( " + groupsIdsSQLN.toString() +" ) ", "group_id", "parent_group_id"); + groupsIdsSQL.append(groupIds); + + + } + + String missionSql4 = "select distinct t.mission_id from v_mission_node_group_4 t where t.node_group_id in ("+groupsIdsSQL+")"; + String missionSql6 = "select distinct t.mission_id from v_mission_node_group_6 t where t.node_group_id in ("+groupsIdsSQL+")"; + JSONArray infoArray = new JSONArray(); + ArrayList fields = new ArrayList(); + StringBuffer searchSql = new StringBuffer(); + ArrayList> mapsList = new ArrayList>(); + //-- Task4 For once Mission + mapsList.clear(); + fields.clear(); + fields.add("mission_id"); + fields.add("mission_type"); + fields.add("command_type"); + fields.add("command_ename"); + fields.add("command_para"); + fields.add("start_time"); + fields.add("end_time"); + fields.add("result"); + fields.add("version"); + searchSql.delete(0, searchSql.length()); + searchSql.append("select t.mission_id,t.mission_type,to_char(t.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,to_char(t.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time, "); + searchSql.append("mpt4.command_type, mpt4.command_ename, mpt4.command_para, mrt4.result,mpt4.version "); + searchSql.append("from mission_state_table t "); + searchSql.append("left join mission_parameter_table4 mpt4 on t.mission_id = mpt4.mission_id "); + searchSql.append("left join mission_result_table4 mrt4 on mrt4.mission_id = mpt4.mission_id "); +// searchSql.append("left join node_table nt on nt.node_group_id = mpt4.node_group_id and nt.system_id = t.system_id "); + searchSql.append("where (t.end_time>sysdate or t.end_time is null) "); + searchSql.append("and t.mission_state =2 "); + searchSql.append("and t.is_loop =0 "); //非周期任务 +// searchSql.append("and mpt4.node_group_id in( "+groupsIdsSQL+") "); + searchSql.append("and mpt4.mission_id in( "+missionSql4+") "); + searchSql.append("and mrt4.result >= 40 "); + searchSql.append("and mrt4.seq_id ="+uuid+" "); + searchSql.append("order by t.create_time asc "); + mapsList = dao.dbSelect(searchSql.toString(), fields); + String split = "|"; + if(mapsList!=null && mapsList.size()>0){ + for(Map map : mapsList){ + Task4 task4 = new Task4(); + JSONObject object = new JSONObject(); + task4.setTaskId(StringUtils.isNotEmpty(map.get("mission_id"))?Long.parseLong(map.get("mission_id")):null); + task4.setTaskType(StringUtils.isNotEmpty(map.get("mission_type"))?Long.parseLong(map.get("mission_type")):null); + task4.setCommandName(map.get("command_ename")); + if(StringUtils.isNotEmpty(map.get("command_para"))){ + StringBuffer newStrs = new StringBuffer(); + String[] strs = map.get("command_para").split("\\|"); + for(String str : strs){ + if(DateUtil.isDate(str,Constants.COMMON_DATE_FORMAT)){ + newStrs.append(split+format.parse(str).getTime()); + }else{ + newStrs.append(split+str); + } + } + newStrs = newStrs.length()>0?newStrs.delete(0, split.length()):newStrs; + task4.setCommandParam(newStrs.toString()); + } + task4.setStartTime(StringUtils.isEmpty(map.get("start_time"))?null:format.parse(map.get("start_time")).getTime()); + task4.setEndTime(StringUtils.isEmpty(map.get("end_time"))?null:format.parse(map.get("end_time")).getTime()); + task4.setCommandType(StringUtils.isNotEmpty(map.get("command_type"))?Long.parseLong(map.get("command_type")):null); + task4.setState(StringUtils.isNotEmpty(map.get("result"))?Long.parseLong(map.get("result")):null); + task4.setVersion(StringUtils.isNotEmpty(map.get("version"))?Long.parseLong(map.get("version")):null); + object.put("typeInfo", task4.getTaskType()); + object.put("taskInfo", task4); + infoArray.add(object); + } + } + //-- Task4 For LoopMission + mapsList.clear(); + fields.clear(); + fields.add("mission_id"); + fields.add("mission_type"); + fields.add("command_type"); + fields.add("command_ename"); + fields.add("command_para"); + fields.add("start_time"); + fields.add("end_time"); + fields.add("result"); + fields.add("version"); + fields.add("is_loop"); + fields.add("loop_delay"); + fields.add("mission_state"); + + searchSql.delete(0, searchSql.length()); + searchSql.append("select t.mission_id,t.mission_type,to_char(t.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,to_char(t.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time, "); + searchSql.append("mpt4.command_type, mpt4.command_ename, mpt4.command_para, mrt4.result,mpt4.version,t.is_loop,t.loop_delay,t.mission_state "); + searchSql.append("from mission_state_table t "); + searchSql.append("left join mission_parameter_table4 mpt4 on t.mission_id = mpt4.mission_id "); + searchSql.append("left join loopmission_state_table lmst on lmst.mission_id = t.mission_id "); + searchSql.append("left join mission_result_table4 mrt4 on mrt4.mission_id = lmst.cur_mission_id "); +// searchSql.append("left join node_table nt on nt.node_group_id = mpt4.node_group_id and nt.system_id = t.system_id "); + searchSql.append("where 1=1 "); + searchSql.append("and t.mission_state =2 "); + searchSql.append("and lmst.cur_mission_id in ( select max(lmst.cur_mission_id) from loopmission_state_table lmst where lmst.mission_state in (1,2) group by lmst.mission_id)"); + searchSql.append("and t.is_loop =1 "); +// searchSql.append("and mpt4.node_group_id in( "+groupsIdsSQL+") "); + searchSql.append("and mpt4.mission_id in( "+missionSql4+") "); +// searchSql.append("and nt.seq_id = "+uuid.longValue()+" "); + searchSql.append("and mrt4.result >= 40 "); + searchSql.append("and mrt4.seq_id ="+uuid+" "); + searchSql.append("order by t.create_time asc "); + mapsList = dao.dbSelect(searchSql.toString(), fields); + split = "|"; + if(mapsList!=null && mapsList.size()>0){ + for(Map map : mapsList){ + Task4 task4 = new Task4(); + JSONObject object = new JSONObject(); + task4.setTaskId(StringUtils.isNotEmpty(map.get("mission_id"))?Long.parseLong(map.get("mission_id")):null); + task4.setTaskType(StringUtils.isNotEmpty(map.get("mission_type"))?Long.parseLong(map.get("mission_type")):null); + task4.setCommandName(map.get("command_ename")); + if(StringUtils.isNotEmpty(map.get("command_para"))){ + StringBuffer newStrs = new StringBuffer(); + String[] strs = map.get("command_para").split("\\|"); + for(String str : strs){ + if(DateUtil.isDate(str,Constants.COMMON_DATE_FORMAT)){ + newStrs.append(split+format.parse(str).getTime()); + }else{ + newStrs.append(split+str); + } + } + newStrs = newStrs.length()>0?newStrs.delete(0, split.length()):newStrs; + task4.setCommandParam(newStrs.toString()); + } + task4.setStartTime(StringUtils.isEmpty(map.get("start_time"))?null:format.parse(map.get("start_time")).getTime()); + task4.setEndTime(StringUtils.isEmpty(map.get("end_time"))?null:format.parse(map.get("end_time")).getTime()); + task4.setCommandType(StringUtils.isNotEmpty(map.get("command_type"))?Long.parseLong(map.get("command_type")):null); + task4.setState(StringUtils.isNotEmpty(map.get("result"))?Long.parseLong(map.get("result")):null); + task4.setVersion(StringUtils.isNotEmpty(map.get("version"))?Long.parseLong(map.get("version")):null); + task4.setIsLoop(StringUtils.isNotEmpty(map.get("is_loop"))?Long.parseLong(map.get("is_loop")):null); + task4.setLoopDelay(StringUtils.isNotEmpty(map.get("loop_delay"))?Long.parseLong(map.get("loop_delay")):null); + task4.setMissionState(StringUtils.isNotEmpty(map.get("mission_state"))?Long.parseLong(map.get("mission_state")):null); + object.put("typeInfo", task4.getTaskType()); + object.put("taskInfo", task4); + infoArray.add(object); + } + } + + //-- Task6 + mapsList.clear(); + fields.clear(); + fields.add("mission_id"); + fields.add("mission_type"); + fields.add("command_type"); + fields.add("command_ename"); + fields.add("command_para"); + fields.add("source_path"); + fields.add("target_path"); + fields.add("check_value"); + fields.add("start_time"); + fields.add("result"); + fields.add("version"); + fields.add("old_task_id"); + searchSql.delete(0, searchSql.length()); + searchSql.append("select t.mission_id,t.mission_type, "); + searchSql.append("mpt6.old_task_id, "); + searchSql.append("mpt6.command_type, mpt6.command_ename, mpt6.command_para,mpt6.source_path,mpt6.target_path,mpt6.check_value,to_char(mpt6.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time, mrt6.result,mpt6.version "); + searchSql.append("from mission_state_table t "); + searchSql.append("left join mission_parameter_table6 mpt6 on t.mission_id = mpt6.mission_id "); + searchSql.append("left join mission_result_table6 mrt6 on mrt6.mission_id = mpt6.mission_id "); +// searchSql.append("left join node_table nt on nt.node_group_id = mpt4.node_group_id and nt.system_id = t.system_id "); + searchSql.append("where (t.end_time>sysdate or t.end_time is null) "); + searchSql.append("and t.mission_state =2 "); +// searchSql.append("and mpt6.node_group_id in( "+groupsIdsSQL+") "); + searchSql.append("and mpt6.mission_id in( "+missionSql6+") "); +// searchSql.append("and nt.seq_id = "+uuid.longValue()+" "); + searchSql.append("and mrt6.result >= 40 "); + searchSql.append("and mrt6.seq_id ="+uuid+" "); + searchSql.append("order by t.create_time asc "); + mapsList = dao.dbSelect(searchSql.toString(), fields); + if(mapsList!=null && mapsList.size()>0){ + for(Map map : mapsList){ + Task6 task6 = new Task6(); + JSONObject object = new JSONObject(); + task6.setTaskId(StringUtils.isNotEmpty(map.get("mission_id"))?Long.parseLong(map.get("mission_id")):null); + task6.setTaskType(StringUtils.isNotEmpty(map.get("mission_type"))?Long.parseLong(map.get("mission_type")):null); + task6.setCommandName(map.get("command_ename")); + task6.setCommandType(StringUtils.isNotEmpty(map.get("command_type"))?Long.parseLong(map.get("command_type")):null); + task6.setCommandParam(map.get("command_para")); + task6.setFileName( SocketUtils.removeTimeTagFileName(map.get("source_path"))); + task6.setDestPath(map.get("target_path")); + task6.setMd5Value(map.get("check_value")); + task6.setUpgradeTime(StringUtils.isEmpty(map.get("start_time"))?null:format.parse(map.get("start_time")).getTime()); + task6.setOldTaskId(StringUtils.isNotEmpty(map.get("old_task_id"))?Long.parseLong(map.get("old_task_id")):null); + task6.setState(StringUtils.isNotEmpty(map.get("result"))?Long.parseLong(map.get("result")):null); + task6.setVersion(StringUtils.isNotEmpty(map.get("version"))?Long.parseLong(map.get("version")):null); + object.put("typeInfo", task6.getTaskType()); + object.put("taskInfo", task6); + infoArray.add(object); + } + } + return infoArray; + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return null; + } + + /** + * 根据任务ID 获取任务信息用于 MissionInfo1 + * @time Oct 25, 2011-10:19:00 AM + * @param missionId 可为空,为空时,全校验,非空时 校验指定Id 返回 + */ + public MissionStateTable getMessionById(Long missionId){ + //-- 查询可执行的任务 + StringBuffer searchSQL = new StringBuffer(); + if(missionId==null || missionId.longValue() == 0l){ + return null; + } + searchSQL.append("select mst.mission_id,"); + searchSQL.append("mst.mission_type,"); + searchSQL.append("mst.mission_state,"); + searchSQL.append("mst.system_id,"); + searchSQL.append("mst.group_id,"); + searchSQL.append("to_char(mst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(mst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time,"); + searchSQL.append("mst.is_loop,"); + searchSQL.append("mst.loop_delay "); + searchSQL.append("from mission_state_table mst "); + searchSQL.append("where 1=1 "); +// searchSQL.append("and (mst.end_time>sysdate or mst.end_time is null) "); +// searchSQL.append("and mst.mission_state in (1,2,3,4) "); + searchSQL.append("and mst.mission_id = '"+missionId+"' "); + searchSQL.append("order by mst.create_time asc "); + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("mission_type"); + fields.add("mission_state"); + fields.add("system_id"); + fields.add("group_id"); + fields.add("start_time"); + fields.add("end_time"); + fields.add("is_loop"); + fields.add("loop_delay"); + ArrayList> rs = null; + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + MissionStateTable mission = new MissionStateTable(); + if(rs!=null && rs.size()>0){ + Map maps = rs.get(0); + mission.setMissionId(StringUtils.isEmpty(maps.get("mission_id"))?null:Long.parseLong(maps.get("mission_id"))); + mission.setMissionType(StringUtils.isEmpty(maps.get("mission_type"))?null:Long.parseLong(maps.get("mission_type")) ); + mission.setMissionState(StringUtils.isEmpty(maps.get("mission_state"))?null:Long.parseLong(maps.get("mission_state"))); + mission.setSystemId(StringUtils.isEmpty(maps.get("system_id"))?null:Long.parseLong(maps.get("system_id"))); + mission.setGroupId(StringUtils.isEmpty(maps.get("group_id"))?null:Long.parseLong(maps.get("group_id"))); + mission.setStartTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time"))); + mission.setEndTime(StringUtils.isEmpty(maps.get("end_time"))?null:format.parse(maps.get("end_time"))); + mission.setLoopFlag(StringUtils.isEmpty(maps.get("is_loop"))?null:Long.parseLong(maps.get("is_loop"))); + mission.setLoopDelay(StringUtils.isEmpty(maps.get("loop_delay"))?null:Long.parseLong(maps.get("loop_delay"))); + } + return mission; + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return null; + } + + /** + * 校验并初始化周期任务轮次信息
+ *

设计说明:
+ * 1、校验 缓存中是否存在 该周期任务信息,不存在 则添加
+ * 2、校验 缓存中是否存在 该周期任务的轮次信息 不存在 则添加
+ * 3、校验 缓存中 轮次信息是否 符合要求:
+ * a、周期判定[startTime,endTime)
+ * b、保证周期任务的轮次信息的连续性和非重复性(起始时间 至 结束时间 无遗漏 无交集)
+ * c、过期的轮次信息创建 其状态为 4(未能执行)
+ * d、预置周期(轮次起始时间大于当前系统时间)数量
+ * 为 Constants.COMMON_MISSION_LOOP_PRESET_NUMBER
+ * e、新预置轮次信息的状态为 0
+ * @time Feb 13, 2012-10:40:41 AM + * @param missionId + * @param startTime + * @param endTime + * @param missionCycle + */ + public void checkAndInitMissionCycle (MissionStateTable mission) throws Exception{ + + /**参数有效性校验*/ + if (mission==null || mission.getMissionId() == null + || mission.getStartTime() == null + || mission.getEndTime() == null + || mission.getLoopFlag() == null ) { + return; + } + + /**设计实现*/ + //- 校验 缓存中是否存在周期任务 + if(Common.getLoopMissionInfoMap().get(mission.getMissionId())==null){ + this.updateLoopMissionInfo(Common.getLoopMissionInfoMap(), mission.getMissionId()); //将周期任务保存到缓存中 + mission = Common.getLoopMissionInfoMap().get(mission.getMissionId()); + } + + /**参数有效性再次校验:出现过经过再次获取Mission仍然mission为null的情况*/ + if (mission==null || mission.getMissionId() == null + || mission.getStartTime() == null + || mission.getEndTime() == null + || mission.getLoopFlag() == null ) { + return; + } + + //- 校验 缓存中是否存在周期任务的轮次信息 + this.updateLoopMissionRoundInfo(Common.getLoopMissionRoundInfoList(), mission.getMissionId()); //更新指定周期任务ID的轮次信息 + List roundInfoList = Common.getLoopMissionRoundInfoList().get(mission.getMissionId()); +// if(roundInfoList==null || roundInfoList.size()==0){ +// roundInfoList = Common.getLoopMissionRoundInfoList().get(mission.getMissionId()); +// } + + //- 校验 轮次信息是否符合要求 + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + long timeStart = mission.getStartTime().getTime(); // 任务的周期起始时间点(毫秒) + long timeEnd = mission.getEndTime().getTime(); // 任务的周期结束时间点(毫秒) + long timeDelay = mission.getLoopDelay()*60*1000; // 任务的周期值(毫秒) + int preset = Constants.MISSION_LOOP_PRESET_NUMBER; //preset预置数 + + //- 新创建任务 时间起始点为当前系统时间 +// if(mission.getMissionState().longValue()==1l){ +// if(timeStart 当前系统时间 + * 调整后的timeStart 为roundInfoList最后一个对象的结束时间 + * */ + + //-- 第一步 获取已存在的轮次信息 调整周期的起始时间、结束时间和预置数 + if(roundInfoList!=null && roundInfoList.size()>0){ //继续校验,并检查 + LoopMissionRoundInfo roundInfo = null; + for (Iterator roundInfoIte = roundInfoList.iterator(); roundInfoIte.hasNext();) { + roundInfo = roundInfoIte.next(); + + if(roundInfo.getStartTime().getTime() > System.currentTimeMillis()){ //预置轮次信息数量判断 + preset--; + } + timeStart = timeStart>roundInfo.getEndTime().getTime()?timeStart:roundInfo.getEndTime().getTime(); + } + } + + //-- 第二步 补齐新轮次信息 + List cycleList = new LinkedList(); //新轮次信息集合 + a: while(preset > 0){ + long timePoint = timeStart+timeDelay; //周期游标 + int cycleState = 0; + if(timePoint > timeEnd){ //周期游标 末端调整 + break; + } + + /** + 周期任务的周期判定:“前闭后开”区间,即:包含起始时间点,不包含结束时间点。 + 当前周期与之后的周期状态均为0(预置),过期周期状态为4(未能执行) + * */ + if(timeStart > System.currentTimeMillis()){ //预置周期 + cycleState = 0; + preset--; + }else if(timePoint < System.currentTimeMillis()){ // 过期周期 + cycleState = 4; + }else{ //当前周期[上一周期的timePoint,下一周期的timeStart) + cycleState = 0; + } + cycleList.add(new String[]{mission.getMissionId()+"",cycleState+"", format.format(timeStart),format.format(timePoint)}); + timeStart = timePoint; + if(timeEnd == timePoint){ + break a; + } + } + + //-- 第三步 保存 并 更新 轮次信息 + try { + dao.setAutoCommit(false); + + if(cycleList.size()>0){ + String sql = "insert into loopmission_state_table (mission_id,mission_state,start_time,end_time) values (?,?,to_date(?,'"+Constants.DB_DATE_FORMAT+"'),to_date(?,'"+Constants.DB_DATE_FORMAT+"')) "; + dao.dbUpdateByBatch(sql, cycleList); + } + + //-- 更新缓存中的周期任务的 轮次信息 + if(cycleList.size()>0){ + this.updateLoopMissionRoundInfo(Common.getLoopMissionRoundInfoList(), mission.getMissionId()); + } + + //if(mission.getMissionState()== MissionConstants.MISSION_STATE_CREATE){ + //-- 修改任务状态为 mission_state = 2已开始执行 + // dao.dbUpdate("update mission_state_table mst set mst.mission_state = ? , mst.AUTO_DESC=? where mst.mission_id= ?",MissionConstants.MISSION_STATE_RUNNING_FAIL,MissionConstants.ERROR_TASK_NO_USED_NODE,mission.getMissionId().longValue()); +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 2 where mst.mission_id="+mission.getMissionId().longValue()); + //} + + dao.commit(); + } catch (Exception e) { + dao.rollback(); + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + /** + * 针对任务已创建状态的任务,为其初始化任务结果集 + * 查询任务状态,校验是否执行初始化 + * 查询初始化结果的节点,保存入库 + * @time Oct 25, 2011-11:00:08 AM + * @param missionId 任务Id + * @param systemId 系统Id + * @param nodeGroupId 节点组IDs 多个逗号隔开,不能超过1000 + * @param nodeIpsId 节点IDs 多个节点逗号隔开,不能超过1000 + * @param missionType 任务类型 + */ + public void initMissionResults(Long missionId,String nodeGroupId,String nodeIpsId, int missionType) throws Exception{ + if(missionId==null || missionId.longValue()==0 || missionType==0){ + return; + } + try { + dao.setAutoCommit(false); + ArrayList fields = new ArrayList(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mst.mission_state from mission_state_table mst where mst.mission_id = "+missionId.longValue()+" and mst.mission_state<>3"); + fields.add("mission_state"); + ArrayList> rs = null; + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + if(rs!= null && rs.size()>0){ + if(rs.get(0)!=null && rs.get(0).get("mission_state")!=null && "2".equals(rs.get(0).get("mission_state").trim())){ + return; + } + } + } catch (Exception e) { + logger.error("",e); + } + searchSQL.delete(0,searchSQL.length()); + searchSQL.append("select distinct nt.seq_id "); + searchSQL.append("from node_table nt "); + searchSQL.append("where 1=1 "); + searchSQL.append(" and nvl(nt.seq_id,-1)<> -1 "); + searchSQL.append(" and nt.node_state = 0 "); + if(StringUtils.isNotBlank(nodeIpsId)){ + searchSQL.append(" and nt.NODE_ID in("+nodeIpsId+") "); + }else{ + if(StringUtils.isNotBlank(nodeGroupId)){ +// searchSQL.append(" and nt.node_group_id in( select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 start with nt.group_id in ("+nodeGroupId+") connect by prior nt.group_id = nt.parent_group_id )"); + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 ", " nt.group_id in ("+nodeGroupId+") ", "group_id", "parent_group_id"); + searchSQL.append(" and nt.node_group_id in( "+startWithConnectBy+")"); + } + } +// searchSQL.append(getIpnSelect()); + fields.clear(); + fields.add("seq_id"); + + rs = dao.dbSelect(searchSQL.toString(), fields); + //-- 循环插入结果信息 + if(rs!=null && rs.size()>0){ + String sql = " insert into mission_result_table"+missionType+" (mission_id,seq_id,result) values (?,?,?)"; + LinkedList params = new LinkedList(); + for(Map maps : rs){ + params.add(new String[]{missionId.longValue()+"",maps.get("seq_id")+"","3"}); + } + //-- 修改任务状态为 mission_state = 2已开始执行 + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 2 where mst.mission_id="+missionId.longValue()); + dao.dbUpdateByBatch(sql, params); + } + dao.commit(); + } catch (Exception e) { + try { + dao.rollback(); + } catch (Exception e1) { + logger.error("",e1); + } + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + /** + * 初始化任务信息(校验任务下发节点有效、非服务器和管理范围外Node 结果判定失败) + * 查询任务下发节点,并做校验和初始化操作 + * 1、校验无有效下发节点(任务未能执行) + * 2、校验非服务器节点(初始化该节点失败的任务结果) + * 3、校验管理范围外节点(初始化该节点失败的任务结果) + * 4、插入任务结果并更新任务状态 + * @time Oct 25, 2011-11:00:08 AM + * @param missionId 任务Id + * @param systemId 系统Id + * @param nodeGroupId 节点组IDs 多个逗号隔开,不能超过1000 + * @param nodeIpsId 节点IDs 多个节点逗号隔开,不能超过1000 + * @param missionType 任务类型 + */ + public void initMissionResults2(Long missionId,String nodeGroupId,String nodeIpsId, int missionType) throws Exception{ + /* 参数校验 */ + if(missionId==null || missionId.longValue()==0 || missionType==0){ + return; + } + + /* 任务初始化业务 */ + try { + dao.setAutoCommit(false); + ArrayList msFields = new ArrayList(); + StringBuffer msSQL = new StringBuffer(); + + //即时判断任务状态是否为已创建状态 + msFields.add("mission_state"); + msSQL.append("select mst.mission_state from mission_state_table mst where mst.mission_id = "+missionId.longValue()+" and mst.mission_state = "+MissionConstants.MISSION_STATE_CREATE); + ArrayList> missionSateMap = dao.dbSelect(msSQL.toString(), msFields); + if(missionSateMap== null || missionSateMap.size() == 0){ + return; + } + + ArrayList nodeFields = new ArrayList(); + StringBuffer nodeSQL = new StringBuffer(); + + //查询任务下发节点 + nodeFields.add("seq_id"); + nodeFields.add("node_type"); + nodeSQL.append("select distinct nt.seq_id,nt.node_type "); + nodeSQL.append("from node_table nt "); + nodeSQL.append("where 1 = 1 "); + nodeSQL.append(" and nvl(nt.seq_id,-1)<> -1 "); + nodeSQL.append(" and nt.node_state = 0 "); + if(StringUtils.isNotBlank(nodeIpsId)){ + nodeSQL.append(" and nt.NODE_ID in("+nodeIpsId+") "); + }else{ + if(StringUtils.isNotBlank(nodeGroupId)){ +// nodeSQL.append(" and nt.node_group_id in( select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 start with nt.group_id in ("+nodeGroupId+") connect by prior nt.group_id = nt.parent_group_id )"); + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 ", " nt.group_id in ("+nodeGroupId+") ", "group_id", "parent_group_id"); + nodeSQL.append(" and nt.node_group_id in( "+startWithConnectBy+")"); + } + } + ArrayList> nodeMapList = dao.dbSelect(nodeSQL.toString(), nodeFields); + + // 任务下发节点 + if(nodeMapList != null && nodeMapList.size()>0){ + + //拼写uuid串,用于校验管理范围外IP + StringBuffer uuidStr = new StringBuffer("0"); + for(Map map : nodeMapList){ + if(StringUtils.isNotEmpty(map.get("seq_id"))){ + uuidStr.append(","+map.get("seq_id")); + } + } + + //查询任务下发节点sql-管理范围外节点 + ArrayList ipnSegmentFields = new ArrayList(); + StringBuffer ipnSegmentSQL = new StringBuffer("select sis.start_ipn,sis.end_ipn from server_ip_segment sis left join server_table st on st.id = sis.server_id where st.server_state='0' and nvl(sis.segment_state,0) <>-1"); + ipnSegmentFields.add("start_ipn"); + ipnSegmentFields.add("end_ipn"); + ArrayList> omNodeMapList = dao.dbSelect(ipnSegmentSQL.toString(), ipnSegmentFields); + StringBuffer omNodeSql = new StringBuffer("select distinct nt.seq_id from node_table nt where 1=1 "); + + if(omNodeMapList!=null && omNodeMapList.size()>0){ + for(Map map : omNodeMapList){ + omNodeSql.append(" and (ipn < '"+map.get("start_ipn")+"' or ipn > '"+map.get("end_ipn")+"')"); + } + } + omNodeSql.append("and nt.seq_id in ("+uuidStr+")"); + ArrayList omNodeFields = new ArrayList(); + omNodeFields.add("seq_id"); + omNodeMapList = dao.dbSelect(omNodeSql.toString(), omNodeFields); + + //循环插入结果信息 + String sql = " insert into mission_result_table"+missionType+" (mission_id,seq_id,result,result_desc) values (?,?,?,?)"; + LinkedList params = new LinkedList(); + node:for(Map nodeMap : nodeMapList){ + if("0".equals(nodeMap.get("node_type"))){ //服务器节点 + if(omNodeMapList!= null && omNodeMapList.size()>0){ + omNode:for(Map map : omNodeMapList){ + if(nodeMap.get("seq_id").equals(map.get("seq_id"))){//管理范围外节点失败结果 + params.add(new String[]{missionId.longValue()+"",nodeMap.get("seq_id")+"","1",MissionConstants.ERROR_RESULT_UN_MANAGEMENT_NODE}); + continue node; + } + } + } + + //可下发服务器节点 + params.add(new String[]{missionId.longValue()+"",nodeMap.get("seq_id")+"","3",null}); + }else{ //非服务器 + params.add(new String[]{missionId.longValue()+"",nodeMap.get("seq_id")+"","1",MissionConstants.ERROR_RESULT_NOT_SERVER}); + } + } + + //-- 修改任务状态为 mission_state = 2已开始执行 + dao.dbUpdate("update mission_state_table mst set mst.mission_state =? , mst.AUTO_DESC=? where mst.mission_id=?",MissionConstants.MISSION_STATE_RUNNING_OK,format.format(System.currentTimeMillis())+" "+MissionConstants.NOTICE_TASK_RUNNING,missionId.longValue()); + dao.dbUpdateByBatch(sql, params); + }else{ + //无节点任务 + dao.dbUpdate("update mission_state_table mst set mst.mission_state = ? , mst.AUTO_DESC=? where mst.mission_id= ?",MissionConstants.MISSION_STATE_RUNNING_FAIL,format.format(System.currentTimeMillis())+" "+MissionConstants.ERROR_TASK_NO_USED_NODE,missionId.longValue()); + } + + dao.commit(); + } catch (Exception e) { + dao.rollback(); + throw e; + }finally{ + dao.clearConn(); + } + } + + /** + * 初始化任务信息(校验任务下发节点有效、非服务器和管理范围外Node 结果判定失败) + * 查询任务下发节点,并做校验和初始化操作 + * 1、校验无有效下发节点(任务未能执行) + * 2、校验非服务器节点(初始化该节点失败的任务结果) + * 3、校验管理范围外节点(初始化该节点失败的任务结果) + * 4、插入任务结果并更新任务状态 + * @time Oct 25, 2011-11:00:08 AM + * @param missionId 任务Id + * @param systemId 系统Id + * @param nodeGroupId 节点组IDs 多个逗号隔开,不能超过1000 + * @param nodeIpsId 节点IDs 多个节点逗号隔开,不能超过1000 + * @param missionType 任务类型 + */ + public void initLoopMissionResults2(Long curMissionId,String nodeGroupId,String nodeIpsId,int resultValue) throws Exception{ + /* 参数校验 */ + if(curMissionId==null || curMissionId.longValue()==0 ){ + return; + } + + /* 任务初始化业务 */ + try { + dao.setAutoCommit(false); + ArrayList msFields = new ArrayList(); + StringBuffer msSQL = new StringBuffer(); + + //即时判断任务状态是否为已创建状态 + msFields.add("mission_state"); + msSQL.append("select mst.mission_state from loopmission_state_table mst where mst.cur_mission_id = "+curMissionId.longValue()+" and (mst.mission_state <> "+MissionConstants.MISSION_STATE_FINISHING+" or mst.mission_state <>"+MissionConstants.MISSION_STATE_CANCEL_FINISHING+")"); + ArrayList> missionSateMap = dao.dbSelect(msSQL.toString(), msFields); + if(missionSateMap== null || missionSateMap.size() == 0){ + return; + } + + ArrayList nodeFields = new ArrayList(); + StringBuffer nodeSQL = new StringBuffer(); + + //查询任务下发节点 + nodeFields.add("seq_id"); + nodeFields.add("node_type"); + nodeSQL.append("select distinct nt.seq_id,nt.node_type "); + nodeSQL.append("from node_table nt "); + nodeSQL.append("where 1 = 1 "); + nodeSQL.append(" and nvl(nt.seq_id,-1)<> -1 "); + nodeSQL.append(" and nt.node_state = 0 "); + logger.info("curMissionId="+curMissionId+"nodeIps="+nodeIpsId); + if(StringUtils.isNotBlank(nodeIpsId)){ + nodeSQL.append(" and nt.NODE_ID in("+nodeIpsId+") "); + }else{ + if(StringUtils.isNotBlank(nodeGroupId)){ +// nodeSQL.append(" and nt.node_group_id in( select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 start with nt.group_id in ("+nodeGroupId+") connect by prior nt.group_id = nt.parent_group_id )"); + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 ", " nt.group_id in ("+nodeGroupId+") ", "group_id", "parent_group_id"); + nodeSQL.append(" and nt.node_group_id in( "+startWithConnectBy+")"); + } + } + ArrayList> nodeMapList = dao.dbSelect(nodeSQL.toString(), nodeFields); + + // 任务下发节点 + if(nodeMapList != null && nodeMapList.size()>0){ + + //拼写uuid串,用于校验管理范围外IP + StringBuffer uuidStr = new StringBuffer("0"); + for(Map map : nodeMapList){ + if(StringUtils.isNotEmpty(map.get("seq_id"))){ + uuidStr.append(","+map.get("seq_id")); + } + } + + //查询任务下发节点sql-管理范围外节点 + ArrayList ipnSegmentFields = new ArrayList(); + StringBuffer ipnSegmentSQL = new StringBuffer("select sis.start_ipn,sis.end_ipn from server_ip_segment sis left join server_table st on st.id = sis.server_id where st.server_state='0' and nvl(sis.segment_state,0) <>-1"); + ipnSegmentFields.add("start_ipn"); + ipnSegmentFields.add("end_ipn"); + ArrayList> omNodeMapList = dao.dbSelect(ipnSegmentSQL.toString(), ipnSegmentFields); + StringBuffer omNodeSql = new StringBuffer("select distinct nt.seq_id from node_table nt where 1=1 "); + + if(omNodeMapList!=null && omNodeMapList.size()>0){ + for(Map map : omNodeMapList){ + omNodeSql.append(" and (ipn < '"+map.get("start_ipn")+"' or ipn > '"+map.get("end_ipn")+"')"); + } + } + omNodeSql.append("and nt.seq_id in ("+uuidStr+")"); + ArrayList omNodeFields = new ArrayList(); + omNodeFields.add("seq_id"); + omNodeMapList = dao.dbSelect(omNodeSql.toString(), omNodeFields); + + //循环插入结果信息 + String sql = " insert into mission_result_table4 (mission_id,seq_id,result,result_desc) values (?,?,?,?)"; + LinkedList params = new LinkedList(); + + //查询当前任务的执行节点中上一周期下发失败的节点-2013-9-27 hyx + //过滤失败的思路有问题,应该过滤下发成功的,未知的默认下发失败,再次下发 + String assignSuccessSeqIds = ""; + if(resultValue==40) { + assignSuccessSeqIds = getAssignSuccessSeqIdsByMiss(curMissionId); + System.out.println("curMissId="+curMissionId+",OKSeqIds="+assignSuccessSeqIds); + logger.info("curMissId="+curMissionId+",OKSeqIds="+assignSuccessSeqIds); + } + boolean hasAssignFailNode = false;//不一定是真下发失败,当未知时,默认下发失败 +// String assignFailSeqIds = ""; +// if(resultValue==40) { +// assignFailSeqIds = getAssignFailSeqIdsByMiss(curMissionId);//当前任务当前周期的当前节点之前的状态 +// System.out.println("curMissId="+curMissionId+",failSeqIds="+assignFailSeqIds); +// logger.info("curMissId="+curMissionId+",failSeqIds="+assignFailSeqIds); +// } + + //由于其他地方导致的缓存中的轮次信息的state和数据库中的不一致,对于已经有结果的,还要初始化,所以此处验证一下,如果已经有结果了,则不做插入操作 + ArrayList hasRltFields = new ArrayList(); + hasRltFields.add("result"); + hasRltFields.add("result_detail"); + ArrayList> hasRltList = dao.dbSelect("select mrt4.result,mrt4.result_detail from mission_result_table4 mrt4 where mrt4.mission_id="+curMissionId,hasRltFields); + + if(hasRltList!=null && hasRltList.size()==0) {//此处只能防止之前插入过此次不允许重复插入,不能防止此次插入重复的记录(curMissId+seqId),也不需要,因为表中设置了联合主键 + node:for(Map nodeMap : nodeMapList){ + String seqIdTmp = nodeMap.get("seq_id")+""; + if("0".equals(nodeMap.get("node_type"))){ //服务器节点 + if(omNodeMapList!= null && omNodeMapList.size()>0){ + omNode:for(Map map : omNodeMapList){ + if(seqIdTmp.equals(map.get("seq_id"))){//管理范围外节点失败结果 + params.add(new String[]{curMissionId.longValue()+"",seqIdTmp,"1",MissionConstants.ERROR_RESULT_UN_MANAGEMENT_NODE}); + continue node; + } + } + } + + //可下发服务器节点 + if(resultValue==40) {//周期任务再次初始化任务结果时,对于下发失败的,要初始化为下发中状态:3 + //对于当前任务的执行节点中下发失败的节点,并将任务结果初始化为3,然后进行下发 + System.out.println("所有的的seq"+seqIdTmp); + logger.info("所有的的seq"+seqIdTmp); + if(!assignSuccessSeqIds.contains(","+seqIdTmp+",")) { + System.out.println("下发失败的seq"+seqIdTmp); + logger.info("下发失败的seq"+seqIdTmp); + hasAssignFailNode = true; + params.add(new String[]{curMissionId.longValue()+"",seqIdTmp,"3",null}); + }else { + params.add(new String[]{curMissionId.longValue()+"",seqIdTmp,resultValue+"",null}); + } + }else { + params.add(new String[]{curMissionId.longValue()+"",seqIdTmp,resultValue+"",null}); + } + }else{ //非服务器 + params.add(new String[]{curMissionId.longValue()+"",seqIdTmp,"1",MissionConstants.ERROR_RESULT_NOT_SERVER}); + } + } + } + + //当之前周期有下发失败的节点时,本周期继续进行下发:,2,(其实是将需要下发的任务id加入到对应的list) + if(hasAssignFailNode) { + MissionStateTable mission = getMissionInfoByCurMissionId(curMissionId); + String[] missIds = new String[1]; + if(mission!=null && mission.getMissionId()!=null) { + missIds[0] = mission.getMissionId()+""; + Common.addAssignFailMissionIds(mission.getMissionId()+""); + } + } + + //-- 修改周期任务状态为 mission_state 开始执行2 或6 + if(resultValue==3){ + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state =? where mst.cur_mission_id=?",MissionConstants.MISSION_STATE_RUNNING_OK,curMissionId.longValue()); + }else if(resultValue==5){ + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state =? where mst.cur_mission_id=?",MissionConstants.MISSION_STATE_CANCEL_RUNNING_OK,curMissionId.longValue()); + + } + dao.dbUpdateByBatch(sql, params); + }else{ + + //无节点任务 将周期置为无效 + dao.dbUpdate("update mission_state_table mst set mst.AUTO_DESC=? where mst.mission_id in (select lmst.mission_id from loopmission_state_table lmst where lmst.cur_mission_id=? )" + , format.format(new Date())+" " +MissionConstants.ERROR_TASK_RUND_NO_USED_NODE + ,curMissionId.longValue()); + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = ? where mst.cur_mission_id= ?",MissionConstants.MISSION_STATE_RUNNING_FAIL,curMissionId.longValue()); + } + + dao.commit(); + } catch (Exception e) { + dao.rollback(); + throw e; + }finally{ + dao.clearConn(); + } + } + + /** + * 初始化所有节点都不通的周期任务的结果:过期的插入下发失败的结果 + * 1.查询未过期的周期,插入结果3 + * 2.查询过期的周期,且未插入结果的,插入1,41 + */ + public void initLoopMissionForAssignFail(Long curMissionId,String nodeGroupId,String nodeIpsId,int resultVal,Integer resultDetail) throws Exception{ + /* 参数校验 */ + if(curMissionId==null || curMissionId.longValue()==0 ){ + return; + } + String rltDetailStr = resultDetail==null?"":resultDetail+""; + + /* 任务初始化业务 */ + try { + dao.setAutoCommit(false); + ArrayList msFields = new ArrayList(); + StringBuffer msSQL = new StringBuffer(); + + //即时判断任务状态是否为已创建状态 + msFields.add("mission_state"); + msSQL.append("select mst.mission_state from loopmission_state_table mst where mst.cur_mission_id = "+curMissionId.longValue()+" and (mst.mission_state <> "+MissionConstants.MISSION_STATE_FINISHING+" or mst.mission_state <>"+MissionConstants.MISSION_STATE_CANCEL_FINISHING+")"); + ArrayList> missionSateMap = dao.dbSelect(msSQL.toString(), msFields); + if(missionSateMap== null || missionSateMap.size() == 0){ + return; + } + + //查询需要插入结果的节点 + ArrayList nodeFields = new ArrayList(); + StringBuffer nodeSQL = new StringBuffer(); + //查询任务下发节点 + nodeFields.add("seq_id"); + nodeFields.add("node_type"); + nodeSQL.append("select distinct nt.seq_id,nt.node_type "); + nodeSQL.append("from node_table nt "); + nodeSQL.append("where 1 = 1 "); + nodeSQL.append(" and nvl(nt.seq_id,-1)<> -1 "); + nodeSQL.append(" and nt.node_state = 0 "); + if(StringUtils.isNotBlank(nodeIpsId)){ + nodeSQL.append(" and nt.NODE_ID in("+nodeIpsId+") "); + }else{ + if(StringUtils.isNotBlank(nodeGroupId)){ +// nodeSQL.append(" and nt.node_group_id in( select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 start with nt.group_id in ("+nodeGroupId+") connect by prior nt.group_id = nt.parent_group_id )"); + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 ", " nt.group_id in ("+nodeGroupId+") ", "group_id", "parent_group_id"); + nodeSQL.append(" and nt.node_group_id in( "+startWithConnectBy+")"); + } + } + ArrayList> nodeMapList = dao.dbSelect(nodeSQL.toString(), nodeFields); + + // 任务下发节点 + if(nodeMapList != null && nodeMapList.size()>0){ + + //拼写uuid串,用于校验管理范围外IP + StringBuffer uuidStr = new StringBuffer("0"); + for(Map map : nodeMapList){ + if(StringUtils.isNotEmpty(map.get("seq_id"))){ + uuidStr.append(","+map.get("seq_id")); + } + } + + //查询任务下发节点sql-管理范围外节点 + ArrayList ipnSegmentFields = new ArrayList(); + StringBuffer ipnSegmentSQL = new StringBuffer("select sis.start_ipn,sis.end_ipn from server_ip_segment sis left join server_table st on st.id = sis.server_id where st.server_state='0' and nvl(sis.segment_state,0) <>-1"); + ipnSegmentFields.add("start_ipn"); + ipnSegmentFields.add("end_ipn"); + ArrayList> omNodeMapList = dao.dbSelect(ipnSegmentSQL.toString(), ipnSegmentFields); + StringBuffer omNodeSql = new StringBuffer("select distinct nt.seq_id from node_table nt where 1=1 "); + + if(omNodeMapList!=null && omNodeMapList.size()>0){ + for(Map map : omNodeMapList){ + omNodeSql.append(" and (ipn < '"+map.get("start_ipn")+"' or ipn > '"+map.get("end_ipn")+"')"); + } + } + omNodeSql.append("and nt.seq_id in ("+uuidStr+")"); + ArrayList omNodeFields = new ArrayList(); + omNodeFields.add("seq_id"); + omNodeMapList = dao.dbSelect(omNodeSql.toString(), omNodeFields); + + //循环插入结果信息 + String sql = " insert into mission_result_table4 (mission_id,seq_id,result,result_detail,result_desc) values (?,?,?,?,?)"; + LinkedList params = new LinkedList(); + + //由于其他地方导致的缓存中的轮次信息的state和数据库中的不一致,对于已经有结果的,还要初始化,所以此处验证一下,如果已经有结果了,则不做插入操作 + ArrayList hasRltFields = new ArrayList(); + hasRltFields.add("result"); + hasRltFields.add("result_detail"); + ArrayList> hasRltList = dao.dbSelect("select mrt4.result,mrt4.result_detail from mission_result_table4 mrt4 where mrt4.mission_id="+curMissionId,hasRltFields); + + if(hasRltList!=null && hasRltList.size()==0) { + node:for(Map nodeMap : nodeMapList){ + String seqIdTmp = nodeMap.get("seq_id")+""; + if("0".equals(nodeMap.get("node_type"))){ //服务器节点 + if(omNodeMapList!= null && omNodeMapList.size()>0){ + omNode:for(Map map : omNodeMapList){ + if(seqIdTmp.equals(map.get("seq_id"))){//管理范围外节点失败结果 + params.add(new String[]{curMissionId.longValue()+"",seqIdTmp,resultVal+"",rltDetailStr,MissionConstants.ERROR_RESULT_UN_MANAGEMENT_NODE}); + continue node; + } + } + } + + + params.add(new String[]{curMissionId.longValue()+"",seqIdTmp,resultVal+"",rltDetailStr,null}); + }else{ //非服务器 + params.add(new String[]{curMissionId.longValue()+"",seqIdTmp,resultVal+"",rltDetailStr,MissionConstants.ERROR_RESULT_NOT_SERVER}); + } + } + } + + //-- 修改周期任务状态为 mission_state 开始执行2 或6 + if(resultVal==3){ + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state =? where mst.cur_mission_id=?",MissionConstants.MISSION_STATE_RUNNING_OK,curMissionId.longValue()); + }else if(resultVal==5){ + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state =? where mst.cur_mission_id=?",MissionConstants.MISSION_STATE_CANCEL_RUNNING_OK,curMissionId.longValue()); + + } + dao.dbUpdateByBatch(sql, params); + } +// else{//此处不会执行到,既然之前周期确定为节点都不通,表明有执行节点 +// //无节点任务 将周期置为无效 +// dao.dbUpdate("update mission_state_table mst set mst.AUTO_DESC=? where mst.mission_id in (select lmst.mission_id from loopmission_state_table lmst where lmst.cur_mission_id=? )" +// , format.format(new Date())+" " +MissionConstants.ERROR_TASK_RUND_NO_USED_NODE +// ,curMissionId.longValue()); +// dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = ? where mst.cur_mission_id= ?",MissionConstants.MISSION_STATE_RUNNING_FAIL,curMissionId.longValue()); +// } +// + dao.commit(); + } catch (Exception e) { + dao.rollback(); + throw e; + }finally{ + dao.clearConn(); + } + } + + //查询当前任务的执行节点中上一周期下发失败的节点 + private String getAssignFailSeqIdsByMiss(Long curMissionId) { + StringBuffer assignFailSeqIds = new StringBuffer(","); + try + { + if(curMissionId!=null) { + ArrayList nodeFields = new ArrayList(); + StringBuffer nodeSQL = new StringBuffer(); + + //查询任务下发节点---此sql有问题,只查完成的,如果之前没有完成的周期,那就没有下发失败的节点,其实是不知道是否下发成功,应该默认下发失败才对,NC会过滤的 + nodeFields.add("seq_id"); + nodeSQL.append(" select mrt4.seq_id from mission_result_table4 mrt4 ");//3:超找此周期中,下发失败的:result=1 and resultDetail=41 + nodeSQL.append(" where mrt4.mission_id =( "); + nodeSQL.append(" select max(lst.cur_mission_id) ");//2:(有可能查到的为null,只有一个周期的,或者之前都是未能执行的,此为第一个周期)查找主任务中当前周期之前的,且执行完成的一个周期的任务id(会不会有的周期任务的结果还没过来,导致查找的不对,条件为:小于当前周期的最大的周期,且不为4未能执行的) + nodeSQL.append(" from loopmission_state_table lst "); + nodeSQL.append(" where lst.mission_id = ( "); + nodeSQL.append(" select lstTmp.mission_id ");//1:超找当前周期对应的主任务id + nodeSQL.append(" from loopmission_state_table lstTmp "); + nodeSQL.append(" where lstTmp.cur_mission_id="+curMissionId+")"); + //下面sql语句不能加state=2,因为为2时,可能结果为3 or 40 此时不知道是否下发成功,应该默认下发失败,再次下发才对,重复下发,NC会过滤,不会重复执行,所以只能要state=3完成的,我们才能准确判断 + nodeSQL.append(" and (lst.mission_state =3 ) and lst.cur_mission_id<"+curMissionId);//lst.mission_state =3,未能执行的在结果表不会有结果,所以第一个条件!=4可以不加,不行,那就看之前是否有执行完成的? + nodeSQL.append(" ) and mrt4.result=1 and mrt4.result_detail=41"); + ArrayList> nodeMapList = dao.dbSelect(nodeSQL.toString(), nodeFields); + if(nodeMapList != null && nodeMapList.size()>0){ + for(Map map : nodeMapList){ + if(StringUtils.isNotEmpty(map.get("seq_id"))){ + assignFailSeqIds.append(map.get("seq_id")+","); + } + } + } + } + + } catch (Exception e) + { + logger.error("The node that failed to query the current task during the previous cycle is curMissionId="+curMissionId+" abnormal", e); + } + + return assignFailSeqIds.toString(); + } + + + //查询当前任务的执行节点中上一周期下发失败的节点 + private String getAssignSuccessSeqIdsByMiss(Long curMissionId) { + StringBuffer assignFailSeqIds = new StringBuffer(","); + try + { + if(curMissionId!=null) { + ArrayList nodeFields = new ArrayList(); + StringBuffer nodeSQL = new StringBuffer(); + + //查询任务下发节点 + nodeFields.add("seq_id"); + nodeSQL.append(" select distinct mrt4.seq_id from mission_result_table4 mrt4 "); + nodeSQL.append(" where mrt4.mission_id in( "); + nodeSQL.append(" select lst.cur_mission_id ");//查找当前周期之前所有周期的任务id + nodeSQL.append(" from loopmission_state_table lst "); + nodeSQL.append(" where lst.mission_id = ( "); + nodeSQL.append(" select lstTmp.mission_id "); + nodeSQL.append(" from loopmission_state_table lstTmp "); + nodeSQL.append(" where lstTmp.cur_mission_id="+curMissionId+")"); + nodeSQL.append(" and lst.cur_mission_id<"+curMissionId); + nodeSQL.append(" ) and mrt4.result in (0,40,50,60,70,80) ");//查一定 下发成功 的节点,对于result=1,result_detail!=41的,也可能是下发失败(周期任务超时时将3修改为1,不过这种情况影响不大) +// nodeSQL.append(" ) and ( mrt4.result in (0,40,50,60,70,80) or ");//查一定 下发成功 的节点 +// nodeSQL.append(" (mrt4.result=1 and (mrt4.result_detail is null or mrt4.result_detail !=41))"); +// nodeSQL.append(" )"); + ArrayList> nodeMapList = dao.dbSelect(nodeSQL.toString(), nodeFields); + if(nodeMapList != null && nodeMapList.size()>0){ + for(Map map : nodeMapList){ + if(StringUtils.isNotEmpty(map.get("seq_id"))){ + assignFailSeqIds.append(map.get("seq_id")+","); + } + } + } + } + + } catch (Exception e) + { + logger.error("The node that succeeds in the last cycle of the execution node for the current task is querying,curMissionId="+curMissionId+" abnormal", e); + } + + return assignFailSeqIds.toString(); + } + + /** + * 统计周期任务中,执行节点全都不通的情况: + * 1.最近的一个周期:1-41 或者 3(多次下发也没关系,NC会自动过滤,不会多次执行) + * 2.更新loop表的state:2 or 4 + * + */ + public List checkLoopMissAssignFail() { + List assignFailMissIds = new ArrayList(); + try + { + ArrayList missFields = new ArrayList(); + StringBuffer missSQL = new StringBuffer(); + + //查询任务下发节点 + missFields.add("mission_id"); + missFields.add("cur_mission_id"); + missFields.add("nodeNum"); + missFields.add("assignFailNum"); + missFields.add("node_groups_id"); + missFields.add("node_ips_id"); + + Date sysdateD = new Date(); + String sysdate = format.format(sysdateD);//为了统一时间,都去DC的时间 + + //查找所有节点都不通的周期任务相关的信息 + missSQL.append(" select tmp.mission_id,tmp.maxCurMissId cur_mission_id,tmp.nodeNum,tmp.assignFailNum,mpt4.node_groups_id,mpt4.node_ips_id "); + missSQL.append(" from ( "); + missSQL.append(" select tmp1.mission_id,tmp1.maxCurMissId,count(*) nodeNum, "); + missSQL.append(" sum(case when mrt4.result=1 and mrt4.result_detail=41 then 1 else 0 end) assignFailNum "); + missSQL.append(" from "); + missSQL.append(" ( "); + missSQL.append(" select mst.mission_id,max(lst.cur_mission_id) maxCurMissId "); + missSQL.append(" from mission_state_table mst "); + missSQL.append(" left join loopmission_state_table lst on lst.mission_id=mst.mission_id "); + missSQL.append(" left join mission_result_table4 mrt4 on mrt4.mission_id=lst.cur_mission_id "); + missSQL.append(" where mst.mission_type=4 and mst.is_loop=1 and mst.end_time>to_date('"+sysdate+"','yyyy-MM-dd HH24:mi:ss') ");//对于过期的,loop表的state为未能执行 + missSQL.append(" and mst.mission_state=2 ");//总任务状态为执行中 + missSQL.append(" and lst.mission_state in (3,2) ");//中间有未能执行的也没关系,只要执行完成的最新的一个周期是都不通,就认为都不通。如果加上2的状态,则下发中的周期就不会认为是网络都不通的情况,这样就不会再去下发了,所以还是要加2的状态的 + missSQL.append(" group by mst.mission_id "); + missSQL.append(" )tmp1 "); + missSQL.append(" left join mission_result_table4 mrt4 on mrt4.mission_id=tmp1.maxCurMissId "); + missSQL.append(" group by tmp1.mission_id,tmp1.maxCurMissId "); + missSQL.append(" )tmp"); + missSQL.append(" left join mission_parameter_table4 mpt4 on mpt4.mission_id = tmp.mission_id "); + missSQL.append(" left join loopmission_state_table lst2 on lst2.cur_mission_id=tmp.maxCurMissId "); + missSQL.append(" where tmp.nodeNum=assignFailNum and tmp.mission_id is not null and tmp.maxCurMissId is not null "); + + ArrayList> missMapList = dao.dbSelect(missSQL.toString(), missFields); + //有记录就说明存在全部通的周期任务 + if(missMapList != null && missMapList.size()>0){ + String initRltCurMissIdSql = " select case when lst.end_time>to_date('"+sysdate+"','yyyy-MM-dd HH24:mi:ss') and lst.start_time<=to_date('"+sysdate+"','yyyy-MM-dd HH24:mi:ss') then lst.cur_mission_id end toAssign ," +//下发失败后,再次下发时,要在周期时间内,不在周期时间内,不进行下发 + " case when lst.end_time<=to_date('"+sysdate+"','yyyy-MM-dd HH24:mi:ss') then lst.cur_mission_id end assignFail " + + " from loopmission_state_table lst " + + " left join mission_state_table mst on mst.mission_id=lst.mission_id " + + " left join mission_result_table4 mrt4 on lst.cur_mission_id=mrt4.mission_id " + + " where lst.mission_id=? and lst.cur_mission_id>? and mrt4.result is null and mst.mission_state in (2,6)"+//总任务状态为已完成的就不初始化结果了 + " and (lst.end_time<=to_date('"+sysdate+"','yyyy-MM-dd HH24:mi:ss') or (lst.start_time<=to_date('"+sysdate+"','yyyy-MM-dd HH24:mi:ss') and lst.end_time>to_date('"+sysdate+"','yyyy-MM-dd HH24:mi:ss') ))" +//下发失败后,再次下发时,要在周期时间内,不在周期时间内,不进行下发 + " order by toAssign asc"; + ArrayList curMissFields = new ArrayList(); + curMissFields.add("toAssign"); + curMissFields.add("assignFail"); + + for(Map map : missMapList){ + if(StringUtils.isNotEmpty(map.get("mission_id"))){ + assignFailMissIds.add(map.get("mission_id")); + //初始化任务结果:下发中 + Long missId = Long.parseLong(map.get("mission_id")); + Long curMissId = Long.parseLong(map.get("cur_mission_id")); + + MissionStateTable mission = this.getMessionById(missId); + + String nodeGroupIds = map.get("node_groups_id"); + String nodeIds = map.get("node_ips_id"); + + //如果已经有结果了,就不要再进行插入结果,过了时间的周期直接默认插入下发失败的结果:因为是全不通,所以要所有节点都插入下发中的结果 + ArrayList> missCurMissMapList = dao.dbSelect(initRltCurMissIdSql, curMissFields, missId,curMissId); + boolean assignOneFlag = true; + if(missCurMissMapList!=null && missCurMissMapList.size()>0) { + for(Map mapTmp : missCurMissMapList){ + LoopMissionRoundInfo roundInfo = null; + if(StringUtils.isNotBlank(mapTmp.get("toAssign")) && assignOneFlag) {//初始化下发结果:只初始化最近的未过期的一个周期,可能周期未执行完毕 + //未过期的,下发 + Long toAssignCurMissId = Long.parseLong(mapTmp.get("toAssign")); + //-- 轮次校验和初始化 + checkAndInitMissionCycle(mission); + + //-- 获取当前周期轮次 + List roundInfoList = Common.getLoopMissionRoundInfoList().get(missId); + if(roundInfoList !=null && roundInfoList.size()>0){ + //-- 获取当前周期轮次 + for (Iterator roundIte = roundInfoList.iterator(); roundIte.hasNext();) { + roundInfo = roundIte.next(); + if(roundInfo.getEndTime().compareTo(sysdateD)==1 ){//大于 + break; + } + } + logger.info("当前轮次的id"+roundInfo.getCurMissionId()+",主id"+missId); + } + + //-- 预置状态的轮次,初始化当前轮次的结果数据 + if(roundInfo!=null && roundInfo.getMissionState()!=null && roundInfo.getMissionState().longValue()==0l || roundInfo.getMissionState().longValue()==1l){ //0 预置状态,未初始化当前轮次的任务结果信息 + initLoopMissionForAssignFail(toAssignCurMissId,nodeGroupIds,nodeIds,3,null);//初始化无需下发的结果信息 3开始执行 40已下发,修改loop表的状态为执行中2 + roundInfo.setMissionState(2l); //更新缓存中的任务轮次信息状态 + } + + assignOneFlag = false;//只将符合条件(无结果,周期结束时间未过)的最小的一个周期的执行结果修改为下发中3 + continue; + } + if(StringUtils.isNotBlank(mapTmp.get("assignFail"))) {//过期的:都初始化为4 + Long assignFailCurMissId = Long.parseLong(mapTmp.get("assignFail")); + + //-- 轮次校验和初始化 + checkAndInitMissionCycle(mission); + + //-- 获取当前周期轮次 + List roundInfoList = Common.getLoopMissionRoundInfoList().get(missId); + if(roundInfoList !=null && roundInfoList.size()>0){ + //-- 获取当前周期轮次 + for (Iterator roundIte = roundInfoList.iterator(); roundIte.hasNext();) { + roundInfo = roundIte.next(); + if(roundInfo.getEndTime().compareTo(sysdateD)==1 ){//大于 + break; + } + } + } + + //过期的,插入结果 + if(roundInfo!=null) { + updateLoopMissionState(assignFailCurMissId, 4);//修改loop表的状态为未能执行4,不需要插入结果了就 + roundInfo.setMissionState(4l); + } + continue; + } + } + } + } + } + } + + } catch (Exception e) + { + logger.error("In the statistical periodic task, the execution nodes are all blocked", e); + } + + return assignFailMissIds; + } + + /** + * 针对任务已创建状态的任务,为其初始化结果集 + * 最大精确到组的任务结果记录插入 + * @time Oct 25, 2011-11:00:08 AM + * @param curMissionId 任务Id + * @param systemId 系统Id + */ + public void initLoopMissionResults(Long curMissionId,String groupsId,String nodeIpsId,int resultValue) throws Exception{ + if(curMissionId==null || curMissionId.longValue()==0){ + return; + } + ArrayList fields = new ArrayList(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mst.mission_state from loopmission_state_table mst where mst.cur_mission_id = "+curMissionId.longValue()+" and mst.mission_state<>3"); + fields.add("mission_state"); + ArrayList> rs = null; + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + if(rs!= null && rs.size()>0){ + if(rs.get(0)!=null && StringUtils.isNotEmpty(rs.get(0).get("mission_state")) && ("2".equals(rs.get(0).get("mission_state").trim()) || "6".equals(rs.get(0).get("mission_state").trim()))){ + return; + } + } + } catch (Exception e) { + logger.error("",e); + } + searchSQL.delete(0,searchSQL.length()); + searchSQL.append("select distinct nt.seq_id "); + searchSQL.append("from node_table nt "); + searchSQL.append("where 1=1 "); + searchSQL.append(" and nvl(nt.seq_id,-1)<> -1 "); + searchSQL.append(" and nt.node_state = 0 "); + if(StringUtils.isNotBlank(nodeIpsId)){ + searchSQL.append(" and nt.NODE_ID in("+nodeIpsId+") "); + }else{ + if(StringUtils.isNotBlank(groupsId)){ +// searchSQL.append(" and nt.node_group_id in( select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 start with nt.group_id in ("+groupsId+") connect by prior nt.group_id = nt.parent_group_id )"); + //@2018年4月26日15:57:27 适配mysql 数据库 + String startWithConnectBy = dao.startWithConnectBy("select nt.group_id from NODEGROUP_TABLE nt where nt.leaf_group = 1 ", " nt.group_id in ("+groupsId+") ", "group_id", "parent_group_id"); + searchSQL.append(" and nt.node_group_id in( "+startWithConnectBy+")"); + } + } +// searchSQL.append(getIpnSelect()); + fields.clear(); + fields.add("seq_id"); + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + //-- 循环插入结果信息 + dao.setAutoCommit(false); + if(rs!=null && rs.size()>0){ + String sql = " insert into mission_result_table4 (mission_id,seq_id,result) values (?,?,?)"; + LinkedList params = new LinkedList(); + for(Map maps : rs){ + params.add(new String[]{curMissionId.longValue()+"",maps.get("seq_id")+"",resultValue+""}); + } + //-- 修改任务状态为 mission_state = 1 以创建 BUG loop state =5 的情况 + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = 1 where mst.cur_mission_id="+curMissionId.longValue()); + dao.dbUpdateByBatch(sql, params); + } + dao.commit(); + } catch (Exception e) { + try { + dao.rollback(); + } catch (Exception e1) { + logger.error("",e1); + } + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + public Task1 getTask1ById(Long missionId){ + if(missionId == null || missionId.longValue() == 0){ + return null; + } + Task1 task1 = new Task1(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mpt1.node_groups_id,mpt1.command_para,mpt1.NODE_IPS_ID "); + searchSQL.append("from mission_parameter_table1 mpt1 "); + searchSQL.append("where mpt1.mission_Id='"+missionId.longValue()+"' "); + ArrayList fields = new ArrayList(); + fields.add("node_groups_id"); + fields.add("NODE_IPS_ID"); + fields.add("command_para"); + ArrayList> rs = null; + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + //-- 整理到对象集合 + if(rs!=null && rs.size()>0){ + Map maps = rs.get(0); + task1.setTaskParam(StringUtils.isEmpty(maps.get("command_para"))?null: maps.get("command_para")); + task1.setNodeGroupsId(StringUtils.isEmpty(maps.get("node_groups_id"))?null: maps.get("node_groups_id")); + task1.setNodeIpsId(StringUtils.isEmpty(maps.get("NODE_IPS_ID"))?null: maps.get("NODE_IPS_ID")); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return task1; + } + public String getHostIpByMissionId(Long mId){ + String hostIp = null; + StringBuffer searchSQL = new StringBuffer(); + ArrayList fields = new ArrayList(); + fields.add("system_ip"); + ArrayList> rs = null; + try { + rs = dao.dbSelect("select st.system_ip from system_table st,mission_state_table mst where mst.system_id = st.system_id and mst.mission_id='"+mId.longValue()+"'", fields); + if(rs!=null && rs.size()>0){ + Map map = (Map) rs.get(0); + hostIp = StringUtils.isEmpty(map.get("system_ip"))?null:StringUtils.trim(map.get("system_ip")); + } + } catch (Exception e) { + logger.error("MissionId is:"+mId+" ServerIp acquisition failure",e); + } + if(StringUtils.isEmpty(hostIp)){ + logger.error("missionId is:"+mId+" ServerIp is empty"); + } + return hostIp; + } + public String getHostIpBySystemId(Long systemId){ + String hostIp = null; + StringBuffer searchSQL = new StringBuffer(); + ArrayList fields = new ArrayList(); + fields.add("system_ip"); + ArrayList> rs = null; + try { + rs = dao.dbSelect("select st.system_ip from system_table st where st.system_id='"+systemId.longValue()+"'", fields); + if(rs!=null && rs.size()>0){ + Map map = (Map) rs.get(0); + hostIp = StringUtils.isEmpty(map.get("system_ip"))?null:StringUtils.trim(map.get("system_ip")); + } + } catch (Exception e) { + logger.error("SystemId 为:"+systemId+" ServerIp acquisition failure",e); + } + if(StringUtils.isEmpty(hostIp)){ + logger.error("SystemId 为:"+systemId+" ServerIp is empty"); + } + return hostIp; + } + public List getMissionFileInformations(Long missionId){ + List pushFileList = new LinkedList(); + StringBuffer searchSQL = new StringBuffer(); + ArrayList fields = new ArrayList(); + fields.add("file_name"); + fields.add("md5_value"); + ArrayList> rs = null; + try { + rs = dao.dbSelect("select mfi.file_name,mfi.md5_value from mission_file_information mfi where mfi.mission_id='"+missionId.longValue()+"' order by mfi.id asc", fields); + if(rs!=null && rs.size()>0){ + for (Iterator iterator = rs.iterator(); iterator.hasNext();) { + Map map = (Map) iterator.next(); + +// String[] strings = new String[]{map.get("file_name"),map.get("md5_value")}; + pushFileList.add(new FileComment(map.get("file_name"),0,0,map.get("md5_value"))); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return pushFileList; + } + + public List getMissionFileInformations2(Long missionId){ + List pushFileList = new LinkedList(); + StringBuffer searchSQL = new StringBuffer(); + ArrayList fields = new ArrayList(); + fields.add("file_name"); + fields.add("md5_value"); + ArrayList> rs = null; + try { + rs = dao.dbSelect("select mfi.file_name,mfi.md5_value from mission_file_information mfi where mfi.mission_id='"+missionId.longValue()+"' order by mfi.id asc", fields); + if(rs!=null && rs.size()>0){ + for (Iterator iterator = rs.iterator(); iterator.hasNext();) { + Map map = (Map) iterator.next(); + +// String[] strings = new String[]{map.get("file_name"),map.get("md5_value")}; + pushFileList.add(new FileComment(map.get("file_name"),0,0,map.get("md5_value"))); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return pushFileList; + } + public boolean checkFileIntegrity(String fileSrc,String md5Val){ + File file = new File(fileSrc); + try { + //不存在 Return false + if(!file.exists()){ + return false; + } + + //比较MD5值 + String md5Val0 = MD5Util.getFileMD5String(file); + if(!md5Val0.equals(md5Val)){ + file.delete(); + return false; + }else + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + public Task4 getTask4ById(Long missionId){ + if(missionId == null || missionId.longValue() == 0){ + return null; + } + Task4 task4 = new Task4(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mpt4.command_Type, "); + searchSQL.append("mpt4.command_ename, "); + searchSQL.append("mpt4.command_para, "); + searchSQL.append("mpt4.node_groups_id, "); + searchSQL.append("mpt4.NODE_IPS_ID, "); + searchSQL.append("mpt4.max_return, "); + searchSQL.append("mpt4.version "); + searchSQL.append("from mission_parameter_table4 mpt4 "); + searchSQL.append("where mpt4.mission_Id='"+missionId.longValue()+"' "); + ArrayList fields = new ArrayList(); + fields.add("command_Type"); + fields.add("command_ename"); + fields.add("command_para"); + fields.add("node_groups_id"); + fields.add("NODE_IPS_ID"); + fields.add("version"); + ArrayList> rs = null; +// MissionResult result = new MissionResult(); + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + String split = ";"; + //-- 整理到对象集合 + if(rs!=null && rs.size()>0){ + Map maps = rs.get(0); + task4.setCommandName(maps.get("command_ename")); + task4.setCommandType(StringUtils.isEmpty(maps.get("command_Type"))?null:Long.parseLong(maps.get("command_Type"))); + task4.setVersion(StringUtils.isEmpty(maps.get("version"))?null:Long.parseLong(maps.get("version"))); + task4.setNodeGroupsId(StringUtils.isEmpty(maps.get("node_groups_id"))?null:maps.get("node_groups_id")); + task4.setNodeIpsId(StringUtils.isEmpty(maps.get("NODE_IPS_ID"))?null:maps.get("NODE_IPS_ID")); + if(StringUtils.isNotEmpty(maps.get("command_para"))){ + StringBuffer newStrs = new StringBuffer(); + String[] strs = maps.get("command_para").split(";"); + for(String str : strs){ + if(DateUtil.isDate(str,Constants.COMMON_DATE_FORMAT)){ + newStrs.append(split+format.parse(str).getTime()); + }else{ + newStrs.append(split+str); + } + } + newStrs = newStrs.length()>0?newStrs.delete(0, split.length()):newStrs; + task4.setCommandParam(newStrs.toString()); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return task4; + } + public Task6 getTask6ById(Long missionId){ + if(missionId == null || missionId.longValue() == 0){ + return null; + } + Task6 task6 = new Task6(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mpt6.command_Type, "); + searchSQL.append("mpt6.command_ename, "); + searchSQL.append("mpt6.command_para, "); + searchSQL.append("mpt6.node_groups_id, "); + searchSQL.append("mpt6.NODE_IPS_ID, "); + searchSQL.append("mpt6.source_path, "); + searchSQL.append("mpt6.target_path, "); + searchSQL.append("mpt6.check_value, "); + searchSQL.append("mpt6.old_task_id, "); + searchSQL.append("to_char(mpt6.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time, "); + searchSQL.append("mpt6.version "); + searchSQL.append("from mission_parameter_table6 mpt6 "); + searchSQL.append("where mpt6.mission_Id='"+missionId.longValue()+"' "); + ArrayList fields = new ArrayList(); + fields.add("command_Type"); + fields.add("command_ename"); + fields.add("command_para"); + fields.add("source_path"); + fields.add("target_path"); + fields.add("check_value"); + fields.add("start_time"); + fields.add("node_groups_id"); + fields.add("NODE_IPS_ID"); + fields.add("old_task_id"); + fields.add("version"); + ArrayList> rs = null; +// MissionResult result = new MissionResult(); + try { + rs = dao.dbSelect(searchSQL.toString(), fields); +// String split = ";"; + //-- 整理到对象集合 + if(rs!=null && rs.size()>0){ + Map maps = rs.get(0); + task6.setCommandName(maps.get("command_ename")); + task6.setCommandType(StringUtils.isEmpty(maps.get("command_Type"))?null:Long.parseLong(maps.get("command_Type"))); + task6.setCommandParam(maps.get("command_para")); + task6.setVersion(StringUtils.isEmpty(maps.get("version"))?null:Long.parseLong(maps.get("version"))); + task6.setNodeGroupsId(StringUtils.isEmpty(maps.get("node_groups_id"))?null:maps.get("node_groups_id")); + task6.setNodeIpsId(StringUtils.isEmpty(maps.get("NODE_IPS_ID"))?null:maps.get("NODE_IPS_ID")); + task6.setOldTaskId(StringUtils.isEmpty(maps.get("old_task_id"))?null:Long.parseLong(maps.get("old_task_id"))); + task6.setFileName(maps.get("source_path")); + task6.setDestPath(maps.get("target_path")); + task6.setMd5Value(maps.get("check_value")); + task6.setUpgradeTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time")).getTime()); +// if(StringUtils.isNotEmpty(maps.get("command_para"))){ +// StringBuffer newStrs = new StringBuffer(); +// String[] strs = maps.get("command_para").split(";"); +// for(String str : strs){ +// if(DateUtil.isDate(str,Constants.COMMON_DATE_FORMAT)){ +// newStrs.append(split+format.parse(str).getTime()); +// }else{ +// newStrs.append(split+str); +// } +// } +// newStrs = newStrs.length()>0?newStrs.delete(0, split.length()):newStrs; +// } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return task6; + } + + /** + * @time Oct 25, 2011-11:00:08 AM + * 文件推送任务 执行结果查询 + * 文件推送结果 result 的值有 + * 3 任务信息下发中 + * 40 任务信息下发成功(信息有NMSAgent端提供) + * 41 任务信息下发失败(信息有DataController端提供) + * @param missionId 任务Id + * @param missionState 任务的状态 + * + */ + public ArrayList getMissionResultsForTask(long missionId,int missionType){ + if(missionType==0 || missionId == 0l){ + return null; + } + ArrayList mrList = new ArrayList(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mrt.mission_id, "); + searchSQL.append("nt.node_ip, "); + searchSQL.append("mrt.seq_id, "); + searchSQL.append("nvl(mrt.result,3) result "); + searchSQL.append("from mission_result_table"+missionType+" mrt "); + searchSQL.append("left join (select distinct node_ip,ipn,seq_id from node_table n ) nt on nt.seq_id=mrt.seq_id "); + searchSQL.append("where mrt.mission_id = "+missionId+" "); + searchSQL.append("and nvl(mrt.result,3) =3 "); + searchSQL.append(getIpnSelect()); + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("node_ip"); + fields.add("result"); + fields.add("seq_id"); + ArrayList> rs = null; + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + //-- 整理到对象集合 + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + MissionResult result = new MissionResult(); + result.setMissionId(Long.parseLong(maps.get("mission_id"))); + result.setNodeIp(maps.get("node_ip")); + result.setUuid(maps.get("seq_id")==null ? null:Long.parseLong(maps.get("seq_id"))); + result.setResult(maps.get("result")==null ? null:Long.parseLong(maps.get("result"))); + mrList.add(result); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return mrList; + } + + /** + * @time Oct 25, 2011-11:00:08 AM + * 文件推送任务 执行结果查询 + * 文件推送结果 result 的值有 + * 3 任务信息下发中 + * 40 任务信息下发成功(信息有NMSAgent端提供) + * 41 任务信息下发失败(信息有DataController端提供) + * @param missionId 任务Id + * @param missionState 任务的状态 + * + */ + public ArrayList getMissionResultsForLoopTask(long missionId,int missionType,Integer resultVal){ + if(missionType==0 || missionId == 0l){ + return null; + } + + ArrayList mrList = new ArrayList(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mrt.mission_id, "); + searchSQL.append("nt.node_ip, "); + searchSQL.append("mrt.seq_id, "); + searchSQL.append("nvl(mrt.result,3) result "); + searchSQL.append("from mission_result_table"+missionType+" mrt "); + searchSQL.append("left join loopmission_state_table lmst on lmst.cur_mission_id = mrt.mission_id "); + searchSQL.append("left join (select distinct node_ip,ipn,seq_id from node_table n) nt on nt.seq_id=mrt.seq_id "); + searchSQL.append("where lmst.mission_id = "+missionId+" "); + + if(resultVal != null){ + searchSQL.append("and nvl(mrt.result,3) ="+resultVal.intValue()); + } + searchSQL.append(getIpnSelect()); + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("node_ip"); + fields.add("result"); + fields.add("seq_id"); + ArrayList> rs = null; + try { + logger.debug(searchSQL.toString()); + rs = dao.dbSelect(searchSQL.toString(), fields); + //-- 整理到对象集合 + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + MissionResult result = new MissionResult(); + result.setMissionId(Long.parseLong(maps.get("mission_id"))); + result.setNodeIp(maps.get("node_ip")); + result.setUuid(maps.get("seq_id")==null ? null:Long.parseLong(maps.get("seq_id"))); + result.setResult(maps.get("result")==null ? null:Long.parseLong(maps.get("result"))); + mrList.add(result); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return mrList; + } + + //获取 + public ArrayList getMissionResultsForLoopTaskByCurMissId(long curMissionId,Integer resultVal){ + int missionType = 4; + if(curMissionId == 0l || resultVal==null){ + return null; + } + + ArrayList mrList = new ArrayList(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mrt.mission_id, "); + searchSQL.append("nt.node_ip, "); + searchSQL.append("mrt.seq_id, "); + searchSQL.append("mrt.result result "); + searchSQL.append("from mission_result_table"+missionType+" mrt "); +// searchSQL.append("left join loopmission_state_table lmst on lmst.cur_mission_id = mrt.mission_id "); + searchSQL.append("left join (select distinct node_ip,ipn,seq_id from node_table n) nt on nt.seq_id=mrt.seq_id "); + searchSQL.append("where mrt.mission_id = "+curMissionId+" "); + + if(resultVal != null){ + searchSQL.append("and mrt.result ="+resultVal.intValue()); + } + searchSQL.append(getIpnSelect()); + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("node_ip"); + fields.add("result"); + fields.add("seq_id"); + ArrayList> rs = null; + try { + logger.debug(searchSQL.toString()); + rs = dao.dbSelect(searchSQL.toString(), fields); + //-- 整理到对象集合 + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + MissionResult result = new MissionResult(); + result.setMissionId(Long.parseLong(maps.get("mission_id"))); + result.setNodeIp(maps.get("node_ip")); + result.setUuid(maps.get("seq_id")==null ? null:Long.parseLong(maps.get("seq_id"))); + result.setResult(maps.get("result")==null ? null:Long.parseLong(maps.get("result"))); + mrList.add(result); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return mrList; + } + /** + * 批量更新任务结果方法业务流程 + * 1、检查数据完整性:任务ID、任务类型、SeqID、任务结果 均不能为空 + * 2、数据规整 + * 周期任务结果处理流程:(判定 loopFlag 为1或-1) + * 2.1、获取 获取任务轮次信息; + * 2.2、规整数据 + * 非周期任务结果处理流程: + * + * @time Nov 16, 2011-2:00:46 PM + * @param mr2List + */ + public void updateMissionResult2ByBatch(int missiontype,List mr2List) throws Exception { + List params = new ArrayList(); + + //-try{}finally{} 用于清理数据库连接 + try { + + //-任务结果List 非空判断 + if(mr2List!= null && mr2List.size()>0){ + + //- 声明 本次业务操作的各对象变量 + EmailService emailService = new EmailService(dao); //邮件功能 + String sql = getUpdateSQL(missiontype); //获取更新sql + int count = 0; //计数器 + StringBuffer cancelMissionStore = new StringBuffer(","); //撤销任务ID暂存 + + List loopMissionIdofInsertRltList = new ArrayList();//记录有结果录入的周期任务的总id,并进行轮次检查和更新(因为如果没轮次了再检测,可能任务的总状态就被统计为已完成了,之后的结果也就不会被统计上了) + + //- 遍历任务结果集 + Iterator mrIte = mr2List.iterator(); + while (mrIte.hasNext()) { + + /* 任务结果: 完整性校验、数据规整、保存 等操作 */ + MissionResult2 mr = (MissionResult2) mrIte.next(); + + //- 完整性校验: + if(mr == null){ //无效跳过 + continue; + } + + if(mr.getMissionId() ==null + || mr.getMissionType() == null + || mr.getUuid() ==null +// || mr.getResult() == null //结果为空时,触发器会采用之前的结果,回传文件的结果信息result为null,所以之前的回传文件都为空 + ){ //无效跳过 + continue; + } + + + //- 数据规整:周期任务判断及处理 + //- 任务类型4 周期任务结果处理 标识判断,loopFlag: 1周期任务标识 -1:Agent无法判断任务类型,先尝试按周期任务处理 + + Long resultsMissionId = mr.getMissionId(); + if(mr.getMissionType().longValue() == 4l + && mr.getLoopFlag()!= null + && (mr.getLoopFlag().longValue()==1l || mr.getLoopFlag().longValue()==-1l)){ + + //-- 获取任务 + MissionStateTable mission = Common.getLoopMissionInfoMap().get(mr.getMissionId());//总任务id + + loopMissionIdofInsertRltList.add(mission); + + if(mission==null){ + mission = getMessionById(mr.getMissionId()); + Common.getLoopMissionInfoMap().put(mr.getMissionId(),mission); + } + + LoopMissionRoundInfo roundInfo = this.getRoundInfoFromCommonList(mr); + + if(roundInfo == null){ + + //-- 检查并预置轮次信息(执行撤销时不再运行) + if(mission.getMissionState()!=6l && mission.getMissionState()!=7l){ + checkAndInitMissionCycle(mission); //检查预置轮次数 + }else{ + this.updateLoopMissionRoundInfo(Common.getLoopMissionRoundInfoList(), mission.getMissionId()); //更新指定周期任务ID的轮次信息 + } + + //-- 再次获取 缓存中的信息 否则 查询数据库(比较旧的轮次信息情况) + roundInfo = this.getRoundInfoFromCommonList(mr); + if(roundInfo ==null){ + + roundInfo = this.getRoundInfoFromDB(mr); + //-- 该任务的轮次信息不存在 通过loopFlag 判断是否继续执行保存 + /* loopFlag 为1 周期任务结果,则抛弃,为-1 未知情况,继续非周期任务信息结果的处理 */ + if(roundInfo == null){ + if(mr.getLoopFlag().longValue()==1l ){ //无效的 周期任务数据 抛弃 + logger.warn("Unable to find cycle information of the cycle task result information 【"+(JSONObject.fromObject(mr).toString())+"】"); + continue; + }else{ //Agent无法判断任务类型 可能为一次性执行的任务 +// logger.info("对应的 roundInfo is null"+(JSONObject.fromObject(mr).toString())); + } + }else{ + List rList = Common.getLoopMissionRoundInfoList().get(mr.getMissionId()); + if(rList == null){ + rList = new LinkedList(); + Common.getLoopMissionRoundInfoList().put(mr.getMissionId(),rList); + } + rList.add(0, roundInfo); + } + } + + } + + //-- 当前轮次校验 + if(roundInfo!=null && roundInfo.getMissionState().longValue() == 0l){ //结果未被初始化,周期内仅执行一次 +// MissionStateTable mission = Common.getLoopMissionInfoMap().get(mr.getMissionId()); + initLoopMissionResults2(roundInfo.getCurMissionId(), mission.getNodeGroupsId(),mission.getNodeIpsId(),40);//初始化无需下发的结果信息 3开始执行 40已下发 + updateLoopMissionState(roundInfo.getCurMissionId(), 2); + roundInfo.setMissionState(2l); +// checkAndInitMissionCycle(mission); //检查预置轮次数 + }else + if(roundInfo!=null && roundInfo.getMissionState().longValue() == 1l){ //结果已经初始化,周期内仅执行一次 +// MissionStateTable mission = Common.getLoopMissionInfoMap().get(mr.getMissionId()); + updateLoopMissionState(roundInfo.getCurMissionId(), 2); + roundInfo.setMissionState(2l); + +// checkAndInitMissionCycle(mission); //检查预置轮次数 + } + + /**无法获取轮次信息的数据跳过 + * */ + if(roundInfo!=null){ //轮次周期 状态为“已创建”,这变更状态并初始化下一轮次 + resultsMissionId = roundInfo.getCurMissionId().longValue(); + } + + logger.debug("MissionResult2 missionId 变更 :>"+(mr.getMissionId()+" ---> "+roundInfo.getCurMissionId().longValue())); + + if(mission.getMissionState().longValue()==6){ + + //-- 检查是否已发出通知 + if(roundInfo!=null){ + if(cancelMissionStore.indexOf(","+mr.getUuid()+":"+mr.getMissionId()+",")==-1){ + cancelMissionStore.append(mr.getUuid()+":"+mr.getMissionId()+","); + if(mr.getResult()==null || (mr.getResult().longValue() != 6l && mr.getResult().longValue() != 7l )){ + MissionResult result = new MissionResult(); + result.setUuid(mr.getUuid()); + Common.sendMissionInfo(mission,new NMSClientTask4Cancel(Common.getNodeIpByUUID(mr.getUuid()),Constants.SSL_CLIENT_PORT,mission.getMissionId()),mr.getUuid(), 1,roundInfo.getStartTime().getTime(),roundInfo.getEndTime().getTime()); + } + } + } + } + } + + + //-- 任务结果保存 + String [] strs = null; + if(mr.getMissionType()!= null && mr.getMissionType().longValue()==4l){ + strs = new String[6]; + strs[0] = mr.getResult()==null?null:mr.getResult()+""; + strs[1] = (StringUtils.isEmpty(mr.getDescription()) || "null".equalsIgnoreCase(mr.getDescription()))?"":(mr.getDescription()+"\n"); + strs[2] = mr.getFileInfo(); + strs[3] = mr.getResultDetail()==null?null:mr.getResultDetail()+""; + strs[4] = resultsMissionId.longValue()+""; + strs[5] = mr.getUuid()+""; + if(StringUtils.isNotEmpty(strs[2])){ + logger.debug("准备入库(含回传文件) :"+Arrays.toString(strs)); + } + }else{ + strs = new String[4]; + strs[0] = mr.getResult()==null?"":mr.getResult()+""; + strs[1] = (StringUtils.isEmpty(mr.getDescription()) || "null".equalsIgnoreCase(mr.getDescription()))?"":(mr.getDescription()+"\n"); + strs[2] = mr.getMissionId()+""; + strs[3] = mr.getUuid()+""; + + } + logger.debug("准备入库 :"+Arrays.toString(strs)); + params.add(strs); + count++; + + //2013-6-24 hyx 注释以下代码某个节点的执行结果失败不发送邮件,任务最终完成后发一封邮件即可(包括统计信息,失败和成功节点数) + //-- 任务失败结果告警 + /* + int result = mr.getResult()==null?-1:mr.getResult().intValue(); + if(result == 1){ //需要告警 + emailService.sendEmailForMissionBySeqId(mr.getMissionId(),missiontype, mr.getUuid(), createEmailInfoForFailResult(mr)); + } + */ + if(count == Constants.DB_EXECUTE_BATCH){ + dao.dbUpdateByBatch(sql, params); + count = 0; + params.clear(); + } + } + + if(loopMissionIdofInsertRltList!=null && loopMissionIdofInsertRltList.size()>0) {//说明有周期任务的结果入库,就需要检测周期任务的轮次 + for(MissionStateTable missTmp:loopMissionIdofInsertRltList) { + //-- 检查并预置轮次信息(执行撤销时不再运行) + if(missTmp!=null) { + if(missTmp.getMissionState()!=null + && missTmp.getMissionState()!=6l && missTmp.getMissionState()!=7l){ + checkAndInitMissionCycle(missTmp); //检查预置轮次数 + }else{ + this.updateLoopMissionRoundInfo(Common.getLoopMissionRoundInfoList(), missTmp.getMissionId());//更新指定周期任务ID的轮次信息(从数据库到缓存) + } + } + + } + } + + if (count > 0) { + dao.dbUpdateByBatch(sql, params); + count = 0; + params.clear(); + } + } + logger.info("任务结果批量入库保存完成"); +// } catch (Exception e) { +// logger.error("批量更新异常 \n"+"",e); + }finally{ + dao.clearConn(); + } + } + + public static void main(String []args){ + CommonDao dao; + try { + + BoneCPPool.initPool(); + dao = new CommonDao(); + UpgradeService service = new UpgradeService(dao); + service.updateLoopMissionInfo(Common.getLoopMissionInfoMap(), null); + Map loopMissionInfoMap = new HashMap(); + loopMissionInfoMap = Common.getLoopMissionInfoMap(); + System.out.println(Common.getLoopMissionInfoMap().size()); +// service.updateLoopMissionComplete(); +// service.getMissionForAgentStart(89l); +// service.checkTimeoutLoopMisssionAndAddResult();//测试:超时周期任务的检查 + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +//// service.checkAndUpdateMissionComplete() ; +//// service.checkAndUpdateLoopMissionComplete(); +//// service.updateCommonMissionComplete();//任务状态:成功(31)、部分成功(32)、全部失败(30) +// +// List mr2List = new ArrayList(); +// mr2List.add(new MissionResult2(1274523l)); +// mr2List.add(new MissionResult2(1274523l)); +// mr2List.add(new MissionResult2(1274538l)); +// mr2List.add(new MissionResult2(1274582l)); +// mr2List.add(new MissionResult2(1274538l)); +// +// service.updateCommonMissionStateDesc(1, mr2List); +// service.updateCommonMissionStateDesc(1, null); +// service.updateCommonMissionStateDesc(4, null); +// service.updateCommonMissionStateDesc(6, null); +// service.updateLoopMissionStateDesc(mr2List); + +// service.getMissionForAgentStart(66l); +// service.updateLoopMissionInfo(Common.getLoopMissionInfoMap(), null); +// service.updateLoopMissionRoundInfo(Common.getLoopMissionRoundInfoList(), null); +// service.checkAndInitMissionCycle(Common.getLoopMissionInfoMap().get(1187604l)); + + } + + public LoopMissionRoundInfo getRoundInfoFromCommonListByID(Long missionId,Long curMissionId){ + List roundList = Common.getLoopMissionRoundInfoList().get(missionId); + if(roundList!= null && roundList.size()>0){ + for (Iterator roundIte = roundList.iterator(); roundIte.hasNext();) { + LoopMissionRoundInfo roundInfo = (LoopMissionRoundInfo) roundIte.next(); + if(curMissionId!=null && curMissionId.longValue() == roundInfo.getCurMissionId().longValue()){ + return roundInfo; + } + } + } + return null; + } + + public LoopMissionRoundInfo getRoundInfoFromCommonList(MissionResult2 mr){ + List roundList = Common.getLoopMissionRoundInfoList().get(mr.getMissionId()); + if(roundList!= null && roundList.size()>0){ + +// pl("mr.getMissionId()"+mr.getMissionId()); +// pl("mr.getMissionType()"+mr.getMissionType()); +// pl("mr.getLoopFlag()"+mr.getLoopFlag()); +// pl("mr.getResult()"+mr.getResult()); +// pl("mr.getUuid()"+mr.getUuid()); +// pl("mr.getStartTime()"+mr.getStartTime()); +// pl("mr.getEndTime()"+mr.getEndTime()); +// pl("mr.getDescription()"+mr.getDescription()); +// pl("mr.getFileInfo()"+mr.getFileInfo()); +// pl("mr.getText()"+mr.getText()); + if(mr.getResult()!= null &&(mr.getResult()==6 || mr.getResult()==7)){ + return roundList.get(roundList.size()-1); + }else{ + for (Iterator roundIte = roundList.iterator(); roundIte.hasNext();) { + LoopMissionRoundInfo roundInfo = (LoopMissionRoundInfo) roundIte.next(); + if(mr.getStartTime()!=null && roundInfo.getStartTime().getTime()<= mr.getStartTime() && roundInfo.getEndTime().getTime()> mr.getStartTime()){ + return roundInfo; + } + } + } + } + return null; + } +public LoopMissionRoundInfo getRoundInfoFromDBByID(Long curMissionId){ + + LoopMissionRoundInfo roundInfo = null; + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select lmst.cur_mission_id, "); + searchSQL.append("lmst.mission_id, "); + searchSQL.append("lmst.mission_state, "); + searchSQL.append("to_char(lmst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(lmst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time "); + searchSQL.append("from loopmission_state_table lmst "); + searchSQL.append("where 1=1 "); + searchSQL.append("and lmst.cur_mission_id = ? "); + ArrayList fields = new ArrayList(); + fields.add("cur_mission_id"); + fields.add("mission_id"); + fields.add("mission_state"); + fields.add("start_time"); + fields.add("end_time"); + String [] params = new String[]{curMissionId.longValue()+""}; + + ArrayList> rs = null; + try { + rs = dao.dbSelect(searchSQL.toString(), fields, params); + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + roundInfo = new LoopMissionRoundInfo(); + roundInfo.setMissionId(StringUtils.isEmpty(maps.get("mission_id"))?null:Long.parseLong(maps.get("mission_id"))); + roundInfo.setCurMissionId(StringUtils.isEmpty(maps.get("cur_mission_id"))?null:Long.parseLong(maps.get("cur_mission_id"))); + roundInfo.setMissionState(StringUtils.isEmpty(maps.get("mission_state"))?null:Long.parseLong(maps.get("mission_state"))); + roundInfo.setStartTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time"))); + roundInfo.setEndTime(StringUtils.isEmpty(maps.get("end_time"))?null:format.parse(maps.get("end_time"))); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return roundInfo; + } + public LoopMissionRoundInfo getRoundInfoFromDB(MissionResult2 mr){ + + LoopMissionRoundInfo roundInfo = null; + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select lmst.cur_mission_id, "); + searchSQL.append("lmst.mission_id, "); + searchSQL.append("lmst.mission_state, "); + searchSQL.append("to_char(lmst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(lmst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time "); + searchSQL.append("from loopmission_state_table lmst "); + searchSQL.append("where lmst.mission_id=? "); + searchSQL.append("and lmst.start_time<=to_date(?,'"+Constants.DB_DATE_FORMAT+"') "); +// searchSQL.append("and lmst.end_time>=to_date(?,'"+Constants.DB_DATE_FORMAT+"') "); + /** + * 去掉sql语句中的等号的原因: + * NC根据整个任务的开始和结束时间执行时,有可能会比DC多一个周期(最后一个周期不满设定的间隔时间),因为DC将最后一个不满间隔的周期抛弃了,而NC却执行了, + * 所以此时如果NC的最后一个周期结果被收集过来,首先判断缓存中是否有此轮次(NC.startTime>=轮次.startTime && NC.startTime<轮次.endTime), + * 结果没有,就会通过此sql从数据库中查找,但是此时的 NC.startTime和轮次.endTime比较时就加了等号,且比较的是日期格式的(只比到了秒)(如果比较的是long格式,NC.startTime肯定会大于最后一个周期的endTime,可能不会出现等于的情况), + * 所以NC最后一个周期的结果就入到了上一个周期里 + */ + searchSQL.append("and lmst.end_time>to_date(?,'"+Constants.DB_DATE_FORMAT+"') "); + ArrayList fields = new ArrayList(); + fields.add("cur_mission_id"); + fields.add("mission_id"); + fields.add("mission_state"); + fields.add("start_time"); + fields.add("end_time"); +// pl(mr.getMissionId()); +// pl(mr.getUuid()); +// pl(mr.getResult()); +// pl(mr.getLoopFlag()); +// pl(mr.getStartTime()); +// pl(mr.getEndTime()); +// pl(mr.getFileInfo()); +// pl(mr.getDescription()); + try { + logger.info("getRoundInfoFromDB方法:mr.getStartTime()="+mr.getStartTime()); + String startTime = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT).format(mr.getStartTime()); + String [] params = new String[]{mr.getMissionId()+"",startTime,startTime}; + + ArrayList> rs = null; + rs = dao.dbSelect(searchSQL.toString(), fields, params); + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + roundInfo = new LoopMissionRoundInfo(); + roundInfo.setMissionId(StringUtils.isEmpty(maps.get("mission_id"))?null:Long.parseLong(maps.get("mission_id"))); + roundInfo.setCurMissionId(StringUtils.isEmpty(maps.get("cur_mission_id"))?null:Long.parseLong(maps.get("cur_mission_id"))); + roundInfo.setMissionState(StringUtils.isEmpty(maps.get("mission_state"))?null:Long.parseLong(maps.get("mission_state"))); + roundInfo.setStartTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time"))); + roundInfo.setEndTime(StringUtils.isEmpty(maps.get("end_time"))?null:format.parse(maps.get("end_time"))); + } + } + } catch (Exception e) { + logger.error("Get the result corresponding to wheel information abnormal task type from DB:"+mr==null?"":mr.getMissionType(),e); + }finally{ + dao.clearConn(); + } + return roundInfo; + } + /** + * 根据MissionID 更新指定的 周期任务信息 + * @time Feb 10, 2012-2:23:35 PM + * @param missionInfoMap + * @param missionId + * @return + */ + public Map updateLoopMissionInfo(Map missionInfoMap,Long missionId) { + //-- 查询可执行的任务 + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mst.mission_id,"); + searchSQL.append("mst.mission_type,"); + searchSQL.append("mst.mission_state,"); + searchSQL.append("mst.system_id,"); + searchSQL.append("mst.group_id,"); + searchSQL.append("mpt4.node_groups_id,"); + searchSQL.append("mpt4.node_ips_id,");//2015-11-23 hyx 增加任务的节点信息(由于之前只查询了节点组的信息,所以在结果入库的时候,以缓存中的周期任务信息的节点组和节点作为参数调用initLoopMissionResults2方法,导致将节点组内所有的节点都进行了初始化(如果新建任务的时候只选择了某些节点,此处就出现问题了),而其他地方(如任务新建的时候)调用initLoopMissionResults2的时候,是直接已参数表的节点和节点为参数,所以不会出现问题) + searchSQL.append("to_char(mst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(mst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time,"); + searchSQL.append("mst.is_loop,"); + searchSQL.append("mst.loop_delay "); + searchSQL.append("from mission_state_table mst "); + searchSQL.append("left join mission_parameter_table4 mpt4 on mst.mission_id = mpt4.mission_id "); + searchSQL.append("where mst.end_time>sysdate "); + searchSQL.append("and mst.mission_state in (1,2,5,6) "); + searchSQL.append("and mst.is_loop = 1 "); + if(missionId!= null && missionId.longValue()!=0){ + searchSQL.append("and mst.mission_id = '"+missionId.longValue()+"' "); + } + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("mission_type"); + fields.add("mission_state"); + fields.add("system_id"); + fields.add("group_id"); + fields.add("node_groups_id"); + fields.add("node_ips_id"); + fields.add("start_time"); + fields.add("end_time"); + fields.add("is_loop"); + fields.add("loop_delay"); + ArrayList> rs = null; + ArrayList mstList = new ArrayList(); + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + MissionStateTable mission = new MissionStateTable(); + mission.setMissionId(StringUtils.isEmpty(maps.get("mission_id"))?null:Long.parseLong(maps.get("mission_id"))); + mission.setMissionType(StringUtils.isEmpty(maps.get("mission_type"))?null:Long.parseLong(maps.get("mission_type")) ); + mission.setMissionState(StringUtils.isEmpty(maps.get("mission_state"))?null:Long.parseLong(maps.get("mission_state"))); + mission.setSystemId(StringUtils.isEmpty(maps.get("system_id"))?null:Long.parseLong(maps.get("system_id"))); + mission.setGroupId(StringUtils.isEmpty(maps.get("group_id"))?null:Long.parseLong(maps.get("group_id"))); + mission.setNodeGroupsId(StringUtils.isEmpty(maps.get("node_groups_id"))?null:maps.get("node_groups_id")); + mission.setNodeIpsId(StringUtils.isEmpty(maps.get("node_ips_id"))?null:maps.get("node_ips_id")); + mission.setStartTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time"))); + mission.setEndTime(StringUtils.isEmpty(maps.get("end_time"))?null:format.parse(maps.get("end_time"))); + mission.setLoopFlag(StringUtils.isEmpty(maps.get("is_loop"))?null:Long.parseLong(maps.get("is_loop"))); + mission.setLoopDelay(StringUtils.isEmpty(maps.get("loop_delay"))?null:Long.parseLong(maps.get("loop_delay"))); + missionInfoMap.put(mission.getMissionId(), mission); + } + } + return missionInfoMap; + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return missionInfoMap; + } + + //根据curMissionId查找主任务信息 + public MissionStateTable getMissionInfoByCurMissionId(Long curMissionId) { + MissionStateTable mission = null; + if(curMissionId!=null) { + //-- 查询可执行的任务 + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select mst.mission_id,"); + searchSQL.append("mst.mission_type,"); + searchSQL.append("mst.mission_state,"); + searchSQL.append("mst.system_id,"); + searchSQL.append("mst.group_id,"); + searchSQL.append("mpt4.node_groups_id,"); + searchSQL.append("to_char(mst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(mst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time,"); + searchSQL.append("mst.is_loop,"); + searchSQL.append("mst.loop_delay "); + searchSQL.append("from mission_state_table mst "); + searchSQL.append("left join mission_parameter_table4 mpt4 on mst.mission_id = mpt4.mission_id "); + searchSQL.append("left join loopmission_state_table lst on lst.mission_id = mst.mission_id "); + searchSQL.append("where mst.end_time>sysdate "); + searchSQL.append("and mst.mission_state in (1,2,5,6) "); + searchSQL.append("and mst.is_loop = 1 "); + searchSQL.append("and lst.cur_mission_id = "+curMissionId.longValue()); + + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("mission_type"); + fields.add("mission_state"); + fields.add("system_id"); + fields.add("group_id"); + fields.add("node_groups_id"); + fields.add("start_time"); + fields.add("end_time"); + fields.add("is_loop"); + fields.add("loop_delay"); + ArrayList> rs = null; + try { + rs = dao.dbSelect(searchSQL.toString(), fields); + if(rs!=null && rs.size()>0){ + mission = new MissionStateTable(); + Map maps = rs.get(0); + mission.setMissionId(StringUtils.isEmpty(maps.get("mission_id"))?null:Long.parseLong(maps.get("mission_id"))); + mission.setMissionType(StringUtils.isEmpty(maps.get("mission_type"))?null:Long.parseLong(maps.get("mission_type")) ); + mission.setMissionState(StringUtils.isEmpty(maps.get("mission_state"))?null:Long.parseLong(maps.get("mission_state"))); + mission.setSystemId(StringUtils.isEmpty(maps.get("system_id"))?null:Long.parseLong(maps.get("system_id"))); + mission.setGroupId(StringUtils.isEmpty(maps.get("group_id"))?null:Long.parseLong(maps.get("group_id"))); + mission.setNodeGroupsId(StringUtils.isEmpty(maps.get("node_groups_id"))?null:maps.get("node_groups_id")); + mission.setStartTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time"))); + mission.setEndTime(StringUtils.isEmpty(maps.get("end_time"))?null:format.parse(maps.get("end_time"))); + mission.setLoopFlag(StringUtils.isEmpty(maps.get("is_loop"))?null:Long.parseLong(maps.get("is_loop"))); + mission.setLoopDelay(StringUtils.isEmpty(maps.get("loop_delay"))?null:Long.parseLong(maps.get("loop_delay"))); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + return mission; + } + /** + * 根据MissionID 更新指定的周期任务的轮次List 到 集合Map> loopMissionRoundMap 中 + * @time Feb 10, 2012-2:23:58 PM + * @param missionInfoMap + * @param missionId + * @return + */ + public void updateLoopMissionRoundInfo(Map> loopMissionRoundMap,Long missionId) { + + List mIdsList = new LinkedList(); + + + if(missionId != null ){ + //将missionId 加入周期任务队列已查询 周期数据 + mIdsList.add(missionId); + + }else{ + + //missionId为空时 进行非指定查询 + String midsSql = "select mst.mission_id from mission_state_table mst where mst.is_loop = 1 and mst.mission_state in(1,2,5,6) and mst.end_time > to_date('"+format.format(new Date())+"','yyyy-MM-dd HH24:mi:ss') "; + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + try { + + //将missionId们存入周期任务队列 + ArrayList> rs = dao.dbSelect(midsSql, fields); + if(rs != null){ + for (Iterator midsIte = rs.iterator(); midsIte.hasNext();) { + Map midMap = (Map) midsIte.next(); + mIdsList.add(StringUtils.isEmpty(midMap.get("mission_id"))?0:Long.parseLong(midMap.get("mission_id"))); + } + } + + } catch (Exception e) { + logger.error("",e); + } + } + + //终止无效操作 + if(mIdsList.size()== 0){ + return; + } + + //循环查询周期数据 + for (Iterator midsIte = mIdsList.iterator(); midsIte.hasNext();) { + + Long mid = (Long) midsIte.next(); + + //-- 跳过无效操作 + if(mid.longValue() == 0){ + continue; + } + //-- 查询可执行的任务 将数据存入缓存集合 + StringBuffer searchSQL = new StringBuffer(); + if(Constants.IS_MYSQL){ + searchSQL.append("select * from( "); + searchSQL.append("select lmst.cur_mission_id, "); + searchSQL.append("lmst.mission_id, "); + searchSQL.append("lmst.mission_state, "); + searchSQL.append("to_char(lmst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(lmst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time "); + searchSQL.append("from loopmission_state_table lmst "); + searchSQL.append("left join mission_state_table mst on mst.mission_id = lmst.mission_id "); + searchSQL.append("where 1=1 "); +// searchSQL.append("and rownum <=20 "); + searchSQL.append("and mst.mission_id = ?"); + searchSQL.append("order by lmst.cur_mission_id desc"); + searchSQL.append(" limit 20"); + searchSQL.append(") ttt order by cur_mission_id asc"); + }else{ + searchSQL.append("select * from( "); + searchSQL.append("select lmst.cur_mission_id, "); + searchSQL.append("lmst.mission_id, "); + searchSQL.append("lmst.mission_state, "); + searchSQL.append("to_char(lmst.start_time,'"+Constants.DB_DATE_FORMAT+"') start_time,"); + searchSQL.append("to_char(lmst.end_time,'"+Constants.DB_DATE_FORMAT+"') end_time "); + searchSQL.append("from loopmission_state_table lmst "); + searchSQL.append("left join mission_state_table mst on mst.mission_id = lmst.mission_id "); + searchSQL.append("where 1=1 "); + searchSQL.append("and rownum <=20 "); + searchSQL.append("and mst.mission_id = ?"); + searchSQL.append("order by lmst.cur_mission_id desc"); + searchSQL.append(") ttt order by cur_mission_id asc"); + } + + ArrayList fields = new ArrayList(); + fields.add("cur_mission_id"); + fields.add("mission_id"); + fields.add("mission_state"); + fields.add("start_time"); + fields.add("end_time"); + ArrayList> rs = null; + try { + rs = dao.dbSelect(searchSQL.toString(), fields,new String[]{mid.longValue()+""}); + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + LoopMissionRoundInfo roundInfo = new LoopMissionRoundInfo(); + roundInfo.setMissionId(StringUtils.isEmpty(maps.get("mission_id"))?null:Long.parseLong(maps.get("mission_id"))); + roundInfo.setCurMissionId(StringUtils.isEmpty(maps.get("cur_mission_id"))?null:Long.parseLong(maps.get("cur_mission_id"))); + roundInfo.setMissionState(StringUtils.isEmpty(maps.get("mission_state"))?null:Long.parseLong(maps.get("mission_state"))); + roundInfo.setStartTime(StringUtils.isEmpty(maps.get("start_time"))?null:format.parse(maps.get("start_time"))); + roundInfo.setEndTime(StringUtils.isEmpty(maps.get("end_time"))?null:format.parse(maps.get("end_time"))); + + if(loopMissionRoundMap.get(roundInfo.getMissionId()) == null){ + List list = new LinkedList(); + loopMissionRoundMap.put(roundInfo.getMissionId(), list); + } + loopMissionRoundMap.get(roundInfo.getMissionId()).add(roundInfo); + if(loopMissionRoundMap.get(roundInfo.getMissionId()).size() > 20){ + loopMissionRoundMap.get(roundInfo.getMissionId()).remove(0); + } + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + } + public String createEmailContextForMission(Long missionId) { + StringBuffer context = new StringBuffer(); + //通过missionID 获取任务信息 名称 创建日期等 + String selectSql = "select mst.mission_name,to_char(mst.create_time,'"+Constants.DB_DATE_FORMAT+"') create_time,mst.mission_state from mission_state_table mst where mst.mission_id = '"+missionId+"' "; + ArrayList fields = new ArrayList(); + fields.add("mission_name"); + fields.add("create_time"); + fields.add("mission_state"); + try { + ArrayList> mapsList = this.dao.dbSelect(selectSql, fields); + if(mapsList !=null && mapsList.size()>0){ + Map maps = mapsList.get(0); +// String state = "已创建"; +// String state = "Created"; + String state = "i18n_server.UpgradeService.created_n81i"; + if("2".equals(maps.get("mission_state"))){ +// state = "正在执行"; +// state = "Executing"; + state = "i18n_server.UpgradeService.exec_n81i"; + }else if("3".equals(maps.get("mission_state"))){ +// state = "已完成"; +// state = "Completed"; + state = "i18n_server.UpgradeService.finish_n81i"; + } +// context.append("任务名称:"+maps.get("mission_name")+"\n"); +// context.append("Task Name:"+maps.get("mission_name")+"\n"); + context.append("i18n_server.UpgradeService.missionName_n81i:"+maps.get("mission_name")+"\n"); +// context.append("创建时间:"+maps.get("create_time")+"\n"); +// context.append("Created Time:"+maps.get("create_time")+"\n"); + context.append("i18n_server.UpgradeService.createTime_n81i:"+maps.get("create_time")+"\n"); +// context.append("任务状态:"+state); +// context.append("Task Status:"+state); + context.append("i18n_server.UpgradeService.missionState_n81i:"+state); + } + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + dao.clearConn(); + } + return context.toString(); + } + + private EmailInfo createEmailInfoForFailResult(MissionResult2 mr){ + StringBuffer context = new StringBuffer(); + String ip = Common.getNodeIpByUUID(mr.getUuid()); + EmailInfo emailInfo = new EmailInfo(); + emailInfo.setActionIp(ip); + emailInfo.setActionType(EmailTypeConstants.TYPE_TASK_NODE_RESULT_ERROR); + emailInfo.setSendFlag(EmailTypeConstants.FLAG_SEND_ALLREADY); + emailInfo.setContent(mr.getDescription()); + emailInfo.setActionDate(format.format(new Date(mr.getStartTime()==null?System.currentTimeMillis():mr.getStartTime()))); + emailInfo.setSendLevel(EmailTypeConstants.URGENT_LATER); + //通过missionID 获取任务信息 名称 创建日期等 + String selectSql = "select mst.mission_name,to_char(mst.create_time,'"+Constants.DB_DATE_FORMAT+"') create_time from mission_state_table mst where mst.mission_id = '"+mr.getMissionId()+"' "; + ArrayList fields = new ArrayList(); + fields.add("mission_name"); +// fields.add("create_time"); + try { + ArrayList> mapsList = this.dao.dbSelect(selectSql, fields); + if(mapsList !=null && mapsList.size()>0){ + Map maps = mapsList.get(0); + emailInfo.setActionDesc(maps.get("mission_name")); + } + + } catch (Exception e) { + logger.error("", e); + }finally{ + dao.clearConn(); + } + return emailInfo; + } + /*private String createEmailContextForFailResult(MissionResult2 mr){ + StringBuffer context = new StringBuffer(); + String ip = Common.getNodeIpByUUID(mr.getUuid()); + //通过missionID 获取任务信息 名称 创建日期等 + String selectSql = "select mst.mission_name,to_char(mst.create_time,'"+Constants.DB_DATE_FORMAT+"') create_time from mission_state_table mst where mst.mission_id = '"+mr.getMissionId()+"' "; + ArrayList fields = new ArrayList(); + fields.add("mission_name"); + fields.add("create_time"); + try { + ArrayList> mapsList = this.dao.dbSelect(selectSql, fields); + if(mapsList !=null && mapsList.size()>0){ + Map maps = mapsList.get(0); + maps.get(""); + context.append("任务名称:"+maps.get("mission_name")+"\n"); + context.append("创建时间:"+maps.get("create_time")+"\n"); + context.append("结果描述:"+ip+" "+mr.getDescription()); + } + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + dao.clearConn(); + } + return context.toString(); + }*/ + /** + * 变更任务状态 + * @time Jan 11, 2012-1:39:27 PM + * @param missionId + * @param missionState + */ + public void updateMissionState(Long missionId,int missionState,String autoDesc) throws Exception{ + if(missionId==null || missionId.longValue()==0){ + return ; + } + String [] params = null; + StringBuffer sql = new StringBuffer("update mission_state_table mst set "); + sql.append(" mst.mission_state = ? "); + pl(autoDesc); + if(StringUtils.isEmpty(autoDesc)){ + params =new String[]{missionState+"",missionId.longValue()+""}; + }else{ + sql.append(" , mst.AUTO_DESC = ? "); + params =new String[]{missionState+"",autoDesc,missionId.longValue()+""}; + } + sql.append(" where mst.mission_id =? "); + try { + dao.dbUpdate(sql.toString(), params); + } catch (Exception e) { + throw e; + }finally{ + dao.clearConn(); + } + } + /** + * 删除预置周期信息 + * @time Jan 11, 2012-1:39:27 PM + * @param missionId + * @param missionState + */ + public void deleteLoopMissionPreset(Long missionId,Long curMissionId) { + if(missionId==null || missionId.longValue()==0){ + return ; + } + + String sql = "delete from loopmission_state_table where mission_state=0 and mission_id ="+missionId.longValue()+" and start_time>=sysdate"; + if(curMissionId!=null){ + sql = "delete from loopmission_state_table where mission_id ="+missionId.longValue()+" and cur_mission_id>"+curMissionId.longValue(); + } + + try { + dao.dbUpdate(sql); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + /** + * 变更周期任务结果的状态 + * @time Jan 11, 2012-1:39:27 PM + * @param missionId + * @param missionState + */ + public void updateMissionResultsResult(long roundMissionId,int missionType,int result) { + if(roundMissionId==0 || missionType==0){ + return ; + } + String sql = "update mission_result_table"+missionType+" mst set mst.result="+result+" where mst.mission_id ="+roundMissionId; + try { + dao.dbUpdate(sql); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + /** + * 变更任务状态 + * @time Jan 11, 2012-1:39:27 PM + * @param missionId + * @param missionState + */ + public void updateLoopMissionState(Long missionId,int missionState) { + if(missionId==null || missionId.longValue()==0){ + return ; + } + String sql = "update loopmission_state_table mst set mst.mission_state ='"+missionState+"' where mst.cur_mission_id ="+missionId.longValue(); + try { + dao.dbUpdate(sql); + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + public String getUpdateSQL(int missionType) { +// "+(mr2.getResult()==null?1:mr2.getResult())+" +// String desc = StringUtils.isEmpty(mr2.getDescription())?"":mr2.getDescription(); +// "+mr2.getMissionId().longValue()+" +// "+mr2.getUuid().longValue()+" +// StringBuffer sql = new StringBuffer(" mrt set mrt.result =? ,mrt.result_desc = substr(mrt.result_desc||?,decode(sign(vsize(mrt.result_desc||?)-1024),-1,0,0,0,1,vsize(mrt.result_desc||?)+1-1024),1024) "); + StringBuffer sql = new StringBuffer("update mission_result_table"+missionType+" mrt set mrt.result =? ,mrt.result_desc = ?"); + switch (missionType) { + case 1: +// sql.insert(0, "update mission_result_table1"); + break; + case 2: +// sql.insert(0, "update mission_result_table2"); + break; + case 3: +// sql.insert(0, "update mission_result_table3"); + break; + case 4: + sql.append(",mrt.file_info = ? ,mrt.result_detail=?"); + break; + case 5: +// sql.insert(0, "update mission_result_table5"); + break; + case 6: +// sql.insert(0, "update mission_result_table6"); + break; + default: + break; + } + sql.append(" where mrt.mission_id =? and mrt.seq_id =? "); + return sql.toString(); + } +// public void updateMissionResult2ByBatch(List mr2List) { +// List updateSqlList = new ArrayList(); +// try { +// if(mr2List!= null && mr2List.size()>0){ +// Iterator mrIte = mr2List.iterator(); +// int count = 0; +// while (mrIte.hasNext()) { +// MissionResult2 mr = (MissionResult2) mrIte.next(); +// String sql = getUpdateSql(mr); +// System.out.println("sql> "+sql); +// updateSqlList.add(sql); +// count++; +// if(count == Constants.DB_EXECUTE_BATCH){ +// dao.dbUpdateByBatch(updateSqlList); +// count = 0; +// updateSqlList.clear(); +// } +// } +// if (count > 0) { +// dao.dbUpdateByBatch(updateSqlList); +// count = 0; +// updateSqlList.clear(); +// } +// } +// } catch (Exception e) { +// logger.error("批量更新异常 \n"+"",e); +// }finally{ +// dao.clearConn(); +// } +// } + + /** + * 非定时任务 检查任务完成状态操作方法 + * @time Nov 17, 2011-4:47:55 PM + */ + /*public void checkAndUpdateMissionComplete() {//此处total是共执行的,还是所有应执行的节点数,再加个字段?描述是在DC端就入库?还是web端再统计? + StringBuffer selectSql = new StringBuffer(); + selectSql.append("select t.mission_id from ("); + selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table1 mrt group by mrt.mission_id) union all "); +// selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table2 mrt group by mrt.mission_id) union all "); +// selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table3 mrt group by mrt.mission_id) union all "); + selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table4 mrt group by mrt.mission_id) union all "); +// selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table5 mrt group by mrt.mission_id) union all "); + selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table6 mrt group by mrt.mission_id)"); + selectSql.append(") t "); + selectSql.append("left join mission_state_table mst on mst.mission_id = t.mission_id "); + selectSql.append("where t.ok + t.fail = t.total "); + selectSql.append("and nvl(t.mission_id,-1) <> -1 "); + selectSql.append("and mst.is_loop = 0 "); + selectSql.append("and mst.mission_state <3 "); + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + try { +// StringBuffer missionIds = new StringBuffer(); + List> mapList = dao.dbSelect(selectSql.toString(), fields); + if(mapList != null && mapList.size()>0){// + for(Map map : mapList){ +// missionIds.append(","+map.get("mission_id")); + if(StringUtils.isNotEmpty(map.get("mission_id"))){ +// createEmailContextForMission(Long.parseLong(map.get("mission_id"))); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 3 where mst.mission_id = ?",map.get("mission_id")); + } + } +// missionIds = missionIds.deleteCharAt(0); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + }*/ + + /** + * 非周期任务:录入任务结果后,若任务已完成,则更新任务状态为成功、部分成功、全部失败中的一个(原来只有一个完成状态,做此修改便于更清晰的描述任务完成的状态) + * 完成(3):t.ok + t.fail = t.total + * 成功(31):t.ok + t.fail = t.total and t.ok = t.total + * 部分成功(32):t.ok + t.fail = t.total and t.ok>0 and t.fail>0 + * 全部失败(30):t.ok + t.fail = t.total and t.fail = t.total + * @time 2013-1-28 9:30:55 AM + * @author hyx + */ + public void updateCommonMissionComplete() {//此处total是共执行的,还是所有应执行的节点数,再加个字段?描述是在DC端就入库?还是web端再统计? + StringBuffer selectSql = new StringBuffer(); + selectSql.append("select t.mission_id,t.ok,t.fail,t.total from ("); + selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table1 mrt group by mrt.mission_id) union all "); + selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table4 mrt group by mrt.mission_id) union all "); + selectSql.append("(select mrt.mission_id,sum(decode(mrt.result,0,1,0) ) ok,sum(decode(mrt.result,1,1,-1,1,0) ) fail,count(mrt.seq_id) total from mission_result_table6 mrt group by mrt.mission_id)"); + selectSql.append(") t "); + selectSql.append("left join mission_state_table mst on mst.mission_id = t.mission_id "); + selectSql.append("where t.ok + t.fail = t.total "); + selectSql.append("and nvl(t.mission_id,-1) <> -1 ");//不取任务id为null的? + selectSql.append("and mst.is_loop = 0 ");//非周期任务 + selectSql.append("and mst.mission_state in(1,2) ");//如果之前已经更新为完成(=3)的任务,不再更新,只更新一次(非周期任务的状态:1创建,2执行中,4未能执行(未能执行的不能更新为完成)) + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("ok"); + fields.add("fail"); + fields.add("total"); + try { + List> mapList = dao.dbSelect(selectSql.toString(), fields); + + if(mapList != null && mapList.size()>0){// + EmailService emailService = new EmailService(dao); + for(Map map : mapList){ + if(StringUtils.isNotEmpty(map.get("mission_id"))){ + Long ok = StringUtils.isEmpty(map.get("ok"))?null:Long.parseLong(map.get("ok")); + Long fail = StringUtils.isEmpty(map.get("fail"))?null:Long.parseLong(map.get("fail")); + Long total = StringUtils.isEmpty(map.get("total"))?null:Long.parseLong(map.get("total")); + //完成的任务才需要更新状态,但是对于新录入的任务,我会更改其mission_state_table里的mission_state_desc + if(ok!=null && ok.equals(total) ) {//成功 +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 30 ,AUTO_DESC=? where mst.mission_id = ?",format.format(System.currentTimeMillis())+" 任务执行完成:共"+total+"个执行节点,成功"+ok+"个,失败"+fail+"个",map.get("mission_id")); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 30 ,AUTO_DESC=? where mst.mission_id = ?",format.format(System.currentTimeMillis())+" i18n_sserver.UpgradeService.sql.complate_n81i "+total+" i18n_sserver.UpgradeService.sql.executeNode_n81i "+ok+" i18n_sserver.UpgradeService.sql.failed_n81i "+fail,map.get("mission_id")); + + }else if(fail!=null && fail.equals(total)) {//全部失败 +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 31 ,AUTO_DESC=? where mst.mission_id = ?",format.format(System.currentTimeMillis())+" 任务执行完成:共"+total+"个执行节点,成功"+ok+"个,失败"+fail+"个",map.get("mission_id")); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 31 ,AUTO_DESC=? where mst.mission_id = ?",format.format(System.currentTimeMillis())+" i18n_sserver.UpgradeService.sql.complate_n81i "+total+" i18n_sserver.UpgradeService.sql.executeNode_n81i "+ok+" i18n_sserver.UpgradeService.sql.failed_n81i "+fail,map.get("mission_id")); + + }else if(fail!=null && fail.longValue()>0 && ok!=null && ok.longValue()>0) {//部分成功 +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 32 ,AUTO_DESC=? where mst.mission_id = ?",format.format(System.currentTimeMillis())+" 任务执行完成:共"+total+"个执行节点,成功"+ok+"个,失败"+fail+"个",map.get("mission_id")); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 32 ,AUTO_DESC=? where mst.mission_id = ?",format.format(System.currentTimeMillis())+" i18n_sserver.UpgradeService.sql.complate_n81i "+total+" i18n_sserver.UpgradeService.sql.executeNode_n81i "+ok+" i18n_sserver.UpgradeService.sql.failed_n81i "+fail,map.get("mission_id")); + + } + // 发送邮件 +// emailService.sendEmailForMission(Long.parseLong(map.get("mission_id")), "任务执行通知", "任务执行结束:共"+total+"个执行节点,成功"+ok+"个,失败"+fail+"个"); +// emailService.sendEmailForMission(Long.parseLong(map.get("mission_id")), "Task execution notification", "End of task execution:A total of"+total+"execution nodes,successful"+ok+"failed"+fail); + emailService.sendEmailForMission(Long.parseLong(map.get("mission_id")), "i18n_server.UpgradeService.missionExec_n81i", "i18n_server.UpgradeService.missionEnd_n81i:i18n_server.UpgradeService.sql.total_n81i "+total+" i18n_server.UpgradeService.sql.executeNode2_n81i,i18n_server.UpgradeService.sql.success_n81i "+ok+" i18n_sserver.UpgradeService.sql.failed_n81i "+fail); + } + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + //更新mission_state_table表里的mission_state_desc字段(只对新录入结果的任务更新,没录入任务结果的任务不需要更新) + public void updateCommonMissionStateDesc(int missiontype,List mr2List) { + if(mr2List!=null && mr2List.size()>0) { + //获得更新结果的任务id的字符串 + String missionIds = getMissionIdLinkStr(mr2List); + + if(!"".equals(missionIds)) { + updateCommonMissionStateDesc(missiontype,missionIds); + + } + } + } + + /** + * 更新mission_state_table表里的mission_state_desc字段(只对新录入结果的任务更新,没录入任务结果的任务不需要更新) + * 根据任务id更新任务的状态-非周期任务 + * + */ + public void updateCommonMissionStateDesc(int missiontype,String missionIds) { + if(StringUtils.isNotBlank(missionIds)) { + StringBuffer selectSql = new StringBuffer(); + selectSql.append(" select t.mission_id,t.ok,t.fail,t.total "); + selectSql.append(" from ("); + selectSql.append(" select mrt.mission_id,"); + selectSql.append(" sum(decode(mrt.result,0,1,0) ) ok, "); + selectSql.append(" sum(decode(mrt.result,1,1,-1,1,0) ) fail, "); + selectSql.append(" count(mrt.seq_id) total "); + selectSql.append(" from mission_result_table"+missiontype+" mrt "); + selectSql.append(" group by mrt.mission_id "); + selectSql.append(" ) t "); + selectSql.append(" left join mission_state_table mst on mst.mission_id = t.mission_id "); + selectSql.append(" where mst.is_loop = 0");//非周期任务:0,周期任务:1 + selectSql.append(" and t.mission_id is not null ");//不取任务id为null的?其实不需要,因为非周期任务的任务id理论上不会为null? + selectSql.append(" and mst.mission_id in("+missionIds+") ");//只更新 新录入了结果的任务的任务状态描述 + + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("ok"); + fields.add("fail"); + fields.add("total"); + try { + List> mapList = dao.dbSelect(selectSql.toString(), fields); + if(mapList != null && mapList.size()>0){// + List sqls = new ArrayList(); + for(Map map : mapList){ + if(StringUtils.isNotEmpty(map.get("mission_id"))){ + Long ok = StringUtils.isEmpty(map.get("ok"))?null:Long.parseLong(map.get("ok")); + Long fail = StringUtils.isEmpty(map.get("fail"))?null:Long.parseLong(map.get("fail")); + Long total = StringUtils.isEmpty(map.get("total"))?null:Long.parseLong(map.get("total")); + //完成的任务才需要更新状态,但是对于新录入的任务,我会更改其mission_state_table里的mission_state_desc +// sqls.add("update mission_state_table mst set mst.mission_state_desc = '共"+total+"个执行节点,
"+(total-ok-fail)+"个未执行,
"+(ok+fail)+"个已执行【成功"+ok+"个,失败"+fail+"个】' where mst.mission_id = "+map.get("mission_id")); + sqls.add("update mission_state_table mst set mst.mission_state_desc = 'i18n_server.UpgradeService.sql.total_n81i "+total+" i18n_server.UpgradeService.sql.executeNode2_n81i,
"+(total-ok-fail)+" i18n_server.UpgradeService.sql.unexecute_n81i,
"+(ok+fail)+" i18n_server.UpgradeService.sql.execute_n81i【i18n_server.UpgradeService.sql.success_n81i "+ok+" i18n_sserver.UpgradeService.sql.failed_n81i "+fail+"】' where mst.mission_id = "+map.get("mission_id")); + } + } + + dao.dbUpdateByBatch(sqls); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + } + + private String getMissionIdLinkStr(List mr2List) { + String missionIds = ""; + for(MissionResult2 missRlt2:mr2List) { + String misId = missRlt2.getMissionId()==null ? "" : missRlt2.getMissionId().toString(); + if(!"".equals(misId)&&!(","+missionIds).contains(","+misId+",")) { + missionIds = missionIds + misId + ","; + } + } + if(missionIds.endsWith(",")) { + missionIds = missionIds.substring(0,missionIds.length()-1); + } + return missionIds; + } + + //更新loopmission_state_table表里的mission_state_desc字段(只对新录入结果的任务更新,没录入任务结果的任务不需要更新) + public void updateLoopMissionStateDesc(List mr2List) { + if(mr2List!=null && mr2List.size()>0) { + //获得更新结果的任务id的字符串 + String missionIds = getMissionIdLinkStr(mr2List); + + if(!"".equals(missionIds)) { + updateLoopMissionStateDesc(missionIds); + } + } + } + + //根据任务id更新任务的状态描述-周期任务 + public void updateLoopMissionStateDesc(String missionIds) { + if(StringUtils.isNotBlank(missionIds)) { +// StringBuffer selectSql = new StringBuffer(); +// selectSql.append(" select sumT.missionId ,sumT.ok, sumT.fail, sumT.total "); +// selectSql.append(" from ("); +// selectSql.append(" select row_number() over(partition by lmst.mission_id order by lmst.cur_mission_id desc) cn, "); +// selectSql.append(" lmst.cur_mission_id ,"); +// selectSql.append(" lmst.mission_id missionId,mst.mission_state, "); +// selectSql.append(" t.ok, t.fail, t.total "); +// selectSql.append(" from ( "); +// selectSql.append(" select mrt.mission_id, "); +// selectSql.append(" sum(decode(mrt.result, 0, 1, 0)) ok, ");//只统计正常执行的周期任务执行情况,对于撤销的不管 +// selectSql.append(" sum(decode(mrt.result, 1, 1, -1, 1, 0)) fail, ");//只统计正常执行的周期任务执行情况,对于撤销的不管 +// selectSql.append(" count(mrt.seq_id) total "); +// selectSql.append(" from mission_result_table4 mrt "); +// selectSql.append(" group by mrt.mission_id "); +// selectSql.append(" ) t "); +// selectSql.append(" left join loopmission_state_table lmst on lmst.cur_mission_id = t.mission_id "); +// selectSql.append(" left join mission_state_table mst on mst.mission_id = lmst.mission_id "); +// selectSql.append(" where lmst.cur_mission_id is not null and mst.is_loop=1 and mst.mission_state =2 ");//mission_state=2表示只修改正常执行的,对于撤销的,不予修改 +// selectSql.append(" and mst.mission_id in("+missionIds+") ");//只修改有结果录入的 +// selectSql.append(" ) sumT ");//只修改有结果录入的 +// selectSql.append(" where sumT.cn=1 ");//只修改有结果录入的 + String selectSql = "select lmst.mission_id missionId, t.ok, t.fail, t.total\n" + + " from (select mrt.mission_id,\n" + + " nvl(sum(decode(mrt.result, 0, 1, 0)), 0) ok,\n" + + " nvl(sum(decode(mrt.result, 1, 1, -1, 1, 0)), 0) fail,\n" + + " count(mrt.seq_id) total\n" + + " from mission_result_table4 mrt\n" + + " group by mrt.mission_id) t\n" + + " left join (select max(lst.cur_mission_id) cur_mission_id, lst.mission_id\n" + + " from mission_result_table4 mrt4\n" + + " left join loopmission_state_table lst on mrt4.mission_id = lst.cur_mission_id\n" + + " group by lst.mission_id) lmst\n" + + " on lmst.cur_mission_id = t.mission_id\n" + + " left join mission_state_table mst\n" + + " on mst.mission_id = lmst.mission_id\n" + + " where lmst.cur_mission_id is not null and mst.is_loop=1 " + + " and mst.mission_state =2 and mst.mission_id in ("+missionIds+")";//mission_state=2表示只修改正常执行的,对于撤销的,不予修改 + + ArrayList fields = new ArrayList(); + fields.add("missionId"); + fields.add("ok"); + fields.add("fail"); + fields.add("total"); + try { + List> mapList = dao.dbSelect(selectSql.toString(), fields); + if(mapList != null && mapList.size()>0){// + List sqls = new ArrayList(); + for(Map map : mapList){ + if(StringUtils.isNotEmpty(map.get("missionId"))){ + Long ok = StringUtils.isEmpty(map.get("ok"))?null:Long.parseLong(map.get("ok")); + Long fail = StringUtils.isEmpty(map.get("fail"))?null:Long.parseLong(map.get("fail")); + Long total = StringUtils.isEmpty(map.get("total"))?null:Long.parseLong(map.get("total")); + //完成的任务才需要更新状态,但是对于新录入的任务,我会更改其mission_state_table里的mission_state_desc +// sqls.add("update mission_state_table mst set mst.mission_state_desc = '最新周期共"+total+"个执行节点,
"+(total-ok-fail)+"个未执行,
"+(ok+fail)+"个已执行【成功"+ok+"个,失败"+fail+"个】' where mst.mission_id = "+map.get("missionId")); + sqls.add("update mission_state_table mst set mst.mission_state_desc = 'i18n_server.UpgradeService.sql.cycle_n81i "+total+" i18n_server.UpgradeService.sql.executeNode2_n81i,
"+(total-ok-fail)+" i18n_server.UpgradeService.sql.unexecute_n81i,
"+(ok+fail)+" i18n_server.UpgradeService.sql.execute_n81i【i18n_server.UpgradeService.sql.success_n81i "+ok+" i18n_sserver.UpgradeService.sql.failed_n81i "+fail+" 】' where mst.mission_id = "+map.get("missionId")); + } + } + + dao.dbUpdateByBatch(sqls); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + } + + /** + * 周期任务: + * 更新loopmission_state_table表: + * 1.查询正常执行的单次周期任务的完成(2->3)+撤销任务的撤销完成(6->7) + * 2.更新表loopmission_state_table中的状态(2->3,6->7) + * 更新mission_state_table表: + * 1.查询正常执行的任务周期全部完成为任务完成(2->3) + * 2.查询撤销任务周的撤销完成为整个任务撤销完成(6->7) + * 任务完成判定标准(20130322):撤销任务为撤销周期完成即整个任务完成 + * 正常周期任务为所有正常均完成则整个任务完成 + * @time Nov 17, 2011-4:47:55 PM + * @author hyx + */ + public void updateLoopMissionComplete() { + //-- 周期任务 的检查SQL + //完成的(正常执行的+撤销的,为了获得完成的任务id,无论是正常执行完成的,还是撤销完成的) + StringBuffer loopSQL = new StringBuffer(); + loopSQL.append(" select lmst.cur_mission_id mission_id ,lmst.mission_state"); + loopSQL.append(" from ("); + loopSQL.append(" select mrt.mission_id,");//0,1,-1和6,7不会同时出现的 + loopSQL.append(" sum(case when mrt.result = 0 then 1 else 0 end) ok,");//正常执行成功的 + loopSQL.append(" sum(case when mrt.result in (1,-1) then 1 else 0 end) fail,");//正常执行失败的 + loopSQL.append(" sum(case when mrt.result = 7 then 1 else 0 end) cxOk,");//撤销成功的 + loopSQL.append(" sum(case when mrt.result = 6 then 1 else 0 end) cxFail,");//撤销失败的 + loopSQL.append(" count(mrt.seq_id) total ");//总数 + loopSQL.append(" from mission_result_table4 mrt "); + loopSQL.append(" group by mrt.mission_id "); + loopSQL.append(" ) t "); + loopSQL.append(" left join loopmission_state_table lmst on lmst.cur_mission_id = t.mission_id "); + loopSQL.append(" where lmst.cur_mission_id is not null "); + loopSQL.append(" and (t.ok + t.fail = t.total or t.cxOk+t.cxFail=t.total )"); + loopSQL.append(" and lmst.mission_state not in (3,7) ");//3:周期任务的单次任务完成,7:周期任务的单次任务完成撤销 + + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("mission_state"); + try { + List> mapList = dao.dbSelect(loopSQL.toString(), fields); + + //- 变更周期任务信息状态 loopmission_state_table + if(mapList != null && mapList.size()>0){ + StringBuffer missionIds = new StringBuffer("0"); //正常周期任务结果入库完成轮次IDS + StringBuffer cancelMissionIds = new StringBuffer("0");//撤销周期任务结果入库完成轮次IDS + for(Map map : mapList){ + if(StringUtils.isNotEmpty(map.get("mission_state")) && map.get("mission_state").equals("6")){ //撤销周期 + cancelMissionIds.append(","+map.get("mission_id")); + }else{ //非撤销周期 + missionIds.append(","+map.get("mission_id")); + } + } + + //存在撤销周期的轮次任务 + if(cancelMissionIds.length()>1){ + //将该周期轮次状态置为撤销完成,将该任务状态置为撤销完成 + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = 7 where mst.mission_state = 6 and mst.cur_mission_id in ("+cancelMissionIds.toString()+")"); +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7 , mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务撤销完成' where mst.mission_state = 6 and mst.mission_id in ( select lmst.mission_id from loopmission_state_table lmst where lmst.cur_mission_id in("+cancelMissionIds.toString()+")) "); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7 , mst.auto_desc='"+format.format(System.currentTimeMillis())+" i18n_server.UpgradeService.sql.revoke_n81i' where mst.mission_state = 6 and mst.mission_id in ( select lmst.mission_id from loopmission_state_table lmst where lmst.cur_mission_id in("+cancelMissionIds.toString()+")) "); + } + + //存在正常周期的轮次任务 + if(missionIds.length()>1){ + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = 3 where mst.mission_state = 2 and mst.cur_mission_id in ("+missionIds.toString()+")"); + } + } + //- 变更周期任务信息状态 mission_state_table + StringBuffer missionSql = new StringBuffer(); + missionSql.append(" select mst.mission_id,mst.mission_state"); + missionSql.append(" from ("); + missionSql.append(" select lmst.mission_id, "); + missionSql.append(" sum(decode(lmst.mission_state, 3, 1, 7, 1, 0)) ok,");//正常执行完成的周期,撤销完成的周期 + missionSql.append(" sum(decode(lmst.mission_state, 4, 1, 0)) fail,");//未能执行的周期 + missionSql.append(" count(lmst.cur_mission_id) total"); + missionSql.append(" from loopmission_state_table lmst"); + missionSql.append(" group by lmst.mission_id"); + missionSql.append(" ) t"); + missionSql.append(" left join mission_state_table mst on mst.mission_id = t.mission_id"); + missionSql.append(" where t.ok + t.fail = t.total"); + missionSql.append(" and nvl(mst.mission_id, -1) <> -1"); + missionSql.append(" and mst.is_loop = 1"); + missionSql.append(" and mst.mission_state not in (3, 7, 4)"); + + StringBuffer missionIds0 = new StringBuffer("0"); + List> mapList0 = dao.dbSelect(missionSql.toString(), fields); + //- 变更周期任务信息状态 mission_state_table + if(mapList0 != null && mapList0.size()>0){ + EmailService emailService = new EmailService(dao); + for(Map map : mapList0){ + missionIds0.append(","+map.get("mission_id")); + if(StringUtils.isNotEmpty(map.get("mission_id"))){ + // 发送邮件 +// emailService.sendEmailForMission(Long.parseLong(map.get("mission_id")), "任务执行通知", "任务执行结束"); + emailService.sendEmailForMission(Long.parseLong(map.get("mission_id")), "i18n_server.UpgradeService.missionExec_n81i", "i18n_server.UpgradeService.missionEnd_n81i"); + } + } + //将撤销任务或撤销超时任务置为撤销完成状态 +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务撤销完成' where mst.mission_state = 6 and (mst.mission_id in ("+missionIds0.toString()+") or mst.end_time <= sysdate)"); +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务撤销完成' where mst.mission_state = 6 and mst.mission_id in ("+missionIds0.toString()+") "); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7,mst.auto_desc='"+format.format(System.currentTimeMillis())+" i18n_server.UpgradeService.sql.revoke_n81i' where mst.mission_state = 6 and mst.mission_id in ("+missionIds0.toString()+") "); + //只针对完成的周期任务,不包括撤销任务(完成的状况可能是:全部完成(mission_state为3的=total),部分未能执行(fail>0)) +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 3,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务执行完成' where mst.mission_state = 2 and (mst.mission_id in ("+missionIds0.toString()+") or mst.end_time <= sysdate)"); +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 3,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务执行完成' where mst.mission_state = 2 and mst.mission_id in ("+missionIds0.toString()+") "); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 3,mst.auto_desc='"+format.format(System.currentTimeMillis())+" i18n_server.UpgradeService.sql.revoke_n81i' where mst.mission_state = 2 and mst.mission_id in ("+missionIds0.toString()+") "); + //--将超时的任务检查,单独提出-checkTimeoutLoopMisssionAndAddResult()方法 + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + + //对于超时周期任务的清理:周期未初始化结果:loop表的state=0 or 1,将loop表的state修改为4未能执行 + public void updateTimeoutLoopMissionState(String timeoutMissIds) { + if(StringUtils.isNotBlank(timeoutMissIds)) { + StringBuffer loopSQL = new StringBuffer(); + loopSQL.append(" select lmst.mission_id,lmst.cur_mission_id , lmst.mission_state"); + loopSQL.append(" from loopmission_state_table lmst "); + loopSQL.append(" where lmst.mission_id in ("+timeoutMissIds+") and lmst.mission_state in (0,1) and lmst.end_time<=to_date('"+format.format(new Date())+"','yyyy-MM-dd HH24:mi:ss') "); + + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("cur_mission_id"); + fields.add("mission_state"); + try { + List> mapList = dao.dbSelect(loopSQL.toString(), fields); + + //- 变更周期任务信息状态 loopmission_state_table + if(mapList != null && mapList.size()>0){ + StringBuffer missionIds = new StringBuffer("0"); + StringBuffer cancelMissionIds = new StringBuffer("0"); + for(Map map : mapList){ + if(StringUtils.isNotEmpty(map.get("mission_state")) && map.get("mission_state").equals("6")){ //撤销周期 + cancelMissionIds.append(","+map.get("cur_mission_id")); + }else{ //非撤销周期 + missionIds.append(","+map.get("cur_mission_id")); + } + } + + //存在撤销周期的轮次任务 + if(cancelMissionIds.length()>1){ + //将该周期轮次状态置为撤销完成,将该任务状态置为撤销完成 + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = 7 where mst.mission_state = 6 and mst.cur_mission_id in ("+cancelMissionIds.toString()+")"); +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7 , mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务撤销完成' where mst.mission_state = 6 and mst.mission_id in ( select lmst.mission_id from loopmission_state_table lmst where lmst.cur_mission_id in("+cancelMissionIds.toString()+")) "); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7 , mst.auto_desc='"+format.format(System.currentTimeMillis())+" i18n_server.UpgradeService.sql.revoke_n81i' where mst.mission_state = 6 and mst.mission_id in ( select lmst.mission_id from loopmission_state_table lmst where lmst.cur_mission_id in("+cancelMissionIds.toString()+")) "); + } + + //存在正常周期的轮次任务 + if(missionIds.length()>1){ + dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = 4 where mst.cur_mission_id in ("+missionIds.toString()+")"); + } + } + + //- 变更周期任务信息状态 mission_state_table + StringBuffer missionSql = new StringBuffer(); + missionSql.append(" select mst.mission_id,mst.mission_state"); + missionSql.append(" from ("); + missionSql.append(" select lmst.mission_id, "); + missionSql.append(" sum(decode(lmst.mission_state, 3, 1, 7, 1, 0)) ok,");//正常执行完成的周期,撤销完成的周期 + missionSql.append(" sum(decode(lmst.mission_state, 4, 1, 0)) fail,");//未能执行的周期 + missionSql.append(" count(lmst.cur_mission_id) total"); + missionSql.append(" from loopmission_state_table lmst"); + missionSql.append(" group by lmst.mission_id"); + missionSql.append(" ) t"); + missionSql.append(" left join mission_state_table mst on mst.mission_id = t.mission_id"); + missionSql.append(" where t.ok + t.fail = t.total"); + missionSql.append(" and nvl(mst.mission_id, -1) <> -1"); + missionSql.append(" and mst.is_loop = 1"); + missionSql.append(" and mst.mission_state not in (3, 7, 4)"); + + ArrayList missFields = new ArrayList(); + missFields.add("mission_id"); + missFields.add("mission_state"); + + StringBuffer missionIds0 = new StringBuffer("0"); + List> mapList0 = dao.dbSelect(missionSql.toString(), missFields); + //- 变更周期任务信息状态 mission_state_table + if(mapList0 != null && mapList0.size()>0){ + EmailService emailService = new EmailService(dao); + for(Map map : mapList0){ + missionIds0.append(","+map.get("mission_id")); + if(StringUtils.isNotEmpty(map.get("mission_id"))){ + // 发送邮件 +// emailService.sendEmailForMission(Long.parseLong(map.get("mission_id")), "任务执行通知", "任务执行结束"); + emailService.sendEmailForMission(Long.parseLong(map.get("mission_id")), "i18n_server.UpgradeService.missionExec_n81i", "i18n_server.UpgradeService.missionExec_n81i"); + } + } + //将撤销任务或撤销超时任务置为撤销完成状态 +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务撤销完成' where mst.mission_state = 6 and mst.mission_id in ("+missionIds0.toString()+") "); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7,mst.auto_desc='"+format.format(System.currentTimeMillis())+" i18n_server.UpgradeService.sql.revoke_n81i' where mst.mission_state = 6 and mst.mission_id in ("+missionIds0.toString()+") "); + //只针对完成的周期任务,不包括撤销任务(完成的状况可能是:全部完成(mission_state为3的=total),部分未能执行(fail>0)) +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 3,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务执行完成' where mst.mission_state = 2 and mst.mission_id in ("+missionIds0.toString()+") "); + dao.dbUpdate("update mission_state_table mst set mst.mission_state = 3,mst.auto_desc='"+format.format(System.currentTimeMillis())+" i18n_server.UpgradeService.sql.executed_n81i' where mst.mission_state = 2 and mst.mission_id in ("+missionIds0.toString()+") "); + //--将超时的任务检查,单独提出-checkTimeoutLoopMisssionAndAddResult()方法 + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + } + } + + /** + * 2013-3-13 hyx + * 检查周期任务中超时的任务,并将结果(state状态修改)存入缓存,等待下一次录入结果线程执行时,更改任务的状态及描述 + * 针对过期未完成的【周期】任务进行清理(非周期任务不需要清理,因为没有结束时间endTime,所以不会超时): + * 1.会不会重复添加过期任务呢?-不会,因为此处将结果加入缓存后,会先执行上面的结果录入,和任务状态的改变,再执行过期任务清理时,之前的过期任务的状态已修改为3或者7,不会被统计 + * 2.清理时,将超时的任务(mission_state_table表中state=2或者6的,且endTime小于当前时间的)的结果存入缓存(Common.addResultToResultList) + * 3.对于过期的周期任务: + * 除了修改周期结果的状态(3->1), + * 还需要修改loopmission_state_table表的状态(2->3或者6->7,对于状态为0的不需要修改,因为界面不会显示?更新状态时也不会统计), + * 及mission_state_table的状态(2->3或者6->7,对于1的情况,在初始化任务时,已经做过超时检查了,初始化后任务的状态变为2了就,所以此处不用对1的做超时检查), + * 注:将过期周期任务结果存入缓存后,上面进行统计时,就会根据结果的状态,统计loop表的状态,及mission_state_table表的状态,所以步骤3的3个修改,我们只需要做第一步,将结果存入缓存 + * + * 程序逻辑: + * 如果获得超时的任务:--所谓的超时,是指表mission_state_table的endTime小于当前时间 + * (1)从mission_state_table表查询:周期任务(isLoop=1 and missType=4),state=2或者6的,且endTime小于当前时间的任务ids + * (2)根据(1)生成的任务ids,查询结果表里的信息(只查找result=3或者5的),再生成MissionResult2类(使result=1) + * (3)将生成的MissionResult2存入缓存 + */ + public void checkTimeoutLoopMisssionAndAddResult() { + //第一步-- 超时周期任务 检查SQL + Date sysdateD = new Date(); + String sysdate = format.format(sysdateD);//为了统一时间,都去DC的时间 + + StringBuffer missionTimeoutCheckSQL = new StringBuffer(); + missionTimeoutCheckSQL.append(" select mst.mission_id, mst.mission_state"); + missionTimeoutCheckSQL.append(" from mission_state_table mst"); + missionTimeoutCheckSQL.append(" where mst.is_loop=1"); + missionTimeoutCheckSQL.append(" and mst.mission_type=4"); + missionTimeoutCheckSQL.append(" and mst.mission_state in(2,6)"); + missionTimeoutCheckSQL.append(" and mst.end_time fields = new ArrayList(); + fields.add("mission_id"); + fields.add("mission_state"); + try { + StringBuffer timeoutMissIds = new StringBuffer("0"); + List> timeoutMissMapList = dao.dbSelect(missionTimeoutCheckSQL.toString(), fields); + if(timeoutMissMapList != null && timeoutMissMapList.size()>0){ + //第二步 + for(Map map : timeoutMissMapList){ + timeoutMissIds.append(","+map.get("mission_id")); + } + System.out.println(timeoutMissIds.toString()); + + //对于超时周期任务的清理:任务结果为下发中的部分 + List mrList = getAllMissRltsForLoopTask(timeoutMissIds.toString()); + //第三步 + for( MissionResult2 mr : mrList){ +// MissionResult2 result2 = new MissionResult2(); +// //- 任务Id long +// result2.setMissionId(mr.getMissionId()); +// //-- 任务类型 long +// result2.setMissionType(4l); +// //- UUID long +// result2.setUuid(mr.getUuid()); +// //- 周期标识 long +// result2.setLoopFlag(1l); +// //- 开始执行时间 long +// result2.setStartTime(null); +// //- 结束时间 long +// result2.setEndTime(null); +// //- 配置截取文本 string +// result2.setText(null); + //- 结果状态 long + mr.setResult(1l); + //- 结果描述 string + mr.setDescription(MissionConstants.ERROR_RESULT_EXEC_TIMEOUT); + + Common.addResultToResultList(mr);//超时未执行的任务 + } + + //对于超时周期任务的清理:周期未初始化结果:loop表的state=0 or 1,将loop表的state修改为4未能执行 + updateTimeoutLoopMissionState(timeoutMissIds.toString()); + } + } catch (Exception e) { + logger.error("Check the timeout task in the cycle task and store the result of the timeout task into the cache exception",e); + }finally{ + dao.clearConn(); + } + } + + //清理指定任务id的周期任务:修改结果表和loop表时会先判断是否超时 + public void clearTimeoutLoopMisssion(Long missionId) { + try { + if(missionId!=null) { + //对于超时周期任务的清理:任务结果为下发中的部分 + List mrList = getAllMissRltsForLoopTask(missionId.toString()); + //第三步 + for( MissionResult2 mr : mrList){ + //- 结果状态 long + mr.setResult(1l); + //- 结果描述 string + mr.setDescription(MissionConstants.ERROR_RESULT_EXEC_TIMEOUT); + Common.addResultToResultList(mr);//超时未执行的任务 + } + //对于超时周期任务的清理:周期未初始化结果:loop表的state=0 or 1,将loop表的state修改为4未能执行 + updateTimeoutLoopMissionState(missionId.toString()); + } + } catch (Exception e) { + logger.error("Clear the specified cycle task and store the result of timeout task into cache exception",e); + }finally{ + dao.clearConn(); + } + } + + + private ArrayList getAllMissRltsForLoopTask(String missionIds){ + if(StringUtils.isBlank(missionIds) || "0".equals(missionIds)){ + return null; + } + + ArrayList mrList = new ArrayList(); + StringBuffer searchSQL = new StringBuffer(); + searchSQL.append("select lmst.mission_id, "); + searchSQL.append(" to_char(lmst.START_TIME,'"+Constants.DB_DATE_FORMAT+"') START_TIME, "); + searchSQL.append(" to_char(lmst.END_TIME,'"+Constants.DB_DATE_FORMAT+"') END_TIME, "); + searchSQL.append(" nt.node_ip, "); + searchSQL.append(" mrt.seq_id, "); + searchSQL.append(" nvl(mrt.result,3) result "); + searchSQL.append("from mission_result_table4 mrt "); + searchSQL.append("left join loopmission_state_table lmst on lmst.cur_mission_id = mrt.mission_id "); + searchSQL.append("left join (select distinct node_ip,ipn,seq_id from node_table n) nt on nt.seq_id=mrt.seq_id "); + searchSQL.append("where lmst.mission_id in("+missionIds+") "); + searchSQL.append(" and nvl(mrt.result,3) in(3,5)"); + searchSQL.append(" and lmst.end_time<=to_date('"+format.format(new Date())+"','yyyy-MM-dd HH24:mi:ss') "); + + ArrayList fields = new ArrayList(); + fields.add("mission_id"); + fields.add("START_TIME"); + fields.add("END_TIME"); + fields.add("node_ip"); + fields.add("result"); + fields.add("seq_id"); + ArrayList> rs = null; + try { + logger.debug(searchSQL.toString()); + rs = dao.dbSelect(searchSQL.toString(), fields); + //-- 整理到对象集合 + if(rs!=null && rs.size()>0){ + for(Map maps : rs){ + MissionResult2 result = new MissionResult2(); + result.setMissionId(Long.parseLong(maps.get("mission_id"))); +// result.setNodeIp(maps.get("node_ip")); + result.setUuid(maps.get("seq_id")==null ? null:Long.parseLong(maps.get("seq_id"))); + result.setResult(maps.get("result")==null ? null:Long.parseLong(maps.get("result"))); + + result.setEndTime(maps.get("END_TIME")==null ? null:format.parse(maps.get("END_TIME")).getTime()); + result.setStartTime(maps.get("START_TIME")==null ? null:format.parse(maps.get("START_TIME")).getTime()); +// result.setEndTime(maps.get("result")==null ? null:Long.parseLong(maps.get("result"))); + result.setMissionType(4l); + result.setLoopFlag(1l); + mrList.add(result); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + dao.clearConn(); + } + return mrList; + } + + + /** + * 将周期任务的部分结果设置为空 + * @time Nov 17, 2011-4:47:55 PM + */ +// public void checkAndUpdateLoopMissionComplete() { +// //-- 周期任务 的检查SQL +// StringBuffer loopSQL = new StringBuffer(); +// +//// SimpleDateFormat format = new SimpleDateFormat(); +// loopSQL.append("select lmst.cur_mission_id mission_id from ("); +// loopSQL.append("(select mrt.mission_id,sum(case when mrt.result = 0 then 1 else 0 end) ok,sum(case when mrt.result in (1,-1) then 1 else 0 end) fail,count(mrt.seq_id) total from mission_result_table4 mrt group by mrt.mission_id ) "); +// loopSQL.append("union all " + +// "(select mrt.mission_id,sum(case when mrt.result='7' then 1 else 0 end) ok,sum(case when mrt.result='0' then 1 else 0 end) fail,count(mrt.seq_id) total from mission_result_table4 mrt group by mrt.mission_id ) "); +// loopSQL.append(") t "); +// loopSQL.append("left join loopmission_state_table lmst on lmst.cur_mission_id = t.mission_id "); +// loopSQL.append("where t.ok + t.fail = t.total "); +// loopSQL.append("and nvl(t.mission_id,-1) <> -1 "); +// loopSQL.append("and lmst.mission_state not in (3,7) "); +// +// ArrayList fields = new ArrayList(); +// fields.add("mission_id"); +// try { +// StringBuffer missionIds = new StringBuffer("0"); +// List> mapList = dao.dbSelect(loopSQL.toString(), fields); +// //- 变更周期任务信息状态 loopmission_state_table +// if(mapList != null && mapList.size()>0){ +// for(Map map : mapList){ +// missionIds.append(","+map.get("mission_id")); +//// if(StringUtils.isNotEmpty(map.get("mission_id"))){ +//// createEmailContextForMission(Long.parseLong(map.get("mission_id"))); +//// } +// } +//// missionIds = missionIds.deleteCharAt(0); +// dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = 3 where mst.mission_state = 2 and mst.cur_mission_id in ("+missionIds.toString()+")"); +// dao.dbUpdate("update loopmission_state_table mst set mst.mission_state = 7 where mst.mission_state = 6 and mst.cur_mission_id in ("+missionIds.toString()+")"); +// } +// //- 变更周期任务信息状态 mission_state_table +// if(mapList != null && mapList.size()>0){ +// StringBuffer missionSql = new StringBuffer(); +// missionSql.append("select mst.mission_id from ("); +// missionSql.append("select lmst.mission_id,sum(decode(mrt.result,3,1,7,1,0) ) ok,sum(decode(mrt.result,4,1,0) ) fail,count(lmst.cur_mission_id) total from loopmission_state_table lmst group by lmst.mission_id "); +// missionSql.append(") t "); +// missionSql.append("left join mission_state_table mst on mst.mission_id = t.mission_id "); +// missionSql.append("where t.ok + t.fail = t.total "); +// missionSql.append("and nvl(t.mission_id,-1) <> -1 "); +// missionSql.append("and mst.is_loop = 1 "); +// missionSql.append("and lmst.mission_state not in(3,7,4) "); +// +// StringBuffer missionIds0 = new StringBuffer("0"); +// List> mapList0 = dao.dbSelect(loopSQL.toString(), fields); +// //- 变更周期任务信息状态 loopmission_state_table +// if(mapList != null && mapList.size()>0){ +// for(Map map : mapList0){ +// missionIds0.append(","+map.get("mission_id")); +// if(StringUtils.isNotEmpty(map.get("mission_id"))){ +// createEmailContextForMission(Long.parseLong(map.get("mission_id"))); +// } +// } +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 7,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务撤销完成' where mst.mission_state = 6 and mst.mission_id in ("+missionIds.toString()+") "); +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 3,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 任务执行完成' where mst.mission_state = 2 and mst.mission_id in ("+missionIds.toString()+") and mst.end_time <= sysdate"); +// dao.dbUpdate("update mission_state_table mst set mst.mission_state = 4,mst.auto_desc='"+format.format(System.currentTimeMillis())+" 当前系统时间 大于 结束时间,无法执行' where mst.mission_state = 1 and mst.end_time <= sysdate"); +// } +// } +// } catch (Exception e) { +// logger.error("",e); +// }finally{ +// dao.clearConn(); +// } +// } + /** + * 获取任务结果更新Sql + * @time Nov 16, 2011-1:32:07 PM + * @param mr + * @return + */ +// public String getUpdateSql(MissionResult2 mr2) { +// StringBuffer sql = new StringBuffer("update mission_result_table"); +// sql.append(mr2.getMissionType()+" mrt set "); +// sql.append("mrt.result ='"+(mr2.getResult()==null?1:mr2.getResult())+"' "); +// String desc = StringUtils.isEmpty(mr2.getDescription())?"":mr2.getDescription(); +// sql.append(",mrt.result_desc = substr(mrt.result_desc||'?',decode(sign(vsize(mrt.result_desc||'"+desc+"')-1024),-1,0,0,0,1,vsize(mrt.result_desc||'"+desc+"')+1-1024),1024) "); +// sql.append("where mrt.mission_id ='"+mr2.getMissionId().longValue()+"' "); +// sql.append("and mrt.seq_id ='"+mr2.getUuid().longValue()+"' "); +// return sql.toString(); +// } + +} \ No newline at end of file diff --git a/src/com/nms/server/snmp/trap/TrapServer.java b/src/com/nms/server/snmp/trap/TrapServer.java new file mode 100644 index 0000000..25dbea8 --- /dev/null +++ b/src/com/nms/server/snmp/trap/TrapServer.java @@ -0,0 +1,375 @@ +package com.nms.server.snmp.trap; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; + +import org.apache.log4j.Logger; +import org.snmp4j.CommandResponder; +import org.snmp4j.CommandResponderEvent; +import org.snmp4j.MessageDispatcherImpl; +import org.snmp4j.PDU; +import org.snmp4j.PDUv1; +import org.snmp4j.Snmp; +import org.snmp4j.TransportMapping; +import org.snmp4j.mp.MPv1; +import org.snmp4j.mp.MPv2c; +import org.snmp4j.mp.MPv3; +import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.security.AuthMD5; +import org.snmp4j.security.AuthSHA; +import org.snmp4j.security.PrivAES128; +import org.snmp4j.security.PrivAES192; +import org.snmp4j.security.PrivAES256; +import org.snmp4j.security.PrivDES; +import org.snmp4j.security.SecurityModels; +import org.snmp4j.security.SecurityProtocols; +import org.snmp4j.security.USM; +import org.snmp4j.smi.Address; +import org.snmp4j.smi.GenericAddress; +import org.snmp4j.smi.OID; +import org.snmp4j.smi.OctetString; +import org.snmp4j.smi.TcpAddress; +import org.snmp4j.smi.UdpAddress; +import org.snmp4j.smi.Variable; +import org.snmp4j.smi.VariableBinding; +import org.snmp4j.transport.DefaultTcpTransportMapping; +import org.snmp4j.transport.DefaultUdpTransportMapping; +import org.snmp4j.util.MultiThreadedMessageDispatcher; +import org.snmp4j.util.ThreadPool; + +import com.nms.server.bean.SetInfo; +import com.nms.server.bean.TrapMessageInfo; +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.util.SNMP4JUtils; + + +/** + * 本类用于监听代理进程的Trap信息 + * + * @author YJS + * + */ +public class TrapServer /**extends Service*/ +implements CommandResponder{// implements CommandResponder, extends Service + private static Logger logger = Logger.getLogger(TrapServer.class); + private static TrapServer ts = null; + public static TrapServer getInstance() throws IOException{ + if(ts == null){ + ts = new TrapServer(); + logger.info("SNMP Trap Server 启动成功"); + }else{ + logger.info("SNMP TrapServer 已启动"); + } + return ts; + } + + private TrapServer() throws IOException{ + ThreadPool threadPool = ThreadPool.create("Trap", Constants.SNMP_TRAP_THREAD_POOL_SIZE); + MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl()); + Address listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress", "udp:" + Common.getLocalIp() + "/" + Constants.SNMP_TRAP_PORT)); // 本地IP与监听端口 + TransportMapping transport = null; + +// 对TCP与UDP协议进行处理 + if (listenAddress instanceof UdpAddress) { + transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress); + } else { + transport = new DefaultTcpTransportMapping((TcpAddress) listenAddress); + } + + Snmp snmp = new Snmp(dispatcher, transport); + snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1()); + snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c()); + snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3()); + USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); + SecurityModels.getInstance().addSecurityModel(usm); + snmp.listen(); + snmp.addCommandResponder(this); +// OID authProtocol = getAuthProtocol(Constants.SNMP_V3_AUTH_PROTOCOL); //认证协议 +// OID privProtocol = getPrivProtocol(Constants.SNMP_V3_PRIV_PROTOCOL); //加密协议 +// OctetString securityName = new OctetString(Constants.SNMP_V3_SECURITY_NAME); //认证用户 +// OctetString authPassphrase = new OctetString(Constants.SNMP_V3_AUTH_PASSPHRASE); //认证密码明文 +// OctetString privPassphrase = new OctetString(Constants.SNMP_V3_PRIV_PASSPHRASE); //加密密码明文 +// snmp.getUSM().addUser(securityName, new UsmUser(securityName, authProtocol, authPassphrase,privProtocol, privPassphrase)); + } + + private OID getAuthProtocol(String auth){ + if (auth.equals("MD5")) { + return AuthMD5.ID; + } else if (auth.equals("SHA")) { + return AuthSHA.ID; + }else{ + return null; + } + } + + private OID getPrivProtocol(String priv){ + if (priv.equals("DES")) { + return PrivDES.ID; + } else if ((priv.equals("AES128")) + || (priv.equals("AES"))) { + return PrivAES128.ID; + } else if (priv.equals("AES192")) { + return PrivAES192.ID; + } else if (priv.equals("AES256")) { + return PrivAES256.ID; + }else{ + return null; + } + } + + + + + /** + * 实现CommandResponder的processPdu方法, 用于处理传入的请求、PDU等信息 当接收到trap时,会自动进入这个方法 + * + * @param respEvnt + */ + + public void processPdu(CommandResponderEvent commandresponderevent) + { + CommonDao dao = null; + try + { + dao = new CommonDao(); + logger.debug((new StringBuilder("接收到snmp event:")).append(commandresponderevent.getPDU()).toString()); + UdpAddress udpaddress = (UdpAddress)commandresponderevent.getPeerAddress(); + String s = udpaddress.getInetAddress().getHostAddress(); + PDU pdu = commandresponderevent.getPDU(); + logger.debug((new StringBuilder("接收到TRap:")).append(pdu).toString()); + TrapMessageInfo trapmessageinfo = new TrapMessageInfo(); + trapmessageinfo.setAgentSendIP(s); + setOIDList(trapmessageinfo, pdu); + setValueList(trapmessageinfo, pdu); + trapmessageinfo.setOriginalPDU(pdu); + if (pdu instanceof PDUv1) + { + PDUv1 pduv1 = (PDUv1)pdu; + trapmessageinfo.setTrapVersion(1); + String s1 = pduv1.getEnterprise().toString(); + trapmessageinfo.setTrapOID(s1); + String s2 = pduv1.getAgentAddress().toString(); + trapmessageinfo.setPduAgentIP(s); + trapmessageinfo.setTrapV1SpecificType(pduv1.getSpecificTrap()); + trapmessageinfo.setTrapV1GenericType(pduv1.getGenericTrap()); + trapmessageinfo.setTrapName(trapmessageinfo.getTrapOID()); + switch (pduv1.getGenericTrap()) + { + case 0: // '\0' +// trapmessageinfo.setTrapName("设备冷启动"); +// trapmessageinfo.setTrapName("Device Cold Start"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.coldStart_n81i"); + break; + + case 1: // '\001' +// trapmessageinfo.setTrapName("设备热启动"); +// trapmessageinfo.setTrapName("Device Hot Start"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.hotStart_n81i"); + break; + + case 2: // '\002' +// trapmessageinfo.setTrapName("接口关闭"); +// trapmessageinfo.setTrapName("The Interface Is Closed"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.interClose_n81i"); + break; + + case 3: // '\003' +// trapmessageinfo.setTrapName("接口启用"); +// trapmessageinfo.setTrapName("Interface Enabled"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.interUsed_n81i"); + break; + + case 4: // '\004' +// trapmessageinfo.setTrapName("SNMP认证失败"); +// trapmessageinfo.setTrapName("SNMP Authentication Failed"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.snmp_n81i"); + break; + + case 5: // '\005' +// trapmessageinfo.setTrapName("EGP邻居丢失"); +// trapmessageinfo.setTrapName("EGP Neighbor Lost"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.egp_n81i"); + break; + } + } else + { + trapmessageinfo.setTrapVersion(2); + Variable variable = trapmessageinfo.getOIDValue(SnmpConstants.snmpTrapAddress.toString()); + logger.debug("pdu.snmpTrapAddress() =-=-= "+variable); + if (variable != null) + trapmessageinfo.setPduAgentIP(variable.toString()); + Variable variable1 = trapmessageinfo.getOIDValue(SnmpConstants.snmpTrapOID.toString()); + if (variable1 != null) + trapmessageinfo.setTrapOID(variable1.toString()); + trapmessageinfo.setTrapName(trapmessageinfo.getTrapOID()); + if (variable1 != null) + if (variable1.equals(SnmpConstants.coldStart)) +// trapmessageinfo.setTrapName("设备冷启动"); +// trapmessageinfo.setTrapName("Device Cold Start"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.coldStart_n81i"); + else + if (variable1.equals(SnmpConstants.warmStart)) +// trapmessageinfo.setTrapName("设备热启动"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.hotStart_n81i"); + else + if (variable1.equals(SnmpConstants.linkDown)) +// trapmessageinfo.setTrapName("接口关闭"); +// trapmessageinfo.setTrapName("The Interface Is Closed"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.interClose_n81i"); + else + if (variable1.equals(SnmpConstants.linkUp)) +// trapmessageinfo.setTrapName("接口启用"); +// trapmessageinfo.setTrapName("Interface Enabled"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.interUsed_n81i"); + else + if (variable1.equals(SnmpConstants.authenticationFailure)) +// trapmessageinfo.setTrapName("SNMP认证失败"); +// trapmessageinfo.setTrapName("SNMP Authentication Failed"); + trapmessageinfo.setTrapName("i18n_server.UpgradeService.snmp_n81i"); + else //系统默认的类别不能匹配时,再使用自定义的trap信息库。是否需要分别自定义snmpV1和snmpValue2的trap信息库(定义到文件或者定义到数据库,必须体统重新加载的界面接口) + if(getTrapName(variable1)!=null &&!variable1.equals("")){ + trapmessageinfo.setTrapName(getTrapName(variable1)); + }else{ +// throw new Exception("未被定义的SNMPTRAP类型!"); + throw new Exception("Undefined SNMPTRAP type!"); + } + } + StringBuffer trapInfo = new StringBuffer(); + trapInfo.append("trapmessageinfo.getTrapName():"+trapmessageinfo.getTrapName()); + trapInfo.append(";trapmessageinfo.getAgentSendIP():"+trapmessageinfo.getAgentSendIP()); + trapInfo.append(";trapmessageinfo.getOriginalPDU():"+trapmessageinfo.getOriginalPDU()); + trapInfo.append(";trapmessageinfo.getTrapVersion():"+trapmessageinfo.getTrapVersion()); + trapInfo.append(";trapmessageinfo.getTrapOID():"+trapmessageinfo.getTrapOID()); + trapInfo.append(";trapmessageinfo.getPduAgentIP():"+trapmessageinfo.getPduAgentIP()); + trapInfo.append(";trapmessageinfo.getTrapV1GenericType():"+trapmessageinfo.getTrapV1GenericType()); + trapInfo.append(";trapmessageinfo.getTrapV1SpecificType():"+trapmessageinfo.getTrapV1SpecificType()); + logger.debug("agent IP:"+s+" trap信息:"+trapInfo.toString()); + new CommonService(dao).newClientCheck(s,trapmessageinfo.getTrapVersion()); + SetInfo info = Common.getSnmpTrapSetInfo(); + String [] dsb = Common.alarmExceptionInfo(info.getId(), s, info.getCheckTypeName(), info.getProcessIden(), trapmessageinfo.getReceiverTime().getTime(), 1,Constants.DETECTION_INFO_ALARM_WRONG, trapInfo.toString()); + Common.addAlarmData(dsb); + + } + catch (Exception exception) + { + logger.debug((new StringBuilder("解析UDP TRAP发生错误:")).append(exception.getMessage()).toString()); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + public String getTrapName(Variable variable1){ + String trapName = ""; + Map allTrapTypeDefine = new HashMap(); + trapName = allTrapTypeDefine.get(variable1.toString()).toString(); + return trapName; + } + + void setOIDList(TrapMessageInfo trapmessageinfo, PDU pdu) + { + Vector vector = pdu.getVariableBindings(); + String s; +// for(int i=0;i " + intLength); + for (int j = 0; j < intLength; j++) { + strOut += str[j]; + } + strCom += vb.getVariable(); + if (str.length != 1) { + logger.debug("==第行=vb.getVariable()=" + SNMP4JUtils.toStringHex(strOut));//显示中文 + } + } + return strCom; + + } + public static void main(String [] args) { + try { + TrapServer.getInstance(); + TrapServer.getInstance(); + TrapServer.getInstance(); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/com/nms/server/thread/DBTestThread.java b/src/com/nms/server/thread/DBTestThread.java new file mode 100644 index 0000000..d1f9453 --- /dev/null +++ b/src/com/nms/server/thread/DBTestThread.java @@ -0,0 +1,40 @@ +package com.nms.server.thread; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.dao.CommonDao; + +/** + * 定时检查数据库连接是否可用 + * @date May 7, 2013 2:31:50 PM + * @author ZhangGang + * + */ +public class DBTestThread implements Runnable{ + private Logger logger = Logger.getLogger(DBTestThread.class); + + @Override + public void run() { +// Thread.currentThread().setName("数据库连接监听线程"); + Thread.currentThread().setName("Database Connection Listening Thread"); + CommonDao dao = null; + try { + dao = new CommonDao(); + Common.setDbConnectedFlag(true); + } catch (Exception e) { + /* 告警 */ + Common.setDbConnectedFlag(false); + logger.error("Try to get a database connection failure", e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + +/* public static void main (String [] args) { + new Thread(new DBTestThread()).start(); + }*/ +} diff --git a/src/com/nms/server/thread/DetecDataCollectThread.java b/src/com/nms/server/thread/DetecDataCollectThread.java new file mode 100644 index 0000000..b7d71c5 --- /dev/null +++ b/src/com/nms/server/thread/DetecDataCollectThread.java @@ -0,0 +1,148 @@ +package com.nms.server.thread; + +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import org.apache.log4j.Logger; +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.thread.dataCollect.DataCollectManagerThread; +import com.nms.server.util.BoneCPPool; + +public class DetecDataCollectThread implements Runnable{ + private Logger logger = Logger.getLogger(DetecDataCollectThread.class); + + public DetecDataCollectThread(){ + + } + + @Override + public void run() { +// Thread.currentThread().setName("DC 数据收集管理线程 监测线程"); + Thread.currentThread().setName("DC Data Collection And Management Thread Monitoring Thread"); + + Object rlt = null; + boolean isDone = false; + boolean isCancel = false; + Future future = null; + try { + + Thread.sleep(5000); + future = Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER); + if(future!=null) { + isDone = future.isDone(); + isCancel = future.isCancelled(); + logger.info("DC已注册数据收集管理线程,isDone="+isDone+",isCancel="+isCancel); + rlt = future.get(); + logger.info("DC数据收集管理线程执行完毕,结果:"+rlt); + }else { + logger.info("DC 未注册 数据收集管理 线程,开始注册。。。"); + Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DATA_COLLECT,Constants.ERROR_INFO_SATAE_ERROR,Constants.ERROR_DESC_DATA_COLLECT_NO_RUN); + + //- DC端主动收集数据(监测数据、任务结果、回传文件)管理线程 + if(Constants.FLAG_DATA_COLLECT==1) { + Thread.sleep(60000); + //注销线程 + Common.cancelRunnableAtOnce(Constants.DATA_COLLECT_MANAGER); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new DataCollectManagerThread(),0, Constants.DATA_COLLECT_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DATA_COLLECT_MANAGER, fcFuture); + logger.info("DC 数据收集管理 线程 注册成功"); + Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DATA_COLLECT,Constants.ERROR_INFO_SATAE_RIGHT_AUTO,Constants.ERROR_DESC_DATA_COLLECT_NO_RUN); + + //单独起一个线程,监督数据收集线程是否运行 + Common.cancelRunnableAtOnce(Constants.DETECTI_DATA_COLLECT_THREAD_NAME);//取消其实是不起作用的? + Future detecDataCollectFuture = Common.service.submit(new DetecDataCollectThread()); + Common.registRunnable(Constants.DETECTI_DATA_COLLECT_THREAD_NAME, detecDataCollectFuture); + + } + } + } catch (Exception e) { + logger.error("DC data collection management thread monitoring thread exceptions, result:"+rlt,e); + + try { + Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DATA_COLLECT,Constants.ERROR_INFO_SATAE_ERROR,Constants.ERROR_DESC_DATA_COLLECT); + + //- DC端主动收集数据(监测数据、任务结果、回传文件)管理线程 + if(future!=null) { + isDone = future.isDone(); + isCancel = future.isCancelled(); + logger.info("DC 数据收集管理线程 运行状态:isDone="+isDone+",isCancel="+isCancel); + } + if(Constants.FLAG_DATA_COLLECT==1 && future!=null && future.isDone()) {//已执行完毕 + Thread.sleep(60000); + //注销线程 + logger.info("DC 数据收集管理 线程 已执行完毕(异常导致),再次注册。。。"); + Common.cancelRunnableAtOnce(Constants.DATA_COLLECT_MANAGER); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new DataCollectManagerThread(),0, Constants.DATA_COLLECT_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DATA_COLLECT_MANAGER, fcFuture); + logger.info("DC 数据收集管理 线程 注册成功"); + Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DATA_COLLECT,Constants.ERROR_INFO_SATAE_RIGHT_AUTO,Constants.ERROR_DESC_DATA_COLLECT); + + + //单独起一个线程,监督数据收集线程是否运行 + Common.cancelRunnableAtOnce(Constants.DETECTI_DATA_COLLECT_THREAD_NAME); + Future detecDataCollectFuture = Common.service.submit(new DetecDataCollectThread()); + Common.registRunnable(Constants.DETECTI_DATA_COLLECT_THREAD_NAME, detecDataCollectFuture); + } + }catch(Exception ee) { + logger.error("DC data collection management thread is abnormal when registered again",ee); + } + + }finally{ +// try{ +// //单独起一个线程,监督数据收集线程是否运行 +// Common.cancelRunnableAtOnce(Constants.DETECTI_DATA_COLLECT_THREAD_NAME); +// Future detecDataCollectFuture = Common.service.submit(new DetecDataCollectThread()); +// Common.registRunnable(Constants.DETECTI_DATA_COLLECT_THREAD_NAME, detecDataCollectFuture); +// }catch (Exception e) { +// logger.error("DC 数据收集管理线程 监测线程 注销 异常", e); +// } + logger.debug("DC 数据收集管理线程 监测线程 结束"); + } + } + + public static void main (String []args){ + try { + + System.out.println(Long.MAX_VALUE/1000/60/60/24); + + BoneCPPool.initPool(); + CommonDao dao = new CommonDao(); + CommonService service = new CommonService(dao); + + // 初始化自身信息(失败情况下会由Web唤醒) + if(!service.initServerParams()){ + return; + } + + + +// //- DC端主动收集数据(监测数据、任务结果、回传文件)管理线程 + if(Constants.FLAG_DATA_COLLECT==1) { + //注销线程 + Common.cancelRunnableAtOnce(Constants.DATA_COLLECT_MANAGER); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new DataCollectManagerThread(),0, Constants.DATA_COLLECT_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DATA_COLLECT_MANAGER, fcFuture); + } +// +// new Thread(new DetecDataCollectThread()).start(); +// +// //- 错误信息(DC程序运行异常捕获报警)解析管理线程 +// if(Constants.FLAG_ERROR_INFO==1){ +// //注销线程 +// Common.cancelRunnableAtOnce(Constants.ERROR_INFO_RESOVE_MANAGER); +// ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new ErrorInfoResoveManagerThread(), 0, 30, TimeUnit.SECONDS); +// //注册 管理线程 +// Common.registRunnable(Constants.ERROR_INFO_RESOVE_MANAGER, fcFuture); +// } + }catch(Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/src/com/nms/server/thread/DetecSelfThread.java b/src/com/nms/server/thread/DetecSelfThread.java new file mode 100644 index 0000000..b0f2115 --- /dev/null +++ b/src/com/nms/server/thread/DetecSelfThread.java @@ -0,0 +1,115 @@ +package com.nms.server.thread; + +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.Map; +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; + +public class DetecSelfThread implements Runnable{ + private Logger logger = Logger.getLogger(DetecSelfThread.class); + + public DetecSelfThread(){ + + } + + @Override + public void run() { +// Thread.currentThread().setName("DC 性能监测线程"); + Thread.currentThread().setName("DC Performance Monitoring Thread"); + + CommonDao dao = null; + try { +// dao = new CommonDao(); + if(Constants.sslServer==null) { + logger.info("DC监听端口状况(60702):sslServer=null"); + }else { + logger.info("DC监听端口状况(60702):isBind="+Constants.sslServer.isBound()); + } + + Future future = Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER); + Future futureDetect = Common.getFutureMap().get(Constants.DETECTI_DATA_COLLECT_THREAD_NAME); + if(future!=null) { + boolean isDone = future.isDone(); + boolean isCancel = future.isCancelled(); + logger.info("DC已注册数据收集管理线程,isDone="+isDone+",isCancel="+isCancel); + }else { + logger.info("DC未注册数据收集管理线程"); + } + if(futureDetect!=null) { + logger.info("DC 数据收集管理线程 监测线程 运行中,isDone="+futureDetect.isDone()+",isCancel="+futureDetect.isCancelled()); + }else { + logger.info("DC 数据收集管理线程 监测线程 未启动"); + } + long freeMemory = Runtime.getRuntime().freeMemory()/1024/1024; + long totalMemory = Runtime.getRuntime().totalMemory()/1024/1024; + long maxMemory = Runtime.getRuntime().maxMemory()/1024/1024; + long usedMemory = totalMemory - freeMemory; + int activeCount = Thread.activeCount();//活跃线程数 + logger.debug("内存使用情况:total-> " + totalMemory +" MB, used -> "+ usedMemory +" MB, max -> " + maxMemory+" MB, free -> " + freeMemory+" MB \r\n 活跃线程数: "+ activeCount); + Common.detecSelf(); + if(logger.isDebugEnabled() && "druid".equalsIgnoreCase(Constants.DB_POOL_TYPE)){ + + } + + +// int [] links = getDBLinkSize(dao); + /* 查询当前DC,数据库连接数 */ +// logger.debug("当前数据库连接数:"+links[0]+" 活动连接数:"+links[1]); +// System.gc(); + } catch (Exception e) { + logger.error("Monitoring their own thread abnormality",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("结束"); + } + } + + public static void main (String []args){ + new Thread(new DetecSelfThread()).start(); + /*Map map = System.getenv(); + pl("用户名称"+map.get("USERNAME")); + pl("计算机名称"+map.get("COMPUTERNAME")); + pl(""+map.get("USEROOMAIN"));*/ + } + + /** + * 查询当前DC的数据库连接数 + * @time Jan 13, 2013-4:25:03 PM + * @param dao + * @return + * @throws Exception + */ + public int[] getDBLinkSize(CommonDao dao) throws Exception{ + ArrayList fields = new ArrayList(); + fields.add("dbLinks"); + fields.add("activeLinks"); +// Map map = System.getenv(); + String dbLinkSizeSql = "select count(1) dbLinks,count(decode(status,'ACTIVE',1,0,0)) activeLinks from v$session t where lower(username) =lower(?) and lower(t.MACHINE) = lower(?)"; + String dbUserName = Constants.DB_USER_NAME; +// String computerName = map.get("COMPUTERNAME"); + String computerName = InetAddress.getLocalHost().getHostName(); + logger.debug("计算机名称:"+computerName); +// logger.debug("计算机名称:"+name); + ArrayList> data = dao.dbSelect(dbLinkSizeSql, fields,dbUserName,computerName); + if(data!= null && data.size()>0){ + String val = data.get(0).get("dbLinks"); + String val2 = data.get(0).get("activeLinks"); + return new int[]{val == null ? -1 : Integer.parseInt(val),val2 == null ? -1 : Integer.parseInt(val2)}; + }else{ + return new int[]{-1,-1}; + } + } + + public static void pl(Object obj) { + System.out.println(obj==null?null:obj.toString()); + } +} \ No newline at end of file diff --git a/src/com/nms/server/thread/GetLocalIpFromWeb.java b/src/com/nms/server/thread/GetLocalIpFromWeb.java new file mode 100644 index 0000000..2d44fca --- /dev/null +++ b/src/com/nms/server/thread/GetLocalIpFromWeb.java @@ -0,0 +1,25 @@ +package com.nms.server.thread; + +import com.nms.server.util.socket.SSLSocketCallable; + +/** + * 通过通讯获取本机IP(暂未使用) + * @date Mar 14, 2013 5:23:58 PM + * @author ZhangGang + * + */ +public class GetLocalIpFromWeb extends SSLSocketCallable{ + + + public GetLocalIpFromWeb(String ip, Integer port) throws Exception { + super(ip, port); + } + + protected Object toDo() throws Exception { + Thread.currentThread().setName("Get The IP Thread"); + this.sendMessage("char:getLocalIp"); + String lIp = this.receiveMessage(); + this.sendMessage(SUCCESS); + return lIp; + } +} diff --git a/src/com/nms/server/thread/InitServerThread.java b/src/com/nms/server/thread/InitServerThread.java new file mode 100644 index 0000000..630eb2d --- /dev/null +++ b/src/com/nms/server/thread/InitServerThread.java @@ -0,0 +1,491 @@ +package com.nms.server.thread; + +import java.io.File; +import java.sql.SQLException; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.ServerTable; +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.service.UpgradeService; +import com.nms.server.snmp.trap.TrapServer; +import com.nms.server.thread.alarmData.AlarmDataResoveManagerThread; +import com.nms.server.thread.change.ChangeManagerThread; +import com.nms.server.thread.dataCollect.DataCollectManagerThread; +import com.nms.server.thread.dataCollect.NoDetectDataCollectManagerThread; +import com.nms.server.thread.dataCollect.NonRltTaskResultCollectManagerThread; +import com.nms.server.thread.deleteFiles.DeleteFilesManagerThread; +import com.nms.server.thread.detecData.DetecDataResoveManagerThread; +import com.nms.server.thread.detectDataHandler.DataInsertManagerThread; +import com.nms.server.thread.detectDataHandler.DataResolveThread; +import com.nms.server.thread.detectDataHandler.DetectInfoInsertThread; +import com.nms.server.thread.email.RefreshEmailFlagThread; +import com.nms.server.thread.errorinfo.ErrorInfoResoveManagerThread; +import com.nms.server.thread.file.UnZipManagerThread; +import com.nms.server.thread.file.upload.FileUploadManagerThread; +import com.nms.server.thread.mission.InitRunningMissionThread; +import com.nms.server.thread.mission.LoadNewMissionThread; +import com.nms.server.thread.mission.MissionResultManagerThread; +import com.nms.server.thread.overrunData.DataFileReaderManagerThread; + +/** + * DC初始化启动线程 + * 初始化流程 + * 1、初始化数据库连接,加载DC自身信息 + * 2、初始化本地目录信息,公共缓存信息 + * 3、启动各解析线程 + * 4、启动任务和主动监测现成 + * 5、启动SNMP TRAP端口 + * 6、变更启动标示 + * @date Mar 27, 2013 2:47:31 PM + * @author ZhangGang + * + */ +public class InitServerThread implements Runnable{ + private Logger logger = Logger.getLogger(InitServerThread.class); + + @Override + public void run() { + +// Thread.currentThread().setName("初始化线程"); + Thread.currentThread().setName("Initialization thread"); + + CommonDao dao = null; + try { + + // 连接数据库 + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + // 初始化自身信息(失败情况下会由Web唤醒) + if(!service.initServerParams()){ + return; + } + + // 根据初始化成功标示,初始化DC实际操作信息 + if(!Constants.flag_init){ + + //- 目录及缓存信息(监测类型和表、数据字段、告警、ip主机映射等) + initCommonInfo(dao); + + try + { + //- 启动SNMP监测服务端口 + Common.getSnmp().addCommandResponder(TrapServer.getInstance()); + } catch (Exception e) + { + logger.error("Start the SNMP monitoring service port exception", e); + } + + //- 监测数据解析 + if(Constants.FLAG_DETEC_RESOVE==1){ + if(Constants.DETECT_INSERT_THREAD_MODE == 1){//单线程 + //注销线程 + Common.cancelRunnableAtOnce(Constants.DETEC_DATA_RESOVE_MANAGER); + int delay = 0;//管理线程延时多久启动 + Date dbNow = service.getDbTime(); + long dbLong = dbNow.getTime(); + int period = Constants.DETEC_DATA_RESOLVE_PERIOD*1000;//周期:ms + ServerTable st = Common.getServerTable(); + int count = st.getConut(); + int index = st.getIndex(); + int cst = (index-1) *period/count;//当前dc 的时间窗口 开始时间 + int pass = (int) (dbLong%period); + + if(cst >= pass){ + delay = cst - pass; + }else{ + delay = period -pass + cst; + } + logger.debug("共 "+count+" 个 有效DC,当前DC index : "+ index+", 数据库当前时间 :" + dbNow.toLocaleString() +" ,DETEC_DATA_RESOLVE_PERIOD :" + Constants.DETEC_DATA_RESOLVE_PERIOD +" s , 监测数据解析入库管理线程延迟:" + delay +" ms 启动"); + /** + * 解析线程scheduleWithFixedDelay不能修改为scheduleAtFixedRate是因为:解析管理线程很快就会执行完毕,不会像收集线程,需要等待节点收集完毕, + * 而且解析线程会尝试5次,如果都在解析,则会将数据存入硬盘,如果改了,那么解析的周期就更短了,因为管理线程几乎用不了1秒就执行完了, + * 这样解析线程就只有5秒左右的执行时间,会有大问题的(解析线程需要延长时间,收集线程需要缩短时间) + */ + /** + * 2018年6月14日14:34:49 修改 根据dc 个数固定 管理线层启动 时间窗口,分散一下dc 入库时间,固定周期时间启动 + */ + ScheduledFuture fcFuture = Common.scheduled.scheduleAtFixedRate(new DetecDataResoveManagerThread(), delay, period, TimeUnit.MILLISECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DETEC_DATA_RESOVE_MANAGER, fcFuture); + }else if(Constants.DETECT_INSERT_THREAD_MODE == 2){//多线程,info表一个线程,其它每个监测详细信息表一个线程共用一个线程池 + int im = Constants.DETECT_INFO_THREAD_MODE; + int it = Constants.DETECT_INFO_THREAD_TIME; + int id = Constants.DETECT_INFO_THREAD_DELAY; + int dm = Constants.DETECT_DETAIL_THREAD_MODE; + int dt = Constants.DETECT_DETAIL_THREAD_TIME; + int dd = Constants.DETECT_DETAIL_THREAD_DELAY; + + Common.cancelRunnableAtOnce(Constants.DETECT_INFO_THREAD_KEY); + Common.cancelRunnableAtOnce(Constants.DETECT_DETAIL_THREAD_KEY); + Common.cancelRunnableAtOnce(Constants.DETECT_RESOVE_THREAD_KEY); + + //info入库线程运行模式,1:每隔 n秒 运行一次,2:间隔 n 秒运行一次 + if(im == 1){ + ScheduledFuture fcFuture = Common.scheduled.scheduleAtFixedRate(new DetectInfoInsertThread(), id, it, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DETECT_INFO_THREAD_KEY, fcFuture); + }else{ + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new DetectInfoInsertThread(), id, it, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DETECT_INFO_THREAD_KEY, fcFuture); + } + + if(dm == 1){ + ScheduledFuture fcFuture = Common.scheduled.scheduleAtFixedRate(new DataInsertManagerThread(), dd, dt, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DETECT_DETAIL_THREAD_KEY, fcFuture); + }else{ + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new DataInsertManagerThread(), dd, dt, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DETECT_DETAIL_THREAD_KEY, fcFuture); + } + + Future future = Common.service.submit(new DataResolveThread()); + Common.registRunnable(Constants.DETECT_RESOVE_THREAD_KEY, future); + }else{ + logger.error("detect.insert.thread.mode = " + Constants.DETECT_INSERT_THREAD_MODE +" is error"); + } + } + + //- 错误信息(DC程序运行异常捕获报警)解析管理线程 + if(Constants.FLAG_ERROR_INFO==1){ + //注销线程 + Common.cancelRunnableAtOnce(Constants.ERROR_INFO_RESOVE_MANAGER); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new ErrorInfoResoveManagerThread(), 0, Constants.ERROR_INFO_RESOVE_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.ERROR_INFO_RESOVE_MANAGER, fcFuture); + } + + //- 告警数据解析管理线程 + if(Constants.FLAG_ALARM_RESOVE==1){ + //注销线程 + Common.cancelRunnableAtOnce(Constants.ALARM_DATA_RESOVE_MANAGER); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new AlarmDataResoveManagerThread(), 0, Constants.DETEC_DATA_RESOLVE_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.ALARM_DATA_RESOVE_MANAGER, fcFuture); + } + + //-硬盘数据(监测数据、告警数据、任务结果:由于数据量过大,暂时无法入库,而存放在硬盘上的数据)解析入库管理线程 + if(Constants.FLAG_FILES_READER_RESOVE==1){ + //注销线程 + Common.cancelRunnableAtOnce(Constants.OVERRUN_DATA_RESOVE_MANAGER); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new DataFileReaderManagerThread(), 0, Constants.DISK_DATA_RESOLVE_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.OVERRUN_DATA_RESOVE_MANAGER, fcFuture); + } + + + //- DC端主动收集数据(监测数据、任务结果、回传文件)管理线程 + if(Constants.FLAG_DATA_COLLECT==1) { + //注销线程 + Common.cancelRunnableAtOnce(Constants.DATA_COLLECT_MANAGER); + /** + * 收集线程scheduleWithFixedDelay修改为scheduleAtFixedRate是因为: + * scheduleWithFixedDelay:等待线程执行完毕,还会再等待指定的周期时间 + * scheduleAtFixedRate:为了节省时间,且线程执行完毕 + * 1.线程执行时间大于周期间隔,则直接执行下一周期 + * 2.线程执行时间小于周期间隔,则两次线程开始执行时间点的间隔就是设定的周期 + * + * (解析线程需要延长时间,收集线程需要缩短时间) + */ + ScheduledFuture fcFuture = Common.scheduled.scheduleAtFixedRate(new DataCollectManagerThread(),0, Constants.DATA_COLLECT_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.DATA_COLLECT_MANAGER, fcFuture); + + Common.cancelRunnableAtOnce(Constants.NO_DETECT_DATA_NODE_DATA_COLLECT_MANAGER); + ScheduledFuture enFuture = Common.scheduled.scheduleAtFixedRate(new NoDetectDataCollectManagerThread(),0, Constants.ERROR_NODE_DATA_COLLECT_PERIOD, TimeUnit.SECONDS); + Common.registRunnable(Constants.NO_DETECT_DATA_NODE_DATA_COLLECT_MANAGER, enFuture); + + + } + + //- 定时的刷新是否启用邮件发送功能:从数据库读取配置 + if(Constants.FLAG_EMAIL_START==1) { + //注销线程 + Common.cancelRunnableAtOnce(Constants.EMAIL_START_THREAD); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new RefreshEmailFlagThread(),0, Constants.REFRESH_EMAIL_FLAG_PEROID, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.EMAIL_START_THREAD, fcFuture); + } + + //定时删除log线程 + if(Constants.FLAG_DELETE_TMP_FILES==1) { + //注销线程 + Common.cancelRunnableAtOnce(Constants.DELETE_TMP_FILES_MANAGER); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new DeleteFilesManagerThread(),0, Constants.DELETE_TMP_FILES_PERIOD, TimeUnit.HOURS); + //注册 管理线程 + Common.registRunnable(Constants.DELETE_TMP_FILES_MANAGER, fcFuture); + } + + + //DC端定时的去收集执行完成但无任务结果返回的任务的结果信息 + if(Constants.FLAG_NONRLTTASK_RESULT_COLLECT==1) { + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new NonRltTaskResultCollectManagerThread(),0, Constants.NONRLTTASK_RESULT_COLLECT_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.NONRLTTASK_RESULT_COLLECT_MANAGER, fcFuture); + } + + + //- 文件上传管理线程 + if(Constants.FLAG_FILE_UPLOAD==1){ + //注销线程 + Common.cancelRunnableAtOnce(Constants.MISSION_FILE_UPLOAD); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new FileUploadManagerThread(), 0, Constants.MISSION_FILE_UPLOAD_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.MISSION_FILE_UPLOAD, fcFuture); + } + + //- 变更操作管理线程 + if(Constants.FLAG_CHANGE_OPERATIONS==1){ + //注销线程 + Common.cancelRunnableAtOnce(Constants.CHANGE_OPERATIONS_MANAGER); + ScheduledFuture fcFuture = Common.scheduled.scheduleWithFixedDelay(new ChangeManagerThread(), 0, Constants.CHANGE_OPERATIONS_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.CHANGE_OPERATIONS_MANAGER, fcFuture); + } + + //- 任务初始化分配和任务线程启动 + if(Constants.FLAG_MISSION==1){ + //-- 启动已执行任务加载线程 + Common.scheduled.schedule(new InitRunningMissionThread(null),Constants.MISSION_FIRST_START, TimeUnit.SECONDS); + + //-- 启动已创建任务加载线程 + Common.scheduled.scheduleWithFixedDelay(new LoadNewMissionThread(null), Constants.MISSION_CHECK_NEW_START, Constants.MISSION_CHECK_NEW_PERIOD, TimeUnit.SECONDS); + + //启动任务结果管理线程 + if(Constants.FLAG_MISSION_RESULT==1){ + //注销线程 + Common.cancelRunnableAtOnce(Constants.MISSION_RESULT_RESOVE_MANAGER); + //logger.debug(Constants.MISSION_RESULTS_SAVE_START+" -- "+Constants.MISSION_RESULTS_SAVE_PERIOD); + ScheduledFuture mrFuture = Common.scheduled.scheduleWithFixedDelay(new MissionResultManagerThread(),Constants.MISSION_RESULTS_SAVE_START, Constants.MISSION_RESULTS_SAVE_PERIOD, TimeUnit.SECONDS); + //注册 管理线程 + Common.registRunnable(Constants.MISSION_RESULT_RESOVE_MANAGER, mrFuture); + } + } + + //服务端NMSC监测任务添加--一种监测--此处的握手监测仅为非服务器的握手监测 + if(Constants.FLAG_NMSC==1){ + initDrivingDetectionThread(dao,Constants.DETEC_NMSC_STR); + } + + //服务端SNMP监测任务添加-system,snmp4j,objectsnmp:特设的电源、风扇、网元系统信息等?---snmp类型的监测,多个 + if(Constants.FLAG_SNMP==1){ + initDrivingDetectionThread(dao,Constants.DETEC_SNMP_STR); + } + //服务端PING监测任务添加--一种监测 + if(Constants.FLAG_PING==1){ + initDrivingDetectionThread(dao,Constants.DETEC_PING_STR); + } + //服务端SWITCH监测任务添加-交换机、特种设备:端口流量监测等--一种监测 + if(Constants.FLAG_SWITCH==1){ + initDrivingDetectionThread(dao,Constants.DETEC_SWITCH_STR); + } + //服务端SYSTEMDATE监测任务添加--一种监测 + if(Constants.FLAG_SYSTEMDATE==1){ + initDrivingDetectionThread(dao,Constants.DETEC_SYSTEMDATE_STR); + } + + //压缩文件解压操作 + if(Constants.FLAG_ZIP==1){ + //启动时,执行一次压缩文件解压操作 + Common.service.submit(new UnZipManagerThread()); + } + if(Constants.DETECSELFTHREAD_FLAG){ + //缓存数据量监测线程 + Common.cancelRunnableAtOnce(Constants.DETECTI_SELF_THREAD_NAME); + ScheduledFuture dsFuture = Common.dcPrivScheduled.scheduleAtFixedRate(new DetecSelfThread(),Constants.DETECSELFTHREAD_INITIALDELAY,Constants.DETECSELFTHREAD_PERIOD,TimeUnit.SECONDS); + Common.registRunnable(Constants.DETECTI_SELF_THREAD_NAME, dsFuture); + } + + + //单独起一个线程,监督数据收集线程是否运行:放在收集数据线程之后即可 + if(Constants.FLAG_DATA_COLLECT==1) { + Common.cancelRunnableAtOnce(Constants.DETECTI_DATA_COLLECT_THREAD_NAME); + Future detecDataCollectFuture = Common.service.submit(new DetecDataCollectThread()); + Common.registRunnable(Constants.DETECTI_DATA_COLLECT_THREAD_NAME, detecDataCollectFuture); + } + + + //DC缓存信息定时刷新功能 + Common.cancelRunnableAtOnce(Constants.RESET_SERVER_THREAD_NAME); + ScheduledFuture rsFuture = Common.dcPrivScheduled.scheduleAtFixedRate(new ResetServerThread(-1),8,8,TimeUnit.HOURS); + Common.registRunnable(Constants.RESET_SERVER_THREAD_NAME, rsFuture); + + //数据库连接监听线程 + Common.cancelRunnableAtOnce(Constants.DB_CONNECTION_THREAD_NAME); + ScheduledFuture dbcFuture = Common.dcPrivScheduled.scheduleAtFixedRate(new DBTestThread(),3,3,TimeUnit.MINUTES); + Common.registRunnable(Constants.DB_CONNECTION_THREAD_NAME, dbcFuture); + + logger.info("------- DataController Start Success------------"); + Constants.flag_init = true; + } + + } catch (SQLException e) { + logger.error("Failed to get database connection, execute again in 5 minutes"+ e.getMessage()); + Common.scheduled.schedule(this, 5, TimeUnit.MINUTES); + Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DB_CONNECT,Constants.ERROR_INFO_SATAE_ERROR,""); + } catch (Exception e) { + logger.error("DataController initialization failure",e); + Common.scheduled.schedule(this, 5, TimeUnit.MINUTES); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("启动程序dao已关闭"); + } + } + + /** + * 初始化主动监测线程 + * @time Sep 20, 2011-7:07:36 PM + * @param dao + * @return + */ + public boolean initDrivingDetectionThread(CommonDao dao,String checkType){ + + boolean isSuccess = true; + CommonService commonService = new CommonService(dao); + ThreadService service = new ThreadService(); + //--获取checkType 监测配置并启动监测任务 + List setList = commonService.getDrivingSetInfo(checkType);//获得checkType类型的所有监测设置 + logger.info("检查主动监测类别:"+checkType+" 关联监测设置:"+(setList==null?0:setList.size())+"个"); + //-- checkType 监测任务启动 和 客户端启动监测任务启动 + if(setList!=null && setList.size()>0){ + for (Iterator setIte = setList.iterator(); setIte.hasNext();) { + SetInfo si = (SetInfo) setIte.next(); + if(Constants.DETEC_SNMP_STR.equalsIgnoreCase(checkType)){ + if(Constants.DETEC_SWITCH_STR.equalsIgnoreCase(si.getCheckTypeName())){ + continue; + } + } + service.updateThread(si); + + } + } + return isSuccess; + } + + /** + * 初始化公共信息方法 + *

Common类中的公共数据集合,在此方法中进行数据初始化

+ * Init server + * @time Aug 24, 2011-9:55:44 AM + * @return + */ + public boolean initCommonInfo(CommonDao dao) { + initCheck(); + boolean flag = true; + + CommonService service = new CommonService(dao); + UpgradeService upService = new UpgradeService(dao); + try{ + //-- 从数据库中查询邮件创建信息 + Constants.flag_email = service.getFlagEmail(); + //-- checkTypeName TableModel Map Init(监测类别名称 与 检测类别信息) + service.updateCheckTableInfo(Common.getInsertTable(), null); + service.updateSetInfoName(Common.getSetInfoNameMape(), null); + //-- Table Columns Map Init(数据库表结构信息) + service.updateTableColumnsMap(Common.getTableMap(), null); + + //alarm Ping Init(告警数据结构信息) + service.updateAlarmInfo(Common.getAlarmInfoMap(),null); + + //-- LoginIp nodeip Init(seq_id-物理节点信息) + service.updateSimpleNodeByUUID(null); + + //--(ClientIP-seq_id信息) + service.updateSeqIdByNodeIp(Common.getIpSeqIdMap(),null); + + service.updateUserPwdByNodeIp(Common.getIpUserPwdMap(),null); + + // init loop Mission info + upService.updateLoopMissionInfo(Common.getLoopMissionInfoMap(), null); + upService.updateLoopMissionRoundInfo(Common.getLoopMissionRoundInfoList(), null); + + // init detection info + List siList = service.getDrivingSetInfo(Constants.ALARM_SNMP_TRAP_STR); + if(siList !=null && siList.size()>0){ + Common.setSnmpTrapSetInfo(siList.get(0)); + } + + }catch (Exception e) { + logger.debug("NMSServer参数初始化失败", e); + System.exit(0); + } + + return flag; + } + + /** + * 程序启动时校验操作 + *

必要文件目录的存在校验操作、及相关必要存在校验操作

+ * @time Aug 24, 2011-10:14:35 AM + * @return + */ + public void initCheck() { + + File file = new File(Constants.MISSION_FILE_UPLOAD_DIR); + if (!file.exists()) { + file.mkdirs(); + } + + file = new File(Constants.MISSION_FILE_DOWNLOAD_DIR); + if (!file.exists()) { + file.mkdirs(); + } + + file = new File(Constants.ZIP_FILE_DETECT_DATA_DIR); + if (!file.exists()) { + file.mkdirs(); + } + + file = new File(Constants.ZIP_FILE_TASK_RESULT_DIR); + if (!file.exists()) { + file.mkdirs(); + } + + file = new File(Constants.ZIP_FILE_TASK_RETURN_DIR); + if (!file.exists()) { + file.mkdirs(); + } + + file = new File(Constants.ZIP_RESOVE_DETECT_DATA_DIR); + if (!file.exists()) { + file.mkdirs(); + } + + file = new File(Constants.ZIP_RESOVE_TASK_RESULT_DIR); + if (!file.exists()) { + file.mkdirs(); + } + file = new File(Constants.ZIP_RESOVE_TASK_RESULT_DIR); + if (!file.exists()) { + file.mkdirs(); + } + + file = new File(Constants.ZIP_RESOVE_TASK_RETURN_DIR); + if (!file.exists()) { + file.mkdirs(); + } + file = new File(Constants.ERROR_DETEC_FILE_DIR); + if (!file.exists()) { + file.mkdirs(); + } + +// return flag; + } +} diff --git a/src/com/nms/server/thread/ResetServerThread.java b/src/com/nms/server/thread/ResetServerThread.java new file mode 100644 index 0000000..4c4208b --- /dev/null +++ b/src/com/nms/server/thread/ResetServerThread.java @@ -0,0 +1,106 @@ +package com.nms.server.thread; + +import java.io.File; +import java.io.IOException; +import org.apache.log4j.Logger; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.ChangeService; +import com.nms.server.util.CommandExecUtil; + +public class ResetServerThread implements Runnable{ + private Logger logger = Logger.getLogger(ResetServerThread.class); + private Integer model = -1; //缺省为-1 + + public ResetServerThread(Integer model){ + this.model = model==null?this.model:model; + } + + @Override + public void run() { + +// Thread.currentThread().setName("程序整理线程"); + Thread.currentThread().setName("Program Manage Thread"); + CommonDao dao = null; + try { + dao = new CommonDao(); + ChangeService service = new ChangeService(dao); + switch (model) { + case 1: //更新自身参数信息 + //NMSServer参数获取失败后,结束程序初始化 + + //- 系统信息初始化(监测类型和表、数据字段、告警、ip主机映射等) + if(!Constants.flag_init){//新安装DC:DC没在web的管理范围内,初始化失败 + logger.info("启动失败,再次启动"); + Common.service.submit(new InitServerThread()); + }else{ + logger.info("启动成功,仅更新参数"); + service.initServerParams();//web端修改DC信息(管理范围修改)时执行 + new InitServerThread().initCommonInfo(dao); + logger.info("参数更新完成"); + } + /*业务重新梳理 + * List list = service.getAllEffectiveNodeInfo(); + if(list!= null && list.size()>0){ + Iterator ite = list.iterator(); + while(ite.hasNext()){ + NodeModel model = ite.next(); + Common.runChangeRunnable(new SSLClient(model.getNodeIp(), Constants.SSL_CLIENT_PORT,"char:reInit","init")); + } + }*/ + + break; +// case 2://重启DC +// String os = System.getProperty("os.name"); +// if (os.startsWith("Windows")) {//重启服务 +//// String path = new File(Constants.SYSTEM_PATH).getParent()+File.separator+Constants.COMMON_TEMP_DIR+File.separator+Constants.COMMON_RUNTIME_PID_FILE; +//// String processName = "DataController.exe"; +//// String path = System.getProperty("user.dir")+"/"+processName; +//// String serverName = "DataController"; +// +// logger.info("web端超周期无监测数据,开始开始重启DC。。。"); +// +// //不行------自己把自己杀了,就没法把自己重启了 +//// if(!CommandExecUtil.killProcess(processName)){/* 删除已存在的进程 */ +//// logger.info("程序停止失败"); +//// }else if(CommandExecUtil.processRunningCheck(processName)){/* 校验程序是否已停止 */ +//// logger.info("程序停止失败,请手动关闭进程["+processName+"]"); +//// }else if(!CommandExecUtil.startNetService(serverName)){/* 启动进程 */ +//// logger.info("程序启动失败"); +//// }else if(CommandExecUtil.processRunningCheck(processName)){/* 校验程序是否已启动 */ +//// logger.info("程序启动成功"); +//// }else{ +//// logger.info("程序启动失败"); +//// } +// +// } else if (os.startsWith("Linux")) {//执行restart.sh脚本 +// String path = new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath()+"/shell/restart.sh"; +// logger.info("linux下,重启DC的restart.sh脚本路径为:"+path+",开始重启DC。。。"); +// CommandExecUtil.execLinuxCmd(path); +// } else { +// throw new IOException("unknown operating system: " + os); +// } +// break; + case -1: //预留操作 + //执行系统清理 (监测结果集合、任务结果集合、重启线程池,重启通讯) + System.gc(); + service.deleteEventRecordOldInfo();//超过4天的变更就删除:NC没启动,就一直执行不成功;(或者给服务器节点发送只有交换机才有的) + logger.info("执行gc操作"); + break; + default: + logger.warn("Indeterminate mode parameter, no operation"); + return; + } + + } catch (Exception e) { + logger.error("NmsServer initialization failure",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("启动程序dao已关闭"); + } + } +} diff --git a/src/com/nms/server/thread/WritePidThread.java b/src/com/nms/server/thread/WritePidThread.java new file mode 100644 index 0000000..64f9f57 --- /dev/null +++ b/src/com/nms/server/thread/WritePidThread.java @@ -0,0 +1,68 @@ +package com.nms.server.thread; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.management.ManagementFactory; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; + +public class WritePidThread implements Runnable{ + + @Override + public void run() { +// Thread.currentThread().setName("写PID线程"); + Thread.currentThread().setName("Write the PID thread"); + Logger logger = Logger.getLogger(WritePidThread.class); + + /* 获取程序运行PID */ + String path = new File(Constants.SYSTEM_PATH).getParent()+File.separator+Constants.COMMON_TEMP_DIR+File.separator+Constants.COMMON_RUNTIME_PID_FILE; + String name = ManagementFactory.getRuntimeMXBean().getName(); + logger.info("当前程序PID:>"+(name.split("@")[0])); + + /* 判断系统类型是否写文件 */ + String os = System.getProperty("os.name"); + if(os!=null && !os.toLowerCase().startsWith("windows")){ + logger.info("非Windows系统 结束执行"); + return ; + } + + /* 获取输出文件并检查文件路径是否存在 */ + File file = new File(path); + if(!file.getParentFile().exists()){ + file.getParentFile().mkdirs(); + } + + /* 将PID写入文件 */ + FileOutputStream oStream = null; + try { + + oStream = new FileOutputStream(path,false); + + oStream.write(name.split("@")[0].getBytes()); + + oStream.flush(); + + logger.info("写PID完成:>"+file.getAbsolutePath()); + } catch (IOException e) { + logger.error("Write PID failure", e); + }finally{ + try { + if(oStream!=null) + oStream.close(); + oStream = null; + } catch (IOException e) { + logger.error("", e); + } + logger.info("线程关闭"); + } + } + public static void main(String [] args) { + new Thread(new WritePidThread()).start(); + } + public static void pl (Object obj) { + System.out.println(obj==null?null:obj.toString()); + } +} diff --git a/src/com/nms/server/thread/alarmData/AlarmDataResoveManagerThread.java b/src/com/nms/server/thread/alarmData/AlarmDataResoveManagerThread.java new file mode 100644 index 0000000..ce81c45 --- /dev/null +++ b/src/com/nms/server/thread/alarmData/AlarmDataResoveManagerThread.java @@ -0,0 +1,167 @@ +package com.nms.server.thread.alarmData; + +import java.io.File; +import java.util.Calendar; +import java.util.LinkedList; +import java.util.concurrent.Future; + +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.StringUtil; + +public class AlarmDataResoveManagerThread implements Runnable{ + private Logger logger = Logger.getLogger(AlarmDataResoveManagerThread.class); + private int testNum = 0;//尝试运行线程Constants.ALARM_DATA_RESOVE的次数 + + public void run() { +// Thread.currentThread().setName("告警数据解析管理线程"); + Thread.currentThread().setName("Alarm Data Parsing Management Threads"); + //将线程运行程序,尽可能的catch捕获异常 + try { + boolean runFlag = Common.isDbConnected();//测试数据库连接 + + //- 检查线程运行状态 + Future future = Common.getFutureMap().get(Constants.ALARM_DATA_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone() && runFlag){ //运行中 + runFlag = false; + logger.info("告警解析 运行中 不再启动"); + } + + /* + * 连续超过Constants.CHECK_WARNING_DATA_OVERRUN次数时,将数据存入硬盘中 + * 否则 正常执行数据解析线程 + * */ + if(!runFlag){ //判断并结束线程 + testNum = (testNum%Constants.CHECK_WARNING_DATA_OVERRUN); + testNum++; + if(testNum==Constants.CHECK_WARNING_DATA_OVERRUN) { + if(Constants.FLAG_WARN_DATA_SAVE_DISK_RESOVE==1) { + /* + * 连续多个周期仍未解析完成 + * 说明list里存有大量的数据,则先将当前未解析的数据列表存入硬盘, + * 然后停掉解析线程,将正在解析的数据列表中的剩余数据存入硬盘 + * */ + logger.warn("Create a database connection failure data to write to the hard disk"); + saveWarningDataToDisk(); + } + } + return; + }else{ //重置计数器 程序继续执行 + testNum = 1; + } + + + //- 非升级操作判断 + if(Common.SERVER_UN_UPGRADE_FLAG){ + + //-- 启动新解析线程 + //-- 变更存储区 + Common.changeAlarmDataFlag(); //变更数据存放集合 + + LinkedList adList = Common.getAlarmDataList(); // 获取非存放状态的数据集合 + if(adList != null && adList.size()>0){ + logger.info("告警解析 空闲中 启动"); +// future = Common.service.submit(new AlarmDataResoveThread("告警解析",adList)); + future = Common.service.submit(new AlarmDataResoveThread("Alarm Parsing",adList)); + //注册 告警线程 + Common.getFutureMap().put(Constants.ALARM_DATA_RESOVE, future); + }else{ + logger.info("告警数据为0,解析结束"); + } + } + + } catch (Exception e) { + logger.error("Running exception",e); + } + } + + /** + * 数据库是否可连接判断 + * @time Mar 7, 2013-2:22:42 PM + * @return + */ + public boolean dbTest() { + CommonDao dao = null; + try { +// if(!false)throw new Exception("强制异常"); + dao = new CommonDao(); + logger.debug("尝试获取数据库连接成功"); + return true; + } catch (Exception e) { + logger.error("Try to get a database connection failure", e); + /* 告警 */ + return false; + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + //2013-2-19 hyx :当告警数据量过大时,将数据先存放到硬盘,再定时入库(由单独的定时线程实现) + public void saveWarningDataToDisk() { + LinkedList notResolvingWarningDataList = Common.getNotResovlingWarnDataList(); // 获取未在解析的告警数据集合 + try { + + //终止正在解析告警数据的线程,并将正在解析的list里未解析的数据存入硬盘 + Future future = Common.getFutureMap().get(Constants.ALARM_DATA_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + future.cancel(true); + } + LinkedList warningDataList = Common.getAlarmDataList(); // 获取非存放状态的数据集合(正在解析的list) + saveStrToFile(warningDataList); + + //将未在解析的list存放到硬盘上 + saveStrToFile(notResolvingWarningDataList); + + + }catch(IndexOutOfBoundsException e) { + logger.error("When the alarm data is too much, the data is stored in the hard disk",e); + }catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + + } + + private void saveStrToFile(LinkedList dataList) { + String filePath = ""; + int dataSize = dataList.size();//如果不按此时的size来操作,则list里的数据可能会一直在增加,就会一直循环下去? + logger.info("告警数据过多,共"+dataSize+"条告警数据,等待存入硬盘"); + try { + for(int j=0;j0) { + String[] data= dataList.get(0); + filePath = Constants.OVERRUN_WARNING_FILE_DIR+"/"+Common.getDateDirName()+"/"+Calendar.getInstance().getTimeInMillis()+"_"+j+".csv"; + File file = new File(filePath); + String dataStr = StringUtil.linkToString(data, Constants.COMMON_DATA_SPLIT, ""); + FileUtils.writeStringToFile(file, dataStr); +// String[] msg = FileUtils.readFileToString(file).split(Constants.COMMON_DATA_SPLIT); +// System.out.println(FileUtils.readFileToString(file)); + dataList.remove(0); + logger.info("告警数据过多,已保存至"+filePath); + + } + } + + }catch(Exception e) { + logger.error("When the alarm data is too much, the data is stored in the hard disk",e); + } + } + + public static void main(String[] args) { + String [] alarms = {"1","2","3","4"}; + String [] alarms2 = {"21","2","3","4"}; + String [] alarms3 = {"31","2","3","4"}; + Common.addAlarmData(alarms); + Common.addAlarmData(alarms2); + Common.changeAlarmDataFlag(); + Common.addAlarmData(alarms3); + new AlarmDataResoveManagerThread().saveWarningDataToDisk(); + } +} diff --git a/src/com/nms/server/thread/alarmData/AlarmDataResoveThread.java b/src/com/nms/server/thread/alarmData/AlarmDataResoveThread.java new file mode 100644 index 0000000..a89c221 --- /dev/null +++ b/src/com/nms/server/thread/alarmData/AlarmDataResoveThread.java @@ -0,0 +1,211 @@ +package com.nms.server.thread.alarmData; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.CommonService; + +/** + * 告警数据解析线程 + * + * @author ZGGG3 + * + */ +public class AlarmDataResoveThread implements Callable { + private Logger logger = Logger.getLogger(AlarmDataResoveThread.class); + volatile boolean stop = false;//线程是否被取消标志 + private String name; // 自定义Thread Name + private LinkedList adList; + + public AlarmDataResoveThread(String name,LinkedList adList) { + this.name = name; + this.adList = adList; + } + + /* + * 线程操作 + * + * 依次解析urlList中仍存在的Files + * + * 实现了依次解析Files,由于后期实现多线程解析操作 + */ + public Object call() { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name); + + //-- 空数据集合 结束操作 + if(adList == null || adList.size() == 0 ){ + return null; + } + + logger.debug(" 解析数量 "+adList.size()); + SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); + CommonDao dao = null; + try { + dao = new CommonDao(); + CommonService service = new CommonService(dao); + int totalCount = adList.size(); + long sTime = System.currentTimeMillis(); + logger.debug("缓存告警数据 " + (totalCount)+ "条,解析开始"); + + //-- 遍历解析 + Iterator adIte = adList.iterator(); + while (adIte.hasNext()&& !stop) {//线程未被中断 + try { + + String [] alarms = adIte.next(); + + if(alarms!=null && alarms.length>0){ + + String dateTime = null; + if(alarms.length>6){ + dateTime = format.format(Long.parseLong(alarms[5])); + } + + logger.debug("告警数据 监测时间》"+dateTime+" 内容》"+Arrays.toString(alarms)); + service.resoveAlarms(alarms); + } + + //解析入库一条数据,从list中remove一条数据 + adIte.remove(); + }catch (InterruptedException e) { + logger.error("Alarm data parsing threads are interrupted",e); + stop = true; + } + //判断当前线程是否被中断 + if (Thread.currentThread().isInterrupted()) { + // 线程中断状态,不会改变中断状态的值 + logger.info("告警数据解析线程 被中断"); + stop = true; + } + } + + //将告警数据入库后,需要检查新入库的数据是否为无效(处理告警数据的有效性标志) + updateDetectWarningDataValid(dao); + + long eTime = System.currentTimeMillis(); + long curTime = eTime-sTime; + int m=(int)curTime/1000/60; + int s=(int)((long)(curTime/1000)%60); + int ms=(int)curTime%1000; + logger.debug((totalCount)+ "条,解析完成 耗时:"+(m)+"分 "+(s)+"秒 "+(ms)+"毫秒"); + + }catch (Exception e) { + logger.error("Running exception",e); + }finally{ +// adList.clear(); + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("数据清理 解析完成"); + } + return null; + } + + //检查入库的告警数据中是否有无效的数据,有则置为无效 + public void updateDetectWarningDataValid(CommonDao dao) throws Exception{ + CommonService service = new CommonService(dao); + Map detectSetSeqIdMap = service.getDetectSetInfo();//查询有效监测设置对应的seqid + Map detectWarnDataSeqIdMap = service.getDetectSeq(Constants.DETECTION_WARNING_TABLE_NAME);//统计 告警数据 表: 监测设置<->已有有效数据的seqid + Iterator detectWarnDateSeqIte = detectWarnDataSeqIdMap.entrySet().iterator(); + String invalidSeqIds = ""; + List sqls = new ArrayList(); + while(detectWarnDateSeqIte.hasNext()) { + invalidSeqIds = ""; + Entry en = (Entry)detectWarnDateSeqIte.next(); + String setId = (String)en.getKey(); + String seqIdsValid = detectSetSeqIdMap.get(setId);//得到监测设置当前有效的seqids:1,2,3 +// System.out.println("有效key:value="+setId+":"+seqIdsValid); + if(seqIdsValid!=null) {//如果有效的监测设置内没有此监测设置,则不予处理 + seqIdsValid = "," + seqIdsValid + ","; + String[] seqIds = (String[])en.getValue(); + for(String seqIdTmp:seqIds) { +// System.out.println("告警数据seqId="+seqIdTmp); + if(seqIdTmp!=null && !"".equals(seqIdTmp)) { + if(!seqIdsValid.contains(","+seqIdTmp+",")) {//不在有效范围内,则更新为无效 + invalidSeqIds = invalidSeqIds + seqIdTmp + ","; + } + } + } + if(invalidSeqIds.endsWith(",")) { + invalidSeqIds = invalidSeqIds.substring(0,invalidSeqIds.length()-1); + sqls.add("update "+Constants.DETECTION_WARNING_TABLE_NAME+" t set valid=0 where t.valid=1 and t.detection_set_info_id="+setId+" and t.seq_id in ("+invalidSeqIds+")"); + } + } + } + dao.dbUpdateByBatch(sqls); + + } + +/* public static void main(String[] args) { + CommonDao dao = null; + try { + dao = new CommonDao(); + new AlarmDataResoveThread("xx",null).updateDetectWarningDataValid(dao); + }catch(Exception e) { + e.printStackTrace(); + } + }*/ + + public static void main(String[] args) { + CommonDao dao = null; + try { + dao = new CommonDao(); + CommonService service = new CommonService(dao); +// boolean flag = initCheck(); + //-- CheckType TableName Map Init(监测类别数据库表名映射结构信息) + service.updateCheckTableInfo(Common.getInsertTable(), null); + //-- Table Columns Map Init(数据库表结构信息) + service.updateTableColumnsMap(Common.getTableMap(), null); + //-- LoginIp nodeip Init(seq_id-物理节点信息) + service.updateSimpleNodeByUUID(null); + //-- (ClientIP-seq_id信息) + service.updateSeqIdByNodeIp(Common.getIpSeqIdMap(),null); + //alarm Ping Init(告警数据结构信息) + service.updateAlarmInfo(Common.getAlarmInfoMap(),null); + // init loop Mission info + + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + + long l = Calendar.getInstance().getTimeInMillis(); + LinkedList adList = new LinkedList(); + adList.add("141$@$143$@$cpu$@$detection288$@$1364280094312$@$1364280094312$@$1$@$0$@$总的使用率(%)12.5%大于告警值1% 不正常;".split(Constants.COMMON_DATA_SPLIT)); + Future future = Common.service.submit(new AlarmDataResoveThread("",adList)); + try { + future.get(); + } catch (InterruptedException e) { +// TODO Auto-generated catch block + e.printStackTrace(); + } catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Common.service.shutdown(); + System.out.println(Calendar.getInstance().getTimeInMillis()-l); + } +} diff --git a/src/com/nms/server/thread/change/ChangeForActiveAlarmInfo.java b/src/com/nms/server/thread/change/ChangeForActiveAlarmInfo.java new file mode 100644 index 0000000..160a848 --- /dev/null +++ b/src/com/nms/server/thread/change/ChangeForActiveAlarmInfo.java @@ -0,0 +1,127 @@ +package com.nms.server.thread.change; + +import java.util.Iterator; +import java.util.List; +import net.sf.json.JSONObject; +import org.apache.commons.lang.StringUtils; +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.ChangeService; +import com.nms.server.service.CommonService; +import com.nms.server.thread.change.socket.ChangeSocketThread; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.DateUtil; + +/** + * 节点是否报主动告警: + * 更新其影响的所有的节点是否报主动告警(通讯不成功的,保存入库,后续再进行通信) + * @date 2014-5-26 + * @author hyx + */ +public class ChangeForActiveAlarmInfo implements Runnable{ + private final Logger logger = Logger.getLogger(ChangeForActiveAlarmInfo.class); + private String datas; + private Long recordId; + + /** + * @param datas:true:NC报主动告警;false:NC不报主动告警 + */ + public ChangeForActiveAlarmInfo(Long recordId,String datas){ + this.datas = datas; + this.recordId = recordId; + } + + public void run() { +// Thread.currentThread().setName("NC是否报主动告警 变更"); + Thread.currentThread().setName("Whether NC Reports Active Alarm Changes"); + + CommonDao dao = null; + + try { + dao = new CommonDao(); + ChangeService service = new ChangeService(dao); + + if(StringUtils.isBlank(datas)){ + logger.warn("Whether NC reports active alarm change invalid data:"+datas); + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString());//删除数据库中记录的通信信息 + } + return ; + } + + JSONObject jObject = JSONObject.fromObject(datas); + Boolean showAutoAlarm = (Boolean)jObject.get("showAutoAlarm"); + String webHandleTime = (String)jObject.get("webHandleTime"); + + +// Boolean isStartActiveAlarm = datas.equalsIgnoreCase("true") ? true : false; + + //组织数据 + logger.debug("下发NC是否报主动告警操作:"+showAutoAlarm+" web端操作时间:"+webHandleTime); + + JSONObject jsonObj = new JSONObject(); + jsonObj.put("showAutoAlarm", showAutoAlarm); + jsonObj.put("webHandleTime", webHandleTime); + this.changeActiveAlarmStartInfo(jsonObj, service); + + + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString()); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + public void changeActiveAlarmStartInfo(JSONObject jObject,CommonService service){ + + //变更范围的节点IP + List ipList = service.getAllEffectiveNodeInfo(); + + logger.debug("NC是否报主动告警 节点变更数量"+ipList.size()); + Iterator ipIte = ipList.iterator(); + while (ipIte.hasNext()) { + NodeModel ip = ipIte.next(); + //仅向0服务器节点发送变更信息:0:服务器 + if(ip!= null && ip.getNodeIp()!= null && ip.getNodeType().longValue()==0l && Common.hasIpInIpSegment(ip.getNodeIp())){ + Common.runChangeRunnable(new ChangeSocketThread(null,ip.getNodeIp(),Constants.SSL_CLIENT_PORT,SocketCMD.WEB_NOTICE_ACTIVE_ALARM_START_ALERT,jObject)); + }else{ +// downLatch.countDown(); + } + } + + } + + /** + * Agent端通知变更方法 + * @time Sep 20, 2011-2:33:37 PM + * @param ip + * @param jObject + */ +// public void sendToClient(String ip,Object jObject) { +// try { +// Common.runChangeRunnable(new ChangeSocketThread(null,ip,Constants.SSL_CLIENT_PORT,"char:updateConfig",jObject)); +// } catch (Exception e) { +// logger.debug("配置变更下发"+ip+"异常"+e.getMessage()); +// } +// } +// public static void main(String [] args) { +//// String[] datas = new String[]{"202","401"}; +// CommonDao dao = new CommonDao(); +// CommonService service = new CommonService(dao); +// SetInfo old = service.getSetInfoById(226l); +// SetInfo news = service.getSetInfoById(1l); +// OperationsUpdateBySetInfo thread = new OperationsUpdateBySetInfo(old,news); +// thread.run(); +// +// } +} diff --git a/src/com/nms/server/thread/change/ChangeForCheckType.java b/src/com/nms/server/thread/change/ChangeForCheckType.java new file mode 100644 index 0000000..2abd7e4 --- /dev/null +++ b/src/com/nms/server/thread/change/ChangeForCheckType.java @@ -0,0 +1,74 @@ +package com.nms.server.thread.change; + +import java.util.Arrays; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.ChangeService; + +/** + * 监测类别信息变更更新操作类 + * @date Dec 11, 2012 4:01:20 PM + * @author ZhangGang + * + */ +public class ChangeForCheckType implements Runnable{ + private final Logger logger = Logger.getLogger(ChangeForCheckType.class); + private String [] data = null; + private Long recordId; + + /** + * 构造方法 + * @param recordId 库表记录ID 可为空 + * @param data 变更的监测类别ID + */ + public ChangeForCheckType(Long recordId,String [] data){ + this.recordId = recordId; + this.data = data; + } + + public void run(){ +// Thread.currentThread().setName("监测类别变更"); + Thread.currentThread().setName("Monitoring Class Change"); + CommonDao dao = null; + + try { + dao = new CommonDao(); + ChangeService service = new ChangeService(dao); + if(data == null || data.length==0){ //无效数据 + logger.warn("It is not effective to monitor class change information:"+(data==null?null:Arrays.toString(data))); + /*删除记录*/ + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString()); + } + return ; + } + String idstr = data[0]; + long checkTypeId = Long.parseLong(idstr); + logger.info("更新监测类别ID "+checkTypeId); + //只更新缓存,对于表结构的改变,需要动代码 + /* 查询 监测类别名称 */ + String obj = service.getCheckTypeNameById(checkTypeId); + //重新初始化 checkType TableName Map + service.updateCheckTableInfo(Common.getInsertTable(), obj); + //重新初始化 Alarm Info Map + service.updateAlarmInfo(Common.getAlarmInfoMap(), checkTypeId); + //重新初始化 Table Columns Map + service.updateTableColumnsMap(Common.getTableMap(), obj); + + /* 删除库表中的记录 */ + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString()); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } +} diff --git a/src/com/nms/server/thread/change/ChangeForNodeGroup.java b/src/com/nms/server/thread/change/ChangeForNodeGroup.java new file mode 100644 index 0000000..c355565 --- /dev/null +++ b/src/com/nms/server/thread/change/ChangeForNodeGroup.java @@ -0,0 +1,207 @@ +package com.nms.server.thread.change; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Iterator; +import java.util.List; + +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.NodeModel; +import com.nms.server.bean.NodegroupModel; +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.ChangeService; +import com.nms.server.service.ThreadService; +import com.nms.server.thread.change.socket.ChangeSocketThread; + +/** + * 执行由节点或节点组状态的变更而变更的操作类 + * @date Dec 23, 2011 1:58:04 PM + * @author ZhangGang + * + */ +public class ChangeForNodeGroup implements Runnable{ + private final Logger logger = Logger.getLogger(ChangeForCheckType.class); + private String [] data = null; + private Long recordId; + + /** + * @param recordId 库表记录ID 可为空 + * @param data [groupId,nodeId] [节点组Id:节点ID 节点Id为空时,即为节点组信息 ] + */ + public ChangeForNodeGroup(Long recordId,String [] data){ + this.recordId = recordId; + this.data = data; + } + + @Override + public void run() { +// Thread.currentThread().setName("节点状态变更"); + Thread.currentThread().setName("Node State Change"); + CommonDao dao = null; + + try { + dao = new CommonDao(); + ChangeService service = new ChangeService(dao); + /* 有效性判断 */ + if(data == null || StringUtils.isEmpty(data[0])){ + logger.warn("Not effective node state change information:"+(data==null?null:Arrays.toString(data))); + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString()); + } + return ; + } + /* 整理变更数据 最终以节点为主 */ + Long nodeGroupId = Long.parseLong(data[0]); + NodegroupModel nodeGroup = service.getNodeGroupInfoById(nodeGroupId);//nodeGroup state 1有效 0无效 + if(nodeGroup == null ){logger.warn("No query to the end of the node group change operation groupId:"+nodeGroupId);return;} + String nodeIdStr = (data.length<=1?null:data[1]); + List nodeList = new ArrayList(); + if(StringUtils.isNotEmpty(nodeIdStr)){ //节点不为空 + Long nodeId = Long.parseLong(nodeIdStr); + NodeModel node = service.getNodeInfoById(nodeId);//node state 0有效 1无效 + if(node!= null && node.getNodeIp()!= null && Common.hasIpInIpSegment(node.getNodeIp())){ + nodeList.add(node); + } + }else{ //节点为空节点组判断 + nodeList = service.getNodeInfoByGroupId(nodeGroupId); + } + logger.debug(nodeGroup.getGroupId()+" "+nodeGroup.getGroupName()+" 影响的节点数为:"+(nodeList==null?0:nodeList.size())); + Iterator ite = nodeList.iterator(); + /* 遍历各节点 处理相关setinfo */ + while (ite.hasNext()) { + try { + NodeModel node = (NodeModel) ite.next(); + boolean isPhysical = false; + if (node == null) { + continue; + } + + //节点类型判断 + if(node.getNodeType() != null && node.getNodeType().longValue() != 0l ){ + logger.info("IP为:"+node.getNodeIp()+"的节点 其节点类型不是服务器,变更无需下发,仅需更新DC的缓存信息"); + //更新缓存中节点的信息 + service.updateSeqIdByNodeIp(Common.getIpSeqIdMap(), node.getNodeIp()); + service.updateUserPwdByNodeIp(Common.getIpUserPwdMap(),node.getNodeIp()); + service.updateSimpleNodeByUUID(node.getSeqId()); + continue; + } + + //-- 节点无效性继承自nodeGroup + logger.debug("逻辑节点 id:"+node.getNodeId()+" 自身状态:"+(node.getNodeState()==0?"有效":"无效")+" 组状态:"+(nodeGroup.getIsValid()==1?"有效":"无效")+" 最终状态:"+((new Long(0).equals(nodeGroup.getIsValid())?1l:node.getNodeState()==null?null:node.getNodeState().longValue())==0l?"有效":"无效")); + node.setNodeState(new Long(0).equals(nodeGroup.getIsValid())?1l:node.getNodeState()); + + //-- 变更节点监测信息,分布走 + //--第一步,查询状态变更的节点ID和他的Seq_id (已完成) + //--第二步,查询节点状态有效唯一的Id或者null (已完成) + //--第三步,当第一步节点有效时:比较第一步的节点id和第二步的节点Id是有相等 或者 当第一步节点无效时:null(已完成) + //--第四步,如果第三步为true,则作为物理节点处理,否则,逻辑节点处理 + List setInfoList = null; + if(service.checkUpdateNodeIsPhysicalNode(node)){ //是物理节点 + logger.debug(node.getNodeIp()+" 该节点可作为物理节点"); + isPhysical = true; + setInfoList = service.selectSetInfoList(null, null, node.getSeqId(), null, null, null, null, null,false); + }else{ //逻辑节点 + logger.debug(node.getNodeIp()+" 该节点仅为逻辑节点"); + setInfoList = service.selectSetInfoList(null, null, null, null, nodeGroupId+"", nodeGroup.getIsValid(), node.getNodeId()+"", null,false); + } + + //更新缓存中节点的信息 + service.updateSeqIdByNodeIp(Common.getIpSeqIdMap(), node.getNodeIp()); + service.updateUserPwdByNodeIp(Common.getIpUserPwdMap(),node.getNodeIp()); + service.updateSimpleNodeByUUID(node.getSeqId()); + + /** 更新其影响的监测设置的操作 + * */ + logger.debug(node.getNodeIp()+" 影响变更的监测设置数量为:"+(setInfoList==null?0:setInfoList.size())); + if(setInfoList!=null && setInfoList.size()>0){ + + for (int i = 0 ; i < setInfoList.size() ; i++) { + try { + SetInfo setInfo = setInfoList.get(i); + //非物理节点,跳过全局 + if(!isPhysical && setInfo.getViewLevel()==4l){logger.debug(node.getNodeIp()+" 逻辑节点跳过全局监测:"+setInfo.getCheckTypeName());continue;} + // 监测方式:0主动 1被动 + if("0".equals(setInfo.getCheckWay())){ //主动监测状态,有NMSServer自己处理 + logger.debug("变更主动监测 "+setInfo.getCheckTypeName()+"跳过"); +// + String runnableName = setInfo.getCheckTypeName()+"_"+setInfo.getProcessIden(); + if(!Common.isRunnableRegist(runnableName)){ + ThreadService service2 = new ThreadService(); + service2.updateThread(setInfo); + } +// //- 设置名称 +// String processIden = StringUtils.isEmpty(setInfo.getProcessIden())?"":setInfo.getProcessIden(); +// +// //-- 监测设置 线程名称 typeName_processIden +// String runnableName = setInfo.getCheckTypeName()+"_"+processIden; +// +// //根据线程类别 线程对象 +// MonitorManagerThread managerThread = (MonitorManagerThread) Common.getMonitorRunnableFromMap(runnableName); +// +// if(managerThread!= null){ +// if(0l == node.getNodeState()){ //0有效 +// managerThread.addIp(node); +// }else { // 1无效 +// managerThread.removeIp(node.getNodeIp()); +// } +// }else{ +// logger.warn("主动监测线程:"+runnableName+" 不存在"); +// } + + setInfoList.remove(setInfo); + i--; + }else{ //被动状态,监测设置发送到NMSClient端 + logger.debug("变更被动监测 "+setInfo.getCheckTypeName()); + //监测设置的 无效性 继承自失败节点的状态 + logger.debug("监测设置 id:"+setInfo.getId()+" 自身状态:"+(setInfo.getCheckState().equals("1")?"有效":"无效")+" 节点状态:"+(node.getNodeState().longValue()==0?"有效":"无效")+" 最终状态:"+((new Long(1).equals(node.getNodeState())?"0":setInfo.getCheckState()).equals("1")?"有效":"无效")); + setInfo.setCheckState(new Long(1).equals(node.getNodeState())?"0":setInfo.getCheckState()); + + } + + } catch (Exception e) { + logger.error("",e); + } + } + + //向node 下发所有被动监测监测设置信息 + if(setInfoList.size()>0){ + List alarmInfoList = service.selectAlarmInfoBySetIfoList(setInfoList); + //组织数据 + JSONObject jsonObj = new JSONObject(); + jsonObj.put("number", Calendar.getInstance().getTimeInMillis()); + jsonObj.put("setInfo", setInfoList); + jsonObj.put("alarmInfo", alarmInfoList); + Common.runChangeRunnable(new ChangeSocketThread(null,node.getNodeIp(),Constants.SSL_CLIENT_PORT,"char:updateConfig",jsonObj)); + } + } + } catch (Exception e) { + logger.error("",e); + } + } + + /** 更新其影响的其他的操作 + * initTask + * */ + //删除变更记录 + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString()); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } +} diff --git a/src/com/nms/server/thread/change/ChangeForSetInfo.java b/src/com/nms/server/thread/change/ChangeForSetInfo.java new file mode 100644 index 0000000..4fbc94f --- /dev/null +++ b/src/com/nms/server/thread/change/ChangeForSetInfo.java @@ -0,0 +1,196 @@ +package com.nms.server.thread.change; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Iterator; +import java.util.List; + +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.AlarmInfo; +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.ChangeService; +import com.nms.server.service.CommonService; +import com.nms.server.service.ThreadService; +import com.nms.server.thread.change.socket.ChangeSocketThread; + +/** + * 监测设置信息变更时 + * 通过新旧设置信息的对别,判断并更新 + * 其影响的所有的节点的监测设置 + * (通讯不成功的监测设置通讯,保存入库,后续) + * @date Dec 27, 2011 2:57:35 PM + * @author ZhangGang + */ +public class ChangeForSetInfo implements Runnable{ + private final Logger logger = Logger.getLogger(ChangeForSetInfo.class); + private String datas; + private Long recordId; + /** + * @param o SetInfo old监测设置信息 + * @param n SetInfo new监测设置信息 + */ + public ChangeForSetInfo(Long recordId,String datas){ + this.recordId = recordId; + this.datas = datas; + } + + public void run() { +// Thread.currentThread().setName("监测设置变更"); + Thread.currentThread().setName("Monitoring Settings Change"); + + JSONObject jObject = JSONObject.fromObject(datas); + SetInfo o = (SetInfo) JSONObject.toBean(jObject.getJSONObject("old"), SetInfo.class); + SetInfo n = (SetInfo) JSONObject.toBean(jObject.getJSONObject("new"), SetInfo.class); +// logger.debug("old checktypename "+o.getCheckTypeName()); +// logger.debug("new checktypename "+n.getCheckTypeName()); + CommonDao dao = null; + + try { + dao = new CommonDao(); + ChangeService service = new ChangeService(dao); + + if(n == null){ + logger.warn("Monitoring settings to change invalid data:"+jObject.toString()); + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString()); + } + return ; + } + /**old 与 new的节点组和节点的变化,影响该设置的监测变更范围 + * old 与 new的监测状态有效性的变化,影响的是监测设置是否执行 + * */ + boolean flag = true; + //-- 监测范围变更校验 + if(o==null){ //新增监测:告警设置时,0=null + flag = true; + }else{ //修改 + String oldGIds = StringUtils.isBlank(o.getNodeGroupsId())?"0":o.getNodeGroupsId(); + String newGIds = StringUtils.isBlank(n.getNodeGroupsId())?"0":n.getNodeGroupsId(); + if(oldGIds.equals(newGIds)){ //节点组相等 + String oldNIPs = StringUtils.isBlank(o.getNodeIpsId())?"": o.getNodeIpsId(); + String newNIPs = StringUtils.isBlank(n.getNodeIpsId())?"": n.getNodeIpsId(); + if(oldNIPs.equals(newNIPs)){ //节点IP相等 + flag = true; + }else{ + flag = false; + } + }else{ + flag = false; + } + } + + if(!flag){ //范围变更 + logger.debug("停用旧监测设置操作"); + o.setCheckState("0");//0:关闭监测设置 + //组织数据 + JSONObject jsonObj = new JSONObject(); + List setInfoList = new ArrayList(); + setInfoList.add(o); + jsonObj.put("number", Calendar.getInstance().getTimeInMillis()); + jsonObj.put("setInfo", setInfoList); + this.changeSetInfo(o,jsonObj, service); + }else{ //范围无变化 + } + SetInfo news = service.getSetInfoById(n.getId()); + //查询 变更的告警信息集合 + List alarmInfoList = service.getAlarmInfoListBySetInfoId(news.getId()); + //---------------------------------------------------------------------- + //NMSServer 解析更新 + //checkType TableName Map Init + service.updateCheckTableInfo(Common.getInsertTable(), news.getCheckTypeName()); + service.updateSetInfoName(Common.getSetInfoNameMape(), null); + //tableMap Init + service.updateTableColumnsMap(Common.getTableMap(), news.getCheckTypeName()); + //alarm Ping Init + service.updateAlarmInfo(Common.getAlarmInfoMap(),news.getId()); + //---------------------------------------------------------------------- + //5秒后执行新监测设置下发 + Thread.currentThread().sleep(5000); + //组织数据 + logger.debug("下发新监测设置操作"); + JSONObject jsonObj = new JSONObject(); + List setInfoList = new ArrayList(); + setInfoList.add(news); + jsonObj.put("number", Calendar.getInstance().getTimeInMillis()); + jsonObj.put("setInfo", setInfoList); + jsonObj.put("alarmInfo", alarmInfoList); + this.changeSetInfo(news,jsonObj, service); + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString()); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + public void changeSetInfo(SetInfo info,JSONObject jObject,CommonService service){ + + logger.info("监测设置"+info.getId()+" 指定CheckTypeName:"+info.getCheckTypeName()); + logger.info("监测设置"+info.getId()+" 状态:"+(StringUtils.isEmpty(info.getCheckState())?"无效操作":"1".equals(info.getCheckState())?"启动操作":"停用操作")); + logger.info("监测设置"+info.getId()+" 节点组ID:"+info.getNodeGroupsId()); + logger.info("监测设置"+info.getId()+" 指定Ip:"+info.getNodeIpsId()); + //判断是主动监测还是被动监测 + if("0".equals(info.getCheckWay())){ //主动 server + //监测重新载入 + new ThreadService().updateThread(info); + + }else { //1被动 client + + //校验 变更范围的节点IP + List ipList = service.getNodeModelListBySetInfo(info); + + //工作数量对象(ipList.size()个工作数,阻塞当前线程执行,直至所有子线程完成执行) +// CountDownLatch downLatch = new CountDownLatch(ipList.size()); + + logger.debug("监测设置"+info.getId()+" 节点变更数量"+ipList.size()); + Iterator ipIte = ipList.iterator(); + while (ipIte.hasNext()) { + NodeModel ip = ipIte.next(); + //仅向0服务器节点发送变更信息:0:服务器 + if(ip!= null && ip.getNodeIp()!= null && ip.getNodeType().longValue()==0l && Common.hasIpInIpSegment(ip.getNodeIp())){ + Common.runChangeRunnable(new ChangeSocketThread(null,ip.getNodeIp(),Constants.SSL_CLIENT_PORT,"char:updateConfig",jObject)); + }else{ +// downLatch.countDown(); + } + } + } + + } + + /** + * Agent端通知变更方法 + * @time Sep 20, 2011-2:33:37 PM + * @param ip + * @param jObject + */ + public void sendToClient(String ip,Object jObject) { + try { + Common.runChangeRunnable(new ChangeSocketThread(null,ip,Constants.SSL_CLIENT_PORT,"char:updateConfig",jObject)); + } catch (Exception e) { + logger.debug("配置变更下发"+ip+"异常"+e.getMessage()); + } + } +// public static void main(String [] args) { +//// String[] datas = new String[]{"202","401"}; +// CommonDao dao = new CommonDao(); +// CommonService service = new CommonService(dao); +// SetInfo old = service.getSetInfoById(226l); +// SetInfo news = service.getSetInfoById(1l); +// OperationsUpdateBySetInfo thread = new OperationsUpdateBySetInfo(old,news); +// thread.run(); +// +// } +} diff --git a/src/com/nms/server/thread/change/ChangeManagerThread.java b/src/com/nms/server/thread/change/ChangeManagerThread.java new file mode 100644 index 0000000..4944605 --- /dev/null +++ b/src/com/nms/server/thread/change/ChangeManagerThread.java @@ -0,0 +1,45 @@ +package com.nms.server.thread.change; + +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; + + +/** + * 周期性检查数据库,将检索变更通讯并下发 + * @date Mar 31, 2012 10:03:04 AM + * @author ZhangGang + * + */ +public class ChangeManagerThread implements Runnable{ + Logger logger = Logger.getLogger(ChangeManagerThread.class); + + @Override + public void run() { + //将线程运行程序,尽可能的catch捕获异常 + try { +// Thread.currentThread().setName("变更操作管理线程"); + Thread.currentThread().setName("Changing The Operation Management Thread"); + + Future future = Common.getFutureMap().get(Constants.CHANGE_OPERATIONS); + + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + logger.info("变更操作线程 运行中 不再启动新线程"); + }else{ + //为空或空闲中 + logger.info("变更操作线程 空闲中 启动新线程"); +// future = Common.service.submit(new ChangeThread("变更操作线程")); + future = Common.service.submit(new ChangeThread("Changing The Operating Thread")); + //注册 + Common.getFutureMap().put(Constants.CHANGE_OPERATIONS, future); + } + } catch (Exception e) { + logger.error("",e); + } + } + + +} diff --git a/src/com/nms/server/thread/change/ChangePluginScriptFile.java b/src/com/nms/server/thread/change/ChangePluginScriptFile.java new file mode 100644 index 0000000..ad9317a --- /dev/null +++ b/src/com/nms/server/thread/change/ChangePluginScriptFile.java @@ -0,0 +1,534 @@ +package com.nms.server.thread.change; + +import static com.nms.server.common.Constants.WEB_SOCKET_IP; +import static com.nms.server.common.Constants.WEB_SOCKET_PORT; +import static com.nms.server.thread.socket.SocketCMD.DOWNLOAD_PLUGIN_SCRIPT; +import static com.nms.server.thread.socket.SocketCMD.SEND_PLUGIN_SCRIPT_FILE; + +import java.io.File; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; + +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.google.common.collect.Sets; +import com.nms.server.bean.EventRecordLibrary; +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.ChangeService; +import com.nms.server.service.CommonService; +import com.nms.server.util.FileUtils; +import com.nms.server.util.StringUtil; +import com.nms.server.util.socket.SSLSocketCallable; +import com.nms.server.util.socket.SocketUtils; + +/** + * 下发第三方监测脚本文件 + */ +public class ChangePluginScriptFile implements Runnable { + private static final Logger logger = Logger.getLogger(ChangePluginScriptFile.class); + + /* + * 脚本文件临时存储目录 + */ + private File pluginScriptDir; + + /* + * 接收脚本文件的NC节点列表 + */ + private Set ncNodesIpStrSet; + + /** + * 向DC管理的所有有效服务器节点发送脚本文件 + * + * @param dpluginScriptDir 脚本文件临时存储目录 + */ + public ChangePluginScriptFile(File pluginScriptDir) { + this.pluginScriptDir = pluginScriptDir; + } + + /** + * 向指定的NC节点发送脚本文件 + * + * @param pluginScriptDir 脚本文件临时存储目录 + * @param ncNodesIpStrSet 待接收脚本的NC节点列表 + */ + public ChangePluginScriptFile(File pluginScriptDir, Set ncNodesIpStrSet) { + this.pluginScriptDir = pluginScriptDir; + this.ncNodesIpStrSet = ncNodesIpStrSet; + } + + + /* + * 用于统计向特定节点发送的脚本文件 + */ + private Map> sendRecordList = new HashMap>(); + + /* + * 统计文件剩余发送次数(用于清理DC临时脚本文件) + */ + private Map fileSendLastCount = new HashMap(); + + + @SuppressWarnings("unchecked") + public void run() { + CommonDao dao = null; + + try { + dao = new CommonDao(); + ChangeService changeService = new ChangeService(dao); + +// Thread.currentThread().setName("发送监测脚本文件"); + Thread.currentThread().setName("Send A Monitoring Script File"); + + Collection files = FileUtils.listFiles(pluginScriptDir, null, false); + + if (ncNodesIpStrSet == null) { + // 未指定接收的NC节点时,根据检测设置确定待接收脚本的NC节点 + Map setInfoMap = new HashMap(); + List setInfos = changeService.getAllSetInfo(1, 1); + for (SetInfo setInfo : setInfos) { + if("2".equals(setInfo.getIsControlStart())) { + setInfoMap.put(setInfo.getProcessIden(), setInfo); + } + } + + for (File pluginFile : files) { + String processIden = pluginFile.getName().replaceAll("\\w+_(\\w+)\\.\\w+", "$1"); + SetInfo setInfo = setInfoMap.get(processIden); + + // 校验 变更范围的节点IP + List ipNodeModelList = changeService.getNodeModelListBySetInfo(setInfo); + for (NodeModel ip : ipNodeModelList) { + // 仅向0服务器节点发送变更信息:0:服务器 + if (ip != null && ip.getNodeIp() != null && ip.getNodeType().longValue() == 0l + && Common.hasIpInIpSegment(ip.getNodeIp())) { + addToSendRecordList(ip.getNodeIp(), pluginFile); // + addTofileSendLastCount(pluginFile); // + } + } + } + ncNodesIpStrSet = sendRecordList.keySet(); + + } else { + // 已指定待接收脚本的NC节点 + for (File pluginFile : files) { + for (String ip : ncNodesIpStrSet) { + addToSendRecordList(ip, pluginFile); // 统计向特定节点发送的脚本文件 + addTofileSendLastCount(pluginFile); // 统计文件剩余发送次数 + } + } + } + + for (String ip : ncNodesIpStrSet) { + Common.runChangeRunnable(new SendPluginScriptFile(ip, sendRecordList.get(ip))); + } + + // 延时清理脚本 + new Thread(new DeletePluginScriptDirAfterSending()).start(); + + } catch (Exception e) { + logger.error("", e); + } finally { + if (dao != null) { + dao.close(); + dao = null; + } + } + } + + private void addTofileSendLastCount(File pluginFile) { + Integer count = fileSendLastCount.get(pluginFile.getName()); + count = (count != null) ? count : 0; + fileSendLastCount.put(pluginFile.getName(), count + 1); + } + + private void addToSendRecordList(String nodeIp, File pluginFile) { + if (sendRecordList.containsKey(nodeIp)) { + sendRecordList.get(nodeIp).add(pluginFile); + } else { + List files = new ArrayList(); + files.add(pluginFile); + sendRecordList.put(nodeIp, files); + } + } + + protected class SendPluginScriptFile extends SocketUtils implements Callable { + private String cmd; + private List fileList; + + public SendPluginScriptFile(String ip, List fileList) { + super(ip, Constants.SSL_CLIENT_PORT); + this.cmd = SEND_PLUGIN_SCRIPT_FILE; + this.fileList = fileList; + } + + @Override + public Object call() throws Exception { +// Thread.currentThread().setName("发送文件 To:>" + ip); + Thread.currentThread().setName("Send File To:>" + ip); + CommonDao dao = null; + try { + if(fileList == null || fileList.size()<1){ + logger.info("没有脚本文件需要发送"); + return null; + } + createClientSocket(); + this.sendMessage(cmd); + logger.debug("cmd:> " + cmd + " 发送命令结果>> " + this.receiveMessage()); + StringBuffer sb = new StringBuffer(); + for (File file : fileList) { + sb.append(",").append(file.getName()); + } + this.sendMessage(sb.substring(1)); + this.receiveMessage(); + this.bpSendFileByBath(fileList, pluginScriptDir.getCanonicalPath()); + logger.debug("cmd:> " + cmd + " 发送脚本文件>> " + this.receiveMessage()); + + return true; + + } catch (Exception e) { + String errorInfo = "Target communication:>" + ip + " create failure:" + e.getMessage(); + logger.error(errorInfo, e); + + dao = new CommonDao(); + ChangeService changeService = new ChangeService(dao); + Map recordContent = new HashMap(); + HashSet pluginFileNames = new HashSet(); + for (File pluginFile : fileList) { + pluginFileNames.add(pluginFile.getName()); + } + String content = StringUtils.join(pluginFileNames.iterator(), ","); + recordContent.put("scriptNames", content); + + changeService.saveEventRecordLibrary(cmd, Common.getIpSeqIdMap().get(ip) + "" + , "S2C", JSONObject.fromObject(recordContent).toString()); + + return false; + + } finally { + close(); // close socket + if(dao != null) { + dao.close(); + dao = null; + } + + // 文件剩余发送次数为0时,由DeletePluginScriptDirAfterSending线程清理脚本 + for (File file : fileList) { + fileSendLastCount.put(file.getName(), fileSendLastCount.get(file.getName()) - 1); + } + } + } + } + + /** + * NC初始化时发送脚本文件 + * + * @param ip 接收脚本的NC节点 + */ + public static void sendPluginFileWhenNcInit(final String ip) { + ChangePluginScriptFile task = null; + File tempPluginDir = ChangePluginScriptFile.getNewTempPluginDirectory(); + + CommonDao dao = null; + List prefixNameList = new ArrayList(); + try { + Set ipList = new HashSet(1); + ipList.add(ip); + task = new ChangePluginScriptFile(tempPluginDir, ipList); + + dao = new CommonDao(); + CommonService service = new CommonService(dao); + List setInfoList = service.selectSetInfoList(1l, null, Common.getIpSeqIdMap().get(ip), null, null, 1l, null, 0l, true); + for (SetInfo setInfo : setInfoList) { + if("2".equals(setInfo.getIsControlStart())) { + String prefixName = setInfo.getCheckTypeName() + "_" + setInfo.getProcessIden() + "."; + prefixNameList.add(prefixName); + } + } + } catch (SQLException e) { + logger.error(e.getMessage(), e); + } finally { + if (dao != null) { + dao.close(); + dao = null; + } + } + + boolean bool = task.waitForGettingScriptFromWeb(tempPluginDir, prefixNameList); + if (bool) { + Common.service.execute(task); + + } else { // 向Web请求脚本失败,记录eventRecordLibrary + try { + dao = new CommonDao(); + ChangeService changeService = new ChangeService(dao); + Map recordContent = new HashMap(); + String content = StringUtils.join(prefixNameList.iterator(), ","); + recordContent.put("scriptNames", content); + changeService.saveEventRecordLibrary(SEND_PLUGIN_SCRIPT_FILE, Common.getIpSeqIdMap().get(ip) + "", + "S2C", JSONObject.fromObject(recordContent).toString()); + + } catch (SQLException e) { + logger.error(e.getMessage(), e); + } finally { + if (dao != null) { + dao.close(); + dao = null; + } + } + } + } + + /** + * 解析失败暂存信息,重新发送脚本文件
+ * + * @param mrlList + */ + public static void sendPluginFileBaseOnEventRecord(List recordList) { + if(recordList == null || recordList.isEmpty()) { + return; + } + + // W2S [脚本名前缀,...] + Set w2sDetecSetInfos = new HashSet(); + + // S2C key:NC地址, value:[脚本名前缀,...] + Map> s2cDetecSetInfos = new HashMap>(); + + CommonDao dao = null; + try { + dao = new CommonDao(); + ChangeService changeService = new ChangeService(dao); + + for (EventRecordLibrary record : recordList) { + if (SEND_PLUGIN_SCRIPT_FILE.equalsIgnoreCase(record.getRecordCommand())) { // 脚本下发 + Map map = StringUtil.getMapFromJsonObjStr(record.getRecordContent()); + String recordType = record.getRecordType(); + String scriptNames = (String) map.get("scriptNames"); + if("W2S".equals(recordType)) { + for (String scriptName : scriptNames.split(",")) { + w2sDetecSetInfos.add(scriptName); + } + + } else if("S2C".equals(recordType)) { + for (String scriptName : scriptNames.split(",")) { + String ip = Common.getNodeIpByUUID(record.getSeqId()); + Set scriptNameSet = s2cDetecSetInfos.get(ip); + if(scriptNameSet == null) { + scriptNameSet = new HashSet(); + } + scriptNameSet.add(scriptName); + s2cDetecSetInfos.put(ip, scriptNameSet); + } + } + } + + changeService.deleteEventRecordByIds(record.getId() + ""); + } + + } catch (SQLException e) { + logger.error("Delete EventRecordLibrary", e); + } + + + // W2S, 向DC管理的所有有效节点发送脚本文件 + if(!w2sDetecSetInfos.isEmpty()) { + File tempPluginDir = ChangePluginScriptFile.getNewTempPluginDirectory(); + ChangePluginScriptFile task = new ChangePluginScriptFile(tempPluginDir); + boolean bool = task.waitForGettingScriptFromWeb(tempPluginDir, w2sDetecSetInfos); + if(bool) { + Common.service.execute(task); + + } else { // 向Web请求脚本失败,记录eventRecordLibrary + try { + dao = new CommonDao(); + ChangeService changeService = new ChangeService(dao); + Map recordContent = new HashMap(); + String content = StringUtils.join(w2sDetecSetInfos.iterator(), ","); + recordContent.put("scriptNames", content); + changeService.saveEventRecordLibrary(SEND_PLUGIN_SCRIPT_FILE, "", + "W2S", JSONObject.fromObject(recordContent).toString()); + + } catch (SQLException e) { + logger.error(e.getMessage(), e); + } finally { + if (dao != null) { + dao.close(); + dao = null; + } + } + } + } + + // S2C, 向指定节点发送脚本文件 + Iterator>> it = s2cDetecSetInfos.entrySet().iterator(); + while(it.hasNext()) { + Entry> entry = it.next(); + Set recvIpSet = Sets.newHashSet(entry.getKey()); + File tempPluginDir = ChangePluginScriptFile.getNewTempPluginDirectory(); + ChangePluginScriptFile task = new ChangePluginScriptFile(tempPluginDir, recvIpSet); + Set prefixNames = entry.getValue(); + boolean bool = task.waitForGettingScriptFromWeb(tempPluginDir, prefixNames); + bool = false; + if(bool) { + Common.service.execute(task); + + } else { // 向Web请求脚本失败,记录eventRecordLibrary + try { + dao = new CommonDao(); + ChangeService changeService = new ChangeService(dao); + Map recordContent = new HashMap(); + String content = StringUtils.join(prefixNames.iterator(), ","); + recordContent.put("scriptNames", content); + String ips = Common.getIpSeqIdMap().get(recvIpSet.iterator().next())+""; + changeService.saveEventRecordLibrary(SEND_PLUGIN_SCRIPT_FILE, ips, + "S2C", JSONObject.fromObject(recordContent).toString()); + + } catch (SQLException e) { + logger.error(e.getMessage(), e); + } finally { + if (dao != null) { + dao.close(); + dao = null; + } + } + } + } + } + + /** + * 根据脚本名称从Web获取脚本文件,并等待脚本获取完成 + * + * @param dir + * @param prefixNames + * @return + */ + protected Boolean waitForGettingScriptFromWeb(File dir, Collection prefixNames) { + GetScriptFileFromWeb task = null; + task = this.new GetScriptFileFromWeb(dir, prefixNames); + FutureTask future = new FutureTask(task); + new Thread(future).start(); + try { + Object result = future.get(); + + if(result == null) { + logger.debug("从Web服务器获取脚本失败,脚本列表:" + prefixNames); + return false; + } + return (Boolean) result; + + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + return false; + } + + protected class GetScriptFileFromWeb extends SSLSocketCallable { + + Logger logger = Logger.getLogger(GetScriptFileFromWeb.class); + + private File tempPluginDir; // 脚本存储目录 + private String prefixNames; // 脚本名称列表(逗号分隔) + + public GetScriptFileFromWeb(File tempPluginDir, Collection prefixNames) { + super(WEB_SOCKET_IP, WEB_SOCKET_PORT); + this.tempPluginDir = tempPluginDir; + if(prefixNames != null && prefixNames.size() > 0){ + StringBuilder sb = new StringBuilder(); + for(String s : prefixNames){ + if(StringUtils.isNotBlank(s)){ + sb.append(","); + sb.append(s); + } + } + if(sb.length() >0){ + sb.deleteCharAt(0); + this.prefixNames = sb.toString(); + } + } + + } + + @Override + protected Object toDo() throws Exception { +// Thread.currentThread().setName("下载第三方监测脚本文件"); + Thread.currentThread().setName("Download Third Party Monitoring Script Files"); + if(StringUtils.isBlank(prefixNames)){ + return true; + } + this.sendMessage(DOWNLOAD_PLUGIN_SCRIPT); + this.receiveMessage(); + this.sendMessage(prefixNames); + String fileName = this.receiveMessage(); + + // 重复请求脚本,Web无对应脚本时返回为空 + if(FAIL.equalsIgnoreCase(fileName) || StringUtils.isBlank(fileName)) { + logger.error("The Web server does not monitor " + prefixNames + "the corresponding script file!"); + return false; + } else { + this.sendMessage(SUCCESS); + this.bpReceiveFileByBath(tempPluginDir.getCanonicalPath()); + logger.info("已接收脚本文件:" + Arrays.toString(tempPluginDir.list())); + return true; + } + } + } + + protected class DeletePluginScriptDirAfterSending implements Runnable { + @Override + public void run() { + while (!fileSendLastCount.isEmpty()) { + Iterator> iterator = fileSendLastCount.entrySet().iterator(); + while (iterator.hasNext()) { + Entry entry = iterator.next(); + if(entry.getValue() == 0) { + iterator.remove(); + } + } + try { + TimeUnit.SECONDS.sleep(2L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + FileUtils.deleteAllFiles(pluginScriptDir, true); + } + } + + /** + * web向DC下发脚本或DC向web请求脚本过程中,每次都创建新的临时目录
+ * (DC向NC下发脚本完成后将删除临时目录) + * + * @return + */ + public static File getNewTempPluginDirectory() { + File tempPluginDir = new File(Constants.PLUGIN_SCRIPT_FILE_DIR, System.currentTimeMillis()+""); + if (!tempPluginDir.exists()) { + tempPluginDir.mkdirs(); + } + return tempPluginDir; + } +} \ No newline at end of file diff --git a/src/com/nms/server/thread/change/ChangeThread.java b/src/com/nms/server/thread/change/ChangeThread.java new file mode 100644 index 0000000..57960fc --- /dev/null +++ b/src/com/nms/server/thread/change/ChangeThread.java @@ -0,0 +1,173 @@ +package com.nms.server.thread.change; + +import static com.nms.server.thread.socket.SocketCMD.SEND_PLUGIN_SCRIPT_FILE; + +import java.util.List; +import java.util.concurrent.Callable; + +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.EventRecordLibrary; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.ChangeService; +import com.nms.server.thread.ResetServerThread; +import com.nms.server.thread.change.socket.ChangeSocketThread; +import com.nms.server.thread.mission.LoadNewMissionThread; + +public class ChangeThread implements Callable{ + Logger logger = Logger.getLogger(ChangeThread.class); + ChangeService changeService = null; + String name ; + /** + * 监测设置信息变更请求命令(设置告警修改) + */ + private String WEB_NOTICE_SET_INFO_ALERT = "char:setInfoAlert"; + /** + * 节点或节点组状态变更请求命令:节点组id:节点id(缺省为0) + */ + private String WEB_NOTICE_NODE_AND_GROUP_STATE_ALERT = "char:nodeAndGroupAlert"; + /** + * 监测类别信息变更包括字段的变更(表结构改变)请求命令 + */ + private String WEB_NOTICE_CHECK_TYPE_ALERT = "char:checkTypeIdAlert"; + /** + * 任务信息变更请求命令 + */ + private String WEB_NOTICE_MISSION_INFO_ALERT = "char:missionInfoAlert"; + + /** + * DC控制信息 + */ + private String WEB_NOTICE_DATACONTROLLER = "char:Datacontroller"; + + /** + * 节点是否报主动告警变更请求命令 + */ + private String WEB_NOTICE_ACTIVE_ALARM_START_ALERT = "char:isActiveAlarmStart"; + + public ChangeThread (String name){ + this.name = name; + } + + @Override + public Object call() throws Exception { + Thread.currentThread().setName(name); + CommonDao dao = null; + try { + dao = new CommonDao(); + final ChangeService service = new ChangeService(dao); + List mrlList = service.getNewEventRecordList(); + + //工作数量对象(ipList.size()个工作数,阻塞当前线程执行,直至所有子线程完成执行) +// CountDownLatch downLatch = new CountDownLatch(mrlList.size()); + + // 根据发送失败暂存信息,重新发送脚本文件 + ChangePluginScriptFile.sendPluginFileBaseOnEventRecord(mrlList); + + StringBuffer ids = new StringBuffer(); + + if(mrlList != null && mrlList.size()>0){ + for (final EventRecordLibrary record : mrlList) { + boolean delFlag = false; + try { + + //-- WEB TO SERVER 通讯,当DC查询了W2S的记录后,就代表web和DC通信成功了,剩下的就是DC和NC的通信了 + if("W2S".equals(record.getRecordType())){ + if(WEB_NOTICE_SET_INFO_ALERT.equals(record.getRecordCommand())){ + Common.service.submit(new ChangeForSetInfo(record.getId(),record.getRecordContent())); + }else if(WEB_NOTICE_NODE_AND_GROUP_STATE_ALERT.equals(record.getRecordCommand())){ + + String datas = record.getRecordContent(); + String [] data = datas.split(":"); + Common.service.submit(new ChangeForNodeGroup(record.getId(),data)); + + }else if(WEB_NOTICE_CHECK_TYPE_ALERT.equals(record.getRecordCommand())){ + + String datas = record.getRecordContent(); + String [] data = datas.split(":"); + Common.service.submit(new ChangeForCheckType(record.getId(),data)); + }else if(WEB_NOTICE_MISSION_INFO_ALERT.equals(record.getRecordCommand())){//新增任务 + + String datas = record.getRecordContent(); + String [] data = datas.split(":"); + if(StringUtils.isNotEmpty(data[0])){ + Common.service.submit(new LoadNewMissionThread(Long.parseLong(data[0]) )); + } + delFlag = true; + }else if(WEB_NOTICE_DATACONTROLLER.equals(record.getRecordCommand())){ + + String datas = record.getRecordContent(); + if(StringUtils.isNotEmpty(datas)){ + Common.service.submit(new ResetServerThread(Integer.parseInt(datas) )); + } + delFlag = true; + }else if(WEB_NOTICE_ACTIVE_ALARM_START_ALERT.equals(record.getRecordCommand())){//NC是否报主动告警 + //5秒后执行下发:否则多次更改状态时,可能不一定按设置的先后时间来 + Thread.sleep(5000); + String datas = record.getRecordContent(); + if(StringUtils.isNotEmpty(datas)){ + Common.service.submit(new ChangeForActiveAlarmInfo(record.getId(),record.getRecordContent())); + } + delFlag = true; + } else { + + // -- 无法判断直接删除 + delFlag = true; + } + } + + //-- SERVER TO CLIENT 通讯 针对于单节点的通信 监测设置的变更 节点状态的有效无效 + else if("S2C".equals(record.getRecordType())){ + if(SEND_PLUGIN_SCRIPT_FILE.equals(record.getRecordCommand())) { + // 第三方检测脚本下发通过ChangePluginScriptFile.sendPluginFileBaseOnEventRecord实现 + continue; + + } else { + if(record.getSeqId()!=null && record.getSeqId().longValue()!=0l){ + String ip = Common.getNodeIpByUUID(record.getSeqId()); + Common.runChangeRunnable( + new ChangeSocketThread(record.getId(),ip, + Constants.SSL_CLIENT_PORT, + record.getRecordCommand(), + JSONObject.fromObject(record.getRecordContent())) + ); + } + } + } + + } catch (NumberFormatException e) { + logger.error("", e); + } + if(delFlag)ids.append(",'"+record.getId()+"'"); + } + + if(ids.length() > 0){ + ids.deleteCharAt(0); + logger.debug("待删除的 recordid" + ids); + service.deleteEventRecordByIds(ids.toString()); + } + } + } catch (Exception e) { + logger.error("Change operation exception", e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + + return null; + } + +/* public static void p(Object ss){ + System.out.print(ss==null?null:ss.toString()); + } + public static void pl(Object ss){ + System.out.println(ss==null?null:ss.toString()); + }*/ +} diff --git a/src/com/nms/server/thread/change/socket/ChangeSocketThread.java b/src/com/nms/server/thread/change/socket/ChangeSocketThread.java new file mode 100644 index 0000000..0a3ef63 --- /dev/null +++ b/src/com/nms/server/thread/change/socket/ChangeSocketThread.java @@ -0,0 +1,108 @@ +package com.nms.server.thread.change.socket; + +import java.util.concurrent.Callable; +import net.sf.json.JSONObject; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import com.nms.server.bean.EventRecordLibrary; +import com.nms.server.common.Common; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.ChangeService; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.DateUtil; +import com.nms.server.util.socket.SocketUtils; + + + +/** + * 变更通讯线程 + * 发送变更信息通讯 + * 线程返回值: + * -3 命令参数为空 + * -2 通讯创建失败 + * -1 变更信息保存入库 + * 0 通讯成功并删除变更信息 + */ + +public class ChangeSocketThread extends SocketUtils implements Callable { + + //日志组件对象 + private static Logger logger = Logger.getLogger(ChangeSocketThread.class); + + private String cmd; + private Object obj; + private Long recordId; + public ChangeSocketThread(Long recordId,String ip, int port,String cmd,Object data) { + super(ip, port); + this.recordId = recordId; + this.cmd = cmd; + this.obj = data; + } + + @Override + public Object call(){ +// Thread.currentThread().setName("变更通讯 To:>"+ip); + Thread.currentThread().setName("Change Communication To:>"+ip); + try { + + createClientSocket(); + + if(StringUtils.isEmpty(cmd)){ + return -3; + } + + if(cmd.contains("char")){ + this.sendMessage(cmd); + logger.debug("cmd:> "+cmd+" 发送命令结果>> "+this.receiveMessage()); + this.sendMessage(((JSONObject)obj).toString()); + logger.debug("cmd:> "+cmd+" 发送数据结果>> "+this.receiveMessage()); + } + + CommonDao dao = null; + try { + dao = new CommonDao(); + ChangeService service = new ChangeService(dao); + if(recordId!=null){ + service.deleteEventRecordByIds(recordId.toString()); + } + } catch (Exception e) { + logger.error("Change record save failure", e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + return 0; + } catch (Exception e) { + String errorInfo = "Target communication:>"+ip+" create failure:"+e.getMessage(); + logger.error(errorInfo); + //Common.addErrorInfo(EmailTypeConstants.ERROR_SOCKET, ip, new Date(), (long)EmailTypeConstants.STATE_FAIL, errorInfo); + if(recordId==null){ + CommonDao dao = null; + try { + dao = new CommonDao(); + ChangeService service = new ChangeService(dao); + if(SocketCMD.WEB_NOTICE_ACTIVE_ALARM_START_ALERT.equalsIgnoreCase(cmd)) {//对于NC节点是否报主动告警的变更保存,针对发给同一客户端(NC)的只保存一条,避免多次变更后,导致NC是否主动报警不是最新状态(因为采用的是多线程,所以不一定按时间先后) + EventRecordLibrary event = service.selectEventRecordInfo("S2C", cmd, Common.getIpSeqIdMap().get(ip)+""); + if (event!=null) {//更新 + service.updateEventRecordLibrary(event,DateUtil.getCurrentTime(),((JSONObject)obj).toString()); + }else {//新增 + service.saveEventRecordLibrary(cmd, Common.getIpSeqIdMap().get(ip)+"", "S2C", ((JSONObject)obj).toString()); + } + }else { + service.saveEventRecordLibrary(cmd, Common.getIpSeqIdMap().get(ip)+"", "S2C", ((JSONObject)obj).toString()); + } + } catch (Exception e1) { + logger.error("Change record save failure", e1); + return -1; + }finally{ + if(dao!=null){dao.close();dao = null;} + } + } + return -2; + }finally{ + close(); + } + } +} \ No newline at end of file diff --git a/src/com/nms/server/thread/dataCollect/DataCollectManagerThread.java b/src/com/nms/server/thread/dataCollect/DataCollectManagerThread.java new file mode 100644 index 0000000..f1ff0ce --- /dev/null +++ b/src/com/nms/server/thread/dataCollect/DataCollectManagerThread.java @@ -0,0 +1,274 @@ +package com.nms.server.thread.dataCollect; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import com.nms.server.bean.DetectInfo; +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.thread.monitor.util.MonitorUtil; +import com.nms.server.util.BoneCPPool; +import com.nms.server.util.ExceptionPrintUtils; + +//监测数据主动收集管理线程 +public class DataCollectManagerThread implements Runnable{ + private Logger logger = Logger.getLogger(DataCollectManagerThread.class); + private long startTime = Calendar.getInstance().getTimeInMillis(); + + public void run() { +// Thread.currentThread().setName("数据(监测数据、任务结果、回传文件)收集管理线程"); + Thread.currentThread().setName("Data (Monitoring data, Task results, Return files) To Collect Management Threads"); + CommonDao dao = null; + try { + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + //- 非升级操作判断 + if(Common.SERVER_UN_UPGRADE_FLAG){//---起什么作用? + //循环向自己管辖的Client端,收集数据 + List ipList = new LinkedList(); + ipList = service.getAllComputerIp();//当前DC管理范围内+有效节点+有效节点组+服务器节点 + int j=0; + String key=""; + /* + * 2013-7-29 hyx + * 获得所有握手监测的节点信息,若数据收集的节点在握手监测节点范围内,则收集数据的同时进行握手监测(传递true),确认一个,从握手监测中删除一个节点,最后未握手的节点则不在数据收集范围内(这种情况不应该存在) + */ + Map nodeMap = getNmsclientNodeList(service);//ip:setinfo + //20160901 hyx 增加此代码的原因是:数据收集管理线程运行一段时间后,会出现获取数据库连接一直阻塞的情况,而此时其它线程是可以正常获取数据库连接的,一个原因可能是之前没有dao=null;另一个原因可能是,之后的数据收集及超时操作,可能由于各种原因,如异常节点较多,导致连接最后没能正常关闭,所以在连接不使用的时候,尽早关闭 + try { + if(dao!=null){ + dao.close(); + dao=null; + } + } catch (Exception e) + { + logger.error("Data collection management thread early closing database connection exception", e); + } + + boolean nmsclientFlag = false;//节点是否进行握手监测的标识 + Map> ipFutureMap = new HashMap>(); + int ipNum = ipList.size(); + for(int i=0;i future = Common.getFutureMap().get(key); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + logger.info("对"+ip+"的数据(监测数据、任务结果、回传文件)收集执行线程 运行中 不再启动新收集线程"); + continue; + } + SetInfo setInfo = nodeMap.get(ip); + if(nodeMap.containsKey(ip)) {//当前节点需要进行握手监测 + nmsclientFlag = true; +// future = Common.service.submit(new DataCollectThread("数据收集执行线程:>"+ip,ip,Constants.SSL_CLIENT_PORT,nmsclientFlag,setInfo,startTime)); + future = Common.service.submit(new DataCollectThread("Data Collection Execution Thread:>"+ip,ip,Constants.SSL_CLIENT_PORT,nmsclientFlag,setInfo,startTime)); + ipFutureMap.put(ip, future); + }else {//当前节点不需要进行握手监测 + nmsclientFlag = false; + future = Common.service.submit(new DataCollectThread("Data Collection Execution Thread(No need shakehand):>"+ip,ip,Constants.SSL_CLIENT_PORT,nmsclientFlag,setInfo,startTime)); + } + + //注册 + Common.getFutureMap().put(key, future); + j++; + } + } + logger.info("*******************************************共"+ipNum+"个节点(实际收集"+(ipNum-Common.noDetectDataNodeList.size())+"个),数据(监测数据、任务结果、回传文件)收集执行线程启动个数:"+j); + + //握手监测信息 + Iterator ite = ipFutureMap.entrySet().iterator(); + + Thread.sleep(Constants.DATA_COLLECT_DAILY*1000);//默认2分钟(socket超时20多秒,ping失败13秒,ssh失败20多秒,共计1分钟左右,再加上数据收集的时间,2分钟满足) + for(;ite.hasNext();) { + Entry en = (Entry)ite.next(); + String ip = (String)en.getKey(); + if(Common.noDetectDataNodeList.contains(ip)){ + continue; + } + SetInfo setInfo = nodeMap.get(ip); + Object rlt = null; + Future futureTmp = (Future)en.getValue(); + try { + logger.info("对节点"+ip+"的数据收集及握手监测 等待中。。。"); + rlt = futureTmp.get(2, TimeUnit.MILLISECONDS);//如果在执行时间内没有获取结果,则直接返回null + + } catch (TimeoutException e) { + logger.debug("对 "+ip+" 的数据收集超时("+Constants.DATA_COLLECT_DAILY+"秒)"); + String keyTmp = Constants.DATA_COLLECT+":"+ip; + Common.cancelRunnableAtOnce(keyTmp);//此处很重要,需要取消线程的执行,如果取消失败,或者不及时,会生成两条握手监测数据(收集线程一条,管理线程一条) + }finally{ + DetectInfo detectInfo = rlt!=null?((DetectInfo)rlt):(new DetectInfo()); + detectInfo.setTestTimes(1);//尝试次数 + detectInfo.setCheckTime(System.currentTimeMillis());//监测时间 + try{ + /** + * -- 监测数据描述信息非空(web界面的状态信息),才生成监测数据: + * 1、监测数据为空可能是收集线程超时导致ping或者ssh未执行完毕,未生成描述信息 + * 2、此时web端进行超周期设置时,过滤掉异常的节点,异常节点的异常信息还是采用之前周期的-此功能 20160906 web端还未实现 + */ + if(StringUtils.isBlank(detectInfo.getDescInfo())){ + detectInfo.setDescInfo(Constants.NO_DETECTDATA_STATUS_INFO); + logger.info("对节点"+ip+"的数据集超时,未获取监测数据"); + MonitorUtil.createMonitorData(ip, setInfo, startTime, Common.getAlarmInfoMap().get(setInfo.getId()+ ""), detectInfo); + if(!Common.noDetectDataNodeList.contains(ip)){ + Common.addNoDeteDataNode(ip); + logger.info("对节点"+ip+"的数据集超时,将节点加入无监测数据列表,由无监测数据收集线程进行收集"); + } + } + } catch (Exception ex) { + logger.error(ExceptionPrintUtils.printExceptionStack(ex)); + String processIden = setInfo.getProcessIden()==null ? "" : setInfo.getProcessIden(); + String[] alarm = Common.alarmExceptionInfo(setInfo.getId(), ip, Constants.DETEC_NMSC_STR,processIden , startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); + Common.addAlarmData(alarm); + } + } + + logger.info("对节点"+ip+"的数据收集及握手监测 完毕"+Constants.DATA_COLLECT_DAILY); + + } + } + } catch (Exception e) { + logger.error("Monitoring data collection management threads run abnormity",e); + } finally { + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("执行结束"); + } + } + + //获得进行握手监测的所有节点:握手的条件:到首次监测时间+节点有效(节点组有效)+监测设置内的节点+监测设置有效 + private Map getNmsclientNodeList(CommonService service){ + List nodeList = new ArrayList(); + Map nodeMap = new HashMap(); + try + { + if(Constants.FLAG_NMSC==1){ + List setList = service.getDrivingSetInfo(Constants.DETEC_NMSC_STR);//获得checkType类型的所有监测设置 + if(setList!=null && setList.size()>0){ + List nodeTmpList = new ArrayList(); + for (Iterator setIte = setList.iterator(); setIte.hasNext();) { + SetInfo setInfo = (SetInfo) setIte.next(); + //如果首次监测时间没到,则不用查询节点 + //- 计算启动后第一次检测时间 默认即时启动 + long initialDelay = 0l; + if(setInfo.getPlanCheckTime()!=null){ + initialDelay = setInfo.getPlanCheckTime().longValue() - (new Date().getTime()); + initialDelay = initialDelay > 0l ? initialDelay : 0l; + } + + if(initialDelay<=0) {//到首次监测时间了,查询进行握手监测的节点 + //节点可能存在重复的情况 + nodeTmpList = service.getNodeModelListBySetInfo(setInfo); + nodeList.addAll(nodeTmpList); + }else { + logger.info("握手监测,未到首次监测时间,距离首次监测时间还有:"+initialDelay/1000/60+"分钟"); + } + + for(NodeModel node:nodeTmpList) { + nodeMap.put(node.getNodeIp(), setInfo); + } + } + + logger.debug("本次服务器握手监测的节点数为:"+nodeMap.size()); + } + }else { + logger.info("未开启握手监测标识"); + } + } catch (Exception e) + { + logger.error("Obtaining a node exception for handshake monitoring", e); + } + + + return nodeMap; + } + + public static void main(String[] args) throws Exception { + + CommonDao dao = null; + ScheduledFuture future = null; + Object rlt = null; + try + { + Thread.sleep(3000); + BoneCPPool.initPool(); + Thread.sleep(3000); +// DataCollectManagerThread runThread = new DataCollectManagerThread(); +// Thread thread = new Thread(runThread); +// thread.start(); + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + // 初始化自身信息(失败情况下会由Web唤醒) + if(!service.initServerParams()){ + return; + } + + + future = Common.scheduled.scheduleWithFixedDelay(new DataCollectManagerThread(), 0, 300, TimeUnit.SECONDS); + Common.registRunnable(Constants.DATA_COLLECT_MANAGER, future); + Thread.sleep(7000); + System.out.println(future==null?"null3":future.isDone()); + System.out.println(future==null?"null4":future.isCancelled()); + System.out.println("***************=====结束了?"); + rlt = future.get(); + Thread.sleep(5000); +// future.cancel(true); + //发生异常未捕捉后: + System.out.println("future="+future); + System.out.println(future==null?"null55":future.isDone()); + System.out.println(future==null?"null55":future.isCancelled()); + System.out.println("==============结束了?55"); + System.out.println("rlt="+rlt); + + + + } catch (Exception e) + { + System.out.println("catch*******************"); + System.out.println("eeeeeeeefuture="+future); + System.out.println(future==null?"eeeeeeeeenull55":future.isDone()); + System.out.println(future==null?"eeeeeeeeeenull55":future.isCancelled()); + System.out.println("eeeeeeeeee==============结束了?55"); + System.out.println("eeeeeeeerlt="+rlt); + + System.out.println("数据收集线程是否注册-catch-done"+Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER).isDone()); + System.out.println("数据收集线程是否注册-catch-cancel"+Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER).isCancelled()); + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + System.out.println("finally**********************************"); +// Common.scheduled.shutdown();//关闭线程池 +// System.out.println(Common.scheduled.isTerminated());//????????? +// System.out.println(Common.scheduled.isShutdown()); + System.out.println("数据收集线程是否注册-catch-done"+Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER).isDone()); + System.out.println("数据收集线程是否注册-catch-cancel"+Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER).isCancelled()); +// System.out.println("关闭线程池"); + + } + } +} diff --git a/src/com/nms/server/thread/dataCollect/DataCollectThread.java b/src/com/nms/server/thread/dataCollect/DataCollectThread.java new file mode 100644 index 0000000..e0a3a9b --- /dev/null +++ b/src/com/nms/server/thread/dataCollect/DataCollectThread.java @@ -0,0 +1,613 @@ +package com.nms.server.thread.dataCollect; + +import java.io.File; +import java.util.Calendar; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.MissionResult2; +import com.nms.server.bean.SetInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.thread.file.UnZipManagerThread; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.socket.SSLSocketCallableForDateCollection; + +/** + * 数据(监测数据、任务结果、回传文件)收集线程 DC发送要收集数据的命令 + * Agent端发送6种数据命令中的一种,就开始收集,如果Agent发送6种数据命令之外的命令,就结束此次通信 + * + * @author hyx + * + */ +public class DataCollectThread extends SSLSocketCallableForDateCollection { + Logger logger = Logger.getLogger(DataCollectThread.class); + private String name; // 自定义Thread Name + private boolean nmsclientFlag = true;// 是否进行握手监测 + private SetInfo setInfo; + private long startTime;//数据收集服务器第一次启动的时间 + + public DataCollectThread(String name, String ip, Integer port, + boolean nmsclientFlag, SetInfo setInfo,long startTime) throws Exception { + super(ip, port); + this.name = name; + this.nmsclientFlag = nmsclientFlag; + this.setInfo = setInfo; + this.startTime = startTime; + } + + /** + * 与Client端进行通信:SSLSocketCallable已经握手过了 收集监测数据、任务结果、回传文件 + */ + @Override + protected Object toDo() throws Exception { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name); + int zipDetect = 0; + int csvDetect = 0; + int zipTaskResult = 0; + int objTaskResult = 0; + int zipTaskReturn = 0; + int fileTaskReturn = 0; + DetectInfo nmscDetec = null; + + try { + // 进行通信:SSLSocketCallable已经建立握手了 + sendMessage(SocketCMD.REQ_DATA_COLLECT);// DC:要进行数据的收集(监测数据、任务结果、回传文件) + String msg0 = receiveMessage();// Client:发送数据文件类型(监测数据、任务结果、回传文件的zip、csv或者list),或者没有数据,结束通信,或者关闭socket,则会收到null + // 执行到此,说明通信已经建立,进行握手 + if (nmsclientFlag && StringUtils.isNotBlank(msg0)) { + nmscDetec = Common.doNmsClientDetect(ip, setInfo, startTime); + } +// else if (StringUtils.isBlank(msg0) && Constants.IS_HANDEL_EXCEPTION_NODE){ +// nmscDetec = Common.errorNodeToDo(name, nmsclientFlag, setInfo, ip, Calendar.getInstance().getTimeInMillis(), startTime); +// } + while (Constants.FLAG_DATA_COLLECT_ONLY_HANDSHAKE==1 && true) { + logger.info("数据收集,client端" + this.ip + "回应信息:" + msg0); + if (Thread.currentThread().isInterrupted()) { + // 线程被中断,结束收集线程 + logger.info("收集线程 被中断,停止收集数据"); + return nmscDetec; + } + // 监测数据---start + if (SocketCMD.DETECT_DATA_ZIP_TYPE.equalsIgnoreCase(msg0)) { + logger.info("数据类型:监测数据的zip文件"); + this.sendMessage(SUCCESS);// DC告诉client,我知道你要发送zip类型的数据了 + String uploadPath = Constants.ZIP_FILE_DETECT_DATA_DIR; + String fileName = receiveMessage();// client发送zip文件的名称 + this.sendMessage(SUCCESS);// DC收到zip文件的名称,给client回成功信息 + bpReceiveFile(uploadPath + File.separator + fileName);// client收到zip文件名发送成功后,发送zip文件,DC接收client发送的zip文件 + this.sendMessage(SUCCESS);// DC接收完client发送的zip文件存入指定路径后,给client回接收成功信息 + + // 启动并注册 解压文件线程 + Future future = Common.getFutureMap().get( + Constants.UNZIP_FILE_MANAGER); + if (future == null) { + future = Common.scheduled.scheduleWithFixedDelay( + new UnZipManagerThread(), 0, 30, + TimeUnit.SECONDS); + Common.registRunnable(Constants.UNZIP_FILE_MANAGER, + future); + } + zipDetect++; + msg0 = receiveMessage();// Client:再次发送数据文件类型,或者没有数据,结束通信 + + } else if (SocketCMD.DETECT_DATA_CSV_TYPE + .equalsIgnoreCase(msg0)) { + logger.info("数据类型:监测数据的csv文件"); + sendMessage(SUCCESS);// DC告诉client,我知道你要发送csv类型的数据了,client端就开始发送csv文件 + LinkedList bslist = receiveFileBytesByBath();// DC端就准备接收csv文件 + if (bslist != null && bslist.size() > 0) { + int detectDataNum = Common.getAllDeteDataNum(); + + if(bslist.size()>Constants.MAX_COLLECT_RESOVE_DETECT_DATA_NUM + || detectDataNum>=Constants.MAX_DETECT_DATA_RESOVE_LIMIT_NUM){ + //存入dc_overrun/detect/zip_resove,等待之后解析,避免内存过大导致内存溢出 + logger.info("收集数据过多:"+bslist.size()+"条,或者 内存中监测数据总数过大:"+detectDataNum+" 为避免内存溢出,暂时存入硬盘,待之后解析入库,"); + Common.saveByteToFile(bslist); + }else { + Common.addAllDeteDataList(bslist); + } + } else { + logger.debug("接收 监测数据 文件数:0"); + } + sendMessage(SUCCESS);// DC端csv文件接收完毕后,告诉client端,文件接收完毕 + csvDetect++; + msg0 = receiveMessage();// Client:再次发送数据文件类型,或者没有数据,结束通信 + + } + // 监测数据---end + // 任务结果---start + else if (SocketCMD.TASK_RESULT_ZIP_TYPE.equalsIgnoreCase(msg0)) { + logger.info("数据类型:任务结果的zip文件"); + this.sendMessage(SUCCESS);// DC告诉client,我知道你要发送zip类型的数据了 + String uploadPath = Constants.ZIP_FILE_TASK_RESULT_DIR; + String fileName = receiveMessage();// client发送zip文件的名称 + this.sendMessage(SUCCESS);// DC收到zip文件的名称,给client回成功信息 + bpReceiveFile(uploadPath + File.separator + fileName);// client收到zip文件名发送成功后,发送zip文件,DC接收client发送的zip文件 + this.sendMessage(SUCCESS);// DC接收完client发送的zip文件存入指定路径后,给client回接收成功信息 + + // 启动并注册 解压文件线程 + Future future = Common.getFutureMap().get( + Constants.UNZIP_FILE_MANAGER); + if (future == null) { + future = Common.scheduled.scheduleWithFixedDelay( + new UnZipManagerThread(), 0, 30, + TimeUnit.SECONDS); + Common.registRunnable(Constants.UNZIP_FILE_MANAGER, + future); + } + zipTaskResult++; + msg0 = receiveMessage();// Client:再次发送数据文件类型,或者没有数据,结束通信 + + } else if (SocketCMD.TASK_RESULT_OBJ_TYPE + .equalsIgnoreCase(msg0)) {// -- + // 接收返回的任务结果:修改为receiveObject + logger.info("数据类型:任务结果的List"); + sendMessage(SUCCESS); + List resultList = (List) receiveObject(); + sendMessage(SUCCESS); + for (String result : resultList) { + logger.info("任务结果:" + result); + String[] misRlt2 = result + .split(Constants.COMMON_DATA_SPLIT); + MissionResult2 result2 = Common + .resoveMissionResult(misRlt2); + Common.addResultToResultList(result2);// 没有addAll,因为传递的任务结果可能是不同类型的任务结果(1-6) + } + objTaskResult++; + msg0 = receiveMessage();// Client:再次发送数据文件类型,或者没有数据,结束通信 + } + // 任务结果---end + // 回传文件---start + else if (SocketCMD.TASK_RETURN_ZIP_TYPE.equalsIgnoreCase(msg0)) { + logger.info("数据类型:回传文件的zip文件"); + this.sendMessage(SUCCESS);// DC告诉client,我知道你要发送zip类型的数据了 + String uploadPath = Constants.ZIP_FILE_TASK_RETURN_DIR; + String fileName = receiveMessage();// client发送zip文件的名称 + this.sendMessage(SUCCESS);// DC收到zip文件的名称,给client回成功信息 + bpReceiveFile(uploadPath + File.separator + fileName);// client收到zip文件名发送成功后,发送zip文件,DC接收client发送的zip文件 + this.sendMessage(SUCCESS);// DC接收完client发送的zip文件存入指定路径后,给client回接收成功信息 + + // 启动并注册 解压文件线程 + Future future = Common.getFutureMap().get( + Constants.UNZIP_FILE_MANAGER); + if (future == null) { + future = Common.scheduled.scheduleWithFixedDelay( + new UnZipManagerThread(), 0, 30, + TimeUnit.SECONDS); + Common.registRunnable(Constants.UNZIP_FILE_MANAGER, + future); + } + zipTaskReturn++; + msg0 = receiveMessage();// Client:再次发送数据文件类型,或者没有数据,结束通信 + + } else if (SocketCMD.TASK_RETURN_FILE_TYPE + .equalsIgnoreCase(msg0)) {// -- 接收任务4“启动”的回传文件 + + sendMessage(SUCCESS); + + String msg1 = receiveMessage(); + sendMessage(SUCCESS); + + logger.info(msg1); + String[] result = msg1.split(Constants.COMMON_DATA_SPLIT); + MissionResult2 result2 = Common.resoveMissionResult(result); + String fileName = receiveMessage(); + sendMessage(SUCCESS); + logger.debug("MISSION_FILE_UPLOAD_DIR" + + Constants.MISSION_FILE_UPLOAD_DIR); + + this.bpReceiveFile(Constants.MISSION_FILE_UPLOAD_DIR + + fileName); + + sendMessage(SUCCESS); + logger.debug("MISSION_FILE_UPLOAD_DIR" + + Constants.MISSION_FILE_UPLOAD_DIR + fileName); + + result2.setFileInfo(fileName); + Common.addResultToResultList(result2); + fileTaskReturn++; + + msg0 = receiveMessage();// Client:再次发送数据文件类型,或者没有数据,结束通信 + } + // 回传文件---end + else {// 如果没有这个else,且对方发送的命令非以上6种,则会死循环 + logger.info("数据收集线程:与client端" + this.ip + "的通信结束"); + break; + } + } + + logger.debug("client端" + this.ip + ",监测数据zip文件发送次数" + zipDetect);// 每次可以发送多个zip + logger.debug("client端" + this.ip + ",监测数据csv文件发送次数" + csvDetect);// 每次可以发送多个csv + logger.debug("client端" + this.ip + ",任务结果zip文件发送次数" + zipTaskResult);// 每次可以发送多个 + logger.debug("client端" + this.ip + ",任务结果obj文件发送次数" + objTaskResult);// 每次可以发送多个 + logger.debug("client端" + this.ip + ",回传文件zip文件发送次数" + zipTaskReturn);// 每次可以发送多个 + logger.debug("client端" + this.ip + ",回传文件file文件发送次数" + + fileTaskReturn);// 每次可以发送多个 + + } catch (Exception e) { + logger.error("Anomaly in the process of data collection", e); +// if(nmscDetec==null && Constants.IS_HANDEL_EXCEPTION_NODE){ +// nmscDetec = Common.errorNodeToDo(name, nmsclientFlag, setInfo, ip, Calendar.getInstance().getTimeInMillis(), startTime); +// } + } + return nmscDetec; + } +// //创建握手监测信息 +// protected void createNmsClientDetectData(DetectInfo detectInfo){ +// if(detectInfo!=null){ +// int testTimes = 1; +// Long checkTime = System.currentTimeMillis(); +// detectInfo.setTestTimes(testTimes);//尝试次数 +// detectInfo.setCheckTime(checkTime);//监测时间 +// try{ +// /** +// * -- 监测数据描述信息非空(web界面的状态信息),才生成监测数据: +// * 1、监测数据为空可能是收集线程超时导致ping或者ssh未执行完毕,未生成描述信息 +// * 2、此时web端进行超周期设置时,过滤掉异常的节点,异常节点的异常信息还是采用之前周期的-此功能 20160906 web端还未实现 +// */ +// if(StringUtils.isNotBlank(detectInfo.getDescInfo())){ +// MonitorUtil.createMonitorData(ip, setInfo, startTime, Common.getAlarmInfoMap().get(setInfo.getId()+ ""), detectInfo); +// } +// } catch (Exception ex) { +// logger.error(ExceptionPrintUtils.printExceptionStack(ex)); +// String processIden = setInfo.getProcessIden()==null ? "" : setInfo.getProcessIden(); +// String[] alarm = Common.alarmExceptionInfo(setInfo.getId(), ip, Constants.DETEC_NMSC_STR,processIden , startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); +// Common.addAlarmData(alarm); +// } +// } +// } + + // 收集数据前先进行握手监测通信 +// private DetectInfo doNmsClientDetect() { +// logger.info("开始握手监测>" + ip); +// DetectInfo detectInfo = null; +// try { +// // -- 发送握手命令 +// String sendTime = Calendar.getInstance().getTimeInMillis() + ""; +// // sendMessage(SocketCMD.CLIENT_REQ_HAND_SHAKE);//DC与NC握手通信后,NC会关闭socket,导致无法收集数据 +// // String msg = receiveMessage();//收到回复的信息后,说明握手成功了 +// +// String backTime = Calendar.getInstance().getTimeInMillis() + ""; +// // -- 信息保存到监测信息对象中 +// detectInfo = new DetectInfo(); +// +// StringBuffer nmsClientDetailDetectInfo = new StringBuffer(); +// nmsClientDetailDetectInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);// 发送信息 +// nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// nmsClientDetailDetectInfo.append(SUCCESS + ":" +// + SocketCMD.CLIENT_REQ_HAND_SHAKE + "|" + sendTime);// 返回信息:DC端伪造的,因为也不需要NC回复信息了 +// nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// nmsClientDetailDetectInfo.append("0");// 是否可达,0:可达,1:不可达 +// nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// nmsClientDetailDetectInfo.append(sendTime);// 发送时间 +// nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// nmsClientDetailDetectInfo.append(backTime);// 返回时间 +// nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// nmsClientDetailDetectInfo.append(DateUtil.getSecondsFromBeinToEnd( +// new Date(Long.parseLong(sendTime)).getTime(), new Date(Long +// .parseLong(backTime)).getTime()) +// + "");// 延迟时间 +// +// detectInfo.getDetailDatas().add( +// nmsClientDetailDetectInfo.toString()); +// +// detectInfo.setDescInfo("开始" +// + DateUtil.getStringByLongStr(sendTime) +// + " NMSClient握手" +// + DateUtil.getStringByLongStr(backTime) +// + " 成功 "); +// +// } catch (Exception e) { +// logger.error("生成节点:"+ip+"信息发生异常", e); +// }finally{ +// if (Thread.currentThread().isInterrupted()) { +// // 线程被中断,结束收集线程 +// logger.info("收集线程 被中断"); +// return detectInfo; +// }else { +// Common.createNmsClientDetectData(setInfo, ip, detectInfo, startTime); +// } +// } +// +// return detectInfo; +// } +// +// @Override +// protected DetectInfo errorNodeToDo(Long checkTime){ +// if(Constants.IS_HANDEL_EXCEPTION_NODE) { +// return Common.errorNodeToDo(name, nmsclientFlag, setInfo, ip, checkTime, startTime); +// }else { +// return null; +// } +// } + +// @Override +// protected DetectInfo errorNodeToDo(Long checkTime){ +// Thread.currentThread().setName(name + "(异常节点)"); +// if (Thread.currentThread().isInterrupted()) { +// // 线程被中断,结束收集线程 +// logger.info("收集线程 被中断"); +// return null; +// } +// if(!nmsclientFlag){//对于不需要进行握手监测的节点,则不进行握手 +// return null; +// } +// DetectInfo detectInfo = null; +// String stateInfo = ""; +// try{ +// detectInfo = new DetectInfo(); +// String failStartTime = Calendar.getInstance().getTimeInMillis() +// + ""; +// long maxTimes = 1l; +// +// if (setInfo != null && setInfo.getCheckMaxTimes() != null) { +// maxTimes = setInfo.getCheckMaxTimes(); +// } +// +// String[] pingData = pingHandshake(ip, maxTimes); +// +// String failEndTime = Calendar.getInstance().getTimeInMillis() + ""; +// +// if ((StringUtils.isEmpty(pingData[1]) || "0".equals(pingData[1]))) {// ping不通,无握手监测详细数据 +// stateInfo = "网络异常"; +// } else { +// // 可以ping通进行ssh测试 如果ssh可以通 就产生一条握手失败信息 如果ssh不同就产生一条ssh失败信息 +// String sshResult = Constants.SUCCESS; +// if("1".equals(Constants.FLAG_HANDWALK_SSH)){ +// String[] userPwdInfo = Common.getIpUserPwdMap().get(ip); +// if (userPwdInfo!=null && +// userPwdInfo.length>=3 && "1".equals(userPwdInfo[2])) {//应该判断nc的操作系统,不是dc的,1:linux; 2:windows; 3:other +// sshResult = sshConnect(ip,userPwdInfo[0],userPwdInfo[1]); +// } +// } +// +// if (Constants.SUCCESS.equals(sshResult)) { +// stateInfo = "开始" + DateUtil.getStringByLong(checkTime) +// + " NMSClient握手" + DateUtil.getCurrentTime() + " 失败 "; +// StringBuffer detailInfo = new StringBuffer(); +// detailInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);// 发送信息 +// detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// detailInfo.append("");// 返回信息 +// detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// detailInfo.append("1");// 是否可达,0:可达,1:不可达 +// detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// detailInfo.append(failStartTime);// 发送时间 +// detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// detailInfo.append(failEndTime);// 返回时间 +// detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 +// detailInfo.append(DateUtil.getSecondsFromBeinToEnd(new Date( +// Long.parseLong(failStartTime)).getTime(), new Date(Long +// .parseLong(failEndTime)).getTime()) +// + "");// 延迟时间 +// detectInfo.getDetailDatas().add(detailInfo.toString()); +// }else{ +// stateInfo = sshResult; +// } +// } +// }catch (Exception e) { +// logger.error("生成异常节点:"+ip+"信息发生异常", e); +// }finally{ +// detectInfo.setDescInfo(stateInfo); +// logger.info("与 " + ip + " 的握手监测发生异常 失败:" + stateInfo); +// createNmsClientDetectData(detectInfo);//生成监测数据信息 +// } +// return detectInfo; +// } +// +// private String[] pingHandshake(String ip, long maxTimes) { +// +// String command = ""; // 命令语句 +// int minTime = Integer.MAX_VALUE, maxTime = 0, totalTime = 0, curTime; // 最短时间,最长时间,总时间,时间变量 +// int snum = 0, fnum = 0; // 发包成功和失败数 +// +// // 判断系统类型 win or Linux +// String system = (String) (System.getProperty("os.name")).toLowerCase(); +// +// if (system.toLowerCase().indexOf("win") != -1) { +// command += "ping -n 4 " + ip;// 尝试次数 -n +// } else if (system.toLowerCase().indexOf("linux") != -1) { +// command += "ping -c 4 " + ip; +// } else { +// command += "ping -w 4 " + ip; +// } +// +// String stateInfo = ""; +// String[] datas2 = new String[7]; +// Process process = null; +// BufferedReader in = null; // 读取 Ping命令返回的信息 +// StringBuffer buffer = new StringBuffer(); +// try { +// logger.debug("ping command:" + command); +// process = Runtime.getRuntime().exec(command); +// in = new BufferedReader(new InputStreamReader(process +// .getInputStream())); +// String line = null; +// long count = maxTimes + 10; +// int index; +// // 最多多读10行 +// while ((line = in.readLine()) != null && count != 0) { +// if ("".equals(line)) { +// count--; +// continue; +// } // 空串跳过 +// buffer.append(line + "\n"); +// line = line.toLowerCase(); +// logger.debug("line:" + line); +// if (line.indexOf("ttl") > 0) { // 获得成功响应的数据 +// count--; // 计数器自减1 +// // 截取time 值 +// if ((index = line.lastIndexOf("ms")) != -1) { +// byte[] buf = line.getBytes(); +// int start = 0, end = buf.length, i, j; +// // 下标末点 +// for (i = index; i >= 0; i--) { +// if (Character.isDigit((char) buf[i])) { +// end = i; +// break; +// } +// } +// if (i == 0) +// continue; +// // 下标起点 +// for (j = end; j >= 0; j--) { +// if (!Character.isDigit((char) buf[j])) { +// start = j + 1; +// break; +// } +// } +// // 时间值截取 +// curTime = Integer.parseInt(new String(buf, start, end +// + 1 - start)); +// snum++; // 成功接收次数加1 +// totalTime += curTime; // 计算总时间 +// +// if (curTime < minTime) { +// minTime = curTime; +// } // 最小时间 +// +// if (curTime > maxTime) { +// maxTime = curTime; +// } // 最大时间 +// } +// } else if (line.split(" ").length < 4) { +// count--; // 计数器自减1 +// fnum++; // 失败接收次数加1 +// } else { +// stateInfo += line + "\n"; +// } +// } +// +// if (totalTime == 0) { +// minTime = 0; +// } +// +// // 已发送包数 +// datas2[0] = "" + (snum + fnum); +// datas2[1] = "" + snum; +// datas2[2] = "" + fnum; +// +// BigDecimal fnum0 = new BigDecimal(fnum); +// BigDecimal tnum0 = new BigDecimal(snum + fnum); +// DecimalFormat df = new DecimalFormat("####0.00"); +// if (tnum0.intValue() != 0) { +// datas2[3] = "" +// + ((new BigDecimal(df.format(fnum0.divide(tnum0, 4, +// RoundingMode.HALF_UP)))) +// .multiply(new BigDecimal(100))); +// } +// datas2[4] = "" + minTime; +// datas2[5] = "" + maxTime; +// datas2[6] = "" + (snum == 0 ? 0 : (totalTime / snum)); +// } catch (Exception e) { +// logger.error(ExceptionPrintUtils.printExceptionStack(e)); +// } finally { +// if (in != null) { +// try { +// in.close(); +// } catch (IOException e) { +// logger.error("", e); +// } +// } +// if (process != null) +// process.destroy(); +// process = null; +// } +// +// /* +// * if(state==-1){ isSuccess = false; }else{ isSuccess = true; } +// */ +// return datas2; +// } +// +// private String sshConnect(String hostname, String username, String password) { +// String result = Constants.SUCCESS; +// long a = new Date().getTime(); +// Connection conn = null; +// Session sess = null; +// try { +// if(StringUtils.isNotBlank(hostname)){ +// conn = new Connection(hostname); +// conn.connect(); +// long b = new Date().getTime(); +// logger.debug("SSHConnect----链接ip耗时:" + (b - a) + "毫秒," + (b - a) +// / 1000 + "秒"); +// boolean isAuthenticated = conn.authenticateWithPassword(username, +// password); +// if (isAuthenticated == true) { +// sess = conn.openSession(); +// sess.execCommand("date"); +// InputStream stdout = new StreamGobbler(sess.getStdout()); +// BufferedReader stdoutReader = new BufferedReader( +// new InputStreamReader(stdout, Charset.forName("utf-8"))); +// while (true) { +// String line = stdoutReader.readLine(); +// if (line == null){ +// break; +// } +// } +// } +// long c = new Date().getTime(); +// logger.debug("SSHConnect----测试完成,总耗时:" + (c - a) + "毫秒," + (c - a)/1000 + "秒"); +// } +// } catch (IOException e) { +// long d = new Date().getTime(); +// logger.error("SSHConnect----连接异常,耗时:" + (d - a) + "毫秒," + (d - a) +// / 1000 + "秒", e); +// if(e.getMessage().contains("Authentication")){ +// result = "SSH 登录用户名或密码错误"; +// }else { +// result = "SSH 连接失败"; +// } +// } catch (Exception e1) { +// long e = new Date().getTime(); +// logger.error("SSHConnect----连接异常,耗时:" + (e - a) + "毫秒," + (e - a) +// / 1000 + "秒", e1); +// result = "SSH 连接失败"; +// }finally { +// if(conn!=null){ +// conn.close(); +// } +// if(sess!=null){ +// sess.close(); +// } +// } +// return result; +// } + +// public static void main(String[] args) { + + /* + * try { + * + * Future future = null; long start = System.currentTimeMillis(); + * future = Common.service.submit(new + * DataCollectThread("数据收集执行线程","10.0.6.234" + * ,Constants.SSL_CLIENT_PORT,true)); + * System.out.println("done1:"+future.isDone()); + * + * // System.out.println("new:"+new + * DataCollectThread("数据收集执行线程","10.0.6.102" + * ,Constants.SSL_CLIENT_PORT,true).toDo()); // Thread.sleep(3000); // + * System.out.println("done2:"+future.isDone()); + * System.out.println("get:"+future.get()); + * System.out.println("done3:"+future + * .isDone()+"="+(System.currentTimeMillis()-start)); + * + * try { future.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + * } catch (Exception e) { // logger.error("DC清理数据超时, 缓存数据存入硬盘", e); } + * + * + * // new + * DataCollectThread("数据收集执行线程","10.0.6.113",Constants.SSL_CLIENT_PORT + * ,true).call(); } catch (Exception e) { // TODO Auto-generated catch + * block e.printStackTrace(); } + */ +// } + +} diff --git a/src/com/nms/server/thread/dataCollect/NoDetectDataCollectManagerThread.java b/src/com/nms/server/thread/dataCollect/NoDetectDataCollectManagerThread.java new file mode 100644 index 0000000..dc4c064 --- /dev/null +++ b/src/com/nms/server/thread/dataCollect/NoDetectDataCollectManagerThread.java @@ -0,0 +1,251 @@ +package com.nms.server.thread.dataCollect; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import org.apache.log4j.Logger; +import com.nms.server.bean.DetectInfo; +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.thread.monitor.util.MonitorUtil; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.BoneCPPool; +import com.nms.server.util.DateUtil; + +//监测数据主动收集管理线程 +public class NoDetectDataCollectManagerThread implements Runnable{ + private Logger logger = Logger.getLogger(NoDetectDataCollectManagerThread.class); + private long startTime = Calendar.getInstance().getTimeInMillis(); + + public void run() { +// Thread.currentThread().setName("无监测数据节点 数据(监测数据、任务结果、回传文件)收集管理线程"); + Thread.currentThread().setName("No Monitoring Data Node Data (Monitoring data, Task results, Return files) Collect Management Threads"); + CommonDao dao = null; + try { + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + //- 非升级操作判断 + if(Common.SERVER_UN_UPGRADE_FLAG && Common.noDetectDataNodeList.size()>0){ + //循环向自己管辖的Client端,收集数据 + List ipList = new LinkedList(); + ipList = service.getAllComputerIp();//当前DC管理范围内+有效节点+有效节点组+服务器节点 + /* + * 2013-7-29 hyx + * 获得所有握手监测的节点信息,若数据收集的节点在握手监测节点范围内,则收集数据的同时进行握手监测(传递true),确认一个,从握手监测中删除一个节点,最后未握手的节点则不在数据收集范围内(这种情况不应该存在) + */ + Map nodeMap = getNmsclientNodeList(service);//ip:setinfo + + //20160901 hyx 增加此代码的原因是:数据收集管理线程运行一段时间后,会出现获取数据库连接一直阻塞的情况,而此时其它线程是可以正常获取数据库连接的,一个原因可能是之前没有dao=null;另一个原因可能是,之后的数据收集及超时操作,可能由于各种原因,如异常节点较多,导致连接最后没能正常关闭,所以在连接不使用的时候,尽早关闭 + try { + if(dao!=null){ + dao.close(); + dao=null; + } + } catch (Exception e) + { + logger.error("Data collection management thread early closing database connection exception", e); + } + + List list = Common.pingHandshake(Common.noDetectDataNodeList); + String stateInfo = ""; + String failStartTime = ""; + String failEndTime = ""; + int ipSize = Common.noDetectDataNodeList.size(); + for(int i=0;i=3 && "1".equals(userPwdInfo[2])) {//应该判断nc的操作系统,不是dc的,1:linux; 2:windows; 3:other + sshResult = Common.sshConnect(ip,userPwdInfo[0],userPwdInfo[1]); + } + } + if(Constants.SUCCESS.equals(sshResult)){ + //ping和ssh都成功 + logger.debug("ip :"+ip+"恢复正常,从异常节点列表中删除"); + Common.noDetectDataNodeList.remove(ip); + i--; + ipSize--; +// stateInfo = "开始" + DateUtil.getStringByLong(startTime)+" NMSClient握手" + DateUtil.getCurrentTime() + " 失败 "; +// stateInfo = "Start" + DateUtil.getStringByLong(startTime)+" NMSClient Shakehand" + DateUtil.getCurrentTime() + " failed"; + stateInfo = "i18n_server.NoDetectDataCollectManagerThread.start_n81i" + DateUtil.getStringByLong(startTime)+" i18n_server.NoDetectDataCollectManagerThread.shakehand_n81i" + DateUtil.getCurrentTime() + " i18n_server.NoDetectDataCollectManagerThread.faild_n81i"; + }else{ + logger.debug("ip :"+ip+" 异常信息:"+sshResult); + stateInfo = sshResult; + } + failEndTime = Calendar.getInstance().getTimeInMillis()+ ""; + StringBuffer detailInfo = new StringBuffer(); + detailInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);// 发送信息 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append("");// 返回信息 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append("1");// 是否可达,0:可达,1:不可达 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(failStartTime);// 发送时间 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(failEndTime);// 返回时间 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(DateUtil.getSecondsFromBeinToEnd(new Date( + Long.parseLong(failStartTime)).getTime(), new Date(Long + .parseLong(failEndTime)).getTime())+ "");// 延迟时间 + detectInfo.getDetailDatas().add(detailInfo.toString()); + } + detectInfo.setDescInfo(stateInfo); + Common.createNmsClientDetectData(nodeMap.get(ip),ip,detectInfo, startTime);//生成监测数据信息 + } + + } + } catch (Exception e) { + logger.error("Monitoring data collection management threads run abnormity",e); + } finally { + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("执行结束"); + } + } + + //获得进行握手监测的所有节点:握手的条件:到首次监测时间+节点有效(节点组有效)+监测设置内的节点+监测设置有效 + private Map getNmsclientNodeList(CommonService service){ + List nodeList = new ArrayList(); + Map nodeMap = new HashMap(); + try + { + if(Constants.FLAG_NMSC==1){ + List setList = service.getDrivingSetInfo(Constants.DETEC_NMSC_STR);//获得checkType类型的所有监测设置 + if(setList!=null && setList.size()>0){ + List nodeTmpList = new ArrayList(); + for (Iterator setIte = setList.iterator(); setIte.hasNext();) { + SetInfo setInfo = (SetInfo) setIte.next(); + //如果首次监测时间没到,则不用查询节点 + //- 计算启动后第一次检测时间 默认即时启动 + long initialDelay = 0l; + if(setInfo.getPlanCheckTime()!=null){ + initialDelay = setInfo.getPlanCheckTime().longValue() - (new Date().getTime()); + initialDelay = initialDelay > 0l ? initialDelay : 0l; + } + + if(initialDelay<=0) {//到首次监测时间了,查询进行握手监测的节点 + //节点可能存在重复的情况 + nodeTmpList = service.getNodeModelListBySetInfo(setInfo); + nodeList.addAll(nodeTmpList); + }else { + logger.info("握手监测,未到首次监测时间,距离首次监测时间还有:"+initialDelay/1000/60+"分钟"); + } + + for(NodeModel node:nodeTmpList) { + nodeMap.put(node.getNodeIp(), setInfo); + } + } + + logger.debug("本次服务器握手监测的节点数为:"+nodeMap.size()); + } + }else { + logger.info("未开启握手监测标识"); + } + } catch (Exception e) + { + logger.error("Obtaining a node exception for handshake monitoring", e); + } + return nodeMap; + } + + public static void main(String[] args) throws Exception { + + CommonDao dao = null; + ScheduledFuture future = null; + Object rlt = null; + try + { + Thread.sleep(3000); + BoneCPPool.initPool(); + Thread.sleep(3000); +// DataCollectManagerThread runThread = new DataCollectManagerThread(); +// Thread thread = new Thread(runThread); +// thread.start(); + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + // 初始化自身信息(失败情况下会由Web唤醒) + if(!service.initServerParams()){ + return; + } + + future = Common.scheduled.scheduleWithFixedDelay(new NoDetectDataCollectManagerThread(), 0, 300, TimeUnit.SECONDS); + Common.registRunnable(Constants.DATA_COLLECT_MANAGER, future); + Thread.sleep(7000); + System.out.println(future==null?"null3":future.isDone()); + System.out.println(future==null?"null4":future.isCancelled()); + System.out.println("***************=====结束了?"); + rlt = future.get(); + Thread.sleep(5000); +// future.cancel(true); + //发生异常未捕捉后: + System.out.println("future="+future); + System.out.println(future==null?"null55":future.isDone()); + System.out.println(future==null?"null55":future.isCancelled()); + System.out.println("==============结束了?55"); + System.out.println("rlt="+rlt); + + + + } catch (Exception e) + { + System.out.println("catch*******************"); + System.out.println("eeeeeeeefuture="+future); + System.out.println(future==null?"eeeeeeeeenull55":future.isDone()); + System.out.println(future==null?"eeeeeeeeeenull55":future.isCancelled()); + System.out.println("eeeeeeeeee==============结束了?55"); + System.out.println("eeeeeeeerlt="+rlt); + + System.out.println("数据收集线程是否注册-catch-done"+Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER).isDone()); + System.out.println("数据收集线程是否注册-catch-cancel"+Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER).isCancelled()); + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + System.out.println("finally**********************************"); +// Common.scheduled.shutdown();//关闭线程池 +// System.out.println(Common.scheduled.isTerminated());//????????? +// System.out.println(Common.scheduled.isShutdown()); + System.out.println("数据收集线程是否注册-catch-done"+Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER).isDone()); + System.out.println("数据收集线程是否注册-catch-cancel"+Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER).isCancelled()); +// System.out.println("关闭线程池"); + + } + } +} diff --git a/src/com/nms/server/thread/dataCollect/NonRltTaskResultCollectManagerThread.java b/src/com/nms/server/thread/dataCollect/NonRltTaskResultCollectManagerThread.java new file mode 100644 index 0000000..b1a66b7 --- /dev/null +++ b/src/com/nms/server/thread/dataCollect/NonRltTaskResultCollectManagerThread.java @@ -0,0 +1,107 @@ +package com.nms.server.thread.dataCollect; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +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.util.ExceptionPrintUtils; + +//主动收集任务(执行完成,但无结果的任务)结果管理线程 +public class NonRltTaskResultCollectManagerThread implements Runnable{ + private Logger logger = Logger.getLogger(NonRltTaskResultCollectManagerThread.class); + + public void run() { + CommonDao dao = null; + try { +// Thread.currentThread().setName("数据(无结果任务的结果数据)收集管理线程"); + Thread.currentThread().setName("Data (No result task result data) To Collect Management Thread"); + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + //- 非升级操作判断 + if(Common.SERVER_UN_UPGRADE_FLAG){//---起什么作用? + /** + * 向表MISSION_STATE_TABLE中查询,MISSION_TYPE任务类型(1,4,6),IS_LOOP是否为周期任务(1是;0不是): + * 一个任务会有多个节点(ip)执行,任务结果表中,一个任务id会有多条,seq_id不同,什么时候任务结果表里会有对应任务id的结果信息?所以从表MISSION_STATE_TABLE开始查 + * 按任务类型来: + */ + //需要收集任务结果的列表(ip:<任务id,>) + Map> ipTaskIdsMap = service.getNonRltIpTaskMap(); + + Iterator ite = ipTaskIdsMap.entrySet().iterator(); + int j=0; + String key = ""; + while(ite.hasNext()) { + Entry en = (Entry)ite.next(); + String ip = en.getKey().toString(); + if(ip!=null&&!"".equals(ip.trim())) { + //- 检查线程运行状态 运行中无操作 + key = Constants.NONRLTTASK_RESULT_COLLECT+":"+ip; + Future future = Common.getFutureMap().get(key); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + logger.info("对"+ip+"的任务结果(对于该有结果,而无结果的任务)收集执行线程 运行中 不再启动新收集线程"); + continue; + } + Map taskInfoMap = (Map)en.getValue();//当前ip上执行成功的任务id +// future = Common.service.submit(new NonRltTaskResultCollectThread("任务结果(对于该有结果,而无结果的任务)收集执行线程",ip,Constants.SSL_CLIENT_PORT,taskInfoMap)); + future = Common.service.submit(new NonRltTaskResultCollectThread("Task Result(For tasks that should have results,but not results) Collect Execution Thread",ip,Constants.SSL_CLIENT_PORT,taskInfoMap)); + //注册 + Common.getFutureMap().put(key, future); + j++; + } + + } + + logger.info("*******************************************共"+ipTaskIdsMap.size()+"个节点,任务结果(对于该有结果,而无结果的任务)收集执行线程启动个数:"+j); + } + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("执行结束"); + } + } + + public static void main(String[] args) { + try + { +// new InitServerThread().initCommonInfo(new CommonDao()); +// NonRltTaskResultCollectManagerThread runThread = new NonRltTaskResultCollectManagerThread(); +// Thread thread = new Thread(runThread); +// thread.start(); +// +// Thread.sleep(10000); +// +// MissionResultThread mrt = new MissionResultThread("rlt"); +// mrt.run(); +// Map m = new HashMap(); +//// String key = ""; +// for(int i=0;i<6;i++) { +// String key="i="+i; +// m.put(key, ":"+i); +// } +// +// Iterator i = m.keySet().iterator(); +// while(i.hasNext()) { +// String e = (String)i.next(); +// System.out.println("key="+e.toString()+",value="+m.get(e).toString()); +// } + + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/com/nms/server/thread/dataCollect/NonRltTaskResultCollectThread.java b/src/com/nms/server/thread/dataCollect/NonRltTaskResultCollectThread.java new file mode 100644 index 0000000..6444aff --- /dev/null +++ b/src/com/nms/server/thread/dataCollect/NonRltTaskResultCollectThread.java @@ -0,0 +1,156 @@ +package com.nms.server.thread.dataCollect; + +import java.io.File; +import java.util.Iterator; +import java.util.Map; +import java.util.ResourceBundle; +import java.util.Map.Entry; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult2; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.DateUtil; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.socket.SSLSocketCallable; + +/** + * 任务(对于该有结果,而无结果的任务)结果收集线程 + * DC发送要收集的命令 + * Agent端发送任务结果 + * + * @author hyx + * + */ +public class NonRltTaskResultCollectThread extends SSLSocketCallable { + Logger logger = Logger.getLogger(NonRltTaskResultCollectThread.class); + private String name; // 自定义Thread Name + private Map taskInfoMap;//需要收集结果的任务id + + public NonRltTaskResultCollectThread(String name,String ip,Integer port,Map taskInfoMap) throws Exception { + super(ip,port); + this.name = name; + this.taskInfoMap = taskInfoMap; + } + + /**与Client端进行通信:SSLSocketCallable已经握手过了 + * 收集监测数据、任务结果、回传文件 + */ + @Override + protected Object toDo() throws Exception + { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name); +// int zipTaskResult = 0; +// int objTaskResult = 0; + + try { +// //设置超时时间 + socket.setSoTimeout(100*1000);//如果放到String msg0 = receiveMessage();下面,会报错,socket closed + //进行通信:SSLSocketCallable已经建立握手了 + sendMessage(SocketCMD.REQ_NONRLTTASK_RESULT_COLLECT);//DC:要进行任务结果收集 + String msg0 = receiveMessage(); + logger.info("任务(对于该有结果,而无结果的任务)结果收集,client端"+this.ip+"回应信息:"+msg0); + + if(SUCCESS.equalsIgnoreCase(msg0)) { + Iterator taskInfoIte = taskInfoMap.entrySet().iterator(); + while(taskInfoIte.hasNext()) { + Entry enTask = (Entry)taskInfoIte.next(); + if(enTask!=null && enTask.getKey()!=null) { + String taskId = enTask.getKey().toString(); + String[] taskInfo = (String[])enTask.getValue(); + if(StringUtils.isNotBlank(taskInfo[2])) { + logger.info("startTime="+taskInfo[2]); + Long timeTmp=null; + + if(NumberUtils.isDigits(taskInfo[2])){ + timeTmp = Long.parseLong(taskInfo[2]); + }else{ + timeTmp = DateUtil.toLongTime(taskInfo[2]); + } + + taskInfo[2] = timeTmp==null?null:timeTmp.toString(); + } + if(StringUtils.isNotBlank(taskInfo[3])) { + logger.info("endTime="+taskInfo[3]); + Long timeTmp3=null; + + if(NumberUtils.isDigits(taskInfo[2])){ + timeTmp3 = Long.parseLong(taskInfo[2]); + }else{ + timeTmp3 = DateUtil.toLongTime(taskInfo[2]); + } + taskInfo[3] = timeTmp3==null?null:timeTmp3.toString(); + } + String taskInfoStr = taskId+Constants.COMMON_DATA_POINT+ + taskInfo[0]+Constants.COMMON_DATA_POINT+ + taskInfo[1]+Constants.COMMON_DATA_POINT+ + (taskInfo[2]==null?"":taskInfo[2])+Constants.COMMON_DATA_POINT+//与NC协商好的,用"" + (taskInfo[3]==null?"":taskInfo[3]); +// System.out.println("向NC端发送的任务信息"+taskInfoStr); + logger.info("DC向NC("+this.ip+")端发送的任务信息:"+taskInfoStr); + this.sendMessage(taskInfoStr); + + msg0 = receiveMessage(); + if(StringUtils.isNotBlank(msg0)) { +// System.out.println("NC端发送的结果信息"+msg0); + logger.info("NC("+this.ip+")向DC端发送的结果信息:"+msg0); + String [] result = msg0.split(Constants.COMMON_DATA_SPLIT); +// logger.info("参数个数:"+result.length); +// logger.info("结果:"+result[0]); + if("1".equals(result[0])) {//如果失败,则将结果存入缓存列表,等待下次入库 + MissionResult2 result2 = new MissionResult2(); + result2.setMissionId(taskId==null?null:Long.parseLong(taskId)); + result2.setResult(1l);//不属于人工判定失败 + result2.setDescription(result[1]); + result2.setStartTime(taskInfo[2]==null?null:Long.parseLong(taskInfo[2])); + result2.setEndTime(taskInfo[3]==null?null:Long.parseLong(taskInfo[3])); + result2.setLoopFlag(taskInfo[1]==null?null:Long.parseLong(taskInfo[1])); + result2.setMissionType(taskInfo[0]==null?null:Long.parseLong(taskInfo[0])); + result2.setUuid(Common.getIpSeqIdMap().get(ip)); + Common.addResultToResultList(result2); + logger.info("存入缓存一条失败结果(NC("+this.ip+")端无任务"+taskId+"对应的结果,人工产生一条失败结果)"); + } + } + } + } + sendMessage(SocketCMD.COMMUNICATION_END); + } + + logger.info("任务结果收集执行线程:与client端"+this.ip+"的通信结束"); + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + return null; + } + + public static void main(String[] args) { + try + { +// new NonRltTaskResultCollectThread("数据收集执行线程","10.0.6.120",Constants.SSL_CLIENT_PORT,"1274605").call(); + + //只处理绝对路径 + ResourceBundle log4jRb = ResourceBundle.getBundle("log4j"); + String logFile = log4jRb.getString("log4j.appender.logfile.File"); + File f = new File(logFile); + if(f!=null&&f.isAbsolute()) { + String logPath = f.getParent(); + System.out.println(logPath); + } +// System.out.println(f.getParent());//1.null 2.\* 3* +// System.out.println(f.isAbsolute());//1.null 2.\* 3* +// System.out.println(f.getParentFile().isDirectory());//1.null 2.\* 3* +// System.out.println(f.getParentFile().getAbsolutePath()); +// String webPathTmp = NonRl/buildPath(webPathTmp.substring(1,webPathTmp.length()-16)); + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/com/nms/server/thread/deleteFiles/DeleteFilesManagerThread.java b/src/com/nms/server/thread/deleteFiles/DeleteFilesManagerThread.java new file mode 100644 index 0000000..e5e2be4 --- /dev/null +++ b/src/com/nms/server/thread/deleteFiles/DeleteFilesManagerThread.java @@ -0,0 +1,38 @@ +package com.nms.server.thread.deleteFiles; + +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.ExceptionPrintUtils; + +//监测数据主动收集管理线程 +public class DeleteFilesManagerThread implements Runnable{ + private Logger logger = Logger.getLogger(DeleteFilesManagerThread.class); + + public void run() { + try { +// Thread.currentThread().setName("定时删除文件(log、error->detect、download)管理线程"); + Thread.currentThread().setName("Regularly Delete Files (log, error->detect, download) Management Thread"); + + Future future = Common.getFutureMap().get(Constants.DELETE_TMP_FILES); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + logger.info("定时删除文件(log、error->detect、download)执行线程 运行中 不再启动新删除线程"); + return; + } + + future = Common.service.submit(new DeleteFilesThread()); + //注册 + Common.getFutureMap().put(Constants.DELETE_TMP_FILES, future); + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + + public static void main(String[] args) { + + } +} diff --git a/src/com/nms/server/thread/deleteFiles/DeleteFilesThread.java b/src/com/nms/server/thread/deleteFiles/DeleteFilesThread.java new file mode 100644 index 0000000..deaa47f --- /dev/null +++ b/src/com/nms/server/thread/deleteFiles/DeleteFilesThread.java @@ -0,0 +1,148 @@ +package com.nms.server.thread.deleteFiles; + +import java.io.File; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.ResourceBundle; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; +import com.nms.server.util.FileUtils; + +/** + * 定时删除指定文件 + * 2013-3-14 hyx + */ +public class DeleteFilesThread implements Runnable{ + private Logger logger = Logger.getLogger(DeleteFilesThread.class); + + @Override + public void run() { + Thread.currentThread().setName("Regularly Delete Files (log, error->detect, download) Thread"); + try { + + //删除日志文件:只处理绝对路径 + ResourceBundle log4jRb = ResourceBundle.getBundle("log4j"); + String logFileStr = null; + try { + logFileStr = log4jRb.getString("log4j.appender.logfile.File"); + } catch (Exception e) { + logger.info("未找到 旧格式日志文件路径 跳过旧格式日志删除"); + } + + if(StringUtils.isNotBlank(logFileStr)){ + File logFile = new File(logFileStr); + if(logFile!=null&&logFile.isAbsolute()) { + String logPath = logFile.getParent(); + File tmpFiles = new File(logPath); + if(tmpFiles!=null&&tmpFiles.isAbsolute()&&tmpFiles.isDirectory()) { + logger.info("删除日志文件开始"); + int delLogFilesNum = deleteFilesFrom(logPath,"日志"); + logger.info("删除日志文件结束,共删除"+delLogFilesNum+"个文件"); + } + } + } + + //删除download里的文件 + String downloadPath = Constants.MISSION_FILE_DOWNLOAD_DIR; + if(StringUtils.isNotBlank(downloadPath)) { + logger.info("删除download文件开始"); + int delDownloadFilesNum = deleteFilesFrom(downloadPath,"download"); + logger.info("删除download文件结束,共删除"+delDownloadFilesNum+"个文件"); + } + + //删除error里的detect里的文件,按文件名进行删除 + String detectPath = Constants.ERROR_DETEC_FILE_DIR; + if(StringUtils.isNotBlank(detectPath)) { + logger.info("删除detect文件开始"); + int delDetectFilesNum = deleteFilesFromByFileName(detectPath,"detect"); + logger.info("删除detect文件结束,共删除"+delDetectFilesNum+"个文件夹"); + } + + } catch (Exception e) { + logger.error("Regularly delete files (log, error->detect, download) exceptions",e); + } + } + + //pathNam:要删除的文件所在的路径 + private int deleteFilesFrom(String pathName,String tip) throws Exception{ + File tmpFiles = new File(pathName); + int deleteFileNum = 0; + if(tmpFiles!=null&&tmpFiles.isAbsolute()&&tmpFiles.isDirectory()) { + logger.info(tip+"文件所在路径:"+pathName); + + if(tmpFiles.exists()) { +// File[] filesTmp = FileUtils.listAllFilesEndWith(logFiles, "txt"); + File[] filesTmp = tmpFiles.listFiles(); + if(filesTmp!=null&&filesTmp.length>0) { + Date now = new Date(); + Calendar nowCal = Calendar.getInstance(); + nowCal.setTime(now); + + nowCal.set(Calendar.HOUR, nowCal.get(Calendar.HOUR)-Constants.KEEP_FILE_DAYS*24); +// nowCal.set(Calendar.MINUTE, nowCal.get(Calendar.MINUTE)-1);//测试时使用 + for(File f:filesTmp) { + Calendar lastModifyTime = FileUtils.getModifiedTime(f); + if(nowCal.compareTo(lastModifyTime)>0) { +// boolean isDel = f.delete();//删除文件夹时,文件夹必须是空的才可以 + FileUtils.deleteAllFiles(f,true); +// if(isDel) { + deleteFileNum++; +// } + } + } + } + } + } + return deleteFileNum; + } + + private int deleteFilesFromByFileName(String pathName,String tip) throws Exception{ + File tmpFiles = new File(pathName); + int deleteFileNum = 0; + if(tmpFiles!=null&&tmpFiles.isAbsolute()&&tmpFiles.isDirectory()) { + logger.info(tip+"文件所在路径:"+pathName); + + if(tmpFiles.exists()) { +// File[] filesTmp = FileUtils.listAllFilesEndWith(logFiles, "txt"); + File[] filesTmp = tmpFiles.listFiles(); + if(filesTmp!=null&&filesTmp.length>0) { + Date now = new Date(); + Calendar nowCal = Calendar.getInstance(); + nowCal.setTime(now); + nowCal.set(Calendar.HOUR, nowCal.get(Calendar.HOUR)-Constants.KEEP_FILE_DAYS*24); +// nowCal.set(Calendar.MINUTE, nowCal.get(Calendar.MINUTE)-5);//测试时使用 + for(File f:filesTmp) { +// Calendar lastModifyTime = FileUtils.getModifiedTime(f); + String fileName = f.getName(); + SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); + try + { + Date date = format.parse(fileName); + Calendar lastModifyTime = Calendar.getInstance(); + lastModifyTime.setTime(date); + if(nowCal.compareTo(lastModifyTime)>0) { + FileUtils.deleteAllFiles(f,true);//删除文件夹时,文件夹必须是空的才可以 + deleteFileNum++; + } + } catch (ParseException e) + { + logger.info("删除 detect文件时,解析"+pathName+"路径下的文件夹名称,转换为日期形式 异常"); + } + } + } + } + } + return deleteFileNum; + } + + public static void main(String [] args){ + new Thread(new DeleteFilesThread()).start(); + + } + +} diff --git a/src/com/nms/server/thread/detecData/DetecDataResoveManagerThread.java b/src/com/nms/server/thread/detecData/DetecDataResoveManagerThread.java new file mode 100644 index 0000000..aa45b4f --- /dev/null +++ b/src/com/nms/server/thread/detecData/DetecDataResoveManagerThread.java @@ -0,0 +1,170 @@ +package com.nms.server.thread.detecData; + +import java.util.Calendar; +import java.util.Date; +import java.util.LinkedList; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.FileUtils; + +public class DetecDataResoveManagerThread implements Runnable { + private Logger logger = Logger.getLogger(DetecDataResoveManagerThread.class); + private int testNum = 0;// 尝试运行线程Constants.DETEC_DATA_RESOVE的次数 + public void run() { + + // 将线程运行程序,尽可能的catch捕获异常 +// Thread.currentThread().setName("监测数据解析管理线程"); + Thread.currentThread().setName("Monitoring Data Parsing Management Thread"); + + try { + // 如果内存中监测数据列表的数据超过指定值,则将数据存入硬盘(10万) + int detectDataNum = Common.getAllDeteDataNum(); + if (detectDataNum >= Constants.MAX_DETECT_DATA_RESOVE_LIMIT_NUM) { + logger.warn("Excesive monitoring data:"+detectDataNum+"pieces, data written to the hard disk"); + saveDetectDataToDisk(); + return; + } + // dc监测数据入库模式,1:由web 主控控制入库,2:自己主动入库,3:智能模式(1和2结合),4,写入文件,定时上传web端 + if (Constants.DETECT_INSERT_MODE == 1) { + logger.debug("DETECT_INSERT_MODE = " + Constants.DETECT_INSERT_MODE + ",不启动监测数据入库线程"); + return; + } else if (Constants.DETECT_INSERT_MODE == 2) { + // 2:自己主动入库 + logger.debug("DETECT_INSERT_MODE = " + Constants.DETECT_INSERT_MODE + ",主动入库"); + modeTwo(); + } else if (Constants.DETECT_INSERT_MODE == 3) { + // 3:智能模式(1和2结合) + long last = Common.getLastWebHandshake(); + long n = System.currentTimeMillis(); + if((n-last)/Constants.WEB_NOTICE_INSERT_DETECT_OVER_TIMES > Constants.DETEC_DATA_RESOLVE_PERIOD*1000){ + //超过两个监测周期都没有 web 握手信息 + logger.debug("超过两个周期都没有web握手信息,dc 主动入库,lastTime : " + new Date(last).toLocaleString()); + modeTwo(); + }else{ + logger.debug("等待web 通知监测数据入库,lastTime : " + new Date(last).toLocaleString()); + } + } else if (Constants.DETECT_INSERT_MODE == 4) { + // 4,写入文件,定时上传web端,暂不实现 + + } + } catch (Exception e) { + logger.error("", e); + } finally { + logger.debug("执行结束"); + } + } + + /** + * mode :2 自己主动入库,查询 数据库 server_table 表共多少有效 dc ,根据自己排序,监测入库周期时间,计算 + * 在哪一个时间段入库 + */ + public void modeTwo() { + // 测试数据库连接 + boolean runFlag = Common.isDbConnected(); + + // - 检查线程运行状态 运行中无操作 + Future future = Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE); + if (future != null && !future.isCancelled() && !future.isDone() && runFlag) { // 运行中 + runFlag = false; + logger.info("监测数据解析执行线程 运行中 不再启动新解析线程"); + } + /* + * 连续超过Constants.CHECK_WARNING_DATA_OVERRUN次数时,将数据存入硬盘中 否则 正常执行数据解析线程 + */ + if (!runFlag) { // 判断并结束线程 + testNum = (testNum % Constants.CHECK_DETEC_DATA_OVERRUN); + testNum++; + if (testNum == Constants.CHECK_DETEC_DATA_OVERRUN) { + if (Constants.FLAG_DETECT_DATA_SAVE_DISK_RESOVE == 1) { + /* + * 连续多个周期仍未解析完成 说明list里存有大量的数据,则先将当前未解析的数据列表存入硬盘, + * 然后停掉解析线程,将正在解析的数据列表中的剩余数据存入硬盘 + */ + logger.warn("Failed to create database connection,data written to hard disk"); + saveDetectDataToDisk(); + } + } + return; + } else { // 重置计数器 程序继续执行 + testNum = 1; + } + + // - 非升级操作判断 + if (Common.SERVER_UN_UPGRADE_FLAG) { + + // - 为空或空闲中 变更存储区 启动新解析线程 + // Common.chengeDeteDataFlag(); //变更数据存放集合 + // 2018年6月14日11:04:57 根据集合的大小自动切换取缓存数据最多的 + LinkedList dsbList = Common.getDeteDataList(); // 获取非存放状态的数据集合 + if (dsbList != null && dsbList.size() > 0) { + logger.info("监测数据解析执行线程 空闲中 启动新解析线程"); + // -- 获取线程执行 需进行主动告警和邮件通知等相关操作,待考虑 + future = Common.dataResoveService.submit(new NewDetecDataResoveThread("Monitoring Data Parse Perform Thread",dsbList)); + // 注册 + Common.getFutureMap().put(Constants.DETEC_DATA_RESOVE, future); + } else { + logger.info("获取到的集合中数据数量为0,无需启动解析线程"); + } + } + } + + // 2013-2-19 hyx :当监测数据量过大时,将数据先存放到硬盘,再定时入库(由单独的定时线程实现) + public void saveDetectDataToDisk() { + LinkedList notResolvingDetectDataList = Common.getNotResovlingDeteDataList(); // 获取未在解析的监测数据集合 + try { + // 终止正在解析监测数据的线程,并将正在解析的list里未解析的数据存入硬盘 + Future future = Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE); + if (future != null && !future.isCancelled() && !future.isDone()) { // 运行中 + future.cancel(true); + } + LinkedList detectDataList = Common.getDeteDataList(); // 获取非存放状态的数据集合(正在解析的list) + saveByteToFile(detectDataList); + + // 将未在解析的list存放到硬盘上 + saveByteToFile(notResolvingDetectDataList); + + } catch (IndexOutOfBoundsException e) { + logger.error("When there is too much monitoring data ,the data is stored on the hard disk exception",e); + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + + } + + private void saveByteToFile(LinkedList dataList) { + String filePath = ""; + int dataSize = dataList.size();// 如果不按此时的size来操作,则list里的数据可能会一直在增加,就会一直循环下去? + logger.info("监测数据过多,共" + dataSize + "条监测数据,等待存入硬盘:" + Constants.OVERRUN_DETEC_FILE_DIR + "/" + + Common.getDateDirName() + "/"); + try { + for (int j = 0; j < dataSize; j++) { + if (dataList.size() > 0) { + byte[] data = dataList.get(0);// 因为每次循环都会dataList.remove(0);所以每次操作的都是第0条记录 + filePath = Constants.OVERRUN_DETEC_FILE_DIR + "/" + Common.getDateDirName() + "/" + + Calendar.getInstance().getTimeInMillis() + "_" + j + ".csv"; + FileUtils.wirteBytesToFile(data, filePath); + dataList.remove(0); + } + } + + } catch (Exception e) { + logger.error("When there is too much monitoring data ,the data is stored on the hard disk exception",e); + } + } + + public static void main(String[] args) { + Common.scheduled.scheduleAtFixedRate(new DetecDataResoveManagerThread(), 0, 50, TimeUnit.SECONDS); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.exit(0); + } +} diff --git a/src/com/nms/server/thread/detecData/DetecDataResoveThread.java b/src/com/nms/server/thread/detecData/DetecDataResoveThread.java new file mode 100644 index 0000000..b237c85 --- /dev/null +++ b/src/com/nms/server/thread/detecData/DetecDataResoveThread.java @@ -0,0 +1,1217 @@ +package com.nms.server.thread.detecData; + +import java.io.File; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Comparator; +import java.util.ConcurrentModificationException; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Callable; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.EmailInfo; +import com.nms.server.bean.TableColumnsModel; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.common.EmailTypeConstants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.CommonService; +import com.nms.server.util.CSVUtils; +import com.nms.server.util.FileUtils; +import com.nms.server.util.StringUtil; + +/** + * CSV解析线程 + * @author ZGGG3 + * + */ +public class DetecDataResoveThread implements Callable { + Logger logger = Logger.getLogger(DetecDataResoveThread.class); + volatile boolean stop = false;//线程是否被取消标志 + private String name; // 自定义Thread Name + private LinkedList dsbList; + private SimpleDateFormat format2 = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); //Java Date 类型数据格式化格式 + + private long infoId = -1; + private long currentId = -1; + private long interval = -1; + private int warnResumeNum = 0; + public DetecDataResoveThread(String name,LinkedList dsbList) { + this.name = name; + this.dsbList = dsbList; + } + + /* + * 线程操作 + * + * 依次解析urlList中仍存在的Files + * + * 实现了依次解析Files,由于后期实现多线程解析操作 + */ + public Object call() { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name); + logger.info("配置文件设置批量入库监测数据条数:"+ Constants.DETECTION_INFO_DATA_MAX_ROWS); + CommonDao dao = null; + try { + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + //-- 空数据集合 结束操作 + if(dsbList == null || dsbList.size() == 0 ){ + logger.debug("缓存监测数据 为空,无需解析"); + return null; + } + + //用于判断网络端口 + long eNewTime = System.currentTimeMillis(); + Map> allPortName= service.getAllPortName();//之后如果数据大了,影响性能了,可以改成获取一个seqid的数据和一个端口的list,查询seqid,得到下标,再从list中获取信息,目前先暂时不改 + long portTime = System.currentTimeMillis()-eNewTime; + int portTimeM=(int)portTime/1000/60; + int portTimeS=(int)((long)(portTime/1000)%60); + int portTimeMS=(int)portTime%1000; + logger.info("第零步:获取端口表的所有数据,总耗时:"+(portTimeM)+"分 "+(portTimeS)+"秒 "+(portTimeMS)+"毫秒"); + + long sTime = System.currentTimeMillis(); + logger.debug("缓存监测数据 " + (dsbList.size())+ "条,解析开始"); + + //存放解析之后的数据:[["systemdate",["5",["1602",[{detectInfo1,detectInfo2,...}]],...],...],...] + List> dataList = new LinkedList>(); + //-- 遍历解析 + Iterator dsbIte = dsbList.iterator(); + int totalCount = dsbList.size(); + int successCount=0; + int falseCount = 0; + int zeroByteCount = 0; + /* + * 第一步:解析所有数据 + * 新格式数据,旧格式同一放入 newDataMap 集合 + */ + while (dsbIte.hasNext()&& !stop) {//线程未被中断 + /* 单条解析异常捕捉处理 */ + byte [] dsb = null; + try { + dsb = dsbIte.next(); + /* 零字节文件记录和过滤 */ + if (dsb != null && dsb.length > 0) { + /* 解析非0字节文件内容 */ + List strsList = CSVUtils.csvBytesParser(dsb,Constants.COMMON_TEXT_CODING); + /* 新旧解析格式判断 */ + /** + * 新旧解析格式区别: + * 区别:第一行基本信息数据 新格式12列固定比旧格式至少15列 + * */ + if(strsList!=null && strsList.size()>0){ //多行数据判断 + Object[] detecInfo = null; + String [] detailsStr = strsList.get(0); //取第一行数据 + + if(detailsStr != null){ + if(detailsStr.length > 12){//旧格式 解析 + logger.debug("旧格式数据"); + detecInfo = service.resoveOldData(strsList,allPortName);//解析数据,不判断状态变更 + }else if(detailsStr.length<12){ + logger.debug( "基本数据解析长度:"+detailsStr.length+" 小于最小解析标准,不予解析;"); + }else{//新格式 解析 + logger.debug("新格式数据"); + detecInfo = service.resoveNewData(strsList,allPortName);//解析数据,不判断状态变更 + } + } + + //将解析之后的数据放入集合,不为空说明解析成功 + if(detecInfo != null){ + successCount++;//成功解析 + detecAddToList(dataList, detecInfo); + }else{ + falseCount ++;//解析失败 + String filePath = getFilePath();//Constants.ERROR_DETEC_FILE_DIR+"/"+Common.getDateDirName()+"/"+Calendar.getInstance().getTimeInMillis()+".csv"; + logger.error("The format of the monitoring data is exceptions, and the file has been saved to"+filePath); + FileUtils.wirteBytesToFile(dsb, filePath); + } + } + }else{ + zeroByteCount++; + } + + //解析入库一条数据,从list中remove一条数据 + dsbIte.remove(); + //判断当前线程是否被中断 + if (Thread.currentThread().isInterrupted()) { + // 线程中断状态,不会改变中断状态的值 + logger.info("监测数据解析线程 被中断"); + stop = true;//如果中断执行(重启、或者升级),就停止解析数据,之后将数据入库 + } + }catch (InterruptedException e) { + logger.error("Monitoring data parsing threads are interrupted",e); + stop = true; + + }catch (ConcurrentModificationException e) { + logger.debug("获取下一条监测数据 异常 停止本轮解析!",e); + stop = true;//由于获取next的时候出错,之后的都出错了就,所以此处stop=true,停止循环 + }catch (Exception e) { + logger.debug("一条监测数据解析失败!",e); + } + } + dsbList.clear(); + long resoveCurTime = System.currentTimeMillis(); + long resoveTime = resoveCurTime-sTime; + int resoveTimeM=(int)resoveTime/1000/60; + int resoveTimeS=(int)((long)(resoveTime/1000)%60); + int resoveTimeMS=(int)resoveTime%1000; + logger.info("第一步:解析完毕,共:"+(totalCount)+"条[成功:"+successCount+"条,失败:"+falseCount+"条,空数据:"+zeroByteCount+"条]" + +" 总耗时:"+(resoveTimeM)+"分 "+(resoveTimeS)+"秒 "+(resoveTimeMS)+"毫秒"); + /** + *打印统计的监测数据:监测类别--监测设置--节点--监测信息 + */ +// printDetectDataInfo(newDataMap); + + + /** + * 第二步:判断监测状态变更 + */ + + if(dataList != null && dataList.size() >0){ + for(List checkTypeList : dataList){ + int checkTypeSize = checkTypeList.size(); + for(int i = 1,j =checkTypeSize; i < j; i ++){ + List setIdList = (List) checkTypeList.get(i); + String setId = (String) setIdList.get(0);//监测设置id + + List detectionInfoNewData = null; + List seqList = new ArrayList(); + //获取new表中对应监测设置的监测数据 + detectionInfoNewData = service.getDetectionInfoNewListBySet(setId,seqList); + int setIdSize = setIdList.size(); + for(int ii = 1,jj = setIdSize; ii < jj ; ii ++){ + List seqIdList = (List) setIdList.get(ii); + String seqId = (String) seqIdList.get(0);//seqId + List allDeteInfo = (List) seqIdList.get(1); + if(allDeteInfo != null && allDeteInfo.size() > 0){ + //detection_info_new中的 最新监测数据 + Object[] oldDetecInfo = null; + if(detectionInfoNewData != null){//detectionInfoNewData:监测设置--节点--监测信息 + oldDetecInfo = service.getIndexObj(seqList, seqId,detectionInfoNewData); + }else{ + oldDetecInfo = null; + } + //数据库中没有当前监测类别对应的seqid节点的监测信息,不需要判断状态变更 + if(oldDetecInfo != null){ + if(allDeteInfo.size() <2 ){//只有一条数据直接比较数据库中的数据 + //只有一条最新监测 + Object[] newDetecInfo = allDeteInfo.get(0); + + int state = (Integer) newDetecInfo[DetectInfo.STATE]; + /* + * 入库数据不是延时数据时,与数据库中的最新数据比对判断状态变更 + * 数据库中的监测时间小于当前需要入库的监测时间 + */ + if((Long)oldDetecInfo[DetectInfo.CHECKTIME] < (Long)newDetecInfo[DetectInfo.CHECKTIME]){ + /*判断状态变更*/ + stateChange(newDetecInfo, oldDetecInfo); + }else{//监测时间小于数据库中监测数据的时间,说明当前需要入库的监测为延迟数据 + if(state != 1){//延迟数据监测状态没有成功,数据库入库的是正常数据,补录一条报警信息 + if((Integer) newDetecInfo[DetectInfo.STATE] == 1 && (Integer)oldDetecInfo[DetectInfo.PLEVEL] == 99){ + newDetecInfo[DetectInfo.APPENDWARNINGINFO]= oldDetecInfo; + warnResumeNum++; + } + } + } + }else{//大于两条数据时,排序之后判断数据库的数据是否为最接近的时间数据 + //未入库的数据排序 + Collections.sort(allDeteInfo, new Comparator() { + @Override + public int compare(Object[] o1, + Object[] o2) { + /*Long time1 = o1.getCheckTime(); + Long time2 = o2.getCheckTime();*/ + Long time1 = (Long) o1[DetectInfo.CHECKTIME]; + Long time2 = (Long) o2[DetectInfo.CHECKTIME]; + return time1 < time2 ? -1 : 1; + } + }); + /*判断状态变更*/ + judgeState(allDeteInfo, oldDetecInfo); + } + } + } + } + //清理内存 + detectionInfoNewData.clear(); + detectionInfoNewData = null; + } + } + } + + long stateCurTime = System.currentTimeMillis(); + long stateCheckTime = stateCurTime-resoveCurTime; + int stateCheckM=(int)stateCheckTime/1000/60; + int stateCheckS=(int)((long)(stateCheckTime/1000)%60); + int stateCheck=(int)stateCheckTime%1000; + logger.info("第二步:判断监测状态变更完毕(只针对解析成功的),共:"+(successCount)+"条 总耗时:"+(stateCheckM)+"分 "+(stateCheckS)+"秒 "+(stateCheck)+"毫秒"); + logger.info(" 第二步-1: 补录告警恢复告警信息:"+warnResumeNum+"条"); + + + /** + * 第三步:批量入库 + */ + + //判断入库标示 + if(Constants.FLAG_RESOVE_COMMIT_DB != 1){ + String s = "file.resove.commitDB.flag 不为1,数据不可保存,请检查配置文件"; + logger.warn(s); + return null; + } + int failBatchInsert=0; + String failBatchInsertCheckType = "";//批量入库失败的监测类别信息 + if(dataList != null && dataList.size() >0){ + int infoCount = 0; + int detailCount = 0; + int sysinfoCount = 0; + int warnningCount = 0; + int statusChange = 0; + int emailCount = 0; + //监测设置对应的联系人email地址 + Map> setInfoEmail = null; + //未指定的监测设置通过seqid找到对应的联系人 + Map> seqIdEmail = null; + List logList = new ArrayList(); + dao.setAutoCommit(false); + LinkedList dsbBatchList = new LinkedList();//用于批量入库后是否将监测数据存入硬盘的overrun :每次入库,或者存硬盘都需要清空此列表 + + //迭代器遍历,方便删除数据 + Iterator> iteDataList = dataList.iterator(); + while(iteDataList.hasNext()){ + List checkTypeList = iteDataList.next();//["cpu",["1",["1602",[{detecAllInfo}]]]], + String checkType = (String) checkTypeList.get(0);//监测类型 + + int checkTypeRecordNum=0;//每种监测类别入库的记录数 + String insertDetailSql = null;//插入详细参数 + List fieldNames = new ArrayList();//detail表的字段名 + //详细信息对应的表名 + String detailTableName = Common.getInsertTable().get(checkType) == null ? null : Common.getInsertTable().get(checkType).getTableName(); + if(detailTableName == null){ + logger.error(checkType + "the corresponding detailed table was not found and the data could not be stored"); + continue; + } + String seqId = null; + + Iterator checkIte = checkTypeList.iterator(); + boolean firstKey1 = true; + while(checkIte.hasNext()){ + if(firstKey1){ + checkIte.next(); + firstKey1 = false; + }else{ + List setIdList = (List) checkIte.next();//["1",["1602",[{detecAllInfo}]]] + String setInfoId = (String) setIdList.get(0);//监测设置id + Iterator setIte = setIdList.iterator(); + boolean firstKey2 = true; + while(setIte.hasNext()){ + if(firstKey2){ + firstKey2 = false; + setIte.next(); + }else{ + List seqIdList = (List) setIte.next();//["1602",[{detecAllInfo,detecAllInfo}]] + seqId = (String) seqIdList.get(0);//监测设置id + List detecDatas = (List) seqIdList.get(1);//{detecAllInfo,detecAllInfo} + Iterator detecIte = detecDatas.iterator(); + + long statusChangeTime = -1;//状态变更时间 + while(detecIte.hasNext()){ + Object[] temInfo = detecIte.next(); + //将解析之后的数据还原 + byte[] dsb = objectToString(temInfo); + //将还原之后的数据缓存,出现异常时保存到文件 + dsbBatchList.add(dsb); + if(temInfo != null){ + currentId = getSequenceId(service); + //addbatch + service.addInfoStmt(currentId,temInfo); + infoCount ++; + logger.debug(" detection_info表id:" + currentId + ",监测类别:" + checkType +",监测设置id:" + setInfoId + ",seqId:" + seqId); + //添加详细信息 + if((Boolean) temInfo[DetectInfo.DELYFLAG]){ + //当delyFlag 为false时 不插入详细信息 + List> details = (List>) temInfo[DetectInfo.DETAILS]; + if(details!= null && details.size()>0 ){ + for(Map tempMap :details){ + //首先组织sql语句 + if(insertDetailSql == null || "".equals(insertDetailSql)){ + //组织detail表的insert语句 + insertDetailSql = createDetailSql(fieldNames,detailTableName,tempMap); + logger.debug("监测设置id: " + setInfoId +" ,detail表插入语句:" + insertDetailSql); + } + dao.setDetailVals(insertDetailSql,fieldNames,tempMap,currentId,detailTableName); + checkTypeRecordNum++; + detailCount ++; + } + } + } + //特殊格式追加信息 + if(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType) ){ + List diskInfoList = (List) temInfo[DetectInfo.DISKINFOLIST]; + List netInfoList = (List) temInfo[DetectInfo.NETINFOLIST]; + if(diskInfoList != null && diskInfoList.size() > 0){//disk + for(String[] temDiskInfo : diskInfoList){ + if(temDiskInfo != null && temDiskInfo.length >7){ + service.addDiskStmt(currentId,temDiskInfo); + sysinfoCount ++; + } + } + } + if(netInfoList != null && netInfoList.size() > 0){//net + for(String[] temNetInfo : netInfoList){ + if(temNetInfo != null && temNetInfo.length >7){ + service.addNetStmt(currentId,temNetInfo); + sysinfoCount ++; + } + } + } + } + + //添加告警信息:告警信息=状态异常+状态改变+推迟入库的 + if((Integer)temInfo[DetectInfo.STATE] != 1 || (Boolean)temInfo[DetectInfo.STATECHANGEFLAG]){ + service.addWarningStmt(currentId +"",temInfo); + logList.addAll(generateLog(dsb," 第三步-1: 添加一条告警信息,状态发生变更或者数据异常!state="+temInfo[DetectInfo.STATE]+",isChange="+(Boolean)temInfo[DetectInfo.STATECHANGEFLAG])); + warnningCount ++; + } + //延迟监测数据追加告警信息 + if(temInfo[DetectInfo.APPENDWARNINGINFO] != null){ + Object[] warningInfo = (Object[]) temInfo[DetectInfo.APPENDWARNINGINFO]; + service.addWarningStmt((String)warningInfo[DetectInfo.DETECTIONINFOID],warningInfo); + logList.addAll(generateLog(dsb," 第三步-1: 追加一条延迟数据的告警信息!")); + warnningCount ++; + } + //添加邮件信息 + if(Constants.flag_email != 1){ + logger.info("邮件功能已关闭"); + }else{ + if((Boolean) temInfo[DetectInfo.SENDEMAILFLAG]){ + EmailInfo emailInfo = (EmailInfo) temInfo[DetectInfo.EMAILINFO]; + if(emailInfo != null){ + if(setInfoEmail == null){ + setInfoEmail = service.getDetecSetEmailListOf123(); + } + if(seqIdEmail == null){ + seqIdEmail = service.getDetecEmailListOf4(); + } + if((setInfoEmail == null || setInfoEmail.size() == 0) && (seqIdEmail == null || seqIdEmail.size() == 0)){ + logger.debug("未找到邮件地址!"); + }else{ + List emailAddress = null; + //1.首先通过监测设置的map中查找 + if(setInfoEmail != null){ + emailAddress = setInfoEmail.get(setInfoId); + } + if(emailAddress == null){ + emailAddress = seqIdEmail.get(seqId); + } + if(emailAddress != null && emailAddress.size() >0){ + for(String tempAddress : emailAddress){ + if(StringUtils.isNotEmpty(tempAddress)){ + emailCount ++; + //addbatch + service.addEmailStmt(emailInfo,tempAddress); + } + } + } + } + } + } + } + + /*判断状态变更时间的最大时间,用于更新new表 + 当监测类型为 nmsclient 时,比较多条相同监测节点数据的状态变更时间,取最大的一个 + */ + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + long temChangeTime = (Long) temInfo[DetectInfo.STATUSCHANGTIME]; + if(temChangeTime > 0 ){ + if(temChangeTime > statusChangeTime){ + statusChangeTime = temChangeTime; + } + } + } + } + //dsbBatchList.add((byte[]) temInfo[27]); + //判断infoStmt的数量是否达到最大入库条数 + if(infoCount != 0 && infoCount % Constants.DETECTION_INFO_DATA_MAX_ROWS == 0){ + int dataLen = dsbBatchList.size(); + try { + //执行批处理 + dao.executeBatchAllStmt(false); + //提交事务 + dao.commit(); + } catch (SQLException e){ + dao.rollback(); + handleException(dsbBatchList, e); + failBatchInsert = failBatchInsert + dataLen; + failBatchInsertCheckType = failBatchInsertCheckType + checkType+" "; + }catch (Exception e) { + logger.error("Batch save failed, database error, total:"+dataLen,e); + dao.rollback(); + + //批量入库失败以后,要将失败的监测数据存到硬盘的overrun里,以备下次进行再次入库 + Common.saveByteToFile(dsbBatchList); + failBatchInsert = failBatchInsert + dataLen; + failBatchInsertCheckType = failBatchInsertCheckType + checkType+" "; + } + dsbBatchList.clear(); + } + detecIte.remove();//删除,内存优化 + } + /* + * 更新状态变更时间 + * 只有当监测类型:nmsclient 时,statusChangeTime才会大于0 + */ + if(statusChangeTime > 0){ + service.addStateChangeStmt(statusChangeTime,seqId); + statusChange++; + logger.info("状态变更详细信息:seqId="+seqId+" changeTime="+statusChangeTime); + } + } + setIte.remove();//删除监测设置 + } + } + checkIte.remove();//删除监测设置,内存优化 + } + iteDataList.remove();//删除监测类型,内存优化 + + int dataLastLen = dsbBatchList.size(); + try{ + //执行批处理 + dao.executeBatchAllStmt(true); + //提交事务 + dao.commit(); + }catch (SQLException e){ + dao.rollback(); + handleException(dsbBatchList, e); + failBatchInsert = failBatchInsert + dataLastLen; + failBatchInsertCheckType = failBatchInsertCheckType + checkType+" "; + }catch(Exception e){ + logger.error("Batch save failed, database error, total:"+dsbBatchList.size(),e); + dao.rollback(); + //批量入库失败以后,要将失败的监测数据存到硬盘的overrun里,以备下次进行再次入库 + logger.error("Monitoring data batch storage, database error, the file has been saved to the hard disk."); + Common.saveByteToFile(dsbBatchList); + failBatchInsert = failBatchInsert + dataLastLen; + failBatchInsertCheckType = failBatchInsertCheckType + checkType+" "; + }finally{ + //关闭stmt:此处逻辑不太顺,最后会有一个对dao的整体的关闭,但是本线程中使用的都是自己定义的preparedstatement,不够统一 + dao.closeDetailBatchStatement(); + dsbBatchList.clear(); + } + logList.add(" 第三步-1: 监测类型:" + checkType +" ,批量入库结束,记录数: "+checkTypeRecordNum); + + } + long endTime = System.currentTimeMillis(); + long curTime = endTime-stateCurTime; + int m=(int)curTime/1000/60; + int s=(int)((long)(curTime/1000)%60); + int ms=(int)curTime%1000; + logList.add(" 第三步-1: 共"+dataList.size()+" 种详细信息 " ); + logList.add(" 第三步-1: new表状态变更 共"+statusChange+" 条 " ); + logger.info("第三步:批量保存入库完毕(只针对解析成功的),共:"+(successCount)+"条[共插入detection_info表: " + infoCount +" 条,detail表: " + detailCount +" 条,特殊追加信息: " + sysinfoCount +" 条,warning 表: " +warnningCount +" 条。]" + +" 总耗时:"+(m)+"分 "+(s)+"秒 "+(ms)+"毫秒,批量入库失败记录数:"+failBatchInsert+" 具体"+failBatchInsertCheckType); + printLogs(logList); + } + + + + //将监测数据入库后,需要检查新入库的数据是否为无效(处理监测数据的有效性标志) + long startUpdateTime = System.currentTimeMillis(); + updateDetectDataValid(dao); + long updateCurTime = System.currentTimeMillis()-startUpdateTime; + int updateCurTimeM=(int)updateCurTime/1000/60; + int updateCurTimeS=(int)((long)(updateCurTime/1000)%60); + int updateCurTimeMS=(int)updateCurTime%1000; + logger.info("第四步:更新监测数据有效性完毕,总耗时:"+(updateCurTimeM)+"分 "+(updateCurTimeS)+"秒 "+(updateCurTimeMS)+"毫秒"); + + long eTime = System.currentTimeMillis(); + long curTime = eTime-sTime; + int m=(int)curTime/1000/60; + int s=(int)((long)(curTime/1000)%60); + int ms=(int)curTime%1000; + logger.info("第一到四步,整体解析批量入库监测数据全过程执行完毕,共:"+(totalCount)+"条[成功:"+successCount+"条,失败:"+falseCount+"条,空数据:"+zeroByteCount+"条]" + +" 总耗时:"+(m)+"分 "+(s)+"秒 "+(ms)+"毫秒,批量入库失败记录数:"+failBatchInsert+" 具体"+failBatchInsertCheckType); + } catch (Exception e) { + logger.error("Running exception",e); + }finally{ + dao.closeCommonBatchStatement(); + dao.closeDetailBatchStatement(); + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("数据清理 解析完成"); + } + return null; + } + + public void handleException(LinkedList dsbBatchList, + SQLException e) { + try + { + //如果数据过错误是:1、ORA-01861:文字与格式字符串不匹配 + // (一般情况下) 2、ORA-01400:不能将空值插入 3、ORA-01722:无效数字 + // 4、ORA-00351:时间无效 + // 5、ORA-01401: 插入的值过大 + // 属于文件内容的错误所以保存到 错误文件夹中 + + int size = dsbBatchList.size(); + if(e.getMessage().indexOf("ORA-01722")>=0 + || e.getMessage().indexOf("ORA-01400")>=0 + || e.getMessage().indexOf("ORA-01401")>=0 + || e.getMessage().indexOf("ORA-01861")>=0 + || e.getMessage().indexOf("ORA-00351")>=0){ + logger.error("Monitoring data batch storage: abnormal monitoring data format, a total of"+size+"files, to save to"+getParentDirPath(),e); + for(int j=0;j> dataList,Object[] detecInfo){ + String checkType = (String) detecInfo[DetectInfo.CHECKTYPE]; + String setInfoId = (String) detecInfo[DetectInfo.SETINFOID]; + String seqId = (String) detecInfo[DetectInfo.SEQID]; + /** + * 设置默认值 + */ + if(detecInfo[DetectInfo.STATE] == null){ + detecInfo[DetectInfo.STATE] = -1;//state + } + if(detecInfo[DetectInfo.PLEVEL] == null){ + detecInfo[DetectInfo.PLEVEL] = 99;//pLevel + } + if(detecInfo[DetectInfo.DELYFLAG] == null){ + detecInfo[DetectInfo.DELYFLAG] = false;//delyFlag + } + if(detecInfo[DetectInfo.URGENTLEVEL] == null){ + detecInfo[DetectInfo.URGENTLEVEL] = 0;//urgentLevel + } + if(detecInfo[DetectInfo.SENDEMAILFLAG] == null){ + detecInfo[DetectInfo.SENDEMAILFLAG] = false;//sendEmailFlag + } + if(detecInfo[DetectInfo.STATECHANGEFLAG] == null){ + detecInfo[DetectInfo.STATECHANGEFLAG] = false;//stateChangeFlag + } + if(detecInfo[DetectInfo.STATUSCHANGTIME] == null){ + detecInfo[DetectInfo.STATUSCHANGTIME] = -1L;//statusChangeTime + } + + if(dataList.size() == 0){ + List detecList = new LinkedList(); + detecList.add(detecInfo); + List seqList = new LinkedList();//seqid list + seqList.add(seqId); + seqList.add(detecList); + List setIdList = new LinkedList();//监测设置 id list + setIdList.add(detecInfo[DetectInfo.SETINFOID]); + setIdList.add(seqList); + List checkTypeList = new LinkedList(); + checkTypeList.add(checkType); + checkTypeList.add(setIdList); + dataList.add(checkTypeList); + }else{ + //第一层 checkType + for(int i = 0 , j = dataList.size(); i < j ; i ++){ + List tempObj = dataList.get(i); + String key = (String)tempObj.get(0);//第一个为key + if(checkType.equals(key)){//监测类型已存在 + for(int ii = 1 , jj = tempObj.size(); ii < jj ; ii ++){ + List tempSet = (LinkedList) tempObj.get(ii); + String setKey = (String) tempSet.get(0);//监测设置id + if(setInfoId.equals(setKey)){ + for(int iii = 1 ,jjj = tempSet.size(); iii < jjj; iii ++){ + List tempSeq = (LinkedList) tempSet.get(iii); + String seqKey = (String) tempSeq.get(0);//监测设置id + if(seqId.equals(seqKey)){//seqId 存在 + List detecList = (List) tempSeq.get(1); + detecList.add(detecInfo); + break; + }else if(iii == jjj -1){//seqid 不存在 + List detecList = new LinkedList(); + detecList.add(detecInfo); + List seqList = new LinkedList();//seqid list + seqList.add(seqId); + seqList.add(detecList); + tempSet.add(seqList); + } + } + break; + }else if(ii == jj-1){//监测设置id不存在 + List detecList = new LinkedList(); + detecList.add(detecInfo); + List seqList = new LinkedList();//seqid list + seqList.add(seqId); + seqList.add(detecList); + List setIdList = new LinkedList();//监测设置 id list + setIdList.add(detecInfo[DetectInfo.SETINFOID]); + setIdList.add(seqList); + tempObj.add(setIdList); + } + } + break;//跳出循环 + }else if(i == j-1){//不存在 + List detecList = new LinkedList(); + detecList.add(detecInfo); + List seqList = new LinkedList();//seqid list + seqList.add(seqId); + seqList.add(detecList); + List setIdList = new LinkedList();//监测设置 id list + setIdList.add(detecInfo[DetectInfo.SETINFOID]); + setIdList.add(seqList); + List checkTypeList = new LinkedList(); + checkTypeList.add(checkType); + checkTypeList.add(setIdList); + dataList.add(checkTypeList); + } + } + } + } + + + /** + * 将解析之后的对象添加到map中 + * @param newDataMap:checkType:setId:seqId:detectInfo + * @param detecInfo + */ + public void detecAddToMap(Map>>> newDataMap,Object[] detecInfo) { + + String detecSetInfoId = (String) detecInfo[DetectInfo.SETINFOID];//监测设置id + String seqId = (String) detecInfo[DetectInfo.SEQID];//节点id + String checkType = (String) detecInfo[DetectInfo.CHECKTYPE];//监测类型:cpu,disk,memory... + if (newDataMap.get(checkType) == null) {//第一次添加 + Map>> setInfoIdMap = new HashMap>>();//新建setInfoIdMap + Map> seqIdMap = new HashMap>();//新建seqIdMap + List detecInfoList = new ArrayList();//新建List + detecInfoList.add(detecInfo);//放入list集合 + seqIdMap.put(seqId, detecInfoList);//放入seqid集合 + setInfoIdMap.put(detecSetInfoId, seqIdMap);//放入setInfoId集合 + newDataMap.put(checkType, setInfoIdMap);//放入checktype集合 + }else{ + Map>> setInfoIdMap = newDataMap.get(checkType); + if(setInfoIdMap.get(detecSetInfoId) == null){ + //第一次添加 + Map> seqIdMap = new HashMap>(); + List detecInfoList = new ArrayList(); + detecInfoList.add(detecInfo); + seqIdMap.put(seqId, detecInfoList); + setInfoIdMap.put(detecSetInfoId, seqIdMap); + }else{ + Map> seqIdMap =setInfoIdMap.get(detecSetInfoId); + if(seqIdMap.get(seqId) == null){ + List detecInfoList = new ArrayList(); + detecInfoList.add(detecInfo); + seqIdMap.put(seqId, detecInfoList); + }else{ + seqIdMap.get(seqId).add(detecInfo); + } + } + } + } + + + + /** + * 组织detail表的insert语句 + * @param fieldNames + * @param detailTableName + * @param tempMap + * @return + */ + public String createDetailSql(List fieldNames, + String detailTableName, Map tempMap) { + String insertDetailSql; + StringBuffer preSql = new StringBuffer(); + StringBuffer sufSql = new StringBuffer(); + preSql.append(",detection_info_id"); + sufSql.append(",?"); + fieldNames.add("detection_info_id"); + for(String tempField : tempMap.keySet()){ + fieldNames.add(tempField); + preSql.append("," + tempField); + String value = tempMap.get(tempField); + sufSql.append(",?"); + } + insertDetailSql = "insert into "+detailTableName +" ( "+ preSql.substring(1) +" ) values (" + sufSql.substring(1) +" )"; + return insertDetailSql; + } + + /** + * 状态变更判断:只对大于等于new表监测时间的监测数据进行是否告警的判断 + * @param allDetectInfo + * @param inDbInfo + */ + public void judgeState(List allDetectInfo , Object[] inDbInfo){ + /*判断每一个的状态变更 + * 1.监测时间小于已经入库的监测数据时间不做判断 + * */ + int startIndex = 0; + for(int i = 0, j = allDetectInfo.size(); i < j ; i++){ + Object[] newDetecInfo = allDetectInfo.get(i); + Object[] oldDetecInfo = null;//前一条监测数据 + //监测时间小于已经入库的监测数据时间不做判断,只在最后一条数据判断插入告警信息 + if( newDetecInfo== null || (Long) newDetecInfo[DetectInfo.CHECKTIME] < (Long)inDbInfo[DetectInfo.CHECKTIME]){ + startIndex++; + if(i < j -1){ + continue; + }else{ + //集合中的最后一条数据监测时间也小于数据库中的时间时,最后一条数据判断是否插入一条告警信息 + if((Integer)newDetecInfo[DetectInfo.STATE] != 1){//延迟数据监测状态没有成功,数据库入库的是正常数据,补录一条报警信息 + if((Integer)inDbInfo[DetectInfo.STATE] == 1 && (Integer)inDbInfo[DetectInfo.PLEVEL] == 99){ + newDetecInfo[DetectInfo.APPENDWARNINGINFO] = oldDetecInfo; + warnResumeNum++; + } + } + } + } + //判断最新的监测数据 + if(i== startIndex){ + oldDetecInfo = inDbInfo; + }else{ + oldDetecInfo = allDetectInfo.get(i-1); + } + /*判断状态变更*/ + stateChange(newDetecInfo, oldDetecInfo); + } + + } + /** + * 判断状态变更具体实现 + * @param newDetecInfo + * @param oldDetecInfo + */ + public void stateChange(Object[] newDetecInfo,Object[] oldDetecInfo ){ + /** + * 判断状态变更 + */ + + if(newDetecInfo == null || oldDetecInfo == null + || ((Long)newDetecInfo[DetectInfo.CHECKTIME] < (Long)oldDetecInfo[DetectInfo.CHECKTIME])){ + return; + } + if(((Integer)oldDetecInfo[DetectInfo.STATE] != (Integer)newDetecInfo[DetectInfo.STATE]) || //状态改变 + (((Integer)oldDetecInfo[DetectInfo.STATE] == (Integer)newDetecInfo[DetectInfo.STATE] + && (Integer)oldDetecInfo[DetectInfo.PLEVEL]!= (Integer)newDetecInfo[DetectInfo.PLEVEL] ))){//状态未变,告警级别改变 + + int state = (Integer)newDetecInfo[DetectInfo.STATE]; + //邮件告警信息 + StringBuffer alarmInfo = new StringBuffer((String)newDetecInfo[DetectInfo.ALARMINFO]);//告警信息 + String dsinfo = (String) newDetecInfo[DetectInfo.DSINFO];//状态信息 + String checkType = (String) newDetecInfo[DetectInfo.CHECKTYPE];//监测类别 + long checkTime = (Long) newDetecInfo[DetectInfo.CHECKTIME]; + int actionType = 11; + int urgentLevel = EmailTypeConstants.URGENT_LATER; + //整理告警数据,拼写邮件信息 + if(state == -1){ //执行失败 +// alarmInfo.append("监测执行失败\n "); +// alarmInfo.append("Failure to monitor execution\n "); + alarmInfo.append("i18n_server.DetecDataResoveThread.alarmInfo1_n81i\n "); + alarmInfo.append(dsinfo+"\n"); + actionType= EmailTypeConstants.TYPE_DETECTION_INFO_EXCEPTION; + if(Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + + } + }else if(state == 0){ //一般异常 + alarmInfo.append(" "+dsinfo+"\n"); + //如果是握手监测,不添加告警设置字段相关信息 + if(!Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + alarmInfo.append(dsinfo+" \n"); + }else { + alarmInfo.delete(0,alarmInfo.length());//如果是握手监测,则不添加告警设置字段相关信息 + alarmInfo.append(dsinfo+" \n"); + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + } + actionType= EmailTypeConstants.TYPE_DETECTION_INFO_EXCEPTION; + }else if(state == 1){ //恢复正常 +// alarmInfo.append("监测恢复正常"); +// alarmInfo.append("Monitoring back to normal"); + alarmInfo.append("i18n_server.DetecDataResoveThread.alarmInfo1_n81i"); + actionType= EmailTypeConstants.TYPE_DETECTION_INFO_RECOVER; + urgentLevel = EmailTypeConstants.URGENT_LATER; + //更新该节点所有监测时间为当前监测时间 + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + newDetecInfo[DetectInfo.STATUSCHANGTIME] = (checkTime); + } + } + // 报警通知 + + newDetecInfo[DetectInfo.STATECHANGEFLAG] = (true); + newDetecInfo[DetectInfo.ALARMINFO] = (alarmInfo.toString()); + newDetecInfo[DetectInfo.SENDEMAILFLAG] = (true); + newDetecInfo[DetectInfo.URGENTLEVEL] = (urgentLevel); + //目前代码中是否紧急和紧急级别不对应,有的是紧急,但是级别是2,所以此处进行修改,如果是紧急,则级别修改为0级 + if(EmailTypeConstants.URGENT_IMMEDIATELY==urgentLevel){ + newDetecInfo[DetectInfo.PLEVEL] = Constants.LEVEL_OF_EMERGENCY; + } + //如果是0级,则设置为紧急,使得紧急状态和级别一致 + if(newDetecInfo[DetectInfo.PLEVEL].equals(Constants.LEVEL_OF_EMERGENCY)){ + newDetecInfo[DetectInfo.URGENTLEVEL] = EmailTypeConstants.URGENT_IMMEDIATELY; + } + long seqIdLong = Long.parseLong((String)newDetecInfo[DetectInfo.SEQID]); + long setInfoIdLong = Long.parseLong((String)newDetecInfo[DetectInfo.SETINFOID]); + + String emailContent = alarmInfo.toString(); + if(emailContent.getBytes().length>290){ + emailContent = StringUtil.substring(emailContent,290); + } + + EmailInfo emailInfo = new EmailInfo(actionType,Common.getSetInfoNameMape().get(setInfoIdLong)+"("+Common.getCheckTypeNameMape().get(checkType)+")",Common.getNodeIpByUUID(seqIdLong), format2.format(new Date(checkTime)),emailContent,EmailTypeConstants.FLAG_SEND_LATER,urgentLevel); + + newDetecInfo[DetectInfo.EMAILINFO] = (emailInfo); + } + } + /** + * id 和 currentId 同时 = -1时,为第一次获取sequence id + * result[0] 为currentID + * result[1] 为基准id,获取的sequence id + * @param id + * @param currentId + * @return + */ + public long getSequenceId(CommonService service){ + + long result = -1L; + if( infoId != -1 && currentId != -1 && currentId < (infoId + interval -1) && interval != -1){ + currentId ++; + result = currentId; + }else{ + //-- 申请sequences for detection_info + long[] temp = service.getDetectionInfoSequenceID(); + if(temp[0] > 0){ + result = infoId = currentId = temp[0];//获取的id + interval = temp[1];//步进 + } + } + return result; + } + + + /** + * + * 获取文件路径,判断当天目录下的文件如果>=500个,则新创建父目录, + * 创建父目录的原则是"日期_1"、"日期_2"、"日期_3"累加的形式 + * + * @author jsj Apr 28, 2013 + * @version 1.0 + * @return + */ + private String getFilePath() + { + String filePath = ""; + try + { + String parentDirPath = getParentDirPath(); + filePath = parentDirPath + "/" + Calendar.getInstance().getTimeInMillis() + ".csv"; + + } catch (Exception e) + { + logger.error("Getting the error file path exception", e); + } + return filePath; + } + + /** + * + *获取文件的父目录 + * 获取当天的父母目录,如果父目录不存在,则为第一次写入文件,直接返回当天的目录, + 存在则判断目录下的文件是否>-500,>=500则新建日期目录,否则取当前目录 + * @author jsj Apr 28, 2013 + * @version 1.0 + * @return + */ + private String getParentDirPath() + { + String parentDirPath = Constants.ERROR_DETEC_FILE_DIR; + String currentDirName = Common.getDateDirName(); + String dirPath = parentDirPath + "/" + currentDirName; + File parentDirFile = new File(parentDirPath); + List existDir = new ArrayList(); + + if (parentDirFile.exists()) + { + File[] dirFiles = parentDirFile.listFiles(); + if (dirFiles.length > 0) + { + for (File dirFile: dirFiles) + { + if (dirFile.getName().contains(Common.getDateDirName())) + { + existDir.add(dirFile); + } + } + } + + } else + { + return dirPath; + } + + boolean isGetFile = false; + + for (File dirFile: existDir) + { + File[] files = dirFile.listFiles(); + if (currentDirName.compareTo(dirFile.getName()) < 0) + { + currentDirName = dirFile.getName(); + } + if (files.length < Constants.ERROR_DETEC_FILE_DIR_FILE_SIZES) + { + dirPath = dirFile.getAbsolutePath(); + isGetFile = true; + break; + } + } + + if (!isGetFile) + { + String[] currentDirNames = currentDirName.split("_"); + if (currentDirNames.length == 1) + { + currentDirName += "_1"; + } else + { + currentDirName = currentDirNames[0] + "_" + + +(Integer.parseInt(currentDirNames[1]) + 1); + } + dirPath = parentDirPath + "/" + currentDirName; + } + return dirPath; + } + + + //检查入库的监测数据中是否有无效的数据,有则置为无效 + public void updateDetectDataValid(CommonDao dao) throws Exception{ + CommonService service = new CommonService(dao); + Map detectSetSeqIdMap = service.getDetectSetInfo();//查询有效监测数值对应的seqid + Map detectDataSeqIdMap = service.getDetectSeq(Constants.DETECTION_INFO_TABLE_NAME);//统计 监测数据 表: 监测设置<->已有有效数据的seqid + Iterator detectDateSeqIte = detectDataSeqIdMap.entrySet().iterator(); + String invalidSeqIds = ""; + List sqls = new ArrayList(); + while(detectDateSeqIte.hasNext()) { + invalidSeqIds = ""; + Entry en = (Entry)detectDateSeqIte.next(); + String setId = (String)en.getKey(); + String seqIdsValid = detectSetSeqIdMap.get(setId);//得到监测设置当前有效的seqids:1,2,3 + if(seqIdsValid!=null) {//如果有效的监测设置内没有此监测设置,则不予处理 + seqIdsValid = "," + seqIdsValid + ","; + String[] seqIds = (String[])en.getValue(); + for(String seqIdTmp:seqIds) { + if(seqIdTmp!=null && !"".equals(seqIdTmp)) { + if(!seqIdsValid.contains(","+seqIdTmp+",")) {//不在有效范围内,则更新为无效 + invalidSeqIds = invalidSeqIds + seqIdTmp + ","; + } + } + } + if(invalidSeqIds.endsWith(",")) { + invalidSeqIds = invalidSeqIds.substring(0,invalidSeqIds.length()-1); + sqls.add("update "+Constants.DETECTION_INFO_TABLE_NAME+" t set valid=0 where t.valid=1 and t.detection_set_info_id="+setId+" and t.seq_id in ("+invalidSeqIds+")"); + } + } + } + if(sqls.size()>0){//一般情况下不需要更新,所以,如果不加if判断,则每次都会浪费时间去创建statement + dao.dbUpdateByBatch(sqls); + } + + } + + + + /** + *打印统计的监测数据:监测类别--监测设置--节点--监测信息 + */ + private void printDetectDataInfo(Map>>> newDataMap) { + /** + *打印统计的监测数据:监测类别--监测设置--节点--监测信息 + */ + if(newDataMap != null && newDataMap.size()>0){ + logger.debug("解析完毕:共" +newDataMap.size()+ "个监测类型"); + for(Map.Entry>>> setInfoIdMap :newDataMap.entrySet()){ + logger.debug("监测类型: " + setInfoIdMap.getKey() + " 共有:" + setInfoIdMap.getValue().size() +" 个监测设置项"); + if(setInfoIdMap.getValue() != null){ + for(Map.Entry>> me : setInfoIdMap.getValue().entrySet() ){ + if(me.getValue() != null){ + logger.debug("监测设置id: " + me.getKey() + " 共包含:" + me.getValue().size() +" 节点"); + for(Map.Entry> seqMe : me.getValue().entrySet()){ + if(seqMe.getValue() != null){ + logger.debug("seqId : " + seqMe.getKey() + " 共包含:" + seqMe.getValue().size() +" 条监测数据"); + } + } + } + } + } + } + } + } + + /** + * 将解析之后的监测数据写入到文件 + * @param temInfo + * @param file + * @return + */ + private byte[] objectToString(Object[] temInfo){ + byte[] dsb = null; + try { + // ------- 总监测数据组织 + int index = 0; + String[] totalData = new String[12]; + totalData[index++] = (String) temInfo[DetectInfo.SEQID];// UUID + totalData[index++] = (String) temInfo[DetectInfo.SETINFOID];// 监测设置ID + String checkType = totalData[index++] = (String) temInfo[DetectInfo.CHECKTYPE];// 监测类别 + totalData[index++] = (String) temInfo[DetectInfo.PROCESS];// 进程名称 + totalData[index++] = temInfo[DetectInfo.STARTTIME] +"";// 监测服务启动时间 + totalData[index++] = temInfo[DetectInfo.DELAYTIME] +"";// 检测时延(秒) + totalData[index++] = temInfo[DetectInfo.CHECKTIME]+ "";// 本次检测时间 + totalData[index++] = temInfo[DetectInfo.CURRENTTIMES] + "";// 尝试次数 + totalData[index++] = temInfo[DetectInfo.NEXTCHECKTIME]+"";// 下次计划监测时间 + totalData[index++] = temInfo[DetectInfo.STATE] + "";// 执行状态是否成功是否正常 + totalData[index++] = temInfo[DetectInfo.DSINFO] +"";// 状态信息(描述信息) + totalData[index++] = temInfo[DetectInfo.PDATA] +"";// 性能数据 + + /** + * 详细数据前三个告警相关信息 + */ + List showNumList = (List) temInfo[DetectInfo.SHOWNUMLIST]; + List plevelList = (List) temInfo[DetectInfo.PLEVELLIST]; + List alarmList = (List) temInfo[DetectInfo.ALARMLIST]; + + List dataList = new LinkedList(); + // 总数据 + dataList.add(totalData); + + //详细信息 + List> details = (List>) temInfo[DetectInfo.DETAILS]; + if(details != null && details.size() > 0){ + Map tableMap = Common.getTableMap().get(checkType); + Map fieldMap = fieldNameToLower(tableMap);//将表字段名称转换成小写 + // 详细信息 + dataList.add(new String[]{"details", details.size()+ ""});// details(解析标识),详细信息条数 + List detailDataList = new LinkedList(); + int listIndex = 0; + for(Map temp : details){ + //单条详细信息 + String[] tempDetail = new String[tableMap.size() + 4]; + tempDetail[0] = showNumList.get(listIndex); + tempDetail[1] = plevelList.get(listIndex); + tempDetail[2] = alarmList.get(listIndex); + for(Map.Entry tempEntry : temp.entrySet()){ + String fieldName = tempEntry.getKey().toLowerCase(); + if("seq_id".equals(fieldName) || //解析时添加的数据跳过 + "detectioned_state".equals(fieldName) || + "detection_set_info_id".equals(fieldName) || + "data_check_time_digital".equals(fieldName) || + "data_check_time".equals(fieldName) || + "data_arrive_time_digital".equals(fieldName) || + "data_arrive_time".equals(fieldName) ){ + continue; + } + String value = tempEntry.getValue(); + int arrIndex = fieldMap.get(fieldName) + 2; + tempDetail[arrIndex] = value; + } + detailDataList.add(tempDetail); + listIndex ++; + } + + dataList.addAll(detailDataList);// 具体的详细数据 + + // 特殊数据定制(目前是针对系统信息监测systeminfo) + if ("systeminfo".equalsIgnoreCase(checkType)) { + List netList = (List) temInfo[DetectInfo.NETINFOLIST]; + if(netList != null && netList.size() > 0){ + dataList.add(new String[]{"net", netList.size() + ""});// 解析标识, 行数(当前解析标识指定的类型有多少条数据) + dataList.addAll(netList);// 多条数据 + } + List diskList = (List) temInfo[DetectInfo.DISKINFOLIST]; + if(netList != null && diskList.size() > 0){ + dataList.add(new String[]{"disk", netList.size() + ""});// 解析标识, 行数(当前解析标识指定的类型有多少条数据) + dataList.addAll(diskList);// 多条数据 + } + } + } + dsb = CSVUtils.csvBytesPrinter1(dataList, Constants.COMMON_TEXT_CODING); + } catch (Exception e) { + e.printStackTrace(); + logger.error("Conversion from Object[] to byte[] failure",e); + } + return dsb; + } + /** + * 表字段名称全部转换成小写,并fieldName做key + * @param tableMap + * @return + */ + private Map fieldNameToLower(Map tableMap){ + Map result = new HashMap(); + if(tableMap != null && tableMap.size()>0){ + for(Map.Entry temp : tableMap.entrySet()){ + TableColumnsModel column = temp.getValue(); + if(column != null){ + result.put(column.getFiledName().toLowerCase(), temp.getKey()); + } + } + } + return result; + } + + + private List generateLog(byte[] info,String head) { + List warnInfoList = new ArrayList(); + try + { + //byte[] info = (byte[]) temInfo[27]; + List strsList = CSVUtils.csvBytesParser(info,Constants.COMMON_TEXT_CODING); + String time = System.currentTimeMillis()+""; + for(String[] strArr:strsList) { +// logger.info(head+" "+time +" "+StringUtils.join(strArr,",")); + warnInfoList.add(head+" "+time +" "+StringUtils.join(strArr,",")); + } + + } catch (Exception e){ + } + return warnInfoList; + } + + private void printLogs(List logList) { + for(String log:logList){ + logger.info(log); + } + } + + public static void main(String[] args) { + + + } + + +} diff --git a/src/com/nms/server/thread/detecData/NewDetecDataResoveThread.java b/src/com/nms/server/thread/detecData/NewDetecDataResoveThread.java new file mode 100644 index 0000000..e763977 --- /dev/null +++ b/src/com/nms/server/thread/detecData/NewDetecDataResoveThread.java @@ -0,0 +1,1264 @@ +package com.nms.server.thread.detecData; + +import java.io.File; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collections; +import java.util.Comparator; +import java.util.ConcurrentModificationException; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Callable; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.EmailInfo; +import com.nms.server.bean.TableColumnsModel; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.common.EmailTypeConstants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.CommonService; +import com.nms.server.thread.socket.SSLClient; +import com.nms.server.util.CSVUtils; +import com.nms.server.util.FileUtils; +import com.nms.server.util.StringUtil; + +/** + * CSV解析线程 + * @author ZGGG3 + * + */ +public class NewDetecDataResoveThread implements Callable { + Logger logger = Logger.getLogger(NewDetecDataResoveThread.class); + volatile boolean stop = false;//线程是否被取消标志 + private String name; // 自定义Thread Name + private LinkedList dsbList; + private SimpleDateFormat format2 = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); //Java Date 类型数据格式化格式 + + private long infoId = -1; + private long currentId = -1; + private long interval = -1; + private int warnResumeNum = 0; + private PreparedStatement infoNewInsertStmt; + private PreparedStatement infoNewUpdateStmt; + private List insertDataTonew; + private List updateDataTonew; + private PreparedStatement warnUpdateStmt; + public NewDetecDataResoveThread(String name,LinkedList dsbList) { + this.name = name; + this.dsbList = dsbList; + } + + /* + * 线程操作 + * + * 依次解析urlList中仍存在的Files + * + * 实现了依次解析Files,由于后期实现多线程解析操作 + */ + public Object call() { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name +"-"+System.currentTimeMillis()); + logger.info("配置文件设置批量入库监测数据条数:"+ Constants.DETECTION_INFO_DATA_MAX_ROWS); + CommonDao dao = null; + try { + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + //-- 空数据集合 结束操作 + if(dsbList == null || dsbList.size() == 0 ){ + logger.debug("缓存监测数据 为空,无需解析"); + return null; + } + + //用于判断网络端口 + long eNewTime = System.currentTimeMillis(); + Map> allPortName= service.getAllPortName();//之后如果数据大了,影响性能了,可以改成获取一个seqid的数据和一个端口的list,查询seqid,得到下标,再从list中获取信息,目前先暂时不改 + long portTime = System.currentTimeMillis()-eNewTime; + int portTimeM=(int)portTime/1000/60; + int portTimeS=(int)((long)(portTime/1000)%60); + int portTimeMS=(int)portTime%1000; + logger.info("第零步:获取端口表的所有数据,总耗时:"+(portTimeM)+"分 "+(portTimeS)+"秒 "+(portTimeMS)+"毫秒"); + + + logger.debug("缓存监测数据 " + (dsbList.size())+ "条,解析开始"); + + //-- 遍历解析 + Iterator dsbIte = dsbList.iterator(); + int totalCount = dsbList.size(); + int successCount=0; + int falseCount = 0; + int zeroByteCount = 0; + + int count = 1; + int resoveCount=1; + int allResove = 0; + int allSuccess = 0; + int allFail = 0; + int allZero = 0; + /* + * 第一步:解析所有数据 + * 新格式数据,旧格式同一放入 newDataMap 集合 + */ + //存放解析之后的数据:[["systemdate",["5",["1602",[{detectInfo1,detectInfo2,...}]],...],...],...] + List> dataList = new LinkedList>(); + long sTime = System.currentTimeMillis(); + while (dsbIte.hasNext()&& !stop) {//线程未被中断 + /* 单条解析异常捕捉处理 */ + byte [] dsb = null; + try { + dsb = dsbIte.next(); + /* 零字节文件记录和过滤 */ + if (dsb != null && dsb.length > 0) { + /* 解析非0字节文件内容 */ + List strsList = CSVUtils.csvBytesParser(dsb,Constants.COMMON_TEXT_CODING); + /* 新旧解析格式判断 */ + /** + * 新旧解析格式区别: + * 区别:第一行基本信息数据 新格式12列固定比旧格式至少15列 + * */ + if(strsList!=null && strsList.size()>0){ //多行数据判断 + Object[] detecInfo = null; + String [] detailsStr = strsList.get(0); //取第一行数据 + + if(detailsStr != null){ + if(detailsStr.length > 12){//旧格式 解析 + logger.debug("旧格式数据"); + detecInfo = service.resoveOldData(strsList,allPortName);//解析数据,不判断状态变更 + }else if(detailsStr.length<12){ + logger.debug( "基本数据解析长度:"+detailsStr.length+" 小于最小解析标准,不予解析;"); + }else{//新格式 解析 + logger.debug("新格式数据"); + detecInfo = service.resoveNewData(strsList,allPortName);//解析数据,不判断状态变更 + } + } + + //将解析之后的数据放入集合,不为空说明解析成功 + if(detecInfo != null){ + successCount++;//成功解析 + detecAddToList(dataList, detecInfo); + }else{ + falseCount ++;//解析失败 + String filePath = getFilePath();//Constants.ERROR_DETEC_FILE_DIR+"/"+Common.getDateDirName()+"/"+Calendar.getInstance().getTimeInMillis()+".csv"; + logger.error("Abnormal monitoring data format,file has been saved to "+filePath); + FileUtils.wirteBytesToFile(dsb, filePath); + } + } + }else{ + zeroByteCount++; + } + + //解析入库一条数据,从list中remove一条数据 + dsbIte.remove(); + //判断当前线程是否被中断 + if (Thread.currentThread().isInterrupted()) { + // 线程中断状态,不会改变中断状态的值 + logger.info("监测数据解析线程 被中断"); + stop = true;//如果中断执行(重启、或者升级),就停止解析数据,之后将数据入库 + } + }catch (InterruptedException e) { + logger.error("Monitoring data parsing threads are interrupted",e); + stop = true; + + }catch (ConcurrentModificationException e) { + logger.debug("获取下一条监测数据 异常 停止本轮解析!",e); + stop = true;//由于获取next的时候出错,之后的都出错了就,所以此处stop=true,停止循环 + }catch (Exception e) { + logger.debug("一条监测数据解析失败!",e); + } + + if(count%Constants.BATCH_RESOVE_COUNT==0||stop||count==totalCount){ + long resoveCurTime = System.currentTimeMillis(); + long resoveTime = resoveCurTime-sTime; + int resoveTimeM=(int)resoveTime/1000/60; + int resoveTimeS=(int)((long)(resoveTime/1000)%60); + int resoveTimeMS=(int)resoveTime%1000; + logger.info("第一步:解析完毕,共:"+(resoveCount)+"条[成功:"+successCount+"条,失败:"+falseCount+"条,空数据:"+zeroByteCount+"条]" + +" 总耗时:"+(resoveTimeM)+"分 "+(resoveTimeS)+"秒 "+(resoveTimeMS)+"毫秒"); + insertDataTonew=new ArrayList(); + updateDataTonew=new ArrayList(); + warnUpdateStmt=dao.getConn().prepareStatement(DetectInfo.getChangeWarningInfoSql()); + judgeStateAndSave(dataList,sTime,resoveCount,successCount,falseCount,zeroByteCount,service, dao); + dataList.clear(); + allResove = allResove + resoveCount; + allSuccess = allSuccess+ successCount; + allFail = allFail + falseCount; + allZero = allZero + zeroByteCount; + warnResumeNum = 0; + resoveCount=0; + successCount=0; + falseCount=0; + zeroByteCount=0; + } + resoveCount++; + count++; + } + dsbList.clear(); + + //将监测数据入库后,需要检查新入库的数据是否为无效(处理监测数据的有效性标志) + long startUpdateTime = System.currentTimeMillis(); + updateDetectDataValid(dao); + long updateCurTime = System.currentTimeMillis()-startUpdateTime; + int updateCurTimeM=(int)updateCurTime/1000/60; + int updateCurTimeS=(int)((long)(updateCurTime/1000)%60); + int updateCurTimeMS=(int)updateCurTime%1000; + logger.info("第四步:更新监测数据有效性完毕,总耗时:"+(updateCurTimeM)+"分 "+(updateCurTimeS)+"秒 "+(updateCurTimeMS)+"毫秒"); + + + long eTime = System.currentTimeMillis(); + long curTime = eTime-sTime; + int m=(int)curTime/1000/60; + int s=(int)((long)(curTime/1000)%60); + int ms=(int)curTime%1000; + logger.info("第一到四步,整体解析批量入库监测数据全过程执行完毕,共:"+(allResove)+"条[成功:"+allSuccess+"条,失败:"+allFail+"条,空数据:"+allZero+"条]" + +" 总耗时:"+(m)+"分 "+(s)+"秒 "+(ms)+"毫秒"); + + } catch (Exception e) { + logger.error("Running exception",e); + }finally{ + dao.closeCommonBatchStatement(); + dao.closeDetailBatchStatement(); + if(dao!=null){ + dao.close(); + dao=null; + } + //1:由web 主控控制入库 + if(Constants.DETECT_INSERT_MODE == 1 ||Constants.DETECT_INSERT_MODE == 3){ + //入库完成,通知web 主控释放锁 + SSLClient.sengReleaseDetect(); + } + logger.debug("数据清理 解析完成"); + } + return null; + } + + + public void judgeStateAndSave(List> dataList,long sTime, int resoveCount, int successCount, int falseCount, int zeroByteCount, CommonService service,CommonDao dao) throws Exception{ + /** + * 第二步:判断监测状态变更 + */ + long resoveCurTime = System.currentTimeMillis(); + if(dataList != null && dataList.size() >0){ + for(List checkTypeList : dataList){ + int checkTypeSize = checkTypeList.size(); + for(int i = 1,j =checkTypeSize; i < j; i ++){ + List setIdList = (List) checkTypeList.get(i); + String setId = (String) setIdList.get(0);//监测设置id + + List detectionInfoNewData = null; + List seqList = new ArrayList(); + int setIdSize = setIdList.size(); + /** + * 2017年1月9日11:15:21 新增 添加过滤条件查询new表数据(只查询当前解析数据seqid的new表数据) + */ + for(int h = 1;h seqIdList = (List) setIdList.get(h); + String seqId = (String) seqIdList.get(0);//seqId + seqList.add(seqId); + } + /**以上*/ + + //获取new表中对应监测设置的监测数据 + detectionInfoNewData = service.getDetectionInfoNewListBySet(setId,seqList); + for(int ii = 1; ii < setIdSize ; ii ++){ + List seqIdList = (List) setIdList.get(ii); + String seqId = (String) seqIdList.get(0);//seqId + List allDeteInfo = (List) seqIdList.get(1); + if(allDeteInfo != null && allDeteInfo.size() > 0){ + //detection_info_new中的 最新监测数据 + Object[] oldDetecInfo = null; + if(detectionInfoNewData != null){//detectionInfoNewData:监测设置--节点--监测信息 + oldDetecInfo = service.getIndexObj(seqList, seqId,detectionInfoNewData); + }else{ + oldDetecInfo = null; + } + //数据库中没有当前监测类别对应的seqid节点的监测信息,不需要判断状态变更 + if(oldDetecInfo != null){ + if(allDeteInfo.size() <2 ){//只有一条数据直接比较数据库中的数据 + //只有一条最新监测 + Object[] newDetecInfo = allDeteInfo.get(0); + + int state = (Integer) newDetecInfo[DetectInfo.STATE]; + /* + * 入库数据不是延时数据时,与数据库中的最新数据比对判断状态变更 + * 数据库中的监测时间小于当前需要入库的监测时间 + */ + if((Long)oldDetecInfo[DetectInfo.CHECKTIME] < (Long)newDetecInfo[DetectInfo.CHECKTIME]){ + /*判断状态变更*/ + stateChange(newDetecInfo, oldDetecInfo); + }else{//监测时间小于数据库中监测数据的时间,说明当前需要入库的监测为延迟数据 + if(state != 1){//延迟数据监测状态没有成功,数据库入库的是正常数据,补录一条报警信息 + if((Integer) newDetecInfo[DetectInfo.STATE] == 1 && (Integer)oldDetecInfo[DetectInfo.PLEVEL] == 99){ + newDetecInfo[DetectInfo.APPENDWARNINGINFO]= oldDetecInfo; + warnResumeNum++; + } + } + } + }else{//大于两条数据时,排序之后判断数据库的数据是否为最接近的时间数据 + //未入库的数据排序 + Collections.sort(allDeteInfo, new Comparator() { + @Override + public int compare(Object[] o1, + Object[] o2) { + Long time1 = (Long) o1[DetectInfo.CHECKTIME]; + Long time2 = (Long) o2[DetectInfo.CHECKTIME]; + return time1 < time2 ? -1 : 1; + } + }); + /*判断状态变更*/ + judgeState(allDeteInfo, oldDetecInfo); + } + judgeAndCollect(allDeteInfo, oldDetecInfo, updateDataTonew,service); + }else{//如果new表中没有数据,应为detection_info status_change_time设置初始值-->data_check_time + if(allDeteInfo!=null&&allDeteInfo.size()>0){ + for (Object[] newInfo : allDeteInfo) { + if(newInfo[DetectInfo.STATUSCHANGTIME]==null||(Long)newInfo[DetectInfo.STATUSCHANGTIME]==-1){ + newInfo[DetectInfo.STATUSCHANGTIME]= + newInfo[DetectInfo.CHECKTIME]==null?newInfo[DetectInfo.STARTTIME]:newInfo[DetectInfo.CHECKTIME]; + } + } + insertDataTonew.addAll(allDeteInfo); + + } + } + } + } + //清理内存 + detectionInfoNewData.clear(); + detectionInfoNewData = null; + } + } + } + long stateCurTime = System.currentTimeMillis(); + long stateCheckTime = stateCurTime-resoveCurTime; + int stateCheckM=(int)stateCheckTime/1000/60; + int stateCheckS=(int)((long)(stateCheckTime/1000)%60); + int stateCheck=(int)stateCheckTime%1000; + logger.info("第二步:判断监测状态变更完毕(只针对解析成功的),共:"+resoveCount+"条 总耗时:"+(stateCheckM)+"分 "+(stateCheckS)+"秒 "+(stateCheck)+"毫秒"); + logger.info(" 第二步-1: 补录告警恢复告警信息:"+warnResumeNum+"条"); + + /** + * 第三步:批量入库 + */ + //判断入库标示 + if(Constants.FLAG_RESOVE_COMMIT_DB != 1){ + String s = "file.resove.commitDB.flag 不为1,数据不可保存,请检查配置文件"; + logger.warn(s); + // return null; + return ; + } + int failBatchInsert=0; + String failBatchInsertCheckType = "";//批量入库失败的监测类别信息 + if(dataList != null && dataList.size() >0){ + int infoCount = 0; + int detailCount = 0; + int sysinfoCount = 0; + int warnningCount = 0; + int statusChange = 0; + int emailCount = 0; + //监测设置对应的联系人email地址 + Map> setInfoEmail = null; + //未指定的监测设置通过seqid找到对应的联系人 + Map> seqIdEmail = null; +// List logList = new ArrayList(); + dao.setAutoCommit(false); + LinkedList dsbBatchList = new LinkedList();//用于批量入库后是否将监测数据存入硬盘的overrun :每次入库,或者存硬盘都需要清空此列表 + + //迭代器遍历,方便删除数据 + Iterator> iteDataList = dataList.iterator(); + while(iteDataList.hasNext()){ + List checkTypeList = iteDataList.next();//["cpu",["1",["1602",[{detecAllInfo}]]]], + String checkType = (String) checkTypeList.get(0);//监测类型 + + int checkTypeRecordNum=0;//每种监测类别入库的记录数 + String insertDetailSql = null;//插入详细参数 + List fieldNames = new ArrayList();//detail表的字段名 + //详细信息对应的表名 + String detailTableName = Common.getInsertTable().get(checkType) == null ? null : Common.getInsertTable().get(checkType).getTableName(); + if(detailTableName == null){ + logger.error(checkType + " the corresponding detailed table was not found and the data could not be stored"); + continue; + } + String seqId = null; + + Iterator checkIte = checkTypeList.iterator(); + boolean firstKey1 = true; + while(checkIte.hasNext()){ + if(firstKey1){ + checkIte.next(); + firstKey1 = false; + }else{ + List setIdList = (List) checkIte.next();//["1",["1602",[{detecAllInfo}]]] + String setInfoId = (String) setIdList.get(0);//监测设置id + Iterator setIte = setIdList.iterator(); + boolean firstKey2 = true; + while(setIte.hasNext()){ + if(firstKey2){ + firstKey2 = false; + setIte.next(); + }else{ + List seqIdList = (List) setIte.next();//["1602",[{detecAllInfo,detecAllInfo}]] + seqId = (String) seqIdList.get(0);//seqid + List detecDatas = (List) seqIdList.get(1);//{detecAllInfo,detecAllInfo} + Iterator detecIte = detecDatas.iterator(); + + long statusChangeTime = -1;//状态变更时间 + while(detecIte.hasNext()){ + Object[] temInfo = detecIte.next(); + //将解析之后的数据还原 + byte[] dsb = objectToString(temInfo); + //将还原之后的数据缓存,出现异常时保存到文件 + dsbBatchList.add(dsb); + if(temInfo != null){ + // 计算 详细表的 外键 时间戳(10) + seqId(5)+ setId (3),主要是为了使用 java 中long 类型 + String checkTime = temInfo[DetectInfo.CHECKTIME].toString(); + String setId = temInfo[DetectInfo.SETINFOID].toString(); + String seqid = temInfo[DetectInfo.SEQID].toString(); + String id = DetectInfo.computeId(checkTime, seqId, setId); +// currentId = getSequenceId(service); + currentId = Long.parseLong(id); + //addbatch + service.addInfoStmt(currentId,temInfo); + + infoCount ++; + logger.debug(" detection_info表id:" + currentId + ",监测类别:" + checkType +",监测设置id:" + setInfoId + ",seqId:" + seqId); + //添加详细信息 + if((Boolean) temInfo[DetectInfo.DELYFLAG]){ + //当delyFlag 为false时 不插入详细信息 + List> details = (List>) temInfo[DetectInfo.DETAILS]; + if(details!= null && details.size()>0 ){ + for(Map tempMap :details){ + //首先组织sql语句 + if(insertDetailSql == null || "".equals(insertDetailSql)){ + //组织detail表的insert语句 + insertDetailSql = createDetailSql(fieldNames,detailTableName,tempMap); + logger.debug("监测设置id: " + setInfoId +" ,detail表插入语句:" + insertDetailSql); + } + dao.setDetailVals(insertDetailSql,fieldNames,tempMap,currentId,detailTableName); + checkTypeRecordNum++; + detailCount ++; + } + } + } + //特殊格式追加信息 + if(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType) ){ + List diskInfoList = (List) temInfo[DetectInfo.DISKINFOLIST]; + List netInfoList = (List) temInfo[DetectInfo.NETINFOLIST]; + if(diskInfoList != null && diskInfoList.size() > 0){//disk + for(String[] temDiskInfo : diskInfoList){ + if(temDiskInfo != null && temDiskInfo.length >7){ + service.addDiskStmt(currentId,temDiskInfo); + sysinfoCount ++; + } + } + } + if(netInfoList != null && netInfoList.size() > 0){//net + for(String[] temNetInfo : netInfoList){ + if(temNetInfo != null && temNetInfo.length >7){ + service.addNetStmt(currentId,temNetInfo); + sysinfoCount ++; + } + } + } + } + + //添加告警信息:告警信息=状态异常+状态改变+推迟入库的 + if((Integer)temInfo[DetectInfo.STATE] != 1 || (Boolean)temInfo[DetectInfo.STATECHANGEFLAG]){ + service.addWarningStmt(currentId +"",temInfo); +// logList.addAll(generateLog(dsb," 第三步-1: 添加一条告警信息,状态发生变更或者数据异常!state="+temInfo[DetectInfo.STATE]+",isChange="+(Boolean)temInfo[DetectInfo.STATECHANGEFLAG])); + warnningCount ++; + } + //延迟监测数据追加告警信息 + if(temInfo[DetectInfo.APPENDWARNINGINFO] != null){ + Object[] warningInfo = (Object[]) temInfo[DetectInfo.APPENDWARNINGINFO]; + service.addWarningStmt((String)warningInfo[DetectInfo.DETECTIONINFOID],warningInfo); +// logList.addAll(generateLog(dsb," 第三步-1: 追加一条延迟数据的告警信息!")); + warnningCount ++; + } + //添加邮件信息 + if(Constants.flag_email != 1){ + logger.info("邮件功能已关闭"); + }else{ + if((Boolean) temInfo[DetectInfo.SENDEMAILFLAG]){ + EmailInfo emailInfo = (EmailInfo) temInfo[DetectInfo.EMAILINFO]; + if(emailInfo != null){ + if(setInfoEmail == null){ + setInfoEmail = service.getDetecSetEmailListOf123(); + } + if(seqIdEmail == null){ + seqIdEmail = service.getDetecEmailListOf4(); + } + if((setInfoEmail == null || setInfoEmail.size() == 0) && (seqIdEmail == null || seqIdEmail.size() == 0)){ + logger.debug("未找到邮件地址!"); + }else{ + List emailAddress = null; + //1.首先通过监测设置的map中查找 + if(setInfoEmail != null){ + emailAddress = setInfoEmail.get(setInfoId); + } + if(emailAddress == null){ + emailAddress = seqIdEmail.get(seqId); + } + if(emailAddress != null && emailAddress.size() >0){ + for(String tempAddress : emailAddress){ + if(StringUtils.isNotEmpty(tempAddress)){ + emailCount ++; + //addbatch + service.addEmailStmt(emailInfo,tempAddress); + } + } + } + } + } + } + } + + /*判断状态变更时间的最大时间,用于更新new表 + 当监测类型为 nmsclient 时,比较多条相同监测节点数据的状态变更时间,取最大的一个 + */ + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + long temChangeTime = (Long) temInfo[DetectInfo.STATUSCHANGTIME]; + if(temChangeTime > 0 ){ + if(temChangeTime > statusChangeTime){ + statusChangeTime = temChangeTime; + } + } + } + } + //dsbBatchList.add((byte[]) temInfo[27]); + //判断infoStmt的数量是否达到最大入库条数 + /*if(infoCount != 0 && infoCount % Constants.DETECTION_INFO_DATA_MAX_ROWS == 0){ + int dataLen = dsbBatchList.size(); + try { + //执行批处理 + dao.executeBatchAllStmt(false); + //提交事务 + dao.commit(); + } catch (SQLException e){ + dao.rollback(); + handleException(dsbBatchList, e); + failBatchInsert = failBatchInsert + dataLen; + failBatchInsertCheckType = failBatchInsertCheckType + checkType+" "; + }catch (Exception e) { + logger.error("批处理保存失败,数据库错误,共"+dataLen+"条",e); + dao.rollback(); + + //批量入库失败以后,要将失败的监测数据存到硬盘的overrun里,以备下次进行再次入库 + Common.saveByteToFile(dsbBatchList); + failBatchInsert = failBatchInsert + dataLen; + failBatchInsertCheckType = failBatchInsertCheckType + checkType+" "; + } + dsbBatchList.clear(); + }*/ + detecIte.remove();//删除,内存优化 + } + /* + * 更新状态变更时间 + * 只有当监测类型:nmsclient 时,statusChangeTime才会大于0 + */ + if(statusChangeTime > 0){ + service.addStateChangeStmt(statusChangeTime,seqId); + statusChange++; + logger.info("状态变更详细信息:seqId="+seqId+" changeTime="+statusChangeTime); + } + } + setIte.remove();//删除监测设置 + } + } + checkIte.remove();//删除监测设置,内存优化 + } + iteDataList.remove();//删除监测类型,内存优化 + + int dataLastLen = dsbBatchList.size(); + try{ + //执行批处理 + dao.executeBatchAllStmt(true); + service.saveToNew(insertDataTonew, 0, infoNewInsertStmt); + service.saveToNew(updateDataTonew, 1, infoNewUpdateStmt); + warnUpdateStmt.executeBatch(); + //提交事务 + dao.commit(); + }catch (SQLException e){ + logger.error("",e); + dao.rollback(); + handleException(dsbBatchList, e); + failBatchInsert = failBatchInsert + dataLastLen; + failBatchInsertCheckType = failBatchInsertCheckType + checkType+" "; + }catch(Exception e){ + logger.error("Batch save failed, database error, total"+dsbBatchList.size(),e); + dao.rollback(); + //批量入库失败以后,要将失败的监测数据存到硬盘的overrun里,以备下次进行再次入库 + logger.error("Monitoring data batch storage, database error, the file has been saved to the hard disk."); + Common.saveByteToFile(dsbBatchList); + failBatchInsert = failBatchInsert + dataLastLen; + failBatchInsertCheckType = failBatchInsertCheckType + checkType+" "; + }finally{ + //关闭stmt:此处逻辑不太顺,最后会有一个对dao的整体的关闭,但是本线程中使用的都是自己定义的preparedstatement,不够统一 + dao.closeDetailBatchStatement(); + dsbBatchList.clear(); + } +// logList.add(" 第三步-1: 监测类型:" + checkType +" ,批量入库结束,记录数: "+checkTypeRecordNum); + + } + long endTime = System.currentTimeMillis(); + long curTime = endTime-stateCurTime; + int m=(int)curTime/1000/60; + int s=(int)((long)(curTime/1000)%60); + int ms=(int)curTime%1000; +// logList.add(" 第三步-1: 共"+dataList.size()+" 种详细信息 " ); +// logList.add(" 第三步-1: new表状态变更 共"+statusChange+" 条 " ); + logger.info("第三步:批量保存入库完毕(只针对解析成功的),共:"+(dataList.size())+"条[共插入detection_info表: " + infoCount +" 条,detail表: " + detailCount +" 条,特殊追加信息: " + sysinfoCount +" 条,warning 表: " +warnningCount +" 条。]" + +" 总耗时:"+(m)+"分 "+(s)+"秒 "+(ms)+"毫秒,批量入库失败记录数:"+failBatchInsert+" 具体"+failBatchInsertCheckType); +// printLogs(logList); + } + + } + + public void handleException(LinkedList dsbBatchList, + SQLException e) { + try + { + //如果数据过错误是:1、ORA-01861:文字与格式字符串不匹配 + // (一般情况下) 2、ORA-01400:不能将空值插入 3、ORA-01722:无效数字 + // 4、ORA-00351:时间无效 + // 5、ORA-01401: 插入的值过大 + // 属于文件内容的错误所以保存到 错误文件夹中 + + int size = dsbBatchList.size(); + if(e.getMessage().indexOf("ORA-01722")>=0 + || e.getMessage().indexOf("ORA-01400")>=0 + || e.getMessage().indexOf("ORA-01401")>=0 + || e.getMessage().indexOf("ORA-01861")>=0 + || e.getMessage().indexOf("ORA-00351")>=0){ + logger.error("Monitoring data batch storage:Monitor data format exception,a total of"+size+",the file has been saved to "+getParentDirPath(),e); + for(int j=0;j> dataList,Object[] detecInfo){ + String checkType = (String) detecInfo[DetectInfo.CHECKTYPE]; + String setInfoId = (String) detecInfo[DetectInfo.SETINFOID]; + String seqId = (String) detecInfo[DetectInfo.SEQID]; + /** + * 设置默认值 + */ + if(detecInfo[DetectInfo.STATE] == null){ + detecInfo[DetectInfo.STATE] = -1;//state + } + if(detecInfo[DetectInfo.PLEVEL] == null){ + detecInfo[DetectInfo.PLEVEL] = 99;//pLevel + } + if(detecInfo[DetectInfo.DELYFLAG] == null){ + detecInfo[DetectInfo.DELYFLAG] = false;//delyFlag + } + if(detecInfo[DetectInfo.URGENTLEVEL] == null){ + detecInfo[DetectInfo.URGENTLEVEL] = 0;//urgentLevel + } + if(detecInfo[DetectInfo.SENDEMAILFLAG] == null){ + detecInfo[DetectInfo.SENDEMAILFLAG] = false;//sendEmailFlag + } + if(detecInfo[DetectInfo.STATECHANGEFLAG] == null){ + detecInfo[DetectInfo.STATECHANGEFLAG] = false;//stateChangeFlag + } + if(detecInfo[DetectInfo.STATUSCHANGTIME] == null){ + detecInfo[DetectInfo.STATUSCHANGTIME] = -1L;//statusChangeTime + } + + if(dataList.size() == 0){ + List detecList = new LinkedList(); + detecList.add(detecInfo); + List seqList = new LinkedList();//seqid list + seqList.add(seqId); + seqList.add(detecList); + List setIdList = new LinkedList();//监测设置 id list + setIdList.add(detecInfo[DetectInfo.SETINFOID]); + setIdList.add(seqList); + List checkTypeList = new LinkedList(); + checkTypeList.add(checkType); + checkTypeList.add(setIdList); + dataList.add(checkTypeList); + }else{ + //第一层 checkType + for(int i = 0 , j = dataList.size(); i < j ; i ++){ + List tempObj = dataList.get(i); + String key = (String)tempObj.get(0);//第一个为key + if(checkType.equals(key)){//监测类型已存在 + for(int ii = 1 , jj = tempObj.size(); ii < jj ; ii ++){ + List tempSet = (LinkedList) tempObj.get(ii); + String setKey = (String) tempSet.get(0);//监测设置id + if(setInfoId.equals(setKey)){ + for(int iii = 1 ,jjj = tempSet.size(); iii < jjj; iii ++){ + List tempSeq = (LinkedList) tempSet.get(iii); + String seqKey = (String) tempSeq.get(0);//监测设置id + if(seqId.equals(seqKey)){//seqId 存在 + List detecList = (List) tempSeq.get(1); + detecList.add(detecInfo); + break; + }else if(iii == jjj -1){//seqid 不存在 + List detecList = new LinkedList(); + detecList.add(detecInfo); + List seqList = new LinkedList();//seqid list + seqList.add(seqId); + seqList.add(detecList); + tempSet.add(seqList); + } + } + break; + }else if(ii == jj-1){//监测设置id不存在 + List detecList = new LinkedList(); + detecList.add(detecInfo); + List seqList = new LinkedList();//seqid list + seqList.add(seqId); + seqList.add(detecList); + List setIdList = new LinkedList();//监测设置 id list + setIdList.add(detecInfo[DetectInfo.SETINFOID]); + setIdList.add(seqList); + tempObj.add(setIdList); + } + } + break;//跳出循环 + }else if(i == j-1){//不存在 + List detecList = new LinkedList(); + detecList.add(detecInfo); + List seqList = new LinkedList();//seqid list + seqList.add(seqId); + seqList.add(detecList); + List setIdList = new LinkedList();//监测设置 id list + setIdList.add(detecInfo[DetectInfo.SETINFOID]); + setIdList.add(seqList); + List checkTypeList = new LinkedList(); + checkTypeList.add(checkType); + checkTypeList.add(setIdList); + dataList.add(checkTypeList); + } + } + } + } + + + /** + * 组织detail表的insert语句 + * @param fieldNames + * @param detailTableName + * @param tempMap + * @return + */ + public String createDetailSql(List fieldNames, + String detailTableName, Map tempMap) { + String insertDetailSql; + StringBuffer preSql = new StringBuffer(); + StringBuffer sufSql = new StringBuffer(); + preSql.append(",detection_info_id"); + sufSql.append(",?"); + fieldNames.add("detection_info_id"); + for(String tempField : tempMap.keySet()){ + fieldNames.add(tempField); + preSql.append("," + tempField); + String value = tempMap.get(tempField); + sufSql.append(",?"); + } + insertDetailSql = "insert into "+detailTableName +" ( "+ preSql.substring(1) +" ) values (" + sufSql.substring(1) +" )"; + return insertDetailSql; + } + + /** + * 状态变更判断:只对大于等于new表监测时间的监测数据进行是否告警的判断 + * @param allDetectInfo + * @param inDbInfo + */ + public void judgeState(List allDetectInfo , Object[] inDbInfo){ + /*判断每一个的状态变更 + * 1.监测时间小于已经入库的监测数据时间不做判断 + * */ + int startIndex = 0; + for(int i = 0, j = allDetectInfo.size(); i < j ; i++){ + Object[] newDetecInfo = allDetectInfo.get(i); + Object[] oldDetecInfo = null;//前一条监测数据 + //监测时间小于已经入库的监测数据时间不做判断,只在最后一条数据判断插入告警信息 + if( newDetecInfo== null || (Long) newDetecInfo[DetectInfo.CHECKTIME] < (Long)inDbInfo[DetectInfo.CHECKTIME]){ + startIndex++; + if(i < j -1){ + continue; + }else{ + //集合中的最后一条数据监测时间也小于数据库中的时间时,最后一条数据判断是否插入一条告警信息 + if((Integer)newDetecInfo[DetectInfo.STATE] != 1){//延迟数据监测状态没有成功,数据库入库的是正常数据,补录一条报警信息 + if((Integer)inDbInfo[DetectInfo.STATE] == 1 && (Integer)inDbInfo[DetectInfo.PLEVEL] == 99){ + newDetecInfo[DetectInfo.APPENDWARNINGINFO] = oldDetecInfo; + warnResumeNum++; + } + } + } + } + //判断最新的监测数据 + if(i== startIndex){ + oldDetecInfo = inDbInfo; + }else{ + oldDetecInfo = allDetectInfo.get(i-1); + } + /*判断状态变更*/ + stateChange(newDetecInfo, oldDetecInfo); + } + + } + /** + * 判断状态变更具体实现 + * @param newDetecInfo + * @param oldDetecInfo + */ + public void stateChange(Object[] newDetecInfo,Object[] oldDetecInfo ){ + /** + * 判断状态变更 + */ + + if(newDetecInfo == null || oldDetecInfo == null + || ((Long)newDetecInfo[DetectInfo.CHECKTIME] < (Long)oldDetecInfo[DetectInfo.CHECKTIME])){ + return; + } + if(((Integer)oldDetecInfo[DetectInfo.STATE] != (Integer)newDetecInfo[DetectInfo.STATE]) || //状态改变 + (((Integer)oldDetecInfo[DetectInfo.STATE] == (Integer)newDetecInfo[DetectInfo.STATE] + && (Integer)oldDetecInfo[DetectInfo.PLEVEL]!= (Integer)newDetecInfo[DetectInfo.PLEVEL] ))){//状态未变,告警级别改变 + + int state = (Integer)newDetecInfo[DetectInfo.STATE]; + //邮件告警信息 + StringBuffer alarmInfo = new StringBuffer((String)newDetecInfo[DetectInfo.ALARMINFO]);//告警信息 + String dsinfo = (String) newDetecInfo[DetectInfo.DSINFO];//状态信息 + String checkType = (String) newDetecInfo[DetectInfo.CHECKTYPE];//监测类别 + long checkTime = (Long) newDetecInfo[DetectInfo.CHECKTIME]; + int actionType = 11; + int urgentLevel = EmailTypeConstants.URGENT_LATER; + //整理告警数据,拼写邮件信息 + if(state == -1){ //执行失败 +// alarmInfo.append("监测执行失败\n "); +// alarmInfo.append("Failure to monitor execution\n "); + alarmInfo.append("i18n_server.DetecDataResoveThread.alarmInfo1_n81i\n "); + alarmInfo.append(dsinfo+"\n"); + actionType= EmailTypeConstants.TYPE_DETECTION_INFO_EXCEPTION; + if(Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + + } + }else if(state == 0){ //一般异常 + alarmInfo.append(" "+dsinfo+"\n"); + //如果是握手监测,不添加告警设置字段相关信息 + if(!Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + alarmInfo.append(dsinfo+" \n"); + }else { + alarmInfo.delete(0,alarmInfo.length());//如果是握手监测,则不添加告警设置字段相关信息 + alarmInfo.append(dsinfo+" \n"); + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + } + actionType= EmailTypeConstants.TYPE_DETECTION_INFO_EXCEPTION; + }else if(state == 1){ //恢复正常 +// alarmInfo.append("监测恢复正常"); +// alarmInfo.append("Monitoring back to normal"); + alarmInfo.append("i18n_server.DetecDataResoveThread.alarmInfo1_n81i"); + actionType= EmailTypeConstants.TYPE_DETECTION_INFO_RECOVER; + urgentLevel = EmailTypeConstants.URGENT_LATER; + //更新该节点所有监测时间为当前监测时间 + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + newDetecInfo[DetectInfo.STATUSCHANGTIME] = (checkTime); + } + } + // 报警通知 + + newDetecInfo[DetectInfo.STATECHANGEFLAG] = (true); + newDetecInfo[DetectInfo.ALARMINFO] = (alarmInfo.toString()); + newDetecInfo[DetectInfo.SENDEMAILFLAG] = (true); + newDetecInfo[DetectInfo.URGENTLEVEL] = (urgentLevel); + //目前代码中是否紧急和紧急级别不对应,有的是紧急,但是级别是2,所以此处进行修改,如果是紧急,则级别修改为0级 + if(EmailTypeConstants.URGENT_IMMEDIATELY==urgentLevel){ + newDetecInfo[DetectInfo.PLEVEL] = Constants.LEVEL_OF_EMERGENCY; + } + //如果是0级,则设置为紧急,使得紧急状态和级别一致 + if(newDetecInfo[DetectInfo.PLEVEL].equals(Constants.LEVEL_OF_EMERGENCY)){ + newDetecInfo[DetectInfo.URGENTLEVEL] = EmailTypeConstants.URGENT_IMMEDIATELY; + } + long seqIdLong = Long.parseLong((String)newDetecInfo[DetectInfo.SEQID]); + long setInfoIdLong = Long.parseLong((String)newDetecInfo[DetectInfo.SETINFOID]); + + String emailContent = alarmInfo.toString(); + if(emailContent.getBytes().length>290){ + emailContent = StringUtil.substring(emailContent,290); + } + + EmailInfo emailInfo = new EmailInfo(actionType,Common.getSetInfoNameMape().get(setInfoIdLong)+"("+Common.getCheckTypeNameMape().get(checkType)+")",Common.getNodeIpByUUID(seqIdLong), format2.format(new Date(checkTime)),emailContent,EmailTypeConstants.FLAG_SEND_LATER,urgentLevel); + + newDetecInfo[DetectInfo.EMAILINFO] = (emailInfo); + //如果新入数据和new表中的监测状态不同,或者监测状态相同,告警级别不同 ,修改 状态改变时间 为获取新入数据的时间(data_check_time) + newDetecInfo[DetectInfo.STATUSCHANGTIME]=newDetecInfo[DetectInfo.CHECKTIME]; + } + else{//如果新入数据和new表中的监测状态相同,且告警级别相同 ,则状态改变时间更新为new表中的时间 + newDetecInfo[DetectInfo.STATUSCHANGTIME]=oldDetecInfo[DetectInfo.STATUSCHANGTIME]; + } + } + /** + * id 和 currentId 同时 = -1时,为第一次获取sequence id + * result[0] 为currentID + * result[1] 为基准id,获取的sequence id + * @param id + * @param currentId + * @return + */ + public long getSequenceId(CommonService service){ + + long result = -1L; + if( infoId != -1 && currentId != -1 && currentId < (infoId + interval -1) && interval != -1){ + currentId ++; + result = currentId; + }else{ + //-- 申请sequences for detection_info + long[] temp = service.getDetectionInfoSequenceID(); + if(temp[0] > 0){ + result = infoId = currentId = temp[0];//获取的id + interval = temp[1];//步进 + } + } + return result; + } + + + /** + * + * 获取文件路径,判断当天目录下的文件如果>=500个,则新创建父目录, + * 创建父目录的原则是"日期_1"、"日期_2"、"日期_3"累加的形式 + * + * @author jsj Apr 28, 2013 + * @version 1.0 + * @return + */ + private String getFilePath() + { + String filePath = ""; + try + { + String parentDirPath = getParentDirPath(); + filePath = parentDirPath + "/" + Calendar.getInstance().getTimeInMillis() + ".csv"; + + } catch (Exception e) + { + logger.error("Getting the error file path exception", e); + } + return filePath; + } + + /** + * + *获取文件的父目录 + * 获取当天的父母目录,如果父目录不存在,则为第一次写入文件,直接返回当天的目录, + 存在则判断目录下的文件是否>-500,>=500则新建日期目录,否则取当前目录 + * @author jsj Apr 28, 2013 + * @version 1.0 + * @return + */ + private String getParentDirPath() + { + String parentDirPath = Constants.ERROR_DETEC_FILE_DIR; + String currentDirName = Common.getDateDirName(); + String dirPath = parentDirPath + "/" + currentDirName; + File parentDirFile = new File(parentDirPath); + List existDir = new ArrayList(); + + if (parentDirFile.exists()) + { + File[] dirFiles = parentDirFile.listFiles(); + if (dirFiles.length > 0) + { + for (File dirFile: dirFiles) + { + if (dirFile.getName().contains(Common.getDateDirName())) + { + existDir.add(dirFile); + } + } + } + + } else + { + return dirPath; + } + + boolean isGetFile = false; + + for (File dirFile: existDir) + { + File[] files = dirFile.listFiles(); + if (currentDirName.compareTo(dirFile.getName()) < 0) + { + currentDirName = dirFile.getName(); + } + if (files.length < Constants.ERROR_DETEC_FILE_DIR_FILE_SIZES) + { + dirPath = dirFile.getAbsolutePath(); + isGetFile = true; + break; + } + } + + if (!isGetFile) + { + String[] currentDirNames = currentDirName.split("_"); + if (currentDirNames.length == 1) + { + currentDirName += "_1"; + } else + { + currentDirName = currentDirNames[0] + "_" + + +(Integer.parseInt(currentDirNames[1]) + 1); + } + dirPath = parentDirPath + "/" + currentDirName; + } + return dirPath; + } + + + //检查入库的监测数据中是否有无效的数据,有则置为无效 + public void updateDetectDataValid(CommonDao dao) throws Exception{ + CommonService service = new CommonService(dao); + Map detectSetSeqIdMap = service.getDetectSetInfo();//查询有效监测数值对应的seqid + Map detectDataSeqIdMap = service.getDetectSeq(Constants.DETECTION_INFO_TABLE_NAME);//统计 监测数据 表: 监测设置<->已有有效数据的seqid + Iterator detectDateSeqIte = detectDataSeqIdMap.entrySet().iterator(); + String invalidSeqIds = ""; + List sqls = new ArrayList(); + while(detectDateSeqIte.hasNext()) { + invalidSeqIds = ""; + Entry en = (Entry)detectDateSeqIte.next(); + String setId = (String)en.getKey(); + String seqIdsValid = detectSetSeqIdMap.get(setId);//得到监测设置当前有效的seqids:1,2,3 + if(seqIdsValid!=null) {//如果有效的监测设置内没有此监测设置,则不予处理 + seqIdsValid = "," + seqIdsValid + ","; + String[] seqIds = (String[])en.getValue(); + for(String seqIdTmp:seqIds) { + if(seqIdTmp!=null && !"".equals(seqIdTmp)) { + if(!seqIdsValid.contains(","+seqIdTmp+",")) {//不在有效范围内,则更新为无效 + invalidSeqIds = invalidSeqIds + seqIdTmp + ","; + } + } + } + if(invalidSeqIds.endsWith(",")) { + invalidSeqIds = invalidSeqIds.substring(0,invalidSeqIds.length()-1); + sqls.add("update "+Constants.DETECTION_INFO_TABLE_NAME+" t set valid=0 where t.valid=1 and t.detection_set_info_id="+setId+" and t.seq_id in ("+invalidSeqIds+")"); + } + } + } + if(sqls.size()>0){//一般情况下不需要更新,所以,如果不加if判断,则每次都会浪费时间去创建statement + dao.dbUpdateByBatch(sqls); + } + + } + + + + /** + *打印统计的监测数据:监测类别--监测设置--节点--监测信息 + */ + private void printDetectDataInfo(Map>>> newDataMap) { + /** + *打印统计的监测数据:监测类别--监测设置--节点--监测信息 + */ + if(newDataMap != null && newDataMap.size()>0){ + logger.debug("解析完毕:共" +newDataMap.size()+ "个监测类型"); + for(Map.Entry>>> setInfoIdMap :newDataMap.entrySet()){ + logger.debug("监测类型: " + setInfoIdMap.getKey() + " 共有:" + setInfoIdMap.getValue().size() +" 个监测设置项"); + if(setInfoIdMap.getValue() != null){ + for(Map.Entry>> me : setInfoIdMap.getValue().entrySet() ){ + if(me.getValue() != null){ + logger.debug("监测设置id: " + me.getKey() + " 共包含:" + me.getValue().size() +" 节点"); + for(Map.Entry> seqMe : me.getValue().entrySet()){ + if(seqMe.getValue() != null){ + logger.debug("seqId : " + seqMe.getKey() + " 共包含:" + seqMe.getValue().size() +" 条监测数据"); + } + } + } + } + } + } + } + } + + /** + * 将解析之后的监测数据写入到文件 + * @param temInfo + * @param file + * @return + */ + private byte[] objectToString(Object[] temInfo){ + byte[] dsb = null; + try { + // ------- 总监测数据组织 + int index = 0; + String[] totalData = new String[12]; + totalData[index++] = (String) temInfo[DetectInfo.SEQID];// UUID + totalData[index++] = (String) temInfo[DetectInfo.SETINFOID];// 监测设置ID + String checkType = totalData[index++] = (String) temInfo[DetectInfo.CHECKTYPE];// 监测类别 + totalData[index++] = (String) temInfo[DetectInfo.PROCESS];// 进程名称 + totalData[index++] = temInfo[DetectInfo.STARTTIME] +"";// 监测服务启动时间 + totalData[index++] = temInfo[DetectInfo.DELAYTIME] +"";// 检测时延(秒) + totalData[index++] = temInfo[DetectInfo.CHECKTIME]+ "";// 本次检测时间 + totalData[index++] = temInfo[DetectInfo.CURRENTTIMES] + "";// 尝试次数 + totalData[index++] = temInfo[DetectInfo.NEXTCHECKTIME]+"";// 下次计划监测时间 + totalData[index++] = temInfo[DetectInfo.STATE] + "";// 执行状态是否成功是否正常 + totalData[index++] = temInfo[DetectInfo.DSINFO] +"";// 状态信息(描述信息) + totalData[index++] = temInfo[DetectInfo.PDATA] +"";// 性能数据 + + /** + * 详细数据前三个告警相关信息 + */ + List showNumList = (List) temInfo[DetectInfo.SHOWNUMLIST]; + List plevelList = (List) temInfo[DetectInfo.PLEVELLIST]; + List alarmList = (List) temInfo[DetectInfo.ALARMLIST]; + + List dataList = new LinkedList(); + // 总数据 + dataList.add(totalData); + + //详细信息 + List> details = (List>) temInfo[DetectInfo.DETAILS]; + if(details != null && details.size() > 0){ + Map tableMap = Common.getTableMap().get(checkType); + Map fieldMap = fieldNameToLower(tableMap);//将表字段名称转换成小写 + // 详细信息 + dataList.add(new String[]{"details", details.size()+ ""});// details(解析标识),详细信息条数 + List detailDataList = new LinkedList(); + int listIndex = 0; + for(Map temp : details){ + //单条详细信息 + String[] tempDetail = new String[tableMap.size() + 4]; + tempDetail[0] = showNumList.get(listIndex); + tempDetail[1] = plevelList.get(listIndex); + tempDetail[2] = alarmList.get(listIndex); + for(Map.Entry tempEntry : temp.entrySet()){ + String fieldName = tempEntry.getKey().toLowerCase(); + if("seq_id".equals(fieldName) || //解析时添加的数据跳过 + "detectioned_state".equals(fieldName) || + "detection_set_info_id".equals(fieldName) || + "data_check_time_digital".equals(fieldName) || + "data_check_time".equals(fieldName) || + "data_arrive_time_digital".equals(fieldName) || + "data_arrive_time".equals(fieldName) ){ + continue; + } + String value = tempEntry.getValue(); + int arrIndex = fieldMap.get(fieldName) + 2; + tempDetail[arrIndex] = value; + } + detailDataList.add(tempDetail); + listIndex ++; + } + + dataList.addAll(detailDataList);// 具体的详细数据 + + // 特殊数据定制(目前是针对系统信息监测systeminfo) + if ("systeminfo".equalsIgnoreCase(checkType)) { + List netList = (List) temInfo[DetectInfo.NETINFOLIST]; + if(netList != null && netList.size() > 0){ + dataList.add(new String[]{"net", netList.size() + ""});// 解析标识, 行数(当前解析标识指定的类型有多少条数据) + dataList.addAll(netList);// 多条数据 + } + List diskList = (List) temInfo[DetectInfo.DISKINFOLIST]; + if(netList != null && diskList.size() > 0){ + dataList.add(new String[]{"disk", netList.size() + ""});// 解析标识, 行数(当前解析标识指定的类型有多少条数据) + dataList.addAll(diskList);// 多条数据 + } + } + } + dsb = CSVUtils.csvBytesPrinter1(dataList, Constants.COMMON_TEXT_CODING); + } catch (Exception e) { + e.printStackTrace(); + logger.error("Conversion from Object[] to byte[] failure",e); + } + return dsb; + } + /** + * 表字段名称全部转换成小写,并fieldName做key + * @param tableMap + * @return + */ + private Map fieldNameToLower(Map tableMap){ + Map result = new HashMap(); + if(tableMap != null && tableMap.size()>0){ + for(Map.Entry temp : tableMap.entrySet()){ + TableColumnsModel column = temp.getValue(); + if(column != null){ + result.put(column.getFiledName().toLowerCase(), temp.getKey()); + } + } + } + return result; + } + + + private List generateLog(byte[] info,String head) { + List warnInfoList = new ArrayList(); + try + { + //byte[] info = (byte[]) temInfo[27]; + List strsList = CSVUtils.csvBytesParser(info,Constants.COMMON_TEXT_CODING); + String time = System.currentTimeMillis()+""; + for(String[] strArr:strsList) { +// logger.info(head+" "+time +" "+StringUtils.join(strArr,",")); + warnInfoList.add(head+" "+time +" "+StringUtils.join(strArr,",")); + } + + } catch (Exception e){ + } + return warnInfoList; + } + + private void printLogs(List logList) { + for(String log:logList){ + logger.info(log); + } + } + private void judgeAndCollect(List allDeteInfo, Object[] oldDetecInfo,List newDataList,CommonService service){ + Object[] newDeteInfo = allDeteInfo.get(0); + if((Long)newDeteInfo[DetectInfo.CHECKTIME]>(Long)oldDetecInfo[DetectInfo.CHECKTIME]){ + newDataList.add(newDeteInfo); + } + if((Integer)newDeteInfo[DetectInfo.STATE]==1){ + Object[] params={newDeteInfo[DetectInfo.SEQID],newDeteInfo[DetectInfo.SETINFOID]}; + try { + service.addRecordToStatement(warnUpdateStmt, params); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + public static void main(String[] args) { + + } + + +} diff --git a/src/com/nms/server/thread/detectDataHandler/DataInsertManagerThread.java b/src/com/nms/server/thread/detectDataHandler/DataInsertManagerThread.java new file mode 100644 index 0000000..3543d93 --- /dev/null +++ b/src/com/nms/server/thread/detectDataHandler/DataInsertManagerThread.java @@ -0,0 +1,51 @@ +package com.nms.server.thread.detectDataHandler; + +import java.util.Hashtable; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.LinkedBlockingDeque; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.DetectInfo; +import com.nms.server.common.Common; + +public class DataInsertManagerThread implements Runnable{ + private static final Logger logger = Logger.getLogger(DataInsertManagerThread.class); + + @Override + @SuppressWarnings("rawtypes") + public void run() { + Thread.currentThread().setName("DataInsertManagerThread"); + try { + //遍历 监测数据等待入库 table + Hashtable hb = Common.DETECT_QUEUE; + Set> es = hb.entrySet(); + Iterator> ite = es.iterator(); + while(ite.hasNext() && ! Common.isStop() && ! Thread.currentThread().isInterrupted() ){ + Entry next = ite.next(); + String key = next.getKey(); + if(DetectInfo.INFO_KEY.equalsIgnoreCase(key)){ + //info 表的数据此线程不做解析 + continue; + } + LinkedBlockingDeque> data = next.getValue(); + List> list = new LinkedList<>(); + int total = data.drainTo(list); + if(total > 0){ + Common.dataResoveService.submit(new DetectDetailInsertThread(key, list)); + logger.info(key +"->" + total); + }else{ + logger.info(key + " 没有监测详细数据"); + } + } + } catch (Exception e) { + logger.error("DataInsertManagerThread error",e); + } + } + +} diff --git a/src/com/nms/server/thread/detectDataHandler/DataResolveThread.java b/src/com/nms/server/thread/detectDataHandler/DataResolveThread.java new file mode 100644 index 0000000..e82848c --- /dev/null +++ b/src/com/nms/server/thread/detectDataHandler/DataResolveThread.java @@ -0,0 +1,1263 @@ +package com.nms.server.thread.detectDataHandler; + +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.fang.lang.Db; +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.TableColumnsModel; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.common.EmailTypeConstants; +import com.nms.server.service.CommonService; +import com.nms.server.util.CSVUtils; +import com.nms.server.util.FileUtils; +import com.nms.server.util.StringUtil; + +/** + * 数据解析线程 + * 1、从队列中获取数据 + * 2、计算监测数据id,十位时间戳 + uuid + setId + * 3、根据监测类别存放到响应的队列 + * @author nanfang + * + */ +public class DataResolveThread implements Runnable{ + private static Logger logger = Logger.getLogger(DataResolveThread.class); + private final SimpleDateFormat format = Common.getFormat(); //Java Date 类型数据格式化格式 + private static Db db = Common.getDb(); + + + @Override + public void run() { + Thread.currentThread().setName("Data Resolve Thread"); + logger.info("监测数据解析线程开始运行"); + while(!Common.isStop() && ! Thread.currentThread().isInterrupted()){ + try { + //从缓存队列中获取,阻塞等待 + byte[] data = Common.takeDeteDataQueue(); + if(data != null && data.length > 0){ + Queue queue = Common.DETECT_QUEUE.get(DetectInfo.INFO_KEY); + if(queue != null && queue.size() > Constants.MAX_DETECT_DATA_RESOVE_LIMIT_NUM){ + //内存缓存达到阈值,保存到硬盘 + logger.warn("Excesive monitoring data:"+queue.size()+"pieces, data written to the hard disk"); + saveByteToFile(data); + }else{ + //csv解析 + List strsList = CSVUtils.csvBytesParser(data,Constants.COMMON_TEXT_CODING); + if(strsList != null && strsList.size() > 0){ + Object[] result = resolveData(strsList);//解析之后的数据 + if(result != null){ + String checkType = (String)result[DetectInfo.CHECKTYPE]; + //将详细监测数据放到缓存队列 + List> detaiList = (List>)result[DetectInfo.DETAILS]; + if(detaiList != null && detaiList.size() >0){ + Common.putAllDetectQueue(checkType, detaiList); + result[DetectInfo.DETAILS] = null; + } + + //特殊监测数据 + if(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType) ) { + //网卡 + List> netList = (List>)result[DetectInfo.NETINFOLIST]; + if(netList != null && netList.size() > 0){ + Common.putAllDetectQueue(DetectInfo.NET_STR_KEY, netList); + result[DetectInfo.NETINFOLIST] = null; + } + + //磁盘 + List> diskList = (List>)result[DetectInfo.DISKINFOLIST]; + if(diskList != null && diskList.size() > 0){ + Common.putAllDetectQueue(DetectInfo.DISK_STR_KEY, diskList); + result[DetectInfo.DISKINFOLIST] = null; + } + } + //info 信息放到队列 + Common.putDetectInfoQueue(result); + } + } + } + } + } catch (Exception e) { + logger.error("",e); + } + } + + logger.info("线程停止前将所有缓存数据写到文件"); + byte[] data = null; + while((data = Common.pollDeteDataQueue())!=null){ + saveByteToFile(data); + } + logger.info("监测数据解析线程停止运行"); + } + + + + /** + * 解析监测数据,监测数据有两种类型:旧格式(15),新格式(12) + * @param list + * @return + * @throws Exception + */ + public Object[] resolveData(List list) throws Exception{ + Object[] detecInfo = null; + String [] detailsStr = list.get(0); //取第一行数据 + if(detailsStr != null){ + if(detailsStr.length > 12){//旧格式 解析 + logger.debug("旧格式数据"); + detecInfo = resoveOldData(list);//解析数据,不判断状态变更 + }else if(detailsStr.length<12){ + logger.debug( "基本数据解析长度:"+detailsStr.length+" 小于最小解析标准,不予解析;"); + }else{//新格式 解析 + logger.debug("新格式数据"); + detecInfo = resoveNewData(list);//解析数据,不判断状态变更 + } + } + return detecInfo; + } + + + + /** + * 根据 seqId 获取 状态有效的端口名称 + * @param seqId + * @return + */ + public static List getPortNameBySeqId(String seqId) { + logger.debug("获取端口名称:seqId = " + seqId ); + List result = null; + //查询detection_info_new数据 + StringBuffer sql = new StringBuffer(); + sql.append(" select t.name_flag from node_figure_info t "); + sql.append(" where (t.port_set_state = 1 or t.port_set_state is null) "); + sql.append(" and seqid = ? " ); + sql.append(" and t.name_flag is not null and t.is_leaf = 1 "); + try { + List> list = db.select(sql.toString(), seqId); + if(list != null && list.size() > 0){ + result = new ArrayList(list.size()); + for(Map m : list){ + result.add((String)m.get("name_flag")); + } + } + } catch (SQLException e) { + logger.warn("",e); + } + return result; + } + + + + /** + * 详细数据解析 + * 第一步:解析公共数据部分,保存各字段值到Map publicInfo 中 + * 第二步:解析详细数据部分,保存各字段到 Map detail 中 + * 第三步:检查告警信息数据,多字段告警去最高级(Level最小) + * 第四步:查询数据库中最新同类监测记录,并判断状态是否变化(同类判定:seq_id、detection_set_info_id,变化判定:DETECTIONED_STATE 和 POLICE_LEVEL 至少一个变化) + * 第五步:根据告警信息数据,拼写告警信息 + * 第六步:状态变更时发送邮件,将告警信息邮件发送 + * 第七步:保存到基本信息、非正常数据和状态变更数据信息保存到告警表中 + * 完成... + * @time Sep 8, 2011-3:40:45 PM + * @param datas + * @param dao + */ + public Object[] resoveOldData(List strsList) throws Exception{ + /* 监测数据有效判断*/ + if(strsList==null || strsList.size()==0){ + logger.debug("旧格式监测数据解析:数据内容为空,不予解析;"); + return null; + } + //监测解析结果 + Object[] detectInfo = null; + Iterator strsIte = strsList.iterator(); + String[] datas = null; + if(strsIte!=null && strsIte.hasNext()) { + datas = strsIte.next();//获得第一行基本信息 + } + if(datas==null){ + logger.debug("旧格式监测数据解析:数据内容为空,不予解析;"); + return null; + }else if(datas.length>0){/* 数据日志尝试输出 */ + String dateTime = null; + if(datas.length>6){ //第7字段 为监测时间字段 作展示标记 + dateTime = format.format(Long.parseLong(datas[6])); + } + logger.debug("监测数据-旧格式 监测时间》"+dateTime+" 内容》"+Arrays.toString(datas)); + /* 监测数据有效判断 */ + if(datas.length<15){ + logger.debug("旧格式监测数据解析:数据解析长度:"+datas.length+" 小于最小解析标准,不予解析;"); + return null; + } + /** + * 公共部分解析格式: + * seqId, + * 监测设置ID, + * 监测类别, + * 进程名称(监测类别设置名称), + * 监测服务启动时间, + * 检测时延(秒), + * 本次检测时间, + * 尝试次数, + * 下次计划监测时间, + * 执行状态是否成功是否正常, + * 告警列序号, + * 告警级别, + * 告警值, + * 状态信息(描述信息), + * 性能数据 + * (,详细数据部分).... + **/ + /* 基本信息解析格式变量 */ + String seqId = null; //seqId + String setInfoId = null; //监测设置ID + String checkType = null; //监测类别 + String process = null; //进程名称(监测类别设置名称) + Long startTime = null; //监测服务启动时间 + Integer delayTime = null; //检测时延(秒) + Long checkTime = null; //本次检测时间 + Integer currentTimes = null; //尝试次数 + Long nextCheckTime = null; //下次计划监测时间 + int state = -1; //执行状态是否成功是否正常 + String showNums = null; //告警序列(数据) + String pLevel = null; //告警级别(数据) + int pl = 99; //告警级别(入库) + String pValue = null; //告 警 值(数据) + String dsinfo = null; //状态信息(描述信息) + String pdata = null; //性能数据 + + + /* 其他变量 */ + StringBuffer errorInfo= new StringBuffer(); //数据文件错误,信息整理 + boolean delyFlag = true; //详细数据获取标识 + //Long stateChangeTime = null;//状态变更时间 + + /* 第一步:解析公共数据部分 */ + try { + //--seqId, + seqId = datas[0]; + if(StringUtils.isEmpty(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不能为空;"); + errorInfo.append("Location"+(0)+"data node seqId (physical host ID) cannot be empty;"); + }else if(!StringUtil.isNumber(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不是正整数:"+seqId); + errorInfo.append("Location"+(0)+"data node seqId (Physical Host ID) is not a positive integer:"+seqId); + } + //--监测设置ID, + setInfoId = datas[1]; + if(StringUtils.isEmpty(setInfoId)){ + errorInfo.append("位置"+(1)+"数据 监测设置ID不能为空;"); + }else if(!StringUtil.isNumber(setInfoId) && !"0".equals(setInfoId)){ + errorInfo.append("位置"+(1)+"数据 监测设置ID不是整数(包括0):"+setInfoId); + } + //--监测类别 + checkType = datas[2]; + if(StringUtils.isEmpty(checkType)){ + errorInfo.append("位置"+(2)+"数据 监测类别不能为空;"); + } + //--进程名称(检查类别设置名称), + process = datas[3]; + //--监测服务启动时间, + String startTimeStr = datas[4]; + if(StringUtils.isNotEmpty(startTimeStr)){ + try { + startTime = Long.parseLong(startTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(4)+"数据 “"+startTime+"” 监测服务启动时间须为整形数:"+startTime); + } + }else { + errorInfo.append("位置"+(4)+"数据 监测服务启动时间不能为空;"); + } + //--检测时延(秒), + String delayTimeStr = datas[5]; + if(StringUtils.isNotEmpty(delayTimeStr)){ + try { + delayTime = Integer.parseInt(delayTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(5)+"数据 “"+delayTime+"” 检测时延(秒)须为整形数:"+delayTime); + } + } + //--本次检测时间, + String checkTimeStr = datas[6]; + if(StringUtils.isNotEmpty(checkTimeStr)){ + try { + checkTime = Long.parseLong(checkTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(6)+"数据 “"+checkTimeStr+"” 本次检测时间须为整形数:"+checkTime); + } + }else { + errorInfo.append("位置"+(6)+"数据 本次检测时间不能为空;"); + } + //--尝试次数, + String currentTimesSre = datas[7]; + if(StringUtils.isNotEmpty(currentTimesSre)){ + try { + currentTimes = Integer.parseInt(currentTimesSre); + } catch (Exception e) { + errorInfo.append("位置"+(7)+"数据 “"+currentTimesSre+"” 尝试次数须为整形数:"+currentTimes); + } + }else{ + errorInfo.append("位置"+(7)+"数据 尝试次数不能为空;"); + } + //--下次计划监测时间, + String nextCheckTimeStr = datas[8]; + if(StringUtils.isNotEmpty(nextCheckTimeStr)){ + try { + nextCheckTime = Long.parseLong(nextCheckTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(8)+"数据 “"+nextCheckTimeStr+"” 下次计划监测时间须为整形数:"+nextCheckTime); + } + }else{ + errorInfo.append("位置"+(8)+"数据 尝试次数不能为空;"); + } + //--执行状态是否成功,记录的状态是否正常(用于报警 -1执行失败 0不正常,1正常), + String stateStr = datas[9]; + if(StringUtils.isNotEmpty(stateStr)){ + try { + state = Integer.parseInt(stateStr); + } catch (Exception e) { + errorInfo.append("位置"+(9)+"数据 “"+stateStr+"” 执行状态须为整形数:"+state); + } + }else{ + errorInfo.append("位置"+(9)+"数据 执行状态不能为空;"); + } + //--告警序列 + showNums = datas[10]; + //--告警级别 + pLevel = datas[11]; + String [] nums0 = pLevel.split("\\|"); + for(String num : nums0){ + if(StringUtils.isEmpty(num)){ + continue; + } + int level = Integer.parseInt(num); + if(pl>level){ + pl = level; + } + } + //失败 最高级 + if(state == -1){ + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + pl = Constants.DETECT_FAIL_NMSC_POLICE_LEVEL; + }else { + pl = Constants.DETECT_FAIL_NON_NMSC_POLICE_LEVEL; + } + } + //-- 告警值 + pValue = datas[12]; + //--状态信息(描述信息), + dsinfo = datas[13]; + if(StringUtils.isNotEmpty(dsinfo)){ + // add by zg 增加对
的处理 begin + dsinfo = dsinfo.replaceAll("
", "\n"); + // add by zg 增加对
的处理 end + if(dsinfo.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + dsinfo = StringUtil.substring(dsinfo,Constants.VARCHAR_MAX_LENGTH); + } + } + //--性能数据 + pdata = datas[14]; + if(StringUtils.isNotEmpty(pdata)){ + // add by zg 增加对
的处理 begin + pdata = pdata.replaceAll("
", "\n"); + // add by zg 增加对
的处理 end + if(pdata.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + pdata = StringUtil.substring(pdata,Constants.VARCHAR_MAX_LENGTH); + } + }else {//界面异常信息列表的状态信息显示为性能数据,所以如果性能数据为空,则取状态信息 + pdata = dsinfo; + } + } catch (Exception e) { + String s = "基本数据部分解析失败"+e.getMessage(); + logger.warn(s); + errorInfo.append(s); + } + //--基本信息解析 存在异常值 无须保存,返回错误信息 用于系统告警 + if(errorInfo.length() != 0){ + logger.error(" 第一步-1: 旧格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); + return null; + } + //-- 根据执行状态判断是否需要获取详细数据的信息 及报警信息 + if(state < 0){ //失败 报警only + delyFlag = false; + } + Calendar arriveTime = Calendar.getInstance(); + detectInfo = new Object[33]; + //detectInfo[DetectInfo.null] = (CSVUtils.csvBytesPrinter1(strsList, Constants.COMMON_TEXT_CODING)); + detectInfo[DetectInfo.SEQID] = (seqId); + detectInfo[DetectInfo.SETINFOID] = (setInfoId); + detectInfo[DetectInfo.STARTTIME] = (startTime); + detectInfo[DetectInfo.DELAYTIME] = (delayTime); + detectInfo[DetectInfo.CHECKTIME] = (checkTime); + detectInfo[DetectInfo.CURRENTTIMES] = (currentTimes); + detectInfo[DetectInfo.NEXTCHECKTIME] = (nextCheckTime); + detectInfo[DetectInfo.CHECKTYPE] = (checkType); + detectInfo[DetectInfo.PDATA] = (pdata); + detectInfo[DetectInfo.ARRIVETIME] = (arriveTime.getTimeInMillis()); + detectInfo[DetectInfo.STATE] = (state); + detectInfo[DetectInfo.PLEVEL] = (pl); + detectInfo[DetectInfo.DSINFO] = (dsinfo); + detectInfo[DetectInfo.PROCESS] = process; + + /** + * 储存详细数据前三个字段,恢复数据时使用 + */ + List showNumList = new LinkedList(); + List plevelList = new LinkedList(); + List alarmList = new LinkedList(); + + showNumList.add(showNums); + plevelList.add(pLevel); + alarmList.add(pValue); + + detectInfo[DetectInfo.SHOWNUMLIST] = (showNumList); + detectInfo[DetectInfo.PLEVELLIST] = (plevelList); + detectInfo[DetectInfo.ALARMLIST] = (alarmList); + + + /* 第二步:解析详细数据部分 */ + Map detail = new HashMap(); //详细数据集合 + Map tableMap = Common.getTableMap().get(checkType); + if(tableMap ==null || tableMap.size() == 0){ + logger.error(""+checkType+"无法获取表结构"); + delyFlag = false; + } + List> detaiList = null; //详细数据集合 + if(delyFlag){ + detaiList = new ArrayList>();//初始化详细数据集合 + int index = 15; //数据集合解析游标 + try { + //-- 详细信息整理 监测具体数据信息(每个字段一列)。 + int i = 0 ; + while(detail.size() < tableMap.size()){ + TableColumnsModel field = tableMap.get(i); + ++i; + if((i-1)>tableMap.size()){ + break; + } + if(field!=null){ + //当监测数据(index == datas.length)时,及有效监测数据已经读取完成,tableMap页无需增加空字段数据 + if(index == datas.length){ + detail.put(field.getFiledName(), null); + continue; + } + try { + String fileType = field.getFiledType().toUpperCase(); + if("DATE".equals(fileType)){ + if(StringUtils.isNumeric(datas[index])){//是数字型时间时格式化 + detail.put(field.getFiledName(), format.format(new Date(Long.parseLong(datas[index].trim())))); + }else{//字符型时间 2016-06-17 08:02:02 + detail.put(field.getFiledName(), datas[index].trim()); + } + }else{ + if("NUMBER".equals(fileType)){ + if (StringUtils.isBlank(datas[index]) + || StringUtil.isRationalNumber(datas[index])) { + detail.put(field.getFiledName(), datas[index].trim()); + } else if (datas[index].trim().contains(" days, ")) { + detail.put(field.getFiledName(), CommonService.getLongTimeStr(datas[index].trim())); + } else { + throw new Exception("NUMBER数据格式错误!"); + } + }else{ + String length = fileType.replaceAll("VARCHAR2\\((\\d+)\\)", "$1"); + if(Integer.parseInt(length) < datas[index].trim().length()) { + throw new Exception("VARCHAR2数据格式错误!"); + } + detail.put(field.getFiledName(), datas[index].trim()); + } + } + } catch (Exception e) { + errorInfo.append(checkType+" 表字段集合信息解析失败;"); + errorInfo.append(field.getFiledName()).append("字段取值") + .append(datas[index]).append("发生错误;异常数据行:"); + for (String string : datas) { // 添加异常数据 + errorInfo.append(string).append(','); + } + errorInfo.deleteCharAt(errorInfo.length()-1); + //监测数据的详细信息如果和指定的字段类型不匹配,则抛弃此条监测数据,避免影响批量入库 + logger.error(" 第一步-1: 旧格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); + return null; + } + index++; + } + } + + detail.put("SEQ_ID", seqId); + detail.put("DETECTIONED_STATE", state+""); + detail.put("DETECTION_SET_INFO_ID", setInfoId+""); + detail.put("DATA_CHECK_TIME_DIGITAL", checkTime+""); // 测试时间 + detail.put("DATA_CHECK_TIME", format.format(new Date(checkTime))); // 测试时间 + detail.put("DATA_ARRIVE_TIME_DIGITAL", arriveTime.getTimeInMillis()+""); // 入库时间 + detail.put("DATA_ARRIVE_TIME",format.format(arriveTime.getTime())); // 入库时间 + detaiList.add(detail); + } catch (Exception e) { + String s = "详细数据部分解析失败"+e.getMessage(); + logger.warn(s); + } + } + + int urgentLevel = EmailTypeConstants.URGENT_LATER; + + //NMSC 监测失败状态 程序设定为为紧急 + if(state == -1 ){ + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + urgentLevel = Constants.DETECT_FAIL_NMSC_POLICE_EMERGENT; + }else { + urgentLevel = Constants.DETECT_FAIL_NON_NMSC_POLICE_EMERGENT; + } + } + /* + *1.整理告警信息 + *2.判断告警级别 + */ + //根据告警设置,获得当前告警紧急状态:由于之前的处理是当监测状态或者告警级别发生改变时,才发送邮件和根据告警设置来确定是否紧急,当第一次入库就异常时,是否紧急的判断就有问题了, + String [] nums = showNums.split("\\|"); + String [] levels = pLevel.split("\\|"); + String [] values = pValue.split("\\|"); + List aList = Common.getAlarmInfoMap().get(setInfoId); + Iterator iteAlarmList = null; + String cpInfoTmp = ""; + StringBuffer alarmInfo = new StringBuffer(); + if(state == 0){ + int pl0 = 99; + for(int i = 0 ; i=Integer.parseInt(levels[i].trim())){ + pl0 = Integer.parseInt(levels[i].trim()); + //告警紧急级别随告警级别走 + if(pl0==Constants.LEVEL_OF_EMERGENCY) { + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + }else { + urgentLevel = EmailTypeConstants.URGENT_LATER; + + } + } + } catch (Exception e) + { + logger.error("旧格式监测数据-修改正告警级别时,解析告警level:"+levels[i]+" 异常", e); + } + /** 字段名称 intDate 实际值: 80》60%(临界值) 级别 40 **/ + String filedName = ""; + String filedComments = ""; + if(tableMap !=null){ + TableColumnsModel tm = tableMap.get(elem.getShowNum()); + filedName = tm==null?"":tm.getFiledName(); + filedComments = tm==null?"":tm.getFiledComments(); + } + + cpInfoTmp += " "+(StringUtils.isEmpty(elem.getFiledCommonts())?filedName:elem.getFiledCommonts()); + cpInfoTmp += " 为:"+detail.get(filedName); + cpInfoTmp += ""+(elem.getPoliceUnit()==null ? "" : "null".equals(elem.getPoliceUnit()) ? "" : elem.getPoliceUnit()); + cpInfoTmp += " 报警级别:"+levels[i]; + cpInfoTmp += " \n"; + } + } + //如果是握手监测,不添加告警设置字段相关信息 + if(!Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + alarmInfo.append(cpInfoTmp); + } + } + } + + detectInfo[DetectInfo.ALARMINFO] = (alarmInfo.toString()); + detectInfo[DetectInfo.URGENTLEVEL] = (urgentLevel); + detectInfo[DetectInfo.DELYFLAG] = (delyFlag);//用于邮件发送 + detectInfo[DetectInfo.DETAILS] = (detaiList);//用于批量入库详细信息 + + //特殊追加信息解析--systeminfo-disk、net + //-- 特殊信息保存整理 + if(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType) ) { + while (strsIte.hasNext()) { + String [] detailsArr = strsIte.next(); //取出 + if(detailsArr == null || detailsArr.length ==0){ //空行不解析 + continue; + } + if(detailsArr.length != 2){ //不是[details:0]格式无法解析 + break; + } + + if("disk".equalsIgnoreCase(detailsArr[0])){ + List> infoList = new ArrayList>(); + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + disk:while (strsIte.hasNext()) { + endNum--; + String [] detailsArr0 = strsIte.next(); //取出 + int len = detailsArr0.length; + Map diskInfo = new HashMap(); + diskInfo.put("DISK_RANG", len>0 ? detailsArr0[0]:"");//盘符 + diskInfo.put("DISK_SIZE", len>1 ? detailsArr0[1]:"");//大小 + diskInfo.put("DATA_CHECK_TIME_DIGITAL", checkTime+"");//监测时间(long) + diskInfo.put("DATA_CHECK_TIME", format.format(new Date(checkTime)));//监测时间(date) + diskInfo.put("DATA_ARRIVE_TIME_DIGITAL", arriveTime.getTimeInMillis()+"");////录入记录时间(当前时间 long) + diskInfo.put("DATA_ARRIVE_TIME", format.format(arriveTime.getTime()));////录入记录时间(当前时间 date) + diskInfo.put("DETECTION_SET_INFO_ID", setInfoId);// + diskInfo.put("SEQ_ID", seqId); + diskInfo.put("DETECTIONED_STATE", state+""); + infoList.add(diskInfo); + if(endNum==0){ + logger.debug("disk解析完成 跳出循环"); + break disk; + } + } + if(infoList.size()>0) { + detectInfo[DetectInfo.DISKINFOLIST] = (infoList); + } + }else if("net".equalsIgnoreCase(detailsArr[0])) { + List> infoList = new ArrayList>(); + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + net:while (strsIte.hasNext()) { + endNum--; + String [] netInfoTmp = strsIte.next(); //取出 + int len = netInfoTmp.length; + Map netInfo = new HashMap(); + netInfo.put("NET_NAME", len>0 ? netInfoTmp[0]:"");//网卡名称 + netInfo.put("NET_STATE", len>1 ? netInfoTmp[1]:"");//网卡状态 + netInfo.put("NET_SPEED", len>2 ? netInfoTmp[2]:"");//网卡带宽 + netInfo.put("NET_IP", len>3 ? netInfoTmp[3]:"");//ip + netInfo.put("NET_SUBMASK", len>4 ? netInfoTmp[4]:"");//子网掩码 + netInfo.put("NET_GATEWAY", len>5 ? netInfoTmp[5]:"");//网关 + netInfo.put("NET_MAC", len>6 ? netInfoTmp[6]:"");//NET_MAC + netInfo.put("DATA_CHECK_TIME", format.format(new Date(checkTime))); + netInfo.put("DATA_ARRIVE_TIME", format.format(arriveTime.getTime()));//录入记录时间(当前时间 date) + netInfo.put("DATA_CHECK_TIME_DIGITAL", checkTime+"");//监测时间(long) + netInfo.put("DATA_ARRIVE_TIME_DIGITAL", arriveTime.getTimeInMillis()+"");//录入记录时间(当前时间 long) + netInfo.put("DETECTION_SET_INFO_ID", setInfoId);// id + netInfo.put("SEQ_ID", seqId); + netInfo.put("DETECTIONED_STATE", state+""); + infoList.add(netInfo); + if(endNum ==0){ + break net; + } + } + if(infoList.size()>0) { + detectInfo[DetectInfo.NETINFOLIST] = (infoList); + } + } + } + } + + } + return detectInfo; + } + + + + public Object[] resoveNewData(List strsList) throws Exception{ + /* 监测数据有效判断*/ + if(strsList==null || strsList.size()==0 + || strsList.get(0).length<12){ + return null; + } + + //监测解析结果 + Object[] detectInfo = null; + Iterator strsIte = strsList.iterator();//一条监测数据:有多行数据 + String [] datas = strsIte.next(); + + /** + * 公共部分解析格式: + * seqId, + * 监测设置ID, + * 监测类别, + * 进程名称(监测类别设置名称), + * 监测服务启动时间, + * 检测时延(秒), + * 本次检测时间, + * 尝试次数, + * 下次计划监测时间, + * 执行状态是否成功是否正常, + * 状态信息(描述信息), + * 性能数据 + **/ + + /* 基本信息解析格式变量 */ + String seqId = null; //seqId + String setInfoId = null; //监测设置ID + String checkType = null; //监测类别 + String process = null; //进程名称(监测类别设置名称) + Long startTime = null; //监测服务启动时间 + + Integer delayTime = null; //检测时延(秒) + Long checkTime = null; //本次检测时间 + Integer currentTimes = null; //尝试次数 + Long nextCheckTime = null; //下次计划监测时间 + int state = -1; //执行状态是否成功是否正常 + String dsinfo = null; //状态信息(描述信息) + String pdata = null; //性能数据 + + + /* 其他变量 */ + StringBuffer errorInfo= new StringBuffer(); //数据文件错误,信息整理 + boolean delyFlag = true; //详细数据获取标识 + Long stateChangeTime = null; //状态变更时间 + + /* 第一步:解析公共数据部分 */ + + try { + + //--seqId, + seqId = datas[0]; +// logger.debug("被监测节点seqId:"+seqId); + if(StringUtils.isEmpty(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不能为空;"); + errorInfo.append("Location"+(0)+"data node seqId (physical host ID) cannot be empty;"); + }else if(!StringUtil.isNumber(seqId)){ +// errorInfo.append("位置"+(0)+"数据 节点seqId(物理主机标识)不是正整数:"+seqId); + errorInfo.append("Location"+(0)+"data node seqId (physical host ID) is not a positive integer:"+seqId); + } + + //--监测设置ID, + setInfoId = datas[1]; +// logger.debug("监测项:"+setInfoId); + if(StringUtils.isEmpty(setInfoId)){ + errorInfo.append("Location"+(1)+"data Monitoring Settings ID cannot be emp;"); + }else if(!StringUtil.isNumber(setInfoId) && !"0".equals(setInfoId)){ +// errorInfo.append("位置"+(1)+"数据 监测设置ID不是整数(可以为0,默认的系统基础信息的设置就为0):"+setInfoId); + errorInfo.append("Location"+(1)+"Data Monitoring setting ID is not an integer (can be 0, the default system basic information is set to 0):"+setInfoId); + } + + //--监测类别 + checkType = datas[2]; + if(StringUtils.isEmpty(checkType)){ + errorInfo.append("位置"+(2)+"数据 监测类别不能为空;"); + } + + //--进程名称(检查类别设置名称), + process = datas[3]; + + //--监测服务启动时间, + String startTimeStr = datas[4]; + if(StringUtils.isNotEmpty(startTimeStr)){ + try { + startTime = Long.parseLong(startTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(4)+"数据 “"+startTime+"” 监测服务启动时间须为整形数:"+startTime); + } + }else { + errorInfo.append("位置"+(4)+"数据 监测服务启动时间不能为空;"); + } + + //--检测时延(秒), + String delayTimeStr = datas[5]; + if(StringUtils.isNotEmpty(delayTimeStr)){ + try { + delayTime = Integer.parseInt(delayTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(5)+"数据 “"+delayTime+"” 检测时延(秒)须为整形数:"+delayTime); + } + } + + //--本次检测时间, + String checkTimeStr = datas[6]; + if(StringUtils.isNotEmpty(checkTimeStr)){ + try { + checkTime = Long.parseLong(checkTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(6)+"数据 “"+checkTimeStr+"” 本次检测时间须为整形数:"+checkTime); + } + }else { + errorInfo.append("位置"+(6)+"数据 本次检测时间不能为空;"); + } + + //--尝试次数, + String currentTimesSre = datas[7]; + if(StringUtils.isNotEmpty(currentTimesSre)){ + try { + currentTimes = Integer.parseInt(currentTimesSre); + } catch (Exception e) { + errorInfo.append("位置"+(7)+"数据 “"+currentTimesSre+"” 尝试次数须为整形数:"+currentTimes); + } + }else{ + errorInfo.append("位置"+(7)+"数据 尝试次数不能为空;"); + } + + //--下次计划监测时间, + String nextCheckTimeStr = datas[8]; + if(StringUtils.isNotEmpty(nextCheckTimeStr)){ + try { + nextCheckTime = Long.parseLong(nextCheckTimeStr); + } catch (Exception e) { + errorInfo.append("位置"+(8)+"数据 “"+nextCheckTimeStr+"” 下次计划监测时间须为整形数:"+nextCheckTime); + } + }else{ + errorInfo.append("位置"+(8)+"数据 尝试次数不能为空;"); + } + + //--执行状态是否成功,记录的状态是否正常(用于报警 -1执行失败 0不正常,1正常), + String stateStr = datas[9]; + if(StringUtils.isNotEmpty(stateStr)){ + try { + state = Integer.parseInt(stateStr); + } catch (Exception e) { + errorInfo.append("位置"+(9)+"数据 “"+stateStr+"” 执行状态须为整形数:"+state); + } + }else{ + errorInfo.append("位置"+(9)+"数据 执行状态不能为空;"); + } + + //--状态信息(描述信息), + dsinfo = datas[10]; + + + //--性能数据 + pdata = datas[11]; + if(StringUtils.isNotEmpty(pdata)){ + // add by zg 增加对
的处理 begin + pdata = pdata.replaceAll("
", "\n"); + // add by zg 增加对
的处理 end + if(pdata.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + pdata = StringUtil.substring(pdata,Constants.VARCHAR_MAX_LENGTH); + } + }else {//界面异常信息列表的状态信息显示为性能数据,所以如果性能数据为空,则取状态信息 + pdata = dsinfo; + } + + } catch (Exception e) { + String s = "基本数据部分解析失败"+e.getMessage(); + logger.warn(s); + errorInfo.append(s); + } + + //--基本信息解析 存在异常值 无须保存,返回错误信息 用于系统告警 + if(errorInfo.length() != 0){ + logger.error(" 第一步-1: 新格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); + return null; + } + + //-- 根据执行状态判断是否需要获取详细数据的信息 及报警信息,执行失败,则不要获取 + if(state < 0){ //失败 报警only + delyFlag = false;//程序执行失败时,不入库详细监测信息 + } + + + //公共信息集合 + Calendar arriveTime = Calendar.getInstance(); + detectInfo = new Object[33]; + //detectInfo[DetectInfo.null] = (CSVUtils.csvBytesPrinter1(strsList, Constants.COMMON_TEXT_CODING)); + detectInfo[DetectInfo.SEQID] = (seqId); + detectInfo[DetectInfo.SETINFOID] = (setInfoId); + detectInfo[DetectInfo.STARTTIME] = (startTime); + detectInfo[DetectInfo.DELAYTIME] = (delayTime); + detectInfo[DetectInfo.CHECKTIME] = (checkTime); + detectInfo[DetectInfo.CURRENTTIMES] = (currentTimes); + detectInfo[DetectInfo.NEXTCHECKTIME] = (nextCheckTime); + detectInfo[DetectInfo.CHECKTYPE] = (checkType); + detectInfo[DetectInfo.PDATA] = (pdata); + detectInfo[DetectInfo.ARRIVETIME] = (arriveTime.getTimeInMillis()); + detectInfo[DetectInfo.PROCESS] = process; + + /* 第二步:解析详细数据部分 */ + Map tableMap = Common.getTableMap().get(checkType); + if(tableMap ==null || tableMap.size() == 0){ + logger.error(""+checkType+"无法获取表结构"); + delyFlag = false; + } + + List> detaiList = new ArrayList>(); //详细数据集合 + int pLevel =99;//默认恢复正常 + StringBuffer alarmInfo = new StringBuffer(); + int urgentLevel = EmailTypeConstants.URGENT_LATER; + if(delyFlag){ + + details:while(strsIte.hasNext()){ + String [] detailsArr = strsIte.next(); //取出 + if(detailsArr == null || detailsArr.length ==0){ //空行不解析 + continue; + } + if(detailsArr.length != 2){ //不是[details:0]格式无法解析 + break details; + } + if("details".equalsIgnoreCase(detailsArr[0])){ + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + /* + * 如果是checkType='switchport'监测,且有详细信息,才判断每个详细信息的状态,再确定公共信息的状态:人为设置状态、流量大于0 + 一、根据端口状态,修改监测信息(detection_info、di_switchport表)的detection_state: + 公共信息状态:-1执行不成功;0异常;1正常 + 端口信息状态:1:正常,0:异常 + 1.查询当前节点人为设置状态port_set_state为up的端口:1up,2down + 2.对于每个端口 + if(端口state==1&&当前端口的设置状态为up) {//如果state非0才做判断,并修改,如果state已经为0,则没必要再判断端口的状态(只要有一个端口发生以下异常,则detection_state=0异常) + + 若IFADMINSTATUS状态为down,则端口state和公共信息state异常(1up,2down) + if(端口state==1) { + 判断流量(输入速度+输出速度)为0,则端口state和公共信息state异常 + } + } + + */ + List portNameList = null; + if(endNum>0 && Constants.DETEC_SWITCH_STR.equalsIgnoreCase(checkType)) { + //查询当前节点设置状态为up的端口名称,生成list + portNameList = DataResolveThread.getPortNameBySeqId(seqId); + } + + /** + * 储存详细数据前三个字段,恢复数据时使用 + */ + List showNumList = new LinkedList(); + List plevelList = new LinkedList(); + List alarmList = new LinkedList(); + while(strsIte.hasNext()){ + endNum--; + String [] detailsArr0 = strsIte.next(); //取出 + Map detail = new HashMap(); //详细数据集合 + //-- 详细数据解析 + if(detailsArr0!=null && detailsArr0.length>0){ + //告警数据相关信息:0-2 + String showNumStr = detailsArr0[0]; + String pLevelStr = detailsArr0[1]; + String alarmValue = detailsArr0[2]; + + showNumList.add(showNumStr); + plevelList.add(pLevelStr); + alarmList.add(alarmValue); + + int index = 3; //数据集合解析游标 + try { + //-- 详细信息整理 监测具体数据信息(每个字段一列)。 + int i = 0 ; + while(detail.size() < tableMap.size()){//将metadata表中对应的字段,按shouNum进行组合数据 + TableColumnsModel field = tableMap.get(i); + ++i; + if((i-1)>tableMap.size()){ + break; + } + if(field!=null){ + //当监测数据(index == detailsArr0.length)时,及有效监测数据已经读取完成,tableMap页无需增加空字段数据 + if(index == detailsArr0.length){ + detail.put(field.getFiledName(), null); + continue; + } + try { + String fileType = field.getFiledType().toUpperCase(); + if("DATE".equals(fileType)){ + if(StringUtils.isNumeric(detailsArr0[index])){//如果为数字型时间,转换 + detail.put(field.getFiledName(), format.format(new Date(Long.parseLong(detailsArr0[index].trim())))); + }else{//eg:2016-03-06 12:12:12 + detail.put(field.getFiledName(), detailsArr0[index].trim()); + } + }else{ + if("NUMBER".equals(fileType)){ + if (StringUtils.isBlank(detailsArr0[index]) + || StringUtil.isRationalNumber(detailsArr0[index])) { + detail.put(field.getFiledName(), detailsArr0[index].trim()); + } else if (detailsArr0[index].trim().contains(" days, ")) { + detail.put(field.getFiledName(), CommonService.getLongTimeStr(detailsArr0[index].trim())); + } else { + throw new Exception("NUMBER数据格式错误!"); + } + }else{ // VARCHAR2(x) + String length = fileType.replaceAll("VARCHAR2\\((\\d+)\\)", "$1"); + if(Integer.parseInt(length) < detailsArr0[index].trim().length()) { + throw new Exception("VARCHAR2数据格式错误!"); + } + detail.put(field.getFiledName(), detailsArr0[index].trim()); + } + } + } catch (Exception e) { + errorInfo.append(checkType+" 表字段集合信息解析失败;"); + errorInfo.append(field.getFiledName()).append("字段取值") + .append(detailsArr0[index]).append("发生错误;异常数据行:"); + for (String string : detailsArr0) { // 添加异常数据 + errorInfo.append(string).append(','); + } + errorInfo.deleteCharAt(errorInfo.length()-1); + //监测数据的详细信息如果和指定的字段类型不匹配,则抛弃此条监测数据,避免影响批量入库 + logger.error(" 第一步-1: 新格式监测数据解析异常: seqId:"+seqId+",setInfoId:"+setInfoId+",checkType:"+checkType+" checkTime:"+new Date(checkTime)+" 监测数据解析异常:"+errorInfo); + return null; + } + index++; + } + } + //组合附加的一些信息 + detail.put("SEQ_ID", seqId); + String detailDetectionState = ""; + if(StringUtils.isBlank(pLevelStr)){//没有告警,当前详细监测信息正常 + detailDetectionState = "1";//正常 + }else{ + detailDetectionState = "0";//异常 + } + //如果是switchport监测,有端口详细信息,且端口状态为正常,才需要再根据以下条件判断端口是否异常 + if(portNameList!=null&&portNameList.size()>0&&"1".equals(detailDetectionState)) { + String ifDescr = detailsArr0[4];//ifDescr-showNum:2--+2=4(因为有3个告警的字段) + /** + * 1.人为设置端口状态为up时,若IFADMINSTATUS或者IFOPERSTATUS状态为down2(!=1:up),则state=0异常--1up,2down + if(state==1) { + 2.流量(输入速度+输出速度)为0,则state=0异常 + } + */ + if(portNameList.contains(ifDescr)) {//如果此端口设置的状态为up + String ifAdminStatus = detailsArr0[9];//showNum=7,1:up,2:down + if("2".equals(ifAdminStatus)) { + state = 0;//监测公共信息的状态 + dsinfo += "$@$端口"+ifDescr+"异常(配置状态与实际状态不符)"; + detailDetectionState = "0";//当前端口的状态 + pLevel = Constants.PORT_ALARM_LEVEL;//非界面设置,默认为5级 + } + + if("1".equals(detailDetectionState)) {//如果当前端口的状态正常,才需要判断流量是否为0 + double inOctetsSpeed = detailsArr0[25]==null?0d:("".equals(detailsArr0[25])?0d:Double.parseDouble(detailsArr0[25].trim()));//23+2=25,瞬时输入字节速度 + double outOctetsSpeed = detailsArr0[29]==null?0d:("".equals(detailsArr0[29])?0d:Double.parseDouble(detailsArr0[29].trim()));//27+2=29,瞬时输出字节速度 + logger.info("test:port:inOctetsSpeed="+inOctetsSpeed+";outOctetsSpeed="+outOctetsSpeed); + if((inOctetsSpeed+outOctetsSpeed)<=0) { + state = 0;//监测公共信息的状态 + dsinfo += "$@$端口"+ifDescr+"异常(流量为0),inOctetsSpeed="+inOctetsSpeed+",outOctetsSpeed="+outOctetsSpeed+",sum="+(inOctetsSpeed+outOctetsSpeed); + detailDetectionState = "0";//当前端口的状态 + pLevel = Constants.PORT_ALARM_LEVEL;//非界面设置,默认为5级 + } + } + } + } + + detail.put("DETECTIONED_STATE", detailDetectionState); + detail.put("DETECTION_SET_INFO_ID",setInfoId); // 入库时间 + detail.put("DATA_CHECK_TIME_DIGITAL", checkTime+""); // 测试时间 + detail.put("DATA_CHECK_TIME", format.format(new Date(checkTime))); // 测试时间 + detail.put("DATA_ARRIVE_TIME_DIGITAL", arriveTime.getTimeInMillis()+""); // 入库时间 + detail.put("DATA_ARRIVE_TIME",format.format(arriveTime.getTime())); // 入库时间 + detaiList.add(detail); + //-- 整理告警信息 + if(state == 0){ + String [] nums = showNumStr.split("\\|"); + String [] levels = pLevelStr.split("\\|"); + String [] values = alarmValue.split("\\|"); + List aList = Common.getAlarmInfoMap().get(setInfoId); + Iterator ite = null; + for(int m = 0 ; m= Integer.parseInt(levels[m].trim())){ + pLevel = Integer.parseInt(levels[m].trim()); + //0级别:对应紧急,其它对应非紧急 + if(pLevel==Constants.LEVEL_OF_EMERGENCY) { + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + }else { + urgentLevel = EmailTypeConstants.URGENT_LATER; + } + } + } catch (Exception e) + { + logger.error("新格式监测数据-修改正告警级别时,解析告警level:"+levels[m]+" 异常", e); + } + + /** 字段名称 intDate 实际值: 80》60%(临界值) 级别 40 **/ + String filedName = ""; + String filedComments = ""; + if(tableMap !=null){ + TableColumnsModel tm = tableMap.get(elem.getShowNum()); + filedName = tm==null?"":tm.getFiledName(); + filedComments = tm==null?"":tm.getFiledComments(); + } + cpInfo += " "+(StringUtils.isEmpty(elem.getFiledCommonts())?filedName:elem.getFiledCommonts()); + cpInfo += " 为:"+detail.get(filedName); + cpInfo += (elem.getPoliceUnit()==null ? "" : "null".equals(elem.getPoliceUnit()) ? "" : elem.getPoliceUnit()); + cpInfo += " 报警级别:"+pLevel; + cpInfo += " \n"; + } + } + //如果是握手监测,不添加告警设置字段相关信息 + if(!Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + alarmInfo.append(cpInfo); + } + } + } + } catch (Exception e) { + String s = "详细数据部分解析失败"+e.getMessage(); + logger.warn(s); + } + } + + detectInfo[DetectInfo.SHOWNUMLIST] = (showNumList); + detectInfo[DetectInfo.PLEVELLIST] = (plevelList); + detectInfo[DetectInfo.ALARMLIST] = (alarmList); + + if(endNum ==0){ + break details; + } + } + if(dsinfo.startsWith("$@$") && dsinfo.length()>3) { + dsinfo = dsinfo.substring(3, dsinfo.length()); + } + } + } + } + if(state == -1){//监测执行失败,是最高级别的告警 + //NMSC 监测失败状态 程序设定为为紧急 + if(checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)){ + pLevel = Constants.DETECT_FAIL_NMSC_POLICE_LEVEL; + urgentLevel = Constants.DETECT_FAIL_NMSC_POLICE_EMERGENT; + }else { + pLevel = Constants.DETECT_FAIL_NON_NMSC_POLICE_LEVEL; + urgentLevel = Constants.DETECT_FAIL_NON_NMSC_POLICE_EMERGENT; + } + } + + //--状态信息(描述信息), + if(StringUtils.isNotEmpty(dsinfo)){ + dsinfo = dsinfo.replaceAll("
", "\n");// add by zg 增加对
的处理 begin + if(dsinfo.getBytes().length>Constants.VARCHAR_MAX_LENGTH){ + dsinfo = StringUtil.substring(dsinfo,Constants.VARCHAR_MAX_LENGTH); + } + } + + detectInfo[DetectInfo.STATE] = (state); + detectInfo[DetectInfo.DSINFO] = (dsinfo); + detectInfo[DetectInfo.PLEVEL] = (pLevel); + detectInfo[DetectInfo.ALARMINFO] = (alarmInfo.toString());//告警信息,发送邮件的时候需要用 + detectInfo[DetectInfo.URGENTLEVEL] = (urgentLevel);//状态是否改变会影响这个告警级别 + detectInfo[DetectInfo.DELYFLAG] = (delyFlag);//根据这个参数,决定是否保存详细信息 + detectInfo[DetectInfo.DETAILS] = (detaiList);//用于批量入库详细信息 + + //特殊追加信息解析--systeminfo-disk、net + //-- 特殊信息保存整理 +// logger.debug("新格式解析 第七步 特殊追加信息解析"+(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType))); + if(Constants.DETEC_SYSTEMINFO_STR.equalsIgnoreCase(checkType) ) { + while (strsIte.hasNext()) { + String [] detailsArr = strsIte.next(); //取出 + if(detailsArr == null || detailsArr.length ==0){ //空行不解析 + continue; + } + if(detailsArr.length != 2){ //不是[details:0]格式无法解析 + break; + } + + if("disk".equalsIgnoreCase(detailsArr[0])){ + List> infoList = new ArrayList>(); + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + disk:while (strsIte.hasNext()) { + endNum--; + String [] detailsArr0 = strsIte.next(); //取出 + int len = detailsArr0.length; + Map diskInfo = new HashMap(); + diskInfo.put("DISK_RANG", len>0 ? detailsArr0[0]:"");//盘符 + diskInfo.put("DISK_SIZE", len>1 ? detailsArr0[1]:"");//大小 + diskInfo.put("DATA_CHECK_TIME_DIGITAL", checkTime+"");//监测时间(long) + diskInfo.put("DATA_CHECK_TIME", format.format(new Date(checkTime)));//监测时间(date) + diskInfo.put("DATA_ARRIVE_TIME_DIGITAL", arriveTime.getTimeInMillis()+"");////录入记录时间(当前时间 long) + diskInfo.put("DATA_ARRIVE_TIME", format.format(arriveTime.getTime()));////录入记录时间(当前时间 date) + diskInfo.put("DETECTION_SET_INFO_ID", setInfoId);// + diskInfo.put("SEQ_ID", seqId); + diskInfo.put("DETECTIONED_STATE", state+""); + infoList.add(diskInfo); + if(endNum==0){ + logger.debug("disk解析完成 跳出循环"); + break disk; + } + } + if(infoList.size()>0) { + detectInfo[DetectInfo.DISKINFOLIST] = (infoList);//插入操作批量执行 + } + }else if("net".equalsIgnoreCase(detailsArr[0])) { + + List> infoList = new ArrayList>(); + int endNum = StringUtils.isEmpty(detailsArr[1])?0:Integer.parseInt(detailsArr[1]); + net:while (strsIte.hasNext()) { + endNum--; + String [] netInfoTmp = strsIte.next(); //取出 + int len = netInfoTmp.length; + Map netInfo = new HashMap(); + netInfo.put("NET_NAME", len>0 ? netInfoTmp[0]:"");//网卡名称 + netInfo.put("NET_STATE", len>1 ? netInfoTmp[1]:"");//网卡状态 + netInfo.put("NET_SPEED", len>2 ? netInfoTmp[2]:"");//网卡带宽 + netInfo.put("NET_IP", len>3 ? netInfoTmp[3]:"");//ip + netInfo.put("NET_SUBMASK", len>4 ? netInfoTmp[4]:"");//子网掩码 + netInfo.put("NET_GATEWAY", len>5 ? netInfoTmp[5]:"");//网关 + netInfo.put("NET_MAC", len>6 ? netInfoTmp[6]:"");//NET_MAC + netInfo.put("DATA_CHECK_TIME", format.format(new Date(checkTime))); + netInfo.put("DATA_ARRIVE_TIME", format.format(arriveTime.getTime()));//录入记录时间(当前时间 date) + netInfo.put("DATA_CHECK_TIME_DIGITAL", checkTime+"");//监测时间(long) + netInfo.put("DATA_ARRIVE_TIME_DIGITAL", arriveTime.getTimeInMillis()+"");//录入记录时间(当前时间 long) + netInfo.put("DETECTION_SET_INFO_ID", setInfoId);// id + netInfo.put("SEQ_ID", seqId); + netInfo.put("DETECTIONED_STATE", state+""); + infoList.add(netInfo); + if(endNum ==0){ + break net; + } + } + if(infoList.size()>0) { + detectInfo[DetectInfo.NETINFOLIST] = (infoList);//插入操作批量执行 + } + } + } + } + if(detectInfo[DetectInfo.STATE] == null){ + detectInfo[DetectInfo.STATE] = -1;//state + } + if(detectInfo[DetectInfo.PLEVEL] == null){ + detectInfo[DetectInfo.PLEVEL] = 99;//pLevel + } + if(detectInfo[DetectInfo.DELYFLAG] == null){ + detectInfo[DetectInfo.DELYFLAG] = false;//delyFlag + } + if(detectInfo[DetectInfo.URGENTLEVEL] == null){ + detectInfo[DetectInfo.URGENTLEVEL] = 0;//urgentLevel + } + if(detectInfo[DetectInfo.SENDEMAILFLAG] == null){ + detectInfo[DetectInfo.SENDEMAILFLAG] = false;//sendEmailFlag + } + if(detectInfo[DetectInfo.STATECHANGEFLAG] == null){ + detectInfo[DetectInfo.STATECHANGEFLAG] = false;//stateChangeFlag + } + if(detectInfo[DetectInfo.STATUSCHANGTIME] == null){ + detectInfo[DetectInfo.STATUSCHANGTIME] = -1L;//statusChangeTime + } + return detectInfo; + } + + /** + * 保存到硬盘 + * @param data + */ + private void saveByteToFile(byte[] data) { + String filePath = ""; + try { + filePath = Constants.OVERRUN_DETEC_FILE_DIR + "/" + Common.getDateDirName() + "/" + + Calendar.getInstance().getTimeInMillis() + "_" + ((int)Math.random()*1000000 +1) + ".csv"; + FileUtils.wirteBytesToFile(data, filePath); + } catch (Exception e) { + logger.error("When there is too much monitoring data ,the data is stored on the hard disk exception",e); + } + } + +} diff --git a/src/com/nms/server/thread/detectDataHandler/DetectDetailInsertThread.java b/src/com/nms/server/thread/detectDataHandler/DetectDetailInsertThread.java new file mode 100644 index 0000000..e821b90 --- /dev/null +++ b/src/com/nms/server/thread/detectDataHandler/DetectDetailInsertThread.java @@ -0,0 +1,220 @@ +package com.nms.server.thread.detectDataHandler; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Queue; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.fang.U; +import com.fang.lang.Db; +import com.fang.lang.StopWatch; +import com.nms.server.bean.DetectInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.DateUtil; +/** + * 监测详细数据入库线程 + * @author dell + * + */ +@SuppressWarnings("all") +public class DetectDetailInsertThread implements Runnable{ + private static final Logger logger = Logger.getLogger(DetectDetailInsertThread.class); + private static Db db = Common.getDb(); + private StopWatch sw = null; + private Connection conn = null; + private String key; + private List> data; + + public DetectDetailInsertThread(String key,List> data) { + this.key = key; + this.data = data; + } + + + @Override + public void run() { + Thread.currentThread().setName("DetectDetailInsertThread-"+key); + logger.info("开始运行->" + key); + sw = U.StopWatch.newStopWacth(); + sw.start();//开始计时 + try { + conn = db.getConnection(); + saveData(key,data); + //遍历 监测数据等待入库 table + /*Hashtable hb = Common.DETECT_QUEUE; + Set> es = hb.entrySet(); + Iterator> ite = es.iterator(); + while(ite.hasNext() && ! Common.isStop() && ! Thread.currentThread().isInterrupted() ){ + Entry next = ite.next(); + String key = next.getKey(); + if(DetectInfo.INFO_KEY.equalsIgnoreCase(key)){ + //info 表的数据此线程不做解析 + continue; + } + Queue> data = next.getValue(); + saveData(key,data); + }*/ + sw.end(); + + } catch (Exception e) { + logger.error("",e); + } finally { + db.closeQuiet(conn); + } + logger.info(key+ " 监测数据详细信息解析入库,耗时: " + U.StopWatch.toString(sw.total())); + logger.info("结束运行->" + key); + sw = null; + } + + /** + * 保存详细监测数据 + * @param data + */ + public void saveData(String checkType ,List> data){ + logger.info("监测类别:" + checkType +" 开始入库"); + sw.tag(checkType + "-start"); + int count = 0; + PreparedStatement stmt = null; + try { + Map ele = null; + //详细信息对应的表名 + String detailTableName = null; + if(DetectInfo.NET_STR_KEY.equalsIgnoreCase(checkType)){ + //系统信息-网卡信息 + detailTableName = "di_systeminfo_net"; + }else if(DetectInfo.DISK_STR_KEY.equalsIgnoreCase(checkType)){ + //系统信息-磁盘信息 + detailTableName = "di_systeminfo_disk"; + }else{ + detailTableName = Common.getInsertTable().get(checkType) == null ? null : Common.getInsertTable().get(checkType).getTableName(); + } + + if(detailTableName == null){ + logger.error(checkType + " the corresponding detailed table was not found and the data could not be stored"); + return; + } + List params = new LinkedList();//等待批处理参数 + List fieldNames = new ArrayList();//保存监测数据的字段 + String sql = null;//insert sql + Iterator> ite = data.iterator(); + while( ite.hasNext()){ + ele = ite.next(); + count ++; + if(sql == null){ + sql = DetectInfo.createDetailSql(fieldNames, detailTableName, ele); + stmt = conn.prepareStatement(sql); + } + + //计算 详细表的 外键 时间戳(10) + seqId(5)+ setId (3),主要是为了使用 java 中long 类型 + String checkTime = ele.get("DATA_CHECK_TIME_DIGITAL"); + String setId = ele.get("DETECTION_SET_INFO_ID"); + String seqId = ele.get("SEQ_ID"); + String id = DetectInfo.computeId(checkTime, seqId, setId); + ele.put("DETECTION_INFO_ID", id); + + Object[] detail = mapToArray(fieldNames, ele); + params.add(detail); + if(count % Constants.BATCH_RESOVE_COUNT == 0){ + //保存到数据库 + saveToDb(params, checkType, stmt); + params.clear(); + } + } + if(params.size() > 0){ + //保存到数据库 + saveToDb(params, checkType, stmt); + params.clear(); + } + } catch (Exception e) { + logger.error("save detect detail error ",e); + }finally { + db.closeQuiet(stmt); + } + sw.tag(checkType + "-end"); + logger.info("监测类别: " + checkType + " , 耗时: " + U.StopWatch.toString(sw.between(checkType+"-end",checkType+"-start")) +" ,共 " + count +"条"); + } + + /** + * 保存到数据库 + * @param params + * @param checkType + * @param sql + */ + + private void saveToDb(List params,String checkType,PreparedStatement stmt){ + Object temp=null; + Object[] arrTemp=null; + long n = System.currentTimeMillis(); + sw.tag(n+"start"); + try { + if (params != null && params.size() >0) { + conn.setAutoCommit(false); + int count=0; + for(Object[] e : params){ + arrTemp=e; + logger.debug(" params " + Arrays.toString(e)+"addToBatch :"+ ++count); + for (int i = 0; i < e.length; i++) { + Object value = e[i]; + temp=value; + if(DateUtil.isDate((String)value, Constants.COMMON_DATE_FORMAT)){ + String str_format=(String)value; + Date parse = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT).parse(str_format); + long time = parse.getTime(); + Timestamp timestamp = new Timestamp(time); + stmt.setTimestamp(i+1,timestamp); +// stmt.setTimestamp(i+1, new Timestamp(Common.getFormat().parse((String)value).getTime())); + }else{ + stmt.setObject(i + 1, value); + } + } + stmt.addBatch(); + } + stmt.executeBatch(); + conn.commit(); + stmt.clearBatch(); + } + } catch (Exception e) { + try { + conn.rollback(); + } catch (SQLException e1) { + } + logger.error("",e); + } + sw.tag(n+"end"); + logger.info("批量保存,类型 :" + checkType + " , 共 " + params.size() +" 条" + " , 耗时: " + U.StopWatch.toString(sw.between(n+"end",n+"start"))); + } + + /** + * 将map组织成 数据,方便executeBatch + * 下一步(可以直接在解析的时候将数据解析成数组) + * @param fieldNames + * @param data + * @return + */ + private Object[] mapToArray(List fieldNames,Map data){ + int size = fieldNames.size(); + Object[] result = new Object[size]; + for(int i = 0;i> setInfoEmail = null; + // 未指定的监测设置通过seqid找到对应的联系人 + Map> seqIdEmail = null; + + private PreparedStatement infoStmt; + private PreparedStatement warnStmt; + private PreparedStatement emailStmt; + private PreparedStatement statusChangeStmt; + private PreparedStatement infoNewInsertStmt; + private PreparedStatement infoNewUpdateStmt; + private List insertDataTonew; + private List updateDataTonew; + private PreparedStatement warnUpdateStmt; + private Connection conn;//使用的连接 + + + @Override + @SuppressWarnings({ "unchecked", "static-access" }) + public void run() { + Thread.currentThread().setName("DetectInfoInsertThread"); + logger.info("开始运行"); + sw = U.StopWatch.newStopWacth(); + sw.start(); + try { + // info数据缓存队列 + LinkedBlockingDeque infoQueue = Common.DETECT_QUEUE.get(DetectInfo.INFO_KEY); + if (infoQueue != null && infoQueue.size() > 0) { + conn = db.getConnection(); + conn.setAutoCommit(false); + Object[] info = null; + List allInfo = U.newLinkedList(); + List params = U.newLinkedList(); + insertDataTonew=new ArrayList(); + updateDataTonew=new ArrayList(); + warnUpdateStmt=conn.prepareStatement(DetectInfo.getChangeWarningInfoSql()); + int total = infoQueue.drainTo(allInfo);//一次性把队列的数据全部取出 + for(int i = 0;i 0){ + judgeAndSave(params); + params.clear(); + } + allInfo.clear(); + } else { + logger.info("info queue is empty"); + } + } catch (Exception e) { + logger.error("error", e); + }finally { + db.closeQuiet(infoStmt,warnStmt,emailStmt,statusChangeStmt,conn,infoNewInsertStmt,infoNewUpdateStmt); + infoStmt = null; + warnStmt = null; + emailStmt = null; + statusChangeStmt = null; + setInfoEmail = null; + seqIdEmail = null; + conn = null; + infoNewInsertStmt=null; + infoNewUpdateStmt=null; + } + sw.end(); + logger.info("监测数据info信息解析入库,耗时: " + U.StopWatch.toString(sw.total()) + " ,共 " + count + " 条"); + sw = null; + count = 0; + } + + /** + * 设置默认值 + * @param detecInfo + */ + private void infoSetDefaultVal(Object[] detecInfo){ + /** + * 设置默认值 + */ + if(detecInfo[DetectInfo.STATE] == null){ + detecInfo[DetectInfo.STATE] = -1;//state + } + if(detecInfo[DetectInfo.PLEVEL] == null){ + detecInfo[DetectInfo.PLEVEL] = 99;//pLevel + } + if(detecInfo[DetectInfo.DELYFLAG] == null){ + detecInfo[DetectInfo.DELYFLAG] = false;//delyFlag + } + if(detecInfo[DetectInfo.URGENTLEVEL] == null){ + detecInfo[DetectInfo.URGENTLEVEL] = 0;//urgentLevel + } + if(detecInfo[DetectInfo.SENDEMAILFLAG] == null){ + detecInfo[DetectInfo.SENDEMAILFLAG] = false;//sendEmailFlag + } + if(detecInfo[DetectInfo.STATECHANGEFLAG] == null){ + detecInfo[DetectInfo.STATECHANGEFLAG] = false;//stateChangeFlag + } + if(detecInfo[DetectInfo.STATUSCHANGTIME] == null){ + detecInfo[DetectInfo.STATUSCHANGTIME] = -1L;//statusChangeTime + } + } + + /** + * 判断状态并保存 + * + * @param params + */ + private void judgeAndSave(List params) { + sw.tag("s" + count); + try { + long l = System.currentTimeMillis(); + sw.tag(l + "s"); + //seqId,setId,list + Map>> orInfo = orderInfo(params);// 整理数据 + Set seqIds = orInfo.keySet(); + // 获取 当前将要入库的数据 对应的 detection_info_new 表中的数据 + Map> detectNewData = this.getDetectNewData(seqIds); + // 判断状态变更 + for (String seqId : seqIds) {//遍历即将入库的数据 + Map> setMap = orInfo.get(seqId); + Set>> setEn = setMap.entrySet(); + for (Entry> si : setEn) { + String setId = si.getKey();// 监测设置id + List allDeteInfo = si.getValue();// 将要入库的info数据 + Object[] oldDetecInfo = null;// new表中的最新记录 + // 获取 数据库 中 new 表的对应值 + Map seqMap = detectNewData.get(seqId); + if (seqMap != null) { + oldDetecInfo = seqMap.get(setId); + } + + // 数据库中没有当前监测类别对应的seqid节点的监测信息,不需要判断状态变更 + if (oldDetecInfo != null) { + judgeStatus(allDeteInfo, oldDetecInfo); + judgeAndCollect(allDeteInfo, oldDetecInfo, updateDataTonew); + + }else{//如果new表中没有数据,应为detection_info status_change_time设置初始值-->data_check_time + if(allDeteInfo!=null&&allDeteInfo.size()>0){ + for (Object[] newInfo : allDeteInfo) { + if(newInfo[DetectInfo.STATUSCHANGTIME]==null||(Long)newInfo[DetectInfo.STATUSCHANGTIME]==-1){ + newInfo[DetectInfo.STATUSCHANGTIME]= + newInfo[DetectInfo.CHECKTIME]==null?newInfo[DetectInfo.STARTTIME]:newInfo[DetectInfo.CHECKTIME]; + } + } + if(allDeteInfo.size()<2){ + insertDataTonew.addAll(allDeteInfo); + }else{ + Collections.sort(allDeteInfo, new Comparator() { + @Override + public int compare(Object[] o1, Object[] o2) { + Long time1 = (Long) o1[DetectInfo.CHECKTIME]; + Long time2 = (Long) o2[DetectInfo.CHECKTIME]; + return time1 < time2 ? -1 : 1; + } + }); + insertDataTonew.add(allDeteInfo.get(0)); + + } + + } + } + } + } + sw.tag(l + "e"); + logger.debug("告警判断完成,耗时:" + U.StopWatch.toString(sw.between(l + "e", l + "s")) ); + + + // 入库 + saveToNew(insertDataTonew, 0); + saveToNew(updateDataTonew, 1); + logger.info("info_new table update success! data size : "+(insertDataTonew.size()+updateDataTonew.size())); + saveInfo(params); + sw.tag(l + "sa"); + logger.debug("入库完成,耗时:" + U.StopWatch.toString(sw.between(l + "sa", l + "e")) ); + } catch (Exception e) { + logger.error("detect info save error", e); + } + sw.tag("e" + count); + logger.info("批量保存info,耗时: " + U.StopWatch.toString(sw.between("e" + count, "s" + count))); + } + + /** + * 保存 + * + * @param infoList + * @throws SQLException + */ + private void saveInfo(List infoList) throws SQLException { + Object[] temInfo = null; + try { + int infoCount = infoList.size(); + int warnCount = 0; + int emailCount = 0; + Map statusChangeMap = U.newHashMap(); + boolean emailFlag = Constants.flag_email == 1;// 是否启用 email + Iterator ite = infoList.iterator(); + while (ite.hasNext()) { + temInfo = ite.next(); + // 计算 详细表的 外键 时间戳(10) + seqId(5)+ setId (3),主要是为了使用 java 中long 类型 + String checkTime = temInfo[DetectInfo.CHECKTIME].toString(); + String setId = temInfo[DetectInfo.SETINFOID].toString(); + String seqId = temInfo[DetectInfo.SEQID].toString(); + String checkType = temInfo[DetectInfo.CHECKTYPE].toString();//监测类别 + String id = DetectInfo.computeId(checkTime, seqId, setId); + + + + Object[] infoParam = new Object[] { id, setId, (String) temInfo[DetectInfo.DSINFO], + (String) temInfo[DetectInfo.PDATA], temInfo[DetectInfo.CURRENTTIMES] + "", + temInfo[DetectInfo.STARTTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.STARTTIME])) : null, + temInfo[DetectInfo.DELAYTIME] + "", + temInfo[DetectInfo.NEXTCHECKTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.NEXTCHECKTIME])) : null, + temInfo[DetectInfo.PLEVEL], + temInfo[DetectInfo.CHECKTIME] != null? format.format(new Date((Long) temInfo[DetectInfo.CHECKTIME])) : null, + temInfo[DetectInfo.ARRIVETIME] != null? format.format(new Date((Long) temInfo[DetectInfo.ARRIVETIME])) : null, + temInfo[DetectInfo.STATE] , checkTime, temInfo[DetectInfo.ARRIVETIME] , seqId, + temInfo[DetectInfo.URGENTLEVEL] , + temInfo[DetectInfo.STATUSCHANGTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.STATUSCHANGTIME])) : null, + Common.SEQUENCE.get(DetectInfo.DETECTION_INFO)==null?null:Common.SEQUENCE.get(DetectInfo.DETECTION_INFO).incrementAndGet()}; + + if(Common.SEQUENCE.get(DetectInfo.DETECTION_INFO)==null){ + infoParam=Arrays.copyOf(infoParam, infoParam.length-1); + } + if(infoStmt == null){ + infoStmt = conn.prepareStatement(DetectInfo.getInsertInfoSql()); + infoStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + addRecordToStatement(infoStmt, infoParam); + infoCount ++; + + // 添加告警信息:告警信息=状态异常+状态改变+推迟入库的 + if ((Integer) temInfo[DetectInfo.STATE] != 1 || (Boolean) temInfo[DetectInfo.STATECHANGEFLAG]) { + Object[] params = new Object[] { id, (String) temInfo[DetectInfo.SETINFOID], + (String) temInfo[DetectInfo.DSINFO], (String) temInfo[DetectInfo.PDATA], + temInfo[DetectInfo.CURRENTTIMES] , + temInfo[DetectInfo.STARTTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.STARTTIME])) : null, + temInfo[DetectInfo.DELAYTIME] , + temInfo[DetectInfo.NEXTCHECKTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.NEXTCHECKTIME])) : null, + temInfo[DetectInfo.PLEVEL] , + temInfo[DetectInfo.CHECKTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.CHECKTIME])) : null, + temInfo[DetectInfo.ARRIVETIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.ARRIVETIME])) : null, + temInfo[DetectInfo.STATE] , temInfo[DetectInfo.CHECKTIME] , + temInfo[DetectInfo.ARRIVETIME] , (String) temInfo[DetectInfo.SEQID], + temInfo[DetectInfo.URGENTLEVEL] }; + + if(warnStmt == null){ + warnStmt = conn.prepareStatement(DetectInfo.getInsertWarningSql()); + warnStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + addRecordToStatement(warnStmt, params); + warnCount++; + } + + // 延迟监测数据追加告警信息 + if (temInfo[DetectInfo.APPENDWARNINGINFO] != null) { + Object[] warningInfo = (Object[]) temInfo[DetectInfo.APPENDWARNINGINFO]; + Object[] params = new Object[] { warningInfo[DetectInfo.DETECTIONINFOID], + (String) warningInfo[DetectInfo.SETINFOID], (String) warningInfo[DetectInfo.DSINFO], + (String) warningInfo[DetectInfo.PDATA], warningInfo[DetectInfo.CURRENTTIMES] , + warningInfo[DetectInfo.STARTTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.STARTTIME])) : null, + warningInfo[DetectInfo.DELAYTIME] , + warningInfo[DetectInfo.NEXTCHECKTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.NEXTCHECKTIME])) : null, + warningInfo[DetectInfo.PLEVEL] , + warningInfo[DetectInfo.CHECKTIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.CHECKTIME])) : null, + warningInfo[DetectInfo.ARRIVETIME] != null ? format.format(new Date((Long) temInfo[DetectInfo.ARRIVETIME])) : null, + warningInfo[DetectInfo.STATE] , warningInfo[DetectInfo.CHECKTIME] , + warningInfo[DetectInfo.ARRIVETIME] , warningInfo[DetectInfo.SEQID], + warningInfo[DetectInfo.URGENTLEVEL] }; + if(warnStmt == null){ + warnStmt = conn.prepareStatement(DetectInfo.getInsertWarningSql()); + warnStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + addRecordToStatement(warnStmt, params); + warnCount++; + } + + // 添加邮件信息 + if (!emailFlag) { + logger.info("邮件功能已关闭"); + } else { + if ((Boolean) temInfo[DetectInfo.SENDEMAILFLAG]) { + EmailInfo emailInfo = (EmailInfo) temInfo[DetectInfo.EMAILINFO]; + if (emailInfo != null) { + if (setInfoEmail == null) { + setInfoEmail = this.getDetecSetEmailListOf123(); + } + if (seqIdEmail == null) { + seqIdEmail = this.getDetecEmailListOf4(); + } + if ((setInfoEmail == null || setInfoEmail.size() == 0) + && (seqIdEmail == null || seqIdEmail.size() == 0)) { + logger.debug("未找到邮件地址!"); + } else { + List emailAddress = null; + // 1.首先通过监测设置的map中查找 + if (setInfoEmail != null) { + emailAddress = setInfoEmail.get(setId); + } + if (emailAddress == null) { + emailAddress = seqIdEmail.get(seqId); + } + if (emailAddress != null && emailAddress.size() > 0) { + for (String tempAddress : emailAddress) { + if (StringUtils.isNotEmpty(tempAddress)) { + Object[] params = new Object[] { tempAddress, emailInfo.getActionType(), + emailInfo.getContent(), emailInfo.getSendFlag(), + emailInfo.getActionIp(), emailInfo.getActionDate(), + emailInfo.getActionDesc(), emailInfo.getSendLevel(), + DateUtil.getCurrentTime() }; + + if(emailStmt == null){ + emailStmt = conn.prepareStatement(DetectInfo.getInsertEmailSql()); + emailStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + addRecordToStatement(emailStmt, params); + emailCount ++; + } + } + } + } + } + } + } + + if(Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)){ + long temChangeTime = (Long) temInfo[DetectInfo.STATUSCHANGTIME]; + if(temChangeTime > 0 ){ + Long l = statusChangeMap.get(seqId); + if(l == null || temChangeTime > l){ + statusChangeMap.put(seqId, temChangeTime); + } + } + } + ite.remove(); + } + if(infoCount >0){ + infoStmt.executeBatch(); + } + if(warnCount >0){ + warnStmt.executeBatch(); + } + if(emailCount >0){ + emailStmt.executeBatch(); + } + + if(statusChangeMap.size() > 0){ + if(statusChangeStmt == null){ + statusChangeStmt = conn.prepareStatement(DetectInfo.getUpdateStatusSql()); + statusChangeStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + Set> entrySet = statusChangeMap.entrySet(); + for(Entry en : entrySet){ + Object[] params = {en.getValue()!=null?format.format(new Date((Long)en.getValue())):null,en.getKey()}; + addRecordToStatement(statusChangeStmt, params); + } + statusChangeStmt.executeBatch(); + } + + warnUpdateStmt.executeBatch();//update warning + + conn.commit(); + logger.info("批量保存,info :" + infoCount + " , warn : " + warnCount + " ,email : "+ emailCount); + } catch (Exception e) { + logger.error("info save error ,temInfo : "+Arrays.toString(temInfo) ,e); + conn.rollback(); + }finally { + if(infoStmt != null){ + infoStmt.clearBatch(); + } + if(warnStmt != null){ + warnStmt.clearBatch(); + } + if(emailStmt != null){ + emailStmt.clearBatch(); + } + if(statusChangeStmt != null){ + statusChangeStmt.clearBatch(); + } + } + } + + private Map> getDetecEmailListOf4() { + Map> result = new HashMap>(); + try { + StringBuilder sb = new StringBuilder(); + sb.append(" select distinct nt.seq_id ,xyj.email"); + sb.append(" from xt_yh_jbxx xyj "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xyji.yhid = xyj.yhid "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join gorup_system_table gst "); + sb.append(" on gst.user_group_id = xjj.jsbh "); + sb.append(" left join system_table st "); + sb.append(" on st.system_id = gst.system_id "); + sb.append(" left join node_table nt "); + sb.append(" on nt.system_id = gst.system_id "); + sb.append(" left join nodegroup_table ngt "); + sb.append(" on ngt.group_id = nt.node_group_id "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and nt.node_state = 0 "); + sb.append(" and ngt.is_valid = 1 "); + sb.append(" and st.system_state = 0 "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + List> map1 = db.select(conn, sb.toString()); + if(map1 != null && map1.size()>0){ + List tempList = null; + String email = null; + String seqId = null; + for(Map temp : map1){ + seqId = (String)temp.get("seq_id"); + email = (String)temp.get("email"); + tempList = result.get(seqId); + if(tempList == null){//第一次添加 + List emailList = new ArrayList(); + emailList.add(email); + result.put(seqId, emailList); + }else{ + tempList.add(email); + } + seqId = null; + email = null; + tempList = null; + } + } + } catch (Exception e) { + logger.debug("查询邮件列表失败",e); + } + return result; + } + + private Map> getDetecSetEmailListOf123() { + Map> result = new HashMap>(); + StringBuilder sb = new StringBuilder(); + sb.append(" select distinct c.id, xyj1.email "); + sb.append(" from xt_yh_jbxx xyj1, "); + sb.append(" (select dsi.id, ',' || dsi.contact_user_ids || ',' as ids "); + sb.append(" from detection_set_info dsi "); + sb.append(" where dsi.contact_user_ids is not null) c "); + sb.append(" where c.ids like ('%,' || xyj1.yhid || ',%') "); + sb.append(" and xyj1.email is not null "); + sb.append(" union "); + sb.append(" (select distinct dsi.id,xyj.email "); + sb.append(" from detection_set_info dsi "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on dsi.create_usergroup_id = xjj.jsbh "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join xt_yh_jbxx xyj "); + sb.append(" on xyj.yhid = xyji.yhid "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + sb.append(" and dsi.view_level = 2 "); + sb.append(" and xyj.email is not null "); + sb.append(" and dsi.create_usergroup_id is not null "); + sb.append(" and dsi.contact_user_ids is null) "); + sb.append(" union "); + sb.append(" ( "); + sb.append(" select distinct dsi.id,xyj.email from detection_set_info dsi "); + sb.append(" left join gorup_system_table gst "); + sb.append(" on gst.system_id = dsi.system_id "); + sb.append(" left join xt_js_jbxx xjj "); + sb.append(" on gst.user_group_id = xjj.jsbh "); + sb.append(" left join xt_yh_js_index xyji "); + sb.append(" on xjj.jsbh = xyji.jsbh "); + sb.append(" left join xt_yh_jbxx xyj "); + sb.append(" on xyji.yhid = xyj.yhid "); + sb.append(" where xyj.is_receiveemail = '0' "); + sb.append(" and xjj.zxbz = 0 "); + sb.append(" and xjj.type = 1 "); + sb.append(" and xyj.zxbz = 0 "); + sb.append(" and dsi.view_level = 3 "); + sb.append(" and xyj.email is not null "); + sb.append(" and dsi.contact_user_ids is null "); + sb.append(" ) "); + + try { + List> map1 = db.select(conn, sb.toString()); + if(map1 != null && map1.size()>0){ + List tempList = null; + String email = null; + String id = null; + for(Map temp : map1){ + id = (String)temp.get("id"); + email = (String)temp.get("email"); + tempList = result.get(id); + if(tempList == null){//第一次添加 + List emailList = new ArrayList(); + emailList.add(email); + result.put(id, emailList); + }else{ + tempList.add(email); + } + id = null; + email = null; + tempList = null; + } + } + } catch (Exception e) { + logger.debug("查询邮件列表失败",e); + } + return result; + } + + /** + * 判断状态变更 + * + * @param allDeteInfo 单条监测设置id下的 监测数据 + * @param oldDetecInfo new表中对应监测设置id下的监测数据 + */ + private void judgeStatus(List allDeteInfo, Object[] oldDetecInfo) { + if (allDeteInfo.size() < 2) {// 只有一条数据直接比较数据库中的数据 + // 只有一条最新监测 + Object[] newDetecInfo = allDeteInfo.get(0); + + int state = (Integer) newDetecInfo[DetectInfo.STATE]; + /* + * 入库数据不是延时数据时,与数据库中的最新数据比对判断状态变更 数据库中的监测时间小于当前需要入库的监测时间 + */ + if ((Long) oldDetecInfo[DetectInfo.CHECKTIME] < (Long) newDetecInfo[DetectInfo.CHECKTIME]) { + /* 判断状态变更 */ + stateChange(newDetecInfo, oldDetecInfo); + } else {// 监测时间小于数据库中监测数据的时间,说明当前需要入库的监测为延迟数据 + if (state != 1) {// 延迟数据监测状态没有成功,数据库入库的是正常数据,补录一条报警信息 + if ((Integer) newDetecInfo[DetectInfo.STATE] == 1 + && (Integer) oldDetecInfo[DetectInfo.PLEVEL] == 99) { + newDetecInfo[DetectInfo.APPENDWARNINGINFO] = oldDetecInfo; + } + } + } + } else {// 大于两条数据时,排序之后判断数据库的数据是否为最接近的时间数据 + // 未入库的数据排序 + Collections.sort(allDeteInfo, new Comparator() { + @Override + public int compare(Object[] o1, Object[] o2) { + Long time1 = (Long) o1[DetectInfo.CHECKTIME]; + Long time2 = (Long) o2[DetectInfo.CHECKTIME]; + return time1 < time2 ? -1 : 1; + } + }); + /* 判断状态变更 */ + judgeState(allDeteInfo, oldDetecInfo); + } + + } + + /** + * 整理 缓存中的 info 数据,方便 判断告警信息 + * + * @param params + * @return + */ + private Map>> orderInfo(List params) { + Map>> result = U.newHashMap(); + // 先将所有的数据整理成 结果集合 + for (Object[] item : params) { + String seqId = (String) item[DetectInfo.SEQID]; + String setId = (String) item[DetectInfo.SETINFOID]; + Map> setMap = result.get(seqId); + if (setMap == null) { + List l = U.newArrayList(); + l.add(item); + setMap = U.newHashMap(); + setMap.put(setId, l); + result.put(seqId, setMap); + } else { + List list = setMap.get(setId); + if (list == null) { + list = U.newArrayList(); + list.add(item); + setMap.put(setId, list); + } else { + list.add(item); + } + } + } + // 将list排序 + if (result.size() > 0) { + Set>>> seqEn = result.entrySet(); + for (Entry>> se : seqEn) { + Map> setMap = se.getValue(); + Set>> setEn = setMap.entrySet(); + for (Entry> st : setEn) { + List list = st.getValue(); + if (list.size() > 1) { + // 统一seqid setid 的数据排序 + Collections.sort(list, new Comparator() { + @Override + public int compare(Object[] o1, Object[] o2) { + if (o1 != null && o2 != null) { + long l1 = (long) o1[DetectInfo.CHECKTIME]; + long l2 = (long) o2[DetectInfo.CHECKTIME]; + return l1 < l2 ? -1 : 1; + } + return 0; + } + }); + } + } + } + } + return result; + } + + /** + * 查询 detection_info_new 表中的最新数据 + * + * @param seqId + * @return + */ + private Map> getDetectNewData(Collection seqId) { + Map> result = U.newHashMap(); + try { + String seqIds = StringUtils.join(seqId.iterator(), ","); + StringBuilder sbSql = new StringBuilder(); + sbSql.append( + "select din.ID,din.DETECTION_SET_INFO_ID,din.CHECK_WAY,din.DETECTION_STATE_INFO,din.PERFORMACE_DATA,din.CURRENT_TIMES,din.START_TIME,din.WAIT_TIME,din.DELAY_TIME,din.NEXT_CHECK_TIME,din.OFF_LINE,din.POLICE_LEVEL,din.DATA_CHECK_TIME,din.DATA_ARRIVE_TIME,din.DETECTIONED_STATE,din.NODE_IP,din.STATUS_CHANGE_TIME,din.DATA_CHECK_TIME_DIGITAL,din.DATA_ARRIVE_TIME_DIGITAL,din.SEQ_ID,din.DETECTION_INFO_ID,din.VALID,din.POLICE_EMERGENT"); + sbSql.append(" from detection_info_new din "); + sbSql.append(" where "); + sbSql.append(" din.seq_id in ( "); + sbSql.append(seqIds); + sbSql.append(" )"); + String sql = sbSql.toString(); + List> list = db.select(conn,sql); + if (list != null) { + for (Map item : list) { + Object[] obj = new Object[33]; + String seq = item.get("SEQ_ID").toString(); + String setId = item.get("DETECTION_SET_INFO_ID").toString(); + obj[DetectInfo.SETINFOID] = setId; + obj[DetectInfo.SEQID] = seq; + obj[DetectInfo.STATE] = (item.get("DETECTIONED_STATE") == null ? null + : Integer.parseInt(item.get("DETECTIONED_STATE").toString())); + obj[DetectInfo.CHECKTIME] = (item.get("DATA_CHECK_TIME_DIGITAL") == null ? null + : Long.parseLong(item.get("DATA_CHECK_TIME_DIGITAL").toString())); + obj[DetectInfo.PLEVEL] = (item.get("POLICE_LEVEL") == null ? -1 + : Integer.parseInt(item.get("POLICE_LEVEL").toString())); + obj[DetectInfo.URGENTLEVEL] = (item.get("POLICE_EMERGENT") == null ? -1 + : Integer.parseInt(item.get("POLICE_EMERGENT").toString())); + obj[DetectInfo.DETECTIONINFOID] = (item.get("DETECTION_INFO_ID")); + obj[DetectInfo.STATUSCHANGTIME] = (item.get("STATUS_CHANGE_TIME") == null ? null + : ((Date) item.get("STATUS_CHANGE_TIME")).getTime()); + obj[DetectInfo.ARRIVETIME] = (item.get("DATA_ARRIVE_TIME_DIGITAL") == null ? null + : Long.parseLong(item.get("DATA_ARRIVE_TIME_DIGITAL").toString())); + obj[DetectInfo.DELAYTIME] = (item.get("DELAY_TIME") == null ? null + : Integer.parseInt(item.get("DELAY_TIME").toString())); + obj[DetectInfo.NEXTCHECKTIME] = (item.get("NEXT_CHECK_TIME") == null ? null + : ((Date) item.get("NEXT_CHECK_TIME")).getTime()); + obj[DetectInfo.STARTTIME] = (item.get("START_TIME") == null ? null + : ((Date) item.get("START_TIME")).getTime()); + obj[DetectInfo.DSINFO] = (item.get("DETECTION_STATE_INFO")); + obj[DetectInfo.PDATA] = (item.get("PERFORMACE_DATA")); + obj[DetectInfo.CURRENTTIMES] = (item.get("CURRENT_TIMES") == null ? -1 + : Integer.parseInt(item.get("CURRENT_TIMES").toString())); + + // 将数据库中的最新数据组织成 seqId setId data + Map map = result.get(seq); + if (map == null) { + map = U.newHashMap(); + map.put(setId, obj); + result.put(seq, map); + } else { + map.put(setId, obj); + } + } + return result; + } + } catch (Exception e) { + logger.warn("select detection_info_new error", e); + } + return result; + } + + /** + * 判断状态变更具体实现 + * + * @param newDetecInfo 准备入库数据 + * + * @param oldDetecInfo new表中的数据 + */ + private void stateChange(Object[] newDetecInfo, Object[] oldDetecInfo) { + /** + * 判断状态变更 + */ + if (newDetecInfo == null || oldDetecInfo == null + || ((Long) newDetecInfo[DetectInfo.CHECKTIME] < (Long) oldDetecInfo[DetectInfo.CHECKTIME])) { + return; + } + if (((Integer) oldDetecInfo[DetectInfo.STATE] != (Integer) newDetecInfo[DetectInfo.STATE]) || // 状态改变 + (((Integer) oldDetecInfo[DetectInfo.STATE] == (Integer) newDetecInfo[DetectInfo.STATE] + && (Integer) oldDetecInfo[DetectInfo.PLEVEL] != (Integer) newDetecInfo[DetectInfo.PLEVEL]))) {// 状态未变,告警级别改变 + + int state = (Integer) newDetecInfo[DetectInfo.STATE]; + // 邮件告警信息 + StringBuffer alarmInfo = new StringBuffer((String) newDetecInfo[DetectInfo.ALARMINFO]);// 告警信息 + String dsinfo = (String) newDetecInfo[DetectInfo.DSINFO];// 状态信息 + String checkType = (String) newDetecInfo[DetectInfo.CHECKTYPE];// 监测类别 + long checkTime = (Long) newDetecInfo[DetectInfo.CHECKTIME]; + int actionType = 11; + int urgentLevel = EmailTypeConstants.URGENT_LATER; + // 整理告警数据,拼写邮件信息 + if (state == -1) { // 执行失败 + // alarmInfo.append("监测执行失败\n "); + // alarmInfo.append("Failure to monitor execution\n "); + alarmInfo.append("i18n_server.DetecDataResoveThread.alarmInfo1_n81i\n "); + alarmInfo.append(dsinfo + "\n"); + actionType = EmailTypeConstants.TYPE_DETECTION_INFO_EXCEPTION; + if (Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + + } + } else if (state == 0) { // 一般异常 + alarmInfo.append(" " + dsinfo + "\n"); + // 如果是握手监测,不添加告警设置字段相关信息 + if (!Constants.DETEC_NMSC_STR.equalsIgnoreCase(checkType)) { + alarmInfo.append(dsinfo + " \n"); + } else { + alarmInfo.delete(0, alarmInfo.length());// 如果是握手监测,则不添加告警设置字段相关信息 + alarmInfo.append(dsinfo + " \n"); + urgentLevel = EmailTypeConstants.URGENT_IMMEDIATELY; + } + actionType = EmailTypeConstants.TYPE_DETECTION_INFO_EXCEPTION; + } else if (state == 1) { // 恢复正常 + // alarmInfo.append("监测恢复正常"); + // alarmInfo.append("Monitoring back to normal"); + alarmInfo.append("i18n_server.DetecDataResoveThread.alarmInfo1_n81i"); + actionType = EmailTypeConstants.TYPE_DETECTION_INFO_RECOVER; + urgentLevel = EmailTypeConstants.URGENT_LATER; + // 更新该节点所有监测时间为当前监测时间 + if (checkType.equalsIgnoreCase(Constants.DETEC_NMSC_STR)) { + newDetecInfo[DetectInfo.STATUSCHANGTIME] = (checkTime); + } +// //update warning table +// collectWarnUpdateInfo(newDetecInfo); + } + // 报警通知 + + newDetecInfo[DetectInfo.STATECHANGEFLAG] = (true); + newDetecInfo[DetectInfo.ALARMINFO] = (alarmInfo.toString()); + newDetecInfo[DetectInfo.SENDEMAILFLAG] = (true); + newDetecInfo[DetectInfo.URGENTLEVEL] = (urgentLevel); + // 目前代码中是否紧急和紧急级别不对应,有的是紧急,但是级别是2,所以此处进行修改,如果是紧急,则级别修改为0级 + if (EmailTypeConstants.URGENT_IMMEDIATELY == urgentLevel) { + newDetecInfo[DetectInfo.PLEVEL] = Constants.LEVEL_OF_EMERGENCY; + } + // 如果是0级,则设置为紧急,使得紧急状态和级别一致 + if (newDetecInfo[DetectInfo.PLEVEL].equals(Constants.LEVEL_OF_EMERGENCY)) { + newDetecInfo[DetectInfo.URGENTLEVEL] = EmailTypeConstants.URGENT_IMMEDIATELY; + } + long seqIdLong = Long.parseLong((String) newDetecInfo[DetectInfo.SEQID]); + long setInfoIdLong = Long.parseLong((String) newDetecInfo[DetectInfo.SETINFOID]); + + String emailContent = alarmInfo.toString(); + if (emailContent.getBytes().length > 290) { + emailContent = StringUtil.substring(emailContent, 290); + } + EmailInfo emailInfo = new EmailInfo(actionType, + Common.getSetInfoNameMape().get(setInfoIdLong) + "(" + Common.getCheckTypeNameMape().get(checkType) + + ")", + Common.getNodeIpByUUID(seqIdLong), format.format(new Date(checkTime)), emailContent, + EmailTypeConstants.FLAG_SEND_LATER, urgentLevel); + newDetecInfo[DetectInfo.EMAILINFO] = (emailInfo); + + + //如果新入数据和new表中的监测状态不同,或者监测状态相同,告警级别不同 ,修改 状态改变时间 为获取新入数据的时间(data_check_time) + newDetecInfo[DetectInfo.STATUSCHANGTIME]=newDetecInfo[DetectInfo.CHECKTIME]; + } + else{//如果新入数据和new表中的监测状态相同,且告警级别相同 ,则状态改变时间更新为new表中的时间 + newDetecInfo[DetectInfo.STATUSCHANGTIME]=oldDetecInfo[DetectInfo.STATUSCHANGTIME]; + } + + + + } + + /** + * 状态变更判断:只对大于等于new表监测时间的监测数据进行是否告警的判断 + * + * @param allDetectInfo + * @param inDbInfo + */ + private void judgeState(List allDetectInfo, Object[] inDbInfo) { + /* + * 判断每一个的状态变更 1.监测时间小于已经入库的监测数据时间不做判断 + */ + int startIndex = 0; + for (int i = 0, j = allDetectInfo.size(); i < j; i++) { + Object[] newDetecInfo = allDetectInfo.get(i); + Object[] oldDetecInfo = null;// 前一条监测数据 + // 监测时间小于已经入库的监测数据时间不做判断,只在最后一条数据判断插入告警信息 + if (newDetecInfo == null + || (Long) newDetecInfo[DetectInfo.CHECKTIME] < (Long) inDbInfo[DetectInfo.CHECKTIME]) { + startIndex++; + if (i < j - 1) { + continue; + } else { + // 集合中的最后一条数据监测时间也小于数据库中的时间时,最后一条数据判断是否插入一条告警信息 + if ((Integer) newDetecInfo[DetectInfo.STATE] != 1) {// 延迟数据监测状态没有成功,数据库入库的是正常数据,补录一条报警信息 + if ((Integer) inDbInfo[DetectInfo.STATE] == 1 && (Integer) inDbInfo[DetectInfo.PLEVEL] == 99) { + newDetecInfo[DetectInfo.APPENDWARNINGINFO] = oldDetecInfo; + } + } + } + } + // 判断最新的监测数据 + if (i == startIndex) { + oldDetecInfo = inDbInfo; + } else { + oldDetecInfo = allDetectInfo.get(i - 1); + } + /* 判断状态变更 */ + stateChange(newDetecInfo, oldDetecInfo); + } + } + + /** + * stmt addbatch + * @param stmt + * @param params + * @throws SQLException + */ + private void addRecordToStatement(PreparedStatement stmt, Object[] params) throws SQLException { + if (params != null) { + logger.debug(" params " + Arrays.toString(params)); + for (int i = 0; i < params.length; i++) { + stmt.setObject(i + 1, params[i]); + } + stmt.addBatch(); + } + } + + /** + * @param allDeteInfo + * @param oldDetecInfo + * @param newDataList + */ + private void judgeAndCollect(List allDeteInfo, Object[] oldDetecInfo,List newDataList){ + Object[] newDeteInfo = allDeteInfo.get(0); + if((Long)newDeteInfo[DetectInfo.CHECKTIME]>(Long)oldDetecInfo[DetectInfo.CHECKTIME]){ + newDataList.add(newDeteInfo); + } + if((Integer)newDeteInfo[DetectInfo.STATE]==1){ + Object[] params={newDeteInfo[DetectInfo.SEQID],newDeteInfo[DetectInfo.SETINFOID]}; + try { + addRecordToStatement(warnUpdateStmt, params); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + private void saveToNew(List dataList,int flag){ + if(dataList!=null&&dataList.size()>0){ + if(flag==0){//insert + String sql=DetectInfo.getSaveToNewSql(); + try { + infoNewInsertStmt=conn.prepareStatement(sql); + for (Object[] newData : dataList) { + // 计算 详细表的 外键 时间戳(10) + seqId(5)+ setId (3),主要是为了使用 java 中long 类型 + String checkTime = newData[DetectInfo.CHECKTIME].toString(); + String setId = newData[DetectInfo.SETINFOID].toString(); + String seqId = newData[DetectInfo.SEQID].toString(); + String id = DetectInfo.computeId(checkTime, seqId, setId); + Object []params={ + newData[DetectInfo.SETINFOID], + newData[DetectInfo.DSINFO], + newData[DetectInfo.PDATA], + newData[DetectInfo.CURRENTTIMES], + newData[DetectInfo.STARTTIME] != null ? format.format(new Date((Long) newData[DetectInfo.STARTTIME])) : null, + newData[DetectInfo.WAITTIME], + newData[DetectInfo.DELAYTIME], + newData[DetectInfo.NEXTCHECKTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.NEXTCHECKTIME])) : null, + newData[DetectInfo.PLEVEL], + newData[DetectInfo.CHECKTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.CHECKTIME])) : null, + newData[DetectInfo.ARRIVETIME]!= null ? format.format(new Date((Long) newData[DetectInfo.ARRIVETIME])) : null, + newData[DetectInfo.STATE], + newData[DetectInfo.STATUSCHANGTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.STATUSCHANGTIME])) : null, + newData[DetectInfo.SEQID], + id, + newData[DetectInfo.CHECKTIME], + newData[DetectInfo.ARRIVETIME], + newData[DetectInfo.URGENTLEVEL] + }; + addRecordToStatement(infoNewInsertStmt, params); + } + infoNewInsertStmt.executeBatch(); + logger.info("info_new table insert success! size : "+ dataList.size()); + } catch (SQLException e) { + logger.error("save info to info_new table error"+e.getMessage()); + } + }else if(flag==1){//update + String sql=DetectInfo.getUpdateToNewSql(); + try { + infoNewUpdateStmt=conn.prepareStatement(sql); + for (Object[] newData : dataList) { + // 计算 详细表的 外键 时间戳(10) + seqId(5)+ setId (3),主要是为了使用 java 中long 类型 + String checkTime = newData[DetectInfo.CHECKTIME].toString(); + String setId = newData[DetectInfo.SETINFOID].toString(); + String seqId = newData[DetectInfo.SEQID].toString(); + String id = DetectInfo.computeId(checkTime, seqId, setId); + Object []params={newData[DetectInfo.DSINFO], + newData[DetectInfo.PDATA], + newData[DetectInfo.CURRENTTIMES], + newData[DetectInfo.STARTTIME] != null ? format.format(new Date((Long) newData[DetectInfo.STARTTIME])) : null, + newData[DetectInfo.WAITTIME], + newData[DetectInfo.DELAYTIME], + newData[DetectInfo.NEXTCHECKTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.NEXTCHECKTIME])) : null, + newData[DetectInfo.PLEVEL], + newData[DetectInfo.CHECKTIME]!= null ? format.format(new Date((Long) newData[DetectInfo.CHECKTIME])) : null, + newData[DetectInfo.ARRIVETIME]!= null ? format.format(new Date((Long) newData[DetectInfo.ARRIVETIME])) : null, + newData[DetectInfo.STATE], + newData[DetectInfo.STATUSCHANGTIME]!= null && (Long)newData[DetectInfo.STATUSCHANGTIME]!= -1? format.format(new Date((Long) newData[DetectInfo.STATUSCHANGTIME])) : null, + id, + newData[DetectInfo.CHECKTIME], + newData[DetectInfo.ARRIVETIME], + newData[DetectInfo.URGENTLEVEL], + newData[DetectInfo.SETINFOID], + newData[DetectInfo.SEQID] + }; + addRecordToStatement(infoNewUpdateStmt, params); + } + infoNewUpdateStmt.executeBatch(); + logger.info("info_new table update success! size : "+ dataList.size()); + } catch (SQLException e) { + logger.error("update info to info_new table error"+e.getMessage()); + } + } + dataList.clear(); + } + } + private void collectWarnUpdateInfo(Object[] newDetectInfo){ + Object[] params={newDetectInfo[DetectInfo.SEQID],newDetectInfo[DetectInfo.SETINFOID]}; + try { + addRecordToStatement(warnUpdateStmt, params); + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/src/com/nms/server/thread/email/RefreshEmailFlagThread.java b/src/com/nms/server/thread/email/RefreshEmailFlagThread.java new file mode 100644 index 0000000..7f90f92 --- /dev/null +++ b/src/com/nms/server/thread/email/RefreshEmailFlagThread.java @@ -0,0 +1,49 @@ +package com.nms.server.thread.email; + +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; + +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.util.BoneCPPool; +import com.nms.server.util.ExceptionPrintUtils; + +//监测数据主动收集管理线程 +public class RefreshEmailFlagThread implements Runnable{ + private Logger logger = Logger.getLogger(RefreshEmailFlagThread.class); + + public void run() { +// Thread.currentThread().setName("更新邮件功能是否启用标识 线程"); + Thread.currentThread().setName("Updata The Mail Function To Enable Identity Thread "); + CommonDao dao = null; + try { + dao = new CommonDao(); + CommonService service = new CommonService(dao); + Constants.flag_email = service.getFlagEmail();//定时的更新邮件发送是否启动标识 + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("执行结束"); + } + } + + public static void main(String[] args) { + try + { + BoneCPPool.initPool(); + Common.scheduled.scheduleWithFixedDelay(new RefreshEmailFlagThread(), 0, 5, TimeUnit.MINUTES); + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/com/nms/server/thread/errorinfo/ErrorInfoResoveManagerThread.java b/src/com/nms/server/thread/errorinfo/ErrorInfoResoveManagerThread.java new file mode 100644 index 0000000..86dc479 --- /dev/null +++ b/src/com/nms/server/thread/errorinfo/ErrorInfoResoveManagerThread.java @@ -0,0 +1,162 @@ +package com.nms.server.thread.errorinfo; + +import java.util.Calendar; +import java.util.LinkedList; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.NmsErrorInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.FileUtils; + +public class ErrorInfoResoveManagerThread implements Runnable{ + private Logger logger = Logger.getLogger(ErrorInfoResoveManagerThread.class); + private int testNum = 0;//尝试运行线程Constants.DETEC_DATA_RESOVE的次数 + + public void run() { + //将线程运行程序,尽可能的catch捕获异常 +// Thread.currentThread().setName("DC错误信息解析管理线程"); + Thread.currentThread().setName("DC Error Message Parsing Management Threads"); + try { + boolean runFlag = Common.isDbConnected();//测试数据库连接 + + //- 检查线程运行状态 运行中无操作 + Future future = Common.getFutureMap().get(Constants.ERROR_INFO_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone() && runFlag){ //运行中 + runFlag = false; + logger.info("DC错误信息解析执行线程 运行中 不再启动新解析线程"); + } + + /* + * 连续超过Constants.CHECK_WARNING_DATA_OVERRUN次数时,将数据存入硬盘中 + * 否则 正常执行数据解析线程 + * */ + if(!runFlag){ //判断并结束线程 + testNum = (testNum%Constants.CHECK_WARNING_DATA_OVERRUN); + testNum++; + if(testNum==Constants.CHECK_WARNING_DATA_OVERRUN) { + if(Constants.FLAG_ERROR_DATA_SAVE_DISK_RESOVE==1) { + /* + * 连续多个周期仍未解析完成 + * 说明list里存有大量的数据,则先将当前未解析的数据列表存入硬盘, + * 然后停掉解析线程,将正在解析的数据列表中的剩余数据存入硬盘 + * */ + logger.warn("Create a database connection failure and data stored to the hard disk"); + saveErrorDataToDisk(); + } + } + return; + }else{ //重置计数器 程序继续执行 + testNum = 1; + + } + + //- 非升级操作判断 + if(Common.SERVER_UN_UPGRADE_FLAG){ + + //- 为空或空闲中 变更存储区 启动新解析线程 + Common.changeErrorInfoFlag(); //变更数据存放集合 + + LinkedList dsbList = Common.getErrorInfoList(); // 获取非存放状态的数据集合 + if(dsbList != null && dsbList.size()>0){ + logger.info("DC错误信息解析执行线程 空闲中 启动新解析线程"); + //-- 获取线程执行 需进行主动告警和邮件通知等相关操作,待考虑 + future = Common.service.submit(new ErrorInfoResoveThread("DC错误信息解析执行线程",dsbList)); + //注册 + Common.getFutureMap().put(Constants.ERROR_INFO_RESOVE, future); + }else{ + logger.info("获取到的集合中数据数量为0,无需启动解析线程"); + } + } + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + }finally{ + logger.debug("执行结束"); + } + } + + /** + * 数据库是否可连接判断 + * @time Mar 7, 2013-2:22:42 PM + * @return + */ + public boolean dbTest() { + CommonDao dao = null; + try { +// if(!false)throw new Exception("强制异常"); + dao = new CommonDao(); + logger.debug("尝试获取数据库连接成功"); + return true; + } catch (Exception e) { + logger.error("Try to get a database connection failure", e); + /* 告警 */ + return false; + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + ////2013-2-19 hyx :当监测数据量过大时,将数据先存放到硬盘,再定时入库(由单独的定时线程实现) + public void saveErrorDataToDisk() { + LinkedList notResolvingErrorInfoList = Common.getNotResovlingErrorInfoList(); // 获取未在解析的监测数据集合 + try { + + //终止正在解析监测数据的线程,并将正在解析的list里未解析的数据存入硬盘 + Future future = Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + future.cancel(true); + } + LinkedList detectDataList = Common.getErrorInfoList(); // 获取非存放状态的数据集合(正在解析的list) + saveByteToFile(detectDataList); + + //将未在解析的list存放到硬盘上 + saveByteToFile(notResolvingErrorInfoList); + + }catch(IndexOutOfBoundsException e) { + logger.error("When monitoring data is too much, the data is stored in the hard disk exception",e); + }catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + + } + + private void saveByteToFile(LinkedList dataList) { + String filePath = ""; + int dataSize = dataList.size();//如果不按此时的size来操作,则list里的数据可能会一直在增加,就会一直循环下去? + logger.info("错误数据过多,共"+dataSize+"条错误数据,等待存入硬盘"); + try { + for(int j=0;j0) { + NmsErrorInfo data= dataList.get(0);//因为每次循环都会dataList.remove(0);所以每次操作的都是第0条记录 + filePath = Constants.OVERRUN_DETEC_FILE_DIR+"/"+Common.getDateDirName()+"/"+Calendar.getInstance().getTimeInMillis()+"_"+j+".csv"; +// FileUtils.wirteBytesToFile(data, filePath);//此处暂时将错误信息抛弃 + logger.info("被抛弃的DC ErrorInfo:"+data.getErrorCode()+"-"+data.getErrortDesc()+"="+data.getErrortGetip()+"="+data.getErrortIp()+"="+data.getErrorTime()); + dataList.remove(0); + logger.info("监测数据过多,已保存至"+filePath); + } + } + + }catch(Exception e) { + logger.error("When monitoring data is too much, the data is stored in the hard disk exception",e); + } + } + + public static void main(String [] args) { + Common.scheduled.scheduleAtFixedRate(new ErrorInfoResoveManagerThread(),0,50,TimeUnit.SECONDS); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.exit(0); + } +} diff --git a/src/com/nms/server/thread/errorinfo/ErrorInfoResoveThread.java b/src/com/nms/server/thread/errorinfo/ErrorInfoResoveThread.java new file mode 100644 index 0000000..59c8b4d --- /dev/null +++ b/src/com/nms/server/thread/errorinfo/ErrorInfoResoveThread.java @@ -0,0 +1,89 @@ +package com.nms.server.thread.errorinfo; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Callable; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.NmsErrorInfo; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.CommonService; + +/** + * CSV解析线程 + * + * @author ZGGG3 + * + */ +public class ErrorInfoResoveThread implements Callable { + Logger logger = Logger.getLogger(ErrorInfoResoveThread.class); + volatile boolean stop = false;//线程是否被取消标志 + private String name; // 自定义Thread Name + private LinkedList dsbList; + + public ErrorInfoResoveThread(String name,LinkedList dsbList) { + this.name = name; + this.dsbList = dsbList; + } + + /* + * 线程操作 + * + * 依次解析urlList中仍存在的Files + * + * 实现了依次解析Files,由于后期实现多线程解析操作 + */ + public Object call() { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name); + logger.debug(" 本次解析错误信息数 "+(dsbList==null?"null":dsbList.size())); + + CommonDao dao = null; + try { + dao = new CommonDao(); + CommonService service = new CommonService(dao); + + //-- 空数据集合 结束操作 + if(dsbList == null || dsbList.size() == 0 ){ + return null; + } + + long sTime = System.currentTimeMillis(); + logger.debug("错误信息数据 " + (dsbList.size())+ "条,保存开始"); + + //-- 执行数据解析 + String id = service.resoveErrorInfo(dsbList);//入库到表detection_info和detection_info_new中。直接用返回的id,若是systeminfo,则入库第一行之后的行 + dsbList.clear(); + + } catch (Exception e) { + + logger.error("Running exception",e); + }finally{ + + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("数据清理 解析完成"); + } + + return null; + } + +// public static void main(String[] args) { +// try { +// LinkedList neiList = new LinkedList(); +// neiList.add(new NmsErrorInfo(Constants.ERROR_CODE_CREATE_SOCKET,new Date(),Common.getLocalIp(),"10.0.6.113",Constants.ERROR_INFO_SATAE_ERROR,"描述")); +// Common.service.submit(new ErrorInfoResoveThread("xx",neiList)); +// }catch(Exception e) { +// e.printStackTrace(); +// } +// } +} diff --git a/src/com/nms/server/thread/file/DetectFileReaderThread.java b/src/com/nms/server/thread/file/DetectFileReaderThread.java new file mode 100644 index 0000000..4316e0a --- /dev/null +++ b/src/com/nms/server/thread/file/DetectFileReaderThread.java @@ -0,0 +1,145 @@ +package com.nms.server.thread.file; + +import java.io.File; +import java.io.FileInputStream; +import java.util.Calendar; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.FileUtils; + +/** + * XML解析线程 + * + * @author ZGGG3 + * + */ +public class DetectFileReaderThread implements Runnable { + Logger logger = Logger.getLogger(DetectFileReaderThread.class); + private String dir; + private String name; // 自定义Thread Name + + public DetectFileReaderThread(String name, String url) { + this.dir = url; + this.name = name; + } + + + public void run() { + + // 为当前线程命名,用与日志友好输出。 + Thread.currentThread().setName(name); + + try { + logger.info("文件夹路径 :>" + dir); + + File driFile = new File(dir); + + // 获取目录下的.csv文件集合 + File[] files = FileUtils.sortByFileNameASC(FileUtils.listFilesEndWith(driFile, ".csv")); // 按文件名称 升序排序 + + // 解析文件数为0时,终止该计划任务 + if(files==null || files.length==0){ + Common.cancelRunnableAtOnce(dir); + return; + } + + logger.debug("文件夹路径 :>" + driFile.getAbsolutePath() + " 下 " + files.length + "个文件,解析开始"); + long l = Calendar.getInstance().getTimeInMillis(); + if (files != null && files.length > 0) { + for (File file : files) { + + if(file.length()>0){ + try { + FileInputStream fis = new FileInputStream(file); + + int fileLength = (int) file.length(); //大小 + int len = 0; + byte[] buff = new byte[fileLength]; + if(fileLength>0){ + len = fis.read(buff); + } + if(buff.length==0){continue;} + Common.addDeteData(buff); + fis.close(); + } catch (Exception e) { + FileUtils.copyFile(file, new File(Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName())); + logger.error("Monitoring data file reading errors:"+Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName(),e); + } + } + file.delete(); + } + } + logger.debug(driFile.getAbsolutePath()+" 解析完成耗时:>"+(Calendar.getInstance().getTimeInMillis()-l)); + + //清理文件 + files = FileUtils.sortByFileNameASC(FileUtils.listFilesEndWith(driFile, ".tp")); // 按文件名称 升序排序 + if(files!=null && files.length>0){ + for(File file : files){ + file.delete(); + } + } + logger.debug("临时文件清理完成"); + } catch (Exception e) { + logger.error("",e); + } + } +/* + public static void main(String[] args) { + CommonDao dao = null; + try { + BoneCPPool.initPool(); + dao = new CommonDao(); + new InitServerThread().initCommonInfo(dao); + } catch (ClassNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (SQLException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + }finally{ + dao.close(); + } + + + + long l = Calendar.getInstance().getTimeInMillis(); + Future future = Common.service.submit(new DetectFileReaderThread("解析 监测数据文件","D:\\NMS\\nmsdata\\dc_zip\\resove\\detect")); + try { + future.get(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(Calendar.getInstance().getTimeInMillis()-l); + future = Common.service.submit(new DetecDataResoveThread("解析 监测数据文件",Common.getDeteDataList())); + try { + future.get(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Common.chengeDeteDataFlag(); + future = Common.service.submit(new DetecDataResoveThread("解析 监测数据文件",Common.getDeteDataList())); + try { + future.get(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }*/ +} diff --git a/src/com/nms/server/thread/file/ResultFileReaderThread.java b/src/com/nms/server/thread/file/ResultFileReaderThread.java new file mode 100644 index 0000000..6e1a4a7 --- /dev/null +++ b/src/com/nms/server/thread/file/ResultFileReaderThread.java @@ -0,0 +1,105 @@ +package com.nms.server.thread.file; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.nio.charset.Charset; +import java.util.Calendar; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult2; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.FileUtils; +import com.nms.server.util.UnicodeReader; + +/** + * XML解析线程 + * + * @author ZGGG3 + * + */ +public class ResultFileReaderThread implements Runnable { + Logger logger = Logger.getLogger(ResultFileReaderThread.class); + private String dir; + private String name; // 自定义Thread Name + + public ResultFileReaderThread(String name, String url) { + this.dir = url; + this.name = name; + } + + public void run() { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name+" "+dir); + + try { + + logger.info("文件夹路径 :>" + dir); + File driFile = new File(dir); + + //获取目录下的.result文件集合 + File[] files = FileUtils.sortByFileNameASC(FileUtils.listFilesEndWith(driFile, ".result")); // 按文件名称 升序排序 + + // 解析文件数为0时,终止该计划任务 + if(files==null || files.length==0){ + Common.cancelRunnableAtOnce(dir); + return; + } + + logger.debug("文件夹路径 :>" + driFile.getAbsolutePath() + " 下 " + files.length + "个文件,解析开始"); + long l = Calendar.getInstance().getTimeInMillis(); + + if (files != null && files.length > 0) { + + for (File file : files) { + + if(file.length()>0){ + try { + BufferedReader reader = new BufferedReader(new UnicodeReader(new FileInputStream(file), Charset.defaultCharset().name())); + String data = StringUtils.trim(reader.readLine()); + String [] result = StringUtils.isEmpty(data)?null:data.split(Constants.COMMON_DATA_SPLIT); + + if(result != null){ + MissionResult2 result2 = Common.resoveMissionResult(result); + Common.addResultToResultList(result2); + } + reader.close(); + } catch (Exception e) { + logger.error("",e); + } + } + file.delete(); + } + } + logger.debug(driFile.getAbsolutePath()+" 解析完成耗时:>"+(Calendar.getInstance().getTimeInMillis()-l)); + //清理 + files = FileUtils.sortByFileNameASC(FileUtils.listFilesEndWith(driFile, ".tp")); // 按文件名称 升序排序 + if(files!=null && files.length>0){ + for(File file : files){ + file.delete(); + } + } + logger.debug("清理临时文件完成"); + } catch (Exception e) { + logger.error("",e); + } + } + +/* public static void main(String[] args) { + long l = Calendar.getInstance().getTimeInMillis(); + Future future = Common.service.submit(new FileReaderThread("文件解析线程", "D:\\cvs\\10.0.6.10")); + try { + future.get(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(Calendar.getInstance().getTimeInMillis()-l); + }*/ +} diff --git a/src/com/nms/server/thread/file/ReturnFileReaderThread.java b/src/com/nms/server/thread/file/ReturnFileReaderThread.java new file mode 100644 index 0000000..aa73e93 --- /dev/null +++ b/src/com/nms/server/thread/file/ReturnFileReaderThread.java @@ -0,0 +1,155 @@ +package com.nms.server.thread.file; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.nio.charset.Charset; +import java.util.Calendar; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult2; +import com.nms.server.bean.ReturnFilePO; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.FileUtils; +import com.nms.server.util.UnicodeReader; + +/** + * XML解析线程 + * + * @author ZGGG3 + * + */ +public class ReturnFileReaderThread implements Runnable { + Logger logger = Logger.getLogger(ReturnFileReaderThread.class); + private String dir; + private String name; // 自定义Thread Name + + public ReturnFileReaderThread(String name, String url) { + this.dir = url; + this.name = name; + } + + public void run() { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name); + + try { + File root = new File(dir); + File[] dirFiles = FileUtils.listFilesForDirectory(root); + + // 解析文件数为0时,终止该计划任务 + if(dirFiles==null || dirFiles.length==0){ + Common.cancelRunnableAtOnce(dir); + return; + } + + for(File driFile : dirFiles){ + + logger.info("文件夹路径 :>" + driFile.getAbsolutePath()); + + //获取目录下的.return文件集合 + File[] files = FileUtils.sortByFileNameASC(FileUtils.listFilesEndWith(driFile, ".return")); // 按文件名称 升序排序 + + // 解析文件数为0时,终止该计划任务 + if(files==null || files.length==0){ + FileUtils.deleteDirectory(driFile); + continue; + } + + logger.debug("文件夹路径 " + files.length + "个文件,解析开始"); + long l = Calendar.getInstance().getTimeInMillis(); + if (files != null && files.length > 0) { + for (File file : files) { + if(file.length()>0){ + + BufferedReader reader = new BufferedReader(new UnicodeReader(new FileInputStream(file), Charset.defaultCharset().name())); + String data = StringUtils.trim(reader.readLine()); + + if(StringUtils.isNotEmpty(data)){ + + ReturnFilePO returnFilePO = (ReturnFilePO) JSONObject.toBean(JSONObject.fromObject(data), ReturnFilePO.class); + String fnInfo = ""; + if(StringUtils.isEmpty(returnFilePO.getReturnFileName())){ + fnInfo= ""; + }else{ +// fnInfo= " 回传成功"; +// fnInfo= " Success comes back"; + fnInfo= " i18n_server.ReturnFileReaderThread.fnInfo_n81i"; + } + MissionResult2 result2 = new MissionResult2( + returnFilePO.getTaskId(), + returnFilePO.getTaskType(), + returnFilePO.getUuid(), + returnFilePO.getStartTime().getTime(), + returnFilePO.getEndTime().getTime(), + null, + returnFilePO.getState(), + returnFilePO.getResDesc()+ fnInfo, + null, + returnFilePO.getIsLoop() + ); + + if(StringUtils.isNotEmpty(returnFilePO.getReturnFileName())){ + String rFilePath = file.getParentFile().getAbsolutePath()+File.separator+returnFilePO.getReturnFileName(); + File rFile = new File(rFilePath); + + if(rFile.exists()){ //回传文件存在 + System.out.println("回传文件存在"); + + result2.setFileInfo(returnFilePO.getReturnFileName()); + FileUtils.copyFile(rFile, new File(Constants.MISSION_FILE_UPLOAD_DIR+returnFilePO.getReturnFileName())); + rFile.delete(); + }else{ + System.out.println("回传文件不存在"); + result2.setDescription(result2.getDescription()+"\n"+returnFilePO.getReturnFileName()+"不存在"); + } + } + + Common.addResultToResultList(result2); + } + reader.close(); + } + file.delete(); + } + } + logger.debug(driFile.getAbsolutePath()+" 解析完成耗时:>"+(Calendar.getInstance().getTimeInMillis()-l)); + //清理 + files = FileUtils.sortByFileNameASC(FileUtils.listFilesEndWith(driFile, ".tp")); // 按文件名称 升序排序 + if(files!=null && files.length>0){ + for(File file : files){ + file.delete(); + } + } + + //删除当前目录 + driFile.delete(); + } + logger.debug("临时文件清理完成"); + } catch (Exception e) { + logger.error("",e); + }finally{ + } + } + + public static void main(String[] args) { + long l = Calendar.getInstance().getTimeInMillis(); + Future future = Common.service.submit(new ReturnFileReaderThread("文件解析线程", "D:\\ControllerData\\dc_zip\\resove\\return")); + try { + future.get(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ExecutionException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(Calendar.getInstance().getTimeInMillis()-l); + } +} diff --git a/src/com/nms/server/thread/file/UnZipManagerThread.java b/src/com/nms/server/thread/file/UnZipManagerThread.java new file mode 100644 index 0000000..a963383 --- /dev/null +++ b/src/com/nms/server/thread/file/UnZipManagerThread.java @@ -0,0 +1,63 @@ +package com.nms.server.thread.file; + +import java.io.File; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.FileUtils; + +public class UnZipManagerThread implements Runnable{ + Logger logger = Logger.getLogger(UnZipManagerThread.class); + + public void run() { + +// Thread.currentThread().setName("UnZip管理线程"); + Thread.currentThread().setName("UnZip Management Thread"); + + // 将线程运行程序,尽可能的catch捕获异常 + try { + // 待解压文件 集合 + List zipFiles = new LinkedList(); + + zipFiles.addAll(Arrays.asList(FileUtils.listFilesEndWith(new File(Constants.ZIP_FILE_DETECT_DATA_DIR), ".zip"))); + zipFiles.addAll(Arrays.asList(FileUtils.listFilesEndWith(new File(Constants.ZIP_FILE_TASK_RESULT_DIR), ".zip"))); + zipFiles.addAll(Arrays.asList(FileUtils.listFilesEndWith(new File(Constants.ZIP_FILE_TASK_RETURN_DIR), ".zip"))); + + // 无可解压的文件时 结束该周期任务 + if(zipFiles.size()==0){ + logger.info("已无可解压文件 周期任务终止"); + Common.cancelRunnableAtOnce(Constants.UNZIP_FILE_MANAGER); + return; + } + + // 校验解析线程是否存在 不存在时创建 并执行 + Future future = Common.getFutureMap().get(Constants.UNZIP_FILE); + if(future != null && !future.isCancelled() && !future.isDone()){ + + //运行中 + logger.info("UnZip线程 运行中 不再重复启动"); + return; + }else{ + + //为空或空闲中 + logger.info("UnZip线程 空闲中 启动新线程"); + future = Common.service.submit(new UnZipThread("UnZip线程", zipFiles)); + //注册 + Common.getFutureMap().put(Constants.UNZIP_FILE, future); + } + + } catch (Exception e) { + logger.error("",e); + } + } + + public static void main(String[] args) { + Common.service.submit(new UnZipManagerThread()); + } +} diff --git a/src/com/nms/server/thread/file/UnZipThread.java b/src/com/nms/server/thread/file/UnZipThread.java new file mode 100644 index 0000000..71599ef --- /dev/null +++ b/src/com/nms/server/thread/file/UnZipThread.java @@ -0,0 +1,124 @@ +package com.nms.server.thread.file; + +import java.io.File; +import java.util.Calendar; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.FileUtils; +import com.nms.server.util.ZipUtil; + +/** + * XML解析线程 + * + * @author ZGGG3 + * + */ +public class UnZipThread implements Callable { + + Logger logger = Logger.getLogger(UnZipThread.class); + + private List zipFileList; + private String name; // 自定义Thread Name + + public UnZipThread(String name ,List zipFileList) { + this.zipFileList = zipFileList; + this.name = name; + } + + + public Object call() { + // 为当前线程命名,用与开发阶段友好输出。 + Thread.currentThread().setName(name); + try { + + //筛选文件目录下的子目录 + File[] files = FileUtils.sortByFileNameASC(zipFileList.toArray(new File[0])); // 按文件名称 升序排序 + + if (files != null && files.length > 0) { + long l = Calendar.getInstance().getTimeInMillis(); + + String resoveDirPath = null; + int fileType = -1; // 1:detect 2:result 3:Return + + for (File file : files) { + + logger.debug("解压文件 :>" + file.getAbsolutePath()); + try { + // 监测数据 + if(file.getAbsolutePath().startsWith(new File(Constants.ZIP_FILE_DETECT_DATA_DIR).getAbsolutePath())){ + resoveDirPath = Constants.ZIP_RESOVE_DETECT_DATA_DIR; + fileType = 1; + } + + // 任务结果 + else if(file.getAbsolutePath().startsWith(new File(Constants.ZIP_FILE_TASK_RESULT_DIR).getAbsolutePath())){ + resoveDirPath = Constants.ZIP_RESOVE_TASK_RESULT_DIR; + fileType = 2; + } + + // 回传文件 + else if(file.getAbsolutePath().startsWith(new File(Constants.ZIP_FILE_TASK_RETURN_DIR).getAbsolutePath())){ + resoveDirPath = Constants.ZIP_RESOVE_TASK_RETURN_DIR; + fileType = 3; + } + // 容错处理 删除无效文件 + else{ + file.delete(); + return null; + } + + //解压zip操作 + if(fileType == 3){ + ZipUtil.unZip(file.getAbsolutePath(), resoveDirPath+file.getName()); + }else{ + ZipUtil.unZip(file.getAbsolutePath(), resoveDirPath); + } + + //删除已解压文件 + file.delete(); + + Future future = Common.getFutureMap().get(resoveDirPath); + + //线程不存在 创建和注册 + if(future == null){ + switch (fileType) { + case 1: //监测数据 20160909 hyx 为了避免因长时间未收集数据,导致一次性收集大量的数据(nc端默认1000csv打包zip,所以如果监测数据打包发送,表明数据较多),如果全部进行解析入库,会导致内存溢出,所以存入dc_overrun等待之后慢慢解析 +// future = Common.scheduled.scheduleWithFixedDelay(new DetectFileReaderThread("解析 监测数据文件",resoveDirPath), 0, 10, TimeUnit.SECONDS); + break; + case 2: //任务结果 + future = Common.scheduled.scheduleWithFixedDelay(new ResultFileReaderThread("解析 任务结果文件",resoveDirPath), 0, 10, TimeUnit.SECONDS); + break; + case 3: //回传文件 + future = Common.scheduled.scheduleWithFixedDelay(new ReturnFileReaderThread("解析 回传文件文件",resoveDirPath), 0, 10, TimeUnit.SECONDS); + break; + default: + break; + } + Common.registRunnable(resoveDirPath, future); + } + + } catch (Exception e) { + logger.error("Unzip file abnormality "+file.getAbsolutePath(),e); + } + } + logger.debug(zipFileList.size()+"个zip文件 解压完成 共耗时:>"+(Calendar.getInstance().getTimeInMillis()-l)); + } + } catch (Exception e) { + logger.error("",e); + } + return null; + } + +/* public static void main(String[] args) { + List zipFiles = new LinkedList(); + zipFiles.addAll(Arrays.asList(FileUtils.listFilesEndWith(new File(Constants.ZIP_FILE_TASK_RETURN_DIR), ".zip"))); + Common.service.submit(new UnZipThread("解压线程", zipFiles)); + }*/ +} diff --git a/src/com/nms/server/thread/file/upload/FileUploadManagerThread.java b/src/com/nms/server/thread/file/upload/FileUploadManagerThread.java new file mode 100644 index 0000000..5d3ffdf --- /dev/null +++ b/src/com/nms/server/thread/file/upload/FileUploadManagerThread.java @@ -0,0 +1,131 @@ +package com.nms.server.thread.file.upload; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.UpgradeService; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.FileUtils; + +public class FileUploadManagerThread implements Runnable{ + Logger logger = Logger.getLogger(FileUploadManagerThread.class); + public void run() { + + //将线程运行程序,尽可能的catch捕获异常 + try { + +// Thread.currentThread().setName("文件上传管理线程"); + Thread.currentThread().setName("File Upload Management Thread"); + String rootDir = Constants.MISSION_FILE_UPLOAD_DIR; + + File rootFile = new File(rootDir); + if(FileUtils.listFilesEndWithout(rootFile, ".tp").length==0){ + logger.info("根目录:> " + rootDir+" 无新文件"); + return ; + } + + //任务回传文件归类 增加任务ID解析 + Map> missionFileMaps = new HashMap>(); + File [] files = FileUtils.listFilesEndWithout(rootFile, ".tp"); + logger.info("根目录:> " + rootDir+" 新文件数:"+files.length); + CommonDao dao = null; + UpgradeService service = null; + try { + for(File taskFile : files){ + try { + Long mid = resoveMissionIdByFileName(taskFile.getName()); + List fileNameList = missionFileMaps.get(mid); + if(fileNameList == null){ + fileNameList = new LinkedList(); + missionFileMaps.put(mid, fileNameList); + } + fileNameList.add(taskFile); + String hostIp = Common.getWebIPByMissionId(mid); + if(hostIp==null){ + if(dao == null){ + dao = new CommonDao(); + service = new UpgradeService(dao); + } + hostIp = service.getHostIpByMissionId(mid); + Common.addMissionIdWebIPMap(mid,hostIp); + } + }catch (NumberFormatException e) { + logger.error("Getting the task ID error from the file name["+taskFile.getName()+"]",e); + FileUtils.copyFile(taskFile, new File(taskFile.getAbsoluteFile()+".Error.tp")); + taskFile.delete(); + }catch (Exception e) { + logger.error("File:"+taskFile.getName(),e); + FileUtils.copyFile(taskFile, new File(taskFile.getAbsoluteFile()+".Error.tp")); + taskFile.delete(); + } + } + } catch (Exception e) { + logger.error("",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + + if(missionFileMaps.size()>0){ + Iterator>> ite = missionFileMaps.entrySet().iterator(); + while (ite.hasNext()) { + Entry> endtry = ite.next(); + Future future = Common.getFutureMap().get(rootFile.getAbsolutePath()+endtry.getKey()); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + logger.info(rootFile.getAbsolutePath()+" 上传文件线程 运行中 不再启动新线程"); + }else{ //为空或空闲中 + logger.info(rootFile.getAbsolutePath()+" 上传文件线程 空闲中 启动新线程"); + String hostIp = Common.getWebIPByMissionId(endtry.getKey()); + if(hostIp==null){ + hostIp = Constants.WEB_SOCKET_IP; + } + future = Common.service.submit(new FileUploadThread3("Upload File Thread",hostIp,endtry.getValue().toArray(new File[0]))); + //注册 + Common.getFutureMap().put(rootFile.getAbsolutePath()+endtry.getKey(), future); + } + } + } + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + }finally{ + logger.info("操作结束"); + } + } + + public static Long resoveMissionIdByFileName(String fileName) { + Long missionId = null; + if(fileName.lastIndexOf("_T")!=-1){ +// System.out.println(fileName.lastIndexOf("_T")); +// System.out.println(fileName.lastIndexOf("_")); +// System.out.println(fileName.indexOf("_",fileName.lastIndexOf("_T")+1)); + String mid = fileName.substring(fileName.lastIndexOf("_T")+2,fileName.indexOf("_",fileName.lastIndexOf("_T")+1)); + missionId = Long.parseLong(mid); + }else{ + String fileName0 = fileName.substring(0,fileName.lastIndexOf("_")); + String mid = fileName0.substring(fileName0.lastIndexOf("_")+1,fileName0.length()); + missionId = Long.parseLong(mid); + } + + return missionId; + } + public static void main(String [] args) { +// System.out.println("mid "+resoveMissionIdByFileName("tasklist_0_T176_1322424525354")); +// System.out.println("mid "+resoveMissionIdByFileName("test_script_T1778_1363762154602994_relative.tar.gz")); +// System.out.println("mid "+resoveMissionIdByFileName("tasklist_0__176_1322424525354.txt")); + Common.service.submit(new FileUploadManagerThread()); + } +} diff --git a/src/com/nms/server/thread/file/upload/FileUploadThread3.java b/src/com/nms/server/thread/file/upload/FileUploadThread3.java new file mode 100644 index 0000000..afbcae8 --- /dev/null +++ b/src/com/nms/server/thread/file/upload/FileUploadThread3.java @@ -0,0 +1,87 @@ +package com.nms.server.thread.file.upload; + +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Calendar; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.socket.SSLSocketCallable; + +/** + * XML解析线程 + * + * @author ZGGG3 + * + */ +public class FileUploadThread3 extends SSLSocketCallable { + Logger logger = Logger.getLogger(FileUploadThread3.class); + private static Integer port = Constants.WEB_SOCKET_PORT; + + private String name; // 自定义Thread Name + private File[] files; + public FileUploadThread3(String name,String ip,File[] files) throws Exception { + super(ip, port); + this.name = name; + this.files = files; + } + + /* + * 线程操作 + * + * 依次解析urlList中仍存在的Files + * + * 实现了依次解析Files,由于后期实现多线程解析操作 + */ + protected Object toDo() throws Exception { + // 为当前线程命名 ,用与开发阶段友好输出。 + Thread.currentThread().setName(name); + + try { + + //循环遍历目录子文件 + logger.debug("上传文件数量:> " + files.length + "个文件"); + + + long l = Calendar.getInstance().getTimeInMillis(); + if (files != null && files.length > 0) { + + sendMessage("byte:bpUploadFiles"); + String msg = receiveMessage(); + + bpSendFileByBath(Arrays.asList(files), null); + String params = this.receiveMessage(); //接收需要上传的文件列表 + System.out.println("bpSendFileByBath "+params); + + } + + for(File file :files){ + file.delete(); + logger.debug(file.getAbsoluteFile()+"上传完成,已删除!"); + } + logger.debug("上传文件 "+files.length+"个 上传耗时:"+(Calendar.getInstance().getTimeInMillis()-l)+" ms"); + logger.info("上传完毕!"); + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + finally{ +// dao.close(); + } + return null; + } + +// public static void main(String[] args) { +// long l = Calendar.getInstance().getTimeInMillis(); +// try { +// Future future = Common.service.submit(new FileUploadThread3("文件上传线程", Constants.MISSION_FILE_UPLOAD_DIR)); +// future.get(); +// System.exit(0); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// System.out.println(Calendar.getInstance().getTimeInMillis()-l); +// } +} diff --git a/src/com/nms/server/thread/mission/InitRunningMissionThread.java b/src/com/nms/server/thread/mission/InitRunningMissionThread.java new file mode 100644 index 0000000..fa524a3 --- /dev/null +++ b/src/com/nms/server/thread/mission/InitRunningMissionThread.java @@ -0,0 +1,285 @@ +package com.nms.server.thread.mission; + +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult; +import com.nms.server.bean.MissionResult2; +import com.nms.server.bean.MissionStateTable; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.common.MissionConstants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.EmailService; +import com.nms.server.service.UpgradeService; +import com.nms.server.thread.socket.NMSWebBPDownload; +import com.socket.utils.FileComment; + +/** + * 任务加载管理线程 + * 每两分钟执行一次,或接收激活 + * @date Nov 9, 2011 3:28:45 PM + * @author ZhangGang + * + */ +public class InitRunningMissionThread implements Runnable{ + /* 线程基本属性 */ + private Logger logger = Logger.getLogger(InitRunningMissionThread.class); + volatile boolean stop = false; //线程中断标示 + + /* 业务操作属性 */ + private CommonDao dao = null; //数据库链接 + private UpgradeService service = null; // + private EmailService emailService = null; // + private String missIds = null; + + + public InitRunningMissionThread(String missIdsTmp){ + missIds = missIdsTmp; + } + + @Override + public void run(){ +// Thread.currentThread().setName("任务载入线程"); + Thread.currentThread().setName("Task Loading Thread"); + + try { + /* 任务加载错开并发时间 等待 0-20秒 */ + /* int waitTime = (int)(Math.random()*20000); + Thread.sleep(waitTime); + logger.debug("任务加载时间ms:"+waitTime);*/ + + dao = new CommonDao(); //数据库链接 + service = new UpgradeService(dao); //专属Service + emailService = new EmailService(dao); + + List missionList = service.getStartMessionList(missIds); + + //- 任务校验及加载处理业务 + if(missionList != null && missionList.size()>0){ +// ml:for(MissionStateTable mission : missionList){ + ml:for(int i=0;i"+mission.getMissionId()+" "+mission.getMissionState()); + if(mission.getMissionState()==null){ //后续可增加任务告警 + continue; + } + + /* 任务状态为3、4、7 无需执行 不会出现 */ +// if(mission.getMissionState().intValue() == 3 +// ||mission.getMissionState().intValue() == 4 +// ||mission.getMissionState().intValue() == 7 ){ +// logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+" 3、4、7状态 执行结束"); +// continue; +// } + + + /* 判断当前DC是否需要执行 */ + List mrList = null; + + if(mission.getLoopFlag()!=null && mission.getLoopFlag().longValue()==1l){ //周期任务 + + //- 任务时间的有效性校验 + if(mission.getEndTime()==null || mission.getEndTime().getTime() < System.currentTimeMillis() ){ + if(mission.getMissionState().longValue() ==2l){ + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); +// service.clearTimeoutLoopMisssion(mission.getMissionId());//2013-10-30 hyx 以下修改主任务表的状态,未修改结果表和loop表的状态 +// service.updateMissionState(mission.getMissionId(), 3,format.format(System.currentTimeMillis())+" 已超过任务结束时间 任务执行结束"); +// emailService.sendEmailForMission(mission.getMissionId(),"任务执行失败"," 已超过任务结束时间 任务执行结束"); +// logger.warn("已超过任务结束时间 任务执行结束"); +// service.updateMissionState(mission.getMissionId(), 3,format.format(System.currentTimeMillis())+" The task has exceeded the end time and the task execution has ended"); +// emailService.sendEmailForMission(mission.getMissionId(),"Task execution failure"," The task has exceeded the end time and the task execution has ended"); + service.updateMissionState(mission.getMissionId(), 3,format.format(System.currentTimeMillis())+" i18n_server.InitRunningMissionThread.outEndTime_n81i"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.InitRunningMissionThread.missionFail_n81i"," i18n_server.InitRunningMissionThread.outEndTime_n81i"); + logger.warn("The task has exceeded the end time and the task execution has ended"); + } + /***告警功能****/ + continue ; + } + + if(mission.getMissionState() == 2l){ + mrList = service.getMissionResultsForLoopTask(mission.getMissionId(), mission.getMissionType().intValue(),3); + }else if(mission.getMissionState() ==6l){ + mrList = service.getMissionResultsForLoopTask(mission.getMissionId(), mission.getMissionType().intValue(),5); + } + }else{ + mrList = service.getMissionResultsForTask(mission.getMissionId(), mission.getMissionType().intValue()); + } + + if(mrList == null || mrList.size() ==0){ //当前DC无需执行执行 + continue; + } + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+((mrList == null || mrList.size() ==0)?"当前DC不执行":"当前DC执行下发")); + + /* 校验并下载附件 */ + List missionFileList = null ; + if(mission.getMissionType().intValue() == 1 + ||mission.getMissionType().intValue() == 6){ + + missionFileList = service.getMissionFileInformations(mission.getMissionId()); + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+("当前DC有附件 数量:"+missionFileList.size())); + }else{ + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+("当前DC无附件 ")); + Common.assignMssion(mission); + continue; + } + + String hostIp = service.getHostIpBySystemId(mission.getSystemId()); + Integer port = Constants.WEB_SOCKET_PORT; + if(missionFileList != null && missionFileList.size()>0){ +// ServerTable serverTable = Common.getServerTable(); +// if(hostIp.equals(serverTable.getServerIp())){ // 同IP机器,尝试本地复制 +// try { +// for(FileComment fileComment: missionFileList){ +// fileComment. +// FileUtils.copyFile(srcFile, destFile); +// } +// } catch (Exception e) { +// logger.equals("文件复制失败"); +// } +// } + + //断点续传下载fileList 的线程结果 + Future bpf = Common.getBpDownMissionFilesMap().get(mission.getMissionId()); + + //-- MD5校验在NMSWebBPDownload中 + //-- 未创建下载 + if(bpf == null){ + bpf = createDownload(mission.getMissionId(),hostIp,port,missionFileList); + if(bpf == null){ + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+(" 附件下载失败 3分后再执行")); + Common.scheduled.schedule(new LoadNewMissionThread(mission.getMissionId()),3,TimeUnit.MINUTES); + continue ml; + } + } + + //-- 下载线程未终止 继续下载中 无操作 + if(!bpf.isDone()){ + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+(" 附件下载未完成 3分后再执行")); + Common.scheduled.schedule(new LoadNewMissionThread(mission.getMissionId()),3,TimeUnit.MINUTES); + continue ml; + } + + //-- dff.getBpDownFuture().isDone() == true 已存在下载结果 + int result = (Integer)bpf.get(1,TimeUnit.SECONDS); + + //-- MD5校验在NMSWebBPDownload中 + //-- 下载失败 则重试 0 成功 -1 -2 重试 -3 失败 + if(result == -1 || result == -2){ + bpf = createDownload(mission.getMissionId(),hostIp,port,missionFileList); + if(bpf == null || !bpf.isDone()){ + Common.scheduled.schedule(new LoadNewMissionThread(mission.getMissionId()),3,TimeUnit.MINUTES); + continue ml; + } + } + + //-- 下载成功 + boolean missionDFFlag = true; //任务下所有文件 下载完成标识 false未完成 true已完成 + String errorFileName = null; + mfl:for(FileComment fileInfo : missionFileList){ + + File file = new File(Constants.MISSION_FILE_DOWNLOAD_DIR+fileInfo.getFileName()); + if(!file.exists()){ // 文件不存在,下载失败 + missionDFFlag =false; + errorFileName = fileInfo.getFileName(); + break mfl; + } + } + + if(!missionDFFlag){ +// List mr2List = new ArrayList(); + //nodeList + for( MissionResult mr : mrList){ + MissionResult2 result2 = new MissionResult2(); + //- 任务Id long + result2.setMissionId(mr.getMissionId()); + //-- 任务类型 long + result2.setMissionType(mission.getMissionType()); + //- UUID long + result2.setUuid(mr.getUuid()); + //- 周期标识 long + result2.setLoopFlag(mission.getLoopFlag()); + //- 开始执行时间 long + result2.setStartTime(null); + //- 结束时间 long + result2.setEndTime(null); + //- 配置截取文本 string + result2.setText(null); + //- 结果状态 long + result2.setResult(1l); + //- 结果描述 string + result2.setDescription(errorFileName+" "+MissionConstants.ERROR_RESULT_FILE_DOWNLOAD_FAIL); +// mr2List.add(result2); + Common.addResultToResultList(result2);//DC下载文件失败 + } +// service.updateMissionResult2ByBatch(mission.getMissionType().intValue(),mr2List); + } + + //清理缓存 和废弃文件 + if(!missionDFFlag){ + for(FileComment fileInfo : missionFileList){ + File file = new File(Constants.MISSION_FILE_DOWNLOAD_DIR+fileInfo.getFileName()); + if(file.exists()){ + file.delete(); + logger.debug(file.getAbsolutePath()+"已删除!"); + }else { + logger.debug(file.getAbsolutePath()+"不存在"); + } + } + }else{ + //--该任务 已无文件可下载, 执行文件下发操作 + Common.assignMssion(mission); + continue; + } + }else{ + //--该任务 已无文件可下载, 执行文件下发操作 + Common.assignMssion(mission); + continue; + } + } + } + }catch (InterruptedException e) { + logger.warn("Thread interrupt operation, execute exit",e); + } + catch (Exception e) { + logger.error("loadNewMission error ",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + + } + + /** + * 创建下载线程 + * @time Mar 20, 2012-4:12:42 PM + * @param missionFileList + * @return + */ + private Future createDownload(Long missionId ,String ip,Integer port,List missionFileList){ + try { + Future bpf = Common.service.submit(new NMSWebBPDownload(ip,port,missionFileList,Constants.MISSION_FILE_DOWNLOAD_DIR,NMSWebBPDownload.TYPE_MISSION)); + bpf.get(Constants.MISSION_FILE_DOWNLOAD_DELAY, TimeUnit.SECONDS); + if(!bpf.isDone()){ + Common.getBpDownMissionFilesMap().put(missionId, bpf); + } + return bpf; + } catch (Exception e) { + logger.error("Failure to create a download thread ",e); + return null; + } + } + + + public static void main(String [] args) { + Common.service.submit(new InitRunningMissionThread(null)); + } +} diff --git a/src/com/nms/server/thread/mission/LoadNewMissionThread.java b/src/com/nms/server/thread/mission/LoadNewMissionThread.java new file mode 100644 index 0000000..205b5ad --- /dev/null +++ b/src/com/nms/server/thread/mission/LoadNewMissionThread.java @@ -0,0 +1,660 @@ +package com.nms.server.thread.mission; + +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.LoopMissionRoundInfo; +import com.nms.server.bean.MissionResult; +import com.nms.server.bean.MissionResult2; +import com.nms.server.bean.MissionStateTable; +import com.nms.server.bean.Task1; +import com.nms.server.bean.Task4; +import com.nms.server.bean.Task6; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.common.MissionConstants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.EmailService; +import com.nms.server.service.UpgradeService; +import com.nms.server.thread.socket.NMSWebBPDownload; +import com.socket.utils.FileComment; + +/** + * 任务加载管理线程 + * 每两分钟执行一次,或接收激活 + * 可加载 指定任务 或 者所有符合条件的任务 + * @date Nov 9, 2011 3:28:45 PM + * @author ZhangGang + * + */ +public class LoadNewMissionThread implements Runnable{ + /* 线程基本属性 */ + private Logger logger = Logger.getLogger(LoadNewMissionThread.class); + volatile boolean stop = false; //线程中断标示 + + /* 业务操作属性 */ + private CommonDao dao = null; //数据库链接 + private UpgradeService service = null; // + private EmailService emailService = null; // + private Long missionId = null; + + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + + public LoadNewMissionThread(Long missionId){ + this.missionId = missionId; + } + + @Override + public void run(){ + if(missionId!=null){ +// Thread.currentThread().setName("新任务载入线程 Id:>"+missionId); + Thread.currentThread().setName("The New Task Is Loaded With Thread Id:>"+missionId); + }else{ +// Thread.currentThread().setName("新任务载入线程"); + Thread.currentThread().setName("The New Task Load Thead"); + } + + try { + /* 任务加载错开并发时间 等待 0-20秒 */ + int waitTime = (int)(Math.random()*20000); + Thread.sleep(waitTime); + logger.debug("任务加载时间(ms):"+waitTime); + + dao = new CommonDao(); + service = new UpgradeService(dao); + emailService = new EmailService(dao); + + List missionList = service.getNewMessionList(missionId);//mission_state_table的状态为1或者5,且未过时(针对周期任务)的任务 + logger.debug("Mission_Run 新任务数量:>"+missionList.size()); + + //遍历任务集合 + if(missionList != null && missionList.size()>0){ + + ml:for(int i=0;i"+mission.getMissionId()); + if(mission.getMissionState()==null){ //后续可增加任务告警 + continue; + } + + //任务状态为3、4、7 无需执行 +// if(mission.getMissionState().intValue() == 3 +// ||mission.getMissionState().intValue() == 4 +// ||mission.getMissionState().intValue() == 7 ){ +// logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+" 3、4、7状态 执行结束"); +// continue; +// }else + + //任务状态为1 或 5 :任务参数校验和初始化任务 + if(mission.getMissionState().intValue() == 1 || mission.getMissionState().intValue() == 5){ + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+" 执行任务初始化"); + + //初始化任务信息 + if(!checkAndInitMission(mission)){ //校验并初始化任务---第一步---会改变任务的状态 + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+"初始化失败 执行结束"); + continue; + } + + } + + /* 判断当前DC是否需要执行 --第二步*/ + List mrList = null; + + if(mission.getLoopFlag()!=null && mission.getLoopFlag().longValue()==1l){ //周期任务 + if(mission.getMissionState() == 2l){ + mrList = service.getMissionResultsForLoopTask(mission.getMissionId(), mission.getMissionType().intValue(),3); + }else if(mission.getMissionState() ==6l){ + mrList = service.getMissionResultsForLoopTask(mission.getMissionId(), mission.getMissionType().intValue(),5); + } + }else{ //非周期任务 + mrList = service.getMissionResultsForTask(mission.getMissionId(), mission.getMissionType().intValue()); + } + + if(mrList == null || mrList.size() ==0){ //当前DC无需执行----------->>>?? + continue; + } + + + + + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+((mrList == null || mrList.size() ==0)?"当前DC不执行":"当前DC执行下发")); + + //校验并下载附件--第三步 + List missionFileList = null ; + if(mission.getMissionType().intValue() == 1 + ||mission.getMissionType().intValue() == 6){ + + missionFileList = service.getMissionFileInformations(mission.getMissionId()); + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+("当前DC有附件 数量:"+missionFileList.size())); + }else{ + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+("当前DC无附件 ")); + Common.assignMssion(mission); + continue; + } + + String hostIp = service.getHostIpBySystemId(mission.getSystemId()); + Integer port = Constants.WEB_SOCKET_PORT; + if(missionFileList != null && missionFileList.size()>0){ +// ServerTable serverTable = Common.getServerTable(); +// if(hostIp.equals(serverTable.getServerIp())){ // 同IP机器,尝试本地复制 +// try { +// for(FileComment fileComment: missionFileList){ +// fileComment. +// FileUtils.copyFile(srcFile, destFile); +// } +// } catch (Exception e) { +// logger.equals("文件复制失败"); +// } +// } + + //断点续传下载fileList 的线程结果 + Future bpf = Common.getBpDownMissionFilesMap().get(mission.getMissionId()); + + //-- MD5校验在NMSWebBPDownload中 + //-- 未创建下载 + if(bpf == null){ + bpf = createDownload(mission.getMissionId(),hostIp,port,missionFileList); + if(bpf == null){ + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+(" 附件下载失败 3分后再执行")); + Common.scheduled.schedule(new LoadNewMissionThread(missionId),3,TimeUnit.MINUTES); + continue ml; + } + } + + //-- 下载线程未终止 继续下载中 无操作 + if(!bpf.isDone()){ + logger.debug("Mission_Run 任务:Id:>"+mission.getMissionId()+(" 附件下载未完成 3分后再执行")); + Common.scheduled.schedule(new LoadNewMissionThread(missionId),3,TimeUnit.MINUTES); + continue ml; + } + + //-- dff.getBpDownFuture().isDone() == true 已存在下载结果 + int result = (Integer)bpf.get(1,TimeUnit.SECONDS); + + //-- MD5校验在NMSWebBPDownload中 + //-- 下载失败 则重试 0 成功 -1 -2 重试 -3 失败 + if(result == -1 || result == -2){ + bpf = createDownload(mission.getMissionId(),hostIp,port,missionFileList); + if(bpf == null || !bpf.isDone()){ + Common.scheduled.schedule(new LoadNewMissionThread(missionId),3,TimeUnit.MINUTES); + continue ml; + } + } + + //-- 下载成功 + boolean missionDFFlag = true; //任务下所有文件 下载完成标识 false未完成 true已完成 + String errorFileName = null; + mfl:for(FileComment fileInfo : missionFileList){ + + File file = new File(Constants.MISSION_FILE_DOWNLOAD_DIR+fileInfo.getFileName()); + logger.debug("DC下载文件的路径:"+Constants.MISSION_FILE_DOWNLOAD_DIR+fileInfo.getFileName()); + if(!file.exists()){ // 文件不存在,下载失败 + missionDFFlag =false; + errorFileName = fileInfo.getFileName(); + break mfl; + } + } + + if(!missionDFFlag){//下载失败,所以将结果表示置为失败1 +// List mr2List = new ArrayList(); + //nodeList + for( MissionResult mr : mrList){ + MissionResult2 result2 = new MissionResult2(); + //- 任务Id long + result2.setMissionId(mr.getMissionId()); + //-- 任务类型 long + result2.setMissionType(mission.getMissionType()); + //- UUID long + result2.setUuid(mr.getUuid()); + //- 周期标识 long + result2.setLoopFlag(mission.getLoopFlag()); + //- 开始执行时间 long + result2.setStartTime(null); + //- 结束时间 long + result2.setEndTime(null); + //- 配置截取文本 string + result2.setText(null); + //- 结果状态 long + result2.setResult(1l); + //- 结果描述 string + result2.setDescription(errorFileName+" "+MissionConstants.ERROR_RESULT_FILE_DOWNLOAD_FAIL); +// mr2List.add(result2); + + Common.addResultToResultList(result2);//DC下载文件失败-2013-3-13 hyx 为了更新任务的状态及描述 + } +// service.updateMissionResult2ByBatch(mission.getMissionType().intValue(),mr2List); + } + + //清理缓存 和废弃文件 + if(!missionDFFlag){ + for(FileComment fileInfo : missionFileList){ + File file = new File(Constants.MISSION_FILE_DOWNLOAD_DIR+fileInfo.getFileName()); + if(file.exists()){ + file.delete(); + logger.debug(file.getAbsolutePath()+"已删除!"); + }else { + logger.debug(file.getAbsolutePath()+"不存在"); + } + } + }else{ + //--该任务 已无文件可下载, 执行文件下发操作 + Common.assignMssion(mission); + continue; + } + }else{ + //--该任务 已无文件可下载, 执行文件下发操作 + Common.assignMssion(mission); + continue; + } + + /* 线程中断判断判断 */ + if(Thread.currentThread().isInterrupted()){ + stop = true; + } + + } + + //初始化任务结束后,需要统计更新任务的状态-2013-3-13 hyx--增加的原因:初始化的时候可能会插入结果(1:失败),但是又未统计mission_state_table表中的state,此处进行统计 + service.updateCommonMissionComplete();//非周期任务:任务状态:成功(31)、部分成功(32)、全部失败(30) + service.updateLoopMissionComplete();//周期任务 + + } + }catch (InterruptedException e) { + logger.warn("Thread interrupt operation, execute exit",e); + }catch (Exception e) { + logger.error("loadNewMission error ",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + + } + + /** + * 校验并初始化参数 + * @time Nov 15, 2011-6:21:52 PM + * @param missionId + */ + public boolean checkAndInitMission(MissionStateTable mission) { + + if(mission == null){ + return false; + } + + boolean taskFlag = true; + + /* 根据任务类型校验参数 */ + switch (mission.getMissionType().intValue()) { + case 1: + taskFlag = checkAndInitTask1(mission); + break; + case 4: + if(mission.getLoopFlag()!=null && mission.getLoopFlag().longValue()==1l){ //周期任务 + taskFlag = checkAndInitLoopTask4(mission); +// taskFlag = false; + }else{ //非周期任务 + taskFlag = checkAndInitTask4(mission); + } + break; + case 6: + taskFlag = checkAndInitTask6(mission); + break; + + default: + taskFlag = false; + break; + } + + //更新任务状态描述-2013-3-13 hyx--增加的原因:初始化的时候可能会插入结果(1:失败),但是又未添加描述,此处添加任务状态描述 + String missType = mission.getMissionType()==null?"":mission.getMissionType().toString(); + String isLoop = mission.getLoopFlag()==null?null:mission.getLoopFlag().toString(); + String missId = mission.getMissionId()==null?"":mission.getMissionId().toString(); + if ("4".equals(missType)&&"1".equals(isLoop)){//周期任务 + service.updateLoopMissionStateDesc(missId);//周期任务:type=4 + }else { + service.updateCommonMissionStateDesc(Integer.parseInt(missType),missId);//非周期任务 + } + + return taskFlag; + } + + /** + * 校验任务类型1(文件推送)可执行和初始化任务信息 + * 1、校验参数完整 + * 2、校验存在附件 + * 3、初始化任务信息(校验任务下发节点有效、非服务器和管理范围外Node 结果判定失败) + * 4、判断当前DC是否可执行该任务 + * + * @time Feb 23, 2013-6:06:24 PM + * @param mission + * @return + */ + @SuppressWarnings("finally") + public boolean checkAndInitTask1(MissionStateTable mission){ + boolean taskFlag = true; + StringBuffer errorInfo = new StringBuffer(); + //查询参数信息和附件信息 + Task1 task1 = service.getTask1ById(mission.getMissionId()); //查询任务具体信息 + List fileCommentsList = null ; + + /* 任务可执行校验 */ + //第一步:任务参数完整 + if(task1 == null || StringUtils.isEmpty(task1.getTaskParam())){ + errorInfo.append(MissionConstants.ERROR_TASK_PARAMS); + taskFlag = false; + } + + //第二步:附件信息校验 + if(taskFlag){ + fileCommentsList = service.getMissionFileInformations(mission.getMissionId()); + if((fileCommentsList == null || fileCommentsList.size()==0)){ + errorInfo.append(MissionConstants.ERROR_TASK_NO_FILE); + taskFlag= false; + } + } + + try { + + //第三步:初始化任务 + if(taskFlag){ + logger.debug("missionId:> "+mission.getMissionId()+" 初始化信息"); + //先走1全部初始化(不作节点类型和节点管理范围外判断) 后续确认优化信息 + service.initMissionResults2(mission.getMissionId(),task1.getNodeGroupsId(),task1.getNodeIpsId(),mission.getMissionType().intValue()); +// emailService.sendEmailForMission(mission.getMissionId(),"任务下发通知","任务下发开始"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionLssue_n81i","i18n_server.LoadNewMissionThread.missionLssueStart_n81i"); + mission.setMissionState(2l);//为了使内存中的数据和数据库中的数据一致,因为service.initMissionResults2()方法修改了missionState状态为2? + }else{//任务失败 + logger.error("missionId:> "+mission.getMissionId()+" init info>"+errorInfo); + service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" "+errorInfo);//hyx 修改:第二个参数传递错误 +// emailService.sendEmailForMission(mission.getMissionId(),"任务初始化失败",errorInfo+""); +// emailService.sendEmailForMission(mission.getMissionId(),"Task initialization failed",errorInfo+""); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionInitFail_n81i",errorInfo+""); + } + + + return taskFlag; + } catch (Exception e) { + //添加告警 + logger.error("Initialization failed",e); + taskFlag = false; + }finally{ + return taskFlag; + } + } + + @SuppressWarnings("finally") + public boolean checkAndInitTask4(MissionStateTable mission){ + boolean taskFlag = true; + StringBuffer errorInfo = new StringBuffer(); + //查询参数信息和附件信息 + Task4 task4 = service.getTask4ById(mission.getMissionId()); //查询任务具体信息 + + /* 任务可执行校验 */ + //第一步:任务参数完整 + if(task4 == null || StringUtils.isEmpty(task4.getCommandName())|| task4.getCommandType() == 0 ){ +// errorInfo.append("命令执行任务参数为空,任务无法执行"); +// errorInfo.append("The command execution task parameter is empty, and the task cannot be executed"); + errorInfo.append("i18n_server.LoadNewMissionThread.errorInfo1_n81i"); + taskFlag = false ; + } + + //第二步:初始化任务 + try { + + if(taskFlag){ + logger.debug("missionId:> "+mission.getMissionId()+" 初始化信息"); + //先走1全部初始化(不作节点类型和节点管理范围外判断) 后续确认优化信息 +// service.initMissionResults(mission.getMissionId(),task1.getNodeGroupsId(),task1.getNodeIpsId(),mission.getMissionType().intValue()); + service.initMissionResults2(mission.getMissionId(),task4.getNodeGroupsId(),task4.getNodeIpsId(),mission.getMissionType().intValue()); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionLssue_n81i","i18n_server.LoadNewMissionThread.missionLssueStart_n81i"); +// mission.setMissionState(2l); + mission.setMissionState(2l); + }else{//任务失败 + logger.error("missionId:> "+mission.getMissionId()+" init info>"+errorInfo); + service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" "+errorInfo);//hyx 修改:第二个参数传递错误 + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionExecFail_n81i",errorInfo+""); + } + + return taskFlag; + } catch (Exception e) { + //添加告警 + logger.error("Initialization failed",e); + taskFlag = false; + }finally{ + return taskFlag; + } + } + + @SuppressWarnings("finally") + public boolean checkAndInitLoopTask4(MissionStateTable mission){ + boolean taskFlag = true; + StringBuffer errorInfo = new StringBuffer(); + //查询参数信息和附件信息 + Task4 task4 = service.getTask4ById(mission.getMissionId()); //查询任务具体信息 + + /* 任务可执行校验 */ + //第一步:任务参数完整 + if(task4 == null || StringUtils.isEmpty(task4.getCommandName())|| task4.getCommandType() == 0 ){ +// errorInfo.append("命令执行任务参数为空,任务无法执行"); +// errorInfo.append("The command execution task parameter is empty, and the task cannot be executed"); + errorInfo.append("i18n_server.LoadNewMissionThread.errorInfo1_n81i"); + taskFlag = false ; + } + + //- 任务时间的有效性校验 + if(mission.getEndTime()==null || mission.getEndTime().getTime() < System.currentTimeMillis() ){ +// errorInfo.append("已超过任务结束时间 任务未能执行"); +// errorInfo.append("The task has exceeded the end time ,and the task execution has ended"); + errorInfo.append("i18n_server.LoadNewMissionThread.errorInfo2_n81i"); + taskFlag = false ; + } + + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + try { + + //初始化任务 任务状态 1 或 5 + logger.debug(mission.getMissionId()+" 周期任务状态"+" "+mission.getMissionState()); + mission.setNodeGroupsId(task4.getNodeGroupsId()); //保存节点组信息 + mission.setNodeIpsId(task4.getNodeIpsId()); //保存节点信息 + + //-- 整理并初始化周期任务信息 + service.checkAndInitMissionCycle(mission);// 轮次校验和初始化 + + //-- 获取当前周期轮次 + LoopMissionRoundInfo roundInfo = null; + List roundInfoList = Common.getLoopMissionRoundInfoList().get(mission.getMissionId()); + if(roundInfoList !=null && roundInfoList.size()>0){ + //-- 获取当前周期轮次 + for (Iterator roundIte = roundInfoList.iterator(); roundIte.hasNext();) { + roundInfo = roundIte.next(); + if(roundInfo.getEndTime().getTime() > System.currentTimeMillis()){ + break; + } + } + } + + if(roundInfo == null){ +// errorInfo.append("周期任务轮次初始化异常,任务无法执行"); +// errorInfo.append("Periodic task initialization is abnormal and cannot be performed"); + errorInfo.append("i18n_server.LoadNewMissionThread.errorInfo3_n81i"); + taskFlag = false ; + } + + if(taskFlag && mission.getMissionState()!= null && mission.getMissionState().intValue()==1){ //创建初始化 + logger.info("该周期任务 按正常业务执行"); + + //-- 预置状态的轮次,初始化当前轮次的结果数据 + if(roundInfo!=null && roundInfo.getMissionState()!=null && roundInfo.getMissionState().longValue()==0l || roundInfo.getMissionState().longValue()==1l){ //0 预置状态,未初始化当前轮次的任务结果信息 + logger.info("周期任务 轮次信息初始化"); + service.initLoopMissionResults2(roundInfo.getCurMissionId(), task4.getNodeGroupsId(),task4.getNodeIpsId(),3);//初始化可下发的结果信息 3 未下发 + roundInfo.setMissionState(2l); //更新缓存中的任务轮次信息状态 + } + + //- 变更任务状态 1(以创建) -> 2(正在执行) +// service.updateMissionState(mission.getMissionId(), 2,format.format(System.currentTimeMillis())+" 任务执行开始"); + service.updateMissionState(mission.getMissionId(), 2,format.format(System.currentTimeMillis())+" i18n_server.LoadNewMissionThread.missionStart_n81i"); + mission.setMissionState(2l); +// emailService.sendEmailForMission(mission.getMissionId(),"任务下发通知","任务开始执行"); +// emailService.sendEmailForMission(mission.getMissionId(),"Notification of send task","Start the task"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionLssue_n81i","i18n_server.LoadNewMissionThread.missionStart_n81i"); + + }else if(taskFlag && mission.getMissionState()!= null && mission.getMissionState().intValue()==5){ //撤销初始化 + + //撤销任务下发操作 (获取当前操作周期或最后一个非初始化周期的轮次信息) + logger.debug("该周期任务 按撤销业务执行"); + + + //-- 旧轮次信息状态记录 + int oldRoundState = roundInfo.getMissionState()==null?0:roundInfo.getMissionState().intValue(); + + //-- 将当前周期状态 变更为撤销准备周期 + service.updateLoopMissionState(roundInfo.getCurMissionId(),5); + + //-- 删除预置周期 + service.deleteLoopMissionPreset(mission.getMissionId(),roundInfo.getCurMissionId()); + + //-- 更新缓存轮次信息(上面已更新完成) +// service.updateLoopMissionRoundInfo(Common.getLoopMissionRoundInfoList(), mission.getMissionId()); + + //-- 将撤销周期的结果数据进行状态初始化 + if(oldRoundState == 0){ //预置周期无结果 + + service.initLoopMissionResults2(roundInfo.getCurMissionId(), task4.getNodeGroupsId(),task4.getNodeIpsId(),5);//初始化可下发的结果信息 5 撤销未下发 + }else if(oldRoundState>0){ //存在结果的周期 及2或3 + + service.updateMissionResultsResult(roundInfo.getCurMissionId(), 4, 5); + service.updateLoopMissionState(roundInfo.getCurMissionId(), 6); //更新 任务轮次信息状态 + } + + roundInfo.setMissionState(6l); //更新缓存中的任务轮次信息状态 + + //- 变更任务状态 5(准备撤销) -> 6(撤销执行) + service.updateMissionState(mission.getMissionId(), 6,format.format(System.currentTimeMillis())+" Task revocation start execute"); + mission.setMissionState(6l); +// emailService.sendEmailForMission(mission.getMissionId(),"任务撤销通知","任务撤销开始执行"); +// emailService.sendEmailForMission(mission.getMissionId(),"Task cancellation notice","Task revocation start execute"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionRevoke_n81i","i18n_server.LoadNewMissionThread.missionRevokeStart_n81i"); + } + + //第三步:初始化任务 + if(!taskFlag){ +// service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" 任务执行失败"); + service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" Task execution failure"); +// emailService.sendEmailForMission(mission.getMissionId(),"任务执行失败",errorInfo+""); +// emailService.sendEmailForMission(mission.getMissionId(),"Task execution failure",errorInfo+""); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionExecFail_n81i",errorInfo+""); + } + + return taskFlag; + } catch (Exception e) { + logger.error("Initialization failed",e); + taskFlag = true; + }finally{ + return taskFlag; + } + } + + /** + * 校验任务类型1(文件推送)可执行和初始化任务信息 + * 1、校验参数完整 + * 2、校验存在附件 + * 3、初始化任务信息(校验任务下发节点有效、非服务器和管理范围外Node 结果判定失败) + * 4、判断当前DC是否可执行该任务 + * + * @time Feb 23, 2013-6:06:24 PM + * @param mission + * @return + */ + @SuppressWarnings("finally") + public boolean checkAndInitTask6(MissionStateTable mission){ + boolean taskFlag = true; + StringBuffer errorInfo = new StringBuffer(); + //查询参数信息和附件信息 + Task6 task6 = service.getTask6ById(mission.getMissionId()); //查询任务具体信息 + List fileCommentsList = null ; + + /* 任务可执行校验 */ + //第一步:任务参数完整 + if(task6 == null || StringUtils.isEmpty(task6.getCommandName())|| task6.getCommandType() == 0 ){ +// errorInfo.append("升级部署任务参数为空,任务无法执行"); +// errorInfo.append("The upgrade deployment task parameter is empty, and the task cannot be executed"); + errorInfo.append("i18n_server.LoadNewMissionThread.errorInfo4_n81i"); + taskFlag = false; + } + + //第二步:附件信息校验 + if(taskFlag){ + fileCommentsList = service.getMissionFileInformations(mission.getMissionId()); + /** 逆向任务 无附件,其判断标示为OldTaskId 不为空 **/ + if(task6.getVersion()==null && task6.getOldTaskId()==null && (fileCommentsList == null || fileCommentsList.size()==0)){ + errorInfo.append(MissionConstants.ERROR_TASK_NO_FILE); + taskFlag= false; + } + } + + try { + + //第三步:初始化任务 + if(taskFlag){ + logger.debug("missionId:> "+mission.getMissionId()+" 初始化信息"); + //先走1全部初始化(不作节点类型和节点管理范围外判断) 后续确认优化信息 + service.initMissionResults2(mission.getMissionId(),task6.getNodeGroupsId(),task6.getNodeIpsId(),mission.getMissionType().intValue()); +// emailService.sendEmailForMission(mission.getMissionId(),"任务下发通知","任务下发开始"); +// emailService.sendEmailForMission(mission.getMissionId(),"Notification of send task","Start task"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionLssue_n81i","i18n_server.LoadNewMissionThread.missionLssueStart_n81i"); + mission.setMissionState(2l); + }else{//任务失败 + logger.error("missionId:> "+mission.getMissionId()+" init info>"+errorInfo); + service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" "+errorInfo);//hyx 修改:第二个参数传递错误 +// emailService.sendEmailForMission(mission.getMissionId(),"任务执行失败",errorInfo+""); +// emailService.sendEmailForMission(mission.getMissionId(),"Task execution failure",errorInfo+""); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.LoadNewMissionThread.missionExecFail_n81i",errorInfo+""); + } + + return taskFlag; + } catch (Exception e) { + //添加告警 + logger.error("Initialization failed",e); + taskFlag = false; + }finally{ + return taskFlag; + } + } + + /** + * 创建下载线程 + * @time Mar 20, 2012-4:12:42 PM + * @param missionFileList + * @return + */ + private Future createDownload(Long missionId ,String ip,Integer port,List missionFileList){ + try { + Future bpf = Common.service.submit(new NMSWebBPDownload(ip,port,missionFileList,Constants.MISSION_FILE_DOWNLOAD_DIR,NMSWebBPDownload.TYPE_MISSION)); + bpf.get(Constants.MISSION_FILE_DOWNLOAD_DELAY, TimeUnit.SECONDS); + if(!bpf.isDone()){ + Common.getBpDownMissionFilesMap().put(missionId, bpf); + } + return bpf; + } catch (Exception e) { + logger.error("Failure to create a download thread ",e); + return null; + } + } + + public static void main(String [] args) { +// Common.service.submit(new LoadNewMissionThread(1343l)); + Common.service.submit(new LoadNewMissionThread(4125l)); + } +} diff --git a/src/com/nms/server/thread/mission/MissionPollingThread.java b/src/com/nms/server/thread/mission/MissionPollingThread.java new file mode 100644 index 0000000..75e17a7 --- /dev/null +++ b/src/com/nms/server/thread/mission/MissionPollingThread.java @@ -0,0 +1,456 @@ +package com.nms.server.thread.mission; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.LoopMissionRoundInfo; +import com.nms.server.bean.MissionResult; +import com.nms.server.bean.MissionStateTable; +import com.nms.server.bean.Task1; +import com.nms.server.bean.Task4; +import com.nms.server.bean.Task6; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.EmailService; +import com.nms.server.service.UpgradeService; +import com.nms.server.util.ExceptionPrintUtils; +import com.socket.utils.FileComment; + +public class MissionPollingThread implements Runnable{ + private final Logger logger = Logger.getLogger(MissionPollingThread.class); + private List missionList = null; + private List missionList2 = new ArrayList(); + private String threadName; + private Integer testNum = 1; + + /** + * 线程单例 + * @time Nov 15, 2011-7:15:34 PM + * @return + */ + public MissionPollingThread (String threadName,final Integer testNum,final List missionList) { + this.missionList = missionList; + this.testNum = testNum; + this.threadName = threadName; + } + public void run() { + Thread.currentThread().setName(threadName); + + //-- 结束无意义操作 + if(testNum==null || missionList == null || missionList.size()==0){ + logger.warn("The task queue is empty and does not need to be executed"); + return; + } + + //-- 循环下发任务 + try { + while(missionList.size()>0){ + MissionStateTable mission = missionList.get(0); + Thread.currentThread().setName(threadName+" Id:>"+mission.getMissionId()); + switch (mission.getMissionType().intValue()) { + case 1: + forTask1(mission); + break; + + case 4: + if(mission.getLoopFlag()!=null && mission.getLoopFlag().longValue()==1l){ //周期任务 + forLoopMission(mission); + }else{ //非周期任务 + forTask4(mission); + } + break; + + case 6: + forTask6(mission); + break; + + default: + break; + } + missionList2.add(mission); + missionList.remove(0); + } + + //-- 创建下一次任务轮询 + if(testNum.intValue() < Constants.MISSION_RELEASE_TIMES.intValue()){ + logger.info("开始第“"+(testNum+1)+"”次任务轮询"); + Common.scheduled.schedule(new MissionPollingThread("第“"+(testNum+1)+"”次轮询线程",(testNum+1),missionList2), Constants.MISSION_RELEASE_PERIOD, TimeUnit.SECONDS); + missionList2 = new ArrayList(); + }else{ + missionList2.clear(); + } + }catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + + /** + * 文件推送任务执行 + * @time Nov 11, 2011-6:55:33 PM + * @param mission + */ + public void forTask1(MissionStateTable mission) { + CommonDao dao = null; //数据库链接 + try { + dao = new CommonDao(); + UpgradeService service = new UpgradeService(dao); //专属Service + + //-- 任务结果查询和推送文件 + Task1 task1 = service.getTask1ById(mission.getMissionId()); //查询任务具体信息 + List fileCommentsList = service.getMissionFileInformations(mission.getMissionId()); + ArrayList mrList = service.getMissionResultsForTask(mission.getMissionId(),mission.getMissionType().intValue()); + + //-- 遍历节点结果集合 + for (MissionResult result : mrList) { + + //-- 针对节点 进行文件推送 + Common.sendMissionInfo(mission,new NMSClientTask1(result.getNodeIp(),Constants.SSL_CLIENT_PORT,mission,task1,fileCommentsList),result.getUuid(), testNum,null,null); + } + } catch (Exception e) { + logger.error("Task Id:>"+mission.getMissionId()+" task execution failure", e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + +//public static void main(String [] args) { +// List missionList = new LinkedList(); +// CommonDao dao = new CommonDao(); //数据库链接 +// UpgradeService service = new UpgradeService(dao); //专属Service +// missionList.add(service.getMessionById(501l)); +// MissionPollingThread thread = new MissionPollingThread ("1",1,missionList); +// thread.run(); +//} + /** + * 命令执行任务执行 + * @time Nov 11, 2011-6:55:33 PM + * @param mission + */ + public void forTask4(MissionStateTable mission) { + CommonDao dao = null; //数据库链接 + try { + dao = new CommonDao(); + UpgradeService service = new UpgradeService(dao); //专属Service + + //-- 任务结果查询和推送文件 + Task4 task4 = service.getTask4ById(mission.getMissionId()); //查询任务具体信息 + ArrayList mrList = service.getMissionResultsForTask(mission.getMissionId(),mission.getMissionType().intValue()); + + //-- 遍历节点结果集合 + for (MissionResult result : mrList) { + //-- 针对节点 进行文件推送 + Common.sendMissionInfo(mission,new NMSClientTask4(result.getNodeIp(),Constants.SSL_CLIENT_PORT,mission,task4,result),result.getUuid(), testNum,null,null); + } + } catch (Exception e) { + logger.error("Task Id:>"+mission.getMissionId()+" task execution failure", e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + /**周期任务代码*/ + /** + * 业务梳理: + * 周期任务操作 :执行操作状态为0、1 ,撤消操作状态为5,(完成状态时3 此项无需考虑) + * 执行状态时: + * 检查并初始化周期任务的轮次(有效轮次 即 轮次起始时间 大于 当前系统时间 的轮次信息条数 <=10) + * 对 当前系统时间 所在的轮次的任务结果初始化 + * 对 当前系统时间 所在的轮次的任务结果指定的结果节点进行下发 + * 撤销状态时: + * 对 当前系统时间 所在的轮次的任务结果指定的节点,通知取消周期任务 + * */ + private void forLoopMission(MissionStateTable mission){ + /**方法执行 参数校验*/ + if(mission == null || mission.getMissionState() == null){ + return ; + } + + CommonDao dao = null; + SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + + try { + dao = new CommonDao(); + UpgradeService service = new UpgradeService(dao); + EmailService emailService = new EmailService(dao); + //- 任务参数的有效性校验 + Task4 task4 = service.getTask4ById(mission.getMissionId()); //查询任务具体信息 + if(task4 == null || StringUtils.isEmpty(task4.getCommandName())|| task4.getCommandType() == 0 ){ +// service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" 任务参数为空,任务无法执行"); +// emailService.sendEmailForMission(mission.getMissionId(),"任务执行失败"," 任务参数为空,任务无法执行"); +// service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" The task parameter is empty and the task cannot be executed"); + service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" i18n_server.MissionPollingThread.noParam_n81i"); +// emailService.sendEmailForMission(mission.getMissionId(),"Task execution failure","The task parameter is empty and the task cannot be executed"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.MissionPollingThread.missionFail_n81i","i18n_server.MissionPollingThread.noParam_n81i"); + logger.warn("The task parameter is empty and the task cannot be executed"); + /***告警功能****/ + return ; + } + + //- 任务时间的有效性校验 + if(mission.getEndTime()==null || mission.getEndTime().getTime() < System.currentTimeMillis() ){ + if(mission.getMissionState().longValue() ==2l){ +// service.updateMissionState(mission.getMissionId(), 3,format.format(System.currentTimeMillis())+" 已超过任务结束时间 任务已完成"); +// service.updateMissionState(mission.getMissionId(), 3,format.format(System.currentTimeMillis())+" The task has exceeded the end time ,and the task execution has ended"); + service.updateMissionState(mission.getMissionId(), 3,format.format(System.currentTimeMillis())+"i18n_server.MissionPollingThread.outEndTime_n81i"); +// emailService.sendEmailForMission(mission.getMissionId(),"任务执行失败"," 已超过任务结束时间 任务已完成"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.MissionPollingThread.missionFail_n81i","i18n_server.MissionPollingThread.outEndTime_n81i"); + logger.warn("The task has exceeded the end time ,and the task execution has ended"); + } + /***告警功能****/ + return ; + } + //- 任务下发 准备操作 + LoopMissionRoundInfo roundInfo = null; + ArrayList mrList = null; + + //- 任务状态判断 已创建(1) 执行中(2) 准备撤销(5) 撤销中(6) + logger.debug(mission.getMissionId()+" 周期任务状态"+" "+mission.getMissionState()); + + mission.setNodeGroupsId(task4.getNodeGroupsId()); //保存节点组信息 + mission.setNodeIpsId(task4.getNodeIpsId()); //保存节点信息 + + //-- 已创建操作(1) + if (mission.getMissionState().longValue() == 1l){ //新任务(已创建)的任务下发操作 + + service.checkAndInitMissionCycle(mission);// 轮次校验和初始化 + List roundInfoList = Common.getLoopMissionRoundInfoList().get(mission.getMissionId()); + + //-- 获取当前轮次信息并初始化当前轮次的任务结果信息 + if(roundInfoList !=null && roundInfoList.size()>0){ + + //-- 获取当前周期轮次 + for (Iterator roundIte = roundInfoList.iterator(); roundIte.hasNext();) { + roundInfo = roundIte.next(); + if(roundInfo.getEndTime().getTime() > System.currentTimeMillis()){ + break; + } + } + + //-- 预置状态的轮次,初始化当前轮次的结果数据 + if(roundInfo!=null && roundInfo.getMissionState()!=null && roundInfo.getMissionState().longValue()==0l){ //0 预置状态,未初始化当前轮次的任务结果信息 + + service.initLoopMissionResults2(roundInfo.getCurMissionId(), task4.getNodeGroupsId(),task4.getNodeIpsId(),3);//初始化可下发的结果信息 3 未下发 +// service.updateLoopMissionState(roundInfo.getCurMissionId(), 2); //更新 任务轮次信息状态 + roundInfo.setMissionState(2l); //更新缓存中的任务轮次信息状态 + mrList = service.getMissionResultsForTask(roundInfo.getCurMissionId(),mission.getMissionType().intValue()); + }else{ + + } + } + + //任务状态变更 +// if(mrList== null || mrList.size()==0){ //未初始化到当前轮次的结果信息 +// +// //- 变更任务状态 1(以创建) -> 4(未能执行) +// service.updateMissionState(mission.getMissionId(), 4,format.format(System.currentTimeMillis())+" 未初始化到可下发节点的结果信息,任务无法执行"); +// mission.setMissionState(4l); +// return; +// }else{ + +// } + + //- 变更任务状态 1(以创建) -> 2(正在执行) +// service.updateMissionState(mission.getMissionId(), 2,format.format(System.currentTimeMillis())+" 任务执行开始"); + service.updateMissionState(mission.getMissionId(), 2,format.format(System.currentTimeMillis())+" i18n_server.MissionPollingThread.missionExec_n81i"); +// emailService.sendEmailForMission(mission.getMissionId(),"任务执行通知"," 任务执行开始"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.MissionPollingThread.missionExecNotice_n81i"," i18n_server.MissionPollingThread.missionExec_n81i"); + mission.setMissionState(2l); + Common.getLoopMissionInfoMap().put(mission.getMissionId(), mission); + + } + + //-- 执行中操作(2) + else if (mission.getMissionState().longValue() == 2l){ //未完成任务下发操作 + + //-- 获取未下发的节点信息 + mrList = service.getMissionResultsForLoopTask(mission.getMissionId(),mission.getMissionType().intValue(),3); + if(mrList == null || mrList.size() == 0){ + return ; + } + + } + + //-- 准备撤销(5) + else if (mission.getMissionState().longValue() == 5l){ //撤销任务下发操作 (获取当前操作周期或最后一个非初始化周期的轮次信息) + logger.debug("周期任务 撤销操作"); + //-- 整理周期任务信息 + service.checkAndInitMissionCycle(mission);// 轮次校验和初始化 + List roundInfoList = Common.getLoopMissionRoundInfoList().get(mission.getMissionId()); + + //-- 获取当前轮次信息并初始化当前轮次的任务结果信息 + if(roundInfoList !=null && roundInfoList.size()>0){ + + //-- 获取当前周期轮次 + for (Iterator roundIte = roundInfoList.iterator(); roundIte.hasNext();) { + roundInfo = roundIte.next(); + if(roundInfo.getEndTime().getTime() > System.currentTimeMillis()){ + break; + } + } + //-- 旧轮次信息状态记录 + int oldRoundState = roundInfo.getMissionState()==null?0:roundInfo.getMissionState().intValue(); + + //-- 将当前周期状态 变更为撤销准备周期 + service.updateLoopMissionState(roundInfo.getCurMissionId(),5); + + //-- 删除预置周期 + service.deleteLoopMissionPreset(mission.getMissionId(),roundInfo.getCurMissionId()); + + //-- 更新缓存轮次信息 + service.updateLoopMissionRoundInfo(Common.getLoopMissionRoundInfoList(), mission.getMissionId()); + + //-- 将撤销周期的结果数据进行状态初始化 + if(oldRoundState == 0){ //预置周期无结果 + + service.initLoopMissionResults2(roundInfo.getCurMissionId(), task4.getNodeGroupsId(),task4.getNodeIpsId(),5);//初始化可下发的结果信息 5 撤销未下发 + + }else if(oldRoundState>0){ //存在结果的周期 及2或3 + + service.updateMissionResultsResult(roundInfo.getCurMissionId(), 4, 5); + + } + } + + + //-- 获取未下发的节点信息 + mrList = service.getMissionResultsForLoopTask(mission.getMissionId(),mission.getMissionType().intValue(),5); + if(mrList == null || mrList.size() == 0){ + + //-- 将周期状态变更为7 完成 + service.updateLoopMissionState(roundInfo.getCurMissionId(), 7); //完成 任务轮次信息状态 + roundInfo.setMissionState(7l); //更新缓存中的任务轮次信息状态 + return ; + }else{ + + //-- 将周期状态变更为6 开始执行 + service.updateLoopMissionState(roundInfo.getCurMissionId(), 6); //更新 任务轮次信息状态 + roundInfo.setMissionState(6l); //更新缓存中的任务轮次信息状态 + } + //- 变更任务状态 5(准备撤销) -> 6(撤销执行) +// service.updateMissionState(mission.getMissionId(), 6,format.format(System.currentTimeMillis())+" 任务撤销开始"); +// service.updateMissionState(mission.getMissionId(), 6,format.format(System.currentTimeMillis())+" Task revocation start"); + service.updateMissionState(mission.getMissionId(), 6,format.format(System.currentTimeMillis())+" i18n_server.MissionPollingThread.missionRevoke_n81i"); + mission.setMissionState(6l); +// emailService.sendEmailForMission(mission.getMissionId(),"任务执行通知"," 任务撤销开始"); +// emailService.sendEmailForMission(mission.getMissionId(),"Task execution notice"," Task revocation start"); + emailService.sendEmailForMission(mission.getMissionId(),"i18n_server.MissionPollingThread.missionExecNotice_n81i"," i18n_server.MissionPollingThread.missionRevoke_n81i"); + } + + //-- 撤销中操作(6) + else if (mission.getMissionState().longValue() == 6l){ //未完成任务下发操作 + + //-- 获取未下发的节点信息 + mrList = service.getMissionResultsForLoopTask(mission.getMissionId(),mission.getMissionType().intValue(),5); + if(mrList == null || mrList.size() == 0){ + service.updateLoopMissionComplete(); + return ; + } + }else{ + return; + } + + //-- 将任务信息存入Map + Common.getLoopMissionInfoMap().put(mission.getMissionId(), mission); + + + task4.setNodeGroupsId(null); //抹掉 节点组 信息 + LoopMissionRoundInfo roundInfo0 = null; + + if(mrList==null){ + return; + } + + for (MissionResult result : mrList) { + //-- 执行成功的结果 不再重复下发,跳过执行 + if(result.getResult() != null && result.getResult().longValue() == 0l){ + continue; + } + + roundInfo0 = service.getRoundInfoFromCommonListByID(mission.getMissionId(), result.getMissionId()); + + //从数据库中获取roundInfo + if(roundInfo0 == null){ + roundInfo0 = service.getRoundInfoFromDBByID(result.getMissionId()); + + //将roundInfo0保存到list中 + if(roundInfo0 != null){ + List list0 = Common.getLoopMissionRoundInfoList().get(mission.getMissionId()); + if(list0== null){ + list0 = new LinkedList(); + Common.getLoopMissionRoundInfoList().put(mission.getMissionId(),list0); + } + list0.add(0,roundInfo0); + }else{ + continue; + } + } + + //-- 针对节点 进行文件推送 + if(mission.getMissionState().longValue() == 6l){ + Common.sendMissionInfo(mission,new NMSClientTask4Cancel(result.getNodeIp(),Constants.SSL_CLIENT_PORT,mission.getMissionId()),result.getUuid(), testNum,roundInfo0.getStartTime().getTime(),roundInfo0.getEndTime().getTime()); + }else if(mission.getMissionState().longValue() == 2l){ + Common.sendMissionInfo(mission, new NMSClientTask4(result.getNodeIp(),Constants.SSL_CLIENT_PORT,mission,task4,result),result.getUuid(), testNum,roundInfo0.getStartTime().getTime(),roundInfo0.getEndTime().getTime()); + } + } + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + /** + * 升级任务任务执行 + * @time Nov 11, 2011-6:55:33 PM + * @param mission + */ + public void forTask6(MissionStateTable mission) { + CommonDao dao = null; //数据库链接 + try { + dao = new CommonDao(); + UpgradeService service = new UpgradeService(dao); //专属Service + + //-- 任务结果查询和推送文件 + Task6 task6 = service.getTask6ById(mission.getMissionId()); //查询任务具体信息 + List fileCommentsList = service.getMissionFileInformations(mission.getMissionId()); + task6.setNodeGroupsId(null); //抹掉 节点组 信息 + ArrayList mrList = service.getMissionResultsForTask(mission.getMissionId(),mission.getMissionType().intValue()); + + //-- 遍历节点结果集合 + if(task6.getVersion()!=null && task6.getVersion().longValue()!=0){ //逆向任务下发 + for (MissionResult result : mrList) { + Common.sendMissionInfo(mission, new NMSClientTask6Cover(result.getNodeIp(),Constants.SSL_CLIENT_PORT,mission,task6,result,fileCommentsList),result.getUuid(), testNum,null , null); + } + }else{ //升级部署任务 + for (MissionResult result : mrList) { + + Common.sendMissionInfo(mission, new NMSClientTask6(result.getNodeIp(),Constants.SSL_CLIENT_PORT,mission,task6,result,fileCommentsList),result.getUuid(), testNum,null , null); + } + } + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } +} diff --git a/src/com/nms/server/thread/mission/MissionResultManagerThread.java b/src/com/nms/server/thread/mission/MissionResultManagerThread.java new file mode 100644 index 0000000..3b12f7d --- /dev/null +++ b/src/com/nms/server/thread/mission/MissionResultManagerThread.java @@ -0,0 +1,208 @@ +package com.nms.server.thread.mission; + +import java.io.File; +import java.util.Calendar; +import java.util.LinkedList; +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult2; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.FileUtils; + +public class MissionResultManagerThread implements Runnable{ + private Logger logger = Logger.getLogger(MissionResultManagerThread.class); + private int testNum = 0;//尝试运行线程Constants.ALARM_DATA_RESOVE的次数 + + @Override + public void run() { + //将线程运行程序,尽可能的catch捕获异常 + try { +// Thread.currentThread().setName("任务结果入库管理线程"); + Thread.currentThread().setName("Task Result Library Management Thread"); + boolean runFlag = true; + + //- 检查线程运行状态 运行中无操作 + Future future = Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + runFlag = false; + logger.info("任务结果入库线程 运行中 不再启动新线程"); + } + + + //测试数据库连接 + runFlag = Common.isDbConnected(); + + + /* + * 连续超过Constants.CHECK_WARNING_DATA_OVERRUN次数时,将数据存入硬盘中 + * 否则 正常执行数据解析线程 + * */ + if(!runFlag){ //判断并结束线程 + testNum = (testNum%Constants.CHECK_RESULT_DATA_OVERRUN); + testNum++; + if(testNum==Constants.CHECK_RESULT_DATA_OVERRUN) { + if(Constants.FLAG_TASK_RLT_SAVE_DISK_RESOVE==1) { + /* + * 连续多个周期仍未解析完成 + * 说明list里存有大量的数据,则先将当前未解析的数据列表存入硬盘, + * 然后停掉解析线程,将正在解析的数据列表中的剩余数据存入硬盘 + * */ + logger.warn("Create a database connection failure data to write to the hard disk"); + saveResultDataToDisk(); + } + } + return; + }else{ //重置计数器 程序继续执行 + testNum = 1; + } + + //- 非升级操作判断 + if(Common.SERVER_UN_UPGRADE_FLAG){ + logger.info("任务结果入库线程 空闲中 启动新解析线程"); + + //-- 获取线程执行 需进行主动告警和邮件通知等相关操作,待考虑 +// future = Common.service.submit(new MissionResultThread("任务结果入库线程")); + future = Common.service.submit(new MissionResultThread("Task Result Database Thread")); + //注册 + Common.getFutureMap().put(Constants.MISSION_RESULT_RESOVE, future); + + } + + logger.info("执行结束"); + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + + /** + * 数据库是否可连接判断 + * @time Mar 7, 2013-2:22:42 PM + * @return + */ + public boolean dbTest() { + /* + * 数据库是否可连接判断 + * 是:进行数据解析 + * 否:否进行数据处理解析 + * */ + CommonDao dao = null; + try { +// if(!false)throw new Exception("强制异常"); + dao = new CommonDao(); + logger.debug("尝试获取数据库连接成功"); + return true; + } catch (Exception e) { + logger.error("Try to get a database connection failure", e); + /* 告警 */ + return false; + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + //2013-2-19 hyx :当任务结果数据量过大时,将数据先存放到硬盘,再定时入库(由单独的定时线程实现) + public void saveResultDataToDisk() { + try { + + //终止正在解析告警数据的线程,并将正在解析的list里未解析的数据存入硬盘 + Future future = Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE); + if(future != null && !future.isCancelled() && !future.isDone()){ //运行中 + future.cancel(true);//停解析任务结果数据入库线程 + } + + for(int i=1 ;i<=6 ; i++){ + //-- 获取结果集合 + LinkedList result = Common.getResultListClone(i); + + //-- 集合不为空则进行解析 存在removeList情况 ,优化方式可参考 监测数据解析的实现方式 + if(result!=null && result.size()>0){ + //将未在解析的list存放到硬盘上 + saveMissObjToFile(result,i); + result.clear(); //清除mr2List + } + } + + }catch(IndexOutOfBoundsException e) { + logger.error("When the result data is too much, the data is stored in the hard disk exception",e); + }catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + + } + + //写一个任务对象到一个文件后,清除列表里的这个任务对象 + public void saveMissObjToFile(LinkedList missObjList,int missionType) { + String filePath = ""; + int missObjSize = missObjList.size();//如果不按此时的size来操作,则list里的数据可能会一直在增加,就会一直循环下去? + try { + String parPath = Constants.OVERRUN_RESULT_FILE_DIR.substring(0,Constants.OVERRUN_RESULT_FILE_DIR.length()-1); + LinkedList tmpList = new LinkedList(); + for(int j=0;j ml = new LinkedList(); + MissionResult2 m1 = new MissionResult2(); + m1.setUuid(1l); +// m1.setDescription("描述1-desc"); +// m1.setDescription("Description 1-desc"); + m1.setDescription("i18n_server.MissionResultManagerThread.desc_n81i 1-desc"); + m1.setMissionType(1l); + MissionResult2 m2 = new MissionResult2(); + m2.setUuid(2l); +// m2.setDescription("描述2-desc"); +// m2.setDescription("Description 2-desc"); + m2.setDescription("i18n_server.MissionResultManagerThread.desc_n81i 2-desc"); + m2.setMissionType(1l); + MissionResult2 m3 = new MissionResult2(); + m3.setUuid(3l); +// m3.setDescription("描述3-desc"); +// m3.setDescription("Description 3-desc"); + m3.setDescription("i18n_server.MissionResultManagerThread.desc_n81i 3-desc"); + m3.setMissionType(1l); + +// ml.add(m1); +// ml.add(m2); +// ml.add(m3); +// new MissionResultManagerThread().saveMissObjToFile(result,1); + + Common.addResultToResultList(m1); + Common.addResultToResultList(m2); + Common.addResultToResultList(m3); + + LinkedList result = Common.getResultListClone(1); + //-- 集合不为空则进行解析 存在removeList情况 ,优化方式可参考 监测数据解析的实现方式 + if(result!=null && result.size()>0){ + //将未在解析的list存放到硬盘上 + new MissionResultManagerThread().saveMissObjToFile(result,1); +// Common.removeResultsFormList(1, result); //在Common中删除掉已保存到硬盘的任务结果 + result.clear(); //清除mr2List + } + } + +} diff --git a/src/com/nms/server/thread/mission/MissionResultThread.java b/src/com/nms/server/thread/mission/MissionResultThread.java new file mode 100644 index 0000000..ed38f75 --- /dev/null +++ b/src/com/nms/server/thread/mission/MissionResultThread.java @@ -0,0 +1,228 @@ +package com.nms.server.thread.mission; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import com.nms.server.bean.MissionResult2; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.UpgradeService; +import com.nms.server.util.BoneCPPool; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.StringUtil; + + +/** + * 任务结果处理线程 + * @date Nov 16, 2011 1:25:26 PM + * @author ZhangGang + * + */ +public class MissionResultThread implements Runnable{ + private Logger logger = Logger.getLogger(MissionResultThread.class); + private String name; + volatile boolean stop = false;//线程是否被取消标志 + public MissionResultThread (String name){ + this.name= name; + } + public void run() { + Thread.currentThread().setName(name); + boolean upFlag = false; + boolean loopFlag = false; + + CommonDao dao = null; //数据库链接 + try { + dao = new CommonDao(); + UpgradeService service = new UpgradeService(dao); //专属Service + //-- 获取1-6 五种任务类型的结果集合,批量入库保存 + for(int i=1 ;i<=6&&!stop ; i++){ + try { + //-- 获取结果集合 + LinkedList mr2List = Common.getResultListClone(i); + + //-- 集合不为空则进行解析 存在removeList情况 ,优化方式可参考 监测数据解析的实现方式 + if(mr2List!=null && mr2List.size()>0){ + upFlag = true; // 开启任务是否完成的检查操作 + + service.updateMissionResult2ByBatch(i,mr2List); //保存mr2List中的数据 + Common.removeResultsFormList(i, mr2List); //在Common中删除掉已保存的任务结果 + //更新mission_state_table表里的mission_state_desc字段(只对新录入结果的任务更新,没录入任务结果的任务不需要更新) + service.updateCommonMissionStateDesc(i,mr2List);//i代表任务类型(1,4,6)-非周期任务 + if (i==4){ + loopFlag = true; // 开启周期任务是否完成的检查操作 + service.updateLoopMissionStateDesc(mr2List);//更新任务类型4的周期任务的状态描述 + } + + dao.commit(); + mr2List.clear(); //清除mr2List + } + }catch (InterruptedException e) { + logger.error("Task result data parsing thread is interrupted",e); + stop = true; + }catch (Exception e1) { + logger.error("The task result data parsing thread ,which is abnormal when task type: "+i+" is parsed ,ends the entry of the result of this type of task,and continues to enter the result of the next type of task",e1); + } + + //判断当前线程是否被中断 + if (Thread.currentThread().isInterrupted()) { + // 线程中断状态,不会改变中断状态的值 + logger.info("任务结果数据解析线程 被中断"); + stop = true; + } + } + + //-- 任务执行进度校验,并更新已完成任务 + if(upFlag){ +// service.checkAndUpdateMissionComplete();//任务状态:完成(3) + service.updateCommonMissionComplete();//任务状态:成功(31)、部分成功(32)、全部失败(30) + if(loopFlag){ +// service.checkAndUpdateLoopMissionComplete();--此方法有问题 + service.updateLoopMissionComplete(); + } + } + + /** + * 针对过期未完成的【周期】任务进行清理(非周期任务不需要清理,因为没有结束时间endTime,所以不会超时): + * 1.会不会重复添加过期任务呢?-不会,因为此处将结果加入缓存后,会先执行上面的结果录入,和任务状态的改变,再执行过期任务清理时,之前的过期任务的状态已修改为3或者7,不会被统计 + * 2.清理时,将超时的任务(mission_state_table表中state=2或者6的,且endTime小于当前时间的)的结果存入缓存(Common.addResultToResultList) + * 3.对于过期的周期任务: + * 除了修改周期结果的状态(3->1), + * 还需要修改loopmission_state_table表的状态(2->3或者6->7,对于状态为0的不需要修改,因为界面不会显示?更新状态时也不会统计), + * 及mission_state_table的状态(2->3或者6->7,对于1的情况,在初始化任务时,已经做过超时检查了,初始化后任务的状态变为2了就,所以此处不用对1的做超时检查), + * 注:将过期周期任务结果存入缓存后,上面进行统计时,就会根据结果的状态,统计loop表的状态,及mission_state_table表的状态,所以步骤3的3个修改,我们只需要做第一步,将结果存入缓存 + * + */ + service.checkTimeoutLoopMisssionAndAddResult(); + +// SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); +// Date date = format.parse(Constants.MISSION_LOOP_FINISHING_DAILY); +// System.out.println(format.format(new Date())); +// if(loopFinishingFlag && ){ +// service.checkAndUpdateLoopMissionComplete(); +// }else{ +// +// } + + /** + * 对于执行任务节点网络都不通的情况,第一周期之后就不会有结果入库,所以要定期的执行,不能加有周期结果入库的条件才操作 + * 将下发失败的节点再次下发(包括部分不通及全部不通的所有任务) + */ + List missIds = service.checkLoopMissAssignFail();//统计周期任务中,执行节点全都不通的情况 + if(missIds!=null && missIds.size()>0) { + Common.addAllAssignFailMissionIds(missIds); + } + String[] idsTmp = new String[0]; + idsTmp = Common.getAssignFailMissIds().toArray(idsTmp); + String assignFailMissionIds = StringUtil.linkToString(idsTmp, ",", ""); + + if(StringUtils.isNotBlank(assignFailMissionIds)) { + //下发的时候会过滤,只下发result=3的 + Common.scheduled.schedule(new InitRunningMissionThread(assignFailMissionIds),Constants.MISSION_FIRST_START, TimeUnit.SECONDS); + Common.removeAllAssignFailMissionIds(); + } + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + try { + if(dao!=null)dao.rollback(); + } catch (Exception e1) { + logger.error(ExceptionPrintUtils.printExceptionStack(e1)); + } + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + public static void main(String[] args) { +// LinkedList lks = new LinkedList(); +// lks.add("1"); +// lks.add("2"); +// lks.add("3"); +// +// LinkedList lks2 = null; +// lks2 = (LinkedList)lks.clone(); +// for(String s:lks2) { +// System.out.println(s); +// } +// +// lks.add("4"); +// lks.set(0, "00"); +// +// lks2.remove(0); +// for(String s:lks2) { +// System.out.println(s); +// } +// +// for(String s:lks) { +// System.out.println(s); +// } + String data = "2695$@$4$@$58$@$0$@$1365574748735$@$1365574761781$@$$@$1$@$执行失败,详细信息如下:执行“/home/xian/hello.sh”失败,结果文件“/home/xian/3.txt”不存在;"; + CommonDao dao = null; //数据库链接 + try { +// String [] result = data.split(Constants.COMMON_DATA_SPLIT); +// MissionResult2 result2 = Common.resoveMissionResult(result); +// Common.addResultToResultList(result2); +// +// Common.service.submit(new MissionResultThread("test")); + BoneCPPool.initPool(); + dao = new CommonDao(); + UpgradeService service = new UpgradeService(dao); //专属Service + +// service.checkTimeoutLoopMisssionAndAddResult(); + + List missIds = service.checkLoopMissAssignFail();//统计周期任务中,执行节点全都不通的情况 + + if(missIds!=null && missIds.size()>0) { + Common.addAllAssignFailMissionIds(missIds); + } + + Common.addAssignFailMissionIds("5122"); + + String[] idsTmp = new String[0]; + idsTmp = Common.getAssignFailMissIds().toArray(idsTmp); + String assignFailMissionIds = StringUtil.linkToString(idsTmp, ",", ""); + + if(StringUtils.isNotBlank(assignFailMissionIds)) { + //下发的时候会过滤,只下发result=3的 + Common.scheduled.schedule(new InitRunningMissionThread(assignFailMissionIds),Constants.MISSION_FIRST_START, TimeUnit.SECONDS); + Common.getAssignFailMissIds().clear(); + } + } catch (Exception e) { + e.printStackTrace(); + } +// +// Future future2 = Common.service.submit(new MissionResultThread("任务结果入库线程")); +// Common.getFutureMap().put(Constants.MISSION_RESULT_RESOVE, future2); +// +// System.out.println("启动ok。。。"); +// +// Future future = Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE); +// System.out.println(future.isCancelled()); +// System.out.println(future.isDone()); +// try +// { +//// Thread.sleep(1000); +// if(future!=null&&!future.isCancelled()&&!future.isDone()) { +// System.out.println("开始中断。。。"); +// Thread.sleep(536);//这个时间是经过多次测试出来的 +// // Thread.sleep(100); +// future.cancel(true);//停解析任务结果数据入库线程 +// } +// Thread.sleep(3000); +// Common.service.shutdownNow(); +// System.out.println(future.isCancelled()); +// System.out.println(future.isDone()); +// } catch (InterruptedException e) +// { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// + } + +} diff --git a/src/com/nms/server/thread/mission/NMSClientTask1.java b/src/com/nms/server/thread/mission/NMSClientTask1.java new file mode 100644 index 0000000..1c0df64 --- /dev/null +++ b/src/com/nms/server/thread/mission/NMSClientTask1.java @@ -0,0 +1,74 @@ +package com.nms.server.thread.mission; + +import java.io.File; +import java.util.LinkedList; +import java.util.List; + +import net.sf.json.JSONObject; + +import org.apache.commons.beanutils.BeanUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionStateTable; +import com.nms.server.bean.Task1; +import com.nms.server.common.Constants; +import com.nms.server.util.socket.SSLSocketCallable; +import com.socket.utils.FileComment; + +public class NMSClientTask1 extends SSLSocketCallable{ + private final Logger logger = Logger.getLogger(NMSClientTask1.class); + private Task1 task1 ; + private MissionStateTable mission; + private List fileCommentsList; + public NMSClientTask1(String ip,int port,MissionStateTable mission,Task1 object,List fileCommentsList) throws Exception{ + super(ip,port); + this.task1 = object; + this.mission = mission; + this.fileCommentsList = fileCommentsList; + } + @Override + protected String toDo() throws Exception { + + try { + Thread.currentThread().setName("Task:> "+mission.getMissionId()+" IP:> "+this.socket.getRemoteSocketAddress()); + task1.setTaskId(mission.getMissionId()); + task1.setTaskType(mission.getMissionType()); + StringBuffer warnInfo = new StringBuffer(""); + int trueFileSize = 0; + + List clone = new LinkedList(); + + for(FileComment strs :fileCommentsList){ + FileComment cloneStrs = new FileComment(); + BeanUtils.copyProperties(cloneStrs,strs); + cloneStrs.setFileName(Constants.MISSION_FILE_DOWNLOAD_DIR+File.separator+cloneStrs.getFileName()); + File file = new File(cloneStrs.getFileName()); + if(!file.exists()){ + trueFileSize++; + warnInfo.append((file.getAbsolutePath())+" 文件不存在\n"); + logger.warn("File does not exist "+(file.getAbsolutePath())); + } + clone.add(cloneStrs); + } + if(trueFileSize!=0){ + return "1:"+warnInfo; + } + JSONObject object = new JSONObject(); + object.put("typeInfo", mission.getMissionType()); + object.put("taskInfo", task1); + logger.debug("JSONObject "+object.toString()); + this.sendMessage("byte:filePush"); + this.receiveMessage(); + this.sendMessage(object.toString()); + this.receiveMessage(); + this.bpSendFileByBathMD5(clone); + String string = this.receiveMessage(); + logger.debug("文件推送结果数据 "+string); + this.sendMessage(SUCCESS); + return string; + } catch (Exception e) { + logger.error("File push failed",e); + return null; + } + } +} diff --git a/src/com/nms/server/thread/mission/NMSClientTask4.java b/src/com/nms/server/thread/mission/NMSClientTask4.java new file mode 100644 index 0000000..9b9b8ee --- /dev/null +++ b/src/com/nms/server/thread/mission/NMSClientTask4.java @@ -0,0 +1,72 @@ +package com.nms.server.thread.mission; + +import net.sf.json.JSONObject; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult; +import com.nms.server.bean.MissionStateTable; +import com.nms.server.bean.Task4; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.socket.SSLSocketCallable; + +public class NMSClientTask4 extends SSLSocketCallable{ + private final Logger logger = Logger.getLogger(NMSClientTask4.class); + private Task4 task4 ; + private MissionStateTable mission; + private MissionResult result; + public NMSClientTask4(String ip,int port,MissionStateTable mission,Task4 object, MissionResult result) throws Exception{ + super(ip,port); + this.task4 = object; + this.mission = mission; + this.result = result; + } + + @Override + protected String toDo() throws Exception { + Thread.currentThread().setName("Task 4:> "+mission.getMissionId()+" IP:> "+this.socket.getRemoteSocketAddress()); + try { + + //任务正常下发 + task4.setTaskId(mission.getMissionId()); + task4.setTaskType(mission.getMissionType()); + task4.setStartTime(mission.getStartTime()==null?null:mission.getStartTime().getTime()); + task4.setEndTime(mission.getEndTime()==null?null:mission.getEndTime().getTime()); + task4.setState(result.getResult()); + task4.setIsLoop(mission.getLoopFlag()); + task4.setLoopDelay(mission.getLoopDelay()); + task4.setMissionState(mission.getMissionState()); + JSONObject object = new JSONObject(); + object.put("typeInfo", mission.getMissionType()); + object.put("taskInfo", task4); + logger.debug("JSONObject "+object.toString()); + this.sendMessage("char:task"); + this.receiveMessage(); + this.sendMessage(object.toString()); + String string = this.receiveMessage(); + logger.debug("任务执行下发结果是 "+string); + this.sendMessage(SUCCESS); + return string; + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + } + } + /* + public static void main(String []args){ + CommonDao dao = new CommonDao(); //数据库链接 + UpgradeService service = new UpgradeService(dao); //专属Service + try { + + Task4 task4 = service.getTask4ById(1274540l); //查询任务具体信息 + MissionStateTable table = service.getMessionById(1274540l); + List list = service.getMissionResultsForTask(1274540l, 4); + MissionResult result = list.get(0); + result.setResult(3l); + Common.service.submit(new NMSClientTask4("10.0.6.242",60701,table,task4, result)); + }catch (Exception e) { + e.printStackTrace(); + } + }*/ +} diff --git a/src/com/nms/server/thread/mission/NMSClientTask4Cancel.java b/src/com/nms/server/thread/mission/NMSClientTask4Cancel.java new file mode 100644 index 0000000..9f70543 --- /dev/null +++ b/src/com/nms/server/thread/mission/NMSClientTask4Cancel.java @@ -0,0 +1,35 @@ +package com.nms.server.thread.mission; + +import org.apache.log4j.Logger; + +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.socket.SSLSocketCallable; + +public class NMSClientTask4Cancel extends SSLSocketCallable{ + private final Logger logger = Logger.getLogger(NMSClientTask4Cancel.class); + private long missionId; + public NMSClientTask4Cancel(String ip,int port,long missionId) throws Exception{ + super(ip,port); + this.missionId = missionId; + } + + @Override + protected String toDo() throws Exception { + Thread.currentThread().setName("Task 4:> "+missionId+" IP:> "+this.socket.getRemoteSocketAddress()); + try { + + //撤销任务 + this.sendMessage("char:taskCancel"); + this.receiveMessage(); + this.sendMessage(missionId+""); + String string = this.receiveMessage(); + logger.debug("任务执行下发结果是 "+string); + this.sendMessage(SUCCESS); + return string; + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + } + } +} diff --git a/src/com/nms/server/thread/mission/NMSClientTask6.java b/src/com/nms/server/thread/mission/NMSClientTask6.java new file mode 100644 index 0000000..30801fb --- /dev/null +++ b/src/com/nms/server/thread/mission/NMSClientTask6.java @@ -0,0 +1,82 @@ +package com.nms.server.thread.mission; + +import java.io.File; +import java.util.LinkedList; +import java.util.List; + +import net.sf.json.JSONObject; + +import org.apache.commons.beanutils.BeanUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult; +import com.nms.server.bean.MissionStateTable; +import com.nms.server.bean.Task6; +import com.nms.server.common.Constants; +import com.nms.server.util.socket.SSLSocketCallable; +import com.socket.utils.FileComment; + +public class NMSClientTask6 extends SSLSocketCallable{ + private final Logger logger = Logger.getLogger(NMSClientTask6.class); + private Task6 task6 ; + private MissionStateTable mission; + private MissionResult result; + private String ip; + private List fileCommentsList; + public NMSClientTask6(String ip,int port,MissionStateTable mission,Task6 object, MissionResult result,List fileCommentsList) throws Exception{ + super(ip,port); + this.ip = ip; + this.task6 = object; + this.mission = mission; + this.result = result; + this.fileCommentsList = fileCommentsList; + } + + @Override + protected String toDo() throws Exception { + Thread.currentThread().setName("Task 6 id:> "+mission.getMissionId()+" IP:> "+this.socket.getRemoteSocketAddress()); + try { + //-- 补全任务信息 + task6.setTaskId(mission.getMissionId()); + task6.setTaskType(mission.getMissionType()); + task6.setState(result.getResult()); + //-- 任务信息整理 + StringBuffer warnInfo = new StringBuffer(""); + int trueFileSize = 0; + List clone = new LinkedList(); + if(fileCommentsList !=null && fileCommentsList.size()>0){ + for(FileComment strs :fileCommentsList){ + FileComment cloneStrs = new FileComment(); + BeanUtils.copyProperties(cloneStrs,strs); + cloneStrs.setFileName(Constants.MISSION_FILE_DOWNLOAD_DIR+File.separator+cloneStrs.getFileName()); + File file = new File(cloneStrs.getFileName()); + if(!file.exists()){ + trueFileSize++; + warnInfo.append((file.getAbsolutePath())+" 文件不存在\n"); + logger.warn("File does not exist "+(file.getAbsolutePath())); + } + clone.add(cloneStrs); + } + } + if(trueFileSize!=0){ + return "1:"+warnInfo; + } + JSONObject object = new JSONObject(); + object.put("typeInfo", mission.getMissionType()); + object.put("taskInfo", task6); + logger.debug("JSONObject "+object.toString()); + this.sendMessage("byte:upgrade"); + this.receiveMessage(); + this.sendMessage(object.toString()); + this.receiveMessage(); + this.bpSendFileByBathMD5(clone); + String string = this.receiveMessage(); + logger.debug("升级部署之文件推送结果 "+string); + this.sendMessage(SUCCESS); + return string; + } catch (Exception e) { + logger.error("Upgrade deployment failure",e); + return null; + } + } +} diff --git a/src/com/nms/server/thread/mission/NMSClientTask6Cover.java b/src/com/nms/server/thread/mission/NMSClientTask6Cover.java new file mode 100644 index 0000000..3682901 --- /dev/null +++ b/src/com/nms/server/thread/mission/NMSClientTask6Cover.java @@ -0,0 +1,57 @@ +package com.nms.server.thread.mission; + +import java.util.List; + +import net.sf.json.JSONObject; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult; +import com.nms.server.bean.MissionStateTable; +import com.nms.server.bean.Task6; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.socket.SSLSocketCallable; +import com.socket.utils.FileComment; + +public class NMSClientTask6Cover extends SSLSocketCallable{ + private final Logger logger = Logger.getLogger(NMSClientTask6Cover.class); + private Task6 task6 ; + private MissionStateTable mission; + private MissionResult result; + private String ip; + private List fileCommentsList; + public NMSClientTask6Cover(String ip,int port,MissionStateTable mission,Task6 object, MissionResult result,List fileCommentsList) throws Exception{ + super(ip,port); + this.ip = ip; + this.task6 = object; + this.mission = mission; + this.result = result; + this.fileCommentsList = fileCommentsList; + } + + @Override + protected String toDo() throws Exception { + Thread.currentThread().setName("Task 6 id:> "+mission.getMissionId()+" IP:> "+this.socket.getRemoteSocketAddress()); + try { + //-- 补全任务信息 + task6.setTaskId(mission.getMissionId()); + task6.setTaskType(mission.getMissionType()); + task6.setState(result.getResult()); + + JSONObject object = new JSONObject(); + object.put("typeInfo", mission.getMissionType()); + object.put("taskInfo", task6); + logger.debug("JSONObject "+object.toString()); + this.sendMessage("char:task"); + this.receiveMessage(); + this.sendMessage(object.toString()); + String string = this.receiveMessage(); + logger.debug("升级部署之文件推送结果 "+string); + this.sendMessage(SUCCESS); + return string; + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + } + } +} diff --git a/src/com/nms/server/thread/monitor/MonitorManagerThread.java b/src/com/nms/server/thread/monitor/MonitorManagerThread.java new file mode 100644 index 0000000..22ed282 --- /dev/null +++ b/src/com/nms/server/thread/monitor/MonitorManagerThread.java @@ -0,0 +1,190 @@ +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 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 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); + } + } + +} diff --git a/src/com/nms/server/thread/monitor/NMSClientForSNMPThread.java b/src/com/nms/server/thread/monitor/NMSClientForSNMPThread.java new file mode 100644 index 0000000..c453028 --- /dev/null +++ b/src/com/nms/server/thread/monitor/NMSClientForSNMPThread.java @@ -0,0 +1,439 @@ +package com.nms.server.thread.monitor; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.math.BigDecimal; +import java.net.Socket; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.snmp4j.Target; +import org.snmp4j.mp.SnmpConstants; + +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.SetInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.CSVUtils; +import com.nms.server.util.DateUtil; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.SNMP4JUtils; + +/** + * 监测数据 + * 监测NMSClient端是否执行执行线程 + * @author ZhangGang + * + */ +public class NMSClientForSNMPThread implements Runnable{ + private Logger logger = Logger.getLogger(NMSClientForSNMPThread.class); + private SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + private String name; + private String ip; + private int port = Constants.SNMP_CLIENT_PORT; + private String checkTypeName = null; + private Long startTime = null; + private Long sitInfoId = null; + private long outTime = 2; + private long maxTimes = 3; + private long gap = 0 ; + protected Socket socket = null; //Socket + private String processIden = null; + private CountDownLatch downLatch = null; + /** + * 构造方法 + * @param name 线程名称 + * @param ip 被监测端IP + * @param port socket通信端口 + * @param info + */ + public NMSClientForSNMPThread(String name,String ip,SetInfo info,Long startTime,CountDownLatch downLatch) { + this.name = name; + this.ip = ip; + this.sitInfoId = info.getId(); + this.checkTypeName = info.getCheckTypeName(); + this.startTime = startTime; + this.outTime = info.getCheckOutTime(); + this.maxTimes = info.getCheckMaxTimes(); + this.processIden = info.getProcessIden()==null ? "" : info.getProcessIden(); + this.downLatch = downLatch; + } + + public NMSClientForSNMPThread(String name,String ip,SetInfo info,Long startTime) { + this.name = name; + this.ip = ip; + this.sitInfoId = info.getId(); + this.checkTypeName = info.getCheckTypeName(); + this.startTime = startTime; + this.outTime = info.getCheckOutTime(); + this.maxTimes = info.getCheckMaxTimes(); + this.processIden = info.getProcessIden()==null ? "" : info.getProcessIden(); + this.downLatch = downLatch; + } + @Override + public void run() { + + Thread.currentThread().setName(name+" "+ip); + + try { + + Calendar checkTime = Calendar.getInstance(); + Calendar nextTime = Calendar.getInstance(); + nextTime.add(Calendar.MINUTE, Long.valueOf(gap).intValue()); + + List datas = new ArrayList(); //拼写数据格式 已安全进入公共数据解析入口 + //--初始部分数据整理 + datas.add(Common.getIpSeqIdMap().get(ip)+"");//IP, + datas.add(sitInfoId+"");//监测设置ID, + datas.add(checkTypeName);//监测类别, + datas.add(processIden);//进程名称(检查类别设置名称), + + String stateInfo =""; + int state = -1; + String[] datas2 = new String[6]; + int a = 0; + String msgArr = ""; + boolean isSuccess = false; + datas2[3] = Calendar.getInstance().getTimeInMillis()+""; + isSuccess = this.sockeHandshake(msgArr); + datas2[4] = Calendar.getInstance().getTimeInMillis()+""; + datas2[0] = "snmp:"+"1.3.6.1.2.1.1.5.0"; + datas2[1] = msgArr; +// stateInfo += "开始"+format.format(new Date(Long.parseLong(datas2[3])))+ " SNMP握手"; +// stateInfo += "Start"+format.format(new Date(Long.parseLong(datas2[3])))+ " SNMP Shakehand"; + stateInfo += "i18n_server.NMSClientForSNMPThread.begin_n81i"+format.format(new Date(Long.parseLong(datas2[3])))+ " i18n_server.NMSClientForSNMPThread.snmpShake_n81i"; + datas.add(startTime+"");//监测服务启动时间, + if(isSuccess){ //成功 +// stateInfo += format.format(new Date(Long.parseLong(datas2[4]))) + " 成功 "; + stateInfo += format.format(new Date(Long.parseLong(datas2[4]))) + " i18n_server.NMSClientForSNMPThread.success_n81i "; + datas2[2] = "0"; + }else{ //失败 +// stateInfo += format.format(new Date(Long.parseLong(datas2[4]))) + " 失败 "; + stateInfo += format.format(new Date(Long.parseLong(datas2[4]))) + " i18n_server.NMSClientForSNMPThread.fail_n81i "; + datas2[2] = "1"; + String [] pingData = pingHandshake(); + + if ((StringUtils.isEmpty(pingData[1]) || "0".equals(pingData[1])) ) { +// stateInfo = "网络异常"; + stateInfo = "i18n_server.NMSClientForSNMPThread.netErr_n81i"; + } else { +// stateInfo = "请检查目标主机NMSClient是否启动"; + } + } + datas2[5] = DateUtil.getSecondsFromBeinToEnd(new Date(Long + .parseLong(datas2[3].toString())).getTime(), new Date(Long + .parseLong(datas2[4].toString())).getTime()) + + ""; + state = 0; + + + datas.add("0");//检测时延(秒) + datas.add(checkTime.getTimeInMillis()+"");//本次检测时间, + + if(state!=-1){ + datas.add(a+"");// 尝试次数,+"/"+maxTimes + datas.add(nextTime.getTimeInMillis()+"");// 下次计划监测时间, + String [] pArray = getAlarmState(Common.getAlarmInfoMap().get(sitInfoId.longValue()+""), datas2); + if(isSuccess){ + datas.add("1"); // 执行状态 + datas.add(""); // 告警列序号, + datas.add(""); // 告警级别, + datas.add(""); // 告警值, + }else { + datas.add("0"); // 执行状态 + datas.add(pArray==null?"":pArray[0]); // 告警列序号, + datas.add(pArray==null?"":pArray[1]); // 告警级别, + datas.add(pArray==null?"":pArray[2]); // 告警值, + } + datas.add(stateInfo); // 状态信息(描述信息), + datas.add(stateInfo); // 性能数据, + datas.addAll(Arrays.asList(datas2));//监测具体数据信息(每个字段一列) + } else { + datas.add(a+"");// 尝试次数,+"/"+maxTimes + datas.add(nextTime.getTimeInMillis()+"");// 下次计划监测时间, + datas.add("-1"); // 执行状态 + datas.add("3"); // 告警列序号,(丢包数 列 序号 为3) + datas.add("1"); // 告警级别, + datas.add("1"); // 告警值, + datas.add(stateInfo); // 状态信息(描述信息), + datas.add(stateInfo); // 性能数据, + } + datas.add(checkTime.getTimeInMillis()+"");//本次检测时间, + + //- 存入缓存 + try { + logger.debug("data:"+Arrays.toString(datas.toArray())); + Common.addDeteData(CSVUtils.csvBytesPrinter(datas, Constants.COMMON_TEXT_CODING)); + logger.debug("监测数据以 CSV格式 保存完成"); + } catch (Exception e) { + logger.error("Monitoring data to save exceptions in CSV format", e); + } + } catch (Exception ex) { + logger.error(ExceptionPrintUtils.printExceptionStack(ex)); + String[] alarm = Common.alarmExceptionInfo(sitInfoId, ip, Constants.DETEC_NMSC_STR, processIden, startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); + Common.addAlarmData(alarm); + }finally{ + if(downLatch != null){ + downLatch.countDown(); + } + } + } + + private void datasHeader(List pubDatas){ + + logger.debug("监测数据头拼写开始"); + + // --初始部分数据整理 + pubDatas.add(Common.getIpSeqIdMap().get(ip)+"");// uuid, + pubDatas.add(sitInfoId + "");// 监测设置ID, + pubDatas.add(checkTypeName);// 监测类别, + pubDatas.add("");// 进程名称(检查类别设置名称), + pubDatas.add(startTime + "");// 监测服务启动时间, + pubDatas.add("0");// 检测时延(秒) + + } + + private boolean sockeHandshake(String value){ + //- 通过 CloumnsMap 获取 oids + String [] oidArray = new String []{"1.3.6.1.2.1.1.5.0"}; + //- 监测数据集合 + List pubDatas = new ArrayList(); // 拼写数据格式 + + //- DetecDatas 拼写 Header + datasHeader(pubDatas); + + int version = Common.getSnmpVerByUUID(Common.getIpSeqIdMap().get(ip)); +// int version = SnmpConstants.version2c; + //-- 执行和解析 即获取详细数据 + int state = -1; + + Map snmpDatas = new HashMap() ; + StringBuffer stateInfo = new StringBuffer(); + try { + Target target= null; + if(version == SnmpConstants.version3){ + target = SNMP4JUtils.createUserTarget(ip, port+"", (int)maxTimes, (int)outTime*1000); + }else{ + target = SNMP4JUtils.createCommunityTarget(ip, port+"", Constants.SNMP_COMMUNITY, (int)maxTimes, (int)outTime*1000, SnmpConstants.version2c); + } + SNMP4JUtils.snmpGet(snmpDatas, oidArray, target); + + state = 1; + } catch (Exception e) { + state = -1; + stateInfo.append(e.getMessage()); + logger.error((version == SnmpConstants.version3?"version3":"version2c")+" GET失败", e); + } + + //- DetecDatas 拼写 Body + if(state==1 && snmpDatas.size()>0 && StringUtils.isNotBlank(snmpDatas.get(oidArray[0]))){ + value = snmpDatas.get(oidArray[0]); + logger.debug((version == SnmpConstants.version3?"version3":"version2c")+" GET:"+oidArray[0]+" "+snmpDatas.get(oidArray[0])); + return true; + }else{ + return false; + } + } + + private String[] pingHandshake(){ + + String command = ""; // 命令语句 + int minTime = Integer.MAX_VALUE, maxTime = 0, totalTime = 0, curTime; // 最短时间,最长时间,总时间,时间变量 + int snum = 0, fnum = 0; // 发包成功和失败数 + + // 判断系统类型 win or Linux + String system = (String) (System.getProperty("os.name")).toLowerCase(); + + if (system.indexOf("win") != -1) { + command += "ping -n 4 -w 4000 " + ip; + } else if (system.indexOf("linux") != -1) { + command += "ping -c 4 -w 4000 " + ip; + } else { + command += "ping " + ip; + } + + String stateInfo = ""; + String[] datas2 = new String[7]; + Process process = null; + BufferedReader in = null; // 读取 Ping命令返回的信息 + StringBuffer buffer = new StringBuffer(); + try { + process = Runtime.getRuntime().exec(command); + in = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = null; + long count = maxTimes + 10; + int index; + // 最多多读10行 + while ((line = in.readLine()) != null && count != 0) { + if ("".equals(line)) { + continue; + } // 空串跳过 + buffer.append(line + "\n"); + line = line.toLowerCase(); + logger.debug("line:"+line); + if (line.indexOf("ttl") > 0) { // 获得成功响应的数据 + count--; // 计数器自减1 + // 截取time 值 + if ((index = line.lastIndexOf("ms")) != -1) { + byte[] buf = line.getBytes(); + int start = 0, end = buf.length, i, j; + // 下标末点 + for (i = index; i >= 0; i--) { + if (Character.isDigit((char) buf[i])) { + end = i; + break; + } + } + if (i == 0) + continue; + // 下标起点 + for (j = end; j >= 0; j--) { + if (!Character.isDigit((char) buf[j])) { + start = j + 1; + break; + } + } + // 时间值截取 + curTime = Integer.parseInt(new String(buf, start, + end + 1 - start)); + snum++; // 成功接收次数加1 + totalTime += curTime; // 计算总时间 + + if (curTime < minTime) { + minTime = curTime; + } // 最小时间 + + if (curTime > maxTime) { + maxTime = curTime; + } // 最大时间 + } + } else if (line.split(" ").length < 4) { + count--; // 计数器自减1 + fnum++; // 失败接收次数加1 + } else { + stateInfo += line + "\n"; + } + } + + if (totalTime == 0) { + minTime = 0; + } + + // 已发送包数 + datas2[0] = "" + (snum + fnum); + datas2[1] = "" + snum; + datas2[2] = "" + fnum; + + BigDecimal fnum0 = new BigDecimal(fnum); + BigDecimal tnum0 = new BigDecimal(snum + fnum); + DecimalFormat df = new DecimalFormat("####0.00"); + datas2[3] = "" + + ((new BigDecimal(df.format(fnum0.divide(tnum0)))) + .multiply(new BigDecimal(100))); + datas2[4] = "" + minTime; + datas2[5] = "" + maxTime; + datas2[6] = "" + (snum == 0 ? 0 : (totalTime / snum)); + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + }finally{ + if(in!=null){try { + in.close(); + } catch (IOException e) { + logger.error("",e); + }} + if(process!= null)process.destroy(); + process = null; + } + + /*if(state==-1){ + isSuccess = false; + }else{ + isSuccess = true; + }*/ + return datas2; + } + + //获得当前的报警设置信息,与相应字段值比较 + private String[] getAlarmState(List alarmInfos, String[] sysData){ + String[] strs = null; + if(alarmInfos!=null){ + strs = new String[]{"", "", ""}; + for(AlarmInfo alarm : alarmInfos){ + if(sysData.length>=alarm.getShowNum()){ + Double data = Math.abs(Double.parseDouble(sysData[alarm.getShowNum()-1])); + if("equal".equals(alarm.getPoliceSysmbols())){ + if((data+"").equals(alarm.getPoliceValue())){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else if("include".equals(alarm.getPoliceSysmbols())){ + if((data+"").indexOf(alarm.getPoliceValue())!=-1){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else if("exclude".equals(alarm.getPoliceSysmbols())){ + if((data+"").indexOf(alarm.getPoliceValue())==-1){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else{ + double result = data - Double.parseDouble(alarm.getPoliceValue()); + if ((">".equals(alarm.getPoliceSysmbols()) && result > 0) + || ("<".equals(alarm.getPoliceSysmbols()) && result < 0) + || ("=".equals(alarm.getPoliceSysmbols()) && result == 0) + || (">=".equals(alarm.getPoliceSysmbols()) && result >= 0) + || ("<=".equals(alarm.getPoliceSysmbols()) && result <=0) ) { + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + } + } + for(int i=0; i0){ + strs[i] = strs[i].substring(0, strs[i].length()-1); + } + } + } + + if(strs!=null && "".equals(strs[0])){ + strs = null; + } + + return strs; + } + + public NMSClientForSNMPThread(String name,Long sitInfoId,String socketIp,long gap,long outTime,long maxTimes) { + this.name = name; + this.ip = socketIp; + this.sitInfoId = sitInfoId; + this.outTime = outTime; + this.maxTimes = maxTimes; + this.gap = gap; + } + + /*public static void main(String [] args) { + Thread thread = new Thread(new NMSClientForSNMPThread("NMSC Thread",10l,"10.0.6.254",2,100,3)); +// String name,Long sitInfoId,String socketIp,int socketPort,long gap,long outTime,long maxTimes + thread.start(); + }*/ + +} diff --git a/src/com/nms/server/thread/monitor/NMSClientForSocketThread.java b/src/com/nms/server/thread/monitor/NMSClientForSocketThread.java new file mode 100644 index 0000000..f0acbfd --- /dev/null +++ b/src/com/nms/server/thread/monitor/NMSClientForSocketThread.java @@ -0,0 +1,757 @@ +package com.nms.server.thread.monitor; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.MissionResult2; +import com.nms.server.bean.SetInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.thread.file.UnZipManagerThread; +import com.nms.server.thread.monitor.util.MonitorUtil; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.CSVUtils; +import com.nms.server.util.DateUtil; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.socket.SocketUtils; +import com.nms.server.util.socket.limiter.InputStreamLimiter; +import com.nms.server.util.socket.limiter.OutputStreamLimiter; + +/** + * 监测数据 + * 监测NMSClient端是否执行执行线程 + * @author ZhangGang + * + */ +public class NMSClientForSocketThread extends SocketUtils implements Runnable{ + private Logger logger = Logger.getLogger(NMSClientForSocketThread.class); + private SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + private String name; + private String checkTypeName = null; + private Long startTime = null; + private Long sitInfoId = null; + private long outTime = 2; + private long maxTimes = 3; + private long gap = 0 ; +// private String cmd = SocketCMD.CLIENT_REQ_HAND_SHAKE; + private String processIden = null; +// protected Socket socket = null; //Socket + private CountDownLatch downLatch; + + private SetInfo info; + /** + * 构造方法 + * @param name 线程名称 + * @param ip 被监测端IP + * @param port socket通信端口 + * @param info + */ + public NMSClientForSocketThread(String name,String ip,int port,SetInfo info,Long startTime,CountDownLatch downLatch) { + super(ip, port); + this.name = name; + this.sitInfoId = info.getId(); + this.checkTypeName = info.getCheckTypeName(); + this.startTime = startTime; + this.outTime = info.getCheckOutTime(); + this.maxTimes = info.getCheckMaxTimes(); + this.processIden = info.getProcessIden()==null ? "" : info.getProcessIden(); + this.downLatch = downLatch; + + this.info = info; + } + + public NMSClientForSocketThread(String name,String ip,int port,SetInfo info,Long startTime) { + super(ip, port); + this.name = name; + this.sitInfoId = info.getId(); + this.checkTypeName = info.getCheckTypeName(); + this.startTime = startTime; + this.outTime = info.getCheckOutTime(); + this.maxTimes = info.getCheckMaxTimes(); + this.processIden = info.getProcessIden()==null ? "" : info.getProcessIden(); + this.downLatch = downLatch; + + this.info = info; + } + + @Override + public void run() { + + Thread.currentThread().setName(name+" "+ip); + try { + + /** + * + * ping不通时,无握手监测详细数据 + * 握手失败时,有握手监测详细数据 + * 握手成功时,有握手监测详细数据 + * + */ + + Long checkTime = System.currentTimeMillis(); + + int a = 0; + DetectInfo detectInfo = null; + String failStartTime = ""; + String failEndTime = ""; + + + loop: + for(; a < maxTimes ;){ + failStartTime = Calendar.getInstance().getTimeInMillis()+""; + detectInfo = this.getNMSClientBysocket(); + failEndTime = Calendar.getInstance().getTimeInMillis()+""; + a++; + if(detectInfo != null){ //成功,如果第一次就成功的话,那么尝试次数为1 + break loop; + }else{ //失败 + continue loop; + } + } + + if(detectInfo == null){//握手失败 + detectInfo = new DetectInfo(); + String stateInfo = ""; + String [] pingData = pingHandshake(); + + if ((StringUtils.isEmpty(pingData[1]) || "0".equals(pingData[1])) ) {//ping不通,无握手监测详细数据 +// stateInfo = "网络异常"; +// stateInfo = "Network Exception"; + stateInfo = "i18n_server.NMSClientForSNMPThread.netErr_n81i"; + } else {//如果可以ping通,但是握手失败,则有详细握手监测数据 +// stateInfo = "开始"+format.format(new Date(checkTime))+ " NMSClient握手" + +// format.format(new Date()) + " 失败 "; +// stateInfo = "Start"+format.format(new Date(checkTime))+ " NMSClient Shakehand" + +// format.format(new Date()) + " failed "; + stateInfo = "i18n_server.NMSClientForSNMPThread.begin_n81i"+format.format(new Date(checkTime))+ " i18n_server.NMSClientForSocketThread.nmsShake_n81i" + + format.format(new Date()) + " i18n_server.NMSClientForSNMPThread.fail_n81i "; + StringBuffer detailInfo = new StringBuffer(); + detailInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);//发送信息 + detailInfo.append(MonitorUtil.SEPARATOR);//分隔符 + detailInfo.append("");//返回信息 + detailInfo.append(MonitorUtil.SEPARATOR);//分隔符 + detailInfo.append("1");//是否可达,0:可达,1:不可达 + detailInfo.append(MonitorUtil.SEPARATOR);//分隔符 + detailInfo.append(failStartTime);//发送时间 + detailInfo.append(MonitorUtil.SEPARATOR);//分隔符 + detailInfo.append(failEndTime);//返回时间 + detailInfo.append(MonitorUtil.SEPARATOR);//分隔符 + detailInfo.append(DateUtil.getSecondsFromBeinToEnd(new Date(Long + .parseLong(failStartTime)).getTime(), new Date(Long + .parseLong(failEndTime)).getTime()) + + "");//延迟时间 + + detectInfo.getDetailDatas().add(detailInfo.toString()); + } + + detectInfo.setDescInfo(stateInfo); + logger.info("与 "+ip+" 的握手监测 异常:"+stateInfo); + } else {//握手成功,则进行监测数据的收集?? + logger.info("与 "+ip+" 的握手监测 正常,开始收集数据"); +// Thread.currentThread().setName("数据收集:>"+ip); + Thread.currentThread().setName("Data Collection:>"+ip); + collectDatas();//收集数据 + Thread.currentThread().setName(name+" "+ip); + } + + detectInfo.setTestTimes(a);//尝试次数 + detectInfo.setCheckTime(checkTime);//监测时间 + + //-- 生成监测数据 + MonitorUtil.createMonitorData(ip, info, startTime, Common.getAlarmInfoMap().get(info.getId()+ ""), detectInfo); + } catch (Exception ex) { + logger.error(ExceptionPrintUtils.printExceptionStack(ex)); + String[] alarm = Common.alarmExceptionInfo(sitInfoId, ip, Constants.DETEC_NMSC_STR, processIden, startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); + Common.addAlarmData(alarm); + }finally{ + if(downLatch!=null){ + downLatch.countDown(); + } + } + } + + private DetectInfo getNMSClientBysocket(){ + + DetectInfo detectInfo = null; + //set up a connection + try { + //-- 创建通讯 + this.createClientSocket(); + + //-- 发送握手命令 + String sendTime = Calendar.getInstance().getTimeInMillis()+""; + sendMessage(SocketCMD.CLIENT_REQ_HAND_SHAKE); + String msg = receiveMessage();//收到回复的信息后,说明握手成功了 + + String backTime = Calendar.getInstance().getTimeInMillis()+""; + //-- 信息保存到监测信息对象中 + detectInfo = new DetectInfo(); + + StringBuffer nmsClientDetailDetectInfo = new StringBuffer(); + nmsClientDetailDetectInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);//发送信息 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);//分隔符 + nmsClientDetailDetectInfo.append(msg);//返回信息 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);//分隔符 + nmsClientDetailDetectInfo.append("0");//是否可达,0:可达,1:不可达 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);//分隔符 + nmsClientDetailDetectInfo.append(sendTime);//发送时间 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);//分隔符 + nmsClientDetailDetectInfo.append(backTime);//返回时间 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);//分隔符 + nmsClientDetailDetectInfo.append(DateUtil.getSecondsFromBeinToEnd(new Date(Long + .parseLong(sendTime)).getTime(), new Date(Long + .parseLong(backTime)).getTime()) + + "");//延迟时间 + + detectInfo.getDetailDatas().add(nmsClientDetailDetectInfo.toString()); + +// detectInfo.setDescInfo("开始"+format.format(new Date(Long.parseLong(sendTime)))+ " NMSClient握手" + +// format.format(new Date(Long.parseLong(backTime))) + " 成功 "); +// detectInfo.setDescInfo("Start"+format.format(new Date(Long.parseLong(sendTime)))+ " NMSClient Shakehand" + +// format.format(new Date(Long.parseLong(backTime))) + "success "); + detectInfo.setDescInfo("i18n_server.NMSClientForSNMPThread.begin_n81i"+format.format(new Date(Long.parseLong(sendTime)))+ " i18n_server.NMSClientForSocketThread.nmsShake_n81i" + + format.format(new Date(Long.parseLong(backTime))) + "i18n_server.NMSClientForSNMPThread.success_n81i "); + + } catch (Exception e) { + logger.error("Failure to shake hands with"+ip+e.getMessage()); + }finally{ + close(); + } + + return detectInfo; + } + + public static long getMinutesFromBeginToEnd(long begin, long end) { + return (end - begin) / (60 * 1000); + } + + private String[] pingHandshake(){ + + String command = ""; // 命令语句 + int minTime = Integer.MAX_VALUE, maxTime = 0, totalTime = 0, curTime; // 最短时间,最长时间,总时间,时间变量 + int snum = 0, fnum = 0; // 发包成功和失败数 + + // 判断系统类型 win or Linux + String system = (String) (System.getProperty("os.name")).toLowerCase(); + + if (system.toLowerCase().indexOf("win") != -1) { + command += "ping -n 4 " + ip;//尝试次数 -n + } else if (system.toLowerCase().indexOf("linux") != -1) { + command += "ping -c 4 " + ip; + } else { + command += "ping -w 4 " + ip; + } + + String stateInfo = ""; + String[] datas2 = new String[7]; + Process process = null; + BufferedReader in = null; // 读取 Ping命令返回的信息 + StringBuffer buffer = new StringBuffer(); + try { + logger.debug("ping command:"+command); + process = Runtime.getRuntime().exec(command); + in = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = null; + long count = maxTimes + 10; + int index; + // 最多多读10行 + while ((line = in.readLine()) != null && count != 0) { + if ("".equals(line)) { + count--; + continue; + } // 空串跳过 + buffer.append(line + "\n"); + line = line.toLowerCase(); + logger.debug("line:"+line); + if (line.indexOf("ttl") > 0) { // 获得成功响应的数据 + count--; // 计数器自减1 + // 截取time 值 + if ((index = line.lastIndexOf("ms")) != -1) { + byte[] buf = line.getBytes(); + int start = 0, end = buf.length, i, j; + // 下标末点 + for (i = index; i >= 0; i--) { + if (Character.isDigit((char) buf[i])) { + end = i; + break; + } + } + if (i == 0) + continue; + // 下标起点 + for (j = end; j >= 0; j--) { + if (!Character.isDigit((char) buf[j])) { + start = j + 1; + break; + } + } + // 时间值截取 + curTime = Integer.parseInt(new String(buf, start, + end + 1 - start)); + snum++; // 成功接收次数加1 + totalTime += curTime; // 计算总时间 + + if (curTime < minTime) { + minTime = curTime; + } // 最小时间 + + if (curTime > maxTime) { + maxTime = curTime; + } // 最大时间 + } + } else if (line.split(" ").length < 4) { + count--; // 计数器自减1 + fnum++; // 失败接收次数加1 + } else { + stateInfo += line + "\n"; + } + } + + if (totalTime == 0) { + minTime = 0; + } + + // 已发送包数 + datas2[0] = "" + (snum + fnum); + datas2[1] = "" + snum; + datas2[2] = "" + fnum; + + BigDecimal fnum0 = new BigDecimal(fnum); + BigDecimal tnum0 = new BigDecimal(snum + fnum); + DecimalFormat df = new DecimalFormat("####0.00"); + datas2[3] = "" + ((new BigDecimal(df.format(fnum0.divide(tnum0)))) + .multiply(new BigDecimal(100))); + datas2[4] = "" + minTime; + datas2[5] = "" + maxTime; + datas2[6] = "" + (snum == 0 ? 0 : (totalTime / snum)); + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + }finally{ + if(in!=null){try { + in.close(); + } catch (IOException e) { + logger.error("",e); + }} + if(process!= null)process.destroy(); + process = null; + } + + /*if(state==-1){ + isSuccess = false; + }else{ + isSuccess = true; + }*/ + return datas2; + } + + //收集数据:DC端主动收集数据(监测数据、任务结果、回传文件) + private void collectDatas(){ + int zipDetect = 0; + int csvDetect = 0; + int zipTaskResult = 0; + int objTaskResult = 0; + int zipTaskReturn = 0; + int fileTaskReturn = 0; + + try { + //- 非升级操作判断 + if(Common.SERVER_UN_UPGRADE_FLAG){ + this.createClientSocket(); + sendMessage(SocketCMD.REQ_DATA_COLLECT);//DC:要进行数据的收集(监测数据、任务结果、回传文件) + String msg0 = receiveMessage();//Client:发送数据文件类型(监测数据、任务结果、回传文件的zip、csv或者list),或者没有数据,结束通信,或者关闭socket,则会收到null + + while(true) { + logger.info("数据收集,client端"+this.ip+"回应信息:"+msg0); + + //监测数据---start + if(SocketCMD.DETECT_DATA_ZIP_TYPE.equalsIgnoreCase(msg0)) { + logger.info("数据类型:监测数据的zip文件"); + this.sendMessage(SUCCESS);//DC告诉client,我知道你要发送zip类型的数据了 + String uploadPath = Constants.ZIP_FILE_DETECT_DATA_DIR; + String fileName = receiveMessage();//client发送zip文件的名称 + this.sendMessage(SUCCESS);//DC收到zip文件的名称,给client回成功信息 + bpReceiveFile(uploadPath+File.separator+fileName);//client收到zip文件名发送成功后,发送zip文件,DC接收client发送的zip文件 + this.sendMessage(SUCCESS);//DC接收完client发送的zip文件存入指定路径后,给client回接收成功信息 + + //启动并注册 解压文件线程 + Future future = Common.getFutureMap().get(Constants.UNZIP_FILE_MANAGER); + if(future == null){ + future = Common.scheduled.scheduleWithFixedDelay(new UnZipManagerThread(), 0, 30, TimeUnit.SECONDS); + Common.registRunnable(Constants.UNZIP_FILE_MANAGER, future); + } + zipDetect++; + msg0 = receiveMessage();//Client:再次发送数据文件类型,或者没有数据,结束通信 + + } + else if(SocketCMD.DETECT_DATA_CSV_TYPE.equalsIgnoreCase(msg0)) { + logger.info("数据类型:监测数据的csv文件"); + sendMessage(SUCCESS);//DC告诉client,我知道你要发送csv类型的数据了,client端就开始发送csv文件 + LinkedList bslist = receiveFileBytesByBath();//DC端就准备接收csv文件:可能是多个csv文件 + if(bslist!=null && bslist.size()>0){ + Common.addAllDeteDataList(bslist); + }else{ + logger.debug("接收 监测数据 文件数:0"); + } + sendMessage(SUCCESS);//DC端csv文件接收完毕后,告诉client端,文件接收完毕 + csvDetect++; + msg0 = receiveMessage();//Client:再次发送数据文件类型,或者没有数据,结束通信 + + } + //监测数据---end + //任务结果---start + else if(SocketCMD.TASK_RESULT_ZIP_TYPE.equalsIgnoreCase(msg0)) { + logger.info("数据类型:任务结果的zip文件"); + this.sendMessage(SUCCESS);//DC告诉client,我知道你要发送zip类型的数据了 + String uploadPath = Constants.ZIP_FILE_TASK_RESULT_DIR; + String fileName = receiveMessage();//client发送zip文件的名称 + this.sendMessage(SUCCESS);//DC收到zip文件的名称,给client回成功信息 + bpReceiveFile(uploadPath+File.separator+fileName);//client收到zip文件名发送成功后,发送zip文件,DC接收client发送的zip文件 + this.sendMessage(SUCCESS);//DC接收完client发送的zip文件存入指定路径后,给client回接收成功信息 + + //启动并注册 解压文件线程 + Future future = Common.getFutureMap().get(Constants.UNZIP_FILE_MANAGER); + if(future == null){ + future = Common.scheduled.scheduleWithFixedDelay(new UnZipManagerThread(), 0, 30, TimeUnit.SECONDS); + Common.registRunnable(Constants.UNZIP_FILE_MANAGER, future); + } + zipTaskResult++; + msg0 = receiveMessage();//Client:再次发送数据文件类型,或者没有数据,结束通信 + + } + else if(SocketCMD.TASK_RESULT_OBJ_TYPE.equalsIgnoreCase(msg0)){//-- 接收返回的任务结果:修改为receiveObject + logger.info("数据类型:任务结果的List"); + sendMessage(SUCCESS); + List resultList = (List)receiveObject(); + sendMessage(SUCCESS); + for (String result: resultList) { + logger.info("任务结果:"+result); + String[] misRlt2 = result.split(Constants.COMMON_DATA_SPLIT); + MissionResult2 result2 = Common.resoveMissionResult(misRlt2); + Common.addResultToResultList(result2);//没有addAll,因为传递的任务结果可能是不同类型的任务结果(1-6) + } + objTaskResult++; + msg0 = receiveMessage();//Client:再次发送数据文件类型,或者没有数据,结束通信 + } + //任务结果---end + //回传文件---start + else if(SocketCMD.TASK_RETURN_ZIP_TYPE.equalsIgnoreCase(msg0)) { + logger.info("数据类型:回传文件的zip文件"); + this.sendMessage(SUCCESS);//DC告诉client,我知道你要发送zip类型的数据了 + String uploadPath = Constants.ZIP_FILE_TASK_RETURN_DIR; + String fileName = receiveMessage();//client发送zip文件的名称 + this.sendMessage(SUCCESS);//DC收到zip文件的名称,给client回成功信息 + bpReceiveFile(uploadPath+File.separator+fileName);//client收到zip文件名发送成功后,发送zip文件,DC接收client发送的zip文件 + this.sendMessage(SUCCESS);//DC接收完client发送的zip文件存入指定路径后,给client回接收成功信息 + + //启动并注册 解压文件线程 + Future future = Common.getFutureMap().get(Constants.UNZIP_FILE_MANAGER); + if(future == null){ + future = Common.scheduled.scheduleWithFixedDelay(new UnZipManagerThread(), 0, 30, TimeUnit.SECONDS); + Common.registRunnable(Constants.UNZIP_FILE_MANAGER, future); + } + zipTaskReturn++; + msg0 = receiveMessage();//Client:再次发送数据文件类型,或者没有数据,结束通信 + + } + else if(SocketCMD.TASK_RETURN_FILE_TYPE.equalsIgnoreCase(msg0)){//-- 接收任务4“启动”的回传文件 + + sendMessage(SUCCESS); + + String msg1 = receiveMessage(); + sendMessage(SUCCESS); + + logger.info(msg1); + String [] result = msg1.split(Constants.COMMON_DATA_SPLIT); + MissionResult2 result2 = Common.resoveMissionResult(result); + String fileName = receiveMessage(); + sendMessage(SUCCESS); + logger.debug("MISSION_FILE_UPLOAD_DIR"+Constants.MISSION_FILE_UPLOAD_DIR); + + this.bpReceiveFile(Constants.MISSION_FILE_UPLOAD_DIR+fileName); + + sendMessage(SUCCESS); + logger.debug("MISSION_FILE_UPLOAD_DIR"+Constants.MISSION_FILE_UPLOAD_DIR+fileName); + + result2.setFileInfo(fileName); + Common.addResultToResultList(result2); + fileTaskReturn++; + + msg0 = receiveMessage();//Client:再次发送数据文件类型,或者没有数据,结束通信 + } + //回传文件---end + else {//如果没有这个else,且对方发送的命令非以上6种,则会死循环 + logger.info("数据收集线程:与client端"+this.ip+"的通信结束"); + break; + } + } + + logger.info("client端"+this.ip+",监测数据zip文件发送次数"+zipDetect);//每次可以发送多个zip + logger.info("client端"+this.ip+",监测数据csv文件发送次数"+csvDetect);//每次可以发送多个csv + logger.info("client端"+this.ip+",任务结果zip文件发送次数"+zipTaskResult);//每次可以发送多个 + logger.info("client端"+this.ip+",任务结果obj文件发送次数"+objTaskResult);//每次可以发送多个 + logger.info("client端"+this.ip+",回传文件zip文件发送次数"+zipTaskReturn);//每次可以发送多个 + logger.info("client端"+this.ip+",回传文件file文件发送次数"+fileTaskReturn);//每次可以发送多个 + + } + + } catch (Exception e) { + logger.error("The DC side actively collects data (monitoring data, task results, return files) exceptions",e); + } + } + +// public void run() { +// +// Thread.currentThread().setName(name+" "+ip); +// +// try { +// +// Calendar checkTime = Calendar.getInstance(); +// Calendar nextTime = Calendar.getInstance(); +// nextTime.add(Calendar.MINUTE, Long.valueOf(gap).intValue()); +// +// List datas = new ArrayList(); //拼写数据格式 已安全进入公共数据解析入口 +// +// //--初始部分数据整理 +// datas.add(Common.getIpSeqIdMap().get(ip)+"");//IP, +// datas.add(sitInfoId+"");//监测设置ID, +// datas.add(checkTypeName);//监测类别, +// datas.add(processIden);//进程名称(检查类别设置名称), +// +// String stateInfo =""; +// int state = -1;//测试使用 +// String[] datas2 = new String[6]; +// int a = 0; +// String [] msgArr = null; +// +// loop: +// for(int i = 0 ; i < maxTimes ;i++){ +// datas2[3] = Calendar.getInstance().getTimeInMillis()+""; +// msgArr = this.sockeHandshake(); +// a = a+1; +// datas2[4] = Calendar.getInstance().getTimeInMillis()+""; +// if("1".equals(msgArr[0])){ //成功 +// break loop; +// }else{ //失败 +// continue loop; +// } +// } +// datas2[0] = SocketCMD.CLIENT_REQ_HAND_SHAKE; +// datas2[1] = msgArr[1]; +// stateInfo += "开始"+format.format(new Date(Long.parseLong(datas2[3])))+ " NMSClient握手"; +// if("1".equals(msgArr[0])){ //成功 +// if(msgArr[1] != null && !"".equals(msgArr[1])){ +// String mreturn[] = msgArr[1].split("\\|"); +// if(mreturn != null && mreturn.length >= 2){ +// datas.add(mreturn[1]);//监测服务启动时间, +// } +// } +// stateInfo += format.format(new Date(Long.parseLong(datas2[4]))) + " 成功 "; +// datas2[2] = "0"; +// }else{ //失败 +// datas.add(startTime+"");//监测服务启动时间, +// stateInfo += format.format(new Date(Long.parseLong(datas2[4]))) + " 失败 "; +// datas2[2] = "1"; +// String [] pingData = pingHandshake(); +// +// if ((StringUtils.isEmpty(pingData[1]) || "0".equals(pingData[1])) ) { +// stateInfo = "网络异常"; +// } else { +//// stateInfo = "请检查目标主机NMSClient是否启动"; +// } +// } +// datas2[5] = DateUtil.getSecondsFromBeinToEnd(new Date(Long +// .parseLong(datas2[3].toString())).getTime(), new Date(Long +// .parseLong(datas2[4].toString())).getTime()) +// + ""; +// state = 0; +// +// +// datas.add("0");//检测时延(秒) +// datas.add(checkTime.getTimeInMillis()+"");//本次检测时间, +// +// if(state!=-1){ +// datas.add(a+"");// 尝试次数,+"/"+maxTimes +// datas.add(nextTime.getTimeInMillis()+"");// 下次计划监测时间, +// String [] pArray = getAlarmState(Common.getAlarmInfoMap().get(sitInfoId.longValue()+""), datas2); +// if("1".equals(msgArr[0])){ +// datas.add("1"); // 执行状态 +// datas.add(""); // 告警列序号, +// datas.add(""); // 告警级别, +// datas.add(""); // 告警值, +// }else { +// datas.add("0"); // 执行状态 +// datas.add(pArray==null?"":pArray[0]); // 告警列序号, +// datas.add(pArray==null?"":pArray[1]); // 告警级别, +// datas.add(pArray==null?"":pArray[2]); // 告警值, +// } +// datas.add(stateInfo); // 状态信息(描述信息), +// datas.add(stateInfo); // 性能数据, +// datas.addAll(Arrays.asList(datas2));//监测具体数据信息(每个字段一列) +// } else {//测试告警的代码,无用 +// datas.add(a+"");// 尝试次数,+"/"+maxTimes +// datas.add(nextTime.getTimeInMillis()+"");// 下次计划监测时间, +// datas.add("-1"); // 执行状态 +// datas.add("3"); // 告警列序号,(丢包数 列 序号 为3) +// datas.add("1"); // 告警级别, +// datas.add("1"); // 告警值, +// datas.add(stateInfo); // 状态信息(描述信息), +// datas.add(stateInfo); // 性能数据, +// } +// datas.add(checkTime.getTimeInMillis()+"");//本次检测时间, +// +// //- 存入缓存 +// try { +// logger.debug("data:"+Arrays.toString(datas.toArray())); +// Common.addDeteData(CSVUtils.csvBytesPrinter(datas, Constants.COMMON_TEXT_CODING)); +// logger.debug("监测数据以 CSV格式 保存完成"); +// } catch (Exception e) { +// logger.error("监测数据以 CSV格式 保存异常", e); +// } +// } catch (Exception ex) { +// logger.error(ExceptionPrintUtils.printExceptionStack(ex)); +// String[] alarm = Common.alarmExceptionInfo(sitInfoId, ip, Constants.DETEC_NMSC_STR, processIden, startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); +// Common.addAlarmData(alarm); +// }finally{ +// if(downLatch!=null){ +// downLatch.countDown(); +// } +// } +// } +// +// private String[] sockeHandshake(){//此方法无用了 +// String [] msgArr = new String[2]; +// msgArr[0] = 0+""; +// +// //set up a connection +// SSLSocketFactory ssf = null; +// try { +// //-- create SocketFactory +// ssf = sSLContext.getSocketFactory(); +// +// //-- create socket +// socket=(SSLSocket)ssf.createSocket(ip,port); +// logger.debug("create socket success."); +// socket.setSoTimeout((int)3000); +// //-- handshake 握手 +// ((SSLSocket) socket).startHandshake(); +// logger.debug("handshake success."); +// +// outl = new OutputStreamLimiter(socket.getOutputStream()); +// inl = new InputStreamLimiter(socket.getInputStream()); +// +// sendMessage(SocketCMD.CLIENT_REQ_HAND_SHAKE); +// msgArr[1] = receiveMessage(); +// +// msgArr[0] = 1+""; +// }catch (IOException e) { +// logger.error("",e); +// } catch (Exception e) { +// logger.error("",e); +// }finally{ +// close(); +// } +// +// return msgArr; +// } + + + + + //获得当前的报警设置信息,与相应字段值比较 +// private String[] getAlarmState(List alarmInfos, String[] sysData){ +// String[] strs = null; +// if(alarmInfos!=null){ +// strs = new String[]{"", "", ""}; +// for(AlarmInfo alarm : alarmInfos){ +// if(sysData.length>=alarm.getShowNum()){ +// Double data = Math.abs(Double.parseDouble(sysData[alarm.getShowNum()-1])); +// if("equal".equals(alarm.getPoliceSysmbols())){ +// if((data+"").equals(alarm.getPoliceValue())){ +// strs[0] += alarm.getShowNum() + "|";// 告警序列号 +// strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 +// strs[2] += alarm.getPoliceValue() + "|"; //告警值 +// } +// } +// else if("include".equals(alarm.getPoliceSysmbols())){ +// if((data+"").indexOf(alarm.getPoliceValue())!=-1){ +// strs[0] += alarm.getShowNum() + "|";// 告警序列号 +// strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 +// strs[2] += alarm.getPoliceValue() + "|"; //告警值 +// } +// } +// else if("exclude".equals(alarm.getPoliceSysmbols())){ +// if((data+"").indexOf(alarm.getPoliceValue())==-1){ +// strs[0] += alarm.getShowNum() + "|";// 告警序列号 +// strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 +// strs[2] += alarm.getPoliceValue() + "|"; //告警值 +// } +// } +// else{ +// double result = data - Double.parseDouble(alarm.getPoliceValue()); +// if ((">".equals(alarm.getPoliceSysmbols()) && result > 0) +// || ("<".equals(alarm.getPoliceSysmbols()) && result < 0) +// || ("=".equals(alarm.getPoliceSysmbols()) && result == 0) +// || (">=".equals(alarm.getPoliceSysmbols()) && result >= 0) +// || ("<=".equals(alarm.getPoliceSysmbols()) && result <=0) ) { +// strs[0] += alarm.getShowNum() + "|";// 告警序列号 +// strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 +// strs[2] += alarm.getPoliceValue() + "|"; //告警值 +// } +// } +// } +// } +// for(int i=0; i0){ +// strs[i] = strs[i].substring(0, strs[i].length()-1); +// } +// } +// } +// +// if(strs!=null && "".equals(strs[0])){ +// strs = null; +// } +// +// return strs; +// } + + + //main测试使用的构造方法 + public NMSClientForSocketThread(String name,Long sitInfoId,String socketIp,int socketPort,long gap,long outTime,long maxTimes) { + super(socketIp, socketPort); + this.name = name; + this.sitInfoId = sitInfoId; + this.outTime = outTime; + this.maxTimes = maxTimes; + this.gap = gap; + + } + + public static void main(String [] args) { + Thread thread = new Thread(new NMSClientForSocketThread("NMSC Thread",10l,"10.0.6.102",60701,2,100,3)); +// String name,Long sitInfoId,String socketIp,int socketPort,long gap,long outTime,long maxTimes + thread.start(); + } + +} diff --git a/src/com/nms/server/thread/monitor/PingThread.java b/src/com/nms/server/thread/monitor/PingThread.java new file mode 100644 index 0000000..e050b0f --- /dev/null +++ b/src/com/nms/server/thread/monitor/PingThread.java @@ -0,0 +1,354 @@ +package com.nms.server.thread.monitor; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.SetInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.CSVUtils; +import com.nms.server.util.ExceptionPrintUtils; + +/** + * 监测数据 Ping数据获取执行线程 + * + * @date Jul 18, 2011 5:32:44 PM + * @author ZhangGang + * + */ +public class PingThread implements Runnable { + private Logger logger = Logger.getLogger(PingThread.class); + private String name; + private String pingIp = ""; + private long startTime = Calendar.getInstance().getTimeInMillis(); + private String checkTypeName = null; + private Long sitInfoId = null; + private long outTime = 2; + private long maxTimes = 3; + private long gap = 0; + private CountDownLatch downLatch; + /** + * 构造方法 + * @param name 线程名称 + * @param ip 被监测端IP + * @param port socket通信端口 + * @param info + */ + public PingThread(String name,String ip,SetInfo info,Long startTime,CountDownLatch downLatch) { + this.name = name; + this.pingIp = ip; + this.sitInfoId = info.getId(); + this.checkTypeName = info.getCheckTypeName(); + this.startTime = startTime; + this.outTime = info.getCheckOutTime(); + this.maxTimes = info.getCheckMaxTimes(); + this.gap = info.getCheckGap(); + this.downLatch = downLatch; + } + + public PingThread(String name,String ip,SetInfo info,Long startTime) { + this.name = name; + this.pingIp = ip; + this.sitInfoId = info.getId(); + this.checkTypeName = info.getCheckTypeName(); + this.startTime = startTime; + this.outTime = info.getCheckOutTime(); + this.maxTimes = info.getCheckMaxTimes(); + this.gap = info.getCheckGap(); + this.downLatch = downLatch; + } + /** + * 构造方法 + * + * @param name + * 线程名称 + * @param pingIp + * 被监测端IP + * @param sitInfoId + * 设置信息ID + * @param outTime + * 超时时间 + * @param maxTimes + * 最大尝试次数 + */ + /*public PingThread(String name, Long sitInfoId, String pingIp, long gap, + long outTime, long maxTimes) { + this.name = name; + this.pingIp = pingIp; + this.sitInfoId = sitInfoId; + this.outTime = outTime; + this.maxTimes = maxTimes; + this.gap = gap; + } +*/ + @Override + public void run() { + Thread.currentThread().setName(name + " " + pingIp); + BufferedReader in = null; // 读取 Ping命令返回的信息 + + try { + + StringBuffer buffer = new StringBuffer(); + logger.debug("Ping 监测获取开始"); + Calendar checkTime = Calendar.getInstance(); + Calendar nextTime = Calendar.getInstance(); + nextTime.add(Calendar.MINUTE, Long.valueOf(gap).intValue()); + List datas = new ArrayList(); // 拼写数据格式 + // --初始部分数据整理 + datas.add(Common.getIpSeqIdMap().get(pingIp)+"");// IP, + datas.add(sitInfoId + "");// 监测设置ID, + datas.add(checkTypeName);// 监测类别, + datas.add("");// 进程名称(检查类别设置名称), + datas.add(startTime + "");// 监测服务启动时间, + datas.add("0");// 检测时延(秒) + datas.add(checkTime.getTimeInMillis() + "");// 本次检测时间, + String command = ""; // 命令语句 + int minTime = Integer.MAX_VALUE, maxTime = 0, totalTime = 0, curTime; // 最短时间,最长时间,总时间,时间变量 + int snum = 0, fnum = 0; // 发包成功和失败数 + // 判断系统类型 win or Linux + String system = (String) (System.getProperty("os.name")) + .toLowerCase(); + if (system.toLowerCase().indexOf("win") != -1) { + command += "ping -n " + maxTimes + " -w " + outTime * 1000 + + " " + pingIp; + } else if (system.toLowerCase().indexOf("linux") != -1) { + command += "ping -c " + maxTimes + " -w " + outTime + + " " + pingIp; + } else { + command += "ping -w 4 " + pingIp; + } + + String stateInfo = ""; + int state = -1; + String[] datas2 = new String[7]; + Process process = null; + try { + process = Runtime.getRuntime().exec(command); + in = new BufferedReader(new InputStreamReader(process + .getInputStream())); + String line = null; + long count = maxTimes + 10; + int index; + // 最多只读10行 + while ((line = in.readLine()) != null && count != 0) { + if ("".equals(line)) { + continue; + } // 空串跳过 + //System.out.println("-- "+line); + buffer.append(line + "\n"); + line = line.toLowerCase(); + // System.out.println(pingIp+"> line === "+line); + if (line.indexOf("ttl") > 0) { // 获得成功响应的数据 + count--; // 计数器自减1 + // 截取time 值 + if ((index = line.lastIndexOf("ms")) != -1) { + byte[] buf = line.getBytes(); + int start = 0, end = buf.length, i, j; + // 下标末点 + for (i = index; i >= 0; i--) { + if (Character.isDigit((char) buf[i])) { + end = i; + break; + } + } + if (i == 0) + continue; + // 下标起点 + for (j = end; j >= 0; j--) { + if (!Character.isDigit((char) buf[j])) { + start = j + 1; + break; + } + } + // 时间值截取 + curTime = Integer.parseInt(new String(buf, start, + end + 1 - start)); + snum++; // 成功接收次数加1 + totalTime += curTime; // 计算总时间 + if (curTime < minTime) { + minTime = curTime; + } // 最小时间 + if (curTime > maxTime) { + maxTime = curTime; + } // 最大时间 + } + } else if (line.split(" ").length < 4) { + count--; // 计数器自减1 + fnum++; // 失败接收次数加1 + } else { + stateInfo += line + "\n"; + } + } + if (totalTime == 0) { + minTime = 0; + } + // 已发送包数 + datas2[0] = "" + (snum + fnum); + datas2[1] = "" + snum; + datas2[2] = "" + fnum; + if (snum == 0 && fnum == 0) { + state = -1; + fnum = 1; + } else { + state = 0; + } + BigDecimal fnum0 = new BigDecimal(fnum); + BigDecimal tnum0 = new BigDecimal(snum + fnum); + DecimalFormat df = new DecimalFormat("####0.00"); + datas2[3] = "" + + ((new BigDecimal(df.format(fnum0.divide(tnum0,2,RoundingMode.HALF_UP)))) + .multiply(new BigDecimal(100))); + datas2[4] = "" + minTime; + datas2[5] = "" + maxTime; + datas2[6] = "" + (snum == 0 ? 0 : (totalTime / snum)); + } catch (Exception e) { + state = -1; + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + }finally{ + if(in!=null){try { + in.close(); + } catch (IOException e) { + logger.error("",e); + }} + if(process!= null)process.destroy(); + process = null; + } + + if (state != -1) { + datas.add((snum + fnum) + "");// 尝试次数, + datas.add(nextTime.getTimeInMillis() + "");// 下次计划监测时间, + // -- 执行状态是否成功,记录的状态是否正常(用于报警), + String[] pArray = getAlarmState(Common.getAlarmInfoMap().get( + sitInfoId.longValue() + ""), datas2); + + if (pArray == null && snum > 0) { + datas.add("1"); // 执行状态 正常 + datas.add(""); // 告警列序号, + datas.add(""); // 告警级别, + datas.add(""); // 告警值, + } else { + datas.add("0"); // 执行状态 非正常 + datas.add(pArray == null ? "" : pArray[0]); // 告警列序号, + datas.add(pArray == null ? "" : pArray[1]); // 告警级别, + datas.add(pArray == null ? "" : pArray[2]); // 告警值, + } + if(snum == 0){ +// stateInfo = "目标主机ping失败 \n"+stateInfo; +// stateInfo = "Failed to ping the target host \n"+stateInfo; + stateInfo = "i18n_server.PingThread.ping_n81i \n"+stateInfo; + } + datas.add(stateInfo); // 状态信息(描述信息), + datas.add(stateInfo); // 性能数据, + datas.addAll(Arrays.asList(datas2));// 监测具体数据信息(每个字段一列) + } else { + datas.add((snum + fnum) + ""); // 尝试次数, + datas.add(nextTime.getTimeInMillis() + "");// 下次计划监测时间, + datas.add("-1"); // 执行状态 + datas.add(""); // 告警列序号, + datas.add(""); // 告警级别, + datas.add(""); // 告警值, + datas.add(stateInfo); // 状态信息(描述信息), + datas.add(stateInfo); // 性能数据, + } + //- 存入缓存 + try { + Common.addDeteData(CSVUtils.csvBytesPrinter(datas, Constants.COMMON_TEXT_CODING)); + logger.debug("监测数据以 CSV格式 保存完成"); + } catch (Exception e) { + logger.error("Monitoring data to save exceptions in CSV format", e); + } + + } catch (Exception ex) { + logger.error("",ex); + String[] alarm = Common.alarmExceptionInfo(sitInfoId, pingIp, Constants.DETEC_PING_STR, "", startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); + Common.addAlarmData(alarm); + } finally { + if(downLatch != null){ + downLatch.countDown(); + } + try { + if (in != null) + in.close(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + } + + // 获得当前的报警设置信息,与相应字段值比较 + private String[] getAlarmState(List alarmInfos, String[] sysData) { + String[] strs = null; + if (alarmInfos != null) { + strs = new String[] { "", "", "" }; + for (AlarmInfo alarm : alarmInfos) { + if (sysData.length >= alarm.getShowNum()) { + Double data = Math.abs(Double.parseDouble(sysData[alarm + .getShowNum() - 1])); + if("equal".equals(alarm.getPoliceSysmbols())){ + if((data+"").equals(alarm.getPoliceValue())){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else if("include".equals(alarm.getPoliceSysmbols())){ + if((data+"").indexOf(alarm.getPoliceValue())!=-1){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else if("exclude".equals(alarm.getPoliceSysmbols())){ + if((data+"").indexOf(alarm.getPoliceValue())==-1){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + } + } + else{ + double result = data - Double.parseDouble(alarm.getPoliceValue()); + if ((">".equals(alarm.getPoliceSysmbols()) && result > 0) + || ("<".equals(alarm.getPoliceSysmbols()) && result < 0) + || ("=".equals(alarm.getPoliceSysmbols()) && result == 0) + || (">=".equals(alarm.getPoliceSysmbols()) && result >= 0) + || ("<=".equals(alarm.getPoliceSysmbols()) && result <= 0)) { + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; // 告警级别 + strs[2] += alarm.getPoliceValue() + "|"; // 告警值 + } + } + } + } + for (int i = 0; i < strs.length; i++) { + if (strs[i].length() > 0) { + strs[i] = strs[i].substring(0, strs[i].length() - 1); + } + } + } + + if (strs != null && "".equals(strs[0])) { + strs = null; + } + + return strs; + } + +/* public static void main(String [] args) { + Thread thread = new Thread(new PingThread("Ping Thread",10l,"10.0.6.11",2,100,3)); +// String name,Long sitInfoId,String socketIp,int socketPort,long gap,long outTime,long maxTimes + thread.start(); + + }*/ +} diff --git a/src/com/nms/server/thread/monitor/SNMP4JThread.java b/src/com/nms/server/thread/monitor/SNMP4JThread.java new file mode 100644 index 0000000..7b376c5 --- /dev/null +++ b/src/com/nms/server/thread/monitor/SNMP4JThread.java @@ -0,0 +1,229 @@ +package com.nms.server.thread.monitor; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.CountDownLatch; + +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.snmp4j.mp.SnmpConstants; + +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.SetInfo; +import com.nms.server.bean.TableColumnsModel; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.CSVUtils; +import com.nms.server.util.SNMP4JUtils; + +public class SNMP4JThread implements Runnable{ + private Logger logger = Logger.getLogger(SNMP4JThread.class); + private Long startTime = Calendar.getInstance().getTimeInMillis(); +// private SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + private String name; + private int port = Constants.SNMP_CLIENT_PORT; + private SetInfo info ; + private String hostIp; + private CountDownLatch downLatch; + /** + * 构造方法 + * @param name 线程名称 + * @param host 目标Ip + * @param info 监测设置信息 + */ + public SNMP4JThread(String name, String hostIp, SetInfo info,Long startTime,CountDownLatch downLatch) { + this.name = name; + this.hostIp = hostIp; + this.info = info; + this.startTime = startTime; + this.downLatch = downLatch; + } + public SNMP4JThread(String name, String hostIp, SetInfo info,Long startTime) { + this.name = name; + this.hostIp = hostIp; + this.info = info; + this.startTime = startTime; + this.downLatch = downLatch; + } + /* + * 通过SNMP4J GET获取 + */ + public void run() { + Thread.currentThread().setName(name + " " + hostIp); + + try { + + //- 通过 CloumnsMap 获取 oids + logger.debug("json setinfo:"+JSONObject.fromObject(info).toString()); + Map CloumnsMap = Common.getTableMap().get(info.getCheckTypeName()); + logger.debug("table CloumnsMap:"+CloumnsMap.size()); + List oidList = new LinkedList(); + if(CloumnsMap!=null && CloumnsMap.size()>0){ + Iterator> entryIte = CloumnsMap.entrySet().iterator(); + while (entryIte.hasNext()) { + Entry cloumnEntry = (Entry) entryIte.next(); + String oid = cloumnEntry.getValue().getOid(); + if (StringUtils.isNotEmpty(oid)){ + oidList.add(oid); + } + } + } + logger.debug("table CloumnsMap:"+Arrays.toString(oidList.toArray())); + + + int version = Common.getSnmpVerByUUID(Common.getIpSeqIdMap().get(hostIp)); + + //-- 执行和解析 即获取详细数据 + int retries = info.getCheckMaxTimes()==null?3:info.getCheckMaxTimes().intValue(); + int timeout = info.getCheckOutTime() == null?10000:info.getCheckOutTime().intValue()*1000; + int state = -1; + + List> snmpDatas = new LinkedList>() ; + StringBuffer stateInfo = new StringBuffer(); + List> snmpDatasList = null; + try { + String rootOid = Common.getInsertTable().get(info.getCheckTypeName()).getOid(); + logger.debug("rootOid "+rootOid); + snmpDatasList = resoveVersion(snmpDatas,oidList,rootOid,version,retries,timeout,stateInfo); + state = 1; + } catch (Exception e) { + state = -1; + stateInfo.append(e.getMessage()); + logger.error((version == SnmpConstants.version3?"version3":"version2c")+" GET failure", e); + } + + //- 监测数据集合 + List pubDatas = new ArrayList(); // 拼写数据格式 + + //- DetecDatas 拼写 Header + pubDatas.add(Common.getIpSeqIdMap().get(hostIp)+"");// uuid, + pubDatas.add(info.getId() + "");// 监测设置ID, + pubDatas.add(info.getCheckTypeName());// 监测类别, + pubDatas.add(info.getProcessIden());// 进程名称(检查类别设置名称), + pubDatas.add(startTime + "");// 监测服务启动时间, + pubDatas.add(timeout+"");// 检测时延(秒) + //- 当前检测时间 + Calendar checkTime = Calendar.getInstance(); + pubDatas.add(checkTime.getTimeInMillis() + "");// 本次检测时间, + pubDatas.add(retries + "");// 尝试次数, + //- 下次检测时间 + Calendar nextTime = Calendar.getInstance(); + nextTime.add(Calendar.MINUTE, info.getCheckGap().intValue()); + pubDatas.add(nextTime.getTimeInMillis() + "");// 下次计划监测时间, + + + + //- DetecDatas 拼写 Body + List> pubDatasList = new ArrayList>(); + if(state != -1){ + pubDatasList = datasBody(retries,stateInfo.toString(),state,snmpDatasList); + } + + pubDatas.add(state+""); // 执行状态 + pubDatas.add(stateInfo.toString()); // 状态信息(描述信息), + pubDatas.add(""); // 性能数据, + + if(pubDatasList.size()>0){ + pubDatasList.add(0,Arrays.asList(new String[]{"details",pubDatasList.size()+""})); + } + pubDatasList.add(0,pubDatas); + + //- 存入缓存 + if(pubDatasList!=null && pubDatasList.size()>0){ + try { + Common.addDeteData(CSVUtils.csvFilePrinter(pubDatasList, Constants.COMMON_TEXT_CODING)); + logger.debug("监测数据以 CSV格式 保存完成"); + } catch (Exception e) { + logger.error("Monitoring data to save exceptions in CSV format", e); + } + } + + } catch (Exception e) { + String[] alarm = Common.alarmExceptionInfo(info.getId(), hostIp, info.getCheckTypeName(), info.getProcessIden(), startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, e.getMessage()); + Common.addAlarmData(alarm); + logger.error("Program unknown error ", e); + }finally{ + if(downLatch != null){ + downLatch.countDown(); + } + } + } + + private List> resoveVersion(List> datasMapList,List oids,String oid,int version ,int retries,int timeout,StringBuffer stateInfo) throws Exception{ + + List> snmpDatasList = new LinkedList>(); + logger.debug("SNMP4J 》》》》"+oid+" "+hostIp+" "+port+" "+Constants.SNMP_COMMUNITY+" "+retries+" "+timeout+" "+ version); + + + SNMP4JUtils.snmpTree(datasMapList, oids,oid, hostIp, port+"", Constants.SNMP_COMMUNITY,retries,timeout,version,info.getCheckTypeName()); + logger.debug("datasMapList size "+datasMapList.size()); + //- 获取表字段结构 + Map cloumnsList = Common.getTableMap().get(info.getCheckTypeName()); + logger.debug(info.getCheckTypeName()+" cloumnsList size "+cloumnsList.size()); + + //- 存值 + if(cloumnsList!=null && cloumnsList.size() > 0 && datasMapList!=null && datasMapList.size()>0){ + for(Map datasMap : datasMapList){ + List snmpDatas = new LinkedList(); + for (int i = 1; i <= cloumnsList.size(); i++) { + TableColumnsModel cloumn = cloumnsList.get(i); + String oid0 = cloumn.getOid(); + snmpDatas.add(datasMap.get(oid0)); +// if(oid0.equals(oid+".1")){ +// stateInfo.append(oid0+":"+datasMap.get(oid0)+"\n"); +// } + } + snmpDatasList.add(snmpDatas); + } + } + return snmpDatasList; + } + + private List> datasBody(int times,String stateInfo,int state,List> snmpDatas2)throws Exception{ + +// List> pubDatasList = new LinkedList>(); + + logger.debug("监测数据主体拼写 state "+state+" size"+(snmpDatas2==null ? -1 :snmpDatas2.size())); + + if(snmpDatas2!=null && snmpDatas2.size()!=0){ + for (List snmpDatas : snmpDatas2) { +// List pubDatas0 = new LinkedList(); +// 执行状态是否成功是否正常, 第三方脚本监测任务执行结果(用于报警 -1执行失败 0不正常,1正常)-1时可保留后续数据为空,但是数据个数需要对应 + // -- 执行状态是否成功,记录的状态是否正常(用于报警), + String [] datas0 = new String[0]; + datas0 = snmpDatas.toArray(datas0); +// logger.debug("snmpDatas>>>> "+Arrays.toString(snmpDatas.toArray(datas0))); +// logger.debug("Common.getAlarmInfoMap()>>>> "+Common.getAlarmInfoMap().size()); + String[] pArray = Common.getAlarmState(Common.getAlarmInfoMap().get(info.getId()+""), datas0); +// logger.debug("pArray>>>> "+Arrays.toString(pArray)); + if (pArray == null || pArray.length==0) { + snmpDatas.add(0, ""); + snmpDatas.add(1, ""); + snmpDatas.add(2, ""); + } else { + state = 0; + List aiList = Common.getAlarmInfoMap().get(info.getId()+""); + snmpDatas.add(0, pArray == null ? "" : pArray[0]); + snmpDatas.add(1, pArray == null ? "" : pArray[1]); + snmpDatas.add(2, pArray == null ? "" : pArray[2]); + stateInfo+=Common.getStateInfo(info.getCheckTypeName(),Common.getAlarmInfoMap().get(info.getId()+""), datas0); + } +// logger.debug("pArray>>>> "+Arrays.toString(pubDatas0.toArray())); + } + } + return snmpDatas2; + + } + + public static void main(String [] args) { + + } +} diff --git a/src/com/nms/server/thread/monitor/ServerDateThread.java b/src/com/nms/server/thread/monitor/ServerDateThread.java new file mode 100644 index 0000000..9879849 --- /dev/null +++ b/src/com/nms/server/thread/monitor/ServerDateThread.java @@ -0,0 +1,84 @@ +package com.nms.server.thread.monitor; + +import java.util.Date; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.DetectInfo; +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.thread.monitor.util.MonitorUtil; +import com.nms.server.util.DateUtil; +/** + * 时钟同步监测 + * @author dell + * + */ +public class ServerDateThread implements Runnable{ + private static Logger logger = Logger.getLogger(ServerDateThread.class); + private String name; + private SetInfo setInfo; + private List nodeList; + private Long startTime; + + public ServerDateThread(String name ,SetInfo setInfo,List nodeList,Long startTime) { + this.setInfo = setInfo; + this.name = name; + this.nodeList = nodeList; + this.startTime = startTime; + } + + @Override + public void run() { + Thread.currentThread().setName("ServerDateThread->" + name); + try { + for(NodeModel model : nodeList){ + if(model.getNodeType() != null && model.getNodeType().longValue() == 0l){ //服务器节点 + String nodeIp = model.getNodeIp(); + Boolean online = (Boolean)Common.getHbInfo(nodeIp, "online"); + Long localDate = (Long)Common.getHbInfo(nodeIp, "dcTime"); + Long sysDate = (Long)Common.getHbInfo(nodeIp, "ncTime"); + try { + DetectInfo detectInfo = null; + if(online != null && online && localDate != null && sysDate != null){//有心跳 + //-- 接收系统时间信息 + logger.debug("服务器系统时间:" + sysDate); + //-- systemdate 监测设置 详细数据整理 + long timeDelay = Math.abs(getMinutesFromBeginToEnd(sysDate, localDate)); + //-- 信息保存到监测信息对象中 + detectInfo = new DetectInfo(); + detectInfo.getDetailDatas().add(localDate + MonitorUtil.SEPARATOR + sysDate + MonitorUtil.SEPARATOR + timeDelay); + detectInfo.setDescInfo("server:" + + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS,new Date(localDate)) + + " agent:" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS,new Date(sysDate))); + } + if(detectInfo == null){ + detectInfo = new DetectInfo(); + detectInfo.setDescInfo("i18n_server.SystemDateThread.getSysTimeFail_n81i"); + } + detectInfo.setTestTimes(1); + detectInfo.setCheckTime(System.currentTimeMillis()); + MonitorUtil.createMonitorData(nodeIp, setInfo, startTime, Common.getAlarmInfoMap().get(setInfo.getId()+ ""), detectInfo); + } catch (Exception ex) { + logger.error("Getting the time anomaly of NC system",ex); + String[] alarm = Common.alarmExceptionInfo(setInfo.getId(), nodeIp, Constants.DETEC_SYSTEMDATE_STR, setInfo.getProcessIden(), startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); + Common.addAlarmData(alarm); + } + }else{ //非服务器节点 + logger.info("监测目标:"+model.getNodeIp()+" 是非服务器节点,不进行"+Constants.DETEC_SYSTEMDATE_STR+"监测"); + } + } + } catch (Exception e) { + logger.error("error",e); + } + } + + + public static long getMinutesFromBeginToEnd(long begin, long end) { + return (end - begin) / (60 * 1000); + } + +} diff --git a/src/com/nms/server/thread/monitor/ServerNMSCThread.java b/src/com/nms/server/thread/monitor/ServerNMSCThread.java new file mode 100644 index 0000000..a353c5d --- /dev/null +++ b/src/com/nms/server/thread/monitor/ServerNMSCThread.java @@ -0,0 +1,181 @@ +package com.nms.server.thread.monitor; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.DetectInfo; +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.thread.monitor.util.MonitorUtil; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.DateUtil; +import com.nms.server.util.socket.SocketUtils; + +/** + * 生成 服务器 的 握手监测 + * + * @author dell + * + */ +public class ServerNMSCThread implements Runnable { + private static Logger logger = Logger.getLogger(ServerNMSCThread.class); + private String name; + private SetInfo setInfo; + private List nodeList; + private Long startTime; + + public ServerNMSCThread(String name,SetInfo info,Long startTime,List nodeList) { + this.name = name; + this.setInfo = info; + this.nodeList = nodeList; + this.startTime = startTime; + } + + @Override + public void run() { + Thread.currentThread().setName("ServerNMSCThread->" + name ); + try { + // - 非升级操作判断 + if (Common.SERVER_UN_UPGRADE_FLAG) {// ---起什么作用? + // 循环向自己管辖的Client端,收集数据 + for(NodeModel model:nodeList){ + if(model.getNodeType() != null && model.getNodeType().longValue() == 0l){//服务器节点 + String ip = model.getNodeIp(); + Boolean heartFlag = Common.getHbInfo(ip, "online"); + logger.debug( ip + ",heartbeat: " + heartFlag); + if (heartFlag != null && heartFlag) {// 节点在线 + doNmsClientDetect(ip, setInfo); + } else {// 握手失败,超过 15秒没有心跳,则判定握手失败 + handShakeFail(setInfo, ip); + } + + } + } + } + } catch (Exception e) { + logger.error("", e); + } + } + + /** + * 握手监测失败 + */ + public void handShakeFail(SetInfo setInfo, String ip) { + Long startTime = System.currentTimeMillis(); + String failStartTime = System.currentTimeMillis() + ""; + DetectInfo detectInfo = new DetectInfo(); + String stateInfo = "i18n_server.NoDetectDataCollectManagerThread.start_n81i" + + DateUtil.getStringByLong(startTime) + " i18n_server.NoDetectDataCollectManagerThread.shakehand_n81i" + + DateUtil.getCurrentTime() + " i18n_server.NoDetectDataCollectManagerThread.faild_n81i"; + /*String failEndTime = Calendar.getInstance().getTimeInMillis() + ""; + StringBuffer detailInfo = new StringBuffer(); + detailInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);// 发送信息 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append("");// 返回信息 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append("1");// 是否可达,0:可达,1:不可达 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(failStartTime);// 发送时间 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(failEndTime);// 返回时间 + detailInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + detailInfo.append(DateUtil.getSecondsFromBeinToEnd(new Date(Long.parseLong(failStartTime)).getTime(), + new Date(Long.parseLong(failEndTime)).getTime()) + "");// 延迟时间 + detectInfo.getDetailDatas().add(detailInfo.toString());*/ + detectInfo.setDescInfo(stateInfo); + detectInfo.setTestTimes(1); + detectInfo.setCheckTime(System.currentTimeMillis()); + Common.createNmsClientDetectData(setInfo, ip, detectInfo, startTime);// 生成监测数据信息 + } + + // 收集数据前先进行握手监测通信 + public static void doNmsClientDetect(String ip, SetInfo setInfo) { + logger.info("开始握手监测>" + ip); + DetectInfo detectInfo = null; + try { + long dcTime = System.currentTimeMillis(); + // -- 信息保存到监测信息对象中 + detectInfo = new DetectInfo(); + StringBuffer nmsClientDetailDetectInfo = new StringBuffer(); + nmsClientDetailDetectInfo.append(SocketCMD.CLIENT_REQ_HAND_SHAKE);// 发送信息 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo + .append(SocketUtils.SUCCESS + ":" + SocketCMD.CLIENT_REQ_HAND_SHAKE + "|" + dcTime);// 返回信息:DC端伪造的,因为也不需要NC回复信息了 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append("0");// 是否可达,0:可达,1:不可达 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append(dcTime);// 发送时间 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append(dcTime);// 返回时间 + nmsClientDetailDetectInfo.append(MonitorUtil.SEPARATOR);// 分隔符 + nmsClientDetailDetectInfo.append( + DateUtil.getSecondsFromBeinToEnd(new Date(dcTime).getTime(), new Date(dcTime).getTime()) + "");// 延迟时间 + + detectInfo.getDetailDatas().add(nmsClientDetailDetectInfo.toString()); + detectInfo.setDescInfo("i18n_server.Common.nmsShakeHandBegin_n81i" + + DateUtil.getStringByLongStr(dcTime + "") + " i18n_server.Common.nmsShakeHand_n81i" + + DateUtil.getStringByLongStr(dcTime + "") + " i18n_server.Common.nmsShakeHandSuccess_n81i "); + Common.createNmsClientDetectData(setInfo, ip, detectInfo, System.currentTimeMillis()); + } catch (Exception e) { + logger.error("Generating node:" + ip + "information anomaly", e); + } finally { + } + } + + // 获得进行握手监测的所有节点:握手的条件:到首次监测时间+节点有效(节点组有效)+监测设置内的节点+监测设置有效 + private Map getNmsclientNodeList(CommonService service) { + List nodeList = new ArrayList(); + Map nodeMap = new HashMap(); + try { + if (Constants.FLAG_NMSC == 1) { + List setList = service.getDrivingSetInfo(Constants.DETEC_NMSC_STR);// 获得checkType类型的所有监测设置 + if (setList != null && setList.size() > 0) { + List nodeTmpList = new ArrayList(); + for (Iterator setIte = setList.iterator(); setIte.hasNext();) { + SetInfo setInfo = (SetInfo) setIte.next(); + // 如果首次监测时间没到,则不用查询节点 + // - 计算启动后第一次检测时间 默认即时启动 + long initialDelay = 0l; + if (setInfo.getPlanCheckTime() != null) { + initialDelay = setInfo.getPlanCheckTime().longValue() - (new Date().getTime()); + initialDelay = initialDelay > 0l ? initialDelay : 0l; + } + + if (initialDelay <= 0) {// 到首次监测时间了,查询进行握手监测的节点 + // 节点可能存在重复的情况 + nodeTmpList = service.getNodeModelListBySetInfo(setInfo); + nodeList.addAll(nodeTmpList); + } else { + logger.info("握手监测,未到首次监测时间,距离首次监测时间还有:" + initialDelay / 1000 / 60 + "分钟"); + } + + for (NodeModel node : nodeTmpList) { + nodeMap.put(node.getNodeIp(), setInfo); + } + } + + logger.debug("本次服务器握手监测的节点数为:" + nodeMap.size()); + } + } else { + logger.info("未开启握手监测标识"); + } + } catch (Exception e) { + logger.error("Obtaining a node exception for handshake monitoring", e); + } + return nodeMap; + } + +} diff --git a/src/com/nms/server/thread/monitor/SwitchPortThread.java b/src/com/nms/server/thread/monitor/SwitchPortThread.java new file mode 100644 index 0000000..c59a41b --- /dev/null +++ b/src/com/nms/server/thread/monitor/SwitchPortThread.java @@ -0,0 +1,1048 @@ +package com.nms.server.thread.monitor; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.CountDownLatch; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.snmp4j.mp.SnmpConstants; + +import com.nms.server.bean.SetInfo; +import com.nms.server.bean.TableColumnsModel; +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.thread.monitor.test.DPSwitchProt64Data; +import com.nms.server.util.BoneCPPool; +import com.nms.server.util.CSVUtils; +import com.nms.server.util.MathUtil; +import com.nms.server.util.SNMP4JUtils; + +public class SwitchPortThread implements Runnable{ + private Logger logger = Logger.getLogger(SwitchPortThread.class); + private Long startTime = Calendar.getInstance().getTimeInMillis(); + private String name; + private int port = Constants.SNMP_CLIENT_PORT; + private SetInfo info ; + private String hostIp; + + private List checkTypeList; + private CountDownLatch downLatch; + /** + * 构造方法 + * @param name 线程名称 + * @param host 目标Ip + * @param info 监测设置信息 + */ + public SwitchPortThread(String name, String hostIp, SetInfo info,Long startTime,List checkTypeList,CountDownLatch downLatch) { + this.name = name; + this.hostIp = hostIp; + this.info = info; + this.startTime = startTime; + this.checkTypeList = checkTypeList; + this.downLatch = downLatch; + } + public SwitchPortThread(String name, String hostIp, SetInfo info,Long startTime) { + this.name = name; + this.hostIp = hostIp; + this.info = info; + this.startTime = startTime; + this.checkTypeList = checkTypeList; + this.downLatch = downLatch; + } + public SwitchPortThread() { + + } + + + /* + * 通过SNMP4J GET获取 + */ + public void run() { + Thread.currentThread().setName("交换机监测 " + hostIp); + try { + /* 准备公共信息 */ + + //-- 校验ifXTable 对应名称(忽略大小写处理) + String ifxtableName = Constants.DETEC_IFXTABLE_STR; + if(Common.getCheckTypeNameMape().get(ifxtableName)==null){ + Iterator ite = Common.getCheckTypeNameMape().keySet().iterator(); + while (ite.hasNext()) { + String checkTypeName = (String) ite.next(); + if(ifxtableName.equalsIgnoreCase(checkTypeName)){ + ifxtableName = checkTypeName; + } + } + } + + //- 通过 CloumnsMap 获取 oids + List oidList = getOidListByCheckTypeName(info.getCheckTypeName());//获取 oids + List oidIfXTabList = getOidListByCheckTypeName(ifxtableName);//ifXTable:应该循环checkTypeList列表? + + //- 版本 + int version = Common.getSnmpVerByUUID(Common.getIpSeqIdMap().get(hostIp)); + + //-- 执行和解析 即获取详细数据 + int retries = info.getCheckMaxTimes()==null?3:info.getCheckMaxTimes().intValue(); + int timeout = info.getCheckOutTime() == null?20000:info.getCheckOutTime().intValue()*1000; + int state = -1;//detection_info表的DETECTIONED_STATE状态,-1执行不成功;0不正常;1正常 + + StringBuffer stateInfo = new StringBuffer(); + List> snmpDatasList1 = new LinkedList>();//为了入库,不包含oid,通过get(i)获取 + List> snmpDatasList2 = new LinkedList>(); + List> datasMapList1 = new ArrayList>();//一个端口有多种属性值,一个端口一个map,多个端口map的list:List> + List> datasMapList2 = new ArrayList>();//为了计算,包含oid + String rootOid = Common.getInsertTable().get(info.getCheckTypeName()).getOid(); + logger.info("rootOid :"+rootOid); + //ifXTable + Map> ifXTabdatasMapList1 = new HashMap>(); + Map> ifXTabdatasMapList2 = new HashMap>(); + String ifXTabRootOid = Common.getInsertTable().get(ifxtableName).getOid(); + logger.info("ifXTabRootOid :"+ifXTabRootOid); + Map mapForDPIfXTab = DPSwitchProt64Data.mapForDPIfXTable(); + + /* 执行二次监测 */ + int waitTime = 30; + long stime32 = 0; + long stime64 = 0; + long etime32 = 0; + long etime64 = 0; + boolean dataFlag64 = true; + double value64 = (1d+Long.MAX_VALUE)*2; + double value32 = (1d+Integer.MAX_VALUE)*2; + try { + //第一次取值 + //32位数据 + snmpDatasList1 = resoveVersion(datasMapList1,oidList,rootOid,version,retries,timeout,stateInfo); + stime32 = System.currentTimeMillis(); + //64位数据 + resoveIfXTabVersion(ifXTabdatasMapList1,oidIfXTabList,ifXTabRootOid,version,retries,timeout,stateInfo); + stime64 = System.currentTimeMillis(); + //间隔时间 + Thread.sleep(waitTime*1000); + + //第二次取值 + snmpDatasList2 = resoveVersion(datasMapList2,oidList,rootOid,version,retries,timeout,stateInfo); + etime32 = System.currentTimeMillis(); + resoveIfXTabVersion(ifXTabdatasMapList2,oidIfXTabList,ifXTabRootOid,version,retries,timeout,stateInfo); + etime64 = System.currentTimeMillis(); +// dataFlag64 = false; + if(ifXTabdatasMapList2.size()>0 && ifXTabdatasMapList1.size()>0 && dataFlag64){ + dataFlag64 = true; + waitTime = new BigDecimal((etime64 - stime64)/1000+0.49d).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); + //替换snmpDatasList2的数据 + logger.info("采用64位的数据,开始替换:将32位数据替换为64位数据"); + replaceValueByKeyMapForListData(snmpDatasList1,info.getCheckTypeName(),rootOid,datasMapList1, mapForDPIfXTab, ifXTabdatasMapList1); + replaceValueByKeyMapForListData(snmpDatasList2,info.getCheckTypeName(),rootOid,datasMapList2, mapForDPIfXTab, ifXTabdatasMapList2); + }else{//64位没取成功,则按32的进行计算,不用替换 + dataFlag64 = false; + waitTime = new BigDecimal((etime32 - stime32)/1000+0.49d).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); + logger.info("采用32位的数据"); + } + logger.debug("dataFlag64 value "+(dataFlag64)); + logger.debug("waitTime value "+(waitTime)+" s"); + state = 1; + } catch (Exception e) { + state = -1; + stateInfo.append(e.getMessage()); + logger.error((version == SnmpConstants.version3?"version3":"version2c")+" GET失败", e); + } + + + + /** + * 二次计算 + 平均输入速率=△ifInOctets/T + 平均输出速率=△ifOutOctets/T + //输入利用率=(△ifInOctets*8)/(ifSpeed*T)*100% + //输出利用率=(△ifOutOctets*8)/(ifSpeed*T)*100% + 平均收包速率=(△ifInUcastPkts+△ifInNucastPkts)/T + 平均发包速率=(△ifOutUcastPks+△ifOutNucastPks)/T + 输入丢包率=△ifInDiscards/(△ifInUcastPkts+△ifInNucastPkts)*100% + 输出丢包率=△ifOutDiscards/(△ifOutUcastPks+△ifOutNucastPks)*100% + 输入差错率=△ifInErrors/(△ifInUcastPkts+△ifInNucastPkts)*100% + 输出差错率=△ifOutErrors/(△ifOutUcastPkts+△ifOutNucastPkts)*100% + //输入吞吐量=(△ifInOctets-△ifInDiscards-△ifInErrors)/T + //输出吞吐量=(△ifOutOctets-△ifOutDiscards-△ifOutErrors)/T + * + *0319 计算变更 + *丢包数:无需计算 + * + */ + + + + //索取字段OID: + /* + + ifInOctets NUMBER ; -- 输入字节数 1.3.6.1.2.1.2.2.1.10 + ifInUcastPkts NUMBER ; -- 单点输入总包数 1.3.6.1.2.1.2.2.1.11 + ifInNucastPkts NUMBER ; -- 输出字节数 1.3.6.1.2.1.2.2.1.12 + ifInDiscards NUMBER ; -- 输出字节数 1.3.6.1.2.1.2.2.1.13 + ifInErrors NUMBER ; -- 输入错包数 1.3.6.1.2.1.2.2.1.14 + + + ifOutOctets NUMBER ; -- 输出字节数 1.3.6.1.2.1.2.2.1.16 + ifOutUcastPkts NUMBER ; -- 输出字节数 1.3.6.1.2.1.2.2.1.17 + ifOutNucastPkts NUMBER ; -- 输出输出总包数 1.3.6.1.2.1.2.2.1.18 + ifOutDiscards NUMBER ; -- 输出字节数 1.3.6.1.2.1.2.2.1.19 + ifOutErrors NUMBER ; -- 输出错包数 1.3.6.1.2.1.2.2.1.20 + + + + + */ + + if(datasMapList1.size()>0 && datasMapList2.size()>0){ + //循环每行数据 + Double defaultVal = 0d; + for(int i = 0 ; i dm2 = datasMapList2.get(i); + Double ifInOctets_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.10"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.10")); + Double ifInUcastPkts_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.11"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.11")); + Double ifInNucastPkts_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.12"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.12")); + Double ifInDiscards_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.13"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.13")); + Double ifInErrors_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.14"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.14")); + + Double ifOutOctets_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.16"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.16")); + Double ifOutUcastPkts_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.17"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.17")); + Double ifOutNucastPkts_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.18"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.18")); + Double ifOutDiscards_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.19"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.19")); + Double ifOutErrors_n = StringUtils.isBlank(dm2.get("1.3.6.1.2.1.2.2.1.20"))?defaultVal : Double.parseDouble(dm2.get("1.3.6.1.2.1.2.2.1.20")); + + Map dm1 = datasMapList1.get(i); + Double ifInOctets_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.10"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.10")); + Double ifInUcastPkts_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.11"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.11")); + Double ifInNucastPkts_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.12"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.12")); + Double ifInDiscards_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.13"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.13")); + Double ifInErrors_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.14"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.14")); + + Double ifOutOctets_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.16"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.16")); + Double ifOutUcastPkts_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.17"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.17")); + Double ifOutNucastPkts_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.18"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.18")); + Double ifOutDiscards_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.19"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.19")); + Double ifOutErrors_o = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.20"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.20")); + //既然上面为空,则下面使用之前需进行判空处理 + + //打印日志 2014-2-18 hyx------------------------------------------------------------------------------ + String curTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()); + String desc = dm1.get("1.3.6.1.2.1.2.2.1.2"); + Double ifSpeedTmp = 0d; + Double ifHigeSpeedTmp = 0d; + Double speedTmp = 0d; + try + { + ifSpeedTmp = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.2.2.1.5"))?defaultVal : Double.parseDouble(dm1.get("1.3.6.1.2.1.2.2.1.5")); + ifHigeSpeedTmp = StringUtils.isBlank(dm1.get("1.3.6.1.2.1.31.1.1.1.15.1.1000000"))?null : Double.parseDouble(dm1.get("1.3.6.1.2.1.31.1.1.1.15.1.1000000")); + if(ifHigeSpeedTmp!=null) { + speedTmp = ifHigeSpeedTmp*1000*1000; + }else { + speedTmp = ifSpeedTmp; + } + } catch (Exception e) + { + logger.error("获取ifSpeed 或者 ifHighSpeed 异常", e); + } + logger.info("ifSpeed:"+ifSpeedTmp+" ifHighSPeed:"+ifHigeSpeedTmp+" speed:"+speedTmp); + + /* 平均输入速率=△ifInOctets/T 单位:bps */ + double dInOctetsL = (ifInOctets_n - ifInOctets_o); + + logger.info(hostIp+"-"+desc+"-"+curTime+"-输入字节数之差:"+dInOctetsL+"="+ifInOctets_n+"-"+ifInOctets_o); + + //非负数处理 + BigDecimal ifInOctetsSpeed = null; + if(dInOctetsL<0){ + double dInOctetsLTmp = dInOctetsL; + if(dataFlag64){ //64位数据 + dInOctetsL = dInOctetsLTmp + value64; + logger.info(hostIp+"-"+desc+"-"+curTime+"-输入字节数之差小于0,64位:"+dInOctetsL+"="+dInOctetsLTmp+"+"+value64); + ifInOctetsSpeed = new BigDecimal(dInOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + //如果按64位进行计算,得到的速度大于带宽,则按32位计算 + if(speedTmp>0d && ifInOctetsSpeed.doubleValue()>speedTmp) {//ifHigeSpeedTmp的单位为Mbps + dInOctetsL = dInOctetsLTmp + value32; + logger.info(hostIp+"-"+desc+"-"+curTime+"-输入字节数之差小于0,按64位算,得到的速度大于带宽,改按32位计算:"+dInOctetsL+"="+dInOctetsLTmp+"+"+value32); + ifInOctetsSpeed = new BigDecimal(dInOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + if(ifInOctetsSpeed.doubleValue()>speedTmp) {//如果改按32位计算,还是大, + logger.info(hostIp+"-64位改为32为计算,输入速度还是大于带宽"); + } + } + }else{ //32位数据 + dInOctetsL += value32; + logger.info(hostIp+"-"+desc+"-"+curTime+"-输入字节数之差小于0,32位:"+dInOctetsL+"="+dInOctetsLTmp+"+"+value32); + ifInOctetsSpeed = new BigDecimal(dInOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + if(speedTmp>0d && ifInOctetsSpeed.doubleValue()>speedTmp) {//ifSpeedTmp的单位为bps + dInOctetsL = dInOctetsLTmp + value64; + logger.info(hostIp+"-"+desc+"-"+curTime+"-输入字节数之差小于0,按32位算,得到的速度大于带宽,改按64位计算:"+dInOctetsL+"="+dInOctetsLTmp+"+"+value64); + ifInOctetsSpeed = new BigDecimal(dInOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + if(ifInOctetsSpeed.doubleValue()>speedTmp) {//如果改按64位计算,还是大, + logger.info(hostIp+"-32位改为64为计算,输入速度还是大于带宽"); + } + } + } + }else { + ifInOctetsSpeed = new BigDecimal(dInOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + } +// BigDecimal ifInOctetsSpeed = new BigDecimal(dInOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + + logger.info(hostIp+"-"+desc+"-"+curTime+"-输入速度(位速度):"+ifInOctetsSpeed); + + //------------------------------------------------------------------------------------------------------ + /* 平均输出速率=△ifOutOctets/T 单位:bps */ + double dOutOctetsL= (ifOutOctets_n - ifOutOctets_o); + logger.info(hostIp+"-"+desc+"-"+curTime+"-输出字节数之差:"+dOutOctetsL+"="+ifOutOctets_n+"-"+ifOutOctets_o); + + //非负数处理 + BigDecimal ifOutOctetsSpeed = null; + if(dOutOctetsL<0){ + double dOutOctetsLTmp = dOutOctetsL; + if(dataFlag64){ //64位数据 + dOutOctetsL += value64; + logger.info(hostIp+"-"+desc+"-"+curTime+"-输出字节数之差小于0,64位:"+dOutOctetsL+"="+dOutOctetsLTmp+"+"+value64); + ifOutOctetsSpeed = new BigDecimal(dOutOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + + //如果按64位进行计算,得到的速度大于带宽,则按32位计算 + if(speedTmp>0d && ifOutOctetsSpeed.doubleValue()>speedTmp) {//ifHigeSpeedTmp的单位为Mbps + dOutOctetsL = dOutOctetsLTmp + value32; + logger.info(hostIp+"-"+desc+"-"+curTime+"-输出字节数之差小于0,按64位算,得到的速度大于带宽,改按32位计算:"+dOutOctetsL+"="+dOutOctetsLTmp+"+"+value32); + ifOutOctetsSpeed = new BigDecimal(dOutOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + if(ifOutOctetsSpeed.doubleValue()>speedTmp) {//如果改按32位计算,还是大, + logger.info(hostIp+"-64位改为32为计算,输出速度还是大于带宽"); + } + } + }else{//32位数据 + dOutOctetsL += value32; + logger.info(hostIp+"-"+desc+"-"+curTime+"-输出字节数之差小于0,32位:"+dOutOctetsL+"="+dOutOctetsLTmp+"+"+value32); + ifOutOctetsSpeed = new BigDecimal(dOutOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + + if(speedTmp>0d && ifOutOctetsSpeed.doubleValue()>speedTmp) {//ifSpeedTmp的单位为bps + dOutOctetsL = dOutOctetsLTmp + value64; + logger.info(hostIp+"-"+desc+"-"+curTime+"-输出字节数之差小于0,按32位算,得到的速度大于带宽,改按64位计算:"+dOutOctetsL+"="+dOutOctetsLTmp+"+"+value64); + ifOutOctetsSpeed = new BigDecimal(dOutOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + if(ifOutOctetsSpeed.doubleValue()>speedTmp) {//如果改按64位计算,还是大, + logger.info(hostIp+"-32位改为64为计算,输出速度还是大于带宽"); + } + } + } + }else { + ifOutOctetsSpeed = new BigDecimal(dOutOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + } +// BigDecimal ifOutOctetsSpeed = new BigDecimal(dOutOctetsL).multiply(new BigDecimal(8)).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + + logger.info(hostIp+"-"+desc+"-"+curTime+"-输出速度(位速度):"+ifOutOctetsSpeed); + + + /* 平均收包速率=(△ifInUcastPkts+△ifInNucastPkts)/T 单位:pps */ + double iupks = ifInUcastPkts_n - ifInUcastPkts_o;//输入单向包 + if(iupks <0){ + if(dataFlag64){ //64位数据 + iupks += value64; + }else{ //32位数据 + iupks += value32; + } + } + + double inupks = ifInNucastPkts_n - ifInNucastPkts_o;//输入非单向包 + if(inupks <0){ + if(dataFlag64){ //64位数据 + inupks += value64; + }else{ //32位数据 + inupks += value32; + } + } + + double dInPks= (iupks)+(inupks); + +// BigDecimal inPksSpeed = new BigDecimal(dInPks).divide(new BigDecimal(waitTime),2, RoundingMode.HALF_UP); +// BigDecimal inPksSpeed = new BigDecimal(dInPks).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + BigDecimal inPksSpeed = new BigDecimal(dInPks).divide(new BigDecimal(waitTime),0, RoundingMode.UP);//平均收包速率,向上取整 + + /* 平均发包速率=(△ifOutUcastPks+△ifOutNucastPks)/T 单位:pps */ + double oupks = ifOutUcastPkts_n - ifOutUcastPkts_o; + if(oupks <0){ + if(dataFlag64){ //64位数据 + oupks += value64; + }else{ //32位数据 + oupks += value32; + } + } + + double onupks = ifOutNucastPkts_n - ifOutNucastPkts_o; + if(onupks <0){ + if(dataFlag64){ //64位数据 + onupks += value64; + }else{ //32位数据 + onupks += value32; + } + } + + double dOutPks= (oupks)+(onupks); + +// BigDecimal outPksSpeed = new BigDecimal(dOutPks).divide(new BigDecimal(waitTime),2, RoundingMode.HALF_UP); +// BigDecimal outPksSpeed = new BigDecimal(dOutPks).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + BigDecimal outPksSpeed = new BigDecimal(dOutPks).divide(new BigDecimal(waitTime),0, RoundingMode.UP);////平均发包速率,向上取整 + + /* 输入丢包率=△ifInDiscards/(△ifInUcastPkts+△ifInNucastPkts)*100% */ + BigDecimal inDiscardsPerc = new BigDecimal(0); + if(dInPks != 0){ + inDiscardsPerc = new BigDecimal(ifInDiscards_n - ifInDiscards_o).divide(new BigDecimal(dInPks),2, RoundingMode.HALF_UP); + } + + + /* 输出丢包率=△ifOutDiscards/(△ifOutUcastPks+△ifOutNucastPks)*100% */ + BigDecimal outDiscardsPerc = new BigDecimal(0); + if(dOutPks != 0){ + outDiscardsPerc = new BigDecimal(ifOutDiscards_n - ifOutDiscards_o).divide(new BigDecimal(dOutPks),2, RoundingMode.HALF_UP); + } + + + /* 输入差错率=△ifInErrors/(△ifInUcastPkts+△ifInNucastPkts)*100% */ +// BigDecimal ifInErrorsPerc = new BigDecimal(ifInErrors_n - ifInErrors_o).divide(new BigDecimal(waitTime),2, RoundingMode.HALF_UP); + BigDecimal ifInErrorsPerc = new BigDecimal(ifInErrors_n - ifInErrors_o).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + + /* 输出差错率=△ifOutErrors/(△ifOutUcastPkts+△ifOutNucastPkts)*100% */ +// BigDecimal ifOutErrorsPerc = new BigDecimal(ifOutErrors_n - ifOutErrors_o).divide(new BigDecimal(waitTime),2, RoundingMode.HALF_UP); + BigDecimal ifOutErrorsPerc = new BigDecimal(ifOutErrors_n - ifOutErrors_o).divide(new BigDecimal(waitTime),2, RoundingMode.UP); + + //-- 计算输入输出速度 + + double ifinoctetsspeed_a = ifInOctetsSpeed.doubleValue(); //--单位bps + double ifoutoctetsspeed_a = ifOutOctetsSpeed.doubleValue(); //--单位bps + + double inPksSpeed_a = inPksSpeed.doubleValue(); //--单位pps + double outPksSpeed_a = outPksSpeed.doubleValue(); //--单位pps + + double ifOutDiscards_a = outDiscardsPerc.doubleValue(); //--单位 + double ifInDiscards_a = inDiscardsPerc.doubleValue(); //--单位 + + double ifInErrors_a = ifInErrorsPerc.doubleValue(); //--单位 + double ifOutErrors_a = ifOutErrorsPerc.doubleValue(); //--单位 + + +// logger.debug("ifinoctetsspeed_a:"+ifinoctetsspeed_a); +// logger.debug("inPksSpeed_a:"+inPksSpeed_a); +// logger.debug("ifInDiscards_a:"+ifInDiscards_a); +// logger.debug("ifInErrors_a:"+ifInErrors_a); +// +// logger.debug("ifoutoctetsspeed_a:"+ifoutoctetsspeed_a); +// logger.debug("outPksSpeed_a:"+outPksSpeed_a); +// logger.debug("ifOutDiscards_a:"+ifOutDiscards_a); +// logger.debug("ifOutErrors_a:"+ifOutErrors_a); + + // + + snmpDatasList2.get(i).remove((23-1)); //list中多余的数据 + snmpDatasList2.get(i).add((23-1),ifinoctetsspeed_a<0?"":ifinoctetsspeed_a+""); + snmpDatasList2.get(i).remove((24-1)); + snmpDatasList2.get(i).add((24-1),inPksSpeed_a<0?"":inPksSpeed_a+""); + snmpDatasList2.get(i).remove((25-1)); + snmpDatasList2.get(i).add((25-1),ifInDiscards_a<0?"":ifInDiscards_a+""); + snmpDatasList2.get(i).remove((26-1)); + snmpDatasList2.get(i).add((26-1),ifInErrors_a<0?"":ifInErrors_a+""); + + snmpDatasList2.get(i).remove((27-1)); + snmpDatasList2.get(i).add((27-1),ifoutoctetsspeed_a<0?"":ifoutoctetsspeed_a+""); + snmpDatasList2.get(i).remove((28-1)); + snmpDatasList2.get(i).add((28-1),outPksSpeed_a<0?"":outPksSpeed_a+""); + snmpDatasList2.get(i).remove((29-1)); + snmpDatasList2.get(i).add((29-1),ifOutDiscards_a<0?"":ifOutDiscards_a+""); + snmpDatasList2.get(i).remove((30-1)); + snmpDatasList2.get(i).add((30-1),ifOutErrors_a<0?"":ifOutErrors_a+""); + + //zhui加1的数据--t1的测试用的值 + snmpDatasList2.get(i).remove((31-1)); + snmpDatasList2.get(i).add((31-1),ifInOctets_o+""); + snmpDatasList2.get(i).remove((32-1)); + snmpDatasList2.get(i).add((32-1),ifInUcastPkts_o+""); + snmpDatasList2.get(i).remove((33-1)); + snmpDatasList2.get(i).add((33-1),ifInNucastPkts_o+""); + snmpDatasList2.get(i).remove((34-1)); + snmpDatasList2.get(i).add((34-1),ifOutOctets_o+""); + snmpDatasList2.get(i).remove((35-1)); + snmpDatasList2.get(i).add((35-1),ifOutUcastPkts_o+""); + snmpDatasList2.get(i).remove((36-1)); + snmpDatasList2.get(i).add((36-1),ifOutNucastPkts_o+""); + snmpDatasList2.get(i).remove((38-1)); + snmpDatasList2.get(i).add((38-1),(dataFlag64?1:0)+""); + + //计算存在小于0的值时,输出 + if(ifinoctetsspeed_a<0 + || inPksSpeed_a<0 + || ifInDiscards_a<0 + || ifInErrors_a<0 + || ifoutoctetsspeed_a<0 + || outPksSpeed_a<0 + || ifOutDiscards_a<0 + || ifOutErrors_a<0){ + logger.info("Exception SwitchPortInfo Old:> "+Arrays.toString(snmpDatasList1.get(i).toArray())); + logger.info("Exception SwitchPortInfo New:> "+Arrays.toString(snmpDatasList2.get(i).toArray())); + } + + } + } + + //- 监测数据集合 + List pubDatas = new ArrayList(); // 拼写数据格式 + + //- DetecDatas 拼写 Header + pubDatas.add(Common.getIpSeqIdMap().get(hostIp)+"");// uuid, + pubDatas.add(info.getId() + "");// 监测设置ID, + pubDatas.add(info.getCheckTypeName());// 监测类别, + pubDatas.add(info.getProcessIden());// 进程名称(检查类别设置名称), + pubDatas.add(startTime + "");// 监测服务启动时间, + pubDatas.add(timeout+"");// 检测时延(秒) + //- 当前检测时间 + Calendar checkTime = Calendar.getInstance(); + pubDatas.add(checkTime.getTimeInMillis() + "");// 本次检测时间, + pubDatas.add(retries + "");// 尝试次数, + //- 下次检测时间 + Calendar nextTime = Calendar.getInstance(); + nextTime.add(Calendar.MINUTE, info.getCheckGap().intValue()); + pubDatas.add(nextTime.getTimeInMillis() + "");// 下次计划监测时间, + + //- DetecDatas 拼写 Body + List> pubDatasList = new ArrayList>(); + if(state != -1){//执行成功了 + pubDatasList = datasBody(retries,stateInfo.toString(),state,snmpDatasList2); + + //64位获取失败告警 + if(!dataFlag64){ + state=0; + stateInfo.insert(0, "获取64位数据失败,使用32位数据计算\n"); + } + } + + pubDatas.add(state+""); // 执行状态--detection_info表的监测状态 + pubDatas.add(stateInfo.toString()); // 状态信息(描述信息), + pubDatas.add(""); // 性能数据, + + if(pubDatasList.size()>0){ + pubDatasList.add(0,Arrays.asList(new String[]{"details",pubDatasList.size()+""})); + } + pubDatasList.add(0,pubDatas); + + //- 存入缓存 + if(pubDatasList!=null && pubDatasList.size()>0){ + try { + //添加监测数据到缓存,等待监测数据解析线程的解析入库 + Common.addDeteData(CSVUtils.csvFilePrinter(pubDatasList, Constants.COMMON_TEXT_CODING)); + logger.debug("监测数据以 CSV格式 保存完成"); + } catch (Exception e) { + logger.error("监测数据以 CSV格式 保存异常", e); + } + } + } catch (Exception e) { + String[] alarm = Common.alarmExceptionInfo(info.getId(), hostIp, info.getCheckTypeName(), info.getProcessIden(), startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, e.getMessage()); + Common.addAlarmData(alarm); + logger.error("程序错误 未知", e); + }finally{ + if(downLatch != null){ + downLatch.countDown(); + } + } + } + + //返回监测类别checkTypeName的List + private List getOidListByCheckTypeName(String checkTypeName){ +// Map oidFiledNameMap= new HashMap(); + List oidList = new ArrayList(); + Map CloumnsMap = Common.getTableMap().get(checkTypeName);//Integer是metadata表里的shouNum(组合cvs文件时字段的顺序)--解析字段的顺序 + if(CloumnsMap!=null && CloumnsMap.size()>0){ + Iterator> entryIte = CloumnsMap.entrySet().iterator(); + while (entryIte.hasNext()) { + Entry cloumnEntry = (Entry) entryIte.next(); + String oid = cloumnEntry.getValue().getOid(); +// String name = cloumnEntry.getValue().getFiledName(); + if (StringUtils.isNotEmpty(oid)){ +// oidFiledNameMap.put(oid, name); + oidList.add(oid); + } + } + } + return oidList; + } + + /** + * 将oldmap里的值,替换成subMap对应的value。 + * keyMap:为oldMap和subMap的key的对应关系,Map + * oldMap:List> + * subMap:Map> + * aimMap:oldMap替换后的值List> + */ + private void replaceValueByKeyMapForListData(List> snmpDateList,String checkTypeName,String rootOid,List> oldMap,Map keyMap,Map> subMap) {//List> + + //循环keyMap + if(keyMap!=null && keyMap.size()>0){ + logger.info(hostIp+"-开始进行64位数据和32位数据的替换"); + logger.info(hostIp+"-替换前32的数据"); + if(oldMap!=null && oldMap.size()>0) { + for(Map tmpMa:oldMap) { + Iterator> oi = tmpMa.entrySet().iterator(); + while(oi.hasNext()) { + Entry en2 =oi.next(); + logger.info(hostIp+"-替换前32的值:"+en2.getKey()+":"+en2.getValue()); + } + logger.info("======"+hostIp+"====="); + } + } + + logger.info(hostIp+"-替换前64的数据"); + Iterator>> oiSum = subMap.entrySet().iterator(); + while(oiSum!=null && oiSum.hasNext()) { + Entry> oi = oiSum.next(); + Map mapTmp = oi.getValue(); + Iterator> oi2 = mapTmp.entrySet().iterator(); + logger.info("key:"+oi.getKey()+"---"); + while(oi2.hasNext()) { + Entry en2 =oi2.next(); + logger.info(hostIp+"-替换前64的值:"+en2.getKey()+":"+en2.getValue()); + } + logger.info("======"+hostIp+"====="); + } + + Map columnsMap = Common.getTableMap().get(checkTypeName); + Iterator> entryIte = keyMap.entrySet().iterator(); + Iterator> tcIte = null; + while (entryIte.hasNext()) { + Entry oidToNewOidEntry = (Entry) entryIte.next(); + String oldOid = oidToNewOidEntry.getKey();//32位:原oid + String newOid = oidToNewOidEntry.getValue();//64位:对应oid + + //获取数据表结构 + tcIte = columnsMap.entrySet().iterator(); + logger.info(hostIp+"-tcIte:"+tcIte+"(switchport对应的metadata表的信息)"); + int showNum = 0; + while (tcIte.hasNext()) { + Entry elem = tcIte.next(); + if(elem !=null && elem.getValue()!=null && elem.getValue().getOid()!=null){ + if(elem.getValue().getOid().equals(oldOid)){ + showNum = elem.getKey(); + break; + } + } + } +// logger.debug("showNum"+showNum); + + for(int i = 0 ; ii){ + Map tmpMap = oldMap.get(i);//一个口的所有oid: oid:value +// logger.debug("tmpSubMap size "+Arrays.toString(tmpMap.keySet().toArray())); + String ifIndex = tmpMap.get(rootOid+".1");//某个端口的ifIndex属性的值,即端口的编号 + logger.info("转换Unsigned前端口index :"+ifIndex); + try { + if(StringUtils.isNotBlank(ifIndex) && Integer.valueOf(ifIndex.trim()) < 0) { + String ifIndexTmp = ""; + ifIndexTmp = MathUtil.getUnsignedIntt(Integer.valueOf(ifIndex.trim()))+""; + ifIndex = ifIndexTmp; + logger.info("转换Unsigned后端口index:"+ifIndex); + } + } catch (Exception e) + { + logger.error("Transform the Unsigned port index :"+ifIndex+" exception", e); + } +// logger.debug("ifIndex "+ifIndex); + List snmpData = snmpDateList.get(i); + Map tmpSubMap = subMap.get(ifIndex); +// logger.debug("ifIndex "+(ifIndex)); +// logger.debug("tmpSubMap "+(JSONArray.fromObject(tmpSubMap).toString())); +// logger.debug("tmpSubMap "+(tmpSubMap==null?null:(Arrays.toString(tmpSubMap.keySet().toArray())))); + if(tmpSubMap!=null && tmpSubMap.size()>0) { + String newVal = tmpSubMap.get(newOid); + //tmpMap.put(oldOid, newVal); +// String vaString = oldOid+" Map["+tmpMap.get(oldOid)+" vs "+newVal+"] "; +// vaString+="snmpData["+snmpData.get(showNum-1)+"]"; +// logger.debug(vaString); + snmpData.remove(showNum-1); + snmpData.add(showNum-1,newVal); + tmpMap.put(oldOid,newVal); + logger.info(hostIp+"-"+ifIndex+"-替换过程中的值:"+oldOid+":"+newVal); + }else {//如果匹配不上,打印信息 + logger.info("端口:"+ifIndex+"在64位数据里无对应值"); + } + } + } + + } + for(Map tmpMa:oldMap) { + Iterator> oi = tmpMa.entrySet().iterator(); + while(oi.hasNext()) { + Entry en2 =oi.next(); + logger.info(hostIp+"-替换后的值oldOid:newValue:"+en2.getKey()+":"+en2.getValue()); + } + logger.info("======"+hostIp+"替换后====="); + } + } + + + } + private void pl(Object obj){ + System.out.println(obj==null?null:obj.toString()); + } + + /** + * 将oldMap里的值,替换成subMap对应的value。 + * keyMap:为oldMap和subMap的key的对应关系,Map + * oldMap:List> + * subMap:Map> + * aimMap:oldMap替换后的值List> + */ + private void replaceValueByKeyMap(List> oldMap,Map keyMap,Map> subMap,String rootOid) {//List> + //循环keyMap + if(keyMap!=null && keyMap.size()>0){ + Iterator> entryIte = keyMap.entrySet().iterator(); + while (entryIte.hasNext()) { + Entry oidToNewOidEntry = (Entry) entryIte.next(); + String oldOid = oidToNewOidEntry.getKey();//32位:原oid + String newOid = oidToNewOidEntry.getValue();//64位:对应oid + + for(Map tmpMap:oldMap) {//oid:value + String ifIndex = tmpMap.get(rootOid+".1");//某个端口的ifIndex属性的值,即端口的编号 + Map tmpSubMap = subMap.get(ifIndex); + if(tmpSubMap!=null && tmpSubMap.size()>0) { + String newVal = tmpSubMap.get(newOid); + tmpMap.put(oldOid, newVal); + } + } + } + } + } + + public static void main(String[] args) { + + CommonDao dao = null; + try { + BoneCPPool.initPool(); + dao = new CommonDao(); + CommonService commonService = new CommonService(dao); + ThreadService service = new ThreadService(); + + new InitServerThread().initCommonInfo(dao); + long startTimeTmp = Calendar.getInstance().getTimeInMillis(); + + + + //--获取checkType 监测配置并启动监测任务 + List setList = commonService.getDrivingSetInfo(Constants.DETEC_SWITCH_STR);//获得checkType类型的所有监测设置 + System.out.println("检查主动监测类别:"+Constants.DETEC_SWITCH_STR+" 关联监测设置:"+(setList==null?0:setList.size())+"个"); + //-- checkType 监测任务启动 和 客户端启动监测任务启动 + SetInfo si = null; + if(setList!=null && setList.size()>0){ + for (Iterator setIte = setList.iterator(); setIte.hasNext();) { +// if(Constants.DETEC_SNMP_STR.equalsIgnoreCase(checkType)){ +// if(Constants.DETEC_SWITCH_STR.equalsIgnoreCase(si.getCheckTypeName())){ +// continue; +// } +// } +// service.updateThread(si); + + si = (SetInfo) setIte.next(); + break; + } + } + Common.service.submit(new SwitchPortThread("254","10.0.6.254",si,startTimeTmp)); +// +// +// +// +// Map mapForDPIfXTab = new HashMap(); +// mapForDPIfXTab.put("1.3.2", "aa"); +// mapForDPIfXTab.put("1.3.3", "bb"); +// mapForDPIfXTab.put("1.3.4", "cc"); +// mapForDPIfXTab.put("1.3.1", "ifIndex"); +// +// List> datasMapList1 = new ArrayList>(); +// Map tmpMap = new LinkedHashMap(); +// tmpMap.put("1.3.2", "11"); +// tmpMap.put("1.3.3", "12"); +// tmpMap.put("1.3.4", "13"); +// tmpMap.put("1.3.1", "1"); +// Map tmpMap2 = new LinkedHashMap(); +// tmpMap2.put("1.3.2", "21"); +// tmpMap2.put("1.3.3", "22"); +// tmpMap2.put("1.3.4", "23"); +// tmpMap2.put("1.3.1", "2"); +// Map tmpMap3 = new LinkedHashMap(); +// tmpMap3.put("1.3.2", "31"); +// tmpMap3.put("1.3.3", "32"); +// tmpMap3.put("1.3.4", "33"); +// tmpMap3.put("1.3.1", "3"); +// datasMapList1.add(tmpMap); +// datasMapList1.add(tmpMap2); +// datasMapList1.add(tmpMap3); +// +// List> snmpDatasList = new LinkedList>(); +// List snmpTmpList = new LinkedList(); +// snmpTmpList.add("11"); +// snmpTmpList.add("12"); +// snmpTmpList.add("13"); +// snmpTmpList.add("1"); +// snmpDatasList.add(snmpTmpList); +// +// List snmpTmpList2 = new LinkedList(); +// snmpTmpList2.add("21"); +// snmpTmpList2.add("22"); +// snmpTmpList2.add("23"); +// snmpTmpList2.add("2"); +// snmpDatasList.add(snmpTmpList2); +// +// List snmpTmpList3 = new LinkedList(); +// snmpTmpList3.add("31"); +// snmpTmpList3.add("32"); +// snmpTmpList3.add("33"); +// snmpTmpList3.add("3"); +// snmpDatasList.add(snmpTmpList3); +// +// +// for(Map tmpMa:datasMapList1) { +// Iterator> oi = tmpMa.entrySet().iterator(); +// while(oi.hasNext()) { +// Entry en2 =oi.next(); +// System.out.print(en2.getKey()+":"); +// System.out.print(en2.getValue()); +// System.out.println(); +// +// } +// System.out.println("==========="); +// } +// +// +// Map> ifXTabdatasMapList1 = new HashMap>(); +// Map tmpMap11 = new LinkedHashMap(); +// tmpMap11.put("aa", "111"); +// tmpMap11.put("bb", "112"); +// tmpMap11.put("cc", "113"); +// tmpMap11.put("ifIndex", "1"); +// Map tmpMap22 = new LinkedHashMap(); +// tmpMap22.put("aa", "221"); +// tmpMap22.put("bb", "222"); +// tmpMap22.put("cc", "223"); +// tmpMap22.put("ifIndex", "2"); +// Map tmpMap33 = new LinkedHashMap(); +// tmpMap33.put("aa", "331"); +// tmpMap33.put("bb", "332"); +// tmpMap33.put("cc", "333"); +// tmpMap33.put("ifIndex", "3"); +// ifXTabdatasMapList1.put("1", tmpMap11); +// ifXTabdatasMapList1.put("2", tmpMap22); +// ifXTabdatasMapList1.put("3", tmpMap33); +// +// +// // new SwitchPortThread().replaceValueByKeyMap(datasMapList1,mapForDPIfXTab,ifXTabdatasMapList1); +// new SwitchPortThread().replaceValueByKeyMapForListData(snmpDatasList, "switchport", "1.3", datasMapList1, mapForDPIfXTab, ifXTabdatasMapList1); +// +// for(Map tmpMa:datasMapList1) { +// Iterator> oi = tmpMa.entrySet().iterator(); +// while(oi.hasNext()) { +// Entry en2 =oi.next(); +// System.out.print(en2.getKey()+":"); +// System.out.print(en2.getValue()); +// System.out.println(); +// +// } +// System.out.println("==========="); +// } + // + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + + + /** + * 执行监测 获取Switch Port 数据 + * @time Jan 30, 2013-4:50:25 PM + * @return + */ + public List> detectionSwitchDataOnce(List> snmpDatasList,List oids,String rootOid,StringBuffer stateInfo,int version ,int retries,int timeout,int state){ + List> snmpDatas = new LinkedList>(); + try { + + snmpDatasList = resoveVersion(snmpDatas,oids,rootOid,version,retries,timeout,stateInfo); + state = 1; + } catch (Exception e) { + state = -1; + stateInfo.append(e.getMessage()); + logger.error((version == SnmpConstants.version3?"version3":"version2c")+" GET failed", e); + } + return snmpDatas; + } + private List> resoveVersion(List> datasMapList,List oids,String oid,int version ,int retries,int timeout,StringBuffer stateInfo) throws Exception{ + + List> snmpDatasList = new LinkedList>(); + logger.debug("SNMP4J 》》》》"+oid+" "+hostIp+" "+port+" "+Constants.SNMP_COMMUNITY+" "+retries+" "+timeout+" "+ version); + SNMP4JUtils.snmpTree(datasMapList, oids,oid, hostIp, port+"", Constants.SNMP_COMMUNITY,retries,timeout,version); +// logger.debug("datasMapList size "+datasMapList.size()); + //- 获取表字段结构 + Map cloumnsList = Common.getTableMap().get(info.getCheckTypeName()); +// logger.debug("cloumnsList size "+cloumnsList.size()); + //- 存值 + if(cloumnsList!=null && cloumnsList.size() > 0 && datasMapList!=null && datasMapList.size()>0){ + for(Map datasMap : datasMapList){ + List snmpDatas = new LinkedList(); + for (int i = 1; i <= cloumnsList.size(); i++) { + TableColumnsModel cloumn = cloumnsList.get(i); + String oid0 = cloumn.getOid(); + snmpDatas.add(datasMap.get(oid0)); + if(oid0.equals(oid+".1")){ +// stateInfo.append(oid0+":"+datasMap.get(oid0)+"\n"); + } + } + snmpDatasList.add(snmpDatas); + } + } + return snmpDatasList; + } + + private void resoveIfXTabVersion(Map> datasMapList,List oids,String oid,int version ,int retries,int timeout,StringBuffer stateInfo) throws Exception{ + +// List> snmpDatasList = new LinkedList>(); + logger.debug("SNMP4J 》》》》"+oid+" "+hostIp+" "+port+" "+Constants.SNMP_COMMUNITY+" "+retries+" "+timeout+" "+ version); + //2013-3-20 hyx add for ifXTable +// SNMP4JUtils.checkTypeName = ifxtableName; + SNMP4JUtils.snmpIfXTabTree(datasMapList, oids,oid, hostIp, port+"", Constants.SNMP_COMMUNITY,retries,timeout,version); +// SNMP4JUtils.checkTypeName = ""; +// logger.debug("datasMapList size "+datasMapList.size()); + //- 获取表字段结构 +// Map cloumnsList = Common.getTableMap().get(info.getCheckTypeName()); +// logger.debug("cloumnsList size "+cloumnsList.size()); + //- 存值 +// if(cloumnsList!=null && cloumnsList.size() > 0 && datasMapList!=null && datasMapList.size()>0){ +// Iterator>> entryIte = datasMapList.entrySet().iterator(); +// while (entryIte.hasNext()) { +// Entry> dataEntry = (Entry>) entryIte.next(); +//// } +//// for(Map datasMap : datasMapList){ +// List snmpDatas = new LinkedList(); +// for (int i = 1; i <= cloumnsList.size(); i++) { +// TableColumnsModel cloumn = cloumnsList.get(i); +// String oid0 = cloumn.getOid(); +// snmpDatas.add(dataEntry.getValue().get(oid0)); +// if(oid0.equals(oid+".1")){ +//// stateInfo.append(oid0+":"+datasMap.get(oid0)+"\n"); +// } +// } +// snmpDatasList.add(snmpDatas); +// } +// } +// return snmpDatasList; + } + + //拼写监测信息的详细信息:对于switchport,就是每个端口的监测信息 + private List> datasBody(int times,String stateInfo,int state,List> snmpDatas2)throws Exception{ + +// List> pubDatasList = new LinkedList>(); + + logger.debug("监测数据主体拼写 state "+state+" size"+(snmpDatas2==null ? -1 :snmpDatas2.size())); + + if(snmpDatas2!=null && snmpDatas2.size()!=0){ + for (List snmpDatas : snmpDatas2) { +// List pubDatas0 = new LinkedList(); +// 执行状态是否成功是否正常, 第三方脚本监测任务执行结果(用于报警 -1执行失败 0不正常,1正常)-1时可保留后续数据为空,但是数据个数需要对应 + // -- 执行状态是否成功,记录的状态是否正常(用于报警), + String [] datas0 = new String[0]; + datas0 = snmpDatas.toArray(datas0); +// logger.debug("snmpDatas>>>> "+Arrays.toString(snmpDatas.toArray(datas0))); +// logger.debug("Common.getAlarmInfoMap()>>>> "+Common.getAlarmInfoMap().size()); + String[] pArray = Common.getAlarmState(Common.getAlarmInfoMap().get(info.getId()+""), datas0); +// logger.debug("pArray>>>> "+Arrays.toString(pArray)); + if (pArray == null || pArray.length==0) {//无告警信息 + snmpDatas.add(0, "");//插入后,列表之后的信息顺序往后移 + snmpDatas.add(1, ""); + snmpDatas.add(2, ""); + } else {//有告警信息 + state = 0;//只要有一个端口有告警信息,则整个监测的状态为0 + snmpDatas.add(0, pArray == null ? "" : pArray[0]); + snmpDatas.add(1, pArray == null ? "" : pArray[1]); + snmpDatas.add(2, pArray == null ? "" : pArray[2]); + stateInfo+=Common.getStateInfo(info.getCheckTypeName(),Common.getAlarmInfoMap().get(info.getId()+""), datas0); + } +// logger.debug("pArray>>>> "+Arrays.toString(pubDatas0.toArray())); + } + } + return snmpDatas2; + + } + /* + class Switch{ + + ifIndex + ifDescr + ifType + ifMtu + ifSpeed + ifPhysAddress + ifAdminStatus + ifOperStatus + ifLastChange + ifInOctets + ifInUcastPkts + ifInNUcastPkts + ifInDiscards + ifInErrors + ifInUnknownProtos + ifOutOctets + ifOutUcastPkts + ifOutNUcastPkts + ifOutDiscards + ifOutErrors + ifOutQLen + ifSpecific + INOCTETSSPEED + INERRORSSCALE + OUTOCTETSSPEED + OUTERRORSSCALE + test + test2 + private Double DETECTION_INFO_ID ; //检测信息ID + private Double IFINDEX ; //接口表惟一的索引项,索引值为1~ ifNumber范围 + private String IFDESCR ; //接口的描述,制造商名,产品名和版本等 + private Double IFTYPE ; //接口的类型,用一个整数表示,表示物理层和数据链路层协议确定的接口类型 + private Double IFMTU ; //表示该接口上可以发送或接收的最大协议数据单元大小(位组数) + private Double IFSPEED ; //指定一个接口的传输速率,单位为“位/秒”(bps) + private String IFPHYSADDRESS ; //接口的物理地址 + private Double IFADMINSTATUS ; //用于配置接口的管理状态,up(1) down(2) testing(3) + private Double IFOPERSTATUS ; //提供一个接口的当前操作状态, up(1) down(2) testing(3) + private String IFLASTCHANGE ; //接口进入当前状态的时间 + private Double IFINOCTETS ; //接口收到的总字节数 + private Double IFINUCASTPKTS ; //单点发送到一种高层协议上的包的总数目 + private Double IFINNUCASTPKTS ; //发往高层协议的非单播包数(广播和多播) + private Double IFINDISCARDS ; //接口丢弃的输入包数 + private Double IFINERRORS ; //由于错误导致接收的错误包数 + private Double IFINUNKNOWNPROTOS ; //由于未知或不支持的协议而被抛弃的输入包数 + private Double IFOUTOCTETS ; //接口发送的总字节数 + private Double IFOUTUCASTPKTS ; //高层协议请求传输的单播包数 + private Double IFOUTNUCASTPKTS ; //高层协议请求的非单播(广播和多播)包数 + private Double IFOUTDISCARDS ; //由于资源局限而导致丢弃的发出包的总数目 + private Double IFOUTERRORS ; //由于错误而导致丢弃的发出包的总数目 + private Double IFOUTQLEN ; //"输出包队列中包的总数(输出队列长度)" + private String IFSPECIFIC ; //指向MIB中特定介质的定义 + private Date DATA_CHECK_TIME ; //检测时间 + private Date DATA_ARRIVE_TIME ; //数据入库时间 + private Double DATA_CHECK_TIME_DIGITAL ; //检测时间(毫秒) + private Double DATA_ARRIVE_TIME_DIGITAL ; //数据入库时间(毫秒) + private Double INOCTETSSPEED ; // + private Double INERRORSSCALE ; // + private Double OUTOCTETSSPEED ; // + private Double OUTERRORSSCALE ; // + }*/ +} diff --git a/src/com/nms/server/thread/monitor/SystemDateThread.java b/src/com/nms/server/thread/monitor/SystemDateThread.java new file mode 100644 index 0000000..20befe5 --- /dev/null +++ b/src/com/nms/server/thread/monitor/SystemDateThread.java @@ -0,0 +1,163 @@ +package com.nms.server.thread.monitor; + +import java.io.IOException; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Date; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.SetInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.thread.monitor.util.MonitorUtil; +import com.nms.server.thread.socket.SocketCMD; +import com.nms.server.util.DateUtil; +import com.nms.server.util.socket.SocketUtils; + +/** + * 监测NC系统时间监测类别执行线程 + * 发送通信获取NC系统时间,比较当前DC系统时间比较,并判断告警 + * @date Jun 4, 2013 9:49:01 AM + * @author ZhangGang + * + */ +public class SystemDateThread extends SocketUtils implements Runnable{ + private Logger logger = Logger.getLogger(SystemDateThread.class); + private String name; + private SetInfo info; + private Long startTime = null; + + /** + * @param name 线程名称 + * @param ip 节点IP + * @param port 端口 + * @param info 监测设置信息 + * @param startTime 该检测启动时间 + */ + public SystemDateThread(String name,String ip,int port,SetInfo info,Long startTime) { + super(ip, port); + this.name = name; + this.info = info; + this.startTime = startTime; + } + + + /** + * 发送通讯获取NC系统时间监测设置模块开发 + * 功能实现分为两部分: + * 第一部分、通讯获取信息:系统时间 + * 第二部分、整理监测结果数据 + */ + public void run() { + + Thread.currentThread().setName(name+" "+ip); + Long sitInfoId = info.getId();//监测设置id + Long maxTimes = info.getCheckMaxTimes();//监测设置最大尝试次数 + Long checkTime = System.currentTimeMillis(); + String processIden = info.getProcessIden(); + + try { + + int a = 0; + DetectInfo detectInfo = null; + //-- 尝试次数范围内发送通信获取NC系统时间 + loop: + for(; a < maxTimes ;){ + detectInfo = this.getSystemDateBysocket(); + a++; + if(detectInfo != null){ //成功,如果第一次就成功的话,那么尝试次数为1 + break loop; + }else{ //失败 + continue loop; + } + } + + if(detectInfo == null){ + detectInfo = new DetectInfo(); +// detectInfo.setDescInfo("获取该节点系统时间失败"); +// detectInfo.setDescInfo("Getting the time failure of the node system"); + detectInfo.setDescInfo("i18n_server.SystemDateThread.getSysTimeFail_n81i"); + } + detectInfo.setTestTimes(a); + detectInfo.setCheckTime(checkTime); + + //-- 生成监测数据 + MonitorUtil.createMonitorData(ip, info, startTime, Common.getAlarmInfoMap().get(info.getId()+ ""), detectInfo); + } catch (Exception ex) { + logger.error("Getting the time anomaly of NC system",ex); + String[] alarm = Common.alarmExceptionInfo(sitInfoId, ip, Constants.DETEC_SYSTEMDATE_STR, processIden, startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, ex.getMessage()); + Common.addAlarmData(alarm); + } + } + + + /** + * 创建通讯获取NC系统时间 + * 将成功信息整理到 DetectInfo 中并返回 + * 失败返回 null + * @time Jun 4, 2013-10:34:41 AM + * @return + */ + private DetectInfo getSystemDateBysocket(){ + + DetectInfo detectInfo = null; + //set up a connection + try { + //-- 创建通讯 + this.createClientSocket(); + + //-- 发送获取系统时间命令 + sendMessage(SocketCMD.REQ_CLIENT_SERVER_SYSTEMDATE); + + //-- 接收系统时间信息 + String msg = receiveMessage(); + logger.debug("服务器系统时间:" + msg); + //-- 回复通讯完成标识 + this.sendMessage(SUCCESS); + + //-- systemdate 监测设置 详细数据整理 + long localDate = System.currentTimeMillis();// 服务器系统时间=DC本地时间 + long sysDate = Long.parseLong(msg);// 客户机系统时间=被监测机时间 + long timeDelay = Math.abs(getMinutesFromBeginToEnd(sysDate, localDate)); + + //-- 信息保存到监测信息对象中 + detectInfo = new DetectInfo(); + detectInfo.getDetailDatas().add(localDate + MonitorUtil.SEPARATOR + sysDate + MonitorUtil.SEPARATOR + timeDelay); + detectInfo.setDescInfo("server:" + + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS,new Date(localDate)) + + " agent:" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS,new Date(sysDate))); + + } catch (Exception e) { + logger.error("Getting system time failure:"+e.getMessage()); + }finally{ + close(); + } + + return detectInfo; + } + + public static long getMinutesFromBeginToEnd(long begin, long end) { + return (end - begin) / (60 * 1000); + } + + public static void main(String [] args) throws UnknownHostException, SocketException, IOException, InterruptedException { +// MonitorManagerThread + +// int a = 0; +// DetectInfo detectInfo = null; +// //-- 尝试次数范围内发送通信获取NC系统时间 +// loop: +// for(; a < 5 ;){ +// detectInfo = new DetectInfo(); +// a++; +// if(detectInfo != null){ //成功 +// break loop; +// } +// } +// +// System.out.println(a); + } + +} diff --git a/src/com/nms/server/thread/monitor/test/DPSwitchProt64Data.java b/src/com/nms/server/thread/monitor/test/DPSwitchProt64Data.java new file mode 100644 index 0000000..9da2183 --- /dev/null +++ b/src/com/nms/server/thread/monitor/test/DPSwitchProt64Data.java @@ -0,0 +1,981 @@ +package com.nms.server.thread.monitor.test; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class DPSwitchProt64Data { + + public static Map mapForDPIfXTable() { + Map map = new HashMap(); + map.put("1.3.6.1.2.1.2.2.1.10","1.3.6.1.2.1.31.1.1.1.6"); //ifHCInOctets:"ifInOctets","ifHCInOctets" + map.put("1.3.6.1.2.1.2.2.1.11","1.3.6.1.2.1.31.1.1.1.7"); //ifHCInUcastPkts:"ifInUcastPkts","HCInUCastPkts" + map.put("1.3.6.1.2.1.2.2.1.12","1.3.6.1.2.1.31.1.1.1.8"); //ifHCInMulticastPkts:"ifInNUcastPkts","HCIMulCastPkts" +// map.put("","HCIBroCastPkts"); //ifHCInBroadcastPkts: + map.put("1.3.6.1.2.1.2.2.1.16","1.3.6.1.2.1.31.1.1.1.10"); //ifHCOutOctets:"ifOutOctets","HCOutOctets" + map.put("1.3.6.1.2.1.2.2.1.17","1.3.6.1.2.1.31.1.1.1.11"); //ifHCOutUcastPkts:"ifOutUcastPkts","HCOUcastPkts" + map.put("1.3.6.1.2.1.2.2.1.18","1.3.6.1.2.1.31.1.1.1.12"); //ifHCOutMulticastPkts:"ifOutNUcastPkts","HCOMulCastPkts" + map.put("1.3.6.1.2.1.31.1.1.1.15.1.1000000","1.3.6.1.2.1.31.1.1.1.15"); //ifHighSpeed + map.put("1.3.6.1.2.1.2.2.1.2","1.3.6.1.2.1.31.1.1.1.1"); //ifdesc +// map.put("","HCOBroCastPkts"); //ifHCOutBroadcastPkts: + return map; + } + public static Map getIfXTableDataIndexMode() { + Map parMap = new HashMap(); + parMap.put("0","262"); + parMap.put("1","390"); + parMap.put("2","518"); + parMap.put("3","646"); + parMap.put("4","774"); + parMap.put("5","902"); + parMap.put("6","1030"); + parMap.put("7","1158"); + parMap.put("8","1286"); + parMap.put("9","1414"); + parMap.put("10","1542"); + parMap.put("11","1670"); + parMap.put("12","1798"); + parMap.put("13","100663367"); + parMap.put("14","100663368"); + parMap.put("15","100663369"); + parMap.put("16","100663370"); + parMap.put("17","100663371"); + parMap.put("18","100663424"); + parMap.put("19","100663425"); + parMap.put("20","100663426"); + parMap.put("21","100663427"); + parMap.put("22","100663428"); + parMap.put("23","100663429"); + parMap.put("24","100663430"); + parMap.put("25","100663431"); + parMap.put("26","100663432"); + parMap.put("27","100663433"); + parMap.put("28","100663434"); + parMap.put("29","100663435"); + parMap.put("30","100663488"); + parMap.put("31","100663489"); + parMap.put("32","100663490"); + parMap.put("33","100663491"); + parMap.put("34","100663492"); + parMap.put("35","100663493"); + parMap.put("36","100663494"); + parMap.put("37","100663495"); + parMap.put("38","100663496"); + parMap.put("39","100663497"); + parMap.put("40","100663498"); + parMap.put("41","100663499"); + parMap.put("42","184549377"); + + +// parMap.put("0","262"); +// parMap.put("1","390"); +// parMap.put("2","518"); +// parMap.put("3","646"); +// parMap.put("4","774"); +// parMap.put("5","902"); +// parMap.put("6","1030"); +// parMap.put("7","1158"); +// parMap.put("8","1286"); +// parMap.put("9","1414"); +// parMap.put("10","1542"); +// parMap.put("11","1670"); +// parMap.put("12","1798"); +// parMap.put("13","1926"); +// parMap.put("14","2054"); +// parMap.put("15","2182"); +// parMap.put("16","2310"); +// parMap.put("17","2438"); +// parMap.put("18","2566"); +// parMap.put("19","2694"); +// parMap.put("20","2822"); +// parMap.put("21","2950"); +// parMap.put("22","3078"); +// parMap.put("23","3206"); +// parMap.put("24","3334"); +// parMap.put("25","3462"); +// parMap.put("26","3590"); +// parMap.put("27","3718"); +// parMap.put("28","3846"); +// parMap.put("29","3974"); +// parMap.put("30","4102"); +// parMap.put("31","4230"); +// parMap.put("32","4358"); +// parMap.put("33","4486"); +// parMap.put("34","4614"); +// parMap.put("35","4742"); +// parMap.put("36","4870"); +// parMap.put("37","4998"); +// parMap.put("38","5126"); +// parMap.put("39","5254"); +// parMap.put("40","5382"); +// parMap.put("41","5510"); +// parMap.put("42","5638"); + return parMap; + } + /** + * @time Mar 18, 2013-4:30:45 PM + * @param args + */ + public static Map> getIfXTableDataMode() { + Map> mapsAll = new HashMap>(); + Map maps1 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.1", maps1); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.262","vlan-if1"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.390","vlan-if4095"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.518","tunnel_ssl0"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.646","meth0_0"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.774","meth0_1"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.902","veth0_31"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.1030","pos1_0"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.1158","pos1_1"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.1286","pos1_2"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.1414","pos1_3"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.1542","eth1_4"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.1670","eth1_5"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.1798","eth1_6"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663367","eth1_7"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663368","eth1_8"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663369","eth1_9"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663370","eth1_10"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663371","eth1_11"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663424","pos2_0"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663425","pos2_1"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663426","pos2_2"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663427","pos2_3"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663428","eth2_4"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663429","eth2_5"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663430","eth2_6"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663431","eth2_7"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663432","eth2_8"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663433","eth2_9"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663434","eth2_10"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663435","eth2_11"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663488","pos3_0"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663489","pos3_1"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663490","pos3_2"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663491","pos3_3"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663492","eth3_4"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663493","eth3_5"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663494","eth3_6"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663495","eth3_7"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663496","eth3_8"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663497","eth3_9"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663498","eth3_10"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.100663499","eth3_11"); + maps1.put("1.3.6.1.2.1.31.1.1.1.1.184549377","template1"); + Map maps2 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.2", maps2); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.262","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.390","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.518","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.646","10598659"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.774","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.902","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.1030","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.1158","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.1286","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.1414","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.1542","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.1670","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.1798","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663367","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663368","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663369","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663370","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663371","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663424","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663425","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663426","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663427","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663428","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663429","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663430","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663431","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663432","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663433","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663434","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663435","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663488","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663489","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663490","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663491","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663492","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663493","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663494","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663495","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663496","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663497","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663498","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.100663499","0"); + maps2.put("1.3.6.1.2.1.31.1.1.1.2.184549377","0"); + Map maps3 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.3", maps3); + + maps3.put("1.3.6.1.2.1.31.1.1.1.3.262","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.390","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.518","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.646","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.774","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.902","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.1030","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.1158","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.1286","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.1414","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.1542","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.1670","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.1798","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663367","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663368","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663369","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663370","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663371","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663424","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663425","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663426","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663427","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663428","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663429","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663430","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663431","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663432","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663433","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663434","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663435","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663488","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663489","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663490","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663491","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663492","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663493","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663494","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663495","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663496","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663497","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663498","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.100663499","0"); + maps3.put("1.3.6.1.2.1.31.1.1.1.3.184549377","0"); + + Map maps4 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.4", maps4); + + maps4.put("1.3.6.1.2.1.31.1.1.1.4.262","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.390","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.518","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.646","10598659"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.774","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.902","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.1030","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.1158","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.1286","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.1414","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.1542","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.1670","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.1798","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663367","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663368","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663369","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663370","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663371","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663424","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663425","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663426","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663427","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663428","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663429","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663430","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663431","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663432","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663433","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663434","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663435","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663488","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663489","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663490","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663491","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663492","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663493","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663494","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663495","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663496","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663497","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663498","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.100663499","0"); + maps4.put("1.3.6.1.2.1.31.1.1.1.4.184549377","0"); + Map maps5 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.5", maps5); + + maps5.put("1.3.6.1.2.1.31.1.1.1.5.262","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.390","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.518","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.646","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.774","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.902","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.1030","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.1158","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.1286","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.1414","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.1542","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.1670","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.1798","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663367","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663368","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663369","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663370","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663371","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663424","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663425","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663426","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663427","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663428","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663429","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663430","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663431","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663432","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663433","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663434","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663435","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663488","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663489","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663490","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663491","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663492","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663493","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663494","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663495","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663496","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663497","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663498","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.100663499","0"); + maps5.put("1.3.6.1.2.1.31.1.1.1.5.184549377","0"); + + Map maps6 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.6", maps6); + + maps6.put("1.3.6.1.2.1.31.1.1.1.6.262","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.390","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.518","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.646","803961719"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.774","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.902","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.1030","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.1158","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.1286","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.1414","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.1542","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.1670","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.1798","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663367","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663368","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663369","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663370","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663371","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663424","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663425","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663426","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663427","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663428","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663429","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663430","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663431","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663432","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663433","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663434","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663435","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663488","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663489","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663490","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663491","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663492","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663493","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663494","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663495","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663496","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663497","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663498","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.100663499","0"); + maps6.put("1.3.6.1.2.1.31.1.1.1.6.184549377","0"); + + Map maps7 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.7", maps7); + + maps7.put("1.3.6.1.2.1.31.1.1.1.7.262","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.390","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.518","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.646","49768"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.774","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.902","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.1030","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.1158","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.1286","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.1414","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.1542","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.1670","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.1798","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663367","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663368","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663369","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663370","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663371","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663424","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663425","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663426","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663427","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663428","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663429","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663430","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663431","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663432","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663433","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663434","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663435","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663488","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663489","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663490","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663491","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663492","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663493","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663494","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663495","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663496","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663497","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663498","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.100663499","0"); + maps7.put("1.3.6.1.2.1.31.1.1.1.7.184549377","0"); + + Map maps8 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.8", maps8); + + maps8.put("1.3.6.1.2.1.31.1.1.1.8.262","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.390","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.518","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.646","10598659"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.774","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.902","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.1030","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.1158","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.1286","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.1414","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.1542","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.1670","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.1798","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663367","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663368","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663369","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663370","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663371","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663424","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663425","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663426","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663427","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663428","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663429","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663430","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663431","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663432","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663433","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663434","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663435","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663488","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663489","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663490","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663491","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663492","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663493","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663494","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663495","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663496","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663497","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663498","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.100663499","0"); + maps8.put("1.3.6.1.2.1.31.1.1.1.8.184549377","0"); + + Map maps9 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.9", maps9); + + maps9.put("1.3.6.1.2.1.31.1.1.1.9.262","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.390","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.518","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.646","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.774","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.902","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.1030","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.1158","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.1286","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.1414","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.1542","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.1670","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.1798","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663367","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663368","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663369","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663370","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663371","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663424","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663425","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663426","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663427","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663428","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663429","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663430","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663431","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663432","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663433","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663434","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663435","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663488","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663489","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663490","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663491","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663492","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663493","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663494","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663495","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663496","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663497","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663498","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.100663499","0"); + maps9.put("1.3.6.1.2.1.31.1.1.1.9.184549377","0"); + + Map maps10 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.10", maps10); + + maps10.put("1.3.6.1.2.1.31.1.1.1.10.262","8684"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.390","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.518","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.646","2134410"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.774","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.902","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.1030","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.1158","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.1286","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.1414","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.1542","746"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.1670","746"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.1798","652"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663367","652"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663368","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663369","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663370","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663371","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663424","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663425","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663426","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663427","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663428","820"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663429","820"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663430","820"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663431","820"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663432","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663433","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663434","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663435","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663488","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663489","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663490","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663491","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663492","652"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663493","652"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663494","652"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663495","652"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663496","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663497","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663498","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.100663499","0"); + maps10.put("1.3.6.1.2.1.31.1.1.1.10.184549377","0"); + + Map maps11 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.11", maps11); + + maps11.put("1.3.6.1.2.1.31.1.1.1.11.262","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.390","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.518","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.646","11212"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.774","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.902","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.1030","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.1158","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.1286","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.1414","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.1542","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.1670","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.1798","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663367","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663368","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663369","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663370","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663371","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663424","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663425","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663426","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663427","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663428","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663429","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663430","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663431","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663432","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663433","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663434","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663435","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663488","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663489","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663490","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663491","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663492","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663493","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663494","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663495","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663496","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663497","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663498","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.100663499","0"); + maps11.put("1.3.6.1.2.1.31.1.1.1.11.184549377","0"); + + Map maps12 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.12", maps12); + + maps12.put("1.3.6.1.2.1.31.1.1.1.12.262","106"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.390","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.518","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.646","6"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.774","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.902","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.1030","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.1158","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.1286","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.1414","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.1542","9"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.1670","9"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.1798","8"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663367","8"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663368","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663369","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663370","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663371","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663424","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663425","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663426","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663427","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663428","10"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663429","10"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663430","10"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663431","10"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663432","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663433","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663434","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663435","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663488","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663489","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663490","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663491","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663492","8"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663493","8"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663494","8"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663495","8"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663496","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663497","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663498","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.100663499","0"); + maps12.put("1.3.6.1.2.1.31.1.1.1.12.184549377","0"); + + Map maps13 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.13", maps13); + + maps13.put("1.3.6.1.2.1.31.1.1.1.13.262","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.390","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.518","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.646","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.774","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.902","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.1030","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.1158","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.1286","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.1414","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.1542","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.1670","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.1798","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663367","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663368","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663369","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663370","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663371","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663424","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663425","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663426","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663427","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663428","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663429","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663430","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663431","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663432","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663433","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663434","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663435","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663488","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663489","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663490","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663491","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663492","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663493","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663494","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663495","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663496","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663497","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663498","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.100663499","0"); + maps13.put("1.3.6.1.2.1.31.1.1.1.13.184549377","0"); + + Map maps14 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.14", maps14); + + maps14.put("1.3.6.1.2.1.31.1.1.1.14.262","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.390","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.518","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.646","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.774","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.902","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.1030","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.1158","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.1286","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.1414","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.1542","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.1670","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.1798","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663367","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663368","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663369","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663370","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663371","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663424","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663425","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663426","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663427","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663428","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663429","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663430","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663431","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663432","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663433","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663434","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663435","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663488","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663489","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663490","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663491","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663492","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663493","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663494","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663495","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663496","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663497","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663498","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.100663499","1"); + maps14.put("1.3.6.1.2.1.31.1.1.1.14.184549377","1"); + + Map maps15 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.15", maps15); + + maps15.put("1.3.6.1.2.1.31.1.1.1.15.262","0"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.390","0"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.518","0"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.646","100"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.774","0"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.902","0"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.1030","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.1158","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.1286","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.1414","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.1542","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.1670","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.1798","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663367","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663368","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663369","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663370","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663371","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663424","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663425","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663426","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663427","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663428","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663429","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663430","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663431","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663432","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663433","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663434","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663435","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663488","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663489","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663490","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663491","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663492","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663493","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663494","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663495","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663496","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663497","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663498","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.100663499","10000"); + maps15.put("1.3.6.1.2.1.31.1.1.1.15.184549377","0"); + + Map maps16 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.16", maps16); + + maps16.put("1.3.6.1.2.1.31.1.1.1.16.262","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.390","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.518","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.646","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.774","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.902","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.1030","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.1158","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.1286","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.1414","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.1542","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.1670","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.1798","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663367","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663368","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663369","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663370","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663371","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663424","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663425","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663426","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663427","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663428","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663429","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663430","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663431","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663432","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663433","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663434","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663435","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663488","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663489","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663490","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663491","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663492","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663493","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663494","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663495","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663496","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663497","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663498","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.100663499","2"); + maps16.put("1.3.6.1.2.1.31.1.1.1.16.184549377","2"); + + Map maps17 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.17", maps17); + + + maps17.put("1.3.6.1.2.1.31.1.1.1.17.262","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.390","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.518","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.646","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.774","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.902","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.1030","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.1158","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.1286","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.1414","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.1542","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.1670","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.1798","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663367","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663368","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663369","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663370","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663371","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663424","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663425","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663426","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663427","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663428","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663429","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663430","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663431","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663432","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663433","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663434","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663435","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663488","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663489","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663490","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663491","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663492","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663493","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663494","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663495","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663496","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663497","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663498","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.100663499","1"); + maps17.put("1.3.6.1.2.1.31.1.1.1.17.184549377","1"); + + Map maps18 = new HashMap(); + mapsAll.put("1.3.6.1.2.1.31.1.1.1.18", maps18); + + + maps18.put("1.3.6.1.2.1.31.1.1.1.18.262",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.390",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.518",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.646",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.774",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.902",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.1030",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.1158",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.1286",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.1414",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.1542",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.1670",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.1798",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663367",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663368",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663369",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663370",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663371",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663424",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663425",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663426",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663427",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663428",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663429",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663430",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663431",""); + maps18.put("1.3.6.1.2.1.31.1.1.1.18.100663432",""); + return mapsAll; + + } + + public static void formatData() { + File file = new File("C:/Documents and Settings/Administrator.LENOVO-2D30CC47/桌面/snmpwalk/dp64w_testData.log"); + File file2 = new File("C:/Documents and Settings/Administrator.LENOVO-2D30CC47/桌面/snmpwalk/dp64w_testData2.log"); + FileReader fr = null; + FileWriter fw = null; + try { + fr = new FileReader(file); + BufferedReader br = new BufferedReader(fr); + fw = new FileWriter(file2); + BufferedWriter bw = new BufferedWriter(fw); + String line = null; + while((line=br.readLine())!=null){ + bw.write("\""+line+"\"+\n"); + } + } catch (Exception e) { + e.printStackTrace(); + }finally{ + try { + fr.close(); + fw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/com/nms/server/thread/monitor/util/MonitorUtil.java b/src/com/nms/server/thread/monitor/util/MonitorUtil.java new file mode 100644 index 0000000..83f3963 --- /dev/null +++ b/src/com/nms/server/thread/monitor/util/MonitorUtil.java @@ -0,0 +1,351 @@ +package com.nms.server.thread.monitor.util; + +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.AlarmInfo; +import com.nms.server.bean.DetectInfo; +import com.nms.server.bean.SetInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.CSVUtils; + + +/** + * 监测数据结果整理类 + * 根据监测设置信息整理监测信息,并做告警处理 + */ +public class MonitorUtil{ + private static Logger logger = Logger.getLogger(MonitorUtil.class); + + public static final String SEPARATOR = "@#@"; + + + /** + * + * TODO + * @author Administrator Jun 20, 2013 + * @version 1.0 + * @param ip + * @param setInfo:监测设置 + * @param startTime:该监测启动的时间 + * @param alarmInfos:当前监测设置的告警设置信息,data_police_relation表 + * @param detectInfo:监测数据相关详细信息 + */ + public static void createMonitorData(String ip, SetInfo setInfo, Long startTime, List alarmInfos,DetectInfo detectInfo) { + + Date writeDate = new Date(); + int testTimes = detectInfo.getTestTimes(); //尝试次数 + + try { + /* + * 2013-4-11 数据顺序调整如下: + * 总数据:本机IP,监测设置ID,监测类别,进程名称(监测类别设置名称),监测服务启动时间,检测时延(秒),本次检测时间,尝试次数,下次计划监测时间, + * 执行状态是否成功是否正常(用于报警:-1执行失败,0不正常,1正常),状态信息(描述信息),性能数据 + * 详细信息:details,详细信息条数(要入到对应的监测类别信息表的数据条数) + * 告警列序号,告警级别,告警值,监测具体数据信息(多列),,,, + */ + + String checkDelayTime = getCheckDelayTime(detectInfo.getCheckTime());// 检测时延 + + // ------- 数据状态(告警)和详细监测数据组织 + int totalStatus = Constants.DETECTION_INFO_DETEC_RIGHT; + int totalAlarmLevel = 99;//似乎没用? + StringBuffer functionSb = new StringBuffer(); + List detailDataList = new LinkedList(); + if (detectInfo == null || detectInfo.getDetailDatas() == null || detectInfo.getDetailDatas().size() == 0) {// 执行失败 + totalStatus = Constants.DETECTION_INFO_DETEC_ERROR;//监测信息异常 + // detectInfo不为空,即无具体的详细数据,有描述信息(目前是针对进程与系统时间) + if (detectInfo != null){ + // 如果是进程监测, 找不到进程或找到多个进程时,告警;如果时间差监测,获取DC系统时间失败,告警 + + //因为肯定会有一条监测数据入库,如果再入告警数据,就重复了? +// String[] alarm = Common.alarmExceptionInfo(setInfo.getId(), ip, setInfo.getCheckTypeName(), "", startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, detectInfo.getDescInfo()); +// Common.addAlarmData(alarm);//所有异常监测,都发主动告警 + } + + logger.info("预设监测 setId: " + setInfo.getId() + " > " + setInfo.getCheckTypeName() + "_" + setInfo.getProcessIden() + ",获取数据失败"); + }else {// 即正常生成详细监测数据 + + List detailDatas = detectInfo.getDetailDatas(); + for(int i=0; i Integer.parseInt(alarmArr[4])){// 将更高的告警级别赋给总告警级别 + totalAlarmLevel = Integer.parseInt(alarmArr[4]); + } + } + if(detailDatas.size()>1 && alarmArr[3].length()>0){ + functionSb.append("【" + detailData[0] + "】 " + alarmArr[3] + "
");// 组织性能数据 + }else{ + functionSb.append(alarmArr[3]);// 组织性能数据 + } + for (int j = 0; j < detailData.length; j++) {// 监测具体数据信息 + dataArr[index + j] = detailData[j]; + } + + detailDataList.add(dataArr); + } + } + + // ------- 主动告警 +// String totalAlarmInfo = null; +// if(totalStatus == Constants.DETECTION_INFO_DETEC_RIGHT){ + // 本次数据正常,看原本的状态:若是告警状态,则发送恢复通知;若是正常状态,则不变 +// if(alarmState){ +// totalAlarmInfo = "【数据状态恢复正常】
" + functionSb.toString(); +// } +// alarmState = false; +// alarmTimes = 0; +// }else if(totalStatus == Constants.DETECTION_INFO_DETEC_WRONG){ +// alarmTimes ++; +// if(!alarmState){// 本次数据不正常,看原本的状态:若是正常状态,则发送告警 +// totalAlarmInfo = "【数据状态异常,超过告警值】
" + functionSb.toString(); +// } +// alarmState = true; +// } + +// if(totalAlarmInfo!=null){ +// String[] alarm = Common.alarmExceptionInfo(setInfo.getId(), pingIp, setInfo.getCheckTypeName(), "", startTime, 1,Constants.DETECTION_INFO_ALARM_WRONG, totalAlarmInfo); +// Common.addAlarmData(alarm); +// } + + // ------- 总监测数据组织 + int index = 0; + String[] totalData = new String[12]; + totalData[index++] = Common.getUUIDByIp(ip)+"";// UUID + totalData[index++] = setInfo.getId() + "";// 监测设置ID + totalData[index++] = setInfo.getCheckTypeName();// 监测类别 + totalData[index++] = setInfo.getProcessIden();// 进程名称 + totalData[index++] = startTime+"";// 监测服务启动时间 + totalData[index++] = checkDelayTime;// 检测时延(秒) + totalData[index++] = writeDate.getTime() + "";// 本次检测时间 + totalData[index++] = testTimes + "";// 尝试次数 + totalData[index++] = getNextCheckTime(writeDate,setInfo.getCheckGap().intValue());// 下次计划监测时间 + totalData[index++] = totalStatus + "";// 执行状态是否成功是否正常:state + totalData[index++] = (detectInfo == null ? "" : detectInfo.getDescInfo());// 状态信息(描述信息):失败的原因等? + if(Constants.DETEC_NMSC_STR.toLowerCase().equals(setInfo.getCheckTypeName().toLowerCase())){//如果是握手监测,则性能数据和状态信息一样,而不是取告警值的信息 + totalData[index++] = (detectInfo == null ? "" : detectInfo.getDescInfo());// 性能数据 + }else { + totalData[index++] = StringUtils.isBlank(functionSb.toString())?(detectInfo == null ? "" : detectInfo.getDescInfo()):functionSb.toString();// 性能数据,就是performace_data字段 + } + + List dataList = new LinkedList(); + // 总数据 + dataList.add(totalData); + // 详细信息 + dataList.add(new String[]{"details", detailDataList.size() + ""});// details(解析标识),详细信息条数 + dataList.addAll(detailDataList);// 具体的详细数据 + + // 特殊数据定制(目前是针对系统信息监测systeminfo) add by zg 0604(DC未使用,须详细测试) + if (detectInfo!=null && detectInfo.getRelatedDatas() != null) {//网卡和硬盘详细信息 + Set>> set = detectInfo.getRelatedDatas().entrySet(); + for(Map.Entry> entry : set){ + dataList.add(new String[]{entry.getKey(), entry.getValue().size() + ""});// 解析标识, 行数(当前解析标识指定的类型有多少条数据) + dataList.addAll(entry.getValue());// 多条数据 + } + } + + for(String[] val : dataList){ + logger.debug(Arrays.toString(val)); + } + + //- 存入缓存 + try { + Common.addDeteData(CSVUtils.csvBytesPrinter1(dataList, Constants.COMMON_TEXT_CODING)); + logger.debug("监测数据以 CSV格式 保存完成"); + } catch (Exception e) { + logger.error("Monitoring data to save exceptions in CSV format", e); + } + + } catch (Exception e) { + logger.error("Error generating monitoring data" ,e); + } + + } + + // 获得当前的报警设置信息,与相应字段值比较:sysData是按showNum排列的 + private static String[] getAlarmState(String[] sysData,List alarmInfos){ + // strs数组数据依次为:告警序列号、告警级别、告警值、性能数据、本次最高告警级别 + String[] strs = new String[]{"", "", "", "", ""}; + if(alarmInfos==null){ + return strs; + } + int maxAlarmLevel = 99; + for (AlarmInfo alarm : alarmInfos) { + if (sysData.length < alarm.getShowNum()) {// 得到的数据个数和告警列数比较,如果设置的告警字段的showNum比总的告警信息字段数还大,则此告警设置不对 + continue; + } + String data = sysData[alarm.getShowNum() - 1]; + boolean alarmFlag = false; + if ("equals".equalsIgnoreCase(alarm.getPoliceSysmbols())) {//相同 + if(data.equals(alarm.getPoliceValue())){ + alarmFlag = true; + } + } else if ("include".equalsIgnoreCase(alarm.getPoliceSysmbols())) {//包含告警值内容 + if(data.contains(alarm.getPoliceValue())){ + alarmFlag = true; + } + } else if ("exclude".equalsIgnoreCase(alarm.getPoliceSysmbols())) {//不包含告警值内容 + if(!data.contains(alarm.getPoliceValue())){ + alarmFlag = true; + } + } else { + double result = Double.parseDouble(data) - Double.parseDouble(alarm.getPoliceValue());//一定就是数字吗?web界面貌似没做数字限定 + if ((">".equals(alarm.getPoliceSysmbols()) && result > 0) + || ("<".equals(alarm.getPoliceSysmbols()) && result < 0) + || ("=".equals(alarm.getPoliceSysmbols()) && result == 0) + || (">=".equals(alarm.getPoliceSysmbols()) && result >= 0) + || ("<=".equals(alarm.getPoliceSysmbols()) && result <=0) ) { + alarmFlag = true; + } + } + String sysmbol = getAlarmSymbol(alarm.getPoliceSysmbols(), alarmFlag); + if(alarmFlag){ + strs[0] += alarm.getShowNum() + "|";// 告警序列号 + strs[1] += alarm.getPoliceLevel() + "|"; //告警级别 + strs[2] += alarm.getPoliceValue() + "|"; //告警值 + //性能信息 +// strs[3] += alarm.getFiledCommonts() +// + sysData[alarm.getShowNum() - 1] +// + alarm.getPoliceUnit() + sysmbol + "告警值" +// + alarm.getPoliceValue() +// + alarm.getPoliceUnit() + " 不正常;"; + strs[3] += alarm.getFiledCommonts() + + sysData[alarm.getShowNum() - 1] + + alarm.getPoliceUnit() + sysmbol + "i18n_server.MonitorUtil.warnValue_n81i" + + alarm.getPoliceValue() + + alarm.getPoliceUnit() + " i18n_server.MonitorUtil.abnormal_n81i;"; + + //2011-09-29 添加了连续几次达到告警值后主动告警,恢复正常后发送恢复信息 + //2013-03-26 添加了告警状态控制是否立刻主动告警,如果已经告警则后期不发送告警信息。 + //alarmHandler(alarm, strs[3]); + + if (maxAlarmLevel > alarm.getPoliceLevel()) {// 保留本次最高告警级别,值越小级别越高 + maxAlarmLevel = alarm.getPoliceLevel(); + } + + }else{ + // 性能信息 +// strs[3] += alarm.getFiledCommonts() +// + sysData[alarm.getShowNum() - 1] +// + alarm.getPoliceUnit() + sysmbol + "告警值" +// + alarm.getPoliceValue() +// + alarm.getPoliceUnit() + " 正常;"; + strs[3] += alarm.getFiledCommonts() + + sysData[alarm.getShowNum() - 1] + + alarm.getPoliceUnit() + sysmbol + "i18n_server.MonitorUtil.warnValue_n81i" + + alarm.getPoliceValue() + + alarm.getPoliceUnit() + " i18n_server.MonitorUtil.normal_n81i;"; + //对发送告警信息后,恢复正常的信息(为保证告警成对) + //2013-03-26 当告警恢复后发送恢复告警信息。 + //alarmRecovery(alarm, strs[3]); + + } + + }//for end + for (int i = 0; i < strs.length-1; i++) { + if (strs[i].length() > 0) { + strs[i] = strs[i].substring(0, strs[i].length() - 1); + } + } + strs[strs.length-1] = maxAlarmLevel + "";// 本次告警最高级别 + return strs; + } + + private static String getAlarmSymbol(String oldSymbol, boolean alarmFlag){ + String symbol = ""; + if(alarmFlag){ +// if(">".equals(oldSymbol)){ +// symbol = "大于"; +// }else if(">=".equals(oldSymbol)){ +// symbol = "超过"; +// }else if("<".equals(oldSymbol)){ +// symbol = "小于"; +// }else if("<=".equals(oldSymbol)){ +// symbol = "未超过"; +// }else if("=".equals(oldSymbol)){ +// symbol = "等于"; +// }else{ +// symbol = oldSymbol; +// } + if(">".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.gt_n81i"; + }else if(">=".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.out_n81i"; + }else if("<".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.lt_n81i"; + }else if("<=".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.in_n81i"; + }else if("=".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.eq_n81i"; + }else{ + symbol = oldSymbol; + } + }else{ +// if(">".equals(oldSymbol)){ +// symbol = "未超过"; +// }else if(">=".equals(oldSymbol)){ +// symbol = "小于"; +// }else if("<".equals(oldSymbol)){ +// symbol = "超过"; +// }else if("<=".equals(oldSymbol)){ +// symbol = "大于"; +// }else if("=".equals(oldSymbol)){ +// symbol = "不等于"; + if(">".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.in_n81i"; + }else if(">=".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.lt_n81i"; + }else if("<".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.out_n81i"; + }else if("<=".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.gt_n81i"; + }else if("=".equals(oldSymbol)){ + symbol = "i18n_server.MonitorUtil.notEquels_n81i"; + }else if("equals".equalsIgnoreCase(oldSymbol)){ + symbol = "not equals"; + }else if("include".equalsIgnoreCase(oldSymbol)){ + symbol = "exclude"; + }else if("exclude".equalsIgnoreCase(oldSymbol)){ + symbol = "include"; + } + } + return symbol; + } + + // 检测时延(秒) + private static String getCheckDelayTime(Long checkTime){ + long val = System.currentTimeMillis() - checkTime; + return val/1000 + ""; + } + + // 下一次检测时间 + private static String getNextCheckTime(Date now,int gap){ + Calendar cal = Calendar.getInstance(); + cal.setTime(now); + cal.add(Calendar.MINUTE, gap); + return cal.getTimeInMillis() + ""; + } +} diff --git a/src/com/nms/server/thread/netty/BusinessType.java b/src/com/nms/server/thread/netty/BusinessType.java new file mode 100644 index 0000000..f5a1c55 --- /dev/null +++ b/src/com/nms/server/thread/netty/BusinessType.java @@ -0,0 +1,40 @@ +package com.nms.server.thread.netty; + +/** + * 消息类型 + * @author nanfang + * + */ +public enum BusinessType { + + HeartBeat("HeartBeat",0),//心跳 + DetectZip("DetectZip",1), // 监测数据zip + DetectData("DetectData",2),// 监测数据 + ObjTaskResult("ObjTaskResult",3),//任务结果 + FileTaskReturn("FileTaskReturn",4);//回传文件 + + private final String name; + private final int type; + + private BusinessType(String name ,int type) { + this.type = type; + this.name = name; + } + + public int getType() { + return this.type; + } + + public String getName(){ + return this.name; + } + + public static BusinessType getType(int type) { + for (BusinessType t : BusinessType.values()) { + if (t.getType() == type) { + return t; + } + } + return null; + } +} \ No newline at end of file diff --git a/src/com/nms/server/thread/netty/Message.java b/src/com/nms/server/thread/netty/Message.java new file mode 100644 index 0000000..5a5094b --- /dev/null +++ b/src/com/nms/server/thread/netty/Message.java @@ -0,0 +1,84 @@ +package com.nms.server.thread.netty; + +import java.io.File; + +public class Message { + + private int messageType;//消息类型 + private BusinessType businessType;//业务类型 + private int length;//消息长度 + private File file;//消息保存的文件 + private String fileName; + private byte[] data; + + public Message(int length,int messageType ,BusinessType type) { + this.length = length; + this.businessType = type; + this.messageType = messageType; + } + public BusinessType getBusinessType() { + return businessType; + } + + public void setBusinessType(BusinessType type) { + this.businessType = type; + } + + public int getLength() { + return length; + } + + public void setLength(int length) { + this.length = length; + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + this.length = (int)file.length(); + } + + public byte[] getData() { + return data; + } + + public void setData(byte[] data) { + this.data = data; + this.length = data.length; + } + + public int getMessageType() { + return messageType; + } + public void setMessageType(int messageType) { + this.messageType = messageType; + } + public String getFileName() { + return fileName; + } + public void setFileName(String fileName) { + this.fileName = fileName; + } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("messageType : "); + sb.append(messageType); + sb.append(",BusinessType : "); + sb.append(businessType); + sb.append(",length : "); + sb.append(length); + if(file != null){ + sb.append(",tempFile:"); + sb.append(file.getAbsolutePath()); + }else{ + sb.append(",content : "); + sb.append(new String(data)); + } + return sb.toString(); + } + +} diff --git a/src/com/nms/server/thread/netty/MessageDecoder.java b/src/com/nms/server/thread/netty/MessageDecoder.java new file mode 100644 index 0000000..9ccd710 --- /dev/null +++ b/src/com/nms/server/thread/netty/MessageDecoder.java @@ -0,0 +1,197 @@ +package com.nms.server.thread.netty; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.List; + +import org.apache.log4j.Logger; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.util.ReferenceCountUtil; + +/** + * 消息解析 + * @author fang + *消息格式 | 4byte | 1byte | 1byte | .................... | + * |bodylength| msg Type| businessType | filename | body | + */ +public class MessageDecoder extends ByteToMessageDecoder { + private final static Logger logger = Logger.getLogger(MessageDecoder.class); + private final int fieldLength = 4;//四个字节保存body length + /** + * 一个字节保存 消息类型,大于 0 为文件, + * 0 为 byte[] ,value 为 文件名的长度, + * 最大支持 127个字节的文件名 + */ + private final int typeLength = 1;// + private final int businessTypeLength = 1;//业务类型 + private final int headerLength = fieldLength + typeLength + businessTypeLength;//协议头长度 + private boolean flag = true;//是否开始解析 + private int bodyLength; + private int messageType;//消息类型 + private BusinessType businessType;//业务类型 + private int remain;//剩余未读取内容长度 + + private File tmpFile; + private String fileName; + private FileOutputStream fos; + private ByteBuf temBuf; + + public MessageDecoder() { + init(); + } + + private void init(){ + this.flag = true; + this.remain = -1; + this.bodyLength = -1; + this.messageType = 0; + this.businessType = null; + this.tmpFile = null; + this.fileName = null; + } + + /** + * 已经成功读取协议头 + * @throws IOException + * @throws FileNotFoundException + */ + private void begin() throws FileNotFoundException, IOException{ + this.flag = false; + if(messageType > 0){ + fos = new FileOutputStream(tmpFile); + }else{ + temBuf = Unpooled.buffer(bodyLength); + } + } + + private void write(byte[] data) throws FileNotFoundException, IOException{ + if(messageType > 0){ + fos.write(data); + fos.flush(); + }else{ + temBuf.writeBytes(data); + } + } + + private void release() throws IOException{ + if(fos != null){ + try { + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + fos = null; + } + if(temBuf != null){ + ReferenceCountUtil.release(temBuf); + temBuf = null; + } + } + + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + Message m = decode(in); + if(m != null){ + out.add(m); + init(); + logger.debug("成功组包一条消息:type : " +m.getBusinessType().getName() +",length : " + m.getLength()); + } + } + + + private Message decode(ByteBuf in) throws Exception{ + int readableBytes = in.readableBytes(); + logger.debug("decode begin,flag:"+flag+ " ,readableBytes:" + readableBytes); + if(flag && readableBytes < (headerLength)){//初始状态,且不能读取 协议头 + return null; + } + try { + byte[] tem = null; + if(flag && readableBytes >= (headerLength) ){ + int fileNamelen = 0; + in.markReaderIndex(); + int bl = in.readInt(); + messageType = in.readByte(); + int bt = in.readByte();//业务类型 + if(messageType < 1){ + remain = bodyLength = bl; + businessType = BusinessType.getType(bt); + }else{//大于100 位 文件 + fileNamelen = messageType ;//文件名长度 + if(readableBytes >= (headerLength + fileNamelen)){ + remain = bodyLength = bl; + businessType = BusinessType.getType(bt); + tem = new byte[fileNamelen]; + in.readBytes(tem);//读取文件名 + String fileName = new String(tem); + this.fileName = fileName; + tmpFile = File.createTempFile("Datacontroller-","-"+fileName); + }else{//不能获取文件名,暂不读取数据 + in.resetReaderIndex(); + return null; + } + } + begin(); + if(readableBytes < (headerLength + bodyLength + fileNamelen)){ + int rl = readableBytes - headerLength - fileNamelen; + tem = new byte[rl]; + in.readBytes(tem); + write(tem); + remain = bodyLength - rl;//剩余 n 个字节 没有读取 + }else{ + tem = new byte[bodyLength]; + in.readBytes(tem); + write(tem); + remain = 0; + } + }else{ + if(readableBytes < remain){ + tem = new byte[readableBytes]; + in.readBytes(tem); + write(tem); + remain = remain - readableBytes; + }else{ + tem = new byte[remain]; + in.readBytes(tem); + write(tem); + remain = 0; + } + } + if(remain == 0){ + Message msg = new Message(bodyLength,messageType, businessType); + if(messageType > 0){ + msg.setFile(tmpFile); + msg.setFileName(this.fileName); + }else{ + msg.setData(temBuf.array()); + } + release(); + return msg; + } + } catch (Exception e) { + if(remain >0){ + in.skipBytes(remain); + } + throw e; + } + logger.debug("decode end,type : "+businessType.getName()+" bodyLength: "+bodyLength+" , remain :" +remain); + return null; + } + + + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + init();//重置 + logger.error("",cause); + ctx.fireExceptionCaught(cause); + } + +} diff --git a/src/com/nms/server/thread/netty/MessageHandler.java b/src/com/nms/server/thread/netty/MessageHandler.java new file mode 100644 index 0000000..9a21277 --- /dev/null +++ b/src/com/nms/server/thread/netty/MessageHandler.java @@ -0,0 +1,248 @@ +package com.nms.server.thread.netty; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.net.InetSocketAddress; +import java.util.Date; +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult2; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.thread.file.UnZipManagerThread; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.handler.timeout.IdleState; +import io.netty.handler.timeout.IdleStateEvent; + +public class MessageHandler extends ChannelInboundHandlerAdapter { + private final static Logger logger = Logger.getLogger(MessageHandler.class); + private ChannelHandlerContext ctx; + private String ip; + private Integer port; + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + if(evt instanceof IdleStateEvent){ + IdleStateEvent ise = (IdleStateEvent)evt; + if(IdleState.ALL_IDLE.equals(ise.state())){ + Common.saveHbInfo(ip, "online", false); + logger.info("ip : "+ip+" 超时"); + ctx.close(); + } + }else{ + ctx.fireUserEventTriggered(evt); + } + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + String res = "success\r\n"; + try { + Message message = (Message)msg; + byte[] data = null; + switch (message.getBusinessType()) { + case HeartBeat://监测zip文件 + // 启动并注册 解压文件线程 + data = message.getData(); + InetSocketAddress addr = (InetSocketAddress)ctx.channel().remoteAddress(); + String ip = addr.getAddress().getHostAddress(); + Common.saveHbInfo(ip, "dcTime", System.currentTimeMillis()); + Common.saveHbInfo(ip, "ncTime", Long.valueOf(new String(data))); + Common.saveHbInfo(ip, "online", true); + //logger.info(ip +" 心跳包 -> " + new Date(Long.valueOf(new String(data))).toLocaleString()); + break; + case DetectData://监测消息 + handlerDetectData(message); + break; + case ObjTaskResult://任务结果 + handlerObjTaskResult(message); + break; + case FileTaskReturn://回传文件 + handlerFileTaskReturn(message); + break; + default: + logger.warn("未知操作"); + break; + } + } catch (Exception e) { + res = "fail\r\n"; + logger.error("",e); + } + ByteBuf resp = Unpooled.copiedBuffer(res.getBytes()); + ctx.writeAndFlush(resp);//给客户端返回成功标识 + } + + /** + * 处理监测数据 + * @param message + */ + public void handlerDetectData(Message message){ + byte[] data = message.getData(); + Common.addDeteData(data); + logger.info("clientIp : " + ip +",成功添加一条监测记录"); + } + + /** + * 处理任务结果 + * @param message + * @throws Exception + * @throws IOException + * @throws ClassNotFoundException + */ + public void handlerObjTaskResult(Message message) throws Exception { + // 接收返回的任务结果:修改为receiveObject + logger.info("数据类型:任务结果的List"); + byte[] data = message.getData(); + ByteArrayInputStream bis = null; + ObjectInputStream ois = null; + try { + bis = new ByteArrayInputStream(data); + ois = new ObjectInputStream(bis); + List resultList = (List) ois.readObject(); + for (String result : resultList) { + logger.info("任务结果:" + result); + String[] misRlt2 = result.split(Constants.COMMON_DATA_SPLIT); + MissionResult2 result2 = Common.resoveMissionResult(misRlt2); + Common.addResultToResultList(result2);// 没有addAll,因为传递的任务结果可能是不同类型的任务结果(1-6) + } + } catch (Exception e) { + logger.error("handler ObjTaskResult error",e); + throw e; + }finally { + + if(ois != null){ + try { ois.close(); } catch (IOException e) {} + } + if(bis != null){ + try { bis.close(); } catch (IOException e) {} + } + } + + } + + /** + * 处理任务回传文件 + * @param message + * @throws Exception + */ + public void handlerFileTaskReturn(Message message) throws Exception{ + File file = message.getFile(); + String fileName = message.getFileName(); + FileInputStream fis = null; + FileOutputStream fos = null; + try { + fis = new FileInputStream(file); + byte[] headerLenbyte = new byte[4]; + fis.read(headerLenbyte); + int headerLen = Common.byteArrayToInt(headerLenbyte); + byte[] msgArr = new byte[headerLen]; + fis.read(msgArr); + String msg1 = new String(msgArr);//消息头 + logger.info("length: " + headerLen+",msg : "+msg1); + String[] result = msg1.split(Constants.COMMON_DATA_SPLIT); + MissionResult2 result2 = Common.resoveMissionResult(result); + File temp = new File(Constants.MISSION_FILE_UPLOAD_DIR + fileName + Constants.TEMP_SUFFIX); + fos = new FileOutputStream(temp); + byte[] buf = new byte[1024]; + int len = -1; + while((len = fis.read(buf)) > 0){ + fos.write(buf, 0, len); + } + fos.flush(); + fos.close(); + logger.debug("回传文件临时文件完成"); + moveFile(temp, new File(Constants.MISSION_FILE_UPLOAD_DIR + fileName)); + logger.debug("回传文件完成"); + result2.setFileInfo(fileName); + Common.addResultToResultList(result2); + logger.debug("任务回传文件处理完成"); + } catch (Exception e) { + logger.error("",e); + throw e; + } finally { + if(fos != null){ + try {fos.close();} catch (IOException e) {} + } + if(fis != null){ + try {fis.close();} catch (IOException e) {} + } + } + } + + /** + * 处理监测zip + * @param message + * @throws IOException + */ + public void handlerDetectZip(Message message) throws IOException{ + // 启动并注册 解压文件线程 + File file = message.getFile(); + String uploadPath = Constants.ZIP_FILE_DETECT_DATA_DIR; + moveFile(file, new File(uploadPath+File.separator +file.getName())); + Future future = Common.getFutureMap().get(Constants.UNZIP_FILE_MANAGER); + if (future == null) { + future = Common.scheduled.scheduleWithFixedDelay( new UnZipManagerThread(), 0, 30,TimeUnit.SECONDS); + Common.registRunnable(Constants.UNZIP_FILE_MANAGER, future); + } + } + /** + * 移动文件 + * @param src + * @param dest + * @throws IOException + */ + public static void moveFile(File src ,File dest) throws IOException{ + boolean f = src.renameTo(dest); + if(!f){ + FileUtils.copyFile(src, dest); + } + } + + @Override + public void handlerAdded(ChannelHandlerContext ctx) throws Exception { + this.ctx = ctx; + InetSocketAddress addr = (InetSocketAddress)ctx.channel().remoteAddress(); + ip = addr.getAddress().getHostAddress(); + port = addr.getPort(); + logger.info("添加->" + ip); + super.handlerAdded(ctx); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + logger.error("error",cause); + Common.saveHbInfo(ip, "online", false); + ctx.close(); + } + + @Override + public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { + Common.saveHbInfo(ip, "online", false); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + Common.saveHbInfo(ip, "online", false); + Common.saveHbInfo(ip, "offlineTime", new Date()); + ctx.close(); + } + + @Override + public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { + Common.saveHbInfo(ip, "online", false); + } + +} diff --git a/src/com/nms/server/thread/netty/NettyServer.java b/src/com/nms/server/thread/netty/NettyServer.java new file mode 100644 index 0000000..d963882 --- /dev/null +++ b/src/com/nms/server/thread/netty/NettyServer.java @@ -0,0 +1,93 @@ +package com.nms.server.thread.netty; + +import java.util.concurrent.Callable; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.socket.SocketUtils; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.ssl.SslHandler; +import io.netty.handler.timeout.IdleStateHandler; +/** + * netty server + * 1、实现 ssl 双向认证 + * 2、自定义了消息解析器 + * 消息解析器实现:内存缓存最大值 maxMemByte,当消息length 超过 maxMemByte ,将消息内容写入文件,否则通过byte[] 数组的方式保存。 + * 消息格式 | 4byte | 1byte |...........| + * |bodylength|bodyType| body | + * 3、消息的业务处理 在 MessageHandler 中处理 + * @author dell + * + */ +public class NettyServer implements Callable { + private final static Logger logger = Logger.getLogger(NettyServer.class); + private int port;//监听端口号 + private boolean ssl;//是否启用ssl + public NettyServer(int port,boolean ssl) { + this.port = port; + this.ssl = ssl; + } + + @Override + public Object call() throws Exception { + Thread.currentThread().setName("NettyServer"); + logger.debug("Thread start"); + final SSLContext sslContext = SocketUtils.getSSLContext();//ssl认证 + EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) + EventLoopGroup workerGroup = new NioEventLoopGroup(); + while(!Common.isStop()&& !Thread.currentThread().isInterrupted()){ + try { + ServerBootstrap b = new ServerBootstrap(); // (2) + b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3) + .childHandler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + + if(ssl){//启用 ssl 通信 + SSLEngine engine = sslContext.createSSLEngine(); + engine.setUseClientMode(false);//服务端模式 + engine.setNeedClientAuth(true);//双向认证 + ch.pipeline().addLast(new SslHandler(engine)); + } + ch.pipeline().addLast(new IdleStateHandler(0, 0, Constants.NETTY_IDLE_TIME)); + ch.pipeline().addLast(new MessageDecoder()); + ch.pipeline().addLast(new MessageHandler()); + } + }).option(ChannelOption.SO_BACKLOG, Constants.NETTY_SO_BACKLOG) // (5) + .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) + ChannelFuture f = b.bind(port).sync(); // (7) + logger.info("server 启动成功"); + f.channel().closeFuture().sync(); + } catch (Exception e) { + e.printStackTrace(); + logger.error("",e); + } finally { + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + } + logger.info("server 关闭"); + } + logger.info("thread end"); + return null; + } + + + public static void main(String[] args) throws Exception { + NettyServer server = new NettyServer(9527,true); + server.call(); + } + +} diff --git a/src/com/nms/server/thread/overrunData/DataFileReaderManagerThread.java b/src/com/nms/server/thread/overrunData/DataFileReaderManagerThread.java new file mode 100644 index 0000000..cdf5cd5 --- /dev/null +++ b/src/com/nms/server/thread/overrunData/DataFileReaderManagerThread.java @@ -0,0 +1,396 @@ +package com.nms.server.thread.overrunData; + +import java.io.File; +import java.io.FileInputStream; +import java.util.Calendar; +import java.util.LinkedList; +import java.util.concurrent.Future; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.thread.InitServerThread; +import com.nms.server.thread.alarmData.AlarmDataResoveThread; +import com.nms.server.thread.detecData.NewDetecDataResoveThread; +import com.nms.server.util.FileUtils; + +/** + * 本地暂时无法入库数据(监测数据、告警数据、任务结果)的解析入库线程 + * + * @author HYX + * + */ +public class DataFileReaderManagerThread implements Runnable { + Logger logger = Logger.getLogger(DataFileReaderManagerThread.class); + + boolean stop = false; + + /** + * 若硬盘数据解析线程正在运行,则退出,否则,根据路径解析文件,存入缓存list,再调用相应的入库线程,最后清理一下临时文件 + */ + public void run() { + + // 为当前线程命名,用与日志友好输出。 +// Thread.currentThread().setName("硬盘数据(监测数据、告警数据、任务结果:由于数据量过大,暂时无法入库,而存放在硬盘上的数据)解析入库管理线程"); + Thread.currentThread().setName("Hard Disk Data (Monitoring Data, Alarm data, Task results: Data stored on hard disk because data is too large, temporarily inaccessible, and stored on the hard disk)"); + + //判断数据库连接是否可用 + if(!Common.isDbConnected()){ + logger.info("数据库连接不可用,操作结束"); + return ; + } + + try { + //要不要配置是否定时操作????? + readDetectFile(); + //readDetectDataFile(); + readWarningDataFile(); + readResultDataFile(); + + } catch (Exception e) { + logger.error("",e); + } + } + + /** + * 读取保存硬盘的监测数据 + */ + public void readDetectFile(){ + try { + + int size = Common.getAllDeteDataNum(); + if(size > Constants.MAX_DETECT_DATA_RESOVE_LIMIT_NUM){ + logger.warn("内存缓存监测数据过多,size :" + size ); + return; + } + logger.info("文件夹路径 :>" + Constants.OVERRUN_DETEC_FILE_DIR); + File driFile = new File(Constants.OVERRUN_DETEC_FILE_DIR); + + if(driFile.exists()) { + size = Constants.MAX_DETECT_DATA_RESOVE_LIMIT_NUM - size; + + // 获取目录下的.csv文件集合 + File[] filesTmp = FileUtils.listAllFilesEndWith(driFile, "csv"); + File[] files = FileUtils.sortByFileNameASC(filesTmp); // 按文件名称 升序排序 + + // 解析文件数为0时,终止该计划任务 + if(files==null || files.length==0){ + return; + } + + int count =0; + for(File f : files){ + if(count > size){ + break; + } + int len = (int)f.length(); + if(len <1){ + continue; + } + FileInputStream fis = null ; + try { + fis = new FileInputStream(f); + byte[] dsb = new byte[len]; + fis.read(dsb); + Common.addDeteData(dsb); + + count ++; + } catch (Exception e) { + FileUtils.copyFile(f, new File(Constants.ERROR_DETEC_FILE_DIR+"/"+f.getName())); + logger.error("Hard disk data (monitoring data) file reading error:"+Constants.ERROR_DETEC_FILE_DIR+"/"+f.getName(),e); + } finally { + if(fis != null){ + fis.close(); + } + boolean delete = f.delete(); + } + } + logger.info("本次共从硬盘读取 " + count + " 个文件"); + } + } catch (Exception e) { + logger.error("",e); + } + } + + + /** + * + * 硬盘监测数据 解析入库 + * @author Administrator Feb 21, 2013 + * @version 1.0 + */ + public void readDetectDataFile() { + try { + + long sTime = System.currentTimeMillis(); + Future future = Common.getFutureMap().get(Constants.OVRERUN_DETEC_DATA_RESOVE); + //线程不存在 创建和注册 + if(future != null && !future.isCancelled() && !future.isDone()){ + logger.info("硬盘监测数据 解析入库线程 运行中 不再启动新线程"); + return ; + }else { + + logger.info("文件夹路径 :>" + Constants.OVERRUN_DETEC_FILE_DIR); + File driFile = new File(Constants.OVERRUN_DETEC_FILE_DIR); + + if(driFile.exists()) { + // 获取目录下的.csv文件集合 + File[] filesTmp = FileUtils.listAllFilesEndWith(driFile, "csv"); + File[] files = FileUtils.sortByFileNameASC(filesTmp); // 按文件名称 升序排序 + + // 解析文件数为0时,终止该计划任务 + if(files==null || files.length==0){ +// Common.cancelRunnableAtOnce(Constants.OVERRUN_DATA_RESOVE_MANAGER);//??? + return; + } + + logger.debug("文件夹路径 :>" + driFile.getAbsolutePath() + " 下 " + files.length + "个文件,解析开始"); + int max = 1000,i=0; + long l = Calendar.getInstance().getTimeInMillis(); + if (files != null && files.length > 0) { + LinkedList dataList = new LinkedList(); + + for (File file : files) { + i++; + if(i>max){ + break; + } + if(file.length()>0){ + try { + FileInputStream fis = new FileInputStream(file); + + int fileLength = (int) file.length(); //大小 + int len = 0; + byte[] buff = new byte[fileLength]; + if(fileLength>0){ + len = fis.read(buff); + } + if(buff.length==0){continue;} + + dataList.add(buff); + fis.close(); + file.delete(); + } catch (Exception e) {//??????? + FileUtils.copyFile(file, new File(Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName())); + logger.error("Hard disk data (monitoring data) file reading error:"+Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName(),e); + } + } + } + + if(dataList!=null&&dataList.size()>0) { + //监测数据 +// future = Common.dataResoveService.submit(new NewDetecDataResoveThread("硬盘监测数据解析执行线程 启动",dataList)); + future = Common.dataResoveService.submit(new NewDetecDataResoveThread("Hard Disk Monitoring Data Parsing Execution Thread Startup",dataList)); + //注册 + Common.getFutureMap().put(Constants.OVRERUN_DETEC_DATA_RESOVE, future); + + } + + } + logger.debug(driFile.getAbsolutePath()+" 解析csv文件完成耗时:>"+(Calendar.getInstance().getTimeInMillis()-sTime)); + + //清理文件 + files = FileUtils.sortByFileNameASC(FileUtils.listAllFilesEndWith(driFile, "tp")); // 按文件名称 升序排序 + if(files!=null && files.length>0){ + for(File file : files){ + file.delete(); + } + } + logger.debug("临时文件清理完成"); + } + } + + } catch (Exception e) { + logger.error("",e); + } + } + + /** + * + * 硬盘告警数据 解析入库 + * @author Administrator Feb 21, 2013 + * @version 1.0 + */ + public void readWarningDataFile() { + try { + + + Future future = Common.getFutureMap().get(Constants.OVRERUN_WARNING_DATA_RESOVE); + //线程不存在 创建和注册 + if(future != null && !future.isCancelled() && !future.isDone()){ + logger.info("硬盘告警数据 解析入库线程 运行中 不再启动新线程"); + return ; + }else { + + logger.info("文件夹路径 :>" + Constants.OVERRUN_WARNING_FILE_DIR); + File driFile = new File(Constants.OVERRUN_WARNING_FILE_DIR); + + if(driFile.exists()) { + // 获取目录下的.csv文件集合 + File[] filesTmp = FileUtils.listAllFilesEndWith(driFile, "csv"); + File[] files = FileUtils.sortByFileNameASC(filesTmp); // 按文件名称 升序排序 + + // 解析文件数为0时,终止该计划任务 + if(files==null || files.length==0){ +// Common.cancelRunnable(Constants.OVERRUN_DATA_RESOVE_MANAGER);//??? + return; + } + logger.debug("文件夹路径 :>" + driFile.getAbsolutePath() + " 下 " + files.length + "个文件,解析开始"); + int max = 500,i=0; + long l = Calendar.getInstance().getTimeInMillis(); + if (files != null && files.length > 0) { + LinkedList dataList = new LinkedList(); + + for (File file : files) { + i++; + if(i>max){ + break; + } + if(file.length()>0){ + try { + String[] warnStr = FileUtils.readFileToString(file).split(Constants.COMMON_DATA_SPLIT); + + dataList.add(warnStr); + file.delete(); + } catch (Exception e) {//??????? + FileUtils.copyFile(file, new File(Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName())); + logger.error("Hard disk data (alarm data) file reading error:"+Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName(),e); + } + } + } + + if(dataList!=null&&dataList.size()>0) { + //告警数据 +// future = Common.service.submit(new AlarmDataResoveThread("硬盘告警数据解析执行线程 启动",dataList)); + future = Common.service.submit(new AlarmDataResoveThread("Hard disk alarm data parsing execution thread startup",dataList)); + //注册 + Common.getFutureMap().put(Constants.OVRERUN_WARNING_DATA_RESOVE, future); + } + + } + logger.debug(driFile.getAbsolutePath()+" 解析完成耗时:>"+(Calendar.getInstance().getTimeInMillis()-l)); + + //清理文件 + files = FileUtils.sortByFileNameASC(FileUtils.listAllFilesEndWith(driFile, "tp")); // 按文件名称 升序排序 + if(files!=null && files.length>0){ + for(File file : files){ + file.delete(); + } + } + logger.debug("临时文件清理完成"); + } + } + + } catch (Exception e) { + logger.error("",e); + } + } + + /** + * + * 硬盘任务结果数据 解析入库 + * @author Administrator Feb 21, 2013 + * @version 1.0 + */ + public void readResultDataFile() { + +// try { +// +// + Future future = Common.getFutureMap().get(Constants.OVRERUN_RESULT_DATA_RESOVE); + //线程不存在 创建和注册 + if(future != null && !future.isCancelled() && !future.isDone()){ + logger.info("硬盘任务结果数据 解析入库线程 运行中 不再启动新线程"); + return ; + }else { + logger.info("文件夹路径 :>" + Constants.OVERRUN_RESULT_FILE_DIR); +// +// //任务结果 +// future = Common.service.submit(new MissionResultFileResoveThread("硬盘任务数据解析执行线程 启动",Constants.OVERRUN_RESULT_FILE_DIR)); + future = Common.service.submit(new MissionResultFileResoveThread("Hard disk alarm data parsing execution thread startup",Constants.OVERRUN_RESULT_FILE_DIR)); +// //注册 + Common.getFutureMap().put(Constants.OVRERUN_RESULT_DATA_RESOVE, future); +// +// /**/ +// +// +// File driFile = new File(Constants.OVERRUN_RESULT_FILE_DIR); +// +// if(driFile.exists()) { +// // 获取目录下的.csv文件集合 +// File[] filesTmp = FileUtils.listAllFilesEndWith(driFile, "result"); +// File[] files = FileUtils.sortByFileNameASC(filesTmp); // 按文件名称 升序排序 +// +// // 解析文件数为0时,终止该计划任务 +// if(files==null || files.length==0){ +//// Common.cancelRunnable(Constants.OVERRUN_DATA_RESOVE_MANAGER);//??? +// return; +// } +// +// logger.debug("文件夹路径 :>" + driFile.getAbsolutePath() + " 下 " + files.length + "个文件,解析开始"); +// int max = 1000,i=0; +// long l = Calendar.getInstance().getTimeInMillis(); +// if (files != null && files.length > 0) { +// +// for (File file : files) { +// i++; +// if(i>max){ +// break; +// } +// if(file.length()>0){ +// try { +// +// MissionResult2 missObj = (MissionResult2)FileUtils.readObjFomFile(file); +// Common.addResultToResultList(missObj); +// file.delete(); +// } catch (Exception e) {//??????? +// FileUtils.copyFile(file, new File(Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName())); +// logger.error("硬盘数据(监测数据)文件读取错误:"+Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName(),e); +// } +// } +// } +// +// //任务结果 +// future = Common.service.submit(new MissionResultThread("硬盘任务数据解析执行线程 启动")); +// //注册 +// Common.getFutureMap().put(Constants.OVRERUN_RESULT_DATA_RESOVE, future); +// +// } +// logger.debug(driFile.getAbsolutePath()+" 解析完成耗时:>"+(Calendar.getInstance().getTimeInMillis()-l)); +// +// //清理文件 +// files = FileUtils.sortByFileNameASC(FileUtils.listAllFilesEndWith(driFile, "tp")); // 按文件名称 升序排序 +// if(files!=null && files.length>0){ +// for(File file : files){ +// file.delete(); +// } +// } +// logger.debug("临时文件清理完成"); +// } + } +// +// } catch (Exception e) { +// logger.error("",e); +// } + } + + public static void main(String[] args) { + CommonDao dao = null; + try { + dao = new CommonDao(); + InitServerThread init = new InitServerThread(); + init.initCommonInfo(dao); + DataFileReaderManagerThread dt = new DataFileReaderManagerThread(); + dt.run(); + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } +} diff --git a/src/com/nms/server/thread/overrunData/MissionResultFileResoveThread.java b/src/com/nms/server/thread/overrunData/MissionResultFileResoveThread.java new file mode 100644 index 0000000..c35edc9 --- /dev/null +++ b/src/com/nms/server/thread/overrunData/MissionResultFileResoveThread.java @@ -0,0 +1,230 @@ +package com.nms.server.thread.overrunData; + +import java.io.File; +import java.util.Calendar; +import java.util.LinkedList; + +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult2; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.UpgradeService; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.FileUtils; + + +/** + * 任务结果处理线程 + * @date Nov 16, 2011 1:25:26 PM + * @author ZhangGang + * + */ +public class MissionResultFileResoveThread implements Runnable{ + private Logger logger = Logger.getLogger(MissionResultFileResoveThread.class); + private String name; + private String dirPath; + volatile boolean stop = false;//线程是否被取消标志 + public MissionResultFileResoveThread (String name,String dirPath){ + this.name= name; + this.dirPath = dirPath; + } + + /** + * + * 硬盘任务结果数据 解析入库 + * @author Administrator Feb 21, 2013 + * @version 1.0 + */ + public void run() { +// Thread.currentThread().setName("任务结果文件解析线程"); + Thread.currentThread().setName("Task Result File Parsing Thread"); + logger.info("文件夹路径 :>" + dirPath); + File driFile = new File(dirPath); + + if(driFile.exists()) { + + // 获取目录下的.csv文件集合 + File[] filesTmp = FileUtils.listAllFilesEndWith(driFile, "result"); + File[] files = FileUtils.sortByFileNameASC(filesTmp); // 按文件名称 升序排序 + + // 解析文件数为0时,终止该计划任务 + if(files==null || files.length==0){ + logger.debug("文件夹路径 :>" + driFile.getAbsolutePath() + " 下" + "无数据文件,解析开始"); + return; + } + + int max = 500,i=0; + long l = Calendar.getInstance().getTimeInMillis(); + + if (files != null && files.length > 0) { + + CommonDao dao = null; + try { + dao = new CommonDao(); + UpgradeService service = new UpgradeService(dao); + + logger.debug("文件夹路径 :>" + driFile.getAbsolutePath() + " 下 " + files.length + "个文件,解析开始"); + LinkedList mr2List1 = new LinkedList(); + LinkedList mr2List4 = new LinkedList(); + LinkedList mr2List6 = new LinkedList(); + for (File file : files) { + try { + MissionResult2 mr2 = (MissionResult2)FileUtils.readObjFomFile(file); + if(mr2 != null){ + switch (mr2.getMissionType().intValue()) { + case 1: + mr2List1.add(mr2); + break; + case 4: + mr2List4.add(mr2); + break; + case 6: + mr2List6.add(mr2); + break; + default: + break; + } + } + } catch (Exception e) { //??????? + FileUtils.copyFile(file, new File(Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName())); + logger.error("Hard disk data (monitoring data) file reading error:"+Constants.ERROR_DETEC_FILE_DIR+"/"+file.getName(),e); + }finally{ + file.delete(); + } + + //计数自增1 + i++; + if(i>max){ //大于max就保存入库 + readResultDataFile(mr2List1,1,dao); + mr2List1.clear(); + readResultDataFile(mr2List4,4,dao); + mr2List4.clear(); + readResultDataFile(mr2List6,6,dao); + mr2List6.clear(); + i=0; + } + + //判断当前线程是否被中断 + if (Thread.currentThread().isInterrupted()) { + // 线程中断状态,不会改变中断状态的值 + logger.info("任务结果数据解析线程 被中断"); + stop = true; + } + } + + service.updateCommonMissionComplete();//任务状态:成功(31)、部分成功(32)、全部失败(30) + service.updateLoopMissionComplete(); + } catch (Exception e) { + logger.error("", e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + } + } + logger.debug(driFile.getAbsolutePath()+" 解析完成耗时:>"+(Calendar.getInstance().getTimeInMillis()-l)); + + //清理文件 + files = FileUtils.sortByFileNameASC(FileUtils.listAllFilesEndWith(driFile, "tp")); // 按文件名称 升序排序 + if(files!=null && files.length>0){ + for(File file : files){ + file.delete(); + } + } + } + logger.debug("临时文件清理完成"); + + } + + public void readResultDataFile(LinkedList mr2List,int i,CommonDao dao) { + + try { + UpgradeService service = new UpgradeService(dao); + dao.setAutoCommit(false); + //-- 集合不为空则进行解析 存在removeList情况 ,优化方式可参考 监测数据解析的实现方式 + if(mr2List!=null && mr2List.size()>0){ + + service.updateMissionResult2ByBatch(i,mr2List); //保存mr2List中的数据 + + service.updateCommonMissionStateDesc(i,mr2List);//i代表任务类型(1,4,6)-非周期任务 + + if (i==4){ + service.updateLoopMissionStateDesc(mr2List);//更新任务类型4的周期任务的状态描述 + } + + dao.commit(); + } + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + try { + if(dao!=null)dao.rollback(); + } catch (Exception e1) { + logger.error(ExceptionPrintUtils.printExceptionStack(e1)); + } + }finally{ + if(dao!=null){ + dao.clearConn(); + } + } + } + + public static void main(String[] args) { + System.out.println(""+Constants.OVERRUN_RESULT_FILE_DIR); +// MissionResultFileResoveThread thread = new MissionResultFileResoveThread("名字",Constants.OVERRUN_RESULT_FILE_DIR); + MissionResultFileResoveThread thread = new MissionResultFileResoveThread("Name",Constants.OVERRUN_RESULT_FILE_DIR); + Common.service.submit(thread); +// LinkedList lks = new LinkedList(); +// lks.add("1"); +// lks.add("2"); +// lks.add("3"); +// +// LinkedList lks2 = null; +// lks2 = (LinkedList)lks.clone(); +// for(String s:lks2) { +// System.out.println(s); +// } +// +// lks.add("4"); +// lks.set(0, "00"); +// +// lks2.remove(0); +// for(String s:lks2) { +// System.out.println(s); +// } +// +// for(String s:lks) { +// System.out.println(s); +// } +// Future future2 = Common.service.submit(new MissionResultFileResoveThread("任务结果入库线程")); +// Common.getFutureMap().put(Constants.MISSION_RESULT_RESOVE, future2); +// +// System.out.println("启动ok。。。"); +// +// Future future = Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE); +// System.out.println(future.isCancelled()); +// System.out.println(future.isDone()); +// try +// { +//// Thread.sleep(1000); +// if(future!=null&&!future.isCancelled()&&!future.isDone()) { +// System.out.println("开始中断。。。"); +// Thread.sleep(536);//这个时间是经过多次测试出来的 +// // Thread.sleep(100); +// future.cancel(true);//停解析任务结果数据入库线程 +// } +// Thread.sleep(3000); +// Common.service.shutdownNow(); +// System.out.println(future.isCancelled()); +// System.out.println(future.isDone()); +// } catch (InterruptedException e) +// { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } + + } + +} diff --git a/src/com/nms/server/thread/socket/NMSWebBPDownload.java b/src/com/nms/server/thread/socket/NMSWebBPDownload.java new file mode 100644 index 0000000..2d4d155 --- /dev/null +++ b/src/com/nms/server/thread/socket/NMSWebBPDownload.java @@ -0,0 +1,136 @@ +package com.nms.server.thread.socket; + +import java.util.List; +import java.util.concurrent.Callable; + +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; +import com.nms.server.util.ExceptionPrintUtils; +import com.nms.server.util.socket.SocketUtils; +import com.nms.server.util.socket.limiter.InputStreamLimiter; +import com.nms.server.util.socket.limiter.OutputStreamLimiter; +import com.socket.utils.FileComment; + + + +/** + * 安全通讯的客户端 + */ + +public class NMSWebBPDownload extends SocketUtils implements Callable{ + private static Logger logger = Logger.getLogger(NMSWebBPDownload.class); + private String cmd = "byte:bpDownloadFile"; + private String downDir=null; + private final List fileList; // + public static final String TYPE_MISSION = "1"; + public static final String TYPE_SNMP_CLASS = "2"; + public static final String TYPE_SNMP_MIB = "3"; + /** + * + * @param fileList 需要下载的文件的 信息结构 String[]{fileName,start,end,MD5} + * @param downDir 保存路径 文件的保存路径 + * @param downType 下载类型 统一下载接口的子分类 用于确认 服务端配置文件、任务文件等非同一目录下保存的文件 + * @throws Exception + */ + public NMSWebBPDownload(String IP,Integer PORT,List fileList,String downDir,String downType) throws Exception{ + + super(IP,PORT); + this.fileList = fileList; + this.downDir = downDir; + cmd +=":"+downType; + } + + /** + * 通讯线程执行方法 默认格式 + * @return + * @throws Exception + */ + public Object call(){ +// Thread.currentThread().setName("任务文件下载线程"); + Thread.currentThread().setName("Task File Download Thread"); + Object obj = null; //返回对象 + //- 校验 是否创建新通讯连接 + if(socket==null && (StringUtils.isNotEmpty(ip) && port != null)){ + + try { + + //-- create SocketFactory + SSLSocketFactory ssf = sSLContext.getSocketFactory(); + + //-- create socket + socket=(SSLSocket)ssf.createSocket(ip,port); + logger.debug("create socket success."); + + //2014-1-23 hyx 如果建立socket成功,但是startHandshake握手失败,且未设置超时时间时,则会一直阻塞 + socket.setSoTimeout(Constants.SSL_SO_TIMEOUT*1000); + + //-- handshake 握手 + ((SSLSocket) socket).startHandshake(); + logger.debug("handshake success."); + + } catch (Exception e) { + + logger.warn("Target communication:>"+ip+" create failure",e); + close(); + return -2; + + } + } + + try { + + //-- 获取通讯IO流 + outl = new OutputStreamLimiter(socket.getOutputStream()); + inl = new InputStreamLimiter(socket.getInputStream()); + //增加通讯超时设置 + socket.setSoTimeout(Constants.SSL_SO_TIMEOUT*1000); +// logger.info("通信超时时间:"+Constants.SSL_SO_TIMEOUT*1000); + + //-- 自定义通讯操作 + obj = toDo(); + + + }catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + obj = -1; + } finally { + close(); + logger.info("--- 通信关闭 ---"); + } + return obj; + } + /* + * 返回值描述 + * 0 下载成功 + * 1 下载失败 + * -1 下载失败 可再次尝试 + * -2 下载失败 且无需再次尝试 + * -3 待下载文件不存在 + */ + public Object toDo() throws Exception { +// Thread.currentThread().setName("任务文件下载线程"); + Thread.currentThread().setName("Task file download thread"); + try { + //存在未下载的文件 开始通讯 + if(fileList.size()>0){ + //- 通讯命令 + sendMessage(cmd); + String str = receiveMessage(); + //- 发送下载文件名称 + sendObject(fileList); + //- 接收文件长度 + return bpReceiveFileByBathMd5(downDir); + } + + return 0; + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return -1; + } + } +} \ No newline at end of file diff --git a/src/com/nms/server/thread/socket/SSLCertManager.java b/src/com/nms/server/thread/socket/SSLCertManager.java new file mode 100644 index 0000000..b33abc5 --- /dev/null +++ b/src/com/nms/server/thread/socket/SSLCertManager.java @@ -0,0 +1,92 @@ +package com.nms.server.thread.socket; + +import java.io.File; + +import com.nms.server.common.Config; +import com.nms.server.common.Constants; +import com.nms.server.util.DateUtil; + +public class SSLCertManager extends SSLCertOper{ + /** + * test main + * @time Aug 28, 2011-12:17:28 PM + * @param args + */ + public static void main(String args[]) { + createServerKeyStoreAndCert(); + } + + /** + * 程序启动初始化操作 (已测试) + * 初始化原始证书文件 + * @time Aug 28, 2011-6:46:10 PM + */ + public void initCheckForServer(){ + //服务端 证书别名 和 密码 + String certPath = Constants.SYSTEM_PATH + File.separator + Constants.SSL_DIR ; + String newServerKeyName = "serverks"+DateUtil.getCurrentDate(DateUtil.YYYYMMDD); + String newServerKeyPsw = "123456"; + //客户端 证书别名 和 密码 + String newClientkeyName = "clientks"+DateUtil.getCurrentDate(DateUtil.YYYYMMDD); + String newClientkeyPsw = "123456"; + //创建服务端证书并导出公钥 + //-- 判断服务端公钥是否存在 + if(!new File(certPath + File.separator+"server.cer").exists()){ + String filepath0 = createKeyAndCert( + newServerKeyName , + certPath+File.separator+Constants.SSL_SERVER_STORE, + "10.0.6.113", newServerKeyPsw, + certPath + File.separator+"server.cer"); + //向客户端公钥证书库引入服务端公钥 + importCertToStore( + newServerKeyName, + certPath + File.separator + Constants.SSL_CLIENT_TRUST, + certPath + File.separator+"server.cer"); + } + //-- 判断客户端公钥是否存在 + if(!new File(certPath + File.separator+"client.cer").exists()){ + //创建客户端证书并导出公钥 + String filepath1 = createKeyAndCert( + newClientkeyName, + certPath + File.separator + Constants.SSL_CLIENT_STORE, + "localhost", newClientkeyPsw, + certPath + File.separator+"server.cer"); + //向服务店公钥证书库引入客户端公钥 + importCertToStore( + newClientkeyName, + certPath+File.separator+Constants.SSL_SERVER_TRUST, + certPath + File.separator+"client.cer"); + } + Config.setValueByName("ssl.server.key.old", Constants.SSL_SERVER_KEY_NEW); + Config.setValueByName("ssl.server.key.old.psw", Constants.SSL_SERVER_KEY_NEW_PSW); + Config.setValueByName("ssl.server.key.new",newServerKeyName); + Config.setValueByName("ssl.server.key.new.psw", newServerKeyPsw); + Config.setValueByName("ssl.client.key",newClientkeyName); + Config.setValueByName("ssl.client.key.psw", newClientkeyPsw); + } + /** + * 服务端创建密匙对 + * @time Aug 28, 2011-7:15:27 PM + */ + public static String createServerKeyStoreAndCert(){ + //服务端 证书别名 和 密码 + String certPath = Constants.SYSTEM_PATH + File.separator + Constants.SSL_DIR ; + String newServerKeyName = "serverks"+DateUtil.getCurrentDate(DateUtil.YYYYMMDD); + String newServerKeyPsw = "123456"; + //-- 判断服务端公钥是否存在 + if(new File(certPath + File.separator+"server.cer").exists()){ + deleteKeyOrCertFromStore(newServerKeyName, certPath+File.separator+Constants.SSL_SERVER_STORE); + } + //创建服务端证书并导出公钥 + String filepath0 = createKeyAndCert( + newServerKeyName , + certPath+File.separator+Constants.SSL_SERVER_STORE, + "10.0.6.113", newServerKeyPsw, + certPath + File.separator+"server.cer"); + Config.setValueByName("ssl.server.key.old", Constants.SSL_SERVER_KEY_NEW); + Config.setValueByName("ssl.server.key.old.psw", Constants.SSL_SERVER_KEY_NEW_PSW); + Config.setValueByName("ssl.server.key.new",newServerKeyName); + Config.setValueByName("ssl.server.key.new.psw", newServerKeyPsw); + return certPath + File.separator+"server.cer"; + } +} diff --git a/src/com/nms/server/thread/socket/SSLCertOper.java b/src/com/nms/server/thread/socket/SSLCertOper.java new file mode 100644 index 0000000..b929b4f --- /dev/null +++ b/src/com/nms/server/thread/socket/SSLCertOper.java @@ -0,0 +1,214 @@ +package com.nms.server.thread.socket; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; +import com.nms.server.util.DateUtil; +import com.nms.server.util.ExceptionPrintUtils; + +public class SSLCertOper { + private static final String STORE_PASS = "client"; + private static final Integer VALIDITY = Constants.SSL_KEY_VALIDITY; + private static Logger logger = Logger.getLogger(SSLCertOper.class); + /** + * 创建 密匙对(私钥和公钥) + * @time Aug 28, 2011-7:07:02 PM + * @param aliasName + * @param storePath + * @param localIp + * @param keyPass + * @param certName + * @return + */ + public static String createKeyAndCert(String aliasName, + String storePath, String localIp, String keyPass, String certName) { + BufferedReader bReader = null; + try { + Process process = Runtime + .getRuntime() + .exec( + "keytool -genkey -v -alias " + + aliasName + + " -keyalg RSA -storetype jceks -keystore " + + storePath + + " -validity "+VALIDITY+" -dname \"CN=" + + localIp + + ",OU=cn,O=cn,L=cn,ST=cn,C=cn\" -storepass "+STORE_PASS+" -keypass " + + keyPass); + process.getOutputStream().close(); + bReader = new BufferedReader(new InputStreamReader( + process.getInputStream())); + String line = null; + while ((line = bReader.readLine()) != null) { + System.out.println(line); + } + + process = Runtime.getRuntime().exec( + "keytool -export -alias " + aliasName + + " -storetype jceks -keystore " + storePath + + " -file " + certName + " -storepass "+STORE_PASS+""); + + bReader = new BufferedReader(new InputStreamReader( + process.getInputStream())); + while ((line = bReader.readLine()) != null) { + System.out.println(line); + } + + return certName; + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + } finally { + if(bReader!=null){ + try { + bReader.close(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + } + + } + + /** + * 将公钥引入KeyStore + * @time Aug 28, 2011-7:07:17 PM + * @param aliasName + * @param storePath + * @param certName + * @return + */ + public static boolean importCertToStore(String aliasName, String storePath, + String certName) { + BufferedReader bReader = null; + PrintWriter pw = null; + try { + Process process = Runtime.getRuntime().exec( + "keytool -import -v -trustcacerts -alias " + + aliasName + " -keystore " + storePath + " -file " + + certName + " -storetype jceks -storepass "+STORE_PASS+""); + + bReader = new BufferedReader(new InputStreamReader( + process.getInputStream())); + pw = new PrintWriter(process.getOutputStream()); + + pw.write("y"); + pw.flush(); + pw.close(); + + String line = null; + while ((line = bReader.readLine()) != null) { + System.out.println(line); + } + + return true; + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return false; + }finally { + if(pw!=null){ + pw.close(); + } + if(bReader!=null){ + try { + bReader.close(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + } + + } + /** + * 将公钥引入KeyStore + * @time Aug 28, 2011-7:07:17 PM + * @param aliasName + * @param storePath + * @param certName + * @return + */ + public static boolean deleteKeyOrCertFromStore(String aliasName, String storePath) { + BufferedReader bReader = null; + PrintWriter pw = null; + try { + Process process = Runtime.getRuntime().exec( + "keytool -delete -v -alias " + + aliasName + " -keystore " + storePath + " -storetype jceks -storepass "+STORE_PASS+""); + + bReader = new BufferedReader(new InputStreamReader( + process.getInputStream())); + pw = new PrintWriter(process.getOutputStream()); + +// pw.write("y"); + pw.flush(); + pw.close(); + + String line = null; + while ((line = bReader.readLine()) != null) { + System.out.println(line); + } + + return true; + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return false; + }finally { + if(pw!=null){ + pw.close(); + } + if(bReader!=null){ + try { + bReader.close(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + } + + } + /** + * test main + * @time Aug 28, 2011-12:17:28 PM + * @param args + */ + public static void main(String args[]) { + String newServerKeyName = "serverks"+DateUtil.getCurrentDate(DateUtil.YYYYMMDD); + String newServerKeyPsw = "123456"; + String newClientkeyName = "clientks"+DateUtil.getCurrentDate(DateUtil.YYYYMMDD); + String newClientkeyPsw = "123456"; + String filepath0 = SSLCertOper.createKeyAndCert( + newServerKeyName , + "D:\\workspace\\nms_server\\cer\\server_ks", + "10.0.6.113", newServerKeyPsw, + "D:\\workspace\\nms_server\\cer\\server.cer"); + + SSLCertOper.importCertToStore( + newServerKeyName, + "D:\\workspace\\nms_server\\cer\\client_ts", + "D:\\workspace\\nms_server\\cer\\server.cer"); + + String filepath1 = SSLCertOper.createKeyAndCert( + newClientkeyName, + "D:\\workspace\\nms_server\\cer\\client_ks", + "localhost", newClientkeyPsw, + "D:\\workspace\\nms_server\\cer\\client.cer"); + + SSLCertOper.importCertToStore( + newClientkeyName, + "D:\\workspace\\nms_server\\cer\\server_ts", + "D:\\workspace\\nms_server\\cer\\client.cer"); + System.out.println(filepath0); + System.out.println(filepath1); +// Config.setValueByName("ssl.server.key.old", Constants.SSL_SERVER_KEY_NEW); +// Config.setValueByName("ssl.server.key.old.psw", Constants.SSL_SERVER_KEY_NEW_PSW); +// Config.setValueByName("ssl.server.key.new",newServerKeyName); +// Config.setValueByName("ssl.server.key.new.psw", newServerKeyPsw); +// Config.setValueByName("ssl.client.key",newClientkeyName); +// Config.setValueByName("ssl.client.key.psw", newClientkeyPsw); + } +} diff --git a/src/com/nms/server/thread/socket/SSLClient.java b/src/com/nms/server/thread/socket/SSLClient.java new file mode 100644 index 0000000..d7a107c --- /dev/null +++ b/src/com/nms/server/thread/socket/SSLClient.java @@ -0,0 +1,62 @@ +package com.nms.server.thread.socket; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.socket.SSLSocketCallable; + + +/** + * 安全通讯的客户端 + */ + +public class SSLClient extends SSLSocketCallable { + private static Logger logger = Logger.getLogger(SSLClient.class); + private String cmd = null; + + public static final String RELEASE_DETECT = "char:releaseDetect"; + + + + public SSLClient(String ip,int port,String cmd){ + super(ip, port); + this.cmd = cmd; + } + + public SSLClient(String ip, int port)throws Exception { + super(ip, port); + } + + @Override + protected Object toDo() throws Exception { + Thread.currentThread().setName("通信线程 TO:>"+ip); + logger.debug("通讯命令:>"+cmd); + if(RELEASE_DETECT.equalsIgnoreCase(cmd)){ + /** + * 通知 web 监测数据入库完成 + */ + sendMessage(cmd); + String msg = receiveMessage(); + logger.debug(RELEASE_DETECT + " -> " + msg); + } + return null; + } + + /** + * 向web 发送 释放 监测数据锁 + * @param ip + * @param port + */ + public static void sengReleaseDetect() { + try { + Common.service.submit(new SSLClient(Constants.WEB_SOCKET_IP, Constants.WEB_SOCKET_PORT, RELEASE_DETECT)); + } catch (Exception e) { + logger.error("",e); + } + } + + public static void main(String [] args){ + + } +} \ No newline at end of file diff --git a/src/com/nms/server/thread/socket/SSLServer.java b/src/com/nms/server/thread/socket/SSLServer.java new file mode 100644 index 0000000..9a4ecf7 --- /dev/null +++ b/src/com/nms/server/thread/socket/SSLServer.java @@ -0,0 +1,563 @@ +package com.nms.server.thread.socket; + + +import java.io.File; +import java.net.Socket; +import java.util.Date; +import java.util.LinkedList; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.bean.MissionResult2; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.dao.CommonDao; +import com.nms.server.service.ChangeService; +import com.nms.server.service.CommonService; +import com.nms.server.service.UpgradeService; +import com.nms.server.thread.InitServerThread; +import com.nms.server.thread.ResetServerThread; +import com.nms.server.thread.alarmData.AlarmDataResoveThread; +import com.nms.server.thread.change.ChangeForActiveAlarmInfo; +import com.nms.server.thread.change.ChangeForCheckType; +import com.nms.server.thread.change.ChangeForNodeGroup; +import com.nms.server.thread.change.ChangeForSetInfo; +import com.nms.server.thread.change.ChangePluginScriptFile; +import com.nms.server.thread.detecData.NewDetecDataResoveThread; +import com.nms.server.thread.file.UnZipManagerThread; +import com.nms.server.thread.mission.LoadNewMissionThread; +import com.nms.server.thread.mission.MissionResultThread; +import com.nms.server.util.socket.SSLSocketCallable; + + +/** + * 服务器通信进程 + * @date Nov 16, 2012 4:46:54 PM + * @author ZhangGang + * + */ +public class SSLServer extends SSLSocketCallable{ + Logger logger = Logger.getLogger(SSLServer.class); + + public SSLServer(Socket socket) { + super(socket); + } + + /** + * 业务类操作 + * @time Aug 28, 2011-8:41:37 PM + * @param out + * @param in + * @throws Exception + */ + protected Object toDo() throws Exception{ + CommonDao dao = null; + try { + String clientIp = socket.getInetAddress().getHostAddress(); +// Thread.currentThread().setName("接收通信:"+clientIp); + Thread.currentThread().setName("Receiving communication:"+clientIp); + + //-- 获取业务操作命令 + String msg = receiveMessage(); + logger.info("通讯命令:"+msg); + + /********* 与NMSClient通讯操作 *************************/ + //-- Agent端监测告警配置初始化 + if(SocketCMD.CLIENT_REQ_LOCAL_UUID.equalsIgnoreCase(msg)){ + dao = new CommonDao(); + CommonService service = new CommonService(dao); + //- 校验IP是否存在 + //- 不存在,创建新节点并下发SEQ_ID(UUID) + //- 存在,将旧的SEQ_ID(UUID)发送回去 + //新节点校验 + boolean newflag = service.newClientCheck(clientIp,null); + //- 主动监测业务调整后,各监测线程每次执行监测时,即时查询数据库节点信息,无需手动变更,所以以下代码废弃 +// if(newflag){ //对新增节点初始化主动监测 +// List setList = service.getDrivingSetInfo(Constants.DETEC_PING_STR, Common.getIpSeqIdMap().get(clientIp)); +// if(setList!=null && setList.size()>0){ +// for (Iterator setIte = setList.iterator(); setIte.hasNext();) { +// SetInfo si = (SetInfo) setIte.next(); +// si.setSingleNodeId(Common.get); +// logger.debug("监测启动>> "+si.getCheckTypeName()); +// new ThreadService(dao).updateSNMPThread(si); +// } +// } +// setList.clear(); +// List setList = service.getDrivingSetInfo(Constants.DETEC_NMSC_STR, Common.getIpSeqIdMap().get(clientIp)); +// if(setList!=null && setList.size()>0){ +// for (Iterator setIte = setList.iterator(); setIte.hasNext();) { +// SetInfo si = (SetInfo) setIte.next(); +// +// //- 设置名称 +// String processIden = StringUtils.isEmpty(si.getProcessIden())?"":si.getProcessIden(); +// +// //-- 监测设置 线程名称 typeName_processIden +// String runnableName = si.getCheckTypeName()+"_"+processIden; +// +// //根据线程类别 线程对象 +// MonitorManagerThread managerThread = (MonitorManagerThread) Common.getMonitorRunnableFromMap(runnableName); +// +// if(managerThread!= null){ +// managerThread.addIp(new NodeModel(clientIp,0l)); +// }else{ +// logger.warn("主动监测线程:"+runnableName+" 不存在"); +// } +// +// } +// } +// } + String returnMsg = Common.getIpSeqIdMap().get(clientIp)+ ":::" + clientIp; + sendMessage(returnMsg); + receiveMessage(); + } + + //-- Agent端监测告警配置初始化 + else if(SocketCMD.CLIENT_REQ_INIT_CONFIG.equalsIgnoreCase(msg)){ + dao = new CommonDao(); + ChangeService service = new ChangeService(dao); + JSONObject object = service.agentInit(clientIp); + sendMessage(object.toString()); + receiveMessage(); + + //--清除该节点的所有变更事件 + service.deleteEventRecordBySeqId(Common.getUUIDByIp(clientIp)); + + //--获取并发送脚本文件(初始化时发送脚) + ChangePluginScriptFile.sendPluginFileWhenNcInit(clientIp); + } + //-- Agent端获取Server端系统日期 + else if(SocketCMD.CLIENT_REQ_SERVER_SYSTEMDATE.equalsIgnoreCase(msg)){ + sendMessage(new Date().getTime()+""); + receiveMessage(); + } + //-- 接收告警信息----------------------------------------------------- + else if(SocketCMD.CLIENT_REQ_ALARM.equalsIgnoreCase(msg)){ + sendMessage(SUCCESS); + String msg0 = receiveMessage(); + logger.info(msg0); + sendMessage(SUCCESS); + if(Constants.FLAG_ALARM_RESOVE==1){//启动了告警信息解析线程,才将接收的信息存入内容,否则不存,不然会将内存越撑越大 + //Ip type 进程标识 信息 + String [] alarms = msg0.split(Constants.COMMON_DATA_SPLIT); + Common.addAlarmData(alarms); + } + } + //-- 接收系统错误信息----------------------------------------------------- + else if(SocketCMD.CLIENT_RQ_NC_ERROR_INFO.equalsIgnoreCase(msg)){ + sendMessage(SUCCESS); + String msg0 = receiveMessage(); + logger.info(msg0); + sendMessage(SUCCESS); + //Ip type 进程标识 信息 + String [] alarms = msg0.split(Constants.COMMON_DATA_SPLIT); + if(alarms.length==5){ + Common.addErrorInfo(alarms[0], alarms[1], alarms[2], new Date(Long.parseLong(alarms[3])), Long.parseLong(alarms[4]),null); + }else if(alarms.length==6){ + Common.addErrorInfo(alarms[0], alarms[1], alarms[2], new Date(Long.parseLong(alarms[3])), Long.parseLong(alarms[4]),alarms[5]); + } + + } + //-- 接收监测数据文件到内存------------csv + else if(SocketCMD.CLIENT_REQ_UPLOAD_DATAS.equalsIgnoreCase(msg)){ + sendMessage(SUCCESS); + long sTime = System.currentTimeMillis(); + logger.debug("采集监测数据文件开始"); + LinkedList bslist = receiveFileBytesByBath(); + logger.debug("采集监测数据文件结束 "+(System.currentTimeMillis()-sTime)+" ms"); + if(bslist!=null && bslist.size()>0){ + Common.addAllDeteDataList(bslist); + }else{ + logger.debug("接收 监测数据 文件数:0"); + } + sendMessage(SUCCESS); + } + //----------------------------------------------------------------------------------------- + //-- 接收返回的任务结果 + else if(SocketCMD.CLIENT_REQ_TASK_RESULT.equalsIgnoreCase(msg)){ + sendMessage(SUCCESS); + String msg0 = receiveMessage(); + logger.info(msg0); + sendMessage(SUCCESS); + String [] result = msg0.split(Constants.COMMON_DATA_SPLIT); + MissionResult2 result2 = Common.resoveMissionResult(result); + Common.addResultToResultList(result2); + } + + //-- Agent请求任务信息 + else if(SocketCMD.CLIENT_REQ_INIT_TASK.equalsIgnoreCase(msg)){ + dao = new CommonDao(); + UpgradeService uService = new UpgradeService(dao); + sendMessage(SUCCESS); + String uuid = receiveMessage(); + JSONArray array = new JSONArray(); + if(StringUtils.isNotEmpty(uuid)){ + array = uService.getMissionForAgentStart(Long.parseLong(uuid)); + } + sendMessage(array.toString()); + receiveMessage(); + } + + //-- Agent端,通信握手 + else if(SocketCMD.CLIENT_REQ_HAND_SHAKE.equalsIgnoreCase(msg)){ + sendMessage(SUCCESS); +// if(!Constants.INIT_FLAG){ +// Common.service.submit(new InitServerThread()); +// } + } + + //-- 接收任务4“启动”的回传文件 + else if(SocketCMD.CLIENT_REQ_TASK_RETURNFILE.equalsIgnoreCase(msg)){ + + sendMessage(SUCCESS); + + String msg0 = receiveMessage(); + sendMessage(SUCCESS); + + logger.info(msg0); + String [] result = msg0.split(Constants.COMMON_DATA_SPLIT); + MissionResult2 result2 = Common.resoveMissionResult(result); + String fileName = receiveMessage(); + sendMessage(SUCCESS); + logger.debug("MISSION_FILE_UPLOAD_DIR"+Constants.MISSION_FILE_UPLOAD_DIR); + + this.bpReceiveFile(Constants.MISSION_FILE_UPLOAD_DIR+fileName); + + sendMessage(SUCCESS); + logger.debug("MISSION_FILE_UPLOAD_DIR"+Constants.MISSION_FILE_UPLOAD_DIR+fileName); + + result2.setFileInfo(fileName); + Common.addResultToResultList(result2); + } + + //-- 服务端升级 + else if(SocketCMD.CLIENT_REQ_SERVER_UPGRADE.equalsIgnoreCase(msg)){ + //清理缓存操作并"关闭"通讯 + try { + //-- 第一步 关闭通讯 + Common.SERVER_UN_UPGRADE_FLAG = false; + + //-- 第二步 通讯恢复“定时”:当NC杀死DC进程不成功时,DC还可以按照旧代码处理工作(监测,任务等) + Common.scheduled.schedule(new Runnable(){ + public void run() { + Common.SERVER_UN_UPGRADE_FLAG = true; + Common.service.submit(new InitServerThread()); + }},Constants.MISSION_UPGRADE_DAILY,TimeUnit.SECONDS); + //-- 第二步 清理缓存 detection & alarm & results + + //-- 关闭线程 清理 监测数据 + if(Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE) != null){ + Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE).cancel(true); + } + LinkedList dsbList = Common.getDeteDataList(); + Future detecFuture = Common.scheduled.submit(new NewDetecDataResoveThread("Server升级前_监测数据缓存入库1",dsbList)); + Common.chengeDeteDataFlag(); + LinkedList dsbList2 = Common.getDeteDataList(); + Future detecFuture2 = Common.scheduled.submit(new NewDetecDataResoveThread("Server升级前_监测数据缓存入库2",dsbList2)); + + //-- 关闭线程 清理 告警数据 + if(Common.getFutureMap().get(Constants.ALARM_DATA_RESOVE) != null){ + Common.getFutureMap().get(Constants.ALARM_DATA_RESOVE).cancel(true); + } + LinkedList adList = Common.getAlarmDataList(); + Future alarmFuture = Common.scheduled.submit(new AlarmDataResoveThread("Server升级前_告警数据缓存入库1",adList)); + Common.changeAlarmDataFlag(); + LinkedList adList2 = Common.getAlarmDataList(); + Future alarmFuture2 = Common.scheduled.submit(new AlarmDataResoveThread("Server升级前_告警数据缓存入库2",adList2)); + + //-- 关闭线程 清理 任务结果数据 + if(Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE) != null){ + Common.getFutureMap().get(Constants.MISSION_RESULT_RESOVE).cancel(true); + } + Future mrFuture = Common.scheduled.submit(new MissionResultThread("Server升级前_任务结果缓存入库")); + try { + detecFuture.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + detecFuture2.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + + alarmFuture.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + alarmFuture2.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + + mrFuture.get(Constants.MISSION_UPGRADE_DAILY, TimeUnit.SECONDS); + } catch (Exception e) { + logger.error("DC cleaning data timeout, cache data stored in the hard disk", e); + Common.saveDataInMemToDisk(); + logger.warn("The cached data is cleaned up and the upgrade is confirmed"); + }finally{ + //-- 最后一步返回success + sendMessage(SUCCESS); + } + } catch (Exception e) { + logger.error("Server upgrade preparation work failed!", e); + //-- 最后一步返回success + sendMessage(FAIL); + } + } + + //-- 数据压缩文件接收--zip + else if(msg!=null && msg.startsWith(SocketCMD.CLIENT_REQ_BP_UPLOAD_FIFE)){ + this.sendMessage(SUCCESS); + String[] cmds = msg.split(":"); + String uploadPath = Constants.ZIP_FILE_DETECT_DATA_DIR; + if(cmds.length>2){ + if(cmds[2].equals(SocketCMD.SUFFIX_BP_TYPE_DETECT_DATA)){ //download MissionFile + uploadPath = Constants.ZIP_FILE_DETECT_DATA_DIR; + }else if(cmds[2].equals(SocketCMD.SUFFIX_BP_TYPE_TASK_RESULT)){ //download snmp jarFile + uploadPath = Constants.ZIP_FILE_TASK_RESULT_DIR; + }else if(cmds[2].equals(SocketCMD.SUFFIX_BP_TYPE_TASK_RETURN)){ //download snmp mibFile + uploadPath = Constants.ZIP_FILE_TASK_RETURN_DIR; + } + } + String fileName = receiveMessage(); + this.sendMessage(SUCCESS); + bpReceiveFile(uploadPath+File.separator+fileName); + this.sendMessage(SUCCESS); + + //启动并注册 解压文件线程 + Future future = Common.getFutureMap().get(Constants.UNZIP_FILE_MANAGER); + if(future == null){ + future = Common.scheduled.scheduleWithFixedDelay(new UnZipManagerThread(), 0, 30, TimeUnit.SECONDS); + Common.registRunnable(Constants.UNZIP_FILE_MANAGER, future); + } + } + //-- Agent申请Node参数变更(Ip systemType) + else if(SocketCMD.CLIENT_AGENT_CHANGE.equalsIgnoreCase(msg)){ + dao = new CommonDao(); + CommonService service = new CommonService(dao); + sendMessage(SUCCESS); + String paramsStr = receiveMessage(); +// pl("paramsStr"+paramsStr); +// UUID$@$操作系统类型(1、2)$@$IP + String[] resultInfo = new String[2]; + if(StringUtils.isNotEmpty(paramsStr)){ + String [] params = paramsStr.split(Constants.COMMON_DATA_SPLIT); + resultInfo = service.checkAndUpdateNodeParams(Long.parseLong(params[0]),clientIp,Integer.parseInt(params[1])); + } + sendMessage(resultInfo[0]+Constants.COMMON_DATA_POINT+resultInfo[1]); + receiveMessage(); + } + //-- 初始化任务信息 + else if("byte:initTask".equalsIgnoreCase(msg)){ + dao = new CommonDao(); + UpgradeService uService = new UpgradeService(dao); + sendMessage(SUCCESS); + String uuid = receiveMessage(); + JSONArray array = new JSONArray(); + if(StringUtils.isNotEmpty(uuid)){ + array = uService.getMissionForAgentStart(Long.parseLong(uuid)); + } + sendMessage(array.toString()); + receiveMessage(); + } + + //-- 证书接收操作 + else if(msg!=null && msg.toLowerCase().startsWith("byte:cert")){ + String aliasName = msg.split(":")[2]; + sendMessage(SUCCESS); + String cerDir = Constants.SYSTEM_PATH+File.separator+Constants.SSL_DIR+File.separator; + String certName = cerDir + clientIp+".cer"; + String storePath = cerDir + Constants.SSL_SERVER_TRUST; + receiveFile(certName); + SSLCertOper.importCertToStore(aliasName, storePath, certName); + sendMessage(SUCCESS); + } + + /********* 与NMSWeb通讯操作 *************************/ + //-- 判断内容,执行操作 + if (SocketCMD.WEB_NOTICE_SET_INFO_ALERT.equalsIgnoreCase(msg)) { + /**监测设置信息变更或时,进行此操作 + * 影响:新旧设置信息在有效性、监测范围、上均有可能变化, + * 需要详细比较新旧信息后,再进行下发 + * 有效性变化:通知最新 + * 监测范围变化:先通知旧设置信息取消监测 + * 再通知新节点信息启动监测 + * 监测内容变化:通知最新 + * (未完成) + * */ + //-- 通讯接收信息 + + sendMessage(SUCCESS); + String datas = receiveMessage(); //JsonObject 对象 + logger.debug(""+datas); + sendMessage(SUCCESS); + + //-- 处理通讯信息 + + Common.service.execute(new ChangeForSetInfo(null,datas)); + } + + else if (SocketCMD.SEND_PLUGIN_SCRIPT_FILE.equalsIgnoreCase(msg)) { + /** + * 下发第三方监测脚本 + * */ + //-- 通讯接收信息 + sendMessage(SUCCESS); + File tempPluginDir = ChangePluginScriptFile.getNewTempPluginDirectory(); + this.bpReceiveFileByBath(tempPluginDir.getCanonicalPath()); + sendMessage(SUCCESS); + //-- 处理通讯信息 + + Common.service.execute(new ChangePluginScriptFile(tempPluginDir)); + } + + else if (SocketCMD.WEB_NOTICE_CHECK_TYPE_ALERT.equalsIgnoreCase(msg)) { //Server端更新缓存信息方法 + /**告警、库表信息变更时,进行此操作 + * 影响:NMSServer端解析需要更新 + * 对Agent端运行无影响及操作。 + * (已完成) + * */ + sendMessage(SUCCESS); + String datas = receiveMessage(); //setInfoId:checkWay:checkState + pl(datas); + String[] data = datas.split(":"); + sendMessage(SUCCESS); + + Common.service.execute(new ChangeForCheckType(null,data)); + } + else if (SocketCMD.WEB_NOTICE_NODE_AND_GROUP_STATE_ALERT.equalsIgnoreCase(msg)) { + /**节点或节点组新增或有效性变化时,进行此通信 + * 影响:仅指状态变化,范围不变 + * 重新查询节点或节点组涉及的监测设置信息,再次下发即可 + * (待测试) + * */ + //-- 通讯开始 并接收数据信息 + sendMessage(SUCCESS); + + String datas = receiveMessage(); //groupId:nodeId [节点组Id:节点ID 节点Id为空时,即为节点组信息 ] + logger.debug("NodeGroup & Node Info: "+datas); + sendMessage(SUCCESS); + + //-- 通信结束,数据解析 + String[] data = datas==null?null:datas.split(":"); + //-- 通过新线程 变更影响范围内的相关信息 + if(data!=null && data.length>0 && StringUtils.isNotEmpty(data[0])){ + Common.service.submit(new ChangeForNodeGroup(null,data) ); + }else{ + logger.warn("Web end “NodeOrGroupEdit” data format is incorrect"); + } + } + else if (SocketCMD.WEB_NOTICE_MISSION_INFO_ALERT.equalsIgnoreCase(msg)) {//任务信息变更,包括创建任务后的任务下发 + sendMessage(SUCCESS); + + String datas = receiveMessage(); //missionId:osLimits + logger.debug("upgrade Message: "+datas); + String[] data = datas.split(":"); + //判断是否需要即时初始化 + //if(data.length>1 && "1".equals(data[1]) ){ + // Future future = Common.service.submit(new LoadNewMissionThread(Long.parseLong(data[0].trim()))); + // future.get(); + + //}else{ + sendMessage(SUCCESS); + //} + + if(data!=null && data.length>0 && StringUtils.isNotEmpty(data[0])){ + Common.service.submit(new LoadNewMissionThread(Long.parseLong(data[0].trim()))); + }else{ + logger.warn("Web terminal “missionInfo” task send empty data communication"); + } + } + else if (SocketCMD.WEB_NOTICE_DATACONTROLLER.equalsIgnoreCase(msg)) { + sendMessage(SUCCESS); + + String datas = receiveMessage(); //int 1 更新自身缓存信息 + logger.debug("upgrade Message: "+datas); + sendMessage(SUCCESS); + if(StringUtils.isNotBlank(datas)){ + Common.service.submit(new ResetServerThread(Integer.parseInt(datas.trim()))); + }else{ + logger.warn("Web terminal “"+SocketCMD.WEB_NOTICE_DATACONTROLLER+"”task send empty data communication"); + } + } + else if (SocketCMD.WEB_NOTICE_ACTIVE_ALARM_START_ALERT.equalsIgnoreCase(msg)) {//NC端是否报主动告警 + sendMessage(SUCCESS); + + String datas = receiveMessage(); + logger.debug("upgrade Message: "+datas); + sendMessage(SUCCESS); + if(StringUtils.isNotBlank(datas)){ + Common.service.submit(new ChangeForActiveAlarmInfo(null,datas)); + }else{ + logger.warn("Web terminal “"+SocketCMD.WEB_NOTICE_ACTIVE_ALARM_START_ALERT+"”send empty data communication"); + } + } + else if (SocketCMD.WEB_UPDATE_CONFIG.equalsIgnoreCase(msg)) { + sendMessage(SUCCESS); + + String datas = receiveMessage(); //int 1 更新自身缓存信息 + logger.debug("upgrade Message: "+datas); + sendMessage(SUCCESS); + if(StringUtils.isNotBlank(datas)){ + Common.service.submit(new ResetServerThread(Integer.parseInt(datas.trim()))); + }else{ + logger.warn("Web terminal “"+SocketCMD.WEB_NOTICE_DATACONTROLLER+"”task send empty data communication"); + } + }else if(SocketCMD.WEB_NOTICE_INSERT_DETECT.equalsIgnoreCase(msg)){ + /** + * 通知监测数据入库 + */ + int size = -1; + Common.setLastWebHandshake(System.currentTimeMillis()); + try { + logger.debug("web :"+ clientIp +" 通知监测数据入库"); + if(Constants.DETECT_INSERT_MODE == 1 || Constants.DETECT_INSERT_MODE == 3){ + //- 非升级操作判断 + if(Common.SERVER_UN_UPGRADE_FLAG){ + Common.chengeDeteDataFlag(); //变更数据存放集合 + LinkedList dsbList = Common.getDeteDataList();// 获取非存放状态的数据集合 + size = dsbList.size(); + if(dsbList != null && size>0){ + //-- 获取线程执行 需进行主动告警和邮件通知等相关操作,待考虑 + Future future = Common.dataResoveService.submit(new NewDetecDataResoveThread("监测数据解析执行线程",dsbList)); + //注册 + Common.getFutureMap().put(Constants.DETEC_DATA_RESOVE, future); + }else{ + logger.info("获取到的集合中数据数量为0,无需启动解析线程"); + } + }else{ + logger.debug("升级中,暂不入库"); + } + }else{ + size = Constants.DETECT_INSERT_MODE -6;//2主动入库 -4,4上传web -2 + logger.debug("DETECT_INSERT_MODE = " + Constants.DETECT_INSERT_MODE +" 不做处理"); + } + } catch (Exception e) { + logger.error("",e); + }finally{ + sendMessage(size+""); + } + }else if(SocketCMD.RELEASE_DETECT.equalsIgnoreCase(msg)){ + /** + * 通知 关闭监测数据入库 + */ + boolean cancel = false; + try { + Future future = Common.getFutureMap().get(Constants.DETEC_DATA_RESOVE); + boolean done = future.isDone(); + logger.debug("监测数据解析执行线程 isDone:" + done); + cancel = future.cancel(true); + logger.debug("监测数据解析执行线程 cancel : " + cancel); + + } catch (Exception e) { + logger.error("",e); + }finally{ + sendMessage(cancel+""); + } + } + + } catch (Exception e) { + logger.error("Communicating exception",e); + }finally{ + if(dao!=null){ + dao.close(); + dao=null; + } + logger.debug("通讯结束"); + } + return null; + } +} diff --git a/src/com/nms/server/thread/socket/SSLServerManager.java b/src/com/nms/server/thread/socket/SSLServerManager.java new file mode 100644 index 0000000..8230f32 --- /dev/null +++ b/src/com/nms/server/thread/socket/SSLServerManager.java @@ -0,0 +1,100 @@ +package com.nms.server.thread.socket; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.Calendar; +import java.util.concurrent.Callable; + +import javax.net.ssl.SSLServerSocket; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.util.socket.SocketUtils; + +/** + * 用于安全通讯的服务Socket,采用java中的SSLServerSocket + **/ +public class SSLServerManager implements Callable{ + + Logger logger = Logger.getLogger(SSLServerManager.class); + + //ServerSocket 通讯服务 + private SSLServerSocket sslServer = null; + + public SSLServerManager(int port,boolean clientAuth) throws Exception{ + int test = 3; + while(true){ +// sslServer = (SSLServerSocket) SocketUtils.getSSLContext().getServerSocketFactory().createServerSocket(port,100); + logger.info("开始绑定端口。。。。。"); + sslServer = (SSLServerSocket) SocketUtils.getSSLContext().getServerSocketFactory().createServerSocket(); + sslServer.setReuseAddress(true); + sslServer.bind(new InetSocketAddress(port),(int)(Constants.EXECUTOR_SOCKET_THREAD_SIZE*0.8)); + //端口监听状态判断 + if(sslServer != null && sslServer.isBound()){ //启动成功跳出循环 + logger.info("端口["+port+"]绑定成功,通讯服务启动成功"); + Constants.sslServer = sslServer; + break; + }else{ //启动是失败,10秒后再次尝试 + if(sslServer!=null){ + sslServer.close(); + sslServer = null; + } + //超过最大尝试次数跳出 + if(0 > --test){ + logger.error("The communication service port binding failed, exceeding the maximum number of attempts."); +// throw new Exception("通讯服务端口绑定失败,且超过最大次数,启动失败"); + throw new Exception("The communication service port binding failed and exceeded the maximum number of times to start failure."); + }else{ + logger.warn("The port["+port+"]binding failed, and the communication service started again after 10 seconds"); + Thread.sleep(10*1000); + } + } + + } + + sslServer.setNeedClientAuth(clientAuth); //客户端是否需要认证 + logger.info("本地通讯端口"+port+"监听线程启动成功"); + } + + /** + * 重载方法:run 处理客户端的请求 + **/ + public Object call() { + Thread.currentThread().setName("Receiving communication"); + try { + while (true) { + Socket socket = sslServer.accept(); + if(Common.SERVER_UN_UPGRADE_FLAG){ //当SOCKET_FLAG为true时,允许建立通讯,否则放弃通讯,用于NMSServer升级功能 + logger.debug("来自:"+socket.getInetAddress().getHostAddress()); + Common.service.submit(new SSLServer(socket)); + }else{ //关闭 放弃的通讯 + logger.debug("Server升级 抛弃通讯:"+socket.getInetAddress().getHostAddress()); + socket.close(); + } + } + } catch (IOException e) { + logger.error("Local communication port monitor thread exception termination"+e.getMessage()); + Common.addErrorInfo(Constants.ERROR_CODE_SOCKET_SERVER_RUNTIME,Common.getLocalIp(), Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,""); + + }finally{ + try { + sslServer.close(); + } catch (IOException e) { + logger.error("",e); + } + } + return null; + } + + //测试函数 + public static void main(String args[]) { + try { + Common.service.submit(new SSLServerManager(60702,true)); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/com/nms/server/thread/socket/SocketCMD.java b/src/com/nms/server/thread/socket/SocketCMD.java new file mode 100644 index 0000000..a1b4edc --- /dev/null +++ b/src/com/nms/server/thread/socket/SocketCMD.java @@ -0,0 +1,147 @@ +package com.nms.server.thread.socket; + +public class SocketCMD { + /* 与NMSClient通讯操作 **/ + /** + * 与Server握手请求 + */ + public static final String CLIENT_REQ_HAND_SHAKE = "char:handshake"; + /** + * 获取本机标志UUID请求 + */ + public static final String CLIENT_REQ_LOCAL_UUID = "char:uuid"; + /** + * 初始化配置请求 + */ + public static final String CLIENT_REQ_INIT_CONFIG = "char:init"; + /** + * 初始化任务请求 + */ + public static final String CLIENT_REQ_INIT_TASK = "char:initTask"; + /** + * 主动告警请求 + */ + public static final String CLIENT_REQ_ALARM = "char:alarm"; + public static final String CLIENT_RQ_NC_ERROR_INFO = "char:ncErrorInfo"; + /** + * 主动告警请求 + */ + public static final String CLIENT_AGENT_CHANGE= "char:agentChange"; + + /** + * 获取Server端系统时间请求 + */ + public static final String CLIENT_REQ_SERVER_SYSTEMDATE = "char:systemdate"; + /** + * 获取Server端系统时间请求 + */ + public static final String REQ_CLIENT_SERVER_SYSTEMDATE = "char:systemdate"; + + /** + * Server升级请求命令 + */ + public static final String CLIENT_REQ_SERVER_UPGRADE = "char:upgradeServer"; + + /** + * 上传回传文件、任务结果文件、数据文件的打包文件请求命令 + */ + public static final String CLIENT_REQ_BP_UPLOAD_FIFE = "byte:bpUploadFile"; + + //数据收集命令:Agent会把监测数据、任务结果、回传文件都发过来 + public static final String REQ_DATA_COLLECT = "byte:collectData"; + + //监测数据收集命令 + public static final String REQ_DETECT_DATA_COLLECT = "byte:collectDetectData"; + public static final String DETECT_DATA_ZIP_TYPE = "zipDetectData"; + public static final String DETECT_DATA_CSV_TYPE = "csvDetectData"; + public static final String DETECT_DATA_COLLECT_END = "end"; + + //任务(该有结果,而无结果的任务)结果收集 + public static final String REQ_NONRLTTASK_RESULT_COLLECT = "char:collectNonRltTaskResult"; + //任务结果收集命令 + public static final String REQ_TASK_RESULT_COLLECT = "byte:collectTaskResult"; + //任务结果类型 + public static final String TASK_RESULT_ZIP_TYPE = "zipTaskResult"; + public static final String TASK_RESULT_OBJ_TYPE = "objTaskResult"; + public static final String COMMUNICATION_END = "end"; + + //回传文件收集命令 + public static final String REQ_TASK_RETURN_FILE_COLLECT = "byte:collectTaskReturnFile"; + public static final String TASK_RETURN_ZIP_TYPE = "zipTaskReturn"; + public static final String TASK_RETURN_FILE_TYPE = "fileTaskReturn"; +// public static final String TASK_RETURN_COLLECT_END = "end"; + + + + //bpUploadFile的子命令类型 + public static final String SUFFIX_BP_TYPE_TASK_RESULT = "taskresult"; + public static final String SUFFIX_BP_TYPE_TASK_RETURN = "taskreturn"; + public static final String SUFFIX_BP_TYPE_DETECT_DATA = "detectdata"; + + /** + * 发送任务结果请求命令 + */ + public static final String CLIENT_REQ_TASK_RESULT = "char:taskResult"; + /** + * 批量上传数据文件请求命令 + */ + public static final String CLIENT_REQ_UPLOAD_DATAS ="byte:datas"; + /** + * 批量上传数据文件请求命令2 + */ + public static final String CLIENT_REQ_UPLOAD_DATAS_2 ="byte:datas2"; + + /** + * 回传文件请求命令 + */ + public static final String CLIENT_REQ_TASK_RETURNFILE = "byte:taskReturn"; + + /* 与NMS_Web通讯操作 **/ + + /** + * 监测设置信息变更请求命令 + */ + public static String WEB_NOTICE_SET_INFO_ALERT = "char:setInfoAlert"; + /** + * 下发第三方监测脚本命令 + */ + public static final String SEND_PLUGIN_SCRIPT_FILE = "char:sendPluginScriptFile"; + /** + * 下载第三方检测脚本 + */ + public static final String DOWNLOAD_PLUGIN_SCRIPT="char:downloadPluginScript"; + /** + * 节点或节点组状态变更请求命令 + */ + public static String WEB_NOTICE_NODE_AND_GROUP_STATE_ALERT = "char:nodeAndGroupAlert"; + /** + * 监测类别信息变更包括字段告警的变更请求命令 + */ + public static String WEB_NOTICE_CHECK_TYPE_ALERT = "char:checkTypeIdAlert"; + /** + * 任务信息变更请求命令 + */ + public static String WEB_NOTICE_MISSION_INFO_ALERT = "char:missionInfoAlert"; + /** + * DC 管理IP segment 重置(准备废弃) + */ + public static String WEB_NOTICE_DATACONTROLLER = "char:Datacontroller"; + + /** + * 节点是否报主动告警变更请求命令 + */ + public static String WEB_NOTICE_ACTIVE_ALARM_START_ALERT = "char:isActiveAlarmStart"; + + /** + * DC 管理IP segment 重置 + */ + public static String WEB_UPDATE_CONFIG = "char:updateConfig"; + /** + * web 端通知 入库监测数据 + */ + public static String WEB_NOTICE_INSERT_DETECT = "char:insertDetect"; + /** + * web 端通知 停止 监测数据入库 + */ + public static String RELEASE_DETECT = "char:releaseDetect"; +} diff --git a/src/com/nms/server/util/BoneCPPool.java b/src/com/nms/server/util/BoneCPPool.java new file mode 100644 index 0000000..19b2745 --- /dev/null +++ b/src/com/nms/server/util/BoneCPPool.java @@ -0,0 +1,217 @@ +package com.nms.server.util; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.concurrent.TimeUnit; + +import org.apache.log4j.Logger; + +import com.jolbox.bonecp.BoneCP; +import com.jolbox.bonecp.BoneCPConfig; +import com.nms.server.common.Constants; + +/** + * JDBC操作数据库工具类 + * + * @date Jul 20, 2011 2:27:04 PM + * @author ZhangGang + * + */ +public class BoneCPPool { + + private static Logger logger = Logger.getLogger(BoneCPPool.class); + private static BoneCP connectionPool; + + /** + * 构造方法实现 加载驱动,创建连接 + */ + public static void initPool() throws ClassNotFoundException ,SQLException ,Exception{ + + //校验并关闭线程池 + shutdown(); + + Class.forName(Constants.DB_DRIVER); + BoneCPConfig config = new BoneCPConfig(); + config.setJdbcUrl(Constants.DB_URL); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb + config.setUsername(Constants.DB_USER_NAME); + config.setPassword(Constants.DB_PASSWORD); + + /** BoneCP主要配置参数 */ + //设置分区个数 + config.setPartitionCount(Constants.DB_POOL_PARTITION_COUNT); + //设置每个分区中的最大连接数 + config.setMaxConnectionsPerPartition(Constants.DB_POOL_CONNECTIONS_PER_PARTITION_MAX); + //设置每个分区中的最小连接数 + config.setMinConnectionsPerPartition(Constants.DB_POOL_CONNECTIONS_PER_PARTITION_MIN); + //当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数 + config.setAcquireIncrement(Constants.DB_POOL_ACQUIRE_INCREMENT); +// config.setPoolAvailabilityThreshold(poolAvailabilityThreshold); //默认20 当空闲线程所占比例小于20%时,增加连接池中连接数 + //设置获取connection超时的时间。在调用getConnection获取connection时,获取时间超过了这个参数,就视为超时并报异常 +// config.setConnectionTimeout(20, TimeUnit.SECONDS); + + /** BoneCP线程配置参数 */ + //连接(Connection)回收助手数量 + config.setReleaseHelperThreads(Constants.DB_POOL_RELEASE_HELPER_THREADS); + //会话(Statement)回收助手数量 + config.setStatementReleaseHelperThreads(Constants.DB_POOL_STATEMENT_RELEASE_HELPER_THREADS); + //设置连接最大存活时间-当连接存在超过最大存活时间时,就会关闭连接 + config.setMaxConnectionAge(Constants.DB_POOL_MAX_CONNECTION_AGE, TimeUnit.MINUTES); + //设置连接空闲时间-当连接空闲时间达到设置的时间时,就会关闭连接 + config.setIdleMaxAge(Constants.DB_POOL_IDLE_MAX_AGE,TimeUnit.MINUTES); + //设置每15分钟检查数据库中的空闲连接数 依赖setIdleMaxAge + config.setIdleConnectionTestPeriod(Constants.DB_POOL_IDLE_CONNECTION_TEST_PERIOD,TimeUnit.MINUTES); + config.setConnectionTestStatement("select 1 from dual"); + config.setInitSQL("select 1 from dual"); + config.setResetConnectionOnClose(false);//当为true时mysql 关闭连接 时 会抛出异常 + /** BoneCP可选配置参数 :之前设置的是1分钟,默认5次,超时时间为Long.max_value,就是会阻塞死掉 + * setConnectionTimeout:获取一次连接超时的时间 + * setAcquireRetryAttempts:获取连接尝试次数 + * setAcquireRetryDelay:两次获取连接的时间间隔 + * */ +// config.setAcquireRetryAttempts(3);//此参数默认为5次 + //设置重新获取连接的次数间隔时间。这个参数默认为7000,单位:毫秒。 + config.setAcquireRetryDelay(10, TimeUnit.SECONDS);// + // 2016-07-02 zbc 设置获取连接超时的时间 + // 在未设置获取连接超时时间情况下,尝试指定连接次数获取连接仍然失败后,可能继续获取连接直至获取连接成功或导致进程假死。(可能为连接池BUG) +// config.setConnectionTimeout(config.getAcquireRetryAttempts()* config.getAcquireRetryDelay(TimeUnit.MINUTES), +// TimeUnit.MINUTES); + config.setConnectionTimeout(Constants.DB_GET_CONNECTION_TIMEOUT,TimeUnit.SECONDS); + //设置连接池初始化功能。 + config.setLazyInit(true); + //缓存的statement数量 默认0 +// config.setStatementsCacheSize(0); + config.setCloseConnectionWatch(true); + //设置sql日志输出 + config.setLogStatementsEnabled(Constants.DB_POOL_LOG_STATEMENT_ENABLE); + //设置sql执行超时时间 + config.setQueryExecuteTimeLimit(Constants.DB_POOL_QUERY_EXECUTE_TIME_LIMIT, TimeUnit.SECONDS); +// config.setPoolName("DC连接池"); + config.setPoolName("DC connection pool"); + + //创建数据库连接 + connectionPool = new BoneCP(config); // setup the connection pool--多次initPool时会出现,多个对象赋值给一个全局静态变量,知道系统推出,才会关闭所有连接(虽然会shutdown,但是总的趋势还是不断的增加数据库连接) + logger.debug("BoneCP初始化连接池成功"); + } + + + + + public static Connection getConnection() throws SQLException{ + if(connectionPool != null ){ + logger.debug("获取连接中。。。"); + Connection connection = connectionPool.getConnection(); + logger.debug("获取连接成功"); + boolean closed = connection.isClosed(); + if( closed){ + logger.warn("connection.toString()->" + connection.toString()); + } + + int [] array = getDBLinkSize(connection); + logger.debug("closed("+closed+"),autoCommit="+connection.getAutoCommit()+" [dbpool:总数("+connectionPool.getTotalCreatedConnections() + +")空闲(" +connectionPool.getTotalFree() + +")已使用(" +connectionPool.getTotalLeased()+")]" + +"[oracle:总数("+array[0]+")活动数("+array[0]+")]"); + connection.setAutoCommit(true); + return connection; + }else{ + return null; + } + } + + /** + * 查询当前DC的数据库连接数 + * @time Jan 13, 2013-4:25:03 PM + * @param dao + * @return + * @throws Exception + */ + public static int[] getDBLinkSize(Connection connection){ + int [] linksArrays = new int[]{-1,0}; + PreparedStatement ps = null; + ResultSet rs = null; + try { + if(Constants.IS_MYSQL){ + + }else{ + /*String dbLinkSizeSql = "select count(1) dbLinks,count(decode(status,'ACTIVE',1,0,0)) activeLinks from v$session t where lower(PROGRAM)=lower('JDBC Thin Client') and lower(username) =lower(?) and lower(t.MACHINE) = lower(?)"; + String dbUserName = Constants.DB_USER_NAME; + String computerName = InetAddress.getLocalHost().getHostName(); + ps = connection.prepareStatement(dbLinkSizeSql); + ps.setString(1, dbUserName); + ps.setString(2, computerName); + rs = ps.executeQuery(); + if(rs.next()){ + linksArrays [0] = rs.getInt(1); + linksArrays [1] = rs.getInt(2); + }*/ + } + } catch (Exception e) { + logger.error("Query activity database connection information exception", e); + }finally{ + try { + if(rs!=null){rs.close();} + if(ps!=null){ps.close();} + } catch (Exception e) { + logger.error("Query activity database connection information exception", e); + } + } + + return linksArrays; + } + + public static void shutdown(){ + if(connectionPool != null){ + connectionPool.close(); + connectionPool.shutdown(); // shutdown connection pool. + connectionPool = null; + } + } + + /* + public static void main(String [] args) { + + try { + initPool(); + } catch (Exception e) { + e.getMessage(); + } + + Runnable runnable = new Runnable(){ + + @Override + public void run() { + CommonDao dao = null; + Map info = new HashMap(); + String value = Math.random()+""; + value = value.length()>19?value.substring(0,19):value; + info.put("test_value", value) ; + try { + dao = new CommonDao(); + dao.setAutoCommit(false); + dao.insertObj("TEST_BONECP_ZG", info); + ArrayList fields = new ArrayList(); + fields.add("counts"); + ArrayList> maps = dao.dbSelect("select count(1) counts from TEST_BONECP_ZG", fields); + if(maps!=null && maps.size()>0){ + String counts = maps.get(0).get("counts"); + logger.debug("counts "+counts); + } + dao.commit(); + } catch (SQLExecuteTimeoutException e) { + logger.error(e.getMessage()); + } catch (SQLException e) { + logger.error(e.getMessage()); + }finally{ + dao.close(); + } + } + + }; + + for (int i = 0; i < 8000; i++) { + Common.scheduled.scheduleAtFixedRate(runnable, 0, 500, TimeUnit.MILLISECONDS); + } + }*/ +} diff --git a/src/com/nms/server/util/BoneCPTest.java b/src/com/nms/server/util/BoneCPTest.java new file mode 100644 index 0000000..d3ead77 --- /dev/null +++ b/src/com/nms/server/util/BoneCPTest.java @@ -0,0 +1,72 @@ +package com.nms.server.util; + + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Date; +import java.util.concurrent.TimeUnit; +import com.jolbox.bonecp.BoneCP; +import com.jolbox.bonecp.BoneCPConfig; + + +public class BoneCPTest { + + public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException { + Class.forName("oracle.jdbc.driver.OracleDriver"); + BoneCPConfig config = new BoneCPConfig(); + + config.setJdbcUrl("jdbc:oracle:thin:@10.0.6.100:1521:ict"); + config.setUsername("nms"); + config.setPassword("nms"); + + config.setPartitionCount(1); // 设置分区个数:1 + config.setMaxConnectionsPerPartition(1); // 设置最大连接数:5 + config.setLazyInit(true); // 连接池初始化默认为空,最小连接数失效 + config.setMinConnectionsPerPartition(1); + config.setAcquireIncrement(1); // 设置每次增长的连接数:1 + + config.setLogStatementsEnabled(true); // 打印SQL + + config.setAcquireRetryDelay(2, TimeUnit.SECONDS); // 20s + config.setAcquireRetryAttempts(3); // 3 times +// config.setConnectionTimeout(10, TimeUnit.SECONDS); // 超时时间 +// config.setMaxConnectionAge(120, TimeUnit.SECONDS); +// config.setInitSQL("select 1 from dual"); + + config.setConnectionTestStatement("select 1234567 from dual"); + config.setIdleConnectionTestPeriod(1, TimeUnit.MINUTES); + + BoneCP bonecpPool = new BoneCP(config); + + System.out.println(config.getConnectionTestStatement()); + System.out.println(config.getIdleConnectionTestPeriodInMinutes()); + + boolean repeatFlag = true; + boolean resetDB = false; + while(repeatFlag) { + Date d = new Date(); + System.out.println(d+"新连接开始"); + try{ + Connection conn = bonecpPool.getConnection(); + System.out.println(d+" "+conn); + // 显示连接池连接个数 + System.out.println(d+"创建连接" + bonecpPool.getTotalCreatedConnections() + "个,已使用" + + bonecpPool.getTotalLeased() + "个,未占用" + bonecpPool.getTotalFree() + "个"); +// boolean isvalid = conn.isValid(0); + boolean isvalid = true; + System.out.println(d+"\t" + isvalid); + if(isvalid) { + conn.prepareStatement("select * from dual").executeQuery(); + } +// conn.close(); + } catch (Exception e) { + System.out.println(d+"start*****************************************"); + e.printStackTrace(); + resetDB = true; + System.out.println(d+"[end]*****************************************"); + } + System.out.println(d+"当前连接结束\n----------\n\n"); + Thread.sleep(5000); + } + } +} diff --git a/src/com/nms/server/util/CSVUtils.java b/src/com/nms/server/util/CSVUtils.java new file mode 100644 index 0000000..e0c780a --- /dev/null +++ b/src/com/nms/server/util/CSVUtils.java @@ -0,0 +1,455 @@ +package com.nms.server.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.InetAddress; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.Ostermiller.util.ExcelCSVParser; +import com.Ostermiller.util.ExcelCSVPrinter; + +/** + * @date Sep 8, 2011 2:30:14 PM + * @author ZhangGang + * + */ +public class CSVUtils { + private static Logger logger = Logger.getLogger(CSVUtils.class); + /** + * 获得本机IP + * + * @return + */ + public static String getLocalIp() { + String nativeip = ""; + try { + InetAddress ipv4 = InetAddress.getLocalHost(); + nativeip = ipv4.getHostAddress().toString(); + + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + + return nativeip; + } + + /** + * 写CSV文件,只写入一行 + * + */ + public static boolean csvFilePrinter(File file, String charset, String[] values){ + OutputStreamWriter fos = null; + try { + fos = new OutputStreamWriter( + new FileOutputStream(file), charset); + + + ExcelCSVPrinter ecsvp = new ExcelCSVPrinter(fos); + ecsvp.changeDelimiter(','); + ecsvp.setAutoFlush(true); + ecsvp.writeln(values); + + fos.flush(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return false; + }finally { + if(fos!=null){ + try { + fos.close(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + } + + return true; + } + + /** + * 写CSV文件,写入多行 + * + */ + public static byte[] csvBytesPrinter1(List values,String charset){ + ExcelCSVPrinter ecsvp = null; + OutputStreamWriter osw = null; + ByteArrayOutputStream baos = null; + byte [] bts = null; + try { + baos = new ByteArrayOutputStream(); + //创建解析信息流 + if(StringUtils.isNotBlank(charset)){ + osw = new OutputStreamWriter(baos, charset); + ecsvp = new ExcelCSVPrinter(osw); + }else{ + ecsvp = new ExcelCSVPrinter(baos); + } + + ecsvp.changeDelimiter(','); + ecsvp.setAutoFlush(true); + if(values!=null && values.size()>0){ + for(String [] datas : values){ + ecsvp.writeln(datas); + } + } + osw.flush(); + bts = baos.toByteArray(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + }finally { + try { + if(ecsvp!=null){ecsvp.close();ecsvp = null;} + if(baos!=null){baos.close();baos = null;} + if(osw!=null){osw.close();osw = null;} + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + return bts; + } + + /** + * 写CSV文件,写入多行 + * + */ + public static byte[] csvFilePrinter(List> values,String charset){ + ExcelCSVPrinter ecsvp = null; + OutputStreamWriter osw = null; + ByteArrayOutputStream baos = null; + byte [] bts = null; + try { + baos = new ByteArrayOutputStream(); + //创建解析信息流 + if(StringUtils.isNotBlank(charset)){ + osw = new OutputStreamWriter(baos, charset); + ecsvp = new ExcelCSVPrinter(osw); + }else{ + ecsvp = new ExcelCSVPrinter(baos); + } + + ecsvp.changeDelimiter(','); + ecsvp.setAutoFlush(true); + if(values!=null && values.size()>0){ + for(List datas : values){ + ecsvp.writeln((String[])datas.toArray(new String[0])); + } + } + osw.flush(); + bts = baos.toByteArray(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + }finally { + try { + if(ecsvp!=null){ecsvp.close();ecsvp = null;} + if(baos!=null){baos.close();baos = null;} + if(osw!=null){osw.close();osw = null;} + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + return bts; + } + + public static boolean csvFilePrinter(File file, String charset, List values) throws Exception{ + OutputStreamWriter fos = null; + try { + fos = new OutputStreamWriter( + new FileOutputStream(file), charset); + + + ExcelCSVPrinter ecsvp = new ExcelCSVPrinter(fos); + ecsvp.changeDelimiter(','); + ecsvp.setAutoFlush(true); + for(String[] val : values){ + ecsvp.writeln(val); + } + + fos.flush(); + } catch (IOException e) { + throw e; + }finally { + if(fos!=null){ + try { + fos.close(); + } catch (IOException e) { + } + } + } + + return true; + } + + /** + * 解析CSV文件缓存 中的指定行 + * + */ + public static byte[] csvBytesPrinter(List datas,String charset){ + String [] datas0 = new String[0]; + return csvBytesPrinter(datas.toArray(datas0),charset); + } + /** + * 解析CSV文件缓存 中的指定行 + * + */ + public static byte[] csvBytesPrinter(String [] datas,String charset){ + ExcelCSVPrinter ecsvp = null; + OutputStreamWriter osw = null; + ByteArrayOutputStream baos = null; + byte [] bts = null; + try { + baos = new ByteArrayOutputStream(); + //创建解析信息流 + if(StringUtils.isNotBlank(charset)){ + osw = new OutputStreamWriter(baos, charset); + ecsvp = new ExcelCSVPrinter(osw); + }else{ + ecsvp = new ExcelCSVPrinter(baos); + } + + ecsvp.changeDelimiter(','); + ecsvp.setAutoFlush(true); + ecsvp.writeln(datas); + osw.flush(); + bts = baos.toByteArray(); + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + }finally { + try { + if(ecsvp!=null){ecsvp.close();ecsvp = null;} + if(baos!=null){baos.close();baos = null;} + if(osw!=null){osw.close();osw = null;} + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + return bts; + } + /** + * 解析CSV文件缓存 中的指定行 + * + */ + public static String[] csvBytesParser(byte[] fileBts,int rowNum ){ + + ByteArrayInputStream bais = null; //字节流对象 + InputStreamReader isr =null; + ExcelCSVParser csvParser = null; //CSV解析对象 + String[] currLine = null; + + try { + + bais = new ByteArrayInputStream(fileBts); + UnicodeReader unicodeReader = new UnicodeReader(bais,Charset.defaultCharset().name()); + csvParser = new ExcelCSVParser(unicodeReader); + + while(true){ + currLine = csvParser.getLine(); + if(currLine == null){ + break; + } + if(rowNum == csvParser.getLastLineNumber()){ + break; + }else{ + currLine = null; + } + } + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + }finally { + try { + if(csvParser!=null){csvParser.close();csvParser = null;} + if(isr!=null){isr.close();isr = null;} + if(bais!=null){bais.close();bais = null;} + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + + return currLine; + } + /** + * 解析CSV文件缓存 中的指定行 + * + */ + public static String[] csvBytesParser(byte[] bts, String charset,int rowNum ){ + + ByteArrayInputStream bais = null; //字节流对象 + InputStreamReader isr =null; + ExcelCSVParser csvParser = null; //CSV解析对象 + String[] currLine = null; + + try { + + //创建解析信息流 + if(StringUtils.isNotBlank(charset)){ + bais = new ByteArrayInputStream(bts); + String encode = new UnicodeReader(bais,charset).getEncoding(); + charset = StringUtils.isBlank(encode)?charset:encode; + isr = new InputStreamReader(bais,charset); + csvParser = new ExcelCSVParser(isr); + }else{ + bais = new ByteArrayInputStream(bts); + csvParser = new ExcelCSVParser(bais); + } + + while(true){ + currLine = csvParser.getLine(); + if(currLine == null){ + break; + } + if(rowNum == csvParser.getLastLineNumber()){ + break; + }else{ + currLine = null; + } + } + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + }finally { + try { + if(csvParser!=null){csvParser.close();csvParser = null;} + if(isr!=null){isr.close();isr = null;} + if(bais!=null){bais.close();bais = null;} + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + + return currLine; + } + /** + * 解析CSV文件缓存 中的所有行 + * + */ + public static List csvBytesParser(byte[] bts, String charset ){ + + ByteArrayInputStream bais = null; //字节流对象 + InputStreamReader isr =null; + ExcelCSVParser csvParser = null; //CSV解析对象 + List allLine = new ArrayList(); + String[] currLine = null; + + try { + + //创建解析信息流 + if(StringUtils.isNotBlank(charset)){ + bais = new ByteArrayInputStream(bts); + String encode = new UnicodeReader(bais,charset).getEncoding(); + charset = StringUtils.isBlank(encode)?charset:encode; + isr = new InputStreamReader(bais,charset); + csvParser = new ExcelCSVParser(isr); + }else{ + bais = new ByteArrayInputStream(bts); + csvParser = new ExcelCSVParser(bais); + } + + while(true){ + currLine = csvParser.getLine(); + if(currLine == null){ + break; + }else { + allLine.add(currLine); + } + } + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + }finally { + try { + if(csvParser!=null){csvParser.close();csvParser = null;} + if(isr!=null){isr.close();isr = null;} + if(bais!=null){bais.close();bais = null;} + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + + return allLine; + } + + /** + * 解析CSV文件中的指定行 + * + */ + public static String[] csvFileParser(File file, String charset, int rowNum){ + //InputStream in = null; + InputStreamReader inputStreamReader = null; //字节流对象 + ExcelCSVParser csvParser = null; //CSV解析对象 + String[] currLine = null; + try { + //创建解析信息流 + inputStreamReader = new InputStreamReader(new FileInputStream(file),charset); + csvParser = new ExcelCSVParser(inputStreamReader); + while(true){ + currLine = csvParser.getLine(); + if(currLine == null){ + break; + } + if(rowNum == csvParser.getLastLineNumber()){ + break; + }else{ + currLine = null; + } + } + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + }finally { + try { + if(csvParser!=null){csvParser.close();csvParser = null;} + if(inputStreamReader!=null){inputStreamReader.close();inputStreamReader = null;} + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + + return currLine; + } + + /** + * 解析CSV文件的所有行 + * + */ + public static List csvFileParser(File file, String charset){ + //InputStream in = null; + InputStreamReader inputStreamReader = null; //字节流对象 + ExcelCSVParser csvParser = null; //CSV解析对象 + List allLineList = null; + try { + //创建解析信息流 + inputStreamReader = new InputStreamReader(new FileInputStream(file),charset); + csvParser = new ExcelCSVParser(inputStreamReader); + String[] currLine = null; + while((currLine=csvParser.getLine())!=null){ + allLineList.add(currLine); + } + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return null; + }finally { + try { + if(csvParser!=null){csvParser.close();csvParser = null;} + if(inputStreamReader!=null){inputStreamReader.close();inputStreamReader = null;} + } catch (IOException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + return allLineList; + } + +} diff --git a/src/com/nms/server/util/CommandExecUtil.java b/src/com/nms/server/util/CommandExecUtil.java new file mode 100644 index 0000000..beab167 --- /dev/null +++ b/src/com/nms/server/util/CommandExecUtil.java @@ -0,0 +1,335 @@ +package com.nms.server.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + + +public class CommandExecUtil +{ + private static Logger logger = Logger.getLogger(CommandExecUtil.class); + + //执行linux命令 + public static String execLinuxCmd(String cmd) throws Exception{ + BufferedReader bReader = null; + BufferedReader errorReader = null; + Process process = null; + try { + logger.info("execLinuxCmd start-------" + cmd); + String[] shellCmd = new String[] { "/bin/bash", "-c", cmd }; + process = Runtime.getRuntime().exec(shellCmd); + process.getOutputStream().close(); + + bReader = new BufferedReader(new InputStreamReader(process + .getInputStream())); + errorReader = new BufferedReader(new InputStreamReader(process + .getErrorStream())); + String line = null; + while ((line = bReader.readLine()) != null) { + logger.debug("execLinuxCmd line--->" + line); + } + StringBuffer errorSb = new StringBuffer(); + while ((line = errorReader.readLine()) != null) { + logger.debug("execLinuxCmd error line--->" + line); + errorSb.append(line + ","); + } + + if (errorSb.toString().length() > 0) { + return errorSb.toString().substring(0, errorSb.toString().length()-1); + } + + logger.info("execLinuxCmd end-------" + cmd); + } catch (Exception e) { + throw e; + } finally { + try { + if (bReader != null) { + bReader.close(); + } + if (errorReader != null) { + errorReader.close(); + } + if (process != null) { + process.destroy(); + } + } catch (Exception e1) { + } + } + return null; + } + + public static boolean startNetService(String cmd) throws Exception{ + boolean flag = false; + Process process = null; + System.out.println(cmd); + String[] cmdArr = new String[] { "cmd.exe", "/C", "net start "+cmd}; + + try { + process = Runtime.getRuntime().exec(cmdArr); + //关闭进程流,保证进程流畅运行 + process.getInputStream().close(); + process.getOutputStream().close(); + process.getErrorStream().close(); + synchronized (process) { + process.wait(1000 * 2); + } + flag = true; + } catch (IOException e) { + throw e; + }finally{ + process.destroy(); + } + return flag; + } + + public static boolean startProcess(String cmd) throws Exception{ + boolean flag = false; + Process process = null; + System.out.println(cmd); + String[] cmdArr = new String[] { "cmd.exe", "/C", cmd}; + + try { + process = Runtime.getRuntime().exec(cmdArr); + //关闭进程流,保证进程流畅运行 + process.getInputStream().close(); + process.getOutputStream().close(); + process.getErrorStream().close(); + synchronized (process) { + process.wait(1000 * 2); + } + flag = true; + } catch (IOException e) { + throw e; + }finally{ + process.destroy(); + } + return flag; + } + + public static boolean processRunningCheck(String imageName) throws Exception{ + Thread.sleep(1000);//检查进程是否存在时,先暂停1秒钟 + boolean flag = false; + String cmd = "TASKLIST /FI \"IMAGENAME eq "+imageName+"\" /FO \"csv\" /NH"; + Process process = null; + System.out.println(cmd); + String[] arr = new String[] {"cmd.exe","/C",cmd}; + BufferedReader br = null; + + try { + process = Runtime.getRuntime().exec(arr); + br = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = null; + while ((line = br.readLine())!= null) { + System.out.println(line); + if(StringUtils.isNotEmpty(line)){ + break; + } + } + if(StringUtils.isNotBlank(line) && line.indexOf("\""+imageName+"\"")!= -1){ + System.out.println("IMAGENAME:>"+imageName+" 进程存在"); + flag = true; + } + } catch (IOException e) { + throw e; + }finally{ + if(br!=null){ + br.close(); + br=null; + } + process.destroy(); + } + return flag; + } + + public static boolean processRunningCheck(Long PID) throws Exception{ + boolean flag = false; + String cmd = "TASKLIST /FI \"PID eq "+PID+"\" /FO \"csv\" /NH"; + Process process = null; + String[] arr = new String[] { "cmd.exe", "/C",cmd}; + BufferedReader br = null; + System.out.println(cmd); + try { + process = Runtime.getRuntime().exec(arr); + br = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = null; + while ((line = br.readLine())!= null) { + System.out.println(line); + if(StringUtils.isNotEmpty(line)){ + break; + } + } + if(StringUtils.isNotBlank(line) && line.indexOf("\""+PID+"\"")!= -1){ + System.out.println("PID:>"+PID+" 进程存在"); + flag = true; + } + } catch (IOException e) { + throw e; + }finally{ + if(br!=null){ + br.close(); + br=null; + } + process.destroy(); + } + return flag; + } + + public static boolean exeCmd(String cmd) throws Exception{ + boolean flag = false; +// String cmd = "WMIC PROCESS WHERE Name=\""+processName+"\" DELETE"; + Process process = null; + String[] arr = new String[] {"cmd.exe","/C", cmd}; + System.out.println(cmd); + BufferedReader br = null; + try { + process = Runtime.getRuntime().exec(arr); + //关闭进程流,保证进程流畅运行 + process.getInputStream().close(); + process.getOutputStream().close(); + process.getErrorStream().close(); + synchronized (process) { + process.wait(1000 * 2); + } + flag = true; + } catch (IOException e) { + throw e; + }finally{ + if(br!=null){ + br.close(); + br=null; + } + process.destroy(); + } + return flag; + } + + public static boolean killProcess(String processName) throws Exception{ + boolean flag = false; + String cmd = "WMIC PROCESS WHERE Name=\""+processName+"\" DELETE"; + Process process = null; + String[] arr = new String[] {"cmd.exe","/C", cmd}; + System.out.println(cmd); + BufferedReader br = null; + try { + process = Runtime.getRuntime().exec(arr); + //关闭进程流,保证进程流畅运行 + process.getInputStream().close(); + process.getOutputStream().close(); + process.getErrorStream().close(); +// br = new BufferedReader(new InputStreamReader(process.getInputStream())); +// String line = br.readLine(); +// System.out.println(line); + synchronized (process) { + process.wait(1000 * 2); + } + flag = true; + } catch (IOException e) { + throw e; + }finally{ + if(br!=null){ + br.close(); + br=null; + } + process.destroy(); + } + return flag; + } + public static boolean killProcess(Long PID) throws Exception{ + boolean flag = false; + String cmd = "WMIC PROCESS WHERE PROCESSID="+PID+" DELETE "; + Process process = null; + String[] arr = new String[] {"cmd.exe", cmd}; + System.out.println(cmd); + BufferedReader br = null; + try { + process = Runtime.getRuntime().exec(arr); + br = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = br.readLine(); + System.out.println(line); + flag = true; + } catch (IOException e) { + throw e; + }finally{ + if(br!=null){ + br.close(); + br=null; + } + process.destroy(); + } + return flag; + } + + //执行windows命令 + public static String execWinCmd(String cmd) throws Exception { + BufferedReader bReader = null; + BufferedReader errorReader = null; + Process process = null; + try { + logger.info("execWinCmd start-------" + cmd); + String[] cmdArr = new String[] { "cmd.exe", "/C", cmd }; + process = Runtime.getRuntime().exec(cmdArr); + if (cmd.endsWith(".exe")) { + synchronized (process) { + process.wait(1000 * 5); + } + } else { + process.getOutputStream().close(); + bReader = new BufferedReader(new InputStreamReader(process + .getInputStream())); + errorReader = new BufferedReader(new InputStreamReader(process + .getErrorStream())); + String line = null; + while ((line = bReader.readLine()) != null) { + logger.debug("execWinCmd line--->" + line); + } + StringBuffer errorSb = new StringBuffer(); + while ((line = errorReader.readLine()) != null) { + logger.debug("execWinCmd error--->" + line); + errorSb.append(line + ","); + } + if (errorSb.toString().length() > 0) { + return errorSb.toString().substring(0, + errorSb.toString().length() - 1); + } + } + + logger.info("execWinCmd end-------" + cmd); + + } catch (Exception e) { + throw e; + } finally { + try { + if (bReader != null) { + bReader.close(); + } + if (errorReader != null) { + errorReader.close(); + } + if (process != null) { + process.destroy(); + } + } catch (Exception e1) { + } + } + + return null; + } + + public static void main(String[] args) { + try + { +// System.out.println(System.getProperty("user.dir")); +// String pp = new File(System.getProperty("user.dir")).getParentFile().getAbsolutePath()+"/conf/myconfig.properties"; +// System.out.println(pp); +// CommandExecUtil.execWinCmd("tasklist"); + CommandExecUtil.execWinCmd("net start DataController"); + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/com/nms/server/util/ConnectionOracle.java b/src/com/nms/server/util/ConnectionOracle.java new file mode 100644 index 0000000..320bfea --- /dev/null +++ b/src/com/nms/server/util/ConnectionOracle.java @@ -0,0 +1,984 @@ +package com.nms.server.util; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; +import com.nms.server.dao.OracleToMysql; + +/** + * JDBC操作数据库工具类 + * + * @date Jul 20, 2011 2:27:04 PM + * @author ZhangGang + * + */ +public class ConnectionOracle { + private Logger logger = Logger.getLogger(ConnectionOracle.class); + private Connection conn = null; + private Statement sta = null; + private PreparedStatement infoStmt = null;//detection_info表对应的preparedStatementstatement + private PreparedStatement warningStmt = null;//warning表对应的preparedStatementstatement + private PreparedStatement emailStmt = null;//email表对应的preparedStatementstatement + private PreparedStatement statusChangeTimeStmt = null;//new表对应的preparedStatementstatement + private PreparedStatement netStmt = null;//特殊格式追加,net表对应的preparedStatementstatement + private PreparedStatement diskStmt = null;//特殊格式追加信息,disk表对应的preparedStatementstatement + private PreparedStatement detailStmt = null;//detail表对应的preparedStatementstatement + /** + * 以下 记录 当前有多少 insert + */ + private int infoCount= 0; + private int warningCount= 0; + private int emailCount= 0; + private int statusChangeTimeCount= 0; + private int netCount= 0; + private int diskCount= 0; + private int detailCount= 0; + + + private PreparedStatement ps = null; + private ArrayList psList = new ArrayList(); + private ResultSet dbResultSet = null; // 操作 select 结果集 + + private SimpleDateFormat format2 = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); //Java Date 类型数据格式化格式 + + /** + * 构造方法实现 加载驱动,创建连接 + */ + protected ConnectionOracle() throws SQLException{ + try + { + conn = DbPool.getConnection(); + /*if(Constants.IS_MYSQL){ + setSqlMode(conn); + }*/ + } catch (SQLException e) + { + logger.error("Getting a database connection failure"); + throw e; + }catch (Exception e){ + logger.error("Getting a database connection failure"); + } + if(conn == null){ + throw new SQLException("Getting a database connection failure"); + }else{ + logger.debug("获得数据库连接"); + } + + } + + + public void setSqlMode(Connection conn){ + try { + PreparedStatement stmt = conn.prepareStatement("set @@sql_mode='NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'"); + stmt.executeUpdate(); + } catch (Exception e) { + logger.error("Setting sql-mode exceptions",e); + } + } + + public boolean isConnClosed() throws SQLException{ + return (conn==null)||(conn.isClosed()); + } + + /** + * 使用Statement对象 实现select操作 注意:在selectSql中的字段和fields中的个数、名称要保持一致 + * + * @param selectSql + * 用于执行的select语句 + * @param fields + * 要选择的字段 + * @return 含有Map的ArrayList,一条记录形成一个Map + */ + public ArrayList> dbSelect(String selectSql, + ArrayList fields) throws SQLException { + ArrayList> selectResult = new ArrayList>(); // 新结果集 + Map recordInfo; + logger.debug("查询SQL> " + selectSql.toString()); + if (sta == null) { + sta = conn.createStatement(); // 定义Statement对象 + sta.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + selectSql = OracleToMysql.trans(selectSql); + dbResultSet = sta.executeQuery(selectSql); + // 循环取值 + while (dbResultSet.next()) { + recordInfo = new HashMap(); + for (int i = 0; i < fields.size(); ++i) + recordInfo.put((String) fields.get(i), dbResultSet + .getString(fields.get(i))); + selectResult.add(recordInfo); + } + dbResultSet.close(); // 断开结果集 + logger.debug("dbSelect success"); + return selectResult; + } + + //查询大量数据,且网络较差的时候使用,减少和数据库的交互次数 + public ArrayList> dbSelectByFetchSize(String selectSql, + ArrayList fields) throws SQLException { + ArrayList> selectResult = new ArrayList>(); // 新结果集 + Map recordInfo; + logger.debug("查询SQL> " + selectSql.toString()); + if (sta == null) { + sta = conn.createStatement(); // 定义Statement对象 + sta.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + sta.setFetchSize(Constants.FETCH_SIZE); + selectSql = OracleToMysql.trans(selectSql); + dbResultSet = sta.executeQuery(selectSql); + // 循环取值 + while (dbResultSet.next()) { + recordInfo = new HashMap(); + for (int i = 0; i < fields.size(); ++i){ + recordInfo.put((String) fields.get(i), dbResultSet + .getString(fields.get(i))); + } + selectResult.add(recordInfo); + } + dbResultSet.close(); // 断开结果集 + logger.debug("dbSelect success,共耗时"); + sta.setFetchSize(0);//为了不影响其它方法,此处将fetchsize修改为默认值,设置为0(jdbc自动取合适的值),一般会默认取10 + return selectResult; + } + + + //查询大量数据,且网络较差的时候使用,减少和数据库的交互次数 + public List dbSelectList(String selectSql, + ArrayList fields) throws SQLException { + List rltList = new ArrayList(); + logger.debug("查询SQL> " + selectSql.toString()); + if (sta == null) { + sta = conn.createStatement(); // 定义Statement对象 + sta.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + sta.setFetchSize(Constants.FETCH_SIZE); + selectSql = OracleToMysql.trans(selectSql); + dbResultSet = sta.executeQuery(selectSql); + // 循环取值 + int fieldsSize = fields.size(); + while (dbResultSet.next()) { + String[] rlt = new String[fields.size()]; + for (int i = 0; i < fieldsSize; ++i){ + rlt[i] = dbResultSet.getString(fields.get(i)); + } + rltList.add(rlt); + } + dbResultSet.close(); // 断开结果集 + logger.debug("dbSelect success,共耗时"); + sta.setFetchSize(0);//为了不影响其它方法,此处将fetchsize修改为默认值,设置为0(jdbc自动取合适的值),一般会默认取10 + return rltList; + } + + /** + * 使用PeraredStatement对象 实现select操作 注意:在selectSql中的字段和fields中的个数、名称要保持一致 + * + * @param selectSql + * 用于执行的select语句 + * @param fields + * 要选择的字段 + * @param params + * 参数数组 + * @return 含有Map的ArrayList,一条记录形成一个Map + */ + public ArrayList> dbSelect(String selectSql, + ArrayList fields, Object... params) throws SQLException { + ArrayList> selectResult = new ArrayList>(); + Map recordInfo; + logger.debug("存储SQL> " + selectSql.toString()); + // 设置参数 + selectSql = OracleToMysql.trans(selectSql); + psList.add(ps = conn.prepareStatement(selectSql)); + ps.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + if (params != null && params.length > 0) { + logger.debug(" params "+Arrays.toString(params)); + for (int i = 0; i < params.length; i++) { + ps.setString((i + 1), StringUtils.isEmpty(params[i]==null?null:params[i].toString())?"": params[i].toString()); + } + } + dbResultSet = ps.executeQuery(); + // 循环取值 + while (dbResultSet.next()) { + recordInfo = new HashMap(); + for (int i = 0; i < fields.size(); ++i) + recordInfo.put((String) fields.get(i), dbResultSet + .getString(fields.get(i))); + selectResult.add(recordInfo); + } + dbResultSet.close(); // 断开结果集 + logger.debug("dbSelect success"); + return selectResult; + } + + /** + * 使用Statement对象 对数据库执行update,delete或insert操作 + * + * @param sql + * 要执行操作的SQL语句 + * @return 成功返回true 失败返回false + */ + + public boolean dbUpdate(String sql) throws SQLExecuteTimeoutException,SQLException,Exception { + logger.debug("更新SQL> " + sql.toString()); + if (sta == null) { + sta = conn.createStatement(); // 定义Statement对象 + sta.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + try { + sql = OracleToMysql.trans(sql); + sta.executeUpdate(sql); + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + logger.debug("dbUpdate success"); + return true; + } + + /** + * 使用PeraredStatement对象 对数据库执行update,delete或insert操作 + * + * @param sql + * 要执行操作的SQL语句 + * @param params + * 参数数组 + * @return 执行成功返回true,失败返回false + */ + public boolean dbUpdate(String sql, Object... params) throws SQLExecuteTimeoutException,SQLException { + logger.debug("更新SQL> " + sql.toString()); + sql = OracleToMysql.trans(sql); + psList.add(ps = conn.prepareStatement(sql)); // 定义PrepareStatement对象 + ps.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + if (params != null && params.length > 0) { + logger.debug(" params "+Arrays.toString(params)); + for (int i = 0; i < params.length; i++) { + ps.setString((i + 1), StringUtils.isEmpty(params[i]==null?null:params[i].toString())?"": params[i].toString()); + } + } + + try { + ps.executeUpdate(); + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + }finally{ + ps.close(); + } + logger.debug("dbUpdate success"); + return true; + } + /** + * 使用PeraredStatement对象 对数据库执行update,delete或insert操作 + * + * @param sql + * 要执行操作的SQL语句 + * @param params + * 参数数组 + * @return 执行成功返回true,失败返回false + */ + public boolean dbUpdateByBatch(String sql, List params) throws SQLExecuteTimeoutException,SQLException { + logger.debug("批量更新SQL> " + sql.toString()); + sql = OracleToMysql.trans(sql); + psList.add(ps = conn.prepareStatement(sql)); // 定义PrepareStatement对象 + ps.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + try { + if (params != null && params.size() > 0) { + for (String [] strs : params) { + logger.debug(" params "+(strs==null?null:Arrays.toString(strs))); + for(int i = 0; i "+(StringUtils.isEmpty(strs[i])?"":strs[i].toString())); + ps.setString((i+1), StringUtils.isEmpty(strs[i])?null:strs[i].toString()); + } + ps.addBatch(); + } + ps.executeBatch(); + } + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + logger.debug("dbUpdate success"); + return true; + } + /** + * 使用PeraredStatement对象 对数据库执行update,delete或insert操作 + * + * @param sqls + * 要执行操作的SQL语句 + * @param params + * 参数数组 + * @return 执行成功返回true,失败返回false + */ + public boolean dbUpdateByBatch(Listsqls) throws SQLExecuteTimeoutException,SQLException { + logger.debug(" 批量更新Sql " + sqls.size()); + if (sta == null) { + sta = conn.createStatement(); // 定义Statement对象 + sta.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + } + + try { + for (String str : sqls) { + str = OracleToMysql.trans(str); + sta.addBatch(str); + } + sta.executeBatch(); + sta.clearBatch(); + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + logger.debug("dbUpdate By Batch success"); + return true; + } + + public boolean isTimeout(String msg) { +// String temp="ORA-01013: 用户请求取消当前的操作"; + boolean flag = false; + + //判断操作 + if(StringUtils.isNotBlank(msg) && msg.indexOf("ORA-01013")!=-1){ + flag = true; + } + + return flag; + } + /** + * 将设置提交方式 + */ + public void setAutoCommit(boolean flag) throws SQLException { + conn.setAutoCommit(flag); + logger.debug("数据库连接 自动提交状态:"+(flag?"是":"否")); + } + + /** + * 提交请求 + */ + public void commit() throws SQLException { + if(!conn.getAutoCommit()){ + conn.commit(); + logger.debug("conn.commit()"); + }else{ + logger.debug("autoCommit=true,不需提交!"); + } + } + + /** + * 回滚操作 + */ + public void rollback() throws SQLException{ + if(!conn.getAutoCommit()){ + conn.rollback(); + }else{ + logger.debug("autoCommit=true,无法回滚!"); + } + } + + /** + * 关闭数据库连接 + * + * @return 成功返回true, 失败返回false + */ + public boolean clearConn() { + try { + + //- 断开结果集 + if (dbResultSet != null) { + dbResultSet.close(); + dbResultSet = null; + } + + //- 断开Statement对象 + if (sta != null) { + sta.close(); + sta = null; + } + //- 断开PeraredStatement对象集合 + if (psList.size() > 0){ + for (PreparedStatement ps : psList){ + if (ps != null){ + ps.close(); // 断开PeraredStatement对象 + } + } + psList.clear(); + } + //- 还原conn + if(conn!= null){ + if(!conn.getAutoCommit()){ + setAutoCommit(true); + } + } + logger.debug("数据库链接清理"); + return true; + } catch (SQLException e) { + logger.error("Database link cleaning exception",e); + return false; + } + } + + /** + * 关闭数据库连接 + * + * @return 成功返回true, 失败返回false + */ + public boolean close() { + try { + //- 断开结果集 + if (dbResultSet != null) { + dbResultSet.close(); + dbResultSet = null; + } + + //- 断开Statement对象 + if (sta != null) { + sta.close(); + sta = null; + } + //- 断开PeraredStatement对象集合 + if (psList.size() > 0){ + for (PreparedStatement ps : psList){ + if (ps != null){ + ps.close(); // 断开PeraredStatement对象 + } + } + psList.clear(); + } + //- 断开数据库链接 + if (conn != null) { + if(!conn.getAutoCommit()){ + conn.rollback();//还原连接 + setAutoCommit(true); + } + conn.close(); + conn = null; + } + logger.debug("数据库连接释放"); + return true; + } catch (SQLException e) { + logger.error("Database connection release exception" ,e); + return false; + } + } + + public Connection getConn(){ + return conn; + } + + private void addRecordToStatement(PreparedStatement pState,List params) + throws SQLException{ + try { + if (params != null && params.size() > 0) { + for (String [] strs : params) { + logger.debug(" params "+Arrays.toString(strs)); + for(int i = 0; i params) + throws SQLException { + if(infoStmt==null) { + sql = OracleToMysql.trans(sql); + infoStmt = conn.prepareStatement(sql); + infoStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + infoCount = 0; + } + try { + addRecordToStatement(infoStmt, params); + infoCount ++; + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + + } + + public void addRecordToDisk(String sql,List params) + throws SQLException { + if(diskStmt==null) { + sql = OracleToMysql.trans(sql); + diskStmt = conn.prepareStatement(sql); + diskStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + diskCount = 0; + } + try { + addRecordToStatement(diskStmt, params); + diskCount ++; + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + + } + + public void addRecordToNet(String sql,List params) + throws SQLException { + if(netStmt==null) { + sql = OracleToMysql.trans(sql); + netStmt = conn.prepareStatement(sql); + netStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + netCount = 0; + } + try { + addRecordToStatement(netStmt, params); + netCount ++; + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + + } + public void addRecordToWarning(String sql,List params) + throws SQLException { + if(warningStmt==null) { + sql = OracleToMysql.trans(sql); + warningStmt = conn.prepareStatement(sql); + warningStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + warningCount = 0; + } + try { + addRecordToStatement(warningStmt, params); + warningCount ++; + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + + } + public void addRecordToEmail(String sql,List params) + throws SQLException { + if(emailStmt==null) { + sql = OracleToMysql.trans(sql); + emailStmt = conn.prepareStatement(sql); + emailStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + emailCount = 0; + } + try { + addRecordToStatement(emailStmt, params); + emailCount ++; + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + + } + public void addRecordToStatusChange(String sql,List params) + throws SQLException { + if(statusChangeTimeStmt==null) { + sql = OracleToMysql.trans(sql); + statusChangeTimeStmt = conn.prepareStatement(sql); + statusChangeTimeStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + statusChangeTimeCount = 0; + } + try { + addRecordToStatement(statusChangeTimeStmt, params); + statusChangeTimeCount ++; + } catch (SQLException e) { + if(isTimeout(e.getMessage())){ + throw new SQLExecuteTimeoutException(); + }else{ + throw e; + } + } + + } + + + public void setDetailVals(String sql,List fieldNames, + MaptempMap,long currentId,String detailTableName){ + try + { + if(detailStmt==null) { + sql = OracleToMysql.trans(sql); + detailStmt = conn.prepareStatement(sql); + detailStmt.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + detailCount = 0; + } + StringBuffer sbInsertTemp = new StringBuffer(); + for(int fieldNum = 0, fieldSize = fieldNames.size(); fieldNum < fieldSize; fieldNum ++){ + String fieldName = fieldNames.get(fieldNum); + String fieldValue = null; + if("detection_info_id".equals(fieldName)){ + fieldValue = currentId + ""; + }else{ + fieldValue = tempMap.get(fieldName); + } + + if(DateUtil.isDate(fieldValue, Constants.COMMON_DATE_FORMAT)){ + detailStmt.setTimestamp(fieldNum+1, new Timestamp(format2.parse(fieldValue).getTime())); + }else{ + detailStmt.setString(fieldNum+1, StringUtils.isEmpty(fieldValue)?null:fieldValue); + } + sbInsertTemp.append(fieldValue + ","); + } + detailStmt.addBatch(); + detailCount ++; + //添加一条详细信息 + logger.debug(detailTableName + " 表添加一条数据value: " + sbInsertTemp.toString()); + + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void executeBatchAllStmt(boolean isExecuteStatusChange) throws SQLException { + //测试代码 + logger.debug("statement==null? info="+infoStmt+" detailStmt="+detailStmt+" warningStmt="+warningStmt+" netStmt="+netStmt+" diskStmt="+diskStmt+" emailStmt="+emailStmt+" statusChangeTimeStmt="+statusChangeTimeStmt); + if(infoStmt!=null && infoCount > 0) { + try + { + infoStmt.executeBatch(); + logger.debug("info 共批量导入:" + infoCount +" 条"); + } catch (SQLException e){ + logger.error(" 第三步-2: 批量入库detection_info表 异常"); + throw new SQLException(e); + }finally { + infoCount = 0; + infoStmt.clearBatch(); + } + }else{ + logger.info(" 第三步-2: detection_info的preparestatement为null "); + } + if(detailStmt!=null && detailCount > 0) { + try{ + detailStmt.executeBatch(); + logger.debug("detail 共批量导入:" + detailCount +" 条"); + } catch (SQLException e){ + logger.error(" 第三步-2: 批量入库详细表 异常"); + throw new SQLException(e); + }finally { + detailCount = 0; + detailStmt.clearBatch(); + } + }else{ + logger.info(" 第三步-2: 详细表的preparestatement为null "); + } + if(warningStmt!=null && warningCount > 0) { + try{ + warningStmt.executeBatch(); + logger.debug("warning 共批量导入:" + warningCount +" 条"); + } catch (SQLException e){ + logger.error(" 第三步-2: 批量入库warning表 异常"); + throw new SQLException(e); + }finally { + warningCount = 0; + warningStmt.clearBatch(); + } + }else{ + logger.info(" 第三步-2: warning的preparestatement为null "); + } + if(netStmt!=null && netCount > 0) { + try{ + netStmt.executeBatch(); + logger.debug("net 共批量导入:" + netCount +" 条"); + } catch (SQLException e){ + logger.error(" 第三步-2: 批量入库系统信息-net表 异常"); + throw new SQLException(e); + }finally { + netCount = 0; + netStmt.clearBatch(); + } + }else{ + logger.info(" 第三步-2: 系统信息net的preparestatement为null "); + } + if(diskStmt!=null && diskCount > 0) { + try{ + diskStmt.executeBatch(); + logger.debug("disk 共批量导入:" + diskCount +" 条"); + } catch (SQLException e){ + logger.error(" 第三步-2: 批量入库系统信息-disk表 异常"); + throw new SQLException(e); + }finally { + diskCount = 0; + diskStmt.clearBatch(); + } + }else{ + logger.info(" 第三步-2: 系统信息disk的preparestatement为null "); + } + if(emailStmt!=null && emailCount > 0) { + try{ + emailStmt.executeBatch(); + logger.debug("email 共批量导入:" + emailCount +" 条"); + } catch (SQLException e){ + logger.error(" 第三步-2: 批量入库email表 异常"); + throw new SQLException(e); + }finally { + emailCount = 0; + emailStmt.clearBatch(); + } + }else{ + logger.info(" 第三步-2: email的preparestatement为null "); + } + if(statusChangeTimeStmt!=null && isExecuteStatusChange && statusChangeTimeCount > 0) { + try{ + statusChangeTimeStmt.executeBatch(); + logger.debug("statusChangeTime 共批量导入:" + statusChangeTimeCount +" 条"); + } catch (SQLException e){ + logger.error(" 第三步-2: 批量更新new表状态变更 异常"); + throw new SQLException(e); + }finally { + statusChangeTimeCount = 0; + statusChangeTimeStmt.clearBatch(); + } + }else{ + logger.info(" 第三步-2: new表状态变更的preparestatement为null "); + } + } + + public void closeCommonBatchStatement(){ + try + { + if(infoStmt!=null) { + infoStmt.close(); + infoStmt = null; + } + if(detailStmt!=null) { + detailStmt.close(); + detailStmt = null; + } + if(warningStmt!=null) { + warningStmt.close(); + warningStmt = null; + } + if(netStmt!=null) { + netStmt.close(); + netStmt = null; + } + if(diskStmt!=null) { + diskStmt.close(); + diskStmt = null; + } + if(emailStmt!=null) { + emailStmt.close(); + emailStmt = null; + } + if(statusChangeTimeStmt!=null) { + statusChangeTimeStmt.close(); + statusChangeTimeStmt = null; + } + } catch (SQLException e){ + logger.error("", e); + }finally{ + infoCount= 0; + warningCount= 0; + emailCount= 0; + statusChangeTimeCount= 0; + netCount= 0; + diskCount= 0; + detailCount= 0; + } + + } + + public void closeDetailBatchStatement(){ + try + { + if(detailStmt!=null) { + detailStmt.close(); + detailStmt = null; + } + } catch (SQLException e){ + logger.error("", e); + }finally{ + detailCount = 0; + } + } + + + public ArrayList dbSelectSingleColumn(String selectSql,Object... params) throws Exception { + ArrayList selectResult = new ArrayList(); + logger.debug("存储SQL> " + selectSql.toString()); + // 设置参数 + psList.add(ps = conn.prepareStatement(selectSql)); + ps.setQueryTimeout(Constants.DB_STATEMENT_EXECUTE_TIMEOUT); + if (params != null && params.length > 0) { + logger.debug(" params "+Arrays.toString(params)); + for (int i = 0; i < params.length; i++) { + ps.setString((i + 1), StringUtils.isEmpty(params[i]==null?null:params[i].toString())?"": params[i].toString()); + } + } + dbResultSet = ps.executeQuery(); + // 循环取值 + while (dbResultSet.next()) { + selectResult.add(dbResultSet.getString(1)); + } + dbResultSet.close(); // 断开结果集 + logger.debug("dbSelect success"); + return selectResult; + } + + /** + * 模拟oracle start with connect by + * @param sql select * from table where xxx + * @param start 开始条件 + * @param idName + * @param pidName + * @return + * @throws Exception + */ + public String startWithConnectBy(String sql ,String start,String idName,String pidName) throws Exception{ + //List result = new ArrayList(); + Set result = new HashSet(); + result.add("-9999"); + StringBuilder sb = new StringBuilder(); + sb.append(sql); + sb.append(" and "); + sb.append(start); + List temp = this.dbSelectSingleColumn(sb.toString()); + //result.addAll(temp); + do { + if(temp == null || temp.size() <1){ + break; + }else{ + result.addAll(temp); + StringBuilder tsb = new StringBuilder(); + for(String m : temp){ + tsb.append(","); + tsb.append(m); + } + tsb.deleteCharAt(0); + start = tsb.toString(); + } + StringBuilder psb = new StringBuilder(); + psb.append(sql); + psb.append(" and "); + psb.append(pidName); + psb.append(" in ( "); + psb.append(start); + psb.append(" )"); + temp = this.dbSelectSingleColumn(psb.toString()); + //result.addAll(temp); + } while (true); + StringBuilder r = new StringBuilder(); + for(String s : result){ + r.append(","); + r.append(s); + } + if(r.length() > 0){ + r.deleteCharAt(0); + } + return r.toString(); + } + + /** + * 反向查找 所有父节点 + * @param sql + * @param start + * @param idName + * @param pidName + * @return + * @throws Exception + */ + public String startWithConnectByReverse(String table,String where ,String start,String idName,String pidName) throws Exception{ +// List result = new ArrayList(); + Set result = new HashSet(); + result.add("-9999"); + String sql = "select " + idName +","+pidName+ " from " + table + " where "+ (StringUtils.isBlank(where)?" 1=1 " : where) ; + StringBuilder sb = new StringBuilder(); + sb.append(sql); + sb.append(" and "); + sb.append(start); + ArrayList fields = new ArrayList(); + fields.add(idName); + fields.add(pidName); + Set temp = new HashSet(); + ArrayList> list = this.dbSelect(sql, fields); + if(list != null){ + for(Map map : list){ + String id = map.get(idName); + String pid = map.get(pidName); + result.add(id); + temp.add(pid); + } + } + sql = "select " +pidName+ " from " + table + " where "+ (StringUtils.isBlank(where)?" 1=1 " : where) ; + do { + if(temp == null || temp.size() <1){ + break; + }else{ + result.addAll(temp); + StringBuilder tsb = new StringBuilder(); + for(String m : temp){ + tsb.append(","); + tsb.append(m); + } + tsb.deleteCharAt(0); + start = tsb.toString(); + } + StringBuilder psb = new StringBuilder(); + psb.append(sql); + psb.append(" and "); + psb.append(idName); + psb.append(" in ( "); + psb.append(start); + psb.append(" )"); + temp.clear(); + List tempList = this.dbSelectSingleColumn(psb.toString()); + temp.addAll(tempList); + } while (true); + StringBuilder r = new StringBuilder(); + for(String s : result){ + r.append(","); + r.append(s); + } + if(r.length() > 0){ + r.deleteCharAt(0); + } + return r.toString(); + } + + +} diff --git a/src/com/nms/server/util/DateUtil.java b/src/com/nms/server/util/DateUtil.java new file mode 100644 index 0000000..81759f1 --- /dev/null +++ b/src/com/nms/server/util/DateUtil.java @@ -0,0 +1,203 @@ +package com.nms.server.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import com.nms.server.common.Constants; + +public class DateUtil { + private static Logger logger = Logger.getLogger(DateUtil.class); + /** + * 处理日期时,用到参数。格式24小时制yyyy-MM-dd HH:mm:ss + */ + public static final SimpleDateFormat YYYY_MM_DD_HH24_MM_SS = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss"); + /** + * 处理日期时,用到参数。格式为yyyy-MM-dd + */ + public static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat( + "yyyy-MM-dd"); + /** + * 处理日期时,用到参数。格式为yyyyMMdd + */ + public static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat( + "yyyyMMdd"); + + public static final SimpleDateFormat format = new SimpleDateFormat(Constants.COMMON_DATE_FORMAT); + + + /** + * 获得当前日期 + * + * @return + */ + public static String getCurrentDate(SimpleDateFormat sf) { + if(sf==null){ + sf = YYYY_MM_DD; + } + return sf.format(new Date()); + } + + /** + * 获得某日期的指定格式的字符串 + * @param sf + * @param date + * @return + */ + public static String getStingDate(SimpleDateFormat sf, Date date) { + if(date==null){ + date = new Date(); + } + if(sf==null){ + sf = YYYY_MM_DD; + } + + return sf.format(date); + } + + public static String getStringByLongStr(String longTime){ + return format.format(new Date(Long.parseLong(longTime))); + } + + public static String getStringByLong(Long longTime){ + return format.format(new Date(longTime)); + } + + public static long getDaysFromBeinToEnd(SimpleDateFormat dateFormat, + String begin, String end) { + Date beginD = null; + Date endD = null; + long days = 0; + try { + if (begin != null && !"".equals(begin)) { + beginD = dateFormat.parse(begin); + } else { + beginD = new Date(); + } + if (end != null && !"".equals(end)) { + endD = dateFormat.parse(end); + } else { + endD = new Date(); + } + days = (endD.getTime() - beginD.getTime()) / (24 * 60 * 60 * 1000); + } catch (ParseException e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + return days; + } + + return days; + + } + public static long getMinutesFromBeinToEnd(long begin, long end) { + return (end - begin) / (60 * 1000); + } + public static long getSecondsFromBeinToEnd(long begin, long end) { + return (end - begin) / 1000; + } + + /** + * + * @param begin YYYY-MM-DD HH:mm:ss + * @param end YYYY-MM-DD HH:mm:ss + * @return + */ + public static long getMinutesFromBeinToEndStr(String begin, String end) { + return (getLongDate(end,1) - getLongDate(begin,1)) / (60 * 1000); + } + + /** + * 将字符串格式日期转换成long型 + * @param strDate 格式:YYYY-MM-DD + * @return + */ + public static Long getLongDate(String strDate){ + Date date = java.sql.Date.valueOf(strDate); + Long lDate = new Long(date.getTime()); + return (lDate); + } + + /** + * 将字符串格式日期转换成long型 + * @param strDate 要转换的日期 + * @param iType value strDate type + * 0 :YYYY-MM-DD + * 1 :YYYY-MM-DD HH:mm:ss + * @return + */ + public static Long getLongDate(String strDate,int iType){ + Long retDate = null; + switch(iType){ + case 0 : + retDate = getLongDate(strDate); + break; + case 1 : + retDate = new Long(java.sql.Timestamp.valueOf(strDate).getTime()); + + } + return retDate; + + } + + /** + * 获取YYYY-MM-DD HH:mm:ss格式的当前日期 + * @return + */ + public static String getCurrentTime(){ + String s = format.format(new Date()); + return s; + } + + public static Long toLongTime(String time) { + Long longTime = null; + try + { + if(StringUtils.isNotBlank(time)) { + Date date = YYYY_MM_DD_HH24_MM_SS.parse(time); + longTime = date.getTime(); + + } + } catch (Exception e) + { + logger.error("Date"+time+"is converted to long type exception", e); + } + return longTime; + } + + /** + * 获取YYYY-MM-DD格式的当前日期 + * @return + */ +// public static String getCurrentDate(){ +// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); +// String s = df.format(new Date()); +// return s; +// } + + /** + * 校验是否为时间类型 + * @param str_input + * @param rDateFormat + * @return + */ + public static boolean isDate(String str_input,String rDateFormat){ + if (StringUtils.isNotBlank(str_input)) { + SimpleDateFormat formatter = new SimpleDateFormat(rDateFormat); + formatter.setLenient(false); + try { + formatter.format(formatter.parse(str_input)); + return true; + } catch (Exception e) { + return false; + } + } + return false; + } +/* public static void main(String args[]){ + System.out.println(DateUtil.getCurrentTime()); + System.out.println(getMinutesFromBeinToEndStr(DateUtil.getCurrentTime(),"2011-09-16 10:00:05")); + }*/ + +} diff --git a/src/com/nms/server/util/DbPool.java b/src/com/nms/server/util/DbPool.java new file mode 100644 index 0000000..8910771 --- /dev/null +++ b/src/com/nms/server/util/DbPool.java @@ -0,0 +1,47 @@ +package com.nms.server.util; + +import java.sql.Connection; +import java.sql.SQLException; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; + +public class DbPool { + private static Logger logger = Logger.getLogger(DbPool.class); + public static void initPool() throws ClassNotFoundException ,SQLException ,Exception{ + logger.debug("连接池初始化开始"); + if("bonecp".equalsIgnoreCase(Constants.DB_POOL_TYPE)){ + BoneCPPool.initPool(); + }else if("druid".equalsIgnoreCase(Constants.DB_POOL_TYPE)){ + DruidPool.initPool(); + }else { + logger.error("Please configure the connection pool type correctly: db.pool.type"); + } + logger.debug("连接池初始化结束"); + } + + public static Connection getConnection() throws SQLException{ + Connection connection = null; + if("bonecp".equalsIgnoreCase(Constants.DB_POOL_TYPE)){ + connection = BoneCPPool.getConnection(); + }else if("druid".equalsIgnoreCase(Constants.DB_POOL_TYPE)){ + connection = DruidPool.getConnection(); + }else { + logger.error("Please configure the connection pool type correctly: db.pool.type"); + } + return connection; + } + + + public static void shutdown(){ + Connection connection = null; + if("bonecp".equalsIgnoreCase(Constants.DB_POOL_TYPE)){ + BoneCPPool.shutdown(); + }else if("druid".equalsIgnoreCase(Constants.DB_POOL_TYPE)){ + DruidPool.shutdown(); + }else { + logger.error("Please configure the connection pool type correctly: db.pool.type"); + } + } +} diff --git a/src/com/nms/server/util/DruidPool.java b/src/com/nms/server/util/DruidPool.java new file mode 100644 index 0000000..5336ff5 --- /dev/null +++ b/src/com/nms/server/util/DruidPool.java @@ -0,0 +1,78 @@ +package com.nms.server.util; + +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; + +import org.apache.log4j.Logger; + +import com.alibaba.druid.pool.DruidDataSource; +import com.nms.server.common.Constants; + +public class DruidPool { + private static DruidDataSource ds; + private static Logger logger = Logger.getLogger(DruidPool.class); + + public static void initPool() throws SQLException { + URL urlObj = DruidPool.class.getClassLoader().getResource("druid.properties"); + Properties properties = new Properties(); + if(urlObj==null){ + System.err.println("找不到配置文件:druid.properties"); + logger.error("No configuration file can be found: druid.properties"); + System.exit(0); + }else{ + try { + properties.load(new FileInputStream(urlObj.getPath().replaceAll("%20", " "))); + logger.debug("配置文件:druid.properties 加载完成"); + } catch (IOException e) { + logger.error("Reading properties file error"+"",e); + } + + } + ds = new DruidDataSource(); + ds.setUrl(Constants.DB_URL); + ds.setDriverClassName(Constants.DB_DRIVER); + ds.setUsername(Constants.DB_USER_NAME); + ds.setPassword(Constants.DB_PASSWORD); + ds.configFromPropety(properties); + ds.setName("Druid连接池"); + ds.init(); + logger.debug("Druid初始化连接池成功,"+ds.toString()); + } + + + public static Connection getConnection() throws SQLException{ + Connection connection = ds.getConnection(); + logger.debug("获取连接成功"); + boolean closed = connection.isClosed(); + if( closed){ + logger.warn("connection.toString()->" + connection.toString()); + } + logger.debug("closed("+closed+"),autoCommit="+connection.getAutoCommit()+", "+ ds.toString()); + connection.setAutoCommit(true); + return connection; + } + + + public static void shutdown(){ + if(ds != null){ + ds.close(); + } + } + + public static void main(String[] args) { + try { + initPool(); + Connection connection = getConnection(); + System.out.println(connection); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + +} diff --git a/src/com/nms/server/util/ExceptionPrintUtils.java b/src/com/nms/server/util/ExceptionPrintUtils.java new file mode 100644 index 0000000..f5e0eea --- /dev/null +++ b/src/com/nms/server/util/ExceptionPrintUtils.java @@ -0,0 +1,13 @@ +package com.nms.server.util; + +public class ExceptionPrintUtils { + public static String printExceptionStack(Exception e){ + StackTraceElement[] ste = e.getStackTrace(); + StringBuffer sb = new StringBuffer(); + sb.append("\n\t" + e.toString() + "\n"); + for(StackTraceElement element : ste){ + sb.append("\t" + element.toString() + "\n"); + } + return sb.toString(); + } +} diff --git a/src/com/nms/server/util/FileUtils.java b/src/com/nms/server/util/FileUtils.java new file mode 100644 index 0000000..a630880 --- /dev/null +++ b/src/com/nms/server/util/FileUtils.java @@ -0,0 +1,448 @@ +package com.nms.server.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Comparator; +import java.util.List; + +/** + * 继承org.apache.commons.io.FileUtils类,继承其对文件的各项操作 + * 并增加文件检索方法,丰富己用 + * 主要新增操作:检索、排序 + * @date Sep 6, 2011 2:28:14 PM + * @author ZhangGang + * + */ +public class FileUtils extends org.apache.commons.io.FileUtils{ + + /** + * 将字节写入新文件 + * @time Jan 13, 2013-2:46:11 PM + * @param bts 存放文件字节数组 + * @param filePath 写入的目标文件路径 + * @throws IOException + */ + public static void wirteBytesToFile(byte [] bts,String filePath) throws IOException{ + File file = new File(filePath); + wirteBytesToFile(bts, file); + } + + /** + * 将字节写入新文件 + * @time Jan 13, 2013-2:46:11 PM + * @param bts 存放文件字节数组 + * @param file 写入的目标文件 + * @throws IOException + */ + public static void wirteBytesToFile(byte [] bts,File file) throws IOException{ + FileOutputStream fos = null; + try { + /* 检查并创建文件路径 */ + if(!file.getParentFile().exists()){ + file.getParentFile().mkdirs(); + } + + /* 删除旧文件 */ + if(file.exists()){ + file.delete(); + } + + /* 创建新文件 */ + file.createNewFile(); + + /* 将字节写入文件*/ + if(bts != null && bts.length>0){ + + /* 创建文件输出流写文件 */ + fos = new FileOutputStream(file); + fos.write(bts); + } + + } catch (IOException e) { + throw e; + }finally{ + if(fos!= null){fos.close();} + } + } + + + + /** + * 文件过滤 + * @time Sep 6, 2011-5:01:51 PM + * @param file + * @param suffix + * @param spaces + */ + public static void folderIterator(File file,String suffix,String spaces) { + if(file.exists()){ //文件存在才有意义检索 + /*检索符合规则的文件 将文件添加到 待解析文件序列*/ + File [] files = listFilesEndWith(file,suffix); + files = sortByFileModifyASC(files); //修改日期升序排序 + if(files != null ){ + for(File file0 : files){ + System.out.println(spaces+"00 "+file0.getName()+" "+file0.lastModified()); + } + } + /*检索 检索目录下文件目录 进行下一级检索*/ + File [] files2 = listFilesForDirectory(file); + if(files2 != null ){ + for(File file0 : files2){ + System.out.println(spaces+"== "+file0.getName()+" "+file0.lastModified()); + folderIterator(file0,suffix,spaces+"\t"); + } + } + } + } + + /**获取指定全部或部分名称的文件 + * @param file + * @param azz + * @return + */ + public static File [] listFilesWith(File file,final String azz){ + if(!file.isDirectory()){ + new Exception(file.getAbsolutePath() + " Not A Directory"); + } + return file.listFiles(new FilenameFilter() { + // 使用匿名内部类重写accept方法; + public boolean accept(File dir, String name) { + if (new File(dir, name).isDirectory()) { + return false; + } + return name.indexOf(azz) != -1 ; // 过滤出指定全部名称或部分名称的文件 + } + }); + } + + /**获取指定后缀名的文件 + * @param file + * @param suffix + * @return + */ + public static File [] listFilesEndWith(File file,final String suffix){ + if(!file.isDirectory()){ + new Exception(file.getAbsolutePath() + " Not A Directory"); + } + return file.listFiles(new FilenameFilter() { + // 使用匿名内部类重写accept方法; + public boolean accept(File dir, String name) { + if (new File(dir, name).isDirectory()) { + return false; + } + return name.endsWith(suffix); // 过滤出注定后缀名称的文件 + } + }); + } + + /**获取指定目录里,所有符合后指定后缀名的文件 + * @param file + * @param suffix + * @return + */ + + //根据给定的目录产生所有的文件对象数组 + public static File[] listAllFilesEndWith(File file,String suffix) { + File[] files = new File[0]; + List fileGroup = new ArrayList(); + try { + generateAllFilesEndWith(file,suffix,fileGroup); + }catch(Exception e) { + e.printStackTrace(); + } + + return fileGroup.toArray(files); + } + + public static void generateAllFilesEndWith(File file,String suffix,List fileGroup) { + try { + if(file.exists()&&file.isDirectory()) { + File[] fileList = file.listFiles(); + for(File f:fileList) { + if(f.exists()) {//文件夹作为节点 + if(f.isDirectory()) { + generateAllFilesEndWith(f,suffix,fileGroup); + }else if(isRightSuffix(f,suffix)) { + fileGroup.add(f); + } + } + } + }else { + fileGroup.add(file); + } + }catch(Exception e) { + e.printStackTrace(); + } + + } + + //文件后缀名是否符合指定字符串 + public static boolean isRightSuffix(File file,String suffix) { + boolean canRead = false; + String fileName = file.getName(); + String extensionName = fileName.substring(fileName.lastIndexOf(".")+1); + if(file.isFile()&&suffix.equals(extensionName)) { + canRead = true; + } + return canRead; + } + + public static Calendar getModifiedTime(File f){ + Calendar cal = Calendar.getInstance(); + long time = f.lastModified(); +// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + cal.setTimeInMillis(time); +// Date date = cal.getTime(); +// System.out.println("修改时间[2] " + formatter.format(cal.getTime())); //输出:修改时间[2] 2009-08-17 10:32:38 + + return cal; + } + + //删除文件夹里的所有文件 + public static void deleteAllFiles(String path,boolean delSelf) { + File tmpFile = new File(path); + deleteAllFiles(tmpFile,delSelf); + } + + //删除文件夹里的所有文件 + public static void deleteAllFiles(File tmpFile,boolean delSelf) { + if(tmpFile!=null&&tmpFile.isDirectory()) { + File[] filesGroup = tmpFile.listFiles(); + deleteFiles(filesGroup); + if(delSelf) {//是否删除自身 + tmpFile.delete(); + } + }else if(tmpFile!=null&&tmpFile.isFile()&&delSelf) { + tmpFile.delete(); + } + } + + public static void deleteFiles(File[] fileGroup) { + for(File file:fileGroup) { + if(file!=null && file.isFile()) { + file.delete(); + }else if(file!=null&&file.isDirectory()){ + File[] files = file.listFiles(); + deleteFiles(files); + file.delete(); + } + } + } + + public static void main(String[] args) { + deleteAllFiles("D:\\ControllerData\\dc_error\\qq",true); + } + + + + /**获取指定后缀名的文件 + * @param file + * @param suffix + * @return + */ + public static File [] listFilesEndWithout(File file,final String suffix){ + if(!file.isDirectory()){ + new Exception(file.getAbsolutePath() + " Not A Directory"); + } + return file.listFiles(new FilenameFilter() { + // 使用匿名内部类重写accept方法; + public boolean accept(File dir, String name) { + if (new File(dir, name).isDirectory()) { + return false; + } + return !name.endsWith(suffix); // 过滤出注定后缀名称的文件 + } + }); + } + /**获取指定后缀名的文件 + * @param file + * @param suffix + * @return + */ + public static String[] listFileNamesEndWith(File file,final String suffix){ + if(!file.isDirectory()){ + new Exception(file.getAbsolutePath() + " Not A Directory"); + } + return file.list(new FilenameFilter() { + // 使用匿名内部类重写accept方法; + public boolean accept(File dir, String name) { + if (new File(dir, name).isDirectory()) { + return false; + } + return name.endsWith(suffix); // 过滤出注定后缀名称的文件 + } + }); + } + + /**获取指定后缀名的文件 + * @param file + * @param suffix + * @return + */ + public static String[] listFileNamesEndWithout(File file,final String suffix){ + if(!file.isDirectory()){ + new Exception(file.getAbsolutePath() + " Not A Directory"); + } + return file.list(new FilenameFilter() { + // 使用匿名内部类重写accept方法; + public boolean accept(File dir, String name) { + if (new File(dir, name).isDirectory()) { + return false; + } + return !name.endsWith(suffix); // 过滤出注定后缀名称的文件 + } + }); + } + /**获取子目录列表 + * @param file + * @return + */ + public static File [] listFilesForDirectory(File file){ + if(!file.isDirectory()){ + new Exception(file.getAbsolutePath() + " Not A Directory"); + } + return file.listFiles(new FilenameFilter() { + // 使用匿名内部类重写accept方法; + public boolean accept(File dir, String name) { + if (new File(dir, name).isDirectory()) { + return true; + }else return false; + } + }); + } + /** + * 文件数组按最后修改日期排序,升序 + * @time Aug 5, 2011-1:32:36 PM + * @param files + * @return + */ + public static File [] sortByFileModifyASC(File[] files) { + Arrays.sort(files, new Comparator(){ + public int compare(File arg0, File arg1) { + if(arg0.lastModified() > arg1.lastModified()){ + return -1; + }else if (arg0.lastModified() < arg1.lastModified()){ + return 1; + }else { + return 0; + } + } + }); + return files; + } + + /** + * 文件数组按名称排序,升序 + * @time Aug 5, 2011-1:32:36 PM + * @param files + * @return + */ + public static File [] sortByFileNameASC(File[] files) { + Arrays.sort(files, new Comparator(){ + public int compare(File arg0, File arg1) { + return arg0.getName().compareTo(arg1.getName()); + } + }); + return files; + } + + /** + * + * 保存对象到文件 + * @author Administrator Feb 22, 2013 + * @version 1.0 + * @param path + */ + public static void writeObjToFile(Object obj,File file){ + FileOutputStream fos = null; + ObjectOutputStream oos = null; + try { + + /* 检查并创建文件路径 */ + if(file!=null&&!file.getParentFile().exists()){ + file.getParentFile().mkdirs(); + } + +// /* 删除旧文件 */ +// if(file.exists()){ +// file.delete(); +// }else { +// /* 创建新文件 */ +// file.createNewFile(); +// +// } + + if(obj != null){ + + fos = new FileOutputStream(file);//默认是覆盖,true是追加 + oos = new ObjectOutputStream(fos); + oos.writeObject(obj); //括号内参数为要保存java对象 + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }finally{ + try { + if(oos!=null) { + oos.close(); + } + if(fos!=null) { + fos.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * + * 从文件读取对象 + * @author Administrator Feb 22, 2013 + * @version 1.0 + * @param path + */ + public static Object readObjFomFile(File file){ + FileInputStream fis = null; + ObjectInputStream ois = null; + Object obj = null; + try { + if(file != null && file.exists() && file.length()>0 ){ + fis = new FileInputStream(file); + ois = new ObjectInputStream(fis); + obj = ois.readObject();//强制类型转换 + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + }finally{ + try { + if(ois!=null) { + ois.close(); + } + if(fis!=null) { + fis.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return obj; + } + +} diff --git a/src/com/nms/server/util/FileWrUtil.java b/src/com/nms/server/util/FileWrUtil.java new file mode 100644 index 0000000..f13959c --- /dev/null +++ b/src/com/nms/server/util/FileWrUtil.java @@ -0,0 +1,284 @@ +package com.nms.server.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.Ostermiller.util.ExcelCSVParser; +import com.Ostermiller.util.ExcelCSVPrinter; + +public class FileWrUtil { + static Logger logger = Logger.getLogger(FileWrUtil.class); + + /** + * 根据文件编码添加文件内容前缀 + * @param file + * @param encoding + */ + protected static boolean addFileHeadByEncoding(File file, String encoding) throws Exception{ + FileOutputStream fos = null; + boolean flag = false; + try{ + byte[] head = null; + if("UTF-8".equalsIgnoreCase(encoding)){ + head = new byte[]{(byte)0xEF,(byte)0xBB,(byte)0xBF}; + }else if("UTF-16BE".equalsIgnoreCase(encoding)){ + head = new byte[]{(byte)0xFE,(byte)0xFF}; + }else if("UTF-16LE".equalsIgnoreCase(encoding)){ + head = new byte[]{(byte)0xFF,(byte)0xFE}; + }else if("UTF-32BE".equalsIgnoreCase(encoding)){ + head = new byte[]{(byte)0x00,(byte)0x00,(byte)0xFE,(byte)0xFF}; + }else if("UTF-32LE".equalsIgnoreCase(encoding)){ + head = new byte[]{(byte)0xFF,(byte)0xFF,(byte)0x00,(byte)0x00}; + } + + if(head!=null){ + fos = new FileOutputStream(file); + fos.write(head); + fos.flush(); + + flag = true; + } + } catch (IOException e) { + throw e; + }finally { + if(fos!=null){ + try { + fos.close(); + } catch (IOException e) { + } + } + } + + return flag; + } + + /** + * 写入文件, .cfg\.ini\.txt + * + */ + public static boolean cfgFilePrinter(File file, String charset, String[] values) throws Exception{ + OutputStreamWriter fos = null; + try { + if(addFileHeadByEncoding(file, charset)){ + fos = new OutputStreamWriter( + new FileOutputStream(file,true), charset); + }else{ + fos = new OutputStreamWriter( + new FileOutputStream(file), charset); + } + for(String val : values){ + fos.write(val); + fos.write("\n"); + } + fos.flush(); + } catch (IOException e) { + throw e; + }finally { + if(fos!=null){ + try { + fos.close(); + } catch (IOException e) { + } + } + } + + return true; + } + + /** + * 追加内容到文件, .cfg\.ini\.txt + * + */ + public static boolean cfgFileAppender(File file, String charset, String[] values) throws Exception{ + OutputStreamWriter fos = null; + try { + fos = new OutputStreamWriter( + new FileOutputStream(file, true), charset); + for(String val : values){ + fos.write(val); + fos.write("\n"); + } + fos.flush(); + } catch (IOException e) { + throw e; + }finally { + if(fos!=null){ + try { + fos.close(); + } catch (IOException e) { + } + } + } + + return true; + } + + /** + * 根据文件的编码读取文件内容, .cfg\.ini\.txt + * + */ + public static String[] cfgFileReader(File file) throws Exception{ + BufferedReader br = null; + ArrayList list = new ArrayList(); + try { + + br = new BufferedReader(new UnicodeReader(new FileInputStream( + file), Charset.defaultCharset().name())); + String str = null; + while((str=br.readLine())!=null){ + list.add(str); + } + } catch (IOException e) { + throw e; + }finally { + if(br!=null){ + try { + br.close(); + } catch (IOException e) { + } + } + } + + String[] values = new String[list.size()]; + return list.toArray(values); + } + + /** + * 写CSV文件,只写入一行 + * + */ + public static boolean csvFilePrinter(File file, String charset, String[] values) throws Exception{ + OutputStreamWriter fos = null; + try { + fos = new OutputStreamWriter( + new FileOutputStream(file), charset); + + + ExcelCSVPrinter ecsvp = new ExcelCSVPrinter(fos); + ecsvp.changeDelimiter(','); + ecsvp.setAutoFlush(true); + ecsvp.writeln(values); + + fos.flush(); + } catch (IOException e) { + throw e; + }finally { + if(fos!=null){ + try { + fos.close(); + } catch (IOException e) { + } + } + } + + return true; + } + + /** + * 写CSV文件,写入多行 + * + */ + public static boolean csvFilePrinter(File file, String charset, ArrayList values) throws Exception{ + OutputStreamWriter fos = null; + try { + fos = new OutputStreamWriter( + new FileOutputStream(file), charset); + + + ExcelCSVPrinter ecsvp = new ExcelCSVPrinter(fos); + ecsvp.changeDelimiter(','); + ecsvp.setAutoFlush(true); + for(String[] val : values){ + ecsvp.writeln(val); + } + + fos.flush(); + } catch (IOException e) { + throw e; + }finally { + if(fos!=null){ + try { + fos.close(); + } catch (IOException e) { + } + } + } + + return true; + } + + /** + * 解析CSV文件中的指定行 + * + */ + public static String[] csvFileParser(File file, String charset, int rowNum) throws Exception{ + Reader in = null; + String[] currLine = null; + try { + //创建解析信息流 + in = new InputStreamReader(new FileInputStream(file),charset); + + ExcelCSVParser csvParser = new ExcelCSVParser(in); + while((currLine=csvParser.getLine())!=null){ + if(rowNum != csvParser.getLastLineNumber()){ + currLine = null; + } + } + + } catch (IOException e) { + throw e; + }finally { + if(in!=null){ + try { + in.close(); + } catch (IOException e) { + } + } + } + + return currLine; + } + + /** + * 解析CSV文件的所有行 + * + */ + public static List csvFileParser(File file, String charset) throws Exception{ + Reader in = null; + List allLineList = new ArrayList(); + try { + //创建解析信息流 + in = new InputStreamReader(new FileInputStream(file),charset); + + ExcelCSVParser csvParser = new ExcelCSVParser(in); + String[] currLine = null; + while((currLine=csvParser.getLine())!=null){ + allLineList.add(currLine); + } + + } catch (IOException e) { + throw e; + }finally { + if(in!=null){ + try { + in.close(); + } catch (IOException e) { + } + } + } + + return allLineList; + } + +} diff --git a/src/com/nms/server/util/IpCovert.java b/src/com/nms/server/util/IpCovert.java new file mode 100644 index 0000000..588766e --- /dev/null +++ b/src/com/nms/server/util/IpCovert.java @@ -0,0 +1,95 @@ +package com.nms.server.util; + +import java.util.regex.Pattern; + +public class IpCovert { + + public static long ipToLong(String strIP) + //将127.0.0.1 形式的IP地址转换成10进制整数,这里没有进行任何错误处理 + { +// int j=0; +// int i=0; + long [] ip=new long[4]; + int position1=strIP.indexOf("."); + int position2=strIP.indexOf(".",position1+1); + int position3=strIP.indexOf(".",position2+1); + ip[0]=Long.parseLong(strIP.substring(0,position1)); + ip[1]=Long.parseLong(strIP.substring(position1+1,position2)); + ip[2]=Long.parseLong(strIP.substring(position2+1,position3)); + ip[3]=Long.parseLong(strIP.substring(position3+1)); + return (ip[0]<<24)+(ip[1]<<16)+(ip[2]<<8)+ip[3]; + } + public static String longToIP(long longIP) + //将10进制整数形式转换成127.0.0.1形式的IP地址,按主机序 + { + StringBuffer sb=new StringBuffer(""); + sb.append(String.valueOf(longIP>>>24&0xFF));//直接右移24位 + sb.append("."); //将高8位置0,然后右移16位 + sb.append(String.valueOf((longIP&0x00FFFFFF)>>>16)); + sb.append("."); + sb.append(String.valueOf((longIP&0x0000FFFF)>>>8)); + sb.append("."); + sb.append(String.valueOf(longIP&0x000000FF)); + //sb.append("."); + return sb.toString(); + } + //将10进制整数形式转换成127.0.0.1形式的IP地址,按网络序 + public static String longToNetIp(long longIP){ + StringBuffer sb=new StringBuffer(""); + sb.append(String.valueOf(longIP&0x000000FF)); + sb.append("."); + sb.append(String.valueOf((longIP&0x0000FFFF)>>>8)); + sb.append(".");//将高8位置0,然后右移16位 + sb.append(String.valueOf((longIP&0x00FFFFFF)>>>16)); + sb.append("."); + sb.append(String.valueOf(longIP>>>24&0xFF));//直接右移24位 + //sb.append("."); + return sb.toString(); + } + public static long netIpToLong(String strIP) + //将127.0.0.1 形式的IP地址转换成10进制整数,这里没有进行任何错误处理 + { +// int j=0; +// int i=0; + long [] ip=new long[4]; + int position1=strIP.indexOf("."); + int position2=strIP.indexOf(".",position1+1); + int position3=strIP.indexOf(".",position2+1); + ip[0]=Long.parseLong(strIP.substring(0,position1)); + ip[1]=Long.parseLong(strIP.substring(position1+1,position2)); + ip[2]=Long.parseLong(strIP.substring(position2+1,position3)); + ip[3]=Long.parseLong(strIP.substring(position3+1)); + return (ip[0])+(ip[1]<<8)+(ip[2]<<16)+(ip[3]<<24); + } +// public static String longToPort(long longPort){ +// StringBuffer sb=new StringBuffer(""); +// sb.append(String.valueOf(longPort&0x000000FF)); +// +// //sb.append("."); +// sb.append(String.valueOf((longPort&0x0000FF00)>>>8)); +// return sb.toString(); +// } + + //测试IP地址是否合法 + public boolean isIp(String ipAddress){ +// String test = "([1-9]|[1-9]\\d|1\\d{2}|2[0-1]\\d|22[0-3])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}"; +// Pattern pattern = Pattern.compile(test); +// Matcher matcher = pattern.matcher(ipAddress); +// return matcher.matches(); + String regex0 = "(2[0-4]\\d)" + "|(25[0-5])"; + String regex1 = "1\\d{2}"; + String regex2 = "[1-9]\\d"; + String regex3 = "\\d"; + String regex = "(" + regex0 + ")|(" + regex1 + ")|(" + regex2 + ")|(" + regex3 + ")"; + regex = "(" + regex + ").(" + regex + ").(" + regex + ").(" + regex + ")"; + String[] str = ipAddress.split("\\.");//根据@拆分IP地址 + if (!Pattern.matches(regex, ipAddress)) + return false; + else if(str!=null && str.length!=4){//如果IP地址拆分后的数组长度不为4则不是正确的IP地址 + return false; + }else{ + return true; + } + } + +} diff --git a/src/com/nms/server/util/LocalAddress.java b/src/com/nms/server/util/LocalAddress.java new file mode 100644 index 0000000..5c2c2ec --- /dev/null +++ b/src/com/nms/server/util/LocalAddress.java @@ -0,0 +1,58 @@ +package com.nms.server.util; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Enumeration; + +public class LocalAddress { + + public static String getRealIp() throws SocketException{ + String localip = null;// 本地IP,如果没有配置外网IP则返回它 + String netip = null;// 外网IP + Enumeration netInterfaces = NetworkInterface + .getNetworkInterfaces(); + InetAddress ip = null; + boolean finded = false;// 是否找到外网IP + while (netInterfaces.hasMoreElements() && !finded) { + NetworkInterface ni = netInterfaces.nextElement(); + Enumeration address = ni.getInetAddresses(); + while (address.hasMoreElements()) { + ip = address.nextElement(); + try { + localip = ip.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// System.out.println(ip); +// if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() +// && ip.getHostAddress().indexOf(":") == -1) { +// netip = ip.getHostAddress(); +// finded = true; +//// break; +// } else if (ip.isSiteLocalAddress() +// && !ip.isLoopbackAddress() +// && ip.getHostAddress().indexOf(":") == -1) { +// localip = ip.getHostAddress(); +// } + break; + } + } + System.out.println("IP们 "+netip+" <---> "+localip); + if (netip != null && !"".equals(netip)) { + return netip; + } else { + return localip; + } + } + + public static void main(String [] args) { + try { + LocalAddress.getRealIp(); + } catch (SocketException e) { + e.printStackTrace(); + } + } +} diff --git a/src/com/nms/server/util/MD5Util.java b/src/com/nms/server/util/MD5Util.java new file mode 100644 index 0000000..24c2024 --- /dev/null +++ b/src/com/nms/server/util/MD5Util.java @@ -0,0 +1,88 @@ +package com.nms.server.util; + + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class MD5Util { +/** +* 默认的密码字符串组合,apache校验下载的文件的正确性用的就是默认的这个组合 +*/ +protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f' }; +protected static MessageDigest messagedigest = null; +static{ + try{ + messagedigest = MessageDigest.getInstance("MD5"); + }catch(NoSuchAlgorithmException nsaex){ + System.err.println(MD5Util.class.getName()+"初始化失败,MessageDigest不支持MD5Util。"); + nsaex.printStackTrace(); + } +} + +public static void main(String[] args) throws IOException { + long begin = System.currentTimeMillis(); + + //2EA3E66AC37DF7610F5BD322EC4FFE48 670M 11s kuri双核1.66G 2G内存 + File big = new File("I:/大型安装程序的压缩版本/Rational rose 2003.rar"); + + String md5=getFileMD5String(big); + + long end = System.currentTimeMillis(); + System.out.println("md5:"+md5+" time:"+((end-begin)/1000)+"s"); +} + +/** +* 适用于上G大的文件 +* @param file +* @return +* @throws IOException +*/ +public static String getFileMD5String(File file) throws IOException { + FileInputStream in = new FileInputStream(file); + FileChannel ch = in.getChannel(); + MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); + messagedigest.update(byteBuffer); + return bufferToHex(messagedigest.digest()); +} + +public static String getMD5String(String s) { + return getMD5String(s.getBytes()); +} + +public static String getMD5String(byte[] bytes) { + messagedigest.update(bytes); + return bufferToHex(messagedigest.digest()); +} + +private static String bufferToHex(byte bytes[]) { + return bufferToHex(bytes, 0, bytes.length); +} + +private static String bufferToHex(byte bytes[], int m, int n) { + StringBuffer stringbuffer = new StringBuffer(2 * n); + int k = m + n; + for (int l = m; l < k; l++) { + appendHexPair(bytes[l], stringbuffer); + } + return stringbuffer.toString(); +} + + +private static void appendHexPair(byte bt, StringBuffer stringbuffer) { + char c0 = hexDigits[(bt & 0xf0) >> 4]; + char c1 = hexDigits[bt & 0xf]; + stringbuffer.append(c0); + stringbuffer.append(c1); +} + +public static boolean checkPassword(String password, String md5PwdStr) { + String s = getMD5String(password); + return s.equals(md5PwdStr); +} +} + diff --git a/src/com/nms/server/util/MathUtil.java b/src/com/nms/server/util/MathUtil.java new file mode 100644 index 0000000..e6b4af7 --- /dev/null +++ b/src/com/nms/server/util/MathUtil.java @@ -0,0 +1,10 @@ +package com.nms.server.util; + + +public class MathUtil { + + public static long getUnsignedIntt (int data){ + //将int数据转换为0~4294967295 (0xFFFFFFFF即DWORD)。 + return data&0x0FFFFFFFFl; + } +} diff --git a/src/com/nms/server/util/SNMP4JUtils.java b/src/com/nms/server/util/SNMP4JUtils.java new file mode 100644 index 0000000..114ea2f --- /dev/null +++ b/src/com/nms/server/util/SNMP4JUtils.java @@ -0,0 +1,541 @@ +package com.nms.server.util; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.Vector; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import org.snmp4j.CommunityTarget; +import org.snmp4j.DefaultTimeoutModel; +import org.snmp4j.PDU; +import org.snmp4j.ScopedPDU; +import org.snmp4j.Snmp; +import org.snmp4j.Target; +import org.snmp4j.TransportMapping; +import org.snmp4j.UserTarget; +import org.snmp4j.event.ResponseEvent; +import org.snmp4j.mp.MPv1; +import org.snmp4j.mp.MPv2c; +import org.snmp4j.mp.MPv3; +import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.security.AuthMD5; +import org.snmp4j.security.AuthSHA; +import org.snmp4j.security.PrivAES128; +import org.snmp4j.security.PrivAES192; +import org.snmp4j.security.PrivAES256; +import org.snmp4j.security.PrivDES; +import org.snmp4j.security.SecurityLevel; +import org.snmp4j.security.SecurityModels; +import org.snmp4j.security.SecurityProtocols; +import org.snmp4j.security.USM; +import org.snmp4j.security.UsmUser; +import org.snmp4j.smi.OID; +import org.snmp4j.smi.OctetString; +import org.snmp4j.smi.UdpAddress; +import org.snmp4j.smi.VariableBinding; +import org.snmp4j.transport.DefaultUdpTransportMapping; +import org.snmp4j.util.DefaultPDUFactory; +import org.snmp4j.util.TreeEvent; +import org.snmp4j.util.TreeUtils; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; + +/** + * 最近项目上用到了SNMP操作,在网上找了找有关java操作SNMP 的例子发现原创的非常少,要么就是不完整。 + * 看了一下SNMP4J的文档自己写了一个工具类, + * @author Administrator + * + */ +public class SNMP4JUtils { + private static Logger logger = Logger.getLogger(SNMP4JUtils.class); + public static final String ROOT="WALK"; + private static final Integer C_P_RETRIES=0; + private static final Integer C_P_TIME_OUT=7000; + private static final String C_P_COMMUNITY="public"; + private static final int C_P_VERSION = SnmpConstants.version2c; + private static final Snmp snmp = createSnmp(); + + + + /** + * + * @param datas + * @param oids + * @param snmp + * @param target + * @throws Exception + */ + public static void snmpGet(Map datas,String[] oids,Target target) throws Exception{ + if(target.getVersion() == SnmpConstants.version3){ + snmpGetV3(datas,oids,target,Constants.SNMP_V3_SECURITY_NAME); + }else{ + snmpGetV2(datas,oids,snmp,target); + } + } + /** + * + * @param datas + * @param oids + * @param snmp + * @param target + * @throws Exception + */ + public static void snmpGetV2(Map datas,String[] oids,Snmp snmp,Target target) throws Exception{ + + PDU request = createPDU(PDU.GET); + for(String oid :oids ){ + if((oid.toString()).endsWith(".0")) + request.add(new VariableBinding(new OID(oid))); + else{ +// throw new Exception(oid+" 为非基本节点"); + throw new Exception(oid+" is not basic node"); + } + } + ResponseEvent respEvent = snmp.send(request, target); + PDU response = respEvent.getResponse(); +// if(isNullPDU(response,"SNMP4J 响应结果为空")){ + if(isNullPDU(response,"The result of the SNMP4J response is empty")){ + return; + } + if(response.getErrorIndex()==PDU.noError&&response.getErrorStatus()==PDU.noError){ + Vector vector = (Vector) response.getVariableBindings(); + for(VariableBinding vb : vector){ + String key = vb.getOid().toString(); + datas.put(key,vb.getVariable().toString()); + //pl(key+" 88 "+vb.getVariable().toString()); + } + }else{ +// throw new Exception("错误信息:"+response.getErrorStatusText()); + throw new Exception("Error information:"+response.getErrorStatusText()); + } + } + + public static void snmpGetV3(Map datas,String[] oids,Target target,String contextName) throws Exception{ + + ScopedPDU request = new ScopedPDU(); + for(String oid :oids){ + if((oid.toString()).endsWith(".0")) + request.add(new VariableBinding(new OID(oid))); + else{ + throw new Exception(oid+" is not basic node"); + } + } +// request.setContextName(new OctetString(contextName)); + request.setType(PDU.GET); + + ResponseEvent respEvent = snmp.send(request, target); + System.out.println(snmp.getUSM().getUserTable().getUser(new OctetString(contextName))); + System.out.println(respEvent.getResponse()); + PDU response = respEvent.getResponse(); +// if(isNullPDU(response,"SNMP4J 响应结果为空")){ + if(isNullPDU(response,"The result of the SNMP4J response is empty")){ + return; + } + + if(response.getErrorIndex()==PDU.noError&&response.getErrorStatus()==PDU.noError){ + Vector vector = (Vector) response.getVariableBindings(); + for(VariableBinding vb : vector){ + String key = vb.getOid().toString(); + datas.put(key,vb.getVariable().toString()); + } + }else{ +// throw new Exception("错误信息:"+response.getErrorStatusText()); + throw new Exception("Error information:"+response.getErrorStatusText()); + } + } + + + private static Map resoveVersion2c(PDU response) throws Exception{ + Map datas = new HashMap(); + if (response.getErrorIndex()==PDU.noError&&response.getErrorStatus()==PDU.noError) { + Object obj = response.getVariableBindings().firstElement(); + VariableBinding variable = (VariableBinding) obj; + String key = variable.getOid().toString(); + System.out.println("key:"+key+":"+variable.getVariable().toString()); + datas.put(key,variable.getVariable().toString()); + } else { +// throw new Exception("错误信息:"+response.getErrorStatusText()); + throw new Exception("Error information:"+response.getErrorStatusText()); + } + return datas; + } + + /** + * 将十进六制转换成为中文 + */ + private static String hexString = "0123456789ABCDEF"; + + public static String toStringHex(String bytes) { + + ByteArrayOutputStream baos = new ByteArrayOutputStream( + bytes.length() / 2); + //将每2位16进制整数组装成一个字节 + for (int i = 0; i < bytes.length(); i += 2) { + baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString + .indexOf(bytes.charAt(i + 1)))); + } + return new String(baos.toByteArray()); + } + + private static Map resoveVersion3(PDU response) throws Exception{ + + Map datas = new HashMap(); + for (int i = 0; i < response.size(); i++) { + VariableBinding vb = response.get(i); + String[] str = vb.getVariable().toString().toUpperCase().split(":"); + String strOut = ""; + int intLength = str.length; + logger.debug("长度==> " + intLength); + for (int j = 0; j < intLength; j++) { + strOut += str[j]; + } +// System.out.println("snmpWalkKey:"+vb.getOid()+" : "+vb.getVariable().toString()+" --中文: "); + datas.put(vb.getOid().toString(), vb.getVariable().toString()); + if (str.length != 1) { + logger.debug("==第284行=vb.getVariable()=" + toStringHex(strOut));//显示中文 + } + } + return datas; + } + + public static CommunityTarget createCommunityTarget(String host,String port,String community,int retries,int timeout,int version){ + CommunityTarget target = new CommunityTarget(); + target.setAddress(new UdpAddress(host+"/"+port));//设置Snmp Agent的访问地址和端口 + target.setCommunity(new OctetString(StringUtils.isEmpty(community)?C_P_COMMUNITY:community));//设置共同体,默认是"public" + target.setRetries(retries==0?C_P_RETRIES:retries);//设置重试次数 + target.setTimeout(timeout==0?C_P_TIME_OUT:timeout);//设置超时毫秒数 + target.setVersion(version==0?C_P_VERSION : version);//设置snmp的版本号 + return target; + } + + public static UserTarget createUserTarget(String host,String port,int retries,int timeout){ + UserTarget target = new UserTarget(); + target.setAddress(new UdpAddress(host+"/"+port));//设置Snmp Agent的访问地址和端口 + target.setRetries(1);//设置重试次数 + target.setRetries(retries==0?C_P_RETRIES : retries);//设置重试次数 + target.setTimeout(timeout==0?C_P_TIME_OUT : timeout);//设置超时毫秒数 + target.setVersion(SnmpConstants.version3);//设置snmp的版本号 + switch (Constants.SNMP_V3_SECURITY_LEVEL) { + case 0: + target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV); + break; + case 1: + target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV); + break; + case 2: + target.setSecurityLevel(SecurityLevel.AUTH_PRIV); + break; + default: + target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV); + break; + } + target.setSecurityName(new OctetString(Constants.SNMP_V3_SECURITY_NAME)); + return target; + } + + public static Snmp createSnmp() { + + try { + if(snmp == null){ + String ip = Common.getLocalIp(); + if(StringUtils.isEmpty(ip)){ + return null; + } + + TransportMapping transport = new DefaultUdpTransportMapping(); + Snmp snmp = new Snmp(transport); + snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1()); + snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c()); + snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3()); + snmp.setTimeoutModel(new DefaultTimeoutModel()); + USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); + SecurityModels.getInstance().addSecurityModel(usm); + snmp.listen(); + + OID authProtocol = getAuthProtocol(Constants.SNMP_V3_AUTH_PROTOCOL); //认证协议 + OID privProtocol = getPrivProtocol(Constants.SNMP_V3_PRIV_PROTOCOL); //加密协议 + OctetString securityName = new OctetString(Constants.SNMP_V3_SECURITY_NAME); //认证用户 + OctetString authPassphrase = new OctetString(Constants.SNMP_V3_AUTH_PASSPHRASE); //认证密码明文 + OctetString privPassphrase = new OctetString(Constants.SNMP_V3_PRIV_PASSPHRASE); //加密密码明文 + snmp.getUSM().addUser(securityName, new UsmUser(securityName, authProtocol, authPassphrase, + privProtocol, privPassphrase)); + logger.info("SNMP 启动成功"); + return snmp; + }else{ + return snmp; + } + } catch (IOException e) { + logger.error("SNMP4J ERROR", e); + } catch (Exception e) { + logger.error("SNMP4J ERROR", e); + } + return snmp; + } + + private static OID getAuthProtocol(String auth){ + if (auth.equals("MD5")) { + return AuthMD5.ID; + } else if (auth.equals("SHA")) { + return AuthSHA.ID; + }else{ + return null; + } + } + + private static OID getPrivProtocol(String priv){ + if (priv.equals("DES")) { + return PrivDES.ID; + } else if ((priv.equals("AES128")) + || (priv.equals("AES"))) { + return PrivAES128.ID; + } else if (priv.equals("AES192")) { + return PrivAES192.ID; + } else if (priv.equals("AES256")) { + return PrivAES256.ID; + }else{ + return null; + } + } + + private static PDU createPDU(int type){ + PDU pdu= new PDU(); + pdu.setType(type); + return pdu; + } + + /** + * PDU为空判断 + * @param pdu 需判断的PDU + * @param messages PDU为空是输出的提示信息 + * @return + * @throws Exception + */ + public static boolean isNullPDU(PDU pdu, String messages) throws Exception{ + boolean isNull = true; + if(pdu!=null){ + isNull = false; + }else{ + isNull = true; + //pdu为空的处理可以添加到这里 +// logger.debug(messages); +// throw new Exception("错误信息:"+messages); + throw new Exception("Error information:"+messages); + } + return isNull; + } + public static String checkTypeName = null; + public static void snmpTree(List> datasList,List oidModel, String oid,String host,String port,String community,int retries,int timeout,int version,String checktypename) throws Exception{ + checkTypeName = checktypename; + snmpTree(datasList,oidModel, oid, host, port, community, retries, timeout, version); + } + public static void snmpTree(List> datasList,List oidModel, String oid,String host,String port,String community,int retries,int timeout,int version) throws Exception{ + Target target = null; + if (version == SnmpConstants.version3) { + target = createUserTarget(host,port,retries,timeout); + }else{ + target = createCommunityTarget(host,port,community,retries,timeout, version); + } + treeUtilNew(datasList,oidModel,snmp,target,oid); + } + + /** + * + * TODO + * @author hyx Mar 20, 2013 + * @version 1.0 + */ + public static void snmpIfXTabTree(Map> datasMap,List oidModel, String oid,String host,String port,String community,int retries,int timeout,int version) throws Exception{ + Target target = null; + if (version == SnmpConstants.version3) { + target = createUserTarget(host,port,retries,timeout); + }else{ + target = createCommunityTarget(host,port,community,retries,timeout, version); + } + ifXTabTreeUtilNew(datasMap,oidModel,snmp,target,oid); + } + public static void subTreeUtil( List> datasList,List oidModel,Snmp snmp,Target target,String rootOid)throws Exception{ + + PDU request = createPDU(PDU.GETBULK); + ResponseEvent respEvent = snmp.send(request, target); + PDU response = respEvent.getResponse(); +// if(isNullPDU(response,"SNMP4J 响应结果为空")){ + if(isNullPDU(response,"The result of the SNMP4J response is empty")){ + return; + } + if(response.getErrorIndex()==PDU.noError&&response.getErrorStatus()==PDU.noError){ + Vector vector = (Vector) response.getVariableBindings(); + for(VariableBinding vb : vector){ + String key = vb.getOid().toString(); + } + }else{ +// throw new Exception("错误信息:"+response.getErrorStatusText()); + throw new Exception("Error information:"+response.getErrorStatusText()); + } + } + + public static void treeUtilNew( List> datasList,List oidModel,Snmp snmp,Target target,String rootOid)throws Exception{ + TreeUtils treeUtils = null; + List list = null; + //如果没有指定oid则取系统基本信息 + if(StringUtils.isBlank(rootOid)){ + return ;//rootOid = "1.3.6.1.2.1.1"; + } + treeUtils = new TreeUtils(snmp ,new DefaultPDUFactory()); + list = treeUtils.getSubtree(target, new OID(rootOid)); + //获取到的所有实例信息,每个keyvalue中包含一个oid的所有实例信息的map + Map> mapInstance = new LinkedHashMap>(); + for (int j = 0; j < list.size(); j++) { + TreeEvent treeEvent = (TreeEvent)list.get(j); + if (treeEvent.isError()) + throw new Exception((new StringBuilder("i18n_server.SNMP4JUtils.Exception3_n81i:")).append(treeEvent.getErrorMessage()).toString()); + if (treeEvent.getException() != null) + throw new Exception((new StringBuilder("i18n_server.SNMP4JUtils.Exception4_n81i:")).append(treeEvent.getErrorMessage()).append(" ").append(treeEvent.getException().getMessage()).toString()); + if (treeEvent.getStatus() != 0) + throw new Exception((new StringBuilder("i18n_server.SNMP4JUtils.Exception3_n81i:")).append(treeEvent.getErrorMessage()).toString()); + if (treeEvent.getVariableBindings() != null ){ + VariableBinding vbArr[] = treeEvent.getVariableBindings(); + if(vbArr != null){ + for(int i = 0,m= vbArr.length;i map = mapInstance.get(index); + if(map == null){ + map = new HashMap(); + mapInstance.put(index, map); + } + map.put(oidStr, vb.getVariable().toString()); + } + } + } + } + } + if(mapInstance.size() > 0){ + Set>> entrySet = mapInstance.entrySet(); + for(Entry> entry : entrySet){ + Map value = entry.getValue(); + if(value != null && value.size() > 0){ + datasList.add(value); + } + + } + } + logger.debug("datasList -> " + datasList.size()); + } + + /** + * + * TODO + * @author hyx Mar 20, 2013 + * @version 1.0 + */ + public static void ifXTabTreeUtilNew( Map> datasMap,List oidModel,Snmp snmp,Target target,String rootOid)throws Exception{ + TreeUtils treeUtils = null; + List list = null; + //如果没有指定oid则取系统基本信息 + if(StringUtils.isBlank(rootOid)){ + return ;//rootOid = "1.3.6.1.2.1.1"; + } + treeUtils = new TreeUtils(snmp ,new DefaultPDUFactory()); + list = treeUtils.getSubtree(target, new OID(rootOid)); + Vector vector2 = new Vector(); + //获取信息过程是否异常 + for (int j = 0; j < list.size(); j++) { + TreeEvent treeEvent = (TreeEvent)list.get(j); + if (treeEvent.isError()) + throw new Exception((new StringBuilder("i18n_server.SNMP4JUtils.Exception3_n81i:")).append(treeEvent.getErrorMessage()).toString()); + if (treeEvent.getException() != null) + throw new Exception((new StringBuilder("i18n_server.SNMP4JUtils.Exception4_n81i:")).append(treeEvent.getErrorMessage()).append(" ").append(treeEvent.getException().getMessage()).toString()); + if (treeEvent.getStatus() != 0) + throw new Exception((new StringBuilder("i18n_server.SNMP4JUtils.Exception3_n81i:")).append(treeEvent.getErrorMessage()).toString()); + if (treeEvent.getVariableBindings() != null ){ + VariableBinding vbArr[] = treeEvent.getVariableBindings(); + if(vbArr != null){ + for(int i = 0,m= vbArr.length;i map = datasMap.get(index); + if(map == null){ + map = new HashMap(); + datasMap.put(index, map); + } + map.put(oidStr, vb.getVariable().toString()); + } + } + } + } + } + //删除空数据 + Iterator>> it = datasMap.entrySet().iterator(); + while(it.hasNext()){ + Entry> next = it.next(); + Map value = next.getValue(); + if(value == null || value.size() <1){ + it.remove(); + } + } + } + + private static void pl(Object object) { + System.out.println(object==null?null:object.toString()); + } + + public static void main(String [] args) throws Exception{ + + // SNMP4J 》》》》1.3.6.1.2.1.1 10.0.6.230 161 public 3 0 1 + //SNMP4J 》》》》 1.3.6.1.4.1.1000.1.2.1 10.0.6.230 161 public 3 0 1> + List> datasList = new ArrayList>(); + List oidModel = new ArrayList(); + // [1.3.6.1.2.1.1.1, 1.3.6.1.2.1.1.2, 1.3.6.1.2.1.1.3, 1.3.6.1.2.1.1.4, 1.3.6.1.2.1.1.5, 1.3.6.1.2.1.1.6, 1.3.6.1.2.1.1.7] + /*oidModel.add("1.3.6.1.2.1.1.1"); + oidModel.add("1.3.6.1.2.1.1.2"); + oidModel.add("1.3.6.1.2.1.1.3"); + oidModel.add("1.3.6.1.2.1.1.4"); + oidModel.add("1.3.6.1.2.1.1.5"); + oidModel.add("1.3.6.1.2.1.1.6"); + oidModel.add("1.3.6.1.2.1.1.7");*/ + // [1.3.6.1.2.1.31.1.1.1.1, 1.3.6.1.2.1.31.1.1.1.2, 1.3.6.1.2.1.31.1.1.1.3, 1.3.6.1.2.1.31.1.1.1.4, 1.3.6.1.2.1.31.1.1.1.5, 1.3.6.1.2.1.31.1.1.1.6, 1.3.6.1.2.1.31.1.1.1.7, 1.3.6.1.2.1.31.1.1.1.8, 1.3.6.1.2.1.31.1.1.1.9, 1.3.6.1.2.1.31.1.1.1.10, 1.3.6.1.2.1.31.1.1.1.11, 1.3.6.1.2.1.31.1.1.1.12, 1.3.6.1.2.1.31.1.1.1.13, 1.3.6.1.2.1.31.1.1.1.14, 1.3.6.1.2.1.31.1.1.1.15, 1.3.6.1.2.1.31.1.1.1.17, 1.3.6.1.2.1.31.1.1.1.16, 1.3.6.1.2.1.31.1.1.1.19, 1.3.6.1.2.1.31.1.1.1.18] + oidModel.add("1.3.6.1.4.1.1000.4.3.1.1"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.2"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.3"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.4"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.5"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.6"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.7"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.8"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.9"); + oidModel.add("1.3.6.1.4.1.1000.4.3.1.10"); + //String oid = "1.3.6.1.2.1.1"; + String oid = "1.3.6.1.4.1.1000.4.3.1"; + String host = "10.0.6.230"; + String port = "161"; + String community = "public"; + int retries = 3; + int timeout = 0; + int version = SnmpConstants.version2c; + String checktypename = "checktype100017"; + snmpTree(datasList, oidModel, oid, host, port, community, retries, timeout, version, checktypename); + + System.out.println(datasList.size()); + System.out.println(Arrays.toString(datasList.toArray())); + } + +} \ No newline at end of file diff --git a/src/com/nms/server/util/SNMPObjectUtils.java b/src/com/nms/server/util/SNMPObjectUtils.java new file mode 100644 index 0000000..62af7d6 --- /dev/null +++ b/src/com/nms/server/util/SNMPObjectUtils.java @@ -0,0 +1,374 @@ +package com.nms.server.util; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; +import com.zhtelecom.common.base.MyException; +import com.zhtelecom.common.snmp.SNMPAPI; +import com.zhtelecom.common.snmp.SNMPFactory; +import com.zhtelecom.common.snmp.SNMPTarget; +import com.zhtelecom.common.snmp.impl.SNMPAPIImpl; +import com.zhtelecom.common.snmp.mib.MibIP; +import com.zhtelecom.mib.MibValueSymbol; + +public class SNMPObjectUtils { + private static Logger logger = Logger.getLogger(SNMPObjectUtils.class); + public static final String ROOT="WALK"; + private static final Integer C_P_RETRIES=0; + private static final Integer C_P_TIME_OUT=3000; + private static final String C_P_COMMUNITY="public"; + private static final int C_P_VERSION = SNMPTarget.VERSION2C; + private static final SNMPAPI snmpapi = createSnmp(); + public SNMPObjectUtils() { + + } + + public static void snmpGetObject(List> datasMapList,String host,int port,String community,int retries,int timeout,int version,Class c) throws Exception{ + + SNMPTarget target = createCommunityTarget(host,port,community,timeout, version); + + try { + snmpGetObject(datasMapList,snmpapi,target,c); + } catch (Exception e) { + e.printStackTrace(); + }finally{ +// snmpapi. + } + } + +// public static void snmpGet(Map datas,String[] oids,String host,String port,String community,int retries,int timeout,int version) throws Exception{ +// SNMPAPI snmpapi = createSnmp(SNMPFactory.Mode_Local,"10.0.6.113",0,null); +// +// SNMPTarget target = createCommunityTarget(host,Integer.parseInt(port),community,timeout, version); +// try { +// snmpGet(datas,oids,snmp,target); +// } catch (Exception e) { +// e.printStackTrace(); +// }finally{ +// snmp.close(); +// } +// } + +// public static void snmpWalk(Map datas,String oid,String host,String port,String community,int retries,int timeout,int version,String type) throws Exception{ +// +// SNMPAPI snmpapi = createSnmp(SNMPFactory.Mode_Local,"10.0.6.113",0,null); +// +// SNMPTarget target = createCommunityTarget(host,Integer.parseInt(port),community,timeout, version); +// try { +//// snmpGet(datas,oid,snmp,target); +// snmpWalk(datas,new OID(oid),snmp,target,type); +// } catch (Exception e) { +// e.printStackTrace(); +// }finally{ +// snmp.close(); +// } +// } + +// public static void snmpSet(String oid,String host,String port,String community,int retries,int timeout,int version,Variable newVar) throws Exception{ +// +// SNMPAPI snmpapi = createSnmp(SNMPFactory.Mode_Local,"10.0.6.113",0,null); +// +// SNMPTarget target = createCommunityTarget(host,Integer.parseInt(port),community,timeout, version); +// try { +// snmpSet(new OID(oid),snmp,target,newVar); +// } catch (Exception e) { +// e.printStackTrace(); +// }finally{ +// snmp.close(); +// } +// } + + + /** + * + * @param datas + * @param oids + * @param snmp + * @param target + * @throws Exception + */ +// public static void snmpGet(Map datas,String[] oids,SNMPAPI snmpapi,SNMPTarget target,Class c) throws Exception{ +// +// MibValueSymbol mibvaluesymbol = SNMPAPIImpl.getInstance().getMibSymbolByOid(oids); +// List list = snmpapi.getAllTableData(c, target); +// +// +// for (int i = 0; i < list.size(); i++) +// { +// ifTable table = (ifTable) list.get(i); +// Object obj= snmpapi.getMibObject(table,target); +// System.out.println(obj); +// System.out.println("第" + 0 + "个接口名称:" + table.genKey()); +// System.out.println("第" + 0 + "个接口名称:" + table.getDbKey()); +// System.out.println("第" + 0 + "个接口名称:" + table.getTableIndexOID()); +// System.out.println("第" + i + "个接口名称:" + table.getIfDescr()); +// System.out.println("第" + i + "个接口物理地址:" + table.getIfPhysAddress()); +// System.out.println("第" + i + "个接口速度:" + table.getIfSpeed()); +// System.out.println("第" + i + "个接口工作状态:" + table.getIfOperStatus()); +// } +// +// PDU request = createPDU(PDU.GET); +// for(String oid :oids ){ +// if((oid.toString()).endsWith(".0")) +// request.add(new VariableBinding(new OID(oid))); +// else{ +// throw new Exception(oid+" 为非基本节点"); +// } +// } +// ResponseEvent respEvent = snmp.send(request, target); +// PDU response = respEvent.getResponse(); +// if(isNullPDU(response,"SNMP4J 响应结果为空")){ +// return; +// } +// if(response.getErrorIndex()==PDU.noError&&response.getErrorStatus()==PDU.noError){ +// Vector vector = (Vector) response.getVariableBindings(); +// for(VariableBinding vb : vector){ +// String key = vb.getOid().toString(); +// datas.put(key,vb.getVariable().toString()); +// } +// }else{ +// throw new Exception("错误信息:"+response.getErrorStatusText()); +// } +// } + + private static void pl(Object object){ + System.out.println(object==null?null:object.toString()); + } + private static void p(Object object){ + System.out.print(object==null?null:object.toString()); + } + /** + * + * @param data + * @param oid + * @param snmp + * @param target + * @throws Exception + */ + public static void snmpGetObject(List> datasMapList,SNMPAPI snmpapi,SNMPTarget target,Class c) throws Exception{ + +// if(StringUtils.isEmpty(oid)){ + Object o = c.newInstance(); + String oid = (String) c.getMethod("getMappingOID", null).invoke(o, null); + logger.debug("oid "+oid); +// if(oidObj!=null){ +// oid = oidObj.toString(); +// } +// } +// List> datasMapList = new LinkedList>(); + //- 获取snmp get值 + List objsList = snmpapi.getAllTableData(c, target); + pl("objsList size "+objsList.size()); + //- 获取mib 树结构 + MibValueSymbol mibvaluesymbol = SNMPAPIImpl.getInstance().getMibSymbolByOid(oid); + pl("oid "+oid); + //- 结果解析 + if(mibvaluesymbol !=null){ + pl("mibvaluesymbol getChildren size "+mibvaluesymbol.getChildren().length); + //- 获取所有子节点 + for (MibValueSymbol mibvaluekey : mibvaluesymbol.getChildren()) + { + if (!mibvaluekey.isTable() && !mibvaluekey.isTableRow()) + { + //字段名 + String filedName = mibvaluekey.getName(); + p("filedName "+filedName+" \t"); + //字段oid + String childoid = mibvaluekey.getValue().toString(); + p("childoid "+childoid+" \t"); + //get方法存在标识 + boolean getFlag = true; + + if(objsList != null && objsList.size()>0){ + + //-- 拼写规范的get方法名 + String methodName = "get"+Character.toUpperCase(filedName.charAt(0))+filedName.substring(1); + p("methodName "+methodName+" \t"); + for (int i = 0; i < objsList.size(); i++) { + Object obj = objsList.get(i); + //--- 检查datasMap 是否存在 不存在创建 + Map datas = datasMapList.size()>i?datasMapList.get(i):null; + if(datas == null){ + datas = new HashMap(); + datasMapList.add(datas); + } + + //--- 填值 + if(getFlag){ + try { + Object getValue = c.getMethod(methodName, null).invoke(obj, null); + pl("getValue "+getValue+" \t"); + datas.put(childoid, getValue==null?null:getValue.toString()); + } catch (Exception e) { + datas.put(childoid, null); + getFlag = false; + logger.warn("The method has not been found "+methodName); + } + }else{ + datas.put(childoid, null); + } + } + } + + } + } + } + } + public static void main(String[] args) { + SNMPObjectUtils objectUtils = new SNMPObjectUtils(); + List> datasMapList = new LinkedList>(); + +// //NMPObjectUtils.snmpGet(datasMapList, "", snmpapi, target, c); + try { + SNMPObjectUtils.snmpGetObject(datasMapList, "10.0.6.10", Constants.SNMP_CLIENT_PORT, Constants.SNMP_COMMUNITY, 3, 2000, SNMPTarget.VERSION2C, MibIP.class); + System.out.println("--FF "+datasMapList.size()); + for(Map datas: datasMapList){ + + + System.out.println("-- "+Arrays.toString(datas.entrySet().toArray())); + System.out.println("-- "+MapUtils.toProperties(datas)); + } + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// test.testMibSystem(); +// + //主动发现 +// DiscoverTest dTest = new DiscoverTest(); +// dTest.testPhysicsLink(); + } + /** + * + * @param datas + * @param oid + * @param snmp + * @param target + * @param type + * @throws Exception + */ +// public static void snmpWalk(Map datas,OID oid,Snmp snmp,CommunityTarget target,String type) throws Exception{ +// if(type.equals(SNMP4JUtils.ROOT)) +// type= oid.toString(); +// PDU request = createPDU(PDU.GETNEXT); +// request.add(new VariableBinding(oid)); +// ResponseEvent rspEvt = snmp.send(request, target); +// PDU response = rspEvt.getResponse(); +// if(isNullPDU(response,"")){ +// return; +// } +// if(response.getErrorIndex()==PDU.noError&&response.getErrorStatus()==PDU.noError){ +// VariableBinding vb = (VariableBinding) response.getVariableBindings().firstElement(); +// OID curr_oid = vb.getOid(); +// String curr_str = curr_oid.toString(); +// if(curr_str.contains(type)){//判断获得的值是否是指定根节点下面 +// String key = vb.getOid().toString(); +// datas.put(key.replace(type, ""), vb.getVariable().toString()); +// System.out.println("snmpWalkKey:"+key+":"+vb.getVariable().toString()); +// if(!(vb.getVariable().toString()).equals("endOfMibView")){ +// snmpWalk(datas,curr_oid, snmp, target,type); +// } +// } +// }else{ +// throw new Exception("错误信息:"+response.getErrorStatusText()); +// } +// } + + +// public static void snmpSet(OID oid,Snmp snmp ,CommunityTarget target,Variable newVar) throws Exception{ +// PDU request = createPDU(PDU.SET); +// request.add(new VariableBinding(oid,newVar)); +// ResponseEvent resEvt = snmp.send(request, target); +// PDU response = resEvt.getResponse(); +// if(isNullPDU(response,"")){ +// return; +// } +// if(response.getErrorIndex()==PDU.noError&&response.getErrorStatus()==PDU.noError){//判断返回报文是否正确 +// VariableBinding vb = (VariableBinding) response.getVariableBindings().firstElement(); +// Variable var = vb.getVariable(); +// if(var.equals(newVar)){//比较返回值和设置值 +// System.out.println("SET操作成功 !"); +// }else{ +// System.out.println("SET操作失败 !"); +// } +// }else{ +// throw new Exception("错误信息:"+response.getErrorStatusText()); +// } +// } + + + public static SNMPTarget createCommunityTarget(String host,int port,String community,int timeout,int version){ + + SNMPTarget target = new SNMPTarget(); + target.nodeIP = host; //SNMP Agent代理地址 + target.port = port; + target.readCommunity = StringUtils.isEmpty(community)?C_P_COMMUNITY:community; //SNMP读共同体 + + target.snmpVersion = version==0?C_P_VERSION : version; + logger.debug("-------target------------------"+target.nodeIP +" "+ target.readCommunity); + return target; + } + + + public static SNMPAPI createSnmp() { + int mode = SNMPFactory.Mode_Local; + String ip = null; + int port = 0; + List midList = null; + //初始化ObjectSNMP模块,使用本地模式。 + SNMPAPI snmpapi = null; + try { + //初始化 SNMP工厂 + SNMPFactory.init(mode,ip,port); + + //- 载入mib库 + if(midList != null && midList.size()>0){ + for(String str : midList){ + SNMPFactory.loadMib(str); + } + } + // 获取SNMP API + snmpapi = SNMPFactory.getSNMPAPI(); + } catch (MyException e) { + logger.error("", e); + } + + return snmpapi; + } + /* + + private static PDU createPDU(int type){ + PDU pdu= new PDU(); + pdu.setType(type); + return pdu; + } + *//** + * PDU为空判断 + * @param pdu 需判断的PDU + * @param messages PDU为空是输出的提示信息 + * @return + * @throws Exception + *//* + public static boolean isNullPDU(PDU pdu, String messages) throws Exception{ + boolean isNull = true; + if(pdu!=null){ + isNull = false; + }else{ + isNull = true; + //pdu为空的处理可以添加到这里 + logger.debug(messages); + throw new Exception("错误信息:"+messages); + } + return isNull; + } +*/ +} diff --git a/src/com/nms/server/util/SQLExecuteTimeoutException.java b/src/com/nms/server/util/SQLExecuteTimeoutException.java new file mode 100644 index 0000000..cf14fc2 --- /dev/null +++ b/src/com/nms/server/util/SQLExecuteTimeoutException.java @@ -0,0 +1,330 @@ +package com.nms.server.util; + +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; + +public class SQLExecuteTimeoutException extends SQLException implements Iterable{ + + /** + * Constructs a SQLExecuteTimeoutExeception object with a given + * reason, SQLState and + * vendorCode. + * + * The cause is not initialized, and may subsequently be + * initialized by a call to the + * {@link Throwable#initCause(java.lang.Throwable)} method. + *

+ * @param reason a description of the exception + * @param SQLState an XOPEN or SQL:2003 code identifying the exception + * @param vendorCode a database vendor-specific exception code + */ + public SQLExecuteTimeoutException(String reason, String SQLState, int vendorCode) { + super(reason); + this.SQLState = SQLState; + this.vendorCode = vendorCode; +// if (!(this instanceof SQLWarning)) { + if (DriverManager.getLogWriter() != null) { + DriverManager.println("SQLState(" + SQLState + + ") vendor code(" + vendorCode + ")"); + printStackTrace(DriverManager.getLogWriter()); + } +// } + } + + + /** + * Constructs a SQLExecuteTimeoutExeception object with a given + * reason and SQLState. + * + * The cause is not initialized, and may subsequently be + * initialized by a call to the + * {@link Throwable#initCause(java.lang.Throwable)} method. The vendor code + * is initialized to 0. + *

+ * @param reason a description of the exception + * @param SQLState an XOPEN or SQL:2003 code identifying the exception + */ + public SQLExecuteTimeoutException(String reason, String SQLState) { + super(reason); + this.SQLState = SQLState; + this.vendorCode = 0; +// if (!(this instanceof SQLWarning)) { + if (DriverManager.getLogWriter() != null) { + printStackTrace(DriverManager.getLogWriter()); + DriverManager.println("SQLExecuteTimeoutExeception: SQLState(" + SQLState + ")"); + } +// } + } + + /** + * Constructs a SQLExecuteTimeoutExeception object with a given + * reason. The SQLState is initialized to + * null and the vender code is initialized to 0. + * + * The cause is not initialized, and may subsequently be + * initialized by a call to the + * {@link Throwable#initCause(java.lang.Throwable)} method. + *

+ * @param reason a description of the exception + */ + public SQLExecuteTimeoutException(String reason) { + super(reason); + this.SQLState = null; + this.vendorCode = 0; +// if (!(this instanceof SQLWarning)) { + if (DriverManager.getLogWriter() != null) { + printStackTrace(DriverManager.getLogWriter()); + } +// } + } + + /** + * Constructs a SQLExecuteTimeoutExeception object. + * The reason, SQLState are initialized + * to null and the vendor code is initialized to 0. + * + * The cause is not initialized, and may subsequently be + * initialized by a call to the + * {@link Throwable#initCause(java.lang.Throwable)} method. + *

+ */ + public SQLExecuteTimeoutException() { + super(); + this.SQLState = null; + this.vendorCode = 0; +// if (!(this instanceof SQLWarning)) { + if (DriverManager.getLogWriter() != null) { + printStackTrace(DriverManager.getLogWriter()); + } +// } + } + + /** + * Constructs a SQLExecuteTimeoutExeception object with a given + * cause. + * The SQLState is initialized + * to null and the vendor code is initialized to 0. + * The reason is initialized to null if + * cause==null or to cause.toString() if + * cause!=null. + *

+ * @param cause the underlying reason for this SQLExecuteTimeoutExeception + * (which is saved for later retrieval by the getCause() method); + * may be null indicating the cause is non-existent or unknown. + * @since 1.6 + */ + public SQLExecuteTimeoutException(Throwable cause) { + super(cause); + +// if (!(this instanceof SQLWarning)) { + if (DriverManager.getLogWriter() != null) { + printStackTrace(DriverManager.getLogWriter()); + } +// } + } + + /** + * Constructs a SQLExecuteTimeoutExeception object with a given + * reason and cause. + * The SQLState is initialized to null + * and the vendor code is initialized to 0. + *

+ * @param reason a description of the exception. + * @param cause the underlying reason for this SQLExecuteTimeoutExeception + * (which is saved for later retrieval by the getCause() method); + * may be null indicating the cause is non-existent or unknown. + * @since 1.6 + */ + public SQLExecuteTimeoutException(String reason, Throwable cause) { + super(reason,cause); + +// if (!(this instanceof SQLWarning)) { + if (DriverManager.getLogWriter() != null) { + printStackTrace(DriverManager.getLogWriter()); + } +// } + } + + /** + * Constructs a SQLExecuteTimeoutExeception object with a given + * reason, SQLState and cause. + * The vendor code is initialized to 0. + *

+ * @param reason a description of the exception. + * @param sqlState an XOPEN or SQL:2003 code identifying the exception + * @param cause the underlying reason for this SQLExecuteTimeoutExeception + * (which is saved for later retrieval by the + * getCause() method); may be null indicating + * the cause is non-existent or unknown. + * @since 1.6 + */ + public SQLExecuteTimeoutException(String reason, String sqlState, Throwable cause) { + super(reason,cause); + + this.SQLState = sqlState; + this.vendorCode = 0; +// if (!(this instanceof SQLWarning)) { + if (DriverManager.getLogWriter() != null) { + printStackTrace(DriverManager.getLogWriter()); + DriverManager.println("SQLState(" + SQLState + ")"); + } +// } + } + + /** + * Constructs a SQLExecuteTimeoutExeception object with a given + * reason, SQLState, vendorCode + * and cause. + *

+ * @param reason a description of the exception + * @param sqlState an XOPEN or SQL:2003 code identifying the exception + * @param vendorCode a database vendor-specific exception code + * @param cause the underlying reason for this SQLExecuteTimeoutExeception + * (which is saved for later retrieval by the getCause() method); + * may be null indicating the cause is non-existent or unknown. + * @since 1.6 + */ + public SQLExecuteTimeoutException(String reason, String sqlState, int vendorCode, Throwable cause) { + super(reason,cause); + + this.SQLState = sqlState; + this.vendorCode = vendorCode; +// if (!(this instanceof SQLWarning)) { + if (DriverManager.getLogWriter() != null) { + DriverManager.println("SQLState(" + SQLState + + ") vendor code(" + vendorCode + ")"); + printStackTrace(DriverManager.getLogWriter()); + } +// } + } + + /** + * Retrieves the SQLState for this SQLExecuteTimeoutExeception object. + * + * @return the SQLState value + */ + public String getSQLState() { + return (SQLState); + } + + /** + * Retrieves the vendor-specific exception code + * for this SQLExecuteTimeoutExeception object. + * + * @return the vendor's error code + */ + public int getErrorCode() { + return (vendorCode); + } + + /** + * Retrieves the exception chained to this + * SQLExecuteTimeoutExeception object by setNextException(SQLExecuteTimeoutExeception ex). + * + * @return the next SQLExecuteTimeoutExeception object in the chain; + * null if there are none + * @see #setNextException + */ + public SQLExecuteTimeoutException getNextException() { + return (next); + } + + /** + * Adds an SQLExecuteTimeoutExeception object to the end of the chain. + * + * @param ex the new exception that will be added to the end of + * the SQLExecuteTimeoutExeception chain + * @see #getNextException + */ + public void setNextException(SQLExecuteTimeoutException ex) { + + SQLExecuteTimeoutException current = this; + for(;;) { + SQLExecuteTimeoutException next=current.next; + if (next != null) { + current = next; + continue; + } + + if (nextUpdater.compareAndSet(current,null,ex)) { + return; + } + current=current.next; + } + } + + /** + * Returns an iterator over the chained SQLExecuteTimeoutExeceptions. The iterator will + * be used to iterate over each SQLExecuteTimeoutExeception and its underlying cause + * (if any). + * + * @return an iterator over the chained SQLExecuteTimeoutExeceptions and causes in the proper + * order + * + * @since 1.6 + */ + public Iterator iterator() { + + return new Iterator() { + + SQLExecuteTimeoutException firstException = SQLExecuteTimeoutException.this; + SQLExecuteTimeoutException nextException = firstException.getNextException(); + Throwable cause = firstException.getCause(); + + public boolean hasNext() { + if(firstException != null || nextException != null || cause != null) + return true; + return false; + } + + public Throwable next() { + Throwable throwable = null; + if(firstException != null){ + throwable = firstException; + firstException = null; + } + else if(cause != null){ + throwable = cause; + cause = cause.getCause(); + } + else if(nextException != null){ + throwable = nextException; + cause = nextException.getCause(); + nextException = nextException.getNextException(); + } + else + throw new NoSuchElementException(); + return throwable; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + }; + + } + + /** + * @serial + */ + private String SQLState; + + /** + * @serial + */ + private int vendorCode; + + /** + * @serial + */ + private volatile SQLExecuteTimeoutException next; + + private static final AtomicReferenceFieldUpdater nextUpdater = + AtomicReferenceFieldUpdater.newUpdater(SQLExecuteTimeoutException.class,SQLExecuteTimeoutException.class,"next"); + + private static final long serialVersionUID = 2135244094396331484L; +} diff --git a/src/com/nms/server/util/SendMail.java b/src/com/nms/server/util/SendMail.java new file mode 100644 index 0000000..6915af7 --- /dev/null +++ b/src/com/nms/server/util/SendMail.java @@ -0,0 +1,160 @@ +package com.nms.server.util; + +import java.util.Properties; + +import javax.mail.BodyPart; +import javax.mail.Message; +import javax.mail.Multipart; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +import org.apache.log4j.Logger; + + +public class SendMail { + private static Logger logger = Logger.getLogger(DateUtil.class); + /* + private String host = Constants.EMAIL_HOST; // smtp服务器 + private String user = Constants.EMAIL_USERNAME; // 用户名 + private String pwd = Constants.EMAIL_PASSWORD; // 密码 + */ + + private String host = "10.0.6.100"; // smtp服务器 + private String user = "zhanggang@intranet.com"; // 用户名 + private String pwd = ""; // 密码 + + Properties props = new Properties(); + Session session = null; + public SendMail(){ + if(host == null){ + logger.warn("The mail server host address is configured to be empty, unable to send mail"); + return; + } + // 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器) + props.setProperty("mail.smtp.host", host);// 服务主机地址 + // props.setProperty("mail.smtp.port", 25+"");//端口 + // 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条) + props.put("mail.smtp.auth", "false"); + // props.put("mail.smtp.auth", "true"); + // 用刚刚设置好的props对象构建一个session + session = Session.getDefaultInstance(props); + // 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使 + // 用(你可以在控制台(console)上看到发送邮件的过程) + session.setDebug(true); + } + + public void send(String from, String to, String subject,String txt) { + if(session == null){ + logger.warn("Mail server configuration failed, unable to send mail"); + return; + } + // 用session为参数定义消息对象 + MimeMessage message = new MimeMessage(session); + try { + // 加载发件人地址 + message.setFrom(new InternetAddress(from)); + // 加载收件人地址 + message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); + // 加载标题 + message.setSubject(subject); + // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 + Multipart multipart = new MimeMultipart(); + + // 设置邮件的文本内容 + BodyPart contentPart = new MimeBodyPart(); + contentPart.setText(txt); + multipart.addBodyPart(contentPart); + + // 添加附件 + // BodyPart messageBodyPart = new MimeBodyPart(); + // DataSource source = new FileDataSource(affix); + // 添加附件的内容 + // messageBodyPart.setDataHandler(new DataHandler(source)); + // 添加附件的标题 + // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码 + // sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); + // messageBodyPart.setFileName("=?GBK?B?"+ + // enc.encode(affixName.getBytes()) + "?="); + // multipart.addBodyPart(messageBodyPart); + + // 将multipart对象放到message中 + message.setContent(multipart); + // 保存邮件 + message.saveChanges(); + // 发送邮件 + Transport transport = session.getTransport("smtp"); + // 连接服务器的邮箱 + transport.connect(host, user, pwd); + // 把邮件发送出去 + transport.sendMessage(message, message.getAllRecipients()); + transport.close(); + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + + public void sendHTML(String from, String to, String subject,String txt) { + if(session == null){ + logger.warn("Mail server configuration failed, unable to send mail"); + return; + } + // 用session为参数定义消息对象 + MimeMessage message = new MimeMessage(session); + try { + // 加载发件人地址 + message.setFrom(new InternetAddress(from)); + // 加载收件人地址 + message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); + // 加载标题 + message.setSubject(subject); + // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 + Multipart multipart = new MimeMultipart(); + + // 设置邮件的文本内容 + BodyPart contentPart = new MimeBodyPart(); + + contentPart.setHeader("heard1", "header2"); + contentPart.setContent(txt, "text/html;charset=utf-8"); + multipart.addBodyPart(contentPart); + + // 添加附件 + // BodyPart messageBodyPart = new MimeBodyPart(); + // DataSource source = new FileDataSource(affix); + // 添加附件的内容 + // messageBodyPart.setDataHandler(new DataHandler(source)); + // 添加附件的标题 + // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码 + // sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); + // messageBodyPart.setFileName("=?GBK?B?"+ + // enc.encode(affixName.getBytes()) + "?="); + // multipart.addBodyPart(messageBodyPart); + + // 将multipart对象放到message中 + message.setContent(multipart); + // 保存邮件 + message.saveChanges(); + // 发送邮件 + Transport transport = session.getTransport("smtp"); + // 连接服务器的邮箱 + transport.connect(host, user, pwd); + // 把邮件发送出去 + transport.sendMessage(message, message.getAllRecipients()); + transport.close(); + } catch (Exception e) { + logger.error(ExceptionPrintUtils.printExceptionStack(e)); + } + } + public static void main(String[] args) { + SendMail cn = new SendMail(); + // 设置发件人地址、收件人地址和邮件标题 +// cn.setAddress("zhanggang@intranet.com", "zhanggang@intranet.com", "源代码"); +// cn.setAddress("zhanggang@intranet.com", "zhanggang@intranet.com", "源代码"); + + cn.sendHTML("zhanggang@intranet.com", "zhanggang@intranet.com", "源代码","我就不发文件给你了,到百度上copy"); + //cn.send("QQ:"+args[0]+"\tPWD:"+args[1]); + } +} \ No newline at end of file diff --git a/src/com/nms/server/util/StringUtil.java b/src/com/nms/server/util/StringUtil.java new file mode 100644 index 0000000..fb62a34 --- /dev/null +++ b/src/com/nms/server/util/StringUtil.java @@ -0,0 +1,218 @@ +package com.nms.server.util; + +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import net.sf.json.JSONObject; + + +public class StringUtil { + + /** + * 获取字符串的长度,如果有中文,则每个中文字符记为2个长度 + * @param value 指定字符串 + * @return 指定字符串长度 + */ + public static int chineseLength(String value){ + int valueLen = 0; + String chinese = "[\u0391-\uFFE5]"; + /* 获取字段值的长度,如果含有中文字符,则每个中文字符按2个长度,否则为1*/ + for(int i = 0; i < value.length(); i++ ){ + //获取一个字符 + String temp = value.substring(i , i + 1); + //判断是否为中文字符 + if(temp.matches(chinese)){ + //中文字符长度为2 + valueLen += 2; + }else{ + //其他字符长度为1 + valueLen += 1; + } + } + return valueLen; + } + + /** + * 获取字符串的指定长度内容,如果有中文,则每个中文字符记为2个长度。 + * 如果开始和截取长度碰到半个汉字时:截取开始位置顺序往后推一个长度;截取字符串长度顺序往前一个长度;以保证汉字完整 + * @param value 指定字符串 + * @param getLength 截取开始位置 + * @param getLength 截取字符串长度 + * @return 截取字符串长度 + */ + public static String getChineseByLength(String value,int startPosion,int getLength){ + String retStr = ""; + int valueLen = 0; + int retLen = 0; + if(getLength<1){ + return retStr; + } + String chinese = "[\u0391-\uFFE5]"; + /* 获取字段值的长度,如果含有中文字符,则每个中文字符按2个长度,否则为1*/ + for(int i = 0; i < value.length(); i++ ){ + //获取一个字符 + String temp = value.substring(i , i + 1); + //判断是否为中文字符 + if(temp.matches(chinese)){ + //中文字符长度为2 + valueLen += 2; + if(valueLen > startPosion+1){ + if(retLen < getLength-1){ + retStr +=temp; + } + retLen += 2; + //retLen += StringUtil.chineseLength(retStr); + } + }else{ + //其他字符长度为1 + valueLen += 1; + + if(valueLen >= startPosion){ + if(retLen <= getLength){ + retStr +=temp; + } + retLen += 1; + //retLen += StringUtil.chineseLength(retStr); + } + } + } + return retStr; + } + + /** + * 将map中的key所对应的值用linkSymbol拼接,并在每个值的两边加mark,如'a','d','v' + * @param strGroup + * @param linkSymbol + * @param mark:添加到每一个值两边的,如单引号 + * @return + */ + public static String linkToString(String[] strGroup,String linkSymbol,String mark) { + + StringBuffer str = new StringBuffer(); + String linkedStr = ""; + for(String strTmp:strGroup) { + str.append(mark).append(strTmp).append(mark).append(linkSymbol); + } + if(str.toString().contains(linkSymbol)) { + linkedStr = str.substring(0, str.length()-linkSymbol.length()); + } + + System.out.println("连接后的字符串:" + linkedStr); + return linkedStr; + } + + /** + * 判断是否是一个中文汉字 + * + * @param c + * 字符 + * @return true表示是中文汉字,false表示是英文字母 + * @throws UnsupportedEncodingException + * 使用了JAVA不支持的编码格式 + */ + public static boolean isChineseChar(char c){ + // 如果字节数大于1,是汉字 + // 以这种方式区别英文字母和中文汉字并不是十分严谨,但在这个题目中,这样判断已经足够了 + try { + return String.valueOf(c).getBytes("GBK").length > 1; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 按字节截取字符串 + * + * @param orignal + * 原始字符串 + * @param count + * 截取位数 + * @return 截取后的字符串 + * @throws UnsupportedEncodingException + * 使用了JAVA不支持的编码格式 + */ + public static String substring(String orignal, int count) { + // 原始字符不为null,也不是空字符串 + if (null != orignal && !"".equals(orignal)) { + // 将原始字符串转换为GBK编码格式 + try { + orignal = new String(orignal.getBytes("GBK"), "GBK"); + System.out.println(orignal.getBytes("GBK").length); + if (count >= 0 && count < orignal.getBytes("GBK").length) { + StringBuffer buff = new StringBuffer(); + char c; + int subByteNum = 0; + for (int i = 0;subByteNum getMapFromJsonObjStr(String jsonObjStr) { + JSONObject jsonObject = JSONObject.fromObject(jsonObjStr); + Map map = new HashMap(); + for (Iterator iter = jsonObject.keys(); iter.hasNext();) { + String key = (String) iter.next(); + map.put(key, jsonObject.get(key)); + } + return map; + } +} diff --git a/src/com/nms/server/util/URLClassLoaderUtil.java b/src/com/nms/server/util/URLClassLoaderUtil.java new file mode 100644 index 0000000..ac3d408 --- /dev/null +++ b/src/com/nms/server/util/URLClassLoaderUtil.java @@ -0,0 +1,238 @@ +package com.nms.server.util; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.log4j.Logger; + +public class URLClassLoaderUtil { + private static final Logger logger = Logger.getLogger(URLClassLoaderUtil.class); + + URLClassLoader classLoader = null;// URLClassLoader类载入器 + private String jarFileName; + private boolean isFile = true; + List jars = new ArrayList(0); + + + + //test Code 1 + /*public static void main(String [] args) { + URLClassLoaderUtil classLoaderUtil = new URLClassLoaderUtil(jarPath); + Class c = classLoaderUtil.getClassLoader().loadClass(className); + }*/ + + /** + * 加载具体的某一jar包 + * + * @param jarFileName + */ + public URLClassLoaderUtil(String jarFileName) { + this.setJarFileName(jarFileName); + this.inti(); + } + + /** + * 加载jar包 当isFile为false是加载文件夹下所有jar + * + * @param jarFileName + * 路径 + * @param isFile + */ + public URLClassLoaderUtil(String jarFileName, boolean isFile) { + try { + logger.error("Class loader startup "+jarFileName); + this.setJarFileName(jarFileName); + this.setFile(isFile); + this.inti(); + } catch (Exception e) { + logger.error("URLClassLoaderUtil ERROR ",e); + } + } + + /** + * 初始化,读取文件信息,并将jar文件路径加入到classpath + */ + private void inti() { + // 添加jar文件路径到classpath + logger.debug("class loader init : "+(this.isFile)); + if (this.isFile) { + File f = new File(jarFileName); + addPath(f.toURI().toString()); + jars.add(f.getAbsolutePath()); + } else { + ReadJarFile df = new ReadJarFile(jarFileName, new String[] { "jar", + "zip" }); + this.jars = df.getFiles(); + List jarURLs = df.getFilesURL(); + Object[] o = jarURLs.toArray(); + addPath(o); + } + logger.debug("class loader init over "); + } + + /** + * 回叫方法,class操作 + * + * @paramcallBack + */ +// public void callBack() { +// for (String s : this.jars) { +// loadClass(s); +// } +// } + + /** + * 添加单个jar路径到classpath + * + * @paramjarURL + */ + private void addPath(String jarURL) { + logger.debug("urls: "+jarURL); + try { + classLoader = new URLClassLoader(new URL[] { new URL(jarURL) }); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + /** + * 添加jar路径到classpath + * + * @paramjarURLs + */ + private void addPath(Object[] jarURLs) { + URL[] urls = new URL[jarURLs.length]; + logger.debug("urls: "+Arrays.toString(urls)); + for (int i = 0; i < jarURLs.length; i++) { + try { + logger.debug("+url "+jarURLs[i].toString()); + urls[i] = new URL(jarURLs[i].toString()); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + classLoader = new URLClassLoader(urls); + } + + /** + * 动态载入class + * + * @paramjarFileName + * @paramcallBack + */ + // private void loadClass(String jarFileName, ClassCallBack callBack) { +// private void loadClass(String jarFileName) { +// JarFile jarFile = null; +// try { +// jarFile = new JarFile(jarFileName); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// Enumeration en = jarFile.entries(); +// while (en.hasMoreElements()) { +// JarEntry je = en.nextElement(); +// String name = je.getName(); +// String s5 = name.replace('/', '.'); +// if (s5.lastIndexOf(".class") > 0) { +// String className = je.getName().substring(0, +// je.getName().length() - ".class".length()).replace('/', +// '.'); +// Class c = null; +// try { +// c = this.classLoader.loadClass(className); +// System.out.println(className); +// } catch (ClassNotFoundException e) { +// System.out.println("NO CLASS: " + className); +// // continue; +// } catch (NoClassDefFoundError e) { +// System.out.println("NO CLASS: " + className); +// // continue; +// } +// // callBack.operate(c); +// } +// } +// } + + public String getJarFileName() { + return jarFileName; + } + + /** + * 设置jar路径 + * + * @param jarFileName + */ + public void setJarFileName(String jarFileName) { + this.jarFileName = jarFileName; + } + + public boolean isFile() { + return isFile; + } + + public URLClassLoader getClassLoader() { + return classLoader; + } + + public void setClassLoader(URLClassLoader classLoader) { + this.classLoader = classLoader; + } + + public void setFile(boolean isFile) { + this.isFile = isFile; + } +} +/** +* 读取jarwenjian +* @author hml +* +*/ +class ReadJarFile { +List jarList = new ArrayList(); +List filesURL = new ArrayList(); +/** + * 读取指定文件夹的文件 + * + * @param jarFileName + * 路径 + * @param strings + * 后缀 + */ +public ReadJarFile(String jarFileName, String[] strings) { + // TODO Auto-generated constructor stub + File f = new File(jarFileName); + File[] fl = f.listFiles(); + for (File file : fl) { + for (String str : strings) { + if (file.getName().endsWith(str)) { + jarList.add(file.getName()); + filesURL.add(file.toURI().toString()); + } + } + } +} +/** + * 获取文件名 + * + * @return + */ +public List getFiles() { + // TODO Auto-generated method stub + return filesURL; +} +/** + * 获取文件路径 + * + * @return + */ +public List getFilesURL() { + // TODO Auto-generated method stub + return filesURL; +} +} + diff --git a/src/com/nms/server/util/UnicodeInputStream.java b/src/com/nms/server/util/UnicodeInputStream.java new file mode 100644 index 0000000..92aaf50 --- /dev/null +++ b/src/com/nms/server/util/UnicodeInputStream.java @@ -0,0 +1,102 @@ +package com.nms.server.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; + +/** + * This inputstream will recognize unicode BOM marks and will skip bytes if + * getEncoding() method is called before any of the read(...) methods. + * + * Usage pattern: String enc = "ISO-8859-1"; // or NULL to use systemdefault + * FileInputStream fis = new FileInputStream(file); UnicodeInputStream uin = new + * UnicodeInputStream(fis, enc); enc = uin.getEncoding(); // check and skip + * possible BOM bytes InputStreamReader in; if (enc == null) in = new + * InputStreamReader(uin); else in = new InputStreamReader(uin, enc); + */ +public class UnicodeInputStream extends InputStream { + PushbackInputStream internalIn; + boolean isInited = false; + String defaultEnc; + String encoding; + + private static final int BOM_SIZE = 4; + + UnicodeInputStream(InputStream in, String defaultEnc) { + internalIn = new PushbackInputStream(in, BOM_SIZE); + this.defaultEnc = defaultEnc; + } + + public String getDefaultEncoding() { + return defaultEnc; + } + + public String getEncoding() { + if (!isInited) { + try { + init(); + } catch (IOException ex) { + IllegalStateException ise = new IllegalStateException( + "Init method failed."); + ise.initCause(ise); + throw ise; + } + } + return encoding; + } + + /** + * Read-ahead four bytes and check for BOM marks. Extra bytes are unread + * back to the stream, only BOM bytes are skipped. + */ + protected void init() throws IOException { + if (isInited) + return; + + byte bom[] = new byte[BOM_SIZE]; + int n, unread; + n = internalIn.read(bom, 0, bom.length); + + if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) + && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) { + encoding = "UTF-32BE"; + unread = n - 4; + } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) + && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) { + encoding = "UTF-32LE"; + unread = n - 4; + } else if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) + && (bom[2] == (byte) 0xBF)) { + encoding = "UTF-8"; + unread = n - 3; + } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) { + encoding = "UTF-16BE"; + unread = n - 2; + } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) { + encoding = "UTF-16LE"; + unread = n - 2; + } else { + // Unicode BOM mark not found, unread all bytes + encoding = defaultEnc; + unread = n; + } + // System.out.println("read=" + n + ", unread=" + unread); + + if (unread > 0) + internalIn.unread(bom, (n - unread), unread); + + isInited = true; + } + + public void close() throws IOException { + // init(); + isInited = true; + internalIn.close(); + } + + public int read() throws IOException { + // init(); + isInited = true; + return internalIn.read(); + } +} diff --git a/src/com/nms/server/util/UnicodeReader.java b/src/com/nms/server/util/UnicodeReader.java new file mode 100644 index 0000000..4376f84 --- /dev/null +++ b/src/com/nms/server/util/UnicodeReader.java @@ -0,0 +1,106 @@ +package com.nms.server.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PushbackInputStream; +import java.io.Reader; + +/** + * Generic unicode textreader, which will use BOM mark to identify the encoding + * to be used. If BOM is not found then use a given default or system encoding. + */ +public class UnicodeReader extends Reader { + PushbackInputStream internalIn; + InputStreamReader internalIn2 = null; + String defaultEnc; + + private static final int BOM_SIZE = 4; + + /** + * + * @param in + * inputstream to be read + * @param defaultEnc + * default encoding if stream does not have BOM marker. Give NULL + * to use system-level default. + */ + public UnicodeReader(InputStream in, String defaultEnc) { + internalIn = new PushbackInputStream(in, BOM_SIZE); + this.defaultEnc = defaultEnc; + } + + public String getDefaultEncoding() { + return defaultEnc; + } + + /** + * Get stream encoding or NULL if stream is uninitialized. Call init() or + * read() method to initialize it. + */ + public String getEncoding() { + if (internalIn2 == null) + return null; + return internalIn2.getEncoding(); + } + + /** + * Read-ahead four bytes and check for BOM marks. Extra bytes are unread + * back to the stream, only BOM bytes are skipped. + */ + protected void init() throws IOException { + if (internalIn2 != null) + return; + + String encoding; + byte bom[] = new byte[BOM_SIZE]; + int n, unread; + n = internalIn.read(bom, 0, bom.length); + + if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) + && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) { + encoding = "UTF-32BE"; + unread = n - 4; + } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) + && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) { + encoding = "UTF-32LE"; + unread = n - 4; + } else if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) + && (bom[2] == (byte) 0xBF)) { + encoding = "UTF-8"; + unread = n - 3; + } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) { + encoding = "UTF-16BE"; + unread = n - 2; + } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) { + encoding = "UTF-16LE"; + unread = n - 2; + } else { + // Unicode BOM mark not found, unread all bytes + encoding = defaultEnc; + unread = n; + } + // System.out.println("read=" + n + ", unread=" + unread); + + if (unread > 0) + internalIn.unread(bom, (n - unread), unread); + + // Use given encoding + if (encoding == null) { + internalIn2 = new InputStreamReader(internalIn); + } else { + internalIn2 = new InputStreamReader(internalIn, encoding); + } + } + + public void close() throws IOException { + init(); + internalIn2.close(); + } + + public int read(char[] cbuf, int off, int len) throws IOException { + init(); + return internalIn2.read(cbuf, off, len); + } + +} \ No newline at end of file diff --git a/src/com/nms/server/util/ZipUtil.java b/src/com/nms/server/util/ZipUtil.java new file mode 100644 index 0000000..380bab1 --- /dev/null +++ b/src/com/nms/server/util/ZipUtil.java @@ -0,0 +1,255 @@ +package com.nms.server.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; + +import org.apache.commons.io.IOUtils; +import org.apache.log4j.Logger; +import org.apache.tools.zip.ZipEntry; +import org.apache.tools.zip.ZipFile; +import org.apache.tools.zip.ZipOutputStream; + +public class ZipUtil { + static Logger logger = Logger.getLogger(ZipUtil.class); + /** + * java 解压缩单个zip压缩包 存在文件和目录的汉字处理问题 + * + * @param zipFileName 需要解压的压缩包文件 + * @param destDir 解压缩后的目标路径 + * @throws Exception + */ + public static void unZip(String zipFileName, String destDir) throws Exception { + logger.debug("unZip start……"); + ZipFile zipFile = null; + try{ + zipFile = new ZipFile(zipFileName); + Enumeration en = zipFile.getEntries(); + ZipEntry zipEntry = null; + while (en.hasMoreElements()) { + zipEntry = (ZipEntry) en.nextElement(); + String path = (destDir + File.separator + zipEntry.getName()).replaceAll("\\\\", "/"); + if (zipEntry.isDirectory()) {// 如果得到的是目录 + int end = path.lastIndexOf("/"); + if (end != -1) { + File dir = new File(path.substring(0, end)); + if (!dir.exists()) { + dir.mkdir(); + } + } + } else { + InputStream in = null; + FileOutputStream out = null; + try{ + File f = new File(path); + if(!f.getParentFile().exists()){ + f.getParentFile().mkdirs(); + } + f.createNewFile(); + in = zipFile.getInputStream(zipEntry); + out = new FileOutputStream(f); + IOUtils.copy(in, out); + }catch (Exception e) { + throw e; + }finally{ + if(in!=null) in.close(); + if(out!=null) out.close(); + } + } + }// while end + } catch (Exception e) { + throw e; + }finally { + if(zipFile != null) zipFile.close(); + } + logger.debug("unZip end……"); + } + + /** + * 将单个文件或一个目录打成zip包 + * + * @param srcDir 需要压压缩的文件目录 + * @param destFile 压缩后的目标文件 + * @throws Exception + */ + public static void zip(String srcDir, String destFile, String[] excludes) throws Exception { + ZipOutputStream out = null; + try{ + out = new ZipOutputStream(new FileOutputStream(destFile)); + //out.setEncoding("GBK");//解决linux乱码 + File srcFile = new File(srcDir); + if (!srcFile.exists()) { +// throw new Exception("压缩目录或文件不存在: " + srcFile.getAbsolutePath()); + throw new Exception("Compressed directories or files do not exist: " + srcFile.getAbsolutePath()); + } + if(excludes!=null && excludes.length>0){ + for (int i=0; i0){ + for(String exclude : excludes){ + if(exclude.equalsIgnoreCase(srcFile.getCanonicalPath())){ + return; + } + } + } + + if (srcFile.isDirectory()) { + File[] files = srcFile.listFiles(); + base = base.length() == 0 ? "" : base + "/"; + if (base.length() > 0) { + out.putNextEntry(new ZipEntry(base)); + /* ZipEntry zipEntry = new ZipEntry(base); + zipEntry.setUnixMode(755);// 解决linux乱码 + out.putNextEntry(zipEntry);*/ + } + for (int i = 0; i < files.length; i++) { + zip(out, files[i], base + files[i].getName(), excludes); + } + } else { + base = base.length() == 0 ? srcFile.getName() : base; + out.putNextEntry(new ZipEntry(base)); + /*ZipEntry zipEntry=new ZipEntry(base); + zipEntry.setUnixMode(644);//解决linux乱码 + out.putNextEntry(zipEntry);*/ + FileInputStream fis = null; + try{ + fis = new FileInputStream(srcFile); + IOUtils.copy(fis, out); + }catch (IOException e) { + throw e; + }finally{ + if(fis!=null) fis.close(); + } + } + out.closeEntry(); + } + + /** + * 将单个文件或一个目录打成zip包,并将原文件删除 + * + * @param srcFiles 需要压压缩的文件列表 + * @param destFile 压缩后的目标文件 + * @param isAddPrefix 是否要添加文件前缀 + * @throws Exception + */ + public static void zipWithDelFile(File[] srcFiles, String destFile, boolean isAddPrefix) throws Exception { + ZipOutputStream out = null; + try{ + out = new ZipOutputStream(new FileOutputStream(destFile)); + //out.setEncoding("GBK");//解决linux乱码 + if (srcFiles==null || srcFiles.length==0) { +// throw new Exception("压缩文件列表为空"); + throw new Exception("The list of compressed files is empty"); + } + for(File file : srcFiles){ + zip(out,file,"", true, isAddPrefix); + } + }catch (Exception e) { + throw e; + }finally { + if(out!=null) out.close(); + } + } + + /** + * zip压缩 + * @param out: Zip压缩流 + * @param srcFile: 要压缩的文件或目录 + * @param base: 要压缩文件在zip包内的路径 + * @param isDel 是否删除压缩的文件 + * @param isAddPrefix 是否要添加文件前缀,true 如果是文件则将父文件夹的名称加到文件名的前缀,作为压缩后的文件名 + * @throws Exception + */ + public static void zip(ZipOutputStream out, File srcFile, String base, boolean isDel, boolean isAddPrefix) throws Exception{ + if (!srcFile.exists()) { +// throw new Exception("压缩目录或文件不存在: " + srcFile.getAbsolutePath()); + throw new Exception("Compressed directories or files do not exist: " + srcFile.getAbsolutePath()); + } + + if (srcFile.isDirectory()) { + File[] files = srcFile.listFiles(); + base = base.length() == 0 ? "" : base + "/"; + if (base.length() > 0) { + out.putNextEntry(new ZipEntry(base)); + /* ZipEntry zipEntry = new ZipEntry(base); + zipEntry.setUnixMode(755);// 解决linux乱码 + out.putNextEntry(zipEntry);*/ + } + for (int i = 0; i < files.length; i++) { + String fileName = files[i].getName(); + if(isAddPrefix){ + fileName = files[i].getParentFile().getName() + "_" + files[i].getName(); + } + zip(out, files[i], base + fileName,isDel, isAddPrefix); + } + } else{ + base = base.length() == 0 ? srcFile.getName() : base; + out.putNextEntry(new ZipEntry(base)); + /*ZipEntry zipEntry=new ZipEntry(base); + zipEntry.setUnixMode(644);//解决linux乱码 + out.putNextEntry(zipEntry);*/ + FileInputStream fis = null; + try{ + fis = new FileInputStream(srcFile); + IOUtils.copy(fis, out); + }catch (IOException e) { + throw e; + }finally{ + if(fis!=null) fis.close(); + } + } + if(srcFile.exists() && isDel){ + srcFile.delete(); + } + out.closeEntry(); + } + +// public static void main(String[] args) { +// try { +// long tt = System.currentTimeMillis(); + /*String[] excludes = new String[3]; + excludes[0] = "D:\\temp\\t1\\test\\src"; + excludes[1] = "D:\\temp\\t1\\test\\WebRoot\\WEB-INF\\lib\\jdom.jar"; + excludes[2] = "D:\\temp\\t1\\test\\ziptest_code.zip"; + ZipUtil.zip("D:\\temp\\t1", "D:\\temp\\test\\t1.zip", excludes); + //ZipUtil.unZip("D:\\temp\\test\\ziptest_code.zip", "D:\\temp"); + System.out.println(System.currentTimeMillis()-tt);*/ +// ZipUtil.zipWithDelFile(new File("D:\\nmstest\\logs\\").listFiles(), "D:\\test\\nmsserver.log.zip", true); +// ZipUtil.unZip("D:\\NMSTrunk\\upgrade\\windows\\DC_Update_0304.zip","D:\\NMSTrunk\\upgrade\\windows\\DC_Update_0304" , tt+""); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +} diff --git a/src/com/nms/server/util/file/BufferedRandomAccessFile.java b/src/com/nms/server/util/file/BufferedRandomAccessFile.java new file mode 100644 index 0000000..516dbfa --- /dev/null +++ b/src/com/nms/server/util/file/BufferedRandomAccessFile.java @@ -0,0 +1,308 @@ +package com.nms.server.util.file; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ResourceBundle; + +/** + *

Title: BufferedRandomAccessFile

+ *

Description: this class provide Buffered Read & Write by extend RandomAccessFile

+ */ + +public class BufferedRandomAccessFile extends RandomAccessFile { + static ResourceBundle res = ResourceBundle.getBundle(Res.class.getName()); + private static final int DEFAULT_BUFFER_BIT_LEN = 10; + private static final int DEFAULT_BUFFER_SIZE = 1 << DEFAULT_BUFFER_BIT_LEN; + + protected byte buf[]; + protected int bufbitlen; + protected int bufsize; + protected long bufmask; + protected boolean bufdirty; + protected int bufusedsize; + protected long curpos; + + protected long bufstartpos; + protected long bufendpos; + protected long fileendpos; + + protected boolean append; + protected String filename; + protected long initfilelen; + + public BufferedRandomAccessFile(String name) throws IOException { + this(name, res.getString("r"), DEFAULT_BUFFER_BIT_LEN); + } + + public BufferedRandomAccessFile(File file) throws IOException, FileNotFoundException { + this(file.getPath(), res.getString("r"), DEFAULT_BUFFER_BIT_LEN); + } + + public BufferedRandomAccessFile(String name, int bufbitlen) throws IOException { + this(name, res.getString("r"), bufbitlen); + } + + public BufferedRandomAccessFile(File file, int bufbitlen) throws IOException, FileNotFoundException { + this(file.getPath(), res.getString("r"), bufbitlen); + } + + public BufferedRandomAccessFile(String name, String mode) throws IOException { + this(name, mode, DEFAULT_BUFFER_BIT_LEN); + } + + public BufferedRandomAccessFile(File file, String mode) throws IOException, FileNotFoundException { + this(file.getPath(), mode, DEFAULT_BUFFER_BIT_LEN); + } + + public BufferedRandomAccessFile(String name, String mode, int bufbitlen) throws IOException { + super(name, mode); + this.init(name, mode, bufbitlen); + } + + public BufferedRandomAccessFile(File file, String mode, int bufbitlen) throws IOException, FileNotFoundException { + this(file.getPath(), mode, bufbitlen); + } + + private void init(String name, String mode, int bufbitlen) throws IOException { + if (mode.equals(res.getString("r")) == true) { + this.append = false; + } else { + this.append = true; + } + + this.filename = name; + this.initfilelen = super.length(); + this.fileendpos = this.initfilelen - 1; + this.curpos = super.getFilePointer(); + + if (bufbitlen < 0) { + throw new IllegalArgumentException(res.getString("bufbitlen_size_must_0")); + } + + this.bufbitlen = bufbitlen; + this.bufsize = 1 << bufbitlen; + this.buf = new byte[this.bufsize]; + this.bufmask = ~((long)this.bufsize - 1L); + this.bufdirty = false; + this.bufusedsize = 0; + this.bufstartpos = -1; + this.bufendpos = -1; + } + + private void flushbuf() throws IOException { + if (this.bufdirty == true) { + if (super.getFilePointer() != this.bufstartpos) { + super.seek(this.bufstartpos); + } + super.write(this.buf, 0, this.bufusedsize); + this.bufdirty = false; + } + } + + private int fillbuf() throws IOException { + super.seek(this.bufstartpos); + this.bufdirty = false; + return super.read(this.buf); + } + + public byte read(long pos) throws IOException { + if (pos < this.bufstartpos || pos > this.bufendpos) { + this.flushbuf(); + this.seek(pos); + + if ((pos < this.bufstartpos) || (pos > this.bufendpos)) { + throw new IOException(); + } + } + this.curpos = pos; + return this.buf[(int)(pos - this.bufstartpos)]; + } + + public boolean write(byte bw) throws IOException { + return this.write(bw, this.curpos); + } + + public boolean append(byte bw) throws IOException { + return this.write(bw, this.fileendpos + 1); + } + + public boolean write(byte bw, long pos) throws IOException { + + if ((pos >= this.bufstartpos) && (pos <= this.bufendpos)) { // write pos in buf + this.buf[(int)(pos - this.bufstartpos)] = bw; + this.bufdirty = true; + + if (pos == this.fileendpos + 1) { // write pos is append pos + this.fileendpos++; + this.bufusedsize++; + } + } else { // write pos not in buf + this.seek(pos); + + if ((pos >= 0) && (pos <= this.fileendpos) && (this.fileendpos != 0)) { // write pos is modify file + this.buf[(int)(pos - this.bufstartpos)] = bw; + + } else if (((pos == 0) && (this.fileendpos == 0)) || (pos == this.fileendpos + 1)) { // write pos is append pos + this.buf[0] = bw; + this.fileendpos++; + this.bufusedsize = 1; + } else { + throw new IndexOutOfBoundsException(); + } + this.bufdirty = true; + } + this.curpos = pos; + return true; + } + + public void write(byte b[], int off, int len) throws IOException { + + long writeendpos = this.curpos + len - 1; + + if (writeendpos <= this.bufendpos) { // b[] in cur buf + System.arraycopy(b, off, this.buf, (int)(this.curpos - this.bufstartpos), len); + this.bufdirty = true; + this.bufusedsize = (int)(writeendpos - this.bufstartpos + 1);//(int)(this.curpos - this.bufstartpos + len - 1); + + } else { // b[] not in cur buf + super.seek(this.curpos); + super.write(b, off, len); + } + + if (writeendpos > this.fileendpos) + this.fileendpos = writeendpos; + + this.seek(writeendpos+1); + } + + public int read(byte b[], int off, int len) throws IOException { + + long readendpos = this.curpos + len - 1; + + if (readendpos <= this.bufendpos && readendpos <= this.fileendpos ) { // read in buf + System.arraycopy(this.buf, (int)(this.curpos - this.bufstartpos), b, off, len); + } else { // read b[] size > buf[] + + if (readendpos > this.fileendpos) { // read b[] part in file + len = (int)(this.length() - this.curpos + 1); + } + + super.seek(this.curpos); + len = super.read(b, off, len); + readendpos = this.curpos + len - 1; + } + this.seek(readendpos + 1); + return len; + } + + public void write(byte b[]) throws IOException { + this.write(b, 0, b.length); + } + + public int read(byte b[]) throws IOException { + return this.read(b, 0, b.length); + } + + public void seek(long pos) throws IOException { + + if ((pos < this.bufstartpos) || (pos > this.bufendpos)) { // seek pos not in buf + this.flushbuf(); + + if ((pos >= 0) && (pos <= this.fileendpos) && (this.fileendpos != 0)) { // seek pos in file (file length > 0) + this.bufstartpos = pos & this.bufmask; + this.bufusedsize = this.fillbuf(); + + } else if (((pos == 0) && (this.fileendpos == 0)) || (pos == this.fileendpos + 1)) { // seek pos is append pos + + this.bufstartpos = pos; + this.bufusedsize = 0; + } + this.bufendpos = this.bufstartpos + this.bufsize - 1; + } + this.curpos = pos; + } + + public long length() throws IOException { + return this.max(this.fileendpos + 1, this.initfilelen); + } + + public void setLength(long newLength) throws IOException { + if (newLength > 0) { + this.fileendpos = newLength - 1; + } else { + this.fileendpos = 0; + } + super.setLength(newLength); + } + public long getFilePointer() throws IOException { + return this.curpos; + } + + private long max(long a, long b) { + if (a > b) return a; + return b; + } + + public void close() throws IOException { + this.flushbuf(); + super.close(); + } + + public static void main(String[] args) throws IOException { + long readfilelen = 0; + BufferedRandomAccessFile brafReadFile, brafWriteFile; + + brafReadFile = new BufferedRandomAccessFile("C:\\WINNT\\Fonts\\STKAITI.TTF"); + readfilelen = brafReadFile.initfilelen; + brafWriteFile = new BufferedRandomAccessFile(".\\STKAITI.001", "rw", 10); + + byte buf[] = new byte[1024]; + int readcount; + + long start = System.currentTimeMillis(); + + while((readcount = brafReadFile.read(buf)) != -1) { + brafWriteFile.write(buf, 0, readcount); + } + + brafWriteFile.close(); + brafReadFile.close(); + + System.out.println("BufferedRandomAccessFile Copy & Write File: " + + brafReadFile.filename + + " FileSize: " + + java.lang.Integer.toString((int)readfilelen >> 1024) + + " (KB) " + + "Spend: " + +(double)(System.currentTimeMillis()-start) / 1000 + + "(s)"); + + java.io.FileInputStream fdin = new java.io.FileInputStream("C:\\WINNT\\Fonts\\STKAITI.TTF"); + java.io.BufferedInputStream bis = new java.io.BufferedInputStream(fdin, 1024); + java.io.DataInputStream dis = new java.io.DataInputStream(bis); + + java.io.FileOutputStream fdout = new java.io.FileOutputStream(".\\STKAITI.002"); + java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(fdout, 1024); + java.io.DataOutputStream dos = new java.io.DataOutputStream(bos); + + start = System.currentTimeMillis(); + + for (int i = 0; i < readfilelen; i++) { + dos.write(dis.readByte()); + } + + dos.close(); + dis.close(); + + System.out.println("DataBufferedios Copy & Write File: " + + brafReadFile.filename + + " FileSize: " + + java.lang.Integer.toString((int)readfilelen >> 1024) + + " (KB) " + + "Spend: " + + (double)(System.currentTimeMillis()-start) / 1000 + + "(s)"); + } +} \ No newline at end of file diff --git a/src/com/nms/server/util/file/Res.java b/src/com/nms/server/util/file/Res.java new file mode 100644 index 0000000..395e08e --- /dev/null +++ b/src/com/nms/server/util/file/Res.java @@ -0,0 +1,17 @@ +package com.nms.server.util.file; + +public class Res extends java.util.ListResourceBundle { + static final Object[][] contents = new String[][]{ + { "r", "r" }, + { "bufbitlen_size_must_0", "bufbitlen size must >= 0" }, + { "rw", "rw" }, + { "BufferedRandomAccess", "BufferedRandomAccessFile Copy & Write File: " }, + { "FileSize_", " FileSize: " }, + { "_KB_", " (KB) " }, + { "Spend_", "Spend: " }, + { "_s_", "(s)" }, + { "DataBufferedios_Copy", "DataBufferedios Copy & Write File: " }}; + public Object[][] getContents() { + return contents; + } +} \ No newline at end of file diff --git a/src/com/nms/server/util/log4j/MyDailyRollingFileAppender.java b/src/com/nms/server/util/log4j/MyDailyRollingFileAppender.java new file mode 100644 index 0000000..6903e30 --- /dev/null +++ b/src/com/nms/server/util/log4j/MyDailyRollingFileAppender.java @@ -0,0 +1,14 @@ +package com.nms.server.util.log4j; + +import org.apache.log4j.DailyRollingFileAppender; +import org.apache.log4j.Priority; + +public class MyDailyRollingFileAppender extends DailyRollingFileAppender { + + @Override + public boolean isAsSevereAsThreshold(Priority priority) { + // 只判断相等,不判断优先级 + return this.getThreshold().equals(priority); + } + +} diff --git a/src/com/nms/server/util/log4j/MyRollingFileAppender.java b/src/com/nms/server/util/log4j/MyRollingFileAppender.java new file mode 100644 index 0000000..17e587f --- /dev/null +++ b/src/com/nms/server/util/log4j/MyRollingFileAppender.java @@ -0,0 +1,14 @@ +package com.nms.server.util.log4j; + +import org.apache.log4j.Priority; +import org.apache.log4j.RollingFileAppender; + +public class MyRollingFileAppender extends RollingFileAppender { + + @Override + public boolean isAsSevereAsThreshold(Priority priority) { + // 只判断相等,不判断优先级 + return this.getThreshold().equals(priority); + } + +} diff --git a/src/com/nms/server/util/socket/SSLSocketCallable.java b/src/com/nms/server/util/socket/SSLSocketCallable.java new file mode 100644 index 0000000..ef72a34 --- /dev/null +++ b/src/com/nms/server/util/socket/SSLSocketCallable.java @@ -0,0 +1,101 @@ +package com.nms.server.util.socket; + +import java.io.IOException; +import java.net.Socket; +import java.net.SocketException; +import java.util.Calendar; +import java.util.concurrent.Callable; + +import javax.net.ssl.SSLServerSocket; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Common; +import com.nms.server.common.Constants; + +/** + * SSL 通讯 工具类 + * @date Feb 29, 2012 10:05:50 AM + * @author ZhangGang + * + */ +public abstract class SSLSocketCallable extends SocketUtils implements Callable{ + + private static Logger logger = Logger.getLogger(SSLSocketCallable.class); + //ServerSocket 通讯服务 + protected SSLServerSocket ss = null; + + /** + * 通讯创建 + * @param ip 目标主机IP + * @param port 目标主机端口 + * @throws Exception + */ + public SSLSocketCallable(String ip,Integer port){ + super(ip, port); + } + + /** + * 通讯创建 + * @param client 目标通讯实例 + */ + public SSLSocketCallable(Socket client) { + super(client); + } + + /** + * 通讯线程执行方法 默认格式 + * @return + * @throws Exception + */ + public Object call(){ + Thread.currentThread().setName("Create communication"+ip+":"+port); + Object obj = null; //返回对象 + + try { + + //- 校验 是否创建新通讯连接 + if(socket==null){ //客户端通讯创建 + createClientSocket(); + }else{ //服务端通讯创建 + createServerSocket(); + } + + } catch (SocketException e) { + logger.error("Create failed:"+e.getMessage(),e); + Common.addErrorInfo(Constants.ERROR_CODE_CREATE_SOCKET,ip, Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,""); + close(); + return obj; + } catch (IOException e) { + close(); + return obj; + }catch (Exception e) { + close(); + return obj; + } + + //-- 自定义通讯操作 + try { + obj = toDo(); + }catch (Exception e) { + logger.error("Communicate contents exception"+e.getMessage(),e); + Common.addErrorInfo(Constants.ERROR_CODE_SOCKET_RUNTIME,ip, Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,""); + } finally { + close(); + } + + return obj; + } + + /** + * 待实现的通信操作 + * @time Aug 28, 2011-9:04:46 PM + * @param outl + * @param inl + * @throws Exception + */ + + protected abstract Object toDo()throws Exception; + + +} diff --git a/src/com/nms/server/util/socket/SSLSocketCallableForDateCollection.java b/src/com/nms/server/util/socket/SSLSocketCallableForDateCollection.java new file mode 100644 index 0000000..56ef7af --- /dev/null +++ b/src/com/nms/server/util/socket/SSLSocketCallableForDateCollection.java @@ -0,0 +1,121 @@ +package com.nms.server.util.socket; + +import java.io.IOException; +import java.net.Socket; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Calendar; +import java.util.concurrent.Callable; +import javax.net.ssl.SSLServerSocket; +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; +import com.nms.server.bean.DetectInfo; +import com.nms.server.common.Common; +import com.nms.server.common.Constants; +import com.nms.server.thread.monitor.util.MonitorUtil; +import com.nms.server.util.ExceptionPrintUtils; + +/** + * SSL 通讯 工具类 + * @date Feb 29, 2012 10:05:50 AM + * @author ZhangGang + * + */ +public abstract class SSLSocketCallableForDateCollection extends SocketUtils implements Callable{ + + private static Logger logger = Logger.getLogger(SSLSocketCallable.class); + //ServerSocket 通讯服务 + protected SSLServerSocket ss = null; + + /** + * 通讯创建 + * @param ip 目标主机IP + * @param port 目标主机端口 + * @throws Exception + */ + public SSLSocketCallableForDateCollection(String ip,Integer port){ + super(ip, port); + } + + /** + * 通讯创建 + * @param client 目标通讯实例 + */ + public SSLSocketCallableForDateCollection(Socket client) { + super(client); + } + + /** + * 通讯线程执行方法 默认格式 + * @return + * @throws Exception + */ + public Object call(){ + Thread.currentThread().setName("Create communication"+ip+":"+port); + Object obj = null; //返回对象 + Long checkTime = System.currentTimeMillis(); //用于产生数据中的开始时间 + try { + //- 校验 是否创建新通讯连接 + if(socket==null){ //客户端通讯创建 + createClientSocket(); + }else{ //服务端通讯创建 + createServerSocket(); + } + + } catch (SocketException e) { + logger.error("Create communication exception", e); + Common.addErrorInfo(Constants.ERROR_CODE_CREATE_SOCKET,ip, Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,""); +// obj = errorNodeToDo(checkTime); + close(); + return obj; + } catch (IOException e) { + logger.error("Create communication exception", e); + close(); +// obj = errorNodeToDo(checkTime); + return obj; + }catch (InterruptedException e) { + logger.error("Create communication exception,thread is interrupted",e);//线程被中断,不再进行握手失败具体原因的判断 + return null; + } catch (Exception e) { + logger.error("Create communication exception", e); + close(); +// obj = errorNodeToDo(checkTime); + return obj; + } + + //-- 自定义通讯操作 + try { + obj = toDo(); + }catch (Exception e) { + logger.error("Communicate content exception"+e.getMessage(),e); + Common.addErrorInfo(Constants.ERROR_CODE_SOCKET_RUNTIME,ip, Calendar.getInstance().getTime(), Constants.ERROR_INFO_SATAE_ERROR,""); + } finally { + close(); + } + + return obj; + } + +// //创建握手监测信息 +// protected abstract void createNmsClientDetectData(DetectInfo detectInfo); + + + /** + * 待实现的通信操作 + * @time Aug 28, 2011-9:04:46 PM + * @param outl + * @param inl + * @throws Exception + */ + + protected abstract Object toDo()throws Exception; + + /** + * 节点连接出现异常 + * @return + * @throws Exception + */ +// protected abstract Object errorNodeToDo(Long checkTime); + + +} diff --git a/src/com/nms/server/util/socket/SocketUtils.java b/src/com/nms/server/util/socket/SocketUtils.java new file mode 100644 index 0000000..642a131 --- /dev/null +++ b/src/com/nms/server/util/socket/SocketUtils.java @@ -0,0 +1,1604 @@ +package com.nms.server.util.socket; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.Socket; +import java.net.SocketException; +import java.net.URL; +import java.net.UnknownHostException; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.LinkedList; +import java.util.List; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManagerFactory; + +import org.apache.commons.lang.StringUtils; +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; +import com.nms.server.util.FileUtils; +import com.nms.server.util.MD5Util; +import com.nms.server.util.file.BufferedRandomAccessFile; +import com.nms.server.util.socket.limiter.InputStreamLimiter; +import com.nms.server.util.socket.limiter.OutputStreamLimiter; +import com.socket.utils.FileComment; + +/** + * SSL 通讯 工具类 + * @date Feb 29, 2012 10:05:50 AM + * @author ZhangGang + * + */ +public abstract class SocketUtils{ + + //文件传输 临时文件命名后缀 + private static final String TEMP_SUFFIX = ".tp"; + + //SSLContext 重置标识 + private static boolean resetSSLContextFlag = false; + + //通讯编码方式 + private static final String DEFAULT_ENCODING = Constants.COMMON_TEXT_CODING; + + //公有密匙库 存放公钥(与其他主机加密通讯对应的密匙) + private static final String SERVER_TRUST = Constants.SYSTEM_PATH + File.separator + Constants.SSL_DIR + File.separator + Constants.SSL_SERVER_TRUST; + + //私有密匙库 存放私钥(向其他主机发布信息的使用的加密密匙) + private static final String SERVER_STORE = Constants.SYSTEM_PATH + File.separator + Constants.SSL_DIR + File.separator + Constants.SSL_SERVER_STORE; + + //密匙库 类型 + private static final String KEYSTORE_TYPE = Constants.SSL_KEYSTORE_TYPE; + + //私有密匙库 密码 + private static final String SERVER_STORE_PSW = Constants.SSL_SERVER_STORE_PSW; + + //公有密匙库 密码 + private static final String SERVER_TRUST_PSW = Constants.SSL_SERVER_TRUST_PSW; + + //私有密匙 密码 + private static final String SERVER_KEY_PSW = Constants.SSL_SERVER_KEY_NEW_PSW; + + //加密上下位 类型 + private static final String SSL_CONTEXT_TYPE = Constants.SSL_TYPE; + + //日志组件对象 + private static Logger logger = Logger.getLogger(SocketUtils.class); + + //Socket 通讯 + protected Socket socket = null; //Socket + +// //字节输出流 +// protected OutputStream outl = null; +// +// //字节输入流 +// protected InputStream inl = null; //读取字符流 + + //字节输出流 + protected InputStreamLimiter inl = null; + + //字节输入流 + protected OutputStreamLimiter outl = null; //读取字符流 + + //通讯目标主机IP + protected String ip = null; + + //通讯目标主机端口 + protected Integer port = null ; + + //SSL通讯上下文对象 + protected static SSLContext sSLContext = getSSLContext(); + + //缓存字节长度 + public static final int BUFF_SIZE = 1024; + + /** + * 通讯正常标识 + */ + public static final String SUCCESS ="success"; //通信操作正常 + + /** + * 通讯异常或终止标识 + */ + protected static final String FAIL ="fail"; //通信操作异常或终止 + + /** + * 通讯创建 + * @param ip 目标主机IP + * @param port 目标主机端口 + * @throws Exception + */ + public SocketUtils(String ip,Integer port){ + logger.info("客户端通讯建立 TO:> "+ip); + this.ip = ip; + this.port = port; + } + + /** + * 通讯创建 + * @param client 目标通讯实例 + */ + public SocketUtils(Socket client) { + socket = client; + logger.info("服务端通讯建立 From:> "+socket.getInetAddress().getHostAddress()); + } + + /** + * 创建请求端通讯对象 + * 创建通讯对象、创建输入输出流、设置参数 + * @time Mar 22, 2013-2:58:52 PM + * @throws UnknownHostException + * @throws IOException + */ + protected void createClientSocket() throws UnknownHostException,SocketException,IOException,InterruptedException{ + + //- 校验 是否创建新通讯连接 + if(StringUtils.isEmpty(ip) || port == null){ + logger.warn("Lack of communication parameters:IP【"+ip+"】 PORT【"+port+"】"); + return ; + } + + //-- create SocketFactory + SSLSocketFactory ssf = sSLContext.getSocketFactory(); + + //-- create socket + socket=(SSLSocket)ssf.createSocket(ip,port); + logger.info("createClientSocket创建成功"); + + //通讯参数 + initSocketParams(); + + //-- handshake 握手 + ((SSLSocket) socket).startHandshake(); + logger.info("createClientSocket握手成功"); + + //-- 获取通讯IO流 + outl = new OutputStreamLimiter(socket.getOutputStream()); + inl = new InputStreamLimiter(socket.getInputStream()); + + } + + /** + * 创建服务端通讯 + * 即创建输入输出流对象并设置参数 + * @time Mar 22, 2013-2:58:24 PM + * @throws UnknownHostException + * @throws IOException + */ + protected void createServerSocket() throws UnknownHostException,SocketException,IOException,InterruptedException{ + + //- 校验 是否创建新通讯连接 + if(socket==null || socket.isClosed()){ + logger.warn("The server-side communication state is unable to complete the communication"); + return ; + } + + //-- 获取通讯IO流 + outl = new OutputStreamLimiter(socket.getOutputStream()); + inl = new InputStreamLimiter(socket.getInputStream()); + + //通讯参数 + initSocketParams(); + } + + /** + * 设置公用基本通讯参数 + * @time Mar 22, 2013-2:58:06 PM + * @throws SocketException + */ + private void initSocketParams() throws SocketException{ + //设置超时时间 + socket.setSoTimeout(Constants.SSL_SO_TIMEOUT*1000); + logger.info("设置socket超时时间:"+Constants.SSL_SO_TIMEOUT+"秒"); + } + + /** + * 字符流 接收信息 + **/ + protected void sendMessage(String msg) throws UnsupportedEncodingException { + + PrintWriter pw = new PrintWriter(new OutputStreamWriter(outl,DEFAULT_ENCODING)); + pw.println(msg); + pw.flush(); + logger.debug("发送命令("+ip+"):"+msg); + + } + + /** + * 字符流 发送信息 + */ + protected String receiveMessage()throws UnsupportedEncodingException ,IOException{ + + BufferedReader br = new BufferedReader(new InputStreamReader(inl,DEFAULT_ENCODING)); + String recvStr = br.readLine(); + logger.debug("接收命令("+ip+"):"+recvStr); + return recvStr; + + } + + /** + * Object 形式 发送信息 + */ + protected void sendObject(Object object) throws IOException{ + + ObjectOutputStream oos = new ObjectOutputStream(outl); + oos.writeObject(object); + oos.flush(); + + } + + /** + * Object 形式 接收信息 + */ + protected Object receiveObject() throws ClassNotFoundException,IOException{ + + ObjectInputStream ois = new ObjectInputStream(inl); + return ois.readObject(); + } + + /** + * 字节流 发送单个文件 + **/ + public void sendFile(File file) throws IOException{ + + if(file==null ||file.length()==0){ + return; + } + + FileInputStream fis = null; + + try { + + //发送文件大小 + sendMessage(file.length() + ""); + + //发送文件内容 + int len; + byte[] buff = new byte[BUFF_SIZE]; + fis = new FileInputStream(file); + + while ((len = fis.read(buff)) != -1) { + + //将读取的内容写入文件 + outl.write(buff, 0, len); + + } + + outl.flush(); +// } catch (IOException e) { +// logger.error("单个发送文件失败!\n"+ExceptionPrintUtils.printExceptionStack(e)); + } finally{ + if(fis!=null){ + //try { + fis.close(); + fis=null; + //} catch (IOException e) { + //logger.error("",e); + //} + } + } + } + + + + /** + * 字节流 接收单个文件 并保存 + */ + protected void receiveFile(String filePath) throws IOException{ + + FileOutputStream fos = null; + + File file = new File(filePath); + if(!file.getParentFile().exists()){ + file.getParentFile().mkdirs(); + } + + if(!file.exists()){ + file.createNewFile(); + } + + try { + + //接收文件大小 + long fileSize = Long.parseLong(receiveMessage()); + + //接收文件内容 + byte[] buff = new byte[BUFF_SIZE]; + fos = new FileOutputStream(filePath); + int nRead = 0; + + //单个文件循环读取 + rfile:while ((nRead = inl.read(buff, 0, (int)(BUFF_SIZE 0) { + fos.write(buff,0,nRead); + fos.flush(); + fileSize -= nRead; + if(fileSize<=0){ + break rfile; + } + } + fos.close(); + +// } catch (IOException e) { +// logger.error("接收文件失败!",e); + }finally{ + if(fos!=null){ + //try { + fos.close(); + fos = null; + //} catch (IOException e) { + // logger.error("",e); + //} + } + } + } + + /** + * 字节流接收文件,文件已临时命名形式接收 (已废弃) + * (DC未使用) + */ + protected TempFile receiveTempFile(String filePath) { + ObjectInputStream ois = null; + FileOutputStream fos = null; + TempFile tempFile = new TempFile(); + try { + ois = new ObjectInputStream(inl); + String[] fileInfo = (String[])ois.readObject(); + //接收文件大小 + long fileSize = Long.parseLong(fileInfo[0]); + tempFile.setFileName(fileInfo[1]); + String tempName = addTimeTagForFileName("temp.tmp",false); +// int buffSize = 1024; +// int counts = (int) ((fileSize % BUFF_SIZE==0)?(fileSize / BUFF_SIZE):(fileSize / BUFF_SIZE + 1)); + //接收文件内容 + byte[] buff = new byte[BUFF_SIZE]; +// int len = 0; + fos = new FileOutputStream(filePath+File.separator+tempName); + int nRead = 0; + + //单个文件循环读取 + rfile:while ((nRead = inl.read(buff, 0, (int)(BUFF_SIZE 0) { + fos.write(buff,0,nRead); + fos.flush(); + fileSize -= nRead; + if(fileSize<=0){ + break rfile; + } + } + fos.close(); + + + /*for (int i = 0; i < counts; i++) { + len = inl.read(buff); + //读联欢数据 + if (len == 1 && buff[0] == TranTool.DATA_END)//数据结束标志 + break; + //将读取的内容写入文件 + fos.write(buff, 0, len); + fos.flush(); + } + fos.close();*/ + File file0 = new File(filePath+File.separator+tempName); + tempName = addTimeTagForFileName("temp.ok",false); + File file = new File(filePath+File.separator+tempName); +// file0.renameTo(file); + FileUtils.copyFile(file0, file); //将临时文件名改为正式文件名,即去掉.tp后缀 + file0.delete(); + tempFile.setFile(file); + } catch (IOException e) { + logger.error("Failed to receive file!",e); + } catch (ClassNotFoundException e) { + logger.error("",e); + }finally{ + if(fos!=null){ + try { + fos.close(); + } catch (IOException e) { + logger.error("",e); + } + } + } + return tempFile; + } + + /** + * 批量上传文件 + * @param dir 本地文件集合根目录绝对路径 + * @param fileList 上传的文件列表 + * (DC未使用) + */ + protected void sendFileByBath(String dir, List fileList) throws IOException{ + ObjectOutputStream oos = null; + FileInputStream fis = null; + + try { + // 第一步发送本地根目录地址(用于地址截取)保证fileList的目录结构完整性 + this.sendMessage(dir); + String result = this.receiveMessage(); + logger.debug("根目录路径通信状态: " + result); + // 第二步 用ObjectOutputStream工具类 发送file对象信息 用于文件名,文件目录,文件大小的获取 + oos = new ObjectOutputStream(outl); + List fileStrList = new ArrayList(); + for(File f : fileList){ + String[] tmpArr = new String[]{ + f.getAbsolutePath(), f.length() + "" + }; + fileStrList.add(tmpArr); + } + oos.writeObject(fileStrList); + // 第三部,发送文件 + byte[] buff = new byte[BUFF_SIZE]; + int len = 0; + // 循环上传文件 + for (File file : fileList) { + fis = new FileInputStream(file); + while ((len = fis.read(buff)) != -1) {// 将读取的内容输出流 + outl.write(buff, 0, len); + } + outl.flush(); + fis.close(); + fis = null; + } + logger.debug("多文件上传结束,共 "+(fileList==null ? 0 : fileList.size())+ "个文件"); +// } catch (IOException e) { +// logger.error("文件发送失败!",e); + } finally { +// try { + if (fis != null) { + fis.close(); + fis = null; + } +// } catch (IOException e) { +// logger.error("",e); +// } + } + } + + /** + * 批量接收文件 + * @param newDir + * (DC未使用) + */ + protected void receiveFileByBath(String newDir) throws IOException,ClassNotFoundException{ + ObjectInputStream ois = null; + FileOutputStream fos = null; + try { + //获取集合文件路径 + String oldDir = this.receiveMessage(); + logger.debug("根目录 记录: " + oldDir+" VS "+newDir); + this.sendMessage(SUCCESS); + ois = new ObjectInputStream(inl); + List fileList = (List)ois.readObject(); +// byte[] buff = null; + if(fileList != null && fileList.size()>0){ + for(String[] arr : fileList){ + String newUrl = arr[0].replace(oldDir, newDir);//新路径 + newUrl = newUrl.replaceAll("\\\\", "/"); + int fileLength = Integer.parseInt(arr[1]); //大小 + File newFile = new File(newUrl); + if(newFile.exists()){ + newFile.delete(); + } + + if(!newFile.getParentFile().exists()){ + newFile.getParentFile().mkdirs(); + } + + fos = new FileOutputStream(newUrl+TEMP_SUFFIX); + int nRead = 0; + byte[] buff = new byte[BUFF_SIZE]; + + + //单个文件循环读取 + rfile:while ((nRead = inl.read(buff, 0, (int)(BUFF_SIZE 0) { + fos.write(buff,0,nRead); + fos.flush(); + fileLength -= nRead; + if(fileLength<=0){ + break rfile; + } + } + fos.close(); + fos = null; + File newFile2 = new File(newUrl+TEMP_SUFFIX); +// newFile2.renameTo(newFile); //将临时文件名改为正式文件名,即去掉.tp后缀 + FileUtils.copyFile(newFile2, newFile); //将临时文件名改为正式文件名,即去掉.tp后缀 + newFile2.delete(); + } + } + logger.debug("多文件接收结束,共 "+(fileList==null ? 0 : fileList.size())+ "个文件"); +// } catch (IOException e) { +// logger.error("",e); +// } catch (ClassNotFoundException e) { +// logger.error("",e); + }finally{ + if(fos!=null){ +// try { + fos.close(); + fos = null; +// } catch (IOException e) { +// logger.error("",e); +// } + } + } + + } + + /** + * 批量接收文件 保存为 List形式 + * @param newDir + */ + protected LinkedList receiveFileBytesByBath() throws IOException,ClassNotFoundException{ + LinkedList bsList = new LinkedList(); + ObjectInputStream ois = null; +// try { + //获取集合文件路径 + String oldDir = this.receiveMessage(); + this.sendMessage(SUCCESS); + ois = new ObjectInputStream(inl); + List fileList = (List)ois.readObject(); + + //循环读取多个文件 + if(fileList != null && fileList.size()>0){ + for(String[] arr : fileList){ + + int fileLength = Integer.parseInt(arr[1]); //大小 + byte[] buff0 = new byte[fileLength]; + byte[] buff = new byte[BUFF_SIZE]; + int nRead = 0; + int j = 0; + + //单个文件循环读取 + rfile:while ((nRead = inl.read(buff, 0, (int)(BUFF_SIZE 0) { + + //将数据存入集合 + for(int i = 0 ; i < nRead ; i++){ + buff0[j] = buff[i]; + j++; + } + +// logger.debug(j+" "+buff0.length); + + fileLength -= nRead; + if(fileLength<=0){ + break rfile; + } + } + + bsList.add(buff0); + } + } + logger.debug("共接收 "+(fileList==null ? 0 : fileList.size())+ "个文件 存入内存"); +// } catch (IOException e) { +// logger.error("",e); +// bsList.clear(); +// } catch (ClassNotFoundException e) { +// logger.error("",e); +// bsList.clear(); +// }finally{ +// } + return bsList; + } + + /** + * 批量上传文件 + * @param dir 本地文件集合根目录绝对路径 + * @param fileList 上传的文件列表 + * (DC未使用) + */ + protected void sendFileByBathMD5(List fileCommentsList) throws IOException{ + ObjectOutputStream oos = null; + FileInputStream fis = null; + + try { + // 第一步 用ObjectOutputStream工具类 发送file对象信息 用于文件名,文件目录,文件大小的获取 + oos = new ObjectOutputStream(outl); + List fileList = new LinkedList(); + List fileStrList = new ArrayList(); + for(String[] fileComments : fileCommentsList){ + File file = new File(fileComments[0]); + if(file.exists()){ + String[] tmpArr = new String[]{ + removeTimeTagFileName(file.getName()), file.length() + "",fileComments[1] + }; + fileList.add(file); + fileStrList.add(tmpArr); + }else { + logger.warn("-- File:>"+file.getAbsolutePath()+" does not exist, can not send"); + } + } + oos.writeObject(fileStrList); + // 第三部,发送文件 + byte[] buff = new byte[BUFF_SIZE]; + int len = 0; + // 循环上传文件 + for (File file : fileList) { + fis = new FileInputStream(file); + while ((len = fis.read(buff)) != -1) {// 将读取的内容输出流 + outl.write(buff, 0, len); + } + outl.flush(); + fis.close(); + fis = null; + } + logger.debug("多文件上传结束,共 "+(fileList==null ? 0 : fileList.size())+ "个文件"); +// } catch (IOException e) { +// logger.error("",e); + } finally { +// try { + if (fis != null) { + fis.close(); + fis = null; + } +// } catch (IOException e) { +// logger.error("",e); +// } + } + } + + /** + * 批量接收文件, 使用Md5校验文件是否完整 + * @param newDir + * (DC未使用) + */ + protected boolean receiveFileByBathMd5(String newDir) { + boolean flag = true; + ObjectInputStream ois = null; + FileOutputStream fos = null; + try { + ois = new ObjectInputStream(inl); + List fileList = (List)ois.readObject(); + if(fileList != null && fileList.size()>0){ + int sucessCnt = 0; + int failCnt = 0; + for(int i=0; i 0) { + fos.write(buff,0,nRead); + fos.flush(); + fileLength -= nRead; + if(fileLength<=0){ + break rfile; + } + } + fos.close(); + +// int buffSize = 1024; +// logger.debug("buffSize = "+ BUFF_SIZE); +// int counts = (int) ((fileLength % BUFF_SIZE == 0) ? (fileLength / BUFF_SIZE) +// : (fileLength / BUFF_SIZE + 1)); +// byte[] buff = new byte[BUFF_SIZE]; + //接收文件内容 + /*if(fileLength>0){ + for (int k = 0; k < counts; k++) { + len = inl.read(buff); + fos.write(buff, 0, len); + fos.flush(); + } + }else{ + fos.write(buff, 0, len); + fos.flush(); + } + fos.close();*/ + fos = null; + File newFile2 = new File(newUrl+TEMP_SUFFIX); + if (md5Val != null + && md5Val + .equals(MD5Util.getFileMD5String(newFile2))) { + logger.debug("接收文件" + (i+1) + "“" + newFile2.getAbsolutePath() + "”完整"); +// newFile2.renameTo(newFile); //将临时文件名改为正式文件名,即去掉.tp后缀 + FileUtils.copyFile(newFile2, newFile); //将临时文件名改为正式文件名,即去掉.tp后缀 + newFile2.delete(); + sucessCnt ++ ; + } else { + logger.debug("接收文件" + (i+1) + "“" + newFile2.getAbsolutePath() + "”不完整,失败"); + failCnt ++ ; + } + } + logger.info("批理接收文件个数:" + fileList.size() + ", 成功:" + sucessCnt + ", 失败:" + failCnt); + if(failCnt > 0) { + flag = false; + } + }else{ + logger.info("批量接收文件列表为空"); + } + } catch (IOException e) { + logger.error("Batch receiving file failed",e); + return false; + } catch (ClassNotFoundException e) { + logger.error("",e); + return false; + }finally{ + if(fos!=null){ + try { + fos.close(); + fos = null; + } catch (IOException e) { + logger.error("",e); + } + } + } + + return flag; + } + + /** + * 断点续传 发送方法 + * @time Mar 2, 2012-2:30:16 PM + * @param filePath + * (DC未使用) + */ + protected int bpSendFile (String filePath) throws UnsupportedEncodingException,IOException { + File file = new File(filePath); + + //发送长度 end + this.sendMessage(file.length()+""); + + String msg = this.receiveMessage(); + long start = Long.parseLong(msg); + long end = file.length(); +// pl("start "+msg); +// pl("end "+end); + bpSendFile(filePath, start, end); +// pl("bpSendFile sucess"); + return 0; + } + + /** + * 断点续传 接收方法 + * @time Mar 2, 2012-2:30:16 PM + * @param filePath + * @param start + * @param end + */ + protected int bpReceiveFile (String filePath) throws UnsupportedEncodingException,IOException{ + File file = new File(filePath); + if(!file.exists()){ + file = new File(filePath+TEMP_SUFFIX); + } + String msg = this.receiveMessage(); + long start = file.length(); + long end = Long.parseLong(msg); + sendMessage(start+""); + + logger.debug(file.getName()+" start:> "+start); + logger.debug(file.getName()+" end :> "+end); + bpReceiveFile(file.getAbsolutePath(), start, end); + if(file.exists()&& !filePath.equals(file.getName())){ + FileUtils.copyFile(file, new File(filePath)); //将临时文件名改为正式文件名,即去掉.tp后缀 + file.delete(); + } + //发送 + logger.debug(file.getName()+" bpReceiveFile sucess"); + return 0; + } + + /** + * 断点续传 发送方法 + * @time Mar 2, 2012-2:30:16 PM + * @param filePath + * @param start + * @param end + * (DC未使用) + */ + protected void bpSendFile (String filePath,long start,long end) throws IOException{ + if(start == end){ + return ; + } + BufferedRandomAccessFile braf = null; + + try { + + File file = new File(filePath); + + //- 不存在,终止; 存在则继续 + if(!file.exists()){ + this.sendMessage(FAIL); + return ; + }else + this.sendMessage(SUCCESS); + + String msg = this.receiveMessage(); +// pl("Recive: " + msg); + + //- BufferedRandomAccessFile 读取指定位置的文件字节数组,写入输出通讯 + byte[] b = new byte[BUFF_SIZE]; + braf = new BufferedRandomAccessFile(file,"r"); + braf.seek(start); + int nRead; + while ((nRead = braf.read(b, 0, BUFF_SIZE)) > 0) { + + outl.write(b, 0, nRead); + start += nRead; + + //-- 读取完成 跳出 + if(start==end){break;} + + } + +// }catch (IOException e) { +// logger.error("",e); + }finally{ + +// try { + //- 关闭 随机访问文件对象(关闭流) + if (braf != null) { + braf.close(); + braf = null; + } + +// } catch (IOException e) { +// logger.error("",e); +// } + + } + } + + /** + * 断点续传 接收方法 + * @time Mar 2, 2012-2:30:16 PM + * @param filePath + * @param start + * @param end + */ + private void bpReceiveFile (String filePath,long start,long end) throws IOException{ + + BufferedRandomAccessFile raf = null; + + try { + if(StringUtils.isEmpty(filePath)){ + return; + } + + File file = new File(filePath); + + //- 文件路径不存在 则创建 + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + + //- 文件不存在 则创建 + if (!file.exists()) { + file.createNewFile(); + } + + //- 文件已传输完成(包括0字节) + if(start == end){ + return ; + } + + //- 接收发送端 发送数据准备 确认信息 + String msg = this.receiveMessage(); + + if (FAIL.equals(msg)) { //结束操作 + return; + } else + this.sendMessage(SUCCESS); // 通知发送端 接收数据准备完成 确认信息 + + // 将通信中读出的数据 写入文件指定位置 + byte[] b = new byte[BUFF_SIZE]; + raf = new BufferedRandomAccessFile(file, "rw"); + raf.seek(start); + int nRead; + + while ((nRead = inl.read(b, 0, BUFF_SIZE)) > 0) { + + raf.write(b, 0, nRead); + start += nRead; + + if (start == end) { //写完跳出 + break; + } + } + +// } catch (IOException e) { +// logger.error("",e); + }finally{ + +// try { + + //- 关闭 随机访问文件对象(关闭流) + if (raf != null) { + raf.close(); + raf= null; + } + +// } catch (IOException e) { +// logger.error("",e); +// } + } + } + /** + * 批量上传文件 + * @param fileList 上传的文件列表 + * @param dir 本地文件集合根目录绝对路径 + */ + protected void bpSendFileByBath(List fileList,String dir) throws ClassNotFoundException,IOException { + BufferedRandomAccessFile oReadFile = null; + + try { + // 第一步发送本地根目录地址(用于地址截取)保证fileList的目录结构完整性 + this.sendMessage("abs:"+(dir==null?"":dir)); + String result = this.receiveMessage(); + logger.debug("根目录路径通信状态: " + result); + // 第二步 用ObjectOutputStream工具类 发送file对象信息 用于文件名,文件目录,文件大小的获取 + + //原文件文件名 和 大小(即end长度) + List sourceFileList = new ArrayList(); + for(File f : fileList){ + String[] tmpArr = new String[]{ + f.getAbsolutePath(),0+"",f.length() + "" + }; + sourceFileList.add(tmpArr); + } + + logger.debug("发送信息: " + Arrays.toString(sourceFileList.toArray())); + this.sendObject(sourceFileList); + + //得到需要下载的文件信息 + List sendFileList = (List)receiveObject(); + + // 第三部,发送文件 + byte[] buff = new byte[BUFF_SIZE]; + + // 循环上传文件 + for (String[] sendFile: sendFileList) { + long start = Long.parseLong(sendFile[1]); + long end = Long.parseLong(sendFile[2]); + if(start >= end){ + continue; + } + File file = new File(sendFile[0]); + oReadFile = new BufferedRandomAccessFile(file,"r"); + + // 定位文件指针到nPos位置 + oReadFile.seek(start); //从0开始 + int nRead; + + // 从输入流中读入字节流,然后写到文件中 + while ((nRead = oReadFile.read(buff, 0, BUFF_SIZE)) > 0) { + + outl.write(buff, 0, nRead); + start += nRead; //调整为从1开始 + if(start >= end){ + break; + } + + } + oReadFile.close(); + oReadFile = null; + } + logger.debug("多文件上传结束,共 "+(fileList==null ? 0 : fileList.size())+ "个文件"); +// } catch (IOException e) { +// logger.error("",e); +// } catch (ClassNotFoundException e) { +// logger.error("",e); + } finally { +// try { + if (oReadFile != null) { + oReadFile.close(); + oReadFile = null; + } +// } catch (IOException e) { +// logger.error("",e); +// } + } + } + + /** + * 批量上传文件 + * @param fileList 上传的文件列表 + * @param dir 本地文件集合根目录绝对路径 + */ + protected void bpSendFileByBathMD5(List fileCommentsList)throws ClassNotFoundException,IOException { + BufferedRandomAccessFile oReadFile = null; + + try { + //原文件文件名 和 大小(即end长度) + List sourceFileList = new ArrayList(); + if(fileCommentsList !=null && fileCommentsList.size()!=0){ + for(FileComment fileComment : fileCommentsList){ + File f = new File(fileComment.getFileName()); +// pl(f.getName()+" " +f.length()); + if(!f.exists()){ + sourceFileList.add(new FileComment(f.getAbsolutePath(),0,-1,fileComment.getMd5Val())); + }else { + String md5Val = StringUtils.isEmpty(fileComment.getMd5Val())?MD5Util.getFileMD5String(f):fileComment.getMd5Val(); + sourceFileList.add(new FileComment(f.getAbsolutePath(),0,f.length(),md5Val)); + } + } + } + + logger.debug("发送信息: " + Arrays.toString(sourceFileList.toArray())); + this.sendObject(sourceFileList); + + //得到需要下载的文件信息 + List sendFileList = (List)receiveObject(); + + // 第三部,发送文件 + byte[] buff = new byte[BUFF_SIZE]; + + // 循环上传文件 + for (FileComment sendFile: sendFileList) { + long start = sendFile.getStart(); + long end = sendFile.getEnd(); + if(start >= end){ + continue; + } + File file = new File(sendFile.getFileName()); + oReadFile = new BufferedRandomAccessFile(file,"r"); + + // 定位文件指针到nPos位置 + oReadFile.seek(start); //从0开始 + int nRead; + + // 从输入流中读入字节流,然后写到文件中 + while ((nRead = oReadFile.read(buff, 0, BUFF_SIZE)) > 0) { + + outl.write(buff, 0, nRead); + start += nRead; //调整为从1开始 + if(start >= end){ + break; + } + + } + oReadFile.close(); + oReadFile = null; + } + logger.debug("多文件上传结束,共 "+(sendFileList==null ? 0 : sendFileList.size())+ "个文件"); +// } catch (IOException e) { +// logger.error("",e); +// } catch (ClassNotFoundException e) { +// logger.error("",e); + } finally { +// try { + if (oReadFile != null) { + oReadFile.close(); + oReadFile = null; + } +// } catch (IOException e) { +// logger.error("批量接收文件失败",e); +// } + } + } + + /** + * 批量接收文件 + * @param newDir + */ + protected int bpReceiveFileByBathMd5(String newDir) { + int rFlag = 0; //0 OK -1 MD5 ERROR -2 Function ERROR -3 文件不存在 + BufferedRandomAccessFile oSavedFile = null; //有缓存的 随机文件IO对象 + try { + + List remoteFileList = (List)receiveObject(); //接收可接收的文件信息 string[]{fileName,start,end,MD5} + List receiveFileList = new LinkedList(); //需要续传的文件及其索引信息 string[]{fileName,start,end,MD5} + byte[] buff = new byte[BUFF_SIZE]; //缓存 大小 + + //- 检查实际接收文件大小 + if(remoteFileList != null && remoteFileList.size()>0){ + for(FileComment arr : remoteFileList){ + + String filePath = arr.getFileName().replaceAll("\\\\", "/"); +// System.out.println("filePath "+filePath); + String fileName = filePath.substring(filePath.lastIndexOf("/")+1, filePath.length()); +// System.out.println("fileName "+fileName); + String newUrl = newDir+fileName; +// System.out.println("newUrl "+newUrl); + File newFile = new File(newUrl); + + //-- 已接收完成 + if(newFile.exists()){ + continue; + } + + //-- 续传文件及起始长度 + newFile = new File(newUrl+TEMP_SUFFIX); + arr.setStart(newFile.length()); + receiveFileList.add(arr); + } + } + this.sendObject(receiveFileList); + + //- 接收文件 + if(receiveFileList != null && receiveFileList.size()>0){ + for(FileComment arr : receiveFileList){ + String filePath = arr.getFileName().replaceAll("\\\\", "/"); +// System.out.println("filePath "+filePath); + String fileName = filePath.substring(filePath.lastIndexOf("/")+1, filePath.length()); +// System.out.println("fileName "+fileName); + String newUrl = newDir+fileName; +// System.out.println("newUrl "+newUrl); + + File newFile = new File(newUrl+TEMP_SUFFIX); + if(!newFile.getParentFile().exists()){ + newFile.getParentFile().mkdirs(); + } + + //创建空文件 + if (!newFile.exists()) { + newFile.createNewFile(); + } + + long start = arr.getStart(); // 起始 + long end = arr.getEnd(); // 结束 + if(end == -1){ + return -3; + } + + if(start 0) { +// logger.info("读取文件长度:"+nRead); + oSavedFile.write(buff,0,nRead); + end -= nRead; + if(end<=0){ + break rfile; + } + } + oSavedFile.close(); + oSavedFile = null; + } +// pl("--!! "+arr.getStart()+" "+arr.getEnd()+" "+arr.getMd5Val()); +// Scanner scanner = new Scanner(System.in); +// newFile.renameTo(new File(newUrl)); // 慎用 将临时文件名改为正式文件名,即去掉.tp后缀 + if(newFile.exists()){ + FileUtils.copyFile(newFile, new File(newUrl)); //将临时文件名改为正式文件名,即去掉.tp后缀 + newFile.delete(); + } + + logger.debug(newFile.getAbsolutePath()+" 下载完成!"); + + //-- MD5为空 无需校验 + if(StringUtils.isEmpty(arr.getMd5Val())){ + continue; + } + + File newFile2 = new File(newUrl); + //-- MD5不相等,则删除该文件 返回-1 + if(!arr.getMd5Val().equals(MD5Util.getFileMD5String(newFile2))){ + newFile.delete(); + logger.debug(newFile.getAbsolutePath()+" MD5值校验不一致"); + return -1; + } + //-- MD5相等 + else + logger.debug(newFile.getAbsolutePath()+" MD5值校验一致"); + } + } + logger.debug("多文件接收结束,共 "+(remoteFileList==null ? 0 : remoteFileList.size())+ "个文件"); +// logger.info("多文件接收结束,共 "+(remoteFileList==null ? 0 : remoteFileList.size())+ "个文件"); + return rFlag; + } catch (IOException e) { + logger.error("",e); + return -2; + } catch (ClassNotFoundException e) { + logger.error("",e); + return -2; + }finally{ +// logger.info("oSavedFile----"+oSavedFile); + if(oSavedFile!=null){ + try { + oSavedFile.close(); + oSavedFile = null; + } catch (IOException e) { + logger.error("",e); + } + } +// logger.info("文件下载的结束----"); + } + } + + + /** + * 批量接收文件 + * @param newDir + * (DC未使用) + */ + protected void bpReceiveFileByBath(String newDir) throws ClassNotFoundException,IOException { + + BufferedRandomAccessFile oSavedFile = null; + try { + //获取集合文件路径 + String oldDir = this.receiveMessage(); + int headLength = "abs:".length(); + oldDir = ((StringUtils.isNotEmpty(oldDir) + && oldDir.length()>=headLength) + ?oldDir.substring(headLength,oldDir.length()) + :oldDir); + if(StringUtils.isEmpty(oldDir)){ + logger.debug("远程 目录根路径为空 接收文件不保留目录格式 统一存放到本地目录:》"+newDir); + }else{ + logger.debug("根目录 记录: " + oldDir+" VS "+newDir); + } + this.sendMessage(SUCCESS); + + List remoteFileList = (List)receiveObject(); + List receiveFileList = new LinkedList(); + byte[] buff = new byte[BUFF_SIZE]; + if(remoteFileList != null && remoteFileList.size()>0){ + for(String[] arr : remoteFileList){ + String newUrl = null; + if(StringUtils.isEmpty(oldDir)){ + newUrl = newDir+(new File(arr[0].replaceAll("\\\\", "/")).getName()); + }else{ + newUrl = arr[0].replace(oldDir, newDir);//新路径 + newUrl = newUrl.replaceAll("\\\\", "/"); + } + + File newFile = new File(newUrl); + + //该文件已存在 + if(newFile.exists()){ + continue; + } + + newFile = new File(newUrl+TEMP_SUFFIX); + arr[1] = newFile.length()+""; + receiveFileList.add(arr); + } + } + this.sendObject(receiveFileList); + + if(receiveFileList != null && receiveFileList.size()>0){ + for(String[] arr : receiveFileList){ + String newUrl = null; + if(StringUtils.isEmpty(oldDir)){ + newUrl = newDir+(new File(arr[0].replaceAll("\\\\", "/")).getName()); + }else{ + newUrl = arr[0].replace(oldDir, newDir);//新路径 + newUrl = newUrl.replaceAll("\\\\", "/"); + } + + File newFile = new File(newUrl+TEMP_SUFFIX); + + if(!newFile.getParentFile().exists()){ + newFile.getParentFile().mkdirs(); + } + + if(!newFile.exists()){ + newFile.createNewFile(); + } + + int start = Integer.parseInt(arr[1]); // 起始 + int end = Integer.parseInt(arr[2]); // 结束 + if(start 0) { + oSavedFile.write(buff,0,nRead); + end -= nRead; + if(end<=0){ + break rfile; + } + } + oSavedFile.close(); + oSavedFile = null; + } +// newFile.renameTo(new File(newUrl)); //将临时文件名改为正式文件名,即去掉.tp后缀 + FileUtils.copyFile(newFile, new File(newUrl)); //将临时文件名改为正式文件名,即去掉.tp后缀 + newFile.delete(); + } + } + logger.debug("多文件接收结束,共 "+(remoteFileList==null ? 0 : remoteFileList.size())+ "个文件"); +// } catch (IOException e) { +// logger.error("",e); +// } catch (ClassNotFoundException e) { +// logger.error("",e); + }finally{ + if(oSavedFile!=null){ +// try { + oSavedFile.close(); + oSavedFile = null; +// } catch (IOException e) { +// logger.error("",e); +// } + } + } + } + + /** + * @time Mar 12, 2012-11:07:02 AM + * @param URL + * @param file + * @param nStartPos + * @param nEndPos + * @return -2错误的URL -1下载失败可再次尝试 1下载成功 + * (DC未使用) + */ + public static int bpDownLoadHTTP(String URL, File file,long nStartPos,long nEndPos) { + BufferedRandomAccessFile oSavedFile = null; + try { + URL url = new URL(URL); + HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); + // 设置User-Agent + httpConnection.setRequestProperty("User-Agent", "NetFox"); + httpConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3"); + httpConnection.setRequestProperty("Accept-Language", "en-us,en;q=0.7,zh-cn;q=0.3"); + httpConnection.setRequestProperty("Accept-Encoding", "aa"); + httpConnection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); + httpConnection.setRequestProperty("Keep-Alive", "300"); + httpConnection.setRequestProperty("Connection", "keep-alive"); + httpConnection.setRequestProperty("If-Modified-Since", "Fri, 02 Jan 2009 17:00:05 GMT"); + httpConnection.setRequestProperty("If-None-Match", "\"1261d8-4290-df64d224\""); + httpConnection.setRequestProperty("Cache-Control", "max-age=0"); + httpConnection.setRequestProperty("Referer", "http://www.skycn.com/soft/14857.html"); + // 设置断点续传的开始位置 + httpConnection.setRequestProperty("RANGE", "bytes="+nStartPos+"-" + nEndPos); + + //判断http status是否为HTTP/1.1 206 Partial Content或者200 OK + //如果不是以上两种状态,把status改为STATUS_HTTPSTATUS_ERROR + if (httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK + && httpConnection.getResponseCode() != HttpURLConnection.HTTP_PARTIAL) { + return -2; + } + logger.debug("httpConnection.getResponseCode() " + httpConnection.getResponseCode() + "\n " + + "HttpURLConnection.HTTP_OK " + HttpURLConnection.HTTP_OK + "\n " + + "HttpURLConnection.HTTP_PARTIAL " + HttpURLConnection.HTTP_PARTIAL); + // 获得输入流 + InputStream input = httpConnection.getInputStream(); + oSavedFile = new BufferedRandomAccessFile(file,"rw"); + + // 定位文件指针到nPos位置 + oSavedFile.seek(nStartPos); //从0开始 + byte[] b = new byte[BUFF_SIZE]; + int nRead; + + // 从输入流中读入字节流,然后写到文件中 + while ((nRead = input.read(b, 0, 1024)) > 0) { + + (oSavedFile).write(b, 0, nRead); + nStartPos += nRead; //调整为从1开始 + if(nStartPos >= nEndPos){ + break; + } + } + + httpConnection.disconnect(); + return 1; + } catch (MalformedURLException e) { //错误的URL + logger.error("",e); + return -2; + } catch (IOException e) { + logger.error("",e); + return -1; + }finally{ + if(oSavedFile!=null){ + try { + oSavedFile.close(); + } catch (IOException e) { + logger.error("",e); + } + oSavedFile=null; + } + } + } + + /** + * 关闭通讯 + * @time Aug 28, 2011-8:35:21 PM + */ + protected void close(){ + try { + if(inl!=null){inl.close();inl=null;} + if(outl!=null){outl.close();outl=null;} + if(socket!=null){socket.close();socket=null;} + } catch (IOException e) { + logger.error("",e); + } + } + + /** + * 创建SSLContext方法 + * @time Feb 29, 2012-11:40:24 AM + * @return + */ + public static SSLContext getSSLContext(){ + SSLContext ctx = null ; + + //- 创建 新的sSLContext 校验 + //- reCreateSSLContextFlag 为false 且 sSLContext 不为空时无需创建 + if(!resetSSLContextFlag && sSLContext !=null){ + return sSLContext; + } + + //- 创建 新的sSLContext + try { + System.setProperty("javax.net.ssl.trustStore", SERVER_TRUST); + //-- 初始化私钥证书库 + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE); + ks.load(new FileInputStream(SERVER_STORE), SERVER_STORE_PSW.toCharArray());//载入keystore + kmf.init(ks, SERVER_KEY_PSW.toCharArray()); + //-- 初始化公钥证书库 + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); + KeyStore tks = KeyStore.getInstance(KEYSTORE_TYPE); + tks.load(new FileInputStream(SERVER_TRUST), SERVER_TRUST_PSW.toCharArray());//载入keystore + tmf.init(tks); + //-- 初始化SSL通讯上下文对象 和 SSL通讯工厂 + ctx = SSLContext.getInstance(SSL_CONTEXT_TYPE); + ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(),new SecureRandom()); + logger.info("证书库载入成功(load keystore success.)"); + resetSSLContextFlag = false; + } catch (NoSuchAlgorithmException e) { + logger.error("",e); + } catch (CertificateException e) { + logger.error("",e); + } catch (FileNotFoundException e) { + logger.error("",e); + } catch (IOException e) { + logger.error("",e); + } catch (KeyStoreException e) { + logger.error("",e); + } catch (UnrecoverableKeyException e) { + logger.error("",e); + } catch (KeyManagementException e) { + logger.error("",e); + }finally{ + } + return ctx; + } + + public static void resetSSLContext(){ + synchronized (sSLContext) { + resetSSLContextFlag = true; + sSLContext = getSSLContext(); + } + } + + /** + * + * @time Mar 12, 2012-11:08:43 AM + * @param url + * @return -1文件不存在 0文件长度为0 N文件长度 + */ + public static long getRemoteFileSize(String url) { + + long size = -1; + + try { + HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection(); + + //请求状态 大于等于400 均为 represent access error + if(conn.getResponseCode() >= 400){ + logger.error("HttpURLConnection Error Code:"+conn.getResponseCode()); + return -2; + } + + //获取ContentLength 并 关闭连接 + size = conn.getContentLength(); + conn.disconnect(); + + } catch (Exception e) { + logger.error("",e); + } + return size; + } + + /** + * 删除addTimeTagForFileName()方法 所添加的时间戳 + * @time Mar 12, 2012-3:36:16 PM + * @param fileName + * @return + */ + public static String removeTimeTagFileName(String fileName) { + + if (StringUtils.isNotBlank(fileName) && fileName.contains("_")) { + + String timeTag = fileName.substring(fileName.lastIndexOf("_"), + fileName.lastIndexOf(".")==-1?fileName.length():fileName.lastIndexOf(".")); //针对无后缀名文件,时间戳截取校验 + fileName = fileName.replace(timeTag, ""); + + } + + return fileName; + + } + + /** + * 上传文件时,判断该文件是否已存在,如存在,则在后面加入时间戳 + * + * @param fileName + * 单纯的文件名 + */ + public static String addTimeTagForFileName(String fileName,boolean isDirectory) { + + Calendar calendar = new GregorianCalendar(); + long timestamp = calendar.getTimeInMillis(); + + // 去掉后缀的文件名 + String fielType = ""; + + if (!isDirectory && fileName.lastIndexOf(".") != -1) { + fielType = fileName.substring(fileName.lastIndexOf(".")); + fileName = fileName.substring(0, fileName.lastIndexOf(".")); + } + + fileName += "_" + timestamp+""+((int)(Math.random()*1000)); + fileName += fielType; + + return fileName; + } + + /** + * 获取 断点续传 文件参数信息 + * @time Apr 17, 2012-9:56:56 AM + * @param filePath + * @return + */ +// public static FileComment getFileParams(String filePath) throws Exception{ +// File file = new File(filePath); +// FileComment fileParam = new FileComment(file.getName(),file.length(),0l,file.exists()?MD5Util.getFileMD5String(file):null); +// if(!file.exists()){ +// file = new File(filePath+TEMP_SUFFIX); +// fileParam.setStart(file.length()); +// } +// return fileParam; +// } + + public static void pl(Object object){ + System.out.println(object==null?null:object.toString()); + } + +} + +class TempFile { + private String fileName = null; + private File File = null; + public String getFileName() { + return fileName; + } + public void setFileName(String fileName) { + this.fileName = fileName; + } + public File getFile() { + return File; + } + public void setFile(File file) { + File = file; + } +} \ No newline at end of file diff --git a/src/com/nms/server/util/socket/limiter/BandwidthLimiter.java b/src/com/nms/server/util/socket/limiter/BandwidthLimiter.java new file mode 100644 index 0000000..05b30e0 --- /dev/null +++ b/src/com/nms/server/util/socket/limiter/BandwidthLimiter.java @@ -0,0 +1,88 @@ +package com.nms.server.util.socket.limiter; + +import org.apache.log4j.Logger; + +import com.nms.server.common.Constants; + +/** + * 共用网络通讯限速器 + * 实现对上行和下行通讯发送频率限制,达到通讯限速目的 + * 设计:实现通讯线程IO数量同步计数linkNumber; + * 总网络带宽/当前IO流数量,计算单IO流通讯带宽限制; + * 通过带宽限制,计算每次已发送数据量的限定时间; + * 用线程睡眠限定时间,降低发送频率,实现通讯限速 + * 网络带宽单位:Mbps(每秒兆字节)单位换算8Kbps = 8Kb/s=1KBps=1KB/s + * @date Nov 13, 2012 10:37:23 AM + * @author ZhangGang + * + */ +public class BandwidthLimiter { + /* Log4j */ + private final static Logger logger = Logger.getLogger(BandwidthLimiter.class); + + /*全局空锁*/ + private final static byte[] lock = new byte[0]; + + /* KB 单位*/ +// private final static Long KB = 1024l; + + /* 最小包长度 chunk length in bytes */ +// private final static Long CHUNK_LENGTH = 1024l; + + /*总链路数*/ + private static int ioNumber = 0; + + /* 缺省 无限速 byte/s */ + private static int maxRate = Constants.MAX_NETWORK_BANDWIDTH==null?0:Constants.MAX_NETWORK_BANDWIDTH*1024*1024/8; + + /** + * Next 1 byte should do bandwidth limit. + */ + public static void limitNextBytes() { + limitNextBytes(1); + } + + /** + * 等待限定时间执行方法 + * 发送 len长度数据限定时间 + * @param len 数据长度 (未涉及到TCP通讯实际发送长度,不确定实际通讯中TCP头数量) + */ + public static void limitNextBytes(int len) { + if(ioNumber<=0){ logger.warn("LinkNumber cannot be calculated for 0. Please execute the BandwidthLimiter. addLink () method first"); return;} + if(len<=0){ logger.warn("Sent bytes of length 0 cannot be calculated"); return;} + if(maxRate == 0){return;} + /* 计算当前 链路数 因为 同一链路内,输入和输出不能同时工作 所以 在计算单链路的通讯带宽需要 IO数减半(余1判断为容错处理) */ + int linkNumber = ioNumber/2 + ioNumber%2; + + + /* 计算当前 发送 len bytes 的数据 时间周期 纳秒数 */ + long timeCostPerChunk = 1000000000l * len / (maxRate / linkNumber) ; // 发送长度 / (单条通信线路长度) *1000000000l纳秒 + try { +// logger.debug("本次接收字节 "+len+" 等待间隔:"+timeCostPerChunk+" ns"); + Thread.sleep(timeCostPerChunk / 1000000,(int) (timeCostPerChunk % 1000000)); //Thread.sleep(毫秒,纳秒); + } catch (InterruptedException e) { + //KB??是不是应该是字节B + logger.error("Communication speed limiter execution thread wait failure .Parameter[Total Bandwidth:"+maxRate+"KB/s linkNumber:"+linkNumber+" Data Length:"+len+"Byte Limit Time:"+timeCostPerChunk+"nm ]", e); + } + } + + /** + * 通讯中输入输出流计数器,IO流数加1 + * @time Nov 15, 2012-10:14:56 AM + */ + public static void addLink() { + synchronized (lock) { + ++ioNumber; + } + } + + /** + * 通讯中输入输出流计数器,IO流数减1 + * @time Nov 15, 2012-10:15:37 AM + */ + public static void removeLink() { + synchronized (lock) { + --ioNumber; + } + } +} diff --git a/src/com/nms/server/util/socket/limiter/InputStreamLimiter.java b/src/com/nms/server/util/socket/limiter/InputStreamLimiter.java new file mode 100644 index 0000000..c988438 --- /dev/null +++ b/src/com/nms/server/util/socket/limiter/InputStreamLimiter.java @@ -0,0 +1,98 @@ +package com.nms.server.util.socket.limiter; + +import java.io.IOException; +import java.io.InputStream; + +import com.nms.server.util.socket.SocketUtils; + + +/** + * 封装InputStream 引入BandwidthLimiter实现带宽限制 + * @date Nov 15, 2012 10:47:18 AM + * @author ZhangGang + * + */ +public class InputStreamLimiter extends InputStream { + + private InputStream is = null; + + /* 设定最小数据包长度,最大限度实现数据读取平均化,降低通讯幅度峰值 */ + private final int BUFF_SIZE = SocketUtils.BUFF_SIZE; + + /** + * 构造函数封装InputStream + * 引入BandwidthLimiter的IO计数器 创建:加1 + * @param is InputStream + */ + public InputStreamLimiter(InputStream is) { + this.is = is; + BandwidthLimiter.addLink(); + } + + @Override + public int read() throws IOException { + BandwidthLimiter.limitNextBytes(); + return this.is.read(); + } + + /** + * 重构read(byte[], int, int)方法 + * 将 b[] 切分数据包到最小数据包读取数据 + * 引入BandwidthLimiter限速方法 + * @see java.io.InputStream#read(byte[], int, int) + */ + public int read(byte b[], int off, int len) throws IOException { + + int len2 = 0; + + int c=-1; + while(len>0){ + int p = len0){ + System.out.println("-- "+n); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + try { + if(stream!=null)stream.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }*/ +} \ No newline at end of file diff --git a/src/com/nms/server/util/socket/limiter/OutputStreamLimiter.java b/src/com/nms/server/util/socket/limiter/OutputStreamLimiter.java new file mode 100644 index 0000000..190ebb6 --- /dev/null +++ b/src/com/nms/server/util/socket/limiter/OutputStreamLimiter.java @@ -0,0 +1,66 @@ +package com.nms.server.util.socket.limiter; + +import java.io.IOException; +import java.io.OutputStream; + +import com.nms.server.util.socket.SocketUtils; + +/** + * 封装OutputStream 引入BandwidthLimiter实现带宽限制 + * @date Nov 15, 2012 10:47:18 AM + * @author ZhangGang + * + */ +public class OutputStreamLimiter extends OutputStream { + + private OutputStream os = null; + + /* 设定最小数据包长度,最大限度实现数据读取平均化,降低通讯幅度峰值 */ + private final int BUFF_SIZE = SocketUtils.BUFF_SIZE; + + /** + * 构造函数封装OutputStream + * 引入BandwidthLimiter的IO计数器 创建:加1 + * @param os OutputStream + */ + public OutputStreamLimiter(OutputStream os) { + this.os = os; + BandwidthLimiter.addLink(); + } + + @Override + public void write(int b) throws IOException { + BandwidthLimiter.limitNextBytes(); + this.os.write(b); + } + /** + * 重构write(byte[], int, int)方法 + * 将 b[] 切分数据包到最小数据包发送数据 + * 引入BandwidthLimiter限速方法 + * @see java.io.OutputStream#write(byte[], int, int) + */ + public void write(byte[] b, int off, int len) throws IOException { + while(len >BUFF_SIZE){ + this.os.write(b, off, BUFF_SIZE); + off += BUFF_SIZE; + len -= BUFF_SIZE; + BandwidthLimiter.limitNextBytes(BUFF_SIZE); + } + + if(len >0){ + this.os.write(b, off, len); + BandwidthLimiter.limitNextBytes(len); + } + + } + + /** + * 关闭IO流 + * 重写父方法,引入BandwidthLimiter的IO计数器 关闭:减1 + * @see java.io.OutputStream#close() + */ + public void close() throws IOException{ + BandwidthLimiter.removeLink(); + this.os.close(); + } +} \ No newline at end of file diff --git a/src/druid.properties b/src/druid.properties new file mode 100644 index 0000000..ab8f9fe --- /dev/null +++ b/src/druid.properties @@ -0,0 +1,38 @@ +##Driver +#druid.driverClassName=com.mysql.jdbc.Driver +##数据库链接, +#druid.url=jdbc:mysql://192.168.0.37:3306/project_demo?useUnicode=true&characterEncoding=UTF-8 +##帐号 +#druid.username=root +##密码 +#druid.password=xxxx +#检测数据库链接是否有效,必须配置 +druid.validationQuery=SELECT 1 from dual +#初始连接数 +druid.initialSize=3 +#最大连接池数量 +druid.maxActive=10 +#去掉,配置文件对应去掉 +#druid.maxIdle=20 +#配置0,当线程池数量不足,自动补充。 +druid.minIdle=3 +#获取链接超时时间为1分钟,单位为毫秒。 +druid.maxWait=60000 +#获取链接的时候,不校验是否可用,开启会有损性能。 +druid.testOnBorrow=false +#归还链接到连接池的时候校验链接是否可用。 +druid.testOnReturn=false +#此项配置为true即可,不影响性能,并且保证安全性。意义为:申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 +druid.testWhileIdle=true +#1.Destroy线程会检测连接的间隔时间 +#2.testWhileIdle的判断依据 +druid.timeBetweenEvictionRunsMillis=600000 +#一个链接生存的时间 +druid.minEvictableIdleTimeMillis=600000 +#链接使用超过时间限制是否回收 +druid.removeAbandoned=false +#超过时间限制时间(单位秒),目前为5分钟,如果有业务处理时间超过5分钟,可以适当调整。 +druid.removeAbandonedTimeout=300 +#链接回收的时候控制台打印信息,测试环境可以加上true,线上环境false。会影响性能。 +druid.logAbandoned=false +druid.filters= \ No newline at end of file diff --git a/src/log4j.properties b/src/log4j.properties new file mode 100644 index 0000000..fe6bc3e --- /dev/null +++ b/src/log4j.properties @@ -0,0 +1,28 @@ +log4j.rootLogger = debug,stdout,logfile,errorLog + +log4j.appender.stdout = org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout.ConversionPattern = %d %p [%l] [%t] - <%m>%n +log4j.appender.stdout.layout = org.apache.log4j.PatternLayout + +#----------------------debug--------------------- + +log4j.appender.logfile = org.apache.log4j.RollingFileAppender +log4j.appender.logfile.MaxFileSize = 50MB +log4j.appender.logfile.MaxBackupIndex = 10 + +log4j.appender.logfile.File = ../dc_logs/datacontroller.log +log4j.appender.logfile.layout.ConversionPattern = %d %p [%l] [%t] - <%m>%n +log4j.appender.logfile.layout = org.apache.log4j.PatternLayout + +#----------------------error--------------------- +log4j.logger.errorLog = warn,errorLog +log4j.appender.errorLog = org.apache.log4j.RollingFileAppender +log4j.appender.errorLog.MaxFileSize = 50MB +log4j.appender.errorLog.Append = true +log4j.appender.errorLog.Threshold = warn +log4j.appender.errorLog.MaxBackupIndex = 10 +log4j.appender.errorLog.File = ../dc_logs/error.log +log4j.appender.errorLog.layout.ConversionPattern = %d %p [%l] [%t] - <%m>%n +log4j.appender.errorLog.layout = org.apache.log4j.PatternLayout + + diff --git a/src/myconfig.properties b/src/myconfig.properties new file mode 100644 index 0000000..66118b0 --- /dev/null +++ b/src/myconfig.properties @@ -0,0 +1,283 @@ +#--------need modify -----------begin +##--公共数据存放父目录(*部署需修改*) +common.datas.dir = D:/nms/nmsdata +##--系统SNMP监测,指定服务器IP 指本机(*部署需修改*) +system.inet.address = 10.0.6.114 +##--NMSWeb端 通讯IP 指WEB(*部署需修改*) +web.socket.ip = 10.0.6.114 +#--数据库驱动 +#db.driver=oracle.jdbc.driver.OracleDriver +db.driver=com.mysql.jdbc.Driver +##--数据库 地址(*部署需修改*) +#db.url = jdbc:oracle:thin:@10.0.6.100:1521:ict +db.url = jdbc:mysql://10.0.6.123:3306/nms?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true&rewriteBatchedStatements=true +##--数据库 用户名(*部署需修改*) +#db.username = nms +db.username = nms +##--数据库 密码(*部署需修改*) +#db.password = nms +db.password = nms +#数据库类型 +db.type=mysql +#--------need modify -----------end +#-------- DB pool -----------begin +db.pool.type=druid +##--##--连接池 设置分区 缺省值 3 一般设置为3~4 +db.pool.partitionCount = 3 +##--设置每个分区中的最大连接数 缺省值 10 注意 此处问每个分区最大线程数 总数为 partitionCount*maxConnectionsPerPartition 即 30 +db.pool.maxConnectionsPerPartition = 20 +##--设置每个分区中的最小连接数 缺省值 5 注意 此处问每个分区最小空闲线程数 总数为 partitionCount*minConnectionsPerPartition 即 15 +db.pool.minConnectionsPerPartition = 3 +##--当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数 缺省值3 +db.pool.acquireIncrement = 3 +#-------- DB pool -----------end +#------- Email ---------begin +##--邮件发送标识,(1启用、0禁用) +email.flag = 0 +##--邮件通知 发送 用户地址(*部署需修改*) +email.address = +##--邮件通知 发送 用户名(*部署需修改*) +email.userName = +##--邮件通知 发送 用户密码(*部署需修改*) +email.password = +##--邮件服务器IP(*部署需修改*) +email.host = +#------- Email ---------end + +##--NMSWeb端 通讯服务监听端口 +web.socket.port = 60703 + +#---Common------- +##--全局文本解析编码,与web端、Agent端一致,不可修改 +common.text.coding = UTF-8 +##--全局日期格式化方式,须与数据库 日期数据格式化方式(db.date.format)保持一致,不建议修改 +common.date.format = yyyy-MM-dd HH:mm:ss + +##--NMSServer升级时,通讯中断延时恢复时间(秒) +mission.update.daily = 300 + +##--最大网络带宽限制(Mbps) +max.network.bandwidth = 5 +#-------Executor Thread--------- +##--公用普通线程池预置线程数(个) +executor.socket.thread.size = 120 +##--公用定时线程池预置线程数(个) +executor.scheduled.thread.size = 30 +##--数据解析线程预置线程数(个) +executor.data.resove.thread.size = 3 +#---Change Operations------- +##--监测、任务、节点等状态及设置信息变更时,执行变更操作的线程,轮询周期(秒) +change.operations.period = 300 +change.release.semaphore.max = 10 +#------- Detection Alarm --------- +##--主动监测执行通讯并发数(个) +detec.release.semaphore.max = 20 +#------- Detection Resolve --------- +##--缓存监测数据解析周期(秒) +detec.data.resovle.period = 60 +##--硬盘监测数据解析周期(秒) + +##--DC端主动收集数据(监测数据、任务结果、回传文件)周期(秒)(应该比生成监测数据的间隔时间小,不要web页面会有段时间报警,说缺失一个周期的监测数据,而实际是你还没去收集,5分钟) +data.collect.period = 300 +##--DC端主动收集数据(执行完成,但无任务结果的任务的结果信息)(大于等于正常收集任务结果的周期data.collect.period的2倍?) +nonRltTask.result.collect.period = 600 +##--告警数据解析周期(秒) +alarm.data.resovle.period = 60 +####common.file.resovle.period unable +##--PING监测类别标识,用于查询数据库中预置PING监测设置的监测类别标识(须与数据库PING监测类别名称一致,默认不可修改) +detec.ping.str = PING +##--NMSC监测类别标识,用于查询数据库中预置PING监测设置的监测类别标识(须与数据库NMSClient监测类别名称一致,默认不可修改) +detec.nmsc.str = NMSClient +##--SNMP监测类别标识,用于查询数据库中预置SNMP监测设置的监测类别标识(不可修改) +detec.snmp.str = SNMP +##--SNMP_SWITCH监测类别标识,用于查询数据库中预置SWITCH监测设置的监测类别标识(不可修改) +detec.switch.str = SWITCHPORT +##--systeminfo监测类别解析标识,特有解析方法,用于查询数据库中预置systeminfo监测设置的监测类别标识(不可修改) +detec.systeminfo.str = systeminfo +##--SNMP_TRAP监测类别标识,用于查询数据库中预置SNMP_TRAP监测设置的监测类别标识(不可修改) +alarm.snmp.trap.str = SNMP_TRAP + +#---Mission Control------- +##--周期任务执行时,预置周期的数量(个) +mission.loop.preset.number = 10 +##--周期任务执行时,默认检查时间(暂未使用) +mission.loop.finishing.daily = 20:00:00 + +#---Mission release------- +##--任务执行,节点下发通讯并发数(个) +mission.release.semaphore.max = 10 +##--任务执行,重复尝试次数(次) +mission.release.times = 2 +##--任务执行,重复下发间隔周期(秒) +mission.release.period = 180 + +#---Mission File------- +##--从WEB端下载任务文件的线程的超时时间(秒) +mission.file.download.delay = 20 +##--上传到WEB端的回传文件线程执行周期(秒) +mission.file.upload.period = 120 +##--数据库 日期格式化方式 须与全局日期格式化方式(common.date.format)保持一致,不建议修改 +db.date.format = yyyy-MM-dd HH24:mi:ss +##--数据库 批处理 最大数 +db.execute.batch = 100 + +#---SNMP------- +##--SNMP 目标主机信息 获取端口 +snmp.client.port = 161 +##--SNMP 接收告警 监听端口 +snmp.trap.port = 162 +##--SNMP 主动告警 接收端口 +snmp.community = public +##--ObjectSNMP class文件打包名称(不可修改) +snmp.class.package = osDomains +##--SNMP 加密认证级别 0 不认证 不加密'zg' 1 认证 不加密'hckings' ,2认证 且 加密'dxy' +snmp.v3.security.level = 0 +##--SNMP 加密认证用户 用户名 +snmp.v3.security.name = zg +##--SNMP 认证协议方式 +snmp.v3.auth.protocol = MD5 +##--SNMP 加密协议方式 +snmp.v3.priv.protocol = DES +##--SNMP 认证密码明文 +snmp.v3.auth.passphrase = 12345678 +##--SNMP 加密密码明文 +snmp.v3.priv.passphrase = 12345678 + +#---SSL------- +##--SSL通讯 密匙类型(不可修改) +ssl.type = TLS +##--SSL通讯 密匙文件存放目录,默认为打包程序工程目录下cer(不建议修改) +ssl.dir = cer +##--SSL通讯 密匙有效期(暂未使用) +ssl.key.validity = 90 +##--SSL通讯 Server端 通讯监听端口,须与WEB端、Agent端通讯发送端口保持一致(不建议修改) +ssl.server.port = 60702 +##--SSL通讯 Agent端 通讯发送端口,须与Agent端 通讯监听端口保持一致(不建议修改) +ssl.client.port = 60701 +##--SSL通讯 密匙库类型(不可修改) +ssl.keystore.type = jceks +##--SSL通讯 Server端 私钥库名称 +ssl.server.store = server_ks +##--SSL通讯 Server端 公钥库名称 +ssl.server.trust = server_ts +##--SSL通讯 Agent端 私钥库名称 与Agent端保持一致(预留) +ssl.client.store = client_ks +##--SSL通讯 Agent端 公钥库名称 与Agent端保持一致(预留) +ssl.client.trust = client_ts +##--SSL通讯 Server端 私钥库密码 +ssl.server.store.psw = client +##--SSL通讯 Server端 公钥库密码(预留) +ssl.server.trust.psw = client +##--SSL通讯 Server端 公钥导出文件名称(预留) +ssl.server.export = server.cer +##--SSL通讯 Agent端 公钥导出文件名称(预留) +ssl.client.export = client.cer +##--SSL通讯 Server端 旧密钥名称(预留) +ssl.server.key.old = serverks20110828 +##--SSL通讯 Server端 旧密钥密码(预留) +ssl.server.key.old.psw = 123456 +##--SSL通讯 Server端 新密钥名称(预留) +ssl.server.key.new = serverks20110828 +##--SSL通讯 Server端 新密钥密码(预留) +ssl.server.key.new.psw = 123456 +##--SSL通讯 Agent端 密钥名称(预留) +ssl.client.key = serverks20110828 +##--SSL通讯 Agent端 密钥密码(预留) +ssl.client.key.psw = 123456 + +#-----------debug------------ +##--ZIP 解析线程标识 启用标识(1启用、0禁用) +zip.flag = 1 +##--监测结果接收超时 启用标识(1启用、0禁用) +detec.timeout.flag = 1 +##--文件上传功能 启用标识(1启用、0禁用) +file.upload.flag = 1 +##--监测数据解析功能 启用标识(1启用、0禁用) +file.resove.flag = 1 +##--数据主动收集功能 启用标识(1启用、0禁用) +data.collect.flag = 0 +##--执行完成,但无任务结果的任务的结果信息的收集标志 启用标识(1启用、0禁用) +nonRltTask.result.collect.flag = 1 +##--告警解析功能 启用标识(1启用、0禁用) +alarm.resove.flag = 1 +##--变更操作功能 启用标识(1启用、0禁用) +change.operations.flag = 1 +##--主动监测PING功能 启用标识(1启用、0禁用) +ping.flag = 1 +##--主动监测NMSClient功能 启用标识(1启用、0禁用) +nmsc.flag = 1 +##--主动监测SNMP功能 启用标识(1启用、0禁用) +snmp.flag = 1 +##--主动监测交换机功能 启用标识(1启用、0禁用) +switch.flag = 1 +##--trap功能 启用标识(1启用、0禁用) +trap.flag = 1 +##--初始化任务执行功能 启用标识(1启用、0禁用) +mission.flag = 1 +##--任务结果解析功能 启用标识(1启用、0禁用) +result.flag = 1 +##--邮件发送功能 启用标识(1启用、0禁用) +email.start.flag = 1 +#-----------debug 2------------ +##--监测数据文件解析 保存到数据库操作标识(1启用、0禁用) +file.resove.commitDB.flag = 1 +delete.files.flag = 1 +config.update.flag = 8 +delete.files.period = 24 +mailing.period = 120 +keep.file.days = 7 +#硬盘监测数据入库线程开关 +data.files.reader.flag = 1 +error.info.flag = 1 +detec.system.str = system + +#握手监测失败时(state=-1),默认的告警级别:0级 +detect.fail.nmsc.police.level=0 +#握手监测失败时(state=-1),默认的紧急状态:0紧急;1非紧急 +detect.fail.nmsc.police.emergent=0 +#非握手监测失败时(state=-1),默认的告警级别:1-5级 +detect.fail.non.nmsc.police.level=1 +#非握手监测失败时(state=-1),默认的紧急状态:0紧急;1非紧急 +detect.fail.non.nmsc.police.emergent=1 +#端口异常(流量为0,设置状态和实际状态不符)时,默认的告警级别:1-5级 +port.alarm.level=5 +#端口异常(流量为0,设置状态和实际状态不符)时,默认的紧急状态:0紧急;1非紧急 +port.alarm.emergent=1 +#NC主动告警(默认为紧急),state=-2时,默认的告警级别:0级, +nc.alarm.police.level=0 +#DC端主动监测,单节点超时时间 +single.node.detect.timeout=180 +#监测数据每500条入库一次 +detection_info_data_max_rows =500 +data.collect.daily=120 +#dc监测数据入库模式,1:由web 主控控制入库,2:自己主动入库,3:智能模式(1和2结合),4,写入文件,定时上传web端 +detect.insert.mode=2 + +############ 多线程入库监测数据 ################ +#监测数据入库线程模式,1:单线程,2:多线程(info,detail) +detect.insert.thread.mode=2 +#info入库线程运行模式,1:每隔 n秒 运行一次,2:间隔 n 秒运行一次 +detect.info.thread.mode=2 +#info入库线程 每隔 或间隔 时间 ,单位:s +detect.info.thread.time=60 +detect.info.thread.delay=5 +#detail入库线程运行模式,1:每隔 n秒 运行一次,2:间隔 n 秒运行一次 +detect.detail.thread.mode=1 +#detail入库线程 每隔 或间隔 时间 ,单位:s +detect.detail.thread.time=60 +detect.detail.thread.delay=5 +############ 多线程入库监测数据 ################ + + +############## netty ###################### +##--dc 接收nc主动上报 监测数据,任务结果,任务回传文件(1启用、0禁用),启用时要同时将data.collect.flag 置为 0 +netty.server.flag=1 +netty.server.port=9527 +netty.server.ssl=true +#client 空闲时间,单位:s,设置比client 心跳时间大一点,判断nc 是否在线 +netty.idle.time=15 +netty.so.backlog=128 + +#代码生成序列,0 关闭,默认为detection_info表,如多个表需要添加序列,多个表名用,隔开 +sequence.table=0 + diff --git a/src/old/interruptThread/model/SimpleModel1.java b/src/old/interruptThread/model/SimpleModel1.java new file mode 100644 index 0000000..f50c41b --- /dev/null +++ b/src/old/interruptThread/model/SimpleModel1.java @@ -0,0 +1,63 @@ +package old.interruptThread.model; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + + +/** + * 线程关闭模型1 + * 宗旨:外部线程通知该线程中断,该线程自行业务结束 + * 非阻塞的循环业务操作,在循环中使用stop标示来结束业务循环 + * + * @date Feb 21, 2013 10:30:53 AM + * @author ZhangGang + * + */ +public class SimpleModel1 implements Runnable { + volatile boolean stop = false; + + public static void main(String[] args) throws Exception { +// Thread thread = new Thread(new SimpleModel1(), "My Thread2"); + ExecutorService service = Executors.newFixedThreadPool(1); + System.out.println("Starting thread..."); + Future future = service.submit(new SimpleModel1(),""); + Thread.sleep(4000); + System.out.println("Interrupting thread..."); + future.cancel(true) ; // 中断线程 + Thread.sleep(3000); + System.out.println("Stopping application..."); + service.shutdownNow(); + } + + public void run() { + for (int i = 0; i < 10000000l && !stop; i++) { + /* 模拟睡眠1秒操作 替换为完整的业务 */ + long time = System.currentTimeMillis(); + while ((System.currentTimeMillis() - time < 1000)) { + } + + /** + * Thread.currentThread().isInterrupted() 为获取线程中断状态,获取后不改变状态值 + * ps:catch(InterruptedException e)异常捕获 上面的取值会改变为false + */ + System.out.println("My Thread is running... isInterrupted? "+Thread.currentThread().isInterrupted()); + + if (Thread.currentThread().isInterrupted()) { // 该值为读取 + // 线程中断状态,不会改变中断状态的值 + System.out.println("结束线程..."); + stop = true; + } + else if (Thread.currentThread().isInterrupted()) { // 该值为读取 + System.out.println("结束线程..."); + return; + } + + /** + * if 和 else if的区别在于 else if没有"My Thread exiting under request..."的输出 + * 推荐使用if 判断 + * */ + } + System.out.println("My Thread exiting under request..."); + } +} \ No newline at end of file diff --git a/src/old/interruptThread/model/SimpleModel2.java b/src/old/interruptThread/model/SimpleModel2.java new file mode 100644 index 0000000..73aa3d0 --- /dev/null +++ b/src/old/interruptThread/model/SimpleModel2.java @@ -0,0 +1,63 @@ +package old.interruptThread.model; + +/** + * 线程关闭模型2 + * 宗旨:外部线程通知该线程中断,该线程自行业务结束 + * 存在阻塞的循环业务操作,如Thread.sleep(),Thread.wait(),Thread.join() 等 + * 外部线程执行thread.interrupt();该线程会抛出异常InterruptedException + * 即 须要增加对该异常的捕捉处理 + * + * @date Feb 21, 2013 10:30:53 AM + * @author ZhangGang + * + */ +public class SimpleModel2 implements Runnable{ +volatile boolean stop = false; + +public static void main(String[] args) throws Exception { + Thread thread = new Thread(new SimpleModel2(), "My Thread2"); + System.out.println("Starting thread..."); + thread.start(); + Thread.sleep(4000); + System.out.println("Interrupting thread..."); + thread.interrupt(); //等同于 future.cancel(true) ; + System.out.println("线程是否中断:" + thread.isInterrupted()); + Thread.sleep(3000); + System.out.println("Stopping application..."); +} + +public void run() { + while (!stop && true) { + + /* 模拟睡眠1秒操作 替换为完整的业务 */ + long time = System.currentTimeMillis(); + while ((System.currentTimeMillis() - time < 1000)) { + } + + /** + * Thread.currentThread().isInterrupted() 为获取线程中断状态,获取后不改变状态值 + * ps:catch(InterruptedException e)异常捕获 上面的取值会改变为false + */ + System.out.println("My Thread is running... isInterrupted? "+Thread.currentThread().isInterrupted()); + + if (Thread.currentThread().isInterrupted()) { // 该值为读取 线程中断状态,不会改变中断状态的值 + System.out.println("结束线程..."); + stop = true; + } + + try { + Thread.sleep(1); //sleep + } catch (InterruptedException e) { + e.printStackTrace(); + //异常被捕获后 Thread.currentThread().isInterrupted() 值 将变为false + System.out.println("结束线程1...?"+Thread.currentThread().isInterrupted()); + Thread.currentThread().interrupt(); + System.out.println("结束线程2...?"+Thread.currentThread().isInterrupted()); + stop = true; + } + //线程中断操作 循环中使用 --End + } + + System.out.println("My Thread exiting under request..."); +} +} \ No newline at end of file -- cgit v1.2.3