initial commit
This commit is contained in:
453
src/com/nis/nmsclient/common/Common.java
Normal file
453
src/com/nis/nmsclient/common/Common.java
Normal file
@@ -0,0 +1,453 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.filefilter.FalseFileFilter;
|
||||
import org.apache.commons.io.filefilter.FileFilterUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.config.DetecConfOper;
|
||||
import com.nis.nmsclient.model.AlarmInfo;
|
||||
import com.nis.nmsclient.model.SetInfo;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmPO;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmUtil;
|
||||
import com.nis.nmsclient.thread.plugin.StartPluginRun;
|
||||
import com.nis.nmsclient.thread.task.LoopTaskThread;
|
||||
import com.nis.nmsclient.util.ProcessUtil;
|
||||
import com.nis.systeminfo.thread.GetInfoRun;
|
||||
|
||||
public class Common {
|
||||
static Logger logger = Logger.getLogger(Common.class);
|
||||
|
||||
public static final String COMMON_SYS_SETINFO = "0";
|
||||
public static boolean NC_UPGRADE_FLAG = false;
|
||||
|
||||
public static final ExecutorService service = Executors
|
||||
.newFixedThreadPool(Contants.COMMON_THREAD_SOCKET_SIZE); // 通讯线程池
|
||||
public static final ScheduledExecutorService scheduled = Executors
|
||||
.newScheduledThreadPool(Contants.COMMON_THREAD_SCHEDULE_SIZE); // 定时执行线程池
|
||||
|
||||
// 任务变更或取消控制集 object[1]=ScheduledFuture<?>,object[2]=LoopTaskThread
|
||||
private static Map<Long, Object[]> taskFutureMap = Collections.synchronizedMap(new HashMap<Long, Object[]>());
|
||||
// 主动告警信息: 主动报警线程使用
|
||||
private static Map<Long, AlarmPO> alarmPOs = Collections.synchronizedMap(new HashMap<Long, AlarmPO>());
|
||||
// 预置监测控制集
|
||||
private static Map<Long, ScheduledFuture<?>> sysDetecFutrue = Collections.synchronizedMap(new HashMap<Long, ScheduledFuture<?>>());
|
||||
// Agent定时启动的三方监测控制集
|
||||
private static Map<Long, ScheduledFuture<?>> pluginDetecFutrue = Collections.synchronizedMap(new HashMap<Long, ScheduledFuture<?>>());
|
||||
// 三方监测设置集
|
||||
private static Map<Long, SetInfo> pluginDetecSetInfoMap = new HashMap<Long, SetInfo>();
|
||||
// 监测信息报警相关信息(alarmInfo.setInfoId, alarmInfo)
|
||||
public static Map<Long, List<AlarmInfo>> detecAlarmInfoMap = new HashMap<Long, List<AlarmInfo>>();
|
||||
|
||||
/**
|
||||
* 缓存三方监测设置
|
||||
*
|
||||
* @param key
|
||||
* @param setInfo
|
||||
* @param lastMergeFileDetecTime
|
||||
*/
|
||||
public static void putPluginDetecSetInfo(Long key, SetInfo setInfo) {
|
||||
long planTime = (setInfo.getControlStartTime() != null) ? setInfo.getControlStartTime() : 0;
|
||||
|
||||
// 初始化已合并的最后一个临时结果文件的监测时间,用于判断是否出现未生成监测数据的周期
|
||||
// 监测设置下发时,记录该监测的计划启动时间
|
||||
// NC重启时,记录当前时间(周期启动监测由NC控制,若NC重启后存在未合并的临时结果,忽略该时间段内未生成监测数据的周期)
|
||||
if(setInfo.getLastMergeDetecTime() == null) {
|
||||
setInfo.setLastMergeDetecTime(Math.max(System.currentTimeMillis(), planTime));
|
||||
}
|
||||
if(setInfo.getPlanCheckTime() == null || setInfo.getPlanCheckTime().longValue() == 0) {
|
||||
// GetRunInfo.startTime
|
||||
setInfo.setPlanCheckTime(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
pluginDetecSetInfoMap.put(key, setInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取三方监测设置集
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Collection<SetInfo> getPluginDetecSetInfos() {
|
||||
return pluginDetecSetInfoMap.values();
|
||||
}
|
||||
|
||||
public static void putAllDetecAlarmInfo(Map<Long, List<AlarmInfo>> alarmMap) {
|
||||
detecAlarmInfoMap.putAll(alarmMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 监测信息报警相关信息
|
||||
* @param setInfoId
|
||||
* @return
|
||||
*/
|
||||
public static List<AlarmInfo> getDetecAlarmInfo(Long setInfoId) {
|
||||
return detecAlarmInfoMap.get(setInfoId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务
|
||||
*/
|
||||
public static ScheduledFuture<?> getTaskFuture(Long key) {
|
||||
synchronized (taskFutureMap) {
|
||||
Object[] objects = taskFutureMap.get(key);
|
||||
if (objects != null && objects.length > 0 && objects[0] != null) {
|
||||
return (ScheduledFuture<?>) objects[0];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加任务
|
||||
*/
|
||||
public static void putTaskFuture(Long key, ScheduledFuture<?> value, LoopTaskThread loopTask) {
|
||||
synchronized (taskFutureMap) {
|
||||
taskFutureMap.put(key, new Object[] { value, loopTask });
|
||||
logger.info("添加任务 id:" + key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销任务
|
||||
*/
|
||||
public static void cancleTaskFuture(final Long key, long delayMs) {
|
||||
scheduled.schedule(new Runnable() {
|
||||
public void run() {
|
||||
synchronized (taskFutureMap) {
|
||||
// Thread.currentThread().setName("注销任务 id:" + key);
|
||||
Thread.currentThread().setName("Write Off Task ID:" + key);
|
||||
Object[] objects = taskFutureMap.get(key);
|
||||
if (objects!=null && objects.length>0 && objects[0]!=null) {
|
||||
ScheduledFuture<?> future = (ScheduledFuture<?>) objects[0];
|
||||
logger.info("任务状态: "
|
||||
+ ((future.isDone() || future
|
||||
.isCancelled()) ? "已停止" : "运行中"));
|
||||
if (objects.length > 1 && objects[1] != null) {
|
||||
LoopTaskThread loopTask = (LoopTaskThread) objects[1];
|
||||
loopTask.cancle();
|
||||
}
|
||||
future.cancel(true);
|
||||
taskFutureMap.remove(key);
|
||||
logger.info("注销成功");
|
||||
} else {
|
||||
logger.info("任务不存在");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}, delayMs, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从全局变量移除执行完成或者取消的任务(每次在上传发送失败的结果时检查并移除)
|
||||
*/
|
||||
public static void removeCancelAndDoneTaskFuture() {
|
||||
synchronized (taskFutureMap) {
|
||||
Iterator<Long> iterator = taskFutureMap.keySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Long key = iterator.next();
|
||||
Object[] objects = taskFutureMap.get(key);
|
||||
if (objects != null && objects.length > 0) {
|
||||
ScheduledFuture<?> future = (ScheduledFuture<?>) objects[0];
|
||||
if (future.isCancelled() || future.isDone()) {
|
||||
iterator.remove();
|
||||
logger.info("任务控制集 移除 id:" + key + " 状态: "
|
||||
+ ((future.isDone() || future
|
||||
.isCancelled()) ? "已停止" : "运行中"));
|
||||
}
|
||||
} else {
|
||||
iterator.remove();
|
||||
logger.info("任务控制集 移除 id:" + key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取存放预警信息集
|
||||
*/
|
||||
public static Map<Long, AlarmPO> getAlarmPOs() {
|
||||
synchronized (alarmPOs) {
|
||||
return alarmPOs;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消某一监测类型的主动预警
|
||||
*/
|
||||
public static void removeAlarmPO(Long key) {
|
||||
synchronized (alarmPOs) {
|
||||
if (alarmPOs.containsKey(key)) {
|
||||
AlarmPO alarmPO = alarmPOs.get(key);
|
||||
alarmPOs.remove(key);
|
||||
logger.info("主动预警集 移除 setId:" + key + " >> "
|
||||
+ alarmPO.getType() + "_" + alarmPO.getProcIden());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加或更新对某一监测类型的主动预警
|
||||
*/
|
||||
public static void addOrUpdateAlarmPO(AlarmPO alarmPO) {
|
||||
synchronized (alarmPOs) {
|
||||
Long key = alarmPO.getId();
|
||||
String infoMsg = "添加";
|
||||
|
||||
if (alarmPOs.containsKey(key)) {
|
||||
infoMsg = "更新";
|
||||
}
|
||||
|
||||
alarmPOs.put(key, alarmPO);
|
||||
|
||||
logger.info("主动预警集 " + infoMsg + " setId:" + key + " >> " + alarmPO.getType() + "_" + alarmPO.getProcIden());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得预设监测总数
|
||||
*/
|
||||
public static int getSysDetecCount() {
|
||||
synchronized (sysDetecFutrue) {
|
||||
return sysDetecFutrue.size();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用预设监测
|
||||
*/
|
||||
public static void stopSysDetec(SetInfo setInfo) {
|
||||
synchronized (sysDetecFutrue) {
|
||||
Long key = setInfo.getId();
|
||||
// String threadName = "预设监测_"
|
||||
String threadName = "Presupposition Monitoring_"
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null);
|
||||
|
||||
ScheduledFuture<?> future = sysDetecFutrue.get(key);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
sysDetecFutrue.remove(key);
|
||||
logger.info("预设监测线程 停用 setId:" + setInfo.getId() + " >> "
|
||||
+ threadName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加或更新系统预设监测
|
||||
* @param setInfo
|
||||
* @param alarmInfos
|
||||
*/
|
||||
public static void addOrUpdateSysDetec(SetInfo setInfo, List<AlarmInfo> alarmInfos) {
|
||||
synchronized (sysDetecFutrue) {
|
||||
Long key = setInfo.getId();
|
||||
String infoMsg = "添加";
|
||||
|
||||
ScheduledFuture<?> future = sysDetecFutrue.get(key);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
sysDetecFutrue.remove(key);
|
||||
|
||||
infoMsg = "更新";
|
||||
}
|
||||
|
||||
long delay = 0;
|
||||
Date startTime = new Date();
|
||||
if (setInfo.getPlanCheckTime() != null) {
|
||||
try {
|
||||
long gap = setInfo.getPlanCheckTime() - System.currentTimeMillis();
|
||||
if (gap > 0) {
|
||||
delay = gap;
|
||||
startTime = new Date(setInfo.getPlanCheckTime());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Please check whether the next test time is set correctly!", e);
|
||||
}
|
||||
}
|
||||
|
||||
// String threadName = "预设监测_"
|
||||
String threadName = "Presupposition Monitoring_"
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null);
|
||||
|
||||
future = Common.scheduled.scheduleAtFixedRate(new GetInfoRun(
|
||||
threadName, setInfo, startTime, alarmInfos), delay, setInfo
|
||||
.getCheckGap(), TimeUnit.MINUTES);
|
||||
sysDetecFutrue.put(key, future);
|
||||
|
||||
logger.info("预设监测线程 " + infoMsg + " setId:" + setInfo.getId() + " >> " + threadName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动三方监测
|
||||
*/
|
||||
public static void startPluginDetec(SetInfo setInfo) {
|
||||
// String threadName = "三方监测_"
|
||||
String threadName = "Three Party Monitoring_"
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null);
|
||||
|
||||
Common.scheduled.schedule(new StartPluginRun(setInfo, threadName), 0,
|
||||
TimeUnit.MILLISECONDS);
|
||||
|
||||
logger.info("三方监测 添加 setId:" + setInfo.getId() + " >> " + threadName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加定时启动的三方监测
|
||||
*/
|
||||
public static void putPluginDetecFuture(Long key, ScheduledFuture<?> future) {
|
||||
synchronized (pluginDetecFutrue) {
|
||||
pluginDetecFutrue.put(key, future);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止定时启动的三方监测任务
|
||||
*/
|
||||
public static void stopPluginDetecFuture(Long key, String threadName) {
|
||||
synchronized (pluginDetecFutrue) {
|
||||
ScheduledFuture<?> future = pluginDetecFutrue.get(key);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
sysDetecFutrue.remove(key);
|
||||
logger.info("三方监测 移除 setId:" + key + " >> " + threadName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查三方监测是否存在(NC周期启动、NC单次启动)
|
||||
*/
|
||||
public static boolean containPluginDetecFuture(Long key) {
|
||||
ScheduledFuture<?> future = pluginDetecFutrue.get(key);
|
||||
return (future != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用三方监测
|
||||
*/
|
||||
public static void stopPluginDetec(SetInfo setInfo) {
|
||||
// NC周期启动监测需要获取三方监测的关键字
|
||||
if("2".equals(setInfo.getIsControlStart())) {
|
||||
generateCommandAndKeyword(setInfo);
|
||||
}
|
||||
|
||||
Long key = setInfo.getId();
|
||||
// String threadName = "三方监测_"
|
||||
String threadName = "Three Party Monitoring_"
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null);
|
||||
synchronized (pluginDetecFutrue) {
|
||||
ScheduledFuture<?> future = pluginDetecFutrue.get(key);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
sysDetecFutrue.remove(key);
|
||||
logger.info("三方监测 移除 setId:" + setInfo.getId() + " >> " + threadName);
|
||||
}
|
||||
}
|
||||
try {
|
||||
// 检查PID
|
||||
Object[] objArr = ProcessUtil.checkPidAndGetPid(setInfo.getProcessFile(), setInfo.getProcessSearchKeyCode());
|
||||
int isExistFlag = Integer.parseInt(objArr[0].toString());
|
||||
String pidInfo = objArr[1].toString();
|
||||
|
||||
if (isExistFlag == 0) {// 不存在
|
||||
logger.info("停用" + threadName + ":进程原本不存在,不用杀进程");
|
||||
} else if (isExistFlag == 1) {// 存在且只有一个进程,杀PID
|
||||
ProcessUtil.killProcess(pidInfo);
|
||||
logger.info("停用" + threadName + ":杀进程 PID:" + pidInfo);
|
||||
} else if (isExistFlag == 2) {// 找到多个进程,告警
|
||||
logger.info("停用" + threadName + ":" + pidInfo);
|
||||
// String alarmMsg = "停用三方监测进程:" + pidInfo;
|
||||
String alarmMsg = "Discontinuation Of The Three Party Monitoring Process:" + pidInfo;
|
||||
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo
|
||||
.getCheckTypeName(), setInfo.getProcessIden(),
|
||||
new Date(), new Date(), 1,
|
||||
Contants.DETECTION_STATUS_FAILURE, alarmMsg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Discontinuation of three party monitoring anomalies", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置三方监测中由Web管理的监测脚本的启动参数(针对NC启动的周期监测)<br/>
|
||||
* 生成三方监测的执行命令及查询关键字
|
||||
*
|
||||
* @param setInfo
|
||||
* @return 脚本启动命令
|
||||
*/
|
||||
public static String generateCommandAndKeyword(SetInfo setInfo) {
|
||||
String command = null;
|
||||
try {
|
||||
if ("2".equals(setInfo.getIsControlStart())) { // NC周期启动
|
||||
File scriptDir = new File(Contants.localPluginScriptPath);
|
||||
final String keyword = "_" + setInfo.getProcessIden() + ".";
|
||||
Collection<?> files = FileUtils.listFiles(scriptDir,
|
||||
FileFilterUtils.asFileFilter(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
if(name.endsWith(".tp")) {
|
||||
return false; // 排除临时文件
|
||||
}
|
||||
return name.contains(keyword);
|
||||
}
|
||||
}), FalseFileFilter.FALSE);
|
||||
if (!files.isEmpty()) {
|
||||
File scriptFile = (File) files.iterator().next();
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.startsWith("Windows")) {
|
||||
command = scriptFile.getCanonicalPath();
|
||||
} else if (os.startsWith("Linux")) {
|
||||
command = "./ " + scriptFile.getCanonicalFile();
|
||||
}
|
||||
setInfo.setProcessPath(command); // 设置执行命令
|
||||
setInfo.setProcessSearchKeyCode(scriptFile.getName()); // 搜索关键字
|
||||
// 更新缓存中的监测设置
|
||||
Common.putPluginDetecSetInfo(setInfo.getId(), setInfo);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
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)
|
||||
};
|
||||
}
|
||||
}
|
||||
307
src/com/nis/nmsclient/common/Contants.java
Normal file
307
src/com/nis/nmsclient/common/Contants.java
Normal file
@@ -0,0 +1,307 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
|
||||
|
||||
public class Contants {
|
||||
|
||||
public static final String SYSTEM_PATH; //NMSClient应用启动主目录
|
||||
|
||||
/** ========= SSLSocket相关配置参数,客户与服务两端交互数据信息 ========**/
|
||||
public static final String SOCKET_SERVER_HOST_KEY= "server_host";
|
||||
public static String SOCKET_SERVER_HOST;//服务器IP
|
||||
public static final Integer SOCKET_SERVER_PORT;//服务器端口
|
||||
public static final Integer SOCKET_AGENT_PORT;//客户端端口
|
||||
public static final Integer SOCKET_TIMEOUT_MINUTES;// Socket通信超时时间
|
||||
public static final String SSL_KEY_STORE;//key证书库文件
|
||||
public static final String SSL_TRUST_KEY_STORE;//认证证书库文件
|
||||
public static final String SSL_JSSE_TYPE = "TLS";//类型TLS、SSL
|
||||
public static final String SSL_KEYSTORE_TYPE = "JCEKS";//KeyStore的类型有:jceks、jks
|
||||
public static final String SSL_KEY_PRIVATE_PASS = "123456";
|
||||
public static final String SSL_KEY_STORE_PASS = "client";
|
||||
|
||||
public static final String keyPath;
|
||||
|
||||
/**=======================本地文件相关参数========================*/
|
||||
public static final String localDetecConfSuffix;// 监测设置信息文件名后缀
|
||||
public static final String localDetecConfPath;// 监测设置信息存放路径
|
||||
public static final String localDataCollection;// 监测数据存放集
|
||||
public static final String localDataFilePath;//------------监测数据存放路径
|
||||
public static final String localDataDonePath;//------------成功处理数据存放路径
|
||||
public static final String localDataErrorPath;//------------不完整数据0大小文件存放路径
|
||||
public static final String localBackupPath;// 备份文件存放目录
|
||||
public static final String localUploadsPath;// 推送文件存入目录
|
||||
public static final String localTaskPath;// 任务相关信息存放目录
|
||||
public static final String localTaskDonePath;// ------------成功处理任务存放目录
|
||||
public static final String localTaskErrorPath;// -----------不完整0大小回传文件存放目录
|
||||
public static final String localTaskResultPath;// ------------任务结果存放目录
|
||||
public static final String localTaskReturnPath;// ------------任务回传文件存放目录
|
||||
public static final String localLogsPath;// 日志存放路径
|
||||
public static final String localTempPath;// 临时文件存放目录
|
||||
public static final String localTempDataIncomingPath; // 第三方监测临时文件存放目录
|
||||
public static final String localAgentPidFile;// Agent自身进程PID存放文件
|
||||
public static final String localPluginScriptPath;// 第三方监测脚本存放目录
|
||||
public static final String LOCAL_SCRIPT_PATH;
|
||||
|
||||
/**=======================系统预计监测类型========================*/
|
||||
public static final String SYS_CHECK_TYPE_CPU;
|
||||
public static final String SYS_CHECK_TYPE_MEMORY;
|
||||
public static final String SYS_CHECK_TYPE_DISK;
|
||||
public static final String SYS_CHECK_TYPE_NET;
|
||||
public static final String SYS_CHECK_TYPE_SYSDATE;
|
||||
public static final String SYS_CHECK_TYPE_PROCESS;
|
||||
public static final String SYS_CHECK_TYPE_PROCESS_NMSAGENT;
|
||||
public static final String SYS_CHECK_TYPE_SYSTEMINFO;
|
||||
|
||||
/** =====================Common时间时隔==================== **/
|
||||
// ------------ 清除本地文件
|
||||
public static final Integer COMMON_DEL_LOG_DAYS;// 删除日志文件间隔时间
|
||||
public static final Integer COMMON_DEL_DATA_HOURS;// 删除数据文件间隔时间
|
||||
public static final Integer COMMON_DEL_TASK_HOURS;// 删除任务相关文件间隔时间
|
||||
public static final Integer COMMON_DEL_UPGRADEFILE_DAYS;// 删除升级文件间隔时间
|
||||
public static final Integer COMMON_DEL_TEMP_DAYS;// 删除临时文件间隔时间
|
||||
public static String[] COMMON_DEL_PATH_INCLUDE;// 指定Agent可删除文件的范围
|
||||
public static String[] COMMON_DEL_PATH_EXCLUDE;// 指定Agent可删除文件范围内不可删除部分
|
||||
// ------------ 监测、任务、预警
|
||||
public static final Integer COMMON_ALARM_MINUTES = 5;// 主动告警轮循间隔时间
|
||||
public static final Integer COMMON_TASK_RESULT_SEND_MINUTES = 5;// 重发之前发送失败的任务执行结果间隔时间
|
||||
public static final Integer COMMON_TASK_INIT_DELAY_MINUTES = 2;// 启动时初始化任务请求延迟时间
|
||||
public static final Integer COMMON_UPLOAD_DATA_MINUTES;// 上传数据轮循间隔时间
|
||||
public static final Integer COMMON_TASK_CLEAR_HOURS;// 定时清理内存中已完成任务的间隔时间
|
||||
// ------------线程池
|
||||
public static final Integer COMMON_THREAD_SOCKET_SIZE ;// socket通信线程最大个数
|
||||
public static final Integer COMMON_THREAD_SCHEDULE_SIZE;// 定时任务线程最大个数
|
||||
// ------------打包上传
|
||||
public static final Integer COMMON_ZIP_MIN_SIZE;// 文件数越过一定值时压缩用
|
||||
public static final Integer COMMON_ZIP_MAX_SIZE;// 文件数越过一定值时压缩文件最多包含文件个数
|
||||
public static final Integer COMMON_MAX_RETURN_CNT;// 回传文件数越过一定值时压缩用
|
||||
// -----------任务结果、主动告警等信息中各字段的分隔符
|
||||
public static final String COMMON_MSG_SEPRATOR = "$@$";
|
||||
public static final String COMMON_MSG_SEPRATOR_SPLIT = "\\$@\\$";
|
||||
public static final int COMMON_MSG_SUCCESS = 0;
|
||||
public static final int COMMON_MSG_FAIL = 1;
|
||||
// -----------设置文件编码方式
|
||||
public static final String charset;
|
||||
|
||||
/** ========================告警状态常量========================== **/
|
||||
//用于报警: -1监测执行失败,0监测信息不正常,1监测信息正常,-2异常主动告警,2主动告警恢复正常)
|
||||
public static final int DETECTION_STATUS_FAILURE = -1;//监测执行失败
|
||||
public static final int DETECTION_STATUS_ABNORMAL = 0;//监测信息不正常
|
||||
public static final int DETECTION_STATUS_NORMAL = 1;//监测信息正常
|
||||
//public static final int DETECTION_ALARM_ABNORMAL = -2;//告警检查:监测线程异常(未取到数据,相应的文件找不到,或者连续N次都超过设置的告警值)
|
||||
//public static final int DETECTION_ALARM_NORMAL = 2;//告警检查:监测线程恢复正常
|
||||
|
||||
/** ========================任务部分文件后缀============================ **/
|
||||
public static final String TASK_RESULT_FILE_SUFFIX = ".result";
|
||||
public static final String TASK_RESULT_AGENTTMPFILE_SUFFIX = ".upgrade";
|
||||
public static final String TASK_RETURN_FILE_SUFFIX = ".return";
|
||||
|
||||
/** ========================Debug============================ **/
|
||||
public static final Integer DEBUG_INIT_TASK_FLAG;
|
||||
public static final Integer DEBUG_PLUGIN_FLAG;
|
||||
public static final Integer DEBUG_SYSDETECT_FLAG;
|
||||
public static final Integer DEBUG_UPLOADDATA_FLAG;
|
||||
public static final Integer DEBUG_ALARM_FLAG;
|
||||
public static final Integer DEBUG_DELFILE_FLAG;
|
||||
public static final Integer DEBUG_TASKRESULT_FLAG;
|
||||
public static final Integer DEBUG_TASKRETURN_FLAG;
|
||||
|
||||
//=================
|
||||
public static final int max_times = 5;// 失败后重试次数
|
||||
public static final long max_delay_seconds = 30;// 重试间隔,秒
|
||||
public static final int noDataTimes = 4;// 未取到数据的次数,用于主动告警
|
||||
public static final int overAlarmValTimes = 4;// 连续超过预警值的次数,用于主动告警
|
||||
//--------------Agent唯一标志UUID
|
||||
public static Long AGENT_HOST_UUID = null;
|
||||
public static String AGENT_OPERATE_SYSTEM = null;
|
||||
public static String AGENT_LOCAL_IP = null;
|
||||
|
||||
public static final String DETEC_STATE_INFO_FORMATE_POINT = "$@$";//用于监测数据的状态信息web界面显示的格式化的连接符
|
||||
|
||||
public static Boolean ACTIIVE_ALARM_START = false;//默认不启动主动告警
|
||||
public static String AGENT_INTERFACE_NAME_KEY = null;//网络端口名称
|
||||
/**
|
||||
* 监测数据主动上报
|
||||
*/
|
||||
//监测数据主动上报
|
||||
public static final int DATA_SEND_THREAD_FLAG;
|
||||
//主动数据上报 IP
|
||||
public static final String DATA_SEND_THREAD_HOST;
|
||||
//主动数据上报 PORT
|
||||
public static final int DATA_SEND_THREAD_PORT;
|
||||
//主动数据上报间隔 INTERVAL,单位 10 S
|
||||
public static final int DATA_SEND_THREAD_INTERVAL;
|
||||
|
||||
static{
|
||||
SYSTEM_PATH = SysConfig.getSystemDir();
|
||||
|
||||
// -------------------SSLSocket
|
||||
SOCKET_SERVER_HOST = SysConfig.getStringVal(SOCKET_SERVER_HOST_KEY);
|
||||
SOCKET_SERVER_PORT = SysConfig.getIntegerVal("server_port");
|
||||
SOCKET_AGENT_PORT = SysConfig.getIntegerVal("agent_port");
|
||||
SOCKET_TIMEOUT_MINUTES = SysConfig.getIntegerVal("socket.timeout.minutes", "30");
|
||||
SSL_KEY_STORE = formatPath(SysConfig.getStringVal("local.ssl.keys"));
|
||||
SSL_TRUST_KEY_STORE = formatPath(SysConfig.getStringVal("local.ssl.trust"));
|
||||
|
||||
keyPath = formatPath(SysConfig.getStringVal("local.ssl.path"));
|
||||
|
||||
/*=======================文件相关参数========================*/
|
||||
// 可删范围
|
||||
String path = SysConfig.getStringVal("common.del.path.include");
|
||||
if(path!=null && !"".equals(path)){
|
||||
COMMON_DEL_PATH_INCLUDE = path.split(",");
|
||||
for(int i=0; i<COMMON_DEL_PATH_INCLUDE.length; i++){
|
||||
COMMON_DEL_PATH_INCLUDE[i] = FileUtil.handlerPath(COMMON_DEL_PATH_INCLUDE[i]);
|
||||
}
|
||||
}
|
||||
// 禁删范围
|
||||
path = SysConfig.getStringVal("common.del.path.exclude");
|
||||
if(path!=null && !"".equals(path)){
|
||||
COMMON_DEL_PATH_EXCLUDE = path.split(",");
|
||||
for(int i=0; i<COMMON_DEL_PATH_EXCLUDE.length; i++){
|
||||
COMMON_DEL_PATH_EXCLUDE[i] = FileUtil.handlerPath(COMMON_DEL_PATH_EXCLUDE[i]);
|
||||
}
|
||||
}
|
||||
// 文件总路径
|
||||
String localFilePath = SysConfig.getStringVal("local.data.path");
|
||||
// ---------------Local Path
|
||||
LOCAL_SCRIPT_PATH = formatPath(SysConfig.getStringVal("local.script.path"));
|
||||
localDetecConfSuffix = SysConfig.getStringVal("local.config.file.suffix");
|
||||
localAgentPidFile = formatPath(SysConfig.getStringVal("local.agent.pidfile"));
|
||||
localDetecConfPath = localFilePath + File.separator + "nc_config";
|
||||
localPluginScriptPath = localFilePath + File.separator + "nc_detecScript";
|
||||
localDataCollection = localFilePath + File.separator + "nc_data";
|
||||
localDataFilePath = localDataCollection + File.separator + "incoming";
|
||||
localDataDonePath = localDataCollection + File.separator + "done";
|
||||
localDataErrorPath = localDataCollection + File.separator + "error";
|
||||
localBackupPath = localFilePath + File.separator + "nc_backup";
|
||||
localUploadsPath = localFilePath + File.separator + "nc_uploads";
|
||||
localTaskPath = localFilePath + File.separator + "nc_task";
|
||||
localTaskDonePath = localTaskPath + File.separator + "done";// 成功处理任务存放目录
|
||||
localTaskErrorPath = localTaskPath + File.separator + "error";// 成功处理任务存放目录
|
||||
localTaskResultPath = localTaskPath + File.separator + "incoming" + File.separator + "result";//任务结果存放目录
|
||||
localTaskReturnPath = localTaskPath + File.separator + "incoming" + File.separator + "return";//任务回传文件存放目录
|
||||
localLogsPath = formatPath(SysConfig.getLogPath());
|
||||
localTempPath = localFilePath + File.separator + "nc_temp";
|
||||
localTempDataIncomingPath = localDataCollection + File.separator + "temp";
|
||||
|
||||
// ---------------字符编码
|
||||
charset = SysConfig.getStringVal("charset");
|
||||
|
||||
// ---------------System Check Type
|
||||
SYS_CHECK_TYPE_CPU = SysConfig.getStringVal("sys.check.type.cpu", "cpu");
|
||||
SYS_CHECK_TYPE_MEMORY = SysConfig.getStringVal("sys.check.type.memory", "memory");
|
||||
SYS_CHECK_TYPE_DISK = SysConfig.getStringVal("sys.check.type.disk", "disk");
|
||||
SYS_CHECK_TYPE_NET = SysConfig.getStringVal("sys.check.type.net", "net");
|
||||
SYS_CHECK_TYPE_SYSDATE = SysConfig.getStringVal("sys.check.type.systemdate", "systemdate");
|
||||
SYS_CHECK_TYPE_PROCESS = SysConfig.getStringVal("sys.check.type.process", "process");
|
||||
SYS_CHECK_TYPE_PROCESS_NMSAGENT = SysConfig.getStringVal("sys.check.type.process.nmsagent", "nmsclient");
|
||||
SYS_CHECK_TYPE_SYSTEMINFO = SysConfig.getStringVal("sys.check.type.systeminfo", "systeminfo");
|
||||
|
||||
// --------------Common Clear Gaps
|
||||
COMMON_DEL_LOG_DAYS = SysConfig.getIntegerVal("common.del.log.days", "7");
|
||||
COMMON_DEL_DATA_HOURS = SysConfig.getIntegerVal("common.del.data.hours", "24");
|
||||
// 2013-5-20 添加功能 删除任务相关信息最小时间,不得少于一天
|
||||
if(COMMON_DEL_DATA_HOURS > 24){
|
||||
COMMON_DEL_TASK_HOURS = COMMON_DEL_DATA_HOURS;
|
||||
}else{
|
||||
COMMON_DEL_TASK_HOURS = 24;
|
||||
}
|
||||
COMMON_DEL_UPGRADEFILE_DAYS = SysConfig.getIntegerVal("common.del.upgradefile.days", "30");
|
||||
COMMON_DEL_TEMP_DAYS = SysConfig.getIntegerVal("common.del.temp.days", "2");
|
||||
COMMON_UPLOAD_DATA_MINUTES = SysConfig.getIntegerVal("common.upload.data.minutes", "5");
|
||||
COMMON_TASK_CLEAR_HOURS = SysConfig.getIntegerVal("common.task.clear.hours", "2");
|
||||
|
||||
// -------------- ThreadPool
|
||||
COMMON_THREAD_SOCKET_SIZE = SysConfig.getIntegerVal("common.thread.socket.size", "10");
|
||||
COMMON_THREAD_SCHEDULE_SIZE = SysConfig.getIntegerVal("common.thread.schedule.size", "15");
|
||||
// -------------- Compress
|
||||
COMMON_ZIP_MIN_SIZE = SysConfig.getIntegerVal("common.zip.min.size", "1000");
|
||||
COMMON_ZIP_MAX_SIZE = SysConfig.getIntegerVal("common.zip.max.size", "2000");
|
||||
COMMON_MAX_RETURN_CNT = SysConfig.getIntegerVal("common.max.return.size", "10");
|
||||
// ----------------Debug
|
||||
DEBUG_INIT_TASK_FLAG = SysConfig.getIntegerVal("debug.init.task.flag", "0");
|
||||
DEBUG_PLUGIN_FLAG = SysConfig.getIntegerVal("debug.plugin.flag", "0");
|
||||
DEBUG_SYSDETECT_FLAG = SysConfig.getIntegerVal("debug.sysdetect.flag", "0");
|
||||
DEBUG_UPLOADDATA_FLAG = SysConfig.getIntegerVal("debug.uploaddata.flag", "0");
|
||||
DEBUG_ALARM_FLAG = SysConfig.getIntegerVal("debug.alarm.flag", "0");
|
||||
DEBUG_DELFILE_FLAG = SysConfig.getIntegerVal("debug.delfile.flag", "0");
|
||||
DEBUG_TASKRESULT_FLAG = SysConfig.getIntegerVal("debug.taskresult.flag", "0");
|
||||
DEBUG_TASKRETURN_FLAG = SysConfig.getIntegerVal("debug.taskreturn.flag", "0");
|
||||
|
||||
// ACTIIVE_ALARM_START = SysConfig.getStringVal("active.alarm.start", "true");//是否启用主动告警,默认不启动主动告警---用于nc配置文件,现修改为从web控制
|
||||
|
||||
|
||||
//监测数据主动上报
|
||||
DATA_SEND_THREAD_FLAG = SysConfig.getIntegerVal("data.send.thread.flag", "0");
|
||||
//主动数据上报 IP
|
||||
DATA_SEND_THREAD_HOST = SysConfig.getStringVal("data.send.thread.host", SOCKET_SERVER_HOST);
|
||||
//主动数据上报 PORT
|
||||
DATA_SEND_THREAD_PORT = SysConfig.getIntegerVal("data.send.thread.port", "9527");
|
||||
//主动数据上报间隔 INTERVAL,单位 10 S
|
||||
DATA_SEND_THREAD_INTERVAL = SysConfig.getIntegerVal("data.send.thread.interval", "10");
|
||||
|
||||
|
||||
// 初始化创建文件夹
|
||||
if(!new File(localDetecConfPath).exists()){
|
||||
new File(localDetecConfPath).mkdirs();
|
||||
}
|
||||
if(!new File(localPluginScriptPath).exists()) {
|
||||
new File(localPluginScriptPath).mkdirs();
|
||||
}
|
||||
if(!new File(localDataCollection).exists()){
|
||||
new File(localDataCollection).mkdirs();
|
||||
}
|
||||
if(!new File(localBackupPath).exists()){
|
||||
new File(localBackupPath).mkdirs();
|
||||
}
|
||||
if(!new File(localUploadsPath).exists()){
|
||||
new File(localUploadsPath).mkdirs();
|
||||
}
|
||||
if(!new File(localTaskPath).exists()){
|
||||
new File(localTaskPath).mkdirs();
|
||||
}
|
||||
if(!new File(localTempPath).exists()){
|
||||
new File(localTempPath).mkdirs();
|
||||
}
|
||||
if(!new File(localTempDataIncomingPath).exists()){
|
||||
new File(localTempDataIncomingPath).mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatPath(String path){
|
||||
String returnPath = path;
|
||||
if(path!=null && !"".equals(path) && !new File(path).isAbsolute()){// 路径不为空且是相对路径
|
||||
returnPath = SYSTEM_PATH + File.separator + path;
|
||||
}
|
||||
return returnPath;
|
||||
}
|
||||
|
||||
public static boolean isSucessByResult(String msg){
|
||||
boolean flag = false;
|
||||
if (!StringUtils.isEmpty(msg)) {
|
||||
String[] result = msg.split(Contants.COMMON_MSG_SEPRATOR_SPLIT);
|
||||
if (result != null && result.length > 0) {
|
||||
if (Integer.parseInt(result[0])==Contants.COMMON_MSG_SUCCESS) {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static String getDescByResult(String msg){
|
||||
String desc = null;
|
||||
if (!StringUtils.isEmpty(msg)) {
|
||||
String[] result = msg.split(Contants.COMMON_MSG_SEPRATOR_SPLIT);
|
||||
if (result != null && result.length > 1) {
|
||||
desc = result[1];
|
||||
}
|
||||
}
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
152
src/com/nis/nmsclient/common/StopWatch.java
Normal file
152
src/com/nis/nmsclient/common/StopWatch.java
Normal file
@@ -0,0 +1,152 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
/**
|
||||
* 秒表计时器
|
||||
* @author fang
|
||||
*
|
||||
*/
|
||||
public class StopWatch {
|
||||
private static final long SEC_MILL = 1000;
|
||||
private static final long MIN_MILL = 60 * SEC_MILL;
|
||||
private static final long HOUR_MILL = 60 * MIN_MILL;
|
||||
private static final long DAY_MILL = 24 * HOUR_MILL;
|
||||
private long start;
|
||||
private long end;
|
||||
private LinkedHashMap<String,Long> tagMap = new LinkedHashMap<String,Long>();
|
||||
|
||||
public StopWatch(){
|
||||
start();
|
||||
}
|
||||
|
||||
public static StopWatch newStopWacth(){
|
||||
return new StopWatch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计时器开始
|
||||
* @return
|
||||
*/
|
||||
public long start(){
|
||||
this.start = System.currentTimeMillis();
|
||||
return start;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计时器结束
|
||||
* @return
|
||||
*/
|
||||
public long end(){
|
||||
this.end = System.currentTimeMillis();
|
||||
return end;
|
||||
}
|
||||
|
||||
|
||||
public long tag(String tag){
|
||||
long l = System.currentTimeMillis();
|
||||
this.tagMap.put(tag, l);
|
||||
return l;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个 tag 之间的时间差
|
||||
* @param b
|
||||
* @param a
|
||||
* @return
|
||||
*/
|
||||
public long between(String b,String a){
|
||||
Long l1 = this.tagMap.get(b);
|
||||
Long l2 = this.tagMap.get(a);
|
||||
if(l1 != null && l2 != null){
|
||||
return l1-l2;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static String toString(long l){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if(l >= DAY_MILL){
|
||||
sb.append((l/DAY_MILL));
|
||||
sb.append( "天");
|
||||
l = l % DAY_MILL;
|
||||
}
|
||||
if(l >= HOUR_MILL){
|
||||
sb.append((l/HOUR_MILL));
|
||||
sb.append( "小时");
|
||||
l = l % HOUR_MILL;
|
||||
}
|
||||
if(l >= MIN_MILL){
|
||||
sb.append((l/MIN_MILL));
|
||||
sb.append( "分");
|
||||
l = l % MIN_MILL;
|
||||
}
|
||||
if(l >= SEC_MILL){
|
||||
sb.append((l/SEC_MILL));
|
||||
sb.append( "秒");
|
||||
l = l % SEC_MILL;
|
||||
}
|
||||
|
||||
sb.append((l));
|
||||
sb.append( "毫秒");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从开始到结束总耗时
|
||||
* @return
|
||||
*/
|
||||
public long total(){
|
||||
long temp = System.currentTimeMillis();
|
||||
if(this.end < this.start){
|
||||
this.end = temp;
|
||||
}
|
||||
return end - start;
|
||||
}
|
||||
|
||||
|
||||
public void reset(){
|
||||
this.tagMap.clear();
|
||||
this.start();
|
||||
}
|
||||
|
||||
public long getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public void setStart(long start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
public long getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public void setEnd(long end) {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, Long> getTag() {
|
||||
return tagMap;
|
||||
}
|
||||
|
||||
public void LinkedHashMap(LinkedHashMap<String, Long> tag) {
|
||||
this.tagMap = tag;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
long s = System.currentTimeMillis();
|
||||
long end = s +2*DAY_MILL+ 12 * MIN_MILL + 30*SEC_MILL + 388;
|
||||
|
||||
String string = StopWatch.toString(end -s);
|
||||
System.out.println(string);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
365
src/com/nis/nmsclient/common/SysConfig.java
Normal file
365
src/com/nis/nmsclient/common/SysConfig.java
Normal file
@@ -0,0 +1,365 @@
|
||||
package com.nis.nmsclient.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 javax.swing.JOptionPane;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
|
||||
public class SysConfig {
|
||||
static Logger logger = Logger.getLogger(SysConfig.class);
|
||||
|
||||
/** ==============myconfig.properties文件获取参数=============== **/
|
||||
private static Properties myProperties;
|
||||
private static String url = null;
|
||||
static {
|
||||
URL urlObj = SysConfig.class.getClassLoader().getResource("myconfig.properties");
|
||||
if(urlObj==null){
|
||||
// JOptionPane.showMessageDialog(null, "缺少配置文件,程序无法执行!\n请先执行参数配置程序进行配置", "错误", JOptionPane.ERROR_MESSAGE);
|
||||
JOptionPane.showMessageDialog(null, "i18n_client.Sysconfig.init_n81i", "i18n_client.Sysconfig.error_n81i", JOptionPane.ERROR_MESSAGE);
|
||||
logger.error("NMSClient program termination: lack of configuration files, programs can not be executed! Please execute the configuration program for configuration first");
|
||||
System.exit(0);
|
||||
}else{
|
||||
url = urlObj.getPath().replaceAll("%20", " ");
|
||||
}
|
||||
myProperties = new Properties();
|
||||
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(url);
|
||||
myProperties.load(fis);
|
||||
} catch (IOException e) {
|
||||
logger.error("Reading myconfig.properties file error", e);
|
||||
}finally{
|
||||
try {
|
||||
if(fis!= null)fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 升级时更新参数配置使用
|
||||
updateConfigFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* 向资源配置文件host_uuid.properties中更新UUID键值
|
||||
*/
|
||||
public static void setInterfaceNameValue(String value) {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
if (uuidUrl != null && !"".equals(uuidUrl)) {
|
||||
properties.load(new FileInputStream(uuidUrl));
|
||||
// 添加或更新键值对
|
||||
properties.setProperty(AGENT_INTERFACE_NAME_KEY, value);
|
||||
// 保存到文件
|
||||
properties.store(new FileOutputStream(uuidUrl), "");
|
||||
}
|
||||
properties.clear();
|
||||
|
||||
if(value != null && !"".equals(value)){
|
||||
Contants.AGENT_INTERFACE_NAME_KEY = value;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Setting the network port name eth* attribute error", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据相应的参数配置来更新配置并写入文件
|
||||
*/
|
||||
private static void updateConfigFile(){
|
||||
FileInputStream fis = null;
|
||||
BufferedReader reader = null;
|
||||
BufferedWriter writer = null;
|
||||
try {
|
||||
ResourceBundle resource = ResourceBundle.getBundle(UpdateParams.class.getName());
|
||||
//判断是否更新properties
|
||||
String updateFlag = myProperties.getProperty(UpdateParams.CONFIG_UPDATE_FLAG,"-1");
|
||||
if(updateFlag.equals(resource.getString(UpdateParams.CONFIG_UPDATE_FLAG))){ //配置文件已经更新,退出操作
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> proList = new LinkedList<String>();
|
||||
|
||||
String encode = System.getProperty("file.encoding");
|
||||
logger.debug("----file.encoding----" + encode);
|
||||
|
||||
//读取配置文件原有的参数到proList
|
||||
fis = new FileInputStream(url);
|
||||
reader = new BufferedReader(new InputStreamReader(fis,Charset.forName(encode)));
|
||||
String str =null;
|
||||
while((str = reader.readLine() )!=null){
|
||||
proList.add(str);
|
||||
}
|
||||
|
||||
//将UpdateParams中的值更新到proList和myProperties中
|
||||
Enumeration<String> 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 strV = proList.get(i);
|
||||
if (StringUtils.isEmpty(strV)) {
|
||||
continue;
|
||||
}
|
||||
if(strV.split("=", 2)[0].trim().equals(elem)){
|
||||
if(elem.equalsIgnoreCase(UpdateParams.CONFIG_UPDATE_FLAG)){
|
||||
proList.set(i, elem + " = " + value);// 更新配置文件中某属性的值
|
||||
logger.info("参数更新:" + elem + " = " + (StringUtils.isBlank(value) ? "" : value));
|
||||
myProperties.put(elem, value);
|
||||
}
|
||||
addFlag = false;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
if(addFlag){
|
||||
proList.add(elem + " = " + value);
|
||||
logger.info("参数新增:" + elem + " = " + (StringUtils.isBlank(value) ? "" : value));
|
||||
myProperties.put(elem, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Update the configuration file myconfig.properties parameter " + elem + "error", e);
|
||||
}
|
||||
}
|
||||
|
||||
//将文件信息写入到文件中
|
||||
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url),Charset.forName(encode)));
|
||||
Iterator<String> it = proList.iterator();
|
||||
while (it.hasNext()) {
|
||||
String elem = (String) it.next();
|
||||
writer.write((elem==null?"":elem)+"\r\n");
|
||||
}
|
||||
writer.flush();
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Update configuration file myconfig.properties exception", e);
|
||||
} finally{
|
||||
try {
|
||||
if(reader!= null)reader.close();
|
||||
if(fis!= null)fis.close();
|
||||
if(writer!= null ){
|
||||
writer.close();
|
||||
writer = null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateConfigFile(String key, String value){
|
||||
FileInputStream fis = null;
|
||||
BufferedReader reader = null;
|
||||
BufferedWriter writer = null;
|
||||
try {
|
||||
List<String> proList = new LinkedList<String>();
|
||||
|
||||
String encode = System.getProperty("file.encoding");
|
||||
logger.debug("----file.encoding----" + encode);
|
||||
|
||||
//读取配置文件原有的参数到proList
|
||||
fis = new FileInputStream(url);
|
||||
reader = new BufferedReader(new InputStreamReader(fis,Charset.forName(encode)));
|
||||
String str =null;
|
||||
while((str = reader.readLine() )!=null){
|
||||
proList.add(str);
|
||||
}
|
||||
|
||||
//将值更新到proList中
|
||||
try {
|
||||
for (int i = 0; i < proList.size(); i++) {
|
||||
String strV = proList.get(i);
|
||||
if (StringUtils.isEmpty(strV)) {
|
||||
continue;
|
||||
}
|
||||
if(strV.split("=", 2)[0].trim().equals(key)){
|
||||
proList.set(i, key + " = " + value);// 更新配置文件中某属性的值
|
||||
logger.info("参数更新:" + key + " = " + (StringUtils.isBlank(value) ? "" : value));
|
||||
break ;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Update the configuration file myconfig.properties parameter " + key + "error", e);
|
||||
}
|
||||
|
||||
//将文件信息写入到文件中
|
||||
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url),Charset.forName(encode)));
|
||||
Iterator<String> it = proList.iterator();
|
||||
while (it.hasNext()) {
|
||||
String elem = (String) it.next();
|
||||
writer.write((elem==null?"":elem)+"\r\n");
|
||||
}
|
||||
writer.flush();
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Update configuration file myconfig.properties exception", e);
|
||||
} finally{
|
||||
try {
|
||||
if(reader!= null)reader.close();
|
||||
if(fis!= null)fis.close();
|
||||
if(writer!= null ){
|
||||
writer.close();
|
||||
writer = null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getStringVal(String key, String... defaultVal){
|
||||
String dStr = "";
|
||||
if(defaultVal!=null && defaultVal.length>=1 && defaultVal[0]!=null && defaultVal.length>0){
|
||||
dStr = defaultVal[0];
|
||||
}
|
||||
return myProperties.getProperty(key,dStr).trim();
|
||||
}
|
||||
|
||||
public static Integer getIntegerVal(String name, String... defaultVal){
|
||||
try {
|
||||
String val = getStringVal(name, defaultVal);
|
||||
return StringUtils.isEmpty(val)? null : Integer.parseInt(val);
|
||||
} catch (Exception e) {
|
||||
logger.error("Digital formatting error", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getSystemDir() {
|
||||
return System.getProperty("user.dir");
|
||||
}
|
||||
|
||||
public static String getLogPath(){
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
String log4jUrl = SysConfig.class.getClassLoader().getResource("log4j.properties").getPath().replaceAll("%20", " ");
|
||||
fis = new FileInputStream(log4jUrl);
|
||||
Properties log4jProperties = new Properties();
|
||||
log4jProperties.load(fis);
|
||||
String logFile = log4jProperties.getProperty("log4j.appender.logfile.File");
|
||||
if(logFile!=null){
|
||||
return new File(logFile).getParent();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("Reading log4j.properties file error"+"",e);
|
||||
}finally{
|
||||
try {
|
||||
if(fis!= null)fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return "../logs";
|
||||
}
|
||||
|
||||
/** ==============host_uuid.properties文件获取参数=============== **/
|
||||
static final String AGENT_HOST_UUID_KEY = "agent_host_uuid";
|
||||
static final String AGENT_OPERATE_SYSTEM_KEY = "agent_operate_system";
|
||||
static final String AGENT_LOCAL_IP_KEY = "agent_local_ip";
|
||||
static String uuidUrl = null;
|
||||
static final String AGENT_INTERFACE_NAME_KEY = "agent_interface_name";
|
||||
static {
|
||||
String name = "host_uuid.properties";
|
||||
// 2014-11-13 jzz modify 由于将该配置文件放于系统安装目录中容易使节点混乱,故将其放于nmsdata指定目录
|
||||
/*URL urlObj = Contants.class.getClassLoader().getResource(name);
|
||||
if(urlObj==null){
|
||||
uuidUrl = new File(url).getParent() + File.separator + name;
|
||||
}else{
|
||||
uuidUrl = urlObj.getPath().replaceAll("%20", " ");
|
||||
}*/
|
||||
String path = SysConfig.getStringVal("local.data.path") + File.separator + "nc_sysconf";
|
||||
File filePath = new File(path);
|
||||
if(!filePath.exists()){
|
||||
filePath.mkdirs();
|
||||
}
|
||||
|
||||
uuidUrl = path + File.separator + name;
|
||||
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
File file = new File(uuidUrl);
|
||||
if(!file.exists()){
|
||||
file.createNewFile();
|
||||
}
|
||||
properties.load(new FileInputStream(uuidUrl));
|
||||
String uuidStr = properties.getProperty(AGENT_HOST_UUID_KEY);
|
||||
if(uuidStr != null && !"".equals(uuidStr.trim())){
|
||||
Contants.AGENT_HOST_UUID = Long.parseLong(uuidStr.trim());
|
||||
}
|
||||
Contants.AGENT_OPERATE_SYSTEM = properties.getProperty(AGENT_OPERATE_SYSTEM_KEY);
|
||||
Contants.AGENT_LOCAL_IP = properties.getProperty(AGENT_LOCAL_IP_KEY);
|
||||
Contants.AGENT_INTERFACE_NAME_KEY = properties.getProperty(AGENT_INTERFACE_NAME_KEY);
|
||||
} catch (IOException e) {
|
||||
logger.error("Reading host_uuid.properties file error", e);
|
||||
}
|
||||
properties.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 向资源配置文件host_uuid.properties中更新UUID键值
|
||||
*/
|
||||
public static void setUUIDValue(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("Setting the UUID attribute error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向资源配置文件host_uuid.properties中更新OperateSystem和LocalIP键值
|
||||
*/
|
||||
public static void setUUIDValue(String sysTypeValue,String localIpValue) {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
if (uuidUrl != null && !"".equals(uuidUrl)) {
|
||||
properties.load(new FileInputStream(uuidUrl));
|
||||
// 添加或更新键值对
|
||||
properties.setProperty(AGENT_OPERATE_SYSTEM_KEY, sysTypeValue);
|
||||
properties.setProperty(AGENT_LOCAL_IP_KEY, localIpValue);
|
||||
// 保存到文件
|
||||
properties.store(new FileOutputStream(uuidUrl), "");
|
||||
}
|
||||
properties.clear();
|
||||
Contants.AGENT_OPERATE_SYSTEM = sysTypeValue;
|
||||
Contants.AGENT_LOCAL_IP = localIpValue;
|
||||
} catch (Exception e) {
|
||||
logger.error("Setting OperateSystemType and LocalIP attribute errors",e);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/com/nis/nmsclient/common/UpdateParams.java
Normal file
18
src/com/nis/nmsclient/common/UpdateParams.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.nis.nmsclient.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 = "5"; //更新标示 该值 缺省值为0 每次修改都要
|
||||
static final String[][] contents = new String[][]{
|
||||
{CONFIG_UPDATE_FLAG,CONFIG_UPDATE_FLAG_VALUE}, //更新标示 固定 判断配置文件指定值更新 建议自增1操作
|
||||
{"socket.timeout.minutes", "30"}, // Socket通信超时时间设置
|
||||
{"common.zip.min.size", "1000"}, // 压缩文件最少包含文件个数
|
||||
{"common.zip.max.size", "2000"}, // 压缩文件最多包含文件个数
|
||||
// {"active.alarm.start","false"}, //是否启用NC的主动告警(只针对监测数据超过设置的值时的主动告警,避免异常信息重复):true:启用,false:停用-暂不使用,修改为web端控制nc是否报主动告警
|
||||
{"alarm.set.marker.separator","|"} //监测数据设置告警时,对于指定多个标识符(如多个盘符、多个CPU、多个网卡)的分隔符
|
||||
};
|
||||
|
||||
public Object[][] getContents() {
|
||||
return contents;
|
||||
}
|
||||
}
|
||||
75
src/com/nis/nmsclient/common/VersionCfg.java
Normal file
75
src/com/nis/nmsclient/common/VersionCfg.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* 获取和保存version信息的类
|
||||
*
|
||||
*/
|
||||
public class VersionCfg {
|
||||
private static Logger logger = Logger.getLogger(VersionCfg.class);
|
||||
public static final String NAGENT_VERSION = "NA_version";
|
||||
public static final String NSERVER_VERSION = "NS_version";
|
||||
|
||||
private static String url = null;
|
||||
private static Properties properties;
|
||||
|
||||
static {
|
||||
url = VersionCfg.class.getClassLoader().getResource("version.properties").getPath().replaceAll("%20", " ");
|
||||
|
||||
FileInputStream fis = null;
|
||||
properties = new Properties();
|
||||
try {
|
||||
fis = new FileInputStream(url);
|
||||
properties.load(fis);
|
||||
} catch (IOException e) {
|
||||
logger.error("Reading version.properties file error", e);
|
||||
}finally{
|
||||
try{
|
||||
if(fis!=null){
|
||||
fis.close();
|
||||
}
|
||||
}catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取version值
|
||||
*
|
||||
*/
|
||||
public static String getValue(String key) {
|
||||
return properties.getProperty(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向资源配置文件中添加或更新version键值对
|
||||
*/
|
||||
public static void setValue(String key, String value) {
|
||||
logger.debug("setVersion----->" + key + "=" + value);
|
||||
// 添加或更新键值对
|
||||
properties.setProperty(key, value);
|
||||
logger.debug("properties.getProperty(\"" + key + "\")----->" + value);
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(url);
|
||||
// 保存到文件
|
||||
if (url != null && !"".equals(url)) {
|
||||
properties.store(fos, "");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e);
|
||||
} finally{
|
||||
try{
|
||||
if(fos!=null){
|
||||
fos.flush();
|
||||
fos.close();
|
||||
}
|
||||
}catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user