commit 56d71f261a8bd6031e47e2bf80867049a2aa13da
Author: chenjinsong
Date: Thu Sep 27 16:11:54 2018 +0800
initial commit
diff --git a/.classpath b/.classpath
new file mode 100644
index 0000000..2aed731
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.project b/.project
new file mode 100644
index 0000000..7a20122
--- /dev/null
+++ b/.project
@@ -0,0 +1,17 @@
+
+
+ nms_client
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..7df0c38
--- /dev/null
+++ b/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,4 @@
+#Thu Mar 22 17:05:34 CST 2012
+eclipse.preferences.version=1
+encoding//src/myconfig.properties=UTF-8
+encoding/=UTF-8
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..5bbb78d
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,4 @@
+Manifest-Version: 1.0
+Class-Path: ../lib/commons-net-ftp-2.0.jar ../lib/dom4j-1.6.1.jar ../lib/json-lib-2.2.2-jdk15.jar ../lib/commons-beanutils-1.7.jar ../lib/commons-collections.jar ../lib/commons-lang.jar ../lib/commons-logging.jar ../lib/ezmorph-1.0.4.jar ../lib/log4j-1.2.15.jar ../lib/junit.jar ../lib/sigar.jar ../lib/ostermillerutils_1_07_00.jar ../lib/commons-io.jar ../lib/ant.jar ../lib/javatar-2.5.jar ../lib/java-unrar-0.3.jar
+Main-Class: com.nis.nmsclient.NmsClient
+
diff --git a/META-INF/readme.txt b/META-INF/readme.txt
new file mode 100644
index 0000000..fab20ae
--- /dev/null
+++ b/META-INF/readme.txt
@@ -0,0 +1,12 @@
+WEB布署说明:
+
+1、Windows下: 在Tomcat目录下的bin中相应的添加sigar-x86-winnt.dll或sigar-amd64-winnt.dll
+
+2、Linux下:加载动态库(.so),在配置文件~/.bash_profile或者/etc/profile中添加变量
+ LD_LIBRARY_PATH = (相应配置libsigar-x86-linux.so或libsigar-amd64-linux.so的存放目录),执行source ./bash_profile或/etc/profile
+
+
+
+JAVA应用程序布署说明:
+1、打nmsclient.jar,并使用MANIFEST.MF配置主函数入口(Main-Class),和需要用到的jar包(Class-Path),jar包路径是相对nmsclient.jar的位置。
+2、Windows下: 相应放sigar-x86-winnt.dll或sigar-amd64-winnt.dll在jdk/bin下。
\ No newline at end of file
diff --git a/bin/log4j.properties.svntmp b/bin/log4j.properties.svntmp
new file mode 100644
index 0000000..c5aa4c7
--- /dev/null
+++ b/bin/log4j.properties.svntmp
@@ -0,0 +1,15 @@
+log4j.rootLogger = debug,stdout,logfile
+
+log4j.appender.stdout = org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout.ConversionPattern = %d %p [%c] [%t] - <%m>%n
+log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
+
+#----------------------debug---------------------
+
+log4j.appender.logfile = org.apache.log4j.RollingFileAppender
+log4j.appender.logfile.MaxFileSize = 50MB
+log4j.appender.logfile.MaxBackupIndex = 20
+
+log4j.appender.logfile.File = ./nc_logs/nmsclient.log
+log4j.appender.logfile.layout.ConversionPattern = %d %p [%c] [%t] - <%m>%n
+log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
diff --git a/lib/commons-beanutils-1.7.jar b/lib/commons-beanutils-1.7.jar
new file mode 100644
index 0000000..b1b89c9
Binary files /dev/null and b/lib/commons-beanutils-1.7.jar differ
diff --git a/lib/commons-collections.jar b/lib/commons-collections.jar
new file mode 100644
index 0000000..75580be
Binary files /dev/null and b/lib/commons-collections.jar differ
diff --git a/lib/commons-io.jar b/lib/commons-io.jar
new file mode 100644
index 0000000..7affdef
Binary files /dev/null and b/lib/commons-io.jar differ
diff --git a/lib/commons-lang.jar b/lib/commons-lang.jar
new file mode 100644
index 0000000..b910561
Binary files /dev/null and b/lib/commons-lang.jar differ
diff --git a/lib/commons-logging.jar b/lib/commons-logging.jar
new file mode 100644
index 0000000..8758a96
Binary files /dev/null and b/lib/commons-logging.jar differ
diff --git a/lib/ezmorph-1.0.4.jar b/lib/ezmorph-1.0.4.jar
new file mode 100644
index 0000000..7625af6
Binary files /dev/null and b/lib/ezmorph-1.0.4.jar differ
diff --git a/lib/fileComment.jar b/lib/fileComment.jar
new file mode 100644
index 0000000..8c251d9
Binary files /dev/null and b/lib/fileComment.jar differ
diff --git a/lib/filemgr/ant.jar b/lib/filemgr/ant.jar
new file mode 100644
index 0000000..0a56a58
Binary files /dev/null and b/lib/filemgr/ant.jar differ
diff --git a/lib/filemgr/java-unrar-0.3.jar b/lib/filemgr/java-unrar-0.3.jar
new file mode 100644
index 0000000..48bebc9
Binary files /dev/null and b/lib/filemgr/java-unrar-0.3.jar differ
diff --git a/lib/filemgr/javatar-2.5.jar b/lib/filemgr/javatar-2.5.jar
new file mode 100644
index 0000000..0d4def6
Binary files /dev/null and b/lib/filemgr/javatar-2.5.jar differ
diff --git a/lib/json-lib-2.2.2-jdk15.jar b/lib/json-lib-2.2.2-jdk15.jar
new file mode 100644
index 0000000..27e7c7c
Binary files /dev/null and b/lib/json-lib-2.2.2-jdk15.jar differ
diff --git a/lib/junit.jar b/lib/junit.jar
new file mode 100644
index 0000000..674d71e
Binary files /dev/null and b/lib/junit.jar differ
diff --git a/lib/log4j-1.2.15.jar b/lib/log4j-1.2.15.jar
new file mode 100644
index 0000000..c930a6a
Binary files /dev/null and b/lib/log4j-1.2.15.jar differ
diff --git a/lib/ostermillerutils_1_07_00.jar b/lib/ostermillerutils_1_07_00.jar
new file mode 100644
index 0000000..a993150
Binary files /dev/null and b/lib/ostermillerutils_1_07_00.jar differ
diff --git a/lib/sigar/.sigar_shellrc b/lib/sigar/.sigar_shellrc
new file mode 100644
index 0000000..ed12204
--- /dev/null
+++ b/lib/sigar/.sigar_shellrc
@@ -0,0 +1,48 @@
+#this file is loaded by the sigar shell.
+#commands in this file are run just as they would be if they
+#were typed in by hand in the shell prompt.
+
+#find weblogic nodes (-1 means last index in the array)
+alias ps-wls ps State.Name.eq=java,Args.-1.eq=weblogic.Server
+
+#find websphere 4.x nodes
+alias ps-was4 ps State.Name.eq=java,Args.*.eq=com.ibm.ejs.sm.server.ManagedServer
+
+#find websphere admin server
+alias ps-was4adm ps State.Name.eq=java,Args.*.eq=com.ibm.ejs.sm.server.AdminServer
+
+#find websphere 5.x nodes
+alias ps-was5 ps State.Name.eq=java,Args.*.eq=com.ibm.ws.runtime.WsServer
+
+#find websphere 4.x and 5.x nodes
+alias ps-was ps State.Name.eq=java,Args.*.eq=com.ibm.ws.bootstrap.WSLauncher
+
+#find jboss (use .sw=java to match 'java' or 'javaw')
+alias ps-jboss ps State.Name.sw=java,Args.*.eq=org.jboss.Main
+
+#find tomcat
+alias ps-tomcat ps State.Name.eq=java,Args.*.eq=org.apache.catalina.startup.Bootstrap
+
+#find apache parent processes
+#($1 is the return value of the first query in the string)
+#'Pne' => 'P' flags means parent of matched process
+#to filter out httpd child processes
+alias ps-apache ps State.Name.re=https?d.*|[Aa]pache2?$,State.Name.Pne=$1
+
+#find ant processes (ant hangs on me sometimes)
+#(* matches any value in the array)
+alias ps-ant ps State.Name.eq=java,Args.*.eq=org.apache.tools.ant.Main
+
+#HQ agents
+alias ps-hqagent ps State.Name.sw=java,Args.-1.eq=org.hyperic.hq.agent.server.AgentDaemon
+
+#find all java procs except the shell itself
+#($$ is the current process id)
+alias ps-java ps State.Name.eq=java,Pid.Pid.ne=$$
+alias java ps-java
+
+#find all processes owned by the current user
+alias ps-me ps CredName.User.eq=$user.name
+
+#VMware Server or GSX VMs
+alias ps-vmx ps State.Name.eq=vmware-vmx,Args.1.eq=-C
diff --git a/lib/sigar/libsigar-amd64-freebsd-6.so b/lib/sigar/libsigar-amd64-freebsd-6.so
new file mode 100644
index 0000000..9ec33a9
Binary files /dev/null and b/lib/sigar/libsigar-amd64-freebsd-6.so differ
diff --git a/lib/sigar/libsigar-amd64-linux.so b/lib/sigar/libsigar-amd64-linux.so
new file mode 100644
index 0000000..913f325
Binary files /dev/null and b/lib/sigar/libsigar-amd64-linux.so differ
diff --git a/lib/sigar/libsigar-amd64-solaris.so b/lib/sigar/libsigar-amd64-solaris.so
new file mode 100644
index 0000000..ae9a4f1
Binary files /dev/null and b/lib/sigar/libsigar-amd64-solaris.so differ
diff --git a/lib/sigar/libsigar-ia64-hpux-11.sl b/lib/sigar/libsigar-ia64-hpux-11.sl
new file mode 100644
index 0000000..1dc74db
Binary files /dev/null and b/lib/sigar/libsigar-ia64-hpux-11.sl differ
diff --git a/lib/sigar/libsigar-ia64-linux.so b/lib/sigar/libsigar-ia64-linux.so
new file mode 100644
index 0000000..2bd2fc8
Binary files /dev/null and b/lib/sigar/libsigar-ia64-linux.so differ
diff --git a/lib/sigar/libsigar-pa-hpux-11.sl b/lib/sigar/libsigar-pa-hpux-11.sl
new file mode 100644
index 0000000..c63eb22
Binary files /dev/null and b/lib/sigar/libsigar-pa-hpux-11.sl differ
diff --git a/lib/sigar/libsigar-ppc-aix-5.so b/lib/sigar/libsigar-ppc-aix-5.so
new file mode 100644
index 0000000..480c440
Binary files /dev/null and b/lib/sigar/libsigar-ppc-aix-5.so differ
diff --git a/lib/sigar/libsigar-ppc-linux.so b/lib/sigar/libsigar-ppc-linux.so
new file mode 100644
index 0000000..d5637a7
Binary files /dev/null and b/lib/sigar/libsigar-ppc-linux.so differ
diff --git a/lib/sigar/libsigar-ppc64-aix-5.so b/lib/sigar/libsigar-ppc64-aix-5.so
new file mode 100644
index 0000000..9a3a737
Binary files /dev/null and b/lib/sigar/libsigar-ppc64-aix-5.so differ
diff --git a/lib/sigar/libsigar-ppc64-linux.so b/lib/sigar/libsigar-ppc64-linux.so
new file mode 100644
index 0000000..4875241
Binary files /dev/null and b/lib/sigar/libsigar-ppc64-linux.so differ
diff --git a/lib/sigar/libsigar-s390x-linux.so b/lib/sigar/libsigar-s390x-linux.so
new file mode 100644
index 0000000..ae8ac4b
Binary files /dev/null and b/lib/sigar/libsigar-s390x-linux.so differ
diff --git a/lib/sigar/libsigar-sparc-solaris.so b/lib/sigar/libsigar-sparc-solaris.so
new file mode 100644
index 0000000..507effe
Binary files /dev/null and b/lib/sigar/libsigar-sparc-solaris.so differ
diff --git a/lib/sigar/libsigar-sparc64-solaris.so b/lib/sigar/libsigar-sparc64-solaris.so
new file mode 100644
index 0000000..1a4bc18
Binary files /dev/null and b/lib/sigar/libsigar-sparc64-solaris.so differ
diff --git a/lib/sigar/libsigar-universal-macosx.dylib b/lib/sigar/libsigar-universal-macosx.dylib
new file mode 100644
index 0000000..4a35824
Binary files /dev/null and b/lib/sigar/libsigar-universal-macosx.dylib differ
diff --git a/lib/sigar/libsigar-universal64-macosx.dylib b/lib/sigar/libsigar-universal64-macosx.dylib
new file mode 100644
index 0000000..dc27122
Binary files /dev/null and b/lib/sigar/libsigar-universal64-macosx.dylib differ
diff --git a/lib/sigar/libsigar-x86-freebsd-5.so b/lib/sigar/libsigar-x86-freebsd-5.so
new file mode 100644
index 0000000..67de5df
Binary files /dev/null and b/lib/sigar/libsigar-x86-freebsd-5.so differ
diff --git a/lib/sigar/libsigar-x86-freebsd-6.so b/lib/sigar/libsigar-x86-freebsd-6.so
new file mode 100644
index 0000000..7b3a264
Binary files /dev/null and b/lib/sigar/libsigar-x86-freebsd-6.so differ
diff --git a/lib/sigar/libsigar-x86-linux.so b/lib/sigar/libsigar-x86-linux.so
new file mode 100644
index 0000000..46df73a
Binary files /dev/null and b/lib/sigar/libsigar-x86-linux.so differ
diff --git a/lib/sigar/libsigar-x86-solaris.so b/lib/sigar/libsigar-x86-solaris.so
new file mode 100644
index 0000000..ea33591
Binary files /dev/null and b/lib/sigar/libsigar-x86-solaris.so differ
diff --git a/lib/sigar/libsigar-x86_64-linux.so b/lib/sigar/libsigar-x86_64-linux.so
new file mode 100644
index 0000000..913f325
Binary files /dev/null and b/lib/sigar/libsigar-x86_64-linux.so differ
diff --git a/lib/sigar/sigar-amd64-winnt.dll b/lib/sigar/sigar-amd64-winnt.dll
new file mode 100644
index 0000000..0d2977f
Binary files /dev/null and b/lib/sigar/sigar-amd64-winnt.dll differ
diff --git a/lib/sigar/sigar-x86-winnt.dll b/lib/sigar/sigar-x86-winnt.dll
new file mode 100644
index 0000000..7b29a4e
Binary files /dev/null and b/lib/sigar/sigar-x86-winnt.dll differ
diff --git a/lib/sigar/sigar-x86-winnt.lib b/lib/sigar/sigar-x86-winnt.lib
new file mode 100644
index 0000000..928b2c7
Binary files /dev/null and b/lib/sigar/sigar-x86-winnt.lib differ
diff --git a/lib/sigar/sigar.jar b/lib/sigar/sigar.jar
new file mode 100644
index 0000000..8fe8400
Binary files /dev/null and b/lib/sigar/sigar.jar differ
diff --git a/linuxinstall/autoinstall.sh b/linuxinstall/autoinstall.sh
new file mode 100644
index 0000000..ce83980
--- /dev/null
+++ b/linuxinstall/autoinstall.sh
@@ -0,0 +1,170 @@
+#!/bin/bash
+
+# --- set default value
+DEFAULT_DATA_DIR="/home/nms/nmsdata"
+DEFAULT_INSTALL_DIR="/home/nms/nmsclient"
+
+# --- set install dir
+PRG="$0"
+PRGDIR=`dirname "$PRG"`
+CUR_PRGDIR=`cd "$PRGDIR"; pwd`
+
+INSTALL_DIR="$DEFAULT_INSTALL_DIR"
+
+if [ ! -d $INSTALL_DIR ]
+ then
+ mkdir -p $INSTALL_DIR
+fi
+
+INSTALL_DIR=`cd "$INSTALL_DIR"; pwd`
+
+# --- check jdk and jdk-version
+install_jdk=0
+javaversion=`java -version 2>&1|grep "java version"`
+if [ -n "$javaversion" ]
+ then
+ # javaversion=${javaversion:14:3}
+ javavmajor=`echo $javaversion | cut -c15`
+ javavminor=`echo $javaversion | cut -c17`
+# OS_TYPE=$( lsb_release -d| cut -d: -f2| cut -f2 )
+# if [ "`echo $OS_TYPE | cut -c1-6`" = "Ubuntu" ]
+# then
+# if [ 2 -gt $javavmajor ]; then
+# if [ 6 -gt $javavminor ]; then
+# install_jdk=1
+# fi
+# fi
+# else
+ if [[ 2 -gt $javavmajor && 6 -gt $javavminor ]]; then
+ install_jdk=1
+ fi
+# fi
+ else
+ install_jdk=1
+fi
+
+# --- install jdk
+cd "$INSTALL_DIR"/..
+NMS_JDK="$(pwd)/nmsjdk"
+JDK_DIR="$(pwd)/jdk1.7.0_80"
+JDK_BIN_FILE=$CUR_PRGDIR"/jre_install/jdk-7u80-linux-i586.tar.gz"
+if [ "`uname -i`" = "x86_64" ];then
+ JDK_BIN_FILE=$CUR_PRGDIR"/jre_install/jdk-7u80-linux-x64.tar.gz"
+fi
+if [ $install_jdk = 1 ]
+ then
+ if [ ! -e "$NMS_JDK" ]
+ then
+ echo "JDK bin file: $JDK_BIN_FILE"
+ echo "now, install jdk: $NMS_JDK"
+ sleep 3
+
+ if [ ! -e "$JDK_DIR" ];then
+ mkdir -p $JDK_DIR
+ fi
+ tar -xzf $JDK_BIN_FILE -C $JDK_DIR --strip-components=1 || installJdk=1
+ if [ -n "$installJdk" ];then
+ echo "install jdk failure, exit program"
+ exit 1
+ fi
+ ln -s $JDK_DIR $NMS_JDK
+ echo "install jdk done"
+ fi
+elif [ -n "$JAVA_HOME" ];then
+ ln -s $JAVA_HOME $NMS_JDK
+else
+ echo "JDK bin file: $JDK_BIN_FILE"
+ echo "now, install jdk: $NMS_JDK"
+ sleep 3
+ if [ ! -e "$JDK_DIR" ];then
+ mkdir -p $JDK_DIR
+ fi
+ tar -xzf $JDK_BIN_FILE -C $JDK_DIR --strip-components=1 || installJdk=1
+ if [ -n "$installJdk" ];then
+ echo "install jdk failure, exit program"
+ exit 1
+ fi
+ ln -s $JDK_DIR $NMS_JDK
+ echo "install jdk done"
+fi
+cd "$CUR_PRGDIR"
+
+echo "==========================================="
+echo "NMS_JDK: $NMS_JDK"
+echo "INSTALL_DIR: $INSTALL_DIR"
+echo "==========================================="
+
+# --- copy file to install_dir
+if [ "$INSTALL_DIR" != "$CUR_PRGDIR" ];then
+ CP_DIR=(
+ bin
+ lib
+ conf
+ shell
+ )
+ #cp
+ for CP_NAME in ${CP_DIR[@]}
+ do
+ cp -a $CUR_PRGDIR"/$CP_NAME" $INSTALL_DIR
+ done
+fi
+
+
+function modify_file(){
+ if [ $# != 2 ]
+ then
+ echo "usage: modify_file [prop_name] [prop_value]"
+ exit 0
+ fi
+ prop_name="$1"
+ prop_value="$2"
+ #echo "modify_file $prop_name $prop_value"
+ if [ -z "$(cat $PROP_FILE |grep $prop_name)" ]
+ then
+ echo "" >> $PROP_FILE
+ echo "$prop_name=$prop_value" >> $PROP_FILE
+ else
+ sed -i "s@^$prop_name.*@$prop_name=$prop_value@" $PROP_FILE
+ fi
+}
+
+# --- modify property
+#-------------file path
+path="$DEFAULT_DATA_DIR"
+#-------------include path
+include_path="$INSTALL_DIR,$path"
+#-------------exclude path
+exclude_path="$INSTALL_DIR/bin,$INSTALL_DIR/lib,$INSTALL_DIR/shell,$INSTALL_DIR/conf"
+#-------------log4j dir
+logs_path="$path/nc_logs"
+
+PROP_FILE=$INSTALL_DIR"/conf/myconfig.properties"
+#echo "PROP_FILE: $PROP_FILE"
+modify_file "local.data.path" $path
+modify_file "common.del.path.include" $include_path
+modify_file "common.del.path.exclude" $exclude_path
+
+# modify log4j
+PROP_FILE=$INSTALL_DIR"/conf/log4j.properties"
+encoding=${LANG#*.}
+modify_file "log4j.appender.stdout.encoding" $encoding
+modify_file "log4j.appender.debugAppender.encoding" $encoding
+modify_file "log4j.appender.infoAppender.encoding" $encoding
+modify_file "log4j.appender.debugAppender.File" "$logs_path/nmsclient_debug.log"
+modify_file "log4j.appender.infoAppender.File" "$logs_path/nmsclient_info.log"
+
+#permit
+cd $INSTALL_DIR"/shell"
+chmod 755 *.sh
+cd $CUR_PRGDIR
+
+if [ -z "$(cat /etc/rc.local|grep $INSTALL_DIR"/shell/startup.sh")" ]
+then
+ echo $INSTALL_DIR"/shell/startup.sh" >> /etc/rc.local
+fi
+
+echo ""
+echo "install successed..."
+echo "please use [$INSTALL_DIR/shell/startup.sh] to run the program..."
+
+$INSTALL_DIR/shell/startup.sh
diff --git a/linuxinstall/conf/jvm.conf b/linuxinstall/conf/jvm.conf
new file mode 100644
index 0000000..f4edf37
--- /dev/null
+++ b/linuxinstall/conf/jvm.conf
@@ -0,0 +1,2 @@
+-Xms64m
+-Xmx128m
\ No newline at end of file
diff --git a/linuxinstall/detectShell/config b/linuxinstall/detectShell/config
new file mode 100644
index 0000000..5b2757e
--- /dev/null
+++ b/linuxinstall/detectShell/config
@@ -0,0 +1,5 @@
+configPath=/home/nms/nmsdata/nc_config
+sysinfoPath=/root/zhong/sysinfo
+ip1=192.168.11.211
+ip2=192.168.11.211
+ip3=192.168.11.211
diff --git a/linuxinstall/detectShell/getMacInfo.v2.sh b/linuxinstall/detectShell/getMacInfo.v2.sh
new file mode 100644
index 0000000..6b32b8e
--- /dev/null
+++ b/linuxinstall/detectShell/getMacInfo.v2.sh
@@ -0,0 +1,590 @@
+#!/bin/bash
+
+basePath=$(cd `dirname $0`; pwd)
+date=`date +%Y%m%d%H%M%S`
+d=`date "+%Y-%m-%d %H:%M:%S"` #temp column
+t=`date -d "$d" +%s` #temp column
+t2=`date "+%N" | awk '{print int($0)}'`
+dateStamp=$((t*1000+t2/1000000)) #current time stamp
+sep=\$@\$
+
+cd ${basePath}
+while read line;do # 读取config
+ eval "$line"
+done < config
+
+if [ ! -d macInfo ]
+ then
+ mkdir macInfo
+fi
+
+if [ ! -d result ]
+ then
+ mkdir result
+fi
+
+if [ ! -f firstTimeDate ]
+ then
+ touch firstTimeDate
+ echo ${dateStamp} > firstTimeDate
+ firstTimeDate=${dateStamp}
+ else
+ firstTimeDate=`cat firstTimeDate`
+fi
+
+# 读取任务配置文件
+OLD_IFS="$IFS"
+IFS=","
+
+cpu_pubInfo=`cat ${configPath}/cpu_cpu.cfg | grep pubInfo | awk -F'=' '{print $2}'`
+cpu_checkState=`cat ${configPath}/cpu_cpu.cfg | grep checkState | awk -F'=' '{print $2}'`
+cpu_checkGap=`cat ${configPath}/cpu_cpu.cfg | grep checkGap | awk -F'=' '{print $2}'`
+cpu_checkOutTime=`cat ${configPath}/cpu_cpu.cfg | grep checkOutTime | awk -F'=' '{print $2}'`
+cpu_checkMaxTimes=`cat ${configPath}/cpu_cpu.cfg | grep checkMaxTimes | awk -F'=' '{print $2}'`
+cpu_planCheckTime=`cat ${configPath}/cpu_cpu.cfg | grep planCheckTime | awk -F'=' '{print $2}'`
+cpu_uploadGap=`cat ${configPath}/cpu_cpu.cfg | grep uploadGap | awk -F'=' '{print $2}'`
+cpu_dataFileDir=`cat ${configPath}/cpu_cpu.cfg | grep dataFileDir | awk -F'=' '{print $2}'`
+cpu_polices=`cat ${configPath}/cpu_cpu.cfg | grep police | awk -F'=' '{print $2}'`
+cpu_polices_arr=($cpu_polices)
+cpu_police_flag=0
+cpu_nextDateStamp=$((cpu_checkGap*60*1000+dateStamp))
+mem_pubInfo=`cat ${configPath}/memory_memory.cfg | grep pubInfo | awk -F'=' '{print $2}'`
+mem_checkState=`cat ${configPath}/memory_memory.cfg | grep checkState | awk -F'=' '{print $2}'`
+mem_checkGap=`cat ${configPath}/memory_memory.cfg | grep checkGap | awk -F'=' '{print $2}'`
+mem_checkOutTime=`cat ${configPath}/memory_memory.cfg | grep checkOutTime | awk -F'=' '{print $2}'`
+mem_checkMaxTimes=`cat ${configPath}/memory_memory.cfg | grep checkMaxTimes | awk -F'=' '{print $2}'`
+mem_planCheckTime=`cat ${configPath}/memory_memory.cfg | grep planCheckTime | awk -F'=' '{print $2}'`
+mem_uploadGap=`cat ${configPath}/memory_memory.cfg | grep uploadGap | awk -F'=' '{print $2}'`
+mem_dataFileDir=`cat ${configPath}/memory_memory.cfg | grep dataFileDir | awk -F'=' '{print $2}'`
+mem_polices=`cat ${configPath}/memory_memory.cfg | grep police | awk -F'=' '{print $2}'`
+mem_polices_arr=($mem_polices)
+mem_police_flag=0
+mem_nextDateStamp=$((mem_checkGap*60*1000+dateStamp))
+disk_pubInfo=`cat ${configPath}/disk_disk.cfg | grep pubInfo | awk -F'=' '{print $2}'`
+disk_checkState=`cat ${configPath}/disk_disk.cfg | grep checkState | awk -F'=' '{print $2}'`
+disk_checkGap=`cat ${configPath}/disk_disk.cfg | grep checkGap | awk -F'=' '{print $2}'`
+disk_checkOutTime=`cat ${configPath}/disk_disk.cfg | grep checkOutTime | awk -F'=' '{print $2}'`
+disk_checkMaxTimes=`cat ${configPath}/disk_disk.cfg | grep checkMaxTimes | awk -F'=' '{print $2}'`
+disk_planCheckTime=`cat ${configPath}/disk_disk.cfg | grep planCheckTime | awk -F'=' '{print $2}'`
+disk_uploadGap=`cat ${configPath}/disk_disk.cfg | grep uploadGap | awk -F'=' '{print $2}'`
+disk_dataFileDir=`cat ${configPath}/disk_disk.cfg | grep dataFileDir | awk -F'=' '{print $2}'`
+disk_polices=`cat ${configPath}/disk_disk.cfg | grep police | awk -F'=' '{print $2}'`
+disk_polices_arr=($disk_polices)
+disk_police_flag=0
+disk_nextDateStamp=$((disk_checkGap*60*1000+dateStamp))
+net_pubInfo=`cat ${configPath}/net_net.cfg | grep pubInfo | awk -F'=' '{print $2}'`
+net_checkState=`cat ${configPath}/net_net.cfg | grep checkState | awk -F'=' '{print $2}'`
+net_checkGap=`cat ${configPath}/net_net.cfg | grep checkGap | awk -F'=' '{print $2}'`
+net_checkOutTime=`cat ${configPath}/net_net.cfg | grep checkOutTime | awk -F'=' '{print $2}'`
+net_checkMaxTimes=`cat ${configPath}/net_net.cfg | grep checkMaxTimes | awk -F'=' '{print $2}'`
+net_planCheckTime=`cat ${configPath}/net_net.cfg | grep planCheckTime | awk -F'=' '{print $2}'`
+net_uploadGap=`cat ${configPath}/net_net.cfg | grep uploadGap | awk -F'=' '{print $2}'`
+net_dataFileDir=`cat ${configPath}/net_net.cfg | grep dataFileDir | awk -F'=' '{print $2}'`
+net_polices=`cat ${configPath}/net_net.cfg | grep police | awk -F'=' '{print $2}'`
+net_polices_arr=($net_polices)
+net_police_flag=0
+net_nextDateStamp=$((net_checkGap*60*1000+dateStamp))
+sys_pubInfo=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep pubInfo | awk -F'=' '{print $2}'`
+sys_checkState=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep checkState | awk -F'=' '{print $2}'`
+sys_checkGap=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep checkGap | awk -F'=' '{print $2}'`
+sys_checkOutTime=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep checkOutTime | awk -F'=' '{print $2}'`
+sys_checkMaxTimes=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep checkMaxTimes | awk -F'=' '{print $2}'`
+sys_planCheckTime=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep planCheckTime | awk -F'=' '{print $2}'`
+sys_uploadGap=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep uploadGap | awk -F'=' '{print $2}'`
+sys_dataFileDir=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep dataFileDir | awk -F'=' '{print $2}'`
+sys_polices=`cat ${configPath}/systeminfo_servicessysinfo.cfg | grep police | awk -F'=' '{print $2}'`
+sys_polices_arr=($sys_polices)
+sys_police_flag=0
+sys_nextDateStamp=$((sys_checkGap*60*1000+dateStamp))
+
+IFS="$OLD_IFS"
+
+sysinfoSrc=`cat ${sysinfoPath}`
+
+## begin content weaving ##
+# task info
+cpuResult_taskInfo=${cpu_pubInfo}","${firstTimeDate}","${cpu_checkOutTime}","${dateStamp}",1,"${cpu_nextDateStamp}","
+memResult_taskInfo=${mem_pubInfo}","${firstTimeDate}","${mem_checkOutTime}","${dateStamp}",1,"${mem_nextDateStamp}","
+diskResult_taskInfo=${disk_pubInfo}","${firstTimeDate}","${disk_checkOutTime}","${dateStamp}",1,"${disk_nextDateStamp}","
+netResult_taskInfo=${net_pubInfo}","${firstTimeDate}","${net_checkOutTime}","${dateStamp}",1,"${net_nextDateStamp}","
+sysResult_taskInfo=${sys_pubInfo}","${firstTimeDate}","${sys_checkOutTime}","${dateStamp}",1,"${sys_nextDateStamp}",1,"
+
+cpuResult_coreNum=0
+disk_total_num=0
+net_total_num=0
+sys_net_total_num=0
+diskResult_baseInfo="\""
+memResult_baseInfo="\""
+netResult_baseInfo="\""
+sysResult_baseInfo="\""
+# result file
+
+cpuResult=result/cpuResult_${date}
+memResult=result/memResult_${date}
+diskResult=result/diskResult_${date}
+netResult=result/netResult_${date}
+sysResult=result/sysResult_${date}
+
+function getDetail {
+ cpuInfo=macInfo/$1_cpuInfo_${date}
+ memInfo=macInfo/$1_memInfo_${date}
+ diskInfo=macInfo/$1_diskInfo_${date}
+ netInfo=macInfo/$1_netInfo_${date}
+ sysInfo=macInfo/$1_sysInfo_${date}
+
+ ssh -tt $2 "sar -P ALL 1 1" > ${cpuInfo}
+ ssh -tt $2 "cat /proc/cpuinfo | grep MHz" > ${cpuInfo}2
+ ssh -tt $2 "free|awk 'NR>1{print}'" > ${memInfo}
+ ssh -tt $2 "df -l|grep -v tmpfs|awk 'NR>1{print}'" > ${diskInfo}
+
+ # 网卡信息
+ special_net_result_t=`ssh -C $2 "/bin/bash" < remote_net.sh` # 获取特殊网卡信息
+ special_net_result_base=${special_net_result_t%details,*}
+ special_net_result_details_t=${special_net_result_t#*details,}
+ special_net_result_details="\""${special_net_result_details_t#*\"}
+ special_net_result_details_t_arr=($special_net_result_details_t)
+ special_net_result_detailsNum=${special_net_result_details_t_arr[0]}
+ special_net_result=${special_net_result_base}"details,"${special_net_result_detailsNum}$'\n'
+ for((i=1;i<${#special_net_result_details_t_arr[@]};i++));do
+ special_net_result=${special_net_result}${special_net_result_details_t_arr[$i]}$'\n'
+ done
+ echo "$special_net_result" > ${netInfo}3
+ getNetInfo
+
+ dos2unix ${cpuInfo} > /dev/null 2>&1
+ dos2unix ${cpuInfo}2 > /dev/null 2>&1
+ dos2unix ${memInfo} > /dev/null 2>&1
+ dos2unix ${diskInfo} > /dev/null 2>&1
+ #dos2unix ${diskInfo} > /dev/null 2>&1
+
+ # cpu info
+ cpu_t=`awk -F "(" '{print $3}' ${cpuInfo}` # temp column
+
+ cpu_coreNum=`echo $cpu_t|awk -F " " '{print $1}'`
+ cpuResult_coreNum=$((cpuResult_coreNum+cpu_coreNum))
+ cpu_totalMHz=`awk '{total+=$4}END{print total}' ${cpuInfo}2`
+ cpu_averMHz=`echo "scale=2;$cpu_totalMHz/$cpu_coreNum"|bc|awk '{printf "%.2f",$0}'`
+ cpu_idle_total=`awk 'NR==4{print $9}' ${cpuInfo}`
+ cpu_shiyonglv=`bc <<<100-$cpu_idle_total|awk '{printf "%.2f",$0}'`
+
+ cpuResult_baseInfo=${cpuResult_baseInfo}${sep}"cpu${index} i18n_client.SystemInfo.mhz_n81i"${cpu_averMHz}"MHz,i18n_client.SystemInfo.shiyonglv_n81i "${cpu_shiyonglv}"%; "
+ getCpuBase2 ${cpuInfo} ${cpuInfo}2
+ getCpuDetail ${cpuInfo} ${cpuInfo}2
+
+ # mem info
+ swap_total_t=`cat ${memInfo}|grep '^S'|awk '{print $2}'`
+ swap_total=`echo "scale=2;${swap_total_t}/1024/1024"|bc|awk '{printf "%.2f",$0}'`
+ swap_free_t=`cat ${memInfo}|grep '^S'|awk '{print $4}'`
+ swap_free=`echo "scale=2;${swap_free_t}/1024/1024"|bc|awk '{printf "%.2f",$0}'`
+ mem_total_t=`awk 'NR==1{print $2}' ${memInfo}`
+ mem_total=`echo "scale=2;${mem_total_t}/1024/1024"|bc|awk '{printf "%.2f",$0}'`
+ mem_used_t=`awk 'NR==1{print $3}' ${memInfo}`
+ mem_used=`echo "scale=2;${mem_used_t}/1024/1024"|bc|awk '{printf "%.2f",$0}'`
+ mem_free=`echo "${mem_total}-${mem_used}"|bc|awk '{printf "%.2f",$0}'`
+ mem_shiyonglv=`echo "scale=2;${mem_used}/${mem_total}*100.00"|bc|awk '{printf "%.2f",$0}'`
+ memResult_baseInfo="${memResult_baseInfo}"${sep}"i18n_client.SystemInfo.memerySize_n81i: "${mem_total}"G, i18n_client.SystemInfo.currentUsed_n81i:"${mem_used}"G, i18n_client.SystemInfo.spaceRemain_n81i:"${mem_free}"G, i18n_client.SystemInfo.shiyonglv_n81i:"${mem_shiyonglv}"% "
+ mem_detail_nopolice=${swap_total}","${swap_free}","${mem_total}","${mem_used}","${mem_free}","${mem_shiyonglv}","$'\n'
+ OLD_IFS="$IFS"
+ IFS=","
+ mem_detail_nopolice_arr=(${mem_detail_nopolice})
+ IFS=$OLD_IFS
+ # police
+ if [ ! -z $mem_polices ]
+ then
+ mem_police_most_level=0
+ mem_police_flag_t=0 #临时变量
+ mem_baseInfo_police=""
+ for((j=0;j<${#mem_polices_arr[@]};j++));do
+ mem_detail_police_t=""
+ OLD_IFS="$IFS"
+ IFS="|"
+ mem_police_arr=(${mem_polices_arr[$j]})
+ IFS=$OLD_IFS
+
+ if (($(echo "${mem_detail_nopolice_arr[((${mem_police_arr[0]}-1))]} ${mem_police_arr[1]} ${mem_police_arr[2]}"|bc) == 1))
+ then #触发告警
+ mem_detail_police_t=${mem_police_arr[0]}","${mem_police_arr[3]}","${mem_police_arr[2]}","
+ mem_police_flag=1
+ mem_police_flag_t=1
+ if ((${mem_police_arr[3]} > ${mem_police_most_level}))
+ then
+ mem_police_most_level=${mem_police_arr[3]}
+ mem_detail_police=$mem_detail_police_t
+ mem_baseInfo_police="【${mem_detail_nopolice_arr[0]}】"${mem_police_arr[4]}${mem_detail_nopolice_arr[((${mem_police_arr[0]}-1))]}"% i18n_client.GetInfoRun.abnormal_n81i"
+ fi
+ elif [ $mem_police_flag_t -eq 0 ]&&[ -z "${mem_baseInfo_police}" ];then
+ mem_detail_police="\"\",,,"
+ mem_baseInfo_police="【${mem_detail_nopolice_arr[0]}】"${mem_police_arr[4]}${mem_detail_nopolice_arr[((${mem_police_arr[0]}-1))]}" i18n_client.GetInfoRun.normal_n81i"
+ fi
+ done
+ memResult_baseInfo_police=${memResult_baseInfo_police}${mem_baseInfo_police}
+ else
+ mem_detail_police="\"\",,,"
+ fi
+ memResult_detail=${memResult_detail}${mem_detail_police}${mem_detail_nopolice}
+
+
+ #memResult_detail="${memResult_detail}""\"\",,,"${swap_total}","${swap_free}","${mem_total}","${mem_used}","${mem_free}","${mem_shiyonglv}","$'\n'
+
+ # disk info
+ disk_total_size=`awk '{total+=$2}END{print total}' ${diskInfo}` # Kb
+ disk_total_used_size=`awk '{total+=$3}END{print total}' ${diskInfo}`
+ disk_total_shiyonglv=`echo "scale=2;(${disk_total_used_size}*100)/${disk_total_size}"|bc|awk '{printf "%.2f",$0}'`
+ diskResult_baseInfo=${diskResult_baseInfo}${sep}"disk-"${index}"i18n_client.SystemInfo.diskSize_n81i:"`echo "scale=2;${disk_total_size}/1024/1024"|bc|awk '{printf "%.2f",$0}'`"G,i18n_client.SystemInfo.used2_n81i "${disk_total_shiyonglv}"% ;"
+ getDiskBase ${diskInfo}
+ getDiskDetail
+
+ # net info
+ #net_avai_if_arr=($(awk 'NR==1{print $0}' ${netInfo}))
+ #net_all_if_arr=($(awk 'NR>1{print}' ${netInfo} | awk -F ':' '{print $1 NR}'))
+ #getNetInfo
+}
+
+function getCpuBase2 {
+ arr1=($(awk '/^[0-2]/{print $9}' $1|awk 'NR>2{print $0}'))
+ arr2=($(awk '{print $4}' $2))
+
+ for((i=0;i<${#arr1[@]};i++));do
+ cpu_t2=`bc <<<100-${arr1[$i]} |awk '{printf "%.2f",$0}'`
+ cpuResult_baseInfo=${cpuResult_baseInfo}${sep}"cpu"${index}"-$i i18n_client.SystemInfo.mhz_n81i"${arr2[$i]}"MHz,i18n_client.SystemInfo.shiyonglv_n81i "${cpu_t2}"%; "
+ done
+}
+
+function getCpuDetail {
+ cpu_t3=`awk '/^[0-2]/{print $0}' $1|awk 'NR>1{print $0}'`
+
+ for((i=2;i<=`echo "${cpu_t3}"|awk 'END{print NR}'`;i++));do
+ cpu_row=`echo "${cpu_t3}"|awk 'NR=="'$i'"{print $0}'`
+ cpu_idle=`echo "${cpu_row}"|awk '{print $9}'`
+ cpu_shiyonglv_t=`bc <<<100-${cpu_idle} |awk '{printf "%.2f",$0}'`
+
+ cpu_detail_nopolice="cpu${index}-$((i-2)),"`echo "${cpu_row}"|awk '{print $4}'`","`echo "${cpu_row}"|awk '{print $6}'`","`echo "${cpu_row}"|awk '{print $7}'`","`echo "${cpu_row}"|awk '{print $5}'`","`echo "${cpu_row}"|awk '{print $9}'`","${cpu_shiyonglv_t}","`awk 'NR=="'$((i-1))'"{print $4}' $2`$'\n'
+
+ # police
+ if [ ! -z $cpu_polices ]
+ then
+ OLD_IFS="$IFS"
+ IFS=","
+ cpu_detail_nopolice_arr=(${cpu_detail_nopolice})
+ IFS=$OLD_IFS
+ cpu_police_most_level=0
+ cpu_police_flag_t=0 #临时变量
+ cpu_baseInfo_police=""
+ for((j=0;j<${#cpu_polices_arr[@]};j++));do
+ cpu_detail_police_t=""
+ OLD_IFS="$IFS"
+ IFS="|"
+ cpu_police_arr=(${cpu_polices_arr[$j]})
+ IFS=$OLD_IFS
+
+ if (($(echo "${cpu_detail_nopolice_arr[((${cpu_police_arr[0]}-1))]} ${cpu_police_arr[1]} ${cpu_police_arr[2]}"|bc) == 1))
+ then #触发告警
+ cpu_detail_police_t=${cpu_police_arr[0]}","${cpu_police_arr[3]}","${cpu_police_arr[2]}","
+ cpu_police_flag=1
+ cpu_police_flag_t=1
+ if ((${cpu_police_arr[3]} > ${cpu_police_most_level}))
+ then
+ cpu_police_most_level=${cpu_police_arr[3]}
+ cpu_detail_police=$cpu_detail_police_t
+ cpu_baseInfo_police="【cpu${index}-$((i-2))】"${cpu_police_arr[4]}${cpu_detail_nopolice_arr[((${cpu_police_arr[0]}-1))]}"% i18n_client.GetInfoRun.abnormal_n81i"
+ fi
+ elif [ $cpu_police_flag_t -eq 0 ]&&[ -z "${cpu_baseInfo_police}" ];then
+ cpu_detail_police="\"\",,,"
+ cpu_baseInfo_police="【cpu${index}-$((i-2))】"${cpu_police_arr[4]}${cpu_detail_nopolice_arr[((${cpu_police_arr[0]}-1))]}"% i18n_client.GetInfoRun.normal_n81i"
+ fi
+ done
+ cpuResult_baseInfo_police=${cpuResult_baseInfo_police}${cpu_baseInfo_police}
+ else
+ cpu_detail_police="\"\",,,"
+ fi
+ cpuResult_detail=${cpuResult_detail}${cpu_detail_police}${cpu_detail_nopolice}
+ done
+ cpuResult_detail=${cpuResult_detail}"\"\",,,cpu${index},"`awk 'NR==4{print $4}' $1`","`awk 'NR==4{print $6}' $1`","`awk 'NR==4{print $7}' $1`","`awk 'NR==4{print $5}' $1`","`awk 'NR==4{print $9}' $1`","${cpu_shiyonglv}","${cpu_averMHz}$'\n'
+}
+
+function getDiskBase {
+ disk_size_arr=($(awk '{print $2}' $1))
+ disk_used_arr=($(awk '{print $3}' $1))
+ disk_avai_arr=($(awk '{print $4}' $1)) # available
+ disk_usep_arr=($(awk '{print $5}' $1))
+ disk_moun_arr=($(awk '{print $6}' $1)) # mount on
+
+ for((i=0;i<${#disk_size_arr[@]};i++));do
+ disk_size=`echo "scale=2;${disk_size_arr[$i]}/1024/1024"|bc|awk '{printf "%.2f",$0}'` # Gb
+ disk_used=`echo "scale=2;${disk_used_arr[$i]}/1024/1024"|bc|awk '{printf "%.2f",$0}'`
+ disk_avai=`echo "scale=2;${disk_avai_arr[$i]}/1024/1024"|bc|awk '{printf "%.2f",$0}'`
+ disk_usep=${disk_usep_arr[$i]}
+ disk_moun=${disk_moun_arr[$i]}
+
+ diskResult_baseInfo=${diskResult_baseInfo}${sep}"【disk-$index:"${disk_moun}"】i18n_client.SystemInfo.size_n81i "${disk_size}"G, i18n_client.SystemInfo.used1_n81i "${disk_used}"G, i18n_client.SystemInfo.spaceRemain_n81i "${disk_avai}"G, i18n_client.SystemInfo.shiyonglv_n81i "${disk_usep}" ; "
+ done
+}
+
+function getDiskDetail {
+ disk_total_num=`echo "${disk_total_num}+${#disk_size_arr[@]}"|bc`
+ for((i=0;i<${#disk_size_arr[@]};i++));do
+ disk_size=`echo "scale=2;${disk_size_arr[$i]}/1024/1024"|bc|awk '{printf "%.2f",$0}'` # Gb
+ disk_avai=`echo "scale=2;${disk_avai_arr[$i]}/1024/1024"|bc|awk '{printf "%.2f",$0}'`
+ disk_detail_nopolice="disk-$index:"${disk_moun_arr[$i]}","${disk_size}","${disk_avai}","${disk_usep_arr[$i]%\%*}",0,"$'\n'
+ OLD_IFS=$IFS
+ IFS=","
+ disk_detail_nopolice_arr=(${disk_detail_nopolice})
+ IFS=$OLD_IFS
+
+ # police
+ if [ ! -z $disk_polices ]
+ then
+ disk_police_most_level=0
+ disk_police_flag_t=0 #临时变量
+ disk_baseInfo_police=""
+ for((j=0;j<${#disk_polices_arr[@]};j++));do
+ disk_detail_police_t=""
+ OLD_IFS="$IFS"
+ IFS="|"
+ disk_police_arr=(${disk_polices_arr[$j]})
+ IFS=$OLD_IFS
+
+ if (($(echo "${disk_detail_nopolice_arr[((${disk_police_arr[0]}-1))]} ${disk_police_arr[1]} ${disk_police_arr[2]}"|bc) == 1))
+ then #触发告警
+ disk_detail_police_t=${disk_police_arr[0]}","${disk_police_arr[3]}","${disk_police_arr[2]}","
+ disk_police_flag=1
+ disk_police_flag_t=1
+ if ((${disk_police_arr[3]} > ${disk_police_most_level}))
+ then
+ disk_police_most_level=${disk_police_arr[3]}
+ disk_detail_police=$disk_detail_police_t
+ disk_baseInfo_police="【${disk_detail_nopolice_arr[0]}】"${disk_police_arr[4]}${disk_detail_nopolice_arr[((${disk_police_arr[0]}-1))]}"% i18n_client.GetInfoRun.abnormal_n81i"
+ fi
+ elif [ $disk_police_flag_t -eq 0 ]&&[ -z "${disk_baseInfo_police}" ];then
+ disk_detail_police="\"\",,,"
+ disk_baseInfo_police="【${disk_detail_nopolice_arr[0]}】"${disk_police_arr[4]}${disk_detail_nopolice_arr[((${disk_police_arr[0]}-1))]}" i18n_client.GetInfoRun.normal_n81i"
+ fi
+ done
+ diskResult_baseInfo_police=${diskResult_baseInfo_police}${disk_baseInfo_police}
+ else
+ disk_detail_police="\"\",,,"
+ fi
+ diskResult_detail=${diskResult_detail}${disk_detail_police}${disk_detail_nopolice}
+ done
+}
+
+function getNetInfo {
+ # 处理特殊网卡信息的内容
+ special_base_row=`awk 'NR==1{print $0}' ${netInfo}3` # 第一行
+ special_base_11=`echo ${special_base_row} | awk -F '"' '{print $2}'` # 11列
+ special_base_11=${sep}${special_base_11#*$sep}
+ special_base_12=`echo $special_base_row | awk -F '"' '{print $4}'` # 12列
+ special_base_12=${sep}${special_base_12#*$sep}
+ special_base_t=${special_base_11}${special_base_12}
+ special_base_t=${special_base_t//$sep/\!}
+ OLDIFS=$IFS
+ IFS="\!"
+ special_base_t_arr=(${special_base_t})
+ IFS=$OLD_IFS
+
+ special_base=""
+ for((i=1;i<${#special_base_t_arr[@]};i++));do
+ special_base=${special_base}"${sep}if${index}-"${special_base_t_arr[$i]}
+ done
+ special_detail_rows=`awk 'NR>2{print}' ${netInfo}3` # details行下方所有行
+ special_detail_rows_arr=($special_detail_rows) # details下方所有行的数组,每个元素为一行内容
+
+ #special_if_t=`echo ${special_detail_rows}|awk -F ',' '{print $4}'` # 取每行第三、四个逗号之间的网卡名
+ special_if_arr=() # 网卡名称数组
+
+ special_if_baseInfo="" # 初始化特殊网卡基础信息
+ for ((i=0;i<${#special_detail_rows_arr[@]};i++));do
+ special_detail_row=`echo ${special_detail_rows_arr[$i]} | awk -F ',' '{print gensub($4,"if"'$index'"-"$4,1)}'` # detail行网卡名替换为ifx-开头的名字
+ netResult_detail=${netResult_detail}${special_detail_row}$'\n'
+
+ special_if_baseInfo=${special_if_baseInfo}${special_base_12}${special_base_11}
+ special_if_arr[$i]=`echo ${special_detail_rows_arr[$i]}| awk -F ',' '{print $4}'`
+ done
+ sys_net_total_num=`echo "${sys_net_total_num}+${#net_all_if_arr[@]}"|bc`
+ net_total_num=`echo "${net_total_num}+${#net_avai_if_arr[@]}"|bc`
+ net_avai_baseInfo=""
+ net_disa_baseInfo=""
+
+ for((i=0;i<${#net_all_if_arr[@]};i++));do
+ ifName_t=${net_all_if_arr[$i]}
+ ifName=${ifName_t%?}
+ echo ${special_if_arr[0]}"--"${special_if_arr[1]}"--"${special_if_arr[2]}"--"${ifName}
+ if echo "${special_if_arr[@]}"|grep -w "${ifName}" &>/dev/null;then
+ let "net_total_num--"
+ let "sys_net_total_num--"
+ continue
+ fi
+
+ net_rowNum=$((${net_all_if_arr[$i]: -1}+1))
+ net_if_name_t=`awk 'NR=="'${net_rowNum}'"{print $1}' ${netInfo}`
+ net_if_name=${net_if_name_t%?}
+ is_avai=`echo "${net_avai_if_arr[@]}" | grep -wq "${net_all_if_arr[$i]%?}" && echo "1" || echo "0"`
+ if [ "1" -eq "$is_avai" ]
+ then
+ echo "#!/bin/bash"$'\n'"ethtool ${net_if_name}" > ethtool.sh
+ net_if_ethtoolResult=`ssh -C $value "/bin/bash" < ethtool.sh`
+ net_if_speed_t=`echo "${net_if_ethtoolResult}"|grep "Speed:"|awk '{print $2}'`
+ if [ ${#net_if_speed_t} -lt 1 ]
+ then
+ net_if_speed=0
+ else
+ net_if_speed=${net_if_speed_t:0:${#net_if_speed_t}-4}
+ fi
+ net_if_rxByte1=`awk 'NR=="'${net_rowNum}'"{print $2}' ${netInfo}`
+ net_if_rxByte2=`awk 'NR=="'${net_rowNum}'"{print $2}' ${netInfo}2`
+ net_if_rxBps=`echo "scale=2;($net_if_rxByte2-$net_if_rxByte1)*8/5"|bc|awk '{printf "%.2f",$0}'`
+ net_if_rxPackets1=`awk 'NR=="'${net_rowNum}'"{print $3}' ${netInfo}`
+ net_if_rxPackets2=`awk 'NR=="'${net_rowNum}'"{print $3}' ${netInfo}2`
+ net_if_rxPps_t=`echo "($net_if_rxPackets2-$net_if_rxPackets1)"|bc`
+ net_if_rxPps=`echo "scale=2;($net_if_rxPackets2-$net_if_rxPackets1)*8/5"|bc|awk '{printf "%.2f",$0}'`
+ net_if_txByte1=`awk 'NR=="'${net_rowNum}'"{print $10}' ${netInfo}`
+ net_if_txByte2=`awk 'NR=="'${net_rowNum}'"{print $10}' ${netInfo}`
+ net_if_txBps=`echo "scale=2;($net_if_txByte2-$net_if_txByte1)*8/5"|bc|awk '{printf "%.2f",$0}'`
+ net_if_txPackets1=`awk 'NR=="'${net_rowNum}'"{print $11}' ${netInfo}`
+ net_if_txPackets2=`awk 'NR=="'${net_rowNum}'"{print $11}' ${netInfo}2`
+ net_if_txPps_t=`echo "($net_if_rxPackets2-$net_if_rxPackets1)"|bc`
+ net_if_txPps=`echo "scale=2;($net_if_rxPackets2-$net_if_rxPackets1)*8/5"|bc|awk '{printf "%.2f",$0}'`
+ net_if_rxError1=`awk 'NR=="'${net_rowNum}'"{print $4}' ${netInfo}`
+ net_if_rxError2=`awk 'NR=="'${net_rowNum}'"{print $4}' ${netInfo}2`
+ net_if_txError1=`awk 'NR=="'${net_rowNum}'"{print $12}' ${netInfo}`
+ net_if_txError2=`awk 'NR=="'${net_rowNum}'"{print $12}' ${netInfo}2`
+ net_if_rxDrop1=`awk 'NR=="'${net_rowNum}'"{print $5}' ${netInfo}`
+ net_if_rxDrop2=`awk 'NR=="'${net_rowNum}'"{print $5}' ${netInfo}2`
+ net_if_txDrop1=`awk 'NR=="'${net_rowNum}'"{print $13}' ${netInfo}`
+ net_if_txDrop2=`awk 'NR=="'${net_rowNum}'"{print $13}' ${netInfo}2`
+
+ if [ $net_if_rxPps_t -gt 0 ]
+ then
+ net_if_rxErrorPerc=`echo "scale=2;($net_if_rxError2-$net_if_rxError1)*100/($net_if_rxPackets2-$net_if_rxPackets1)"|bc|awk '{printf "%.2f",$0}'`
+ net_if_rxDropPerc=`echo "scale=2;($net_if_rxDrop2-$net_if_rxDrop1)*100/($net_if_rxPackets2-$net_if_rxPackets1)"|bc|awk '{printf "%.2f",$0}'`
+ else
+ net_if_rxErrorPerc=0.00
+ net_if_rxDropPerc=0.00
+ fi
+ if [ $net_if_txPps_t -gt 0 ]
+ then
+ net_if_txErrorPerc=`echo "scale=2;($net_if_txError2-$net_if_txError1)*100/($net_if_txPackets2-$net_if_txPackets1)"|bc|awk '{printf "%.2f",$0}'`
+ net_if_txDropPerc=`echo "scale=2;($net_if_txDrop2-$net_if_txDrop1)*100/($net_if_txPackets2-$net_if_txPackets1)"|bc|awk '{printf "%.2f",$0}'`
+ else
+ net_if_txErrorPerc=0.00
+ net_if_txDropPerc=0.00
+ fi
+ net_avai_baseInfo=${net_avai_baseInfo}${sep}"if"${index}"-"${net_if_name}" i18n_client.SystemInfo.netSpeed_n81i"${net_if_speed}"Mbps, i18n_client.SystemInfo.input_n81i"${net_if_rxBps}"bps、"${net_if_rxPps}"pps, Output"${net_if_txBps}"bps、"${net_if_txPps}"pps; "
+ net_detail_nopolice="if${index}-"${net_if_name}","${net_if_rxPackets2}","${net_if_txPackets2}","${net_if_rxByte2}","${net_if_txByte2}","${net_if_rxError2}","${net_if_txError2}","${net_if_rxDrop2}","${net_if_txDrop2}","${net_if_speed}","${net_if_rxBps}","${net_if_txBps}","${net_if_rxPps}","${net_if_txPps}","${net_if_rxErrorPerc}","${net_if_txErrorPerc}","${net_if_rxDropPerc}","${net_if_txDropPerc}$'\n'
+ #netResult_detail=${netResult_detail}"\"\",,,if${index}-"${net_if_name}","${net_if_rxPackets2}","${net_if_txPackets2}","${net_if_rxByte2}","${net_if_txByte2}","${net_if_rxError2}","${net_if_txError2}","${net_if_rxDrop2}","${net_if_txDrop2}","${net_if_speed}","${net_if_rxBps}","${net_if_txBps}","${net_if_rxPps}","${net_if_txPps}","${net_if_rxErrorPerc}","${net_if_txErrorPerc}","${net_if_rxDropPerc}","${net_if_txDropPerc}$'\n'
+ # police
+ if [ ! -z $net_polices ]
+ then
+ OLD_IFS="$IFS"
+ IFS=","
+ net_detail_nopolice_arr=(${net_detail_nopolice})
+ IFS=$OLD_IFS
+ net_police_most_level=0
+ net_police_flag_t=0 #临时变量
+ net_baseInfo_police=""
+ for((j=0;j<${#net_polices_arr[@]};j++));do
+ net_detail_police_t=""
+ OLD_IFS="$IFS"
+ IFS="|"
+ net_police_arr=(${net_polices_arr[$j]})
+ IFS=$OLD_IFS
+
+ if (($(echo "${net_detail_nopolice_arr[((${net_police_arr[0]}-1))]} ${net_police_arr[1]} ${net_police_arr[2]}"|bc) == 1))
+ then #触发告警
+ net_detail_police_t=${net_police_arr[0]}","${net_police_arr[3]}","${net_police_arr[2]}","
+ net_police_flag=1
+ net_police_flag_t=1
+ if ((${net_police_arr[3]} > ${net_police_most_level}))
+ then
+ net_police_most_level=${net_police_arr[3]}
+ net_detail_police=$net_detail_police_t
+ net_baseInfo_police="【${net_detail_nopolice_arr[0]}】"${net_police_arr[4]}${net_detail_nopolice_arr[((${net_police_arr[0]}-1))]}"% i18n_client.GetInfoRun.abnormal_n81i"
+ fi
+ elif [ $net_police_flag_t -eq 0 ]&&[ -z "${net_baseInfo_police}" ];then
+ net_detail_police="\"\",,,"
+ net_baseInfo_police="【${net_detail_nopolice_arr[0]}】"${net_police_arr[4]}${net_detail_nopolice_arr[((${net_police_arr[0]}-1))]}"% i18n_client.GetInfoRun.normal_n81i"
+ fi
+ done
+ netResult_baseInfo_police=${netResult_baseInfo_police}${net_baseInfo_police}
+ else
+ net_detail_police="\"\",,,"
+ fi
+ netResult_detail=${netResult_detail}${net_detail_police}${net_detail_nopolice}
+ else
+ net_disa_baseInfo=${net_disa_baseInfo}${sep}"if"${index}"-"${net_if_name}"i18n_client.SystemInfo.disable_n81i; "
+ fi
+ done
+
+ sys_net_total_num=`echo "${sys_net_total_num}+${#special_detail_rows_arr[@]}"|bc`
+ net_total_num=`echo "$net_total_num+${#special_detail_rows_arr[@]}"|bc`
+ netResult_baseInfo=${netResult_baseInfo}"${sep}if"${index}"-"${#special_detail_rows_arr[@]}"i18n_client.SystemInfo.insert_n81i: "${special_base}${net_avai_baseInfo}${net_disa_baseInfo}$'\n'
+}
+
+# main
+index=1
+flag=1
+while [ $flag -eq 1 ]; do
+ key='ip'${index}
+ value=`eval echo '$'${key}`
+
+ if [ -z ${value} ]
+ then
+ flag=0
+ else
+ getDetail ${key} ${value}
+ let "index++"
+ fi
+done
+
+## result
+# cpu
+cpuResult_baseInfo=${cpuResult_baseInfo}"\""
+if ((${cpu_police_flag} == 0 ))
+ then
+ cpuResult_baseInfo_police="\""${cpuResult_baseInfo}
+ cpu_police_flag=1
+ else
+ cpuResult_baseInfo_police="\""${cpuResult_baseInfo_police}"\""
+ cpu_police_flag=0
+fi
+echo "${cpuResult_taskInfo}""${cpu_police_flag},""\"${cpuResult_coreNum}""i18n_client.SystemInfo.core_n81i: ""${cpuResult_baseInfo}"",""${cpuResult_baseInfo_police}"$'\n'"details,""$((cpuResult_coreNum+index-1))"$'\n'"${cpuResult_detail}" > ${cpu_dataFileDir}/${date}".csv" #${cpuResult}
+
+# mem
+memResult_baseInfo=${memResult_baseInfo}"\""
+if ((${mem_police_flag} == 0 ))
+ then
+ memResult_baseInfo_police=${memResult_baseInfo}
+ mem_police_flag=1
+ else
+ memResult_baseInfo_police="\""${memResult_baseInfo_police}"\""
+ mem_police_flag=0
+fi
+echo "${memResult_taskInfo}"${mem_police_flag}",${memResult_baseInfo}"",""${memResult_baseInfo_police}"$'\n'"details,""$((index-1))"$'\n'"${memResult_detail}" > ${mem_dataFileDir}/${date}".csv" #${memResult}
+
+# disk
+diskResult_baseInfo=${diskResult_baseInfo}"\""
+if ((${disk_police_flag} == 0 ))
+ then
+ diskResult_baseInfo_police=${diskResult_baseInfo}
+ disk_police_flag=1
+ else
+ diskResult_baseInfo_police="\""${diskResult_baseInfo_police}"\""
+ disk_police_flag=0
+fi
+echo "${diskResult_taskInfo}"${disk_police_flag}",${diskResult_baseInfo}"",""${diskResult_baseInfo_police}"$'\n'"details,$disk_total_num"$'\n'"${diskResult_detail}" > ${disk_dataFileDir}/${date}".csv" #${diskResult}
+
+# net
+netResult_baseInfo=${netResult_baseInfo}"\""
+if ((${net_police_flag} == 0 ))
+ then
+ netResult_baseInfo_police=${netResult_baseInfo}
+ net_police_flag=1
+ else
+ netResult_baseInfo_police="\""${netResult_baseInfo_police}"\""
+ net_police_flag=0
+fi
+echo "${netResult_taskInfo}"${net_police_flag}",${netResult_baseInfo}"",""${netResult_baseInfo_police}"$'\n'"details,$net_total_num"$'\n'"${netResult_detail}" > ${net_dataFileDir}/${date}".csv" #${netResult}
+
+# sys
+sysinfo=`cat ${sysinfoPath}`
+echo "${sysResult_taskInfo}""${sysinfo}" > ${sys_dataFileDir}/${date}".csv" #${sysResult}
diff --git a/linuxinstall/detectShell/monitor_net.py b/linuxinstall/detectShell/monitor_net.py
new file mode 100644
index 0000000..9e0b8bc
--- /dev/null
+++ b/linuxinstall/detectShell/monitor_net.py
@@ -0,0 +1,362 @@
+#!/usr/bin/env python
+# -*- coding: UTF-8 -*-
+
+# created by zhongyoub
+'''
+get all net card info and write to file with csv format
+time: ms
+'''
+
+import time,re,os,csv
+import ConfigParser
+import StringIO
+from datetime import datetime
+
+global cfg_path_str
+global nic_path_str
+global marsio_cmd
+cfg_path_str="/home/nms/nc_config/net_net/net_net.cfg"
+nic_path_str="/proc/net/dev"
+marsio_cmd="monit_device"
+
+def round2(a):
+ return ("%.2f"%a)
+
+class monitor_net():
+ def __init__(self):
+ self.seqId="" # seqId
+ self.id="" # 监测设置ID
+ self.cattory="" # 监测类别
+ self.process="" # 进程名称
+
+ self.start_time=int(round(time.time()*1000)) # 监测服务启动时间
+ self.delay=0 # 检测时延
+ self.checktime=int(round(time.time()*1000)) # 本次检测时间
+ self.try_times=1 # 尝试次数
+ self.next_time=0 # 下次监测时间 self.start_time + self.interval
+ self.checkState=1 # 检测状态
+
+ self.checkGap=0
+ self.config_path=cfg_path_str
+ self.nic_path=nic_path_str
+
+ '''
+ nic info
+ '''
+ self.nic_info={"eth_name":"", "rx_packets":0, "tx_packets":0, "rx_bytes":0, "tx_bytes":0,
+ "rx_errs":0, "tx_errs":0, "rx_drop":0, "tx_drops":0, "speed":10000,
+ "rx_bps":0, "tx_bps":0, "rx_pps":0, "tx_pps":0,
+ "rx_err_perc":0, "tx_err_perc":0, "rx_drop_perc":0, "tx_drop_perc":0 }
+ self.marsio_info={"eth_name":"", "rx_packets":0, "tx_packets":0, "rx_bytes":0, "tx_bytes":0,
+ "rx_errs":0, "tx_errs":0, "rx_drop":0, "tx_drops":0, "speed":10000,
+ "rx_bps":0, "tx_bps":0, "rx_pps":0, "tx_pps":0,
+ "rx_err_perc":0, "tx_err_perc":0, "rx_drop_perc":0, "tx_drop_perc":0 }
+ # self.vf_dict={'0':'','1':'','2':''}
+ self.vf_list=[]
+ self.marsio_list=[]
+
+
+ '''
+ read config file
+ '''
+ def read_config(self):
+ '''
+ with open(self.config_path, 'r') as f:
+ section_string = '[dummy_section]\n' + f.read()
+ '''
+
+ config=StringIO.StringIO()
+ config.write('[dummy_section]\n')
+ config.write(open(self.config_path).read())
+ config.seek(0,os.SEEK_SET)
+
+ cfg = ConfigParser.ConfigParser()
+ # cfg.readfp(section_string)
+ cfg.readfp(config)
+ pubInfo=cfg.get("dummy_section", "pubInfo")
+ self.seqId=pubInfo.split(",",3)[0]
+ self.id=pubInfo.split(",",3)[1]
+ self.cattory=pubInfo.split(",",3)[2]
+ self.process=pubInfo.split(",",3)[3]
+
+ self.delay=cfg.get("dummy_section","checkOutTime")
+ self.try_times=cfg.get("dummy_section","checkMaxTimes")
+ self.checkGap=int(cfg.get("dummy_section","checkGap"))*60*1000 # ms
+ self.next_time=self.start_time+self.checkGap
+ self.checkState=cfg.get("dummy_section","checkState")
+
+
+ '''
+ get net card info/home/nms/nc_config/net_net
+ '''
+ def get_vfnet_info(self):
+ net_info=open("/proc/net/dev","r")
+ info=net_info.readlines()
+ net_info.close()
+# print self.marsio_list
+ for line in info: # find nic name like ens*f*
+ self.nic_info={"eth_name":"", "rx_packets":0, "tx_packets":0, "rx_bytes":0, "tx_bytes":0,
+ "rx_errs":0, "tx_errs":0, "rx_drop":0, "tx_drops":0, "speed":10000,
+ "rx_bps":0, "tx_bps":0, "rx_pps":0, "tx_pps":0,
+ "rx_err_perc":0, "tx_err_perc":0, "rx_drop_perc":0, "tx_drop_perc":0 }
+ if re.search(r'ens.f.',line):
+ field=line.split()
+ self.nic_info["eth_name"]=field[0].split(":")[0]
+ self.nic_info["rx_bytes"]=field[1]
+ self.nic_info["rx_packets"]=field[2]
+ self.nic_info["rx_errs"]=field[3]
+ self.nic_info["rx_drop"]=field[4]
+ self.nic_info["tx_bytes"]=field[9]
+ self.nic_info["tx_packets"]=field[10]
+ self.nic_info["tx_errs"]=field[11]
+ self.nic_info["tx_drop"]=field[12]
+ # self.vf_dict[str(i)]=self.nic_info
+ # print "nic_info"
+ # print self.nic_info
+ self.vf_list.append(self.nic_info)
+ # print self.vf_list
+# print self.vf_list[str(i)]
+
+ '''
+ eth_name=field[0].split(":")[0]
+ rx_bytes=field[1]
+ rx_packets=field[2]
+ rx_errs=field[3]
+ rx_drop=field[4]
+ tx_bytes=field[9]
+ tx_packts=field[10]
+ tx_errs=field[11]
+ tx_drop=field[12]
+ return(eth_name, int(rx_bytes),int(rx_packets),int(rx_errs),int(rx_drop),int(tx_bytes),
+ int(tx_packts),int(tx_errs),int(tx_drop))
+ '''
+
+ '''
+ get marsio net card info by open(monit_device)
+ '''
+ def get_marsionet_info(self):
+ info=os.popen(marsio_cmd)
+ for line in info:
+ if re.search(r'ens.f*',line):
+ self.marsio_info["eth_name"]=line.split(",")[1].split(":")[1].strip()
+ if re.search(r'Accu',line):
+ self.marsio_info["rx_packets"]=line.split()[1]
+ self.marsio_info["rx_bytes"]=int(line.split()[2])/8 # bit to byte(float)
+ self.marsio_info["rx_drop"]=line.split()[9]
+ self.marsio_info["rx_errs"]=line.split()[4]
+ self.marsio_info["tx_packets"]=line.split()[6]
+ self.marsio_info["tx_bytes"]=int(line.split()[7])/8
+ self.marsio_info["tx_errs"]=line.split()[8]
+ self.marsio_info['tx_drop']=line.split()[10]
+
+ if re.search(r'Second',line):
+ self.marsio_info["rx_bps"]=line.split()[3]
+ rx_packets=self.marsio_info["rx_pps"]=line.split()[2]
+ tx_packets=self.marsio_info["tx_pps"]=line.split()[7]
+ self.marsio_info["tx_bps"]=line.split()[8]
+
+ rx_err_per=line.split()[5]
+ tx_err_per=line.split()[9]
+ if int(rx_packets)!=0:
+ self.marsio_info["rx_err_perc"]=round2(float(rx_err_per)/int(rx_packets))
+ else:
+ self.marsio_info["rx_err_perc"]=round2(0)
+# print self.marsio_info["rx_err_perc"]
+ if int(tx_packets)!=0:
+ self.marsio_info["tx_err_perc"]=round2(float(tx_err_per)/int(tx_packets))
+ else:
+ self.marsio_info["tx_err_perc"]=round2(0)
+ # print self.marsio_info["tx_err_perc"]
+ rx_drop_per=line.split()[10]
+ tx_drop_per=line.split()[11]
+ if int(rx_packets)!=0:
+ self.marsio_info["rx_drop_perc"]=round2(float(rx_drop_per)/int(rx_packets))
+ else:
+ self.marsio_info["rx_drop_perc"]=round2(0)
+# print self.marsio_info["rx_drop_perc"]
+ if int(tx_packets)!=0:
+ self.marsio_info["tx_drop_perc"]=round2(float(tx_drop_per)/int(tx_packets))
+ else:
+ self.marsio_info["tx_drop_perc"]=round2(0)
+# print self.marsio_info["tx_drop_perc"]
+ self.marsio_list.append(self.marsio_info)
+# print "marsio_info"
+# print self.marsio_info
+
+def list_append(list1, dict1):
+ list1.append(dict1["eth_name"])
+ list1.append(dict1["rx_packets"])
+ list1.append(dict1["tx_packets"])
+ list1.append(dict1["rx_bytes"])
+ list1.append(dict1["tx_bytes"])
+ list1.append(dict1["rx_errs"])
+ list1.append(dict1["tx_errs"])
+ list1.append(dict1["rx_drop"])
+ list1.append(dict1["tx_drop"])
+ list1.append(dict1["speed"])
+ list1.append(dict1["rx_bps"])
+ list1.append(dict1["tx_bps"])
+ list1.append(dict1["rx_pps"])
+ list1.append(dict1["tx_pps"])
+ list1.append(dict1["rx_err_perc"])
+ list1.append(dict1["tx_err_perc"])
+ list1.append(dict1["rx_drop_perc"])
+ list1.append(dict1["tx_drop_perc"])
+
+def main():
+ monitor=monitor_net()
+ monitor.read_config()
+ monitor.chectime=int(round(time.time()*1000))
+ monitor.get_marsionet_info()
+ list3=monitor.marsio_list
+ monitor.get_vfnet_info()
+ list1=monitor.vf_list
+# print "list1"
+# print list1
+ monitor.vf_list=[]
+# print monitor.vf_list
+ # monitor.vf_list.clear()
+ time.sleep(1)
+ monitor.get_vfnet_info()
+ list2=monitor.vf_list
+# print "list2"
+# print list2
+ for x, y in zip(list1,list2):
+ rx_bytes_pers=int(y["rx_bytes"])-int(x["rx_bytes"])
+ tx_bytes_pers=int(y["tx_bytes"])-int(x["tx_bytes"])
+ rx_packet_pers=float(y["rx_packets"])-int(x["rx_packets"])
+ tx_packet_pers=float(y["tx_packets"])-int(x["tx_packets"])
+
+ rx_bps=rx_bytes_pers*8
+ rx_pps=rx_packet_pers
+ tx_bps=tx_bytes_pers*8
+ tx_pps=tx_packet_pers
+ if rx_packet_pers >0:
+ rx_err_rate=(int(y["rx_errs"])-int(x["rx_errs"]))/rx_packet_pers
+ else:
+ rx_err_rate=0
+ if tx_packet_pers>0:
+ tx_err_rate=(int(y["tx_errs"])-int(x["tx_errs"]))/tx_packet_pers
+ else:
+ tx_err_rate=0
+ if rx_packet_pers>0:
+ rx_drop_rate=(int(y["rx_drop"])-int(x["rx_drop"]))/rx_packet_pers
+ else:
+ rx_drop_rate=0
+ if tx_packet_pers>0:
+ tx_drop_rate=(int(y["tx_drop"])-int(x["tx_drop"]))/tx_packet_pers
+ else:
+ tx_drop_rate=0
+ '''
+ y["rx_bps"]=round2(rx_bps)
+ y["rx_pps"]=round2(rx_pps)
+ y["tx_bps"]=round2(tx_bps)
+ y["tx_pps"]=round2(tx_pps)
+ '''
+ y["rx_err_perc"]=round2(rx_err_rate)
+ y["tx_err_perc"]=round2(tx_err_rate)
+ y["rx_drop_perc"]=round2(rx_drop_rate)
+ y["tx_drop_perc"]=round2(tx_drop_rate)
+
+# print list2
+# print list3
+ vf_num=len(monitor.vf_list)
+ # print monitor.vf_list
+ # print vf_num
+ marsio_num=len(list3)
+
+ csv_file=datetime.now().strftime("%Y%m%d%H%M%S")
+
+ header=[monitor.seqId,monitor.id,monitor.cattory,monitor.process,monitor.start_time,monitor.delay,
+ monitor.chectime,monitor.try_times,monitor.next_time,monitor.checkState]
+
+ first_str=str(vf_num)+"i18n_client.SystemInfo.insert_n81i: "
+ for each in list2:
+ first_str=first_str+"$@$"+each["eth_name"]+ \
+ " i18n_client.SystemInfo.netSpeed_n81i10000Mbps,"+"i18n_client.SystemInfo.input_n81i"+ \
+ str(each["rx_bps"])+"bps、"+str(each["rx_pps"])+"pps,"+"Output"+str(each["tx_bps"])+"bps、"+str(each["tx_pps"])+ \
+ "pps;"
+
+ second_str=str(marsio_num)+"i18n_client.SystemInfo.insert_n81i: "
+ for each in list3:
+ second_str=second_str+"$@$"+each["eth_name"]+ \
+ " i18n_client.SystemInfo.netSpeed_n81i10000Mbps,"+"i18n_client.SystemInfo.input_n81i"+ \
+ str(each["rx_bps"])+"bps、 "+str(each["rx_pps"])+"pps,"+"Output"+str(each["tx_bps"])+"bps、"+str(each["tx_pps"])+ \
+ "pps;"
+
+ four_1=['\"','','']
+ four_2=['\"','','']
+ four_3=['\"','','']
+ if len(list2)>1:
+ first_dict=list2[0]
+ second_dict=list2[1]
+ list_append(four_1,first_dict)
+ list_append(four_2,second_dict)
+ elif len(list2)==1:
+ first_dict=list2[0]
+ list_append(four_1,first_dict)
+ four_2=[]
+ else:
+ four_1=[]
+ four_2=[]
+ if len(list3)>0:
+ three_dict=list3[0]
+ '''
+ three_dict["rx_bps"]=round2(int(three_dict["rx_bps"]))
+ three_dict["rx_pps"]=round2(int(three_dict["rx_pps"]))
+ three_dict["tx_bps"]=round2(int(three_dict["tx_bps"]))
+ three_dict["tx_bps"]=round2(int(three_dict["tx_bps"]))
+ three_dict["rx_err_perc"]=round2(int(three_dict["rx_err_perc"]))
+ three_dict["tx_err_perc"]=round2(int(three_dict["tx_err_perc"]))
+ three_dict["rx_drop_perc"]=round2(int(three_dict["rx_drop_perc"]))
+ three_dict["tx_drop_perc"]=round2(int(three_dict["tx_drop_perc"]))
+ '''
+ list_append(four_3,three_dict)
+ else:
+ four_3=[]
+ '''
+ four_1.append(first_list["eth_name"])
+ four_1.append(first_list["rx_packets"])
+ four_1.append(first_list["tx_packets"])
+ four_1.append(first_list["rx_bytes"])
+ four_1.append(first_list["tx_bytes"])
+ four_1.append(first_list["rx_errs"])
+ four_1.append(first_list["tx_errs"])
+ four_1.append(first_list["rx_drop"])
+ four_1.append(first_list["tx_drop"])
+ four_1.append(first_list["speed"])
+ four_1.append(first_list["rx_bps"])
+ four_1.append(first_list["tx_bps"])
+ four_1.append(first_list["rx_pps"])
+ four_1.append(first_list["tx_pps"])
+ four_1.append(first_list["rx_err_perc"])
+ four_1.append(first_list["tx_err_perc"])
+ four_1.append(first_list["rx_drop_perc"])
+ '''
+# print four_1
+# print four_2
+# print four_3
+ header.append(first_str)
+ header.append(second_str)
+ two_list=["details"]
+ two_list.append((vf_num+marsio_num))
+
+ with open(csv_file+".csv","w") as f:
+ f_writer=csv.writer(f,lineterminator='\n')
+ f_writer.writerow(header)
+ f_writer.writerow(two_list)
+ if len(four_1):
+ f_writer.writerow(four_1)
+ if len(four_2):
+ f_writer.writerow(four_2)
+ if len(four_3):
+ f_writer.writerow(four_3)
+ f.close()
+
+if __name__ == '__main__':
+ main()
+ exit(0)
+
+
+
+
diff --git a/linuxinstall/detectShell/remote_net.sh b/linuxinstall/detectShell/remote_net.sh
new file mode 100644
index 0000000..0476b01
--- /dev/null
+++ b/linuxinstall/detectShell/remote_net.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+python /root/zhong/monitor_net.py #py脚本的路径,需要手动填写
+cat `ls|grep csv`
+rm -f *.csv
diff --git a/linuxinstall/detectShell/sysinfo b/linuxinstall/detectShell/sysinfo
new file mode 100644
index 0000000..47eeb21
--- /dev/null
+++ b/linuxinstall/detectShell/sysinfo
@@ -0,0 +1,29 @@
+"$@$1-localhost.localdomain): 3.10.0-693.el7.x86_64, 8i18n_client.SystemInfo.message.core_n81i CPU, 1466.65MHz, 15.44i18n_client.SystemInfo.message.memery_n81i, 1885477224i18n_client.SystemInfo.message.disk_n81i, 4i18n_client.SystemInfo.message.netcard_n81i$@$2-localhost.localdomain): 3.10.0-693.el7.x86_64, 8i18n_client.SystemInfo.message.core_n81i CPU, 1498.72MHz, 15.44i18n_client.SystemInfo.message.memery_n81i, 1885477224i18n_client.SystemInfo.message.disk_n81i, 4i18n_client.SystemInfo.message.netcard_n81i$@$3-localhost.localdomain): 3.10.0-693.el7.x86_64, 8i18n_client.SystemInfo.message.core_n81i CPU, 1567.90MHz, 15.44i18n_client.SystemInfo.message.memery_n81i, 1885477224i18n_client.SystemInfo.message.disk_n81i, 4i18n_client.SystemInfo.message.netcard_n81i","$@$1-localhost.localdomain): 3.10.0-693.el7.x86_64, 8i18n_client.SystemInfo.message.core_n81i CPU, 1466.65MHz, 15.44i18n_client.SystemInfo.message.memery_n81i, 1885477224i18n_client.SystemInfo.message.disk_n81i, 4i18n_client.SystemInfo.message.netcard_n81i$@$2-localhost.localdomain): 3.10.0-693.el7.x86_64, 8i18n_client.SystemInfo.message.core_n81i CPU, 1498.72MHz, 15.44i18n_client.SystemInfo.message.memery_n81i, 1885477224i18n_client.SystemInfo.message.disk_n81i, 4i18n_client.SystemInfo.message.netcard_n81i$@$3-localhost.localdomain): 3.10.0-693.el7.x86_64, 8i18n_client.SystemInfo.message.core_n81i CPU, 1567.90MHz, 15.44i18n_client.SystemInfo.message.memery_n81i, 1885477224i18n_client.SystemInfo.message.disk_n81i, 4i18n_client.SystemInfo.message.netcard_n81i"
+details,3
+"",,,1-localhost.localdomain),"3.10.0-693.el7.x86_64",8,1466.65,15.44,63.99,1885477224,4,
+"",,,2-localhost.localdomain),"3.10.0-693.el7.x86_64",8,1498.72,15.44,63.99,1885477224,4,
+"",,,3-localhost.localdomain),"3.10.0-693.el7.x86_64",8,1567.90,15.44,63.99,1885477224,4,
+disk,9
+1:/,1884767064,
+1:/boot,505580,
+1:/boot/efi,204580,
+2:/,1884767064,
+2:/boot,505580,
+2:/boot/efi,204580,
+3:/,1884767064,
+3:/boot,505580,
+3:/boot/efi,204580,
+net,12
+1-enp0s31f6,Available,100,10.0.6.247,255.255.255.0,10.0.6.100,30:9c:23:4d:9c:93,
+1-wlp3s0,Unavailable,0,0.0.0.0,0.0.0.0,0,0,0,0,f8:28:19:d4:13:db,
+1-virbr0-nic,Unavailable,10,0.0.0.0,0.0.0.0,0,0,0,0,52:54:00:37:52:32,
+1-virbr0,Available,0,192.168.122.1,255.255.255.0,10.0.6.100,52:54:00:37:52:32,
+2-enp0s31f6,Available,100,10.0.6.247,255.255.255.0,10.0.6.100,30:9c:23:4d:9c:93,
+2-wlp3s0,Unavailable,0,0.0.0.0,0.0.0.0,0,0,0,0,f8:28:19:d4:13:db,
+2-virbr0-nic,Unavailable,10,0.0.0.0,0.0.0.0,0,0,0,0,52:54:00:37:52:32,
+2-virbr0,Available,0,192.168.122.1,255.255.255.0,10.0.6.100,52:54:00:37:52:32,
+3-enp0s31f6,Available,100,10.0.6.247,255.255.255.0,10.0.6.100,30:9c:23:4d:9c:93,
+3-wlp3s0,Unavailable,0,0.0.0.0,0.0.0.0,0,0,0,0,f8:28:19:d4:13:db,
+3-virbr0-nic,Unavailable,10,0.0.0.0,0.0.0.0,0,0,0,0,52:54:00:37:52:32,
+3-virbr0,Available,0,192.168.122.1,255.255.255.0,10.0.6.100,52:54:00:37:52:32,
+
diff --git a/linuxinstall/install.sh b/linuxinstall/install.sh
new file mode 100644
index 0000000..8164514
--- /dev/null
+++ b/linuxinstall/install.sh
@@ -0,0 +1,215 @@
+#!/bin/bash
+
+# --- set default value
+DEFAULT_DATA_DIR="/home/nms/nmsdata"
+DEFAULT_INSTALL_DIR="/home/nms/nmsclient"
+
+# --- set install dir
+PRG="$0"
+PRGDIR=`dirname "$PRG"`
+CUR_PRGDIR=`cd "$PRGDIR"; pwd`
+
+if [ $# = 0 ]
+ then
+ echo -n "enter intall dir [default: $DEFAULT_INSTALL_DIR]:"
+ read INSTALL_DIR
+ if [ -z "$INSTALL_DIR" ]; then
+ INSTALL_DIR="$DEFAULT_INSTALL_DIR"
+ fi
+else
+ INSTALL_DIR="$1"
+fi
+
+if [ ! -d $INSTALL_DIR ]
+ then
+ mkdir -p $INSTALL_DIR
+fi
+
+INSTALL_DIR=`cd "$INSTALL_DIR"; pwd`
+
+# --- check jdk and jdk-version
+install_jdk=0
+javaversion=`java -version 2>&1|grep "java version"`
+if [ -n "$javaversion" ]
+ then
+ # javaversion=${javaversion:14:3}
+ javavmajor=`echo $javaversion | cut -c15`
+ javavminor=`echo $javaversion | cut -c17`
+# OS_TYPE=$( lsb_release -d| cut -d: -f2| cut -f2 )
+# if [ "`echo $OS_TYPE | cut -c1-6`" = "Ubuntu" ]
+# then
+# if [ 2 -gt $javavmajor ]; then
+# if [ 6 -gt $javavminor ]; then
+# install_jdk=1
+# fi
+# fi
+# else
+ if [[ 2 -gt $javavmajor && 6 -gt $javavminor ]]; then
+ install_jdk=1
+ fi
+# fi
+ else
+ install_jdk=1
+fi
+
+# --- install jdk
+cd "$INSTALL_DIR"/..
+NMS_JDK="$(pwd)/nmsjdk"
+JDK_DIR="$(pwd)/jdk1.7.0_80"
+JDK_BIN_FILE=$CUR_PRGDIR"/jre_install/jdk-7u80-linux-i586.tar.gz"
+if [ "`uname -i`" = "x86_64" ];then
+ JDK_BIN_FILE=$CUR_PRGDIR"/jre_install/jdk-7u80-linux-x64.tar.gz"
+fi
+if [ $install_jdk = 1 ]
+ then
+ if [ ! -e "$NMS_JDK" ]
+ then
+ echo "JDK bin file: $JDK_BIN_FILE"
+ echo "now, install jdk: $JDK_DIR"
+ sleep 3
+ if [ ! -e "$JDK_DIR" ];then
+ mkdir -p $JDK_DIR
+ fi
+ tar -xzf $JDK_BIN_FILE -C $JDK_DIR --strip-components=1 || installJdk=1
+ if [ -n "$installJdk" ];then
+ echo "install jdk failure, exit program"
+ exit 1
+ fi
+ ln -s $JDK_DIR $NMS_JDK
+ echo "install jdk done"
+ fi
+elif [ -n "$JAVA_HOME" ];then
+ ln -s $JAVA_HOME $NMS_JDK
+else
+ echo "JDK bin file: $JDK_BIN_FILE"
+ echo "now, install jdk: $JDK_DIR"
+ sleep 3
+ if [ ! -e "$JDK_DIR" ];then
+ mkdir -p $JDK_DIR
+ fi
+ tar -xzf $JDK_BIN_FILE -C $JDK_DIR --strip-components=1 || installJdk=1
+ if [ -n "$installJdk" ];then
+ echo "install jdk failure, exit program"
+ exit 1
+ fi
+ ln -s $JDK_DIR $NMS_JDK
+ echo "install jdk done"
+fi
+cd "$CUR_PRGDIR"
+
+echo "==========================================="
+echo "NMS_JDK: $NMS_JDK"
+echo "INSTALL_DIR: $INSTALL_DIR"
+echo "==========================================="
+
+# --- copy file to install_dir
+if [ "$INSTALL_DIR" == "$CUR_PRGDIR" ]
+ then
+ echo "install directory is current program directory..."
+else
+ echo "install program, it may take a few time..."
+ CP_DIR=(
+ bin
+ lib
+ conf
+ shell
+ )
+ #cp
+ for CP_NAME in ${CP_DIR[@]}
+ do
+ cp -a $CUR_PRGDIR"/$CP_NAME" $INSTALL_DIR
+ done
+fi
+
+
+function modify_file(){
+ if [ $# != 2 ]
+ then
+ echo "usage: modify_file [prop_name] [prop_value]"
+ exit 0
+ fi
+ prop_name="$1"
+ prop_value="$2"
+ #echo "modify_file $prop_name $prop_value"
+ if [ -z "$(cat $PROP_FILE |grep $prop_name)" ]
+ then
+ echo "" >> $PROP_FILE
+ echo "$prop_name=$prop_value" >> $PROP_FILE
+ else
+ sed -i "s@^$prop_name.*@$prop_name=$prop_value@" $PROP_FILE
+ fi
+}
+
+# --- modify property
+#-------------file path
+echo -n "please enter data path, notice: this path not within $INSTALL_DIR, [default $DEFAULT_DATA_DIR]: "
+read path
+if [ -z "$path" ]
+ then
+ path="$DEFAULT_DATA_DIR"
+fi
+#-------------include path
+echo -n "delete include path [default $INSTALL_DIR,$path ]: "
+read include_path
+if [ -z "$include_path" ]
+ then
+ include_path="$INSTALL_DIR,$path"
+else
+ include_path="$INSTALL_DIR,$path,$include_path"
+fi
+#-------------exclude path
+echo -n "delete exclude path [default $INSTALL_DIR/bin,$INSTALL_DIR/lib,$INSTALL_DIR/shell,$INSTALL_DIR/conf ]: "
+read exclude_path
+if [ -z "$exclude_path" ]
+ then
+ exclude_path="$INSTALL_DIR/bin,$INSTALL_DIR/lib,$INSTALL_DIR/shell"
+else
+ exclude_path="$$INSTALL_DIR/bin,$INSTALL_DIR/lib,$INSTALL_DIR/shell,$exclude_path"
+fi
+#-------------nmsserver ip
+echo -n "enter DataController ip: "
+read server_ip
+while [ -z "$server_ip" ]
+ do
+ echo -n "DataController ip cannot null, please enter ip: "
+ read server_ip
+done
+#-------------log4j dir
+echo -n "enter logs path [default $path/nc_logs]: "
+read logs_path
+if [ -z "$logs_path" ]
+ then
+ logs_path="$path/nc_logs"
+fi
+
+#path=${path//\//\\/}
+
+PROP_FILE=$INSTALL_DIR"/conf/myconfig.properties"
+#echo "PROP_FILE: $PROP_FILE"
+modify_file "local.data.path" $path
+modify_file "common.del.path.include" $include_path
+modify_file "common.del.path.exclude" $exclude_path
+modify_file "server_host" $server_ip
+
+# modify log4j
+PROP_FILE=$INSTALL_DIR"/conf/log4j.properties"
+encoding=${LANG#*.}
+modify_file "log4j.appender.stdout.encoding" $encoding
+modify_file "log4j.appender.debugAppender.encoding" $encoding
+modify_file "log4j.appender.infoAppender.encoding" $encoding
+modify_file "log4j.appender.debugAppender.File" "$logs_path/nmsclient_debug.log"
+modify_file "log4j.appender.infoAppender.File" "$logs_path/nmsclient_info.log"
+
+#permit
+cd $INSTALL_DIR"/shell"
+chmod 755 *.sh
+cd $CUR_PRGDIR
+
+if [ -z "$(cat /etc/rc.local|grep $INSTALL_DIR"/shell/startup.sh")" ]
+then
+ echo $INSTALL_DIR"/shell/startup.sh" >> /etc/rc.local
+fi
+
+echo ""
+echo "install successed..."
+echo "please use [$INSTALL_DIR/shell/startup.sh] to run the program..."
diff --git a/linuxinstall/readme.txt b/linuxinstall/readme.txt
new file mode 100644
index 0000000..d8631d2
--- /dev/null
+++ b/linuxinstall/readme.txt
@@ -0,0 +1,59 @@
+NC--Linux下的安装文件结构:
+
+|--NC_install
+| |--autoinstall.sh
+| |--install.sh
+| |--bin
+| | |--cer
+| | | |--client_ks
+| | | |--client_ts
+| | |--fileComment.jar
+| | |--nmsclient.jar
+| |--conf
+| | |--jvm.conf
+| | |--log4j.properties
+| | |--myconfig.properties
+| | |--version.properties
+| |--jre_install
+| | |--jre-6u45-linux-i586.bin
+| | |--jre-6u45-linux-x64.bin
+| |--lib
+| | |--ant.jar
+| | |--commons-beanutils-1.7.jar
+| | |--commons-collections.jar
+| | |--commons-io.jar
+| | |--commons-lang.jar
+| | |--commons-logging.jar
+| | |--ezmorph-1.0.4.jar
+| | |--java-unrar-0.3.jar
+| | |--javatar-2.5.jar
+| | |--jreloader.jar
+| | |--json-lib-2.2.2-jdk15.jar
+| | |--junit.jar
+| | |--log4j-1.2.15.jar
+| | |--ostermillerutils_1_07_00.jar
+| | |--sigar.jar
+| | |--libsigar-amd64-linux.so
+| | |--libsigar-x86-linux.so
+| |--shell
+| | |--check_userpass.sh
+| | |--execCmdBySu.sh
+| | |--nmsagent.sh
+| | |--nmsclient_shouhu.sh
+| | |--restart.sh
+| | |--shutdown.sh
+| | |--startup.sh
+
+
+脚本说明:
+
+1、安装脚本install.sh:
+ 先检查JDK是否安装且Java版本是否适合,来选择是否安装自带的JRE。
+ 若需自带安装,则判断系统的位数,相应安装32位JDK或64位JDK。
+ JDK安装目录为/home/nms/jre1.6.0_45, 并建软连接/home/nms/nmsjdk
+ 若系统已安装相应版本的JDK,则建连接/home/nms/nmsjdk
+
+2、自动安装脚本autoinstall.sh
+ JDK的检查逻辑与install.sh一样,只是所有参数都按默认值,不需人工交互,并安装完成后自动启动。
+
+3、shell目录下,脚本nmsagent.sh和datacontroller.sh,不检查JDK版本,只检查nmsjdk是否存在且nmsjdk/bin/Java命令能否正常使用
\ No newline at end of file
diff --git a/linuxinstall/shell/check_userpass.sh b/linuxinstall/shell/check_userpass.sh
new file mode 100644
index 0000000..6e6dc7d
--- /dev/null
+++ b/linuxinstall/shell/check_userpass.sh
@@ -0,0 +1,47 @@
+#!/usr/bin/expect
+
+if {$argc!=3} {
+ send_user "usage: check_userpass.sh ip name password\n"
+ exit 1
+}
+
+set host [lindex $argv 0]
+set user [lindex $argv 1]
+set pass [lindex $argv 2]
+
+set isSend 0
+spawn ssh -t "${user}@${host}"
+
+while 1 {
+ expect {
+ "* (yes/no)*" {send "yes\r"}
+ "* *" {
+ send "${pass}\r"
+ set isSend 1
+ }
+ "* password:*" {
+ send "${pass}\r"
+ set isSend 1
+ }
+ "Permission denied" {
+ send_user "Not allowed\n"
+ exit 1
+ }
+ "*~]" {
+ send_user "OK\n"
+ send "exit\r"
+ exit 0
+ }
+ "Last login:*" {
+ send_user "OK\n"
+ send "exit\r"
+ exit 0
+ }
+ default {
+ send_user "error\n"
+ exit 2
+ }
+ }
+}
+
+
diff --git a/linuxinstall/shell/execCmdBySu.sh b/linuxinstall/shell/execCmdBySu.sh
new file mode 100644
index 0000000..4a94436
--- /dev/null
+++ b/linuxinstall/shell/execCmdBySu.sh
@@ -0,0 +1,78 @@
+#!/usr/bin/expect
+
+if {$argc!=4&&$argc!=3} {
+ send_user "usage: execCmdBySu.sh resultFlag command name \[password\]\n"
+ exit 1
+}
+# 0ִȡý1ִ&̨
+set resultFlag [lindex $argv 0]
+set cmd [lindex $argv 1]
+set user [lindex $argv 2]
+set pass [lindex $argv 3]
+
+set timeout 30
+set flag 0
+
+#رտ̨Ϣ
+log_user 0
+
+spawn whoami
+expect "root" {set flag 1}
+
+# ִ${cmd}"echo $?",Ϊȡcmdеʵʽ, εصϢ
+if {$resultFlag==0} {
+ spawn su - -c "${cmd};echo $?" ${user}
+
+ #ǰ¼ûroot
+ if {$flag==0} {
+ expect {
+ "*" {send ${pass}\r}
+ "password*" {send ${pass}\r}
+ default {
+ send_error "error\n"
+ exit 2
+ }
+ }
+ }
+
+ #̨Ϣ
+ log_user 1
+
+ expect {
+ "* 벻ȷ*" {exit 1}
+ #----˴ĿΪܶȡ
+ eof {exit 0}
+ }
+
+} else {
+ spawn su - -c "${cmd}" ${user}
+
+ #ǰ¼ûroot
+ if {$flag==0} {
+ expect {
+ "*" {send ${pass}\r}
+ "password*" {send ${pass}\r}
+ default {
+ send_error "error\n"
+ exit 2
+ }
+ }
+ }
+ expect {
+ "* 벻ȷ*" {exit 1}
+ #----Ϊ&̨
+ eof {
+ send_user "ok\n"
+ exit 0
+ }
+ }
+}
+interact
+exit 0
+
+
+#overlay chess
+#overlay program args
+#disconnect
+#close -onexec 0 -i ${spawn_id}
+#remove_nulls 1
\ No newline at end of file
diff --git a/linuxinstall/shell/nmsagent.sh b/linuxinstall/shell/nmsagent.sh
new file mode 100644
index 0000000..9450828
--- /dev/null
+++ b/linuxinstall/shell/nmsagent.sh
@@ -0,0 +1,260 @@
+#!/bin/sh
+
+# -----------------------------------------------------------------------------
+# Start/Stop Script for the NMS Client
+#
+# Environment Variable Prequisites
+#
+# NMSCLEINT_HOME May point at your Catalina "build" directory.
+#
+# NMSCLIENT_TASKDIR (Optional) Directory path location of taskresult directory
+# Defaults to %NMSCLIENT_HOME%/task.
+#
+# NMSCLIENT_TMPDIR (Optional) Directory path location of temporary directory
+# the JVM should use (java.io.tmpdir). Defaults to
+# $NMSCLIENT_HOME/temp.
+#
+# JAVA_HOME Must point at your Java Development Kit installation.
+# Required to run the with the "debug" argument.
+#
+# -----------------------------------------------------------------------------
+# resolve links - $0 may be a softlink
+PRG="$0"
+
+#OS_TYPE=$( lsb_release -d| cut -d: -f2| cut -f2 )
+#echo $OS_TYPE
+while [ -h "$PRG" ]; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`/"$link"
+ fi
+done
+
+# Get standard environment variables
+PRGDIR=`dirname "$PRG"`
+
+# Only set NMSCLEINT_HOME if not already set
+[ -z "$NMSCLEINT_HOME" ] && NMSCLEINT_HOME=`cd "$PRGDIR/.." ; pwd`
+cd "$NMSCLEINT_HOME"/shell
+
+if [ -z "$NMSCLIENT_TMPDIR" ] ; then
+ NMSCLIENT_TMPDIR="$NMSCLEINT_HOME"/temp
+fi
+if [ ! -d $NMSCLIENT_TMPDIR ]
+ then
+ mkdir $NMSCLIENT_TMPDIR
+fi
+
+if [ -n "$4" ]; then
+ NMSCLIENT_TASKDIR=`dirname "$4"`
+fi
+if [ -z "$NMSCLIENT_TASKDIR" ] ; then
+ NMSCLIENT_TASKDIR="$NMSCLEINT_HOME"/task
+fi
+
+
+# -------- check jdk
+# check nmsjdk
+NMS_HOME=`cd "$NMSCLEINT_HOME/.." ; pwd`
+cd "$NMSCLEINT_HOME"/shell
+NMS_JDK="$NMS_HOME"/nmsjdk
+if [ ! -e "$NMS_JDK" ]
+ then
+ echo "$NMS_JDK not exist"
+ exit 0
+fi
+# check java -version
+javaversion=`$NMS_JDK/bin/java -version 2>&1|grep "java version"`
+if [ ! -n "$javaversion" ]
+ then
+ echo "$NMS_JDK cannot use, please install"
+ exit 0
+fi
+
+# -------- set jdk path
+export JAVA_HOME=$NMS_JDK
+export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
+export PATH=$JAVA_HOME/bin:$PATH
+
+temp=$CLASSPATH
+#setting libs path
+libs="$NMSCLEINT_HOME"/lib/*
+append(){
+ temp=$temp":"$1
+}
+for file in $libs; do
+ append $file
+done
+jars="$NMSCLEINT_HOME"/bin/*
+for file in $jars; do
+ append $file
+done
+export NC_CLASSPATH=$temp:.:$NMSCLEINT_HOME/conf
+export LD_LIBRARY_PATH=$NMSCLEINT_HOME/lib
+export LANG=zh_CN.UTF-8
+
+# Bugzilla 37848: When no TTY is available, don't output to console
+have_tty=0
+if [ "`tty`" != "not a tty" ]; then
+ have_tty=1
+fi
+
+# ----- Execute The Requested Command -----------------------------------------
+
+# Bugzilla 37848: only output this if we have a TTY
+if [ $have_tty -eq 1 ]; then
+ echo "Using NMSCLEINT_HOME: $NMSCLEINT_HOME"
+ echo "Using NMSCLIENT_TMPDIR: $NMSCLIENT_TMPDIR"
+ echo "Using JAVA_HOME: $JAVA_HOME"
+ echo "Using CLASSPATH: $NC_CLASSPATH"
+fi
+
+# ---- get jvm param
+jvmconf_file=$NMSCLEINT_HOME"/conf/jvm.conf"
+if [ -f $jvmconf_file ]
+ then
+ #sed -n '/Xmx=/p' $jvmconf_file | sed 's/Xmx=//g'
+ XmsOpt=`sed -n '/-Xms/p' $jvmconf_file`
+ XmxOpt=`sed -n '/-Xmx/p' $jvmconf_file`
+ JAVA_OPTS="$XmsOpt $XmxOpt -XX:+UseParNewGC"
+else
+ JAVA_OPTS="-Xms64m -Xmx128m -XX:+UseParNewGC"
+fi
+
+program="com.nis.nmsclient.NmsClient"
+name="NmsClient"
+
+proc_id=
+
+# handler agent upgrade result
+handleTask()
+{
+ if [ -d "$NMSCLIENT_TASKDIR" ]
+ then
+ cd $NMSCLIENT_TASKDIR
+ for i in *.upgrade;do mv "$i" "${i%.upgrade}.result";done >/dev/null 2>&1
+ cd "$NMSCLEINT_HOME"/shell
+ fi
+}
+writePid()
+{
+ ps aux|grep java|grep $program|grep -v grep|awk '{print $2}' > $NMSCLIENT_TMPDIR/agentPid.temp
+}
+getPid()
+{
+ unset proc_id
+ proc_id=`ps aux|grep java|grep $program|grep -v grep|awk '{print $2}'`
+}
+shouhu_proc="$NMSCLEINT_HOME"/shell/nmsclient_shouhu.sh
+stopShouhuProc()
+{
+ shouhu_proc_id=`ps aux|grep $shouhu_proc|grep -v grep|awk '{print $2}'`
+ if [ -n "$shouhu_proc_id" ]
+ then
+ echo "kill shouhu process ....."
+ kill -9 $shouhu_proc_id
+ fi
+}
+startShouhuProc()
+{
+ shouhu_proc_id=`ps aux|grep $shouhu_proc|grep -v grep|awk '{print $2}'`
+ if [ ! -n "$shouhu_proc_id" ]
+ then
+ echo "start shouhu process ....."
+ nohup $shouhu_proc $NMSCLIENT_TASKDIR >/dev/null &
+ fi
+}
+if [ "$1" = "start" ] ; then
+ getPid
+ if [ -n "$proc_id" ]
+ then
+ echo "$name already running......"
+ else
+ nohup java $JAVA_OPTS -classpath $NC_CLASSPATH $program >/dev/null &
+ sleep 3
+ getPid
+ if [ -n "$proc_id" ]
+ then
+ echo "$name start success!!!!!"
+ writePid
+ else
+ echo "$name start error!!!!!"
+ fi
+ handleTask
+ fi
+elif [ "$1" = "stop" ]; then
+ getPid
+ if [ -n "$proc_id" ]
+ then
+ stopShouhuProc
+ sleep 1
+ echo "$name is start, now kill......"
+ kill -9 $proc_id
+ writePid
+ echo "$name kill ok !!!!!!!!!!!!!"
+ else
+ echo "$name is not start!!!!!!!!!!!"
+ fi
+elif [ "$1" = "restart" ] ; then
+ getPid
+ if [ -n "$proc_id" ]
+ then
+ echo "$name is start, now restart......"
+ stopShouhuProc
+ sleep 1
+ startShouhuProc
+ kill -9 $proc_id
+ getPid
+ if [ -n "$proc_id" ]
+ then
+ echo "$name stop error!!!!!!!!!!"
+ handleTask
+ exit 1
+ fi
+ else
+ echo "$name is not start, now start......"
+ fi
+ copyError=
+ if [ -n "$2" ] ; then
+ srcFile=$2
+ updateFile=/dev/null
+ if [ -d "$2" ]; then
+ srcFile="$2"/*
+ fi
+ if [ -n "$4" ]; then
+ updateFile=$4
+ fi
+ if [ -n "$3" ]; then
+ unalias cp >/dev/null 2>&1
+ cp -rvf $srcFile $3 >>$updateFile 2>&1 ||copyError=1
+ fi
+ rm -rf $2 >/dev/null 2>&1
+ fi
+ if [ -n "$copyError" ]
+ then
+ #----copy error
+ handleTask
+ else
+ #--------copy right, start proc
+ nohup java $JAVA_OPTS -classpath $NC_CLASSPATH $program >/dev/null &
+ sleep 3
+ getPid
+ if [ -n "$proc_id" ]
+ then
+ writePid
+ else
+ echo "$name restart error!!!!!!!!!!"
+ handleTask
+ fi
+ fi
+
+else
+ echo "Usage: nmsagent.sh ( commands ... )"
+ echo "commands:"
+ echo " start Start $name in a separate window"
+ echo " restart ReStart $name in a separate window"
+ echo " stop Stop $name"
+fi
diff --git a/linuxinstall/shell/nmsclient_shouhu.sh b/linuxinstall/shell/nmsclient_shouhu.sh
new file mode 100644
index 0000000..821884f
--- /dev/null
+++ b/linuxinstall/shell/nmsclient_shouhu.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+# resolve links - $0 may be a softlink
+PRG="$0"
+
+while [ -h "$PRG" ]; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`/"$link"
+ fi
+done
+
+# Get standard environment variables
+PRGDIR=`dirname "$PRG"`
+
+# Only set NMSCLEINT_HOME if not already set
+[ -z "$NMSCLEINT_HOME" ] && NMSCLEINT_HOME=`cd "$PRGDIR/.." ; pwd`
+cd "$NMSCLEINT_HOME"/shell
+
+NC_TASKDIR="$1"
+log_file="$NMSCLEINT_HOME"/temp/job.log
+echo "NC_TASKDIR:$NC_TASKDIR" >> $log_file
+
+#count=1
+while [ 1 -eq 1 ]
+do
+ sleep 50
+ if [ `ps -ef | grep NmsClient | grep -v grep | wc -l` -lt 1 ]
+ then
+ echo -n "Down at:" >> $log_file
+ date >> $log_file
+ # ---------- handler agent upgrade result
+ if [ -d "$NC_TASKDIR" ]
+ then
+ cd $NC_TASKDIR
+ for i in *.upgrade;do mv "$i" "${i%.upgrade}.result";done >>$log_file 2>&1
+ cd "$NMSCLEINT_HOME"/shell
+ fi
+ # --------- start NC
+ "$NMSCLEINT_HOME"/shell/startup.sh
+ echo "NmsClient start...." >>$log_file
+ fi
+ sleep 1
+ #count=$count+1
+
+ #if [ count -eq 900 ]
+ #then
+ # jpid=`ps -ef | grep java | grep -v grep | cut -c0-5`
+ # kill -9 $jpid
+ # count=1
+ #fi
+done
\ No newline at end of file
diff --git a/linuxinstall/shell/restart.sh b/linuxinstall/shell/restart.sh
new file mode 100644
index 0000000..f3c288d
--- /dev/null
+++ b/linuxinstall/shell/restart.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+# -----------------------------------------------------------------------------
+# Retart Script for the NMS Client
+#
+# -----------------------------------------------------------------------------
+
+# resolve links - $0 may be a softlink
+PRG="$0"
+
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`/"$link"
+ fi
+done
+
+PRGDIR=`dirname "$PRG"`
+EXECUTABLE=nmsagent.sh
+
+if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
+ echo "Cannot find $PRGDIR/$EXECUTABLE"
+ echo "This file is needed to run this program"
+ exit 1
+fi
+
+exec "$PRGDIR"/"$EXECUTABLE" restart $1 $2 $3 "$@"
\ No newline at end of file
diff --git a/linuxinstall/shell/shutdown.sh b/linuxinstall/shell/shutdown.sh
new file mode 100644
index 0000000..bc5ecc9
--- /dev/null
+++ b/linuxinstall/shell/shutdown.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+# -----------------------------------------------------------------------------
+# Stop Script for the NMS Client
+#
+# -----------------------------------------------------------------------------
+
+# resolve links - $0 may be a softlink
+PRG="$0"
+
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`/"$link"
+ fi
+done
+
+PRGDIR=`dirname "$PRG"`
+EXECUTABLE=nmsagent.sh
+
+if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
+ echo "Cannot find $PRGDIR/$EXECUTABLE"
+ echo "This file is needed to run this program"
+ exit 1
+fi
+
+exec "$PRGDIR"/"$EXECUTABLE" stop "$@"
\ No newline at end of file
diff --git a/linuxinstall/shell/startup.sh b/linuxinstall/shell/startup.sh
new file mode 100644
index 0000000..a28c64a
--- /dev/null
+++ b/linuxinstall/shell/startup.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+# -----------------------------------------------------------------------------
+# Start Script for the NMS Client
+#
+# -----------------------------------------------------------------------------
+
+# resolve links - $0 may be a softlink
+PRG="$0"
+
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`/"$link"
+ fi
+done
+
+PRGDIR=`dirname "$PRG"`
+EXECUTABLE=nmsagent.sh
+
+if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
+ echo "Cannot find $PRGDIR/$EXECUTABLE"
+ echo "This file is needed to run this program"
+ exit 1
+fi
+
+exec "$PRGDIR"/"$EXECUTABLE" start "$@"
\ No newline at end of file
diff --git a/src/com/nis/nmsclient/NmsClient.java b/src/com/nis/nmsclient/NmsClient.java
new file mode 100644
index 0000000..101a4c6
--- /dev/null
+++ b/src/com/nis/nmsclient/NmsClient.java
@@ -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 setInfos = new LinkedList();
+ // 监测设置的报警字段设置信息,初始化完成后清空
+ public Map> alarmInfos = new HashMap>();
+ 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会将执行失败的任务结果发送给DC(TaskResultOper.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.全部存盘,下次启动时入库
+ }
+ }
+ });
+ }
+
+}
diff --git a/src/com/nis/nmsclient/common/Common.java b/src/com/nis/nmsclient/common/Common.java
new file mode 100644
index 0000000..7b56c16
--- /dev/null
+++ b/src/com/nis/nmsclient/common/Common.java
@@ -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 taskFutureMap = Collections.synchronizedMap(new HashMap());
+ // 主动告警信息: 主动报警线程使用
+ private static Map alarmPOs = Collections.synchronizedMap(new HashMap());
+ // 预置监测控制集
+ private static Map> sysDetecFutrue = Collections.synchronizedMap(new HashMap>());
+ // Agent定时启动的三方监测控制集
+ private static Map> pluginDetecFutrue = Collections.synchronizedMap(new HashMap>());
+ // 三方监测设置集
+ private static Map pluginDetecSetInfoMap = new HashMap();
+ // 监测信息报警相关信息(alarmInfo.setInfoId, alarmInfo)
+ public static Map> detecAlarmInfoMap = new HashMap>();
+
+ /**
+ * 缓存三方监测设置
+ *
+ * @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 getPluginDetecSetInfos() {
+ return pluginDetecSetInfoMap.values();
+ }
+
+ public static void putAllDetecAlarmInfo(Map> alarmMap) {
+ detecAlarmInfoMap.putAll(alarmMap);
+ }
+
+ /**
+ * 监测信息报警相关信息
+ * @param setInfoId
+ * @return
+ */
+ public static List 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 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 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 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启动的周期监测)
+ * 生成三方监测的执行命令及查询关键字
+ *
+ * @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)
+ };
+ }
+}
diff --git a/src/com/nis/nmsclient/common/Contants.java b/src/com/nis/nmsclient/common/Contants.java
new file mode 100644
index 0000000..3f43a06
--- /dev/null
+++ b/src/com/nis/nmsclient/common/Contants.java
@@ -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 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;
+ }
+}
diff --git a/src/com/nis/nmsclient/common/StopWatch.java b/src/com/nis/nmsclient/common/StopWatch.java
new file mode 100644
index 0000000..f11655f
--- /dev/null
+++ b/src/com/nis/nmsclient/common/StopWatch.java
@@ -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 tagMap = new LinkedHashMap();
+
+ 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 getTag() {
+ return tagMap;
+ }
+
+ public void LinkedHashMap(LinkedHashMap 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);
+
+ }
+
+}
diff --git a/src/com/nis/nmsclient/common/SysConfig.java b/src/com/nis/nmsclient/common/SysConfig.java
new file mode 100644
index 0000000..e22a35f
--- /dev/null
+++ b/src/com/nis/nmsclient/common/SysConfig.java
@@ -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 proList = new LinkedList();
+
+ 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 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 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 proList = new LinkedList();
+
+ 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 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);
+ }
+ }
+}
diff --git a/src/com/nis/nmsclient/common/UpdateParams.java b/src/com/nis/nmsclient/common/UpdateParams.java
new file mode 100644
index 0000000..b79b9fc
--- /dev/null
+++ b/src/com/nis/nmsclient/common/UpdateParams.java
@@ -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;
+ }
+}
diff --git a/src/com/nis/nmsclient/common/VersionCfg.java b/src/com/nis/nmsclient/common/VersionCfg.java
new file mode 100644
index 0000000..f48812c
--- /dev/null
+++ b/src/com/nis/nmsclient/common/VersionCfg.java
@@ -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) {}
+ }
+ }
+}
diff --git a/src/com/nis/nmsclient/config/DetecConfOper.java b/src/com/nis/nmsclient/config/DetecConfOper.java
new file mode 100644
index 0000000..a3fc65b
--- /dev/null
+++ b/src/com/nis/nmsclient/config/DetecConfOper.java
@@ -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();
+ }
+
+}
diff --git a/src/com/nis/nmsclient/config/DetecConfReqHandle.java b/src/com/nis/nmsclient/config/DetecConfReqHandle.java
new file mode 100644
index 0000000..22ef967
--- /dev/null
+++ b/src/com/nis/nmsclient/config/DetecConfReqHandle.java
@@ -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 setInfos, Map> 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 setList = null;
+ if (str.toLowerCase().contains("setinfo")) {
+ JSONArray jsonArr = jsonObj.getJSONArray("setInfo");
+ setList = handerSetInfo(jsonArr);
+ }
+ if (setList == null || setList.size() == 0) {
+ return;
+ }
+ Map> 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 setList = null;
+ if (str.toLowerCase().contains("setinfo")) {
+ JSONArray jsonArr = jsonObj.getJSONArray("setInfo");
+ setList = handerSetInfo(jsonArr);
+ }
+ if (setList == null || setList.size() == 0) {
+ return;
+ }
+ Map> 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 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 handerSetInfo(JSONArray jsonArr) {
+ // 文件路径
+ List setList = new ArrayList();
+ 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> handerAlarmInfo(JSONArray jsonArr, List 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> alarmMap = new HashMap>();
+ 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 aList = alarmMap.get(alarmInfo.getSetInfoId());
+ aList.add(alarmInfo);
+ alarmMap.put(alarmInfo.getSetInfoId(), aList);
+ } else {
+ List aList = new ArrayList();
+ aList.add(alarmInfo);
+ alarmMap.put(alarmInfo.getSetInfoId(), aList);
+ }
+ }
+ }
+ }
+ Map> alarmMapNew = new HashMap>();
+ // 依次查看监测设置信息,不管有没有设置告警信息,都要写配置police=,若无告警信息,配置字段值为空
+ for(SetInfo setInfo : setList){
+ Long key = setInfo.getId();
+ List 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 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();
+ }
+}
diff --git a/src/com/nis/nmsclient/model/AlarmInfo.java b/src/com/nis/nmsclient/model/AlarmInfo.java
new file mode 100644
index 0000000..58d85e6
--- /dev/null
+++ b/src/com/nis/nmsclient/model/AlarmInfo.java
@@ -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;
+ }
+
+}
diff --git a/src/com/nis/nmsclient/model/CommandPO.java b/src/com/nis/nmsclient/model/CommandPO.java
new file mode 100644
index 0000000..0644924
--- /dev/null
+++ b/src/com/nis/nmsclient/model/CommandPO.java
@@ -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;
+ }
+
+}
diff --git a/src/com/nis/nmsclient/model/ParamBackup.java b/src/com/nis/nmsclient/model/ParamBackup.java
new file mode 100644
index 0000000..5deac2a
--- /dev/null
+++ b/src/com/nis/nmsclient/model/ParamBackup.java
@@ -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;
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/nis/nmsclient/model/ParamCmdExec.java b/src/com/nis/nmsclient/model/ParamCmdExec.java
new file mode 100644
index 0000000..c66b6a5
--- /dev/null
+++ b/src/com/nis/nmsclient/model/ParamCmdExec.java
@@ -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、常驻内存的,写入PID,2、非常驻的,写执行结果:结果标识(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;
+ }
+}
diff --git a/src/com/nis/nmsclient/model/ParamCoverUpdate.java b/src/com/nis/nmsclient/model/ParamCoverUpdate.java
new file mode 100644
index 0000000..3d731d4
--- /dev/null
+++ b/src/com/nis/nmsclient/model/ParamCoverUpdate.java
@@ -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;
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/nis/nmsclient/model/ParamFilePush.java b/src/com/nis/nmsclient/model/ParamFilePush.java
new file mode 100644
index 0000000..e775e22
--- /dev/null
+++ b/src/com/nis/nmsclient/model/ParamFilePush.java
@@ -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;
+ }
+}
diff --git a/src/com/nis/nmsclient/model/ParamUpgrade.java b/src/com/nis/nmsclient/model/ParamUpgrade.java
new file mode 100644
index 0000000..891bbf1
--- /dev/null
+++ b/src/com/nis/nmsclient/model/ParamUpgrade.java
@@ -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;
+ }
+
+}
diff --git a/src/com/nis/nmsclient/model/ReturnFilePO.java b/src/com/nis/nmsclient/model/ReturnFilePO.java
new file mode 100644
index 0000000..e781f42
--- /dev/null
+++ b/src/com/nis/nmsclient/model/ReturnFilePO.java
@@ -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 filePathMap;
+ /**
+ * 当前要回传的文件
+ */
+// private File curRetrunFile;
+ /**
+ * 回传文件名(统一处理后一个任务只有一个回传文件)
+ */
+ private String returnFileName;
+ /**
+ * 回传状态:0 成功,1 失败
+ */
+ private long state;
+ /**
+ * 开始时间
+ */
+ private Date startTime;
+ /**
+ * 结束时间
+ */
+ private Date endTime;
+ /**
+ * 是否循环任务
+ */
+ private long isLoop;
+ /**
+ * 回传文件的结果描述
+ */
+ private String resDesc;
+
+ public Long getTaskId() {
+ return taskId;
+ }
+ public void setTaskId(Long taskId) {
+ this.taskId = taskId;
+ }
+ public long getTaskType() {
+ return taskType;
+ }
+ public void setTaskType(long taskType) {
+ this.taskType = taskType;
+ }
+ public long getState() {
+ return state;
+ }
+ public void setState(long state) {
+ this.state = state;
+ }
+ public Date getStartTime() {
+ return startTime;
+ }
+ public void setStartTime(Date startTime) {
+ this.startTime = startTime;
+ }
+ public Date getEndTime() {
+ return endTime;
+ }
+ public void setEndTime(Date endTime) {
+ this.endTime = endTime;
+ }
+ public long getIsLoop() {
+ return isLoop;
+ }
+ public void setIsLoop(long isLoop) {
+ this.isLoop = isLoop;
+ }
+ public String getResDesc() {
+ return resDesc;
+ }
+ public void setResDesc(String resDesc) {
+ this.resDesc = resDesc;
+ }
+ public String getReturnFileName() {
+ return returnFileName;
+ }
+ public void setReturnFileName(String returnFileName) {
+ this.returnFileName = returnFileName;
+ }
+ public long getUuid() {
+ return uuid;
+ }
+ public void setUuid(long uuid) {
+ this.uuid = uuid;
+ }
+
+}
diff --git a/src/com/nis/nmsclient/model/SetInfo.java b/src/com/nis/nmsclient/model/SetInfo.java
new file mode 100644
index 0000000..c0e1338
--- /dev/null
+++ b/src/com/nis/nmsclient/model/SetInfo.java
@@ -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 list = new ArrayList();
+ 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);
+ }
+
+
+}
diff --git a/src/com/nis/nmsclient/model/Task1.java b/src/com/nis/nmsclient/model/Task1.java
new file mode 100644
index 0000000..32950fb
--- /dev/null
+++ b/src/com/nis/nmsclient/model/Task1.java
@@ -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;
+ }
+}
diff --git a/src/com/nis/nmsclient/model/Task4.java b/src/com/nis/nmsclient/model/Task4.java
new file mode 100644
index 0000000..302252c
--- /dev/null
+++ b/src/com/nis/nmsclient/model/Task4.java
@@ -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;
+ }
+
+}
diff --git a/src/com/nis/nmsclient/model/Task6.java b/src/com/nis/nmsclient/model/Task6.java
new file mode 100644
index 0000000..06ddec7
--- /dev/null
+++ b/src/com/nis/nmsclient/model/Task6.java
@@ -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;
+ }
+
+}
diff --git a/src/com/nis/nmsclient/thread/WritePidThread.java b/src/com/nis/nmsclient/thread/WritePidThread.java
new file mode 100644
index 0000000..8126978
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/WritePidThread.java
@@ -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());
+ }
+
+}
diff --git a/src/com/nis/nmsclient/thread/alarm/AlarmPO.java b/src/com/nis/nmsclient/thread/alarm/AlarmPO.java
new file mode 100644
index 0000000..e1495ed
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/alarm/AlarmPO.java
@@ -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;
+ }
+
+}
diff --git a/src/com/nis/nmsclient/thread/alarm/AlarmThread.java b/src/com/nis/nmsclient/thread/alarm/AlarmThread.java
new file mode 100644
index 0000000..41d989a
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/alarm/AlarmThread.java
@@ -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 alarmSmMap = new HashMap();
+
+ public AlarmThread(String name) {
+ super();
+ this.name = name;
+ }
+
+ public void run() {
+ Thread.currentThread().setName(name);
+
+ logger.debug("主动预警检查开始 ~~~~~~~");
+ try{
+ Map alarmPOs = new HashMap(Common
+ .getAlarmPOs());
+ // 清除垃圾数据
+ if (alarmSmMap.size() > alarmPOs.size()) {
+ Iterator 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> entrys = alarmPOs.entrySet();
+ int alarmCnt = 0;
+ StringBuffer alarmDescInfo = new StringBuffer();
+ for (Map.Entry 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;
+ }
+ }
+
+}
diff --git a/src/com/nis/nmsclient/thread/alarm/AlarmUtil.java b/src/com/nis/nmsclient/thread/alarm/AlarmUtil.java
new file mode 100644
index 0000000..1c0a76f
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/alarm/AlarmUtil.java
@@ -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);
+ }
+
+ }
+}
diff --git a/src/com/nis/nmsclient/thread/alarm/ErrorCode.java b/src/com/nis/nmsclient/thread/alarm/ErrorCode.java
new file mode 100644
index 0000000..0f62cb9
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/alarm/ErrorCode.java
@@ -0,0 +1,11 @@
+package com.nis.nmsclient.thread.alarm;
+
+public enum ErrorCode {
+ ProcessNotExist, //进程不存在
+ MultipleProcessExist,//多个进程存在
+ ThreadRuntimeError, //执行异常
+ HandShakeError, //通讯握手失败
+ ProtListenerError, //端口监听失败
+ DeamonNotExist, //守护进程不存在
+ SocketError//通讯失败
+}
diff --git a/src/com/nis/nmsclient/thread/plugin/StartPluginRun.java b/src/com/nis/nmsclient/thread/plugin/StartPluginRun.java
new file mode 100644
index 0000000..43ed9bc
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/plugin/StartPluginRun.java
@@ -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);
+ }
+ }
+ }
+
+}
diff --git a/src/com/nis/nmsclient/thread/socket/CommonSocket.java b/src/com/nis/nmsclient/thread/socket/CommonSocket.java
new file mode 100644
index 0000000..8248f32
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/socket/CommonSocket.java
@@ -0,0 +1,1199 @@
+package com.nis.nmsclient.thread.socket;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+
+import com.nis.nmsclient.common.Contants;
+import com.nis.nmsclient.util.FileUtil;
+import com.nis.nmsclient.util.MD5Util;
+import com.nis.nmsclient.util.Utils;
+import com.nis.nmsclient.util.file.BufferedRandomAccessFile;
+import com.socket.utils.FileComment;
+
+public class CommonSocket{
+ static Logger logger = Logger.getLogger(CommonSocket.class);
+ protected static final String TEMP_SUFFIX = ".tp";
+ //缓存字节长度
+ protected static final int BUFF_SIZE = 1024;
+ public static final String SUCCESS = "success";
+ public static final String FAIL = "fail";
+ public static final String END = "end";
+ /**
+ * 与Server握手请求
+ */
+ public static final String REQ_HAND_SHAKE = "char:handshake";
+ /**
+ * 获取本机标志UUID请求
+ */
+ public static final String REQ_LOCAL_UUID = "char:uuid";
+ /**
+ * 发送本机变更信息请求
+ */
+ public static final String REQ_LOCAL_CHANGE = "char:agentChange";
+ /**
+ * 初始化配置请求
+ */
+ public static final String REQ_INIT_CONFIG = "char:init";
+ /**
+ * 初始化任务请求
+ */
+ public static final String REQ_INIT_TASK = "char:initTask";
+ /**
+ * 主动告警请求
+ */
+ public static final String REQ_ALARM = "char:alarm";
+ /**
+ * 获取Server端系统时间请求
+ */
+ public static final String REQ_SERVER_SYSTEMDATE = "char:systemdate";
+ /**
+ * Server升级请求
+ */
+ public static final String REQ_SERVER_UPGRADE = "char:upgradeServer";
+ /**
+ * NC向DC发送错误信息
+ */
+ public static final String REQ_ERROR_INFO = "char:ncErrorInfo";
+ // ========== 现由DC主动获取数据,以下命令暂留
+ /**
+ * 批量上传数据文件请求
+ */
+ public static final String REQ_UPLOAD_DATAS ="byte:datas";
+ /**
+ * 回传文件请求
+ */
+ public static final String REQ_TASK_RETURNFILE = "byte:taskReturn";
+ // ========== 现由DC主动获取数据,以上命令暂留
+ /**
+ * 发送任务结果请求【数据收集方式改为DC主动后,此请求只在NC启动时发送所有任务结果使用】
+ */
+ public static final String REQ_TASK_RESULT = "char:taskResult";
+ /**
+ * 上传回传文件、任务结果文件、数据文件的打包文件请求【数据收集方式改为DC主动后,此请求只在NC启动时发送所有任务结果使用】
+ */
+ public static final String REQ_BP_UPLOAD_FIFE = "byte:bpUploadFile";
+ //bpUploadFile的子命令类型
+ public static final String BP_TYPE_TASK_RESULT = "taskresult";
+ public static final String BP_TYPE_TASK_RETURN = "taskreturn";
+ public static final String BP_TYPE_DETECT_DATA = "detectdata";
+ /**
+ * 交换证书通信命令
+ */
+ public static final String REQ_CERT = "byte:cert";
+ /**
+ * 更新监测设置信息通信命令
+ */
+ public static final String SERVER_UPDATE_CONFIG = "char:updateConfig";
+ /**
+ * 下发第三方监测脚本命令
+ */
+ public static final String SEND_PLUGIN_SCRIPT_FILE = "char:sendPluginScriptFile";
+ /**
+ * NC端是否报主动告警 变更
+ */
+ public static final String ACTIVE_ALARM_START_ALERT = "char:isActiveAlarmStart";
+ /**
+ * 文件推送通信命令
+ */
+ public static final String SERVER_FILE_PUSH = "byte:filePush";
+ /**
+ * 升级通信命令
+ */
+ public static final String SERVER_UPGRADE = "byte:upgrade";
+ /**
+ * 下发任务通信命令
+ */
+ public static final String SERVER_TASK = "char:task";
+ /**
+ * 任务撤消命令
+ */
+ public static final String SERVER_TASK_CANCEL = "char:taskCancel";
+ /**
+ * DC主动向NC再次获取任务结果
+ */
+ public static final String SERVER_GET_TASKRESULT = "char:collectNonRltTaskResult";
+ /**
+ * DC主动向NC收集监测数据
+ */
+ public static final String SERVER_COLLECT_DATA = "byte:collectData";
+ //收集数据,Agent发送的类型
+ public static final String DATA_TYPE_ZIP_DETECT = "zipDetectData";//监测数据zip
+ public static final String DATA_TYPE_CSV_DETECT = "csvDetectData";//批量上传csv监测数据
+ public static final String DATA_TYPE_ZIP_TASKRESULT = "zipTaskResult";//任务结果zip
+ public static final String DATA_TYPE_OBJ_TASKRESULT = "objTaskResult";//批量上传任务结果obj
+ public static final String DATA_TYPE_ZIP_TASKRETURN = "zipTaskReturn";//任务回传文件zip
+ public static final String DATA_TYPE_FILE_TASKETURN = "fileTaskReturn";//单个任务回传文件
+
+ protected Socket socket = null;
+ protected OutputStream out = null;
+ protected InputStream in = null;
+
+ public CommonSocket() {
+ super();
+ }
+
+ public CommonSocket(Socket client) throws Exception {
+ socket = client;
+ out = socket.getOutputStream();
+ in = socket.getInputStream();
+ }
+
+ /**
+ * 发送消息,以字符形式,发送一行信息
+ **/
+ public boolean sendMessageByChar(String msg) throws Exception {
+ logger.debug("sendMessageByChar---" + msg);
+ PrintWriter pw = new PrintWriter(new OutputStreamWriter(out,
+ Contants.charset));
+ pw.println(msg);
+ pw.flush();
+
+ return true;
+ }
+
+
+ /**
+ * 接收信息,以字符形式,接收一行信息
+ *
+ */
+ public String receiveMessageByChar() throws Exception {
+ BufferedReader br = new BufferedReader(new InputStreamReader(in,
+ Contants.charset));
+ String str = br.readLine();
+ logger.debug("receiveMessageByChar---" + str);
+ return str;
+ }
+
+ /**
+ * 发送单个文件
+ **/
+ public boolean sendFileByByte(File file) throws Exception {
+ ObjectOutputStream oos = null;
+ FileInputStream fis = null;
+ try {
+ //发送文件大小和文件名
+ oos = new ObjectOutputStream(out);
+ String[] strArr = new String[]{
+ file.length() + "", file.getName()
+ };
+ oos.writeObject(strArr);
+ //发送文件内容
+ byte[] buff = new byte[BUFF_SIZE];
+ int len = 0;
+ fis = new FileInputStream(file);
+ while ((len = fis.read(buff)) != -1) {
+ //将读取的内容写入文件
+ out.write(buff, 0, len);
+ }
+ out.flush();
+ } catch (Exception e) {
+ logger.error("Single file sending failure");
+ throw e;
+ } finally{
+ if(fis!=null){
+ try {
+ fis.close();
+ } catch (IOException e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * 接收单个文件
+ *
+ */
+ public boolean receiveFileByByte(String filePath) throws Exception {
+ ObjectInputStream ois = null;
+ FileOutputStream fos = null;
+ try {
+
+ ois = new ObjectInputStream(in);
+ String[] strArr = (String[])ois.readObject();
+ //接收文件大小
+ long fileSize = Long.parseLong(strArr[0]);
+ //接收文件名
+ String fileName = strArr[1];
+
+ //接收文件内容
+ byte[] buff = new byte[BUFF_SIZE];
+ fos = new FileOutputStream(filePath + File.separator + fileName);
+ int nRead = 0;
+
+ //单个文件循环读取
+ while ((nRead = in.read(buff, 0, (int)(BUFF_SIZE 0) {
+ fos.write(buff,0,nRead);
+ fos.flush();
+ fileSize -= nRead;
+ if(fileSize<=0){
+ break;
+ }
+ }
+ fos.close();
+ } catch (Exception e) {
+ logger.error("Single file receiving failure");
+ throw e;
+ } finally {
+ try {
+ if (fos != null) {
+ fos.close();
+ }
+ } catch (IOException e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+
+ }
+
+ return true;
+
+ }
+
+ /**
+ * 批量上传文件
+ * @param dir 本地文件集合根目录绝对路径
+ * @param fileList 上传的文件列表
+ */
+ public void sendFileByBath(String dir, List fileList) throws Exception {
+ ObjectOutputStream oos = null;
+ FileInputStream fis = null;
+
+ try {
+ // 第一步发送本地根目录地址(用于地址截取)保证fileList的目录结构完整性
+ this.sendMessageByChar(dir);
+ String result = this.receiveMessageByChar();
+ logger.debug("根目录地址发送状态: " + result);
+ // 第二步 用ObjectOutputStream工具类 发送file对象信息 用于文件名,文件目录,文件大小的获取
+ oos = new ObjectOutputStream(out);
+ List fileStrList = new ArrayList();
+ if(fileList!=null && fileList.size()>0){
+ for (File f : fileList) {
+ if (f.exists()) {
+ String[] tmpArr = new String[] { f.getAbsolutePath(),
+ f.length() + "" };
+ fileStrList.add(tmpArr);
+ } else {
+ logger.warn("File:>" + f.getAbsolutePath()
+ + " do not exist, can not send");
+ }
+ }
+ oos.writeObject(fileStrList);
+ // 第三部,发送文件
+ byte[] buff = new byte[BUFF_SIZE];
+ int len = 0;
+ // 循环上传文件
+ for (File file : fileList) {
+ logger.debug("--sendFileByBath---" + file.getName() + "---length=" + file.length());
+ fis = new FileInputStream(file);
+ while ((len = fis.read(buff)) != -1) {// 将读取的内容输出流
+ out.write(buff, 0, len);
+ }
+ out.flush();
+ fis.close();
+ fis = null;
+ }
+ }
+ logger.debug("批量发送文件个数:" + (fileList==null ? 0 : fileList.size()));
+ } catch (Exception e) {
+ throw e;
+ } finally {
+ try {
+ if (fis != null) {
+ fis.close();
+ fis = null;
+ }
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ /**
+ * 批量接收文件
+ * @param newDir
+ */
+ public boolean receiveFileByBath(String newDir) throws Exception {
+ ObjectInputStream ois = null;
+ FileOutputStream fos = null;
+ try {
+ //获取集合文件路径
+ String oldDir = this.receiveMessageByChar();
+ //logger.info("旧上传文件集合根目录: " + oldDir);
+ this.sendMessageByChar("success");
+ ois = new ObjectInputStream(in);
+ List fileList = (List)ois.readObject();
+ if(fileList != null && fileList.size()>0){
+ for(String[] arr : fileList){
+ String newUrl = arr[0].replace(oldDir, newDir);//新路径
+ newUrl = newUrl.replaceAll("\\\\", "/");
+ int fileLength = Integer.parseInt(arr[1]); //大小
+ File newFile = new File(newUrl);
+ if(newFile.exists()){
+ FileUtil.delDir(newFile);
+ logger.debug("receiveFileByBath delete file---" + newFile.getAbsolutePath());
+ }
+ if(!newFile.getParentFile().exists()){
+ newFile.getParentFile().mkdirs();
+ }
+ fos = new FileOutputStream(newUrl+TEMP_SUFFIX);
+
+ int nRead = 0;
+ byte[] buff = new byte[BUFF_SIZE];
+ //单个文件循环读取
+ while ((nRead = in.read(buff, 0, (int)(BUFF_SIZE 0) {
+ fos.write(buff,0,nRead);
+ fos.flush();
+ fileLength -= nRead;
+ if(fileLength<=0){
+ break;
+ }
+ }
+ fos.close();
+ fos = null;
+ File newFile2 = new File(newUrl+TEMP_SUFFIX);
+ //newFile2.renameTo(newFile);
+ FileUtils.copyFile(newFile2, newFile);//将临时文件名改为正式文件名,即去掉.tp后缀
+ newFile2.delete();// 将临时文件删除
+ }
+ }
+ logger.debug("批量接收文件个数:" + (fileList==null ? 0 : fileList.size()));
+ } catch (Exception e) {
+ throw e;
+ }finally{
+ if(fos!=null){
+ try {
+ fos.close();
+ fos = null;
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ return true;
+
+ }
+
+ /**
+ * 批量上传文件, 并传入文件的Md5值
+ * @param fileList 上传的文件列表
+ */
+ protected void sendFileWithMd5ByBath(List fileCommentsList) throws IOException {
+ ObjectOutputStream oos = null;
+ FileInputStream fis = null;
+
+ try {
+ // 第一步 用ObjectOutputStream工具类 发送file对象信息 用于文件名,文件目录,文件大小的获取
+ oos = new ObjectOutputStream(out);
+ List fileList = new LinkedList();
+ List fileStrList = new ArrayList();
+ for(String[] fileComments : fileCommentsList){
+ File file = new File(fileComments[0]);
+ if(file.exists()){
+ String[] tmpArr = new String[]{
+ file.getName(), file.length() + "",fileComments[1]
+ };
+ fileList.add(file);
+ fileStrList.add(tmpArr);
+ }else {
+ logger.warn("File:>"+file.getAbsolutePath()+" do not exist, can not send");
+ }
+ }
+ oos.writeObject(fileStrList);
+ // 第三部,发送文件
+ byte[] buff = new byte[BUFF_SIZE];
+ int len = 0;
+ // 循环上传文件
+ for (File file : fileList) {
+ fis = new FileInputStream(file);
+ while ((len = fis.read(buff)) != -1) {// 将读取的内容输出流
+ out.write(buff, 0, len);
+ }
+ out.flush();
+ fis.close();
+ fis = null;
+ }
+ logger.debug("批量发送文件结束,共 "+(fileList==null ? 0 : fileList.size())+ "个文件");
+ } catch (IOException e) {
+ logger.error("Batch file failed!");
+ throw new IOException(e);
+ } finally {
+ try {
+ if (fis != null) {
+ fis.close();
+ fis = null;
+ }
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ /**
+ * 批量接收文件, 使用Md5校验文件是否完整
+ * @param newDir
+ */
+ public boolean receiveFileWithMd5ByBath(String newDir) throws IOException {
+ boolean flag = true;
+ ObjectInputStream ois = null;
+ FileOutputStream fos = null;
+ try {
+ ois = new ObjectInputStream(in);
+ List fileList = (List)ois.readObject();
+ if(fileList != null && fileList.size()>0){
+ int sucessCnt = 0;
+ int failCnt = 0;
+ for(int i=0; i 0) {
+ fos.write(buff,0,nRead);
+ fos.flush();
+ fileLength -= nRead;
+ if(fileLength<=0){
+ break;
+ }
+ }
+ fos.close();
+ fos = null;
+ File newFile2 = new File(newUrl+TEMP_SUFFIX);
+ if (md5Val != null
+ && md5Val
+ .equals(MD5Util.getFileMD5String(newFile2))) {
+ logger.debug("接收文件" + (i+1) + "“" + newFile.getAbsolutePath() + "”完整");
+ //newFile2.renameTo(newFile);
+ FileUtils.copyFile(newFile2, newFile);//将临时文件名改为正式文件名,即去掉.tp后缀
+ newFile2.delete();// 将临时文件删除
+ sucessCnt ++ ;
+ } else {
+ logger.debug("接收文件" + (i+1) + "“" + newFile.getAbsolutePath() + "”不完整,失败");
+ failCnt ++ ;
+ }
+ }
+ logger.info("批理接收文件个数:" + fileList.size() + ", 成功:" + sucessCnt + ", 失败:" + failCnt);
+ if(failCnt > 0) {
+ flag = false;
+ }
+ }else{
+ logger.info("批量接收文件列表为空");
+ }
+ } catch (Exception e) {
+ logger.error("Batch file failure");
+ throw new IOException(e);
+ }finally{
+ if(fos!=null){
+ try {
+ fos.close();
+ fos = null;
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ return flag;
+ }
+
+ /**
+ * 断点续传 发送方法
+ * @time Mar 2, 2012-2:30:16 PM
+ * @param filePath
+ */
+ protected boolean bpSendFile (String filePath) throws Exception {
+ File file = new File(filePath);
+
+ //发送长度 end
+ this.sendMessageByChar(file.length()+"");
+
+ String msg = this.receiveMessageByChar();
+ long start = Long.parseLong(msg);
+ long end = file.length();
+ logger.debug("start "+start);
+ logger.debug("end "+end);
+ bpSendFile(filePath, start, end);
+
+ return true;
+ }
+
+ /**
+ * 断点续传 接收方法
+ * @time Mar 2, 2012-2:30:16 PM
+ * @param filePath
+ * @param start
+ * @param end
+ */
+ protected int bpReceiveFile (String filePath) throws Exception {
+ File file = new File(filePath);
+ if(!file.exists()){
+ file = new File(filePath+TEMP_SUFFIX);
+ }
+ String msg = this.receiveMessageByChar();
+ long start = file.length();
+ long end = Long.parseLong(msg);
+ this.sendMessageByChar(start+"");
+
+ logger.debug("start "+start);
+ logger.debug("end "+end);
+ bpReceiveFile(file.getAbsolutePath(), start, end);
+
+ //file.renameTo(new File(filePath));
+ FileUtils.copyFile(file, new File(filePath));//将临时文件名改为正式文件名,即去掉.tp后缀
+ file.delete();// 将临时文件删除
+
+ logger.debug("bpReceiveFile sucess");
+ return 0;
+ }
+
+ /**
+ * 断点续传 发送方法
+ * @time Mar 2, 2012-2:30:16 PM
+ * @param filePath
+ * @param start
+ * @param end
+ */
+ protected void bpSendFile (String filePath,long start,long end) throws Exception {
+ if (start == end) {
+ return;
+ }
+ BufferedRandomAccessFile braf = null;
+ try {
+ File file = new File(filePath);
+
+ //- 不存在,终止; 存在则继续
+ if(!file.exists()){
+ this.sendMessageByChar(FAIL);
+ return ;
+ }else {
+ this.sendMessageByChar(SUCCESS);
+ }
+
+ String msg = this.receiveMessageByChar();
+ logger.debug("Recive: " + msg);
+
+ //- BufferedRandomAccessFile 读取指定位置的文件字节数组,写入输出通讯
+ byte[] b = new byte[BUFF_SIZE];
+ braf = new BufferedRandomAccessFile(file,"r");
+ braf.seek(start);
+ int nRead;
+ while ((nRead = braf.read(b, 0, BUFF_SIZE)) > 0) {
+
+ out.write(b, 0, nRead);
+ start += nRead;
+
+ //-- 读取完成 跳出
+ if(start==end){break;}
+
+ }
+
+ }catch (Exception e) {
+ throw e;
+ }finally{
+ try {
+ //- 关闭 随机访问文件对象(关闭流)
+ if(braf!= null){braf.close();}
+ } catch (IOException e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+
+ }
+ }
+ /**
+ * 断点续传 接收方法
+ * @time Mar 2, 2012-2:30:16 PM
+ * @param filePath
+ * @param start
+ * @param end
+ */
+ protected void bpReceiveFile (String filePath,long start,long end) throws Exception {
+ if(StringUtils.isEmpty(filePath)){
+ return;
+ }
+ if (start == end) {
+ return;
+ }
+ BufferedRandomAccessFile raf = null;
+ try {
+ File file = new File(filePath);
+
+ //- 文件路径不存在 则创建
+ if (!file.getParentFile().exists()) {
+ file.getParentFile().mkdirs();
+ }
+
+ //- 文件不存在 则创建
+ if (!file.exists()) {
+ file.createNewFile();
+ }
+
+ //- 接收发送端 发送数据准备 确认信息
+ String msg = this.receiveMessageByChar();
+
+ if (FAIL.equals(msg)) { //结束操作
+ return;
+ } else {
+ this.sendMessageByChar(SUCCESS); // 通知发送端 接收数据准备完成 确认信息
+ }
+
+ // 将通信中读出的数据 写入文件指定位置
+ byte[] b = new byte[BUFF_SIZE];
+ raf = new BufferedRandomAccessFile(file, "rw");
+ raf.seek(start);
+ int nRead;
+
+ while ((nRead = in.read(b, 0, BUFF_SIZE)) > 0) {
+ raf.write(b, 0, nRead);
+ start += nRead;
+
+ if (start == end) { //写完跳出
+ break;
+ }
+ }
+ } catch (Exception e) {
+ throw e;
+ }finally{
+ try {
+ //- 关闭 随机访问文件对象(关闭流)
+ if(raf!= null){raf.close();}
+ } catch (IOException e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+ }
+ }
+
+ /**
+ * 断点续传 批量上传文件
+ * @param fileList 上传的文件列表
+ * @param dir 本地文件集合根目录绝对路径
+ */
+ protected void bpSendFileByBath(List fileList,String dir) throws Exception {
+ BufferedRandomAccessFile oReadFile = null;
+
+ try {
+ // 第一步发送本地根目录地址(用于地址截取)保证fileList的目录结构完整性
+ this.sendMessageByChar("abs:"+(dir==null?"":dir));
+ String result = this.receiveMessageByChar();
+ logger.debug("根目录路径通信状态: " + result);
+ // 第二步 用ObjectOutputStream工具类 发送file对象信息 用于文件名,文件目录,文件大小的获取
+
+ //原文件文件名 和 大小(即end长度)
+ List sourceFileList = new ArrayList();
+ for(File f : fileList){
+ String[] tmpArr = new String[]{
+ f.getAbsolutePath(),0+"",f.length() + ""
+ };
+ sourceFileList.add(tmpArr);
+ }
+
+ logger.debug("发送信息: " + Arrays.toString(sourceFileList.toArray()));
+ this.sendObject(sourceFileList);
+
+ //得到需要下载的文件信息
+ List sendFileList = (List)receiveObject();
+
+ // 第三部,发送文件
+ byte[] buff = new byte[BUFF_SIZE];
+
+ // 循环上传文件
+ for (String[] sendFile: sendFileList) {
+ long start = Long.parseLong(sendFile[1]);
+ long end = Long.parseLong(sendFile[2]);
+ if(start >= end){
+ continue;
+ }
+ File file = new File(sendFile[0]);
+ oReadFile = new BufferedRandomAccessFile(file,"r");
+
+ // 定位文件指针到nPos位置
+ oReadFile.seek(start); //从0开始
+ int nRead;
+
+ // 从输入流中读入字节流,然后写到文件中
+ while ((nRead = oReadFile.read(buff, 0, BUFF_SIZE)) > 0) {
+
+ out.write(buff, 0, nRead);
+ start += nRead; //调整为从1开始
+ if(start >= end){
+ break;
+ }
+
+ }
+ oReadFile.close();
+ oReadFile = null;
+ }
+ logger.debug("多文件上传结束,共 "+(fileList==null ? 0 : fileList.size())+ "个文件");
+ } catch (Exception e) {
+ throw e;
+ } finally {
+ try {
+ if (oReadFile != null) {
+ oReadFile.close();
+ oReadFile = null;
+ }
+ } catch (IOException e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+ }
+ }
+
+ /**
+ * 断点续传 批量接收文件
+ * @param newDir
+ */
+ protected void bpReceiveFileByBath(String newDir) throws Exception {
+
+ BufferedRandomAccessFile oSavedFile = null;
+ try {
+ //获取集合文件路径
+ String oldDir = this.receiveMessageByChar();
+ int headLength = "abs:".length();
+ oldDir = ((StringUtils.isNotEmpty(oldDir)
+ && oldDir.length()>=headLength)
+ ?oldDir.substring(headLength,oldDir.length())
+ :oldDir);
+ if(StringUtils.isEmpty(oldDir)){
+ logger.debug("远程 目录根路径为空 接收文件不保留目录格式 统一存放到本地目录:》"+newDir);
+ }else{
+ logger.debug("根目录 记录: " + oldDir+" VS "+newDir);
+ }
+ this.sendMessageByChar(SUCCESS);
+
+ List remoteFileList = (List)receiveObject();
+ List receiveFileList = new LinkedList();
+ byte[] buff = new byte[BUFF_SIZE];
+ if(remoteFileList != null && remoteFileList.size()>0){
+ for(String[] arr : remoteFileList){
+ String newUrl = null;
+ if(StringUtils.isEmpty(oldDir)){
+ newUrl = newDir+(new File(arr[0].replaceAll("\\\\", "/")).getName());
+ }else{
+ newUrl = arr[0].replace(oldDir, newDir);//新路径
+ newUrl = newUrl.replaceAll("\\\\", "/");
+ }
+
+ File newFile = new File(newUrl);
+
+ //该文件已存在
+ if(newFile.exists()){
+ continue;
+ }
+
+ newFile = new File(newUrl+TEMP_SUFFIX);
+ arr[1] = newFile.length()+"";
+ receiveFileList.add(arr);
+ }
+ }
+ this.sendObject(receiveFileList);
+
+ if(receiveFileList != null && receiveFileList.size()>0){
+ for(String[] arr : receiveFileList){
+ String newUrl = null;
+ if(StringUtils.isEmpty(oldDir)){
+ newUrl = newDir+(new File(arr[0].replaceAll("\\\\", "/")).getName());
+ }else{
+ newUrl = arr[0].replace(oldDir, newDir);//新路径
+ newUrl = newUrl.replaceAll("\\\\", "/");
+ }
+
+ File newFile = new File(newUrl+TEMP_SUFFIX);
+
+ if(!newFile.getParentFile().exists()){
+ newFile.getParentFile().mkdirs();
+ }
+
+ if(!newFile.exists()){
+ newFile.createNewFile();
+ }
+
+ int start = Integer.parseInt(arr[1]); // 起始
+ int end = Integer.parseInt(arr[2]); // 结束
+ if(start 0) {
+ oSavedFile.write(buff,0,nRead);
+ end -= nRead;
+ if(end<=0){
+ break rfile;
+ }
+ }
+ oSavedFile.close();
+ oSavedFile = null;
+ }
+// newFile.renameTo(new File(newUrl)); //将临时文件名改为正式文件名,即去掉.tp后缀
+ FileUtils.copyFile(newFile, new File(newUrl)); //将临时文件名改为正式文件名,即去掉.tp后缀
+ newFile.delete();
+ }
+ }
+ logger.debug("多文件接收结束,共 "+(remoteFileList==null ? 0 : remoteFileList.size())+ "个文件");
+// } catch (IOException e) {
+// logger.error("",e);
+// } catch (ClassNotFoundException e) {
+// logger.error("",e);
+ }finally{
+ if(oSavedFile!=null){
+// try {
+ oSavedFile.close();
+ oSavedFile = null;
+// } catch (IOException e) {
+// logger.error("",e);
+// }
+ }
+ }
+ }
+
+ /**
+ * 断点续传 批量上传文件, 并传入文件的Md5值
+ * @param fileList 上传的文件列表
+ * @param dir 本地文件集合根目录绝对路径
+ */
+ protected void bpSendFileByBathMD5(List fileCommentsList) throws Exception {
+ BufferedRandomAccessFile oReadFile = null;
+
+ try {
+ //原文件文件名 和 大小(即end长度)
+ List sourceFileList = new ArrayList();
+ if(fileCommentsList !=null && fileCommentsList.size()!=0){
+ for(FileComment fileComment : fileCommentsList){
+ File f = new File(fileComment.getFileName());
+ if(!f.exists()){
+ sourceFileList.add(new FileComment(f.getAbsolutePath(),0,-1,fileComment.getMd5Val()));
+ }else {
+ String md5Val = StringUtils.isEmpty(fileComment.getMd5Val())?MD5Util.getFileMD5String(f):fileComment.getMd5Val();
+ sourceFileList.add(new FileComment(f.getAbsolutePath(),0,f.length(),md5Val));
+ }
+ }
+ }
+
+ logger.debug("发送信息: " + Arrays.toString(sourceFileList.toArray()));
+ this.sendObject(sourceFileList);
+
+ //得到需要下载的文件信息
+ List sendFileList = (List)receiveObject();
+
+ // 第三部,发送文件
+ byte[] buff = new byte[BUFF_SIZE];
+
+ // 循环上传文件
+ for (FileComment sendFile: sendFileList) {
+ long start = sendFile.getStart();
+ long end = sendFile.getEnd();
+ if(start >= end){
+ continue;
+ }
+ File file = new File(sendFile.getFileName());
+ oReadFile = new BufferedRandomAccessFile(file,"r");
+
+ // 定位文件指针到nPos位置
+ oReadFile.seek(start); //从0开始
+ int nRead;
+
+ // 从输入流中读入字节流,然后写到文件中
+ while ((nRead = oReadFile.read(buff, 0, BUFF_SIZE)) > 0) {
+
+ out.write(buff, 0, nRead);
+ start += nRead; //调整为从1开始
+ if(start >= end){
+ break;
+ }
+
+ }
+ oReadFile.close();
+ oReadFile = null;
+ }
+ logger.debug("多文件上传结束,共 "+(sendFileList==null ? 0 : sendFileList.size())+ "个文件");
+ } catch (Exception e) {
+ throw e;
+ } finally {
+ try {
+ if (oReadFile != null) {
+ oReadFile.close();
+ oReadFile = null;
+ }
+ } catch (IOException e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+ }
+ }
+
+
+ /**
+ * 断点续传 批量接收文件, 使用Md5校验文件是否完整
+ * @param newDir
+ */
+ public int bpReceiveFileByBathMd5(String newDir) throws Exception{
+ if(newDir!=null){
+ newDir += File.separator;
+ }
+ int rFlag = 0; //0 OK -1 MD5 ERROR -2 Function ERROR -3 文件不存在
+ BufferedRandomAccessFile oSavedFile = null; //有缓存的 随机文件IO对象
+ try {
+
+ List remoteFileList = (List)receiveObject(); //接收可接收的文件信息 string[]{fileName,start,end,MD5}
+ List receiveFileList = new LinkedList(); //需要续传的文件及其索引信息 string[]{fileName,start,end,MD5}
+ byte[] buff = new byte[BUFF_SIZE]; //缓存 大小
+
+ //- 检查实际接收文件大小
+ if(remoteFileList != null && remoteFileList.size()>0){
+ for(FileComment arr : remoteFileList){
+ //String newUrl = newDir+removeTimeTagFileName(new File(arr.getFileName()).getName(),null);
+ String filePath = arr.getFileName().replaceAll("\\\\", "/");
+ String fileName = filePath.substring(filePath.lastIndexOf("/")+1, filePath.length());
+ String newUrl = newDir+removeTimeTagFileName(fileName,null);
+
+ File newFile = new File(newUrl);
+
+ //-- 已接收完成
+ if(newFile.exists()){
+ // continue;
+ // 2013-1-6 jzz 如果接收完成也比较MD5值,主要是针对再次执行任务,直接拷来的文件
+ //-- MD5为空 无需校验
+ if(StringUtils.isEmpty(arr.getMd5Val())){
+ continue;
+ }
+ //-- MD5相等, 接收完成
+ if(arr.getMd5Val().equals(MD5Util.getFileMD5String(newFile))){
+ logger.debug("1--" + newFile.getAbsolutePath()+" MD5值校验一致");
+ continue;
+ } else {//-- MD5不相等,则删除该文件,下面重新接收
+ FileUtil.delDir(newFile);
+ logger.debug("1--bpReceiveFileByBathMd5 delete file ---" + newFile.getAbsolutePath());
+ logger.debug("1--" + newFile.getAbsolutePath()+" MD5值校验不一致");
+ }
+ // 2013-1-6 jzz 修改结束
+ }
+
+ //-- 续传文件及起始长度
+ newFile = new File(newUrl+TEMP_SUFFIX);
+ arr.setStart(newFile.length());
+ receiveFileList.add(arr);
+ }
+ }
+ this.sendObject(receiveFileList);
+
+ //- 接收文件
+ if(receiveFileList != null && receiveFileList.size()>0){
+ for(FileComment arr : receiveFileList){
+ //String newUrl = newDir+removeTimeTagFileName(new File(arr.getFileName()).getName(),null);
+ String filePath = arr.getFileName().replaceAll("\\\\", "/");
+ String fileName = filePath.substring(filePath.lastIndexOf("/")+1, filePath.length());
+ String newUrl = newDir+removeTimeTagFileName(fileName,null);
+
+ File newFile = new File(newUrl+TEMP_SUFFIX);
+
+ if(!newFile.getParentFile().exists()){
+ newFile.getParentFile().mkdirs();
+ }
+
+ //创建空文件
+ if (!newFile.exists()) {
+ newFile.createNewFile();
+ }
+
+ long start = arr.getStart(); // 起始
+ long end = arr.getEnd(); // 结束
+ if(end == -1){
+ return -3;
+ }
+
+ if(start 0) {
+ oSavedFile.write(buff,0,nRead);
+ end -= nRead;
+ if(end<=0){
+ break rfile;
+ }
+ }
+ oSavedFile.close();
+ oSavedFile = null;
+ }
+ //newFile.renameTo(new File(newUrl));
+ FileUtils.copyFile(newFile, new File(newUrl));//将临时文件名改为正式文件名,即去掉.tp后缀
+ newFile.delete();// 将临时文件删除
+ logger.debug(newFile.getAbsolutePath()+" 下载完成!");
+
+ //-- MD5为空 无需校验
+ if(StringUtils.isEmpty(arr.getMd5Val())){
+ continue;
+ }
+
+ File newFile2 = new File(newUrl);
+ //-- MD5不相等,则删除该文件 返回-1
+ if(!arr.getMd5Val().equals(MD5Util.getFileMD5String(newFile2))){
+ //newFile.delete_bak();
+ //使用删除文件公共方法
+ FileUtil.delDir(newFile);
+ logger.debug("bpReceiveFileByBathMd5 delete file ---" + newFile.getAbsolutePath());
+ //FileUtil.checkParentDirExist(newFile);
+ logger.debug(newFile.getAbsolutePath()+" MD5值校验不一致");
+ return -1;
+ } else {//-- MD5相等
+ logger.debug(newFile.getAbsolutePath()+" MD5值校验一致");
+ }
+ }
+ }
+ logger.debug("多文件接收结束,共 "+(remoteFileList==null ? 0 : remoteFileList.size())+ "个文件");
+ return rFlag;
+ } catch (Exception e) {
+ //return -2;
+ throw e;
+ }finally{
+ if(oSavedFile!=null){
+ try {
+ oSavedFile.close();
+ oSavedFile = null;
+ } catch (IOException e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+ }
+ }
+ }
+
+ /**
+ * Object 形式 发送信息
+ */
+ protected void sendObject(Object object) throws Exception {
+ ObjectOutputStream oos = new ObjectOutputStream(out);
+ oos.writeObject(object);
+ oos.flush();
+ }
+
+ /**
+ * Object 形式 接收信息
+ */
+ protected Object receiveObject() throws Exception {
+ ObjectInputStream ois = new ObjectInputStream(in);
+ return ois.readObject();
+ }
+
+ /**
+ * 关闭通讯
+ */
+ public void close() {
+ try {
+ if(out!=null){
+ out.close();
+ out = null;
+ }
+ if(in!=null){
+ in.close();
+ in = null;
+ }
+ if(socket!=null && socket.isConnected()){
+ socket.close();
+ socket = null;
+ }
+ } catch (Exception e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+ }
+
+ /**
+ * 删除addTimeTagForFileName()方法 所添加的时间戳
+ * @time Mar 12, 2012-3:36:16 PM
+ * @param fileName
+ * @return
+ */
+ public static String removeTimeTagFileName(String fileName, String taskId) {
+
+ if (StringUtils.isNotBlank(fileName) && fileName.contains("_")) {
+
+ String timeTag = fileName.substring(fileName.lastIndexOf("_"),
+ fileName.lastIndexOf(".")==-1?fileName.length():fileName.lastIndexOf(".")); //针对无后缀名文件,时间戳截取校验
+ fileName = fileName.replace(timeTag, "");
+
+ if(taskId!=null){
+ fileName = fileName.replace("_" + taskId, "");
+ }
+
+ }
+
+ return fileName;
+
+ }
+
+ /**
+ * 上传文件时,判断该文件是否已存在,如存在,则在后面加入时间戳
+ *
+ * @param fileName
+ * 单纯的文件名
+ * @param taskId 标识ID
+ * @return
+ * @throws UnknownHostException
+ */
+ public static String addTimeTagForFileName(String fileName, String taskId, boolean isFile){
+
+ try
+ {
+ Calendar calendar = new GregorianCalendar();
+ long timestamp = calendar.getTimeInMillis();
+
+ // 文件后缀
+ String fielType = "";
+
+ if (isFile) {// 只是文件做名称处理,目录的话不用处理,直接使用原名称
+ if (fileName.lastIndexOf(".") != -1) {
+ fielType = fileName.substring(fileName.lastIndexOf("."));
+ fileName = fileName.substring(0, fileName.lastIndexOf("."));
+ }
+ }
+
+ if(taskId!=null){
+ fileName += "_" + taskId;
+ }
+ fileName += "_" + timestamp+""+((int)(Math.random()*1000));
+
+ if(StringUtils.isNotBlank(Contants.AGENT_LOCAL_IP)) {
+ fileName = fileName+"_"+Contants.AGENT_LOCAL_IP;
+ }
+ fileName += fielType;
+ logger.debug("回传文件名称为: "+fileName);
+ } catch (Exception e)
+ {
+ logger.error("Generating the name exception of the return file", e);
+ }
+
+ return fileName;
+ }
+}
diff --git a/src/com/nis/nmsclient/thread/socket/SSLCertOper.java b/src/com/nis/nmsclient/thread/socket/SSLCertOper.java
new file mode 100644
index 0000000..32cf8d0
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/socket/SSLCertOper.java
@@ -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);
+ }
+}
diff --git a/src/com/nis/nmsclient/thread/socket/SSLClient.java b/src/com/nis/nmsclient/thread/socket/SSLClient.java
new file mode 100644
index 0000000..db8dd6d
--- /dev/null
+++ b/src/com/nis/nmsclient/thread/socket/SSLClient.java
@@ -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
+ *
+ * StringUtil.convertSimplifiedCase("325") = ”三二五"
+ *
+ * @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();
+ }
+
+
+ /**
+ *
+ * 把字符串中的数字转换成繁体大写中文的格式.
+ *
+ * StringUtil.convertTraditionalCase("325") = "叁贰伍"
+ *
+ * @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();
+ }
+
+ /**
+ *
+ * 创建唯一标识字符串.
+ *
+ * StringUtil.createUUID() = "00000DAF3CFC4E0B8DF2D5BEACB14D75"
+ *
+ * @return 返回标识符字符串
+ */
+ public static String createUUID() {
+ String uuid = UUID.randomUUID().toString();
+ uuid = uuid.replace("-", "");
+ return uuid.toUpperCase();
+ }
+
+
+ /**
+ *
+ * 字符串过滤,负责将html的textarea属性获得的字符转换成html格式的字符集.
+ * @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("'");
+ break;
+
+ case 34:
+ stringbuffer.append(""");
+ break;
+
+ case 60:
+ stringbuffer.append("<");
+ break;
+
+ case 62:
+ stringbuffer.append(">");
+ break;
+
+ case 38:
+ stringbuffer.append("&");
+ break;
+
+ case 32:
+ stringbuffer.append(" ");
+ break;
+
+ case 10:
+ stringbuffer.append("
");
+ break;
+
+ case 8220:
+ stringbuffer.append("“");
+ break;
+
+ case 8221:
+ stringbuffer.append("”");
+ break;
+
+ default:
+ stringbuffer.append(c);
+ break;
+ }
+ }
+
+ return stringbuffer.toString();
+ }
+
+}
diff --git a/src/com/nis/nmsclient/util/Utils.java b/src/com/nis/nmsclient/util/Utils.java
new file mode 100644
index 0000000..97d5497
--- /dev/null
+++ b/src/com/nis/nmsclient/util/Utils.java
@@ -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 netInterfaces = NetworkInterface.getNetworkInterfaces();
+ while (netInterfaces.hasMoreElements()) {
+ if (findIp) {
+ break;
+ }
+ NetworkInterface ni = netInterfaces.nextElement();
+ logger.debug("------getLocalIp--NetWorkName=" + ni.getName());
+ Enumeration 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 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;
+ }
+
+}
diff --git a/src/com/nis/nmsclient/util/ZipUtil.java b/src/com/nis/nmsclient/util/ZipUtil.java
new file mode 100644
index 0000000..ee8e984
--- /dev/null
+++ b/src/com/nis/nmsclient/util/ZipUtil.java
@@ -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; i0){
+ for(String exclude : excludes){
+ if(exclude.equalsIgnoreCase(srcFile.getCanonicalPath())){
+ return;
+ }
+ }
+ }
+
+ if (srcFile.isDirectory()) {
+ File[] files = srcFile.listFiles();
+ base = base.length() == 0 ? "" : base + "/";
+ if (base.length() > 0) {
+ out.putNextEntry(new ZipEntry(base));
+ /* ZipEntry zipEntry = new ZipEntry(base);
+ zipEntry.setUnixMode(755);// 解决linux乱码
+ out.putNextEntry(zipEntry);*/
+ }
+ for (int i = 0; i < files.length; i++) {
+ zip(out, files[i], base + files[i].getName(), excludes);
+ }
+ } else {
+ base = base.length() == 0 ? srcFile.getName() : base;
+ out.putNextEntry(new ZipEntry(base));
+ /*ZipEntry zipEntry=new ZipEntry(base);
+ zipEntry.setUnixMode(644);//解决linux乱码
+ out.putNextEntry(zipEntry);*/
+ FileInputStream fis = null;
+ try{
+ fis = new FileInputStream(srcFile);
+ IOUtils.copy(fis, out);
+ }catch (IOException e) {
+ throw e;
+ }finally{
+ if(fis!=null) fis.close();
+ }
+ }
+ out.closeEntry();
+ }
+
+ /**
+ * 将单个文件或一个目录打成zip包,并将原文件删除
+ *
+ * @param srcFiles 需要压压缩的文件列表
+ * @param destFile 压缩后的目标文件
+ * @param isAddPrefix 是否要添加文件前缀
+ * @throws Exception
+ */
+ public static void zipWithDelFile(File[] srcFiles, String destFile, boolean isAddPrefix) throws Exception {
+ 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();
+ }
+ }
+}
diff --git a/src/com/nis/nmsclient/util/file/BufferedRandomAccessFile.java b/src/com/nis/nmsclient/util/file/BufferedRandomAccessFile.java
new file mode 100644
index 0000000..ca78182
--- /dev/null
+++ b/src/com/nis/nmsclient/util/file/BufferedRandomAccessFile.java
@@ -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;
+
+/**
+ * Title: BufferedRandomAccessFile
+ * Description: this class provide Buffered Read & Write by extend RandomAccessFile
+ * Copyright: Copyright (c) 2002 Cui Zhixiang
+ * Company: soho
+ * @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)");
+ }
+}
diff --git a/src/com/nis/nmsclient/util/file/Res.java b/src/com/nis/nmsclient/util/file/Res.java
new file mode 100644
index 0000000..7e61830
--- /dev/null
+++ b/src/com/nis/nmsclient/util/file/Res.java
@@ -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;
+ }
+}
\ No newline at end of file
diff --git a/src/com/nis/nmsclient/util/io/UnicodeInputStream.java b/src/com/nis/nmsclient/util/io/UnicodeInputStream.java
new file mode 100644
index 0000000..9e61d42
--- /dev/null
+++ b/src/com/nis/nmsclient/util/io/UnicodeInputStream.java
@@ -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();
+ }
+}
diff --git a/src/com/nis/nmsclient/util/io/UnicodeReader.java b/src/com/nis/nmsclient/util/io/UnicodeReader.java
new file mode 100644
index 0000000..c4169b5
--- /dev/null
+++ b/src/com/nis/nmsclient/util/io/UnicodeReader.java
@@ -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);
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/nis/nmsclient/util/log4j/MyDailyRollingFileAppender.java b/src/com/nis/nmsclient/util/log4j/MyDailyRollingFileAppender.java
new file mode 100644
index 0000000..b003cc4
--- /dev/null
+++ b/src/com/nis/nmsclient/util/log4j/MyDailyRollingFileAppender.java
@@ -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);
+ }
+
+}
diff --git a/src/com/nis/nmsclient/util/log4j/MyRollingFileAppender.java b/src/com/nis/nmsclient/util/log4j/MyRollingFileAppender.java
new file mode 100644
index 0000000..133c592
--- /dev/null
+++ b/src/com/nis/nmsclient/util/log4j/MyRollingFileAppender.java
@@ -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);
+ }
+
+}
diff --git a/src/com/nis/systeminfo/model/DetectInfo.java b/src/com/nis/systeminfo/model/DetectInfo.java
new file mode 100644
index 0000000..8ffd12f
--- /dev/null
+++ b/src/com/nis/systeminfo/model/DetectInfo.java
@@ -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 detailDatas = new LinkedList();
+ /*
+ * 针对公共数据的描述信息
+ */
+ private String descInfo;
+ /*
+ * 该字段是针对一条数据,关联的其他表的数据;写入到一个文件的: String 是解析标识, List是多条数据(String[]存入表中的数据)
+ * 如:系统信息监测,数据中有网卡数量,网卡数量关联着其他表(表中又有多个字段,如网卡名称,IP,子网掩码等等)
+ */
+ private Map> relatedDatas;
+
+ private String diskMsg;//用于存储硬盘只读和磁盘满的异常信息
+
+ public List getDetailDatas() {
+ return detailDatas;
+ }
+ public void setDetailDatas(List detailDatas) {
+ this.detailDatas = detailDatas;
+ }
+ public String getDescInfo() {
+ return descInfo;
+ }
+ public void setDescInfo(String descInfo) {
+ this.descInfo = descInfo;
+ }
+ public Map> getRelatedDatas() {
+ return relatedDatas;
+ }
+ public void setRelatedDatas(Map> relatedDatas) {
+ this.relatedDatas = relatedDatas;
+ }
+ public String getDiskMsg() {
+ return diskMsg;
+ }
+ public void setDiskMsg(String diskMsg) {
+ this.diskMsg = diskMsg;
+ }
+
+}
diff --git a/src/com/nis/systeminfo/model/SystemInfo.java b/src/com/nis/systeminfo/model/SystemInfo.java
new file mode 100644
index 0000000..0a2d42f
--- /dev/null
+++ b/src/com/nis/systeminfo/model/SystemInfo.java
@@ -0,0 +1,1029 @@
+package com.nis.systeminfo.model;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+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 java.util.concurrent.TimeoutException;
+import org.apache.log4j.Logger;
+import org.hyperic.sigar.Cpu;
+import org.hyperic.sigar.CpuInfo;
+import org.hyperic.sigar.CpuPerc;
+import org.hyperic.sigar.FileSystem;
+import org.hyperic.sigar.FileSystemUsage;
+import org.hyperic.sigar.Mem;
+import org.hyperic.sigar.NetFlags;
+import org.hyperic.sigar.NetInterfaceConfig;
+import org.hyperic.sigar.NetInterfaceStat;
+import org.hyperic.sigar.ProcCpu;
+import org.hyperic.sigar.ProcState;
+import org.hyperic.sigar.Sigar;
+import org.hyperic.sigar.SigarException;
+import org.hyperic.sigar.Swap;
+import org.hyperic.sigar.Uptime;
+import org.hyperic.sigar.cmd.Ps;
+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.ProcessUtil;
+import com.nis.nmsclient.util.Utils;
+
+public class SystemInfo
+{
+
+ static Logger logger = Logger.getLogger(SystemInfo.class);
+
+ public static String SEPARATOR = "@#@";
+
+ private Thread thread = null;// 这个属性是取进程信息中启动进程时使用
+
+ public DetectInfo getDetectInfo(String checkType, String pidFile, String processPath,
+ String procSearchKey, boolean isStart)
+ {
+ DetectInfo detectInfo = null;
+ if (checkType != null)
+ {
+ if (checkType.equalsIgnoreCase(Contants.SYS_CHECK_TYPE_CPU))
+ {// CPU
+ detectInfo = testCpuPerc();
+ } else if (checkType.equalsIgnoreCase(Contants.SYS_CHECK_TYPE_MEMORY))
+ {// 内存
+ detectInfo = testPhysicalMemory();
+ } else if (checkType.equalsIgnoreCase(Contants.SYS_CHECK_TYPE_DISK))
+ {// 硬盘
+ detectInfo = testFileSystemInfo();
+ } else if (checkType.equalsIgnoreCase(Contants.SYS_CHECK_TYPE_NET))
+ {// 网络流量
+ detectInfo = testNetDataList();
+ } else if (checkType.equalsIgnoreCase(Contants.SYS_CHECK_TYPE_SYSDATE))
+ {// 系统时间
+ detectInfo = testSystemDate();
+ } else if (checkType.equalsIgnoreCase(Contants.SYS_CHECK_TYPE_PROCESS))
+ {// 进程信息
+ detectInfo = testProcessInfo(pidFile, processPath, procSearchKey, isStart);
+ } else if (checkType.equalsIgnoreCase(Contants.SYS_CHECK_TYPE_SYSTEMINFO))
+ {// 系统信息
+ detectInfo = testSystemInfo();
+ }
+ }
+
+ return detectInfo;
+ }
+
+ // CPU的用户使用量、系统使用剩余量、总的剩余量、总的使用占用量等(单位:100%)
+ public DetectInfo testCpuPerc()
+ {
+ DetectInfo detectInfo = new DetectInfo();
+ try
+ {
+ Sigar sigar = new Sigar();
+ // 不管是单块CPU还是多CPU都适用
+ CpuPerc cpuList[] = sigar.getCpuPercList();
+ CpuInfo[] cpuInfos = sigar.getCpuInfoList();
+// StringBuffer sb2 = new StringBuffer(cpuList.length + "核: ");
+ StringBuffer sb2 = new StringBuffer();
+ double userCpuPerc = 0.0;
+ double sysCpuPerc = 0.0;
+ double waitCpuPerc = 0.0;
+ double niceCpuPerc = 0.0;
+ double idleCpuPerc = 0.0;
+ double totalCpuPerc = 0.0;
+ long mhzCpuPerc = 0;
+ for (int i = 0; i < cpuList.length; i++)
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append("cpu" + i);// 标识
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(cpuList[i].getUser()));// 用户使用率:CPU耗费在不正常的用户进程的使用率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(cpuList[i].getSys()));// 系统使用率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(cpuList[i].getWait()));// 当前等待率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(cpuList[i].getNice()));// 用户使用率:正常的用户进程的CPU使用率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(cpuList[i].getIdle()));// 当前空闲率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(cpuList[i].getCombined()));// 总的使用率
+ sb.append(SEPARATOR);
+ sb.append(cpuInfos[i].getMhz());// 主频
+ detectInfo.getDetailDatas().add(sb.toString().replace("%", ""));
+// sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+"cpu" + i + " 主频" + cpuInfos[i].getMhz() + "MHz,使用率 "
+ sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+"cpu" + i + " i18n_client.SystemInfo.mhz_n81i" + cpuInfos[i].getMhz() + "MHz,i18n_client.SystemInfo.shiyonglv_n81i "
+ + CpuPerc.format(cpuList[i].getCombined()) + "; ");
+
+ // CPU总体信息情况
+ userCpuPerc += cpuList[i].getUser();// 用户使用率累加
+ sysCpuPerc += cpuList[i].getSys();// 系统使用率
+ waitCpuPerc += cpuList[i].getWait();// 当前等待率
+ niceCpuPerc += cpuList[i].getNice();// 用户使用率:正常的用户进程的CPU使用率
+ idleCpuPerc += cpuList[i].getIdle();// 当前空闲率
+ totalCpuPerc += cpuList[i].getCombined();// 总的使用率
+ mhzCpuPerc = cpuInfos[i].getMhz();// 主频
+ }
+
+ StringBuffer sb = new StringBuffer();
+ sb.append("cpu");// 标识
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(userCpuPerc/cpuList.length));// 用户使用率:CPU耗费在不正常的用户进程的使用率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(sysCpuPerc/cpuList.length));// 系统使用率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(waitCpuPerc/cpuList.length));// 当前等待率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(niceCpuPerc/cpuList.length));// 用户使用率:正常的用户进程的CPU使用率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(idleCpuPerc/cpuList.length));// 当前空闲率
+ sb.append(SEPARATOR);
+ sb.append(CpuPerc.format(totalCpuPerc/cpuList.length));// 总的使用率
+ sb.append(SEPARATOR);
+ sb.append(mhzCpuPerc);// 主频
+ detectInfo.getDetailDatas().add(sb.toString().replace("%", ""));
+// sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+"cpu" + " 主频" +mhzCpuPerc + "MHz,使用率 " + CpuPerc.format(totalCpuPerc/cpuList.length) + "; ");
+// sb2.insert(0,cpuList.length + "核: "+Contants.DETEC_STATE_INFO_FORMATE_POINT+"cpu" + " 主频" +mhzCpuPerc + "MHz,使用率 " + CpuPerc.format(totalCpuPerc/cpuList.length) + "; ");
+ sb2.insert(0,cpuList.length + "i18n_client.SystemInfo.core_n81i: "+Contants.DETEC_STATE_INFO_FORMATE_POINT+"cpu" + "i18n_client.SystemInfo.mhz_n81i" +mhzCpuPerc + "MHz,i18n_client.SystemInfo.shiyonglv_n81i " + CpuPerc.format(totalCpuPerc/cpuList.length) + "; ");
+
+ detectInfo.setDescInfo(sb2.toString());
+ logger.debug("testCpuPerc---" + sb2.toString());
+ } catch (Exception e)
+ {
+ logger.error("Getting CPU information exception", e);
+ return null;
+ }
+
+ return detectInfo;
+ }
+
+ // 内存资源信息
+ public DetectInfo testPhysicalMemory()
+ {
+ DetectInfo detectInfo = new DetectInfo();
+ try
+ {
+ Sigar sigar = new Sigar();
+
+ StringBuffer sb = new StringBuffer();
+ // b)系统页面文件交换区信息
+ Swap swap = sigar.getSwap();
+ Long swapTotal = swap.getTotal(); // 交换区总量
+ Long swapFree = swap.getFree();// 当前交换区剩余量
+ sb.append(doubleFormat(swapTotal / (1024 * 1024 * 1024.0), 2));// 交换区总量,单位:"G"
+ sb.append(SEPARATOR);
+ sb.append(doubleFormat(swapFree / (1024 * 1024 * 1024.0), 2));// 当前交换区剩余量,单位:"G"
+ sb.append(SEPARATOR);
+ // sb.append(SEPARATOR);
+ // sb.append(swap.getUsed() / 1024L);// 当前交换区使用量,单位:"K"
+ // a)物理内存信息
+ Mem mem = sigar.getMem();
+ Long memTotal = mem.getTotal();// 内存总量
+ Long memUsed = mem.getUsed();// 当前内存使用量
+ Long memFree = mem.getFree();// 当前内存剩余量
+ sb.append(doubleFormat(memTotal / (1024 * 1024 * 1024.0), 2));// 内存总量,单位:"G"
+ sb.append(SEPARATOR);
+ sb.append(doubleFormat(memUsed / (1024 * 1024 * 1024.0), 2));// 当前内存使用量,单位:"G"
+ sb.append(SEPARATOR);
+ sb.append(doubleFormat(memFree / (1024 * 1024 * 1024.0), 2));// 当前内存剩余量,单位:"G"
+ sb.append(SEPARATOR);
+ sb.append(doubleFormat(((memTotal - memFree) * 100.0) / memTotal, 1));// 内存使用率
+
+ StringBuffer sb2 = new StringBuffer();
+// sb2.append("内存总大小: " + doubleFormat(memTotal / (1024 * 1024 * 1024.0), 2) + "G, 现使用:"
+// + doubleFormat(memUsed / (1024 * 1024 * 1024.0), 2) + "G, 剩余:"
+// + doubleFormat(memFree / (1024 * 1024 * 1024.0), 2) + "G, 使用率:"
+// + doubleFormat(((memTotal - memFree) * 100.0) / memTotal, 1) + "%");
+ sb2.append("i18n_client.SystemInfo.memerySize_n81i: " + doubleFormat(memTotal / (1024 * 1024 * 1024.0), 2) + "G, i18n_client.SystemInfo.currentUsed_n81i:"
+ + doubleFormat(memUsed / (1024 * 1024 * 1024.0), 2) + "G, i18n_client.SystemInfo.spaceRemain_n81i:"
+ + doubleFormat(memFree / (1024 * 1024 * 1024.0), 2) + "G, i18n_client.SystemInfo.shiyonglv_n81i:"
+ + doubleFormat(((memTotal - memFree) * 100.0) / memTotal, 1) + "%");
+
+ detectInfo.getDetailDatas().add(sb.toString());
+ detectInfo.setDescInfo(sb2.toString());
+
+ logger.debug("testPhysicalMemory--sb2=[ " + sb2.toString() + " ] -- tt="
+ + doubleFormat(mem.getUsedPercent(), 1));
+ } catch (SigarException e)
+ {
+ logger.error("Getting the exception of memory information", e);
+ return null;
+ }
+
+ return detectInfo;
+ }
+
+ // 资源信息(主要是硬盘已有的分区及其详细信息)
+ public DetectInfo testFileSystemInfo()
+ {
+ int num=0;
+ DetectInfo detectInfo = new DetectInfo();
+ try
+ {
+ Sigar sigar = new Sigar();
+ long allTotal = 0l;
+ long allUse = 0l;
+ // long allFree = 0l;
+ StringBuffer sb2 = new StringBuffer();
+ String dirName = "";
+ FileSystem fslist[] = sigar.getFileSystemList();
+ for (int i = 0; i < fslist.length; i++)
+ {
+ FileSystem fs = fslist[i];
+ try
+ {
+ FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
+ if (fs.getType() == 2)
+ {// 先取每个盘符的大小和剩余空间
+ long total = usage.getTotal();// 文件系统总大小, 单位:"KB"
+ long free = usage.getFree();// 文件系统剩余大小, 单位:"KB"
+ String usePerc = doubleFormat(usage.getUsePercent() * 100, 1); // 文件系统资源的利用率
+
+ allTotal += total;// 文件系统总大小, 单位:"KB"
+ // allFree += free;// 文件系统剩余大小, 单位:"KB"
+ allUse += usage.getUsed();//文件系统使用大小, 单位:"KB"
+
+ StringBuffer sb = new StringBuffer();
+ sb.append(fs.getDirName());
+ dirName +=fs.getDirName()+",";
+ sb.append(SEPARATOR);
+ sb.append(doubleFormat(total / (1024 * 1024.0), 2));// 文件系统总大小,
+ // 单位:"GB"
+ sb.append(SEPARATOR);
+ sb.append(doubleFormat(free / (1024 * 1024.0), 2));// 文件系统剩余大小,
+ // 单位:"GB"
+ sb.append(SEPARATOR);
+ sb.append(usePerc);// 文件系统资源的利用率
+ sb.append(SEPARATOR);
+ if (checkFileSystemIsWrite(fs.getDirName()))
+ {// 文件系统是否可写
+ sb.append("0");// 可写:0
+ } else
+ {
+ sb.append("1");// 不可写:1
+ num ++;
+ }
+
+ detectInfo.getDetailDatas().add(sb.toString());
+
+// sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+"【" + fs.getDirName() + "】大小 "
+// + doubleFormat(total / (1024 * 1024.0), 2) + "G, 已使用 "
+// + doubleFormat((total-free) / (1024 * 1024.0), 2) + "G, 剩余 "
+// + doubleFormat(free / (1024 * 1024.0), 2) + "G, Usage 使用率 "
+// + usePerc + "% ; ");
+ sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+"【" + fs.getDirName() + "】i18n_client.SystemInfo.size_n81i "
+ + doubleFormat(total / (1024 * 1024.0), 2) + "G, i18n_client.SystemInfo.used1_n81i "
+ + doubleFormat((total-free) / (1024 * 1024.0), 2) + "G, i18n_client.SystemInfo.spaceRemain_n81i "
+ + doubleFormat(free / (1024 * 1024.0), 2) + "G, i18n_client.SystemInfo.shiyonglv_n81i "
+ + usePerc + "% ; ");
+ }
+ } catch (SigarException e)
+ {
+ if (fs.getType() == 2)
+ throw e;
+ continue;
+ }
+ }
+ // 文件系统资源的总利用率
+ // String allUsePerc = doubleFormat((allUse * 1.0 / allTotal) * 100,
+ // 1);
+
+ if(num==detectInfo.getDetailDatas().size()){//所有硬盘不可写 主动告警
+// String msg = "磁盘"+dirName.substring(0,dirName.length()-1)+"只读";
+ String msg = "i18n_client.SystemInfo.disk_n81i"+dirName.substring(0,dirName.length()-1)+"i18n_client.SystemInfo.readOnly_n81i";
+ detectInfo.setDiskMsg(msg);
+ return detectInfo;
+ }
+ if(allUse==allTotal){//当磁盘满时候主动告警
+// String msg = "磁盘总大小"+doubleFormat(allTotal / (1024 * 1024.0), 2)+"G,总的使用率 100%,分区 "+dirName.substring(0,dirName.length()-1)+"剩余 0%";
+ String msg = "i18n_client.SystemInfo.diskSize_n81i"+doubleFormat(allTotal / (1024 * 1024.0), 2)+"G,i18n_client.SystemInfo.zongShiYongLv_n81i,i18n_client.SystemInfo.zone_n81i "+dirName.substring(0,dirName.length()-1)+"i18n_client.SystemInfo.spaceRemain_n81i 0%";
+ detectInfo.setDiskMsg(msg);
+ return detectInfo;
+ }
+
+ DecimalFormat decimalFormat = new DecimalFormat("0.00");
+// sb2.insert(0, "磁盘总大小:" + doubleFormat(allTotal / (1024 * 1024.0), 2) + "G,已使用 "+decimalFormat.format(((double)allUse/(double)allTotal)*100)+"% ; ");
+ sb2.insert(0, "i18n_client.SystemInfo.diskSize_n81i:" + doubleFormat(allTotal / (1024 * 1024.0), 2) + "G,i18n_client.SystemInfo.used2_n81i "+decimalFormat.format(((double)allUse/(double)allTotal)*100)+"% ; ");
+ detectInfo.setDescInfo(sb2.toString());
+ logger.debug("testFileSystemInfo--sb2=[ " + sb2.toString() + " ]");
+
+ } catch (SigarException e)
+ {
+ logger.error("Getting hard disk information abnormity", e);
+ return null;
+ }
+
+ return detectInfo;
+ }
+
+ // 获取网络流量等信息
+ public DetectInfo testNetDataList()
+ {
+ DetectInfo detectInfo = new DetectInfo();
+ try
+ {
+ Sigar sigar = new Sigar();
+ String ifNames[] = sigar.getNetInterfaceList();
+ if (ifNames.length > 0)
+ {
+ StringBuffer sb2 = new StringBuffer();
+ int ethernetCnt = 0;
+
+ // 2013-02-26 设备名的处理并去重,这个是针对一块网卡有多个逻辑配置 如 eth0和eth0:1,只取eth0即可
+ List nameList = new LinkedList();
+ for (int i = 0; i < ifNames.length; i++)
+ {
+ String name = ifNames[i];
+
+ if (name.indexOf(":") != -1)
+ {
+ name = name.substring(0, name.indexOf(":"));
+ }
+
+ if (!nameList.contains(name))
+ {
+ nameList.add(name);
+ }
+ }
+
+ for (int i = 0; i < nameList.size(); i++)
+ {
+ String name = nameList.get(i);
+
+ NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
+ logger.debug("testNetDataList----type= " + ifconfig.getType());
+
+ // *** 网卡类型:ethernet, Local Loopback(本地环回网卡)
+ // *** 这里只取 ethernet 网卡类型
+ if ((ifconfig.getFlags() & NetFlags.IFF_LOOPBACK) > 0)
+ {
+ continue;
+ }
+ /*
+ * if ((ifconfig.getFlags() & NetFlags.IFF_POINTOPOINT) > 0) {
+ * continue; }
+ */
+ // 2013-4-22 针对LInux,按名称过滤掉sit0,实际命令查看类型为:IPv6-in-IPv4
+ if (name.equals("sit0"))
+ {
+ continue;
+ }
+ // 2013-5-23 针对Win7取到的网卡数量太多问题
+ if ((ifconfig.getFlags() & NetFlags.IFF_BROADCAST) <= 0)
+ {// broadcast地址无效
+ continue;
+ }
+ if ((ifconfig.getFlags() & NetFlags.IFF_MULTICAST) <= 0)
+ {// 不支持multicast
+ continue;
+ }
+ ethernetCnt++;
+
+ if ((ifconfig.getFlags() & NetFlags.IFF_UP) <= 0)
+ {
+ // print("!IFF_UP...skipping getNetInterfaceStat");
+// sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+name + " 不可用; ");
+ sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+name + " i18n_client.SystemInfo.disable_n81i; ");
+ continue;
+ }
+
+ NetInterfaceStat ifstat;
+ try
+ {
+ ifstat = sigar.getNetInterfaceStat(name);
+ } catch (SigarException e)
+ {
+ continue;
+ }
+ Long rxPackets1 = ifstat.getRxPackets();// 接收的总包裹数
+ Long txPackets1 = ifstat.getTxPackets();// 发送的总包裹数
+ Long rxBytes1 = ifstat.getRxBytes();// 接收到的总字节数
+ Long txBytes1 = ifstat.getTxBytes();// 发送的总字节数
+ Long rxErrors1 = ifstat.getRxErrors();// 接收到的错误包数
+ Long txErrors1 = ifstat.getTxErrors();// 发送数据包时的错误数
+ Long rxDropped1 = ifstat.getRxDropped();// 接收时丢弃的包数
+ Long txDropped1 = ifstat.getTxDropped();// 发送时丢弃的包数
+
+ int seconds = 5;
+ try
+ {
+ Thread.sleep(seconds * 1000);// 等待5秒
+ } catch (InterruptedException e)
+ {
+ e.printStackTrace();
+ }
+
+ ifstat = sigar.getNetInterfaceStat(name);
+ Long rxPackets2 = ifstat.getRxPackets();// 接收的总包裹数
+ Long txPackets2 = ifstat.getTxPackets();// 发送的总包裹数
+ Long rxBytes2 = ifstat.getRxBytes();// 接收到的总字节数
+ Long txBytes2 = ifstat.getTxBytes();// 发送的总字节数
+ Long rxErrors2 = ifstat.getRxErrors();// 接收到的错误包数
+ Long txErrors2 = ifstat.getTxErrors();// 发送数据包时的错误数
+ Long rxDropped2 = ifstat.getRxDropped();// 接收时丢弃的包数
+ Long txDropped2 = ifstat.getTxDropped();// 发送时丢弃的包数
+
+
+ // 添加监测日志,用于检查网络流量出现负值的原因
+ logger.debug(" bps 输入: rxBytes1:" + rxBytes1 + ", rxBytes2:" + rxBytes2);
+ logger.debug(" bps 输出: txBytes1:" + txBytes1 + ", txBytes2:" + txBytes2);
+ logger.debug(" pps 输入: rxPackets1:" + rxPackets1 + ", rxPackets2:" + rxPackets2);
+ logger.debug(" pps 输出: txPackets1:" + txPackets1 + ", txPackets2:" + txPackets2);
+
+ Long speed = ifstat.getSpeed() / (1000 * 1000);// 带宽:Mbps
+ // bps 输入:((第二次接收的总字节数-第一次)*8)/(时间秒*1000*1000)Mbps
+ // String rx_bps = doubleFormat(((rxBytes2 - rxBytes1) * 8)
+ // / (seconds * 1000 * 1000), 2);
+ // bps 输入:((第二次接收的总字节数-第一次)*8)/(时间秒)bps
+ String rx_bps = doubleFormat(((rxBytes2 - rxBytes1) * 8) / (seconds), 2);
+ // bps 输出:((第二次发送的总字节数-第一次)*8)/(时间秒)bps
+ String tx_bps = doubleFormat(((txBytes2 - txBytes1) * 8) / (seconds), 2);
+ // pps 输入:(第二次接收的总包裹数-第一次)/时间秒
+ Long rx_pps = (rxPackets2 - rxPackets1) / seconds;
+ // pps 输出:(第二次发送的总包裹数-第一次)/时间秒
+ Long tx_pps = (txPackets2 - txPackets1) / seconds;
+ Long rx_errorPerc = 0l;// 接收错包率
+ Long tx_errorPerc = 0l;// 发送错包率
+ Long rx_droppedPerc = 0l;// 接收丢包率
+ Long tx_droppedPerc = 0l;// 发送丢包率
+ if (rxPackets2 - rxPackets1 > 0)
+ {
+ rx_errorPerc = (rxErrors2 - rxErrors1) / (rxPackets2 - rxPackets1) * 100;// 接收错包率
+ rx_droppedPerc = (rxDropped2 - rxDropped1) / (rxPackets2 - rxPackets1)
+ * 100;// 接收丢包率
+ }
+ if (txPackets2 - txPackets1 > 0)
+ {
+ tx_errorPerc = (txErrors2 - txErrors1) / (txPackets2 - txPackets1) * 100;// 发送错包率
+ tx_droppedPerc = (txDropped2 - txDropped1) / (txPackets2 - txPackets1)
+ * 100;// 发送丢包率
+ }
+
+ StringBuffer sb = new StringBuffer();
+ sb.append(name);// 网卡名称
+ sb.append(SEPARATOR);
+ sb.append(rxPackets2);// 接收的总包裹数
+ sb.append(SEPARATOR);
+ sb.append(txPackets2);// 发送的总包裹数
+ sb.append(SEPARATOR);
+ sb.append(rxBytes2);// 接收到的总字节数
+ sb.append(SEPARATOR);
+ sb.append(txBytes2);// 发送到的总字节数
+ sb.append(SEPARATOR);
+ sb.append(rxErrors2);// 接收到的错误包数
+ sb.append(SEPARATOR);
+ sb.append(txErrors2);// 发送到的错误包数
+ sb.append(SEPARATOR);
+ sb.append(rxDropped2);// 接收时丢弃的包数
+ sb.append(SEPARATOR);
+ sb.append(txDropped2);// 发送时丢弃的包数
+ sb.append(SEPARATOR);
+ sb.append(speed);// 带宽:Mbps
+ sb.append(SEPARATOR);
+ sb.append(rx_bps);// bps 输入:Mbps
+ sb.append(SEPARATOR);
+ sb.append(tx_bps);// bps 输出:Mbps
+ sb.append(SEPARATOR);
+ sb.append(rx_pps);// pps 输入:每秒接收包数
+ sb.append(SEPARATOR);
+ sb.append(tx_pps);// pps 输出:每秒发送包数
+ sb.append(SEPARATOR);
+ sb.append(rx_errorPerc);// 接收错包率
+ sb.append(SEPARATOR);
+ sb.append(tx_errorPerc);// 发送错包率
+ sb.append(SEPARATOR);
+ sb.append(rx_droppedPerc);// 接收丢包率
+ sb.append(SEPARATOR);
+ sb.append(tx_droppedPerc);// 发送丢包率
+ detectInfo.getDetailDatas().add(sb.toString());
+
+// sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+name + " 带宽" + speed + "Mbps, 输入" + rx_bps + "bps、" + rx_pps
+// + "pps, 输出" + tx_bps + "bps、" + tx_pps + "pps; ");
+ sb2.append(Contants.DETEC_STATE_INFO_FORMATE_POINT+name + " i18n_client.SystemInfo.netSpeed_n81i" + speed + "Mbps, i18n_client.SystemInfo.input_n81i" + rx_bps + "bps、" + rx_pps
+ + "pps, Output" + tx_bps + "bps、" + tx_pps + "pps; ");
+ }
+// sb2.insert(0, ethernetCnt + "个逻辑网卡: ");
+ sb2.insert(0, ethernetCnt + "i18n_client.SystemInfo.insert_n81i: ");
+ detectInfo.setDescInfo(sb2.toString());
+ logger.debug("testNetDataList----" + sb2);
+ }
+
+ } catch (SigarException e)
+ {
+ logger.error("Obtaining network information abnormity", e);
+ return null;
+ }
+ return detectInfo;
+ }
+
+ public DetectInfo testSystemDate()
+ {
+ DetectInfo detectInfo = new DetectInfo();
+ try
+ {
+ String sysDateStr = getServerSystemDate();
+ if (sysDateStr != null)
+ {
+ long sysDate = Long.parseLong(sysDateStr);// 服务器系统时间
+ long localDate = System.currentTimeMillis();// 客户机系统时间
+ long timeDelay = Math.abs(DateUtil.getMinutesFromBeginToEnd(sysDate, localDate));
+
+ detectInfo.getDetailDatas().add(sysDate + SEPARATOR + localDate + SEPARATOR
+ + timeDelay);
+ detectInfo.setDescInfo("server:"
+ + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(sysDate))
+ + " agent:"
+ + DateUtil
+ .getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(localDate)));
+ } else
+ {
+// detectInfo.setDescInfo("获取DataController系统时间失败");
+ detectInfo.setDescInfo("i18n_client.SystemInfo.getSysTimeErr_n81i");
+ }
+
+ } catch (Exception e)
+ {
+ logger.error("Getting the time anomaly of the system", e);
+ return null;
+ }
+ return detectInfo;
+ }
+
+ /*
+ * 获取服务端系统时间
+ */
+ private static String getServerSystemDate()
+ {
+ String date = null;
+ try
+ {
+ Future> future = Common.service.submit(new SSLClient(
+ Thread.currentThread().getName(), CommonSocket.REQ_SERVER_SYSTEMDATE, null));
+ String msg = (String) future.get();
+ if (Contants.isSucessByResult(msg))
+ {
+ date = Contants.getDescByResult(msg);
+ }
+ } catch (Exception e)
+ {
+ logger.error("Getting the time exception of the server system:" + Utils.printExceptionStack(e));
+ date = null;
+ }
+
+ return date;
+ }
+
+ public DetectInfo testProcessInfo(String pidFile, final String procPath, String procSearchKey,
+ boolean isStart)
+ {
+ DetectInfo detectInfo = new DetectInfo();
+ try
+ {
+ Object[] objArr = ProcessUtil.checkPidAndGetPid(pidFile, procSearchKey);
+ int isExistFlag = Integer.parseInt(objArr[0].toString());
+ String pidInfo = objArr[1].toString();
+
+ if (isStart && isExistFlag == 0)
+ {// Agent启动且进程不存在,启动
+ logger.info("进程“" + procPath + "”不存在,正在启动……");
+ // -------------------启动进程开始
+ final String threadName = Thread.currentThread().getName();
+ Future future = Common.service.submit(new Runnable()
+ {
+
+ @Override
+ public void run()
+ {
+ thread = Thread.currentThread();
+ Thread.currentThread().setName(threadName);
+ try
+ {
+ ProcessUtil.runExec(procPath, null, null, null);
+ } catch (IOException e)
+ {
+ logger.error(e);
+ }
+ }
+
+ });
+ // ----获取最大等待时间(单位秒)
+ long delay = 3 * 60;// 3分钟
+ // ----超过一定时间,终止执行线程,返回结果超时
+ try
+ {
+ future.get(delay, TimeUnit.SECONDS);
+ } catch (TimeoutException e)
+ {
+ if (thread != null && thread.isAlive())
+ {
+ thread.stop();
+ logger.debug(procPath + "---testProcessInfo Timeout stop thread--"
+ + thread.isAlive());
+ }
+ future.cancel(true);
+ logger.info("执行“" + procPath + "”, 超时");
+ }
+ // -------------------启动进程结束
+ // -------------------检查PID
+ objArr = ProcessUtil.checkPidAndGetPid(pidFile, procSearchKey);
+ isExistFlag = Integer.parseInt(objArr[0].toString());
+ pidInfo = objArr[1].toString();
+ if (isExistFlag != 0)
+ {
+ logger.info("进程“" + procPath + "”启动成功");
+ } else
+ {
+ logger.info("进程“" + procPath + "”启动失败");
+ }
+ }
+ logger.debug("testProcessInfo pidInfo --> " + pidInfo);
+ if(isExistFlag == 0){
+ StringBuffer sb = new StringBuffer();
+ //sb.append(CpuPerc.format(procCpu.getTotal() * 1.0 / cpuTotal));// 进程的CPU使用率
+ sb.append(SEPARATOR);
+ //sb.append(doubleFormat((memUse * 1.0 / sigar.getMem().getTotal()) * 100, 1) + "%");// 进程的内存使用率
+ sb.append(SEPARATOR);
+ //sb.append(procCpu.getStartTime());// 进程的启动时间
+ sb.append(SEPARATOR);
+ sb.append("NO");// 进程状态
+ sb.append(SEPARATOR);
+ //sb.append(state.getPriority());// 进程优先级
+
+ int index = 0;
+ String[] tmp = sb.toString().split(SEPARATOR);
+
+ detectInfo.getDetailDatas().add(sb.toString().replace("%", ""));
+ detectInfo.setDescInfo(pidInfo);
+ return detectInfo;
+ }
+ if (isExistFlag != 1)
+ {// 进程不存在或找到多个进程
+ detectInfo.setDescInfo(pidInfo);
+ return detectInfo;
+ } else
+ {
+ long pid = Long.parseLong(pidInfo);
+
+ Sigar sigar = new Sigar();
+ // 获取进程的CPU信息
+ ProcCpu procCpu = sigar.getProcCpu(pid);
+ long cpuTotal = 0;
+ Cpu[] cpuArr = sigar.getCpuList();
+ for (Cpu cpu2: cpuArr)
+ {
+ cpuTotal += cpu2.getTotal();
+ }
+ logger.debug("testProcessInfo---->cpuTotal=" + cpuTotal);
+ logger.debug("testProcessInfo---->cpuUse=" + procCpu.getTotal());
+
+ ProcState state = sigar.getProcState(pid);
+
+ Ps ps = new Ps();
+ List list = ps.getInfo(sigar, pid);
+ String memUseStr = "";
+ for (int i = 0; i < list.size(); i++)
+ {
+ switch (i)
+ {
+ // case 0 : System.out.print("--Pid=" + list.get(0));
+ // break;
+ // case 1 : System.out.print("\tUser=" + list.get(1));
+ // break;
+ // case 2 : System.out.print("\tStartTimes=" +
+ // list.get(2)); break;
+ // case 3 : memSizeStr= list.get(3); break;
+ case 4:
+ memUseStr = list.get(4);
+ break;
+ // case 5 : logger.debug("testProcessInfo---->MemHare="
+ // + list.get(5)); break;
+ // case 6 : System.out.print("\tState=" + list.get(6));
+ // break;
+ // case 7 : System.out.print("\tCpuTime=" +
+ // list.get(7)); break;
+ // case 8 : System.out.println("\tName=" + list.get(8));
+ // break;
+ default:
+ break;
+ }
+ }
+ double memUse = Double.parseDouble(memUseStr.substring(0, memUseStr.length() - 1));
+ char useUnit = memUseStr.toUpperCase().charAt(memUseStr.length() - 1);
+ switch (useUnit)
+ {
+ case 'K':
+ memUse = memUse * 1024;
+ break;
+ case 'M':
+ memUse = memUse * 1024 * 1024;
+ break;
+ case 'G':
+ memUse = memUse * 1024 * 1024 * 1024;
+ break;
+ case 'T':
+ memUse = memUse * 1024 * 1024 * 1024 * 1024;
+ break;
+ case 'P':
+ memUse = memUse * 1024 * 1024 * 1024 * 1024 * 1024;
+ break;
+ default:
+ break;
+ }
+
+ logger.debug("testProcessInfo---->memTotal=" + sigar.getMem().getTotal() / 1024
+ + "KB");
+ logger.debug("testProcessInfo---->memUseStr=" + memUseStr);
+ logger.debug("testProcessInfo---->memUse=" + memUse);
+
+ StringBuffer sb = new StringBuffer();
+ sb.append(CpuPerc.format(procCpu.getTotal() * 1.0 / cpuTotal));// 进程的CPU使用率
+ sb.append(SEPARATOR);
+ sb.append(doubleFormat((memUse * 1.0 / sigar.getMem().getTotal()) * 100, 1) + "%");// 进程的内存使用率
+ sb.append(SEPARATOR);
+ sb.append(procCpu.getStartTime());// 进程的启动时间
+ sb.append(SEPARATOR);
+ sb.append(state.getState());// 进程状态
+ sb.append(SEPARATOR);
+ sb.append(state.getPriority());// 进程优先级
+
+ int index = 0;
+ String[] tmp = sb.toString().split(SEPARATOR);
+ StringBuffer sb2 = new StringBuffer();
+ sb2.append("cpuUsage:" + tmp[index++]);
+ sb2.append(" memUsage:" + tmp[index++]);
+ sb2.append(" startTime:"
+ + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(Long
+ .parseLong(tmp[index++]))));
+ sb2.append(" state:" + tmp[index++]);
+ sb2.append(" priority:" + tmp[index++]);
+
+ detectInfo.getDetailDatas().add(sb.toString().replace("%", ""));
+ detectInfo.setDescInfo(sb2.toString());
+
+ logger.debug("testProcessInfo---->" + sb2.toString());
+
+ }
+ } catch (Exception e)
+ {
+ logger.error("Getting process information abnormity", e);
+ return null;
+ }
+
+ return detectInfo;
+ }
+
+ public DetectInfo testSystemInfo()
+ {
+ DetectInfo detectInfo = new DetectInfo();
+ try
+ {
+ Sigar sigar = new Sigar();
+
+ StringBuffer sb = new StringBuffer();
+ sb.append(sigar.getNetInfo().getHostName());// 主机名称
+ sb.append(SEPARATOR);
+ // 操作系统和发行版本 (os.getVendorName() + " " + os.getVersion())
+ // Linux: uname -r;uname -i;lsb_release -d| cut -d: -f2| cut -f2
+ // Windows: ver
+ String operateSystem = Utils.getOperateSystem();
+ sb.append(operateSystem);// 操作系统和发行版本
+ sb.append(SEPARATOR);
+ sb.append(sigar.getCpuInfoList().length);// CPU核数
+ sb.append(SEPARATOR);
+ sb.append(sigar.getCpuInfoList()[0].getMhz());// CPU主频
+ sb.append(SEPARATOR);
+ String totalMem = doubleFormat(sigar.getMem().getTotal() / (1024 * 1024 * 1024.0), 2);
+ sb.append(totalMem);// 内存大小,单位:G
+ sb.append(SEPARATOR);
+ sb.append(doubleFormat(sigar.getSwap().getTotal() / (1024 * 1024 * 1024.0), 2));// SWAP大小,单位:G
+ sb.append(SEPARATOR);
+
+ long allTotal = 0;
+ // **************** 以下是各分区大小 ******************* //
+ List diskDatas = new ArrayList();
+ FileSystem fslist[] = sigar.getFileSystemList();
+ for (int i = 0; i < fslist.length; i++)
+ {
+ FileSystem fs = fslist[i];
+ try
+ {
+ FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
+ if (fs.getType() == 2)
+ {// 先取每个盘符的大小和剩余空间
+ long total = usage.getTotal();// 文件系统大小, 单位:"KB"
+ allTotal += total;// 文件系统总大小, 单位:"KB"
+ // 各分区名称和大小(单位G)
+ diskDatas.add(new String[] { fs.getDirName(),
+ doubleFormat(total / (1024 * 1024.0), 2) });
+ }
+ } catch (SigarException e)
+ {
+ if (fs.getType() == 2)
+ throw e;
+ continue;
+ }
+ }
+ // ---------------- 以上是各分区大小 ------------------- //
+ sb.append(doubleFormat(allTotal / (1024 * 1024.0), 2));// 硬盘总大小,单位:G
+ sb.append(SEPARATOR);
+
+ int ethernetCnt = 0;
+ // **************** 以下是各网卡状态 ******************* //
+ List netDatas = new ArrayList();
+ String ifNames[] = sigar.getNetInterfaceList();
+ if (ifNames.length > 0)
+ {
+ String defaultGateWay = sigar.getNetInfo().getDefaultGateway();
+ for (int i = 0; i < ifNames.length; i++)
+ {
+ String name = ifNames[i];
+
+ NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
+
+ // *** 网卡类型:ethernet, Local Loopback(本地环回网卡)
+ // *** 这里只取 ethernet 网卡类型
+ if ((ifconfig.getFlags() & NetFlags.IFF_LOOPBACK) > 0)
+ {
+ continue;
+ }
+ /*
+ * if ((ifconfig.getFlags() & NetFlags.IFF_POINTOPOINT) > 0) {
+ * continue; }
+ */
+ // 2013-4-22 针对LInux,按名称过滤掉sit0,实际命令查看类型为:IPv6-in-IPv4
+ if (name.equals("sit0"))
+ {
+ continue;
+ }
+ // 2013-5-23 针对Win7取到的网卡数量太多问题
+ if ((ifconfig.getFlags() & NetFlags.IFF_BROADCAST) <= 0)
+ {// broadcast地址无效
+ continue;
+ }
+ if ((ifconfig.getFlags() & NetFlags.IFF_MULTICAST) <= 0)
+ {// 不支持multicast
+ continue;
+ }
+ ethernetCnt++;
+
+// String state = "可用";
+ String state = "Available";
+ if ((ifconfig.getFlags() & NetFlags.IFF_UP) <= 0)
+ {// 网卡状态: 2050 网络电缆被拔出 2115 已连接上
+// state = "不可用";
+ state = "Unavailable";
+ }
+ String speed = null;
+ try
+ {
+ NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
+ speed = ifstat.getSpeed() / (1000 * 1000) + "";// 带宽:Mbps
+ } catch (SigarException e)
+ {
+ speed = "";
+ }
+
+ netDatas.add(new String[] { name, // 网卡名称
+ state, // 网卡状态
+ speed, // 带宽
+ ifconfig.getAddress(), // IP
+ ifconfig.getNetmask(), // 子网掩码
+ defaultGateWay, // 网关
+ ifconfig.getHwaddr() // Mac地址
+ });
+ }
+ }
+ // System.out.println(sigar.getNetInfo().getDefaultGateway());
+ // ---------------- 以上是各网卡状态 ------------------- //
+ sb.append(ethernetCnt);// 网卡数量
+
+ detectInfo.getDetailDatas().add(sb.toString());
+
+ StringBuffer sb2 = new StringBuffer();
+// sb2.append(sigar.getNetInfo().getHostName() + ":" + operateSystem + ", "
+// + sigar.getCpuInfoList().length + "核CPU, " + sigar.getCpuInfoList()[0].getMhz()
+// + "MHz, " + totalMem + "GB的内存, " + doubleFormat(allTotal / (1024 * 1024.0), 2)
+// + "GB的硬盘, " + ethernetCnt + "个逻辑网卡");
+ sb2.append(sigar.getNetInfo().getHostName() + ":" + operateSystem + ", "
+ + sigar.getCpuInfoList().length + "i18n_client.SystemInfo.message.core_n81i CPU, " + sigar.getCpuInfoList()[0].getMhz()
+ + "MHz, " + totalMem + "i18n_client.SystemInfo.message.memery_n81i, " + doubleFormat(allTotal / (1024 * 1024.0), 2)
+ + "i18n_client.SystemInfo.message.disk_n81i, " + ethernetCnt + "i18n_client.SystemInfo.message.netcard_n81i");
+ detectInfo.setDescInfo(sb2.toString());
+ logger.debug("testSystemInfo--sb2--" + sb2.toString());
+
+ Map> relatedDataMap = new HashMap>();
+ relatedDataMap.put("disk", diskDatas);
+ relatedDataMap.put("net", netDatas);
+ detectInfo.setRelatedDatas(relatedDataMap);
+
+ } catch (Exception e)
+ {
+ logger.error("Getting the information anomaly of the system", e);
+ return null;
+ }
+
+ return detectInfo;
+ }
+
+ public static String getStartComputerTime()
+ {
+ String value = "";
+ Sigar sigar = new Sigar();
+ try
+ {
+ Uptime uptime = sigar.getUptime();
+ Double d = new Double(uptime.getUptime());
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.SECOND, -d.intValue());
+
+ value = cal.getTimeInMillis() + "";
+ } catch (SigarException e)
+ {
+ logger.error("Getting the boot time anomaly", e);
+ return value;
+ }
+
+ return value;
+ }
+
+ private String doubleFormat(double val, int xsws)
+ {
+// String pattern = "#.";
+ String pattern = "0.";
+ if (xsws < 1)
+ {
+ xsws = 1;
+ }
+ for (int i = 1; i <= xsws; i++)
+ {
+ pattern += "0";
+ }
+
+ String temp = new DecimalFormat(pattern).format(val);
+// if (val < 1)
+// {
+// return "0" + temp;
+// } else
+// {
+// return temp;
+// }
+ return temp;
+ }
+
+ /**
+ * 检查硬盘是否可写:在指定盘符目录下写指定字节大小的文件,写异常-不可写,正常-可写
+ *
+ * @param fsDirName
+ * 盘符
+ * @return
+ */
+ private boolean checkFileSystemIsWrite(String fsDirName)
+ {
+ boolean isWrite = true;
+ OutputStreamWriter fos = null;
+ File file = new File(fsDirName + File.separator + "writetest.temp");
+ logger.debug("checkFileSystemIsWrite---file path = " + file.getAbsolutePath());
+ try
+ {
+ fos = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
+// fos.write("测试硬盘是否可写,现在开始写入文件\n");
+ fos.write("i18n_client.SystemInfo.outputTest_n81i");
+ fos.flush();
+ } catch (IOException e)
+ {
+ logger.error("Test whether the hard disk can be written:" + e.getMessage());
+ isWrite = false;
+ } finally
+ {
+ if (fos != null)
+ {
+ try
+ {
+ fos.close();
+ } catch (IOException e)
+ {
+ }
+ }
+ }
+ if (file.exists())
+ {// 不管是否写异常都将删除文件
+ file.delete();
+ logger.debug("checkFileSystemIsWrite---delete file = " + file.getAbsolutePath());
+ }
+ return isWrite;
+ }
+
+ public SystemInfo()
+ {
+ }
+
+}
diff --git a/src/com/nis/systeminfo/thread/GetInfoRun.java b/src/com/nis/systeminfo/thread/GetInfoRun.java
new file mode 100644
index 0000000..17cf8a2
--- /dev/null
+++ b/src/com/nis/systeminfo/thread/GetInfoRun.java
@@ -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 alarmInfos;
+
+ private int diskUsageTime=0;
+ private int cpuUsageTime=0;
+ private String cpuUsageName="";
+ private String diskUsageName="";
+
+ private List detailDatas ;
+
+ //暂存各告警字段连续超过告警值的次数
+ //public static Map alarmTimes = new HashMap();
+
+ //暂存各告警信息告警状态
+ //public static Map alarmStates = new HashMap();
+
+ private int alarmTimes = 0;
+ private boolean alarmState = false;
+
+ private String firstPerformData="";
+
+ public GetInfoRun(String name, SetInfo setInfo, Date startTime, List 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 detailDataList = new LinkedList();
+ 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 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] + "");// 组织性能数据
+ }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+"");
+ }
+ // ------- 主动告警
+ String totalAlarmInfo = null;
+ if(totalStatus == Contants.DETECTION_STATUS_NORMAL){
+ // 本次数据正常,看原本的状态:若是告警状态,则发送恢复通知;若是正常状态,则不变
+ if(alarmState){
+ totalAlarmInfo = "【i18n_client.GetInfoRun.totalAlarmInfo1_n81i】" + 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】" + functionSb.toString();
+ }
+ }else if(alarmTimes == Contants.overAlarmValTimes && alarmState){//若原本的状态是告警状态,则判断是否连续几次达到告警值,次数会一直累积,知道监测恢复正常,所以如果一直异常,则也只告警一次连续4次异常
+ totalAlarmInfo = "【i18n_client.GetInfoRun.totalAlarmInfo3_n81i】" + 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 dataList = new LinkedList();
+ // 总数据
+ dataList.add(totalData);
+ // 详细信息
+ dataList.add(new String[]{"details", detailDataList.size() + ""});// details(解析标识),详细信息条数
+ dataList.addAll(detailDataList);// 具体的详细数据
+ // 特殊数据定制(目前是针对系统信息监测systeminfo)
+ if (detectInfo!=null && detectInfo.getRelatedDatas() != null) {
+ Set>> set = detectInfo.getRelatedDatas().entrySet();
+ for(Map.Entry> entry : set){
+ dataList.add(new String[]{entry.getKey(), entry.getValue().size() + ""});// 解析标识, 行数(当前解析标识指定的类型有多少条数据)
+ dataList.addAll(entry.getValue());// 多条数据
+ }
+ }
+
+ for(String[] val : dataList){
+// logger.debug(Arrays.toString(val));//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 getAlarmInfos() {
+ return alarmInfos;
+ }
+
+ public void setAlarmInfos(List alarmInfos) {
+ this.alarmInfos = alarmInfos;
+ }
+
+ public String getFirstPerformData() {
+ return firstPerformData;
+ }
+
+ public void setFirstPerformData(String firstPerformData) {
+ this.firstPerformData = firstPerformData;
+ }
+
+}
diff --git a/src/com/nis/systeminfo/thread/NewPluginResultMerge.java b/src/com/nis/systeminfo/thread/NewPluginResultMerge.java
new file mode 100644
index 0000000..900e26a
--- /dev/null
+++ b/src/com/nis/systeminfo/thread/NewPluginResultMerge.java
@@ -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 historyAlarmStateMap = new HashMap();
+ private static HashMap historyAlarmTimesMap = new HashMap();
+
+ 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 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 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] + ""); // 组织性能数据
+ } else {
+ if(StringUtils.isNotBlank(warninfos[3])) {
+ functionSb.append(warninfos[3] + ""); // 组织性能数据
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // 主动告警
+ 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】" + 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】" + functionSb.toString();
+ }
+ if(getHistoryAlarmTimes(setInfo.getId()) == Contants.overAlarmValTimes) {//若原本的状态是告警状态,则判断是否连续几次达到告警值,次数会一直累积,知道监测恢复正常,所以如果一直异常,则也只告警一次连续4次异常
+ totalAlarmInfo = "【i18n_client.NewPluginResultMerge.totalAlarmInfo3_n81i】" + 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 dataList = new LinkedList();
+ // 总数据
+ 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 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);
+ }
+}
diff --git a/src/com/nis/systeminfo/thread/PluginResultMerge.java b/src/com/nis/systeminfo/thread/PluginResultMerge.java
new file mode 100644
index 0000000..9d9d245
--- /dev/null
+++ b/src/com/nis/systeminfo/thread/PluginResultMerge.java
@@ -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 historyAlarmStateMap = new HashMap();
+ private static HashMap historyAlarmTimesMap = new HashMap();
+
+ public void merge() {
+ try {
+ logger.debug("合并临时结果文件开始 ~~~~~~~");
+
+ Collection 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 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() { // 同一类结果文件按按时间先后排序
+ 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 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] + ""); // 组织性能数据
+ } else {
+ if(StringUtils.isNotBlank(warninfos[3])) {
+ functionSb.append(warninfos[3] + ""); // 组织性能数据
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // 主动告警
+ 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】" + 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】" + functionSb.toString();
+ }
+ if(getHistoryAlarmTimes(setInfo.getId()) == Contants.overAlarmValTimes) {//若原本的状态是告警状态,则判断是否连续几次达到告警值,次数会一直累积,知道监测恢复正常,所以如果一直异常,则也只告警一次连续4次异常
+ totalAlarmInfo = "【i18n_client.NewPluginResultMerge.totalAlarmInfo3_n81i】" + 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 dataList = new LinkedList();
+ // 总数据
+ 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 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);
+ }
+}
diff --git a/src/de/innosystec/unrar/unpack/Unpack.java b/src/de/innosystec/unrar/unpack/Unpack.java
new file mode 100644
index 0000000..07555e0
--- /dev/null
+++ b/src/de/innosystec/unrar/unpack/Unpack.java
@@ -0,0 +1,1057 @@
+/*
+ * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
+ * Original author: Edmund Wagner
+ * Creation date: 31.05.2007
+ *
+ * Source: $HeadURL$
+ * Last changed: $LastChangedDate$
+ *
+ * the unrar licence applies to all junrar source and binary distributions
+ * you are not allowed to use this source to re-create the RAR compression algorithm
+ *
+ * Here some html entities which can be used for escaping javadoc tags:
+ * "&": "&" or "&"
+ * "<": "<" or "<"
+ * ">": ">" or ">"
+ * "@": "@"
+ */
+package de.innosystec.unrar.unpack;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Vector;
+
+import de.innosystec.unrar.exception.RarException;
+import de.innosystec.unrar.unpack.decode.Compress;
+import de.innosystec.unrar.unpack.ppm.BlockTypes;
+import de.innosystec.unrar.unpack.ppm.ModelPPM;
+import de.innosystec.unrar.unpack.ppm.SubAllocator;
+import de.innosystec.unrar.unpack.vm.BitInput;
+import de.innosystec.unrar.unpack.vm.RarVM;
+import de.innosystec.unrar.unpack.vm.VMPreparedProgram;
+
+/**
+ * DOCUMENT ME
+ *
+ * @author $LastChangedBy$
+ * @version $LastChangedRevision$
+ */
+public final class Unpack extends Unpack20 {
+
+ private final ModelPPM ppm = new ModelPPM();
+
+ private int ppmEscChar;
+
+ private RarVM rarVM = new RarVM();
+
+ /* Filters code, one entry per filter */
+ private List filters = new ArrayList();
+
+ /* Filters stack, several entrances of same filter are possible */
+ private List prgStack = new ArrayList();
+
+ /*
+ * lengths of preceding blocks, one length per filter. Used to reduce size
+ * required to write block length if lengths are repeating
+ */
+ private List oldFilterLengths = new ArrayList();
+
+ private int lastFilter;
+
+ private boolean tablesRead;
+
+ private byte[] unpOldTable = new byte[Compress.HUFF_TABLE_SIZE];
+
+ private BlockTypes unpBlockType;
+
+ private boolean externalWindow;
+
+ private long writtenFileSize;
+
+ private boolean fileExtracted;
+
+ private boolean ppmError;
+
+ private int prevLowDist;
+
+ private int lowDistRepCount;
+
+ public static int[] DBitLengthCounts = { 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 14, 0, 12 };
+
+ public Unpack(ComprDataIO DataIO) {
+ unpIO = DataIO;
+ window = null;
+ externalWindow = false;
+ suspended = false;
+ unpAllBuf = false;
+ unpSomeRead = false;
+ }
+
+ public void init(byte[] window) {
+ // 2012-11-13 RAR解压占用内存问题 修改源代码
+ if(this.window == null){
+ this.window = new byte[Compress.MAXWINSIZE];
+ }else{
+ externalWindow = true;
+ }
+ /*if (window == null) {
+ this.window = new byte[Compress.MAXWINSIZE];
+ } else {
+ this.window = window;
+ externalWindow = true;
+ }*/
+ inAddr = 0;
+ unpInitData(false);
+ }
+
+ public void doUnpack(int method, boolean solid) throws IOException,
+ RarException {
+ if (unpIO.getSubHeader().getUnpMethod() == 0x30) {
+ unstoreFile();
+ }
+ switch (method) {
+ case 15: // rar 1.5 compression
+ unpack15(solid);
+ break;
+ case 20: // rar 2.x compression
+ case 26: // files larger than 2GB
+ unpack20(solid);
+ break;
+ case 29: // rar 3.x compression
+ case 36: // alternative hash
+ unpack29(solid);
+ break;
+ }
+ }
+
+ private void unstoreFile() throws IOException, RarException {
+ byte[] buffer = new byte[0x10000];
+ while (true) {
+ int code = unpIO.unpRead(buffer, 0, (int) Math.min(buffer.length,
+ destUnpSize));
+ if (code == 0 || code == -1)
+ break;
+ code = code < destUnpSize ? code : (int) destUnpSize;
+ unpIO.unpWrite(buffer, 0, code);
+ if (destUnpSize >= 0)
+ destUnpSize -= code;
+ }
+
+ }
+
+ private void unpack29(boolean solid) throws IOException, RarException {
+
+ int[] DDecode = new int[Compress.DC];
+ byte[] DBits = new byte[Compress.DC];
+
+ int Bits;
+
+ if (DDecode[1] == 0) {
+ int Dist = 0, BitLength = 0, Slot = 0;
+ for (int I = 0; I < DBitLengthCounts.length; I++, BitLength++) {
+ int count = DBitLengthCounts[I];
+ for (int J = 0; J < count; J++, Slot++, Dist += (1 << BitLength)) {
+ DDecode[Slot] = Dist;
+ DBits[Slot] = (byte) BitLength;
+ }
+ }
+ }
+
+ fileExtracted = true;
+
+ if (!suspended) {
+ unpInitData(solid);
+ if (!unpReadBuf()) {
+ return;
+ }
+ if ((!solid || !tablesRead) && !readTables()) {
+ return;
+ }
+ }
+
+ if (ppmError) {
+ return;
+ }
+
+ while (true) {
+ unpPtr &= Compress.MAXWINMASK;
+
+ if (inAddr > readBorder) {
+ if (!unpReadBuf()) {
+ break;
+ }
+ }
+ // System.out.println(((wrPtr - unpPtr) &
+ // Compress.MAXWINMASK)+":"+wrPtr+":"+unpPtr);
+ if (((wrPtr - unpPtr) & Compress.MAXWINMASK) < 260
+ && wrPtr != unpPtr) {
+
+ UnpWriteBuf();
+ if (writtenFileSize > destUnpSize) {
+ return;
+ }
+ if (suspended) {
+ fileExtracted = false;
+ return;
+ }
+ }
+ if (unpBlockType == BlockTypes.BLOCK_PPM) {
+ int Ch = ppm.decodeChar();
+ if (Ch == -1) {
+ ppmError = true;
+ break;
+ }
+ if (Ch == ppmEscChar) {
+ int NextCh = ppm.decodeChar();
+ if (NextCh == 0) {
+ if (!readTables()) {
+ break;
+ }
+ continue;
+ }
+ if (NextCh == 2 || NextCh == -1) {
+ break;
+ }
+ if (NextCh == 3) {
+ if (!readVMCodePPM()) {
+ break;
+ }
+ continue;
+ }
+ if (NextCh == 4) {
+ int Distance = 0, Length = 0;
+ boolean failed = false;
+ for (int I = 0; I < 4 && !failed; I++) {
+ int ch = ppm.decodeChar();
+ if (ch == -1) {
+ failed = true;
+ } else {
+ if (I == 3) {
+ // Bug fixed
+ Length = ch & 0xff;
+ } else {
+ // Bug fixed
+ Distance = (Distance << 8) + (ch & 0xff);
+ }
+ }
+ }
+ if (failed) {
+ break;
+ }
+ copyString(Length + 32, Distance + 2);
+ continue;
+ }
+ if (NextCh == 5) {
+ int Length = ppm.decodeChar();
+ if (Length == -1) {
+ break;
+ }
+ copyString(Length + 4, 1);
+ continue;
+ }
+ }
+ window[unpPtr++] = (byte) Ch;
+ continue;
+ }
+
+ int Number = decodeNumber(LD);
+ if (Number < 256) {
+ window[unpPtr++] = (byte) Number;
+ continue;
+ }
+ if (Number >= 271) {
+ int Length = LDecode[Number -= 271] + 3;
+ if ((Bits = LBits[Number]) > 0) {
+ Length += getbits() >>> (16 - Bits);
+ addbits(Bits);
+ }
+
+ int DistNumber = decodeNumber(DD);
+ int Distance = DDecode[DistNumber] + 1;
+ if ((Bits = DBits[DistNumber]) > 0) {
+ if (DistNumber > 9) {
+ if (Bits > 4) {
+ Distance += ((getbits() >>> (20 - Bits)) << 4);
+ addbits(Bits - 4);
+ }
+ if (lowDistRepCount > 0) {
+ lowDistRepCount--;
+ Distance += prevLowDist;
+ } else {
+ int LowDist = decodeNumber(LDD);
+ if (LowDist == 16) {
+ lowDistRepCount = Compress.LOW_DIST_REP_COUNT - 1;
+ Distance += prevLowDist;
+ } else {
+ Distance += LowDist;
+ prevLowDist = LowDist;
+ }
+ }
+ } else {
+ Distance += getbits() >>> (16 - Bits);
+ addbits(Bits);
+ }
+ }
+
+ if (Distance >= 0x2000) {
+ Length++;
+ if (Distance >= 0x40000L) {
+ Length++;
+ }
+ }
+
+ insertOldDist(Distance);
+ insertLastMatch(Length, Distance);
+
+ copyString(Length, Distance);
+ continue;
+ }
+ if (Number == 256) {
+ if (!readEndOfBlock()) {
+ break;
+ }
+ continue;
+ }
+ if (Number == 257) {
+ if (!readVMCode()) {
+ break;
+ }
+ continue;
+ }
+ if (Number == 258) {
+ if (lastLength != 0) {
+ copyString(lastLength, lastDist);
+ }
+ continue;
+ }
+ if (Number < 263) {
+ int DistNum = Number - 259;
+ int Distance = oldDist[DistNum];
+ for (int I = DistNum; I > 0; I--) {
+ oldDist[I] = oldDist[I - 1];
+ }
+ oldDist[0] = Distance;
+
+ int LengthNumber = decodeNumber(RD);
+ int Length = LDecode[LengthNumber] + 2;
+ if ((Bits = LBits[LengthNumber]) > 0) {
+ Length += getbits() >>> (16 - Bits);
+ addbits(Bits);
+ }
+ insertLastMatch(Length, Distance);
+ copyString(Length, Distance);
+ continue;
+ }
+ if (Number < 272) {
+ int Distance = SDDecode[Number -= 263] + 1;
+ if ((Bits = SDBits[Number]) > 0) {
+ Distance += getbits() >>> (16 - Bits);
+ addbits(Bits);
+ }
+ insertOldDist(Distance);
+ insertLastMatch(2, Distance);
+ copyString(2, Distance);
+ continue;
+ }
+ }
+ UnpWriteBuf();
+
+ }
+
+ private void UnpWriteBuf() throws IOException {
+ int WrittenBorder = wrPtr;
+ int WriteSize = (unpPtr - WrittenBorder) & Compress.MAXWINMASK;
+ for (int I = 0; I < prgStack.size(); I++) {
+ UnpackFilter flt = prgStack.get(I);
+ if (flt == null) {
+ continue;
+ }
+ if (flt.isNextWindow()) {
+ flt.setNextWindow(false);// ->NextWindow=false;
+ continue;
+ }
+ int BlockStart = flt.getBlockStart();// ->BlockStart;
+ int BlockLength = flt.getBlockLength();// ->BlockLength;
+ if (((BlockStart - WrittenBorder) & Compress.MAXWINMASK) < WriteSize) {
+ if (WrittenBorder != BlockStart) {
+ UnpWriteArea(WrittenBorder, BlockStart);
+ WrittenBorder = BlockStart;
+ WriteSize = (unpPtr - WrittenBorder) & Compress.MAXWINMASK;
+ }
+ if (BlockLength <= WriteSize) {
+ int BlockEnd = (BlockStart + BlockLength)
+ & Compress.MAXWINMASK;
+ if (BlockStart < BlockEnd || BlockEnd == 0) {
+ // VM.SetMemory(0,Window+BlockStart,BlockLength);
+ rarVM.setMemory(0, window, BlockStart, BlockLength);
+ } else {
+ int FirstPartLength = Compress.MAXWINSIZE - BlockStart;
+ // VM.SetMemory(0,Window+BlockStart,FirstPartLength);
+ rarVM.setMemory(0, window, BlockStart, FirstPartLength);
+ // VM.SetMemory(FirstPartLength,Window,BlockEnd);
+ rarVM.setMemory(FirstPartLength, window, 0, BlockEnd);
+
+ }
+
+ VMPreparedProgram ParentPrg = filters.get(
+ flt.getParentFilter()).getPrg();
+ VMPreparedProgram Prg = flt.getPrg();
+
+ if (ParentPrg.getGlobalData().size() > RarVM.VM_FIXEDGLOBALSIZE) {
+ // copy global data from previous script execution if
+ // any
+ // Prg->GlobalData.Alloc(ParentPrg->GlobalData.Size());
+ // memcpy(&Prg->GlobalData[VM_FIXEDGLOBALSIZE],&ParentPrg->GlobalData[VM_FIXEDGLOBALSIZE],ParentPrg->GlobalData.Size()-VM_FIXEDGLOBALSIZE);
+ Prg.getGlobalData().setSize(
+ ParentPrg.getGlobalData().size());
+ for (int i = 0; i < ParentPrg.getGlobalData().size()
+ - RarVM.VM_FIXEDGLOBALSIZE; i++) {
+ Prg.getGlobalData().set(
+ RarVM.VM_FIXEDGLOBALSIZE + i,
+ ParentPrg.getGlobalData().get(
+ RarVM.VM_FIXEDGLOBALSIZE + i));
+ }
+ }
+
+ ExecuteCode(Prg);
+
+ if (Prg.getGlobalData().size() > RarVM.VM_FIXEDGLOBALSIZE) {
+ // save global data for next script execution
+ if (ParentPrg.getGlobalData().size() < Prg
+ .getGlobalData().size()) {
+ ParentPrg.getGlobalData().setSize(
+ Prg.getGlobalData().size());// ->GlobalData.Alloc(Prg->GlobalData.Size());
+ }
+ // memcpy(&ParentPrg->GlobalData[VM_FIXEDGLOBALSIZE],&Prg->GlobalData[VM_FIXEDGLOBALSIZE],Prg->GlobalData.Size()-VM_FIXEDGLOBALSIZE);
+ for (int i = 0; i < Prg.getGlobalData().size()
+ - RarVM.VM_FIXEDGLOBALSIZE; i++) {
+ ParentPrg.getGlobalData().set(
+ RarVM.VM_FIXEDGLOBALSIZE + i,
+ Prg.getGlobalData().get(
+ RarVM.VM_FIXEDGLOBALSIZE + i));
+ }
+ } else {
+ ParentPrg.getGlobalData().clear();
+ }
+
+ int FilteredDataOffset = Prg.getFilteredDataOffset();
+ int FilteredDataSize = Prg.getFilteredDataSize();
+ byte[] FilteredData = new byte[FilteredDataSize];
+
+ for (int i = 0; i < FilteredDataSize; i++) {
+ FilteredData[i] = rarVM.getMem()[FilteredDataOffset + i];// Prg.getGlobalData().get(FilteredDataOffset
+ // +
+ // i);
+ }
+
+ prgStack.set(I, null);
+ while (I + 1 < prgStack.size()) {
+ UnpackFilter NextFilter = prgStack.get(I + 1);
+ if (NextFilter == null
+ || NextFilter.getBlockStart() != BlockStart
+ || NextFilter.getBlockLength() != FilteredDataSize
+ || NextFilter.isNextWindow()) {
+ break;
+ }
+ // apply several filters to same data block
+
+ rarVM.setMemory(0, FilteredData, 0, FilteredDataSize);// .SetMemory(0,FilteredData,FilteredDataSize);
+
+ VMPreparedProgram pPrg = filters.get(
+ NextFilter.getParentFilter()).getPrg();
+ VMPreparedProgram NextPrg = NextFilter.getPrg();
+
+ if (pPrg.getGlobalData().size() > RarVM.VM_FIXEDGLOBALSIZE) {
+ // copy global data from previous script execution
+ // if any
+ // NextPrg->GlobalData.Alloc(ParentPrg->GlobalData.Size());
+ NextPrg.getGlobalData().setSize(
+ pPrg.getGlobalData().size());
+ // memcpy(&NextPrg->GlobalData[VM_FIXEDGLOBALSIZE],&ParentPrg->GlobalData[VM_FIXEDGLOBALSIZE],ParentPrg->GlobalData.Size()-VM_FIXEDGLOBALSIZE);
+ for (int i = 0; i < pPrg.getGlobalData().size()
+ - RarVM.VM_FIXEDGLOBALSIZE; i++) {
+ NextPrg.getGlobalData().set(
+ RarVM.VM_FIXEDGLOBALSIZE + i,
+ pPrg.getGlobalData().get(
+ RarVM.VM_FIXEDGLOBALSIZE + i));
+ }
+ }
+
+ ExecuteCode(NextPrg);
+
+ if (NextPrg.getGlobalData().size() > RarVM.VM_FIXEDGLOBALSIZE) {
+ // save global data for next script execution
+ if (pPrg.getGlobalData().size() < NextPrg
+ .getGlobalData().size()) {
+ pPrg.getGlobalData().setSize(
+ NextPrg.getGlobalData().size());
+ }
+ // memcpy(&ParentPrg->GlobalData[VM_FIXEDGLOBALSIZE],&NextPrg->GlobalData[VM_FIXEDGLOBALSIZE],NextPrg->GlobalData.Size()-VM_FIXEDGLOBALSIZE);
+ for (int i = 0; i < NextPrg.getGlobalData().size()
+ - RarVM.VM_FIXEDGLOBALSIZE; i++) {
+ pPrg.getGlobalData().set(
+ RarVM.VM_FIXEDGLOBALSIZE + i,
+ NextPrg.getGlobalData().get(
+ RarVM.VM_FIXEDGLOBALSIZE + i));
+ }
+ } else {
+ pPrg.getGlobalData().clear();
+ }
+ FilteredDataOffset = NextPrg.getFilteredDataOffset();
+ FilteredDataSize = NextPrg.getFilteredDataSize();
+
+ FilteredData = new byte[FilteredDataSize];
+ for (int i = 0; i < FilteredDataSize; i++) {
+ FilteredData[i] = NextPrg.getGlobalData().get(
+ FilteredDataOffset + i);
+ }
+
+ I++;
+ prgStack.set(I, null);
+ }
+ unpIO.unpWrite(FilteredData, 0, FilteredDataSize);
+ unpSomeRead = true;
+ writtenFileSize += FilteredDataSize;
+ WrittenBorder = BlockEnd;
+ WriteSize = (unpPtr - WrittenBorder) & Compress.MAXWINMASK;
+ } else {
+ for (int J = I; J < prgStack.size(); J++) {
+ UnpackFilter filt = prgStack.get(J);
+ if (filt != null && filt.isNextWindow()) {
+ filt.setNextWindow(false);
+ }
+ }
+ wrPtr = WrittenBorder;
+ return;
+ }
+ }
+ }
+
+ UnpWriteArea(WrittenBorder, unpPtr);
+ wrPtr = unpPtr;
+
+ }
+
+ private void UnpWriteArea(int startPtr, int endPtr) throws IOException {
+ if (endPtr != startPtr) {
+ unpSomeRead = true;
+ }
+ if (endPtr < startPtr) {
+ UnpWriteData(window, startPtr, -startPtr & Compress.MAXWINMASK);
+ UnpWriteData(window, 0, endPtr);
+ unpAllBuf = true;
+ } else {
+ UnpWriteData(window, startPtr, endPtr - startPtr);
+ }
+ }
+
+ private void UnpWriteData(byte[] data, int offset, int size)
+ throws IOException {
+ if (writtenFileSize >= destUnpSize) {
+ return;
+ }
+ int writeSize = size;
+ long leftToWrite = destUnpSize - writtenFileSize;
+ if (writeSize > leftToWrite) {
+ writeSize = (int) leftToWrite;
+ }
+ unpIO.unpWrite(data, offset, writeSize);
+
+ writtenFileSize += size;
+
+ }
+
+ private void insertOldDist(int distance) {
+ oldDist[3] = oldDist[2];
+ oldDist[2] = oldDist[1];
+ oldDist[1] = oldDist[0];
+ oldDist[0] = distance;
+ }
+
+ private void insertLastMatch(int length, int distance) {
+ lastDist = distance;
+ lastLength = length;
+ }
+
+ private void copyString(int length, int distance) {
+ // System.out.println("copyString(" + length + ", " + distance + ")");
+
+ int destPtr = unpPtr - distance;
+ // System.out.println(unpPtr+":"+distance);
+ if (destPtr >= 0 && destPtr < Compress.MAXWINSIZE - 260
+ && unpPtr < Compress.MAXWINSIZE - 260) {
+
+ window[unpPtr++] = window[destPtr++];
+
+ while (--length > 0)
+
+ window[unpPtr++] = window[destPtr++];
+ } else
+ while (length-- != 0) {
+ window[unpPtr] = window[destPtr++ & Compress.MAXWINMASK];
+ unpPtr = (unpPtr + 1) & Compress.MAXWINMASK;
+ }
+ }
+
+ protected void unpInitData(boolean solid) {
+ if (!solid) {
+ tablesRead = false;
+ Arrays.fill(oldDist, 0); // memset(oldDist,0,sizeof(OldDist));
+
+ oldDistPtr = 0;
+ lastDist = 0;
+ lastLength = 0;
+
+ Arrays.fill(unpOldTable, (byte) 0);// memset(UnpOldTable,0,sizeof(UnpOldTable));
+
+ unpPtr = 0;
+ wrPtr = 0;
+ ppmEscChar = 2;
+
+ initFilters();
+ }
+ InitBitInput();
+ ppmError = false;
+ writtenFileSize = 0;
+ readTop = 0;
+ readBorder = 0;
+ unpInitData20(solid);
+ }
+
+ private void initFilters() {
+ oldFilterLengths.clear();
+ lastFilter = 0;
+
+ filters.clear();
+
+ prgStack.clear();
+ }
+
+ private boolean readEndOfBlock() throws IOException, RarException {
+ int BitField = getbits();
+ boolean NewTable, NewFile = false;
+ if ((BitField & 0x8000) != 0) {
+ NewTable = true;
+ addbits(1);
+ } else {
+ NewFile = true;
+ NewTable = (BitField & 0x4000) != 0 ? true : false;
+ addbits(2);
+ }
+ tablesRead = !NewTable;
+ return !(NewFile || NewTable && !readTables());
+ }
+
+ private boolean readTables() throws IOException, RarException {
+ byte[] bitLength = new byte[Compress.BC];
+
+ byte[] table = new byte[Compress.HUFF_TABLE_SIZE];
+ if (inAddr > readTop - 25) {
+ if (!unpReadBuf()) {
+ return (false);
+ }
+ }
+ faddbits((8 - inBit) & 7);
+ long bitField = fgetbits() & 0xffFFffFF;
+ if ((bitField & 0x8000) != 0) {
+ unpBlockType = BlockTypes.BLOCK_PPM;
+ return (ppm.decodeInit(this, ppmEscChar));
+ }
+ unpBlockType = BlockTypes.BLOCK_LZ;
+
+ prevLowDist = 0;
+ lowDistRepCount = 0;
+
+ if ((bitField & 0x4000) == 0) {
+ Arrays.fill(unpOldTable, (byte) 0);// memset(UnpOldTable,0,sizeof(UnpOldTable));
+ }
+ faddbits(2);
+
+ for (int i = 0; i < Compress.BC; i++) {
+ int length = (fgetbits() >>> 12) & 0xFF;
+ faddbits(4);
+ if (length == 15) {
+ int zeroCount = (fgetbits() >>> 12) & 0xFF;
+ faddbits(4);
+ if (zeroCount == 0) {
+ bitLength[i] = 15;
+ } else {
+ zeroCount += 2;
+ while (zeroCount-- > 0 && i < bitLength.length) {
+ bitLength[i++] = 0;
+ }
+ i--;
+ }
+ } else {
+ bitLength[i] = (byte) length;
+ }
+ }
+
+ makeDecodeTables(bitLength, 0, BD, Compress.BC);
+
+ int TableSize = Compress.HUFF_TABLE_SIZE;
+
+ for (int i = 0; i < TableSize;) {
+ if (inAddr > readTop - 5) {
+ if (!unpReadBuf()) {
+ return (false);
+ }
+ }
+ int Number = decodeNumber(BD);
+ if (Number < 16) {
+ table[i] = (byte) ((Number + unpOldTable[i]) & 0xf);
+ i++;
+ } else if (Number < 18) {
+ int N;
+ if (Number == 16) {
+ N = (fgetbits() >>> 13) + 3;
+ faddbits(3);
+ } else {
+ N = (fgetbits() >>> 9) + 11;
+ faddbits(7);
+ }
+ while (N-- > 0 && i < TableSize) {
+ table[i] = table[i - 1];
+ i++;
+ }
+ } else {
+ int N;
+ if (Number == 18) {
+ N = (fgetbits() >>> 13) + 3;
+ faddbits(3);
+ } else {
+ N = (fgetbits() >>> 9) + 11;
+ faddbits(7);
+ }
+ while (N-- > 0 && i < TableSize) {
+ table[i++] = 0;
+ }
+ }
+ }
+ tablesRead = true;
+ if (inAddr > readTop) {
+ return (false);
+ }
+ makeDecodeTables(table, 0, LD, Compress.NC);
+ makeDecodeTables(table, Compress.NC, DD, Compress.DC);
+ makeDecodeTables(table, Compress.NC + Compress.DC, LDD, Compress.LDC);
+ makeDecodeTables(table, Compress.NC + Compress.DC + Compress.LDC, RD,
+ Compress.RC);
+
+ // memcpy(unpOldTable,table,sizeof(unpOldTable));
+ for (int i = 0; i < unpOldTable.length; i++) {
+ unpOldTable[i] = table[i];
+ }
+ return (true);
+
+ }
+
+ private boolean readVMCode() throws IOException, RarException {
+ int FirstByte = getbits() >> 8;
+ addbits(8);
+ int Length = (FirstByte & 7) + 1;
+ if (Length == 7) {
+ Length = (getbits() >> 8) + 7;
+ addbits(8);
+ } else if (Length == 8) {
+ Length = getbits();
+ addbits(16);
+ }
+ List vmCode = new ArrayList();
+ for (int I = 0; I < Length; I++) {
+ if (inAddr >= readTop - 1 && !unpReadBuf() && I < Length - 1) {
+ return (false);
+ }
+ vmCode.add(Byte.valueOf((byte) (getbits() >> 8)));
+ addbits(8);
+ }
+ return (addVMCode(FirstByte, vmCode, Length));
+ }
+
+ private boolean readVMCodePPM() throws IOException, RarException {
+ int FirstByte = ppm.decodeChar();
+ if ((int) FirstByte == -1) {
+ return (false);
+ }
+ int Length = (FirstByte & 7) + 1;
+ if (Length == 7) {
+ int B1 = ppm.decodeChar();
+ if (B1 == -1) {
+ return (false);
+ }
+ Length = B1 + 7;
+ } else if (Length == 8) {
+ int B1 = ppm.decodeChar();
+ if (B1 == -1) {
+ return (false);
+ }
+ int B2 = ppm.decodeChar();
+ if (B2 == -1) {
+ return (false);
+ }
+ Length = B1 * 256 + B2;
+ }
+ List vmCode = new ArrayList();
+ for (int I = 0; I < Length; I++) {
+ int Ch = ppm.decodeChar();
+ if (Ch == -1) {
+ return (false);
+ }
+ vmCode.add(Byte.valueOf((byte) Ch));// VMCode[I]=Ch;
+ }
+ return (addVMCode(FirstByte, vmCode, Length));
+ }
+
+ private boolean addVMCode(int firstByte, List vmCode, int length) {
+ BitInput Inp = new BitInput();
+ Inp.InitBitInput();
+ // memcpy(Inp.InBuf,Code,Min(BitInput::MAX_SIZE,CodeSize));
+ for (int i = 0; i < Math.min(BitInput.MAX_SIZE, vmCode.size()); i++) {
+ Inp.getInBuf()[i] = vmCode.get(i);
+ }
+ rarVM.init();
+
+ int FiltPos;
+ if ((firstByte & 0x80) != 0) {
+ FiltPos = RarVM.ReadData(Inp);
+ if (FiltPos == 0) {
+ initFilters();
+ } else {
+ FiltPos--;
+ }
+ } else
+ FiltPos = lastFilter; // use the same filter as last time
+
+ if (FiltPos > filters.size() || FiltPos > oldFilterLengths.size()) {
+ return (false);
+ }
+ lastFilter = FiltPos;
+ boolean NewFilter = (FiltPos == filters.size());
+
+ UnpackFilter StackFilter = new UnpackFilter(); // new filter for
+ // PrgStack
+
+ UnpackFilter Filter;
+ if (NewFilter) // new filter code, never used before since VM reset
+ {
+ // too many different filters, corrupt archive
+ if (FiltPos > 1024) {
+ return (false);
+ }
+
+ // Filters[Filters.Size()-1]=Filter=new UnpackFilter;
+ Filter = new UnpackFilter();
+ filters.add(Filter);
+ StackFilter.setParentFilter(filters.size() - 1);
+ oldFilterLengths.add(0);
+ Filter.setExecCount(0);
+ } else // filter was used in the past
+ {
+ Filter = filters.get(FiltPos);
+ StackFilter.setParentFilter(FiltPos);
+ Filter.setExecCount(Filter.getExecCount() + 1);// ->ExecCount++;
+ }
+
+ prgStack.add(StackFilter);
+ StackFilter.setExecCount(Filter.getExecCount());// ->ExecCount;
+
+ int BlockStart = RarVM.ReadData(Inp);
+ if ((firstByte & 0x40) != 0) {
+ BlockStart += 258;
+ }
+ StackFilter.setBlockStart((BlockStart + unpPtr) & Compress.MAXWINMASK);
+ if ((firstByte & 0x20) != 0) {
+ StackFilter.setBlockLength(RarVM.ReadData(Inp));
+ } else {
+ StackFilter
+ .setBlockLength(FiltPos < oldFilterLengths.size() ? oldFilterLengths
+ .get(FiltPos)
+ : 0);
+ }
+ StackFilter.setNextWindow((wrPtr != unpPtr)
+ && ((wrPtr - unpPtr) & Compress.MAXWINMASK) <= BlockStart);
+
+ // DebugLog("\nNextWindow: UnpPtr=%08x WrPtr=%08x
+ // BlockStart=%08x",UnpPtr,WrPtr,BlockStart);
+
+ oldFilterLengths.set(FiltPos, StackFilter.getBlockLength());
+
+ // memset(StackFilter->Prg.InitR,0,sizeof(StackFilter->Prg.InitR));
+ Arrays.fill(StackFilter.getPrg().getInitR(), 0);
+ StackFilter.getPrg().getInitR()[3] = RarVM.VM_GLOBALMEMADDR;// StackFilter->Prg.InitR[3]=VM_GLOBALMEMADDR;
+ StackFilter.getPrg().getInitR()[4] = StackFilter.getBlockLength();// StackFilter->Prg.InitR[4]=StackFilter->BlockLength;
+ StackFilter.getPrg().getInitR()[5] = StackFilter.getExecCount();// StackFilter->Prg.InitR[5]=StackFilter->ExecCount;
+
+ if ((firstByte & 0x10) != 0) // set registers to optional parameters
+ // if any
+ {
+ int InitMask = Inp.fgetbits() >>> 9;
+ Inp.faddbits(7);
+ for (int I = 0; I < 7; I++) {
+ if ((InitMask & (1 << I)) != 0) {
+ // StackFilter->Prg.InitR[I]=RarVM::ReadData(Inp);
+ StackFilter.getPrg().getInitR()[I] = RarVM.ReadData(Inp);
+ }
+ }
+ }
+
+ if (NewFilter) {
+ int VMCodeSize = RarVM.ReadData(Inp);
+ if (VMCodeSize >= 0x10000 || VMCodeSize == 0) {
+ return (false);
+ }
+ byte[] VMCode = new byte[VMCodeSize];
+ for (int I = 0; I < VMCodeSize; I++) {
+ if (Inp.Overflow(3)) {
+ return (false);
+ }
+ VMCode[I] = (byte) (Inp.fgetbits() >> 8);
+ Inp.faddbits(8);
+ }
+ // VM.Prepare(&VMCode[0],VMCodeSize,&Filter->Prg);
+ rarVM.prepare(VMCode, VMCodeSize, Filter.getPrg());
+ }
+ StackFilter.getPrg().setAltCmd(Filter.getPrg().getCmd());// StackFilter->Prg.AltCmd=&Filter->Prg.Cmd[0];
+ StackFilter.getPrg().setCmdCount(Filter.getPrg().getCmdCount());// StackFilter->Prg.CmdCount=Filter->Prg.CmdCount;
+
+ int StaticDataSize = Filter.getPrg().getStaticData().size();
+ if (StaticDataSize > 0 && StaticDataSize < RarVM.VM_GLOBALMEMSIZE) {
+ // read statically defined data contained in DB commands
+ // StackFilter->Prg.StaticData.Add(StaticDataSize);
+ StackFilter.getPrg().setStaticData(Filter.getPrg().getStaticData());
+ // memcpy(&StackFilter->Prg.StaticData[0],&Filter->Prg.StaticData[0],StaticDataSize);
+ }
+
+ if (StackFilter.getPrg().getGlobalData().size() < RarVM.VM_FIXEDGLOBALSIZE) {
+ // StackFilter->Prg.GlobalData.Reset();
+ // StackFilter->Prg.GlobalData.Add(VM_FIXEDGLOBALSIZE);
+ StackFilter.getPrg().getGlobalData().clear();
+ StackFilter.getPrg().getGlobalData().setSize(
+ RarVM.VM_FIXEDGLOBALSIZE);
+ }
+
+ // byte *GlobalData=&StackFilter->Prg.GlobalData[0];
+ Vector globalData = StackFilter.getPrg().getGlobalData();
+ for (int I = 0; I < 7; I++) {
+ rarVM.setLowEndianValue(globalData, I * 4, StackFilter.getPrg()
+ .getInitR()[I]);
+ }
+
+ // VM.SetLowEndianValue((uint
+ // *)&GlobalData[0x1c],StackFilter->BlockLength);
+ rarVM.setLowEndianValue(globalData, 0x1c, StackFilter.getBlockLength());
+ // VM.SetLowEndianValue((uint *)&GlobalData[0x20],0);
+ rarVM.setLowEndianValue(globalData, 0x20, 0);
+ rarVM.setLowEndianValue(globalData, 0x24, 0);
+ rarVM.setLowEndianValue(globalData, 0x28, 0);
+
+ // VM.SetLowEndianValue((uint
+ // *)&GlobalData[0x2c],StackFilter->ExecCount);
+ rarVM.setLowEndianValue(globalData, 0x2c, StackFilter.getExecCount());
+ // memset(&GlobalData[0x30],0,16);
+ for (int i = 0; i < 16; i++) {
+ globalData.set(0x30 + i, Byte.valueOf((byte) (0)));
+ }
+ if ((firstByte & 8) != 0) // put data block passed as parameter if any
+ {
+ if (Inp.Overflow(3)) {
+ return (false);
+ }
+ int DataSize = RarVM.ReadData(Inp);
+ if (DataSize > RarVM.VM_GLOBALMEMSIZE - RarVM.VM_FIXEDGLOBALSIZE) {
+ return (false);
+ }
+ int CurSize = StackFilter.getPrg().getGlobalData().size();
+ if (CurSize < DataSize + RarVM.VM_FIXEDGLOBALSIZE) {
+ // StackFilter->Prg.GlobalData.Add(DataSize+VM_FIXEDGLOBALSIZE-CurSize);
+ StackFilter.getPrg().getGlobalData().setSize(
+ DataSize + RarVM.VM_FIXEDGLOBALSIZE - CurSize);
+ }
+ int offset = RarVM.VM_FIXEDGLOBALSIZE;
+ globalData = StackFilter.getPrg().getGlobalData();
+ for (int I = 0; I < DataSize; I++) {
+ if (Inp.Overflow(3)) {
+ return (false);
+ }
+ globalData.set(offset + I, Byte
+ .valueOf((byte) (Inp.fgetbits() >>> 8)));
+ Inp.faddbits(8);
+ }
+ }
+ return (true);
+ }
+
+ private void ExecuteCode(VMPreparedProgram Prg) {
+ if (Prg.getGlobalData().size() > 0) {
+ // Prg->InitR[6]=int64to32(WrittenFileSize);
+ Prg.getInitR()[6] = (int) (writtenFileSize);
+ // rarVM.SetLowEndianValue((uint
+ // *)&Prg->GlobalData[0x24],int64to32(WrittenFileSize));
+ rarVM.setLowEndianValue(Prg.getGlobalData(), 0x24,
+ (int) writtenFileSize);
+ // rarVM.SetLowEndianValue((uint
+ // *)&Prg->GlobalData[0x28],int64to32(WrittenFileSize>>32));
+ rarVM.setLowEndianValue(Prg.getGlobalData(), 0x28,
+ (int) (writtenFileSize >>> 32));
+ rarVM.execute(Prg);
+ }
+ }
+
+ // Duplicate method
+ // private boolean ReadEndOfBlock() throws IOException, RarException
+ // {
+ // int BitField = getbits();
+ // boolean NewTable, NewFile = false;
+ // if ((BitField & 0x8000) != 0) {
+ // NewTable = true;
+ // addbits(1);
+ // } else {
+ // NewFile = true;
+ // NewTable = (BitField & 0x4000) != 0;
+ // addbits(2);
+ // }
+ // tablesRead = !NewTable;
+ // return !(NewFile || NewTable && !readTables());
+ // }
+
+ public boolean isFileExtracted() {
+ return fileExtracted;
+ }
+
+ public void setDestSize(long destSize) {
+ this.destUnpSize = destSize;
+ this.fileExtracted = false;
+ }
+
+ public void setSuspended(boolean suspended) {
+ this.suspended = suspended;
+ }
+
+ public int getChar() throws IOException, RarException {
+ if (inAddr > BitInput.MAX_SIZE - 30) {
+ unpReadBuf();
+ }
+ return (inBuf[inAddr++] & 0xff);
+ }
+
+ public int getPpmEscChar() {
+ return ppmEscChar;
+ }
+
+ public void setPpmEscChar(int ppmEscChar) {
+ this.ppmEscChar = ppmEscChar;
+ }
+
+ public void cleanUp() {
+ if (ppm != null) {
+ SubAllocator allocator = ppm.getSubAlloc();
+ if (allocator != null) {
+ allocator.stopSubAllocator();
+ }
+ }
+ System.out.println(this.window.length);
+ }
+}
diff --git a/src/key/bat/create_ks_cert.bat b/src/key/bat/create_ks_cert.bat
new file mode 100644
index 0000000..dd3f144
--- /dev/null
+++ b/src/key/bat/create_ks_cert.bat
@@ -0,0 +1,24 @@
+@echo off
+set aliasName=clientkey
+set storePath=./client_ks
+set local=192.168.10.100
+set keyPass=123123
+set certName=./client_key.cer
+if not "%1"=="" (
+ set aliasName=%1
+)
+if not "%2"=="" (
+ set storePath=%2
+)
+if not "%3"=="" (
+ set local=%3
+)
+if not "%4"=="" (
+ set keyPass=%4
+)
+if not "%5"=="" (
+ set certName=%5
+)
+@echo %aliasName% %storePath% %local% %keyPass% %certName%
+keytool -genkey -v -alias %aliasName% -keyalg RSA -storetype jceks -keystore %storePath% -validity 90 -dname "CN=%local%,OU=cn,O=cn,L=cn,ST=cn,C=cn" -storepass client -keypass %keyPass%
+keytool -export -alias %aliasName% -storetype jceks -keystore %storePath% -file %certName% -storepass client
diff --git a/src/key/bat/import_trust_cert.bat b/src/key/bat/import_trust_cert.bat
new file mode 100644
index 0000000..35c4ee8
--- /dev/null
+++ b/src/key/bat/import_trust_cert.bat
@@ -0,0 +1,18 @@
+@echo off
+@echo Կƣ%1 Կ·ƣ%2 ֤·ƣ%3
+set aliasName=clientkey
+set storePath=./client_ks
+set local=localhost
+set certName=./client_key.cer
+if not "%1"=="" (
+ set aliasName=%1
+)
+if not "%2"=="" (
+ set storePath=%2
+)
+if not "%3"=="" (
+ set certName=%3
+)
+
+@echo %aliasName% %storePath% %certName%
+echo y|keytool -import -v -trustcacerts -alias %aliasName% -keystore %storePath% -file %certName% -storetype jceks -storepass client
diff --git a/src/key/cer/client_ks b/src/key/cer/client_ks
new file mode 100644
index 0000000..79459cc
Binary files /dev/null and b/src/key/cer/client_ks differ
diff --git a/src/key/cer/client_ts b/src/key/cer/client_ts
new file mode 100644
index 0000000..312ecd6
Binary files /dev/null and b/src/key/cer/client_ts differ
diff --git a/src/log4j.properties b/src/log4j.properties
new file mode 100644
index 0000000..3bffcbc
--- /dev/null
+++ b/src/log4j.properties
@@ -0,0 +1,26 @@
+log4j.rootLogger = debug,stdout,logfile
+
+log4j.appender.stdout = org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout.ConversionPattern = %d %p [%l] [%t] - <%m>%n
+log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
+
+#----------------------debug---------------------
+
+log4j.appender.logfile = org.apache.log4j.RollingFileAppender
+log4j.appender.logfile.MaxFileSize = 50MB
+log4j.appender.logfile.MaxBackupIndex = 20
+
+log4j.appender.logfile.File = ../nc_logs/nmsclient.log
+log4j.appender.logfile.layout.ConversionPattern = %d %p [%l] [%t] - <%m>%n
+log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
+
+#----------------------error---------------------
+log4j.logger.errorLog = warn,errorLog
+log4j.appender.errorLog = org.apache.log4j.RollingFileAppender
+log4j.appender.errorLog.MaxFileSize = 50MB
+log4j.appender.errorLog.Append = true
+log4j.appender.errorLog.Threshold = warn
+log4j.appender.errorLog.MaxBackupIndex = 10
+log4j.appender.errorLog.File = ../nc_logs/error.log
+log4j.appender.errorLog.layout.ConversionPattern = %d %p [%l] [%t] - <%m>%n
+log4j.appender.errorLog.layout = org.apache.log4j.PatternLayout
diff --git a/src/myconfig.properties b/src/myconfig.properties
new file mode 100644
index 0000000..856938b
--- /dev/null
+++ b/src/myconfig.properties
@@ -0,0 +1,87 @@
+#-------------------需要修改配置(Local Path)-----------------
+# 文件存放总路径,不要放于NmsClient部署目录下,根据盘符相应修改
+local.data.path = D:/nms/nmsdata
+# 可删范围,只有在此路径下的文件Agent才可以删除,多个路径逗号隔开
+common.del.path.include=D:/nms/nmsclient,D:/nms/nmsdata
+# 禁删范围,Agent可删路径下不能删除的路径,多个路径逗号隔开(注:若是禁删配置有单个文件,需相应修改代码)
+common.del.path.exclude=D:/nms/nmsclient/bin,D:/nms/nmsclient/lib,D:/nms/nmsclient/shell,D:/nms/nmsclient/conf
+#------------------------SSLSocket----------------------
+# DataController部署的主机IP,需对应修改
+server_host = 10.0.6.100
+# DataController端的监听端口,用来与Server通信
+server_port = 60702
+# NMSClient自己的监听端口,用来接收Server发送的信息
+agent_port = 60701
+# SSL通信,此值不能更改
+local.ssl.keys=./bin/cer/client_ks
+local.ssl.trust=./bin/cer/client_ts
+local.ssl.path=./bin/cer
+# socket通信超时时间
+socket.timeout.minutes=30
+#------------------------Common------------------------
+# 写文件与通信流默认编码
+charset =UTF-8
+# 删除日志文件,单位天,默认保存七天,可根据情况修改
+common.del.log.days=7
+# 删除数据文件(包括监测数据和任务相关文件),单位小时,默认保存24小时,可根据情况修改
+common.del.data.hours=24
+# 删除升级时推送过来的文件,单位天,默认保存90天,可根据情况修改
+common.del.upgradefile.days=90
+# 删除临时文件,单位天,默认保存2天,可根据情况修改
+common.del.temp.days=2
+# 上传数据循环间隔,默认5分钟上传一次,可根据情况修改【数据由Server主动收集,该属性 废弃】
+common.upload.data.minutes=5
+# 定时清理内存中已完成的任务时间间隔,单位小时,默认2小时
+common.task.clear.hours=2
+#------------------------ThreadPool------------------------
+# Socket通讯线程池最大个数
+common.thread.socket.size=10
+# 定时执行线程池最大个数
+common.thread.schedule.size=15
+#------------------------Compress--------------------------
+# 文件累积达到一定数量压缩打包上传,默认1000个,可修改
+common.zip.min.size=1000
+# 压缩打包最多包含文件个数,默认2000个,可修改
+common.zip.max.size=2000
+# 回传文件累积达到一定数量压缩打包上传,默认10个,可修改
+common.max.return.size=10
+#-------------------------Local Path-------------------------
+# 自己写的脚本存放路径,不需修改
+local.script.path = ./shell
+# 监测配置基本信息文件后缀
+local.config.file.suffix = .cfg
+# NmsClient启动后PID存放文件,需与启动脚本中一致,建议不修改
+local.agent.pidfile = ./temp/agentPid.temp
+#-----------------系统预定义的监测类型(不需修改)----------------
+sys.check.type.cpu=cpu
+sys.check.type.disk=disk
+sys.check.type.memory=memory
+sys.check.type.net=net
+sys.check.type.systemdate=systemdate
+sys.check.type.process=process
+sys.check.type.process.nmsagent=nmsclient
+sys.check.type.systeminfo=systeminfo
+#------------------Debug调试使用(不需修改)---------------------
+debug.init.task.flag = 0
+debug.plugin.flag = 0
+debug.sysdetect.flag = 0
+#--【数据由Server主动收集,该属性 废弃】
+debug.uploaddata.flag = 0
+debug.alarm.flag = 0
+debug.delfile.flag = 0
+#--【数据由Server主动收集,该属性 废弃】
+debug.taskresult.flag = 0
+#--【数据由Server主动收集,该属性 废弃】
+debug.taskreturn.flag =0
+active.alarm.start=true
+#监测数据设置告警时,对于指定多个标识符(如多个盘符、多个CPU、多个网卡)的分隔符
+alarm.set.marker.separator=|
+
+#监测数据主动上报,0:启用,1:不启用
+data.send.thread.flag=0
+#主动数据上报 ip,不填默认为 server_host 的值
+data.send.thread.host=
+#主动数据上报 port,默认:9527
+data.send.thread.port=9527
+#主动数据上报间隔 interval,单位 s,默认为 10s
+data.send.thread.interval=10
\ No newline at end of file
diff --git a/src/old/thread/UpgradeOper.java b/src/old/thread/UpgradeOper.java
new file mode 100644
index 0000000..193287e
--- /dev/null
+++ b/src/old/thread/UpgradeOper.java
@@ -0,0 +1,71 @@
+package old.thread;
+
+import org.apache.log4j.Logger;
+
+public class UpgradeOper {
+ static Logger logger = Logger.getLogger(UpgradeOper.class);
+
+ public static boolean checkAgentVersionAndUpdate(){
+ boolean flag = true;
+ /*String curVer = VersionCfg.getValue();
+ SSLClient sc = null;
+ try {
+ logger.info("检查最新版本……");
+
+ sc = new SSLClient();
+ sc.sendMessageByChar("char:agentVersion");
+ String str = sc.receiveMessageByChar();//system type
+ sc.sendMessageByChar(System.getProperty("os.name"));
+ str = sc.receiveMessageByChar();//version num
+
+ long newVersion = Long.parseLong(str);
+ if (newVersion > Long.parseLong(curVer)) {
+ logger.info("正在下载最新版本" + str);
+ sc.sendMessageByChar("download");
+ str = sc.receiveMessageByChar();// 文件相关属性
+ sc.sendMessageByChar(CommonSocket.SUCCESS);
+ JSONObject jsonObj = JSONObject.fromObject(str);
+
+ Object obj = JSONObject.toBean(jsonObj, Task6.class);
+ Task6 task = (Task6) obj;
+ // 接收升级文件
+ String error = new TaskReqHandle().filePush(sc, task.getCommandParam(),
+ task.getTaskId(), true);
+ flag = (error!=null && error.length()>0) ? true : false;
+ if (flag) {
+ sc.sendMessageByChar(AgentCommand.RESULT_SEND_OK
+ + TaskResultOper.TASK_RESULT_MSG_SEPRATOR + "下发成功");
+ CommandPO command = new CommandPO();
+ command.setExecId(task.getTaskId());
+ command.setExecType(task.getTaskType());
+ command.setCommandName(task.getCommandName());
+ command.setCommandParam(task.getCommandParam());
+ command.setExecState(task.getState());
+ command.setExecVersion(task.getVersion());
+ command.setSrcPath(TaskReqHandle.getUpgradeTaskPushPath(task.getTaskId()));
+ flag = new AgentCommand(command).execAndSendResult(
+ AgentCommand.UpgradeSteps.upgrade_agent.toString(),
+ task.getCommandParam());// 相应的升级操作及各步骤结果状态的发送
+ } else {
+ sc.sendMessageByChar(AgentCommand.RESULT_FAIL
+ + TaskResultOper.TASK_RESULT_MSG_SEPRATOR
+ + "下发失败,详细信息如下: " + error);
+ }
+ sc.receiveMessageByChar();
+ } else {
+ sc.sendMessageByChar(CommonSocket.SUCCESS);
+ logger.info("当前”" + curVer + "“是最新版本, 不用更新");
+ }
+ } catch (Exception e) {
+ logger.error("检查更新最新版本失败" );
+ logger.error(Utils.printExceptionStack(e));
+ flag = false;
+ } finally {
+ if (sc != null)
+ sc.close();
+ }*/
+
+ return flag;
+ }
+
+}
diff --git a/src/test/HotSwapCL.java b/src/test/HotSwapCL.java
new file mode 100644
index 0000000..488e27a
--- /dev/null
+++ b/src/test/HotSwapCL.java
@@ -0,0 +1,73 @@
+package test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashSet;
+import org.apache.log4j.Logger;
+
+import com.nis.nmsclient.util.Utils;
+
+class HotSwapCL extends ClassLoader {
+ private Logger logger = Logger.getLogger(ClassLoader.class);
+ private String basedir; // 需要该类加载器直接加载的类文件的基目录
+ private HashSet dynaclazns; // 需要由该类加载器直接加载的类名
+
+ public HotSwapCL(String basedir, String[] clazns) {
+ super(null); // 指定父类加载器为 null
+ this.basedir = basedir;
+ dynaclazns = new HashSet();
+ loadClassByMe(clazns);
+ }
+
+ private void loadClassByMe(String[] clazns) {
+ for (int i = 0; i < clazns.length; i++) {
+ try {
+ loadDirectly(clazns[i]);
+ } catch (Exception e) {
+ logger.error(Utils.printExceptionStack(e));
+ }
+ dynaclazns.add(clazns[i]);
+ }
+ }
+
+ private Class loadDirectly(String name) throws Exception{
+ Class cls = null;
+ StringBuffer sb = new StringBuffer(basedir);
+ String classname = name.replace('.', File.separatorChar) + ".class";
+ sb.append(File.separator + classname);
+
+ File classF = new File(sb.toString());
+ cls = instantiateClass(name, new FileInputStream(classF), classF
+ .length());
+ return cls;
+ }
+
+ private Class instantiateClass(String name, InputStream fin, long len) throws Exception{
+ byte[] raw = new byte[(int) len];
+ try {
+ fin.read(raw);
+ } catch (IOException e) {
+ throw e;
+ }finally{
+ if(fin!=null){
+ fin.close();
+ }
+ }
+ return defineClass(name, raw, 0, raw.length);
+ }
+
+ protected Class loadClass(String name, boolean resolve)
+ throws ClassNotFoundException {
+ Class cls = null;
+ cls = findLoadedClass(name);
+ if (!this.dynaclazns.contains(name) && cls == null)
+ cls = getSystemClassLoader().loadClass(name);
+ if (cls == null)
+ throw new ClassNotFoundException(name);
+ if (resolve)
+ resolveClass(cls);
+ return cls;
+ }
+}
diff --git a/src/test/Test.java b/src/test/Test.java
new file mode 100644
index 0000000..9100a94
--- /dev/null
+++ b/src/test/Test.java
@@ -0,0 +1,182 @@
+package test;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.nis.nmsclient.common.Contants;
+import com.nis.nmsclient.util.DateUtil;
+import com.nis.nmsclient.util.FileUtil;
+import com.nis.nmsclient.util.FileWrUtil;
+
+public class Test {
+ public static void main(String[] args) {
+ //method2();
+ //method1();
+
+ /*File file = new File("C:\\Documents and Settings\\zhenzhen\\桌面\\nmsdata\\test1");
+ long now = System.currentTimeMillis();
+
+ File[] files = file.listFiles();
+ for(File file2 : files){
+ System.out.println("========"+file2.getName());
+ }
+ System.out.println(System.currentTimeMillis()-now);
+
+ now = System.currentTimeMillis();
+
+ String[] fileNames = file.list();
+ for(String file2 : fileNames){
+ System.out.println("========"+file2);
+ }
+ System.out.println(System.currentTimeMillis()-now);*/
+ /*try {
+ long now = System.currentTimeMillis();
+ ProcessUtil.getProcessId("javaw.exe");
+ System.out.println(System.currentTimeMillis()-now);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }*/
+
+ /*try {
+ FileUtils.copyDirectoryToDirectory(new File("D:\\temp\\test"), new File("D:\\temp\\t1"));
+ FileUtils.copyDirectory(new File("D:\\temp\\test"), new File("D:\\temp\\test2"));
+ File file = new File("D:\\temp\\tt\\temp");
+ if(file.getParentFile().canWrite()){
+ System.out.println("Yes");
+ }
+ String[] except = new String[3];
+ except[0]="D:\\temp\\test\\WebRoot";
+ except[1]="D:\\temp\\test\\t1.tar.gz";
+ except[2]="D:\\temp\\test\\src\\filemgr\\ZipUtil.java";
+ //new AgentCommand().backup(new File("D:\\temp\\test"), new File("D:\\temp\\t2"), except);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }*/
+ /*String filepath = "";
+ if(filepath.startsWith("/") || (filepath.length()>3 && filepath.substring(1,3).equals(":\\"))){
+ System.out.println("ff");
+ }*/
+ /*File file = new File("D:\\temp");
+ File file2 = new File("D:\\temp\\test\\..");
+ System.out.println(file.compareTo(file2));
+ try{
+ System.out.println(file.getCanonicalPath() + "--" + file2.getCanonicalPath());
+ }catch (Exception e) {
+ // TODO: handle exception
+ }*/
+ try {
+ //ProcessUtil.runExec("D:/test.bat");
+ /*ProcessUtil.runExec("D:\\Program Files\\PLSQL Developer\\plsqldev.exe");
+ String cmd ="D:/Program Files/Navicat for MySQL/navicat.exe";
+ Runtime.getRuntime().exec("cmd /c " + cmd.replace(" ", "\" \""));
+ //ProcessUtil.runExec("D:/Program Files/feiq/飞秋FeiQ.exe");
+ String tempString = "0";
+ int length = tempString.split(":").length;
+ System.out.println(length);*/
+
+ /*Properties prop = System.getProperties();
+ for(Map.Entry