initial commit

This commit is contained in:
chenjinsong
2018-09-27 16:11:54 +08:00
commit 56d71f261a
143 changed files with 23523 additions and 0 deletions

View File

@@ -0,0 +1,549 @@
package com.nis.nmsclient;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.common.SysConfig;
import com.nis.nmsclient.config.DetecConfReqHandle;
import com.nis.nmsclient.model.AlarmInfo;
import com.nis.nmsclient.model.SetInfo;
import com.nis.nmsclient.thread.WritePidThread;
import com.nis.nmsclient.thread.alarm.AlarmThread;
import com.nis.nmsclient.thread.alarm.AlarmUtil;
import com.nis.nmsclient.thread.alarm.ErrorCode;
import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.thread.socket.SSLClient;
import com.nis.nmsclient.thread.socket.SSLServer;
import com.nis.nmsclient.thread.task.TaskResultOper;
import com.nis.nmsclient.thread.timer.DelLocalFileThread;
import com.nis.nmsclient.thread.upload.DataSendThread;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.ProcessUtil;
import com.nis.nmsclient.util.Utils;
/**
* NMSClient 程序启动主方法类
*/
public class NmsClient{
static Logger logger = Logger.getLogger(NmsClient.class);
// 监测设置信息,初始化完成后清空
public List<SetInfo> setInfos = new LinkedList<SetInfo>();
// 监测设置的报警字段设置信息,初始化完成后清空
public Map<Long, List<AlarmInfo>> alarmInfos = new HashMap<Long, List<AlarmInfo>>();
private int testGap = 60; //单位:秒
static{
// Thread.currentThread().setName("NMSClient主程序");
Thread.currentThread().setName("NMSClient Main Program");
}
/**
* NMSClient 程序启动入口
*/
public static void main(String[] args) {
logger.info("------- NMSClient 启动开始------------");
//NMSClient进程停止保存缓存操作
doShutDownWork();
/**
* 启动通信程序, 如果端口已存在即SSLServer创建失败退出程序
*/
//通讯端口放在第一步为了确保初始化配置不成功或个数为0时 新建任务下发、DC收集数据、握手等操作 能正常执行
//注意要在通讯程序中判断SeqId是否为空为空 则只接收握手通信、收集数据通信,其他的通信都抛弃
SSLServer sslServer = null;
try {
sslServer = new SSLServer();
} catch (IOException e) {
TaskResultOper.handerAgentUpgradeResult(false);
logger.error("NMSClient Program termination" + e.getMessage());
AlarmUtil.sendNMSErrorMsg(ErrorCode.ProtListenerError, Utils.getLocalIp(), "i18n_client.NmsClient.ncCommunicatePortErr_n81i");
System.exit(0);
}
// Thread server = new Thread(sslServer, "通讯线程");
Thread server = new Thread(sslServer, "Communication Thread");
server.start();
/**
* 启动守护进程
*/
try {
String os = System.getProperty("os.name");
String cmd = "";
String procSearchKey = null;
if (os.startsWith("Windows")) {
String homePath = new File(Contants.SYSTEM_PATH).getParent();//NC布署路径
cmd = homePath + File.separator + "script" + File.separator + "nmsclient_shouhu.bat";
procSearchKey = "nmsclient_shouhu.bat";
} else if (os.startsWith("Linux")) {
cmd = Contants.SYSTEM_PATH + File.separator + "nmsclient_shouhu.sh";
procSearchKey = "nmsclient_shouhu.sh";
}
Object[] objArr = ProcessUtil.checkPidAndGetPid(null, procSearchKey);
int isExistFlag = Integer.parseInt(objArr[0].toString());
if(isExistFlag == 0){// 守护进程不存在,启动
logger.info("正在启动守护进程...");
ProcessUtil.runExec(cmd, new String[]{Contants.localTaskResultPath}, null, null, true);
objArr = ProcessUtil.checkPidAndGetPid(null, procSearchKey);
isExistFlag = Integer.parseInt(objArr[0].toString());
if(isExistFlag != 0){
logger.info("守护进程 启动成功");
}else{
logger.info("守护进程 启动失败");
AlarmUtil.sendNMSErrorMsg(ErrorCode.DeamonNotExist, Utils.getLocalIp(), "i18n_client.NmsClient.ncDeamonStartFail_n81i");
}
}else {
logger.info("守护进程 已存在,无需再启动");
}
} catch (Exception e) {
logger.error("Start the daemon exception", e);
AlarmUtil.sendNMSErrorMsg(ErrorCode.DeamonNotExist, Utils.getLocalIp(), "i18n_client.NmsClient.ncDeamonStartException_n81i" + e.getMessage());
}
/**
* 相关业务操作入口
*/
new NmsClient().run();
}
public void run() {
//执行写PID线程
Common.service.submit(new WritePidThread());
// 2013-3-8 由于初始化监测配置个数为0时不断重新获取致使任务初始化无法执行先将任务与监测配置分开执行
// 为了将初始化监测配置放于任务之后所以在开始执行之前先与DC握手通讯以保证通讯正常
while (true) {
try {
//与Server通信
Future<?> serFuture = Common.service.submit(new SSLClient(
Thread.currentThread().getName(),
CommonSocket.REQ_HAND_SHAKE, null));
if (Contants.isSucessByResult((String) serFuture.get())) {
break;
}
} catch (Exception e) {
logger.error("Handshake communication abnormality" + Utils.printExceptionStack(e));
}
try {
Thread.sleep(1000 * testGap);// 如果握手失败让当前线程暂停N秒再重试
} catch (InterruptedException e) {
logger.error(Utils.printExceptionStack(e));
continue;
}
}
// 获取本机唯一标识
initUUID();
// 检查本机操作系统和IP是否变更
checkLocalOperSystemAndIp();
/**************************** 任务部分处理操作 ***************************/
// 处理Agent自身升级时的执行结果文件
TaskResultOper.handerAgentUpgradeResult(true);
// 发送所有之前上发失败的任务结果
TaskResultOper.initSendAllTaskResult();
// 初始化执行中的任务
if (Contants.DEBUG_INIT_TASK_FLAG == 0) {
// Common.scheduled.schedule(new SSLClient("初始化任务",
Common.scheduled.schedule(new SSLClient("Initialization Task",
CommonSocket.REQ_INIT_TASK, null),
Contants.COMMON_TASK_INIT_DELAY_MINUTES, TimeUnit.MINUTES);
}
// 定时上传发送失败的任务结果
/*if (Contants.DEBUG_TASKRESULT_FLAG == 0) {
Common.scheduled.scheduleWithFixedDelay(new Runnable() {
public void run() {
Thread.currentThread().setName("上传任务结果");
TaskResultOper.initSendAllTaskResult();
}
}, 0, Contants.COMMON_TASK_RESULT_SEND_MINUTES, TimeUnit.MINUTES);
}*/
// 定时上传发送失败的回传文件
/*if (Contants.DEBUG_TASKRETURN_FLAG == 0) {
Common.scheduled.scheduleWithFixedDelay(new Runnable() {
public void run() {
Thread.currentThread().setName("回传文件");
new TaskReturnHandle().sendAllTaskReturnFile();
}
}, 0, Contants.COMMON_TASK_RESULT_SEND_MINUTES, TimeUnit.MINUTES);
}*/
// 定时清理内存中已完成的任务
Common.scheduled.scheduleWithFixedDelay(new Runnable() {
public void run() {
// Thread.currentThread().setName("清理已完成任务");
Thread.currentThread().setName("Clean Up The Completed Task");
// == 1、针对结果文件过多时打包上传未完成的文件
File taskDir = new File(Contants.localTaskPath);
if (taskDir.exists()) {
// ----取所有未上传完成的Zip文件
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
// 若存在未上传完成的ZIP结果文件则不清理内存中的任务
if (zipArr.length > 0) {
return;
}
}
// == 2、检查当前结果文件数量
File resultDir = new File(TaskResultOper.getTaskResultPath());
if(resultDir.exists()){
File[] fileArr = FileUtil.getFilesEndWith(resultDir, Contants.TASK_RESULT_FILE_SUFFIX);
// 若存在未上传的结果文件,则不清理内存中的任务
if(fileArr.length > 0){
return;
}
}
// -- 清理已完成的任务,待考虑 以后定时清理方案
Common.removeCancelAndDoneTaskFuture();
}
}, Contants.COMMON_TASK_CLEAR_HOURS, Contants.COMMON_TASK_CLEAR_HOURS, TimeUnit.HOURS);
/**************************** 定时清理本地文件操作 ***************************/
// 定时删除本地生成的文件
if (Contants.DEBUG_DELFILE_FLAG == 0) {
//2012-4-28 将所有删除文件的线程合并为一个,取设置的所有清理文件间隔中的最小值作为检查间隔
//2012-12-17 第一次执行删除文件时间不是启动立即删除设置延迟到启动之后的第一个凌晨0点
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long delay = cal.getTimeInMillis()-System.currentTimeMillis();
// Common.scheduled.scheduleAtFixedRate(new DelLocalFileThread("删除文件"), delay,
Common.scheduled.scheduleAtFixedRate(new DelLocalFileThread("Delete Files"), delay,
getMinCheckPeriod() * 60 * 60 * 1000, TimeUnit.MILLISECONDS);
}
/**************************** 监测配置信息处理操作 ***************************/
// 初始化监测配置信息
initDetecConfig();
// 启动三方监测程序
if (Contants.DEBUG_PLUGIN_FLAG == 0) {
//-------------初始化三方监测 start
for (SetInfo setInfo : setInfos) {
// 缓存三方监测配置信息,用于合并临时结果文件
Common.putPluginDetecSetInfo(setInfo.getId(), setInfo);
if (!Common.COMMON_SYS_SETINFO.equals(setInfo.getIsSchedule())) { // 第三方且由Agent启动
Common.startPluginDetec(setInfo);
}
}
//-------------初始化三方监测 end
}
//启动预设监测程序
if (Contants.DEBUG_SYSDETECT_FLAG == 0) {
//-------------初始化预设监测 start
for (SetInfo setInfo : setInfos) {
if (Common.COMMON_SYS_SETINFO.equals(setInfo.getIsSchedule())) {// 判断如果是系统预设类型
Common.addOrUpdateSysDetec(setInfo, alarmInfos
.get(setInfo.getId()));
}
}
//-------------初始化预设监测 end
}
//启用监测主动上报
if(Contants.DATA_SEND_THREAD_FLAG == 0){
Common.scheduled.scheduleWithFixedDelay(new DataSendThread(Contants.DATA_SEND_THREAD_HOST, Contants.DATA_SEND_THREAD_PORT), (int)(Math.random()*Contants.DATA_SEND_THREAD_INTERVAL), Contants.DATA_SEND_THREAD_INTERVAL, TimeUnit.SECONDS);
logger.info("监测主动上报已成功添加到线程池");
}
// 启动上传数据程序
/*if (Contants.DEBUG_UPLOADDATA_FLAG == 0) {
Common.scheduled.scheduleWithFixedDelay(new UploadDataThread("上传数据"), 1,
Contants.COMMON_UPLOAD_DATA_MINUTES, TimeUnit.MINUTES);
}*/
// 启动主动报警程序
if (Contants.DEBUG_ALARM_FLAG == 0) {
// Common.scheduled.scheduleAtFixedRate(new AlarmThread("主动预警"), 1,
Common.scheduled.scheduleAtFixedRate(new AlarmThread("Active Early Warning"), 1,
Contants.COMMON_UPLOAD_DATA_MINUTES, TimeUnit.MINUTES);
}
//清空变量
setInfos.clear();
setInfos = null;
alarmInfos.clear();
alarmInfos = null;
}
/**
* 第一次布署NMSAgent时初始化本机唯一标志
*/
public void initUUID() {
if (Contants.AGENT_HOST_UUID == null) {// 第一次布署Agent
while (true) {
String uuid = null;
try {
Future<?> future = Common.service.submit(new SSLClient(
// "获取本机标识", CommonSocket.REQ_LOCAL_UUID, null));
"Obtain The Local Identity", CommonSocket.REQ_LOCAL_UUID, null));
String msg = (String) future.get();
if (Contants.isSucessByResult(msg)) {
// dc发送的数据格式为 uuid:::localIp
String[] result = Contants.getDescByResult(msg).split(
":::");
uuid = result[0];
String localIp = result[1];
logger.info("本机标识ID" + uuid);
if (!(uuid == null || "".equals(uuid)
|| "null".equals(uuid) || localIp == null
|| "".equals(localIp) || "null".equals(localIp))) {
SysConfig.setUUIDValue(uuid);
//根据ip地址获取端口名称
String name = Utils.getNetInterfaceNameByIp(localIp);
//将端口名称写入配置文件
SysConfig.setInterfaceNameValue(name);
break;
}
}
} catch (Exception e) {
logger.error("Get the unique identity of the native or IP port name exception" + Utils.printExceptionStack(e));
}
try {
logger.debug((1000 * testGap ) +"s 后重试");
Thread.sleep(1000 * testGap);// 如果获取失败让当前线程暂停N秒再重试
} catch (InterruptedException e) {
logger.error(Utils.printExceptionStack(e));
}
}
}
}
/**
* 每次启动时检查本机操作系统类型和IP是否有变更若有则将最新信息写入文件并发送信息到Server
* 由Server端根据SeqID更新所有相关节点信息
*/
public void checkLocalOperSystemAndIp() {
// ---- 取本机相关信息
String operateSysType = getOperateSysType();
String localIp = Utils.getIpAddressByEthName(Contants.AGENT_INTERFACE_NAME_KEY);
logger.info("本机通讯IP" + localIp);
if (localIp != null && !"".equals(localIp) && !"null".equals(localIp)) {
// ----- 与原有信息比较
if (Contants.AGENT_OPERATE_SYSTEM == null
|| Contants.AGENT_LOCAL_IP == null
|| !operateSysType.equals(Contants.AGENT_OPERATE_SYSTEM)
|| !localIp.equals(Contants.AGENT_LOCAL_IP)) {
// 若有变更则将本机系统和IP写入文件再发送到Server
SysConfig.setUUIDValue(operateSysType, localIp);
// 发送UUID、SystemType、LocalIp到Server
String sendMsg = Contants.AGENT_HOST_UUID
+ Contants.COMMON_MSG_SEPRATOR
+ Contants.AGENT_OPERATE_SYSTEM;
try {
Future<?> future1 = Common.service.submit(new SSLClient(
// "信息变更", CommonSocket.REQ_LOCAL_CHANGE, sendMsg));
"Information Change", CommonSocket.REQ_LOCAL_CHANGE, sendMsg));
String resultMsg = (String) future1.get();
String descMsg = Contants.getDescByResult(resultMsg);
if (!Contants.isSucessByResult(resultMsg)) {
if (descMsg == null || "".equals(descMsg)
|| "null".equals(descMsg)) {
// descMsg = "信息变更出现问题可能存在重复IP请手动检查";
descMsg = "Information changes may occur. Duplicate IP may exist. Please check manually.";
}
logger.error("Failure of information change" + descMsg);
} else if (descMsg != null && !"".equals(descMsg)) {
logger.info("信息变更:" + descMsg);
}
} catch (Exception e) {
logger.error("Information change" + Utils.printExceptionStack(e));
}
}
}
}
/**
* 获取本机操作系统类型
*
* @return 1代表linux 2代表windows “” 为其它
*/
public static String getOperateSysType() {
String operateSysType = null;
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
operateSysType = "2";
} else if (os.startsWith("Linux")) {
operateSysType = "1";
} else {
operateSysType = "";
}
return operateSysType;
}
// /**
// * 第一次布署NMSAgent时初始化本机唯一标志
// */
// public void initUUID(){
// if(Contants.AGENT_HOST_UUID == null){//第一次布署Agent
// while (true) {
// String uuid = null;
// try {
// Future<?> future = Common.service.submit(new SSLClient(
// "获取本机标识", CommonSocket.REQ_LOCAL_UUID, null));
// String msg = (String) future.get();
// if (Contants.isSucessByResult(msg)) {
// uuid = Contants.getDescByResult(msg);
// logger.info("本机标识ID" + uuid);
// if(uuid != null && !"".equals(uuid) && !"null".equals(uuid)){
// SysConfig.setUUIDValue(uuid);
// break;
// }
// }
// } catch (Exception e) {
// logger.error("获取本机唯一标识异常:" + Utils.printExceptionStack(e));
// }
//
// try {
// Thread.sleep(1000 * testGap);// 如果获取失败让当前线程暂停N秒再重试
// } catch (InterruptedException e) {
// logger.error(Utils.printExceptionStack(e));
// }
// }
// }
// }
//
// /**
// * 每次启动时检查本机操作系统类型和IP是否有变更若有则将最新信息写入文件并发送信息到Server由Server端根据SeqID更新所有相关节点信息
// */
// public void checkLocalOperSystemAndIp(){
// //---- 取本机相关信息
// String operateSysType = null;
// String os = System.getProperty("os.name");
// if (os.startsWith("Windows")) {
// operateSysType = "2";
// }else if (os.startsWith("Linux")){
// operateSysType = "1";
// }else{
// operateSysType = "";
// }
// String localIp = Utils.getLocalIp();
// //----- 与原有信息比较
// if (Contants.AGENT_OPERATE_SYSTEM == null
// || Contants.AGENT_LOCAL_IP == null
// || !operateSysType.equals(Contants.AGENT_OPERATE_SYSTEM)
// || !localIp.equals(Contants.AGENT_LOCAL_IP)) {
// //若有变更则将本机系统和IP写入文件再发送到Server
// SysConfig.setUUIDValue(operateSysType, localIp);
// //发送UUID、SystemType、LocalIp到Server
// String sendMsg = Contants.AGENT_HOST_UUID
// + Contants.COMMON_MSG_SEPRATOR
// + Contants.AGENT_OPERATE_SYSTEM;
// try {
// Future<?> future = Common.service.submit(new SSLClient("信息变更",
// CommonSocket.REQ_LOCAL_CHANGE, sendMsg));
// String resultMsg = (String) future.get();
// String descMsg = Contants.getDescByResult(resultMsg);
// if (!Contants.isSucessByResult(resultMsg)) {
// if(descMsg == null || "".equals(descMsg) || "null".equals(descMsg)){
// descMsg = "信息变更出现问题可能存在重复IP请手动检查";
// }
// logger.error("信息变更失败:" + descMsg);
// }else if(descMsg!=null && !"".equals(descMsg)){
// logger.info("信息变更:" + descMsg);
// }
// } catch (Exception e) {
// logger.error("信息变更:" + Utils.printExceptionStack(e));
// }
// }
// }
/**
* 请求获得初始化监测配置信息: 获取失败或获取配置个数为0则不断循环获取
*/
public void initDetecConfig(){
while (true) {
try {
Future<?> future = Common.service.submit(new SSLClient(
// "初始化监测配置", CommonSocket.REQ_INIT_CONFIG, null));
"Initialization Of Monitoring Configuration", CommonSocket.REQ_INIT_CONFIG, null));
String msg = (String) future.get();
if (Contants.isSucessByResult(msg)) {
msg = Contants.getDescByResult(msg);
new DetecConfReqHandle().handlerConfigByInit(msg, setInfos, alarmInfos);
logger.info("初始化监测配置个数:" + setInfos.size());
if(setInfos.size()>0){
break;
}
}
} catch (Exception e) {
logger.error("Initialization of monitoring configuration exceptions" + Utils.printExceptionStack(e));
}
try {
Thread.sleep(1000 * testGap);//如果初始化失败让当前线程暂停N秒再重试
} catch (InterruptedException e) {
logger.error(Utils.printExceptionStack(e));
}
}
}
/**
* 取设置的所有清理文件间隔中的最小值, 单位:小时
*/
public int getMinCheckPeriod(){
int period = Contants.COMMON_DEL_DATA_HOURS;
period = period < Contants.COMMON_DEL_LOG_DAYS * 24 ? period
: Contants.COMMON_DEL_LOG_DAYS * 24;
period = period < Contants.COMMON_DEL_TEMP_DAYS * 24 ? period
: Contants.COMMON_DEL_TEMP_DAYS * 24;
period = period < Contants.COMMON_DEL_UPGRADEFILE_DAYS * 24 ? period
: Contants.COMMON_DEL_UPGRADEFILE_DAYS * 24;
logger.debug("=========del file period=" + period);
return period;
}
/**
* 进程停用时,触发该事件,将缓存数据存入硬盘
* 在NC侧 当前情况是当NMSClient服务停止时无需做操作因为NC启动的时候NC会将执行失败的任务结果发送给DCTaskResultOper.initSendAllTaskResult()
* 再收集DC的任务不会有数据丢失。
*
* 暂时无需做操作,先提供退出触发的机制,供后续使用
*
* @author jinshujuan Jul 15, 2013
* @version 1.0
*/
public static void doShutDownWork() {
logger.info("注册程序退出事件");
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
// Thread.currentThread().setName("退出NMSClient缓存数据清理线程");
Thread.currentThread().setName("Exit NMSClient, Caching Data Cleaning Thread");
logger.info("停止NMSClient处理缓存数据。。。");
//清理缓存数据Common类控制
//logger.info("清空缓存");
logger.info("停止NMSClient处理缓存数据 完成");
} catch (Exception ex) {
logger.error("Stop NMSClient, cache data entry anomalies, cache data to disk.", ex);//1.全部入库,入库异常时存盘 或 2.全部存盘,下次启动时入库
//缓存数据存入硬盘操作
//logger.info("保存缓存");
logger.error("Stop NMSClient, cache data entry anomalies, cache data stored in hard disk to complete", ex);//1.全部入库,入库异常时存盘 或 2.全部存盘,下次启动时入库
}
}
});
}
}

View 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)
};
}
}

View 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;
}
}

View 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);
}
}

View 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);
}
}
}

View 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;
}
}

View 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) {}
}
}
}

View File

@@ -0,0 +1,58 @@
package com.nis.nmsclient.config;
import java.io.File;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.model.SetInfo;
public class DetecConfOper {
static Logger logger = Logger.getLogger(DetecConfOper.class);
public static File getConfigFile(File dir, String fileName) {
File tmp = null;
if (dir.isDirectory()) {
for (File f : dir.listFiles()) {
if (!f.isDirectory()) {
if (f.getName().equals(fileName)) {
tmp = f;
break;
}
}
}
}
return tmp;
}
public static String getFileName(String checkType, String processIden,
String suffix) {
String fileName = "";
if (checkType != null && !"".equals(checkType)) {
fileName += checkType;
}
if (processIden != null && !"".equals(processIden)) {
fileName += "_" + processIden;
}
if (suffix != null && !"".equals(suffix)) {
fileName += suffix;
}
return fileName;
}
public static boolean isProcess(SetInfo setInfo){
boolean flag = false;
if("1".equals(setInfo.getIsSchedule())){//非预设监测
flag = true;
}else if(Contants.SYS_CHECK_TYPE_PROCESS.equalsIgnoreCase(setInfo.getCheckTypeName())){
flag = true;
}
return flag;
}
public static String getProcess(SetInfo setInfo){
return setInfo.getProcessPath();// + File.separator + setInfo.getProcessFile();
}
}

View File

@@ -0,0 +1,297 @@
package com.nis.nmsclient.config;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.model.AlarmInfo;
import com.nis.nmsclient.model.SetInfo;
import com.nis.nmsclient.thread.alarm.AlarmUtil;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.FileWrUtil;
import com.nis.nmsclient.util.ProcessUtil;
import com.nis.nmsclient.util.Utils;
public class DetecConfReqHandle {
Logger logger = Logger.getLogger(DetecConfReqHandle.class);
/**
* 初始化监测配置
* 处理接收到监测配置信息:解析成自定义的对象
* @param str JSONObject串
*/
public void handlerConfigByInit(String str, List<SetInfo> setInfos, Map<Long, List<AlarmInfo>> alarmInfos) {
// -- 参数处理
if (str == null || str.trim().length() <= 0) {
return;
}
if(setInfos==null || alarmInfos == null){
return;
}
setInfos.clear();
alarmInfos.clear();
// -- 初始化配置环境处理
File file = new File(Contants.localDetecConfPath);
if (!file.exists()) {// 判断存放目录是否存在,不存创建
file.mkdirs();
}
File[] cfgFiles = file.listFiles();
for (int i = 0; i < cfgFiles.length; i++) {//清空所有配置文件
// 使用删除文件公共方法
FileUtil.delDir(cfgFiles[i]);
logger.debug("handlerConfigByInit()--delete file=" + cfgFiles[i].getAbsolutePath());
}
// -- 监测配置解析
JSONObject jsonObj = JSONObject.fromObject(str);
List<SetInfo> setList = null;
if (str.toLowerCase().contains("setinfo")) {
JSONArray jsonArr = jsonObj.getJSONArray("setInfo");
setList = handerSetInfo(jsonArr);
}
if (setList == null || setList.size() == 0) {
return;
}
Map<Long, List<AlarmInfo>> alarmMap = null;
if (str.toLowerCase().contains("alarminfo")) {
JSONArray jsonArr = jsonObj.getJSONArray("alarmInfo");
alarmMap = handerAlarmInfo(jsonArr, setList);
}
// 20170413 zbc 缓存AlarmInfo信息cfg文件中无报警单位字段
if(alarmMap != null) {
Common.putAllDetecAlarmInfo(alarmMap);
}
for (SetInfo setInfo : setList) {
if ("1".equals(setInfo.getCheckState())
&& "1".equals(setInfo.getCheckWay())) {// 有效,被动方式获取
setInfos.add(setInfo);
Common.addOrUpdateAlarmPO(AlarmUtil.getAlarmPO(setInfo));
}
}
if (alarmMap != null && alarmMap.size()>0) {
alarmInfos.putAll(alarmMap);
}
if (str.toLowerCase().contains("showautoalarm")) {
Object obj = jsonObj.get("showAutoAlarm");
if (obj != null) {
Contants.ACTIIVE_ALARM_START = (Boolean) obj;
}
}
}
/**
* 更新监测配置:先解析成自定义的对象,再根据对象信息添加、更新、停用监测
* 注三方监测停用时Agent根据指定的PID杀掉进程
* @param str JSONObject串
*/
public void handlerConfigByUpdate(String str) {
if (str == null || str.trim().length() <= 0) {
return;
}
File file = new File(Contants.localDetecConfPath);
if (!file.exists()) {// 判断存放目录是否存在,不存创建
file.mkdirs();
}
JSONObject jsonObj = JSONObject.fromObject(str);
/*long flag = -1;
if (str.toLowerCase().contains("number")) {
flag = jsonObj.getLong("number");
}*/
List<SetInfo> setList = null;
if (str.toLowerCase().contains("setinfo")) {
JSONArray jsonArr = jsonObj.getJSONArray("setInfo");
setList = handerSetInfo(jsonArr);
}
if (setList == null || setList.size() == 0) {
return;
}
Map<Long, List<AlarmInfo>> alarmMap = null;
if (str.toLowerCase().contains("alarminfo")) {
JSONArray jsonArr = jsonObj.getJSONArray("alarmInfo");
alarmMap = handerAlarmInfo(jsonArr, setList);
}
for (SetInfo setInfo : setList) {
if ("0".equals(setInfo.getCheckState())
|| "0".equals(setInfo.getCheckWay())) {// 将有效置为无效,或者将被动设为主动,停止线程
Common.removeAlarmPO(setInfo.getId());
if ("0".equals(setInfo.getIsSchedule())) {
Common.stopSysDetec(setInfo);
} else {
Common.stopPluginDetec(setInfo);
}
} else {
Common.addOrUpdateAlarmPO(AlarmUtil.getAlarmPO(setInfo));
if ("0".equals(setInfo.getIsSchedule())) {// 如果是系统预设监测类型,则修改相应的监控线程
List<AlarmInfo> alarmList = null;
if (alarmMap != null) {
alarmList = alarmMap.get(setInfo.getId());
}
Common.addOrUpdateSysDetec(setInfo, alarmList);
} else { // 第三方监测
// 缓存三方监测配置信息,用于合并临时结果文件
Common.putPluginDetecSetInfo(setInfo.getId(), setInfo);
if(!Common.COMMON_SYS_SETINFO.equals(setInfo.getIsSchedule())) { // 由Agent启动的第三方插件
Common.startPluginDetec(setInfo);
}
}
}
}// for end
}
/**
* 解析监测设置基本信息
* 1、相应类型的监测数据目录不存在创建若是Linux并赋读写权限
* 2、监测配置文件存在先删除再重写停用监测不再重写
*/
private List<SetInfo> handerSetInfo(JSONArray jsonArr) {
// 文件路径
List<SetInfo> setList = new ArrayList<SetInfo>();
for (int i = 0; i < jsonArr.size(); i++) {
Object obj = JSONObject.toBean(JSONObject
.fromObject(jsonArr.get(i)), SetInfo.class);
if (obj == null) {
continue;
}
SetInfo setInfo = (SetInfo) obj;
// 创建建相应类型的数据文件夹
File dataDir = new File(Contants.localDataFilePath
+ File.separator
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
setInfo.getProcessIden(), null));
if (!dataDir.exists()) {
dataDir.mkdirs();
logger.debug("handerSetInfo()--create dataDir=" +dataDir.getAbsolutePath());
}
// 文件名
String fileName = DetecConfOper.getFileName(
setInfo.getCheckTypeName(), setInfo.getProcessIden(),
Contants.localDetecConfSuffix);
// 判断文件是否存在,如果存在删除
File cfgfile = new File(Contants.localDetecConfPath + File.separator
+ fileName);
if (cfgfile.exists()){
//cfgfile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(cfgfile);
logger.debug("handerSetInfo()--delete cfgfile=" + cfgfile.getAbsolutePath());
//FileUtil.checkParentDirExist(cfgfile);
}
if ("1".equals(setInfo.getCheckState())
&& "1".equals(setInfo.getCheckWay())) {//有效且被动方式获取的写入配置文件
String dataDirStr = null;
try {// 为了去除路径中的"." 和 ".."
dataDirStr = dataDir.getCanonicalPath();
} catch (IOException e) {
dataDirStr = dataDir.getAbsolutePath();
}
try {
FileWrUtil.cfgFilePrinter(cfgfile, Contants.charset,
setInfo.getValArr(Contants.AGENT_HOST_UUID, dataDirStr));
logger.debug("handerSetInfo()--write cfgfile=" + cfgfile.getAbsolutePath());
} catch (Exception e) {
logger.error("Write the document“" + cfgfile.getAbsolutePath() + "”error: " + Utils.printExceptionStack(e));
continue;
}
}
setList.add(setInfo);
}// for end
//修改数据目录的权限
ProcessUtil.permit(777, new File(Contants.localDataFilePath));
return setList;
}
/**
* 解析监测设置的告警信息
* 1、将同一监测的所有告警信息整理放到一个列表
* 2、根据监测设置类型和设置名称找配置文件找到了将告警信息追加到配置文件
* 3、只要有监测设置不管有没有设置告警信息都要将相应字段追加到配置文件只是无告警信息时配置字段为空
*/
private Map<Long, List<AlarmInfo>> handerAlarmInfo(JSONArray jsonArr, List<SetInfo> setList) {
if(setList==null || setList.size()==0){
logger.info("无监测设置信息,无需处理报警设置");
return null;
}
File dir = new File(Contants.localDetecConfPath);
if (!dir.exists()) {// 判断存放目录是否存在, 不存在返回
logger.error("Please check whether the alarm settings information is consistent with the monitoring settings information");
return null;
}
Map<Long, List<AlarmInfo>> alarmMap = new HashMap<Long, List<AlarmInfo>>();
if(jsonArr!=null){
for (int i = 0; i < jsonArr.size(); i++) {
Object obj = JSONObject.toBean(JSONObject
.fromObject(jsonArr.get(i)), AlarmInfo.class);
if (obj != null) {
AlarmInfo alarmInfo = (AlarmInfo) obj;
if (alarmMap.containsKey(alarmInfo.getSetInfoId())) {// 如果报警信息中已经存在相应类型的报警则追加
List<AlarmInfo> aList = alarmMap.get(alarmInfo.getSetInfoId());
aList.add(alarmInfo);
alarmMap.put(alarmInfo.getSetInfoId(), aList);
} else {
List<AlarmInfo> aList = new ArrayList<AlarmInfo>();
aList.add(alarmInfo);
alarmMap.put(alarmInfo.getSetInfoId(), aList);
}
}
}
}
Map<Long, List<AlarmInfo>> alarmMapNew = new HashMap<Long, List<AlarmInfo>>();
// 依次查看监测设置信息不管有没有设置告警信息都要写配置police=,若无告警信息,配置字段值为空
for(SetInfo setInfo : setList){
Long key = setInfo.getId();
List<AlarmInfo> alarmList = alarmMap.get(key);
// 配置信息保存对应的文件名
String fileName = DetecConfOper.getFileName(
setInfo.getCheckTypeName(), setInfo.getProcessIden(),
Contants.localDetecConfSuffix);
File f = DetecConfOper.getConfigFile(dir, fileName);
{// 追加报警设置到相应的监测配置文件
if (f != null && f.exists())
try {
FileWrUtil.cfgFileAppender(f,
Contants.charset, new String[]{getAlarmArr(alarmList)});
if(alarmList!=null){// 只有成功保存了文件并且设置为告警信息的,才返回给上一方法
alarmMapNew.put(key, alarmList);
}
} catch (Exception e) {
logger.error("Write the document“" + f.getAbsolutePath() + "”error: " + Utils.printExceptionStack(e));
}
}
}
return alarmMapNew;
}
private static String getAlarmArr(List<AlarmInfo> alarmList) {
StringBuffer sb = new StringBuffer();
sb.append("police=");
if (alarmList != null && alarmList.size() > 0) {
for (AlarmInfo alarmInfo : alarmList) {
sb.append(alarmInfo.toStringVal() + ",");
}
sb.delete(sb.toString().length() - 1, sb.toString().length());
}
return sb.toString();
}
}

View File

@@ -0,0 +1,184 @@
package com.nis.nmsclient.model;
/**
* 监测信息报警相关信息实体
*
*/
public class AlarmInfo {
/**
* 告警设置id
*/
private Long id;
/**
* 监测设置id
*/
private Long setInfoId;
/**
* 监测类型
*/
private String checkType;
/**
* 进程设置名称
*/
private String processIden;
/**
* 字段Id
*/
private Long metadataId;
/**
* 字段描述
*/
private String filedCommonts;
/**
* 字段序号
*/
private Integer showNum;
/**
* 报警状态
*/
private String policeState;
/**
* 报警值
*/
private String policeValue;
/**
* 报警单位
*/
private String policeUnit;
/**
* 报警比较符针对number型数据>、<、>=、<=、= 针对字符串类型数据equal、 include、exclude
*/
private String policeSysmbols;
/**
* 报警等级
*/
private Integer policeLevel;
/**
* 设置告警时指定多个标识符如多个盘符、多个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 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 String getPoliceValue() {
return policeValue;
}
public void setPoliceValue(String policeValue) {
this.policeValue = policeValue;
}
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;
}
}

View File

@@ -0,0 +1,86 @@
package com.nis.nmsclient.model;
public class CommandPO {
/**
* ID
*/
private Long execId;
/**
* 类型4 命令执行6 升级
*/
private long execType;
/**
* 升级文件存放目录
*/
private String srcPath;
/**
* 命令名称
*/
private String commandName;
/**
* 命令参数
*/
private String commandParam;
/**
* 执行状态4下发任务40 下发成功41下发失败5杀进程50成功51失败、6备份、7更新覆盖 、8启动进程
*/
private long execState;
/**
* 恢复版本
*/
private Long execVersion;
/**
* 是否周期任务
*/
private long isLoop;
public Long getExecId() {
return execId;
}
public void setExecId(Long execId) {
this.execId = execId;
}
public long getExecType() {
return execType;
}
public void setExecType(long execType) {
this.execType = execType;
}
public String getSrcPath() {
return srcPath;
}
public void setSrcPath(String srcPath) {
this.srcPath = srcPath;
}
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 getExecState() {
return execState;
}
public void setExecState(long execState) {
this.execState = execState;
}
public Long getExecVersion() {
return execVersion;
}
public void setExecVersion(Long execVersion) {
this.execVersion = execVersion;
}
public long getIsLoop() {
return isLoop;
}
public void setIsLoop(long isLoop) {
this.isLoop = isLoop;
}
}

View File

@@ -0,0 +1,52 @@
package com.nis.nmsclient.model;
/**
* 备份命令的参数实体类
* @date Mar 23, 2012
* @author zhenzhen
* @version
*/
public class ParamBackup{
/**
* 需要备份的文件或目录
*/
private String backup = null;
/**
* 备份文件到的目的路径
*/
private String backupTo = null;
/**
* 排除的文件或目录,即不需要备份的,如果是相对路径,则相对需要备份的文件或目录
*/
private String[] except = null;
/**
* 备份压缩包时Y 按绝对路径压缩N 按进入备份目录压缩这个属性只针对Linux有效Windows下只按进入备份目录压缩
*/
private String isAbs = null;
public String getBackup() {
return backup;
}
public void setBackup(String backup) {
this.backup = backup;
}
public String getBackupTo() {
return backupTo;
}
public void setBackupTo(String backupTo) {
this.backupTo = backupTo;
}
public String[] getExcept() {
return except;
}
public void setExcept(String[] except) {
this.except = except;
}
public String getIsAbs() {
return isAbs;
}
public void setIsAbs(String isAbs) {
this.isAbs = isAbs;
}
}

View File

@@ -0,0 +1,111 @@
package com.nis.nmsclient.model;
/**
* 命令执行(启动命令)的参数实体类
* @date Mar 23, 2012
* @author zhenzhen
* @version
*/
public class ParamCmdExec {
/**
* 执行文件或命令
*/
private String execCmd;
/**
* 执行文件或命令的参数序列
*/
private String[] execParams;
/**
* 该命令是否强制执行Y是N否
*/
private String forceExec;
/**
* 常驻内存标识: Y是N否
*/
private String residentFlag;
/**
* 存放执行结果标识的文件:1、常驻内存的写入PID2、非常驻的写执行结果结果标识0 成功 1 失败)|结果描述
*/
private String execResult;
/**
* 回传标识: Y是N否
*/
private String returnFlag;
/**
* 回传文件或目录路径
*/
private String returnPath;
/**
* 最终结果获取最大等待时间(单位:秒)
*/
private String maxWaitTime;
/**
* 执行文件或命令的用户名只针对Linux有效
*/
private String username;
/**
* 执行用户的密码只针对Linux有效
*/
private String param1;
public String getExecCmd() {
return execCmd;
}
public void setExecCmd(String execCmd) {
this.execCmd = execCmd;
}
public String getForceExec() {
return forceExec;
}
public void setForceExec(String forceExec) {
this.forceExec = forceExec;
}
public String getResidentFlag() {
return residentFlag;
}
public void setResidentFlag(String residentFlag) {
this.residentFlag = residentFlag;
}
public String getExecResult() {
return execResult;
}
public void setExecResult(String execResult) {
this.execResult = execResult;
}
public String getReturnFlag() {
return returnFlag;
}
public void setReturnFlag(String returnFlag) {
this.returnFlag = returnFlag;
}
public String getReturnPath() {
return returnPath;
}
public void setReturnPath(String returnPath) {
this.returnPath = returnPath;
}
public String getMaxWaitTime() {
return maxWaitTime;
}
public void setMaxWaitTime(String maxWaitTime) {
this.maxWaitTime = maxWaitTime;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String[] getExecParams() {
return execParams;
}
public void setExecParams(String[] execParams) {
this.execParams = execParams;
}
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
}

View File

@@ -0,0 +1,62 @@
package com.nis.nmsclient.model;
/**
* 覆盖更新的参数实体类
* @date Mar 23, 2012
* @author zhenzhen
* @version
*/
public class ParamCoverUpdate{
/**
* 指定更新需要的文件
*/
private String source = null;
/**
* 指定文件覆盖目录
*/
private String cover = null;
/**
* 是否创建覆盖目录
*/
private String isCreateCover = null;
/**
* 删除的文件或目录,如果是相对路径,则相对需要更新的目录
*/
private String[] delete = null;
/**
* 如果更新源文件是个压缩包Y 按绝对路径解压即在根目录下解压N 按进入备份目录解压这个属性只针对Linux有效Windows下只按进入备份目录解压
*/
private String isAbs = null;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getIsCreateCover() {
return isCreateCover;
}
public void setIsCreateCover(String isCreateCover) {
this.isCreateCover = isCreateCover;
}
public String[] getDelete() {
return delete;
}
public void setDelete(String[] delete) {
this.delete = delete;
}
public String getIsAbs() {
return isAbs;
}
public void setIsAbs(String isAbs) {
this.isAbs = isAbs;
}
}

View File

@@ -0,0 +1,81 @@
package com.nis.nmsclient.model;
/**
* 文件推送的参数实体类
* @date Mar 23, 2012
* @author zhenzhen
* @version
*/
public class ParamFilePush {
/**
* 推送文件名
*/
private String fileName;
/**
* 推送目的地
*/
private String destPath;
/**
* 是否覆盖Y是N否
*/
private String isCover;
/**
* 推送文件的属主只针对Linux有效
*/
private String username;
/**
* 属主密码只针对Linux有效
*/
private String param1;
/**
* 推送文件的属组只针对Linux有效
*/
private String groupName;
/**
* 推送文件拥有的权限只针对Linux有效
*/
private String permisson;
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 getIsCover() {
return isCover;
}
public void setIsCover(String isCover) {
this.isCover = isCover;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getPermisson() {
return permisson;
}
public void setPermisson(String permisson) {
this.permisson = permisson;
}
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
}

View File

@@ -0,0 +1,174 @@
package com.nis.nmsclient.model;
/**
* 升级命令的参数实体类
* @date Mar 23, 2012
* @author zhenzhen
* @version
*/
public class ParamUpgrade {
/**
* 指定更新需要的文件,如果是相对路径,则相对推送目的地
*/
private String fileName = null;
/**
* 指定文件覆盖目录
*/
private String cover = null;
/**
* 是否创建覆盖目录
*/
private String isCreateCover = null;
/**
* 如果更新源文件是个压缩包Y 按绝对路径解压即在根目录下解压N 按进入备份目录解压这个属性只针对Linux有效Windows下只按进入备份目录解压
*/
private String isAbs = null;
/**
* 需要删除的目录,如果是相对路径,则相对覆盖目录
*/
private String [] delete = null;
/**
* 该命令是否强制执行Y是N否
*/
private String forceExec;
/**
* 文件的属主或运行的用户只针对Linux有效
*/
private String username;
/**
* 用户密码只针对Linux有效
*/
private String param1;
/**
* 文件的属组只针对Linux有效
*/
private String groupName;
/**
* 权限设置
*/
private String permisson = null;
//以上是升级用到的参数属性,以下是升级和恢复都有的参数属性
/**
* 备份目录,如果是相对路径,则相对覆盖目录
*/
private ParamBackup [] backups = null;
/**
* 恢复目录,逆向任务时使用
*/
private ParamCoverUpdate[] recoverys;
/**
* 进程PID文件全路径
*/
private String pidFile = null;
/**
* 进程启动文件全路径
*/
private String startupFile = null;
/**
* 执行文件的参数序列
*/
private String[] execParams;
/**
* 最终结果获取最大等待时间(单位:秒)
*/
private String maxWaitTime;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public ParamBackup[] getBackups() {
return backups;
}
public void setBackups(ParamBackup[] backups) {
this.backups = backups;
}
public String getPidFile() {
return pidFile;
}
public void setPidFile(String pidFile) {
this.pidFile = pidFile;
}
public String getStartupFile() {
return startupFile;
}
public void setStartupFile(String startupFile) {
this.startupFile = startupFile;
}
public String[] getDelete() {
return delete;
}
public void setDelete(String[] delete) {
this.delete = delete;
}
public String getIsCreateCover() {
return isCreateCover;
}
public void setIsCreateCover(String isCreateCover) {
this.isCreateCover = isCreateCover;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPermisson() {
return permisson;
}
public void setPermisson(String permisson) {
this.permisson = permisson;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getIsAbs() {
return isAbs;
}
public void setIsAbs(String isAbs) {
this.isAbs = isAbs;
}
public String getForceExec() {
return forceExec;
}
public void setForceExec(String forceExec) {
this.forceExec = forceExec;
}
public String getMaxWaitTime() {
return maxWaitTime;
}
public void setMaxWaitTime(String maxWaitTime) {
this.maxWaitTime = maxWaitTime;
}
public void setExecParams(String[] execParams) {
this.execParams = execParams;
}
public String[] getExecParams() {
return execParams;
}
public ParamCoverUpdate[] getRecoverys() {
return recoverys;
}
public void setRecoverys(ParamCoverUpdate[] recoverys) {
this.recoverys = recoverys;
}
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
}

View File

@@ -0,0 +1,106 @@
package com.nis.nmsclient.model;
import java.util.Date;
public class ReturnFilePO {
/**
* ID
*/
private Long taskId;
/**
* 类型4 命令执行6 升级
*/
private long taskType;
/**
* 唯一标志一台物理机类似于IP
*/
private long uuid;
/**
* 回传路径集: key 文件实际路径 value 文件别名
*/
// private Map<String, String> 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;
}
}

View File

@@ -0,0 +1,272 @@
package com.nis.nmsclient.model;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import com.nis.nmsclient.common.Contants;
/**
* 客户端用到的监测设置信息实体
*
*/
public class SetInfo {
/**
* 监测设置信息ID
*/
private Long id;
/**
* 检测类型
*/
private String checkTypeName;//如CPU、DISK等
/**
* 检测类型的ID预留
*/
private Long checkTypeId;
/**
* 最大测试次数
*/
private Long checkMaxTimes;
/**
* 时间间隔(单位:分钟)
*/
private Long checkGap;
/**
* 超时时间(单位:秒)
*/
private Long checkOutTime;
/**
* 监测状态0无效1有效
*/
private String checkState;
/**
* 监测方式0主动(DC执行)1被动(NC执行)
*/
private String checkWay;
/**
* 设置标志
*/
private String processIden;
/**
* 进程PID存放文件
*/
private String processFile;
/**
* 进程执行文件
*/
private String processPath;
/**
* 是否系统启动NMSAgent启动/第三方自己启动默认0自启动1NMSAgent启动
*/
private String IsControlStart;
/**
* 控制启动时间
*/
private Long controlStartTime;
/**
*上传数据时间间隔单位分钟不能为空默认15分钟。监测数据上传到NMSServer周期。
*/
private Long uploadGap;
/**
* 计划检测时间:针对当前配置信息首次执行时间
*/
private Long planCheckTime;
/**
* 是否预置监测,0是1否
*/
private String isSchedule;
/**
* 进程搜索关键字
*/
private String processSearchKeyCode;
// ====== 以下信息仅供DC端使用这里添加是为了不出现WARN信息
private String nodeGroupsId;
private String nodeIpsId;
private Long viewLevel;
private String nodeIp2;
private Long isSNMP;
// ======= 保存最后合并的临时结果文件的检测时间仅用于NC
private Long lastMergeDetecTime;
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;
}
public String[] getValArr(Long uuid, String dataFileDir) {
List<String> list = new ArrayList<String>();
list.add("pubInfo=" + uuid + "," + id + "," + checkTypeName + "," + processIden);
list.add("checkState=" + checkState);
//list.add("checkWay=" + checkWay);
//list.add("isControlStart=" + IsControlStart);
//list.add("controlStartTime=" + controlStartTime);
list.add("checkGap=" + checkGap);
list.add("checkOutTime=" + checkOutTime);
list.add("checkMaxTimes=" + checkMaxTimes);
//list.add("processFile=" + processFile);
//list.add("processPath=" + processPath);
list.add("planCheckTime=" + planCheckTime);
list.add("uploadGap=" + uploadGap);
list.add("dataFileDir=" + dataFileDir);
try {
if("1".equals(isSchedule)) { // 第三方监测
File dataDir = new File(dataFileDir);
File tempDataDir = new File(Contants.localTempDataIncomingPath, dataDir.getName());
list.add("tempDataFileDir=" + tempDataDir.getCanonicalPath());
}
} catch (IOException e) {
e.printStackTrace();
}
String[] val = new String[list.size()];
return list.toArray(val);
}
public String getIsControlStart() {
return IsControlStart;
}
public void setIsControlStart(String isControlStart) {
IsControlStart = isControlStart;
}
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 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 String getProcessSearchKeyCode() {
return processSearchKeyCode;
}
public void setProcessSearchKeyCode(String processSearchKeyCode) {
this.processSearchKeyCode = processSearchKeyCode;
}
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 Long getViewLevel() {
return viewLevel;
}
public void setViewLevel(Long viewLevel) {
this.viewLevel = viewLevel;
}
public String getNodeIp2() {
return nodeIp2;
}
public void setNodeIp2(String nodeIp2) {
this.nodeIp2 = nodeIp2;
}
public Long getIsSNMP() {
return isSNMP;
}
public void setIsSNMP(Long isSNMP) {
this.isSNMP = isSNMP;
}
public void setLastMergeDetecTime(Long lastMergeDetecTime) {
this.lastMergeDetecTime = lastMergeDetecTime;
}
public Long getLastMergeDetecTime() {
return lastMergeDetecTime;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}

View File

@@ -0,0 +1,49 @@
package com.nis.nmsclient.model;
/**
* 任务实体类1 文件推送
*
*/
public class Task1 {
/**
* 任务ID
*/
private Long taskId;
/**
* 任务类型1 文件推送2 非流文本数据获取3 流文本数据获取4 命令执行5 shell注册
*/
private long taskType;
/**
* 推送文件参数设置
*/
private String taskParam;
/**
* 重新执行原任务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 String getTaskParam() {
return taskParam;
}
public void setTaskParam(String taskParam) {
this.taskParam = taskParam;
}
public Long getOldTaskId() {
return oldTaskId;
}
public void setOldTaskId(Long oldTaskId) {
this.oldTaskId = oldTaskId;
}
}

View File

@@ -0,0 +1,166 @@
package com.nis.nmsclient.model;
/**
* 任务实体类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;
/**
* 执行时间
*/
private Long startTime;
/**
* 结束时间
*/
private Long endTime;
/**
* 是否循环任务: 0 非周期, 1 周期默认是0
*/
private long isLoop;
/**
* 如果是循环任务,循环周期
*/
private long loopDelay;
/**
* 任务状态6撤销执行
*/
private Long missionState;
/**
* 重新执行原任务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 long getState() {
return state;
}
public void setState(long state) {
this.state = state;
}
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 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;
}
public Long getMissionState() {
return missionState;
}
public void setMissionState(Long missionState) {
this.missionState = missionState;
}
public Long getOldTaskId() {
return oldTaskId;
}
public void setOldTaskId(Long oldTaskId) {
this.oldTaskId = oldTaskId;
}
}

View File

@@ -0,0 +1,121 @@
package com.nis.nmsclient.model;
/**
* 任务实体类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;
/**
* 如果是恢复(逆向任务),则为恢复到的版本恢复到的任务ID。判断它是否为空来确定是否逆向任务
*/
private Long version;
/**
* 升级时间
*/
private Long upgradeTime;
/**
* 全局权限用户名(未来)
*/
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 getUpgradeTime() {
return upgradeTime;
}
public void setUpgradeTime(Long upgradeTime) {
this.upgradeTime = upgradeTime;
}
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;
}
}

View File

@@ -0,0 +1,66 @@
package com.nis.nmsclient.thread;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
public class WritePidThread implements Runnable{
@Override
public void run() {
String SYSTEM_PATH = System.getProperty("user.dir");
// Thread.currentThread().setName("写PID线程");
Thread.currentThread().setName("Write The PID Thread");
Logger logger = Logger.getLogger(WritePidThread.class);
/* 获取程序运行PID */
String path = Contants.localAgentPidFile;//2015-11-25:之前一直写的是"NMSClientPid.temp"不对配置的是agentPid.temp
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写入文件 */
FileWriter writer = null;
try {
writer = new FileWriter(file);
writer.write(name.split("@")[0]);
writer.flush();
logger.info("写PID完成");
} catch (IOException e) {
logger.error("Write PID failure", e);
}finally{
try {
if(writer!=null)
writer.close();
writer = 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());
}
}

View File

@@ -0,0 +1,146 @@
package com.nis.nmsclient.thread.alarm;
public class AlarmPO {
/**
* 监测ID
*/
private long id;
/**
* 监测类型
*/
private String type;
/**
* 监测设置名称
*/
private String procIden;
/**
* 时间间隔(单位:分钟)
*/
private long checkGap;
/**
* 是否是进程
*/
private boolean isproc;
/**
* 进程路径与名称
*/
private String proc;
/**
* PID文件
*/
private String pidFileStr;
/**
* 进程搜索关键字
*/
private String processSearchKeyCode;
/**
* 控制启动时间
*/
private Long controlStartTime;
/**
* 是否系统启动NMSAgent启动/第三方自己启动默认0自启动1NMSAgent启动
*/
private String IsControlStart;
public AlarmPO() {
super();
}
public AlarmPO(long id, String type, String procIden, long checkGap,
boolean isproc, String proc, String pidFileStr,
Long controlStartTime, String processSearchKeyCode, String IsControlStart) {
super();
this.id = id;
this.type = type;
this.procIden = procIden;
this.checkGap = checkGap;
this.isproc = isproc;
this.proc = proc;
this.pidFileStr = pidFileStr;
this.controlStartTime = controlStartTime;
this.processSearchKeyCode = processSearchKeyCode;
this.IsControlStart = IsControlStart;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getProcIden() {
return procIden;
}
public void setProcIden(String procIden) {
this.procIden = procIden;
}
public long getCheckGap() {
return checkGap;
}
public void setCheckGap(long checkGap) {
this.checkGap = checkGap;
}
public boolean isIsproc() {
return isproc;
}
public void setIsproc(boolean isproc) {
this.isproc = isproc;
}
public String getProc() {
return proc;
}
public void setProc(String proc) {
this.proc = proc;
}
public String getPidFileStr() {
return pidFileStr;
}
public void setPidFileStr(String pidFileStr) {
this.pidFileStr = pidFileStr;
}
public Long getControlStartTime() {
return controlStartTime;
}
public void setControlStartTime(Long controlStartTime) {
this.controlStartTime = controlStartTime;
}
public String getProcessSearchKeyCode() {
return processSearchKeyCode;
}
public void setProcessSearchKeyCode(String processSearchKeyCode) {
this.processSearchKeyCode = processSearchKeyCode;
}
public String getIsControlStart() {
return IsControlStart;
}
public void setIsControlStart(String isControlStart) {
IsControlStart = isControlStart;
}
}

View File

@@ -0,0 +1,315 @@
package com.nis.nmsclient.thread.alarm;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.config.DetecConfOper;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.ProcessUtil;
import com.nis.nmsclient.util.Utils;
/**
* 主动报警线程:针对当前所有有效监测设置进行主动报警
* 定时监测某设置是否正常生成数据N次未取到数据后向Server报警恢复正常后向Server发送提示信息
* @date Mar 22, 2012
* @author zhenzhen
* @version
*/
public class AlarmThread implements Runnable{
Logger logger = Logger.getLogger(AlarmThread.class);
private String name;
private Map<Long, AlarmStateMsg> alarmSmMap = new HashMap<Long, AlarmStateMsg>();
public AlarmThread(String name) {
super();
this.name = name;
}
public void run() {
Thread.currentThread().setName(name);
logger.debug("主动预警检查开始 ~~~~~~~");
try{
Map<Long, AlarmPO> alarmPOs = new HashMap<Long, AlarmPO>(Common
.getAlarmPOs());
// 清除垃圾数据
if (alarmSmMap.size() > alarmPOs.size()) {
Iterator<Long> it = alarmSmMap.keySet().iterator();
while (it.hasNext()) {
Long key = it.next();
if(!alarmPOs.containsKey(key)){
//alarmSmMap.remove(key);
it.remove();//update by jinsj for 2013-07-08 java.util.ConcurrentModificationException
}
}
}
Set<Map.Entry<Long, AlarmPO>> entrys = alarmPOs.entrySet();
int alarmCnt = 0;
StringBuffer alarmDescInfo = new StringBuffer();
for (Map.Entry<Long, AlarmPO> entry : entrys) {
// 如果监测进程未启动,则不进行告警检查
if (entry.getValue().getControlStartTime() != null
&& entry.getValue().getControlStartTime() > System
.currentTimeMillis()) {
continue;
}
// 对各监测设置告警状态信息进行赋初值
Long key = entry.getValue().getId();
if (alarmSmMap.get(key) == null) {
alarmSmMap.put(key, new AlarmStateMsg(new Date(), 0,
new StringBuffer(), false));
}else if(alarmSmMap.get(key).getAlarmMsg().length() == 0){
alarmSmMap.get(key).setStart(new Date());
}
boolean flag = alarm(entry.getValue());
if(flag){
alarmCnt += 1;
alarmDescInfo.append("\tsetId: " + key + " >> " + entry.getValue().getType() + "_" + entry.getValue().getProcIden());
alarmDescInfo.append("\n");
}
}
logger.info("本次预警轮循检查监测设置总数:" + alarmPOs.size() + ",正常:"
+ (alarmPOs.size() - alarmCnt) + ",不正常:" + alarmCnt);
if(alarmCnt>0){
logger.info("本次预警检查不正常的监测如下:\n" + alarmDescInfo.toString());
}
}catch (Exception e) {
logger.error("Active early warning thread exception" + Utils.printExceptionStack(e));
}
logger.debug("主动预警检查结束 ~~~~~~~");
}
/**
* 针对一个监测设置的主动预警具体逻辑实现
* @param alarm
* @return true 告警监测不正常 false 监测正常
* @throws Exception
*/
public boolean alarm(AlarmPO alarm) throws Exception{
Long key = alarm.getId();
String sonDirName = DetecConfOper.getFileName(alarm.getType(), alarm.getProcIden(), null);
StringBuffer sbMsg = new StringBuffer();
/**
* 1、检查是否存在数据: alarmLevel 0 -- 数据存在非0 -- 数据不存在
*/
int alarmLevel = checkDataIsExist(sonDirName, alarm.getCheckGap(), sbMsg);
/**
* 2、判断进程是否存在是进程且数据不存在才判断不是进程如系统类型CPU则不进行这些操作
*/
if(alarmLevel!=0 && alarm.isIsproc()){
if("2".equals(alarm.getIsControlStart())) { // NC周期启动(脚本间断性执行且执行时间较短通过判断Java定时任务确认进程)
if(Common.containPluginDetecFuture(key)){ // 进程存在
String temp = sbMsg.toString();
sbMsg.delete(0, sbMsg.length());
// sbMsg.append("进程存在,但" + temp);
sbMsg.append("i18n_client.AlarmThread.processExists_n81i" + temp);
alarmLevel = 2;
} else { // 进程不存在
sbMsg.delete(0, sbMsg.length());
// sbMsg.append("定时任务“" + alarm.getType() + "”不存在");
sbMsg.append("i18n_client.AlarmThread.loopMission1_n81i“" + alarm.getType() + "”i18n_client.AlarmThread.loopMission2_n81i");
alarmLevel = 1;
}
} else {
Object[] objArr = ProcessUtil.checkPidAndGetPid(alarm.getPidFileStr(), alarm.getProcessSearchKeyCode());
int isExistFlag = Integer.parseInt(objArr[0].toString());
String pidInfo = objArr[1].toString();
if(isExistFlag==0){// 进程不存在(包括PID文件不存在PID值不存在搜索关键字也找不到进程)
sbMsg.delete(0, sbMsg.length());
sbMsg.append(pidInfo);
alarmLevel = 1;
} else {// 进程存在(一个进程或多个进程)
String temp = sbMsg.toString();
sbMsg.delete(0, sbMsg.length());
// sbMsg.append("进程存在,但" + temp);
sbMsg.append("i18n_client.AlarmThread.processExists_n81i" + temp);
alarmLevel = 2;
}
}
}
/**
* 3、统计并发送报警信息
*/
AlarmStateMsg asMsg= alarmSmMap.get(key);
int times = asMsg.getTimes();
boolean flag = true;
if (sbMsg.length()>0 && sbMsg.toString().equals(asMsg.getAlarmMsg().toString())) {
times ++ ;
if (times == Contants.noDataTimes) {// Contants.noDataTime次未取到数据报警
AlarmUtil.sendAlarmMsg(alarm, asMsg.getStart(), new Date(),
alarmLevel, Contants.DETECTION_STATUS_ABNORMAL, asMsg
.getAlarmMsg().toString()
// + ",此状态持续了" + times + "次");
+ "i18n_client.AlarmThread.state_n81i" + times + "i18n_client.AlarmThread.times_n81i");
asMsg.setAlarmState(true);
}
} else if (sbMsg.length() > 0
&& !sbMsg.toString().equals(asMsg.getAlarmMsg().toString())) {
asMsg.getAlarmMsg().delete(0, asMsg.getAlarmMsg().length());
asMsg.getAlarmMsg().append(sbMsg.toString());
times = 1;
} else {
asMsg.getAlarmMsg().delete(0, asMsg.getAlarmMsg().length());
times = 0;
if(asMsg.isAlarmState()){
/*AlarmUtil.sendAlarmMsg(alarm, asMsg.getStart(), new Date(),
99, Contants.DETECTION_STATUS_NORMAL, "恢复正常");*/
asMsg.setAlarmState(false);
}
flag = false;
}
asMsg.setTimes(times);
alarmSmMap.put(key, asMsg);
return flag;
}
/**
* 检查是否存在数据
* @param sonDirName
* @param checkGap
* @return
*/
private int checkDataIsExist(String sonDirName, long checkGap, StringBuffer sbMsg){
int alarmLevel = 0;
// ------- 1、先查看当前正在写入的数据文件夹
File curDir = new File(Contants.localDataFilePath + File.separator + sonDirName);
if(!curDir.exists()){
// sbMsg.append("数据文件目录“" + curDir.getAbsolutePath() + "”不存在");
sbMsg.append("i18n_client.AlarmThread.dataFileNotExists1_n81i“" + curDir.getAbsolutePath() + "”i18n_client.AlarmThread.dataFileNotExists2_n81i");
alarmLevel = 1;
return alarmLevel;
}
long timeMillis = System.currentTimeMillis() - Contants.noDataTimes * checkGap * 60 * 1000;
File[] rightFiles = FileUtil.getFilesAfterMillis(curDir, timeMillis);
if(rightFiles!=null && rightFiles.length>0){// 找到数据,正常
alarmLevel = 0;
return alarmLevel;
}
// -------- 2、如果当前正在写入的数据文件夹没有找到符合条件的数据则查找上传成功后备份到的相应目录
// 2013-3-28 修改内容:原本只检查当天日期目录是否存在数据,针对监测时间间隔超过一天的存在问题,所以当间隔时间大于等于一天时,改为检查当天日期之前的目录
String dateDirName = DateUtil.getCurrentDate(DateUtil.YYYYMMDD);
File doneDir = new File(Contants.localDataDonePath + File.separator + sonDirName);
if(doneDir.exists()){
if(checkGap >= 1440){// --- 检查当天日期及之前的目录
// 找到指定的日期目录及之前的日期目录,将其降序排列,是为了最先查找当天日期
File[] dateDirs = FileUtil.sortDescByFileName(FileUtil.getDirsBeforeDateName(doneDir, dateDirName));
// 在找到的日期目录下检查文件是否存在
for(File dateDir : dateDirs){
rightFiles = FileUtil.getFilesAfterMillis(dateDir, timeMillis);
if(rightFiles!=null && rightFiles.length>0){// 在任一目录找到则不再继续查找其他目录
break;
}
}
}else {// --- 只检查当天日期
File dateDir = new File(doneDir.getAbsolutePath() + File.separator + dateDirName);
if(dateDir.exists()){
rightFiles = FileUtil.getFilesAfterMillis(dateDir, timeMillis);
}
}
}
if(rightFiles!=null && rightFiles.length>0){// 找到数据,正常
alarmLevel = 0;
return alarmLevel;
}
// -------- 3、查找不完整数据即0大小文件备份到的相应目录
File[] errorFiles = null;
File errorDir = new File(Contants.localDataErrorPath + File.separator + sonDirName);
if(errorDir.exists()){
if(checkGap >= 1440){// --- 检查当天日期及之前的目录
// 找到指定的日期目录及之前的日期目录,将其降序排列,是为了最先查找当天日期
File[] dateDirs = FileUtil.sortDescByFileName(FileUtil.getDirsBeforeDateName(errorDir, dateDirName));
// 在找到的日期目录下检查文件是否存在
for(File dateDir : dateDirs){
errorFiles = FileUtil.getFilesAfterMillis(dateDir, timeMillis);
if(errorFiles!=null && errorFiles.length>0){// 在任一目录找到则不再继续查找其他目录
break;
}
}
}else {// --- 只检查当天日期
File dateDir = new File(doneDir.getAbsolutePath() + File.separator + dateDirName);
if(dateDir.exists()){
errorFiles = FileUtil.getFilesAfterMillis(errorDir, timeMillis);
}
}
}
if(errorFiles!=null && errorFiles.length>0){
// sbMsg.append("监测数据文件大小为0");
sbMsg.append("i18n_client.AlarmThread.dataSize_n81i");
alarmLevel = 1;
}else{
// sbMsg.append("未取到监测数据");
sbMsg.append("i18n_client.AlarmThread.noDetecateData_n81i");
alarmLevel = 1;
}
return alarmLevel;
}
/**
* 告警检查过程中针对每个监测设置的过程变量状态的封装类
* @date Mar 22, 2012
* @author zhenzhen
* @version
*/
class AlarmStateMsg {
private Date start;//告警检查不正常情况的开始时间
private Integer times;//连续告警次数
private StringBuffer alarmMsg;//告警信息
private boolean alarmState;//是否向Server发送了主动告警请求
public AlarmStateMsg(Date start, Integer times, StringBuffer alarmMsg,
boolean alarmState) {
super();
this.start = start;
this.times = times;
this.alarmMsg = alarmMsg;
this.alarmState = alarmState;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
public Integer getTimes() {
return times;
}
public void setTimes(Integer times) {
this.times = times;
}
public StringBuffer getAlarmMsg() {
return alarmMsg;
}
public void setAlarmMsg(StringBuffer alarmMsg) {
this.alarmMsg = alarmMsg;
}
public boolean isAlarmState() {
return alarmState;
}
public void setAlarmState(boolean alarmState) {
this.alarmState = alarmState;
}
}
}

View File

@@ -0,0 +1,79 @@
package com.nis.nmsclient.thread.alarm;
import java.util.Date;
import java.util.concurrent.Future;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.config.DetecConfOper;
import com.nis.nmsclient.model.SetInfo;
import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.thread.socket.SSLClient;
import com.nis.nmsclient.util.Utils;
public class AlarmUtil {
static Logger logger = Logger.getLogger(AlarmUtil.class);
public static void sendAlarmMsg(AlarmPO alarm, Date start,
Date end, int alarmLevel, int state, String alarmMsg,String... showNum) {
sendAlarmMsg(alarm.getId(), alarm.getType(), alarm
.getProcIden(), start, end, alarmLevel, state, alarmMsg,showNum);
}
public static void sendAlarmMsg(Long alarmId, String checkType,
String procIden, Date start, Date end, int alarmLevel, int state,
String alarmMsg,String... showNum) {
String seprator = Contants.COMMON_MSG_SEPRATOR;
String showAlarmNum="";
if (showNum!=null && showNum.length>0)
{
showAlarmNum = showNum[0];
}
String msg = alarmId + seprator + Contants.AGENT_HOST_UUID + seprator
+ checkType + seprator + procIden + seprator + start.getTime()
+ seprator + end.getTime() + seprator + alarmLevel + seprator
+ state + seprator + alarmMsg+ seprator + showAlarmNum;
Common.service.submit(new SSLClient(Thread.currentThread()
.getName(), CommonSocket.REQ_ALARM, msg));
logger.warn("Active alarm level" + alarmLevel + " >> setId: " + alarmId
+ "" + checkType + "_" + procIden + "state: " + state + " > "
+ alarmMsg+ "showNum: " +showAlarmNum);
}
public static AlarmPO getAlarmPO(SetInfo setInfo){
AlarmPO alarmPO = new AlarmPO(setInfo.getId(), setInfo
.getCheckTypeName(), setInfo.getProcessIden(), setInfo
.getCheckGap(), DetecConfOper.isProcess(setInfo), DetecConfOper
.getProcess(setInfo), setInfo.getProcessFile(), setInfo
.getControlStartTime(), setInfo.getProcessSearchKeyCode(),
setInfo.getIsControlStart());
return alarmPO;
}
// 向DC发送NC端的异常信息
public static void sendNMSErrorMsg(ErrorCode errorCode, String errorIp, String errorDesc) {
String seprator = Contants.COMMON_MSG_SEPRATOR;
try {
//异常信息顺序CODE、发送IP、发生错误IP、发生错误时间、处理状态(1)、错误描述
String msg = errorCode.toString() + seprator + Utils.getLocalIp() + seprator + errorIp
+ seprator + System.currentTimeMillis() + seprator + "1"
+ seprator + errorDesc;
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
.getName(), CommonSocket.REQ_ERROR_INFO, msg));
if(errorCode.equals(ErrorCode.ProtListenerError)){
future.get();
}
logger.error("Abnormal information " + errorCode + " >> errorIp: " + errorIp + " > "
+ errorDesc);
} catch (Exception e) {
logger.error(e);
}
}
}

View File

@@ -0,0 +1,11 @@
package com.nis.nmsclient.thread.alarm;
public enum ErrorCode {
ProcessNotExist, //进程不存在
MultipleProcessExist,//多个进程存在
ThreadRuntimeError, //执行异常
HandShakeError, //通讯握手失败
ProtListenerError, //端口监听失败
DeamonNotExist, //守护进程不存在
SocketError//通讯失败
}

View File

@@ -0,0 +1,191 @@
package com.nis.nmsclient.thread.plugin;
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.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.config.DetecConfOper;
import com.nis.nmsclient.model.SetInfo;
import com.nis.nmsclient.thread.alarm.AlarmUtil;
import com.nis.nmsclient.util.ProcessUtil;
import com.nis.systeminfo.thread.NewPluginResultMerge;
public class StartPluginRun implements Runnable {
static Logger logger = Logger.getLogger(StartPluginRun.class);
private String name;
private SetInfo setInfo;
public StartPluginRun(SetInfo setInfo, String name) {
this.name = name;
this.setInfo = setInfo;
}
public void run() {
Thread.currentThread().setName(name);
logger.info("启动第三方插件程序开始!");
if ("1".equals(setInfo.getCheckState())
&& "1".equals(setInfo.getCheckWay())
&& "1,2".contains(setInfo.getIsControlStart())) {// 有效,被动方式获取,Agent端启动
Date startTime = null;
if (setInfo.getControlStartTime() != null && setInfo.getControlStartTime().longValue() != 0) {
startTime = new Date(setInfo.getControlStartTime());
} else {
startTime = new Date();
}
/*
* 停止未启动的定时任务/周期任务
*/
Common.stopPluginDetecFuture(setInfo.getId(), Thread.currentThread().getName());
/*
* 首先看进程设置的启动时间是否过时:如果过时,则查看进程是否存在,不存在启动;如果未过时,则定时启动
*/
String cmd = DetecConfOper.getProcess(setInfo);
try {
/**
* 进程存在与否的判断标准:
* 1、先检查PID文件取出PID验证PID指定进程是否存在
* PID文件不存在或者PID文件中内容为空或者取出的PID进程不存在都认为不存在进行下一步检查
* 2、不存在再使用搜索关键字查找进程
* 若未找出进程,不存在,若找出一个进程,正常存在,若找出多个进程,报警
*/
Object[] objArr = ProcessUtil.checkPidAndGetPid(setInfo.getProcessFile(), setInfo.getProcessSearchKeyCode());
int isExistFlag = Integer.parseInt(objArr[0].toString());
String pidInfo = objArr[1].toString();
if (isExistFlag == 0 || isExistFlag == 1) { // 进程不存在/存在一个进程
if (isExistFlag == 1) {// 存在一个进程
logger.info("三方监测进程”" + cmd + "“已存在,重启启动");
ProcessUtil.killProcess(pidInfo);
}
long delay = startTime.getTime() - System.currentTimeMillis();
delay = Math.max(delay, 0);
ScheduledFuture<?> future = null;
if ("1".equals(setInfo.getIsControlStart())) {
future = Common.scheduled.schedule(new ExecProcess(), delay, TimeUnit.MILLISECONDS);
} else if ("2".equals(setInfo.getIsControlStart())) {
long period = setInfo.getCheckGap() * 60 * 1000;
future = Common.scheduled.scheduleAtFixedRate(new ExecProcess2(), delay, period, TimeUnit.MILLISECONDS);
}
Common.putPluginDetecFuture(setInfo.getId(), future);
logger.info("三方监测程序已加入定时器定时启动");
} else { // 找到多个进程,告警
// String alarmMsg = "启动三方监测:" + pidInfo;
String alarmMsg = "i18n_client.StartPluginRun.startupDetecate_n81i" + pidInfo;
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo
.getCheckTypeName(), setInfo.getProcessIden(),
new Date(), new Date(), 1,
Contants.DETECTION_STATUS_FAILURE, alarmMsg);
}
} catch (Exception e) {
logger.error("Start the three party monitoring exception. Please check whether the process execution file and the PID file are set correctly" + cmd, e);
}
}
logger.info("启动第三方插件程序结束!");
}// run end
class ExecProcess implements Runnable {
public void run() {
Thread.currentThread().setName(name);
String cmd = DetecConfOper.getProcess(setInfo);
try {
// 启动
ProcessUtil.runExec(cmd, null, null, null);
// 检查PID
Object[] objArr = ProcessUtil.checkPidAndGetPid(setInfo.getProcessFile(), setInfo.getProcessSearchKeyCode());
int isExistFlag = Integer.parseInt(objArr[0].toString());
String pidInfo = objArr[1].toString();
if (isExistFlag == 1) {// 存在, 一个进程
logger.info("三方监测程序“" + cmd + "“启动成功");
} else { // 进程不存在 或 找到多个进程,告警
String alarmMsg = null;
if (isExistFlag == 0) {// 进程不存在
// alarmMsg = "三方监测程序启动失败,请检查进程启动文件“" + cmd + "”是否设置正确";
alarmMsg = "i18n_client.StartPluginRun.startupDetecateErr1_n81i “" + cmd + "” i18n_client.StartPluginRun.startupDetecateErr1.isTrue_n81i";
} else {// 找到多个进程
// alarmMsg = "启动三方监测:" + pidInfo;
alarmMsg = "i18n_client.StartPluginRun.startupDetecate_n81i" + pidInfo;
}
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo
.getCheckTypeName(), setInfo.getProcessIden(),
new Date(), new Date(), 1,
Contants.DETECTION_STATUS_FAILURE, alarmMsg);
}
} catch (Exception e) {
logger.error("Start the three party monitoring exception. Please check whether the process execution file and the PID file are set correctly" + cmd, e);
}
}
}
class ExecProcess2 implements Runnable {
public void run() {
Thread.currentThread().setName(name);
// 三方监测脚本执行命令不存在,获取脚本命令及关键词
// 因脚本下发与配置下发不同步,需要在执行任务中多次获取执行命令
String cmd = Common.generateCommandAndKeyword(setInfo);
try {
boolean executeStatus = true;
// 脚本执行前,清理临时数据目录
new NewPluginResultMerge().clearTmpFile(setInfo);
if (StringUtils.isBlank(cmd)) {
// 三方监测脚本执行命令不存在当前时间NC上无监测脚本
logger.info("三方监测程序启动失败,监测脚本下发未完成!");
// String alarmMsg = "启动三方监测异常,监测脚本下发未完成!";
String alarmMsg = "i18n_client.StartPluginRun.startupDetecateErr2_n81i";
AlarmUtil.sendAlarmMsg(setInfo.getId(),
setInfo.getCheckTypeName(),
setInfo.getProcessIden(), new Date(), new Date(),
1, Contants.DETECTION_STATUS_FAILURE, alarmMsg);
executeStatus = false;
} else {
// 启动脚本
String msg = ProcessUtil.runExec(cmd, null, null, null);
if (StringUtils.isBlank(msg)) { // 执行成功
logger.info("三方监测程序“" + cmd + "”执行成功");
} else { // 执行失败,返回错误信息
logger.error("Three party monitoring procedure“" + cmd + "”erroneous execution" + msg);
// String alarmMsg = "启动三方监测异常,监测脚本执行错误!";
String alarmMsg = "i18n_client.StartPluginRun.startupDetecateErr3_n81i";
AlarmUtil.sendAlarmMsg(setInfo.getId(),
setInfo.getCheckTypeName(),
setInfo.getProcessIden(), new Date(),
new Date(), 1,
Contants.DETECTION_STATUS_FAILURE, alarmMsg);
executeStatus = false;
}
}
// 针对执行合并临时文件ProcessUtil.runExec为同步执行合并临时文件前已完成脚本执行过程
if(executeStatus) {
new NewPluginResultMerge().merge(setInfo);
}
} catch (Exception e) {
logger.error("Start the three party monitoring exception. Please check whether the process execution file and the PID file are set correctly" + cmd, e);
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,238 @@
package com.nis.nmsclient.thread.socket;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.KeyStore;
import java.security.SecureRandom;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.Utils;
public class SSLCertOper {
static Logger logger = Logger.getLogger(SSLCertOper.class);
public static SSLContext getSSLContext() throws Exception {
// 初始化上下文
SSLContext ctx = SSLContext.getInstance(Contants.SSL_JSSE_TYPE);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance(Contants.SSL_KEYSTORE_TYPE);
ks.load(new FileInputStream(Contants.SSL_KEY_STORE),
Contants.SSL_KEY_STORE_PASS.toCharArray());// 载入keystore
kmf.init(ks, Contants.SSL_KEY_PRIVATE_PASS.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
KeyStore tks = KeyStore.getInstance(Contants.SSL_KEYSTORE_TYPE);
tks.load(new FileInputStream(Contants.SSL_TRUST_KEY_STORE),
Contants.SSL_KEY_STORE_PASS.toCharArray());// 载入keystore
tmf.init(tks);
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(),
new SecureRandom());
logger.debug("load keystore success.");
return ctx;
}
/**
* 创建 密匙对(私钥和公钥)
*
*/
public static String createKeyAndCert(String aliasName, String storePath,
String localIp, String keyPass, String storePass, String certName) {
BufferedReader bReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec(
"keytool -genkey -v -alias " + aliasName
+ " -keyalg RSA -storetype "
+ Contants.SSL_KEYSTORE_TYPE + " -keystore "
+ storePath + " -validity 90 -dname \"CN="
+ localIp
+ ",OU=cn,O=cn,L=cn,ST=cn,C=cn\" -storepass "
+ storePass + " -keypass " + keyPass);
process.getOutputStream().close();
bReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
process.getErrorStream().close();
String line = null;
while ((line = bReader.readLine()) != null) {
System.out.println(line);
}
process = Runtime.getRuntime().exec(
"keytool -export -alias " + aliasName + " -storetype "
+ Contants.SSL_KEYSTORE_TYPE + " -keystore "
+ storePath + " -file " + certName + " -storepass "
+ storePass + "");
bReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
while ((line = bReader.readLine()) != null) {
System.out.println(line);
}
return certName;
} catch (IOException e) {
logger.error("Create a key pair error!");
logger.error(Utils.printExceptionStack(e));
return null;
} finally {
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
logger.error(Utils.printExceptionStack(e));
}
}
}
}
/**
* 将公钥引入KeyStore
*
*/
public static boolean importCertToStore(String aliasName, String storePath,
String certName, String storePass) {
BufferedReader bReader = null;
PrintWriter pw = null;
try {
Process process = Runtime.getRuntime().exec(
"keytool -import -v -trustcacerts -alias " + aliasName
+ " -keystore " + storePath + " -file " + certName
+ " -storetype " + Contants.SSL_KEYSTORE_TYPE
+ " -storepass " + storePass + "");
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("Error of importing authentication certificate!");
logger.error(Utils.printExceptionStack(e));
return false;
} finally {
if (pw != null) {
pw.close();
}
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
logger.error(Utils.printExceptionStack(e));
}
}
}
}
/**
* 删除KeyStore库中的密钥
*
*/
public static boolean deleteKeyOrCertFromStore(String aliasName,
String storePath, String storePass) {
BufferedReader bReader = null;
PrintWriter pw = null;
try {
Process process = Runtime.getRuntime().exec(
"keytool -delete -v -alias " + aliasName + " -keystore "
+ storePath + " -storetype "
+ Contants.SSL_KEYSTORE_TYPE + " -storepass "
+ storePass + "");
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("Delete" + storePath+ "library Key" + aliasName + "make a mistake");
logger.error(Utils.printExceptionStack(e));
return false;
} finally {
if (pw != null) {
pw.close();
}
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
logger.error(Utils.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_client\\src\\key\\server_ks", "10.0.6.120",
newServerKeyPsw, "server",
"D:\\workspace\\nms_client\\src\\key\\server.cer");
SSLCertOper.importCertToStore(newServerKeyName,
"D:\\workspace\\nms_client\\src\\key\\client_ts",
"D:\\workspace\\nms_client\\src\\key\\server.cer", "client");
String filepath1 = SSLCertOper.createKeyAndCert(newClientkeyName,
"D:\\workspace\\nms_client\\src\\key\\client_ks", "localhost",
newClientkeyPsw, "client",
"D:\\workspace\\nms_client\\src\\key\\client.cer");
SSLCertOper.importCertToStore(newClientkeyName,
"D:\\workspace\\nms_client\\src\\key\\server_ts",
"D:\\workspace\\nms_client\\src\\key\\client.cer", "server");
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);
}
}

View File

@@ -0,0 +1,288 @@
package com.nis.nmsclient.thread.socket;
import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.model.ReturnFilePO;
import com.nis.nmsclient.thread.task.TaskReqHandle;
import com.nis.nmsclient.thread.task.TaskResultOper;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.Utils;
/**
* 安全通讯的客户端
**/
public class SSLClient extends CommonSocket implements Callable<Object>{
Logger logger = Logger.getLogger(SSLClient.class);
private String name;
private String reqCmd;
private Object obj;
private String serverHost;
public SSLClient(String name, String reqCmd, Object obj) {
this.name = name;
this.reqCmd = reqCmd;
this.obj = obj;
this.serverHost = Contants.SOCKET_SERVER_HOST;
}
public SSLClient(String name, String reqCmd, Object obj, String serverHost) {
this.name = name;
this.reqCmd = reqCmd;
this.obj = obj;
this.serverHost = serverHost;
}
/**
* 初始化客户端Socket
**/
public void init() throws Exception {
SSLContext ctx = SSLCertOper.getSSLContext();
SSLSocketFactory ssf = ctx.getSocketFactory();
socket = (SSLSocket) ssf.createSocket(serverHost,
Contants.SOCKET_SERVER_PORT);
logger.debug("create socket success.");
//2014-1-23 hyx 如果建立socket成功但是startHandshake握手失败且未设置超时时间时则会一直阻塞
socket.setSoTimeout(1000 * 60 * Contants.SOCKET_TIMEOUT_MINUTES);
((SSLSocket) socket).startHandshake();
logger.debug("handshake success.");
}
@Override
public Object call() throws Exception {
// Thread.currentThread().setName(name + ">>通信 " + serverHost);
Thread.currentThread().setName(name + ">>Communication " + serverHost);
Object object = null;
try {
init();
if(socket!=null){
out = socket.getOutputStream();
in = socket.getInputStream();
//设置超时时间
socket.setSoTimeout(1000 * 60 * Contants.SOCKET_TIMEOUT_MINUTES);
object = toDo();
}
}catch (Exception e) {
// object = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "异常";
object = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "anomaly";
logger.error("Communication anomaly" + Utils.printExceptionStack(e));
} finally {
logger.debug("关闭通信");
close();
}
return object;
}
protected Object toDo() throws Exception {
logger.debug("发送通信请求:" + reqCmd);
//-- 无效操作处理
if(StringUtils.isEmpty(reqCmd)){
return null;
}
boolean flag = false;
String msg = null;
String result = null;
//-- 命令判断
// 与Server通信
if(reqCmd.equals(REQ_HAND_SHAKE)){
flag = this.sendMessageByChar(reqCmd);
logger.debug("握手状态:" + (result = this.receiveMessageByChar()));
}
// 通知Server准备升级
if(reqCmd.equals(REQ_SERVER_UPGRADE)){
flag = this.sendMessageByChar(reqCmd);
result = this.receiveMessageByChar();
}
// 获取本机标志UUID
if(reqCmd.equals(REQ_LOCAL_UUID)){
flag = this.sendMessageByChar(reqCmd);
msg = this.receiveMessageByChar();
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
logger.info("本机标志UUID" + msg);
}
//注释 by jinsj 2012-0531 修改为DC主动获取NC时间
// 获取服务器系统时间
if(reqCmd.equals(REQ_SERVER_SYSTEMDATE)){
flag = this.sendMessageByChar(reqCmd);
msg = this.receiveMessageByChar();
logger.debug("服务器系统时间:" + msg);
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
}
// 发送本机的变更信息操作系统类型和IP
if(reqCmd.equals(REQ_LOCAL_CHANGE)){
// 发送请求
flag = this.sendMessageByChar(reqCmd);
result = this.receiveMessageByChar();
// 发送信息: UUID$@$操作系统类型$@$LocalIP
flag = this.sendMessageByChar((String)obj);
// 接收变更结果: 0/1 $@$ 信息
msg = this.receiveMessageByChar();
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
return msg;
}
// 初始化配置
if(reqCmd.equals(REQ_INIT_CONFIG)){
flag = this.sendMessageByChar(reqCmd);
msg = this.receiveMessageByChar();//接收配置信息
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
}
// 初始化任务
if(reqCmd.equals(REQ_INIT_TASK)){
// 发送请求
flag = this.sendMessageByChar(reqCmd);
result = this.receiveMessageByChar();
// 发送本机唯一标识
flag = this.sendMessageByChar(Contants.AGENT_HOST_UUID + "");
msg = this.receiveMessageByChar();
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
if (msg != null && !"".equals(msg)) {
JSONArray jsonArr = JSONArray.fromObject(msg);
//这里处理的任务原则上不应含有”1 文件推送“类型的任务
for (int i = 0; i < jsonArr.size(); i++) {
new TaskReqHandle().taskHandle(jsonArr.get(i).toString());
}
}
logger.debug("初始化任务完成--" + msg);
return null;
}
// 发送主动告警信息
if(reqCmd.equals(REQ_ALARM)){
if(obj!=null){
// 主动告警请求
flag = this.sendMessageByChar(reqCmd);
result = this.receiveMessageByChar();
// 发送主动告警信息内容
flag = this.sendMessageByChar((String)obj);
logger.debug("主动告警信息:" + (String)obj);
result = this.receiveMessageByChar();
}else{
logger.debug("主动告警信息为空");
}
}
// 发送任务结果
if(reqCmd.equals(REQ_TASK_RESULT)){
if(obj!=null){
//发送任务结果请求
flag = this.sendMessageByChar(reqCmd);
result = this.receiveMessageByChar();
//发送任务结果内容
flag = this.sendMessageByChar((String)obj);
result = this.receiveMessageByChar();
}else{
logger.warn("Task result information is empty");
}
}
// 批量上传监测数据【数据收集方式改为DC主动后此通信废弃】
if(reqCmd.equals(REQ_UPLOAD_DATAS)){
if(obj!=null && obj instanceof Object[]) {
Object[] objArr = (Object[])obj;
if (objArr != null && objArr.length > 1 && objArr[0] != null
&& objArr[1] != null && objArr[1] instanceof List) {
//发送上传数据请求
flag = this.sendMessageByChar(reqCmd);
result = this.receiveMessageByChar();
//上传数据
this.sendFileByBath((String) objArr[0], (List<File>) objArr[1]);
result = this.receiveMessageByChar();
}else{
logger.warn("Uploading the contents of the monitored data object is incorrect");
}
}else{
logger.warn("Uploading monitoring data objects is empty");
}
}
// 任务执行的回传文件单个文件发送断点续传【数据收集方式改为DC主动后此类废弃】
if(reqCmd.equals(REQ_TASK_RETURNFILE)){
if(obj!=null && obj instanceof ReturnFilePO){
ReturnFilePO rfPo = (ReturnFilePO)obj;
//发送回传文件请求
flag = this.sendMessageByChar(reqCmd);
result = this.receiveMessageByChar();
//发送回传文件任务信息
this.sendMessageByChar(TaskResultOper.getTaskResultMsg(rfPo
.getTaskId(), rfPo.getTaskType(), null, null, null, rfPo
.getStartTime(), rfPo.getEndTime(), rfPo.getIsLoop()));
result = this.receiveMessageByChar();
//发送回传文件文件名称
this.sendMessageByChar(rfPo.getReturnFileName());
result = this.receiveMessageByChar();
//发送回传文件
flag = this.bpSendFile(Contants.localTaskReturnPath + File.separator + rfPo.getReturnFileName());
result = this.receiveMessageByChar();
}else{
logger.warn("The return file object is empty");
}
}
// 发送压缩文件,断点续传
if(reqCmd.equals(REQ_BP_UPLOAD_FIFE)){
if(obj!=null && obj instanceof String[]) {
String[] strArr = (String[])obj;
if (strArr != null && strArr.length > 1){
//打包上传文件请求
flag = this.sendMessageByChar(reqCmd + ":" + strArr[0]);
result = this.receiveMessageByChar();
//发送打包文件名
File file = new File(strArr[1]);
flag = this.sendMessageByChar(file.getName());
result = this.receiveMessageByChar();
//上传打包文件
flag = this.bpSendFile(strArr[1]);
result = this.receiveMessageByChar();
//上传成功后删除或移动文件
if(flag && SUCCESS.equalsIgnoreCase(result) && file.exists()){
String dataType = strArr[0];
if(BP_TYPE_DETECT_DATA.equalsIgnoreCase(dataType)){
FileUtil.moveFile(file, Contants.localDataDonePath, true);
}else if(BP_TYPE_TASK_RESULT.equalsIgnoreCase(dataType)){
FileUtil.moveFile(file, Contants.localTaskDonePath, true);
}else if(BP_TYPE_TASK_RETURN.equalsIgnoreCase(dataType)){
FileUtil.moveFile(file, Contants.localTaskDonePath, true);
}
}
}
}
}
// 向DC发送NC端异常信息
if(reqCmd.equals(REQ_ERROR_INFO)){
if(obj!=null){
flag = this.sendMessageByChar(reqCmd);
result = this.receiveMessageByChar();
//发送异常内容
flag = this.sendMessageByChar((String)obj);
result = this.receiveMessageByChar();
}else{
logger.warn("Abnormal information is empty");
}
}
if (flag && (SUCCESS.equalsIgnoreCase(result) || msg!=null)) {
msg = Contants.COMMON_MSG_SUCCESS + Contants.COMMON_MSG_SEPRATOR + (msg!=null ? msg : "成功");
} else {
// msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "失败";
msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "failed";
}
logger.debug("SSLClient toDo()---" + msg);
logger.debug("发送通信请求结束:" + reqCmd);
// -- 命令判断
return msg;
}
}

View File

@@ -0,0 +1,534 @@
package com.nis.nmsclient.thread.socket;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FalseFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.common.SysConfig;
import com.nis.nmsclient.config.DetecConfReqHandle;
import com.nis.nmsclient.model.ReturnFilePO;
import com.nis.nmsclient.model.Task1;
import com.nis.nmsclient.model.Task4;
import com.nis.nmsclient.model.Task6;
import com.nis.nmsclient.thread.alarm.AlarmUtil;
import com.nis.nmsclient.thread.alarm.ErrorCode;
import com.nis.nmsclient.thread.task.AgentCommand;
import com.nis.nmsclient.thread.task.TaskReqHandle;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.FileWrUtil;
import com.nis.nmsclient.util.Utils;
/**
* 用于安全通讯的服务Socket采用java中的SSLServerSocket
* 接收服务端发送过来的对该客户端的各配置参数信息,并对相应程序进行设置
**/
public class SSLServer implements Runnable {
static Logger logger = Logger.getLogger(SSLServer.class);
SSLServerSocket ss = null;
private String startTime;// 服务启动时间
public SSLServer() throws IOException{
init();
}
/**
* 初始化服务Socket
**/
public void init() throws IOException {
try {
startTime = System.currentTimeMillis() + "";
//初始化上下文
SSLContext ctx = SSLCertOper.getSSLContext();
ss = (SSLServerSocket) ctx.getServerSocketFactory()
.createServerSocket(Contants.SOCKET_AGENT_PORT);
ss.setNeedClientAuth(true);// 客户端要认证
} catch (Exception e) {
logger.error(Utils.printExceptionStack(e));
// throw new IOException("NmsClient监听端口[" + Contants.SOCKET_AGENT_PORT + "]创建失败");
throw new IOException("NmsClient monitor port[" + Contants.SOCKET_AGENT_PORT + "]Create failure");
}
}
/**
* 重载方法run 处理客户端的请求
**/
public void run() {
logger.info("通讯线程启动 成功");
Socket socket = null;
while(true){
try {
socket = ss.accept();
if(!Common.NC_UPGRADE_FLAG){//当NC_UPGRADE_FLAG为false时允许建立通讯否则放弃通讯用于NC升级功能
logger.debug("来自:"+socket.getInetAddress().getHostAddress());
Common.service.submit(new ServerThread(socket));
}else{ //关闭 放弃的通讯
logger.info("NC升级 抛弃通讯:"+socket.getInetAddress().getHostAddress());
socket.close();
}
} catch (Exception e) {
logger.error("Failure to establish communication " + ss.getInetAddress().getHostAddress()
+ ": " + Utils.printExceptionStack(e));
}
}
}
class ServerThread extends CommonSocket implements Runnable {
public ServerThread(Socket s) throws Exception {
super(s);
}
public void run(){
String ip = null;
try {
ip = socket.getInetAddress().getHostAddress();
//设置超时时间
socket.setSoTimeout(1000 * 60 * Contants.SOCKET_TIMEOUT_MINUTES);
// Thread.currentThread().setName("通讯线程 》" + socket.getInetAddress().getHostAddress());
Thread.currentThread().setName("Communication Thread 》" + socket.getInetAddress().getHostAddress());
String msg = this.receiveMessageByChar();
logger.info("接收请求 " + msg);
if(REQ_HAND_SHAKE.equalsIgnoreCase(msg)){// 握手操作
//返回接收到的信息和NMSClient启动时间
this.sendMessageByChar(SUCCESS+":"+msg+"|"+startTime);
}else if(SERVER_COLLECT_DATA.equalsIgnoreCase(msg)){// DC主动向NC收集数据
/** ----当前通信DC_IP与配置DC_IP不同更新IP---- **/
if(!Contants.SOCKET_SERVER_HOST.equals(ip)){
logger.info("变更通信DC_IP: " + Contants.SOCKET_SERVER_HOST + " --> " + ip);
Contants.SOCKET_SERVER_HOST = ip;
/** ----SeqId未取到更新配置文件---- **/
if(Contants.AGENT_HOST_UUID == null){
SysConfig.updateConfigFile(Contants.SOCKET_SERVER_HOST_KEY, Contants.SOCKET_SERVER_HOST);
}
}
new ServerCollectData(this).sendData();
}else if(SERVER_GET_TASKRESULT.equalsIgnoreCase(msg)){// DC再次向NC获取未入库的任务结果
this.sendMessageByChar(SUCCESS);
String taskInfo = null;
List<File> fileList = new LinkedList<File>();
while(!END.equalsIgnoreCase(taskInfo = this.receiveMessageByChar())){
// taskInfo组织顺序TaskId、TaskType、isLoop、startTime、endTime
String[] infos = taskInfo.split(Contants.COMMON_MSG_SEPRATOR_SPLIT);
if (infos.length < 4 || (!"0".equals(infos[2]) && infos.length < 5)) {// 参数个数不够4或者周期任务参数个数不够5则跳过本次处理
logger.warn("DC gets the task result again, the task attribute is incomplete, skip this processing.");
this.sendMessageByChar(AgentCommand.RESULT_FAIL
// + Contants.COMMON_MSG_SEPRATOR + "任务参数不全,找不到任务结果");
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.SSLServer.sendMsg_n81i");
continue;
}
String eTime = null;
if(infos.length > 4){// 非周期任务参数个数为4
eTime = infos[4];
}
// 检查指定任务的的结果或回传文件是否存在若存在移动到incoming目录若不存在回复失败
boolean isExistResult = checkTaskResultExist(infos[0], infos[1], infos[2], infos[3], eTime, fileList);
if(isExistResult){
this.sendMessageByChar(AgentCommand.RESULT_OK
+ Contants.COMMON_MSG_SEPRATOR + "");
} else {
this.sendMessageByChar(AgentCommand.RESULT_FAIL
// + Contants.COMMON_MSG_SEPRATOR + "任务结果不存在");
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.SSLServer.noResult_n81i");
}
}
this.sendMessageByChar(SUCCESS);
// 移动存在的任务结果和回传文件
moveTaskResultOrReturn(fileList);
}else if(REQ_SERVER_SYSTEMDATE.equalsIgnoreCase(msg)){//add by jinsj 2012-05-31 DC主动获取NC时间
this.sendMessageByChar(new Date().getTime()+"");
this.receiveMessageByChar();
}else if(Contants.AGENT_HOST_UUID == null ){
// 除了以上三个通信其他通信都得判断SeqID是否获取到若未取到则要放弃通讯
logger.info("NC尚未取到SeqID 抛弃通讯:"+socket.getInetAddress().getHostAddress());
close();
return;
}
if(REQ_CERT.equalsIgnoreCase(msg)){
this.sendMessageByChar(SUCCESS);
//接收证书
this.receiveFileByByte(Contants.keyPath + File.separator + "server.cer");
this.sendMessageByChar(SUCCESS);
//导入认证证书到库
SSLCertOper.importCertToStore("serverks"
+ DateUtil.getCurrentDate(DateUtil.YYYYMMDD),
Contants.SSL_TRUST_KEY_STORE,
Contants.keyPath + File.separator + "server.cer", Contants.SSL_KEY_STORE_PASS);
File file = new File(Contants.keyPath + File.separator + "server.cer");
if(file.exists()){
FileUtil.delDir(file);
}
/*SSLClient sc = new SSLClient();
SSLCertOper.CreateAndSendCert(sc);
sc.close();*/
}else if(SERVER_UPDATE_CONFIG.equalsIgnoreCase(msg)){// 更新监测配置
this.sendMessageByChar(SUCCESS);
String str = this.receiveMessageByChar();
this.sendMessageByChar(SUCCESS);
logger.debug("updateConfig-->" + str);
new DetecConfReqHandle().handlerConfigByUpdate(str);
} else if(SEND_PLUGIN_SCRIPT_FILE.equalsIgnoreCase(msg)) { // 下发脚本
File pluginDir = new File(Contants.localPluginScriptPath);
this.sendMessageByChar(SUCCESS);
String fileNames = this.receiveMessageByChar();
Collection<?> files = FileUtils.listFiles(pluginDir,
new PrefixFileFilter(fileNames.split(",")), FalseFileFilter.FALSE);
for (Object file : files) {
((File)file).delete();
}
this.sendMessageByChar(SUCCESS);
this.bpReceiveFileByBath(pluginDir.getCanonicalPath());
this.sendMessageByChar(SUCCESS);
} else if(SERVER_FILE_PUSH.equalsIgnoreCase(msg)){//任务操作:文件推送
this.sendMessageByChar(SUCCESS);
String str = this.receiveMessageByChar();
logger.debug("task-->" + str);
this.sendMessageByChar(SUCCESS);
JSONObject jsonObj = JSONObject.fromObject(str);
String resultMsg = null;
if(str.contains("taskInfo")){
JSONObject jsonObj2 = jsonObj.getJSONObject("taskInfo");
Object obj = JSONObject.toBean(jsonObj2,Task1.class);
Task1 fileInfo = (Task1) obj;
// 接收文件
resultMsg = new TaskReqHandle().filePush(this, fileInfo.getTaskParam(), fileInfo.getTaskId(), false);
}
if (resultMsg !=null && Contants.isSucessByResult(resultMsg)) {
this.sendMessageByChar(AgentCommand.RESULT_OK
+ Contants.COMMON_MSG_SEPRATOR
// + "成功,详细信息如下:" + Contants.getDescByResult(resultMsg));
+ "i18n_client.SSLServer.success_n81i" + Contants.getDescByResult(resultMsg));
} else {
this.sendMessageByChar(AgentCommand.RESULT_FAIL
+ Contants.COMMON_MSG_SEPRATOR
// + "失败,详细信息如下:" + Contants.getDescByResult(resultMsg));
+ "i18n_client.SSLServer.fail_n81i:" + Contants.getDescByResult(resultMsg));
}
this.receiveMessageByChar();
}else if(SERVER_UPGRADE.equalsIgnoreCase(msg)){//任务操作:升级
this.sendMessageByChar(SUCCESS);
String str = this.receiveMessageByChar();
logger.debug("task-->" + str);
this.sendMessageByChar(SUCCESS);
JSONObject jsonObj = JSONObject.fromObject(str);
String resultMsg = null;
TaskReqHandle handle = new TaskReqHandle();
if(str.contains("taskInfo")){
JSONObject jsonObj2 = jsonObj.getJSONObject("taskInfo");
Object obj = JSONObject.toBean(jsonObj2,Task6.class);
Task6 task = (Task6) obj;
// 判断是否重新执行任务,并作提前处理
reExecTask(task.getTaskId(), task.getOldTaskId());
// 接收升级文件
resultMsg = handle.filePush(this, task.getCommandParam(),
task.getTaskId(), true);
}
if (resultMsg !=null && Contants.isSucessByResult(resultMsg)) {
this.sendMessageByChar(AgentCommand.RESULT_SEND_OK
+ Contants.COMMON_MSG_SEPRATOR
// + "下发成功,详细信息如下:" + Contants.getDescByResult(resultMsg));
+ "i18n_client.SSLServer.lssueSuccess_n81i" + Contants.getDescByResult(resultMsg));
} else {
this.sendMessageByChar(AgentCommand.RESULT_FAIL
+ Contants.COMMON_MSG_SEPRATOR
// + "失败,详细信息如下: " + Contants.getDescByResult(resultMsg));
+ "i18n_client.SSLServer.fail_n81i: " + Contants.getDescByResult(resultMsg));
}
String receiveMsg = this.receiveMessageByChar();
if(resultMsg !=null && Contants.isSucessByResult(resultMsg) &&receiveMsg.equals(SUCCESS)){//处理升级
handle.taskHandle(str);
}
}else if(SERVER_TASK.equalsIgnoreCase(msg)){//任务操作:命令执行和升级逆向任务
this.sendMessageByChar(SUCCESS);
String str = this.receiveMessageByChar();
logger.debug("task-->" + str);
this.sendMessageByChar(AgentCommand.RESULT_SEND_OK
// + Contants.COMMON_MSG_SEPRATOR + "下发成功");
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.SSLServer.lssueSuccess1_n81i");
this.receiveMessageByChar();
//2015-6-23 针对reboot命令之前存在会多次重启的问题现修改为接收到命令执行任务时如果该任务的结果已经存在incoming或者done里有则不再执行
try {
int taskType = 0;
JSONObject jsonObj = JSONObject.fromObject(str);
if(str.contains("typeInfo")){
taskType = jsonObj.getInt("typeInfo");
}
if(str.contains("taskInfo") && taskType==4){//taskType命令执行任务4
JSONObject jsonObj2 = jsonObj.getJSONObject("taskInfo");
Task4 task4 = (Task4)JSONObject.toBean(jsonObj2,Task4.class);
String taskId = task4.getTaskId()==null?"0":(task4.getTaskId()+"");
String isLoop = task4.getIsLoop()+"";
String startTime = task4.getStartTime()==null?"":task4.getStartTime()+"";
String endTime = task4.getEndTime()==null?"":task4.getEndTime()+"";
if(task4.getCommandType() == 2){//命令执行(4)->可执行命令(2)
logger.info("可执行命令 taskId:" + task4.getTaskId());
List<File> fileList = new ArrayList<File>();
boolean isExist = checkTaskResultExistFromDoneAndIncoming(taskId+"", taskType+"", isLoop, startTime, endTime, fileList);//非周期任务0
if(isExist) {
logger.info("任务已执行不再重复执行taskId"+taskId+" taskType"+taskType);
return;
}
}
}
} catch (Exception e) {
logger.error("For the next task, determine whether there is a result, if the result is no longer performing the exception", e);
}
new TaskReqHandle().taskHandle(str);
}else if(SERVER_TASK_CANCEL.equalsIgnoreCase(msg)){//任务撤消操作
this.sendMessageByChar(SUCCESS);
String str = this.receiveMessageByChar();
logger.debug("taskcancle-->" + str);
if(str!=null && !"".equals(str)){
Common.cancleTaskFuture(Long.parseLong(str), 0);
}
this.sendMessageByChar(AgentCommand.MISSION_CANCEL_FINISH
// + Contants.COMMON_MSG_SEPRATOR + "任务已撤消完成");
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.SSLServer.missionRevokeSuccess_n81i");
this.receiveMessageByChar();
}else if(ACTIVE_ALARM_START_ALERT.equalsIgnoreCase(msg)){// NC端是否报主动告警 变更
this.sendMessageByChar(SUCCESS);
String str = this.receiveMessageByChar();
this.sendMessageByChar(SUCCESS);
logger.debug("isStartActiveAlarm-->" + str);
JSONObject jsonObj = JSONObject.fromObject(str);
Boolean isStartActiveAlarm = (Boolean)jsonObj.get("showAutoAlarm");
String webHandleTime = (String)jsonObj.get("webHandleTime");
//更新Contants.ACTIIVE_ALARM_START
if(isStartActiveAlarm!=null) {
Contants.ACTIIVE_ALARM_START = isStartActiveAlarm;
logger.info("NC是否主动告警"+Contants.ACTIIVE_ALARM_START+" web端操作时间"+webHandleTime);
}
}
logger.debug("接收请求 " + msg + " 完成");
} catch (Exception e) {
logger.error("Receiving information anomaly" + Utils.printExceptionStack(e));
if(ip==null){
ip = Utils.getLocalIp();
}
// AlarmUtil.sendNMSErrorMsg(ErrorCode.SocketError, ip , "NC通讯线程异常" + e.getMessage());
AlarmUtil.sendNMSErrorMsg(ErrorCode.SocketError, ip , "NC communication thread exception" + e.getMessage());
return;
} finally {
logger.debug("关闭通信");
close();
}
}
/**
* 重新执行任务,针对升级任务的推送文件的提前处理,将原任务的文件拷贝到新任务的临时目录
* @param taskId
* @param oldTaskId
* @throws Exception
*/
private void reExecTask(Long taskId, Long oldTaskId) throws Exception {
// 如果原任务ID为空说明不是重新执行任务不执行任何操作
if (oldTaskId == null || "".equals(oldTaskId.toString())
|| "0".equals(oldTaskId.toString())) {
return;
}
File tempDir = new File(Contants.localTempDataIncomingPath + File.separator
+ "filepush_" + taskId);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
// 如果是升级任务推送文件的保存路径getUpgradeTaskPushPath(taskId)
File oldFileDir = new File(TaskReqHandle.getUpgradeTaskPushPath(oldTaskId));
try {
if(oldFileDir.exists()){
FileUtils.copyDirectory(oldFileDir, tempDir);
}
} catch (IOException e) {
logger.error(e);
}
}
/**
* DC再次获取任务结果 -- 检查指定任务的的结果或回传文件是否存在
* @param isLoop 是否循环任务: 0 非周期, 1 周期
* @param startTime 若非周期任务,升级时间或创建时间;若周期任务,某一周期的起始时间
* @param endTime 若非周期任务,为空;若周期任务,某一周期的结束时间
* @param fileList 用于存在找到的结果文件和回传文件
* @return
*/
private boolean checkTaskResultExist(String taskId, String taskType, String isLoop, String sTime, String eTime, List<File> fileList) throws Exception{
long startTime=(sTime==null || "".equals(sTime) || "null".equals(sTime)) ? 0l : Long.parseLong(sTime);
long endTime=(eTime==null || "".equals(eTime) || "null".equals(eTime)) ? 0l : Long.parseLong(eTime);
logger.debug("checkTaskResultExist startTime=" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(startTime))+" -- endTime=" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(endTime)));
String dateName = DateUtil.getStingDate(DateUtil.YYYYMMDD, new Date(startTime));
String prefix = "tasktype" + taskType + "_" + taskId;
boolean isExistResult = false;
// 依次取nc_task/done下的result和return目录
File[] fileDirs = FileUtil.getDirectoryArray(new File(Contants.localTaskDonePath));
if(fileDirs==null){
logger.info("fileDirs为空");
}else{
try{
for(File dir : fileDirs){
// -- 找到指定的日期目录dateName及之后的日期目录
File[] dateFiles = FileUtil.sortASCByFileName(FileUtil.getDirsAfterDateName(dir, dateName));
// -- 在找到的日期目录下检查文件是否存在
for(File dateFile : dateFiles){
File[] files = null;
if("0".equals(isLoop)){// 0 非周期
files = FileUtil.getFilesStartWith(dateFile, prefix);
}else{//--- 周期任务取某一时间段内的结果与回传文件
files = FileUtil.getFilesStartWithByMillis(dateFile, prefix, startTime, endTime);
}
if(files.length>0){// 若在任一目录下找到则不用再找其他日期目录跳出第二个For循环
fileList.addAll(Arrays.asList(files));
isExistResult = true;
break;
}
}
}
}catch(Exception e){
logger.error(e);
}
}
if(!isExistResult){
logger.info("再次获取任务结果 > TaskId: " + taskId + ", TaskType: "
+ taskType + ", IsLoop: " + isLoop + " > 任务结果不存在");
}
return isExistResult;
}
/**
*
* 检查done和incoming里是否有任务结果信息如果有则不进行再次执行避免重复执行任务如重复reboot
* @author dell Jun 23, 2015
* @version 1.0
* @param taskId
* @param taskType
* @param isLoop
* @param sTime
* @param eTime
* @param fileList
* @return
* @throws Exception
*/
private boolean checkTaskResultExistFromDoneAndIncoming(String taskId, String taskType, String isLoop, String sTime, String eTime, List<File> fileList) throws Exception{
long startTime=(sTime==null || "".equals(sTime) || "null".equals(sTime)) ? 0l : Long.parseLong(sTime);
long endTime=(eTime==null || "".equals(eTime) || "null".equals(eTime)) ? 0l : Long.parseLong(eTime);
logger.debug("checkTaskResultExist startTime=" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(startTime))+" -- endTime=" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(endTime)));
String dateName = DateUtil.getStingDate(DateUtil.YYYYMMDD, new Date(startTime));
String prefix = "tasktype" + taskType + "_" + taskId;
boolean isExistResult = false;
// 依次取nc_task/done和incoming下的result和return目录
File[] fileDoneDirs = FileUtil.getDirectoryArray(new File(Contants.localTaskDonePath));
File[] fileIncomingDirs = FileUtil.getDirectoryArray(new File(Contants.localTaskResultPath));
File[] fileDirs = null;
if(fileIncomingDirs!=null && fileDoneDirs!=null) {
fileDirs = (File[])ArrayUtils.addAll(fileDoneDirs, fileIncomingDirs);
}
if(fileDirs==null){
logger.info("fileDirs为空");
}else{
try{
for(File dir : fileDirs){
// -- 找到指定的日期目录dateName及之后的日期目录
File[] dateFiles = FileUtil.sortASCByFileName(FileUtil.getDirsAfterDateName(dir, dateName));
// -- 在找到的日期目录下检查文件是否存在
for(File dateFile : dateFiles){
File[] files = null;
if("0".equals(isLoop)){// 0 非周期
files = FileUtil.getFilesStartWith(dateFile, prefix);
}else{//--- 周期任务取某一时间段内的结果与回传文件
files = FileUtil.getFilesStartWithByMillis(dateFile, prefix, startTime, endTime);
}
if(files.length>0){// 若在任一目录下找到则不用再找其他日期目录跳出第二个For循环
fileList.addAll(Arrays.asList(files));
isExistResult = true;
break;
}
}
}
}catch(Exception e){
logger.error(e);
}
}
if(!isExistResult){
logger.info("判断新下发的任务结果是否已经存在 > TaskId: " + taskId + ", TaskType: "
+ taskType + ", IsLoop: " + isLoop + " > 任务结果不存在");
}else {
logger.info("判断新下发的任务结果是否已经存在 > TaskId: " + taskId + ", TaskType: "
+ taskType + ", IsLoop: " + isLoop + " > 任务结果已存在");
}
return isExistResult;
}
/**
* DC再次获取任务结果 -- 移动找到的结果文件和回传文件到incoming目录
*/
private void moveTaskResultOrReturn(List<File> fileList){
if(fileList==null || fileList.size()==0){
return;
}
try {
for(File file : fileList){
// ---------- 任务回传文件处理
if(file.getName().endsWith(Contants.TASK_RETURN_FILE_SUFFIX)){
if(!file.exists() || !file.isFile()){
continue;
}
// 移动实际回传的文件
String[] resultArr = FileWrUtil.cfgFileReader(file);
if (resultArr != null && resultArr.length > 0) {
JSONObject jsonObject = JSONObject.fromObject(resultArr[0]);
ReturnFilePO rfPo = (ReturnFilePO) JSONObject.toBean(jsonObject, ReturnFilePO.class);
if(rfPo.getReturnFileName()!=null && !"".equals(rfPo.getReturnFileName())){
File returnFile = new File(file.getParent() + File.separator + rfPo.getReturnFileName());
FileUtil.moveFile(returnFile, Contants.localTaskReturnPath, true);
}
}
// 移动记录任务回传的临时文件
FileUtil.moveFile(file, Contants.localTaskReturnPath, true);
}else {
// ---------- 任务结果处理
FileUtil.moveFile(file, Contants.localTaskResultPath, true);
}
}
} catch (Exception e) {
logger.error("Get the task result again > mobile file exception again", e);
}
}
}
}

View File

@@ -0,0 +1,581 @@
package com.nis.nmsclient.thread.socket;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.model.ReturnFilePO;
import com.nis.nmsclient.thread.task.TaskResultOper;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.FileWrUtil;
import com.nis.nmsclient.util.StringUtil;
import com.nis.nmsclient.util.Utils;
import com.nis.nmsclient.util.ZipUtil;
/**
* 用于定时扫描并上传监测数据文件
*
**/
public class ServerCollectData {
Logger logger = Logger.getLogger(ServerCollectData.class);
private CommonSocket socket;
public ServerCollectData(CommonSocket socket) {
this.socket = socket;
}
public void sendData() {
logger.debug("传送数据开始 ~~~~~~~");
try {
// 发送监测数据
handleDetectData();
// 发送任务结果
handleTaskResult();
// 发送任务回传文件
handleTaskReturnFile();
// 发送任务结果--针对有回传文件时写的任务结果
handleTaskResult();
// 结束通讯
socket.sendMessageByChar(CommonSocket.END);
} catch (Exception e) {
logger.error("Transmits data anomalies" + Utils.printExceptionStack(e));
}
logger.debug("传送数据结束 ~~~~~~~");
}
private void handleDetectData() throws Exception {
logger.debug("传送监测数据开始 ~~~~~~~");
long startTime = System.currentTimeMillis();
File parDir = new File(Contants.localDataCollection);
if(!parDir.exists()){
return;
}
// == 1、针对数据文件过多时打包上传未完成的文件继续上传
// ------------取所有未上传完成的Zip文件
List<File> fileList = new LinkedList<File>();
File[] fileArr = FileUtil.getFilesEndWith(parDir, ".zip");
if (fileArr != null && fileArr.length > 0) {
for (File file : fileArr) {
if (!file.getName().startsWith(CommonSocket.BP_TYPE_DETECT_DATA)) {
continue;
}
fileList.add(file);
}
}
// ------------传送Zip文件
if (fileList.size() > 0) {
sendZipFile(fileList, CommonSocket.DATA_TYPE_ZIP_DETECT);
}
// == 2、检查当前数据文件数量批量发送文件或打包上传
File dataDir = new File(Contants.localDataFilePath);
if (!dataDir.exists()) {
logger.warn("Data directory“" + dataDir.getAbsolutePath() + "”Non-existent");
} else {
long total = 0;
List<File> allFiles = new ArrayList<File>();
File[] dataDirs = FileUtil.getDirectoryArray(dataDir);
// ---- 数据处理
total = handleNullDataFile(allFiles, dataDirs);
logger.info("本次收集监测数据文件总数:" + total + ", 正常数据:" + allFiles.size() + ", 空数据:" + (total - allFiles.size()));
total = allFiles.size();// 正常的要上传的数据个数
// --- 将所有数据文件一起打包,发送
if (total > Contants.COMMON_ZIP_MIN_SIZE) {
long zipCnt = total/Contants.COMMON_ZIP_MAX_SIZE;
if (zipCnt > 0) {//2013-5-6 未上传的数据太多时,将监测数据压缩为多个文件
for(int i=0; i<total/Contants.COMMON_ZIP_MAX_SIZE; i++){
// 组织每次压缩的文件数组
File[] dataFiles = new File[Contants.COMMON_ZIP_MAX_SIZE];
int start = i * Contants.COMMON_ZIP_MAX_SIZE;
int end = (i + 1) * Contants.COMMON_ZIP_MAX_SIZE;
for (int j = start, k = 0; j < end; j++, k++) {
dataFiles[k] = allFiles.get(j);
}
// 压缩并发送监测数据
compressAndSendDetecData(dataFiles);
}
}else{
// 压缩并发送监测数据
compressAndSendDetecData(dataDirs);
}
logger.info("本次收集将所有监测数据打包传送,监测数据总数:" + total + ",用时:"
+ (System.currentTimeMillis() - startTime) + "ms");
} else if (total > 0) {
// -- 按正常所有监测数据批量上传
sendCSVData(dataDir, allFiles);
logger.info("本次收集传送监测数据总数:" + total + ",用时:"
+ (System.currentTimeMillis() - startTime) + "ms");
} else {
logger.info("本次收集未传送监测数据");
}
}
logger.debug("传送监测数据结束 ~~~~~~~");
}
private void handleTaskResult() throws Exception {
logger.debug("传送任务结果开始 ~~~~~~~");
long startTime = System.currentTimeMillis();
// == 1、针对结果文件过多时打包上传未完成的文件继续上传
File taskDir = new File(Contants.localTaskPath);
if (!taskDir.exists()) {
return;
}
// ------------取所有未上传完成的Zip文件
List<File> fileList = new LinkedList<File>();
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
if (zipArr != null && zipArr.length > 0) {
for (File file : zipArr) {
if (!file.getName().startsWith(CommonSocket.BP_TYPE_TASK_RESULT)) {
continue;
}
fileList.add(file);
}
}
// ------------传送Zip文件
if(fileList.size()>0){
sendZipFile(fileList, CommonSocket.DATA_TYPE_ZIP_TASKRESULT);
}
// == 2、检查当前结果文件数量批量发送文件或打包上传
File resultDir = new File(TaskResultOper.getTaskResultPath());
if(!resultDir.exists()){
return;
}
File[] fileArr = FileUtil.getFilesEndWith(resultDir, Contants.TASK_RESULT_FILE_SUFFIX);
// -- 将所有任务结果文件一起打包,发送
if(fileArr.length > Contants.COMMON_ZIP_MIN_SIZE){
int zipCnt = fileArr.length/Contants.COMMON_ZIP_MAX_SIZE;
if(zipCnt>0){//2013-5-6 未上传的结果文件太多时,将结果文件压缩为多个文件
for(int i=0; i<fileArr.length/Contants.COMMON_ZIP_MAX_SIZE; i++){
// 组织每次压缩的文件数组
File[] resultFiles = new File[Contants.COMMON_ZIP_MAX_SIZE];
int start = i * Contants.COMMON_ZIP_MAX_SIZE;
int end = (i + 1) * Contants.COMMON_ZIP_MAX_SIZE;
for(int j=start,k=0; j<end; j++,k++){
resultFiles[k] = fileArr[j];
}
// 压缩并发送任务结果
compressAndSendTaskResult(resultFiles);
}
}else{
// 压缩并发送任务结果
compressAndSendTaskResult(fileArr);
}
logger.info("本次收集将所有任务结果打包传送,任务结果总数:" + fileArr.length + ",用时:" + (System.currentTimeMillis() - startTime) + "ms");
}else if(fileArr.length > 0){
// -- 按正常的多个结果批量发送
sendTaskResult(fileArr);
logger.info("本次收集传送任务结果总数:" + fileArr.length + ",用时:"
+ (System.currentTimeMillis() - startTime) + "ms");
} else {
logger.info("本次收集未传送任务结果");
}
logger.debug("传送任务结果结束 ~~~~~~~");
}
private void handleTaskReturnFile() throws Exception {
logger.debug("传送回传文件开始 ~~~~~~~");
long startTime = System.currentTimeMillis();
// == 1、针对回传文件过多时打包上传未完成的文件继续上传
File taskDir = new File(Contants.localTaskPath);
if (!taskDir.exists()) {
return;
}
// ------------取所有未上传完成的Zip文件
List<File> fileList = new LinkedList<File>();
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
if (zipArr != null && zipArr.length > 0) {
for (File file : zipArr) {
if (!file.getName().startsWith(CommonSocket.BP_TYPE_TASK_RETURN)) {
continue;
}
fileList.add(file);
}
}
// ------------传送Zip文件
if(fileList.size()>0){
sendZipFile(fileList, CommonSocket.DATA_TYPE_ZIP_TASKRETURN);
}
// == 2、检查当前回传文件数量单个发送文件或打包上传
File returnDir = new File(Contants.localTaskReturnPath);
if(!returnDir.exists()){
return;
}
File[] fileArr = FileUtil.getFilesEndWith(returnDir, Contants.TASK_RETURN_FILE_SUFFIX);
if(fileArr == null || fileArr.length == 0){
return;
}
//--- 将所有任务的回传文件及回传信息保存文件一起打包,发送
if(fileArr.length > Contants.COMMON_MAX_RETURN_CNT){
//压缩并删除原文件
String compressFileStr = Contants.localTaskPath
+ File.separator
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RETURN,
null, true)
+ ".zip";
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现现修改将任务结果和回传文件压缩时不删除文件而是将其移动到相应的日期目录
ZipUtil.zipWithMoveFile(returnDir.listFiles(), compressFileStr, false);
//发送
sendZipFile(new File(compressFileStr), CommonSocket.DATA_TYPE_ZIP_TASKRETURN);
logger.info("本次收集将所有任务回传文件打包传送,回传文件总数:" + fileArr.length + ",用时:" + (System.currentTimeMillis() - startTime) + "ms");
}else if(fileArr.length > 0){
//-- 按正常的一个任务一个任务的回传
sendTaskReturn(fileArr);
logger.info("本次收集传送任务回传总数:" + fileArr.length + ",用时:"
+ (System.currentTimeMillis() - startTime) + "ms");
} else {
logger.info("本次收集未传送任务回传文件");
}
logger.debug("传送回传文件结束 ~~~~~~~");
}
/**
* 遍历所有准备上传的数据文件,将空数据文件移动到指定目录,并记录所有文件总数
* @param allFiles 所有非空文件集合
* @param dataDirs 所有数据目录
* @return 所有文件个数(包括空文件)
* @throws Exception
*/
private long handleNullDataFile(List<File> allFiles, File[] dataDirs) throws Exception {
long total = 0l;
for(File dir : dataDirs){
File[] files = FileUtil.getFilesEndWith(dir, ".csv");
if(files==null || files.length==0){
continue;
}
files = FileUtil.sortASCByModify(files); // 修改日期升序排序
total += files.length;
for (File file : files) {
if (file.length() > 0) {
allFiles.add(file);
continue;
}
//--- 处理空文件数据:移动空文件数据到指定日期目录
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = Contants.localDataErrorPath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
FileUtil.moveFile(file, newDir, true);
}
}
return total;
}
private void compressAndSendDetecData(File[] dataFiles) throws Exception{
// 压缩并移动原文件
String compressFileStr = Contants.localDataCollection
+ File.separator
+ CommonSocket.addTimeTagForFileName(
CommonSocket.BP_TYPE_DETECT_DATA, null, true)
+ ".zip";
// 2013-3-29 由于压缩上传数据后,主动告警线程部分对数据检查存在问题,现将压缩后删除数据改为移动数据到日期目录
ZipUtil.zipWithMoveFile(dataFiles, compressFileStr, true);
// 发送
sendZipFile(new File(compressFileStr), CommonSocket.DATA_TYPE_ZIP_DETECT);
}
private void compressAndSendTaskResult(File[] resultFiles) throws Exception {
//压缩并删除原文件
String compressFileStr = Contants.localTaskPath
+ File.separator
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RESULT, null, true)
+ ".zip";
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现现修改将任务结果和回传文件压缩时不删除文件而是将其移动到相应的日期目录
ZipUtil.zipWithMoveFile(resultFiles, compressFileStr, true);
//发送
sendZipFile(new File(compressFileStr), CommonSocket.DATA_TYPE_ZIP_TASKRESULT);
}
private void compressAndSendTaskReturn(File[] returnFiles) throws Exception {
//压缩并删除原文件
String compressFileStr = Contants.localTaskPath
+ File.separator
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RETURN,
null, true)
+ ".zip";
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现现修改将任务结果和回传文件压缩时不删除文件而是将其移动到相应的日期目录
ZipUtil.zipWithMoveFile(returnFiles, compressFileStr, false);
//发送
sendZipFile(new File(compressFileStr), CommonSocket.DATA_TYPE_ZIP_TASKRETURN);
}
/**
* 发送打包文件:整个命令通信包装方法
* @param file
* @throws Exception
*/
private void sendZipFile(File file, String dataType) throws Exception {
//打包上传文件请求
socket.sendMessageByChar(dataType);
socket.receiveMessageByChar();
socket.sendMessageByChar(file.getName());//发送打包文件名
socket.receiveMessageByChar();
//上传打包文件
socket.bpSendFile(file.getAbsolutePath());
String result = socket.receiveMessageByChar();
//上传成功后移动文件
if(CommonSocket.SUCCESS.equalsIgnoreCase(result)){
if(CommonSocket.DATA_TYPE_ZIP_DETECT.equalsIgnoreCase(dataType)){
FileUtil.moveFile(file, Contants.localDataDonePath, true);
}else if(CommonSocket.DATA_TYPE_ZIP_TASKRESULT.equalsIgnoreCase(dataType)){
FileUtil.moveFile(file, Contants.localTaskDonePath, true);
}else if(CommonSocket.DATA_TYPE_ZIP_TASKRETURN.equalsIgnoreCase(dataType)){
FileUtil.moveFile(file, Contants.localTaskDonePath, true);
}
}
}
private void sendZipFile(List<File> fileList, String dataType) throws Exception {
for(File file : fileList){
sendZipFile(file, dataType);
}
}
// 批量发送ZIP的
/*private void sendZipFile(List<File> fileList, String parDir) throws Exception {
//打包上传文件请求
socket.sendMessageByChar(CommonSocket.DETECT_DATA_TYPE_ZIP);
socket.receiveMessageByChar();
//上传打包文件
socket.bpSendFileByBath(fileList, parDir);
String result = socket.receiveMessageByChar();
//上传成功后移动文件
if(CommonSocket.SUCCESS.equalsIgnoreCase(result)){
for(File file : fileList){
FileUtil.moveFile(file, Contants.localDataDonePath, true);
}
}
}*/
/**
* 批量发送CSV数据文件
* @param dataDir
* @param allFiles
* @throws Exception
*/
private void sendCSVData(File dataDir, List<File> allFiles) throws Exception {
//发送上传数据请求
socket.sendMessageByChar(CommonSocket.DATA_TYPE_CSV_DETECT);
socket.receiveMessageByChar();
//上传数据
socket.sendFileByBath(dataDir.getParent(), allFiles);
String result = socket.receiveMessageByChar();
if (CommonSocket.SUCCESS.equalsIgnoreCase(result)) {
/**
* 移动上传成功的数据文件到指定日期目录
*/
File[] files = new File[allFiles.size()];
moveDetecDataToDateDir(allFiles.toArray(files));
}
}
/**
* 批量发送任务结果
* @param fileArr
* @throws Exception
*/
private void sendTaskResult(File[] fileArr) throws Exception {
//2013-4-16 修改升序排列方式:按修改时间 改为 按文件名,任务结果文件名都有时间后缀(ms)文件修改时间只到s取不到ms
fileArr = FileUtil.sortASCByFileName(fileArr);
List<String> results = new LinkedList<String>();
StringBuffer sb = new StringBuffer();
for(File file : fileArr){
sb.delete(0, sb.length());
if(!file.exists() || !file.isFile()){
continue;
}
String[] resultArr = FileWrUtil.cfgFileReader(file);
if(resultArr!=null && resultArr.length>0){
for(String res : resultArr){
sb.append(res + ";");
}
sb.deleteCharAt(sb.length()-1);
results.add(sb.toString());
}
}
logger.debug("sendTaskResult-->" + Arrays.toString(results.toArray()));
//发送任务结果请求
socket.sendMessageByChar(CommonSocket.DATA_TYPE_OBJ_TASKRESULT);
socket.receiveMessageByChar();
//发送任务结果内容
socket.sendObject(results);
String result = socket.receiveMessageByChar();
if (CommonSocket.SUCCESS.equalsIgnoreCase(result)) {
// 移动上传成功的任务结果到指定日期目录
moveTaskResultToDateDir(fileArr);
}
}
/**
* 单个发送任务回传文件
* @param fileArr
* @throws Exception
*/
private void sendTaskReturn(File[] fileArr) throws Exception {
fileArr = FileUtil.sortASCByModify(fileArr); //修改日期升序排序
for(File file : fileArr){
if(!file.exists() || !file.isFile()){
continue;
}
String[] resultArr = FileWrUtil.cfgFileReader(file);
if (resultArr == null || resultArr.length <= 0) {
continue;
}
JSONObject jsonObject = JSONObject.fromObject(resultArr[0]);
ReturnFilePO rfPo = (ReturnFilePO) JSONObject.toBean(jsonObject, ReturnFilePO.class);
//--回传文件名和回传描述信息均为空时,则无回传文件
if(StringUtil.isEmpty(rfPo.getReturnFileName()) && StringUtil.isEmpty(rfPo.getResDesc())){
logger.warn("No return file, no return");
FileUtil.delDir(file);
continue;
}
//--回传文件名为空但回传描述信息不为空则进行步骤2发送任务结果、删除文件
/**
* 步骤1、回传文件
*/
StringBuffer sb = new StringBuffer();
if(rfPo.getResDesc()!=null){//取已有的结果描述信息
sb.append(rfPo.getResDesc());
}
//准备回传文件,回传文件名不为空即有回传的文件时,再回传
boolean success = false;
if(rfPo.getReturnFileName()!=null && rfPo.getReturnFileName().trim().length()>0){
//发送回传文件请求
socket.sendMessageByChar(CommonSocket.DATA_TYPE_FILE_TASKETURN);
socket.receiveMessageByChar();
//发送回传文件任务信息
socket.sendMessageByChar(TaskResultOper.getTaskResultMsg(rfPo
.getTaskId(), rfPo.getTaskType(), null, null, null, rfPo
.getStartTime(), rfPo.getEndTime(), rfPo.getIsLoop()));
socket.receiveMessageByChar();
//发送回传文件文件名称
socket.sendMessageByChar(rfPo.getReturnFileName());
socket.receiveMessageByChar();
//发送回传文件
socket.bpSendFile(Contants.localTaskReturnPath + File.separator + rfPo.getReturnFileName());
String result = socket.receiveMessageByChar();
success = true;
// sb.append("回传成功");
sb.append("i18n_client.ServerCollectData.transFile_n81i");
}else{
success = true;
}
/**
* 步骤2、判断文件是否回传完成
*/
if(success){
/**
* 步骤2-1、发送任务结果
*/
TaskResultOper.sendTaskResult(rfPo.getTaskId(), rfPo.getTaskType(),
rfPo.getState(), sb.toString(), "", rfPo.getStartTime(),
rfPo.getEndTime(), false, rfPo.getIsLoop());
/**
* 步骤2-2、移动上传成功的 保存回传文件信息的文件 和 回传文件 到指定日期目录
*/
moveTaskReturnToDateDir(file, rfPo.getReturnFileName());
}
}
}
/**
* 移动上传成功的任务结果到指定日期目录
* 完整文件到目录:.../done/result/yyyyMMdd
* 0大小文件到目录: .../error/result/yyyyMMdd
*/
public static void moveTaskResultToDateDir(File... fileArr){
if(fileArr==null || fileArr.length==0){
return;
}
for (File file : fileArr) {
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = "";
if (file.length() > 0) {
newDir = Contants.localTaskDonePath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
} else {
newDir = Contants.localTaskErrorPath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
}
// -- 文件移动
FileUtil.moveFile(file, newDir, true);
}
}
/**
* 移动上传成功的 保存回传文件信息的文件 和 回传文件 到指定日期目录
* 完整文件到目录:.../done/return/yyyyMMdd
* 0大小文件到目录: .../error/return/yyyyMMdd
*/
public static void moveTaskReturnToDateDir(File file, String returnFileName){
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = Contants.localTaskDonePath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
// -- 文件移动
FileUtil.moveFile(file, newDir, true);// 保留任务信息的临时文件.return
if(returnFileName!=null && !"".equals(returnFileName)){// 实际回传的文件
File curReturnFile = new File(Contants.localTaskReturnPath + File.separator + returnFileName);
FileUtil.moveFile(curReturnFile, newDir, true);
}
}
/**
* 移动上传成功的数据文件到指定日期目录
* 完整文件到目录:.../done/type_procIden/yyyyMMdd
* 0大小文件到目录: .../error/type_procIden/yyyyMMdd
*/
public static void moveDetecDataToDateDir(File... allFiles){
if(allFiles==null || allFiles.length==0){
return;
}
for (File file : allFiles) {
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = "";
if (file.length() > 0) {
newDir = Contants.localDataDonePath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
} else {
newDir = Contants.localDataErrorPath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
}
// -- 文件移动
FileUtil.moveFile(file, newDir, true);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,106 @@
package com.nis.nmsclient.thread.task;
import java.util.Date;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.model.CommandPO;
import com.nis.nmsclient.util.Utils;
public class LoopTaskThread extends Thread{
Logger logger = Logger.getLogger(LoopTaskThread.class);
private String threadName;
private long loopDelay;
private CommandPO command;
private long i = 0;
private Future<?> future = null;
private Thread thread;
private boolean isCancle;
public LoopTaskThread(String threadName, CommandPO command, long loopDelay, Future<?> singleFuture, Thread singleThread){
this.threadName = threadName;
this.loopDelay = loopDelay;
this.command = command;
this.future = singleFuture;
this.thread = singleThread;
isCancle = false;
}
public synchronized void cancle() {
isCancle = true;
}
public void run() {
i++;
// Thread.currentThread().setName(threadName + " 周期" + i);
Thread.currentThread().setName(threadName + " Cycle" + i);
long et = System.currentTimeMillis();// 本次开始时间,即上次执行结束时间
long st = et - (i * loopDelay * 60 * 1000);// 上次开始时间
try {
if (i == 1 && future != null
&& !future.isCancelled()
&& !future.isDone()) {
try {
future.get(1, TimeUnit.MICROSECONDS);
} catch (TimeoutException e) {
if(thread!=null && thread.isAlive()){
thread.stop();
logger.debug("LoopTaskThread run Timeout stop singleThread--" + thread.isAlive());
}
future.cancel(true);
}
}
if(!isCancle){
// 周期开始执行
future = Common.scheduled.schedule(new Runnable() {
public void run() {
thread = Thread.currentThread();
// Thread.currentThread().setName(threadName + " 周期" + i);
Thread.currentThread().setName(threadName + " Cycle" + i);
new AgentCommand(command).exec();
}
}, 0, TimeUnit.MILLISECONDS);
try {
future.get(loopDelay, TimeUnit.MINUTES);
} catch (TimeoutException e) {
if(thread!=null && thread.isAlive()){
thread.stop();
logger.debug("LoopTaskThread run Timeout stop thread--" + thread.isAlive());
}
future.cancel(true);
logger.debug("---------LoopTaskThread run timeout----------");
TaskResultOper.sendTaskResult(command.getExecId(),
command.getExecType(),
AgentCommand.RESULT_FAIL,
// "本周期任务执行超时", "", new Date(st),
// "this task execution timeout", "", new Date(st),
"i18n_client.LoopTaskThread.loopTaskOuttime_n81i", "", new Date(st),
new Date(et), false, command.getIsLoop());
}catch (InterruptedException e) {
if(thread!=null && thread.isAlive()){
thread.stop();
logger.debug("LoopTaskThread run Interrupted stop thread--" + thread.isAlive());
}
future.cancel(true);
logger.debug("---------LoopTaskThread run Interrupted----------");
}
}
if(thread!=null){
logger.debug("LoopTaskThread run thread state--" + thread.isAlive());
}
} catch (Exception e) {
logger.info(Utils.printExceptionStack(e));
}
}
}

View File

@@ -0,0 +1,397 @@
package com.nis.nmsclient.thread.task;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.model.CommandPO;
import com.nis.nmsclient.model.ParamFilePush;
import com.nis.nmsclient.model.ParamUpgrade;
import com.nis.nmsclient.model.Task4;
import com.nis.nmsclient.model.Task6;
import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.ProcessUtil;
import com.nis.nmsclient.util.Utils;
public class TaskReqHandle {
Logger logger = Logger.getLogger(TaskReqHandle.class);
private Thread singleThread;
/**
* 任务请求处理步骤1分析任务请求参数分不同任务类型处理
*/
public void taskHandle(String str){
Date execTime = new Date();
boolean flag = true;
int taskType = 0;
long taskId = 0;
String threadName = null;
CommandPO command = null;
try {
JSONObject jsonObj = JSONObject.fromObject(str);
if(str.contains("typeInfo")){
taskType = jsonObj.getInt("typeInfo");
}
if(str.contains("taskInfo")){
JSONObject jsonObj2 = jsonObj.getJSONObject("taskInfo");
Object obj = null;
/**
* 任务类型2 非流文本数据获取3 流文本数据获取4 命令执行5 shell注册, 6升级
*/
switch (taskType) {
case 4:
obj = JSONObject.toBean(jsonObj2,Task4.class);
Task4 task4 = (Task4) obj;
taskId = task4.getTaskId();
/**
* 命令类型1 Agent原生支持命令2可执行命令(2 脚本3 shell命令)
*/
if(task4.getCommandType() == 1){
// threadName = "原生命令 id:" + task4.getTaskId() + ">>" + task4.getCommandName();
threadName = "Native command ID:" + task4.getTaskId() + ">>" + task4.getCommandName();
}else if(task4.getCommandType() == 2){
// threadName = "可执行命令 id:" + task4.getTaskId();
threadName = "Executable command ID:" + task4.getTaskId();
}
command = new CommandPO();
command.setExecId(task4.getTaskId());
command.setExecType(task4.getTaskType());
command.setCommandName(task4.getCommandName());
command.setCommandParam(task4.getCommandParam());
command.setExecState(task4.getState());
command.setExecVersion(null);
command.setIsLoop(task4.getIsLoop());
handleTaskThread(task4.getTaskId(), task4.getStartTime(), task4.getEndTime(), task4
.getIsLoop(), task4.getLoopDelay(), command, threadName, task4.getMissionState());
break;
case 6:
obj = JSONObject.toBean(jsonObj2,Task6.class);
Task6 task6 = (Task6) obj;
taskId = task6.getTaskId();
// threadName = "升级 id:" + task6.getTaskId() + ">>" + task6.getCommandName();
threadName = "Upgrade ID:" + task6.getTaskId() + ">>" + task6.getCommandName();
command = new CommandPO();
command.setExecId(task6.getTaskId());
command.setExecType(task6.getTaskType());
command.setCommandName(task6.getCommandName());
command.setCommandParam(task6.getCommandParam());
command.setExecState(task6.getState());
command.setExecVersion(task6.getVersion());
command.setSrcPath(getUpgradeTaskPushPath(task6.getTaskId()));
handleTaskThread(task6.getTaskId(), task6.getUpgradeTime(), null, 0, 0, command, threadName, 0);
break;
default:
flag = false;
break;
}
}else{
flag = false;
}
} catch (Exception e) {
logger.error(Utils.printExceptionStack(e));
flag = false;
}
if(!flag){
TaskResultOper.sendTaskResult(taskId, taskType,
// AgentCommand.RESULT_FAIL, "发送内容格式不正确", "", execTime, new Date(), false, -1l);
// AgentCommand.RESULT_FAIL, "Incorrect content format", "", execTime, new Date(), false, -1l);
AgentCommand.RESULT_FAIL, "i18n_client.TaskReqHandle.sendInfoFormatErr_n81i", "", execTime, new Date(), false, -1l);
}
}
/**
* 文件推送处理
*/
public String filePush(CommonSocket socket, String taskParam, long taskId, boolean isUpgrade){
String msg = null;
StringBuffer sb = new StringBuffer();
File tempDir = null;
try {
tempDir = new File(Contants.localTempPath + File.separator
+ "filepush_" + taskId);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
// ------步骤1接收Md5校验的推送文件到临时目录
int flag = socket.bpReceiveFileByBathMd5(tempDir.getAbsolutePath());
if (flag == 0){// ------步骤2接收成功与参数比对
if(taskParam==null || "".equals(taskParam)){
msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "i18n_client.TaskReqHandle.pushFileParamIsNull_n81i";
// logger.info(msg);//i18nlog
return msg;
}
// ------步骤2-1解析参数
String[] params = taskParam.trim().split(AgentCommand.PARAM_SEPRATOR);
if (params != null && params.length >= 1) {
for (int i = 0; i < params.length; i++) {
//2012-4-28 任务参数中对路径的格式化将在界面上进行,原因此处会对转义字符的\也转换为/故replace("\\", "/")去掉
params[i] = params[i].trim().replaceAll("[\n\t\r]","");//.replace("\\", "/");//[\\s*\n\t\r]
logger.debug("filePush-->param: " + params[i]);
ParamFilePush fParam = null;
if(isUpgrade){
ParamUpgrade cfu = (ParamUpgrade) JSONObject.toBean(JSONObject
.fromObject(params[i]), ParamUpgrade.class);
fParam = new ParamFilePush();
fParam.setFileName(cfu.getFileName());
fParam.setUsername(cfu.getUsername());
fParam.setGroupName(cfu.getGroupName());
fParam.setParam1(cfu.getParam1());
}else{
fParam = (ParamFilePush) JSONObject.toBean(
JSONObject.fromObject(params[i]),
ParamFilePush.class);
}
if(fParam.getDestPath()==null || fParam.getDestPath().trim().length()<=0){
fParam.setDestPath(getUpgradeTaskPushPath(taskId));//设置默认推送目的地
logger.debug("filePush-->destPath: " + fParam.getDestPath());
}
if(fParam.getFileName()==null || fParam.getFileName().trim().length()<=0){
// msg = "推送文件名参数为空";
// msg = "File push parameters are empty";
msg = "i18n_client.TaskReqHandle.pushFileNameParamIsNull_n81i";
// logger.debug(msg);//i18nlog
break;
}
/*if(!ProcessUtil.checkUserPass(fParam.getUsername(), fParam.getParam1())){
msg = "[" + fParam.getFileName() + "]推送文件的用户名或密码不正确;";
logger.debug(msg);
break;
}*/
// 判断用户名是否正确
if(!ProcessUtil.checkUserOrGroupExist(fParam.getUsername(), fParam.getGroupName())){
// msg = "[" + fParam.getFileName() + "]推送文件的属主或属群不正确;";
// msg = "[" + fParam.getFileName() + "]The owner or group of the push file is incorrect;";
msg = "[" + fParam.getFileName() + "]i18n_client.TaskReqHandle.userGroupErr_n81i;";
// logger.debug(msg);//i18nlog
break;
}
// ------步骤2-2文件存在并与Md5值比较文件是否完整
File pushFile = new File(tempDir.getAbsolutePath()
+ File.separator + fParam.getFileName());
if (!pushFile.exists()){
// msg = "推送临时文件不存在,请检查推送文件名称与参数文件名("
// + fParam.getFileName() + ")是否一致";
// msg = "The push temporary file does not exist. Please check whether the push file name is consistent with the parameter file name("
// + fParam.getFileName() + ")";
msg = "i18n_client.TaskReqHandle.pushFileNotExists_n81i("
+ fParam.getFileName() + ")";
// logger.warn(msg + "--" + pushFile.getAbsolutePath());//i18nlog
break;
}
// ------步骤2-3判断推送目录是否存在不存在创建
File destFile = new File(fParam.getDestPath()
+ File.separator + fParam.getFileName());
if(!destFile.getParentFile().exists()){
destFile.getParentFile().mkdirs();
}
// ------步骤2-4判断是否直接覆盖
if (fParam.getIsCover() != null
&& "Y".equalsIgnoreCase(fParam.getIsCover())) {// 覆盖则直接Copy并赋权限与所有者
if (destFile.exists()) {
//destFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(destFile);
logger.debug("filePush delete file--" + destFile.getAbsolutePath());
//FileUtil.checkParentDirExist(destFile);
}
msg = copyAndSetPermission(pushFile
.getCanonicalPath(), destFile
.getCanonicalPath(), fParam.getUsername(),
fParam.getGroupName(), fParam
.getPermisson());
} else if (!destFile.exists()) {// 不覆盖则判断文件不存在的话再Copy并赋权限与所有者
msg = copyAndSetPermission(pushFile
.getCanonicalPath(), destFile
.getCanonicalPath(), fParam.getUsername(),
fParam.getGroupName(), fParam
.getPermisson());
}
if(msg==null || msg.length()<=0){
// sb.append("[" + fParam.getFileName() + "]成功推送到[" + fParam.getDestPath() + "];");
// sb.append("[" + fParam.getFileName() + "]successfully pushed to[" + fParam.getDestPath() + "];");
sb.append("[" + fParam.getFileName() + "]i18n_client.TaskReqHandle.successPush_n81i[" + fParam.getDestPath() + "];");
logger.debug("推送文件" + (i+1) + "成功---" + pushFile.getCanonicalPath());
}else{
// msg = msg+"[" + fParam.getFileName() + "]推送失败;";//文件推送失败的具体原因
msg = msg+"[" + fParam.getFileName() + "]i18n_client.TaskReqHandle.pushFail_n81i;";//文件推送失败的具体原因
logger.debug("推送文件" + (i+1) + "失败---" + pushFile.getCanonicalPath());
break;
}
}//for end
} else {
// msg = "文件推送参数不正确";
// msg = "File push parameter is incorrect";
msg = "i18n_client.TaskReqHandle.pushParamErr_n81i";
// logger.warn(msg + "<" + taskParam + ">");//i18nlog
}
//所有文件推送成功,删除临时接收文件目录
if(msg==null || msg.length()<=0){
if(tempDir!=null && tempDir.exists()){
try {
logger.debug("删除临时目录--" + tempDir.getAbsolutePath());
FileUtils.deleteDirectory(tempDir);
FileUtil.checkParentDirExist(tempDir);
} catch (IOException e) {
}
}
}
}else {
socket.close();
}
if(msg == null){
msg = Contants.COMMON_MSG_SUCCESS + Contants.COMMON_MSG_SEPRATOR + sb.toString();
}else {
sb.append(msg);
msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + sb.toString();
}
} catch (Exception e) {
logger.error("Receive push file exception" + Utils.printExceptionStack(e));
// msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "接收推送文件异常," + e.getMessage();
// msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "Received push file exception," + e.getMessage();
msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "i18n_client.TaskReqHandle.reciveFileErr_n81i," + e.getMessage();
return msg;
}finally{
if(tempDir!=null && tempDir.exists() && tempDir.listFiles().length==0){
try {
FileUtils.deleteDirectory(tempDir);
logger.debug("finally删除临时目录--" + tempDir.getAbsolutePath());
FileUtil.checkParentDirExist(tempDir);
} catch (IOException e) {
}
}
}
return msg;
}
/**
* 文件推送部分的拷备工作,由临时文件目录拷备到推送的最终目的地,并赋相应的权限组
*/
private String copyAndSetPermission(String source, String destFile, String user, String group, String permission) throws Exception{
String result = null;
if (source != null && destFile != null) {
// 根据操作系统确定获取进程ID的方式
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
FileUtils.copyFile(new File(source), new File(
destFile));//目标路径不存在自动创建
} else if (os.startsWith("Linux")) {
StringBuffer sb = new StringBuffer();
//source destFile都不能含有空格
source = source.replace(" ", "\\ ");
destFile = destFile.replace(" ", "\\ ");
sb.append("\\cp -f " + source + " " + destFile + ";");//2015-11-6 hyx: cp - f修改成\\cp -f (有时候如果不加\\会提示是否,就会有问题)
if (permission != null && !"".equals(permission.trim())) {
sb.append("chmod " + permission + " "
+ destFile + ";");
}
if (user != null && !"".equals(user.trim())) {
sb.append("chown " + user + " " + destFile + ";");
}
if (group != null && !"".equals(group.trim())) {
sb.append("chgrp " + group + " " + destFile);
}
result = ProcessUtil.execLinuxCmd(sb.toString());
} else {
throw new IOException("unknown operating system: " + os);
}
}else{
// result = "源文件或目标文件为空";
// result = "The source file or target file is empty";
result = "i18n_client.TaskReqHandle.sourceOrTargetIsNull_n81i";
}
return result;
}
/**
* 任务请求处理步骤2将分析包装好的任务统一判断处理并添加到线程中执行
*/
public void handleTaskThread(Long taskId, Long startTime, Long endTime,
long isLoop, long loopDelay, final CommandPO command,
final String threadName, long missionState) {
if(missionState == AgentCommand.MISSION_CANCEL_START){//如果任务状态为,撤消任务
logger.warn("The task is in the revocation, and the ID is not processed:" + taskId);
return;
}
if(Common.getTaskFuture(taskId)!=null){//当前任务已存在执行,则不执行该当前任务
logger.warn("The task already exists to execute the ID:" + taskId);
return;
}
// 设置任务结束时间,且当前时间已超过任务结束时间
if (endTime != null && endTime.longValue() > 0
&& endTime.longValue() <= System.currentTimeMillis()) {
logger.warn("The task has expired ID:" + taskId);
return;
}
long delay = 0;
if (startTime != null) {
delay = startTime - System.currentTimeMillis();
}
ScheduledFuture<?> taskFuture = null;
LoopTaskThread loopTaskThread = null;
if (isLoop == 0) {// 非周期任务
taskFuture = Common.scheduled.schedule(new Runnable() {
public void run() {
Thread.currentThread().setName(threadName);
new AgentCommand(command).exec();
}
}, delay, TimeUnit.MILLISECONDS);
} else {
Future<?> singleFuture = null;
if(delay <= 0){//开始时间之后接到任务,先执行一次,第二次按周期点执行
long now = System.currentTimeMillis();
long cnt = (now - startTime)/(loopDelay * 60 * 1000);
if((now - startTime)%(loopDelay * 60 * 1000)!=0){
delay = startTime + loopDelay *60 *1000 * (cnt + 1) - System.currentTimeMillis();
///仅执行一次的,并在周期执行代码中第一次执行判断单次执行完成与否,未完成结束掉
singleFuture = Common.scheduled.schedule(new Runnable() {
public void run() {
singleThread = Thread.currentThread();
// Thread.currentThread().setName(threadName + " 周期单次");
Thread.currentThread().setName(threadName + " Periodic Single Time");
new AgentCommand(command).exec();
}
}, 0, TimeUnit.MILLISECONDS);
}
}
loopTaskThread = new LoopTaskThread(threadName, command, loopDelay, singleFuture, singleThread);
taskFuture = Common.scheduled.scheduleAtFixedRate(loopTaskThread, delay, loopDelay * 60 * 1000, TimeUnit.MILLISECONDS);
// 周期任务,若设置结束时间,则添加取消线程
if (endTime != null && endTime.longValue() > 0) {
long endDelay = endTime.longValue() - System.currentTimeMillis();
if (endDelay > 0) {
Common.cancleTaskFuture(taskId, endDelay);
}
}// 取消线程结束
}
// 将正在执行的任务添加到全局变量,目的是避免重复执行任务
Common.putTaskFuture(taskId, taskFuture, loopTaskThread);
}
public static String getUpgradeTaskPushPath(long taskId){
return Contants.localUploadsPath + File.separator + taskId;
}
}

View File

@@ -0,0 +1,327 @@
package com.nis.nmsclient.thread.task;
import java.io.File;
import java.util.Date;
import java.util.concurrent.Future;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.common.VersionCfg;
import com.nis.nmsclient.config.DetecConfOper;
import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.thread.socket.SSLClient;
import com.nis.nmsclient.thread.socket.ServerCollectData;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.FileWrUtil;
import com.nis.nmsclient.util.Utils;
import com.nis.nmsclient.util.ZipUtil;
public class TaskResultOper {
static Logger logger = Logger.getLogger(DetecConfOper.class);
/**
* Agent在启动时发送所有之前发送失败的任务结果
*/
public static void initSendAllTaskResult(){
try {
//针对结果文件过多时打包上传未完成的文件继续上传
File taskDir = new File(Contants.localTaskPath);
if (!taskDir.exists()) {
return;
}
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
if (zipArr != null && zipArr.length > 0) {
for (File file : zipArr) {
if (!file.getName().startsWith(CommonSocket.BP_TYPE_TASK_RESULT)) {
continue;
}
Future<?> future = Common.service.submit(new SSLClient(Thread
.currentThread().getName(),
CommonSocket.REQ_BP_UPLOAD_FIFE, new String[] {
CommonSocket.BP_TYPE_TASK_RESULT, file.getAbsolutePath() }));
future.get();
}
}
//-------------------------------------
File resultDir = new File(getTaskResultPath());
if(!resultDir.exists()){
return;
}
File[] fileArr = FileUtil.getFilesEndWith(resultDir, Contants.TASK_RESULT_FILE_SUFFIX);
if(fileArr == null || fileArr.length == 0){
/*
* 当任务正常执行完成或者取消后则从全局变量移除
*/
Common.removeCancelAndDoneTaskFuture();
return;
}
//--- 将所有任务结果文件一起打包,发送
if(fileArr.length > Contants.COMMON_ZIP_MIN_SIZE){
//与Server通信
Future<?> serFuture = Common.service.submit(new SSLClient(
Thread.currentThread().getName(),
CommonSocket.REQ_HAND_SHAKE, null));
if (!Contants.isSucessByResult((String) serFuture.get())) {
return;
}
int zipCnt = fileArr.length/Contants.COMMON_ZIP_MAX_SIZE;
if(zipCnt>0){//2013-5-6 未上传的结果文件太多时,将结果文件压缩为多个文件
for(int i=0; i<fileArr.length/Contants.COMMON_ZIP_MAX_SIZE; i++){
// 组织每次压缩的文件数组
File[] resultFiles = new File[Contants.COMMON_ZIP_MAX_SIZE];
int start = i * Contants.COMMON_ZIP_MAX_SIZE;
int end = (i + 1) * Contants.COMMON_ZIP_MAX_SIZE;
for(int j=start,k=0; j<end; j++,k++){
resultFiles[k] = fileArr[j];
}
// 压缩并发送任务结果
String compressFileStr = Contants.localTaskPath
+ File.separator
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RESULT, null, true)
+ ".zip";
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现现修改将任务结果和回传文件压缩时不删除文件而是将其移动到相应的日期目录
ZipUtil.zipWithMoveFile(resultDir.listFiles(), compressFileStr, true);
//发送
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
.getName(), CommonSocket.REQ_BP_UPLOAD_FIFE,
new String[] { CommonSocket.BP_TYPE_TASK_RESULT, compressFileStr }));
future.get();
}
}else{
// 压缩并发送任务结果
String compressFileStr = Contants.localTaskPath
+ File.separator
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RESULT, null, true)
+ ".zip";
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现现修改将任务结果和回传文件压缩时不删除文件而是将其移动到相应的日期目录
ZipUtil.zipWithMoveFile(resultDir.listFiles(), compressFileStr, true);
//发送
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
.getName(), CommonSocket.REQ_BP_UPLOAD_FIFE,
new String[] { CommonSocket.BP_TYPE_TASK_RESULT, compressFileStr }));
future.get();
}
}else{//-- 按正常的一个结果一个结果的发送
fileArr = FileUtil.sortASCByFileName(fileArr); //按文件名升序排列,任务结果文件名都有时间后缀(ms)
StringBuffer sb = new StringBuffer();
for(File file : fileArr){
sb.delete(0, sb.length());
if(!file.exists() || !file.isFile()){
continue;
}
String[] resultArr = FileWrUtil.cfgFileReader(file);
if(resultArr!=null && resultArr.length>0){
for(String res : resultArr){
sb.append(res + ";");
}
sb.deleteCharAt(sb.length()-1);
Future<?> future = Common.service.submit(new SSLClient(
// "上传任务结果",
"Upload Task Results",
CommonSocket.REQ_TASK_RESULT, sb.toString()));
String msg = (String) future.get();
if (Contants.isSucessByResult(msg)) {
// 移动上传成功的任务结果到指定日期目录
ServerCollectData.moveTaskResultToDateDir(file);
}
}else{
// 移动上传成功的任务结果到指定日期目录
ServerCollectData.moveTaskResultToDateDir(file);
}
}
}
} catch (Exception e) {
logger.error("Upload task result exception" + Utils.printExceptionStack(e));
}
}
/**
* 处理Agent自动升级的最终结果文件即升级后是否启动成功
* Agent升级的临时结果文件初始写入状态为升级失败
* 1、当启动失败后将其临时文件后缀改为任务结果文件的后缀;
* 2、当启动成功后先修改文件第一行的后两项内容为执行状态与描述为成功再改其后缀。
*
* @param isSuccess
*/
public static void handerAgentUpgradeResult(boolean isSuccess){
try {
File dir = new File(getTaskResultPath());
if(!dir.exists()){
return;
}
File[] fileArr = FileUtil.getFilesEndWith(dir, Contants.TASK_RESULT_AGENTTMPFILE_SUFFIX);
fileArr = FileUtil.sortASCByFileName(fileArr); //按文件名升序排列,任务结果文件名都有时间后缀(ms)
if (fileArr != null && fileArr.length > 0) {
for(int i=0; i<fileArr.length; i++){
if (isSuccess && i == fileArr.length-1) {// 如果启动成功处理其结果内容
try {// 本层try是为了防止其中一个结果文件不正常影响其他结果文件不能正常修改
String[] msgs = FileWrUtil.cfgFileReader(fileArr[i]);
if (msgs != null && msgs.length > 0) {
int descIndex = msgs[0]
.lastIndexOf(Contants.COMMON_MSG_SEPRATOR);
int stateIndex = msgs[0].substring(0, descIndex)
.lastIndexOf(Contants.COMMON_MSG_SEPRATOR);
msgs[0] = msgs[0].substring(0, stateIndex)
+ Contants.COMMON_MSG_SEPRATOR + AgentCommand.RESULT_OK
// + Contants.COMMON_MSG_SEPRATOR + "重启成功";
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.TaskResultOper.restart_n81i";
FileWrUtil.cfgFilePrinter(fileArr[i], Contants.charset, msgs);
int taskIdIndex = msgs[0].indexOf(Contants.COMMON_MSG_SEPRATOR);
//处理写入当前更新的版本信息
String taskId = msgs[0].substring(0, taskIdIndex);
Long curVer = Long.parseLong(VersionCfg
.getValue(VersionCfg.NAGENT_VERSION));
if (curVer < Long.parseLong(taskId.trim())) {
VersionCfg.setValue(VersionCfg.NAGENT_VERSION, taskId);
logger.info("NC更新为版本" + taskId);
}
}
} catch (Exception e) {
logger.error("Handling the exception of the NC upgrade result file" + Utils.printExceptionStack(e));
}
}
String fileStr = fileArr[i].getAbsolutePath();
int index = fileStr.lastIndexOf(Contants.TASK_RESULT_AGENTTMPFILE_SUFFIX);
fileStr = fileStr.substring(0, index);
fileArr[i].renameTo(new File(fileStr + Contants.TASK_RESULT_FILE_SUFFIX));
}
}
} catch (Exception e) {
logger.error("Handling the exception of the NC upgrade result file" + Utils.printExceptionStack(e));
}
}
/**
* 发送任务执行结果:若发送失败写入文件
*/
/*private static void sendTaskResult(long taskId, long taskType, String msg) {
try{
Future<?> future = Common.service.submit(new SSLClient(
Thread.currentThread().getName(),
CommonSocket.REQ_TASK_RESULT, msg));
String result = (String) future.get();
if (!Contants.isSucessByResult(result)) {//失败
File file = new File(getTaskResultFile(taskType, taskId));
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileWrUtil.cfgFilePrinter(file, Contants.charset, new String[]{msg});
}
}catch (Exception e) {
logger.error("发送任务结果异常:" + Utils.printExceptionStack(e));
}
}*/
/**
* 发送任务执行结果判断是否是Server升级若是直接写入文件若不是则发送结果
* @param taskId 任务ID
* @param taskType 任务类型
* @param resultState 执行结果状态
* @param resultDesc 执行结果描述
* @param resultConfig
* @param startTime 开始执行时间
* @param endTime 执行结束时间
* @param isServer 是否是Server升级
* @param isLoop 是否是循环任务
*/
/*public static void sendTaskResult(long taskId, long taskType,
long resultState, String resultDesc, String resultConfig,
Date startTime, Date endTime, boolean isServer, long isLoop) {
try {
String msg = getTaskResultMsg(taskId, taskType, resultState,
resultDesc, resultConfig, startTime, endTime, isLoop);
if(isServer){//如果是Server升级直接写入文件
File file = new File(getTaskResultFile(taskType, taskId));
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileWrUtil.cfgFilePrinter(file, Contants.charset, new String[]{msg});
}else{//反之,发送任务结果
sendTaskResult(taskId, taskType, msg);
}
} catch (Exception e) {
logger.error("发送任务结果异常:" + Utils.printExceptionStack(e));
}
}*/
/**
* 发送任务结果: 将结果保存到文件等待DC主动来收集
* @date Jan 15, 2013
* @author zhenzhen
* @version
*/
public static void sendTaskResult(long taskId, long taskType,
long resultState, String resultDesc, String resultConfig,
Date startTime, Date endTime, boolean isServer, long isLoop) {
try {
// 2013-4-9 为了防止多步操作时结果文件名重复所以暂停50ms为了使时间后缀不一样
if (resultState > AgentCommand.RESULT_SEND_OK) {
try {Thread.sleep(50);} catch (Exception ignored) { }
}
String msg = getTaskResultMsg(taskId, taskType, resultState,
resultDesc, resultConfig, startTime, endTime, isLoop);
File file = new File(getTaskResultFile(taskType, taskId));
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileWrUtil.cfgFilePrinter(file, Contants.charset, new String[]{msg});
} catch (Exception e) {
logger.error("Send task result exception" + Utils.printExceptionStack(e));
}
}
/**
* 按指定的分隔符和顺序拼写任务执行结果信息
*/
public static String getTaskResultMsg(long taskId, long taskType,
Long resultState, String resultDesc, String resultConfig,
Date startTime, Date endTime, long isLoop) {
StringBuffer sb = new StringBuffer();
sb.append(taskId);
sb.append(Contants.COMMON_MSG_SEPRATOR);
sb.append(taskType);
sb.append(Contants.COMMON_MSG_SEPRATOR);
sb.append(Contants.AGENT_HOST_UUID);
sb.append(Contants.COMMON_MSG_SEPRATOR);
sb.append(isLoop);
sb.append(Contants.COMMON_MSG_SEPRATOR);
sb.append(startTime.getTime());
sb.append(Contants.COMMON_MSG_SEPRATOR);
sb.append(endTime.getTime());
sb.append(Contants.COMMON_MSG_SEPRATOR);
sb.append(resultConfig);
sb.append(Contants.COMMON_MSG_SEPRATOR);
sb.append(resultState);
sb.append(Contants.COMMON_MSG_SEPRATOR);
sb.append(resultDesc);
return sb.toString();
}
public static String getTaskResultFile(long taskType, long taskId){
return getTaskResultFileNoSuffix(taskType, taskId) + "_" + System.currentTimeMillis()
+ Contants.TASK_RESULT_FILE_SUFFIX;
}
public static String getAgentUpgradeResultTempFile(long taskType, long taskId){
return getTaskResultFileNoSuffix(taskType, taskId)
+ Contants.TASK_RESULT_AGENTTMPFILE_SUFFIX;
}
private static String getTaskResultFileNoSuffix(long taskType, long taskId){
return getTaskResultPath() + File.separator + "tasktype" + taskType + "_" + taskId;
}
public static String getTaskResultPath(){
return Contants.localTaskResultPath;
}
}

View File

@@ -0,0 +1,351 @@
package com.nis.nmsclient.thread.task;
import java.io.File;
import java.io.IOException;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.model.ReturnFilePO;
import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.util.CompressFileMgr;
import com.nis.nmsclient.util.FileWrUtil;
import com.nis.nmsclient.util.Utils;
public class TaskReturnHandle {
Logger logger = Logger.getLogger(TaskReturnHandle.class);
/*public void sendAllTaskReturnFile(){
try {
//针对回传文件过多时打包上传未完成的文件继续上传
File taskDir = new File(Contants.localTaskPath);
if (!taskDir.exists()) {
return;
}
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
if (zipArr != null && zipArr.length > 0) {
for (File file : zipArr) {
if (!file.getName().startsWith(CommonSocket.BP_TYPE_TASK_RETURN)) {
continue;
}
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
.getName(), CommonSocket.REQ_BP_UPLOAD_FIFE,
new String[] { CommonSocket.BP_TYPE_TASK_RETURN,
file.getAbsolutePath() }));
future.get();
}
}
//----------------------------------
File returnDir = new File(Contants.localTaskReturnPath);
if(!returnDir.exists()){
return;
}
File[] fileArr = FileUtil.getFilesEndWith(returnDir, Contants.TASK_RETURN_FILE_SUFFIX);
if(fileArr == null || fileArr.length == 0){
return;
}
//--- 将所有任务的回传文件及回传信息保存文件一起打包,发送
if(fileArr.length > Contants.COMMON_MAX_RETURN_CNT){
//与Server通信
Future<?> serFuture = Common.service.submit(new SSLClient(
Thread.currentThread().getName(),
CommonSocket.REQ_HAND_SHAKE, null));
if (!Contants.isSucessByResult((String) serFuture.get())) {
return;
}
//压缩并删除原文件
String compressFileStr = Contants.localTaskPath
+ File.separator
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RETURN,
null)
+ ".zip";
ZipUtil.zipWithDelFile(returnDir.listFiles(), compressFileStr, false);
//发送
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
.getName(), CommonSocket.REQ_BP_UPLOAD_FIFE,
new String[] { CommonSocket.BP_TYPE_TASK_RETURN,
compressFileStr }));
future.get();
}else{//-- 按正常的一个任务一个任务的回传
fileArr = FileUtil.sortASCByModify(fileArr); //修改日期升序排序
for(File file : fileArr){
if(!file.exists() || !file.isFile()){
continue;
}
String[] resultArr = FileWrUtil.cfgFileReader(file);
if (resultArr != null && resultArr.length > 0) {
JSONObject jsonObject = JSONObject.fromObject(resultArr[0]);
ReturnFilePO rfPo = (ReturnFilePO) JSONObject.toBean(jsonObject, ReturnFilePO.class);
sendTaskReturnFile(rfPo);
}else{
//file.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(file);
logger.debug("TaskReturnHadle.sendAllTaskReturnFile()()任务回传临时文件(记录任务结果信息)删除:"+file.getAbsolutePath());
}
}
}
} catch (Exception e) {
logger.error(Utils.printExceptionStack(e));
}
}*/
public static void startTaskReturnFileThread(final ReturnFilePO rfPo, final List<String> filePaths) {
Common.service.execute(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("Backpass File Processing ID:" + rfPo.getTaskId());
TaskReturnHandle tReturnHandle = new TaskReturnHandle();
ReturnFilePO rfPo2 = tReturnHandle.taskReturnFileHandle(rfPo, filePaths);
//tReturnHandle.sendTaskReturnFile(rfPo2);
}
});
}
/**
* 回传文件的统一处理:将回传文件移动到统一回传位置,处理后一个任务只有一个回传文件,修改回传文件名,将任务回传信息写入文件
*/
public ReturnFilePO taskReturnFileHandle(ReturnFilePO rfPo, List<String> filePaths){
if(rfPo==null || filePaths==null || filePaths.size()<=0){
logger.warn("No return file, no return");
return null;
}
File tempDir = null;
try {
File file = new File(getTaskReturnFile(rfPo.getTaskType(),
rfPo.getTaskId(), rfPo.getIsLoop()));
if (file.exists() && file.length()>0) {// 如果存在,说明之前已经整理过回传文件了,本次不做操作
return null;
}else if(!file.getParentFile().exists()){// 创建回传文件目录
file.getParentFile().mkdirs();
}
StringBuffer sb = new StringBuffer();
tempDir = new File(Contants.localTempPath + File.separator
+ "return_" + rfPo.getTaskId());
if(!tempDir.exists()){
tempDir.mkdirs();
}
/**
* 处理回传文件1、过滤不存在或文件路径为空的文件 2、对文件重命名移动到临时目录
*/
for(String returnPath: filePaths){
if (StringUtils.isEmpty(returnPath)) {
// sb.append("回传“" + returnPath + "”失败,回传文件路径值为空;");
// sb.append("Backpass“" + returnPath + "” failed, the return file path value is empty;");
sb.append("i18n_client.TaskReturnHandle.backpass_n81i“" + returnPath + "” i18n_client.TaskReturnHandle.backFail1_n81i;");
continue;
}
File returnFile = new File(returnPath);
if (!returnFile.exists()) {
// sb.append("Backpass“" + returnPath + "” failed, return file does not exist;");
sb.append("i18n_client.TaskReturnHandle.backpass_n81i“" + returnPath + "” i18n_client.TaskReturnHandle.backFail2_n81i;");
continue;
}
//回传文件取别名:原文件名_T任务ID_时间戳
String aliasName = CommonSocket.addTimeTagForFileName(
returnFile.getName(), "T" + rfPo.getTaskId(), returnFile.isFile());
//---- 移动回传文件到临时目录,并重命名为:原文件名_T任务ID_时间戳
File returnTmpFile = new File(tempDir.getCanonicalPath()
+ File.separator
+ aliasName);
if (returnFile.isDirectory()) {
FileUtils.copyDirectory(returnFile, returnTmpFile);
} else {
FileUtils.copyFile(returnFile, returnTmpFile);
}
}
/**
* 将回传的所有文件压缩放到统一回传位置
*/
String returnFileName = null;
//---- 若回传文件的个数等于1则把临时目录下的一个文件按文件夹的话压缩到统一回传位置文件的话直接移动到统一回传位置
File[] files = tempDir.listFiles();
if(files.length==1){
returnFileName = files[0].getName();
File destFile = new File(Contants.localTaskReturnPath + File.separator + files[0].getName());
if (files[0].isDirectory()) {
String compressFileStr = destFile.getAbsolutePath() + CompressFileMgr.getCompressSuffixByOs(false);
returnFileName = new File(compressFileStr).getName();
new CompressFileMgr().compressFile(files[0]
.getAbsolutePath(), compressFileStr, null, false);
} else {
FileUtils.copyFile(files[0], destFile, false);// 复制并修改文件日期
}
}else if(files.length > 1){//---- 若回传文件的个数大于1则把临时目录整个压缩到统一回传位置
String compressFileStr = Contants.localTaskReturnPath + File.separator + tempDir.getName() + CompressFileMgr.getCompressSuffixByOs(false);
returnFileName = new File(compressFileStr).getName();
new CompressFileMgr().compressFile(tempDir
.getAbsolutePath(), compressFileStr, null, false);
}
rfPo.setReturnFileName(returnFileName);
rfPo.setResDesc(sb.toString());
/**
* 将信息写入文件
*/
if (!file.exists()) {
String[] values = new String[] { JSONObject.fromObject(
rfPo).toString() };
FileWrUtil.cfgFilePrinter(file, Contants.charset,
values);
}
return rfPo;
} catch (Exception e) {
logger.error("Handling backpass file exceptions" + Utils.printExceptionStack(e));
return null;
} finally {
if(tempDir!=null && tempDir.exists()){
try {
logger.debug("删除临时目录--" + tempDir.getAbsolutePath());
FileUtils.deleteDirectory(tempDir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 回传文件先将信息写入临时文件再判断与Server端通信是否成功
* 1、通信成功回传文件发送最终结果删除临时文件
* 2、通信失败保留临时文件直接返回
* @param rfPo 回传文件实体类
*/
/*public void sendTaskReturnFile(ReturnFilePO rfPo) {
if(rfPo==null){
logger.debug("回传文件实体对象为空, 不用回传");
return;
}
try {
File file = new File(getTaskReturnFile(rfPo.getTaskType(),
rfPo.getTaskId()));
//--回传文件名和回传描述信息均为空时,则无回传文件
if(StringUtil.isEmpty(rfPo.getReturnFileName()) && StringUtil.isEmpty(rfPo.getResDesc())){
logger.warn("无回传文件, 不用回传");
if(file.exists()){
//file.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(file);
logger.debug("TaskReturnHandle.sendTaskReturnFile()成功发送任务回传文件后删除:"+file.getAbsolutePath());
}
return;
}
//--回传文件名为空但回传描述信息不为空则进行步骤4发送任务结果、删除文件
*//**
* 步骤1、将信息写入文件
*//*
if (!file.exists()) {
String[] values = new String[] { JSONObject.fromObject(
rfPo).toString() };
FileWrUtil.cfgFilePrinter(file, Contants.charset,
values);
}
*//**
* 步骤2、与Server通信
*//*
Future<?> serFuture = Common.service.submit(new SSLClient(
Thread.currentThread().getName(),
CommonSocket.REQ_HAND_SHAKE, null));
if (!Contants.isSucessByResult((String) serFuture.get())) {
return;
}
*//**
* 步骤3、回传文件
*//*
StringBuffer sb = new StringBuffer();
if(rfPo.getResDesc()!=null){//取已有的结果描述信息
sb.append(rfPo.getResDesc());
}
//准备回传文件,回传文件名不为空即有回传的文件时,再回传
boolean success = false;
if(rfPo.getReturnFileName()!=null && rfPo.getReturnFileName().trim().length()>0){
for(int i=0; i<Contants.max_times; i++){//回传失败,尝试几次
Future<?> future = Common.service.submit(new SSLClient(
Thread.currentThread().getName(),
CommonSocket.REQ_TASK_RETURNFILE, rfPo));
String msg = (String) future.get();
success = Contants.isSucessByResult(msg);
if(success){
sb.append("回传成功;");
break;
}
try {// 如果更新失败,让当前线程暂停几秒,再重试
Thread.sleep(1000 * Contants.max_delay_seconds);
} catch (InterruptedException e) {
logger.error(Utils.printExceptionStack(e));
continue;
}
}
}else{
success = true;
}
*//**
* 步骤4、判断文件是否回传完成
*//*
if(success){
*//**
* 步骤4-1、发送任务结果
*//*
TaskResultOper.sendTaskResult(rfPo.getTaskId(), rfPo.getTaskType(),
rfPo.getState(), sb.toString(), "", rfPo.getStartTime(),
rfPo.getEndTime(), false, rfPo.getIsLoop());
*//**
* 步骤4-2、删除保存回传文件信息的文件
*//*
if(file.exists()){
//file.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(file);
logger.debug("TaskReturnHandle.sendTaskReturnFile()删除保存回传文件信息的文件:"+file.getAbsolutePath());
}
*//**
* 步骤4-3、正常回传完成删除文件
*//*
File curReturnFile = new File(Contants.localTaskReturnPath + File.separator + rfPo.getReturnFileName());
if(curReturnFile.exists()){
//curReturnFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(curReturnFile);
logger.debug("TaskReturnHandle.sendTaskReturnFile()正常回传完成,删除文件:"+curReturnFile.getAbsolutePath());
}
}
} catch (Exception e) {
logger.error(Utils.printExceptionStack(e));
}
}*/
/**
* 取任务回传临时记录任务信息文件的文件名: 若是周期任务加时间后缀taskreturn_taskId_taskType_time.return若未周期任务, taskreturn_taskId_taskType.return
*/
public static String getTaskReturnFile(long taskType, long taskId, long isLoop) {
//isLoop 是否周期任务: 0 非周期, 1 周期
if(isLoop==0){
return Contants.localTaskReturnPath + File.separator + "tasktype"
+ taskType + "_" + taskId
+ Contants.TASK_RETURN_FILE_SUFFIX;
}else{
return Contants.localTaskReturnPath + File.separator + "tasktype"
+ taskType + "_" + taskId + "_"
+ System.currentTimeMillis()
+ Contants.TASK_RETURN_FILE_SUFFIX;
}
}
}

View File

@@ -0,0 +1,197 @@
package com.nis.nmsclient.thread.timer;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.Utils;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class DelLocalFileThread implements Runnable {
Logger logger = Logger.getLogger(DelLocalFileThread.class);
private String name;
public DelLocalFileThread(String name){
this.name = name;
}
@Override
public void run() {
Thread.currentThread().setName(name);
delLogFile();
delDataFile();
delTempFile();
delUpgradeFile();
}
/**
* 删除日志文件
*/
public void delLogFile() {
long curDate = System.currentTimeMillis();
// 删除指定天数之前的日志文件
File logs = new File(Contants.localLogsPath);
if (!logs.exists() || !logs.isDirectory()) {
return;
}
for (File f : logs.listFiles()) {
long diff = DateUtil.getDaysFromBeginToEnd(f.lastModified(),
curDate);
if (f.isFile() && diff > Contants.COMMON_DEL_LOG_DAYS) {
//f.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(f);
logger.info("删除日志文件" + f.getAbsolutePath());
//FileUtil.checkParentDirExist(f);
}
}
}
/**
* 清理数据文件(包括监测数据和任务文件)
*/
public void delDataFile(){
long curDateMillis = System.currentTimeMillis();
// == 1、删除指定分钟之前的所有监测文件
File doneFile = new File(Contants.localDataDonePath);
if (doneFile.exists()) {
//---删除打包上传的数据
long timeMillis = curDateMillis - Contants.COMMON_DEL_DATA_HOURS
* 60 * 60 * 1000;
File[] files = FileUtil.getFilesEndWithBeforeMillis(doneFile, ".zip",
timeMillis);
if (files != null && files.length > 0) {
for (File f : files) {
FileUtil.delDir(f);
logger.debug("delDataFile 删除打包文件--" + f.getAbsolutePath());
}
}
//---删除正常上传的数据文件
File[] doneDirs = FileUtil.getDirectoryArray(doneFile);
delDataFileCallBack(curDateMillis, doneDirs, Contants.COMMON_DEL_DATA_HOURS);
}
File errorFile = new File(Contants.localDataErrorPath);
if (errorFile.exists()) {
File[] errorDirs = FileUtil.getDirectoryArray(errorFile);
delDataFileCallBack(curDateMillis, errorDirs, Contants.COMMON_DEL_DATA_HOURS);
}
// == 2、删除指定分钟之前的所有任务相关的文件
doneFile = new File(Contants.localTaskDonePath);
if (doneFile.exists()) {
//---删除打包上传的任务结果和回传文件
long timeMillis = curDateMillis - Contants.COMMON_DEL_TASK_HOURS
* 60 * 60 * 1000;
File[] files = FileUtil.getFilesEndWithBeforeMillis(doneFile, ".zip",
timeMillis);
if (files != null && files.length > 0) {
for (File f : files) {
FileUtil.delDir(f);
logger.debug("delDataFile 删除打包文件2--" + f.getAbsolutePath());
}
}
//---删除正常上传的任务结果和回传文件
File[] doneDirs = FileUtil.getDirectoryArray(doneFile);
delDataFileCallBack(curDateMillis, doneDirs, Contants.COMMON_DEL_TASK_HOURS);
}
errorFile = new File(Contants.localTaskErrorPath);
if (errorFile.exists()) {
File[] errorDirs = FileUtil.getDirectoryArray(errorFile);
delDataFileCallBack(curDateMillis, errorDirs, Contants.COMMON_DEL_TASK_HOURS);
}
}
/**
* 清理数据文件的递归回调函数
*/
public void delDataFileCallBack(long curDateMillis, File[] dirs, int delHours) {
if (dirs == null || dirs.length == 0) {
return;
}
for (File dir : dirs) {
long timeMillis = curDateMillis - delHours
* 60 * 60 * 1000;
File[] sonDirs = FileUtil.getDirsBeforeDateName(dir, DateUtil
.getStingDate(DateUtil.YYYYMMDD, new Date(timeMillis)));
delDataFileCallBack(curDateMillis, sonDirs, delHours);
File[] files = FileUtil.getFilesBeforeMillis(dir,timeMillis);
if (files != null && files.length > 0) {
for (File f : files) {
FileUtil.delDir(f);
logger.debug("删除文件--" + f.getAbsolutePath());
}
}
if (files == null && files.length > 0) {
logger.info("删除文件夹" + dir.getAbsolutePath() + "下的文件:文件"
+ files.length + "");
}
if (dir.exists() && dir.listFiles().length <= 0) {
FileUtil.delDir(dir);
logger.info("删除文件夹" + dir.getAbsolutePath());
}
}
}
/**
* 清理临时目录的文件
*/
public void delTempFile() {
try {
long curDate = System.currentTimeMillis();
// 删除指定日期之前的临时文件
File temp = new File(Contants.localTempPath);
if (!temp.exists() || !temp.isDirectory()) {
return;
}
for (File f : temp.listFiles()) {
long diff = DateUtil.getDaysFromBeginToEnd(f.lastModified(),
curDate);
if (f.exists() && diff > Contants.COMMON_DEL_TEMP_DAYS) {
//使用删除文件公共方法
FileUtil.delDir(f);
logger.info("删除临时文件或文件夹" + f.getAbsolutePath());
}
}
} catch (Exception e) {
logger.error(Utils.printExceptionStack(e));
}
}
/**
* 清理UPLOAD目录下的文件
*/
public void delUpgradeFile() {
try {
long curDate = System.currentTimeMillis();
// 删除指定日期之前的升级文件
File uploads = new File(Contants.localUploadsPath);
if (!uploads.exists() || !uploads.isDirectory()) {
return;
}
for (File f : uploads.listFiles()) {
long diff = DateUtil.getDaysFromBeginToEnd(f.lastModified(),
curDate);
if (f.exists() && diff > Contants.COMMON_DEL_UPGRADEFILE_DAYS) {
//使用删除文件公共方法
FileUtil.delDir(f);
logger.info("删除升级文件或文件夹" + f.getAbsolutePath());
}
}
} catch (Exception e) {
logger.error(Utils.printExceptionStack(e));
}
}
}

View File

@@ -0,0 +1,41 @@
package com.nis.nmsclient.thread.upload;
/**
* 消息类型
* @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;
}
}

View File

@@ -0,0 +1,589 @@
package com.nis.nmsclient.thread.upload;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.common.StopWatch;
import com.nis.nmsclient.model.ReturnFilePO;
import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.thread.socket.SSLCertOper;
import com.nis.nmsclient.thread.task.TaskResultOper;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.FileWrUtil;
import com.nis.nmsclient.util.StringUtil;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import net.sf.json.JSONObject;
/**
* 数据发送线程
* 1、暂时只发送监测数据csv或zip
* 2、间隔 10 秒启动一次,同时具备心跳的功能
* 3、实现断线重连
* @author dell
*
*/
public class DataSendThread implements Runnable{
public static final Logger logger = Logger.getLogger(DataSendThread.class);
private Socket socket;
private OutputStream os;
private InputStream is;
private String host;
private int port;
public DataSendThread(String host,int port) {
this.host = host;
this.port = port;
}
public void init() throws Exception {
SSLContext ctx = SSLCertOper.getSSLContext();
SSLSocketFactory ssf = ctx.getSocketFactory();
socket = (SSLSocket) ssf.createSocket(host,port);
logger.debug("create socket success.");
//2014-1-23 hyx 如果建立socket成功但是startHandshake握手失败且未设置超时时间时则会一直阻塞
socket.setSoTimeout(1000 * 60 * Contants.SOCKET_TIMEOUT_MINUTES);
((SSLSocket) socket).startHandshake();
logger.debug("handshake success.");
os = socket.getOutputStream();
is = socket.getInputStream();
}
@Override
public void run() {
Thread.currentThread().setName("DataSendThread");
logger.debug("开始");
StopWatch sw = new StopWatch();
sw.start();
try {
//重连
if(socket == null || socket.isClosed()|| !socket.isConnected()|| os == null || is ==null ){
init();
}
//发送心跳
sendHeartBeat();
//发送监测数据
sendDetectData();
//发送任务回传文件
sendTaskReturnFile();
//发送任务结果
sendTaskResult();
} catch (SSLException | SocketException e){
logger.error("SocketException|SSLException",e);
try {
init();
} catch (Exception e1) { }
} catch (Exception e) {
logger.error("",e);
}
sw.end();
logger.debug("耗时:"+sw.toString(sw.total()));
logger.debug("结束");
}
/**
* 发送监测数据
*/
private void sendDetectData(){
StopWatch sw = new StopWatch();
try {
File dataDir = new File(Contants.localDataFilePath);
if (!dataDir.exists()) {
logger.warn("数据目录 : " + dataDir.getAbsolutePath() + " 不存在!!!");
} else{
long total = 0;
List<File> allFiles = new ArrayList<File>();
File[] dataDirs = FileUtil.getDirectoryArray(dataDir);
// ---- 数据处理
total = handleNullDataFile(allFiles, dataDirs);
logger.info("本次收集监测数据文件总数:" + total + ", 正常数据:" + allFiles.size() + ", 空数据:" + (total - allFiles.size()));
total = allFiles.size();// 正常的要上传的数据个数
if(total > 0){
for (int i = 0; i < total; i++) {
File tem = allFiles.get(i);
this.sendData(tem, BusinessType.DetectData);
String res = this.readLine();
if(CommonSocket.SUCCESS.equalsIgnoreCase(res)){//发送成功
this.moveDetecDataToDateDir(tem);
}
}
}
}
} catch (Exception e) {
logger.error("",e);
}
sw.end();
logger.debug("耗时:"+sw.toString(sw.total()));
logger.debug("结束");
}
/**
* 发送任务结果
*/
private void sendTaskResult() {
StopWatch sw = new StopWatch();
try {
logger.debug("传送任务结果开始 ~~~~~~~");
long startTime = System.currentTimeMillis();
// == 1、针对结果文件过多时打包上传未完成的文件继续上传
File taskDir = new File(Contants.localTaskPath);
if (!taskDir.exists()) {
return;
}
// == 2、检查当前结果文件数量批量发送文件或打包上传
File resultDir = new File(TaskResultOper.getTaskResultPath());
if(!resultDir.exists()){
return;
}
// == 3
File[] fileArr = FileUtil.getFilesEndWith(resultDir, Contants.TASK_RESULT_FILE_SUFFIX);
fileArr = FileUtil.sortASCByFileName(fileArr);
List<String> results = new LinkedList<String>();
StringBuffer sb = new StringBuffer();
for(File file : fileArr){
sb.delete(0, sb.length());
if(!file.exists() || !file.isFile()){
continue;
}
String[] resultArr = FileWrUtil.cfgFileReader(file);
if(resultArr!=null && resultArr.length>0){
for(String res : resultArr){
sb.append(res + ";");
}
sb.deleteCharAt(sb.length()-1);
results.add(sb.toString());
}
}
if(results.size() >0 ){
logger.debug("sendTaskResult-->" + Arrays.toString(results.toArray()));
//发送任务结果请求
this.sendObject(results, BusinessType.ObjTaskResult);
String res = this.readLine();
if (CommonSocket.SUCCESS.equalsIgnoreCase(res)) {
// 移动上传成功的任务结果到指定日期目录
moveTaskResultToDateDir(fileArr);
}
}
sw.end();
logger.info("本次收集传送任务结果总数:" + fileArr.length + ",用时:" + sw.toString(sw.total()));
} catch (Exception e) {
logger.error("",e);
}
logger.debug("结束");
}
private void sendTaskReturnFile(){
StopWatch sw = new StopWatch();
logger.debug("传送回传文件开始 ~~~~~~~");
try {
// == 1、针对回传文件过多时打包上传未完成的文件继续上传
File taskDir = new File(Contants.localTaskPath);
if (!taskDir.exists()) {
return;
}
// == 2、检查当前回传文件数量单个发送文件或打包上传
File returnDir = new File(Contants.localTaskReturnPath);
if(!returnDir.exists()){
return;
}
File[] fileArr = FileUtil.getFilesEndWith(returnDir, Contants.TASK_RETURN_FILE_SUFFIX);
if(fileArr == null || fileArr.length == 0){
return;
}
// == 3、发送
fileArr = FileUtil.sortASCByModify(fileArr); //修改日期升序排序
for(File file : fileArr){
if(!file.exists() || !file.isFile()){
continue;
}
String[] resultArr = FileWrUtil.cfgFileReader(file);
if (resultArr == null || resultArr.length <= 0) {
continue;
}
JSONObject jsonObject = JSONObject.fromObject(resultArr[0]);
ReturnFilePO rfPo = (ReturnFilePO) JSONObject.toBean(jsonObject, ReturnFilePO.class);
//--回传文件名和回传描述信息均为空时,则无回传文件
if(StringUtil.isEmpty(rfPo.getReturnFileName()) && StringUtil.isEmpty(rfPo.getResDesc())){
logger.warn("无回传文件, 不用回传");
FileUtil.delDir(file);
continue;
}
//--回传文件名为空但回传描述信息不为空则进行步骤2发送任务结果、删除文件
/**
* 步骤1、回传文件
*/
StringBuffer sb = new StringBuffer();
if(rfPo.getResDesc()!=null){//取已有的结果描述信息
sb.append(rfPo.getResDesc());
}
//准备回传文件,回传文件名不为空即有回传的文件时,再回传
if(rfPo.getReturnFileName()!=null && rfPo.getReturnFileName().trim().length()>0){
//发送回传文件请求
String msg = TaskResultOper.getTaskResultMsg(rfPo
.getTaskId(), rfPo.getTaskType(), null, null, null, rfPo
.getStartTime(), rfPo.getEndTime(), rfPo.getIsLoop());
File returnFile = new File(Contants.localTaskReturnPath + File.separator + rfPo.getReturnFileName());
String res = this.sendReturnFile(msg, returnFile, BusinessType.FileTaskReturn);
if(CommonSocket.SUCCESS.equals(res)){
/**
* 步骤2-1、发送任务结果
*/
TaskResultOper.sendTaskResult(rfPo.getTaskId(), rfPo.getTaskType(),
rfPo.getState(), sb.toString(), "", rfPo.getStartTime(),
rfPo.getEndTime(), false, rfPo.getIsLoop());
/**
* 步骤2-2、移动上传成功的 保存回传文件信息的文件 和 回传文件 到指定日期目录
*/
moveTaskReturnToDateDir(file, rfPo.getReturnFileName());
}
}
}
sw.end();
logger.info("本次收集传送任务回传总数:" + fileArr.length + ",用时:" + sw.toString(sw.total()));
} catch (Exception e) {
logger.error("send Task Return File error",e);
}
logger.debug("结束");
}
/**
* 发送心跳
* @throws IOException
*/
private void sendHeartBeat() throws IOException{
byte[] msg = (System.currentTimeMillis()+"").getBytes();
int len = msg.length;
int t = BusinessType.HeartBeat.getType();
os.write(Common.intToByteArray(len));
os.write(0);//消息类型
os.write(t);//业务类型
os.write(msg);
os.flush();
String res = readLine();
logger.debug("HeartBeat :" + res);
}
/***
* 发送任务回传文件
* @param msg
* @param file
* @param type
* @throws IOException
*/
private String sendReturnFile(String msg,File file,BusinessType type) throws IOException{
String name = file.getName();
byte[] nameByte = name.getBytes();
if(nameByte.length > Byte.MAX_VALUE){
throw new IllegalArgumentException("file name too long,max length is 127");
}
byte[] msgBytes = msg.getBytes();
int msgLen = msgBytes.length;
int totalLen = (int)file.length() + msgBytes.length +4;
int t = type.getType();
os.write(Common.intToByteArray(totalLen));
os.write(nameByte.length);//文件名的长度
os.write(t);//业务类型
os.write(nameByte);//文件名
os.write(Common.intToByteArray(msgLen));//消息头长度
os.write(msgBytes);//消息头
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
IOUtils.copy(fis, os);//文件内容
os.flush();
} finally {
if(fis != null){
fis.close();
}
}
String res = readLine();
logger.debug("sendReturnFile :" + res);
return res;
}
private void sendObject(Object data,BusinessType type) throws IOException{
ByteOutputStream bos = null;
ObjectOutputStream oos = null;
try {
bos = new ByteOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(data);
byte[] bytes = bos.getBytes();
int len = bytes.length;
int t = type.getType();
os.write(Common.intToByteArray(len));
os.write(0);
os.write(t);
os.write(bytes);
} finally {
if(bos != null){
bos.close();
}
if(oos != null){
oos.close();
}
}
}
/**
* 发送数据
* @param data
* @param type
* @throws IOException
*/
private void sendData(byte[] data,BusinessType type) throws IOException{
int len = data.length;
int t = type.getType();
os.write(Common.intToByteArray(len));
os.write(0);
os.write(t);
os.write(data);
}
/**
* 发送数据
* @param file
* @param bt
* @throws IOException
*/
private void sendData(File file,BusinessType bt) throws IOException{
int len = (int)file.length();
int t = bt.getType();
os.write(Common.intToByteArray(len));
os.write(0);
os.write(t);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
IOUtils.copy(fis, os);
os.flush();
} finally {
if(fis != null){
fis.close();
}
}
}
/**
* 发送文件 包括 文件名
* @param file
* @param type
* @throws IOException
*/
private void sendFile(File file ,BusinessType type) throws IOException{
String name = file.getName();
byte[] nameByte = name.getBytes();
if(nameByte.length > Byte.MAX_VALUE){
throw new IllegalArgumentException("file name too long,max length is 127");
}
int len = (int)file.length();
os.write(Common.intToByteArray(len));//文件长度
os.write(nameByte.length);//文件名长度
os.write((byte)type.getType());//业务类型
os.write(name.getBytes());//文件名
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
IOUtils.copy(fis, os);
os.flush();
} finally {
if(fis != null){
fis.close();
}
}
String res = readLine();
logger.debug("HeartBeat :" + res);
}
private String readLine() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(is, Contants.charset));
String str = br.readLine();
logger.debug("recieve : " + str);
return str;
}
/**
* 移动上传成功的数据文件到指定日期目录
* 完整文件到目录:.../done/type_procIden/yyyyMMdd
* 0大小文件到目录: .../error/type_procIden/yyyyMMdd
*/
public static void moveDetecDataToDateDir(File... allFiles){
if(allFiles==null || allFiles.length==0){
return;
}
for (File file : allFiles) {
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = "";
if (file.length() > 0) {
newDir = Contants.localDataDonePath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
} else {
newDir = Contants.localDataErrorPath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
}
// -- 文件移动
FileUtil.moveFile(file, newDir, true);
}
}
/**
* 遍历所有准备上传的数据文件,将空数据文件移动到指定目录,并记录所有文件总数
* @param allFiles 所有非空文件集合
* @param dataDirs 所有数据目录
* @return 所有文件个数(包括空文件)
* @throws Exception
*/
private long handleNullDataFile(List<File> allFiles, File[] dataDirs) throws Exception {
long total = 0l;
for(File dir : dataDirs){
File[] files = FileUtil.getFilesEndWith(dir, ".csv");
if(files==null || files.length==0){
continue;
}
files = FileUtil.sortASCByModify(files); // 修改日期升序排序
total += files.length;
for (File file : files) {
if (file.length() > 0) {
allFiles.add(file);
continue;
}
//--- 处理空文件数据:移动空文件数据到指定日期目录
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = Contants.localDataErrorPath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
FileUtil.moveFile(file, newDir, true);
}
}
return total;
}
/**
* 移动上传成功的任务结果到指定日期目录
* 完整文件到目录:.../done/result/yyyyMMdd
* 0大小文件到目录: .../error/result/yyyyMMdd
*/
public static void moveTaskResultToDateDir(File... fileArr){
if(fileArr==null || fileArr.length==0){
return;
}
for (File file : fileArr) {
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = "";
if (file.length() > 0) {
newDir = Contants.localTaskDonePath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
} else {
newDir = Contants.localTaskErrorPath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
}
// -- 文件移动
FileUtil.moveFile(file, newDir, true);
}
}
/**
* 移动上传成功的 保存回传文件信息的文件 和 回传文件 到指定日期目录
* 完整文件到目录:.../done/return/yyyyMMdd
* 0大小文件到目录: .../error/return/yyyyMMdd
*/
public static void moveTaskReturnToDateDir(File file, String returnFileName){
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = Contants.localTaskDonePath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
// -- 文件移动
FileUtil.moveFile(file, newDir, true);// 保留任务信息的临时文件.return
if(returnFileName!=null && !"".equals(returnFileName)){// 实际回传的文件
File curReturnFile = new File(Contants.localTaskReturnPath + File.separator + returnFileName);
FileUtil.moveFile(curReturnFile, newDir, true);
}
}
public static void main(String[] args) throws Exception {
DataSendThread.logger.debug("--------------开始-----------------");
int threadSize = 1;//默认开10个线程
int interval = 10;//间隔 60 s
if(args != null&&args.length >0){
threadSize = Integer.valueOf(args[0]);
}
if(args != null&&args.length >1){
interval = Integer.valueOf(args[1]);
}
ScheduledExecutorService stp = Executors.newScheduledThreadPool(100);
for(int i = 0;i< threadSize;i++){
DataSendThread dst = new DataSendThread("10.0.6.108", 9527);
stp.scheduleWithFixedDelay(dst, 0, interval, TimeUnit.SECONDS);
}
}
}

View File

@@ -0,0 +1,158 @@
package com.nis.nmsclient.thread.upload;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.thread.socket.SSLClient;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileUtil;
import com.nis.nmsclient.util.Utils;
import com.nis.nmsclient.util.ZipUtil;
/**
* 用于定时扫描并上传监测数据文件【数据收集方式改为DC主动后此类废弃】
*
**/
public class UploadDataThread implements Runnable {
Logger logger = Logger.getLogger(UploadDataThread.class);
private String name;
public UploadDataThread(String name) {
this.name = name;
}
public void run() {
long startTime = System.currentTimeMillis();
Thread.currentThread().setName(name);
logger.debug("上传数据开始 ~~~~~~~");
try {
//针对数据文件过多时打包上传未完成的文件继续上传
File parDir = new File(Contants.localDataCollection);
if(!parDir.exists()){
return;
}
File[] fileArr = FileUtil.getFilesEndWith(parDir, ".zip");
if (fileArr != null && fileArr.length > 0) {
for (File file : fileArr) {
if (!file.getName().startsWith(CommonSocket.BP_TYPE_DETECT_DATA)) {
continue;
}
Future<?> future = Common.service.submit(new SSLClient(name,
CommonSocket.REQ_BP_UPLOAD_FIFE, new String[] {
CommonSocket.BP_TYPE_DETECT_DATA,
file.getAbsolutePath() }));
future.get();
}
}
//----------------------------------------
File dataDir = new File(Contants.localDataFilePath);
if (!dataDir.exists()) {
logger.warn("Data directory“" + dataDir.getAbsolutePath() + "”Non-existent");
return;
}
long total = 0;
List<File> allFiles = new ArrayList<File>();
File[] dataDirs = FileUtil.getDirectoryArray(dataDir);
for(File dir : dataDirs){
File[] files = FileUtil.getFilesEndWith(dir, ".csv");
if(files==null || files.length==0){
continue;
}
files = FileUtil.sortASCByModify(files); // 修改日期升序排序
//allFiles.addAll(Arrays.asList(files));
total += files.length;
for (File file : files) {
if (file.length() > 0) {
allFiles.add(file);
continue;
}
//--- 处理空文件数据:移动空文件数据到指定日期目录
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = Contants.localDataErrorPath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
FileUtil.moveFile(file, newDir, true);
}
}
logger.info("本次轮循数据文件总数:" + total + ", 正常数据:" + allFiles.size() + ", 空数据:" + (total-allFiles.size()));
total = allFiles.size();//正常的要上传的数据个数
//--- 将所有数据文件一起打包,发送
if(total>Contants.COMMON_ZIP_MIN_SIZE){
//与Server通信
Future<?> serFuture = Common.service.submit(new SSLClient(
Thread.currentThread().getName(),
CommonSocket.REQ_HAND_SHAKE, null));
if (!Contants.isSucessByResult((String) serFuture.get())) {
logger.debug("UploadDataThread--ServerHandShake--fail return");
return;
}
//压缩并删除原文件
String compressFileStr = Contants.localDataCollection
+ File.separator
+ CommonSocket.addTimeTagForFileName(
CommonSocket.BP_TYPE_DETECT_DATA, null, true) + ".zip";
ZipUtil.zipWithDelFile(dataDirs, compressFileStr, true);
//发送
Future<?> future = Common.service.submit(new SSLClient(
Thread.currentThread().getName(),
CommonSocket.REQ_BP_UPLOAD_FIFE, new String[]{CommonSocket.BP_TYPE_DETECT_DATA, compressFileStr}));
future.get();
logger.info("-----本次轮循将所有数据打包上传");
} else if (total > 0) {// -- 按正常所有监测数据批量上传
Future<?> future = Common.service.submit(new SSLClient(name,
CommonSocket.REQ_UPLOAD_DATAS, new Object[] {
dataDir.getParent(), allFiles }));
String msg = (String) future.get();
if (Contants.isSucessByResult(msg)) {
/**
* 移动上传成功的文件到指定日期目录
* 完整文件到目录:.../done/type_procIden/yyyyMMdd
* 0大小文件到目录: .../error/type_procIden/yyyyMMdd
*/
for (File file : allFiles) {
String dirTime = DateUtil.getStingDate(
DateUtil.YYYYMMDD,
new Date(file.lastModified()));
String newDir = "";
if (file.length() > 0) {
newDir = Contants.localDataDonePath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
} else {
newDir = Contants.localDataErrorPath
+ File.separator
+ file.getParentFile().getName()
+ File.separator + dirTime;
}
// -- 文件移动
FileUtil.moveFile(file, newDir, true);
}
}
logger.info("-----本次轮循上传数据总数:" + total + ",用时:"
+ (System.currentTimeMillis() - startTime) + "ms");
}else{
logger.info("-----本次轮循未上传数据");
}
} catch (Exception e) {
logger.error("Upload data exception" + Utils.printExceptionStack(e));
}
logger.debug("上传数据结束 ~~~~~~~");
}
}

View File

@@ -0,0 +1,135 @@
package com.nis.nmsclient.util;
import java.io.File;
import org.apache.log4j.Logger;
import com.nis.nmsclient.util.GzipUtil;
import com.nis.nmsclient.util.RarUtil;
import com.nis.nmsclient.util.ZipUtil;
/**
* 文件解压缩的统一处理
* @date Mar 20, 2012
* @version
*/
public class CompressFileMgr {
static Logger logger = Logger.getLogger(CompressFileMgr.class);
/**
* 文件压缩
* @param sourcePath 源文件或目录
* @param destFile 压缩后的文件
* @param excludes 源目录中不要压缩的文件列表
* @param isAbs 相对路径还是绝对路径压缩
* @return
* @throws Exception
*/
public boolean compressFile(String sourcePath, String destFile,
String[] excludes, boolean isAbs) throws Exception {
boolean returnFlag = true;
File folder = new File(sourcePath);
if (!folder.exists()) {
return false;
}
if (destFile.length() == (destFile.lastIndexOf(".zip") + 4)) {// 判断是否zip包
ZipUtil.zip(sourcePath, destFile, excludes);
} else if (destFile.length() == (destFile.lastIndexOf(".gz") + 3)) {// 判断是否gzip包
GzipUtil.gzipByCmd(sourcePath, destFile, excludes, isAbs);
} else if (destFile.length() == (destFile.lastIndexOf(".tar") + 4)) {// 判断是否tar包
GzipUtil.tarByCmd(sourcePath, destFile, excludes, isAbs);
} else {
throw new Exception("unable to compress this compressed file");
}
return returnFlag;
}
/**
* 文件解压如果sourcePath是一个目录则解压它下面的所有压缩文件
* @param sourcePath 需要解压的文件或需要解压文件的目录
* @param destPath 解压到的目标路径
* @param isAbs 相对路径还是绝对路径解压
* @return
* @throws Exception
*/
public boolean decompressFile(String sourcePath, String destPath, boolean isAbs)
throws Exception {
logger.debug("decompressFile start……");
boolean returnFlag = true;
logger.debug("decompressFile sourcePath-----" + sourcePath);
logger.debug("decompressFile destPath-----" + destPath);
File folder = new File(sourcePath);
if (!folder.exists()) {
return false;
}
if (folder.isDirectory()) {
String files[] = folder.list();
String fileAbs = null;
for (int i = 0; i < files.length; i++) {
fileAbs = sourcePath + File.separator + files[i];
decompressFile(fileAbs, destPath, isAbs);
}
} else {
decompress(sourcePath, destPath, isAbs);
}
logger.debug("decompressFile end!");
return returnFlag;
}
/**
* 解压文件,只针对单个压缩文件
* @param sourceFile 需要解压的文件
* @param destPath 解压到的目标路径
* @param isAbs 相对路径还是绝对路径解压
* @throws Exception
*/
private void decompress(String sourceFile, String destPath, boolean isAbs)
throws Exception {
logger.debug("decompress start……");
logger.debug("decompress sourceFile---" + sourceFile);
logger.debug("decompress 1-----"+(sourceFile.length() == (sourceFile.lastIndexOf(".zip") + 4)));
logger.debug("decompress 2-----"+(sourceFile.length() == (sourceFile.lastIndexOf(".rar") + 4)));
logger.debug("decompress 3-----"+(sourceFile.length() == (sourceFile.lastIndexOf(".gz") + 3)));
if (sourceFile.length() == (sourceFile.lastIndexOf(".zip") + 4)) {// 判断是否zip包
ZipUtil.unZip(sourceFile, destPath);
} else if (sourceFile.length() == (sourceFile.lastIndexOf(".rar") + 4)) {// 判断是否rar包
RarUtil.unRar(sourceFile, destPath);
} else if (sourceFile.length() == (sourceFile.lastIndexOf(".gz") + 3)) {// 判断是否gzip包
GzipUtil.unGzipByCmd(sourceFile, destPath, isAbs);
} else if (sourceFile.length() == (sourceFile.lastIndexOf(".tar") + 4)) {// 判断是否tar包
GzipUtil.unTarByCmd(sourceFile, destPath, isAbs);
} else {
throw new Exception("unable to decompress this decompressed file");
}
logger.debug("decompress end!");
}
public static String getCompressSuffixByOs(boolean isAbs){
String suffix = ".zip";
//根据操作系统确定获取进程ID的方式
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
suffix = ".zip";
}else if (os.startsWith("Linux")){
suffix = (isAbs ? "_absolute" : "_relative") + ".tar.gz";
}
return suffix;
}
public static boolean isCompressFile(File file){
boolean flag = false;
if(file.isFile()){
String fileName = file.getName();
if (".rar".equalsIgnoreCase(fileName.substring(fileName.length() - 4))
|| ".zip".equalsIgnoreCase(fileName.substring(fileName.length() - 4))
|| ".tar".equalsIgnoreCase(fileName.substring(fileName.length() - 4))
|| ".gz".equalsIgnoreCase(fileName.substring(fileName.length() - 3))) {
flag = true;
}
}
return flag;
}
}

View File

@@ -0,0 +1,83 @@
package com.nis.nmsclient.util;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 通过DES加密解密实现一个String字符串的加密和解密.
*
*/
public class DESUtil {
private final static String key = "longstar";
/**
* DES加密方法
*
*/
public static String desEncrypt(String message) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("ASCII"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("ASCII"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte data[] = message.getBytes("ASCII");
byte[] encryptedData = cipher.doFinal(data);
return getBASE64(encryptedData);
}
/**
* DES解密方法
*
*/
public static String desDecrypt(String message) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("ASCII"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("ASCII"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte data[] = getFromBASE64(message);
byte[] encryptedData = cipher.doFinal(data);
return new String(encryptedData);
}
// 对base64编码的string解码成byte数组
private static byte[] getFromBASE64(String s) {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return b;
} catch (Exception e) {
return null;
}
}
//将 byte数组 进行 BASE64 编码
private static String getBASE64(byte[] b) {
if (b == null)
return null;
try {
String returnstr = (new BASE64Encoder()).encode(b);
return returnstr;
} catch (Exception e) {
return null;
}
}
}

View File

@@ -0,0 +1,142 @@
package com.nis.nmsclient.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
public class DateUtil {
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");
/**
* 处理日期时用到参数。格式24小时制yyyyMMddHHmmss
*/
public static final SimpleDateFormat YYYYMMDDHH24MMSS = new SimpleDateFormat(
"yyyyMMddHHmmss");
/**
* 处理日期时用到参数。格式为yyyy-MM-dd
*/
public static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat(
"yyyy-MM-dd");
/**
* 处理日期时用到参数。格式为yyyyMMdd
*/
public static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat(
"yyyyMMdd");
/**
* 获得当前日期
*
* @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 long getDaysFromBeginToEnd(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 = getDaysFromBeginToEnd(beginD, endD);
} catch (ParseException e) {
logger.error(Utils.printExceptionStack(e));
return days;
}
return days;
}
public static long getDaysFromBeginToEnd(Date begin, Date end) {
long days = 0;
if (begin != null && end != null) {
days = getDaysFromBeginToEnd(begin.getTime(), end.getTime());
}
return days;
}
public static long getDaysFromBeginToEnd(long begin, long end) {
return (end - begin) / (24 * 60 * 60 * 1000);
}
public static long getMinutesFromBeginToEnd(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 = getMinutesFromBeginToEnd(beginD, endD);
} catch (ParseException e) {
logger.error(Utils.printExceptionStack(e));
return days;
}
return days;
}
public static long getMinutesFromBeginToEnd(Date begin, Date end) {
long days = 0;
if (begin != null && end != null) {
days = getMinutesFromBeginToEnd(begin.getTime(), end.getTime());
}
return days;
}
public static long getMinutesFromBeginToEnd(long begin, long end) {
return (end - begin) / (60 * 1000);
}
}

View File

@@ -0,0 +1,477 @@
package com.nis.nmsclient.util;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Contants;
/**
* 文件操作类:检索、排序
*
*/
public class FileUtil {
static Logger logger = Logger.getLogger(FileUtil.class);
/**
* 获取子目录列表
* @param file
* @return
*/
public static File[] getDirectoryArray(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;
}
});
}
/**
* 获取指定全部或部分名称的文件
* @param file
* @param azz
* @return
*/
public static File[] getFilesArray(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[] getFilesStartWith(File file, final String prefix) {
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.startsWith(prefix); // 过滤出注定前缀名称的文件
}
});
}
/**
* 获取指定时间段指定前缀的文件
* @param file
* @param suffix
* @return
*/
public static File[] getFilesStartWithByMillis(File file, final String prefix, final long sTimeMillis, final long eTimeMillis) {
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;
}
if (sTimeMillis > 0 && eTimeMillis > 0) {
if (new File(dir, name).lastModified() < sTimeMillis
|| new File(dir, name).lastModified() > eTimeMillis) {// 不在指定时间段
return false;
}
}
return name.startsWith(prefix); // 过滤出注定前缀名称的文件
}
});
}
/**
* 获取指定后缀名的文件
* @param file
* @param suffix
* @return
*/
public static File[] getFilesEndWith(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[] getFilesEndWithBeforeMillis(File file, final String suffix, final long timeMillis) {
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() || new File(dir,name).lastModified() > timeMillis) {
return false;
}
return name.endsWith(suffix); // 过滤出注定后缀名称的文件
}
});
}
/**
* 获取指定时间修改的文件
* @return
*/
public static File[] getFilesBeforeMillis(File file, final long timeMillis) {
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() || new File(dir,name).lastModified() > timeMillis) {
return false;
}
return true;
}
});
}
/**
* 获取指定毫秒之后修改过的文件
* @param file
* @param timeMillis
* @return
*/
public static File[] getFilesAfterMillis(File file, final long timeMillis) {
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).lastModified() >= timeMillis) {
return true;
} else
return false;
}
});
}
/**
* 获取指定时间名称及之前的文件夹
* @param file
* @param suffix
* @return
*/
public static File[] getDirsBeforeDateName(File file, final String yyyyMMddName) {
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() && name.compareTo(yyyyMMddName)<=0) {
return true;
}
return false;
}
});
}
/**
* 获取指定时间名称及之后的文件夹
* @param file
* @param suffix
* @return
*/
public static File[] getDirsAfterDateName(File file, final String yyyyMMddName) {
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() && name.compareTo(yyyyMMddName)>=0) {
return true;
}
return false;
}
});
}
/**
* 文件数组按最后修改日期排序,升序
* @param files
* @return
*/
public static File[] sortASCByModify(File[] files) {
Arrays.sort(files, new Comparator<File>(){
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;
}
/**
* 文件数组按最后修改日期排序,降序
* @param files
* @return
*/
public static File[] sortDescByModify(File[] files) {
Arrays.sort(files, new Comparator<File>(){
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;
}
/**
* 文件数组按名称排序,升序
* @param files
* @return
*/
public static File [] sortASCByFileName(File[] files) {
Arrays.sort(files, new Comparator<File>(){
public int compare(File arg0, File arg1) {
return arg0.getName().compareTo(arg1.getName());
}
});
return files;
}
/**
* 文件数组按名称排序,降序
* @param files
* @return
*/
public static File [] sortDescByFileName(File[] files) {
Arrays.sort(files, new Comparator<File>(){
public int compare(File arg0, File arg1) {
return arg1.getName().compareTo(arg0.getName());
}
});
return files;
}
/**
* 将文件移动到指定文件夹
*
* @param f
* 源文件
* @param newDir
* 新目录
* @param flag
* 是否覆盖 true:覆盖 false:不覆盖
*/
public static void moveFile(File f, String newDir, boolean flag) {
if (!f.exists()) {
logger.debug("文件已不存在,无法移动!" + f.getAbsolutePath());
return;
}
File fileDir = new File(newDir); // 备份目录
if (!fileDir.exists()) { // 检查并创建新目录
fileDir.mkdirs();
}
File dtnFile = new File(fileDir.getAbsolutePath() + File.separator
+ f.getName()); // 制定目标文件路径以及文件名
if (dtnFile.exists() && flag) { // 检查并删除已存在文件
//dtnFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(dtnFile);
logger.debug("moveFile 删除已存在文件--" + dtnFile.getAbsolutePath());
//FileUtil.checkParentDirExist(dtnFile);
}
try {
FileUtils.copyFile(f, dtnFile); // 移动文件
logger.debug("文件 From 》 " + f.getAbsolutePath()
+ "\n To 》 " + dtnFile.getAbsolutePath());
if (f.exists()) {
logger.debug("删除源文件" + f.getAbsolutePath());
//f.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(f);
//FileUtil.checkParentDirExist(f);
} else {
logger.debug("源文件不存在!不用删了!" + f.getAbsolutePath());
return;
}
} catch (IOException e) {
logger.error(Utils.printExceptionStack(e));
}
}
/**
* 删除文件或文件夹
*/
public static void delDir(File file) {
try {//文件在可删范围并且不在禁删范围
if (file.exists() && isIncludeDelRange(file) && !isExcludeDelRange(file)) {
if (!file.isDirectory()) {
file.delete();
logger.debug("FileUtil.delDir(File file)删除文件--" + file.getAbsolutePath());
FileUtil.checkParentDirExist(file);
} else if (file.isDirectory()) {
delDir2(file);
}
}
} catch (IOException e) {
logger.error(Utils.printExceptionStack(e));
}
}
/**
* 递归删除文件夹及其下的所有文件
*/
private static void delDir2(File file) throws IOException {
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
file.delete();
logger.debug("FileUtil.delDir2(File file)删除文件--" + file.getAbsolutePath());
FileUtil.checkParentDirExist(file);
} else if (file.isDirectory()) {
if (!isExcludeDelRange(file)) {// 文件不在禁删范围
for (File f : file.listFiles()) {
delDir2(f);
}
file.delete();
logger.debug("FileUtil.delDir2(File file)删除文件夹--" + file.getAbsolutePath());
FileUtil.checkParentDirExist(file);
}
}
}
/**
* 检查文件是否在可删范围内
* @param file
* @return true:是false:否
*/
public static boolean isIncludeDelRange(File file) throws IOException{
boolean flag = false;
String path = file.getCanonicalPath();
if(Contants.COMMON_DEL_PATH_INCLUDE!=null){
for(String include : Contants.COMMON_DEL_PATH_INCLUDE){
if(path.startsWith(include)){
flag = true;
break;
}
}
}else{//删除范围参数为空,则返回在删除范围
flag = true;
}
return flag;
}
/**
* 检查文件是否在禁删范围内
* @param file
* @return true:是false:否
*/
public static boolean isExcludeDelRange(File file) throws IOException{
boolean flag = false;
String path = file.getCanonicalPath();
if(Contants.COMMON_DEL_PATH_EXCLUDE!=null){
for(String exclude : Contants.COMMON_DEL_PATH_EXCLUDE){
if(path.startsWith(exclude)){
flag = true;
break;
}
}
}else{//禁删参数为空,则返回不在禁删范围
flag = false;
}
return flag;
}
public static void checkParentDirExist(File file){
logger.debug("检查父目录是否存在---" +file.getParentFile().exists() + "---" + file.getParent());
}
/**
* 格式化处理路径
* @param path
* @return
*/
public static String handlerPath(String path){
File file = new File(path);
try {
path = file.getCanonicalPath();
} catch (IOException e) {
logger.error(Utils.printExceptionStack(e));
}
return path;
}
public static File[] getAll(File dir, int level) {
logger.info(getLevel(level) + dir.getName());
level++;
File[] files = dir.listFiles();
for (int x = 0; x < files.length; x++) {
if (files[x].isDirectory())
getAll(files[x], level);
else
logger.debug(getLevel(level) + files[x].getName());
}
return files;
}
public static String getLevel(int level) {
StringBuilder sb = new StringBuilder();
sb.append("|--");
for (int x = 0; x < level; x++) {
sb.insert(0, "| ");
}
return sb.toString();
}
}

View File

@@ -0,0 +1,285 @@
package com.nis.nmsclient.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;
import com.nis.nmsclient.util.io.UnicodeReader;
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<String> list = new ArrayList<String>();
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, List<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);
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<String[]> csvFileParser(File file, String charset) throws Exception{
Reader in = null;
List<String[]> allLineList = new ArrayList<String[]>();
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;
}
}

View File

@@ -0,0 +1,680 @@
package com.nis.nmsclient.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.OutputStream;
import java.io.OutputStreamWriter;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import com.ice.tar.TarEntry;
import com.ice.tar.TarInputStream;
import com.ice.tar.TarOutputStream;
import com.nis.nmsclient.common.Contants;
/**
* gzip文件压缩与解压缩对文件tar归档与解归档工具类
*
*/
public class GzipUtil {
static Logger logger = Logger.getLogger(GzipUtil.class);
/**
* 使用命令对单个gz压缩包解压
* @param srcFileStr 需要解压的gz文件
* @param destDir 解压后的目标路径
* @param isAbs 解压文件时 true 按绝对路径解压, false 进入目标路径后解压
* @throws Exception
*/
public static void unGzipByCmd(String srcFileStr, String destDir, boolean isAbs) throws Exception{
logger.debug("unGzip start……");
BufferedReader bReader = null;
BufferedReader errorReader = null;
Process process = null;
try {
logger.debug("unGzipByCmd srcFile---" + srcFileStr);
logger.debug("unGzipByCmd destDir---" + destDir);
File srcFile = new File(srcFileStr);
File descFile = new File(destDir);
if(!srcFile.exists()){
throw new Exception("source file not exist");
}
if(!srcFile.isFile()){
throw new Exception("source file is not a file");
}
if(!descFile.exists()){
descFile.mkdirs();
}
if(!descFile.isDirectory()){
throw new Exception("compress destination path is not a directory");
}
String tarFile = srcFile.getParent() + File.separator + srcFile.getName().substring(0, srcFile.getName().length()-3);
StringBuffer sb = new StringBuffer();
sb.append("gunzip " + srcFileStr);
sb.append(";tar xvpPf " + tarFile);
if(!isAbs){
sb.append(" -C " + destDir);
}else{
sb.append(" -C /");
}
sb.append(";gzip " + tarFile);
logger.debug("unGzipByCmd cmd--->" + sb.toString());
String[] shellCmd = new String[] { "/bin/bash", "-c", sb.toString() };
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("unGzipByCmd--->" + line);
}
while ((line = errorReader.readLine()) != null) {
logger.debug("unGzipByCmd error--->" + line);
throw new IOException(line);
}
logger.debug("unGzipByCmd end……");
} catch (Exception e) {
throw e;
} finally {
try {
if (bReader != null) {
bReader.close();
}
if (errorReader != null) {
errorReader.close();
}
if (process != null) {
process.destroy();
}
} catch (Exception e) {
}
}
}
/**
* 使用命令将单个文件或一个目录压缩成Gzip格式
* @param srcPath 需要压缩的文件目录或文件
* @param destFileStr 压缩后的目标文件
* @param excludes 排除的目录或文件(全是绝对路径),不包含到压缩包的
* @param isAbs 压缩文件时 true 按绝对路径压缩(含有完成的路径压缩包), false 进入目标路径后压缩
* @throws Exception
*/
public static void gzipByCmd(String srcPath, String destFileStr, String[] excludes, boolean isAbs) throws Exception{
logger.debug("gzipByCmd start……");
OutputStreamWriter fos = null;
BufferedReader bReader = null;
BufferedReader errorReader = null;
Process process = null;
File excludeFile = null;
boolean flag = true;
try {
logger.debug("gzipByCmd srcPath---" + srcPath);
logger.debug("gzipByCmd destFileStr---" + destFileStr);
File srcFile = new File(srcPath);
File descFile = new File(destFileStr);
if(!srcFile.exists()){
throw new Exception("decompress file not exist");
}
if(!descFile.getParentFile().exists()){
descFile.getParentFile().mkdirs();
}
StringBuffer sb = new StringBuffer();
if(!isAbs){
if(srcFile.isFile()){
sb.append("cd " + srcFile.getParentFile().getCanonicalPath() + ";");
}else{
sb.append("cd " + srcFile.getCanonicalPath() + ";");
}
}
sb.append("tar -czvpPf " + destFileStr);
if(excludes!=null && excludes.length>0){
if(!isAbs){
for (int i = 0; i < excludes.length; i++) {
int index = excludes[i].indexOf(srcFile
.getCanonicalPath());
logger.debug("gzipByCmd index--->" + index + "---src=" +srcFile
.getCanonicalPath());
int start = index + srcFile.getCanonicalPath().length() + 1;
logger.debug("gzipByCmd start--->" + start + "--length=" + excludes[i].length() +"---exclude=" + excludes[i]);
if (index != -1 && start <= excludes[i].length()) {
excludes[i] = excludes[i].substring(start,
excludes[i].length());
logger.debug("gzipByCmd exclude--->" + excludes[i]);
}
}
}
excludeFile = new File(Contants.localTempPath + File.separator + "excludelist_" + System.currentTimeMillis() + ".temp");
// 2012-11-21 修改 由于使用FileWrUtil写的文件有文件头的Linux下无法正确识别
fos = new OutputStreamWriter(
new FileOutputStream(excludeFile, true), System.getProperty("file.encoding"));
logger.debug("gzipByCmd ---- " + System.getProperty("file.encoding"));
for(String val : excludes){
fos.write(val);
fos.write("\n");
}
fos.flush();
//FileWrUtil.cfgFilePrinter(excludeFile, Contants.charset, excludes);
sb.append(" -X " + excludeFile.getCanonicalPath());
}
if(!isAbs){
if(srcFile.isFile()){
sb.append(" " + srcFile.getName());
}else{
sb.append(" *");
}
}else{
sb.append(" " + srcFile.getCanonicalPath());
}
logger.debug("gzipByCmd cmd--->" + sb.toString());
String[] shellCmd = new String[] { "/bin/bash", "-c", sb.toString() };
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("gzipByCmd--->" + line);
}
while ((line = errorReader.readLine()) != null) {
logger.debug("gzipByCmd error--->" + line);
flag = false;
throw new IOException(line);
}
logger.debug("gzipByCmd end……");
} catch (Exception e) {
throw e;
} finally {
try {
if(fos!=null){
fos.close();
}
if(excludeFile!=null && excludeFile.exists()){
//excludeFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(excludeFile);
logger.debug("GzipUtil.gzipByCmd()--delete excludeFile=" + excludeFile.getAbsolutePath());
//FileUtil.checkParentDirExist(excludeFile);
}
if (bReader != null) {
bReader.close();
}
if (errorReader != null) {
errorReader.close();
}
if (process != null) {
process.destroy();
}
if(!flag){
File destFile = new File(destFileStr);
if(destFile.exists()){
//destFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(destFile);
logger.debug("GzipUtil.gzipByCmd()--delete destFile=" + destFile.getAbsolutePath());
//FileUtil.checkParentDirExist(destFile);
}
}
} catch (Exception e) {
}
}
}
/**
* 使用命令对单个tar压缩包解压
* @param srcFileStr 需要解压的gz文件
* @param destDir 解压后的目标路径
* @param isAbs 解压文件时 true 按绝对路径解压, false 进入目标路径后解压
* @throws Exception
*/
public static void unTarByCmd(String srcFileStr, String destDir, boolean isAbs) throws Exception{
logger.debug("unTarByCmd start……");
BufferedReader bReader = null;
BufferedReader errorReader = null;
Process process = null;
try {
logger.debug("unTarByCmd srcFile---" + srcFileStr);
logger.debug("unTarByCmd destDir---" + destDir);
File srcFile = new File(srcFileStr);
File descFile = new File(destDir);
if(!srcFile.exists()){
throw new Exception("source file not exist");
}
if(!srcFile.isFile()){
throw new Exception("source file is not a file");
}
if(!descFile.exists()){
descFile.mkdirs();
}
if(!descFile.isDirectory()){
throw new Exception("compress destination path is not a directory");
}
StringBuffer sb = new StringBuffer();
sb.append("tar xvpPf " + srcFileStr);
if(!isAbs){
sb.append(" -C " + destDir);
}else{
sb.append(" -C /");
}
logger.debug("unTarByCmd cmd--->" + sb.toString());
String[] shellCmd = new String[] { "/bin/bash", "-c", sb.toString() };
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("unTarByCmd--->" + line);
}
while ((line = errorReader.readLine()) != null) {
logger.debug("unTarByCmd--->" + line);
throw new IOException(line);
}
logger.debug("unTarByCmd end……");
} catch (Exception e) {
throw e;
} finally {
try {
if (bReader != null) {
bReader.close();
}
if (errorReader != null) {
errorReader.close();
}
if (process != null) {
process.destroy();
}
} catch (Exception e) {
}
}
}
/**
* 使用命令将单个文件或一个目录压缩成tar格式
* @param srcPath 需要压缩的文件目录或文件
* @param destFileStr 压缩后的目标文件
* @param excludes 排除的目录或文件(全是绝对路径),不包含到压缩包的
* @param isAbs 压缩文件时 true 按绝对路径压缩(含有完成的路径压缩包), false 进入目标路径后压缩
* @throws Exception
*/
public static void tarByCmd(String srcPath, String destFileStr, String[] excludes, boolean isAbs) throws Exception{
logger.debug("tarByCmd start……");
BufferedReader bReader = null;
BufferedReader errorReader = null;
Process process = null;
File excludeFile = null;
boolean flag = true;
try {
logger.debug("tarByCmd srcPath---" + srcPath);
logger.debug("tarByCmd destFileStr---" + destFileStr);
File srcFile = new File(srcPath);
File descFile = new File(destFileStr);
if(!srcFile.exists()){
throw new Exception("decompress file not exist");
}
if(!descFile.getParentFile().exists()){
descFile.getParentFile().mkdirs();
}
StringBuffer sb = new StringBuffer();
if(!isAbs){
if(srcFile.isFile()){
sb.append("cd " + srcFile.getParentFile().getCanonicalPath() + ";");
}else{
sb.append("cd " + srcFile.getCanonicalPath() + ";");
}
}
sb.append("tar -cvpPf " + destFileStr);
if(excludes!=null && excludes.length>0){
if(!isAbs){
for (int i = 0; i < excludes.length; i++) {
int index = excludes[i].indexOf(srcFile
.getCanonicalPath());
logger.debug("tarByCmd index--->" + index + "---src=" +srcFile
.getCanonicalPath());
int start = index + srcFile.getCanonicalPath().length() + 1;
logger.debug("tarByCmd start--->" + start + "--length=" + excludes[i].length() +"---exclude=" + excludes[i]);
if (index != -1 && start <= excludes[i].length()) {
excludes[i] = excludes[i].substring(start,
excludes[i].length());
logger.debug("tarByCmd exclude--->" + excludes[i]);
}
}
}
excludeFile = new File(Contants.localTempPath + File.separator + "excludelist_" + System.currentTimeMillis() + ".temp");
FileWrUtil.cfgFilePrinter(excludeFile, Contants.charset, excludes);
sb.append(" -X " + excludeFile.getCanonicalPath());
}
if(!isAbs){
if(srcFile.isFile()){
sb.append(" " + srcFile.getName());
}else{
sb.append(" *");
}
}else{
sb.append(" " + srcFile.getCanonicalPath());
}
logger.debug("tarByCmd cmd--->" + sb.toString());
String[] shellCmd = new String[] { "/bin/bash", "-c", sb.toString() };
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("tarByCmd--->" + line);
}
while ((line = errorReader.readLine()) != null) {
logger.debug("tarByCmd error--->" + line);
flag = false;
throw new IOException(line);
}
logger.debug("tarByCmd end……");
} catch (Exception e) {
throw e;
} finally {
try {
if(excludeFile!=null && excludeFile.exists()){
//excludeFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(excludeFile);
logger.debug("GzipUtil.tarByCmd()--delete excludeFile=" + excludeFile.getAbsolutePath());
//FileUtil.checkParentDirExist(excludeFile);
}
if (bReader != null) {
bReader.close();
}
if (errorReader != null) {
errorReader.close();
}
if (process != null) {
process.destroy();
}
if(!flag){
File destFile = new File(destFileStr);
if(destFile.exists()){
//destFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(destFile);
logger.debug("GzipUtil.tarByCmd()--delete destFile=" + destFile.getAbsolutePath());
//FileUtil.checkParentDirExist(destFile);
}
}
} catch (Exception e) {
}
}
}
/**
* 对单个gz压缩包解压
* @param srcFile 需要解压的gz文件
* @param destDir 解压后的目标路径
* @throws Exception
*/
public static void unGzip(String srcFile, String destDir) throws Exception{
logger.debug("unGzip start……");
OutputStream out = null;
GZIPInputStream gis = null;
File tempFile = new File(destDir + File.separator + System.currentTimeMillis() + ".tar");
try {
logger.debug("unGzip tempFile----" + tempFile.getAbsolutePath());
logger.debug("unGzip srcFile---" + srcFile);
logger.debug("unGzip destDir---" + destDir);
gis = new GZIPInputStream(new FileInputStream(new File(srcFile)));
if(!tempFile.getParentFile().exists()){
tempFile.getParentFile().mkdirs();
}
out = new FileOutputStream(tempFile);
logger.debug("unGzip tempFile---------" + tempFile.getAbsolutePath());
byte[] buf = new byte[1024 * 2];
int len = 0;
while ((len = gis.read(buf)) != -1) {
logger.debug("unGzip len---------" + len);
out.write(buf, 0, len);
}
out.flush();
//tar包解归档
GzipUtil.unTar(tempFile.getAbsolutePath(), destDir);
logger.debug("unGzip end……");
} catch (Exception e) {
throw e;
} finally {
try {
if(gis!=null) gis.close();
if(out!=null) out.close();
} catch (Exception e) {
e.printStackTrace();
}
logger.debug("tempFile.delete() start……" + tempFile.getAbsolutePath());
if(tempFile.exists()){
//tempFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(tempFile);
logger.debug("unGzip delete file --" + tempFile.getAbsolutePath());
//FileUtil.checkParentDirExist(tempFile);
}
logger.debug("tempFile.delete() end……" + tempFile.getAbsolutePath());
}
}
/**
* 将单个文件或一个目录压缩成Gzip格式
*
* @param srcPath 需要压缩的文件目录或文件
* @param destFile 压缩后的目标文件
* @throws Exception
*/
public static void gzip(String srcPath, String destFile) throws Exception {
File tempFile = new File(new File(destFile).getParent() + File.separator + System.currentTimeMillis() + ".tar");
GZIPOutputStream gzout = null;
FileInputStream tarin = null;
try{
//tar包归档
GzipUtil.tar(srcPath, tempFile.getAbsolutePath());
//建立gzip压缩输出流
gzout = new GZIPOutputStream(new FileOutputStream(destFile));
//打开需压缩文件作为文件输入流
tarin = new FileInputStream(tempFile);
int len;
byte[] buf = new byte[1024 * 2];
while ((len = tarin.read(buf)) != -1) {
gzout.write(buf, 0, len);
}
gzout.flush();
}catch (Exception e) {
throw e;
}finally {
if(tarin!=null) tarin.close();
if(gzout!=null) gzout.close();
if(tempFile.exists()){
//tempFile.delete_bak();
//使用删除文件公共方法
FileUtil.delDir(tempFile);
logger.debug("GzipUtil.gzip()--delete tempFile=" + tempFile.getAbsolutePath());
//FileUtil.checkParentDirExist(tempFile);
}
}
}
/**
* 对单个tar压缩包解压即解归档
*
* @param srcFile 需要解压的tar文件
* @param outpath 解压后的目标路径
* @throws Exception
*/
public static void unTar(String srcFile, String outpath) throws Exception{
logger.debug("unTar start……");
TarInputStream tis = null;
try {
logger.debug("unTar srcFile---" + srcFile);
logger.debug("unTar outpath---" + outpath);
File file = new File(srcFile);
if(!file.exists()){
// throw new Exception("解压源文件不存在: " + file.getAbsolutePath());
throw new Exception("Unzip source file does not exist: " + file.getAbsolutePath());
}
tis = new TarInputStream(new FileInputStream(file));
/*关键在于这个TarEntry 的理解, 实际你的tar包里有多少文件就有多少TarEntry*/
TarEntry tarEntry = null;
while ((tarEntry = tis.getNextEntry()) != null) {
logger.debug("unTar tarEntry---" + tarEntry);
String tempFileName = (outpath + File.separator + tarEntry.getName()).replaceAll("\\\\", "/");
logger.debug("unTar tempFileName---" + tempFileName);
if (tarEntry.isDirectory()) {
logger.debug("unTar tarEntry is Dir");
int end = tempFileName.lastIndexOf("/");
if (end != -1) {
File dir = new File(tempFileName.substring(0, end));
if (!dir.exists()) {
dir.mkdirs();
}
}
} else {
logger.debug("unTar tarEntry is file");
File tempFile = new File(tempFileName);
if(!tempFile.getParentFile().exists()){
tempFile.getParentFile().mkdirs();
}
tempFile.createNewFile();
OutputStream out = new FileOutputStream(tempFile);
IOUtils.copy(tis, out);
out.close();
}
}
logger.debug("unTar end……");
} catch (Exception e) {
throw e;
} finally {
try {
if(tis!=null) tis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 将单个文件或一个目录打成tar包即归档
*
* @param srcDir 需要压缩的文件目录或文件
* @param destFile 压缩后的目标文件
* @throws Exception
*/
public static void tar(String srcDir, String destFile) throws Exception {
TarOutputStream tout = null;
try{
//建立tar压缩输出流
tout = new TarOutputStream(new FileOutputStream(destFile));
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(srcFile.isDirectory()){
tar(tout,srcFile,srcFile.getName());
}else{
tar(tout,srcFile,"");
}
}catch (Exception e) {
throw e;
}finally {
if(tout!=null) tout.close();
}
}
/**
* tar压缩
* @param out tar压缩流
* @param srcFile 需要压缩的目录或文件
* @param base 需要压缩的文件在压缩后的tar包内的路径
* @throws Exception
*/
public static void tar(TarOutputStream tout, File srcFile, String base) 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) {
TarEntry tarEn = new TarEntry(srcFile); //此处必须使用new TarEntry(File file);
tarEn.setName(base); //此处需重置名称,默认是带全路径的,否则打包后会带全路径
tout.putNextEntry(tarEn);
}
for (int i = 0; i < files.length; i++) {
tar(tout, files[i], base + files[i].getName());
}
} else {
base = base.length() == 0 ? srcFile.getName() : base;
TarEntry tarEn = new TarEntry(srcFile);
tarEn.setName(base);
tout.putNextEntry(tarEn);
FileInputStream fis = null;
try{
fis = new FileInputStream(srcFile);
IOUtils.copy(fis, tout);
}catch (IOException e) {
throw e;
}finally{
if(fis!=null) fis.close();
}
}
tout.closeEntry();
}
public static void main(String[] args) {
try {
long tt = System.currentTimeMillis();
//tar("D:\\temp\\test", "d:\\temp\\t1.tar");
//unTar("D:\\temp\\t1.tar", "D:\temp\\t1");
//gzip("D:\\temp\\t1\\gd-2.0.350000", "d:\\temp\\test\\t1.tar.gz");
unGzip("d:\\temp\\nms.tar.gz", "D:\\temp\\t1");
System.out.println(System.currentTimeMillis()-tt);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,99 @@
package com.nis.nmsclient.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 = null;
try {
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());
} catch (Exception e) {
throw new IOException(e.getCause());
}finally{
if(in!=null){
in.close();
}
}
}
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);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,125 @@
package com.nis.nmsclient.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
/**
* 对rar进行解压缩
*
*/
public class RarUtil {
static Logger logger = Logger.getLogger(RarUtil.class);
/**
* 对单个rar压缩包解压
* @param rarFileName 需要解压的rar文件
* @param destDir 解压后的目标目录
* @throws Exception
*/
public static void unRar(String rarFileName, String destDir)
throws Exception {
logger.debug("unRar start……");
Archive archive = null;
FileHeader fh = null;
try {
logger.debug("unRar rarFileName---" + rarFileName);
logger.debug("unRar destDir---" + destDir);
File file = new File(rarFileName);
if(!file.exists()){
// throw new Exception("解压源文件不存在: " + file.getAbsolutePath());
throw new Exception("Unzip source file does not exist: " + file.getAbsolutePath());
}
logger.debug("unRar: get archive……");
archive = new Archive(file);
logger.debug("unRar: archive =" + archive);
if (archive != null) {
logger.debug("unRar: while start");
int index = 0;
while ((fh = archive.nextFileHeader()) != null) {
logger.debug("unRar: " + ++index + " start");
String fileName = fh.getFileNameW().trim();
if (!existZH(fileName)) {
fileName = fh.getFileNameString().trim();
}
String path = (destDir + File.separator + fileName)
.replaceAll("\\\\", "/");
logger.debug("unRar: path---" + path);
if (fh.isDirectory()) {
//logger.debug("unRar: FileHeader is directory");
int end = path.lastIndexOf("/");
if (end != -1) {
File dir = new File(path.substring(0, end));
if (!dir.exists()) {
dir.mkdirs();
}
}
}else{
//logger.debug("unRar: FileHeader is file");
FileOutputStream os = null;
try{
File out = new File(path);
if(!out.getParentFile().exists()){
out.getParentFile().mkdirs();
}
os = new FileOutputStream(out);
archive.extractFile(fh, os);
}catch (Exception e) {
throw e;
}finally{
fh = null;
if (os != null) {
os.close();
os = null;
}
}
}
logger.debug("unRar: " + index + " end");
}
logger.debug("unRar: while end");
}
logger.debug("unRar end……");
} catch (Exception e) {
throw e;
} finally {
fh = null;
try {
if (archive != null) {
archive.close();
archive = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static boolean existZH(String str) {
String regEx = "[\\u4e00-\\u9fa5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
return true;
}
return false;
}
public static void main(String[] args) {
try {
unRar("D:\\temp\\logs.rar", "D:\\temp\\log");
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,528 @@
/*
* @(#)StringUtil.java 1.0
*
* Copyright 2010 NIS, Inc. All rights reserved.
*
*/
package com.nis.nmsclient.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import sun.misc.BASE64Encoder;
/**
*
* <p>字符串处理工具类.</p>
* @author 中科智源育成信息有限公司 E-mail: doufengh@software.ict.ac.cn
* @version 1.0 创建时间Nov 2, 2010 2:57:56 PM
*
*/
public final class StringUtil {
/**
* ISO8859_1 编码集
*/
public static final String CODE_ISO8859_1 = "ISO8859_1";
/**
* GB2312 编码集
*/
public static final String CODE_GB2312 = "GB2312";
/**
* GBK 编码集
*/
public static final String CODE_GBK = "GBK";
/**
* UTF-8 编码集
*/
public static final String CODE_UTF_8 = "UTF-8";
private static int size = 0;
private static final String[] SIMPLIFIED_CASE = { "O", "", "", "", "", "",
"", "", "", "", "" };
// private static final String[] SIMPLIFIED_CASE = { "Zero", "One", "Two", "Three", "Four", "Five",
// "Six", "Seven", "Eight", "Nine", "Ten" };
private static final String[] TRADITIONAL_CASE = { "", "", "", "", "", "",
"", "", "", "", "" };
// private static final String[] TRADITIONAL_CASE = { "Zero", "One", "Two", "Three", "Four", "Five",
// "Six", "Seven", "Eight", "Nine", "Ten" };
/**
*<p>Description:抑制默认的构造器,避免实例化对象 </p>
*/
private StringUtil() {
}
/**
*
* <p>判断一个对象是否为空</p>
* <p>
* <code>object</code>元素判断所有对象是否为空.
* 另外对{@link String}、{@link Collection} 、{@link Map} 进行长度验证,如果长度为0,视为空对象.
* </p>
* <pre>
* String aa = " ";
* List list = new ArrayList()
* LinkedHashSet set = new LinkedHashSet();
* StringUtil.isEmpty(aa) = true
* StringUtil.isEmpty(list) = true
* StringUtil.isEmpty(set) = true
* StringUtil.isEmpty("\t") = true
* </pre>
* @param object 对象元素
* @return <code>true</code> 对象为<code>null</code>,<code>false</code> 对象不为<code>null</code>.
*/
public static boolean isEmpty(Object object) {
initSize(object);
return size==0;
}
/**
*
* 判断对象是否有数据存在? 不存在为0、存在不为0的值.
* @param object 对象值
*/
private static void initSize(Object object){
if (object == null) {
size = 0;
} else {
if (object instanceof String) {
size = ((String)object).trim().length();
} else if (object instanceof Collection) {
size = ((Collection)object).size();
} else if (object instanceof Map) {
size = ((Map)object).size();
//其他数据类型
} else {
size = 1;
}
}
}
/**
*
* <p>如果对象为空时,返回默认值.</p>
* @param object 要判断是否为空的对象
* @param defaultValue 为空时设的默认值
* @see #isEmpty(Object)
* @return 获取处理后的数据
*/
public static Object setDefaultValueIfNull(Object object,Object defaultValue){
if(isEmpty(object)){
return defaultValue;
}
return object;
}
/**
*
* <p>对字符串进行MD5加密.</p>
* <p>
* 一般作为密码的处理方式,首先通过MD5进行加密然后将字符串进行Base64编码获得所需字符.
* </p>
* <pre>
* String str = "ceshi";
* StringUtil.md5(str) = "zBfDDNERxyFfyPUfh5Dg4Q=="
* </pre>
* @param msg 要加密的字符串
* @return 返回加密后的25位字符,如果解析出现异常将返回<code>null</code>.
*/
public static String md5(String msg) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] b = md.digest(msg.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
String code = encoder.encode(b);
return code;
} catch (Exception e) {
return null;
}
}
/**
* <p>截取处理字符串,当字符串超过指定的截取长度时,用“......”补充</p>
* <pre>
* String str = "中华人民共和国";
* StringUtil.getMoreString(str, 6) = "中华人民共和......"
* </pre>
* @param text 字符串数据
* @param length 截取的长度值
* @return 返回处理后字符
*/
public static String getMoreString(String text,int length){
StringBuilder textBuilder = new StringBuilder();
if(isEmpty(text)){
return null;
}
if(text.length()>length){
text = text.substring(0,length);
textBuilder.append(text).append("......");
}else {
textBuilder.append(text);
}
return textBuilder.toString();
}
/**
*
* <p>通过给定url获取域名地址.</p>
* <p>
* 获得域名信息需去除 http/https/ftp 与 www 一些头信息,获取有效地址.
* </p>
* <pre>
* StringUtil.getDomain("http://www.baidu.com") = "baidu.com"
* </pre>
* @param webSiteUrl url 地址
* @return 返回截取后的域名地址
*/
public static String getDomain(String webSiteUrl) {
String url = "";
if(isEmpty(webSiteUrl)){
return url;
}
if (webSiteUrl.indexOf("http://") >= 0) {
url = webSiteUrl.substring("http://".length(), webSiteUrl.length());
} else if (webSiteUrl.indexOf("https://", 0) >= 0) {
url = webSiteUrl.substring("https://".length(), webSiteUrl.length());
} else if (webSiteUrl.indexOf("ftp://", 0) >= 0) {
url = webSiteUrl.substring("ftp://".length(), webSiteUrl.length());
} else {
url = webSiteUrl;
}
if (url.indexOf("/", 0) >= 0) {
url = url.substring(0, url.indexOf("/", 1));
}
if (url.indexOf("www.") == 0) {
url = url.substring(url.indexOf(".") + 1, url.length());
}
if (url.indexOf("?") >= 0) {
url = url.substring(0, url.indexOf("?"));
}
return url;
}
/**
*
* <p>按照给定规则分隔字符串</p>
* <pre>例子说明:</pre>
* @param str 需要分隔的字符串
* @param regex 分隔规则
* @return 返回字符串数组,如果分隔字符串为空返回<code>null</code>.
*/
public static String[] Split(String str,String regex) {
if(StringUtil.isEmpty(str)){
return null;
}
return str.split(regex);
}
/**
*
* <p>字符编码转换,需要提供字符串的源编码格式.</p>
* <p>
* 字符串工具类中提供一些字符编码常量:
* {@link #CODE_GB2312}\{@link #CODE_GBK}\{@link #CODE_ISO8859_1}\{@link #CODE_UTF_8}
* </p>
* @param value 要编码的值
* @param sourceCodingFormat 字符的原始编码格式,具体编码格式可看本类提供的编码样式.
* @param destCodingFormat 要转换字符的编码格式,具体编码格式可看本类提供的编码样式.
* @return 返回编码后的字符串.
* @throws UnsupportedEncodingException
*/
public static String getCodingConversionResult(String value,String sourceCodingFormat,
String destCodingFormat ) throws UnsupportedEncodingException{
if(isEmpty(value)){
return null;
}
return new String(value.getBytes(sourceCodingFormat), destCodingFormat);
}
/**
*
* <p>将url转为utf编码格式url,当url符合utf8格式,不会转换.</p>
* <pre>
* StringUtil.getUTF8URLEncode("http://www.baidu.com/s?param='中国'") =
* "http%3A%2F%2Fwww.baidu.com%2Fs%3Fparam%3D%27%E4%B8%AD%E5%9B%BD%27"
* </pre>
* @see #getUTF8URLDecode(String)
* @param url 字符串url
* @return 返回utf8转换后的字符url
* @throws UnsupportedEncodingException
*/
public static String getUTF8URLEncode(String url)throws UnsupportedEncodingException{
if(isUtf8Url(url)){
return url;
}
return URLEncoder.encode(url, StringUtil.CODE_UTF_8);
}
/**
*
* <p>将utf8编码的url解析为原始url.当url不符合utf8格式时,不转换.</p>
* <pre>例子说明:</pre>
* @see #getUTF8URLEncode(String)
* @param url 字符串url
* @return 返回解析后字符url
* @throws UnsupportedEncodingException
*/
public static String getUTF8URLDecode(String url)throws UnsupportedEncodingException{
/*if(!isUtf8Url(url)){
return url;
}*/
return URLDecoder.decode(url, StringUtil.CODE_UTF_8);
}
/** * 编码是否有效
* @param text
* @return
*/
private static boolean Utf8codeCheck(String text){
String sign = "";
if (text.startsWith("%e")){
for (int i = 0, p = 0; p != -1; i++) {
p = text.indexOf("%", p);
if (p != -1){
p++;
}
sign += p;
}
}
return sign.equals("147-1");
}
/**
* 是否Utf8Url编码
* @param text
* @return true or false
*/
private static boolean isUtf8Url(String text) {
text = text.toLowerCase();
int p = text.indexOf("%");
if (p != -1 && text.length() - p > 9) {
text = text.substring(p, p + 9);
}
return Utf8codeCheck(text);
}
/**
*
* <p>判断字符串是否是数字格式(包括小数形式).</p>
* <pre>
* String a1 = "12";
* String a2 = "0.01";
* String a3 = "0.0.1";
* String a4 = "123a";
* StringUtil.isNumeric(a1) = true
* StringUtil.isNumeric(a2) = true
* StringUtil.isNumeric(a3) = false
* StringUtil.isNumeric(a4) = false
* </pre>
* @param numberString 数字格式字符串
* @return <code>true</code> 符合数字格式(包括小数),<code>false</code> 不符合数字格式.
*/
public static boolean isNumeric(String numberString){
if(isEmpty(numberString)){
return false;
}
if(numberString.startsWith(".")||numberString.endsWith(".")){
return false;
}
int length = numberString.split("\\.").length-1; //判断小数点在字符串中出现的次数。
if(length>1) { //小数点大于1次不符合数字规范
return false;
}
for(int i=0; i<numberString.length(); i++){
if(!Character.isDigit(numberString.charAt(i))&&!".".equals(String.valueOf(numberString.charAt(i)))){
return false;
}
}
return true;
}
/**
*
* <p>将字符串数字转换为简体大写中文格式.</p>
* <pre>
* StringUtil.convertSimplifiedCase("325") = ”三二五"
* </pre>
* @param numberString 数字字符串
* @return 返回简体大写后的数字
*/
public static String convertSimplifiedCase(String numberString) {
StringBuilder simplifiedBuilder = new StringBuilder();
if(isEmpty(numberString)){
return null;
}
for (int i = 0; i < numberString.length(); i++) {
String tempNumberString = String.valueOf(numberString.charAt(i));
if ("0123456789".indexOf(tempNumberString) >= 0) {
int number = Integer.parseInt(tempNumberString);
simplifiedBuilder.append(SIMPLIFIED_CASE[number]);
} else {
simplifiedBuilder.append(tempNumberString);
}
}
return simplifiedBuilder.toString();
}
/**
*
* <p>把字符串中的数字转换成繁体大写中文的格式.</p>
* <pre>
* StringUtil.convertTraditionalCase("325") = "叁贰伍"
* </pre>
* @param numberString 数字字符串
* @return 返回繁体大写后的数字
*/
public static String convertTraditionalCase(String numberString) {
StringBuilder simplifiedBuilder = new StringBuilder();
if(isEmpty(numberString)){
return null;
}
for (int i = 0; i < numberString.length(); i++) {
String tempNumberString = String.valueOf(numberString.charAt(i));
if ("0123456789".indexOf(tempNumberString) >= 0) {
int number = Integer.parseInt(tempNumberString);
simplifiedBuilder.append(TRADITIONAL_CASE[number]);
} else {
simplifiedBuilder.append(tempNumberString);
}
}
return simplifiedBuilder.toString();
}
/**
*
* <p>创建唯一标识字符串.</p>
* <pre>
* StringUtil.createUUID() = "00000DAF3CFC4E0B8DF2D5BEACB14D75"
* </pre>
* @return 返回标识符字符串
*/
public static String createUUID() {
String uuid = UUID.randomUUID().toString();
uuid = uuid.replace("-", "");
return uuid.toUpperCase();
}
/**
*
* <p>字符串过滤,负责将html的textarea属性获得的字符转换成html格式的字符集.</p>
* @param str 要解析的字符串
* @return 是解析后的字符串
*/
public static String htmlFilter(String str) {
StringBuffer stringbuffer = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
switch (c) {
case 39:
stringbuffer.append("&#039;");
break;
case 34:
stringbuffer.append("&quot;");
break;
case 60:
stringbuffer.append("&lt;");
break;
case 62:
stringbuffer.append("&gt;");
break;
case 38:
stringbuffer.append("&amp;");
break;
case 32:
stringbuffer.append("&#32;");
break;
case 10:
stringbuffer.append("<br>");
break;
case 8220:
stringbuffer.append("&ldquo;");
break;
case 8221:
stringbuffer.append("&rdquo;");
break;
default:
stringbuffer.append(c);
break;
}
}
return stringbuffer.toString();
}
}

View File

@@ -0,0 +1,323 @@
package com.nis.nmsclient.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
public class Utils {
static Logger logger = Logger.getLogger(Utils.class);
/**
* 获得本机IP
*
* @return
*/
public static String getLocalIp() {
String nativeip = "";
try {
//根据操作系统确定获取进程ID的方式
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
InetAddress ipv4 = InetAddress.getLocalHost();
nativeip = ipv4.getHostAddress().toString();
logger.debug("------getLocalIp--nativeip=" + nativeip);
}else if (os.startsWith("Linux")){
InetAddress ip = null;
boolean findIp = false;
// 根据网卡取本机配置的IP
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (findIp) {
break;
}
NetworkInterface ni = netInterfaces.nextElement();
logger.debug("------getLocalIp--NetWorkName=" + ni.getName());
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements())
ip = ips.nextElement();
logger.debug("------getLocalIp--ip.isSiteLocalAddress()="
+ ip.isSiteLocalAddress());
logger.debug("------getLocalIp--ip.isLoopbackAddress()="
+ ip.isLoopbackAddress());
logger.debug("------getLocalIp--HostAddress="
+ ip.getHostAddress());
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {
findIp = true;
logger.debug("------findIp--" + ip.getHostAddress());
break;
}
}
if (ip != null) {
nativeip = ip.getHostAddress();
}
} else {
throw new IOException("unknown operating system: " + os);
}
} catch (Exception e) {
logger.error(Utils.printExceptionStack(e));
}
return nativeip;
}
public static String getOperateSystem() {
BufferedReader bReader = null;
BufferedReader errorReader = null;
Process process = null;
try {
String os = System.getProperty("os.name");//根据操作系统确定运行方式
String[] cmdArr = null;
if (os.startsWith("Windows")) {
cmdArr = new String[] { "cmd.exe", "/C", "ver"};
} else if (os.startsWith("Linux")) {
cmdArr = new String[] { "/bin/bash", "-c", "uname -r;uname -i;lsb_release -d| cut -d: -f2| cut -f2" };
} else {
throw new IOException("unknown operating system: " + os);
}
process = Runtime.getRuntime().exec(cmdArr);
process.getOutputStream().close();
bReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
errorReader = new BufferedReader(new InputStreamReader(process
.getErrorStream()));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = bReader.readLine()) != null) {
if(line.trim().length()>0){
sb.append(line.trim() + ",");
logger.debug("getOperateSystem right-->" + line);
}
}
while ((line = errorReader.readLine()) != null) {
logger.debug("getOperateSystem error-->" + line);
}
if(sb.length() > 0) {
if (os.startsWith("Windows")) {
String osInfo = System.getProperty("sun.arch.data.model");//32位 or 64 位
sb.append(osInfo+"i18n_client.Utils.bit_n81i");
}else {
sb.deleteCharAt(sb.length()-1);//去掉最后一个逗号
}
}
return sb.toString();
} catch (Exception e) {
logger.error("Get the exception of the operating system and the release version", 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 checkIP(String checkStr) {
try {
String number = checkStr.substring(0, checkStr.indexOf('.'));
if (Integer.parseInt(number) > 255 || Integer.parseInt(number) < 0) {
return false;
}
checkStr = checkStr.substring(checkStr.indexOf('.') + 1);
number = checkStr.substring(0, checkStr.indexOf('.'));
if (Integer.parseInt(number) > 255 || Integer.parseInt(number) < 0) {
return false;
}
checkStr = checkStr.substring(checkStr.indexOf('.') + 1);
number = checkStr.substring(0, checkStr.indexOf('.'));
if (Integer.parseInt(number) > 255 || Integer.parseInt(number) < 0) {
return false;
}
number = checkStr.substring(checkStr.indexOf('.') + 1);
if (Integer.parseInt(number) > 255 || Integer.parseInt(number) < 0) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
//测试IP地址是否合法
public static 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;
}
}
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 void main(String argus[]){
System.out.println(Utils.checkIP("10.a.1.1"));
System.out.println(Utils.isIp("10.1.1.1"));
}
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();
}
/**
* 根据网口名称获取此网口的Ip地址
* @param ethName
* @return
*/
public static String getIpAddressByEthName(String ethName) {
String ip = null;
try {
//根据端口名称获取网卡信息
NetworkInterface netWork = NetworkInterface.getByName(ethName);
//获取此网卡的ip地址可能包含ipv4和ipv6
Enumeration<InetAddress> adds = netWork.getInetAddresses();
while (adds.hasMoreElements()) {
InetAddress add = adds.nextElement();
String ipStirng = add.getHostAddress();
// 如果Ip地址长度大于16说明是ipv6格式地址
if (ipStirng.length() > 16) {
continue;
}
ip = ipStirng;
}
} catch (Exception e) {
logger.error("Getting IP address failure based on port name",e);
}
return ip;
}
/**
* 根据ip地址查找端口的名字
*
* @param ip
* @return
*/
public static String getNetInterfaceNameByIp(String ip) {
byte[] ipArr = Utils.ipStringToByte(ip);
String name = null;
try {
NetworkInterface local = NetworkInterface
.getByInetAddress(InetAddress.getByAddress(ipArr));
name = local.getName();
} catch (Exception e) {
logger.error("Getting port name failure based on IP address", e);
}
return name;
}
/**
* 将string类型的ip地址转换成数组类型只支持ipv4
*
* @param ip
* @return
*/
public static byte[] ipStringToByte(String ip) {
if (!Utils.isIp(ip)) {
logger.error("IP is not legal, can not be converted!");
return null;
}
String[] ipArr = ip.split("\\.");
byte[] result = new byte[4];
for (int i = 0; i < ipArr.length; i++) {
int tem = Integer.parseInt(ipArr[i]);
result[i] = (byte) tem;
}
return result;
}
}

View File

@@ -0,0 +1,321 @@
package com.nis.nmsclient.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;
import com.nis.nmsclient.thread.socket.CommonSocket;
import com.nis.nmsclient.thread.socket.ServerCollectData;
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();
zipFile = null;
}
}
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; i<excludes.length; i++) {
File excudeFile = new File(excludes[i]);
excludes[i] = excudeFile.getCanonicalPath();
}
}
/* if(srcFile.isDirectory()){
zip(out,srcFile,srcFile.getName(), excludes);
}else{
zip(out,srcFile,"", excludes);
}*/
zip(out,srcFile,"", excludes);
}catch (Exception e) {
throw e;
}finally {
if(out!=null) out.close();
}
}
/**
* zip压缩
* @param out Zip压缩流
* @param srcFile 要压缩的目录或文件
* @param base 要压缩文件在zip包内的路径
* @throws Exception
*/
protected static void zip(ZipOutputStream out, File srcFile, String base, String[] excludes) throws Exception{
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(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 {
logger.debug("pass ZipUtil zipWithDelFile(File[] srcFiles, String destFile, boolean isAddPrefix):");
if(srcFiles!=null){
logger.debug("ZipUtil zipWithDelFile(File[] srcFiles, String destFile, boolean isAddPrefix) srcFiles:"+srcFiles.length);
}
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, null);
}
}catch (Exception e) {
throw e;
}finally {
if(out!=null) out.close();
}
}
/**
* 将单个文件或一个目录打成zip包并将原文件删除或移动
*
* @param srcFiles 需要压压缩的文件列表
* @param destFile 压缩后的目标文件
* @param isAddPrefix 是否要添加文件前缀
* @throws Exception
*/
public static void zipWithMoveFile(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");
}
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现现修改将任务结果和回传文件压缩时不删除文件而是将其移动到相应的日期目录
String dataType = null;
String destFileName = new File(destFile).getName();
if(destFileName.startsWith(CommonSocket.BP_TYPE_TASK_RESULT)){
dataType = CommonSocket.BP_TYPE_TASK_RESULT;
}else if(destFileName.startsWith(CommonSocket.BP_TYPE_TASK_RETURN)){
dataType = CommonSocket.BP_TYPE_TASK_RETURN;
}else if(destFileName.startsWith(CommonSocket.BP_TYPE_DETECT_DATA)){
dataType = CommonSocket.BP_TYPE_DETECT_DATA;
}
logger.debug("zipWithMoveFile --- dataType = " + dataType);
for(File file : srcFiles){
// 2013-5-6 针对监测数据打包文件个数的限制即srcFiles传来的是一定个数的文件不是所有监测的目录所以压缩文件中的文件名要加上文件的父目录名
if(CommonSocket.BP_TYPE_DETECT_DATA.equalsIgnoreCase(dataType) && file.isFile()){
zip(out, file, file.getParentFile().getName() + "_" + file.getName(), true, isAddPrefix, dataType);
}else{
zip(out,file,"", true, isAddPrefix, dataType);
}
}
}catch (Exception e) {
throw e;
}finally {
if(out!=null) out.close();
}
}
/**
* zip压缩
* @param out Zip压缩流
* @param srcFile 要压缩的文件或目录
* @param base 要压缩文件在zip包内的路径
* @param isDel 是否删除压缩的文件, 若dataType为null则直接删除若dataType不为null, 则移动到指定目录
* @param isAddPrefix 是否要添加文件前缀true 如果是文件则将父文件夹的名称加到文件名的前缀,作为压缩后的文件名
* @throws Exception
*/
protected static void zip(ZipOutputStream out, File srcFile, String base, boolean isDel, boolean isAddPrefix, String dataType) throws Exception{
if (!srcFile.exists()) {
// throw new Exception("压缩目录或文件不存在: " + srcFile.getAbsolutePath());
throw new Exception("Compressed directories or files do not exist: " + srcFile.getAbsolutePath());
}
logger.debug("pass ZipUtil zip");
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, dataType);
}
} 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){
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现现修改将任务结果和回传文件压缩时不删除文件而是将其移动到相应的日期目录
if(CommonSocket.BP_TYPE_TASK_RESULT.equalsIgnoreCase(dataType)){
ServerCollectData.moveTaskResultToDateDir(srcFile);
}else if(CommonSocket.BP_TYPE_TASK_RETURN.equalsIgnoreCase(dataType)){
ServerCollectData.moveTaskReturnToDateDir(srcFile, null);
}else if(CommonSocket.BP_TYPE_DETECT_DATA.equalsIgnoreCase(dataType)){
ServerCollectData.moveDetecDataToDateDir(srcFile);
}else {
FileUtil.delDir(srcFile);
logger.debug("ZipUtil.zip()----delete srcFile=" + srcFile.getAbsolutePath());
}
}
}
out.closeEntry();
}
public static void main(String[] args) {
try {
File f = new File("D:/work/create_tablespace");
System.out.println(f.getPath());
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);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,313 @@
package com.nis.nmsclient.util.file;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ResourceBundle;
/**
* <p>Title: BufferedRandomAccessFile</p>
* <p>Description: this class provide Buffered Read & Write by extend RandomAccessFile</p>
* <p>Copyright: Copyright (c) 2002 Cui Zhixiang </p>
* <p>Company: soho </p>
* @author Cui Zhixiang
* @version 1.0, 2002/10/12
*/
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)");
}
}

View File

@@ -0,0 +1,17 @@
package com.nis.nmsclient.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;
}
}

View File

@@ -0,0 +1,118 @@
package com.nis.nmsclient.util.io;
/**
version: 1.1 / 2007-01-25
- changed BOM recognition ordering (longer boms first)
Original pseudocode : Thomas Weidenfeller
Implementation tweaked: Aki Nieminen
http://www.unicode.org/unicode/faq/utf_bom.html
BOMs in byte length ordering:
00 00 FE FF = UTF-32, big-endian
FF FE 00 00 = UTF-32, little-endian
EF BB BF = UTF-8,
FE FF = UTF-16, big-endian
FF FE = UTF-16, little-endian
Win2k Notepad:
Unicode format = UTF-16LE
***/
import java.io.*;
/**
* 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();
}
}

View File

@@ -0,0 +1,120 @@
package com.nis.nmsclient.util.io;
/**
version: 1.1 / 2007-01-25
- changed BOM recognition ordering (longer boms first)
Original pseudocode : Thomas Weidenfeller
Implementation tweaked: Aki Nieminen
http://www.unicode.org/unicode/faq/utf_bom.html
BOMs:
00 00 FE FF = UTF-32, big-endian
FF FE 00 00 = UTF-32, little-endian
EF BB BF = UTF-8,
FE FF = UTF-16, big-endian
FF FE = UTF-16, little-endian
Win2k Notepad:
Unicode format = UTF-16LE
***/
import java.io.*;
/**
* 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);
}
}

View File

@@ -0,0 +1,14 @@
package com.nis.nmsclient.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);
}
}

View File

@@ -0,0 +1,14 @@
package com.nis.nmsclient.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);
}
}

View File

@@ -0,0 +1,49 @@
package com.nis.systeminfo.model;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class DetectInfo {
/*
* 该字段是针对,同一时间点多条具体数据(写多个文件),比如:网卡监测(多个网卡问题)
*/
private List<String> detailDatas = new LinkedList<String>();
/*
* 针对公共数据的描述信息
*/
private String descInfo;
/*
* 该字段是针对一条数据,关联的其他表的数据;写入到一个文件的: String 是解析标识, List<String[]>是多条数据String[]存入表中的数据)
* 如系统信息监测数据中有网卡数量网卡数量关联着其他表表中又有多个字段如网卡名称IP子网掩码等等
*/
private Map<String, List<String[]>> relatedDatas;
private String diskMsg;//用于存储硬盘只读和磁盘满的异常信息
public List<String> getDetailDatas() {
return detailDatas;
}
public void setDetailDatas(List<String> detailDatas) {
this.detailDatas = detailDatas;
}
public String getDescInfo() {
return descInfo;
}
public void setDescInfo(String descInfo) {
this.descInfo = descInfo;
}
public Map<String, List<String[]>> getRelatedDatas() {
return relatedDatas;
}
public void setRelatedDatas(Map<String, List<String[]>> relatedDatas) {
this.relatedDatas = relatedDatas;
}
public String getDiskMsg() {
return diskMsg;
}
public void setDiskMsg(String diskMsg) {
this.diskMsg = diskMsg;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,592 @@
package com.nis.systeminfo.thread;
import java.io.File;
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.nis.nmsclient.common.Contants;
import com.nis.nmsclient.common.SysConfig;
import com.nis.nmsclient.config.DetecConfOper;
import com.nis.nmsclient.model.AlarmInfo;
import com.nis.nmsclient.model.SetInfo;
import com.nis.nmsclient.thread.alarm.AlarmUtil;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileWrUtil;
import com.nis.nmsclient.util.Utils;
import com.nis.systeminfo.model.DetectInfo;
import com.nis.systeminfo.model.SystemInfo;
/**
* 客户端读取本机信息,并写入到.csv文件的具体实现体
*
*/
public class GetInfoRun implements Runnable{
Logger logger = Logger.getLogger(GetInfoRun.class);
private String name;
private SetInfo setInfo; // 当前线程读取的信息要存入的表如CPU信息表、内存信息表等
private Date startTime;// 服务启动时间
private List<AlarmInfo> alarmInfos;
private int diskUsageTime=0;
private int cpuUsageTime=0;
private String cpuUsageName="";
private String diskUsageName="";
private List<String> detailDatas ;
//暂存各告警字段连续超过告警值的次数
//public static Map<Long, Integer> alarmTimes = new HashMap<Long, Integer>();
//暂存各告警信息告警状态
//public static Map<Long, Boolean> alarmStates = new HashMap<Long, Boolean>();
private int alarmTimes = 0;
private boolean alarmState = false;
private String firstPerformData="";
public GetInfoRun(String name, SetInfo setInfo, Date startTime, List<AlarmInfo> alarmInfos) {
super();
this.name = name;
this.setInfo = setInfo;
this.startTime = startTime;
this.alarmInfos = alarmInfos;
//如果设置是process类型且设置名称是nmsclient即是自身则设置进程PID文件
if (Contants.SYS_CHECK_TYPE_PROCESS.equalsIgnoreCase(setInfo.getCheckTypeName())
&& Contants.SYS_CHECK_TYPE_PROCESS_NMSAGENT
.equalsIgnoreCase(setInfo.getProcessIden())) {
setInfo.setProcessFile(Contants.localAgentPidFile);
}
}
public void run() {
Thread.currentThread().setName(name);
Date writeDate = new Date();
int checkTimes = 0;//尝试次数
//初始化值
String filePath = Contants.localDataFilePath
+ File.separator
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(), setInfo
.getProcessIden(), null) + File.separator;
String fileName = DateUtil.getStingDate(DateUtil.YYYYMMDDHH24MMSS, writeDate) + ".csv";
// 针对进程是NC启动还是手动启动
boolean isAgentStart = ("0".equals(setInfo.getIsControlStart()) ? false : true);
do {
checkTimes++;
try {
/*
* 2013-4-11 数据顺序调整如下:
* 总数据本机IP,监测设置ID,监测类别,进程名称(监测类别设置名称),监测服务启动时间,检测时延(秒),本次检测时间,尝试次数,下次计划监测时间,
* 执行状态是否成功是否正常(用于报警:-1执行失败0不正常1正常,状态信息(描述信息),性能数据
* 详细信息details,详细信息条数(要入到对应的监测类别信息表的数据条数)
* 告警列序号,告警级别,告警值,监测具体数据信息(多列),,,,
*/
// detailDatas存放监测具体数据信息(可能多条,如多个网卡)descInfo存放状态信息
DetectInfo detectInfo = new SystemInfo().getDetectInfo(setInfo
.getCheckTypeName(), setInfo.getProcessFile(), setInfo
.getProcessPath(), setInfo.getProcessSearchKeyCode(), isAgentStart);
String checkDelayTime = getCheckDelayTime(writeDate);// 检测时延
// ------- 数据状态(告警)和详细监测数据组织
int totalStatus = Contants.DETECTION_STATUS_NORMAL;
int totalAlarmLevel = 99;
String totalShowNum ="";//告警序列号取告警级别高的showNum add by jinsj for 紧急告警
StringBuffer functionSb = new StringBuffer();
List<String[]> detailDataList = new LinkedList<String[]>();
if (detectInfo == null || detectInfo.getDetailDatas() == null || detectInfo.getDetailDatas().size() == 0) {// 执行失败
totalStatus = Contants.DETECTION_STATUS_FAILURE;
// detectInfo不为空即无具体的详细数据有描述信息目前是针对进程与系统时间
if (detectInfo != null){
// 如果是进程监测, 找不到进程或找到多个进程时告警如果时间差监测获取DC系统时间失败告警
String alarmMsg = detectInfo.getDescInfo();
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
new Date(), new Date(), 1, Contants.DETECTION_STATUS_FAILURE, alarmMsg);
}
logger.info("预设监测 setId: " + this.setInfo.getId() + " > "
+ this.setInfo.getCheckTypeName() + "_" + setInfo.getProcessIden() + ",获取数据失败");
}else if(!StringUtils.isBlank(detectInfo.getDiskMsg())){
//出现硬盘不可写或磁盘满
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
new Date(), new Date(), 0, Contants.DETECTION_STATUS_ABNORMAL, detectInfo.getDiskMsg());
return ;
}else {// 即正常生成详细监测数据
detailDatas = detectInfo.getDetailDatas();
for(int i=0; i<detailDatas.size(); i++){
String[] detailData = detailDatas.get(i).split(SystemInfo.SEPARATOR);
int index = 0;
String[] dataArr = new String[4 + detailData.length];
// 与报警字段值比较是否状态正常
String[] alarmArr = getAlarmState(detailData);
if (alarmArr != null && "".equals(alarmArr[0])) {// 记录的状态正常
dataArr[index++] = "";// 告警列序号
dataArr[index++] = "";// 告警级别
dataArr[index++] = "";// 告警值
} else {// 记录的状态不正常
totalStatus = Contants.DETECTION_STATUS_ABNORMAL;
dataArr[index++] = alarmArr[0];// 告警列序号
dataArr[index++] = alarmArr[1];// 告警级别
dataArr[index++] = alarmArr[2];// 告警值
if(!"".equals(alarmArr[4]) && totalAlarmLevel > Integer.parseInt(alarmArr[4])){// 将更高的告警级别赋给总告警级别
totalAlarmLevel = Integer.parseInt(alarmArr[4]);
totalShowNum = alarmArr[5];
}
}
if(detailDatas.size()>1 && alarmArr[3].length()>0){
functionSb.append("" + detailData[0] + "" + alarmArr[3] + "</br>");// 组织性能数据
}else{
functionSb.append(alarmArr[3]);// 组织性能数据
}
for (int j = 0; j < detailData.length; j++) {// 监测具体数据信息
dataArr[index + j] = detailData[j];
}
detailDataList.add(dataArr);
}
}
if(StringUtils.isNotBlank(firstPerformData)){
functionSb.insert(0,firstPerformData+"</br>");
}
// ------- 主动告警
String totalAlarmInfo = null;
if(totalStatus == Contants.DETECTION_STATUS_NORMAL){
// 本次数据正常,看原本的状态:若是告警状态,则发送恢复通知;若是正常状态,则不变
if(alarmState){
totalAlarmInfo = "【i18n_client.GetInfoRun.totalAlarmInfo1_n81i】</br>" + functionSb.toString();
}
alarmState = false;
alarmTimes = 0;
}else if(totalStatus == Contants.DETECTION_STATUS_ABNORMAL){//状态异常
alarmTimes ++;
if(!alarmState){// 本次数据不正常,看原本的状态:若是正常状态,则发送告警,此主动告警也只有一次,除非监测恢复正常,之后又异常
if(setInfo!=null && "process".equalsIgnoreCase(setInfo.getCheckTypeName())) {//如果是进程监测
totalAlarmInfo = functionSb.toString();
}else {
totalAlarmInfo = "【i18n_client.GetInfoRun.totalAlarmInfo2_n81i】</br>" + functionSb.toString();
}
}else if(alarmTimes == Contants.overAlarmValTimes && alarmState){//若原本的状态是告警状态则判断是否连续几次达到告警值次数会一直累积知道监测恢复正常所以如果一直异常则也只告警一次连续4次异常
totalAlarmInfo = "【i18n_client.GetInfoRun.totalAlarmInfo3_n81i】</br>" + functionSb.toString();
}
alarmState = true;
}
if(totalAlarmInfo!=null){
//2014-5-12 add根据配置的参数决定是否启用监测数据超过设定值时的主动告警
Boolean activeAlarmStart = Contants.ACTIIVE_ALARM_START;
if(totalStatus == Contants.DETECTION_STATUS_NORMAL ||//数据恢复主动告警
(activeAlarmStart && totalStatus == Contants.DETECTION_STATUS_ABNORMAL)) {//数据异常主动告警
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
new Date(), new Date(), totalAlarmLevel, totalStatus, totalAlarmInfo,totalShowNum);
}
}
// ------- 总监测数据组织
int index = 0;
String[] totalData = new String[12];
totalData[index++] = getUUID();// UUID
totalData[index++] = setInfo.getId() + "";// 监测设置ID
totalData[index++] = setInfo.getCheckTypeName();// 监测类别
totalData[index++] = setInfo.getProcessIden();// 进程名称
totalData[index++] = getStartClientTime();// 监测服务启动时间
totalData[index++] = checkDelayTime;// 检测时延(秒)
totalData[index++] = writeDate.getTime() + "";// 本次检测时间
totalData[index++] = checkTimes + "";// 尝试次数
totalData[index++] = getNextCheckTime(writeDate);// 下次计划监测时间
totalData[index++] = totalStatus + "";// 执行状态是否成功是否正常
totalData[index++] = (detectInfo == null ? "" : detectInfo.getDescInfo());// 状态信息(描述信息)
totalData[index++] = StringUtils.isBlank(functionSb.toString())?detectInfo.getDescInfo():functionSb.toString();// 性能数据
List<String[]> dataList = new LinkedList<String[]>();
// 总数据
dataList.add(totalData);
// 详细信息
dataList.add(new String[]{"details", detailDataList.size() + ""});// details(解析标识),详细信息条数
dataList.addAll(detailDataList);// 具体的详细数据
// 特殊数据定制(目前是针对系统信息监测systeminfo)
if (detectInfo!=null && detectInfo.getRelatedDatas() != null) {
Set<Map.Entry<String, List<String[]>>> set = detectInfo.getRelatedDatas().entrySet();
for(Map.Entry<String, List<String[]>> entry : set){
dataList.add(new String[]{entry.getKey(), entry.getValue().size() + ""});// 解析标识, 行数(当前解析标识指定的类型有多少条数据)
dataList.addAll(entry.getValue());// 多条数据
}
}
for(String[] val : dataList){
// logger.debug(Arrays.toString(val));//i18nlog
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
file = new File(filePath + fileName + ".tp");
FileWrUtil.csvFilePrinter(file, Contants.charset, dataList);
File file2 = new File(filePath + fileName);
file.renameTo(file2);
break;
} catch (Exception e) {
logger.error("Client write file" + fileName + "error: " + Utils.printExceptionStack(e));
continue;
}finally{
diskUsageTime=0;
cpuUsageTime=0;
cpuUsageName="";
diskUsageName="";
}
} while (checkTimes < setInfo.getCheckMaxTimes());
}
/**
* 获得当前的报警设置信息,与相应字段值比较
* 2014-5-15 update: 标识字段,标识
*/
private String[] getAlarmState(String[] sysData){
// strs数组数据依次为告警序列号、告警级别、告警值、性能数据、本次最高告警级别
String[] strs = new String[]{"", "", "", "", "",""};
if(alarmInfos==null){
return strs;
}
int maxAlarmLevel = 99;
String maxShowNum = "" ;
for (AlarmInfo alarm : alarmInfos) {
// logger.info("*****marker:"+alarm.getMarker());
if (sysData.length < alarm.getShowNum()) {// 得到的数据个数和告警列数比较
continue;
}
String data = sysData[alarm.getShowNum() - 1];
boolean alarmFlag = false;
/**
* 2014-5-15 add
* 1.指定了特定的标识:
* (1).当前标识非空:不在指定标识内,则不做告警判断;在,则做告警判断
* (2).当前标识为空:空不在指定标识内,不做告警判断
* 2.未指定特定的标识:所有标识都进行告警判断
*/
String marker = alarm.getMarker();
// Integer markerShowNum = 1;//先默认取第一个
Integer markerShowNum = alarm.getMarkerFiledShowNum();
logger.info("告警设置checkType|"+alarm.getCheckType()+" setInfoId|"+alarm.getSetInfoId()+" markerShowNum|"+alarm.getMarkerFiledShowNum()+" marker|"+alarm.getMarker());
if(markerShowNum!=null && markerShowNum>0 //若未指定标识字段则从DC传递到NC的json字符串中markerShowNum的值为0
&& StringUtils.isNotBlank(marker)) {
String markerCurVal = sysData[markerShowNum - 1];//当前条详细监测数据的标识符
String sperator = SysConfig.getStringVal("alarm.set.marker.separator", "|");
if(StringUtils.isNotBlank(markerCurVal)) {
if(!(sperator+marker.trim()+sperator).toLowerCase().contains((sperator+markerCurVal.trim()+sperator).toLowerCase())) {//当前标识不在指定的标识里
continue;
}
}else {
continue;
}
}
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());
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() + "|"; //告警值
//性能信息
if(setInfo!=null && "process".equalsIgnoreCase(setInfo.getCheckTypeName())) {//如果是进程监测
// strs[3] +="进程不存在 ";//如果不加\",则只显示进程不存,少一个字符,所以在最后加空格,这样就不会少显示一个字符了
strs[3] +="i18n_client.GetInfoRun.processNotExists_n81i ";//如果不加\",则只显示进程不存,少一个字符,所以在最后加空格,这样就不会少显示一个字符了
// strs[3] +="进程不存在进程PID文件"+setInfo.getProcessFile()+",进程搜索关键字:"+setInfo.getProcessSearchKeyCode()+"";
// }else if(setInfo!=null && "disk".equalsIgnoreCase(setInfo.getCheckTypeName()) && "磁盘是否可写".equals(alarm.getFiledCommonts())){
}else if(setInfo!=null && "disk".equalsIgnoreCase(setInfo.getCheckTypeName()) && "i18n_client.GetInfoRun.isDiskWriteAble_n81i".equals(alarm.getFiledCommonts())){
strs[3] += alarm.getFiledCommonts()+":not writable;";
}else {
// if(setInfo!=null && "disk".equalsIgnoreCase(setInfo.getCheckTypeName()) && "磁盘使用率(%)".equals(alarm.getFiledCommonts())){
if(setInfo!=null && "disk".equalsIgnoreCase(setInfo.getCheckTypeName()) && "i18n_client.GetInfoRun.diskUsed_n81i".equals(alarm.getFiledCommonts())){
diskUsageTime ++;
if("Linux".equals(System.getProperties().getProperty("os.name"))){
diskUsageName+=sysData[0]+",";
// firstPerformData = diskUsageTime+"个磁盘使用率超过"+alarm.getPoliceValue()+"%"+diskUsageName;
firstPerformData = diskUsageTime+"i18n_client.GetInfoRun.warning_n81i"+alarm.getPoliceValue()+"%"+diskUsageName;
}else{
diskUsageName+=sysData[0].substring(0,sysData[0].length()-2)+",";
// firstPerformData = diskUsageTime+"个磁盘使用率超过"+alarm.getPoliceValue()+"%"+diskUsageName.substring(0,diskUsageName.length()-1);
firstPerformData = diskUsageTime+"i18n_client.GetInfoRun.warning_n81i"+alarm.getPoliceValue()+"%"+diskUsageName.substring(0,diskUsageName.length()-1);
}
}
// if(setInfo!=null && "cpu".equalsIgnoreCase(setInfo.getCheckTypeName()) && "总的使用率(%)".equals(alarm.getFiledCommonts())){
if(setInfo!=null && "cpu".equalsIgnoreCase(setInfo.getCheckTypeName()) && "i18n_client.GetInfoRun.ZongShiYongLv_n81i".equals(alarm.getFiledCommonts())){
cpuUsageTime ++;//超过告警值得cpu数量
cpuUsageName+=sysData[0]+",";
// firstPerformData = cpuUsageTime+"个CPU的使用率占"+alarm.getPoliceValue()+"%"+cpuUsageName.substring(0,cpuUsageName.length()-1);
firstPerformData = cpuUsageTime+"i18n_client.GetInfoRun.cpuShiyonglv_n81i"+alarm.getPoliceValue()+"%"+cpuUsageName.substring(0,cpuUsageName.length()-1);
}
// 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_client.GetInfoRun.warningValue_n81i"
+ alarm.getPoliceValue()
+ "("+alarm.getPoliceUnit()+") " + " i18n_client.GetInfoRun.abnormal_n81i";
}
//2011-09-29 添加了连续几次达到告警值后主动告警,恢复正常后发送恢复信息
//2013-03-26 添加了告警状态控制是否立刻主动告警,如果已经告警则后期不发送告警信息。
//alarmHandler(alarm, strs[3]);
if (maxAlarmLevel > alarm.getPoliceLevel()) {// 保留本次最高告警级别,值越小级别越高
maxAlarmLevel = alarm.getPoliceLevel();
maxShowNum = alarm.getShowNum()+"";
}
}else{
// if(setInfo!=null && "disk".equalsIgnoreCase(setInfo.getCheckTypeName()) && "磁盘是否可写".equals(alarm.getFiledCommonts())){
// strs[3] += alarm.getFiledCommonts()+":可写;";
if(setInfo!=null && "disk".equalsIgnoreCase(setInfo.getCheckTypeName()) && "i18n_client.GetInfoRun.isDiskWriteAble_n81i".equals(alarm.getFiledCommonts())){
strs[3] += alarm.getFiledCommonts()+":i18n_client.GetInfoRun.writeAble_n81i;";
}else{
// 性能信息
strs[3] += alarm.getFiledCommonts()
+ sysData[alarm.getShowNum() - 1]
+ alarm.getPoliceUnit() + sysmbol + "i18n_client.GetInfoRun.warningValue_n81i"
+ alarm.getPoliceValue()
+ alarm.getPoliceUnit() + " i18n_client.GetInfoRun.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-2] = maxAlarmLevel + "";// 本次告警最高级别
strs[strs.length-1] = maxShowNum ;// 本次告警最高级别对应的序列号showNum
return strs;
}
private 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_client.GetInfoRun.gt_n81i";
}else if(">=".equals(oldSymbol)){
symbol = "i18n_client.GetInfoRun.out_n81i";
}else if("<".equals(oldSymbol)){
symbol = "i18n_client.GetInfoRun.lt_n81i";
}else if("<=".equals(oldSymbol)){
symbol = "i18n_client.GetInfoRun.in_n81i";
}else if("=".equals(oldSymbol)){
symbol = "i18n_client.GetInfoRun.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_client.GetInfoRun.in_n81i";
}else if(">=".equals(oldSymbol)){
symbol = "i18n_client.GetInfoRun.lt_n81i";
}else if("<".equals(oldSymbol)){
symbol = "i18n_client.GetInfoRun.out_n81i";
}else if("<=".equals(oldSymbol)){
symbol = "i18n_client.GetInfoRun.gt_n81i";
}else if("=".equals(oldSymbol)){
symbol = "i18n_client.GetInfoRun.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 void alarmHandler(AlarmInfo alarm, String alarmInfo){
// 判断告警状态是否变化,主动告警
synchronized (alarmStates) {
boolean isAlarm = false;
if(alarmStates.containsKey(alarm.getId())){
isAlarm = alarmStates.get(alarm.getId());
}
if(!isAlarm){
alarmStates.put(alarm.getId(), true);
// 报警
AlarmUtil.sendAlarmMsg(setInfo.getId(),
setInfo.getCheckTypeName(),
setInfo.getProcessIden(), new Date(),
new Date(), alarm.getPoliceLevel(),
Contants.DETECTION_STATUS_ABNORMAL, alarmInfo);
logger.debug("主动告警:" + alarmInfo);
logger.debug("------id=" + alarm.getId() + "---Comments="+alarm.getFiledCommonts()+"---val=-1");
}
}
// 判断连续达到告警值的次数,达到一定次数,主动告警
synchronized (alarmTimes) {
if(alarmTimes.containsKey(alarm.getId())){
int alarmCnt = alarmTimes.get(alarm.getId());
if(alarmCnt + 1 >= Contants.overAlarmValTimes){//连续几次达到告警值
String alarmMsg = alarm.getFiledCommonts() + ", 连续" + (alarmCnt + 1) + "次达到告警值";
AlarmUtil.sendAlarmMsg(setInfo.getId(),
setInfo.getCheckTypeName(),
setInfo.getProcessIden(), new Date(),
new Date(), alarm.getPoliceLevel(),
Contants.DETECTION_STATUS_ABNORMAL, alarmMsg);
logger.debug("主动告警:"+alarmMsg);
alarmTimes.put(alarm.getId(), -1);
logger.debug("------id=" + alarm.getId() + "---Comments="+alarm.getFiledCommonts()+"---val=-1");
}else if(alarmCnt > -1){
alarmTimes.put(alarm.getId(), alarmCnt + 1);
logger.debug("------id=" + alarm.getId() + "---Comments="+alarm.getFiledCommonts()+"---val="+(alarmCnt + 1));
}
}else{
alarmTimes.put(alarm.getId(), 1);
logger.debug("---nokey---id=" + alarm.getId() + "---Comments="+alarm.getFiledCommonts()+"---val=1");
}
}
}
// 判断是否恢复正常
private void alarmRecovery(AlarmInfo alarm, String alarmInfo){
// 判断是否告警状态,恢复正常
synchronized (alarmStates) {
if(alarmStates.containsKey(alarm.getId())){
boolean isAlarm = alarmStates.get(alarm.getId());
if(isAlarm){
alarmStates.put(alarm.getId(), false);
// 报警状态:恢复正常
AlarmUtil.sendAlarmMsg(setInfo.getId(),
setInfo.getCheckTypeName(),
setInfo.getProcessIden(), new Date(),
new Date(), 99, Contants.DETECTION_STATUS_NORMAL,
alarmInfo);
logger.debug(alarmInfo + ", 恢复正常");
logger.debug("------id=" + alarm.getId() + "---Comments="+alarm.getFiledCommonts()+"---val=0");
}
}
}
// 判断是否连续几次达到告警值后主动告警,恢复正常
synchronized (alarmTimes) {
if(alarmTimes.containsKey(alarm.getId())){
if(alarmTimes.get(alarm.getId()) == -1){
AlarmUtil.sendAlarmMsg(setInfo.getId(),
setInfo.getCheckTypeName(),
setInfo.getProcessIden(), new Date(),
new Date(), 99, Contants.DETECTION_STATUS_NORMAL,
alarm.getFiledCommonts() + ", 恢复正常");
logger.debug(alarm.getFiledCommonts() + ", 恢复正常");
}
alarmTimes.put(alarm.getId(), 0);
logger.debug("------id=" + alarm.getId() + "---Comments="+alarm.getFiledCommonts()+"---val=0");
}
}
}*/
private String getUUID(){
return Contants.AGENT_HOST_UUID + ""; //Utils.getLocalIp();
}
// 检测时延(秒)
private String getCheckDelayTime(Date now){
long val = System.currentTimeMillis() - now.getTime();
return val/1000 + "";
}
// 下一次检测时间
private String getNextCheckTime(Date now){
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.MINUTE, setInfo.getCheckGap().intValue());
return cal.getTimeInMillis() + "";
}
// 服务监控开始时间
private String getStartClientTime(){
return this.startTime.getTime() + "";
}
// 开机时间
private String getStartComputerTime(){
return SystemInfo.getStartComputerTime();
}
public SetInfo getSetInfo() {
return setInfo;
}
public void setSetInfo(SetInfo setInfo) {
this.setInfo = setInfo;
}
public List<AlarmInfo> getAlarmInfos() {
return alarmInfos;
}
public void setAlarmInfos(List<AlarmInfo> alarmInfos) {
this.alarmInfos = alarmInfos;
}
public String getFirstPerformData() {
return firstPerformData;
}
public void setFirstPerformData(String firstPerformData) {
this.firstPerformData = firstPerformData;
}
}

View File

@@ -0,0 +1,476 @@
package com.nis.systeminfo.thread;
import java.io.File;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.common.SysConfig;
import com.nis.nmsclient.model.AlarmInfo;
import com.nis.nmsclient.model.SetInfo;
import com.nis.nmsclient.thread.alarm.AlarmUtil;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileWrUtil;
public class NewPluginResultMerge {
private static final Logger logger = Logger.getLogger(NewPluginResultMerge.class);
private static final File srcRootDir = new File(Contants.localTempDataIncomingPath);
private static final File tarRootDir = new File(Contants.localDataFilePath);
private static HashMap<Long, Boolean> historyAlarmStateMap = new HashMap<Long, Boolean>();
private static HashMap<Long, Integer> historyAlarmTimesMap = new HashMap<Long, Integer>();
public void clearTmpFile(SetInfo setInfo) {
try {
String subDir = setInfo.getCheckTypeName() + "_" + setInfo.getProcessIden();
File dir = new File(srcRootDir, subDir);
if(!dir.exists()) {
dir.mkdirs(); // 临时目录不存在
return;
}
File[] results = dir.listFiles();
for (File file : results) {
file.delete();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
public void merge(SetInfo setInfo) {
try {
logger.debug("合并临时结果文件开始 ~~~~~~~");
String subDir = setInfo.getCheckTypeName() + "_" + setInfo.getProcessIden();
File dir = new File(srcRootDir, subDir);
File tarDir = new File(tarRootDir, subDir);
/*
* 临时目录为空,当前检测未生成结果数据
*/
if (!dir.exists() || dir.listFiles().length == 0) {
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
// new Date(), new Date(), 1, Contants.DETECTION_STATUS_FAILURE, "监测数据未生成");
new Date(), new Date(), 1, Contants.DETECTION_STATUS_FAILURE, "i18n_client.NewPluginResultMerge.detecateData_n81i");
return;
}
// 监测信息报警相关信息
System.out.println(Common.detecAlarmInfoMap);
List<AlarmInfo> alarmInfoList = Common.getDetecAlarmInfo(setInfo.getId());
// 获取临时结果文件
File srcFile = dir.listFiles()[0];
// 从文件名获取监测时间毫秒数YYYYMMDDhhmmss.tmp
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(srcFile.getName().substring(0, 4)));
cal.set(Calendar.MONTH, Integer.parseInt(srcFile.getName().substring(4, 6)) - 1);
cal.set(Calendar.DATE, Integer.parseInt(srcFile.getName().substring(6, 8)));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(srcFile.getName().substring(8, 10)));
cal.set(Calendar.MINUTE, Integer.parseInt(srcFile.getName().substring(10, 12)));
cal.set(Calendar.SECOND, Integer.parseInt(srcFile.getName().substring(12, 14)));
Long detecTime = cal.getTimeInMillis();
Date writeDate = new Date(detecTime); // 临时文件写入时间
//读取临时结果文件
List<String[]> lines = FileWrUtil.csvFileParser(srcFile, Contants.charset);
/* 临时数据文件新格式形式
* line1: 监测时延(秒),尝试次数,状态信息(描述信息),字段1,字段2,···
*
* 旧格式转新格式
*/
if(lines.size() == 1 && lines.get(0).length > 3) {
String[] data = lines.get(0);
lines.clear();
lines.add((String[]) ArrayUtils.subarray(data, 0, 3));
lines.add(new String[]{"details","1"});
lines.add((String[]) ArrayUtils.subarray(data, 3, data.length));
}
/*
* 临时数据文件新格式形式
* line1: 监测时延(秒),尝试次数,状态信息(描述信息)
* line2: details,具体数据条数
* line3: 监测字段1,监测字段2,···
* ··· ··· ···
*
* 任务执行失败状态判断(临时文件存在时,告警并删除临时文件):
* 1、公共信息不完整2、无具体数据信息或具体数据信息为空
*/
boolean execFail = false;
if(lines.size() < 1 || lines.get(0).length < 3) { // 公共信息不完整
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",公共信息不完整,监测执行错误");
execFail = true;
}
if(lines.size() < 3) { // 无具体数据信息
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",无具体数据信息,监测执行错误");
execFail = true;
}
if(lines.size() >= 3) { // 具体数据信息为空字段
String detailStr = Arrays.toString(lines.get(1));
String detailReg = "^\\[details,\\s*(\\d+)\\s*\\]$";
if(detailStr.matches(detailReg)) {
int endNum = Integer.parseInt(detailStr.replaceAll(detailReg, "$1")); // details格式校验
if (endNum < 1) {
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",具体数据信息格式错误,监测执行错误");
execFail = true;
}
for (int r = 0; r < endNum; r++) {
String[] sysData = lines.get(r + 2);
if (Arrays.toString(sysData).matches("^\\[[,|\\s]*\\]$")) { // 空字段
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",具体数据信息为空字段,监测执行错误");
execFail = true;
}
}
} else {
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",具体数据信息格式错误,监测执行错误");
execFail = true;
}
}
if(execFail) {
historyAlarmStateMap.put(setInfo.getId(), true); // 数据状态不正常
srcFile.delete(); // 删除错误临时文件,并告警
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
// new Date(detecTime), new Date(detecTime), 1, Contants.DETECTION_STATUS_FAILURE, "监测数据未生成或数据格式错误");
new Date(detecTime), new Date(detecTime), 1, Contants.DETECTION_STATUS_FAILURE, "i18n_client.NewPluginResultMerge.formatErr_n81i");
return;
}
String checkDelayTime = lines.get(0)[0];// 检测时延(秒)
String checkTimes = lines.get(0)[1]; // 尝试次数
String detectInfo = lines.get(0)[2]; // 状态信息(描述信息)
StringBuffer functionSb = new StringBuffer(); // 性能数据
// 执行状态,告警级别
int totalStatus = Contants.DETECTION_STATUS_ABNORMAL; //监测信息不正常
int totalAlarmLevel = 99;
String totalShowNum ="";//告警序列号取告警级别高的showNum add by jinsj for 紧急告警
if(lines.size() > 1) {
String[] detail = lines.get(1);
if("details".equalsIgnoreCase(detail[0]) && detail[1].trim().matches("\\d+")) {
int endNum = StringUtils.isBlank(detail[1])?0:Integer.parseInt(detail[1].trim());
if(endNum > 0) { // 详细信息条数大于0且所有数据不超过告警值时监测信息正常
totalStatus = Contants.DETECTION_STATUS_NORMAL; // 监测信息正常
for (int r = 0; r < endNum; r++) {
String[] sysData = lines.get(r + 2);
String[] warninfos = getAlarmState(alarmInfoList, sysData);
String[] combine = new String[sysData.length + 3]; // 组合详细信息
System.arraycopy(warninfos, 0, combine, 0, 3);
System.arraycopy(sysData, 0, combine, 3, sysData.length);
lines.set(r + 2, combine);
if(StringUtils.isNotBlank(warninfos[4])) {
totalAlarmLevel = Integer.parseInt(warninfos[4]);
}
totalShowNum = warninfos[5];
if(StringUtils.isNotBlank(warninfos[0])) {
totalStatus = Contants.DETECTION_STATUS_ABNORMAL; // 监测信息不正常
functionSb.append(warninfos[3] + "</br>"); // 组织性能数据
} else {
if(StringUtils.isNotBlank(warninfos[3])) {
functionSb.append(warninfos[3] + "</br>"); // 组织性能数据
}
}
}
}
}
}
// 主动告警
String totalAlarmInfo = null;
if(totalStatus == Contants.DETECTION_STATUS_NORMAL){
historyAlarmStateMap.put(setInfo.getId(), false);
historyAlarmTimesMap.put(setInfo.getId(), 0);
// 本次数据正常,看原本的状态:若是告警状态,则发送恢复通知;若是正常状态,则不变
if(getHistoryAlarmState(setInfo.getId())){
totalAlarmInfo = "【i18n_client.NewPluginResultMerge.totalAlarmInfo1_n81i】</br>" + functionSb.toString();
}
}else if(totalStatus == Contants.DETECTION_STATUS_ABNORMAL){//状态异常
historyAlarmStateMap.put(setInfo.getId(), true);
historyAlarmTimesMap.put(setInfo.getId(), getHistoryAlarmTimes(setInfo.getId()) + 1);
// 本次数据不正常,看原本的状态:若是正常状态,则发送告警,此主动告警也只有一次,除非监测恢复正常,之后又异常
if(getHistoryAlarmTimes(setInfo.getId()) == 1) {//如果是进程监测
totalAlarmInfo = "【i18n_client.NewPluginResultMerge.totalAlarmInfo2_n81i】</br>" + functionSb.toString();
}
if(getHistoryAlarmTimes(setInfo.getId()) == Contants.overAlarmValTimes) {//若原本的状态是告警状态则判断是否连续几次达到告警值次数会一直累积知道监测恢复正常所以如果一直异常则也只告警一次连续4次异常
totalAlarmInfo = "【i18n_client.NewPluginResultMerge.totalAlarmInfo3_n81i】</br>" + functionSb.toString();
}
}
if(totalAlarmInfo!=null){
//2014-5-12 add根据配置的参数决定是否启用监测数据超过设定值时的主动告警
Boolean activeAlarmStart = Contants.ACTIIVE_ALARM_START;
if(totalStatus == Contants.DETECTION_STATUS_NORMAL || //数据恢复主动告警
(activeAlarmStart && totalStatus == Contants.DETECTION_STATUS_ABNORMAL)) { //数据异常主动告警
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
new Date(), new Date(), totalAlarmLevel, totalStatus, totalAlarmInfo,totalShowNum);
}
}
// 组织监测数据组织
List<String[]> dataList = new LinkedList<String[]>();
// 总数据
String[] totalData = new String[12];
int index = 0;
totalData[index++] = Contants.AGENT_HOST_UUID + ""; // UUID
totalData[index++] = setInfo.getId() + ""; // 监测设置ID
totalData[index++] = setInfo.getCheckTypeName(); // 监测类别
totalData[index++] = setInfo.getProcessIden(); // 进程名称
totalData[index++] = setInfo.getPlanCheckTime() + ""; // 监测服务启动时间
totalData[index++] = checkDelayTime; // 检测时延(秒)
totalData[index++] = writeDate.getTime() + ""; // 本次检测时间
totalData[index++] = checkTimes; // 尝试次数
totalData[index++] = getNextCheckTime(writeDate, setInfo);// 下次计划监测时间
totalData[index++] = totalStatus + "";// 执行状态是否成功是否正常
totalData[index++] = detectInfo; // 状态信息(描述信息)
totalData[index++] = StringUtils.isBlank(functionSb.toString()) ? detectInfo : functionSb.toString();// 性能数据
dataList.add(totalData);
// 详细信息
dataList.addAll(lines.subList(1, lines.size()));
// 写入文件
String fileName = DateUtil.getStingDate(DateUtil.YYYYMMDDHH24MMSS, writeDate) + ".csv";
File tarFile1 = new File(tarDir, fileName + ".tp");
FileWrUtil.csvFilePrinter(tarFile1, Contants.charset, dataList);
File tarFile2 = new File(tarDir, fileName);
if (tarFile2.exists())
tarFile2.delete();
tarFile1.renameTo(tarFile2);
srcFile.delete();
logger.info("生成监测结果文件:" + tarFile2.getParentFile().getName() + "/" + tarFile2.getName());
// 忽略NC停止时间内的监测周期
// setInfo.setLastMergeDetecTime(detecTime);
setInfo.setLastMergeDetecTime(Math.max(detecTime, setInfo.getLastMergeDetecTime()));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
// 下一次检测时间
private String getNextCheckTime(Date now, SetInfo setInfo) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.MINUTE, setInfo.getCheckGap().intValue());
return cal.getTimeInMillis() + "";
}
/**
* 获取当前行告警信息
* @param sysData
* @return
*/
private String[] getAlarmState(List<AlarmInfo> alarmInfos, String[] sysData) {
// strs数组数据依次为告警序列号、告警级别、告警值、性能数据、本次最高告警级别
String[] strs = new String[] { "", "", "", "", "", "" };
if (alarmInfos == null) {
return strs;
}
int maxAlarmLevel = 99;
String maxShowNum = "";
for (AlarmInfo alarm : alarmInfos) {
if (sysData.length < alarm.getShowNum()) {// 得到的数据个数和告警列数比较
continue;
}
String data = sysData[alarm.getShowNum() - 1];
boolean alarmFlag = false;
/**
* 2014-5-15 add 1.指定了特定的标识: (1).当前标识非空:不在指定标识内,则不做告警判断;在,则做告警判断
* (2).当前标识为空:空不在指定标识内,不做告警判断 2.未指定特定的标识:所有标识都进行告警判断
*/
String marker = alarm.getMarker();
// Integer markerShowNum = 1;//先默认取第一个
Integer markerShowNum = alarm.getMarkerFiledShowNum();
logger.info("告警设置checkType|" + alarm.getCheckType() + " setInfoId|" + alarm.getSetInfoId()
+ " markerShowNum|" + alarm.getMarkerFiledShowNum() + " marker|" + alarm.getMarker());
if (markerShowNum != null && markerShowNum > 0 // 若未指定标识字段则从DC传递到NC的json字符串中markerShowNum的值为0
&& StringUtils.isNotBlank(marker)) {
String markerCurVal = sysData[markerShowNum - 1];// 当前条详细监测数据的标识符
String sperator = SysConfig.getStringVal("alarm.set.marker.separator", "|");
if (StringUtils.isNotBlank(markerCurVal)) {
if (!(sperator + marker.trim() + sperator).toLowerCase().contains(
(sperator + markerCurVal.trim() + sperator).toLowerCase())) {// 当前标识不在指定的标识里
continue;
}
} else {
continue;
}
}
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());
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_client.NewPluginResultMerge.warningValue_n81i"
+ alarm.getPoliceValue() + "(" + alarm.getPoliceUnit()
+ ") " + "i18n_client.NewPluginResultMerge.abnormal_n81i";
} 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_client.NewPluginResultMerge.warningValue_n81i"
+ alarm.getPoliceValue() + "(" + alarm.getPoliceUnit()
+ ") " + "i18n_client.NewPluginResultMerge.normal_n81i";
}
// 2011-09-29 添加了连续几次达到告警值后主动告警,恢复正常后发送恢复信息
// 2013-03-26 添加了告警状态控制是否立刻主动告警,如果已经告警则后期不发送告警信息。
if (maxAlarmLevel > alarm.getPoliceLevel()) {// 保留本次最高告警级别,值越小级别越高
maxAlarmLevel = alarm.getPoliceLevel();
maxShowNum = alarm.getShowNum() + "";
}
}// 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 - 2] = maxAlarmLevel + "";// 本次告警最高级别
strs[strs.length - 1] = maxShowNum;// 本次告警最高级别对应的序列号showNum
return strs;
}
private 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;
// }
// } 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 = "不等于";
// } else if ("equals".equalsIgnoreCase(oldSymbol)) {
// symbol = "not equals";
// } else if ("include".equalsIgnoreCase(oldSymbol)) {
// symbol = "exclude";
// } else if ("exclude".equalsIgnoreCase(oldSymbol)) {
// symbol = "include";
// }
// }
if (alarmFlag) {
if (">".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.gt_n81i";
} else if (">=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.out_n81i";
} else if ("<".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.lt_n81i";
} else if ("<=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.in_n81i";
} else if ("=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.eq_n81i";
} else {
symbol = oldSymbol;
}
} else {
if (">".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.in_n81i";
} else if (">=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.lt_n81i";
} else if ("<".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.out_n81i";
} else if ("<=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.gt_n81i";
} else if ("=".equals(oldSymbol)) {
symbol = "i18n_client.GetInfoRun.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 boolean getHistoryAlarmState(Long setInfoId) {
Boolean alarmState = historyAlarmStateMap.get(setInfoId);
return (alarmState != null && alarmState == true);
}
private int getHistoryAlarmTimes(Long setInfoId) {
Integer alarmTimes = historyAlarmTimesMap.get(setInfoId);
return (alarmTimes != null ? alarmTimes : 0);
}
}

View File

@@ -0,0 +1,451 @@
package com.nis.systeminfo.thread;
import java.io.File;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.nis.nmsclient.NmsClient;
import com.nis.nmsclient.common.Common;
import com.nis.nmsclient.common.Contants;
import com.nis.nmsclient.common.SysConfig;
import com.nis.nmsclient.model.AlarmInfo;
import com.nis.nmsclient.model.SetInfo;
import com.nis.nmsclient.thread.alarm.AlarmUtil;
import com.nis.nmsclient.util.DateUtil;
import com.nis.nmsclient.util.FileWrUtil;
/**
* 临时文件的合并过程替换为NewPluginResultMerge
*/
@Deprecated
public class PluginResultMerge {
private static final Logger logger = Logger.getLogger(PluginResultMerge.class);
private static final File srcRootDir = new File(Contants.localTempDataIncomingPath);
private static final File tarRootDir = new File(Contants.localDataFilePath);
private static HashMap<Long, Boolean> historyAlarmStateMap = new HashMap<Long, Boolean>();
private static HashMap<Long, Integer> historyAlarmTimesMap = new HashMap<Long, Integer>();
public void merge() {
try {
logger.debug("合并临时结果文件开始 ~~~~~~~");
Collection<SetInfo> setInfos = Common.getPluginDetecSetInfos();
System.out.println(setInfos.size());
for (SetInfo setInfo : setInfos) {
String subDir = setInfo.getCheckTypeName() + "_" + setInfo.getProcessIden();
File dir = new File(srcRootDir, subDir);
File tarDir = new File(tarRootDir, subDir);
if (!dir.exists() || dir.listFiles().length == 0) {
continue;
}
// 监测信息报警相关信息
System.out.println(Common.detecAlarmInfoMap);
List<AlarmInfo> alarmInfoList = Common.getDetecAlarmInfo(setInfo.getId());
// 相邻的连个结果文件超过两个监测周期时,该时间段内出现未生成数据结果的周期
Long period = setInfo.getCheckGap() * (60 * 1000); // millisecond
Long detecTimeout = period * 2;
File[] results = dir.listFiles();
Arrays.sort(results, new Comparator<File>() { // 同一类结果文件按按时间先后排序
public int compare(File f1, File f2) {
return f1.getName().compareTo(f2.getName());
}
});
for (File srcFile : results) {
// 从文件名获取监测时间毫秒数YYYYMMDDhhmmss.tmp
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(srcFile.getName().substring(0, 4)));
cal.set(Calendar.MONTH, Integer.parseInt(srcFile.getName().substring(4, 6)) - 1);
cal.set(Calendar.DATE, Integer.parseInt(srcFile.getName().substring(6, 8)));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(srcFile.getName().substring(8, 10)));
cal.set(Calendar.MINUTE, Integer.parseInt(srcFile.getName().substring(10, 12)));
cal.set(Calendar.SECOND, Integer.parseInt(srcFile.getName().substring(12, 14)));
Long detecTime = cal.getTimeInMillis();
Date writeDate = new Date(detecTime); // 临时文件写入时间
while (setInfo.getLastMergeDetecTime() + detecTimeout < detecTime) {
// 存在未生成数据结果的监测周期(每个周期记录一次)
historyAlarmStateMap.put(setInfo.getId(), true); // 数据状态不正常
// 未生成监测数据主动报警AlarmThread连续四次未生成监测数据报警
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
// new Date(detecTime), new Date(detecTime), 1, Contants.DETECTION_STATUS_FAILURE, "监测数据未生成");
new Date(detecTime), new Date(detecTime), 1, Contants.DETECTION_STATUS_FAILURE, "i18n_client.NewPluginResultMerge.detecateData_n81i");
// TODO 存在主动告警发送延时问题,可能会有多次告警连续发送
setInfo.setLastMergeDetecTime(setInfo.getLastMergeDetecTime() + period);
}
//读取临时结果文件
List<String[]> lines = FileWrUtil.csvFileParser(srcFile, Contants.charset);
/*
* 任务执行失败状态判断(临时文件存在时,告警并删除临时文件):
* 1、公共信息不完整2、无具体数据信息或具体数据信息为空
*/
boolean execFail = false;
if(lines.size() < 1 || lines.get(0).length < 3) { // 公共信息不完整
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",公共信息不完整,监测执行错误");
execFail = true;
}
if(lines.size() < 3) { // 无具体数据信息
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",无具体数据信息,监测执行错误");
execFail = true;
}
if(lines.size() >= 3) { // 具体数据信息为空字段
String detailStr = Arrays.toString(lines.get(1));
String detailReg = "^\\[details, (\\d+)\\]$";
if(detailStr.matches(detailReg)) {
int endNum = Integer.parseInt(detailStr.replaceAll(detailReg, "$1")); // details格式校验
if (endNum < 1) {
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",具体数据信息格式错误,监测执行错误");
execFail = true;
}
for (int r = 0; r < endNum; r++) {
String[] sysData = lines.get(r + 2);
if (Arrays.toString(sysData).matches("^\\[[,|\\s]*\\]$")) { // 空字段
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",具体数据信息为空字段,监测执行错误");
execFail = true;
}
}
} else {
logger.info("预设监测 setId: " + setInfo.getId() + " > " + srcFile.getName() + ",具体数据信息格式错误,监测执行错误");
execFail = true;
}
}
if(execFail) {
historyAlarmStateMap.put(setInfo.getId(), true); // 数据状态不正常
srcFile.delete(); // 删除错误临时文件,并告警
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
// new Date(detecTime), new Date(detecTime), 1, Contants.DETECTION_STATUS_FAILURE, "监测数据未生成");
new Date(detecTime), new Date(detecTime), 1, Contants.DETECTION_STATUS_FAILURE, "i18n_client.NewPluginResultMerge.detecateData_n81i");
continue;
}
String checkDelayTime = lines.get(0)[0];// 检测时延(秒)
String checkTimes = lines.get(0)[1]; // 尝试次数
String detectInfo = lines.get(0)[2]; // 状态信息(描述信息)
StringBuffer functionSb = new StringBuffer(); // 性能数据
// 执行状态,告警级别
int totalStatus = Contants.DETECTION_STATUS_ABNORMAL; //监测信息不正常
int totalAlarmLevel = 99;
String totalShowNum ="";//告警序列号取告警级别高的showNum add by jinsj for 紧急告警
if(lines.size() > 1) {
String[] detail = lines.get(1);
if("details".equalsIgnoreCase(detail[0]) && detail[1].trim().matches("\\d+")) {
int endNum = StringUtils.isBlank(detail[1])?0:Integer.parseInt(detail[1].trim());
if(endNum > 0) { // 详细信息条数大于0且所有数据不超过告警值时监测信息正常
totalStatus = Contants.DETECTION_STATUS_NORMAL; // 监测信息正常
for (int r = 0; r < endNum; r++) {
String[] sysData = lines.get(r + 2);
String[] warninfos = getAlarmState(alarmInfoList, sysData);
String[] combine = new String[sysData.length + 3]; // 组合详细信息
System.arraycopy(warninfos, 0, combine, 0, 3);
System.arraycopy(sysData, 0, combine, 3, sysData.length);
lines.set(r + 2, combine);
totalAlarmLevel = Integer.parseInt(warninfos[4]);
totalShowNum = warninfos[5];
if(StringUtils.isNotBlank(warninfos[0])) {
totalStatus = Contants.DETECTION_STATUS_ABNORMAL; // 监测信息不正常
functionSb.append(warninfos[3] + "</br>"); // 组织性能数据
} else {
if(StringUtils.isNotBlank(warninfos[3])) {
functionSb.append(warninfos[3] + "</br>"); // 组织性能数据
}
}
}
}
}
}
// 主动告警
String totalAlarmInfo = null;
if(totalStatus == Contants.DETECTION_STATUS_NORMAL){
historyAlarmStateMap.put(setInfo.getId(), false);
historyAlarmTimesMap.put(setInfo.getId(), 0);
// 本次数据正常,看原本的状态:若是告警状态,则发送恢复通知;若是正常状态,则不变
if(getHistoryAlarmState(setInfo.getId())){
totalAlarmInfo = "【i18n_client.NewPluginResultMerge.totalAlarmInfo1_n81i】</br>" + functionSb.toString();
}
}else if(totalStatus == Contants.DETECTION_STATUS_ABNORMAL){//状态异常
historyAlarmStateMap.put(setInfo.getId(), true);
historyAlarmTimesMap.put(setInfo.getId(), getHistoryAlarmTimes(setInfo.getId()) + 1);
// 本次数据不正常,看原本的状态:若是正常状态,则发送告警,此主动告警也只有一次,除非监测恢复正常,之后又异常
if(getHistoryAlarmTimes(setInfo.getId()) == 1) {//如果是进程监测
totalAlarmInfo = "【i18n_client.NewPluginResultMerge.totalAlarmInfo2_n81i】</br>" + functionSb.toString();
}
if(getHistoryAlarmTimes(setInfo.getId()) == Contants.overAlarmValTimes) {//若原本的状态是告警状态则判断是否连续几次达到告警值次数会一直累积知道监测恢复正常所以如果一直异常则也只告警一次连续4次异常
totalAlarmInfo = "【i18n_client.NewPluginResultMerge.totalAlarmInfo3_n81i】</br>" + functionSb.toString();
}
}
if(totalAlarmInfo!=null){
//2014-5-12 add根据配置的参数决定是否启用监测数据超过设定值时的主动告警
Boolean activeAlarmStart = Contants.ACTIIVE_ALARM_START;
if(totalStatus == Contants.DETECTION_STATUS_NORMAL || //数据恢复主动告警
(activeAlarmStart && totalStatus == Contants.DETECTION_STATUS_ABNORMAL)) { //数据异常主动告警
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo.getCheckTypeName(), setInfo.getProcessIden(),
new Date(), new Date(), totalAlarmLevel, totalStatus, totalAlarmInfo,totalShowNum);
}
}
// 组织监测数据组织
List<String[]> dataList = new LinkedList<String[]>();
// 总数据
String[] totalData = new String[12];
int index = 0;
totalData[index++] = Contants.AGENT_HOST_UUID + ""; // UUID
totalData[index++] = setInfo.getId() + ""; // 监测设置ID
totalData[index++] = setInfo.getCheckTypeName(); // 监测类别
totalData[index++] = setInfo.getProcessIden(); // 进程名称
totalData[index++] = setInfo.getPlanCheckTime() + ""; // 监测服务启动时间
totalData[index++] = checkDelayTime; // 检测时延(秒)
totalData[index++] = writeDate.getTime() + ""; // 本次检测时间
totalData[index++] = checkTimes; // 尝试次数
totalData[index++] = getNextCheckTime(writeDate, setInfo);// 下次计划监测时间
totalData[index++] = totalStatus + "";// 执行状态是否成功是否正常
totalData[index++] = detectInfo; // 状态信息(描述信息)
totalData[index++] = StringUtils.isBlank(functionSb.toString()) ? detectInfo : functionSb.toString();// 性能数据
dataList.add(totalData);
// 详细信息
dataList.addAll(lines.subList(1, lines.size()));
// 写入文件
String fileName = DateUtil.getStingDate(DateUtil.YYYYMMDDHH24MMSS, writeDate) + ".csv";
File tarFile1 = new File(tarDir, fileName + ".tp");
FileWrUtil.csvFilePrinter(tarFile1, Contants.charset, dataList);
File tarFile2 = new File(tarDir, fileName);
if (tarFile2.exists())
tarFile2.delete();
tarFile1.renameTo(tarFile2);
srcFile.delete();
logger.info("生成监测结果文件:" + tarFile2.getParentFile().getName() + "/" + tarFile2.getName());
// 忽略NC停止时间内的监测周期
// setInfo.setLastMergeDetecTime(detecTime);
setInfo.setLastMergeDetecTime(Math.max(detecTime, setInfo.getLastMergeDetecTime()));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
logger.debug("合并临时结果文件结束 ~~~~~~~");
}
}
// 下一次检测时间
private String getNextCheckTime(Date now, SetInfo setInfo) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.MINUTE, setInfo.getCheckGap().intValue());
return cal.getTimeInMillis() + "";
}
public static void main(String[] args) {
NmsClient client = new NmsClient();
client.initDetecConfig();
for (SetInfo setInfo : client.setInfos) {
Common.putPluginDetecSetInfo(setInfo.getId(), setInfo);
}
new PluginResultMerge().merge();
}
/**
* 获取当前行告警信息
* @param sysData
* @return
*/
private String[] getAlarmState(List<AlarmInfo> alarmInfos, String[] sysData) {
// strs数组数据依次为告警序列号、告警级别、告警值、性能数据、本次最高告警级别
String[] strs = new String[] { "", "", "", "", "", "" };
if (alarmInfos == null) {
return strs;
}
int maxAlarmLevel = 99;
String maxShowNum = "";
for (AlarmInfo alarm : alarmInfos) {
if (sysData.length < alarm.getShowNum()) {// 得到的数据个数和告警列数比较
continue;
}
String data = sysData[alarm.getShowNum() - 1];
boolean alarmFlag = false;
/**
* 2014-5-15 add 1.指定了特定的标识: (1).当前标识非空:不在指定标识内,则不做告警判断;在,则做告警判断
* (2).当前标识为空:空不在指定标识内,不做告警判断 2.未指定特定的标识:所有标识都进行告警判断
*/
String marker = alarm.getMarker();
// Integer markerShowNum = 1;//先默认取第一个
Integer markerShowNum = alarm.getMarkerFiledShowNum();
logger.info("告警设置checkType|" + alarm.getCheckType() + " setInfoId|" + alarm.getSetInfoId()
+ " markerShowNum|" + alarm.getMarkerFiledShowNum() + " marker|" + alarm.getMarker());
if (markerShowNum != null && markerShowNum > 0 // 若未指定标识字段则从DC传递到NC的json字符串中markerShowNum的值为0
&& StringUtils.isNotBlank(marker)) {
String markerCurVal = sysData[markerShowNum - 1];// 当前条详细监测数据的标识符
String sperator = SysConfig.getStringVal("alarm.set.marker.separator", "|");
if (StringUtils.isNotBlank(markerCurVal)) {
if (!(sperator + marker.trim() + sperator).toLowerCase().contains(
(sperator + markerCurVal.trim() + sperator).toLowerCase())) {// 当前标识不在指定的标识里
continue;
}
} else {
continue;
}
}
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());
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_client.NewPluginResultMerge.warningValue_n81i"
+ alarm.getPoliceValue() + "(" + alarm.getPoliceUnit()
+ ") " + "i18n_client.NewPluginResultMerge.abnormal_n81i";
} else {
// 性能信息
strs[3] += alarm.getFiledCommonts() + ":"
+ sysData[alarm.getShowNum() - 1] + "("
+ alarm.getPoliceUnit() + ") " + sysmbol + "i18n_client.NewPluginResultMerge.warningValue_n81i"
+ alarm.getPoliceValue() + "(" + alarm.getPoliceUnit()
+ ") " + "i18n_client.NewPluginResultMerge.normal_n81i";
}
// 2011-09-29 添加了连续几次达到告警值后主动告警,恢复正常后发送恢复信息
// 2013-03-26 添加了告警状态控制是否立刻主动告警,如果已经告警则后期不发送告警信息。
if (maxAlarmLevel > alarm.getPoliceLevel()) {// 保留本次最高告警级别,值越小级别越高
maxAlarmLevel = alarm.getPoliceLevel();
maxShowNum = alarm.getShowNum() + "";
}
}// 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 - 2] = maxAlarmLevel + "";// 本次告警最高级别
strs[strs.length - 1] = maxShowNum;// 本次告警最高级别对应的序列号showNum
return strs;
}
private 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_client.NewPluginResultMerge.gt_n81i";
} else if (">=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.out_n81i";
} else if ("<".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.lt_n81i";
} else if ("<=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.in_n81i";
} else if ("=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.eq_n81i";
} else {
symbol = oldSymbol;
}
} else {
if (">".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.in_n81i";
} else if (">=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.lt_n81i";
} else if ("<".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.out_n81i";
} else if ("<=".equals(oldSymbol)) {
symbol = "i18n_client.NewPluginResultMerge.gt_n81i";
} else if ("=".equals(oldSymbol)) {
symbol = "i18n_client.GetInfoRun.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 boolean getHistoryAlarmState(Long setInfoId) {
Boolean alarmState = historyAlarmStateMap.get(setInfoId);
return (alarmState != null && alarmState == true);
}
private int getHistoryAlarmTimes(Long setInfoId) {
Integer alarmTimes = historyAlarmTimesMap.get(setInfoId);
return (alarmTimes != null ? alarmTimes : 0);
}
}