initial commit
This commit is contained in:
22
.classpath
Normal file
22
.classpath
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry excluding="conf/|key/" kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="src/key"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="lib" path="lib/sigar/sigar.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-beanutils-1.7.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-collections.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-io.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-lang.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-logging.jar"/>
|
||||
<classpathentry kind="lib" path="lib/ezmorph-1.0.4.jar"/>
|
||||
<classpathentry kind="lib" path="lib/json-lib-2.2.2-jdk15.jar"/>
|
||||
<classpathentry kind="lib" path="lib/junit.jar"/>
|
||||
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/>
|
||||
<classpathentry kind="lib" path="lib/ostermillerutils_1_07_00.jar"/>
|
||||
<classpathentry kind="lib" path="lib/filemgr/ant.jar"/>
|
||||
<classpathentry kind="lib" path="lib/filemgr/javatar-2.5.jar"/>
|
||||
<classpathentry kind="lib" path="lib/filemgr/java-unrar-0.3.jar"/>
|
||||
<classpathentry kind="lib" path="lib/fileComment.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
.project
Normal file
17
.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>nms_client</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
4
.settings/org.eclipse.core.resources.prefs
Normal file
4
.settings/org.eclipse.core.resources.prefs
Normal file
@@ -0,0 +1,4 @@
|
||||
#Thu Mar 22 17:05:34 CST 2012
|
||||
eclipse.preferences.version=1
|
||||
encoding//src/myconfig.properties=UTF-8
|
||||
encoding/<project>=UTF-8
|
||||
4
META-INF/MANIFEST.MF
Normal file
4
META-INF/MANIFEST.MF
Normal file
@@ -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
|
||||
|
||||
12
META-INF/readme.txt
Normal file
12
META-INF/readme.txt
Normal file
@@ -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下。
|
||||
15
bin/log4j.properties.svntmp
Normal file
15
bin/log4j.properties.svntmp
Normal file
@@ -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
|
||||
BIN
lib/commons-beanutils-1.7.jar
Normal file
BIN
lib/commons-beanutils-1.7.jar
Normal file
Binary file not shown.
BIN
lib/commons-collections.jar
Normal file
BIN
lib/commons-collections.jar
Normal file
Binary file not shown.
BIN
lib/commons-io.jar
Normal file
BIN
lib/commons-io.jar
Normal file
Binary file not shown.
BIN
lib/commons-lang.jar
Normal file
BIN
lib/commons-lang.jar
Normal file
Binary file not shown.
BIN
lib/commons-logging.jar
Normal file
BIN
lib/commons-logging.jar
Normal file
Binary file not shown.
BIN
lib/ezmorph-1.0.4.jar
Normal file
BIN
lib/ezmorph-1.0.4.jar
Normal file
Binary file not shown.
BIN
lib/fileComment.jar
Normal file
BIN
lib/fileComment.jar
Normal file
Binary file not shown.
BIN
lib/filemgr/ant.jar
Normal file
BIN
lib/filemgr/ant.jar
Normal file
Binary file not shown.
BIN
lib/filemgr/java-unrar-0.3.jar
Normal file
BIN
lib/filemgr/java-unrar-0.3.jar
Normal file
Binary file not shown.
BIN
lib/filemgr/javatar-2.5.jar
Normal file
BIN
lib/filemgr/javatar-2.5.jar
Normal file
Binary file not shown.
BIN
lib/json-lib-2.2.2-jdk15.jar
Normal file
BIN
lib/json-lib-2.2.2-jdk15.jar
Normal file
Binary file not shown.
BIN
lib/junit.jar
Normal file
BIN
lib/junit.jar
Normal file
Binary file not shown.
BIN
lib/log4j-1.2.15.jar
Normal file
BIN
lib/log4j-1.2.15.jar
Normal file
Binary file not shown.
BIN
lib/ostermillerutils_1_07_00.jar
Normal file
BIN
lib/ostermillerutils_1_07_00.jar
Normal file
Binary file not shown.
48
lib/sigar/.sigar_shellrc
Normal file
48
lib/sigar/.sigar_shellrc
Normal file
@@ -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
|
||||
BIN
lib/sigar/libsigar-amd64-freebsd-6.so
Normal file
BIN
lib/sigar/libsigar-amd64-freebsd-6.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-amd64-linux.so
Normal file
BIN
lib/sigar/libsigar-amd64-linux.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-amd64-solaris.so
Normal file
BIN
lib/sigar/libsigar-amd64-solaris.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-ia64-hpux-11.sl
Normal file
BIN
lib/sigar/libsigar-ia64-hpux-11.sl
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-ia64-linux.so
Normal file
BIN
lib/sigar/libsigar-ia64-linux.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-pa-hpux-11.sl
Normal file
BIN
lib/sigar/libsigar-pa-hpux-11.sl
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-ppc-aix-5.so
Normal file
BIN
lib/sigar/libsigar-ppc-aix-5.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-ppc-linux.so
Normal file
BIN
lib/sigar/libsigar-ppc-linux.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-ppc64-aix-5.so
Normal file
BIN
lib/sigar/libsigar-ppc64-aix-5.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-ppc64-linux.so
Normal file
BIN
lib/sigar/libsigar-ppc64-linux.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-s390x-linux.so
Normal file
BIN
lib/sigar/libsigar-s390x-linux.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-sparc-solaris.so
Normal file
BIN
lib/sigar/libsigar-sparc-solaris.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-sparc64-solaris.so
Normal file
BIN
lib/sigar/libsigar-sparc64-solaris.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-universal-macosx.dylib
Normal file
BIN
lib/sigar/libsigar-universal-macosx.dylib
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-universal64-macosx.dylib
Normal file
BIN
lib/sigar/libsigar-universal64-macosx.dylib
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-x86-freebsd-5.so
Normal file
BIN
lib/sigar/libsigar-x86-freebsd-5.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-x86-freebsd-6.so
Normal file
BIN
lib/sigar/libsigar-x86-freebsd-6.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-x86-linux.so
Normal file
BIN
lib/sigar/libsigar-x86-linux.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-x86-solaris.so
Normal file
BIN
lib/sigar/libsigar-x86-solaris.so
Normal file
Binary file not shown.
BIN
lib/sigar/libsigar-x86_64-linux.so
Normal file
BIN
lib/sigar/libsigar-x86_64-linux.so
Normal file
Binary file not shown.
BIN
lib/sigar/sigar-amd64-winnt.dll
Normal file
BIN
lib/sigar/sigar-amd64-winnt.dll
Normal file
Binary file not shown.
BIN
lib/sigar/sigar-x86-winnt.dll
Normal file
BIN
lib/sigar/sigar-x86-winnt.dll
Normal file
Binary file not shown.
BIN
lib/sigar/sigar-x86-winnt.lib
Normal file
BIN
lib/sigar/sigar-x86-winnt.lib
Normal file
Binary file not shown.
BIN
lib/sigar/sigar.jar
Normal file
BIN
lib/sigar/sigar.jar
Normal file
Binary file not shown.
170
linuxinstall/autoinstall.sh
Normal file
170
linuxinstall/autoinstall.sh
Normal file
@@ -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
|
||||
2
linuxinstall/conf/jvm.conf
Normal file
2
linuxinstall/conf/jvm.conf
Normal file
@@ -0,0 +1,2 @@
|
||||
-Xms64m
|
||||
-Xmx128m
|
||||
5
linuxinstall/detectShell/config
Normal file
5
linuxinstall/detectShell/config
Normal file
@@ -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
|
||||
590
linuxinstall/detectShell/getMacInfo.v2.sh
Normal file
590
linuxinstall/detectShell/getMacInfo.v2.sh
Normal file
@@ -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</br>"
|
||||
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</br>"
|
||||
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</br>"
|
||||
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</br>"
|
||||
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</br>"
|
||||
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</br>"
|
||||
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</br>"
|
||||
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</br>"
|
||||
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}
|
||||
362
linuxinstall/detectShell/monitor_net.py
Normal file
362
linuxinstall/detectShell/monitor_net.py
Normal file
@@ -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)
|
||||
|
||||
|
||||
|
||||
|
||||
5
linuxinstall/detectShell/remote_net.sh
Normal file
5
linuxinstall/detectShell/remote_net.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
python /root/zhong/monitor_net.py #py脚本的路径,需要手动填写
|
||||
cat `ls|grep csv`
|
||||
rm -f *.csv
|
||||
29
linuxinstall/detectShell/sysinfo
Normal file
29
linuxinstall/detectShell/sysinfo
Normal file
@@ -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,
|
||||
|
||||
215
linuxinstall/install.sh
Normal file
215
linuxinstall/install.sh
Normal file
@@ -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..."
|
||||
59
linuxinstall/readme.txt
Normal file
59
linuxinstall/readme.txt
Normal file
@@ -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命令能否正常使用
|
||||
47
linuxinstall/shell/check_userpass.sh
Normal file
47
linuxinstall/shell/check_userpass.sh
Normal file
@@ -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"}
|
||||
"* <20><><EFBFBD><EFBFBD>*" {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
78
linuxinstall/shell/execCmdBySu.sh
Normal file
78
linuxinstall/shell/execCmdBySu.sh
Normal file
@@ -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<><30>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD>ȡ<EEB2A2>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>ִ<EFBFBD><D6B4>&<26><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>
|
||||
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
|
||||
|
||||
#<23>رտ<D8B1><D5BF><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
||||
log_user 0
|
||||
|
||||
spawn whoami
|
||||
expect "root" {set flag 1}
|
||||
|
||||
# <20><>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>${cmd}<7D><><EFBFBD><D7B7>"echo $?",Ϊ<><CEAA>ȡcmd<6D><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ʵ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>ε<EFBFBD><CEB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
||||
if {$resultFlag==0} {
|
||||
spawn su - -c "${cmd};echo $?" ${user}
|
||||
|
||||
#<23><>ǰ<EFBFBD><C7B0>¼<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>root<6F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if {$flag==0} {
|
||||
expect {
|
||||
"<22><><EFBFBD><EFBFBD>*" {send ${pass}\r}
|
||||
"password*" {send ${pass}\r}
|
||||
default {
|
||||
send_error "error\n"
|
||||
exit 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#<23><EFBFBD><F2BFAABF><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
||||
log_user 1
|
||||
|
||||
expect {
|
||||
"* <20><><EFBFBD>벻<EFBFBD><EBB2BB>ȷ*" {exit 1}
|
||||
#----<2D>˴<EFBFBD><CBB4><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ܶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
eof {exit 0}
|
||||
}
|
||||
|
||||
} else {
|
||||
spawn su - -c "${cmd}" ${user}
|
||||
|
||||
#<23><>ǰ<EFBFBD><C7B0>¼<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>root<6F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
if {$flag==0} {
|
||||
expect {
|
||||
"<22><><EFBFBD><EFBFBD>*" {send ${pass}\r}
|
||||
"password*" {send ${pass}\r}
|
||||
default {
|
||||
send_error "error\n"
|
||||
exit 2
|
||||
}
|
||||
}
|
||||
}
|
||||
expect {
|
||||
"* <20><><EFBFBD>벻<EFBFBD><EBB2BB>ȷ*" {exit 1}
|
||||
#----Ϊ<><CEAA>&<26><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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
|
||||
260
linuxinstall/shell/nmsagent.sh
Normal file
260
linuxinstall/shell/nmsagent.sh
Normal file
@@ -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
|
||||
55
linuxinstall/shell/nmsclient_shouhu.sh
Normal file
55
linuxinstall/shell/nmsclient_shouhu.sh
Normal file
@@ -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
|
||||
30
linuxinstall/shell/restart.sh
Normal file
30
linuxinstall/shell/restart.sh
Normal file
@@ -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 "$@"
|
||||
30
linuxinstall/shell/shutdown.sh
Normal file
30
linuxinstall/shell/shutdown.sh
Normal file
@@ -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 "$@"
|
||||
30
linuxinstall/shell/startup.sh
Normal file
30
linuxinstall/shell/startup.sh
Normal file
@@ -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 "$@"
|
||||
549
src/com/nis/nmsclient/NmsClient.java
Normal file
549
src/com/nis/nmsclient/NmsClient.java
Normal file
@@ -0,0 +1,549 @@
|
||||
package com.nis.nmsclient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.common.SysConfig;
|
||||
import com.nis.nmsclient.config.DetecConfReqHandle;
|
||||
import com.nis.nmsclient.model.AlarmInfo;
|
||||
import com.nis.nmsclient.model.SetInfo;
|
||||
import com.nis.nmsclient.thread.WritePidThread;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmThread;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmUtil;
|
||||
import com.nis.nmsclient.thread.alarm.ErrorCode;
|
||||
import com.nis.nmsclient.thread.socket.CommonSocket;
|
||||
import com.nis.nmsclient.thread.socket.SSLClient;
|
||||
import com.nis.nmsclient.thread.socket.SSLServer;
|
||||
import com.nis.nmsclient.thread.task.TaskResultOper;
|
||||
import com.nis.nmsclient.thread.timer.DelLocalFileThread;
|
||||
import com.nis.nmsclient.thread.upload.DataSendThread;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.ProcessUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
/**
|
||||
* NMSClient 程序启动主方法类
|
||||
*/
|
||||
public class NmsClient{
|
||||
static Logger logger = Logger.getLogger(NmsClient.class);
|
||||
// 监测设置信息,初始化完成后清空
|
||||
public List<SetInfo> setInfos = new LinkedList<SetInfo>();
|
||||
// 监测设置的报警字段设置信息,初始化完成后清空
|
||||
public Map<Long, List<AlarmInfo>> alarmInfos = new HashMap<Long, List<AlarmInfo>>();
|
||||
private int testGap = 60; //单位:秒
|
||||
|
||||
static{
|
||||
// Thread.currentThread().setName("NMSClient主程序");
|
||||
Thread.currentThread().setName("NMSClient Main Program");
|
||||
}
|
||||
|
||||
/**
|
||||
* NMSClient 程序启动入口
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
logger.info("------- NMSClient 启动开始------------");
|
||||
|
||||
//NMSClient进程停止保存缓存操作
|
||||
doShutDownWork();
|
||||
|
||||
/**
|
||||
* 启动通信程序, 如果端口已存在即SSLServer创建失败,退出程序
|
||||
*/
|
||||
//通讯端口放在第一步,为了确保初始化配置不成功或个数为0时 新建任务下发、DC收集数据、握手等操作 能正常执行
|
||||
//注意:要在通讯程序中判断SeqId是否为空,为空 则只接收握手通信、收集数据通信,其他的通信都抛弃
|
||||
SSLServer sslServer = null;
|
||||
try {
|
||||
sslServer = new SSLServer();
|
||||
} catch (IOException e) {
|
||||
TaskResultOper.handerAgentUpgradeResult(false);
|
||||
logger.error("NMSClient Program termination:" + e.getMessage());
|
||||
AlarmUtil.sendNMSErrorMsg(ErrorCode.ProtListenerError, Utils.getLocalIp(), "i18n_client.NmsClient.ncCommunicatePortErr_n81i");
|
||||
System.exit(0);
|
||||
}
|
||||
// Thread server = new Thread(sslServer, "通讯线程");
|
||||
Thread server = new Thread(sslServer, "Communication Thread");
|
||||
server.start();
|
||||
|
||||
/**
|
||||
* 启动守护进程
|
||||
*/
|
||||
try {
|
||||
String os = System.getProperty("os.name");
|
||||
String cmd = "";
|
||||
String procSearchKey = null;
|
||||
if (os.startsWith("Windows")) {
|
||||
String homePath = new File(Contants.SYSTEM_PATH).getParent();//NC布署路径
|
||||
cmd = homePath + File.separator + "script" + File.separator + "nmsclient_shouhu.bat";
|
||||
procSearchKey = "nmsclient_shouhu.bat";
|
||||
} else if (os.startsWith("Linux")) {
|
||||
cmd = Contants.SYSTEM_PATH + File.separator + "nmsclient_shouhu.sh";
|
||||
procSearchKey = "nmsclient_shouhu.sh";
|
||||
}
|
||||
Object[] objArr = ProcessUtil.checkPidAndGetPid(null, procSearchKey);
|
||||
int isExistFlag = Integer.parseInt(objArr[0].toString());
|
||||
if(isExistFlag == 0){// 守护进程不存在,启动
|
||||
logger.info("正在启动守护进程...");
|
||||
ProcessUtil.runExec(cmd, new String[]{Contants.localTaskResultPath}, null, null, true);
|
||||
objArr = ProcessUtil.checkPidAndGetPid(null, procSearchKey);
|
||||
isExistFlag = Integer.parseInt(objArr[0].toString());
|
||||
if(isExistFlag != 0){
|
||||
logger.info("守护进程 启动成功");
|
||||
}else{
|
||||
logger.info("守护进程 启动失败");
|
||||
AlarmUtil.sendNMSErrorMsg(ErrorCode.DeamonNotExist, Utils.getLocalIp(), "i18n_client.NmsClient.ncDeamonStartFail_n81i");
|
||||
}
|
||||
}else {
|
||||
logger.info("守护进程 已存在,无需再启动");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Start the daemon exception", e);
|
||||
AlarmUtil.sendNMSErrorMsg(ErrorCode.DeamonNotExist, Utils.getLocalIp(), "i18n_client.NmsClient.ncDeamonStartException_n81i," + e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 相关业务操作入口
|
||||
*/
|
||||
new NmsClient().run();
|
||||
|
||||
}
|
||||
|
||||
public void run() {
|
||||
//执行写PID线程
|
||||
Common.service.submit(new WritePidThread());
|
||||
|
||||
// 2013-3-8 由于初始化监测配置个数为0时不断重新获取,致使任务初始化无法执行,先将任务与监测配置分开执行
|
||||
// 为了将初始化监测配置放于任务之后,所以在开始执行之前,先与DC握手通讯,以保证通讯正常
|
||||
while (true) {
|
||||
try {
|
||||
//与Server通信
|
||||
Future<?> serFuture = Common.service.submit(new SSLClient(
|
||||
Thread.currentThread().getName(),
|
||||
CommonSocket.REQ_HAND_SHAKE, null));
|
||||
if (Contants.isSucessByResult((String) serFuture.get())) {
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Handshake communication abnormality:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(1000 * testGap);// 如果握手失败,让当前线程暂停N秒,再重试
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取本机唯一标识
|
||||
initUUID();
|
||||
// 检查本机操作系统和IP是否变更
|
||||
checkLocalOperSystemAndIp();
|
||||
|
||||
/**************************** 任务部分处理操作 ***************************/
|
||||
// 处理Agent自身升级时的执行结果文件
|
||||
TaskResultOper.handerAgentUpgradeResult(true);
|
||||
// 发送所有之前上发失败的任务结果
|
||||
TaskResultOper.initSendAllTaskResult();
|
||||
|
||||
// 初始化执行中的任务
|
||||
if (Contants.DEBUG_INIT_TASK_FLAG == 0) {
|
||||
// Common.scheduled.schedule(new SSLClient("初始化任务",
|
||||
Common.scheduled.schedule(new SSLClient("Initialization Task",
|
||||
CommonSocket.REQ_INIT_TASK, null),
|
||||
Contants.COMMON_TASK_INIT_DELAY_MINUTES, TimeUnit.MINUTES);
|
||||
}
|
||||
// 定时上传发送失败的任务结果
|
||||
/*if (Contants.DEBUG_TASKRESULT_FLAG == 0) {
|
||||
Common.scheduled.scheduleWithFixedDelay(new Runnable() {
|
||||
public void run() {
|
||||
Thread.currentThread().setName("上传任务结果");
|
||||
TaskResultOper.initSendAllTaskResult();
|
||||
}
|
||||
}, 0, Contants.COMMON_TASK_RESULT_SEND_MINUTES, TimeUnit.MINUTES);
|
||||
}*/
|
||||
// 定时上传发送失败的回传文件
|
||||
/*if (Contants.DEBUG_TASKRETURN_FLAG == 0) {
|
||||
Common.scheduled.scheduleWithFixedDelay(new Runnable() {
|
||||
public void run() {
|
||||
Thread.currentThread().setName("回传文件");
|
||||
new TaskReturnHandle().sendAllTaskReturnFile();
|
||||
}
|
||||
}, 0, Contants.COMMON_TASK_RESULT_SEND_MINUTES, TimeUnit.MINUTES);
|
||||
}*/
|
||||
// 定时清理内存中已完成的任务
|
||||
Common.scheduled.scheduleWithFixedDelay(new Runnable() {
|
||||
public void run() {
|
||||
// Thread.currentThread().setName("清理已完成任务");
|
||||
Thread.currentThread().setName("Clean Up The Completed Task");
|
||||
// == 1、针对结果文件过多时打包上传未完成的文件
|
||||
File taskDir = new File(Contants.localTaskPath);
|
||||
if (taskDir.exists()) {
|
||||
// ----取所有未上传完成的Zip文件
|
||||
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
|
||||
// 若存在未上传完成的ZIP结果文件,则不清理内存中的任务
|
||||
if (zipArr.length > 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// == 2、检查当前结果文件数量
|
||||
File resultDir = new File(TaskResultOper.getTaskResultPath());
|
||||
if(resultDir.exists()){
|
||||
File[] fileArr = FileUtil.getFilesEndWith(resultDir, Contants.TASK_RESULT_FILE_SUFFIX);
|
||||
// 若存在未上传的结果文件,则不清理内存中的任务
|
||||
if(fileArr.length > 0){
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// -- 清理已完成的任务,待考虑 以后定时清理方案
|
||||
Common.removeCancelAndDoneTaskFuture();
|
||||
}
|
||||
}, Contants.COMMON_TASK_CLEAR_HOURS, Contants.COMMON_TASK_CLEAR_HOURS, TimeUnit.HOURS);
|
||||
|
||||
/**************************** 定时清理本地文件操作 ***************************/
|
||||
// 定时删除本地生成的文件
|
||||
if (Contants.DEBUG_DELFILE_FLAG == 0) {
|
||||
//2012-4-28 将所有删除文件的线程合并为一个,取设置的所有清理文件间隔中的最小值作为检查间隔
|
||||
//2012-12-17 第一次执行删除文件时间,不是启动立即删除,设置延迟到启动之后的第一个凌晨0点
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.DAY_OF_MONTH, 1);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
long delay = cal.getTimeInMillis()-System.currentTimeMillis();
|
||||
// Common.scheduled.scheduleAtFixedRate(new DelLocalFileThread("删除文件"), delay,
|
||||
Common.scheduled.scheduleAtFixedRate(new DelLocalFileThread("Delete Files"), delay,
|
||||
getMinCheckPeriod() * 60 * 60 * 1000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**************************** 监测配置信息处理操作 ***************************/
|
||||
// 初始化监测配置信息
|
||||
initDetecConfig();
|
||||
// 启动三方监测程序
|
||||
if (Contants.DEBUG_PLUGIN_FLAG == 0) {
|
||||
//-------------初始化三方监测 start
|
||||
for (SetInfo setInfo : setInfos) {
|
||||
// 缓存三方监测配置信息,用于合并临时结果文件
|
||||
Common.putPluginDetecSetInfo(setInfo.getId(), setInfo);
|
||||
|
||||
if (!Common.COMMON_SYS_SETINFO.equals(setInfo.getIsSchedule())) { // 第三方且由Agent启动
|
||||
Common.startPluginDetec(setInfo);
|
||||
}
|
||||
}
|
||||
//-------------初始化三方监测 end
|
||||
}
|
||||
//启动预设监测程序
|
||||
if (Contants.DEBUG_SYSDETECT_FLAG == 0) {
|
||||
//-------------初始化预设监测 start
|
||||
for (SetInfo setInfo : setInfos) {
|
||||
if (Common.COMMON_SYS_SETINFO.equals(setInfo.getIsSchedule())) {// 判断如果是系统预设类型
|
||||
Common.addOrUpdateSysDetec(setInfo, alarmInfos
|
||||
.get(setInfo.getId()));
|
||||
}
|
||||
}
|
||||
//-------------初始化预设监测 end
|
||||
}
|
||||
|
||||
//启用监测主动上报
|
||||
if(Contants.DATA_SEND_THREAD_FLAG == 0){
|
||||
Common.scheduled.scheduleWithFixedDelay(new DataSendThread(Contants.DATA_SEND_THREAD_HOST, Contants.DATA_SEND_THREAD_PORT), (int)(Math.random()*Contants.DATA_SEND_THREAD_INTERVAL), Contants.DATA_SEND_THREAD_INTERVAL, TimeUnit.SECONDS);
|
||||
logger.info("监测主动上报已成功添加到线程池");
|
||||
}
|
||||
|
||||
// 启动上传数据程序
|
||||
/*if (Contants.DEBUG_UPLOADDATA_FLAG == 0) {
|
||||
Common.scheduled.scheduleWithFixedDelay(new UploadDataThread("上传数据"), 1,
|
||||
Contants.COMMON_UPLOAD_DATA_MINUTES, TimeUnit.MINUTES);
|
||||
}*/
|
||||
// 启动主动报警程序
|
||||
if (Contants.DEBUG_ALARM_FLAG == 0) {
|
||||
// Common.scheduled.scheduleAtFixedRate(new AlarmThread("主动预警"), 1,
|
||||
Common.scheduled.scheduleAtFixedRate(new AlarmThread("Active Early Warning"), 1,
|
||||
Contants.COMMON_UPLOAD_DATA_MINUTES, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
//清空变量
|
||||
setInfos.clear();
|
||||
setInfos = null;
|
||||
alarmInfos.clear();
|
||||
alarmInfos = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 第一次布署NMSAgent时,初始化本机唯一标志
|
||||
*/
|
||||
public void initUUID() {
|
||||
if (Contants.AGENT_HOST_UUID == null) {// 第一次布署Agent
|
||||
while (true) {
|
||||
String uuid = null;
|
||||
try {
|
||||
Future<?> future = Common.service.submit(new SSLClient(
|
||||
// "获取本机标识", CommonSocket.REQ_LOCAL_UUID, null));
|
||||
"Obtain The Local Identity", CommonSocket.REQ_LOCAL_UUID, null));
|
||||
String msg = (String) future.get();
|
||||
if (Contants.isSucessByResult(msg)) {
|
||||
// dc发送的数据格式为 uuid:::localIp
|
||||
String[] result = Contants.getDescByResult(msg).split(
|
||||
":::");
|
||||
uuid = result[0];
|
||||
String localIp = result[1];
|
||||
logger.info("本机标识ID:" + uuid);
|
||||
if (!(uuid == null || "".equals(uuid)
|
||||
|| "null".equals(uuid) || localIp == null
|
||||
|| "".equals(localIp) || "null".equals(localIp))) {
|
||||
|
||||
SysConfig.setUUIDValue(uuid);
|
||||
//根据ip地址获取端口名称
|
||||
String name = Utils.getNetInterfaceNameByIp(localIp);
|
||||
//将端口名称写入配置文件
|
||||
SysConfig.setInterfaceNameValue(name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Get the unique identity of the native or IP port name exception:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
try {
|
||||
logger.debug((1000 * testGap ) +"s 后重试");
|
||||
Thread.sleep(1000 * testGap);// 如果获取失败,让当前线程暂停N秒,再重试
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 每次启动时检查本机操作系统类型和IP是否有变更,若有,则将最新信息写入文件并发送信息到Server,
|
||||
* 由Server端根据SeqID更新所有相关节点信息
|
||||
*/
|
||||
public void checkLocalOperSystemAndIp() {
|
||||
// ---- 取本机相关信息
|
||||
String operateSysType = getOperateSysType();
|
||||
String localIp = Utils.getIpAddressByEthName(Contants.AGENT_INTERFACE_NAME_KEY);
|
||||
|
||||
logger.info("本机通讯IP:" + localIp);
|
||||
if (localIp != null && !"".equals(localIp) && !"null".equals(localIp)) {
|
||||
// ----- 与原有信息比较
|
||||
if (Contants.AGENT_OPERATE_SYSTEM == null
|
||||
|| Contants.AGENT_LOCAL_IP == null
|
||||
|| !operateSysType.equals(Contants.AGENT_OPERATE_SYSTEM)
|
||||
|| !localIp.equals(Contants.AGENT_LOCAL_IP)) {
|
||||
// 若有变更,则将本机系统和IP写入文件,再发送到Server
|
||||
SysConfig.setUUIDValue(operateSysType, localIp);
|
||||
// 发送UUID、SystemType、LocalIp到Server
|
||||
String sendMsg = Contants.AGENT_HOST_UUID
|
||||
+ Contants.COMMON_MSG_SEPRATOR
|
||||
+ Contants.AGENT_OPERATE_SYSTEM;
|
||||
try {
|
||||
Future<?> future1 = Common.service.submit(new SSLClient(
|
||||
// "信息变更", CommonSocket.REQ_LOCAL_CHANGE, sendMsg));
|
||||
"Information Change", CommonSocket.REQ_LOCAL_CHANGE, sendMsg));
|
||||
String resultMsg = (String) future1.get();
|
||||
String descMsg = Contants.getDescByResult(resultMsg);
|
||||
if (!Contants.isSucessByResult(resultMsg)) {
|
||||
if (descMsg == null || "".equals(descMsg)
|
||||
|| "null".equals(descMsg)) {
|
||||
// descMsg = "信息变更出现问题,可能存在重复IP,请手动检查";
|
||||
descMsg = "Information changes may occur. Duplicate IP may exist. Please check manually.";
|
||||
}
|
||||
logger.error("Failure of information change:" + descMsg);
|
||||
} else if (descMsg != null && !"".equals(descMsg)) {
|
||||
logger.info("信息变更:" + descMsg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Information change:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取本机操作系统类型
|
||||
*
|
||||
* @return 1代表linux ;2代表windows; “” 为其它
|
||||
*/
|
||||
public static String getOperateSysType() {
|
||||
String operateSysType = null;
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.startsWith("Windows")) {
|
||||
operateSysType = "2";
|
||||
} else if (os.startsWith("Linux")) {
|
||||
operateSysType = "1";
|
||||
} else {
|
||||
operateSysType = "";
|
||||
}
|
||||
return operateSysType;
|
||||
}
|
||||
|
||||
|
||||
// /**
|
||||
// * 第一次布署NMSAgent时,初始化本机唯一标志
|
||||
// */
|
||||
// public void initUUID(){
|
||||
// if(Contants.AGENT_HOST_UUID == null){//第一次布署Agent
|
||||
// while (true) {
|
||||
// String uuid = null;
|
||||
// try {
|
||||
// Future<?> future = Common.service.submit(new SSLClient(
|
||||
// "获取本机标识", CommonSocket.REQ_LOCAL_UUID, null));
|
||||
// String msg = (String) future.get();
|
||||
// if (Contants.isSucessByResult(msg)) {
|
||||
// uuid = Contants.getDescByResult(msg);
|
||||
// logger.info("本机标识ID:" + uuid);
|
||||
// if(uuid != null && !"".equals(uuid) && !"null".equals(uuid)){
|
||||
// SysConfig.setUUIDValue(uuid);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("获取本机唯一标识异常:" + Utils.printExceptionStack(e));
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// Thread.sleep(1000 * testGap);// 如果获取失败,让当前线程暂停N秒,再重试
|
||||
// } catch (InterruptedException e) {
|
||||
// logger.error(Utils.printExceptionStack(e));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 每次启动时检查本机操作系统类型和IP是否有变更,若有,则将最新信息写入文件并发送信息到Server,由Server端根据SeqID更新所有相关节点信息
|
||||
// */
|
||||
// public void checkLocalOperSystemAndIp(){
|
||||
// //---- 取本机相关信息
|
||||
// String operateSysType = null;
|
||||
// String os = System.getProperty("os.name");
|
||||
// if (os.startsWith("Windows")) {
|
||||
// operateSysType = "2";
|
||||
// }else if (os.startsWith("Linux")){
|
||||
// operateSysType = "1";
|
||||
// }else{
|
||||
// operateSysType = "";
|
||||
// }
|
||||
// String localIp = Utils.getLocalIp();
|
||||
// //----- 与原有信息比较
|
||||
// if (Contants.AGENT_OPERATE_SYSTEM == null
|
||||
// || Contants.AGENT_LOCAL_IP == null
|
||||
// || !operateSysType.equals(Contants.AGENT_OPERATE_SYSTEM)
|
||||
// || !localIp.equals(Contants.AGENT_LOCAL_IP)) {
|
||||
// //若有变更,则将本机系统和IP写入文件,再发送到Server
|
||||
// SysConfig.setUUIDValue(operateSysType, localIp);
|
||||
// //发送UUID、SystemType、LocalIp到Server
|
||||
// String sendMsg = Contants.AGENT_HOST_UUID
|
||||
// + Contants.COMMON_MSG_SEPRATOR
|
||||
// + Contants.AGENT_OPERATE_SYSTEM;
|
||||
// try {
|
||||
// Future<?> future = Common.service.submit(new SSLClient("信息变更",
|
||||
// CommonSocket.REQ_LOCAL_CHANGE, sendMsg));
|
||||
// String resultMsg = (String) future.get();
|
||||
// String descMsg = Contants.getDescByResult(resultMsg);
|
||||
// if (!Contants.isSucessByResult(resultMsg)) {
|
||||
// if(descMsg == null || "".equals(descMsg) || "null".equals(descMsg)){
|
||||
// descMsg = "信息变更出现问题,可能存在重复IP,请手动检查";
|
||||
// }
|
||||
// logger.error("信息变更失败:" + descMsg);
|
||||
// }else if(descMsg!=null && !"".equals(descMsg)){
|
||||
// logger.info("信息变更:" + descMsg);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// logger.error("信息变更:" + Utils.printExceptionStack(e));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 请求获得初始化监测配置信息: 获取失败或获取配置个数为0,则不断循环获取
|
||||
*/
|
||||
public void initDetecConfig(){
|
||||
while (true) {
|
||||
try {
|
||||
Future<?> future = Common.service.submit(new SSLClient(
|
||||
// "初始化监测配置", CommonSocket.REQ_INIT_CONFIG, null));
|
||||
"Initialization Of Monitoring Configuration", CommonSocket.REQ_INIT_CONFIG, null));
|
||||
String msg = (String) future.get();
|
||||
if (Contants.isSucessByResult(msg)) {
|
||||
msg = Contants.getDescByResult(msg);
|
||||
new DetecConfReqHandle().handlerConfigByInit(msg, setInfos, alarmInfos);
|
||||
logger.info("初始化监测配置个数:" + setInfos.size());
|
||||
if(setInfos.size()>0){
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Initialization of monitoring configuration exceptions:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(1000 * testGap);//如果初始化失败,让当前线程暂停N秒,再重试
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取设置的所有清理文件间隔中的最小值, 单位:小时
|
||||
*/
|
||||
public int getMinCheckPeriod(){
|
||||
int period = Contants.COMMON_DEL_DATA_HOURS;
|
||||
period = period < Contants.COMMON_DEL_LOG_DAYS * 24 ? period
|
||||
: Contants.COMMON_DEL_LOG_DAYS * 24;
|
||||
period = period < Contants.COMMON_DEL_TEMP_DAYS * 24 ? period
|
||||
: Contants.COMMON_DEL_TEMP_DAYS * 24;
|
||||
period = period < Contants.COMMON_DEL_UPGRADEFILE_DAYS * 24 ? period
|
||||
: Contants.COMMON_DEL_UPGRADEFILE_DAYS * 24;
|
||||
logger.debug("=========del file period=" + period);
|
||||
|
||||
return period;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 进程停用时,触发该事件,将缓存数据存入硬盘
|
||||
* 在NC侧 当前情况是当NMSClient服务停止时无需做操作,因为NC启动的时候NC会将执行失败的任务结果发送给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.全部存盘,下次启动时入库
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
453
src/com/nis/nmsclient/common/Common.java
Normal file
453
src/com/nis/nmsclient/common/Common.java
Normal file
@@ -0,0 +1,453 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.filefilter.FalseFileFilter;
|
||||
import org.apache.commons.io.filefilter.FileFilterUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.config.DetecConfOper;
|
||||
import com.nis.nmsclient.model.AlarmInfo;
|
||||
import com.nis.nmsclient.model.SetInfo;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmPO;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmUtil;
|
||||
import com.nis.nmsclient.thread.plugin.StartPluginRun;
|
||||
import com.nis.nmsclient.thread.task.LoopTaskThread;
|
||||
import com.nis.nmsclient.util.ProcessUtil;
|
||||
import com.nis.systeminfo.thread.GetInfoRun;
|
||||
|
||||
public class Common {
|
||||
static Logger logger = Logger.getLogger(Common.class);
|
||||
|
||||
public static final String COMMON_SYS_SETINFO = "0";
|
||||
public static boolean NC_UPGRADE_FLAG = false;
|
||||
|
||||
public static final ExecutorService service = Executors
|
||||
.newFixedThreadPool(Contants.COMMON_THREAD_SOCKET_SIZE); // 通讯线程池
|
||||
public static final ScheduledExecutorService scheduled = Executors
|
||||
.newScheduledThreadPool(Contants.COMMON_THREAD_SCHEDULE_SIZE); // 定时执行线程池
|
||||
|
||||
// 任务变更或取消控制集 object[1]=ScheduledFuture<?>,object[2]=LoopTaskThread
|
||||
private static Map<Long, Object[]> taskFutureMap = Collections.synchronizedMap(new HashMap<Long, Object[]>());
|
||||
// 主动告警信息: 主动报警线程使用
|
||||
private static Map<Long, AlarmPO> alarmPOs = Collections.synchronizedMap(new HashMap<Long, AlarmPO>());
|
||||
// 预置监测控制集
|
||||
private static Map<Long, ScheduledFuture<?>> sysDetecFutrue = Collections.synchronizedMap(new HashMap<Long, ScheduledFuture<?>>());
|
||||
// Agent定时启动的三方监测控制集
|
||||
private static Map<Long, ScheduledFuture<?>> pluginDetecFutrue = Collections.synchronizedMap(new HashMap<Long, ScheduledFuture<?>>());
|
||||
// 三方监测设置集
|
||||
private static Map<Long, SetInfo> pluginDetecSetInfoMap = new HashMap<Long, SetInfo>();
|
||||
// 监测信息报警相关信息(alarmInfo.setInfoId, alarmInfo)
|
||||
public static Map<Long, List<AlarmInfo>> detecAlarmInfoMap = new HashMap<Long, List<AlarmInfo>>();
|
||||
|
||||
/**
|
||||
* 缓存三方监测设置
|
||||
*
|
||||
* @param key
|
||||
* @param setInfo
|
||||
* @param lastMergeFileDetecTime
|
||||
*/
|
||||
public static void putPluginDetecSetInfo(Long key, SetInfo setInfo) {
|
||||
long planTime = (setInfo.getControlStartTime() != null) ? setInfo.getControlStartTime() : 0;
|
||||
|
||||
// 初始化已合并的最后一个临时结果文件的监测时间,用于判断是否出现未生成监测数据的周期
|
||||
// 监测设置下发时,记录该监测的计划启动时间
|
||||
// NC重启时,记录当前时间(周期启动监测由NC控制,若NC重启后存在未合并的临时结果,忽略该时间段内未生成监测数据的周期)
|
||||
if(setInfo.getLastMergeDetecTime() == null) {
|
||||
setInfo.setLastMergeDetecTime(Math.max(System.currentTimeMillis(), planTime));
|
||||
}
|
||||
if(setInfo.getPlanCheckTime() == null || setInfo.getPlanCheckTime().longValue() == 0) {
|
||||
// GetRunInfo.startTime
|
||||
setInfo.setPlanCheckTime(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
pluginDetecSetInfoMap.put(key, setInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取三方监测设置集
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static Collection<SetInfo> getPluginDetecSetInfos() {
|
||||
return pluginDetecSetInfoMap.values();
|
||||
}
|
||||
|
||||
public static void putAllDetecAlarmInfo(Map<Long, List<AlarmInfo>> alarmMap) {
|
||||
detecAlarmInfoMap.putAll(alarmMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 监测信息报警相关信息
|
||||
* @param setInfoId
|
||||
* @return
|
||||
*/
|
||||
public static List<AlarmInfo> getDetecAlarmInfo(Long setInfoId) {
|
||||
return detecAlarmInfoMap.get(setInfoId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务
|
||||
*/
|
||||
public static ScheduledFuture<?> getTaskFuture(Long key) {
|
||||
synchronized (taskFutureMap) {
|
||||
Object[] objects = taskFutureMap.get(key);
|
||||
if (objects != null && objects.length > 0 && objects[0] != null) {
|
||||
return (ScheduledFuture<?>) objects[0];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加任务
|
||||
*/
|
||||
public static void putTaskFuture(Long key, ScheduledFuture<?> value, LoopTaskThread loopTask) {
|
||||
synchronized (taskFutureMap) {
|
||||
taskFutureMap.put(key, new Object[] { value, loopTask });
|
||||
logger.info("添加任务 id:" + key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销任务
|
||||
*/
|
||||
public static void cancleTaskFuture(final Long key, long delayMs) {
|
||||
scheduled.schedule(new Runnable() {
|
||||
public void run() {
|
||||
synchronized (taskFutureMap) {
|
||||
// Thread.currentThread().setName("注销任务 id:" + key);
|
||||
Thread.currentThread().setName("Write Off Task ID:" + key);
|
||||
Object[] objects = taskFutureMap.get(key);
|
||||
if (objects!=null && objects.length>0 && objects[0]!=null) {
|
||||
ScheduledFuture<?> future = (ScheduledFuture<?>) objects[0];
|
||||
logger.info("任务状态: "
|
||||
+ ((future.isDone() || future
|
||||
.isCancelled()) ? "已停止" : "运行中"));
|
||||
if (objects.length > 1 && objects[1] != null) {
|
||||
LoopTaskThread loopTask = (LoopTaskThread) objects[1];
|
||||
loopTask.cancle();
|
||||
}
|
||||
future.cancel(true);
|
||||
taskFutureMap.remove(key);
|
||||
logger.info("注销成功");
|
||||
} else {
|
||||
logger.info("任务不存在");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}, delayMs, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从全局变量移除执行完成或者取消的任务(每次在上传发送失败的结果时检查并移除)
|
||||
*/
|
||||
public static void removeCancelAndDoneTaskFuture() {
|
||||
synchronized (taskFutureMap) {
|
||||
Iterator<Long> iterator = taskFutureMap.keySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Long key = iterator.next();
|
||||
Object[] objects = taskFutureMap.get(key);
|
||||
if (objects != null && objects.length > 0) {
|
||||
ScheduledFuture<?> future = (ScheduledFuture<?>) objects[0];
|
||||
if (future.isCancelled() || future.isDone()) {
|
||||
iterator.remove();
|
||||
logger.info("任务控制集 移除 id:" + key + " 状态: "
|
||||
+ ((future.isDone() || future
|
||||
.isCancelled()) ? "已停止" : "运行中"));
|
||||
}
|
||||
} else {
|
||||
iterator.remove();
|
||||
logger.info("任务控制集 移除 id:" + key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取存放预警信息集
|
||||
*/
|
||||
public static Map<Long, AlarmPO> getAlarmPOs() {
|
||||
synchronized (alarmPOs) {
|
||||
return alarmPOs;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消某一监测类型的主动预警
|
||||
*/
|
||||
public static void removeAlarmPO(Long key) {
|
||||
synchronized (alarmPOs) {
|
||||
if (alarmPOs.containsKey(key)) {
|
||||
AlarmPO alarmPO = alarmPOs.get(key);
|
||||
alarmPOs.remove(key);
|
||||
logger.info("主动预警集 移除 setId:" + key + " >> "
|
||||
+ alarmPO.getType() + "_" + alarmPO.getProcIden());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加或更新对某一监测类型的主动预警
|
||||
*/
|
||||
public static void addOrUpdateAlarmPO(AlarmPO alarmPO) {
|
||||
synchronized (alarmPOs) {
|
||||
Long key = alarmPO.getId();
|
||||
String infoMsg = "添加";
|
||||
|
||||
if (alarmPOs.containsKey(key)) {
|
||||
infoMsg = "更新";
|
||||
}
|
||||
|
||||
alarmPOs.put(key, alarmPO);
|
||||
|
||||
logger.info("主动预警集 " + infoMsg + " setId:" + key + " >> " + alarmPO.getType() + "_" + alarmPO.getProcIden());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得预设监测总数
|
||||
*/
|
||||
public static int getSysDetecCount() {
|
||||
synchronized (sysDetecFutrue) {
|
||||
return sysDetecFutrue.size();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用预设监测
|
||||
*/
|
||||
public static void stopSysDetec(SetInfo setInfo) {
|
||||
synchronized (sysDetecFutrue) {
|
||||
Long key = setInfo.getId();
|
||||
// String threadName = "预设监测_"
|
||||
String threadName = "Presupposition Monitoring_"
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null);
|
||||
|
||||
ScheduledFuture<?> future = sysDetecFutrue.get(key);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
sysDetecFutrue.remove(key);
|
||||
logger.info("预设监测线程 停用 setId:" + setInfo.getId() + " >> "
|
||||
+ threadName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加或更新系统预设监测
|
||||
* @param setInfo
|
||||
* @param alarmInfos
|
||||
*/
|
||||
public static void addOrUpdateSysDetec(SetInfo setInfo, List<AlarmInfo> alarmInfos) {
|
||||
synchronized (sysDetecFutrue) {
|
||||
Long key = setInfo.getId();
|
||||
String infoMsg = "添加";
|
||||
|
||||
ScheduledFuture<?> future = sysDetecFutrue.get(key);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
sysDetecFutrue.remove(key);
|
||||
|
||||
infoMsg = "更新";
|
||||
}
|
||||
|
||||
long delay = 0;
|
||||
Date startTime = new Date();
|
||||
if (setInfo.getPlanCheckTime() != null) {
|
||||
try {
|
||||
long gap = setInfo.getPlanCheckTime() - System.currentTimeMillis();
|
||||
if (gap > 0) {
|
||||
delay = gap;
|
||||
startTime = new Date(setInfo.getPlanCheckTime());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Please check whether the next test time is set correctly!", e);
|
||||
}
|
||||
}
|
||||
|
||||
// String threadName = "预设监测_"
|
||||
String threadName = "Presupposition Monitoring_"
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null);
|
||||
|
||||
future = Common.scheduled.scheduleAtFixedRate(new GetInfoRun(
|
||||
threadName, setInfo, startTime, alarmInfos), delay, setInfo
|
||||
.getCheckGap(), TimeUnit.MINUTES);
|
||||
sysDetecFutrue.put(key, future);
|
||||
|
||||
logger.info("预设监测线程 " + infoMsg + " setId:" + setInfo.getId() + " >> " + threadName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动三方监测
|
||||
*/
|
||||
public static void startPluginDetec(SetInfo setInfo) {
|
||||
// String threadName = "三方监测_"
|
||||
String threadName = "Three Party Monitoring_"
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null);
|
||||
|
||||
Common.scheduled.schedule(new StartPluginRun(setInfo, threadName), 0,
|
||||
TimeUnit.MILLISECONDS);
|
||||
|
||||
logger.info("三方监测 添加 setId:" + setInfo.getId() + " >> " + threadName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加定时启动的三方监测
|
||||
*/
|
||||
public static void putPluginDetecFuture(Long key, ScheduledFuture<?> future) {
|
||||
synchronized (pluginDetecFutrue) {
|
||||
pluginDetecFutrue.put(key, future);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止定时启动的三方监测任务
|
||||
*/
|
||||
public static void stopPluginDetecFuture(Long key, String threadName) {
|
||||
synchronized (pluginDetecFutrue) {
|
||||
ScheduledFuture<?> future = pluginDetecFutrue.get(key);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
sysDetecFutrue.remove(key);
|
||||
logger.info("三方监测 移除 setId:" + key + " >> " + threadName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查三方监测是否存在(NC周期启动、NC单次启动)
|
||||
*/
|
||||
public static boolean containPluginDetecFuture(Long key) {
|
||||
ScheduledFuture<?> future = pluginDetecFutrue.get(key);
|
||||
return (future != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用三方监测
|
||||
*/
|
||||
public static void stopPluginDetec(SetInfo setInfo) {
|
||||
// NC周期启动监测需要获取三方监测的关键字
|
||||
if("2".equals(setInfo.getIsControlStart())) {
|
||||
generateCommandAndKeyword(setInfo);
|
||||
}
|
||||
|
||||
Long key = setInfo.getId();
|
||||
// String threadName = "三方监测_"
|
||||
String threadName = "Three Party Monitoring_"
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null);
|
||||
synchronized (pluginDetecFutrue) {
|
||||
ScheduledFuture<?> future = pluginDetecFutrue.get(key);
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
sysDetecFutrue.remove(key);
|
||||
logger.info("三方监测 移除 setId:" + setInfo.getId() + " >> " + threadName);
|
||||
}
|
||||
}
|
||||
try {
|
||||
// 检查PID
|
||||
Object[] objArr = ProcessUtil.checkPidAndGetPid(setInfo.getProcessFile(), setInfo.getProcessSearchKeyCode());
|
||||
int isExistFlag = Integer.parseInt(objArr[0].toString());
|
||||
String pidInfo = objArr[1].toString();
|
||||
|
||||
if (isExistFlag == 0) {// 不存在
|
||||
logger.info("停用" + threadName + ":进程原本不存在,不用杀进程");
|
||||
} else if (isExistFlag == 1) {// 存在且只有一个进程,杀PID
|
||||
ProcessUtil.killProcess(pidInfo);
|
||||
logger.info("停用" + threadName + ":杀进程 PID:" + pidInfo);
|
||||
} else if (isExistFlag == 2) {// 找到多个进程,告警
|
||||
logger.info("停用" + threadName + ":" + pidInfo);
|
||||
// String alarmMsg = "停用三方监测进程:" + pidInfo;
|
||||
String alarmMsg = "Discontinuation Of The Three Party Monitoring Process:" + pidInfo;
|
||||
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo
|
||||
.getCheckTypeName(), setInfo.getProcessIden(),
|
||||
new Date(), new Date(), 1,
|
||||
Contants.DETECTION_STATUS_FAILURE, alarmMsg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Discontinuation of three party monitoring anomalies", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置三方监测中由Web管理的监测脚本的启动参数(针对NC启动的周期监测)<br/>
|
||||
* 生成三方监测的执行命令及查询关键字
|
||||
*
|
||||
* @param setInfo
|
||||
* @return 脚本启动命令
|
||||
*/
|
||||
public static String generateCommandAndKeyword(SetInfo setInfo) {
|
||||
String command = null;
|
||||
try {
|
||||
if ("2".equals(setInfo.getIsControlStart())) { // NC周期启动
|
||||
File scriptDir = new File(Contants.localPluginScriptPath);
|
||||
final String keyword = "_" + setInfo.getProcessIden() + ".";
|
||||
Collection<?> files = FileUtils.listFiles(scriptDir,
|
||||
FileFilterUtils.asFileFilter(new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
if(name.endsWith(".tp")) {
|
||||
return false; // 排除临时文件
|
||||
}
|
||||
return name.contains(keyword);
|
||||
}
|
||||
}), FalseFileFilter.FALSE);
|
||||
if (!files.isEmpty()) {
|
||||
File scriptFile = (File) files.iterator().next();
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.startsWith("Windows")) {
|
||||
command = scriptFile.getCanonicalPath();
|
||||
} else if (os.startsWith("Linux")) {
|
||||
command = "./ " + scriptFile.getCanonicalFile();
|
||||
}
|
||||
setInfo.setProcessPath(command); // 设置执行命令
|
||||
setInfo.setProcessSearchKeyCode(scriptFile.getName()); // 搜索关键字
|
||||
// 更新缓存中的监测设置
|
||||
Common.putPluginDetecSetInfo(setInfo.getId(), setInfo);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
public static int byteArrayToInt(byte[] b) {
|
||||
return b[3] & 0xFF |
|
||||
(b[2] & 0xFF) << 8 |
|
||||
(b[1] & 0xFF) << 16 |
|
||||
(b[0] & 0xFF) << 24;
|
||||
}
|
||||
|
||||
public static byte[] intToByteArray(int a) {
|
||||
return new byte[] {
|
||||
(byte) ((a >> 24) & 0xFF),
|
||||
(byte) ((a >> 16) & 0xFF),
|
||||
(byte) ((a >> 8) & 0xFF),
|
||||
(byte) (a & 0xFF)
|
||||
};
|
||||
}
|
||||
}
|
||||
307
src/com/nis/nmsclient/common/Contants.java
Normal file
307
src/com/nis/nmsclient/common/Contants.java
Normal file
@@ -0,0 +1,307 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
|
||||
|
||||
public class Contants {
|
||||
|
||||
public static final String SYSTEM_PATH; //NMSClient应用启动主目录
|
||||
|
||||
/** ========= SSLSocket相关配置参数,客户与服务两端交互数据信息 ========**/
|
||||
public static final String SOCKET_SERVER_HOST_KEY= "server_host";
|
||||
public static String SOCKET_SERVER_HOST;//服务器IP
|
||||
public static final Integer SOCKET_SERVER_PORT;//服务器端口
|
||||
public static final Integer SOCKET_AGENT_PORT;//客户端端口
|
||||
public static final Integer SOCKET_TIMEOUT_MINUTES;// Socket通信超时时间
|
||||
public static final String SSL_KEY_STORE;//key证书库文件
|
||||
public static final String SSL_TRUST_KEY_STORE;//认证证书库文件
|
||||
public static final String SSL_JSSE_TYPE = "TLS";//类型TLS、SSL
|
||||
public static final String SSL_KEYSTORE_TYPE = "JCEKS";//KeyStore的类型有:jceks、jks
|
||||
public static final String SSL_KEY_PRIVATE_PASS = "123456";
|
||||
public static final String SSL_KEY_STORE_PASS = "client";
|
||||
|
||||
public static final String keyPath;
|
||||
|
||||
/**=======================本地文件相关参数========================*/
|
||||
public static final String localDetecConfSuffix;// 监测设置信息文件名后缀
|
||||
public static final String localDetecConfPath;// 监测设置信息存放路径
|
||||
public static final String localDataCollection;// 监测数据存放集
|
||||
public static final String localDataFilePath;//------------监测数据存放路径
|
||||
public static final String localDataDonePath;//------------成功处理数据存放路径
|
||||
public static final String localDataErrorPath;//------------不完整数据0大小文件存放路径
|
||||
public static final String localBackupPath;// 备份文件存放目录
|
||||
public static final String localUploadsPath;// 推送文件存入目录
|
||||
public static final String localTaskPath;// 任务相关信息存放目录
|
||||
public static final String localTaskDonePath;// ------------成功处理任务存放目录
|
||||
public static final String localTaskErrorPath;// -----------不完整0大小回传文件存放目录
|
||||
public static final String localTaskResultPath;// ------------任务结果存放目录
|
||||
public static final String localTaskReturnPath;// ------------任务回传文件存放目录
|
||||
public static final String localLogsPath;// 日志存放路径
|
||||
public static final String localTempPath;// 临时文件存放目录
|
||||
public static final String localTempDataIncomingPath; // 第三方监测临时文件存放目录
|
||||
public static final String localAgentPidFile;// Agent自身进程PID存放文件
|
||||
public static final String localPluginScriptPath;// 第三方监测脚本存放目录
|
||||
public static final String LOCAL_SCRIPT_PATH;
|
||||
|
||||
/**=======================系统预计监测类型========================*/
|
||||
public static final String SYS_CHECK_TYPE_CPU;
|
||||
public static final String SYS_CHECK_TYPE_MEMORY;
|
||||
public static final String SYS_CHECK_TYPE_DISK;
|
||||
public static final String SYS_CHECK_TYPE_NET;
|
||||
public static final String SYS_CHECK_TYPE_SYSDATE;
|
||||
public static final String SYS_CHECK_TYPE_PROCESS;
|
||||
public static final String SYS_CHECK_TYPE_PROCESS_NMSAGENT;
|
||||
public static final String SYS_CHECK_TYPE_SYSTEMINFO;
|
||||
|
||||
/** =====================Common时间时隔==================== **/
|
||||
// ------------ 清除本地文件
|
||||
public static final Integer COMMON_DEL_LOG_DAYS;// 删除日志文件间隔时间
|
||||
public static final Integer COMMON_DEL_DATA_HOURS;// 删除数据文件间隔时间
|
||||
public static final Integer COMMON_DEL_TASK_HOURS;// 删除任务相关文件间隔时间
|
||||
public static final Integer COMMON_DEL_UPGRADEFILE_DAYS;// 删除升级文件间隔时间
|
||||
public static final Integer COMMON_DEL_TEMP_DAYS;// 删除临时文件间隔时间
|
||||
public static String[] COMMON_DEL_PATH_INCLUDE;// 指定Agent可删除文件的范围
|
||||
public static String[] COMMON_DEL_PATH_EXCLUDE;// 指定Agent可删除文件范围内不可删除部分
|
||||
// ------------ 监测、任务、预警
|
||||
public static final Integer COMMON_ALARM_MINUTES = 5;// 主动告警轮循间隔时间
|
||||
public static final Integer COMMON_TASK_RESULT_SEND_MINUTES = 5;// 重发之前发送失败的任务执行结果间隔时间
|
||||
public static final Integer COMMON_TASK_INIT_DELAY_MINUTES = 2;// 启动时初始化任务请求延迟时间
|
||||
public static final Integer COMMON_UPLOAD_DATA_MINUTES;// 上传数据轮循间隔时间
|
||||
public static final Integer COMMON_TASK_CLEAR_HOURS;// 定时清理内存中已完成任务的间隔时间
|
||||
// ------------线程池
|
||||
public static final Integer COMMON_THREAD_SOCKET_SIZE ;// socket通信线程最大个数
|
||||
public static final Integer COMMON_THREAD_SCHEDULE_SIZE;// 定时任务线程最大个数
|
||||
// ------------打包上传
|
||||
public static final Integer COMMON_ZIP_MIN_SIZE;// 文件数越过一定值时压缩用
|
||||
public static final Integer COMMON_ZIP_MAX_SIZE;// 文件数越过一定值时压缩文件最多包含文件个数
|
||||
public static final Integer COMMON_MAX_RETURN_CNT;// 回传文件数越过一定值时压缩用
|
||||
// -----------任务结果、主动告警等信息中各字段的分隔符
|
||||
public static final String COMMON_MSG_SEPRATOR = "$@$";
|
||||
public static final String COMMON_MSG_SEPRATOR_SPLIT = "\\$@\\$";
|
||||
public static final int COMMON_MSG_SUCCESS = 0;
|
||||
public static final int COMMON_MSG_FAIL = 1;
|
||||
// -----------设置文件编码方式
|
||||
public static final String charset;
|
||||
|
||||
/** ========================告警状态常量========================== **/
|
||||
//用于报警: -1监测执行失败,0监测信息不正常,1监测信息正常,-2异常主动告警,2主动告警恢复正常)
|
||||
public static final int DETECTION_STATUS_FAILURE = -1;//监测执行失败
|
||||
public static final int DETECTION_STATUS_ABNORMAL = 0;//监测信息不正常
|
||||
public static final int DETECTION_STATUS_NORMAL = 1;//监测信息正常
|
||||
//public static final int DETECTION_ALARM_ABNORMAL = -2;//告警检查:监测线程异常(未取到数据,相应的文件找不到,或者连续N次都超过设置的告警值)
|
||||
//public static final int DETECTION_ALARM_NORMAL = 2;//告警检查:监测线程恢复正常
|
||||
|
||||
/** ========================任务部分文件后缀============================ **/
|
||||
public static final String TASK_RESULT_FILE_SUFFIX = ".result";
|
||||
public static final String TASK_RESULT_AGENTTMPFILE_SUFFIX = ".upgrade";
|
||||
public static final String TASK_RETURN_FILE_SUFFIX = ".return";
|
||||
|
||||
/** ========================Debug============================ **/
|
||||
public static final Integer DEBUG_INIT_TASK_FLAG;
|
||||
public static final Integer DEBUG_PLUGIN_FLAG;
|
||||
public static final Integer DEBUG_SYSDETECT_FLAG;
|
||||
public static final Integer DEBUG_UPLOADDATA_FLAG;
|
||||
public static final Integer DEBUG_ALARM_FLAG;
|
||||
public static final Integer DEBUG_DELFILE_FLAG;
|
||||
public static final Integer DEBUG_TASKRESULT_FLAG;
|
||||
public static final Integer DEBUG_TASKRETURN_FLAG;
|
||||
|
||||
//=================
|
||||
public static final int max_times = 5;// 失败后重试次数
|
||||
public static final long max_delay_seconds = 30;// 重试间隔,秒
|
||||
public static final int noDataTimes = 4;// 未取到数据的次数,用于主动告警
|
||||
public static final int overAlarmValTimes = 4;// 连续超过预警值的次数,用于主动告警
|
||||
//--------------Agent唯一标志UUID
|
||||
public static Long AGENT_HOST_UUID = null;
|
||||
public static String AGENT_OPERATE_SYSTEM = null;
|
||||
public static String AGENT_LOCAL_IP = null;
|
||||
|
||||
public static final String DETEC_STATE_INFO_FORMATE_POINT = "$@$";//用于监测数据的状态信息web界面显示的格式化的连接符
|
||||
|
||||
public static Boolean ACTIIVE_ALARM_START = false;//默认不启动主动告警
|
||||
public static String AGENT_INTERFACE_NAME_KEY = null;//网络端口名称
|
||||
/**
|
||||
* 监测数据主动上报
|
||||
*/
|
||||
//监测数据主动上报
|
||||
public static final int DATA_SEND_THREAD_FLAG;
|
||||
//主动数据上报 IP
|
||||
public static final String DATA_SEND_THREAD_HOST;
|
||||
//主动数据上报 PORT
|
||||
public static final int DATA_SEND_THREAD_PORT;
|
||||
//主动数据上报间隔 INTERVAL,单位 10 S
|
||||
public static final int DATA_SEND_THREAD_INTERVAL;
|
||||
|
||||
static{
|
||||
SYSTEM_PATH = SysConfig.getSystemDir();
|
||||
|
||||
// -------------------SSLSocket
|
||||
SOCKET_SERVER_HOST = SysConfig.getStringVal(SOCKET_SERVER_HOST_KEY);
|
||||
SOCKET_SERVER_PORT = SysConfig.getIntegerVal("server_port");
|
||||
SOCKET_AGENT_PORT = SysConfig.getIntegerVal("agent_port");
|
||||
SOCKET_TIMEOUT_MINUTES = SysConfig.getIntegerVal("socket.timeout.minutes", "30");
|
||||
SSL_KEY_STORE = formatPath(SysConfig.getStringVal("local.ssl.keys"));
|
||||
SSL_TRUST_KEY_STORE = formatPath(SysConfig.getStringVal("local.ssl.trust"));
|
||||
|
||||
keyPath = formatPath(SysConfig.getStringVal("local.ssl.path"));
|
||||
|
||||
/*=======================文件相关参数========================*/
|
||||
// 可删范围
|
||||
String path = SysConfig.getStringVal("common.del.path.include");
|
||||
if(path!=null && !"".equals(path)){
|
||||
COMMON_DEL_PATH_INCLUDE = path.split(",");
|
||||
for(int i=0; i<COMMON_DEL_PATH_INCLUDE.length; i++){
|
||||
COMMON_DEL_PATH_INCLUDE[i] = FileUtil.handlerPath(COMMON_DEL_PATH_INCLUDE[i]);
|
||||
}
|
||||
}
|
||||
// 禁删范围
|
||||
path = SysConfig.getStringVal("common.del.path.exclude");
|
||||
if(path!=null && !"".equals(path)){
|
||||
COMMON_DEL_PATH_EXCLUDE = path.split(",");
|
||||
for(int i=0; i<COMMON_DEL_PATH_EXCLUDE.length; i++){
|
||||
COMMON_DEL_PATH_EXCLUDE[i] = FileUtil.handlerPath(COMMON_DEL_PATH_EXCLUDE[i]);
|
||||
}
|
||||
}
|
||||
// 文件总路径
|
||||
String localFilePath = SysConfig.getStringVal("local.data.path");
|
||||
// ---------------Local Path
|
||||
LOCAL_SCRIPT_PATH = formatPath(SysConfig.getStringVal("local.script.path"));
|
||||
localDetecConfSuffix = SysConfig.getStringVal("local.config.file.suffix");
|
||||
localAgentPidFile = formatPath(SysConfig.getStringVal("local.agent.pidfile"));
|
||||
localDetecConfPath = localFilePath + File.separator + "nc_config";
|
||||
localPluginScriptPath = localFilePath + File.separator + "nc_detecScript";
|
||||
localDataCollection = localFilePath + File.separator + "nc_data";
|
||||
localDataFilePath = localDataCollection + File.separator + "incoming";
|
||||
localDataDonePath = localDataCollection + File.separator + "done";
|
||||
localDataErrorPath = localDataCollection + File.separator + "error";
|
||||
localBackupPath = localFilePath + File.separator + "nc_backup";
|
||||
localUploadsPath = localFilePath + File.separator + "nc_uploads";
|
||||
localTaskPath = localFilePath + File.separator + "nc_task";
|
||||
localTaskDonePath = localTaskPath + File.separator + "done";// 成功处理任务存放目录
|
||||
localTaskErrorPath = localTaskPath + File.separator + "error";// 成功处理任务存放目录
|
||||
localTaskResultPath = localTaskPath + File.separator + "incoming" + File.separator + "result";//任务结果存放目录
|
||||
localTaskReturnPath = localTaskPath + File.separator + "incoming" + File.separator + "return";//任务回传文件存放目录
|
||||
localLogsPath = formatPath(SysConfig.getLogPath());
|
||||
localTempPath = localFilePath + File.separator + "nc_temp";
|
||||
localTempDataIncomingPath = localDataCollection + File.separator + "temp";
|
||||
|
||||
// ---------------字符编码
|
||||
charset = SysConfig.getStringVal("charset");
|
||||
|
||||
// ---------------System Check Type
|
||||
SYS_CHECK_TYPE_CPU = SysConfig.getStringVal("sys.check.type.cpu", "cpu");
|
||||
SYS_CHECK_TYPE_MEMORY = SysConfig.getStringVal("sys.check.type.memory", "memory");
|
||||
SYS_CHECK_TYPE_DISK = SysConfig.getStringVal("sys.check.type.disk", "disk");
|
||||
SYS_CHECK_TYPE_NET = SysConfig.getStringVal("sys.check.type.net", "net");
|
||||
SYS_CHECK_TYPE_SYSDATE = SysConfig.getStringVal("sys.check.type.systemdate", "systemdate");
|
||||
SYS_CHECK_TYPE_PROCESS = SysConfig.getStringVal("sys.check.type.process", "process");
|
||||
SYS_CHECK_TYPE_PROCESS_NMSAGENT = SysConfig.getStringVal("sys.check.type.process.nmsagent", "nmsclient");
|
||||
SYS_CHECK_TYPE_SYSTEMINFO = SysConfig.getStringVal("sys.check.type.systeminfo", "systeminfo");
|
||||
|
||||
// --------------Common Clear Gaps
|
||||
COMMON_DEL_LOG_DAYS = SysConfig.getIntegerVal("common.del.log.days", "7");
|
||||
COMMON_DEL_DATA_HOURS = SysConfig.getIntegerVal("common.del.data.hours", "24");
|
||||
// 2013-5-20 添加功能 删除任务相关信息最小时间,不得少于一天
|
||||
if(COMMON_DEL_DATA_HOURS > 24){
|
||||
COMMON_DEL_TASK_HOURS = COMMON_DEL_DATA_HOURS;
|
||||
}else{
|
||||
COMMON_DEL_TASK_HOURS = 24;
|
||||
}
|
||||
COMMON_DEL_UPGRADEFILE_DAYS = SysConfig.getIntegerVal("common.del.upgradefile.days", "30");
|
||||
COMMON_DEL_TEMP_DAYS = SysConfig.getIntegerVal("common.del.temp.days", "2");
|
||||
COMMON_UPLOAD_DATA_MINUTES = SysConfig.getIntegerVal("common.upload.data.minutes", "5");
|
||||
COMMON_TASK_CLEAR_HOURS = SysConfig.getIntegerVal("common.task.clear.hours", "2");
|
||||
|
||||
// -------------- ThreadPool
|
||||
COMMON_THREAD_SOCKET_SIZE = SysConfig.getIntegerVal("common.thread.socket.size", "10");
|
||||
COMMON_THREAD_SCHEDULE_SIZE = SysConfig.getIntegerVal("common.thread.schedule.size", "15");
|
||||
// -------------- Compress
|
||||
COMMON_ZIP_MIN_SIZE = SysConfig.getIntegerVal("common.zip.min.size", "1000");
|
||||
COMMON_ZIP_MAX_SIZE = SysConfig.getIntegerVal("common.zip.max.size", "2000");
|
||||
COMMON_MAX_RETURN_CNT = SysConfig.getIntegerVal("common.max.return.size", "10");
|
||||
// ----------------Debug
|
||||
DEBUG_INIT_TASK_FLAG = SysConfig.getIntegerVal("debug.init.task.flag", "0");
|
||||
DEBUG_PLUGIN_FLAG = SysConfig.getIntegerVal("debug.plugin.flag", "0");
|
||||
DEBUG_SYSDETECT_FLAG = SysConfig.getIntegerVal("debug.sysdetect.flag", "0");
|
||||
DEBUG_UPLOADDATA_FLAG = SysConfig.getIntegerVal("debug.uploaddata.flag", "0");
|
||||
DEBUG_ALARM_FLAG = SysConfig.getIntegerVal("debug.alarm.flag", "0");
|
||||
DEBUG_DELFILE_FLAG = SysConfig.getIntegerVal("debug.delfile.flag", "0");
|
||||
DEBUG_TASKRESULT_FLAG = SysConfig.getIntegerVal("debug.taskresult.flag", "0");
|
||||
DEBUG_TASKRETURN_FLAG = SysConfig.getIntegerVal("debug.taskreturn.flag", "0");
|
||||
|
||||
// ACTIIVE_ALARM_START = SysConfig.getStringVal("active.alarm.start", "true");//是否启用主动告警,默认不启动主动告警---用于nc配置文件,现修改为从web控制
|
||||
|
||||
|
||||
//监测数据主动上报
|
||||
DATA_SEND_THREAD_FLAG = SysConfig.getIntegerVal("data.send.thread.flag", "0");
|
||||
//主动数据上报 IP
|
||||
DATA_SEND_THREAD_HOST = SysConfig.getStringVal("data.send.thread.host", SOCKET_SERVER_HOST);
|
||||
//主动数据上报 PORT
|
||||
DATA_SEND_THREAD_PORT = SysConfig.getIntegerVal("data.send.thread.port", "9527");
|
||||
//主动数据上报间隔 INTERVAL,单位 10 S
|
||||
DATA_SEND_THREAD_INTERVAL = SysConfig.getIntegerVal("data.send.thread.interval", "10");
|
||||
|
||||
|
||||
// 初始化创建文件夹
|
||||
if(!new File(localDetecConfPath).exists()){
|
||||
new File(localDetecConfPath).mkdirs();
|
||||
}
|
||||
if(!new File(localPluginScriptPath).exists()) {
|
||||
new File(localPluginScriptPath).mkdirs();
|
||||
}
|
||||
if(!new File(localDataCollection).exists()){
|
||||
new File(localDataCollection).mkdirs();
|
||||
}
|
||||
if(!new File(localBackupPath).exists()){
|
||||
new File(localBackupPath).mkdirs();
|
||||
}
|
||||
if(!new File(localUploadsPath).exists()){
|
||||
new File(localUploadsPath).mkdirs();
|
||||
}
|
||||
if(!new File(localTaskPath).exists()){
|
||||
new File(localTaskPath).mkdirs();
|
||||
}
|
||||
if(!new File(localTempPath).exists()){
|
||||
new File(localTempPath).mkdirs();
|
||||
}
|
||||
if(!new File(localTempDataIncomingPath).exists()){
|
||||
new File(localTempDataIncomingPath).mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatPath(String path){
|
||||
String returnPath = path;
|
||||
if(path!=null && !"".equals(path) && !new File(path).isAbsolute()){// 路径不为空且是相对路径
|
||||
returnPath = SYSTEM_PATH + File.separator + path;
|
||||
}
|
||||
return returnPath;
|
||||
}
|
||||
|
||||
public static boolean isSucessByResult(String msg){
|
||||
boolean flag = false;
|
||||
if (!StringUtils.isEmpty(msg)) {
|
||||
String[] result = msg.split(Contants.COMMON_MSG_SEPRATOR_SPLIT);
|
||||
if (result != null && result.length > 0) {
|
||||
if (Integer.parseInt(result[0])==Contants.COMMON_MSG_SUCCESS) {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static String getDescByResult(String msg){
|
||||
String desc = null;
|
||||
if (!StringUtils.isEmpty(msg)) {
|
||||
String[] result = msg.split(Contants.COMMON_MSG_SEPRATOR_SPLIT);
|
||||
if (result != null && result.length > 1) {
|
||||
desc = result[1];
|
||||
}
|
||||
}
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
152
src/com/nis/nmsclient/common/StopWatch.java
Normal file
152
src/com/nis/nmsclient/common/StopWatch.java
Normal file
@@ -0,0 +1,152 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
/**
|
||||
* 秒表计时器
|
||||
* @author fang
|
||||
*
|
||||
*/
|
||||
public class StopWatch {
|
||||
private static final long SEC_MILL = 1000;
|
||||
private static final long MIN_MILL = 60 * SEC_MILL;
|
||||
private static final long HOUR_MILL = 60 * MIN_MILL;
|
||||
private static final long DAY_MILL = 24 * HOUR_MILL;
|
||||
private long start;
|
||||
private long end;
|
||||
private LinkedHashMap<String,Long> tagMap = new LinkedHashMap<String,Long>();
|
||||
|
||||
public StopWatch(){
|
||||
start();
|
||||
}
|
||||
|
||||
public static StopWatch newStopWacth(){
|
||||
return new StopWatch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计时器开始
|
||||
* @return
|
||||
*/
|
||||
public long start(){
|
||||
this.start = System.currentTimeMillis();
|
||||
return start;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计时器结束
|
||||
* @return
|
||||
*/
|
||||
public long end(){
|
||||
this.end = System.currentTimeMillis();
|
||||
return end;
|
||||
}
|
||||
|
||||
|
||||
public long tag(String tag){
|
||||
long l = System.currentTimeMillis();
|
||||
this.tagMap.put(tag, l);
|
||||
return l;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算两个 tag 之间的时间差
|
||||
* @param b
|
||||
* @param a
|
||||
* @return
|
||||
*/
|
||||
public long between(String b,String a){
|
||||
Long l1 = this.tagMap.get(b);
|
||||
Long l2 = this.tagMap.get(a);
|
||||
if(l1 != null && l2 != null){
|
||||
return l1-l2;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static String toString(long l){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if(l >= DAY_MILL){
|
||||
sb.append((l/DAY_MILL));
|
||||
sb.append( "天");
|
||||
l = l % DAY_MILL;
|
||||
}
|
||||
if(l >= HOUR_MILL){
|
||||
sb.append((l/HOUR_MILL));
|
||||
sb.append( "小时");
|
||||
l = l % HOUR_MILL;
|
||||
}
|
||||
if(l >= MIN_MILL){
|
||||
sb.append((l/MIN_MILL));
|
||||
sb.append( "分");
|
||||
l = l % MIN_MILL;
|
||||
}
|
||||
if(l >= SEC_MILL){
|
||||
sb.append((l/SEC_MILL));
|
||||
sb.append( "秒");
|
||||
l = l % SEC_MILL;
|
||||
}
|
||||
|
||||
sb.append((l));
|
||||
sb.append( "毫秒");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从开始到结束总耗时
|
||||
* @return
|
||||
*/
|
||||
public long total(){
|
||||
long temp = System.currentTimeMillis();
|
||||
if(this.end < this.start){
|
||||
this.end = temp;
|
||||
}
|
||||
return end - start;
|
||||
}
|
||||
|
||||
|
||||
public void reset(){
|
||||
this.tagMap.clear();
|
||||
this.start();
|
||||
}
|
||||
|
||||
public long getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public void setStart(long start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
public long getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public void setEnd(long end) {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, Long> getTag() {
|
||||
return tagMap;
|
||||
}
|
||||
|
||||
public void LinkedHashMap(LinkedHashMap<String, Long> tag) {
|
||||
this.tagMap = tag;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
long s = System.currentTimeMillis();
|
||||
long end = s +2*DAY_MILL+ 12 * MIN_MILL + 30*SEC_MILL + 388;
|
||||
|
||||
String string = StopWatch.toString(end -s);
|
||||
System.out.println(string);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
365
src/com/nis/nmsclient/common/SysConfig.java
Normal file
365
src/com/nis/nmsclient/common/SysConfig.java
Normal file
@@ -0,0 +1,365 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
|
||||
public class SysConfig {
|
||||
static Logger logger = Logger.getLogger(SysConfig.class);
|
||||
|
||||
/** ==============myconfig.properties文件获取参数=============== **/
|
||||
private static Properties myProperties;
|
||||
private static String url = null;
|
||||
static {
|
||||
URL urlObj = SysConfig.class.getClassLoader().getResource("myconfig.properties");
|
||||
if(urlObj==null){
|
||||
// JOptionPane.showMessageDialog(null, "缺少配置文件,程序无法执行!\n请先执行参数配置程序进行配置", "错误", JOptionPane.ERROR_MESSAGE);
|
||||
JOptionPane.showMessageDialog(null, "i18n_client.Sysconfig.init_n81i", "i18n_client.Sysconfig.error_n81i", JOptionPane.ERROR_MESSAGE);
|
||||
logger.error("NMSClient program termination: lack of configuration files, programs can not be executed! Please execute the configuration program for configuration first");
|
||||
System.exit(0);
|
||||
}else{
|
||||
url = urlObj.getPath().replaceAll("%20", " ");
|
||||
}
|
||||
myProperties = new Properties();
|
||||
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(url);
|
||||
myProperties.load(fis);
|
||||
} catch (IOException e) {
|
||||
logger.error("Reading myconfig.properties file error", e);
|
||||
}finally{
|
||||
try {
|
||||
if(fis!= null)fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 升级时更新参数配置使用
|
||||
updateConfigFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* 向资源配置文件host_uuid.properties中更新UUID键值
|
||||
*/
|
||||
public static void setInterfaceNameValue(String value) {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
if (uuidUrl != null && !"".equals(uuidUrl)) {
|
||||
properties.load(new FileInputStream(uuidUrl));
|
||||
// 添加或更新键值对
|
||||
properties.setProperty(AGENT_INTERFACE_NAME_KEY, value);
|
||||
// 保存到文件
|
||||
properties.store(new FileOutputStream(uuidUrl), "");
|
||||
}
|
||||
properties.clear();
|
||||
|
||||
if(value != null && !"".equals(value)){
|
||||
Contants.AGENT_INTERFACE_NAME_KEY = value;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Setting the network port name eth* attribute error", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据相应的参数配置来更新配置并写入文件
|
||||
*/
|
||||
private static void updateConfigFile(){
|
||||
FileInputStream fis = null;
|
||||
BufferedReader reader = null;
|
||||
BufferedWriter writer = null;
|
||||
try {
|
||||
ResourceBundle resource = ResourceBundle.getBundle(UpdateParams.class.getName());
|
||||
//判断是否更新properties
|
||||
String updateFlag = myProperties.getProperty(UpdateParams.CONFIG_UPDATE_FLAG,"-1");
|
||||
if(updateFlag.equals(resource.getString(UpdateParams.CONFIG_UPDATE_FLAG))){ //配置文件已经更新,退出操作
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> proList = new LinkedList<String>();
|
||||
|
||||
String encode = System.getProperty("file.encoding");
|
||||
logger.debug("----file.encoding----" + encode);
|
||||
|
||||
//读取配置文件原有的参数到proList
|
||||
fis = new FileInputStream(url);
|
||||
reader = new BufferedReader(new InputStreamReader(fis,Charset.forName(encode)));
|
||||
String str =null;
|
||||
while((str = reader.readLine() )!=null){
|
||||
proList.add(str);
|
||||
}
|
||||
|
||||
//将UpdateParams中的值更新到proList和myProperties中
|
||||
Enumeration<String> en = resource.getKeys();
|
||||
while (en.hasMoreElements()) {
|
||||
String elem = (String) en.nextElement();
|
||||
String value = resource.getString(elem);
|
||||
boolean addFlag = true;
|
||||
try {
|
||||
for (int i = 0; i < proList.size(); i++) {
|
||||
String strV = proList.get(i);
|
||||
if (StringUtils.isEmpty(strV)) {
|
||||
continue;
|
||||
}
|
||||
if(strV.split("=", 2)[0].trim().equals(elem)){
|
||||
if(elem.equalsIgnoreCase(UpdateParams.CONFIG_UPDATE_FLAG)){
|
||||
proList.set(i, elem + " = " + value);// 更新配置文件中某属性的值
|
||||
logger.info("参数更新:" + elem + " = " + (StringUtils.isBlank(value) ? "" : value));
|
||||
myProperties.put(elem, value);
|
||||
}
|
||||
addFlag = false;
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
if(addFlag){
|
||||
proList.add(elem + " = " + value);
|
||||
logger.info("参数新增:" + elem + " = " + (StringUtils.isBlank(value) ? "" : value));
|
||||
myProperties.put(elem, value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Update the configuration file myconfig.properties parameter " + elem + "error", e);
|
||||
}
|
||||
}
|
||||
|
||||
//将文件信息写入到文件中
|
||||
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url),Charset.forName(encode)));
|
||||
Iterator<String> it = proList.iterator();
|
||||
while (it.hasNext()) {
|
||||
String elem = (String) it.next();
|
||||
writer.write((elem==null?"":elem)+"\r\n");
|
||||
}
|
||||
writer.flush();
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Update configuration file myconfig.properties exception", e);
|
||||
} finally{
|
||||
try {
|
||||
if(reader!= null)reader.close();
|
||||
if(fis!= null)fis.close();
|
||||
if(writer!= null ){
|
||||
writer.close();
|
||||
writer = null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateConfigFile(String key, String value){
|
||||
FileInputStream fis = null;
|
||||
BufferedReader reader = null;
|
||||
BufferedWriter writer = null;
|
||||
try {
|
||||
List<String> proList = new LinkedList<String>();
|
||||
|
||||
String encode = System.getProperty("file.encoding");
|
||||
logger.debug("----file.encoding----" + encode);
|
||||
|
||||
//读取配置文件原有的参数到proList
|
||||
fis = new FileInputStream(url);
|
||||
reader = new BufferedReader(new InputStreamReader(fis,Charset.forName(encode)));
|
||||
String str =null;
|
||||
while((str = reader.readLine() )!=null){
|
||||
proList.add(str);
|
||||
}
|
||||
|
||||
//将值更新到proList中
|
||||
try {
|
||||
for (int i = 0; i < proList.size(); i++) {
|
||||
String strV = proList.get(i);
|
||||
if (StringUtils.isEmpty(strV)) {
|
||||
continue;
|
||||
}
|
||||
if(strV.split("=", 2)[0].trim().equals(key)){
|
||||
proList.set(i, key + " = " + value);// 更新配置文件中某属性的值
|
||||
logger.info("参数更新:" + key + " = " + (StringUtils.isBlank(value) ? "" : value));
|
||||
break ;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Update the configuration file myconfig.properties parameter " + key + "error", e);
|
||||
}
|
||||
|
||||
//将文件信息写入到文件中
|
||||
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url),Charset.forName(encode)));
|
||||
Iterator<String> it = proList.iterator();
|
||||
while (it.hasNext()) {
|
||||
String elem = (String) it.next();
|
||||
writer.write((elem==null?"":elem)+"\r\n");
|
||||
}
|
||||
writer.flush();
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Update configuration file myconfig.properties exception", e);
|
||||
} finally{
|
||||
try {
|
||||
if(reader!= null)reader.close();
|
||||
if(fis!= null)fis.close();
|
||||
if(writer!= null ){
|
||||
writer.close();
|
||||
writer = null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getStringVal(String key, String... defaultVal){
|
||||
String dStr = "";
|
||||
if(defaultVal!=null && defaultVal.length>=1 && defaultVal[0]!=null && defaultVal.length>0){
|
||||
dStr = defaultVal[0];
|
||||
}
|
||||
return myProperties.getProperty(key,dStr).trim();
|
||||
}
|
||||
|
||||
public static Integer getIntegerVal(String name, String... defaultVal){
|
||||
try {
|
||||
String val = getStringVal(name, defaultVal);
|
||||
return StringUtils.isEmpty(val)? null : Integer.parseInt(val);
|
||||
} catch (Exception e) {
|
||||
logger.error("Digital formatting error", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getSystemDir() {
|
||||
return System.getProperty("user.dir");
|
||||
}
|
||||
|
||||
public static String getLogPath(){
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
String log4jUrl = SysConfig.class.getClassLoader().getResource("log4j.properties").getPath().replaceAll("%20", " ");
|
||||
fis = new FileInputStream(log4jUrl);
|
||||
Properties log4jProperties = new Properties();
|
||||
log4jProperties.load(fis);
|
||||
String logFile = log4jProperties.getProperty("log4j.appender.logfile.File");
|
||||
if(logFile!=null){
|
||||
return new File(logFile).getParent();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("Reading log4j.properties file error"+"",e);
|
||||
}finally{
|
||||
try {
|
||||
if(fis!= null)fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return "../logs";
|
||||
}
|
||||
|
||||
/** ==============host_uuid.properties文件获取参数=============== **/
|
||||
static final String AGENT_HOST_UUID_KEY = "agent_host_uuid";
|
||||
static final String AGENT_OPERATE_SYSTEM_KEY = "agent_operate_system";
|
||||
static final String AGENT_LOCAL_IP_KEY = "agent_local_ip";
|
||||
static String uuidUrl = null;
|
||||
static final String AGENT_INTERFACE_NAME_KEY = "agent_interface_name";
|
||||
static {
|
||||
String name = "host_uuid.properties";
|
||||
// 2014-11-13 jzz modify 由于将该配置文件放于系统安装目录中容易使节点混乱,故将其放于nmsdata指定目录
|
||||
/*URL urlObj = Contants.class.getClassLoader().getResource(name);
|
||||
if(urlObj==null){
|
||||
uuidUrl = new File(url).getParent() + File.separator + name;
|
||||
}else{
|
||||
uuidUrl = urlObj.getPath().replaceAll("%20", " ");
|
||||
}*/
|
||||
String path = SysConfig.getStringVal("local.data.path") + File.separator + "nc_sysconf";
|
||||
File filePath = new File(path);
|
||||
if(!filePath.exists()){
|
||||
filePath.mkdirs();
|
||||
}
|
||||
|
||||
uuidUrl = path + File.separator + name;
|
||||
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
File file = new File(uuidUrl);
|
||||
if(!file.exists()){
|
||||
file.createNewFile();
|
||||
}
|
||||
properties.load(new FileInputStream(uuidUrl));
|
||||
String uuidStr = properties.getProperty(AGENT_HOST_UUID_KEY);
|
||||
if(uuidStr != null && !"".equals(uuidStr.trim())){
|
||||
Contants.AGENT_HOST_UUID = Long.parseLong(uuidStr.trim());
|
||||
}
|
||||
Contants.AGENT_OPERATE_SYSTEM = properties.getProperty(AGENT_OPERATE_SYSTEM_KEY);
|
||||
Contants.AGENT_LOCAL_IP = properties.getProperty(AGENT_LOCAL_IP_KEY);
|
||||
Contants.AGENT_INTERFACE_NAME_KEY = properties.getProperty(AGENT_INTERFACE_NAME_KEY);
|
||||
} catch (IOException e) {
|
||||
logger.error("Reading host_uuid.properties file error", e);
|
||||
}
|
||||
properties.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 向资源配置文件host_uuid.properties中更新UUID键值
|
||||
*/
|
||||
public static void setUUIDValue(String value) {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
if (uuidUrl != null && !"".equals(uuidUrl)) {
|
||||
properties.load(new FileInputStream(uuidUrl));
|
||||
// 添加或更新键值对
|
||||
properties.setProperty(AGENT_HOST_UUID_KEY, value);
|
||||
// 保存到文件
|
||||
properties.store(new FileOutputStream(uuidUrl), "");
|
||||
}
|
||||
properties.clear();
|
||||
|
||||
if(value != null && !"".equals(value)){
|
||||
Contants.AGENT_HOST_UUID = Long.parseLong(value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Setting the UUID attribute error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向资源配置文件host_uuid.properties中更新OperateSystem和LocalIP键值
|
||||
*/
|
||||
public static void setUUIDValue(String sysTypeValue,String localIpValue) {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
if (uuidUrl != null && !"".equals(uuidUrl)) {
|
||||
properties.load(new FileInputStream(uuidUrl));
|
||||
// 添加或更新键值对
|
||||
properties.setProperty(AGENT_OPERATE_SYSTEM_KEY, sysTypeValue);
|
||||
properties.setProperty(AGENT_LOCAL_IP_KEY, localIpValue);
|
||||
// 保存到文件
|
||||
properties.store(new FileOutputStream(uuidUrl), "");
|
||||
}
|
||||
properties.clear();
|
||||
Contants.AGENT_OPERATE_SYSTEM = sysTypeValue;
|
||||
Contants.AGENT_LOCAL_IP = localIpValue;
|
||||
} catch (Exception e) {
|
||||
logger.error("Setting OperateSystemType and LocalIP attribute errors",e);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/com/nis/nmsclient/common/UpdateParams.java
Normal file
18
src/com/nis/nmsclient/common/UpdateParams.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
public class UpdateParams extends java.util.ListResourceBundle {
|
||||
public static String CONFIG_UPDATE_FLAG = "config.update.flag"; //更新标示 固定 判断配置文件指定值更新 建议自增1操作
|
||||
public static String CONFIG_UPDATE_FLAG_VALUE = "5"; //更新标示 该值 缺省值为0 每次修改都要
|
||||
static final String[][] contents = new String[][]{
|
||||
{CONFIG_UPDATE_FLAG,CONFIG_UPDATE_FLAG_VALUE}, //更新标示 固定 判断配置文件指定值更新 建议自增1操作
|
||||
{"socket.timeout.minutes", "30"}, // Socket通信超时时间设置
|
||||
{"common.zip.min.size", "1000"}, // 压缩文件最少包含文件个数
|
||||
{"common.zip.max.size", "2000"}, // 压缩文件最多包含文件个数
|
||||
// {"active.alarm.start","false"}, //是否启用NC的主动告警(只针对监测数据超过设置的值时的主动告警,避免异常信息重复):true:启用,false:停用-暂不使用,修改为web端控制nc是否报主动告警
|
||||
{"alarm.set.marker.separator","|"} //监测数据设置告警时,对于指定多个标识符(如多个盘符、多个CPU、多个网卡)的分隔符
|
||||
};
|
||||
|
||||
public Object[][] getContents() {
|
||||
return contents;
|
||||
}
|
||||
}
|
||||
75
src/com/nis/nmsclient/common/VersionCfg.java
Normal file
75
src/com/nis/nmsclient/common/VersionCfg.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package com.nis.nmsclient.common;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* 获取和保存version信息的类
|
||||
*
|
||||
*/
|
||||
public class VersionCfg {
|
||||
private static Logger logger = Logger.getLogger(VersionCfg.class);
|
||||
public static final String NAGENT_VERSION = "NA_version";
|
||||
public static final String NSERVER_VERSION = "NS_version";
|
||||
|
||||
private static String url = null;
|
||||
private static Properties properties;
|
||||
|
||||
static {
|
||||
url = VersionCfg.class.getClassLoader().getResource("version.properties").getPath().replaceAll("%20", " ");
|
||||
|
||||
FileInputStream fis = null;
|
||||
properties = new Properties();
|
||||
try {
|
||||
fis = new FileInputStream(url);
|
||||
properties.load(fis);
|
||||
} catch (IOException e) {
|
||||
logger.error("Reading version.properties file error", e);
|
||||
}finally{
|
||||
try{
|
||||
if(fis!=null){
|
||||
fis.close();
|
||||
}
|
||||
}catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取version值
|
||||
*
|
||||
*/
|
||||
public static String getValue(String key) {
|
||||
return properties.getProperty(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向资源配置文件中添加或更新version键值对
|
||||
*/
|
||||
public static void setValue(String key, String value) {
|
||||
logger.debug("setVersion----->" + key + "=" + value);
|
||||
// 添加或更新键值对
|
||||
properties.setProperty(key, value);
|
||||
logger.debug("properties.getProperty(\"" + key + "\")----->" + value);
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(url);
|
||||
// 保存到文件
|
||||
if (url != null && !"".equals(url)) {
|
||||
properties.store(fos, "");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e);
|
||||
} finally{
|
||||
try{
|
||||
if(fos!=null){
|
||||
fos.flush();
|
||||
fos.close();
|
||||
}
|
||||
}catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
58
src/com/nis/nmsclient/config/DetecConfOper.java
Normal file
58
src/com/nis/nmsclient/config/DetecConfOper.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package com.nis.nmsclient.config;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.model.SetInfo;
|
||||
|
||||
public class DetecConfOper {
|
||||
static Logger logger = Logger.getLogger(DetecConfOper.class);
|
||||
|
||||
public static File getConfigFile(File dir, String fileName) {
|
||||
File tmp = null;
|
||||
if (dir.isDirectory()) {
|
||||
for (File f : dir.listFiles()) {
|
||||
if (!f.isDirectory()) {
|
||||
if (f.getName().equals(fileName)) {
|
||||
tmp = f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static String getFileName(String checkType, String processIden,
|
||||
String suffix) {
|
||||
String fileName = "";
|
||||
if (checkType != null && !"".equals(checkType)) {
|
||||
fileName += checkType;
|
||||
}
|
||||
if (processIden != null && !"".equals(processIden)) {
|
||||
fileName += "_" + processIden;
|
||||
}
|
||||
if (suffix != null && !"".equals(suffix)) {
|
||||
fileName += suffix;
|
||||
}
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public static boolean isProcess(SetInfo setInfo){
|
||||
boolean flag = false;
|
||||
if("1".equals(setInfo.getIsSchedule())){//非预设监测
|
||||
flag = true;
|
||||
}else if(Contants.SYS_CHECK_TYPE_PROCESS.equalsIgnoreCase(setInfo.getCheckTypeName())){
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static String getProcess(SetInfo setInfo){
|
||||
return setInfo.getProcessPath();// + File.separator + setInfo.getProcessFile();
|
||||
}
|
||||
|
||||
}
|
||||
297
src/com/nis/nmsclient/config/DetecConfReqHandle.java
Normal file
297
src/com/nis/nmsclient/config/DetecConfReqHandle.java
Normal file
@@ -0,0 +1,297 @@
|
||||
package com.nis.nmsclient.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.model.AlarmInfo;
|
||||
import com.nis.nmsclient.model.SetInfo;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmUtil;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.FileWrUtil;
|
||||
import com.nis.nmsclient.util.ProcessUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
public class DetecConfReqHandle {
|
||||
Logger logger = Logger.getLogger(DetecConfReqHandle.class);
|
||||
|
||||
/**
|
||||
* 初始化监测配置
|
||||
* 处理接收到监测配置信息:解析成自定义的对象
|
||||
* @param str JSONObject串
|
||||
*/
|
||||
public void handlerConfigByInit(String str, List<SetInfo> setInfos, Map<Long, List<AlarmInfo>> alarmInfos) {
|
||||
// -- 参数处理
|
||||
if (str == null || str.trim().length() <= 0) {
|
||||
return;
|
||||
}
|
||||
if(setInfos==null || alarmInfos == null){
|
||||
return;
|
||||
}
|
||||
setInfos.clear();
|
||||
alarmInfos.clear();
|
||||
|
||||
// -- 初始化配置环境处理
|
||||
File file = new File(Contants.localDetecConfPath);
|
||||
if (!file.exists()) {// 判断存放目录是否存在,不存创建
|
||||
file.mkdirs();
|
||||
}
|
||||
File[] cfgFiles = file.listFiles();
|
||||
for (int i = 0; i < cfgFiles.length; i++) {//清空所有配置文件
|
||||
// 使用删除文件公共方法
|
||||
FileUtil.delDir(cfgFiles[i]);
|
||||
logger.debug("handlerConfigByInit()--delete file=" + cfgFiles[i].getAbsolutePath());
|
||||
}
|
||||
|
||||
// -- 监测配置解析
|
||||
JSONObject jsonObj = JSONObject.fromObject(str);
|
||||
|
||||
List<SetInfo> setList = null;
|
||||
if (str.toLowerCase().contains("setinfo")) {
|
||||
JSONArray jsonArr = jsonObj.getJSONArray("setInfo");
|
||||
setList = handerSetInfo(jsonArr);
|
||||
}
|
||||
if (setList == null || setList.size() == 0) {
|
||||
return;
|
||||
}
|
||||
Map<Long, List<AlarmInfo>> alarmMap = null;
|
||||
if (str.toLowerCase().contains("alarminfo")) {
|
||||
JSONArray jsonArr = jsonObj.getJSONArray("alarmInfo");
|
||||
alarmMap = handerAlarmInfo(jsonArr, setList);
|
||||
}
|
||||
// 20170413 zbc 缓存AlarmInfo信息(cfg文件中无报警单位字段)
|
||||
if(alarmMap != null) {
|
||||
Common.putAllDetecAlarmInfo(alarmMap);
|
||||
}
|
||||
|
||||
for (SetInfo setInfo : setList) {
|
||||
if ("1".equals(setInfo.getCheckState())
|
||||
&& "1".equals(setInfo.getCheckWay())) {// 有效,被动方式获取
|
||||
setInfos.add(setInfo);
|
||||
Common.addOrUpdateAlarmPO(AlarmUtil.getAlarmPO(setInfo));
|
||||
}
|
||||
}
|
||||
if (alarmMap != null && alarmMap.size()>0) {
|
||||
alarmInfos.putAll(alarmMap);
|
||||
}
|
||||
|
||||
if (str.toLowerCase().contains("showautoalarm")) {
|
||||
Object obj = jsonObj.get("showAutoAlarm");
|
||||
if (obj != null) {
|
||||
Contants.ACTIIVE_ALARM_START = (Boolean) obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新监测配置:先解析成自定义的对象,再根据对象信息添加、更新、停用监测
|
||||
* 注:三方监测停用时Agent根据指定的PID杀掉进程
|
||||
* @param str JSONObject串
|
||||
*/
|
||||
public void handlerConfigByUpdate(String str) {
|
||||
if (str == null || str.trim().length() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(Contants.localDetecConfPath);
|
||||
if (!file.exists()) {// 判断存放目录是否存在,不存创建
|
||||
file.mkdirs();
|
||||
}
|
||||
|
||||
JSONObject jsonObj = JSONObject.fromObject(str);
|
||||
/*long flag = -1;
|
||||
if (str.toLowerCase().contains("number")) {
|
||||
flag = jsonObj.getLong("number");
|
||||
}*/
|
||||
List<SetInfo> setList = null;
|
||||
if (str.toLowerCase().contains("setinfo")) {
|
||||
JSONArray jsonArr = jsonObj.getJSONArray("setInfo");
|
||||
setList = handerSetInfo(jsonArr);
|
||||
}
|
||||
if (setList == null || setList.size() == 0) {
|
||||
return;
|
||||
}
|
||||
Map<Long, List<AlarmInfo>> alarmMap = null;
|
||||
if (str.toLowerCase().contains("alarminfo")) {
|
||||
JSONArray jsonArr = jsonObj.getJSONArray("alarmInfo");
|
||||
alarmMap = handerAlarmInfo(jsonArr, setList);
|
||||
}
|
||||
|
||||
for (SetInfo setInfo : setList) {
|
||||
if ("0".equals(setInfo.getCheckState())
|
||||
|| "0".equals(setInfo.getCheckWay())) {// 将有效置为无效,或者将被动设为主动,停止线程
|
||||
Common.removeAlarmPO(setInfo.getId());
|
||||
if ("0".equals(setInfo.getIsSchedule())) {
|
||||
Common.stopSysDetec(setInfo);
|
||||
} else {
|
||||
Common.stopPluginDetec(setInfo);
|
||||
}
|
||||
} else {
|
||||
Common.addOrUpdateAlarmPO(AlarmUtil.getAlarmPO(setInfo));
|
||||
if ("0".equals(setInfo.getIsSchedule())) {// 如果是系统预设监测类型,则修改相应的监控线程
|
||||
List<AlarmInfo> alarmList = null;
|
||||
if (alarmMap != null) {
|
||||
alarmList = alarmMap.get(setInfo.getId());
|
||||
}
|
||||
Common.addOrUpdateSysDetec(setInfo, alarmList);
|
||||
} else { // 第三方监测
|
||||
// 缓存三方监测配置信息,用于合并临时结果文件
|
||||
Common.putPluginDetecSetInfo(setInfo.getId(), setInfo);
|
||||
|
||||
if(!Common.COMMON_SYS_SETINFO.equals(setInfo.getIsSchedule())) { // 由Agent启动的第三方插件
|
||||
Common.startPluginDetec(setInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}// for end
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析监测设置基本信息
|
||||
* 1、相应类型的监测数据目录不存在,创建,若是Linux并赋读写权限
|
||||
* 2、监测配置文件存在,先删除再重写,停用监测不再重写
|
||||
*/
|
||||
private List<SetInfo> handerSetInfo(JSONArray jsonArr) {
|
||||
// 文件路径
|
||||
List<SetInfo> setList = new ArrayList<SetInfo>();
|
||||
for (int i = 0; i < jsonArr.size(); i++) {
|
||||
Object obj = JSONObject.toBean(JSONObject
|
||||
.fromObject(jsonArr.get(i)), SetInfo.class);
|
||||
if (obj == null) {
|
||||
continue;
|
||||
}
|
||||
SetInfo setInfo = (SetInfo) obj;
|
||||
// 创建建相应类型的数据文件夹
|
||||
File dataDir = new File(Contants.localDataFilePath
|
||||
+ File.separator
|
||||
+ DetecConfOper.getFileName(setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), null));
|
||||
if (!dataDir.exists()) {
|
||||
dataDir.mkdirs();
|
||||
logger.debug("handerSetInfo()--create dataDir=" +dataDir.getAbsolutePath());
|
||||
}
|
||||
// 文件名
|
||||
String fileName = DetecConfOper.getFileName(
|
||||
setInfo.getCheckTypeName(), setInfo.getProcessIden(),
|
||||
Contants.localDetecConfSuffix);
|
||||
|
||||
// 判断文件是否存在,如果存在删除
|
||||
File cfgfile = new File(Contants.localDetecConfPath + File.separator
|
||||
+ fileName);
|
||||
if (cfgfile.exists()){
|
||||
//cfgfile.delete_bak();
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(cfgfile);
|
||||
logger.debug("handerSetInfo()--delete cfgfile=" + cfgfile.getAbsolutePath());
|
||||
//FileUtil.checkParentDirExist(cfgfile);
|
||||
}
|
||||
|
||||
if ("1".equals(setInfo.getCheckState())
|
||||
&& "1".equals(setInfo.getCheckWay())) {//有效且被动方式获取的写入配置文件
|
||||
String dataDirStr = null;
|
||||
try {// 为了去除路径中的"." 和 ".."
|
||||
dataDirStr = dataDir.getCanonicalPath();
|
||||
} catch (IOException e) {
|
||||
dataDirStr = dataDir.getAbsolutePath();
|
||||
}
|
||||
try {
|
||||
FileWrUtil.cfgFilePrinter(cfgfile, Contants.charset,
|
||||
setInfo.getValArr(Contants.AGENT_HOST_UUID, dataDirStr));
|
||||
logger.debug("handerSetInfo()--write cfgfile=" + cfgfile.getAbsolutePath());
|
||||
} catch (Exception e) {
|
||||
logger.error("Write the document“" + cfgfile.getAbsolutePath() + "”error: " + Utils.printExceptionStack(e));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
setList.add(setInfo);
|
||||
|
||||
}// for end
|
||||
//修改数据目录的权限
|
||||
ProcessUtil.permit(777, new File(Contants.localDataFilePath));
|
||||
|
||||
return setList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析监测设置的告警信息
|
||||
* 1、将同一监测的所有告警信息整理放到一个列表
|
||||
* 2、根据监测设置类型和设置名称找配置文件,找到了将告警信息追加到配置文件
|
||||
* 3、只要有监测设置,不管有没有设置告警信息,都要将相应字段追加到配置文件,只是无告警信息时,配置字段为空
|
||||
*/
|
||||
private Map<Long, List<AlarmInfo>> handerAlarmInfo(JSONArray jsonArr, List<SetInfo> setList) {
|
||||
if(setList==null || setList.size()==0){
|
||||
logger.info("无监测设置信息,无需处理报警设置");
|
||||
return null;
|
||||
}
|
||||
File dir = new File(Contants.localDetecConfPath);
|
||||
if (!dir.exists()) {// 判断存放目录是否存在, 不存在返回
|
||||
logger.error("Please check whether the alarm settings information is consistent with the monitoring settings information!");
|
||||
return null;
|
||||
}
|
||||
Map<Long, List<AlarmInfo>> alarmMap = new HashMap<Long, List<AlarmInfo>>();
|
||||
if(jsonArr!=null){
|
||||
for (int i = 0; i < jsonArr.size(); i++) {
|
||||
Object obj = JSONObject.toBean(JSONObject
|
||||
.fromObject(jsonArr.get(i)), AlarmInfo.class);
|
||||
if (obj != null) {
|
||||
AlarmInfo alarmInfo = (AlarmInfo) obj;
|
||||
if (alarmMap.containsKey(alarmInfo.getSetInfoId())) {// 如果报警信息中已经存在相应类型的报警则追加
|
||||
List<AlarmInfo> aList = alarmMap.get(alarmInfo.getSetInfoId());
|
||||
aList.add(alarmInfo);
|
||||
alarmMap.put(alarmInfo.getSetInfoId(), aList);
|
||||
} else {
|
||||
List<AlarmInfo> aList = new ArrayList<AlarmInfo>();
|
||||
aList.add(alarmInfo);
|
||||
alarmMap.put(alarmInfo.getSetInfoId(), aList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<Long, List<AlarmInfo>> alarmMapNew = new HashMap<Long, List<AlarmInfo>>();
|
||||
// 依次查看监测设置信息,不管有没有设置告警信息,都要写配置police=,若无告警信息,配置字段值为空
|
||||
for(SetInfo setInfo : setList){
|
||||
Long key = setInfo.getId();
|
||||
List<AlarmInfo> alarmList = alarmMap.get(key);
|
||||
// 配置信息保存对应的文件名
|
||||
String fileName = DetecConfOper.getFileName(
|
||||
setInfo.getCheckTypeName(), setInfo.getProcessIden(),
|
||||
Contants.localDetecConfSuffix);
|
||||
File f = DetecConfOper.getConfigFile(dir, fileName);
|
||||
{// 追加报警设置到相应的监测配置文件
|
||||
if (f != null && f.exists())
|
||||
try {
|
||||
FileWrUtil.cfgFileAppender(f,
|
||||
Contants.charset, new String[]{getAlarmArr(alarmList)});
|
||||
if(alarmList!=null){// 只有成功保存了文件并且设置为告警信息的,才返回给上一方法
|
||||
alarmMapNew.put(key, alarmList);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Write the document“" + f.getAbsolutePath() + "”error: " + Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
return alarmMapNew;
|
||||
}
|
||||
|
||||
private static String getAlarmArr(List<AlarmInfo> alarmList) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("police=");
|
||||
if (alarmList != null && alarmList.size() > 0) {
|
||||
for (AlarmInfo alarmInfo : alarmList) {
|
||||
sb.append(alarmInfo.toStringVal() + ",");
|
||||
}
|
||||
sb.delete(sb.toString().length() - 1, sb.toString().length());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
184
src/com/nis/nmsclient/model/AlarmInfo.java
Normal file
184
src/com/nis/nmsclient/model/AlarmInfo.java
Normal file
@@ -0,0 +1,184 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
/**
|
||||
* 监测信息报警相关信息实体
|
||||
*
|
||||
*/
|
||||
public class AlarmInfo {
|
||||
/**
|
||||
* 告警设置id
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 监测设置id
|
||||
*/
|
||||
private Long setInfoId;
|
||||
/**
|
||||
* 监测类型
|
||||
*/
|
||||
private String checkType;
|
||||
/**
|
||||
* 进程设置名称
|
||||
*/
|
||||
private String processIden;
|
||||
/**
|
||||
* 字段Id
|
||||
*/
|
||||
private Long metadataId;
|
||||
/**
|
||||
* 字段描述
|
||||
*/
|
||||
private String filedCommonts;
|
||||
/**
|
||||
* 字段序号
|
||||
*/
|
||||
private Integer showNum;
|
||||
/**
|
||||
* 报警状态
|
||||
*/
|
||||
private String policeState;
|
||||
/**
|
||||
* 报警值
|
||||
*/
|
||||
private String policeValue;
|
||||
/**
|
||||
* 报警单位
|
||||
*/
|
||||
private String policeUnit;
|
||||
/**
|
||||
* 报警比较符:针对number型数据>、<、>=、<=、= 针对字符串类型数据equal、 include、exclude
|
||||
*/
|
||||
private String policeSysmbols;
|
||||
/**
|
||||
* 报警等级
|
||||
*/
|
||||
private Integer policeLevel;
|
||||
/**
|
||||
* 设置告警时,指定多个标识符(如多个盘符、多个CPU、多个网卡),如硬盘使用率,空:所有盘存在一个盘使用率超过告警值,则告警;all:所有盘总的使用率超过告警值,则告警;指定多个盘符:指定盘存在一个盘使用率超过告警值,则告警
|
||||
*/
|
||||
private String marker;
|
||||
/**
|
||||
* 标识符对应字段在metadata表中的id
|
||||
*/
|
||||
private Integer markerFiledId;;
|
||||
/**
|
||||
* 标识符对应字段在metadata表中的showNum
|
||||
*/
|
||||
private Integer markerFiledShowNum;
|
||||
|
||||
|
||||
public Long getSetInfoId() {
|
||||
return setInfoId;
|
||||
}
|
||||
public void setSetInfoId(Long setInfoId) {
|
||||
this.setInfoId = setInfoId;
|
||||
}
|
||||
public Long getMetadataId() {
|
||||
return metadataId;
|
||||
}
|
||||
public void setMetadataId(Long metadataId) {
|
||||
this.metadataId = metadataId;
|
||||
}
|
||||
public Integer getShowNum() {
|
||||
return showNum;
|
||||
}
|
||||
public void setShowNum(Integer showNum) {
|
||||
this.showNum = showNum;
|
||||
}
|
||||
public String getPoliceState() {
|
||||
return policeState;
|
||||
}
|
||||
public void setPoliceState(String policeState) {
|
||||
this.policeState = policeState;
|
||||
}
|
||||
public String getPoliceUnit() {
|
||||
return policeUnit;
|
||||
}
|
||||
public void setPoliceUnit(String policeUnit) {
|
||||
this.policeUnit = policeUnit;
|
||||
}
|
||||
public String getPoliceSysmbols() {
|
||||
return policeSysmbols;
|
||||
}
|
||||
public void setPoliceSysmbols(String policeSysmbols) {
|
||||
this.policeSysmbols = policeSysmbols;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public Integer getPoliceLevel() {
|
||||
return policeLevel;
|
||||
}
|
||||
public void setPoliceLevel(Integer policeLevel) {
|
||||
this.policeLevel = policeLevel;
|
||||
}
|
||||
public String getCheckType() {
|
||||
return checkType;
|
||||
}
|
||||
public void setCheckType(String checkType) {
|
||||
this.checkType = checkType;
|
||||
}
|
||||
public String getProcessIden() {
|
||||
return processIden;
|
||||
}
|
||||
public void setProcessIden(String processIden) {
|
||||
this.processIden = processIden;
|
||||
}
|
||||
public String getFiledCommonts() {
|
||||
return filedCommonts;
|
||||
}
|
||||
public void setFiledCommonts(String filedCommonts) {
|
||||
this.filedCommonts = filedCommonts;
|
||||
}
|
||||
public String getPoliceValue() {
|
||||
return policeValue;
|
||||
}
|
||||
public void setPoliceValue(String policeValue) {
|
||||
this.policeValue = policeValue;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "showNum=" + showNum + ",policeSysmbols=" + policeSysmbols
|
||||
+ ",policeValue=" + policeValue + ",policeLevel=" + policeLevel;
|
||||
}
|
||||
|
||||
public String toStringVal(){
|
||||
return showNum + "|" + policeSysmbols + "|" + policeValue + "|"
|
||||
+ policeLevel + "|" + filedCommonts;
|
||||
}
|
||||
|
||||
public String getMarker()
|
||||
{
|
||||
return marker;
|
||||
}
|
||||
|
||||
public void setMarker(String marker)
|
||||
{
|
||||
this.marker = marker;
|
||||
}
|
||||
|
||||
public Integer getMarkerFiledId()
|
||||
{
|
||||
return markerFiledId;
|
||||
}
|
||||
|
||||
public void setMarkerFiledId(Integer markerFiledId)
|
||||
{
|
||||
this.markerFiledId = markerFiledId;
|
||||
}
|
||||
|
||||
public Integer getMarkerFiledShowNum()
|
||||
{
|
||||
return markerFiledShowNum;
|
||||
}
|
||||
|
||||
public void setMarkerFiledShowNum(Integer markerFiledShowNum)
|
||||
{
|
||||
this.markerFiledShowNum = markerFiledShowNum;
|
||||
}
|
||||
|
||||
}
|
||||
86
src/com/nis/nmsclient/model/CommandPO.java
Normal file
86
src/com/nis/nmsclient/model/CommandPO.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
public class CommandPO {
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
private Long execId;
|
||||
/**
|
||||
* 类型:4 命令执行,6 升级
|
||||
*/
|
||||
private long execType;
|
||||
/**
|
||||
* 升级文件存放目录
|
||||
*/
|
||||
private String srcPath;
|
||||
/**
|
||||
* 命令名称
|
||||
*/
|
||||
private String commandName;
|
||||
/**
|
||||
* 命令参数
|
||||
*/
|
||||
private String commandParam;
|
||||
/**
|
||||
* 执行状态:4下发任务(40 下发成功,41下发失败),5杀进程(50成功,51失败)、6备份、7更新(覆盖) 、8启动进程
|
||||
*/
|
||||
private long execState;
|
||||
/**
|
||||
* 恢复版本
|
||||
*/
|
||||
private Long execVersion;
|
||||
/**
|
||||
* 是否周期任务
|
||||
*/
|
||||
private long isLoop;
|
||||
|
||||
public Long getExecId() {
|
||||
return execId;
|
||||
}
|
||||
public void setExecId(Long execId) {
|
||||
this.execId = execId;
|
||||
}
|
||||
public long getExecType() {
|
||||
return execType;
|
||||
}
|
||||
public void setExecType(long execType) {
|
||||
this.execType = execType;
|
||||
}
|
||||
public String getSrcPath() {
|
||||
return srcPath;
|
||||
}
|
||||
public void setSrcPath(String srcPath) {
|
||||
this.srcPath = srcPath;
|
||||
}
|
||||
public String getCommandName() {
|
||||
return commandName;
|
||||
}
|
||||
public void setCommandName(String commandName) {
|
||||
this.commandName = commandName;
|
||||
}
|
||||
public String getCommandParam() {
|
||||
return commandParam;
|
||||
}
|
||||
public void setCommandParam(String commandParam) {
|
||||
this.commandParam = commandParam;
|
||||
}
|
||||
public long getExecState() {
|
||||
return execState;
|
||||
}
|
||||
public void setExecState(long execState) {
|
||||
this.execState = execState;
|
||||
}
|
||||
public Long getExecVersion() {
|
||||
return execVersion;
|
||||
}
|
||||
public void setExecVersion(Long execVersion) {
|
||||
this.execVersion = execVersion;
|
||||
}
|
||||
public long getIsLoop() {
|
||||
return isLoop;
|
||||
}
|
||||
public void setIsLoop(long isLoop) {
|
||||
this.isLoop = isLoop;
|
||||
}
|
||||
|
||||
}
|
||||
52
src/com/nis/nmsclient/model/ParamBackup.java
Normal file
52
src/com/nis/nmsclient/model/ParamBackup.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
/**
|
||||
* 备份命令的参数实体类
|
||||
* @date Mar 23, 2012
|
||||
* @author zhenzhen
|
||||
* @version
|
||||
*/
|
||||
public class ParamBackup{
|
||||
/**
|
||||
* 需要备份的文件或目录
|
||||
*/
|
||||
private String backup = null;
|
||||
/**
|
||||
* 备份文件到的目的路径
|
||||
*/
|
||||
private String backupTo = null;
|
||||
/**
|
||||
* 排除的文件或目录,即不需要备份的,如果是相对路径,则相对需要备份的文件或目录
|
||||
*/
|
||||
private String[] except = null;
|
||||
/**
|
||||
* 备份压缩包时:Y 按绝对路径压缩,N 按进入备份目录压缩(这个属性只针对Linux有效,Windows下只按进入备份目录压缩)
|
||||
*/
|
||||
private String isAbs = null;
|
||||
|
||||
public String getBackup() {
|
||||
return backup;
|
||||
}
|
||||
public void setBackup(String backup) {
|
||||
this.backup = backup;
|
||||
}
|
||||
public String getBackupTo() {
|
||||
return backupTo;
|
||||
}
|
||||
public void setBackupTo(String backupTo) {
|
||||
this.backupTo = backupTo;
|
||||
}
|
||||
public String[] getExcept() {
|
||||
return except;
|
||||
}
|
||||
public void setExcept(String[] except) {
|
||||
this.except = except;
|
||||
}
|
||||
public String getIsAbs() {
|
||||
return isAbs;
|
||||
}
|
||||
public void setIsAbs(String isAbs) {
|
||||
this.isAbs = isAbs;
|
||||
}
|
||||
|
||||
}
|
||||
111
src/com/nis/nmsclient/model/ParamCmdExec.java
Normal file
111
src/com/nis/nmsclient/model/ParamCmdExec.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
/**
|
||||
* 命令执行(启动命令)的参数实体类
|
||||
* @date Mar 23, 2012
|
||||
* @author zhenzhen
|
||||
* @version
|
||||
*/
|
||||
public class ParamCmdExec {
|
||||
/**
|
||||
* 执行文件或命令
|
||||
*/
|
||||
private String execCmd;
|
||||
/**
|
||||
* 执行文件或命令的参数序列
|
||||
*/
|
||||
private String[] execParams;
|
||||
/**
|
||||
* 该命令是否强制执行:Y是,N否
|
||||
*/
|
||||
private String forceExec;
|
||||
/**
|
||||
* 常驻内存标识: Y是,N否
|
||||
*/
|
||||
private String residentFlag;
|
||||
/**
|
||||
* 存放执行结果标识的文件:1、常驻内存的,写入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;
|
||||
}
|
||||
}
|
||||
62
src/com/nis/nmsclient/model/ParamCoverUpdate.java
Normal file
62
src/com/nis/nmsclient/model/ParamCoverUpdate.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
/**
|
||||
* 覆盖更新的参数实体类
|
||||
* @date Mar 23, 2012
|
||||
* @author zhenzhen
|
||||
* @version
|
||||
*/
|
||||
public class ParamCoverUpdate{
|
||||
/**
|
||||
* 指定更新需要的文件
|
||||
*/
|
||||
private String source = null;
|
||||
/**
|
||||
* 指定文件覆盖目录
|
||||
*/
|
||||
private String cover = null;
|
||||
/**
|
||||
* 是否创建覆盖目录
|
||||
*/
|
||||
private String isCreateCover = null;
|
||||
/**
|
||||
* 删除的文件或目录,如果是相对路径,则相对需要更新的目录
|
||||
*/
|
||||
private String[] delete = null;
|
||||
/**
|
||||
* 如果更新源文件是个压缩包:Y 按绝对路径解压即在根目录下解压,N 按进入备份目录解压(这个属性只针对Linux有效,Windows下只按进入备份目录解压)
|
||||
*/
|
||||
private String isAbs = null;
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
public void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
public String getCover() {
|
||||
return cover;
|
||||
}
|
||||
public void setCover(String cover) {
|
||||
this.cover = cover;
|
||||
}
|
||||
public String getIsCreateCover() {
|
||||
return isCreateCover;
|
||||
}
|
||||
public void setIsCreateCover(String isCreateCover) {
|
||||
this.isCreateCover = isCreateCover;
|
||||
}
|
||||
public String[] getDelete() {
|
||||
return delete;
|
||||
}
|
||||
public void setDelete(String[] delete) {
|
||||
this.delete = delete;
|
||||
}
|
||||
public String getIsAbs() {
|
||||
return isAbs;
|
||||
}
|
||||
public void setIsAbs(String isAbs) {
|
||||
this.isAbs = isAbs;
|
||||
}
|
||||
|
||||
}
|
||||
81
src/com/nis/nmsclient/model/ParamFilePush.java
Normal file
81
src/com/nis/nmsclient/model/ParamFilePush.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
/**
|
||||
* 文件推送的参数实体类
|
||||
* @date Mar 23, 2012
|
||||
* @author zhenzhen
|
||||
* @version
|
||||
*/
|
||||
public class ParamFilePush {
|
||||
/**
|
||||
* 推送文件名
|
||||
*/
|
||||
private String fileName;
|
||||
/**
|
||||
* 推送目的地
|
||||
*/
|
||||
private String destPath;
|
||||
/**
|
||||
* 是否覆盖:Y是,N否
|
||||
*/
|
||||
private String isCover;
|
||||
/**
|
||||
* 推送文件的属主(只针对Linux有效)
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 属主密码(只针对Linux有效)
|
||||
*/
|
||||
private String param1;
|
||||
/**
|
||||
* 推送文件的属组(只针对Linux有效)
|
||||
*/
|
||||
private String groupName;
|
||||
/**
|
||||
* 推送文件拥有的权限(只针对Linux有效)
|
||||
*/
|
||||
private String permisson;
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
public String getDestPath() {
|
||||
return destPath;
|
||||
}
|
||||
public void setDestPath(String destPath) {
|
||||
this.destPath = destPath;
|
||||
}
|
||||
public String getIsCover() {
|
||||
return isCover;
|
||||
}
|
||||
public void setIsCover(String isCover) {
|
||||
this.isCover = isCover;
|
||||
}
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
public void setGroupName(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
public String getPermisson() {
|
||||
return permisson;
|
||||
}
|
||||
public void setPermisson(String permisson) {
|
||||
this.permisson = permisson;
|
||||
}
|
||||
public String getParam1() {
|
||||
return param1;
|
||||
}
|
||||
public void setParam1(String param1) {
|
||||
this.param1 = param1;
|
||||
}
|
||||
}
|
||||
174
src/com/nis/nmsclient/model/ParamUpgrade.java
Normal file
174
src/com/nis/nmsclient/model/ParamUpgrade.java
Normal file
@@ -0,0 +1,174 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
|
||||
/**
|
||||
* 升级命令的参数实体类
|
||||
* @date Mar 23, 2012
|
||||
* @author zhenzhen
|
||||
* @version
|
||||
*/
|
||||
public class ParamUpgrade {
|
||||
/**
|
||||
* 指定更新需要的文件,如果是相对路径,则相对推送目的地
|
||||
*/
|
||||
private String fileName = null;
|
||||
/**
|
||||
* 指定文件覆盖目录
|
||||
*/
|
||||
private String cover = null;
|
||||
/**
|
||||
* 是否创建覆盖目录
|
||||
*/
|
||||
private String isCreateCover = null;
|
||||
/**
|
||||
* 如果更新源文件是个压缩包:Y 按绝对路径解压即在根目录下解压,N 按进入备份目录解压(这个属性只针对Linux有效,Windows下只按进入备份目录解压)
|
||||
*/
|
||||
private String isAbs = null;
|
||||
/**
|
||||
* 需要删除的目录,如果是相对路径,则相对覆盖目录
|
||||
*/
|
||||
private String [] delete = null;
|
||||
/**
|
||||
* 该命令是否强制执行:Y是,N否
|
||||
*/
|
||||
private String forceExec;
|
||||
/**
|
||||
* 文件的属主或运行的用户(只针对Linux有效)
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 用户密码(只针对Linux有效)
|
||||
*/
|
||||
private String param1;
|
||||
/**
|
||||
* 文件的属组(只针对Linux有效)
|
||||
*/
|
||||
private String groupName;
|
||||
/**
|
||||
* 权限设置
|
||||
*/
|
||||
private String permisson = null;
|
||||
//以上是升级用到的参数属性,以下是升级和恢复都有的参数属性
|
||||
/**
|
||||
* 备份目录,如果是相对路径,则相对覆盖目录
|
||||
*/
|
||||
private ParamBackup [] backups = null;
|
||||
/**
|
||||
* 恢复目录,逆向任务时使用
|
||||
*/
|
||||
private ParamCoverUpdate[] recoverys;
|
||||
/**
|
||||
* 进程PID文件全路径
|
||||
*/
|
||||
private String pidFile = null;
|
||||
/**
|
||||
* 进程启动文件全路径
|
||||
*/
|
||||
private String startupFile = null;
|
||||
/**
|
||||
* 执行文件的参数序列
|
||||
*/
|
||||
private String[] execParams;
|
||||
/**
|
||||
* 最终结果获取最大等待时间(单位:秒)
|
||||
*/
|
||||
private String maxWaitTime;
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
public String getCover() {
|
||||
return cover;
|
||||
}
|
||||
public void setCover(String cover) {
|
||||
this.cover = cover;
|
||||
}
|
||||
public ParamBackup[] getBackups() {
|
||||
return backups;
|
||||
}
|
||||
public void setBackups(ParamBackup[] backups) {
|
||||
this.backups = backups;
|
||||
}
|
||||
public String getPidFile() {
|
||||
return pidFile;
|
||||
}
|
||||
public void setPidFile(String pidFile) {
|
||||
this.pidFile = pidFile;
|
||||
}
|
||||
public String getStartupFile() {
|
||||
return startupFile;
|
||||
}
|
||||
public void setStartupFile(String startupFile) {
|
||||
this.startupFile = startupFile;
|
||||
}
|
||||
public String[] getDelete() {
|
||||
return delete;
|
||||
}
|
||||
public void setDelete(String[] delete) {
|
||||
this.delete = delete;
|
||||
}
|
||||
public String getIsCreateCover() {
|
||||
return isCreateCover;
|
||||
}
|
||||
public void setIsCreateCover(String isCreateCover) {
|
||||
this.isCreateCover = isCreateCover;
|
||||
}
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
public String getPermisson() {
|
||||
return permisson;
|
||||
}
|
||||
public void setPermisson(String permisson) {
|
||||
this.permisson = permisson;
|
||||
}
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
public void setGroupName(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
public String getIsAbs() {
|
||||
return isAbs;
|
||||
}
|
||||
public void setIsAbs(String isAbs) {
|
||||
this.isAbs = isAbs;
|
||||
}
|
||||
public String getForceExec() {
|
||||
return forceExec;
|
||||
}
|
||||
public void setForceExec(String forceExec) {
|
||||
this.forceExec = forceExec;
|
||||
}
|
||||
public String getMaxWaitTime() {
|
||||
return maxWaitTime;
|
||||
}
|
||||
public void setMaxWaitTime(String maxWaitTime) {
|
||||
this.maxWaitTime = maxWaitTime;
|
||||
}
|
||||
public void setExecParams(String[] execParams) {
|
||||
this.execParams = execParams;
|
||||
}
|
||||
public String[] getExecParams() {
|
||||
return execParams;
|
||||
}
|
||||
public ParamCoverUpdate[] getRecoverys() {
|
||||
return recoverys;
|
||||
}
|
||||
public void setRecoverys(ParamCoverUpdate[] recoverys) {
|
||||
this.recoverys = recoverys;
|
||||
}
|
||||
public String getParam1() {
|
||||
return param1;
|
||||
}
|
||||
public void setParam1(String param1) {
|
||||
this.param1 = param1;
|
||||
}
|
||||
|
||||
}
|
||||
106
src/com/nis/nmsclient/model/ReturnFilePO.java
Normal file
106
src/com/nis/nmsclient/model/ReturnFilePO.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class ReturnFilePO {
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
private Long taskId;
|
||||
/**
|
||||
* 类型:4 命令执行,6 升级
|
||||
*/
|
||||
private long taskType;
|
||||
/**
|
||||
* 唯一标志一台物理机(类似于IP)
|
||||
*/
|
||||
private long uuid;
|
||||
/**
|
||||
* 回传路径集: key 文件实际路径 value 文件别名
|
||||
*/
|
||||
// private Map<String, String> filePathMap;
|
||||
/**
|
||||
* 当前要回传的文件
|
||||
*/
|
||||
// private File curRetrunFile;
|
||||
/**
|
||||
* 回传文件名(统一处理后一个任务只有一个回传文件)
|
||||
*/
|
||||
private String returnFileName;
|
||||
/**
|
||||
* 回传状态:0 成功,1 失败
|
||||
*/
|
||||
private long state;
|
||||
/**
|
||||
* 开始时间
|
||||
*/
|
||||
private Date startTime;
|
||||
/**
|
||||
* 结束时间
|
||||
*/
|
||||
private Date endTime;
|
||||
/**
|
||||
* 是否循环任务
|
||||
*/
|
||||
private long isLoop;
|
||||
/**
|
||||
* 回传文件的结果描述
|
||||
*/
|
||||
private String resDesc;
|
||||
|
||||
public Long getTaskId() {
|
||||
return taskId;
|
||||
}
|
||||
public void setTaskId(Long taskId) {
|
||||
this.taskId = taskId;
|
||||
}
|
||||
public long getTaskType() {
|
||||
return taskType;
|
||||
}
|
||||
public void setTaskType(long taskType) {
|
||||
this.taskType = taskType;
|
||||
}
|
||||
public long getState() {
|
||||
return state;
|
||||
}
|
||||
public void setState(long state) {
|
||||
this.state = state;
|
||||
}
|
||||
public Date getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
public void setStartTime(Date startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
public Date getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
public void setEndTime(Date endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
public long getIsLoop() {
|
||||
return isLoop;
|
||||
}
|
||||
public void setIsLoop(long isLoop) {
|
||||
this.isLoop = isLoop;
|
||||
}
|
||||
public String getResDesc() {
|
||||
return resDesc;
|
||||
}
|
||||
public void setResDesc(String resDesc) {
|
||||
this.resDesc = resDesc;
|
||||
}
|
||||
public String getReturnFileName() {
|
||||
return returnFileName;
|
||||
}
|
||||
public void setReturnFileName(String returnFileName) {
|
||||
this.returnFileName = returnFileName;
|
||||
}
|
||||
public long getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
public void setUuid(long uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
}
|
||||
272
src/com/nis/nmsclient/model/SetInfo.java
Normal file
272
src/com/nis/nmsclient/model/SetInfo.java
Normal file
@@ -0,0 +1,272 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
|
||||
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
|
||||
/**
|
||||
* 客户端用到的监测设置信息实体
|
||||
*
|
||||
*/
|
||||
public class SetInfo {
|
||||
/**
|
||||
* 监测设置信息ID
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 检测类型
|
||||
*/
|
||||
private String checkTypeName;//如:CPU、DISK等
|
||||
/**
|
||||
* 检测类型的ID,预留
|
||||
*/
|
||||
private Long checkTypeId;
|
||||
/**
|
||||
* 最大测试次数
|
||||
*/
|
||||
private Long checkMaxTimes;
|
||||
/**
|
||||
* 时间间隔(单位:分钟)
|
||||
*/
|
||||
private Long checkGap;
|
||||
/**
|
||||
* 超时时间(单位:秒)
|
||||
*/
|
||||
private Long checkOutTime;
|
||||
/**
|
||||
* 监测状态:0无效;1有效
|
||||
*/
|
||||
private String checkState;
|
||||
/**
|
||||
* 监测方式:0主动(DC执行),1被动(NC执行)
|
||||
*/
|
||||
private String checkWay;
|
||||
/**
|
||||
* 设置标志
|
||||
*/
|
||||
private String processIden;
|
||||
/**
|
||||
* 进程PID存放文件
|
||||
*/
|
||||
private String processFile;
|
||||
/**
|
||||
* 进程执行文件
|
||||
*/
|
||||
private String processPath;
|
||||
/**
|
||||
* 是否系统启动(NMSAgent启动/第三方自己启动);默认0自启动;1NMSAgent启动
|
||||
*/
|
||||
private String IsControlStart;
|
||||
/**
|
||||
* 控制启动时间
|
||||
*/
|
||||
private Long controlStartTime;
|
||||
/**
|
||||
*上传数据时间间隔单位分钟:不能为空,默认15分钟。监测数据上传到NMSServer周期。
|
||||
*/
|
||||
private Long uploadGap;
|
||||
|
||||
/**
|
||||
* 计划检测时间:针对当前配置信息首次执行时间
|
||||
*/
|
||||
private Long planCheckTime;
|
||||
/**
|
||||
* 是否预置监测,0是,1否
|
||||
*/
|
||||
private String isSchedule;
|
||||
/**
|
||||
* 进程搜索关键字
|
||||
*/
|
||||
private String processSearchKeyCode;
|
||||
|
||||
// ====== 以下信息仅供DC端使用,这里添加是为了不出现WARN信息
|
||||
private String nodeGroupsId;
|
||||
private String nodeIpsId;
|
||||
private Long viewLevel;
|
||||
private String nodeIp2;
|
||||
private Long isSNMP;
|
||||
|
||||
// ======= 保存最后合并的临时结果文件的检测时间(仅用于NC)
|
||||
private Long lastMergeDetecTime;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getCheckTypeName() {
|
||||
return checkTypeName;
|
||||
}
|
||||
public void setCheckTypeName(String checkTypeName) {
|
||||
this.checkTypeName = checkTypeName;
|
||||
}
|
||||
public Long getCheckTypeId() {
|
||||
return checkTypeId;
|
||||
}
|
||||
public Long getCheckMaxTimes() {
|
||||
return checkMaxTimes;
|
||||
}
|
||||
public void setCheckMaxTimes(Long checkMaxTimes) {
|
||||
this.checkMaxTimes = checkMaxTimes;
|
||||
}
|
||||
public Long getCheckGap() {
|
||||
return checkGap;
|
||||
}
|
||||
public void setCheckGap(Long checkGap) {
|
||||
this.checkGap = checkGap;
|
||||
}
|
||||
public Long getCheckOutTime() {
|
||||
return checkOutTime;
|
||||
}
|
||||
public void setCheckOutTime(Long checkOutTime) {
|
||||
this.checkOutTime = checkOutTime;
|
||||
}
|
||||
public String getCheckState() {
|
||||
return checkState;
|
||||
}
|
||||
public void setCheckState(String checkState) {
|
||||
this.checkState = checkState;
|
||||
}
|
||||
public String getProcessIden() {
|
||||
return processIden;
|
||||
}
|
||||
public void setProcessIden(String processIden) {
|
||||
this.processIden = processIden;
|
||||
}
|
||||
public String getProcessFile() {
|
||||
return processFile;
|
||||
}
|
||||
public void setProcessFile(String processFile) {
|
||||
this.processFile = processFile;
|
||||
}
|
||||
public String getProcessPath() {
|
||||
return processPath;
|
||||
}
|
||||
public void setProcessPath(String processPath) {
|
||||
this.processPath = processPath;
|
||||
}
|
||||
public void setCheckTypeId(Long checkTypeId) {
|
||||
this.checkTypeId = checkTypeId;
|
||||
}
|
||||
public String getCheckWay() {
|
||||
return checkWay;
|
||||
}
|
||||
public void setCheckWay(String checkWay) {
|
||||
this.checkWay = checkWay;
|
||||
}
|
||||
|
||||
public String[] getValArr(Long uuid, String dataFileDir) {
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add("pubInfo=" + uuid + "," + id + "," + checkTypeName + "," + processIden);
|
||||
list.add("checkState=" + checkState);
|
||||
//list.add("checkWay=" + checkWay);
|
||||
//list.add("isControlStart=" + IsControlStart);
|
||||
//list.add("controlStartTime=" + controlStartTime);
|
||||
list.add("checkGap=" + checkGap);
|
||||
list.add("checkOutTime=" + checkOutTime);
|
||||
list.add("checkMaxTimes=" + checkMaxTimes);
|
||||
//list.add("processFile=" + processFile);
|
||||
//list.add("processPath=" + processPath);
|
||||
list.add("planCheckTime=" + planCheckTime);
|
||||
list.add("uploadGap=" + uploadGap);
|
||||
list.add("dataFileDir=" + dataFileDir);
|
||||
|
||||
try {
|
||||
if("1".equals(isSchedule)) { // 第三方监测
|
||||
File dataDir = new File(dataFileDir);
|
||||
File tempDataDir = new File(Contants.localTempDataIncomingPath, dataDir.getName());
|
||||
list.add("tempDataFileDir=" + tempDataDir.getCanonicalPath());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String[] val = new String[list.size()];
|
||||
return list.toArray(val);
|
||||
}
|
||||
public String getIsControlStart() {
|
||||
return IsControlStart;
|
||||
}
|
||||
public void setIsControlStart(String isControlStart) {
|
||||
IsControlStart = isControlStart;
|
||||
}
|
||||
public Long getUploadGap() {
|
||||
return uploadGap;
|
||||
}
|
||||
public void setUploadGap(Long uploadGap) {
|
||||
this.uploadGap = uploadGap;
|
||||
}
|
||||
public String getIsSchedule() {
|
||||
return isSchedule;
|
||||
}
|
||||
public void setIsSchedule(String isSchedule) {
|
||||
this.isSchedule = isSchedule;
|
||||
}
|
||||
public Long getControlStartTime() {
|
||||
return controlStartTime;
|
||||
}
|
||||
public void setControlStartTime(Long controlStartTime) {
|
||||
this.controlStartTime = controlStartTime;
|
||||
}
|
||||
public Long getPlanCheckTime() {
|
||||
return planCheckTime;
|
||||
}
|
||||
public void setPlanCheckTime(Long planCheckTime) {
|
||||
this.planCheckTime = planCheckTime;
|
||||
}
|
||||
public String getProcessSearchKeyCode() {
|
||||
return processSearchKeyCode;
|
||||
}
|
||||
public void setProcessSearchKeyCode(String processSearchKeyCode) {
|
||||
this.processSearchKeyCode = processSearchKeyCode;
|
||||
}
|
||||
public String getNodeGroupsId() {
|
||||
return nodeGroupsId;
|
||||
}
|
||||
public void setNodeGroupsId(String nodeGroupsId) {
|
||||
this.nodeGroupsId = nodeGroupsId;
|
||||
}
|
||||
public String getNodeIpsId() {
|
||||
return nodeIpsId;
|
||||
}
|
||||
public void setNodeIpsId(String nodeIpsId) {
|
||||
this.nodeIpsId = nodeIpsId;
|
||||
}
|
||||
public Long getViewLevel() {
|
||||
return viewLevel;
|
||||
}
|
||||
public void setViewLevel(Long viewLevel) {
|
||||
this.viewLevel = viewLevel;
|
||||
}
|
||||
public String getNodeIp2() {
|
||||
return nodeIp2;
|
||||
}
|
||||
public void setNodeIp2(String nodeIp2) {
|
||||
this.nodeIp2 = nodeIp2;
|
||||
}
|
||||
public Long getIsSNMP() {
|
||||
return isSNMP;
|
||||
}
|
||||
public void setIsSNMP(Long isSNMP) {
|
||||
this.isSNMP = isSNMP;
|
||||
}
|
||||
public void setLastMergeDetecTime(Long lastMergeDetecTime) {
|
||||
this.lastMergeDetecTime = lastMergeDetecTime;
|
||||
}
|
||||
public Long getLastMergeDetecTime() {
|
||||
return lastMergeDetecTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ReflectionToStringBuilder.toString(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
49
src/com/nis/nmsclient/model/Task1.java
Normal file
49
src/com/nis/nmsclient/model/Task1.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
/**
|
||||
* 任务实体类:1 文件推送
|
||||
*
|
||||
*/
|
||||
public class Task1 {
|
||||
/**
|
||||
* 任务ID
|
||||
*/
|
||||
private Long taskId;
|
||||
/**
|
||||
* 任务类型:1 文件推送,2 非流文本数据获取,3 流文本数据获取,4 命令执行,5 shell注册
|
||||
*/
|
||||
private long taskType;
|
||||
/**
|
||||
* 推送文件参数设置
|
||||
*/
|
||||
private String taskParam;
|
||||
/**
|
||||
* 重新执行,原任务ID
|
||||
*/
|
||||
private Long oldTaskId;
|
||||
|
||||
public Long getTaskId() {
|
||||
return taskId;
|
||||
}
|
||||
public void setTaskId(Long taskId) {
|
||||
this.taskId = taskId;
|
||||
}
|
||||
public long getTaskType() {
|
||||
return taskType;
|
||||
}
|
||||
public void setTaskType(long taskType) {
|
||||
this.taskType = taskType;
|
||||
}
|
||||
public String getTaskParam() {
|
||||
return taskParam;
|
||||
}
|
||||
public void setTaskParam(String taskParam) {
|
||||
this.taskParam = taskParam;
|
||||
}
|
||||
public Long getOldTaskId() {
|
||||
return oldTaskId;
|
||||
}
|
||||
public void setOldTaskId(Long oldTaskId) {
|
||||
this.oldTaskId = oldTaskId;
|
||||
}
|
||||
}
|
||||
166
src/com/nis/nmsclient/model/Task4.java
Normal file
166
src/com/nis/nmsclient/model/Task4.java
Normal file
@@ -0,0 +1,166 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
|
||||
/**
|
||||
* 任务实体类:4 命令执行
|
||||
*
|
||||
*/
|
||||
public class Task4 {
|
||||
/**
|
||||
* 任务ID
|
||||
*/
|
||||
private Long taskId;
|
||||
/**
|
||||
* 任务类型:1 文件推送,2 非流文本数据获取,3 流文本数据获取,4 命令执行,5 shell注册
|
||||
*/
|
||||
private long taskType;
|
||||
/**
|
||||
* 命令类型:1 Agent原生支持命令,2 脚本,3 shell命令
|
||||
*/
|
||||
private long commandType;
|
||||
/**
|
||||
* 命令名称
|
||||
*/
|
||||
private String commandName;
|
||||
/**
|
||||
* 命令参数
|
||||
*/
|
||||
private String commandParam;
|
||||
/**
|
||||
* 脚本路径(已无用)
|
||||
*/
|
||||
private String scriptPath;
|
||||
/**
|
||||
* 执行状态:4下发任务(40 下发成功,41下发失败),5杀进程(50成功,51失败)、6备份、7更新(覆盖) 、8启动进程
|
||||
*/
|
||||
private long state;
|
||||
/**
|
||||
* 执行时间
|
||||
*/
|
||||
private Long startTime;
|
||||
/**
|
||||
* 结束时间
|
||||
*/
|
||||
private Long endTime;
|
||||
/**
|
||||
* 是否循环任务: 0 非周期, 1 周期,默认是0
|
||||
*/
|
||||
private long isLoop;
|
||||
/**
|
||||
* 如果是循环任务,循环周期
|
||||
*/
|
||||
private long loopDelay;
|
||||
/**
|
||||
* 任务状态:6撤销执行
|
||||
*/
|
||||
private Long missionState;
|
||||
/**
|
||||
* 重新执行,原任务ID
|
||||
*/
|
||||
private Long oldTaskId;
|
||||
|
||||
public Long getTaskId() {
|
||||
return taskId;
|
||||
}
|
||||
|
||||
public void setTaskId(Long taskId) {
|
||||
this.taskId = taskId;
|
||||
}
|
||||
|
||||
public long getTaskType() {
|
||||
return taskType;
|
||||
}
|
||||
|
||||
public void setTaskType(long taskType) {
|
||||
this.taskType = taskType;
|
||||
}
|
||||
|
||||
public long getCommandType() {
|
||||
return commandType;
|
||||
}
|
||||
|
||||
public void setCommandType(long commandType) {
|
||||
this.commandType = commandType;
|
||||
}
|
||||
|
||||
public long getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(long state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getCommandName() {
|
||||
return commandName;
|
||||
}
|
||||
|
||||
public void setCommandName(String commandName) {
|
||||
this.commandName = commandName;
|
||||
}
|
||||
|
||||
public String getCommandParam() {
|
||||
return commandParam;
|
||||
}
|
||||
|
||||
public void setCommandParam(String commandParam) {
|
||||
this.commandParam = commandParam;
|
||||
}
|
||||
|
||||
public String getScriptPath() {
|
||||
return scriptPath;
|
||||
}
|
||||
|
||||
public void setScriptPath(String scriptPath) {
|
||||
this.scriptPath = scriptPath;
|
||||
}
|
||||
|
||||
public Long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(Long startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public Long getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(Long endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public long getIsLoop() {
|
||||
return isLoop;
|
||||
}
|
||||
|
||||
public void setIsLoop(long isLoop) {
|
||||
this.isLoop = isLoop;
|
||||
}
|
||||
|
||||
public long getLoopDelay() {
|
||||
return loopDelay;
|
||||
}
|
||||
|
||||
public void setLoopDelay(long loopDelay) {
|
||||
this.loopDelay = loopDelay;
|
||||
}
|
||||
|
||||
public Long getMissionState() {
|
||||
return missionState;
|
||||
}
|
||||
|
||||
public void setMissionState(Long missionState) {
|
||||
this.missionState = missionState;
|
||||
}
|
||||
|
||||
public Long getOldTaskId() {
|
||||
return oldTaskId;
|
||||
}
|
||||
|
||||
public void setOldTaskId(Long oldTaskId) {
|
||||
this.oldTaskId = oldTaskId;
|
||||
}
|
||||
|
||||
}
|
||||
121
src/com/nis/nmsclient/model/Task6.java
Normal file
121
src/com/nis/nmsclient/model/Task6.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package com.nis.nmsclient.model;
|
||||
|
||||
|
||||
/**
|
||||
* 任务实体类:6 升级
|
||||
*
|
||||
*/
|
||||
public class Task6 {
|
||||
/**
|
||||
* 任务ID
|
||||
*/
|
||||
private Long taskId;
|
||||
/**
|
||||
* 任务类型:1 文件推送,4 命令执行,5 shell注册,6 升级
|
||||
*/
|
||||
private long taskType;
|
||||
/**
|
||||
* 命令类型:4 Agent原生支持命令
|
||||
*/
|
||||
private long commandType;
|
||||
/**
|
||||
* 命令名称
|
||||
*/
|
||||
private String commandName;
|
||||
/**
|
||||
* 命令参数
|
||||
*/
|
||||
private String commandParam;
|
||||
/**
|
||||
* 执行状态:4下发任务(40 下发成功,41下发失败),5杀进程(50成功,51失败)、6备份、7更新(覆盖) 、8启动进程
|
||||
*/
|
||||
private Long state;
|
||||
/**
|
||||
* 如果是恢复(逆向任务),则为恢复到的版本(恢复到的任务ID)。判断它是否为空,来确定是否逆向任务
|
||||
*/
|
||||
private Long version;
|
||||
/**
|
||||
* 升级时间
|
||||
*/
|
||||
private Long upgradeTime;
|
||||
/**
|
||||
* 全局权限用户名(未来)
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 全局权限密码(未来 约定加密方式)
|
||||
*/
|
||||
private String userpwd;
|
||||
/**
|
||||
* 重新执行,原任务ID
|
||||
*/
|
||||
private Long oldTaskId;
|
||||
|
||||
public Long getTaskId() {
|
||||
return taskId;
|
||||
}
|
||||
public void setTaskId(Long taskId) {
|
||||
this.taskId = taskId;
|
||||
}
|
||||
public long getTaskType() {
|
||||
return taskType;
|
||||
}
|
||||
public void setTaskType(long taskType) {
|
||||
this.taskType = taskType;
|
||||
}
|
||||
public long getCommandType() {
|
||||
return commandType;
|
||||
}
|
||||
public void setCommandType(long commandType) {
|
||||
this.commandType = commandType;
|
||||
}
|
||||
public String getCommandName() {
|
||||
return commandName;
|
||||
}
|
||||
public void setCommandName(String commandName) {
|
||||
this.commandName = commandName;
|
||||
}
|
||||
public String getCommandParam() {
|
||||
return commandParam;
|
||||
}
|
||||
public void setCommandParam(String commandParam) {
|
||||
this.commandParam = commandParam;
|
||||
}
|
||||
public Long getState() {
|
||||
return state;
|
||||
}
|
||||
public void setState(Long state) {
|
||||
this.state = state;
|
||||
}
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
public Long getUpgradeTime() {
|
||||
return upgradeTime;
|
||||
}
|
||||
public void setUpgradeTime(Long upgradeTime) {
|
||||
this.upgradeTime = upgradeTime;
|
||||
}
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
public String getUserpwd() {
|
||||
return userpwd;
|
||||
}
|
||||
public void setUserpwd(String userpwd) {
|
||||
this.userpwd = userpwd;
|
||||
}
|
||||
public Long getOldTaskId() {
|
||||
return oldTaskId;
|
||||
}
|
||||
public void setOldTaskId(Long oldTaskId) {
|
||||
this.oldTaskId = oldTaskId;
|
||||
}
|
||||
|
||||
}
|
||||
66
src/com/nis/nmsclient/thread/WritePidThread.java
Normal file
66
src/com/nis/nmsclient/thread/WritePidThread.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.nis.nmsclient.thread;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
|
||||
public class WritePidThread implements Runnable{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String SYSTEM_PATH = System.getProperty("user.dir");
|
||||
// Thread.currentThread().setName("写PID线程");
|
||||
Thread.currentThread().setName("Write The PID Thread");
|
||||
Logger logger = Logger.getLogger(WritePidThread.class);
|
||||
|
||||
/* 获取程序运行PID */
|
||||
String path = Contants.localAgentPidFile;//2015-11-25:之前一直写的是"NMSClientPid.temp",不对配置的是agentPid.temp
|
||||
String name = ManagementFactory.getRuntimeMXBean().getName();
|
||||
logger.info("当前程序PID:>"+(name.split("@")[0]));
|
||||
|
||||
/* 判断系统类型是否写文件 */
|
||||
String os = System.getProperty("os.name");
|
||||
if(os!=null && !os.toLowerCase().startsWith("windows")){
|
||||
logger.info("非Windows系统 结束执行");
|
||||
return ;
|
||||
}
|
||||
/* 获取输出文件并检查文件路径是否存在 */
|
||||
File file = new File(path);
|
||||
if(!file.getParentFile().exists()){
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
/* 将PID写入文件 */
|
||||
FileWriter writer = null;
|
||||
try {
|
||||
writer = new FileWriter(file);
|
||||
writer.write(name.split("@")[0]);
|
||||
writer.flush();
|
||||
logger.info("写PID完成");
|
||||
} catch (IOException e) {
|
||||
logger.error("Write PID failure", e);
|
||||
}finally{
|
||||
try {
|
||||
if(writer!=null)
|
||||
writer.close();
|
||||
writer = null;
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
logger.info("线程关闭");
|
||||
}
|
||||
}
|
||||
public static void main(String [] args) {
|
||||
new Thread(new WritePidThread()).start();
|
||||
}
|
||||
public static void pl (Object obj) {
|
||||
System.out.println(obj==null?null:obj.toString());
|
||||
}
|
||||
|
||||
}
|
||||
146
src/com/nis/nmsclient/thread/alarm/AlarmPO.java
Normal file
146
src/com/nis/nmsclient/thread/alarm/AlarmPO.java
Normal file
@@ -0,0 +1,146 @@
|
||||
package com.nis.nmsclient.thread.alarm;
|
||||
|
||||
|
||||
public class AlarmPO {
|
||||
/**
|
||||
* 监测ID
|
||||
*/
|
||||
private long id;
|
||||
/**
|
||||
* 监测类型
|
||||
*/
|
||||
private String type;
|
||||
/**
|
||||
* 监测设置名称
|
||||
*/
|
||||
private String procIden;
|
||||
/**
|
||||
* 时间间隔(单位:分钟)
|
||||
*/
|
||||
private long checkGap;
|
||||
/**
|
||||
* 是否是进程
|
||||
*/
|
||||
private boolean isproc;
|
||||
/**
|
||||
* 进程路径与名称
|
||||
*/
|
||||
private String proc;
|
||||
/**
|
||||
* PID文件
|
||||
*/
|
||||
private String pidFileStr;
|
||||
/**
|
||||
* 进程搜索关键字
|
||||
*/
|
||||
private String processSearchKeyCode;
|
||||
/**
|
||||
* 控制启动时间
|
||||
*/
|
||||
private Long controlStartTime;
|
||||
/**
|
||||
* 是否系统启动(NMSAgent启动/第三方自己启动);默认0自启动;1NMSAgent启动
|
||||
*/
|
||||
private String IsControlStart;
|
||||
|
||||
public AlarmPO() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AlarmPO(long id, String type, String procIden, long checkGap,
|
||||
boolean isproc, String proc, String pidFileStr,
|
||||
Long controlStartTime, String processSearchKeyCode, String IsControlStart) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.procIden = procIden;
|
||||
this.checkGap = checkGap;
|
||||
this.isproc = isproc;
|
||||
this.proc = proc;
|
||||
this.pidFileStr = pidFileStr;
|
||||
this.controlStartTime = controlStartTime;
|
||||
this.processSearchKeyCode = processSearchKeyCode;
|
||||
this.IsControlStart = IsControlStart;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getProcIden() {
|
||||
return procIden;
|
||||
}
|
||||
|
||||
public void setProcIden(String procIden) {
|
||||
this.procIden = procIden;
|
||||
}
|
||||
|
||||
public long getCheckGap() {
|
||||
return checkGap;
|
||||
}
|
||||
|
||||
public void setCheckGap(long checkGap) {
|
||||
this.checkGap = checkGap;
|
||||
}
|
||||
|
||||
public boolean isIsproc() {
|
||||
return isproc;
|
||||
}
|
||||
|
||||
public void setIsproc(boolean isproc) {
|
||||
this.isproc = isproc;
|
||||
}
|
||||
|
||||
public String getProc() {
|
||||
return proc;
|
||||
}
|
||||
|
||||
public void setProc(String proc) {
|
||||
this.proc = proc;
|
||||
}
|
||||
|
||||
public String getPidFileStr() {
|
||||
return pidFileStr;
|
||||
}
|
||||
|
||||
public void setPidFileStr(String pidFileStr) {
|
||||
this.pidFileStr = pidFileStr;
|
||||
}
|
||||
|
||||
public Long getControlStartTime() {
|
||||
return controlStartTime;
|
||||
}
|
||||
|
||||
public void setControlStartTime(Long controlStartTime) {
|
||||
this.controlStartTime = controlStartTime;
|
||||
}
|
||||
|
||||
public String getProcessSearchKeyCode() {
|
||||
return processSearchKeyCode;
|
||||
}
|
||||
|
||||
public void setProcessSearchKeyCode(String processSearchKeyCode) {
|
||||
this.processSearchKeyCode = processSearchKeyCode;
|
||||
}
|
||||
|
||||
public String getIsControlStart() {
|
||||
return IsControlStart;
|
||||
}
|
||||
|
||||
public void setIsControlStart(String isControlStart) {
|
||||
IsControlStart = isControlStart;
|
||||
}
|
||||
|
||||
}
|
||||
315
src/com/nis/nmsclient/thread/alarm/AlarmThread.java
Normal file
315
src/com/nis/nmsclient/thread/alarm/AlarmThread.java
Normal file
@@ -0,0 +1,315 @@
|
||||
package com.nis.nmsclient.thread.alarm;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.config.DetecConfOper;
|
||||
import com.nis.nmsclient.util.DateUtil;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.ProcessUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
/**
|
||||
* 主动报警线程:针对当前所有有效监测设置进行主动报警
|
||||
* 定时监测某设置是否正常生成数据,N次未取到数据后向Server报警,恢复正常后向Server发送提示信息
|
||||
* @date Mar 22, 2012
|
||||
* @author zhenzhen
|
||||
* @version
|
||||
*/
|
||||
public class AlarmThread implements Runnable{
|
||||
Logger logger = Logger.getLogger(AlarmThread.class);
|
||||
private String name;
|
||||
|
||||
private Map<Long, AlarmStateMsg> alarmSmMap = new HashMap<Long, AlarmStateMsg>();
|
||||
|
||||
public AlarmThread(String name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Thread.currentThread().setName(name);
|
||||
|
||||
logger.debug("主动预警检查开始 ~~~~~~~");
|
||||
try{
|
||||
Map<Long, AlarmPO> alarmPOs = new HashMap<Long, AlarmPO>(Common
|
||||
.getAlarmPOs());
|
||||
// 清除垃圾数据
|
||||
if (alarmSmMap.size() > alarmPOs.size()) {
|
||||
Iterator<Long> it = alarmSmMap.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Long key = it.next();
|
||||
if(!alarmPOs.containsKey(key)){
|
||||
//alarmSmMap.remove(key);
|
||||
it.remove();//update by jinsj for 2013-07-08 java.util.ConcurrentModificationException
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Set<Map.Entry<Long, AlarmPO>> entrys = alarmPOs.entrySet();
|
||||
int alarmCnt = 0;
|
||||
StringBuffer alarmDescInfo = new StringBuffer();
|
||||
for (Map.Entry<Long, AlarmPO> entry : entrys) {
|
||||
// 如果监测进程未启动,则不进行告警检查
|
||||
if (entry.getValue().getControlStartTime() != null
|
||||
&& entry.getValue().getControlStartTime() > System
|
||||
.currentTimeMillis()) {
|
||||
continue;
|
||||
}
|
||||
// 对各监测设置告警状态信息进行赋初值
|
||||
Long key = entry.getValue().getId();
|
||||
if (alarmSmMap.get(key) == null) {
|
||||
alarmSmMap.put(key, new AlarmStateMsg(new Date(), 0,
|
||||
new StringBuffer(), false));
|
||||
}else if(alarmSmMap.get(key).getAlarmMsg().length() == 0){
|
||||
alarmSmMap.get(key).setStart(new Date());
|
||||
}
|
||||
|
||||
boolean flag = alarm(entry.getValue());
|
||||
if(flag){
|
||||
alarmCnt += 1;
|
||||
alarmDescInfo.append("\tsetId: " + key + " >> " + entry.getValue().getType() + "_" + entry.getValue().getProcIden());
|
||||
alarmDescInfo.append("\n");
|
||||
}
|
||||
}
|
||||
logger.info("本次预警轮循检查监测设置总数:" + alarmPOs.size() + ",正常:"
|
||||
+ (alarmPOs.size() - alarmCnt) + ",不正常:" + alarmCnt);
|
||||
if(alarmCnt>0){
|
||||
logger.info("本次预警检查不正常的监测如下:\n" + alarmDescInfo.toString());
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("Active early warning thread exception:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
logger.debug("主动预警检查结束 ~~~~~~~");
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对一个监测设置的主动预警具体逻辑实现
|
||||
* @param alarm
|
||||
* @return true 告警监测不正常 false 监测正常
|
||||
* @throws Exception
|
||||
*/
|
||||
public boolean alarm(AlarmPO alarm) throws Exception{
|
||||
Long key = alarm.getId();
|
||||
String sonDirName = DetecConfOper.getFileName(alarm.getType(), alarm.getProcIden(), null);
|
||||
StringBuffer sbMsg = new StringBuffer();
|
||||
/**
|
||||
* 1、检查是否存在数据: alarmLevel 0 -- 数据存在,非0 -- 数据不存在
|
||||
*/
|
||||
int alarmLevel = checkDataIsExist(sonDirName, alarm.getCheckGap(), sbMsg);
|
||||
|
||||
/**
|
||||
* 2、判断进程是否存在:是进程且数据不存在才判断,不是进程如系统类型CPU,则不进行这些操作
|
||||
*/
|
||||
if(alarmLevel!=0 && alarm.isIsproc()){
|
||||
if("2".equals(alarm.getIsControlStart())) { // NC周期启动(脚本间断性执行且执行时间较短,通过判断Java定时任务确认进程)
|
||||
if(Common.containPluginDetecFuture(key)){ // 进程存在
|
||||
String temp = sbMsg.toString();
|
||||
sbMsg.delete(0, sbMsg.length());
|
||||
// sbMsg.append("进程存在,但" + temp);
|
||||
sbMsg.append("i18n_client.AlarmThread.processExists_n81i" + temp);
|
||||
alarmLevel = 2;
|
||||
} else { // 进程不存在
|
||||
sbMsg.delete(0, sbMsg.length());
|
||||
// sbMsg.append("定时任务“" + alarm.getType() + "”不存在");
|
||||
sbMsg.append("i18n_client.AlarmThread.loopMission1_n81i“" + alarm.getType() + "”i18n_client.AlarmThread.loopMission2_n81i");
|
||||
alarmLevel = 1;
|
||||
}
|
||||
} else {
|
||||
Object[] objArr = ProcessUtil.checkPidAndGetPid(alarm.getPidFileStr(), alarm.getProcessSearchKeyCode());
|
||||
int isExistFlag = Integer.parseInt(objArr[0].toString());
|
||||
String pidInfo = objArr[1].toString();
|
||||
|
||||
if(isExistFlag==0){// 进程不存在(包括PID文件不存在,PID值不存在,搜索关键字也找不到进程)
|
||||
sbMsg.delete(0, sbMsg.length());
|
||||
sbMsg.append(pidInfo);
|
||||
alarmLevel = 1;
|
||||
} else {// 进程存在(一个进程或多个进程)
|
||||
String temp = sbMsg.toString();
|
||||
sbMsg.delete(0, sbMsg.length());
|
||||
// sbMsg.append("进程存在,但" + temp);
|
||||
sbMsg.append("i18n_client.AlarmThread.processExists_n81i" + temp);
|
||||
alarmLevel = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 3、统计并发送报警信息
|
||||
*/
|
||||
AlarmStateMsg asMsg= alarmSmMap.get(key);
|
||||
int times = asMsg.getTimes();
|
||||
boolean flag = true;
|
||||
if (sbMsg.length()>0 && sbMsg.toString().equals(asMsg.getAlarmMsg().toString())) {
|
||||
times ++ ;
|
||||
|
||||
if (times == Contants.noDataTimes) {// Contants.noDataTime次未取到数据,报警
|
||||
AlarmUtil.sendAlarmMsg(alarm, asMsg.getStart(), new Date(),
|
||||
alarmLevel, Contants.DETECTION_STATUS_ABNORMAL, asMsg
|
||||
.getAlarmMsg().toString()
|
||||
// + ",此状态持续了" + times + "次");
|
||||
+ ",i18n_client.AlarmThread.state_n81i" + times + "i18n_client.AlarmThread.times_n81i");
|
||||
asMsg.setAlarmState(true);
|
||||
}
|
||||
} else if (sbMsg.length() > 0
|
||||
&& !sbMsg.toString().equals(asMsg.getAlarmMsg().toString())) {
|
||||
asMsg.getAlarmMsg().delete(0, asMsg.getAlarmMsg().length());
|
||||
asMsg.getAlarmMsg().append(sbMsg.toString());
|
||||
times = 1;
|
||||
} else {
|
||||
asMsg.getAlarmMsg().delete(0, asMsg.getAlarmMsg().length());
|
||||
times = 0;
|
||||
if(asMsg.isAlarmState()){
|
||||
/*AlarmUtil.sendAlarmMsg(alarm, asMsg.getStart(), new Date(),
|
||||
99, Contants.DETECTION_STATUS_NORMAL, "恢复正常");*/
|
||||
asMsg.setAlarmState(false);
|
||||
}
|
||||
flag = false;
|
||||
}
|
||||
asMsg.setTimes(times);
|
||||
alarmSmMap.put(key, asMsg);
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否存在数据
|
||||
* @param sonDirName
|
||||
* @param checkGap
|
||||
* @return
|
||||
*/
|
||||
private int checkDataIsExist(String sonDirName, long checkGap, StringBuffer sbMsg){
|
||||
int alarmLevel = 0;
|
||||
// ------- 1、先查看当前正在写入的数据文件夹
|
||||
File curDir = new File(Contants.localDataFilePath + File.separator + sonDirName);
|
||||
if(!curDir.exists()){
|
||||
// sbMsg.append("数据文件目录“" + curDir.getAbsolutePath() + "”不存在");
|
||||
sbMsg.append("i18n_client.AlarmThread.dataFileNotExists1_n81i“" + curDir.getAbsolutePath() + "”i18n_client.AlarmThread.dataFileNotExists2_n81i");
|
||||
alarmLevel = 1;
|
||||
return alarmLevel;
|
||||
}
|
||||
long timeMillis = System.currentTimeMillis() - Contants.noDataTimes * checkGap * 60 * 1000;
|
||||
File[] rightFiles = FileUtil.getFilesAfterMillis(curDir, timeMillis);
|
||||
if(rightFiles!=null && rightFiles.length>0){// 找到数据,正常
|
||||
alarmLevel = 0;
|
||||
return alarmLevel;
|
||||
}
|
||||
|
||||
// -------- 2、如果当前正在写入的数据文件夹没有找到符合条件的数据,则查找上传成功后备份到的相应目录
|
||||
// 2013-3-28 修改内容:原本只检查当天日期目录是否存在数据,针对监测时间间隔超过一天的存在问题,所以当间隔时间大于等于一天时,改为检查当天日期之前的目录
|
||||
String dateDirName = DateUtil.getCurrentDate(DateUtil.YYYYMMDD);
|
||||
File doneDir = new File(Contants.localDataDonePath + File.separator + sonDirName);
|
||||
if(doneDir.exists()){
|
||||
if(checkGap >= 1440){// --- 检查当天日期及之前的目录
|
||||
// 找到指定的日期目录及之前的日期目录,将其降序排列,是为了最先查找当天日期
|
||||
File[] dateDirs = FileUtil.sortDescByFileName(FileUtil.getDirsBeforeDateName(doneDir, dateDirName));
|
||||
// 在找到的日期目录下检查文件是否存在
|
||||
for(File dateDir : dateDirs){
|
||||
rightFiles = FileUtil.getFilesAfterMillis(dateDir, timeMillis);
|
||||
if(rightFiles!=null && rightFiles.length>0){// 在任一目录找到则不再继续查找其他目录
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else {// --- 只检查当天日期
|
||||
File dateDir = new File(doneDir.getAbsolutePath() + File.separator + dateDirName);
|
||||
if(dateDir.exists()){
|
||||
rightFiles = FileUtil.getFilesAfterMillis(dateDir, timeMillis);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(rightFiles!=null && rightFiles.length>0){// 找到数据,正常
|
||||
alarmLevel = 0;
|
||||
return alarmLevel;
|
||||
}
|
||||
|
||||
// -------- 3、查找不完整数据即0大小文件备份到的相应目录
|
||||
File[] errorFiles = null;
|
||||
File errorDir = new File(Contants.localDataErrorPath + File.separator + sonDirName);
|
||||
if(errorDir.exists()){
|
||||
if(checkGap >= 1440){// --- 检查当天日期及之前的目录
|
||||
// 找到指定的日期目录及之前的日期目录,将其降序排列,是为了最先查找当天日期
|
||||
File[] dateDirs = FileUtil.sortDescByFileName(FileUtil.getDirsBeforeDateName(errorDir, dateDirName));
|
||||
// 在找到的日期目录下检查文件是否存在
|
||||
for(File dateDir : dateDirs){
|
||||
errorFiles = FileUtil.getFilesAfterMillis(dateDir, timeMillis);
|
||||
if(errorFiles!=null && errorFiles.length>0){// 在任一目录找到则不再继续查找其他目录
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else {// --- 只检查当天日期
|
||||
File dateDir = new File(doneDir.getAbsolutePath() + File.separator + dateDirName);
|
||||
if(dateDir.exists()){
|
||||
errorFiles = FileUtil.getFilesAfterMillis(errorDir, timeMillis);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if(errorFiles!=null && errorFiles.length>0){
|
||||
// sbMsg.append("监测数据文件大小为0");
|
||||
sbMsg.append("i18n_client.AlarmThread.dataSize_n81i");
|
||||
alarmLevel = 1;
|
||||
}else{
|
||||
// sbMsg.append("未取到监测数据");
|
||||
sbMsg.append("i18n_client.AlarmThread.noDetecateData_n81i");
|
||||
alarmLevel = 1;
|
||||
}
|
||||
|
||||
return alarmLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 告警检查过程中针对每个监测设置的过程变量状态的封装类
|
||||
* @date Mar 22, 2012
|
||||
* @author zhenzhen
|
||||
* @version
|
||||
*/
|
||||
class AlarmStateMsg {
|
||||
private Date start;//告警检查不正常情况的开始时间
|
||||
private Integer times;//连续告警次数
|
||||
private StringBuffer alarmMsg;//告警信息
|
||||
private boolean alarmState;//是否向Server发送了主动告警请求
|
||||
|
||||
public AlarmStateMsg(Date start, Integer times, StringBuffer alarmMsg,
|
||||
boolean alarmState) {
|
||||
super();
|
||||
this.start = start;
|
||||
this.times = times;
|
||||
this.alarmMsg = alarmMsg;
|
||||
this.alarmState = alarmState;
|
||||
}
|
||||
|
||||
public Date getStart() {
|
||||
return start;
|
||||
}
|
||||
public void setStart(Date start) {
|
||||
this.start = start;
|
||||
}
|
||||
public Integer getTimes() {
|
||||
return times;
|
||||
}
|
||||
public void setTimes(Integer times) {
|
||||
this.times = times;
|
||||
}
|
||||
public StringBuffer getAlarmMsg() {
|
||||
return alarmMsg;
|
||||
}
|
||||
public void setAlarmMsg(StringBuffer alarmMsg) {
|
||||
this.alarmMsg = alarmMsg;
|
||||
}
|
||||
public boolean isAlarmState() {
|
||||
return alarmState;
|
||||
}
|
||||
public void setAlarmState(boolean alarmState) {
|
||||
this.alarmState = alarmState;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
79
src/com/nis/nmsclient/thread/alarm/AlarmUtil.java
Normal file
79
src/com/nis/nmsclient/thread/alarm/AlarmUtil.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package com.nis.nmsclient.thread.alarm;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.Future;
|
||||
import org.apache.log4j.Logger;
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.config.DetecConfOper;
|
||||
import com.nis.nmsclient.model.SetInfo;
|
||||
import com.nis.nmsclient.thread.socket.CommonSocket;
|
||||
import com.nis.nmsclient.thread.socket.SSLClient;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
public class AlarmUtil {
|
||||
static Logger logger = Logger.getLogger(AlarmUtil.class);
|
||||
|
||||
public static void sendAlarmMsg(AlarmPO alarm, Date start,
|
||||
Date end, int alarmLevel, int state, String alarmMsg,String... showNum) {
|
||||
sendAlarmMsg(alarm.getId(), alarm.getType(), alarm
|
||||
.getProcIden(), start, end, alarmLevel, state, alarmMsg,showNum);
|
||||
}
|
||||
|
||||
public static void sendAlarmMsg(Long alarmId, String checkType,
|
||||
String procIden, Date start, Date end, int alarmLevel, int state,
|
||||
String alarmMsg,String... showNum) {
|
||||
String seprator = Contants.COMMON_MSG_SEPRATOR;
|
||||
String showAlarmNum="";
|
||||
if (showNum!=null && showNum.length>0)
|
||||
{
|
||||
showAlarmNum = showNum[0];
|
||||
}
|
||||
String msg = alarmId + seprator + Contants.AGENT_HOST_UUID + seprator
|
||||
+ checkType + seprator + procIden + seprator + start.getTime()
|
||||
+ seprator + end.getTime() + seprator + alarmLevel + seprator
|
||||
+ state + seprator + alarmMsg+ seprator + showAlarmNum;
|
||||
|
||||
Common.service.submit(new SSLClient(Thread.currentThread()
|
||||
.getName(), CommonSocket.REQ_ALARM, msg));
|
||||
|
||||
logger.warn("Active alarm level" + alarmLevel + " >> setId: " + alarmId
|
||||
+ "," + checkType + "_" + procIden + ",state: " + state + " > "
|
||||
+ alarmMsg+ ",showNum: " +showAlarmNum);
|
||||
}
|
||||
|
||||
public static AlarmPO getAlarmPO(SetInfo setInfo){
|
||||
AlarmPO alarmPO = new AlarmPO(setInfo.getId(), setInfo
|
||||
.getCheckTypeName(), setInfo.getProcessIden(), setInfo
|
||||
.getCheckGap(), DetecConfOper.isProcess(setInfo), DetecConfOper
|
||||
.getProcess(setInfo), setInfo.getProcessFile(), setInfo
|
||||
.getControlStartTime(), setInfo.getProcessSearchKeyCode(),
|
||||
setInfo.getIsControlStart());
|
||||
|
||||
return alarmPO;
|
||||
}
|
||||
|
||||
// 向DC发送NC端的异常信息
|
||||
public static void sendNMSErrorMsg(ErrorCode errorCode, String errorIp, String errorDesc) {
|
||||
String seprator = Contants.COMMON_MSG_SEPRATOR;
|
||||
try {
|
||||
//异常信息顺序:CODE、发送IP、发生错误IP、发生错误时间、处理状态(1)、错误描述
|
||||
String msg = errorCode.toString() + seprator + Utils.getLocalIp() + seprator + errorIp
|
||||
+ seprator + System.currentTimeMillis() + seprator + "1"
|
||||
+ seprator + errorDesc;
|
||||
|
||||
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
|
||||
.getName(), CommonSocket.REQ_ERROR_INFO, msg));
|
||||
|
||||
if(errorCode.equals(ErrorCode.ProtListenerError)){
|
||||
future.get();
|
||||
}
|
||||
|
||||
logger.error("Abnormal information " + errorCode + " >> errorIp: " + errorIp + " > "
|
||||
+ errorDesc);
|
||||
} catch (Exception e) {
|
||||
logger.error(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
11
src/com/nis/nmsclient/thread/alarm/ErrorCode.java
Normal file
11
src/com/nis/nmsclient/thread/alarm/ErrorCode.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.nis.nmsclient.thread.alarm;
|
||||
|
||||
public enum ErrorCode {
|
||||
ProcessNotExist, //进程不存在
|
||||
MultipleProcessExist,//多个进程存在
|
||||
ThreadRuntimeError, //执行异常
|
||||
HandShakeError, //通讯握手失败
|
||||
ProtListenerError, //端口监听失败
|
||||
DeamonNotExist, //守护进程不存在
|
||||
SocketError//通讯失败
|
||||
}
|
||||
191
src/com/nis/nmsclient/thread/plugin/StartPluginRun.java
Normal file
191
src/com/nis/nmsclient/thread/plugin/StartPluginRun.java
Normal file
@@ -0,0 +1,191 @@
|
||||
package com.nis.nmsclient.thread.plugin;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.config.DetecConfOper;
|
||||
import com.nis.nmsclient.model.SetInfo;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmUtil;
|
||||
import com.nis.nmsclient.util.ProcessUtil;
|
||||
import com.nis.systeminfo.thread.NewPluginResultMerge;
|
||||
|
||||
public class StartPluginRun implements Runnable {
|
||||
static Logger logger = Logger.getLogger(StartPluginRun.class);
|
||||
private String name;
|
||||
private SetInfo setInfo;
|
||||
|
||||
public StartPluginRun(SetInfo setInfo, String name) {
|
||||
this.name = name;
|
||||
this.setInfo = setInfo;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Thread.currentThread().setName(name);
|
||||
|
||||
logger.info("启动第三方插件程序开始!");
|
||||
if ("1".equals(setInfo.getCheckState())
|
||||
&& "1".equals(setInfo.getCheckWay())
|
||||
&& "1,2".contains(setInfo.getIsControlStart())) {// 有效,被动方式获取,Agent端启动
|
||||
Date startTime = null;
|
||||
if (setInfo.getControlStartTime() != null && setInfo.getControlStartTime().longValue() != 0) {
|
||||
startTime = new Date(setInfo.getControlStartTime());
|
||||
} else {
|
||||
startTime = new Date();
|
||||
}
|
||||
|
||||
/*
|
||||
* 停止未启动的定时任务/周期任务
|
||||
*/
|
||||
Common.stopPluginDetecFuture(setInfo.getId(), Thread.currentThread().getName());
|
||||
|
||||
/*
|
||||
* 首先看进程设置的启动时间是否过时:如果过时,则查看进程是否存在,不存在启动;如果未过时,则定时启动
|
||||
*/
|
||||
String cmd = DetecConfOper.getProcess(setInfo);
|
||||
try {
|
||||
/**
|
||||
* 进程存在与否的判断标准:
|
||||
* 1、先检查PID文件,取出PID,验证PID指定进程是否存在
|
||||
* PID文件不存在,或者PID文件中内容为空,或者取出的PID进程不存在,都认为不存在,进行下一步检查
|
||||
* 2、不存在再使用搜索关键字查找进程
|
||||
* 若未找出进程,不存在,若找出一个进程,正常存在,若找出多个进程,报警
|
||||
*/
|
||||
|
||||
Object[] objArr = ProcessUtil.checkPidAndGetPid(setInfo.getProcessFile(), setInfo.getProcessSearchKeyCode());
|
||||
int isExistFlag = Integer.parseInt(objArr[0].toString());
|
||||
String pidInfo = objArr[1].toString();
|
||||
|
||||
if (isExistFlag == 0 || isExistFlag == 1) { // 进程不存在/存在一个进程
|
||||
if (isExistFlag == 1) {// 存在一个进程
|
||||
logger.info("三方监测进程”" + cmd + "“已存在,重启启动");
|
||||
ProcessUtil.killProcess(pidInfo);
|
||||
}
|
||||
|
||||
long delay = startTime.getTime() - System.currentTimeMillis();
|
||||
delay = Math.max(delay, 0);
|
||||
|
||||
ScheduledFuture<?> future = null;
|
||||
if ("1".equals(setInfo.getIsControlStart())) {
|
||||
future = Common.scheduled.schedule(new ExecProcess(), delay, TimeUnit.MILLISECONDS);
|
||||
} else if ("2".equals(setInfo.getIsControlStart())) {
|
||||
long period = setInfo.getCheckGap() * 60 * 1000;
|
||||
future = Common.scheduled.scheduleAtFixedRate(new ExecProcess2(), delay, period, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
Common.putPluginDetecFuture(setInfo.getId(), future);
|
||||
logger.info("三方监测程序已加入定时器定时启动");
|
||||
|
||||
} else { // 找到多个进程,告警
|
||||
// String alarmMsg = "启动三方监测:" + pidInfo;
|
||||
String alarmMsg = "i18n_client.StartPluginRun.startupDetecate_n81i:" + pidInfo;
|
||||
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo
|
||||
.getCheckTypeName(), setInfo.getProcessIden(),
|
||||
new Date(), new Date(), 1,
|
||||
Contants.DETECTION_STATUS_FAILURE, alarmMsg);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Start the three party monitoring exception. Please check whether the process execution file and the PID file are set correctly:" + cmd, e);
|
||||
}
|
||||
}
|
||||
logger.info("启动第三方插件程序结束!");
|
||||
}// run end
|
||||
|
||||
class ExecProcess implements Runnable {
|
||||
public void run() {
|
||||
Thread.currentThread().setName(name);
|
||||
String cmd = DetecConfOper.getProcess(setInfo);
|
||||
try {
|
||||
// 启动
|
||||
ProcessUtil.runExec(cmd, null, null, null);
|
||||
|
||||
// 检查PID
|
||||
Object[] objArr = ProcessUtil.checkPidAndGetPid(setInfo.getProcessFile(), setInfo.getProcessSearchKeyCode());
|
||||
int isExistFlag = Integer.parseInt(objArr[0].toString());
|
||||
String pidInfo = objArr[1].toString();
|
||||
|
||||
if (isExistFlag == 1) {// 存在, 一个进程
|
||||
logger.info("三方监测程序“" + cmd + "“启动成功");
|
||||
} else { // 进程不存在 或 找到多个进程,告警
|
||||
String alarmMsg = null;
|
||||
if (isExistFlag == 0) {// 进程不存在
|
||||
// alarmMsg = "三方监测程序启动失败,请检查进程启动文件“" + cmd + "”是否设置正确";
|
||||
alarmMsg = "i18n_client.StartPluginRun.startupDetecateErr1_n81i “" + cmd + "” i18n_client.StartPluginRun.startupDetecateErr1.isTrue_n81i";
|
||||
} else {// 找到多个进程
|
||||
// alarmMsg = "启动三方监测:" + pidInfo;
|
||||
alarmMsg = "i18n_client.StartPluginRun.startupDetecate_n81i:" + pidInfo;
|
||||
}
|
||||
AlarmUtil.sendAlarmMsg(setInfo.getId(), setInfo
|
||||
.getCheckTypeName(), setInfo.getProcessIden(),
|
||||
new Date(), new Date(), 1,
|
||||
Contants.DETECTION_STATUS_FAILURE, alarmMsg);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Start the three party monitoring exception. Please check whether the process execution file and the PID file are set correctly:" + cmd, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ExecProcess2 implements Runnable {
|
||||
public void run() {
|
||||
|
||||
Thread.currentThread().setName(name);
|
||||
// 三方监测脚本执行命令不存在,获取脚本命令及关键词
|
||||
// 因脚本下发与配置下发不同步,需要在执行任务中多次获取执行命令
|
||||
String cmd = Common.generateCommandAndKeyword(setInfo);
|
||||
try {
|
||||
boolean executeStatus = true;
|
||||
|
||||
// 脚本执行前,清理临时数据目录
|
||||
new NewPluginResultMerge().clearTmpFile(setInfo);
|
||||
|
||||
if (StringUtils.isBlank(cmd)) {
|
||||
// 三方监测脚本执行命令不存在,当前时间NC上无监测脚本
|
||||
logger.info("三方监测程序启动失败,监测脚本下发未完成!");
|
||||
// String alarmMsg = "启动三方监测异常,监测脚本下发未完成!";
|
||||
String alarmMsg = "i18n_client.StartPluginRun.startupDetecateErr2_n81i";
|
||||
AlarmUtil.sendAlarmMsg(setInfo.getId(),
|
||||
setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), new Date(), new Date(),
|
||||
1, Contants.DETECTION_STATUS_FAILURE, alarmMsg);
|
||||
executeStatus = false;
|
||||
|
||||
} else {
|
||||
// 启动脚本
|
||||
String msg = ProcessUtil.runExec(cmd, null, null, null);
|
||||
|
||||
if (StringUtils.isBlank(msg)) { // 执行成功
|
||||
logger.info("三方监测程序“" + cmd + "”执行成功");
|
||||
|
||||
} else { // 执行失败,返回错误信息
|
||||
logger.error("Three party monitoring procedure“" + cmd + "”erroneous execution:" + msg);
|
||||
// String alarmMsg = "启动三方监测异常,监测脚本执行错误!";
|
||||
String alarmMsg = "i18n_client.StartPluginRun.startupDetecateErr3_n81i";
|
||||
AlarmUtil.sendAlarmMsg(setInfo.getId(),
|
||||
setInfo.getCheckTypeName(),
|
||||
setInfo.getProcessIden(), new Date(),
|
||||
new Date(), 1,
|
||||
Contants.DETECTION_STATUS_FAILURE, alarmMsg);
|
||||
executeStatus = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 针对执行合并临时文件(ProcessUtil.runExec为同步执行,合并临时文件前已完成脚本执行过程)
|
||||
if(executeStatus) {
|
||||
new NewPluginResultMerge().merge(setInfo);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Start the three party monitoring exception. Please check whether the process execution file and the PID file are set correctly:" + cmd, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
1199
src/com/nis/nmsclient/thread/socket/CommonSocket.java
Normal file
1199
src/com/nis/nmsclient/thread/socket/CommonSocket.java
Normal file
File diff suppressed because it is too large
Load Diff
238
src/com/nis/nmsclient/thread/socket/SSLCertOper.java
Normal file
238
src/com/nis/nmsclient/thread/socket/SSLCertOper.java
Normal file
@@ -0,0 +1,238 @@
|
||||
package com.nis.nmsclient.thread.socket;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.security.KeyStore;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.util.DateUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
public class SSLCertOper {
|
||||
static Logger logger = Logger.getLogger(SSLCertOper.class);
|
||||
|
||||
public static SSLContext getSSLContext() throws Exception {
|
||||
// 初始化上下文
|
||||
SSLContext ctx = SSLContext.getInstance(Contants.SSL_JSSE_TYPE);
|
||||
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
||||
KeyStore ks = KeyStore.getInstance(Contants.SSL_KEYSTORE_TYPE);
|
||||
ks.load(new FileInputStream(Contants.SSL_KEY_STORE),
|
||||
Contants.SSL_KEY_STORE_PASS.toCharArray());// 载入keystore
|
||||
kmf.init(ks, Contants.SSL_KEY_PRIVATE_PASS.toCharArray());
|
||||
|
||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
|
||||
KeyStore tks = KeyStore.getInstance(Contants.SSL_KEYSTORE_TYPE);
|
||||
tks.load(new FileInputStream(Contants.SSL_TRUST_KEY_STORE),
|
||||
Contants.SSL_KEY_STORE_PASS.toCharArray());// 载入keystore
|
||||
tmf.init(tks);
|
||||
|
||||
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(),
|
||||
new SecureRandom());
|
||||
logger.debug("load keystore success.");
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 密匙对(私钥和公钥)
|
||||
*
|
||||
*/
|
||||
public static String createKeyAndCert(String aliasName, String storePath,
|
||||
String localIp, String keyPass, String storePass, String certName) {
|
||||
BufferedReader bReader = null;
|
||||
Process process = null;
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(
|
||||
"keytool -genkey -v -alias " + aliasName
|
||||
+ " -keyalg RSA -storetype "
|
||||
+ Contants.SSL_KEYSTORE_TYPE + " -keystore "
|
||||
+ storePath + " -validity 90 -dname \"CN="
|
||||
+ localIp
|
||||
+ ",OU=cn,O=cn,L=cn,ST=cn,C=cn\" -storepass "
|
||||
+ storePass + " -keypass " + keyPass);
|
||||
process.getOutputStream().close();
|
||||
bReader = new BufferedReader(new InputStreamReader(process
|
||||
.getInputStream()));
|
||||
process.getErrorStream().close();
|
||||
String line = null;
|
||||
while ((line = bReader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
process = Runtime.getRuntime().exec(
|
||||
"keytool -export -alias " + aliasName + " -storetype "
|
||||
+ Contants.SSL_KEYSTORE_TYPE + " -keystore "
|
||||
+ storePath + " -file " + certName + " -storepass "
|
||||
+ storePass + "");
|
||||
|
||||
bReader = new BufferedReader(new InputStreamReader(process
|
||||
.getInputStream()));
|
||||
while ((line = bReader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
return certName;
|
||||
} catch (IOException e) {
|
||||
logger.error("Create a key pair error!");
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
return null;
|
||||
} finally {
|
||||
if (bReader != null) {
|
||||
try {
|
||||
bReader.close();
|
||||
} catch (IOException e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将公钥引入KeyStore
|
||||
*
|
||||
*/
|
||||
public static boolean importCertToStore(String aliasName, String storePath,
|
||||
String certName, String storePass) {
|
||||
BufferedReader bReader = null;
|
||||
PrintWriter pw = null;
|
||||
try {
|
||||
Process process = Runtime.getRuntime().exec(
|
||||
"keytool -import -v -trustcacerts -alias " + aliasName
|
||||
+ " -keystore " + storePath + " -file " + certName
|
||||
+ " -storetype " + Contants.SSL_KEYSTORE_TYPE
|
||||
+ " -storepass " + storePass + "");
|
||||
|
||||
bReader = new BufferedReader(new InputStreamReader(process
|
||||
.getInputStream()));
|
||||
pw = new PrintWriter(process.getOutputStream());
|
||||
|
||||
pw.write("y");
|
||||
pw.flush();
|
||||
pw.close();
|
||||
|
||||
String line = null;
|
||||
while ((line = bReader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
logger.error("Error of importing authentication certificate!");
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
return false;
|
||||
} finally {
|
||||
if (pw != null) {
|
||||
pw.close();
|
||||
}
|
||||
if (bReader != null) {
|
||||
try {
|
||||
bReader.close();
|
||||
} catch (IOException e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除KeyStore库中的密钥
|
||||
*
|
||||
*/
|
||||
public static boolean deleteKeyOrCertFromStore(String aliasName,
|
||||
String storePath, String storePass) {
|
||||
BufferedReader bReader = null;
|
||||
PrintWriter pw = null;
|
||||
try {
|
||||
Process process = Runtime.getRuntime().exec(
|
||||
"keytool -delete -v -alias " + aliasName + " -keystore "
|
||||
+ storePath + " -storetype "
|
||||
+ Contants.SSL_KEYSTORE_TYPE + " -storepass "
|
||||
+ storePass + "");
|
||||
|
||||
bReader = new BufferedReader(new InputStreamReader(process
|
||||
.getInputStream()));
|
||||
pw = new PrintWriter(process.getOutputStream());
|
||||
|
||||
// pw.write("y");
|
||||
pw.flush();
|
||||
pw.close();
|
||||
|
||||
String line = null;
|
||||
while ((line = bReader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
logger.error("Delete" + storePath+ "library Key" + aliasName + "make a mistake!");
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
return false;
|
||||
} finally {
|
||||
if (pw != null) {
|
||||
pw.close();
|
||||
}
|
||||
if (bReader != null) {
|
||||
try {
|
||||
bReader.close();
|
||||
} catch (IOException e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* test main
|
||||
*
|
||||
* @time Aug 28, 2011-12:17:28 PM
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
String newServerKeyName = "serverks"
|
||||
+ DateUtil.getCurrentDate(DateUtil.YYYYMMDD);
|
||||
String newServerKeyPsw = "123456";
|
||||
String newClientkeyName = "clientks"
|
||||
+ DateUtil.getCurrentDate(DateUtil.YYYYMMDD);
|
||||
String newClientkeyPsw = "123456";
|
||||
String filepath0 = SSLCertOper.createKeyAndCert(newServerKeyName,
|
||||
"D:\\workspace\\nms_client\\src\\key\\server_ks", "10.0.6.120",
|
||||
newServerKeyPsw, "server",
|
||||
"D:\\workspace\\nms_client\\src\\key\\server.cer");
|
||||
|
||||
SSLCertOper.importCertToStore(newServerKeyName,
|
||||
"D:\\workspace\\nms_client\\src\\key\\client_ts",
|
||||
"D:\\workspace\\nms_client\\src\\key\\server.cer", "client");
|
||||
|
||||
String filepath1 = SSLCertOper.createKeyAndCert(newClientkeyName,
|
||||
"D:\\workspace\\nms_client\\src\\key\\client_ks", "localhost",
|
||||
newClientkeyPsw, "client",
|
||||
"D:\\workspace\\nms_client\\src\\key\\client.cer");
|
||||
|
||||
SSLCertOper.importCertToStore(newClientkeyName,
|
||||
"D:\\workspace\\nms_client\\src\\key\\server_ts",
|
||||
"D:\\workspace\\nms_client\\src\\key\\client.cer", "server");
|
||||
System.out.println(filepath0);
|
||||
System.out.println(filepath1);
|
||||
// Config.setValueByName("ssl.server.key.old",
|
||||
// Constants.SSL_SERVER_KEY_NEW);
|
||||
// Config.setValueByName("ssl.server.key.old.psw",
|
||||
// Constants.SSL_SERVER_KEY_NEW_PSW);
|
||||
// Config.setValueByName("ssl.server.key.new",newServerKeyName);
|
||||
// Config.setValueByName("ssl.server.key.new.psw", newServerKeyPsw);
|
||||
// Config.setValueByName("ssl.client.key",newClientkeyName);
|
||||
// Config.setValueByName("ssl.client.key.psw", newClientkeyPsw);
|
||||
}
|
||||
}
|
||||
288
src/com/nis/nmsclient/thread/socket/SSLClient.java
Normal file
288
src/com/nis/nmsclient/thread/socket/SSLClient.java
Normal file
@@ -0,0 +1,288 @@
|
||||
package com.nis.nmsclient.thread.socket;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
import net.sf.json.JSONArray;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.model.ReturnFilePO;
|
||||
import com.nis.nmsclient.thread.task.TaskReqHandle;
|
||||
import com.nis.nmsclient.thread.task.TaskResultOper;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
|
||||
/**
|
||||
* 安全通讯的客户端
|
||||
**/
|
||||
|
||||
public class SSLClient extends CommonSocket implements Callable<Object>{
|
||||
Logger logger = Logger.getLogger(SSLClient.class);
|
||||
private String name;
|
||||
private String reqCmd;
|
||||
private Object obj;
|
||||
private String serverHost;
|
||||
|
||||
public SSLClient(String name, String reqCmd, Object obj) {
|
||||
this.name = name;
|
||||
this.reqCmd = reqCmd;
|
||||
this.obj = obj;
|
||||
this.serverHost = Contants.SOCKET_SERVER_HOST;
|
||||
}
|
||||
|
||||
public SSLClient(String name, String reqCmd, Object obj, String serverHost) {
|
||||
this.name = name;
|
||||
this.reqCmd = reqCmd;
|
||||
this.obj = obj;
|
||||
this.serverHost = serverHost;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化客户端Socket
|
||||
**/
|
||||
public void init() throws Exception {
|
||||
SSLContext ctx = SSLCertOper.getSSLContext();
|
||||
SSLSocketFactory ssf = ctx.getSocketFactory();
|
||||
|
||||
socket = (SSLSocket) ssf.createSocket(serverHost,
|
||||
Contants.SOCKET_SERVER_PORT);
|
||||
logger.debug("create socket success.");
|
||||
|
||||
//2014-1-23 hyx 如果建立socket成功,但是startHandshake握手失败,且未设置超时时间时,则会一直阻塞
|
||||
socket.setSoTimeout(1000 * 60 * Contants.SOCKET_TIMEOUT_MINUTES);
|
||||
|
||||
((SSLSocket) socket).startHandshake();
|
||||
logger.debug("handshake success.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
// Thread.currentThread().setName(name + ">>通信 " + serverHost);
|
||||
Thread.currentThread().setName(name + ">>Communication " + serverHost);
|
||||
Object object = null;
|
||||
try {
|
||||
init();
|
||||
if(socket!=null){
|
||||
out = socket.getOutputStream();
|
||||
in = socket.getInputStream();
|
||||
//设置超时时间
|
||||
socket.setSoTimeout(1000 * 60 * Contants.SOCKET_TIMEOUT_MINUTES);
|
||||
object = toDo();
|
||||
}
|
||||
}catch (Exception e) {
|
||||
// object = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "异常";
|
||||
object = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "anomaly";
|
||||
logger.error("Communication anomaly:" + Utils.printExceptionStack(e));
|
||||
} finally {
|
||||
logger.debug("关闭通信");
|
||||
close();
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
protected Object toDo() throws Exception {
|
||||
logger.debug("发送通信请求:" + reqCmd);
|
||||
//-- 无效操作处理
|
||||
if(StringUtils.isEmpty(reqCmd)){
|
||||
return null;
|
||||
}
|
||||
boolean flag = false;
|
||||
String msg = null;
|
||||
String result = null;
|
||||
//-- 命令判断
|
||||
// 与Server通信
|
||||
if(reqCmd.equals(REQ_HAND_SHAKE)){
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
logger.debug("握手状态:" + (result = this.receiveMessageByChar()));
|
||||
}
|
||||
// 通知Server准备升级
|
||||
if(reqCmd.equals(REQ_SERVER_UPGRADE)){
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
result = this.receiveMessageByChar();
|
||||
}
|
||||
// 获取本机标志UUID
|
||||
if(reqCmd.equals(REQ_LOCAL_UUID)){
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
msg = this.receiveMessageByChar();
|
||||
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
|
||||
logger.info("本机标志UUID:" + msg);
|
||||
}
|
||||
//注释 by jinsj 2012-0531 修改为DC主动获取NC时间
|
||||
// 获取服务器系统时间
|
||||
if(reqCmd.equals(REQ_SERVER_SYSTEMDATE)){
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
msg = this.receiveMessageByChar();
|
||||
logger.debug("服务器系统时间:" + msg);
|
||||
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
|
||||
}
|
||||
// 发送本机的变更信息(操作系统类型和IP)
|
||||
if(reqCmd.equals(REQ_LOCAL_CHANGE)){
|
||||
// 发送请求
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
result = this.receiveMessageByChar();
|
||||
// 发送信息: UUID$@$操作系统类型$@$LocalIP
|
||||
flag = this.sendMessageByChar((String)obj);
|
||||
// 接收变更结果: 0/1 $@$ 信息
|
||||
msg = this.receiveMessageByChar();
|
||||
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
|
||||
return msg;
|
||||
}
|
||||
// 初始化配置
|
||||
if(reqCmd.equals(REQ_INIT_CONFIG)){
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
msg = this.receiveMessageByChar();//接收配置信息
|
||||
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
|
||||
}
|
||||
// 初始化任务
|
||||
if(reqCmd.equals(REQ_INIT_TASK)){
|
||||
// 发送请求
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
result = this.receiveMessageByChar();
|
||||
// 发送本机唯一标识
|
||||
flag = this.sendMessageByChar(Contants.AGENT_HOST_UUID + "");
|
||||
msg = this.receiveMessageByChar();
|
||||
flag = this.sendMessageByChar(CommonSocket.SUCCESS);
|
||||
if (msg != null && !"".equals(msg)) {
|
||||
JSONArray jsonArr = JSONArray.fromObject(msg);
|
||||
//这里处理的任务,原则上不应含有”1 文件推送“类型的任务
|
||||
for (int i = 0; i < jsonArr.size(); i++) {
|
||||
new TaskReqHandle().taskHandle(jsonArr.get(i).toString());
|
||||
}
|
||||
}
|
||||
logger.debug("初始化任务完成--" + msg);
|
||||
return null;
|
||||
}
|
||||
// 发送主动告警信息
|
||||
if(reqCmd.equals(REQ_ALARM)){
|
||||
if(obj!=null){
|
||||
// 主动告警请求
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
result = this.receiveMessageByChar();
|
||||
// 发送主动告警信息内容
|
||||
flag = this.sendMessageByChar((String)obj);
|
||||
logger.debug("主动告警信息:" + (String)obj);
|
||||
result = this.receiveMessageByChar();
|
||||
}else{
|
||||
logger.debug("主动告警信息为空");
|
||||
}
|
||||
}
|
||||
// 发送任务结果
|
||||
if(reqCmd.equals(REQ_TASK_RESULT)){
|
||||
if(obj!=null){
|
||||
//发送任务结果请求
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
result = this.receiveMessageByChar();
|
||||
//发送任务结果内容
|
||||
flag = this.sendMessageByChar((String)obj);
|
||||
result = this.receiveMessageByChar();
|
||||
}else{
|
||||
logger.warn("Task result information is empty");
|
||||
}
|
||||
}
|
||||
// 批量上传监测数据【数据收集方式改为DC主动后,此通信废弃】
|
||||
if(reqCmd.equals(REQ_UPLOAD_DATAS)){
|
||||
if(obj!=null && obj instanceof Object[]) {
|
||||
Object[] objArr = (Object[])obj;
|
||||
if (objArr != null && objArr.length > 1 && objArr[0] != null
|
||||
&& objArr[1] != null && objArr[1] instanceof List) {
|
||||
//发送上传数据请求
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
result = this.receiveMessageByChar();
|
||||
//上传数据
|
||||
this.sendFileByBath((String) objArr[0], (List<File>) objArr[1]);
|
||||
result = this.receiveMessageByChar();
|
||||
}else{
|
||||
logger.warn("Uploading the contents of the monitored data object is incorrect");
|
||||
}
|
||||
}else{
|
||||
logger.warn("Uploading monitoring data objects is empty");
|
||||
}
|
||||
}
|
||||
// 任务执行的回传文件:单个文件发送,断点续传【数据收集方式改为DC主动后,此类废弃】
|
||||
if(reqCmd.equals(REQ_TASK_RETURNFILE)){
|
||||
if(obj!=null && obj instanceof ReturnFilePO){
|
||||
ReturnFilePO rfPo = (ReturnFilePO)obj;
|
||||
//发送回传文件请求
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
result = this.receiveMessageByChar();
|
||||
//发送回传文件任务信息
|
||||
this.sendMessageByChar(TaskResultOper.getTaskResultMsg(rfPo
|
||||
.getTaskId(), rfPo.getTaskType(), null, null, null, rfPo
|
||||
.getStartTime(), rfPo.getEndTime(), rfPo.getIsLoop()));
|
||||
result = this.receiveMessageByChar();
|
||||
//发送回传文件文件名称
|
||||
this.sendMessageByChar(rfPo.getReturnFileName());
|
||||
result = this.receiveMessageByChar();
|
||||
//发送回传文件
|
||||
flag = this.bpSendFile(Contants.localTaskReturnPath + File.separator + rfPo.getReturnFileName());
|
||||
result = this.receiveMessageByChar();
|
||||
}else{
|
||||
logger.warn("The return file object is empty");
|
||||
}
|
||||
}
|
||||
|
||||
// 发送压缩文件,断点续传
|
||||
if(reqCmd.equals(REQ_BP_UPLOAD_FIFE)){
|
||||
if(obj!=null && obj instanceof String[]) {
|
||||
String[] strArr = (String[])obj;
|
||||
if (strArr != null && strArr.length > 1){
|
||||
//打包上传文件请求
|
||||
flag = this.sendMessageByChar(reqCmd + ":" + strArr[0]);
|
||||
result = this.receiveMessageByChar();
|
||||
//发送打包文件名
|
||||
File file = new File(strArr[1]);
|
||||
flag = this.sendMessageByChar(file.getName());
|
||||
result = this.receiveMessageByChar();
|
||||
//上传打包文件
|
||||
flag = this.bpSendFile(strArr[1]);
|
||||
result = this.receiveMessageByChar();
|
||||
//上传成功后删除或移动文件
|
||||
if(flag && SUCCESS.equalsIgnoreCase(result) && file.exists()){
|
||||
String dataType = strArr[0];
|
||||
if(BP_TYPE_DETECT_DATA.equalsIgnoreCase(dataType)){
|
||||
FileUtil.moveFile(file, Contants.localDataDonePath, true);
|
||||
}else if(BP_TYPE_TASK_RESULT.equalsIgnoreCase(dataType)){
|
||||
FileUtil.moveFile(file, Contants.localTaskDonePath, true);
|
||||
}else if(BP_TYPE_TASK_RETURN.equalsIgnoreCase(dataType)){
|
||||
FileUtil.moveFile(file, Contants.localTaskDonePath, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 向DC发送NC端异常信息
|
||||
if(reqCmd.equals(REQ_ERROR_INFO)){
|
||||
if(obj!=null){
|
||||
flag = this.sendMessageByChar(reqCmd);
|
||||
result = this.receiveMessageByChar();
|
||||
//发送异常内容
|
||||
flag = this.sendMessageByChar((String)obj);
|
||||
result = this.receiveMessageByChar();
|
||||
}else{
|
||||
logger.warn("Abnormal information is empty");
|
||||
}
|
||||
}
|
||||
|
||||
if (flag && (SUCCESS.equalsIgnoreCase(result) || msg!=null)) {
|
||||
msg = Contants.COMMON_MSG_SUCCESS + Contants.COMMON_MSG_SEPRATOR + (msg!=null ? msg : "成功");
|
||||
} else {
|
||||
// msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "失败";
|
||||
msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "failed";
|
||||
}
|
||||
logger.debug("SSLClient toDo()---" + msg);
|
||||
|
||||
logger.debug("发送通信请求结束:" + reqCmd);
|
||||
// -- 命令判断
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
534
src/com/nis/nmsclient/thread/socket/SSLServer.java
Normal file
534
src/com/nis/nmsclient/thread/socket/SSLServer.java
Normal file
@@ -0,0 +1,534 @@
|
||||
package com.nis.nmsclient.thread.socket;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLServerSocket;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.filefilter.FalseFileFilter;
|
||||
import org.apache.commons.io.filefilter.PrefixFileFilter;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.common.SysConfig;
|
||||
import com.nis.nmsclient.config.DetecConfReqHandle;
|
||||
import com.nis.nmsclient.model.ReturnFilePO;
|
||||
import com.nis.nmsclient.model.Task1;
|
||||
import com.nis.nmsclient.model.Task4;
|
||||
import com.nis.nmsclient.model.Task6;
|
||||
import com.nis.nmsclient.thread.alarm.AlarmUtil;
|
||||
import com.nis.nmsclient.thread.alarm.ErrorCode;
|
||||
import com.nis.nmsclient.thread.task.AgentCommand;
|
||||
import com.nis.nmsclient.thread.task.TaskReqHandle;
|
||||
import com.nis.nmsclient.util.DateUtil;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.FileWrUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
/**
|
||||
* 用于安全通讯的服务Socket,采用java中的SSLServerSocket
|
||||
* 接收服务端发送过来的对该客户端的各配置参数信息,并对相应程序进行设置
|
||||
**/
|
||||
public class SSLServer implements Runnable {
|
||||
static Logger logger = Logger.getLogger(SSLServer.class);
|
||||
SSLServerSocket ss = null;
|
||||
|
||||
private String startTime;// 服务启动时间
|
||||
|
||||
public SSLServer() throws IOException{
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务Socket
|
||||
**/
|
||||
public void init() throws IOException {
|
||||
try {
|
||||
startTime = System.currentTimeMillis() + "";
|
||||
|
||||
//初始化上下文
|
||||
SSLContext ctx = SSLCertOper.getSSLContext();
|
||||
ss = (SSLServerSocket) ctx.getServerSocketFactory()
|
||||
.createServerSocket(Contants.SOCKET_AGENT_PORT);
|
||||
ss.setNeedClientAuth(true);// 客户端要认证
|
||||
} catch (Exception e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
// throw new IOException("NmsClient监听端口[" + Contants.SOCKET_AGENT_PORT + "]创建失败");
|
||||
throw new IOException("NmsClient monitor port[" + Contants.SOCKET_AGENT_PORT + "]Create failure");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载方法:run 处理客户端的请求
|
||||
**/
|
||||
public void run() {
|
||||
logger.info("通讯线程启动 成功");
|
||||
Socket socket = null;
|
||||
while(true){
|
||||
try {
|
||||
socket = ss.accept();
|
||||
if(!Common.NC_UPGRADE_FLAG){//当NC_UPGRADE_FLAG为false时,允许建立通讯,否则放弃通讯,用于NC升级功能
|
||||
logger.debug("来自:"+socket.getInetAddress().getHostAddress());
|
||||
Common.service.submit(new ServerThread(socket));
|
||||
}else{ //关闭 放弃的通讯
|
||||
logger.info("NC升级 抛弃通讯:"+socket.getInetAddress().getHostAddress());
|
||||
socket.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Failure to establish communication " + ss.getInetAddress().getHostAddress()
|
||||
+ ": " + Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ServerThread extends CommonSocket implements Runnable {
|
||||
|
||||
public ServerThread(Socket s) throws Exception {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public void run(){
|
||||
String ip = null;
|
||||
try {
|
||||
ip = socket.getInetAddress().getHostAddress();
|
||||
|
||||
//设置超时时间
|
||||
socket.setSoTimeout(1000 * 60 * Contants.SOCKET_TIMEOUT_MINUTES);
|
||||
|
||||
// Thread.currentThread().setName("通讯线程 》" + socket.getInetAddress().getHostAddress());
|
||||
Thread.currentThread().setName("Communication Thread 》" + socket.getInetAddress().getHostAddress());
|
||||
|
||||
String msg = this.receiveMessageByChar();
|
||||
logger.info("接收请求 " + msg);
|
||||
|
||||
if(REQ_HAND_SHAKE.equalsIgnoreCase(msg)){// 握手操作
|
||||
//返回接收到的信息和NMSClient启动时间
|
||||
this.sendMessageByChar(SUCCESS+":"+msg+"|"+startTime);
|
||||
}else if(SERVER_COLLECT_DATA.equalsIgnoreCase(msg)){// DC主动向NC收集数据
|
||||
/** ----当前通信DC_IP与配置DC_IP不同,更新IP---- **/
|
||||
if(!Contants.SOCKET_SERVER_HOST.equals(ip)){
|
||||
logger.info("变更通信DC_IP: " + Contants.SOCKET_SERVER_HOST + " --> " + ip);
|
||||
Contants.SOCKET_SERVER_HOST = ip;
|
||||
/** ----SeqId未取到,更新配置文件---- **/
|
||||
if(Contants.AGENT_HOST_UUID == null){
|
||||
SysConfig.updateConfigFile(Contants.SOCKET_SERVER_HOST_KEY, Contants.SOCKET_SERVER_HOST);
|
||||
}
|
||||
}
|
||||
new ServerCollectData(this).sendData();
|
||||
}else if(SERVER_GET_TASKRESULT.equalsIgnoreCase(msg)){// DC再次向NC获取未入库的任务结果
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
String taskInfo = null;
|
||||
List<File> fileList = new LinkedList<File>();
|
||||
while(!END.equalsIgnoreCase(taskInfo = this.receiveMessageByChar())){
|
||||
// taskInfo组织顺序:TaskId、TaskType、isLoop、startTime、endTime
|
||||
String[] infos = taskInfo.split(Contants.COMMON_MSG_SEPRATOR_SPLIT);
|
||||
if (infos.length < 4 || (!"0".equals(infos[2]) && infos.length < 5)) {// 参数个数不够4,或者周期任务参数个数不够5,则跳过本次处理
|
||||
logger.warn("DC gets the task result again, the task attribute is incomplete, skip this processing.");
|
||||
this.sendMessageByChar(AgentCommand.RESULT_FAIL
|
||||
// + Contants.COMMON_MSG_SEPRATOR + "任务参数不全,找不到任务结果");
|
||||
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.SSLServer.sendMsg_n81i");
|
||||
continue;
|
||||
}
|
||||
String eTime = null;
|
||||
if(infos.length > 4){// 非周期任务,参数个数为4
|
||||
eTime = infos[4];
|
||||
}
|
||||
// 检查指定任务的的结果或回传文件是否存在:若存在,移动到incoming目录;若不存在,回复失败
|
||||
boolean isExistResult = checkTaskResultExist(infos[0], infos[1], infos[2], infos[3], eTime, fileList);
|
||||
if(isExistResult){
|
||||
this.sendMessageByChar(AgentCommand.RESULT_OK
|
||||
+ Contants.COMMON_MSG_SEPRATOR + "");
|
||||
} else {
|
||||
this.sendMessageByChar(AgentCommand.RESULT_FAIL
|
||||
// + Contants.COMMON_MSG_SEPRATOR + "任务结果不存在");
|
||||
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.SSLServer.noResult_n81i");
|
||||
}
|
||||
}
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
// 移动存在的任务结果和回传文件
|
||||
moveTaskResultOrReturn(fileList);
|
||||
}else if(REQ_SERVER_SYSTEMDATE.equalsIgnoreCase(msg)){//add by jinsj 2012-05-31 DC主动获取NC时间
|
||||
this.sendMessageByChar(new Date().getTime()+"");
|
||||
this.receiveMessageByChar();
|
||||
}else if(Contants.AGENT_HOST_UUID == null ){
|
||||
// 除了以上三个通信,其他通信都得判断SeqID是否获取到,若未取到,则要放弃通讯
|
||||
logger.info("NC尚未取到SeqID 抛弃通讯:"+socket.getInetAddress().getHostAddress());
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
if(REQ_CERT.equalsIgnoreCase(msg)){
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
//接收证书
|
||||
this.receiveFileByByte(Contants.keyPath + File.separator + "server.cer");
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
//导入认证证书到库
|
||||
SSLCertOper.importCertToStore("serverks"
|
||||
+ DateUtil.getCurrentDate(DateUtil.YYYYMMDD),
|
||||
Contants.SSL_TRUST_KEY_STORE,
|
||||
Contants.keyPath + File.separator + "server.cer", Contants.SSL_KEY_STORE_PASS);
|
||||
File file = new File(Contants.keyPath + File.separator + "server.cer");
|
||||
if(file.exists()){
|
||||
FileUtil.delDir(file);
|
||||
}
|
||||
|
||||
/*SSLClient sc = new SSLClient();
|
||||
SSLCertOper.CreateAndSendCert(sc);
|
||||
sc.close();*/
|
||||
}else if(SERVER_UPDATE_CONFIG.equalsIgnoreCase(msg)){// 更新监测配置
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
String str = this.receiveMessageByChar();
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
logger.debug("updateConfig-->" + str);
|
||||
new DetecConfReqHandle().handlerConfigByUpdate(str);
|
||||
} else if(SEND_PLUGIN_SCRIPT_FILE.equalsIgnoreCase(msg)) { // 下发脚本
|
||||
File pluginDir = new File(Contants.localPluginScriptPath);
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
String fileNames = this.receiveMessageByChar();
|
||||
Collection<?> files = FileUtils.listFiles(pluginDir,
|
||||
new PrefixFileFilter(fileNames.split(",")), FalseFileFilter.FALSE);
|
||||
for (Object file : files) {
|
||||
((File)file).delete();
|
||||
}
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
this.bpReceiveFileByBath(pluginDir.getCanonicalPath());
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
|
||||
} else if(SERVER_FILE_PUSH.equalsIgnoreCase(msg)){//任务操作:文件推送
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
String str = this.receiveMessageByChar();
|
||||
logger.debug("task-->" + str);
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
JSONObject jsonObj = JSONObject.fromObject(str);
|
||||
String resultMsg = null;
|
||||
if(str.contains("taskInfo")){
|
||||
JSONObject jsonObj2 = jsonObj.getJSONObject("taskInfo");
|
||||
Object obj = JSONObject.toBean(jsonObj2,Task1.class);
|
||||
Task1 fileInfo = (Task1) obj;
|
||||
// 接收文件
|
||||
resultMsg = new TaskReqHandle().filePush(this, fileInfo.getTaskParam(), fileInfo.getTaskId(), false);
|
||||
}
|
||||
if (resultMsg !=null && Contants.isSucessByResult(resultMsg)) {
|
||||
this.sendMessageByChar(AgentCommand.RESULT_OK
|
||||
+ Contants.COMMON_MSG_SEPRATOR
|
||||
// + "成功,详细信息如下:" + Contants.getDescByResult(resultMsg));
|
||||
+ "i18n_client.SSLServer.success_n81i:" + Contants.getDescByResult(resultMsg));
|
||||
} else {
|
||||
this.sendMessageByChar(AgentCommand.RESULT_FAIL
|
||||
+ Contants.COMMON_MSG_SEPRATOR
|
||||
// + "失败,详细信息如下:" + Contants.getDescByResult(resultMsg));
|
||||
+ "i18n_client.SSLServer.fail_n81i:" + Contants.getDescByResult(resultMsg));
|
||||
}
|
||||
this.receiveMessageByChar();
|
||||
}else if(SERVER_UPGRADE.equalsIgnoreCase(msg)){//任务操作:升级
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
String str = this.receiveMessageByChar();
|
||||
logger.debug("task-->" + str);
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
JSONObject jsonObj = JSONObject.fromObject(str);
|
||||
String resultMsg = null;
|
||||
TaskReqHandle handle = new TaskReqHandle();
|
||||
if(str.contains("taskInfo")){
|
||||
JSONObject jsonObj2 = jsonObj.getJSONObject("taskInfo");
|
||||
Object obj = JSONObject.toBean(jsonObj2,Task6.class);
|
||||
Task6 task = (Task6) obj;
|
||||
// 判断是否重新执行任务,并作提前处理
|
||||
reExecTask(task.getTaskId(), task.getOldTaskId());
|
||||
// 接收升级文件
|
||||
resultMsg = handle.filePush(this, task.getCommandParam(),
|
||||
task.getTaskId(), true);
|
||||
}
|
||||
if (resultMsg !=null && Contants.isSucessByResult(resultMsg)) {
|
||||
this.sendMessageByChar(AgentCommand.RESULT_SEND_OK
|
||||
+ Contants.COMMON_MSG_SEPRATOR
|
||||
// + "下发成功,详细信息如下:" + Contants.getDescByResult(resultMsg));
|
||||
+ "i18n_client.SSLServer.lssueSuccess_n81i:" + Contants.getDescByResult(resultMsg));
|
||||
} else {
|
||||
this.sendMessageByChar(AgentCommand.RESULT_FAIL
|
||||
+ Contants.COMMON_MSG_SEPRATOR
|
||||
// + "失败,详细信息如下: " + Contants.getDescByResult(resultMsg));
|
||||
+ "i18n_client.SSLServer.fail_n81i: " + Contants.getDescByResult(resultMsg));
|
||||
}
|
||||
String receiveMsg = this.receiveMessageByChar();
|
||||
if(resultMsg !=null && Contants.isSucessByResult(resultMsg) &&receiveMsg.equals(SUCCESS)){//处理升级
|
||||
handle.taskHandle(str);
|
||||
}
|
||||
}else if(SERVER_TASK.equalsIgnoreCase(msg)){//任务操作:命令执行和升级逆向任务
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
String str = this.receiveMessageByChar();
|
||||
logger.debug("task-->" + str);
|
||||
this.sendMessageByChar(AgentCommand.RESULT_SEND_OK
|
||||
// + Contants.COMMON_MSG_SEPRATOR + "下发成功");
|
||||
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.SSLServer.lssueSuccess1_n81i");
|
||||
this.receiveMessageByChar();
|
||||
|
||||
//2015-6-23 针对reboot命令(之前存在会多次重启的问题,现修改为,接收到命令执行任务时,如果该任务的结果已经存在(incoming或者done里有),则不再执行)
|
||||
try {
|
||||
int taskType = 0;
|
||||
JSONObject jsonObj = JSONObject.fromObject(str);
|
||||
if(str.contains("typeInfo")){
|
||||
taskType = jsonObj.getInt("typeInfo");
|
||||
}
|
||||
if(str.contains("taskInfo") && taskType==4){//taskType:命令执行任务(4)
|
||||
JSONObject jsonObj2 = jsonObj.getJSONObject("taskInfo");
|
||||
Task4 task4 = (Task4)JSONObject.toBean(jsonObj2,Task4.class);
|
||||
String taskId = task4.getTaskId()==null?"0":(task4.getTaskId()+"");
|
||||
String isLoop = task4.getIsLoop()+"";
|
||||
String startTime = task4.getStartTime()==null?"":task4.getStartTime()+"";
|
||||
String endTime = task4.getEndTime()==null?"":task4.getEndTime()+"";
|
||||
if(task4.getCommandType() == 2){//命令执行(4)->可执行命令(2)
|
||||
logger.info("可执行命令 taskId:" + task4.getTaskId());
|
||||
List<File> fileList = new ArrayList<File>();
|
||||
boolean isExist = checkTaskResultExistFromDoneAndIncoming(taskId+"", taskType+"", isLoop, startTime, endTime, fileList);//非周期任务:0
|
||||
if(isExist) {
|
||||
logger.info("任务已执行,不再重复执行:taskId:"+taskId+" taskType:"+taskType);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("For the next task, determine whether there is a result, if the result is no longer performing the exception", e);
|
||||
}
|
||||
|
||||
new TaskReqHandle().taskHandle(str);
|
||||
}else if(SERVER_TASK_CANCEL.equalsIgnoreCase(msg)){//任务撤消操作
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
String str = this.receiveMessageByChar();
|
||||
logger.debug("taskcancle-->" + str);
|
||||
if(str!=null && !"".equals(str)){
|
||||
Common.cancleTaskFuture(Long.parseLong(str), 0);
|
||||
}
|
||||
this.sendMessageByChar(AgentCommand.MISSION_CANCEL_FINISH
|
||||
// + Contants.COMMON_MSG_SEPRATOR + "任务已撤消完成");
|
||||
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.SSLServer.missionRevokeSuccess_n81i");
|
||||
this.receiveMessageByChar();
|
||||
}else if(ACTIVE_ALARM_START_ALERT.equalsIgnoreCase(msg)){// NC端是否报主动告警 变更
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
String str = this.receiveMessageByChar();
|
||||
this.sendMessageByChar(SUCCESS);
|
||||
logger.debug("isStartActiveAlarm-->" + str);
|
||||
JSONObject jsonObj = JSONObject.fromObject(str);
|
||||
Boolean isStartActiveAlarm = (Boolean)jsonObj.get("showAutoAlarm");
|
||||
String webHandleTime = (String)jsonObj.get("webHandleTime");
|
||||
//更新Contants.ACTIIVE_ALARM_START
|
||||
if(isStartActiveAlarm!=null) {
|
||||
|
||||
Contants.ACTIIVE_ALARM_START = isStartActiveAlarm;
|
||||
logger.info("NC是否主动告警:"+Contants.ACTIIVE_ALARM_START+" web端操作时间:"+webHandleTime);
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("接收请求 " + msg + " 完成");
|
||||
} catch (Exception e) {
|
||||
logger.error("Receiving information anomaly:" + Utils.printExceptionStack(e));
|
||||
if(ip==null){
|
||||
ip = Utils.getLocalIp();
|
||||
}
|
||||
// AlarmUtil.sendNMSErrorMsg(ErrorCode.SocketError, ip , "NC通讯线程异常:" + e.getMessage());
|
||||
AlarmUtil.sendNMSErrorMsg(ErrorCode.SocketError, ip , "NC communication thread exception:" + e.getMessage());
|
||||
|
||||
return;
|
||||
} finally {
|
||||
logger.debug("关闭通信");
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新执行任务,针对升级任务的推送文件的提前处理,将原任务的文件拷贝到新任务的临时目录
|
||||
* @param taskId
|
||||
* @param oldTaskId
|
||||
* @throws Exception
|
||||
*/
|
||||
private void reExecTask(Long taskId, Long oldTaskId) throws Exception {
|
||||
// 如果原任务ID为空,说明不是重新执行任务,不执行任何操作
|
||||
if (oldTaskId == null || "".equals(oldTaskId.toString())
|
||||
|| "0".equals(oldTaskId.toString())) {
|
||||
return;
|
||||
}
|
||||
File tempDir = new File(Contants.localTempDataIncomingPath + File.separator
|
||||
+ "filepush_" + taskId);
|
||||
if (!tempDir.exists()) {
|
||||
tempDir.mkdirs();
|
||||
}
|
||||
// 如果是升级任务,推送文件的保存路径getUpgradeTaskPushPath(taskId)
|
||||
File oldFileDir = new File(TaskReqHandle.getUpgradeTaskPushPath(oldTaskId));
|
||||
try {
|
||||
if(oldFileDir.exists()){
|
||||
FileUtils.copyDirectory(oldFileDir, tempDir);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DC再次获取任务结果 -- 检查指定任务的的结果或回传文件是否存在
|
||||
* @param isLoop 是否循环任务: 0 非周期, 1 周期
|
||||
* @param startTime 若非周期任务,升级时间或创建时间;若周期任务,某一周期的起始时间
|
||||
* @param endTime 若非周期任务,为空;若周期任务,某一周期的结束时间
|
||||
* @param fileList 用于存在找到的结果文件和回传文件
|
||||
* @return
|
||||
*/
|
||||
private boolean checkTaskResultExist(String taskId, String taskType, String isLoop, String sTime, String eTime, List<File> fileList) throws Exception{
|
||||
long startTime=(sTime==null || "".equals(sTime) || "null".equals(sTime)) ? 0l : Long.parseLong(sTime);
|
||||
long endTime=(eTime==null || "".equals(eTime) || "null".equals(eTime)) ? 0l : Long.parseLong(eTime);
|
||||
logger.debug("checkTaskResultExist startTime=" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(startTime))+" -- endTime=" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(endTime)));
|
||||
|
||||
String dateName = DateUtil.getStingDate(DateUtil.YYYYMMDD, new Date(startTime));
|
||||
String prefix = "tasktype" + taskType + "_" + taskId;
|
||||
|
||||
boolean isExistResult = false;
|
||||
// 依次取nc_task/done下的result和return目录
|
||||
File[] fileDirs = FileUtil.getDirectoryArray(new File(Contants.localTaskDonePath));
|
||||
if(fileDirs==null){
|
||||
logger.info("fileDirs为空");
|
||||
}else{
|
||||
try{
|
||||
for(File dir : fileDirs){
|
||||
// -- 找到指定的日期目录dateName及之后的日期目录
|
||||
File[] dateFiles = FileUtil.sortASCByFileName(FileUtil.getDirsAfterDateName(dir, dateName));
|
||||
// -- 在找到的日期目录下检查文件是否存在
|
||||
for(File dateFile : dateFiles){
|
||||
File[] files = null;
|
||||
if("0".equals(isLoop)){// 0 非周期
|
||||
files = FileUtil.getFilesStartWith(dateFile, prefix);
|
||||
}else{//--- 周期任务取某一时间段内的结果与回传文件
|
||||
files = FileUtil.getFilesStartWithByMillis(dateFile, prefix, startTime, endTime);
|
||||
}
|
||||
|
||||
if(files.length>0){// 若在任一目录下找到,则不用再找其他日期目录,跳出第二个For循环
|
||||
fileList.addAll(Arrays.asList(files));
|
||||
isExistResult = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch(Exception e){
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
if(!isExistResult){
|
||||
logger.info("再次获取任务结果 > TaskId: " + taskId + ", TaskType: "
|
||||
+ taskType + ", IsLoop: " + isLoop + " > 任务结果不存在");
|
||||
}
|
||||
|
||||
return isExistResult;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 检查done和incoming里是否有任务结果信息(如果有,则不进行再次执行,避免重复执行任务,如重复reboot)
|
||||
* @author dell Jun 23, 2015
|
||||
* @version 1.0
|
||||
* @param taskId
|
||||
* @param taskType
|
||||
* @param isLoop
|
||||
* @param sTime
|
||||
* @param eTime
|
||||
* @param fileList
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private boolean checkTaskResultExistFromDoneAndIncoming(String taskId, String taskType, String isLoop, String sTime, String eTime, List<File> fileList) throws Exception{
|
||||
long startTime=(sTime==null || "".equals(sTime) || "null".equals(sTime)) ? 0l : Long.parseLong(sTime);
|
||||
long endTime=(eTime==null || "".equals(eTime) || "null".equals(eTime)) ? 0l : Long.parseLong(eTime);
|
||||
logger.debug("checkTaskResultExist startTime=" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(startTime))+" -- endTime=" + DateUtil.getStingDate(DateUtil.YYYY_MM_DD_HH24_MM_SS, new Date(endTime)));
|
||||
|
||||
String dateName = DateUtil.getStingDate(DateUtil.YYYYMMDD, new Date(startTime));
|
||||
String prefix = "tasktype" + taskType + "_" + taskId;
|
||||
|
||||
boolean isExistResult = false;
|
||||
// 依次取nc_task/done和incoming下的result和return目录
|
||||
File[] fileDoneDirs = FileUtil.getDirectoryArray(new File(Contants.localTaskDonePath));
|
||||
File[] fileIncomingDirs = FileUtil.getDirectoryArray(new File(Contants.localTaskResultPath));
|
||||
File[] fileDirs = null;
|
||||
if(fileIncomingDirs!=null && fileDoneDirs!=null) {
|
||||
fileDirs = (File[])ArrayUtils.addAll(fileDoneDirs, fileIncomingDirs);
|
||||
}
|
||||
if(fileDirs==null){
|
||||
logger.info("fileDirs为空");
|
||||
}else{
|
||||
try{
|
||||
for(File dir : fileDirs){
|
||||
// -- 找到指定的日期目录dateName及之后的日期目录
|
||||
File[] dateFiles = FileUtil.sortASCByFileName(FileUtil.getDirsAfterDateName(dir, dateName));
|
||||
// -- 在找到的日期目录下检查文件是否存在
|
||||
for(File dateFile : dateFiles){
|
||||
File[] files = null;
|
||||
if("0".equals(isLoop)){// 0 非周期
|
||||
files = FileUtil.getFilesStartWith(dateFile, prefix);
|
||||
}else{//--- 周期任务取某一时间段内的结果与回传文件
|
||||
files = FileUtil.getFilesStartWithByMillis(dateFile, prefix, startTime, endTime);
|
||||
}
|
||||
|
||||
if(files.length>0){// 若在任一目录下找到,则不用再找其他日期目录,跳出第二个For循环
|
||||
fileList.addAll(Arrays.asList(files));
|
||||
isExistResult = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch(Exception e){
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
if(!isExistResult){
|
||||
logger.info("判断新下发的任务结果是否已经存在 > TaskId: " + taskId + ", TaskType: "
|
||||
+ taskType + ", IsLoop: " + isLoop + " > 任务结果不存在");
|
||||
}else {
|
||||
logger.info("判断新下发的任务结果是否已经存在 > TaskId: " + taskId + ", TaskType: "
|
||||
+ taskType + ", IsLoop: " + isLoop + " > 任务结果已存在");
|
||||
}
|
||||
|
||||
return isExistResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* DC再次获取任务结果 -- 移动找到的结果文件和回传文件到incoming目录
|
||||
*/
|
||||
private void moveTaskResultOrReturn(List<File> fileList){
|
||||
if(fileList==null || fileList.size()==0){
|
||||
return;
|
||||
}
|
||||
try {
|
||||
for(File file : fileList){
|
||||
// ---------- 任务回传文件处理
|
||||
if(file.getName().endsWith(Contants.TASK_RETURN_FILE_SUFFIX)){
|
||||
if(!file.exists() || !file.isFile()){
|
||||
continue;
|
||||
}
|
||||
// 移动实际回传的文件
|
||||
String[] resultArr = FileWrUtil.cfgFileReader(file);
|
||||
if (resultArr != null && resultArr.length > 0) {
|
||||
JSONObject jsonObject = JSONObject.fromObject(resultArr[0]);
|
||||
ReturnFilePO rfPo = (ReturnFilePO) JSONObject.toBean(jsonObject, ReturnFilePO.class);
|
||||
if(rfPo.getReturnFileName()!=null && !"".equals(rfPo.getReturnFileName())){
|
||||
File returnFile = new File(file.getParent() + File.separator + rfPo.getReturnFileName());
|
||||
FileUtil.moveFile(returnFile, Contants.localTaskReturnPath, true);
|
||||
}
|
||||
}
|
||||
// 移动记录任务回传的临时文件
|
||||
FileUtil.moveFile(file, Contants.localTaskReturnPath, true);
|
||||
}else {
|
||||
// ---------- 任务结果处理
|
||||
FileUtil.moveFile(file, Contants.localTaskResultPath, true);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Get the task result again > mobile file exception again", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
581
src/com/nis/nmsclient/thread/socket/ServerCollectData.java
Normal file
581
src/com/nis/nmsclient/thread/socket/ServerCollectData.java
Normal file
@@ -0,0 +1,581 @@
|
||||
package com.nis.nmsclient.thread.socket;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.model.ReturnFilePO;
|
||||
import com.nis.nmsclient.thread.task.TaskResultOper;
|
||||
import com.nis.nmsclient.util.DateUtil;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.FileWrUtil;
|
||||
import com.nis.nmsclient.util.StringUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
import com.nis.nmsclient.util.ZipUtil;
|
||||
|
||||
/**
|
||||
* 用于定时扫描并上传监测数据文件
|
||||
*
|
||||
**/
|
||||
public class ServerCollectData {
|
||||
Logger logger = Logger.getLogger(ServerCollectData.class);
|
||||
private CommonSocket socket;
|
||||
|
||||
public ServerCollectData(CommonSocket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public void sendData() {
|
||||
logger.debug("传送数据开始 ~~~~~~~");
|
||||
try {
|
||||
// 发送监测数据
|
||||
handleDetectData();
|
||||
// 发送任务结果
|
||||
handleTaskResult();
|
||||
// 发送任务回传文件
|
||||
handleTaskReturnFile();
|
||||
// 发送任务结果--针对有回传文件时写的任务结果
|
||||
handleTaskResult();
|
||||
// 结束通讯
|
||||
socket.sendMessageByChar(CommonSocket.END);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Transmits data anomalies:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
logger.debug("传送数据结束 ~~~~~~~");
|
||||
}
|
||||
|
||||
private void handleDetectData() throws Exception {
|
||||
logger.debug("传送监测数据开始 ~~~~~~~");
|
||||
long startTime = System.currentTimeMillis();
|
||||
File parDir = new File(Contants.localDataCollection);
|
||||
if(!parDir.exists()){
|
||||
return;
|
||||
}
|
||||
// == 1、针对数据文件过多时打包上传未完成的文件继续上传
|
||||
// ------------取所有未上传完成的Zip文件
|
||||
List<File> fileList = new LinkedList<File>();
|
||||
File[] fileArr = FileUtil.getFilesEndWith(parDir, ".zip");
|
||||
if (fileArr != null && fileArr.length > 0) {
|
||||
for (File file : fileArr) {
|
||||
if (!file.getName().startsWith(CommonSocket.BP_TYPE_DETECT_DATA)) {
|
||||
continue;
|
||||
}
|
||||
fileList.add(file);
|
||||
}
|
||||
}
|
||||
// ------------传送Zip文件
|
||||
if (fileList.size() > 0) {
|
||||
sendZipFile(fileList, CommonSocket.DATA_TYPE_ZIP_DETECT);
|
||||
}
|
||||
|
||||
// == 2、检查当前数据文件数量,批量发送文件或打包上传
|
||||
File dataDir = new File(Contants.localDataFilePath);
|
||||
if (!dataDir.exists()) {
|
||||
logger.warn("Data directory“" + dataDir.getAbsolutePath() + "”Non-existent!!!");
|
||||
} else {
|
||||
long total = 0;
|
||||
List<File> allFiles = new ArrayList<File>();
|
||||
File[] dataDirs = FileUtil.getDirectoryArray(dataDir);
|
||||
// ---- 数据处理
|
||||
total = handleNullDataFile(allFiles, dataDirs);
|
||||
logger.info("本次收集监测数据文件总数:" + total + ", 正常数据:" + allFiles.size() + ", 空数据:" + (total - allFiles.size()));
|
||||
total = allFiles.size();// 正常的要上传的数据个数
|
||||
|
||||
// --- 将所有数据文件一起打包,发送
|
||||
if (total > Contants.COMMON_ZIP_MIN_SIZE) {
|
||||
long zipCnt = total/Contants.COMMON_ZIP_MAX_SIZE;
|
||||
if (zipCnt > 0) {//2013-5-6 未上传的数据太多时,将监测数据压缩为多个文件
|
||||
for(int i=0; i<total/Contants.COMMON_ZIP_MAX_SIZE; i++){
|
||||
// 组织每次压缩的文件数组
|
||||
File[] dataFiles = new File[Contants.COMMON_ZIP_MAX_SIZE];
|
||||
int start = i * Contants.COMMON_ZIP_MAX_SIZE;
|
||||
int end = (i + 1) * Contants.COMMON_ZIP_MAX_SIZE;
|
||||
for (int j = start, k = 0; j < end; j++, k++) {
|
||||
dataFiles[k] = allFiles.get(j);
|
||||
}
|
||||
// 压缩并发送监测数据
|
||||
compressAndSendDetecData(dataFiles);
|
||||
}
|
||||
}else{
|
||||
// 压缩并发送监测数据
|
||||
compressAndSendDetecData(dataDirs);
|
||||
}
|
||||
logger.info("本次收集将所有监测数据打包传送,监测数据总数:" + total + ",用时:"
|
||||
+ (System.currentTimeMillis() - startTime) + "ms");
|
||||
} else if (total > 0) {
|
||||
// -- 按正常所有监测数据批量上传
|
||||
sendCSVData(dataDir, allFiles);
|
||||
logger.info("本次收集传送监测数据总数:" + total + ",用时:"
|
||||
+ (System.currentTimeMillis() - startTime) + "ms");
|
||||
} else {
|
||||
logger.info("本次收集未传送监测数据");
|
||||
}
|
||||
}
|
||||
logger.debug("传送监测数据结束 ~~~~~~~");
|
||||
}
|
||||
|
||||
private void handleTaskResult() throws Exception {
|
||||
logger.debug("传送任务结果开始 ~~~~~~~");
|
||||
long startTime = System.currentTimeMillis();
|
||||
// == 1、针对结果文件过多时打包上传未完成的文件继续上传
|
||||
File taskDir = new File(Contants.localTaskPath);
|
||||
if (!taskDir.exists()) {
|
||||
return;
|
||||
}
|
||||
// ------------取所有未上传完成的Zip文件
|
||||
List<File> fileList = new LinkedList<File>();
|
||||
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
|
||||
if (zipArr != null && zipArr.length > 0) {
|
||||
for (File file : zipArr) {
|
||||
if (!file.getName().startsWith(CommonSocket.BP_TYPE_TASK_RESULT)) {
|
||||
continue;
|
||||
}
|
||||
fileList.add(file);
|
||||
}
|
||||
}
|
||||
// ------------传送Zip文件
|
||||
if(fileList.size()>0){
|
||||
sendZipFile(fileList, CommonSocket.DATA_TYPE_ZIP_TASKRESULT);
|
||||
}
|
||||
|
||||
// == 2、检查当前结果文件数量,批量发送文件或打包上传
|
||||
File resultDir = new File(TaskResultOper.getTaskResultPath());
|
||||
if(!resultDir.exists()){
|
||||
return;
|
||||
}
|
||||
File[] fileArr = FileUtil.getFilesEndWith(resultDir, Contants.TASK_RESULT_FILE_SUFFIX);
|
||||
// -- 将所有任务结果文件一起打包,发送
|
||||
if(fileArr.length > Contants.COMMON_ZIP_MIN_SIZE){
|
||||
int zipCnt = fileArr.length/Contants.COMMON_ZIP_MAX_SIZE;
|
||||
if(zipCnt>0){//2013-5-6 未上传的结果文件太多时,将结果文件压缩为多个文件
|
||||
for(int i=0; i<fileArr.length/Contants.COMMON_ZIP_MAX_SIZE; i++){
|
||||
// 组织每次压缩的文件数组
|
||||
File[] resultFiles = new File[Contants.COMMON_ZIP_MAX_SIZE];
|
||||
int start = i * Contants.COMMON_ZIP_MAX_SIZE;
|
||||
int end = (i + 1) * Contants.COMMON_ZIP_MAX_SIZE;
|
||||
for(int j=start,k=0; j<end; j++,k++){
|
||||
resultFiles[k] = fileArr[j];
|
||||
}
|
||||
// 压缩并发送任务结果
|
||||
compressAndSendTaskResult(resultFiles);
|
||||
}
|
||||
}else{
|
||||
// 压缩并发送任务结果
|
||||
compressAndSendTaskResult(fileArr);
|
||||
}
|
||||
|
||||
logger.info("本次收集将所有任务结果打包传送,任务结果总数:" + fileArr.length + ",用时:" + (System.currentTimeMillis() - startTime) + "ms");
|
||||
}else if(fileArr.length > 0){
|
||||
// -- 按正常的多个结果批量发送
|
||||
sendTaskResult(fileArr);
|
||||
logger.info("本次收集传送任务结果总数:" + fileArr.length + ",用时:"
|
||||
+ (System.currentTimeMillis() - startTime) + "ms");
|
||||
} else {
|
||||
logger.info("本次收集未传送任务结果");
|
||||
}
|
||||
logger.debug("传送任务结果结束 ~~~~~~~");
|
||||
}
|
||||
|
||||
private void handleTaskReturnFile() throws Exception {
|
||||
logger.debug("传送回传文件开始 ~~~~~~~");
|
||||
long startTime = System.currentTimeMillis();
|
||||
// == 1、针对回传文件过多时打包上传未完成的文件继续上传
|
||||
File taskDir = new File(Contants.localTaskPath);
|
||||
if (!taskDir.exists()) {
|
||||
return;
|
||||
}
|
||||
// ------------取所有未上传完成的Zip文件
|
||||
List<File> fileList = new LinkedList<File>();
|
||||
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
|
||||
if (zipArr != null && zipArr.length > 0) {
|
||||
for (File file : zipArr) {
|
||||
if (!file.getName().startsWith(CommonSocket.BP_TYPE_TASK_RETURN)) {
|
||||
continue;
|
||||
}
|
||||
fileList.add(file);
|
||||
}
|
||||
}
|
||||
// ------------传送Zip文件
|
||||
if(fileList.size()>0){
|
||||
sendZipFile(fileList, CommonSocket.DATA_TYPE_ZIP_TASKRETURN);
|
||||
}
|
||||
|
||||
// == 2、检查当前回传文件数量,单个发送文件或打包上传
|
||||
File returnDir = new File(Contants.localTaskReturnPath);
|
||||
if(!returnDir.exists()){
|
||||
return;
|
||||
}
|
||||
File[] fileArr = FileUtil.getFilesEndWith(returnDir, Contants.TASK_RETURN_FILE_SUFFIX);
|
||||
if(fileArr == null || fileArr.length == 0){
|
||||
return;
|
||||
}
|
||||
//--- 将所有任务的回传文件及回传信息保存文件一起打包,发送
|
||||
if(fileArr.length > Contants.COMMON_MAX_RETURN_CNT){
|
||||
//压缩并删除原文件
|
||||
String compressFileStr = Contants.localTaskPath
|
||||
+ File.separator
|
||||
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RETURN,
|
||||
null, true)
|
||||
+ ".zip";
|
||||
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现,现修改将任务结果和回传文件压缩时不删除文件,而是将其移动到相应的日期目录
|
||||
ZipUtil.zipWithMoveFile(returnDir.listFiles(), compressFileStr, false);
|
||||
//发送
|
||||
sendZipFile(new File(compressFileStr), CommonSocket.DATA_TYPE_ZIP_TASKRETURN);
|
||||
logger.info("本次收集将所有任务回传文件打包传送,回传文件总数:" + fileArr.length + ",用时:" + (System.currentTimeMillis() - startTime) + "ms");
|
||||
}else if(fileArr.length > 0){
|
||||
//-- 按正常的一个任务一个任务的回传
|
||||
sendTaskReturn(fileArr);
|
||||
logger.info("本次收集传送任务回传总数:" + fileArr.length + ",用时:"
|
||||
+ (System.currentTimeMillis() - startTime) + "ms");
|
||||
} else {
|
||||
logger.info("本次收集未传送任务回传文件");
|
||||
}
|
||||
logger.debug("传送回传文件结束 ~~~~~~~");
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历所有准备上传的数据文件,将空数据文件移动到指定目录,并记录所有文件总数
|
||||
* @param allFiles 所有非空文件集合
|
||||
* @param dataDirs 所有数据目录
|
||||
* @return 所有文件个数(包括空文件)
|
||||
* @throws Exception
|
||||
*/
|
||||
private long handleNullDataFile(List<File> allFiles, File[] dataDirs) throws Exception {
|
||||
long total = 0l;
|
||||
for(File dir : dataDirs){
|
||||
File[] files = FileUtil.getFilesEndWith(dir, ".csv");
|
||||
if(files==null || files.length==0){
|
||||
continue;
|
||||
}
|
||||
files = FileUtil.sortASCByModify(files); // 修改日期升序排序
|
||||
total += files.length;
|
||||
for (File file : files) {
|
||||
if (file.length() > 0) {
|
||||
allFiles.add(file);
|
||||
continue;
|
||||
}
|
||||
//--- 处理空文件数据:移动空文件数据到指定日期目录
|
||||
String dirTime = DateUtil.getStingDate(
|
||||
DateUtil.YYYYMMDD,
|
||||
new Date(file.lastModified()));
|
||||
String newDir = Contants.localDataErrorPath
|
||||
+ File.separator
|
||||
+ file.getParentFile().getName()
|
||||
+ File.separator + dirTime;
|
||||
FileUtil.moveFile(file, newDir, true);
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private void compressAndSendDetecData(File[] dataFiles) throws Exception{
|
||||
// 压缩并移动原文件
|
||||
String compressFileStr = Contants.localDataCollection
|
||||
+ File.separator
|
||||
+ CommonSocket.addTimeTagForFileName(
|
||||
CommonSocket.BP_TYPE_DETECT_DATA, null, true)
|
||||
+ ".zip";
|
||||
// 2013-3-29 由于压缩上传数据后,主动告警线程部分对数据检查存在问题,现将压缩后删除数据改为移动数据到日期目录
|
||||
ZipUtil.zipWithMoveFile(dataFiles, compressFileStr, true);
|
||||
// 发送
|
||||
sendZipFile(new File(compressFileStr), CommonSocket.DATA_TYPE_ZIP_DETECT);
|
||||
}
|
||||
|
||||
private void compressAndSendTaskResult(File[] resultFiles) throws Exception {
|
||||
//压缩并删除原文件
|
||||
String compressFileStr = Contants.localTaskPath
|
||||
+ File.separator
|
||||
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RESULT, null, true)
|
||||
+ ".zip";
|
||||
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现,现修改将任务结果和回传文件压缩时不删除文件,而是将其移动到相应的日期目录
|
||||
ZipUtil.zipWithMoveFile(resultFiles, compressFileStr, true);
|
||||
//发送
|
||||
sendZipFile(new File(compressFileStr), CommonSocket.DATA_TYPE_ZIP_TASKRESULT);
|
||||
}
|
||||
|
||||
private void compressAndSendTaskReturn(File[] returnFiles) throws Exception {
|
||||
//压缩并删除原文件
|
||||
String compressFileStr = Contants.localTaskPath
|
||||
+ File.separator
|
||||
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RETURN,
|
||||
null, true)
|
||||
+ ".zip";
|
||||
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现,现修改将任务结果和回传文件压缩时不删除文件,而是将其移动到相应的日期目录
|
||||
ZipUtil.zipWithMoveFile(returnFiles, compressFileStr, false);
|
||||
//发送
|
||||
sendZipFile(new File(compressFileStr), CommonSocket.DATA_TYPE_ZIP_TASKRETURN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送打包文件:整个命令通信包装方法
|
||||
* @param file
|
||||
* @throws Exception
|
||||
*/
|
||||
private void sendZipFile(File file, String dataType) throws Exception {
|
||||
//打包上传文件请求
|
||||
socket.sendMessageByChar(dataType);
|
||||
socket.receiveMessageByChar();
|
||||
socket.sendMessageByChar(file.getName());//发送打包文件名
|
||||
socket.receiveMessageByChar();
|
||||
//上传打包文件
|
||||
socket.bpSendFile(file.getAbsolutePath());
|
||||
String result = socket.receiveMessageByChar();
|
||||
//上传成功后移动文件
|
||||
if(CommonSocket.SUCCESS.equalsIgnoreCase(result)){
|
||||
if(CommonSocket.DATA_TYPE_ZIP_DETECT.equalsIgnoreCase(dataType)){
|
||||
FileUtil.moveFile(file, Contants.localDataDonePath, true);
|
||||
}else if(CommonSocket.DATA_TYPE_ZIP_TASKRESULT.equalsIgnoreCase(dataType)){
|
||||
FileUtil.moveFile(file, Contants.localTaskDonePath, true);
|
||||
}else if(CommonSocket.DATA_TYPE_ZIP_TASKRETURN.equalsIgnoreCase(dataType)){
|
||||
FileUtil.moveFile(file, Contants.localTaskDonePath, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sendZipFile(List<File> fileList, String dataType) throws Exception {
|
||||
for(File file : fileList){
|
||||
sendZipFile(file, dataType);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量发送ZIP的
|
||||
/*private void sendZipFile(List<File> fileList, String parDir) throws Exception {
|
||||
//打包上传文件请求
|
||||
socket.sendMessageByChar(CommonSocket.DETECT_DATA_TYPE_ZIP);
|
||||
socket.receiveMessageByChar();
|
||||
//上传打包文件
|
||||
socket.bpSendFileByBath(fileList, parDir);
|
||||
String result = socket.receiveMessageByChar();
|
||||
//上传成功后移动文件
|
||||
if(CommonSocket.SUCCESS.equalsIgnoreCase(result)){
|
||||
for(File file : fileList){
|
||||
FileUtil.moveFile(file, Contants.localDataDonePath, true);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 批量发送CSV数据文件
|
||||
* @param dataDir
|
||||
* @param allFiles
|
||||
* @throws Exception
|
||||
*/
|
||||
private void sendCSVData(File dataDir, List<File> allFiles) throws Exception {
|
||||
//发送上传数据请求
|
||||
socket.sendMessageByChar(CommonSocket.DATA_TYPE_CSV_DETECT);
|
||||
socket.receiveMessageByChar();
|
||||
//上传数据
|
||||
socket.sendFileByBath(dataDir.getParent(), allFiles);
|
||||
String result = socket.receiveMessageByChar();
|
||||
if (CommonSocket.SUCCESS.equalsIgnoreCase(result)) {
|
||||
/**
|
||||
* 移动上传成功的数据文件到指定日期目录
|
||||
*/
|
||||
File[] files = new File[allFiles.size()];
|
||||
moveDetecDataToDateDir(allFiles.toArray(files));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量发送任务结果
|
||||
* @param fileArr
|
||||
* @throws Exception
|
||||
*/
|
||||
private void sendTaskResult(File[] fileArr) throws Exception {
|
||||
//2013-4-16 修改升序排列方式:按修改时间 改为 按文件名,任务结果文件名都有时间后缀(ms),文件修改时间只到s取不到ms
|
||||
fileArr = FileUtil.sortASCByFileName(fileArr);
|
||||
List<String> results = new LinkedList<String>();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for(File file : fileArr){
|
||||
sb.delete(0, sb.length());
|
||||
if(!file.exists() || !file.isFile()){
|
||||
continue;
|
||||
}
|
||||
String[] resultArr = FileWrUtil.cfgFileReader(file);
|
||||
if(resultArr!=null && resultArr.length>0){
|
||||
for(String res : resultArr){
|
||||
sb.append(res + ";");
|
||||
}
|
||||
sb.deleteCharAt(sb.length()-1);
|
||||
results.add(sb.toString());
|
||||
}
|
||||
}
|
||||
logger.debug("sendTaskResult-->" + Arrays.toString(results.toArray()));
|
||||
//发送任务结果请求
|
||||
socket.sendMessageByChar(CommonSocket.DATA_TYPE_OBJ_TASKRESULT);
|
||||
socket.receiveMessageByChar();
|
||||
//发送任务结果内容
|
||||
socket.sendObject(results);
|
||||
String result = socket.receiveMessageByChar();
|
||||
if (CommonSocket.SUCCESS.equalsIgnoreCase(result)) {
|
||||
// 移动上传成功的任务结果到指定日期目录
|
||||
moveTaskResultToDateDir(fileArr);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 单个发送任务回传文件
|
||||
* @param fileArr
|
||||
* @throws Exception
|
||||
*/
|
||||
private void sendTaskReturn(File[] fileArr) throws Exception {
|
||||
fileArr = FileUtil.sortASCByModify(fileArr); //修改日期升序排序
|
||||
for(File file : fileArr){
|
||||
if(!file.exists() || !file.isFile()){
|
||||
continue;
|
||||
}
|
||||
String[] resultArr = FileWrUtil.cfgFileReader(file);
|
||||
if (resultArr == null || resultArr.length <= 0) {
|
||||
continue;
|
||||
}
|
||||
JSONObject jsonObject = JSONObject.fromObject(resultArr[0]);
|
||||
ReturnFilePO rfPo = (ReturnFilePO) JSONObject.toBean(jsonObject, ReturnFilePO.class);
|
||||
|
||||
//--回传文件名和回传描述信息均为空时,则无回传文件
|
||||
if(StringUtil.isEmpty(rfPo.getReturnFileName()) && StringUtil.isEmpty(rfPo.getResDesc())){
|
||||
logger.warn("No return file, no return");
|
||||
FileUtil.delDir(file);
|
||||
continue;
|
||||
}
|
||||
//--回传文件名为空,但回传描述信息不为空,则进行步骤2发送任务结果、删除文件
|
||||
/**
|
||||
* 步骤1、回传文件
|
||||
*/
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if(rfPo.getResDesc()!=null){//取已有的结果描述信息
|
||||
sb.append(rfPo.getResDesc());
|
||||
}
|
||||
//准备回传文件,回传文件名不为空即有回传的文件时,再回传
|
||||
boolean success = false;
|
||||
if(rfPo.getReturnFileName()!=null && rfPo.getReturnFileName().trim().length()>0){
|
||||
//发送回传文件请求
|
||||
socket.sendMessageByChar(CommonSocket.DATA_TYPE_FILE_TASKETURN);
|
||||
socket.receiveMessageByChar();
|
||||
//发送回传文件任务信息
|
||||
socket.sendMessageByChar(TaskResultOper.getTaskResultMsg(rfPo
|
||||
.getTaskId(), rfPo.getTaskType(), null, null, null, rfPo
|
||||
.getStartTime(), rfPo.getEndTime(), rfPo.getIsLoop()));
|
||||
socket.receiveMessageByChar();
|
||||
//发送回传文件文件名称
|
||||
socket.sendMessageByChar(rfPo.getReturnFileName());
|
||||
socket.receiveMessageByChar();
|
||||
//发送回传文件
|
||||
socket.bpSendFile(Contants.localTaskReturnPath + File.separator + rfPo.getReturnFileName());
|
||||
String result = socket.receiveMessageByChar();
|
||||
success = true;
|
||||
// sb.append("回传成功");
|
||||
sb.append("i18n_client.ServerCollectData.transFile_n81i");
|
||||
}else{
|
||||
success = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 步骤2、判断文件是否回传完成
|
||||
*/
|
||||
if(success){
|
||||
/**
|
||||
* 步骤2-1、发送任务结果
|
||||
*/
|
||||
TaskResultOper.sendTaskResult(rfPo.getTaskId(), rfPo.getTaskType(),
|
||||
rfPo.getState(), sb.toString(), "", rfPo.getStartTime(),
|
||||
rfPo.getEndTime(), false, rfPo.getIsLoop());
|
||||
/**
|
||||
* 步骤2-2、移动上传成功的 保存回传文件信息的文件 和 回传文件 到指定日期目录
|
||||
*/
|
||||
moveTaskReturnToDateDir(file, rfPo.getReturnFileName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 移动上传成功的任务结果到指定日期目录
|
||||
* 完整文件到目录:.../done/result/yyyyMMdd
|
||||
* 0大小文件到目录: .../error/result/yyyyMMdd
|
||||
*/
|
||||
public static void moveTaskResultToDateDir(File... fileArr){
|
||||
if(fileArr==null || fileArr.length==0){
|
||||
return;
|
||||
}
|
||||
for (File file : fileArr) {
|
||||
String dirTime = DateUtil.getStingDate(
|
||||
DateUtil.YYYYMMDD,
|
||||
new Date(file.lastModified()));
|
||||
String newDir = "";
|
||||
if (file.length() > 0) {
|
||||
newDir = Contants.localTaskDonePath
|
||||
+ File.separator
|
||||
+ file.getParentFile().getName()
|
||||
+ File.separator + dirTime;
|
||||
} else {
|
||||
newDir = Contants.localTaskErrorPath
|
||||
+ File.separator
|
||||
+ file.getParentFile().getName()
|
||||
+ File.separator + dirTime;
|
||||
}
|
||||
// -- 文件移动
|
||||
FileUtil.moveFile(file, newDir, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动上传成功的 保存回传文件信息的文件 和 回传文件 到指定日期目录
|
||||
* 完整文件到目录:.../done/return/yyyyMMdd
|
||||
* 0大小文件到目录: .../error/return/yyyyMMdd
|
||||
*/
|
||||
public static void moveTaskReturnToDateDir(File file, String returnFileName){
|
||||
String dirTime = DateUtil.getStingDate(
|
||||
DateUtil.YYYYMMDD,
|
||||
new Date(file.lastModified()));
|
||||
String newDir = Contants.localTaskDonePath
|
||||
+ File.separator
|
||||
+ file.getParentFile().getName()
|
||||
+ File.separator + dirTime;
|
||||
// -- 文件移动
|
||||
FileUtil.moveFile(file, newDir, true);// 保留任务信息的临时文件.return
|
||||
if(returnFileName!=null && !"".equals(returnFileName)){// 实际回传的文件
|
||||
File curReturnFile = new File(Contants.localTaskReturnPath + File.separator + returnFileName);
|
||||
FileUtil.moveFile(curReturnFile, newDir, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动上传成功的数据文件到指定日期目录
|
||||
* 完整文件到目录:.../done/type_procIden/yyyyMMdd
|
||||
* 0大小文件到目录: .../error/type_procIden/yyyyMMdd
|
||||
*/
|
||||
public static void moveDetecDataToDateDir(File... allFiles){
|
||||
if(allFiles==null || allFiles.length==0){
|
||||
return;
|
||||
}
|
||||
for (File file : allFiles) {
|
||||
String dirTime = DateUtil.getStingDate(
|
||||
DateUtil.YYYYMMDD,
|
||||
new Date(file.lastModified()));
|
||||
String newDir = "";
|
||||
if (file.length() > 0) {
|
||||
newDir = Contants.localDataDonePath
|
||||
+ File.separator
|
||||
+ file.getParentFile().getName()
|
||||
+ File.separator + dirTime;
|
||||
} else {
|
||||
newDir = Contants.localDataErrorPath
|
||||
+ File.separator
|
||||
+ file.getParentFile().getName()
|
||||
+ File.separator + dirTime;
|
||||
}
|
||||
// -- 文件移动
|
||||
FileUtil.moveFile(file, newDir, true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
1928
src/com/nis/nmsclient/thread/task/AgentCommand.java
Normal file
1928
src/com/nis/nmsclient/thread/task/AgentCommand.java
Normal file
File diff suppressed because it is too large
Load Diff
106
src/com/nis/nmsclient/thread/task/LoopTaskThread.java
Normal file
106
src/com/nis/nmsclient/thread/task/LoopTaskThread.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package com.nis.nmsclient.thread.task;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.model.CommandPO;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
public class LoopTaskThread extends Thread{
|
||||
Logger logger = Logger.getLogger(LoopTaskThread.class);
|
||||
|
||||
private String threadName;
|
||||
private long loopDelay;
|
||||
private CommandPO command;
|
||||
|
||||
private long i = 0;
|
||||
private Future<?> future = null;
|
||||
private Thread thread;
|
||||
|
||||
private boolean isCancle;
|
||||
|
||||
public LoopTaskThread(String threadName, CommandPO command, long loopDelay, Future<?> singleFuture, Thread singleThread){
|
||||
this.threadName = threadName;
|
||||
this.loopDelay = loopDelay;
|
||||
this.command = command;
|
||||
this.future = singleFuture;
|
||||
this.thread = singleThread;
|
||||
|
||||
isCancle = false;
|
||||
}
|
||||
|
||||
public synchronized void cancle() {
|
||||
isCancle = true;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
i++;
|
||||
// Thread.currentThread().setName(threadName + " 周期" + i);
|
||||
Thread.currentThread().setName(threadName + " Cycle" + i);
|
||||
long et = System.currentTimeMillis();// 本次开始时间,即上次执行结束时间
|
||||
long st = et - (i * loopDelay * 60 * 1000);// 上次开始时间
|
||||
try {
|
||||
if (i == 1 && future != null
|
||||
&& !future.isCancelled()
|
||||
&& !future.isDone()) {
|
||||
try {
|
||||
future.get(1, TimeUnit.MICROSECONDS);
|
||||
} catch (TimeoutException e) {
|
||||
if(thread!=null && thread.isAlive()){
|
||||
thread.stop();
|
||||
logger.debug("LoopTaskThread run Timeout stop singleThread--" + thread.isAlive());
|
||||
}
|
||||
future.cancel(true);
|
||||
}
|
||||
|
||||
}
|
||||
if(!isCancle){
|
||||
// 周期开始执行
|
||||
future = Common.scheduled.schedule(new Runnable() {
|
||||
public void run() {
|
||||
thread = Thread.currentThread();
|
||||
// Thread.currentThread().setName(threadName + " 周期" + i);
|
||||
Thread.currentThread().setName(threadName + " Cycle" + i);
|
||||
new AgentCommand(command).exec();
|
||||
}
|
||||
}, 0, TimeUnit.MILLISECONDS);
|
||||
try {
|
||||
future.get(loopDelay, TimeUnit.MINUTES);
|
||||
} catch (TimeoutException e) {
|
||||
if(thread!=null && thread.isAlive()){
|
||||
thread.stop();
|
||||
logger.debug("LoopTaskThread run Timeout stop thread--" + thread.isAlive());
|
||||
}
|
||||
future.cancel(true);
|
||||
logger.debug("---------LoopTaskThread run timeout----------");
|
||||
TaskResultOper.sendTaskResult(command.getExecId(),
|
||||
command.getExecType(),
|
||||
AgentCommand.RESULT_FAIL,
|
||||
// "本周期任务执行超时", "", new Date(st),
|
||||
// "this task execution timeout", "", new Date(st),
|
||||
"i18n_client.LoopTaskThread.loopTaskOuttime_n81i", "", new Date(st),
|
||||
new Date(et), false, command.getIsLoop());
|
||||
}catch (InterruptedException e) {
|
||||
if(thread!=null && thread.isAlive()){
|
||||
thread.stop();
|
||||
logger.debug("LoopTaskThread run Interrupted stop thread--" + thread.isAlive());
|
||||
}
|
||||
future.cancel(true);
|
||||
logger.debug("---------LoopTaskThread run Interrupted----------");
|
||||
}
|
||||
}
|
||||
|
||||
if(thread!=null){
|
||||
logger.debug("LoopTaskThread run thread state--" + thread.isAlive());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.info(Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
397
src/com/nis/nmsclient/thread/task/TaskReqHandle.java
Normal file
397
src/com/nis/nmsclient/thread/task/TaskReqHandle.java
Normal file
@@ -0,0 +1,397 @@
|
||||
package com.nis.nmsclient.thread.task;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.model.CommandPO;
|
||||
import com.nis.nmsclient.model.ParamFilePush;
|
||||
import com.nis.nmsclient.model.ParamUpgrade;
|
||||
import com.nis.nmsclient.model.Task4;
|
||||
import com.nis.nmsclient.model.Task6;
|
||||
import com.nis.nmsclient.thread.socket.CommonSocket;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.ProcessUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
public class TaskReqHandle {
|
||||
Logger logger = Logger.getLogger(TaskReqHandle.class);
|
||||
private Thread singleThread;
|
||||
|
||||
/**
|
||||
* 任务请求处理步骤1:分析任务请求参数,分不同任务类型处理
|
||||
*/
|
||||
public void taskHandle(String str){
|
||||
Date execTime = new Date();
|
||||
boolean flag = true;
|
||||
int taskType = 0;
|
||||
long taskId = 0;
|
||||
String threadName = null;
|
||||
CommandPO command = null;
|
||||
try {
|
||||
JSONObject jsonObj = JSONObject.fromObject(str);
|
||||
if(str.contains("typeInfo")){
|
||||
taskType = jsonObj.getInt("typeInfo");
|
||||
}
|
||||
if(str.contains("taskInfo")){
|
||||
JSONObject jsonObj2 = jsonObj.getJSONObject("taskInfo");
|
||||
Object obj = null;
|
||||
/**
|
||||
* 任务类型:2 非流文本数据获取,3 流文本数据获取,4 命令执行,5 shell注册, 6升级
|
||||
*/
|
||||
switch (taskType) {
|
||||
case 4:
|
||||
obj = JSONObject.toBean(jsonObj2,Task4.class);
|
||||
Task4 task4 = (Task4) obj;
|
||||
taskId = task4.getTaskId();
|
||||
/**
|
||||
* 命令类型:1 Agent原生支持命令,2可执行命令(2 脚本,3 shell命令)
|
||||
*/
|
||||
if(task4.getCommandType() == 1){
|
||||
// threadName = "原生命令 id:" + task4.getTaskId() + ">>" + task4.getCommandName();
|
||||
threadName = "Native command ID:" + task4.getTaskId() + ">>" + task4.getCommandName();
|
||||
}else if(task4.getCommandType() == 2){
|
||||
// threadName = "可执行命令 id:" + task4.getTaskId();
|
||||
threadName = "Executable command ID:" + task4.getTaskId();
|
||||
}
|
||||
command = new CommandPO();
|
||||
command.setExecId(task4.getTaskId());
|
||||
command.setExecType(task4.getTaskType());
|
||||
command.setCommandName(task4.getCommandName());
|
||||
command.setCommandParam(task4.getCommandParam());
|
||||
command.setExecState(task4.getState());
|
||||
command.setExecVersion(null);
|
||||
command.setIsLoop(task4.getIsLoop());
|
||||
|
||||
handleTaskThread(task4.getTaskId(), task4.getStartTime(), task4.getEndTime(), task4
|
||||
.getIsLoop(), task4.getLoopDelay(), command, threadName, task4.getMissionState());
|
||||
|
||||
break;
|
||||
case 6:
|
||||
obj = JSONObject.toBean(jsonObj2,Task6.class);
|
||||
Task6 task6 = (Task6) obj;
|
||||
taskId = task6.getTaskId();
|
||||
// threadName = "升级 id:" + task6.getTaskId() + ">>" + task6.getCommandName();
|
||||
threadName = "Upgrade ID:" + task6.getTaskId() + ">>" + task6.getCommandName();
|
||||
command = new CommandPO();
|
||||
command.setExecId(task6.getTaskId());
|
||||
command.setExecType(task6.getTaskType());
|
||||
command.setCommandName(task6.getCommandName());
|
||||
command.setCommandParam(task6.getCommandParam());
|
||||
command.setExecState(task6.getState());
|
||||
command.setExecVersion(task6.getVersion());
|
||||
command.setSrcPath(getUpgradeTaskPushPath(task6.getTaskId()));
|
||||
|
||||
handleTaskThread(task6.getTaskId(), task6.getUpgradeTime(), null, 0, 0, command, threadName, 0);
|
||||
break;
|
||||
default:
|
||||
flag = false;
|
||||
break;
|
||||
}
|
||||
}else{
|
||||
flag = false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
flag = false;
|
||||
}
|
||||
|
||||
if(!flag){
|
||||
TaskResultOper.sendTaskResult(taskId, taskType,
|
||||
// AgentCommand.RESULT_FAIL, "发送内容格式不正确", "", execTime, new Date(), false, -1l);
|
||||
// AgentCommand.RESULT_FAIL, "Incorrect content format", "", execTime, new Date(), false, -1l);
|
||||
AgentCommand.RESULT_FAIL, "i18n_client.TaskReqHandle.sendInfoFormatErr_n81i", "", execTime, new Date(), false, -1l);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件推送处理
|
||||
*/
|
||||
public String filePush(CommonSocket socket, String taskParam, long taskId, boolean isUpgrade){
|
||||
String msg = null;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
File tempDir = null;
|
||||
try {
|
||||
tempDir = new File(Contants.localTempPath + File.separator
|
||||
+ "filepush_" + taskId);
|
||||
if (!tempDir.exists()) {
|
||||
tempDir.mkdirs();
|
||||
}
|
||||
// ------步骤1:接收Md5校验的推送文件到临时目录
|
||||
int flag = socket.bpReceiveFileByBathMd5(tempDir.getAbsolutePath());
|
||||
|
||||
if (flag == 0){// ------步骤2:接收成功,与参数比对
|
||||
if(taskParam==null || "".equals(taskParam)){
|
||||
msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "i18n_client.TaskReqHandle.pushFileParamIsNull_n81i";
|
||||
// logger.info(msg);//i18nlog
|
||||
return msg;
|
||||
}
|
||||
// ------步骤2-1:解析参数
|
||||
String[] params = taskParam.trim().split(AgentCommand.PARAM_SEPRATOR);
|
||||
if (params != null && params.length >= 1) {
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
//2012-4-28 任务参数中对路径的格式化将在界面上进行,原因此处会对转义字符的\也转换为/,故replace("\\", "/")去掉
|
||||
params[i] = params[i].trim().replaceAll("[\n\t\r]","");//.replace("\\", "/");//[\\s*\n\t\r]
|
||||
logger.debug("filePush-->param: " + params[i]);
|
||||
ParamFilePush fParam = null;
|
||||
if(isUpgrade){
|
||||
ParamUpgrade cfu = (ParamUpgrade) JSONObject.toBean(JSONObject
|
||||
.fromObject(params[i]), ParamUpgrade.class);
|
||||
fParam = new ParamFilePush();
|
||||
fParam.setFileName(cfu.getFileName());
|
||||
fParam.setUsername(cfu.getUsername());
|
||||
fParam.setGroupName(cfu.getGroupName());
|
||||
fParam.setParam1(cfu.getParam1());
|
||||
}else{
|
||||
fParam = (ParamFilePush) JSONObject.toBean(
|
||||
JSONObject.fromObject(params[i]),
|
||||
ParamFilePush.class);
|
||||
}
|
||||
if(fParam.getDestPath()==null || fParam.getDestPath().trim().length()<=0){
|
||||
fParam.setDestPath(getUpgradeTaskPushPath(taskId));//设置默认推送目的地
|
||||
logger.debug("filePush-->destPath: " + fParam.getDestPath());
|
||||
}
|
||||
if(fParam.getFileName()==null || fParam.getFileName().trim().length()<=0){
|
||||
// msg = "推送文件名参数为空";
|
||||
// msg = "File push parameters are empty";
|
||||
msg = "i18n_client.TaskReqHandle.pushFileNameParamIsNull_n81i";
|
||||
// logger.debug(msg);//i18nlog
|
||||
break;
|
||||
}
|
||||
/*if(!ProcessUtil.checkUserPass(fParam.getUsername(), fParam.getParam1())){
|
||||
msg = "[" + fParam.getFileName() + "]推送文件的用户名或密码不正确;";
|
||||
logger.debug(msg);
|
||||
break;
|
||||
}*/
|
||||
// 判断用户名是否正确
|
||||
if(!ProcessUtil.checkUserOrGroupExist(fParam.getUsername(), fParam.getGroupName())){
|
||||
// msg = "[" + fParam.getFileName() + "]推送文件的属主或属群不正确;";
|
||||
// msg = "[" + fParam.getFileName() + "]The owner or group of the push file is incorrect;";
|
||||
msg = "[" + fParam.getFileName() + "]i18n_client.TaskReqHandle.userGroupErr_n81i;";
|
||||
// logger.debug(msg);//i18nlog
|
||||
break;
|
||||
}
|
||||
// ------步骤2-2:文件存在并与Md5值比较文件是否完整
|
||||
File pushFile = new File(tempDir.getAbsolutePath()
|
||||
+ File.separator + fParam.getFileName());
|
||||
if (!pushFile.exists()){
|
||||
// msg = "推送临时文件不存在,请检查推送文件名称与参数文件名("
|
||||
// + fParam.getFileName() + ")是否一致";
|
||||
// msg = "The push temporary file does not exist. Please check whether the push file name is consistent with the parameter file name("
|
||||
// + fParam.getFileName() + ")";
|
||||
msg = "i18n_client.TaskReqHandle.pushFileNotExists_n81i("
|
||||
+ fParam.getFileName() + ")";
|
||||
// logger.warn(msg + "--" + pushFile.getAbsolutePath());//i18nlog
|
||||
break;
|
||||
}
|
||||
// ------步骤2-3:判断推送目录是否存在,不存在创建
|
||||
File destFile = new File(fParam.getDestPath()
|
||||
+ File.separator + fParam.getFileName());
|
||||
if(!destFile.getParentFile().exists()){
|
||||
destFile.getParentFile().mkdirs();
|
||||
}
|
||||
// ------步骤2-4:判断是否直接覆盖
|
||||
if (fParam.getIsCover() != null
|
||||
&& "Y".equalsIgnoreCase(fParam.getIsCover())) {// 覆盖,则直接Copy并赋权限与所有者
|
||||
if (destFile.exists()) {
|
||||
//destFile.delete_bak();
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(destFile);
|
||||
logger.debug("filePush delete file--" + destFile.getAbsolutePath());
|
||||
//FileUtil.checkParentDirExist(destFile);
|
||||
}
|
||||
msg = copyAndSetPermission(pushFile
|
||||
.getCanonicalPath(), destFile
|
||||
.getCanonicalPath(), fParam.getUsername(),
|
||||
fParam.getGroupName(), fParam
|
||||
.getPermisson());
|
||||
} else if (!destFile.exists()) {// 不覆盖,则判断文件不存在的话,再Copy并赋权限与所有者
|
||||
msg = copyAndSetPermission(pushFile
|
||||
.getCanonicalPath(), destFile
|
||||
.getCanonicalPath(), fParam.getUsername(),
|
||||
fParam.getGroupName(), fParam
|
||||
.getPermisson());
|
||||
}
|
||||
if(msg==null || msg.length()<=0){
|
||||
// sb.append("[" + fParam.getFileName() + "]成功推送到[" + fParam.getDestPath() + "];");
|
||||
// sb.append("[" + fParam.getFileName() + "]successfully pushed to[" + fParam.getDestPath() + "];");
|
||||
sb.append("[" + fParam.getFileName() + "]i18n_client.TaskReqHandle.successPush_n81i[" + fParam.getDestPath() + "];");
|
||||
logger.debug("推送文件" + (i+1) + "成功---" + pushFile.getCanonicalPath());
|
||||
}else{
|
||||
// msg = msg+"[" + fParam.getFileName() + "]推送失败;";//文件推送失败的具体原因
|
||||
msg = msg+"[" + fParam.getFileName() + "]i18n_client.TaskReqHandle.pushFail_n81i;";//文件推送失败的具体原因
|
||||
logger.debug("推送文件" + (i+1) + "失败---" + pushFile.getCanonicalPath());
|
||||
break;
|
||||
}
|
||||
}//for end
|
||||
} else {
|
||||
// msg = "文件推送参数不正确";
|
||||
// msg = "File push parameter is incorrect";
|
||||
msg = "i18n_client.TaskReqHandle.pushParamErr_n81i";
|
||||
// logger.warn(msg + "<" + taskParam + ">");//i18nlog
|
||||
}
|
||||
//所有文件推送成功,删除临时接收文件目录
|
||||
if(msg==null || msg.length()<=0){
|
||||
if(tempDir!=null && tempDir.exists()){
|
||||
try {
|
||||
logger.debug("删除临时目录--" + tempDir.getAbsolutePath());
|
||||
FileUtils.deleteDirectory(tempDir);
|
||||
FileUtil.checkParentDirExist(tempDir);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}else {
|
||||
socket.close();
|
||||
}
|
||||
|
||||
if(msg == null){
|
||||
msg = Contants.COMMON_MSG_SUCCESS + Contants.COMMON_MSG_SEPRATOR + sb.toString();
|
||||
}else {
|
||||
sb.append(msg);
|
||||
msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + sb.toString();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Receive push file exception:" + Utils.printExceptionStack(e));
|
||||
// msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "接收推送文件异常," + e.getMessage();
|
||||
// msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "Received push file exception," + e.getMessage();
|
||||
msg = Contants.COMMON_MSG_FAIL + Contants.COMMON_MSG_SEPRATOR + "i18n_client.TaskReqHandle.reciveFileErr_n81i," + e.getMessage();
|
||||
return msg;
|
||||
}finally{
|
||||
if(tempDir!=null && tempDir.exists() && tempDir.listFiles().length==0){
|
||||
try {
|
||||
FileUtils.deleteDirectory(tempDir);
|
||||
logger.debug("finally删除临时目录--" + tempDir.getAbsolutePath());
|
||||
FileUtil.checkParentDirExist(tempDir);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件推送部分的拷备工作,由临时文件目录拷备到推送的最终目的地,并赋相应的权限组
|
||||
*/
|
||||
private String copyAndSetPermission(String source, String destFile, String user, String group, String permission) throws Exception{
|
||||
String result = null;
|
||||
if (source != null && destFile != null) {
|
||||
// 根据操作系统确定获取进程ID的方式
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.startsWith("Windows")) {
|
||||
FileUtils.copyFile(new File(source), new File(
|
||||
destFile));//目标路径不存在自动创建
|
||||
} else if (os.startsWith("Linux")) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
//source destFile都不能含有空格
|
||||
source = source.replace(" ", "\\ ");
|
||||
destFile = destFile.replace(" ", "\\ ");
|
||||
sb.append("\\cp -f " + source + " " + destFile + ";");//2015-11-6 hyx: cp - f修改成\\cp -f (有时候如果不加\\会提示是否,就会有问题)
|
||||
if (permission != null && !"".equals(permission.trim())) {
|
||||
sb.append("chmod " + permission + " "
|
||||
+ destFile + ";");
|
||||
}
|
||||
if (user != null && !"".equals(user.trim())) {
|
||||
sb.append("chown " + user + " " + destFile + ";");
|
||||
}
|
||||
if (group != null && !"".equals(group.trim())) {
|
||||
sb.append("chgrp " + group + " " + destFile);
|
||||
}
|
||||
result = ProcessUtil.execLinuxCmd(sb.toString());
|
||||
} else {
|
||||
throw new IOException("unknown operating system: " + os);
|
||||
}
|
||||
}else{
|
||||
// result = "源文件或目标文件为空";
|
||||
// result = "The source file or target file is empty";
|
||||
result = "i18n_client.TaskReqHandle.sourceOrTargetIsNull_n81i";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 任务请求处理步骤2:将分析包装好的任务,统一判断处理并添加到线程中执行
|
||||
*/
|
||||
public void handleTaskThread(Long taskId, Long startTime, Long endTime,
|
||||
long isLoop, long loopDelay, final CommandPO command,
|
||||
final String threadName, long missionState) {
|
||||
if(missionState == AgentCommand.MISSION_CANCEL_START){//如果任务状态为,撤消任务
|
||||
logger.warn("The task is in the revocation, and the ID is not processed:" + taskId);
|
||||
return;
|
||||
}
|
||||
if(Common.getTaskFuture(taskId)!=null){//当前任务已存在执行,则不执行该当前任务
|
||||
logger.warn("The task already exists to execute the ID:" + taskId);
|
||||
return;
|
||||
}
|
||||
// 设置任务结束时间,且当前时间已超过任务结束时间
|
||||
if (endTime != null && endTime.longValue() > 0
|
||||
&& endTime.longValue() <= System.currentTimeMillis()) {
|
||||
logger.warn("The task has expired ID:" + taskId);
|
||||
return;
|
||||
}
|
||||
long delay = 0;
|
||||
if (startTime != null) {
|
||||
delay = startTime - System.currentTimeMillis();
|
||||
}
|
||||
ScheduledFuture<?> taskFuture = null;
|
||||
LoopTaskThread loopTaskThread = null;
|
||||
if (isLoop == 0) {// 非周期任务
|
||||
taskFuture = Common.scheduled.schedule(new Runnable() {
|
||||
public void run() {
|
||||
Thread.currentThread().setName(threadName);
|
||||
new AgentCommand(command).exec();
|
||||
}
|
||||
}, delay, TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
Future<?> singleFuture = null;
|
||||
if(delay <= 0){//开始时间之后接到任务,先执行一次,第二次按周期点执行
|
||||
long now = System.currentTimeMillis();
|
||||
long cnt = (now - startTime)/(loopDelay * 60 * 1000);
|
||||
if((now - startTime)%(loopDelay * 60 * 1000)!=0){
|
||||
delay = startTime + loopDelay *60 *1000 * (cnt + 1) - System.currentTimeMillis();
|
||||
///仅执行一次的,并在周期执行代码中第一次执行判断单次执行完成与否,未完成结束掉
|
||||
singleFuture = Common.scheduled.schedule(new Runnable() {
|
||||
public void run() {
|
||||
singleThread = Thread.currentThread();
|
||||
// Thread.currentThread().setName(threadName + " 周期单次");
|
||||
Thread.currentThread().setName(threadName + " Periodic Single Time");
|
||||
new AgentCommand(command).exec();
|
||||
}
|
||||
}, 0, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
loopTaskThread = new LoopTaskThread(threadName, command, loopDelay, singleFuture, singleThread);
|
||||
taskFuture = Common.scheduled.scheduleAtFixedRate(loopTaskThread, delay, loopDelay * 60 * 1000, TimeUnit.MILLISECONDS);
|
||||
// 周期任务,若设置结束时间,则添加取消线程
|
||||
if (endTime != null && endTime.longValue() > 0) {
|
||||
long endDelay = endTime.longValue() - System.currentTimeMillis();
|
||||
if (endDelay > 0) {
|
||||
Common.cancleTaskFuture(taskId, endDelay);
|
||||
}
|
||||
}// 取消线程结束
|
||||
|
||||
}
|
||||
// 将正在执行的任务添加到全局变量,目的是避免重复执行任务
|
||||
Common.putTaskFuture(taskId, taskFuture, loopTaskThread);
|
||||
}
|
||||
|
||||
public static String getUpgradeTaskPushPath(long taskId){
|
||||
return Contants.localUploadsPath + File.separator + taskId;
|
||||
}
|
||||
}
|
||||
|
||||
327
src/com/nis/nmsclient/thread/task/TaskResultOper.java
Normal file
327
src/com/nis/nmsclient/thread/task/TaskResultOper.java
Normal file
@@ -0,0 +1,327 @@
|
||||
package com.nis.nmsclient.thread.task;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.common.VersionCfg;
|
||||
import com.nis.nmsclient.config.DetecConfOper;
|
||||
import com.nis.nmsclient.thread.socket.CommonSocket;
|
||||
import com.nis.nmsclient.thread.socket.SSLClient;
|
||||
import com.nis.nmsclient.thread.socket.ServerCollectData;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.FileWrUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
import com.nis.nmsclient.util.ZipUtil;
|
||||
|
||||
public class TaskResultOper {
|
||||
static Logger logger = Logger.getLogger(DetecConfOper.class);
|
||||
|
||||
/**
|
||||
* Agent在启动时,发送所有之前发送失败的任务结果
|
||||
*/
|
||||
public static void initSendAllTaskResult(){
|
||||
try {
|
||||
//针对结果文件过多时打包上传未完成的文件继续上传
|
||||
File taskDir = new File(Contants.localTaskPath);
|
||||
if (!taskDir.exists()) {
|
||||
return;
|
||||
}
|
||||
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
|
||||
if (zipArr != null && zipArr.length > 0) {
|
||||
for (File file : zipArr) {
|
||||
if (!file.getName().startsWith(CommonSocket.BP_TYPE_TASK_RESULT)) {
|
||||
continue;
|
||||
}
|
||||
Future<?> future = Common.service.submit(new SSLClient(Thread
|
||||
.currentThread().getName(),
|
||||
CommonSocket.REQ_BP_UPLOAD_FIFE, new String[] {
|
||||
CommonSocket.BP_TYPE_TASK_RESULT, file.getAbsolutePath() }));
|
||||
future.get();
|
||||
}
|
||||
}
|
||||
//-------------------------------------
|
||||
|
||||
File resultDir = new File(getTaskResultPath());
|
||||
if(!resultDir.exists()){
|
||||
return;
|
||||
}
|
||||
File[] fileArr = FileUtil.getFilesEndWith(resultDir, Contants.TASK_RESULT_FILE_SUFFIX);
|
||||
if(fileArr == null || fileArr.length == 0){
|
||||
/*
|
||||
* 当任务正常执行完成或者取消后则从全局变量移除
|
||||
*/
|
||||
Common.removeCancelAndDoneTaskFuture();
|
||||
return;
|
||||
}
|
||||
//--- 将所有任务结果文件一起打包,发送
|
||||
if(fileArr.length > Contants.COMMON_ZIP_MIN_SIZE){
|
||||
//与Server通信
|
||||
Future<?> serFuture = Common.service.submit(new SSLClient(
|
||||
Thread.currentThread().getName(),
|
||||
CommonSocket.REQ_HAND_SHAKE, null));
|
||||
if (!Contants.isSucessByResult((String) serFuture.get())) {
|
||||
return;
|
||||
}
|
||||
|
||||
int zipCnt = fileArr.length/Contants.COMMON_ZIP_MAX_SIZE;
|
||||
if(zipCnt>0){//2013-5-6 未上传的结果文件太多时,将结果文件压缩为多个文件
|
||||
for(int i=0; i<fileArr.length/Contants.COMMON_ZIP_MAX_SIZE; i++){
|
||||
// 组织每次压缩的文件数组
|
||||
File[] resultFiles = new File[Contants.COMMON_ZIP_MAX_SIZE];
|
||||
int start = i * Contants.COMMON_ZIP_MAX_SIZE;
|
||||
int end = (i + 1) * Contants.COMMON_ZIP_MAX_SIZE;
|
||||
for(int j=start,k=0; j<end; j++,k++){
|
||||
resultFiles[k] = fileArr[j];
|
||||
}
|
||||
// 压缩并发送任务结果
|
||||
String compressFileStr = Contants.localTaskPath
|
||||
+ File.separator
|
||||
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RESULT, null, true)
|
||||
+ ".zip";
|
||||
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现,现修改将任务结果和回传文件压缩时不删除文件,而是将其移动到相应的日期目录
|
||||
ZipUtil.zipWithMoveFile(resultDir.listFiles(), compressFileStr, true);
|
||||
//发送
|
||||
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
|
||||
.getName(), CommonSocket.REQ_BP_UPLOAD_FIFE,
|
||||
new String[] { CommonSocket.BP_TYPE_TASK_RESULT, compressFileStr }));
|
||||
future.get();
|
||||
}
|
||||
}else{
|
||||
// 压缩并发送任务结果
|
||||
String compressFileStr = Contants.localTaskPath
|
||||
+ File.separator
|
||||
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RESULT, null, true)
|
||||
+ ".zip";
|
||||
// 2013-03-22 由于DC再次获取未保存任务结果这个功能的实现,现修改将任务结果和回传文件压缩时不删除文件,而是将其移动到相应的日期目录
|
||||
ZipUtil.zipWithMoveFile(resultDir.listFiles(), compressFileStr, true);
|
||||
//发送
|
||||
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
|
||||
.getName(), CommonSocket.REQ_BP_UPLOAD_FIFE,
|
||||
new String[] { CommonSocket.BP_TYPE_TASK_RESULT, compressFileStr }));
|
||||
future.get();
|
||||
}
|
||||
}else{//-- 按正常的一个结果一个结果的发送
|
||||
fileArr = FileUtil.sortASCByFileName(fileArr); //按文件名升序排列,任务结果文件名都有时间后缀(ms)
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for(File file : fileArr){
|
||||
sb.delete(0, sb.length());
|
||||
if(!file.exists() || !file.isFile()){
|
||||
continue;
|
||||
}
|
||||
String[] resultArr = FileWrUtil.cfgFileReader(file);
|
||||
if(resultArr!=null && resultArr.length>0){
|
||||
for(String res : resultArr){
|
||||
sb.append(res + ";");
|
||||
}
|
||||
sb.deleteCharAt(sb.length()-1);
|
||||
Future<?> future = Common.service.submit(new SSLClient(
|
||||
// "上传任务结果",
|
||||
"Upload Task Results",
|
||||
CommonSocket.REQ_TASK_RESULT, sb.toString()));
|
||||
String msg = (String) future.get();
|
||||
if (Contants.isSucessByResult(msg)) {
|
||||
// 移动上传成功的任务结果到指定日期目录
|
||||
ServerCollectData.moveTaskResultToDateDir(file);
|
||||
}
|
||||
}else{
|
||||
// 移动上传成功的任务结果到指定日期目录
|
||||
ServerCollectData.moveTaskResultToDateDir(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Upload task result exception:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理Agent自动升级的最终结果文件,即升级后是否启动成功
|
||||
* Agent升级的临时结果文件,初始写入状态为升级失败:
|
||||
* 1、当启动失败后将其临时文件后缀改为任务结果文件的后缀;
|
||||
* 2、当启动成功后先修改文件第一行的后两项内容为执行状态与描述为成功,再改其后缀。
|
||||
*
|
||||
* @param isSuccess
|
||||
*/
|
||||
public static void handerAgentUpgradeResult(boolean isSuccess){
|
||||
try {
|
||||
File dir = new File(getTaskResultPath());
|
||||
if(!dir.exists()){
|
||||
return;
|
||||
}
|
||||
File[] fileArr = FileUtil.getFilesEndWith(dir, Contants.TASK_RESULT_AGENTTMPFILE_SUFFIX);
|
||||
fileArr = FileUtil.sortASCByFileName(fileArr); //按文件名升序排列,任务结果文件名都有时间后缀(ms)
|
||||
if (fileArr != null && fileArr.length > 0) {
|
||||
for(int i=0; i<fileArr.length; i++){
|
||||
if (isSuccess && i == fileArr.length-1) {// 如果启动成功处理其结果内容
|
||||
try {// 本层try是为了防止其中一个结果文件不正常,影响其他结果文件不能正常修改
|
||||
String[] msgs = FileWrUtil.cfgFileReader(fileArr[i]);
|
||||
if (msgs != null && msgs.length > 0) {
|
||||
int descIndex = msgs[0]
|
||||
.lastIndexOf(Contants.COMMON_MSG_SEPRATOR);
|
||||
int stateIndex = msgs[0].substring(0, descIndex)
|
||||
.lastIndexOf(Contants.COMMON_MSG_SEPRATOR);
|
||||
msgs[0] = msgs[0].substring(0, stateIndex)
|
||||
+ Contants.COMMON_MSG_SEPRATOR + AgentCommand.RESULT_OK
|
||||
// + Contants.COMMON_MSG_SEPRATOR + "重启成功";
|
||||
+ Contants.COMMON_MSG_SEPRATOR + "i18n_client.TaskResultOper.restart_n81i";
|
||||
|
||||
FileWrUtil.cfgFilePrinter(fileArr[i], Contants.charset, msgs);
|
||||
|
||||
int taskIdIndex = msgs[0].indexOf(Contants.COMMON_MSG_SEPRATOR);
|
||||
//处理写入当前更新的版本信息
|
||||
String taskId = msgs[0].substring(0, taskIdIndex);
|
||||
Long curVer = Long.parseLong(VersionCfg
|
||||
.getValue(VersionCfg.NAGENT_VERSION));
|
||||
if (curVer < Long.parseLong(taskId.trim())) {
|
||||
VersionCfg.setValue(VersionCfg.NAGENT_VERSION, taskId);
|
||||
logger.info("NC更新为版本" + taskId);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Handling the exception of the NC upgrade result file:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
String fileStr = fileArr[i].getAbsolutePath();
|
||||
int index = fileStr.lastIndexOf(Contants.TASK_RESULT_AGENTTMPFILE_SUFFIX);
|
||||
fileStr = fileStr.substring(0, index);
|
||||
fileArr[i].renameTo(new File(fileStr + Contants.TASK_RESULT_FILE_SUFFIX));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Handling the exception of the NC upgrade result file:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送任务执行结果:若发送失败写入文件
|
||||
*/
|
||||
/*private static void sendTaskResult(long taskId, long taskType, String msg) {
|
||||
try{
|
||||
Future<?> future = Common.service.submit(new SSLClient(
|
||||
Thread.currentThread().getName(),
|
||||
CommonSocket.REQ_TASK_RESULT, msg));
|
||||
|
||||
String result = (String) future.get();
|
||||
|
||||
if (!Contants.isSucessByResult(result)) {//失败
|
||||
File file = new File(getTaskResultFile(taskType, taskId));
|
||||
if(!file.getParentFile().exists()){
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
FileWrUtil.cfgFilePrinter(file, Contants.charset, new String[]{msg});
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error("发送任务结果异常:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 发送任务执行结果:判断是否是Server升级,若是直接写入文件,若不是则发送结果
|
||||
* @param taskId 任务ID
|
||||
* @param taskType 任务类型
|
||||
* @param resultState 执行结果状态
|
||||
* @param resultDesc 执行结果描述
|
||||
* @param resultConfig
|
||||
* @param startTime 开始执行时间
|
||||
* @param endTime 执行结束时间
|
||||
* @param isServer 是否是Server升级
|
||||
* @param isLoop 是否是循环任务
|
||||
*/
|
||||
/*public static void sendTaskResult(long taskId, long taskType,
|
||||
long resultState, String resultDesc, String resultConfig,
|
||||
Date startTime, Date endTime, boolean isServer, long isLoop) {
|
||||
try {
|
||||
String msg = getTaskResultMsg(taskId, taskType, resultState,
|
||||
resultDesc, resultConfig, startTime, endTime, isLoop);
|
||||
if(isServer){//如果是Server升级,直接写入文件
|
||||
File file = new File(getTaskResultFile(taskType, taskId));
|
||||
if(!file.getParentFile().exists()){
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
FileWrUtil.cfgFilePrinter(file, Contants.charset, new String[]{msg});
|
||||
}else{//反之,发送任务结果
|
||||
sendTaskResult(taskId, taskType, msg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("发送任务结果异常:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 发送任务结果: 将结果保存到文件,等待DC主动来收集
|
||||
* @date Jan 15, 2013
|
||||
* @author zhenzhen
|
||||
* @version
|
||||
*/
|
||||
public static void sendTaskResult(long taskId, long taskType,
|
||||
long resultState, String resultDesc, String resultConfig,
|
||||
Date startTime, Date endTime, boolean isServer, long isLoop) {
|
||||
try {
|
||||
// 2013-4-9 为了防止多步操作时结果文件名重复,所以暂停50ms为了使时间后缀不一样
|
||||
if (resultState > AgentCommand.RESULT_SEND_OK) {
|
||||
try {Thread.sleep(50);} catch (Exception ignored) { }
|
||||
}
|
||||
|
||||
String msg = getTaskResultMsg(taskId, taskType, resultState,
|
||||
resultDesc, resultConfig, startTime, endTime, isLoop);
|
||||
File file = new File(getTaskResultFile(taskType, taskId));
|
||||
if(!file.getParentFile().exists()){
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
FileWrUtil.cfgFilePrinter(file, Contants.charset, new String[]{msg});
|
||||
} catch (Exception e) {
|
||||
logger.error("Send task result exception:" + Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按指定的分隔符和顺序拼写任务执行结果信息
|
||||
*/
|
||||
public static String getTaskResultMsg(long taskId, long taskType,
|
||||
Long resultState, String resultDesc, String resultConfig,
|
||||
Date startTime, Date endTime, long isLoop) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(taskId);
|
||||
sb.append(Contants.COMMON_MSG_SEPRATOR);
|
||||
sb.append(taskType);
|
||||
sb.append(Contants.COMMON_MSG_SEPRATOR);
|
||||
sb.append(Contants.AGENT_HOST_UUID);
|
||||
sb.append(Contants.COMMON_MSG_SEPRATOR);
|
||||
sb.append(isLoop);
|
||||
sb.append(Contants.COMMON_MSG_SEPRATOR);
|
||||
sb.append(startTime.getTime());
|
||||
sb.append(Contants.COMMON_MSG_SEPRATOR);
|
||||
sb.append(endTime.getTime());
|
||||
sb.append(Contants.COMMON_MSG_SEPRATOR);
|
||||
sb.append(resultConfig);
|
||||
sb.append(Contants.COMMON_MSG_SEPRATOR);
|
||||
sb.append(resultState);
|
||||
sb.append(Contants.COMMON_MSG_SEPRATOR);
|
||||
sb.append(resultDesc);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String getTaskResultFile(long taskType, long taskId){
|
||||
return getTaskResultFileNoSuffix(taskType, taskId) + "_" + System.currentTimeMillis()
|
||||
+ Contants.TASK_RESULT_FILE_SUFFIX;
|
||||
}
|
||||
|
||||
public static String getAgentUpgradeResultTempFile(long taskType, long taskId){
|
||||
return getTaskResultFileNoSuffix(taskType, taskId)
|
||||
+ Contants.TASK_RESULT_AGENTTMPFILE_SUFFIX;
|
||||
}
|
||||
|
||||
private static String getTaskResultFileNoSuffix(long taskType, long taskId){
|
||||
return getTaskResultPath() + File.separator + "tasktype" + taskType + "_" + taskId;
|
||||
}
|
||||
|
||||
public static String getTaskResultPath(){
|
||||
return Contants.localTaskResultPath;
|
||||
}
|
||||
}
|
||||
351
src/com/nis/nmsclient/thread/task/TaskReturnHandle.java
Normal file
351
src/com/nis/nmsclient/thread/task/TaskReturnHandle.java
Normal file
@@ -0,0 +1,351 @@
|
||||
package com.nis.nmsclient.thread.task;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Common;
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.model.ReturnFilePO;
|
||||
import com.nis.nmsclient.thread.socket.CommonSocket;
|
||||
import com.nis.nmsclient.util.CompressFileMgr;
|
||||
import com.nis.nmsclient.util.FileWrUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
|
||||
public class TaskReturnHandle {
|
||||
Logger logger = Logger.getLogger(TaskReturnHandle.class);
|
||||
|
||||
/*public void sendAllTaskReturnFile(){
|
||||
try {
|
||||
//针对回传文件过多时打包上传未完成的文件继续上传
|
||||
File taskDir = new File(Contants.localTaskPath);
|
||||
if (!taskDir.exists()) {
|
||||
return;
|
||||
}
|
||||
File[] zipArr = FileUtil.getFilesEndWith(taskDir, ".zip");
|
||||
if (zipArr != null && zipArr.length > 0) {
|
||||
for (File file : zipArr) {
|
||||
if (!file.getName().startsWith(CommonSocket.BP_TYPE_TASK_RETURN)) {
|
||||
continue;
|
||||
}
|
||||
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
|
||||
.getName(), CommonSocket.REQ_BP_UPLOAD_FIFE,
|
||||
new String[] { CommonSocket.BP_TYPE_TASK_RETURN,
|
||||
file.getAbsolutePath() }));
|
||||
future.get();
|
||||
}
|
||||
}
|
||||
//----------------------------------
|
||||
|
||||
File returnDir = new File(Contants.localTaskReturnPath);
|
||||
if(!returnDir.exists()){
|
||||
return;
|
||||
}
|
||||
File[] fileArr = FileUtil.getFilesEndWith(returnDir, Contants.TASK_RETURN_FILE_SUFFIX);
|
||||
if(fileArr == null || fileArr.length == 0){
|
||||
return;
|
||||
}
|
||||
//--- 将所有任务的回传文件及回传信息保存文件一起打包,发送
|
||||
if(fileArr.length > Contants.COMMON_MAX_RETURN_CNT){
|
||||
//与Server通信
|
||||
Future<?> serFuture = Common.service.submit(new SSLClient(
|
||||
Thread.currentThread().getName(),
|
||||
CommonSocket.REQ_HAND_SHAKE, null));
|
||||
if (!Contants.isSucessByResult((String) serFuture.get())) {
|
||||
return;
|
||||
}
|
||||
//压缩并删除原文件
|
||||
String compressFileStr = Contants.localTaskPath
|
||||
+ File.separator
|
||||
+ CommonSocket.addTimeTagForFileName(CommonSocket.BP_TYPE_TASK_RETURN,
|
||||
null)
|
||||
+ ".zip";
|
||||
ZipUtil.zipWithDelFile(returnDir.listFiles(), compressFileStr, false);
|
||||
//发送
|
||||
Future<?> future = Common.service.submit(new SSLClient(Thread.currentThread()
|
||||
.getName(), CommonSocket.REQ_BP_UPLOAD_FIFE,
|
||||
new String[] { CommonSocket.BP_TYPE_TASK_RETURN,
|
||||
compressFileStr }));
|
||||
future.get();
|
||||
}else{//-- 按正常的一个任务一个任务的回传
|
||||
fileArr = FileUtil.sortASCByModify(fileArr); //修改日期升序排序
|
||||
for(File file : fileArr){
|
||||
if(!file.exists() || !file.isFile()){
|
||||
continue;
|
||||
}
|
||||
String[] resultArr = FileWrUtil.cfgFileReader(file);
|
||||
if (resultArr != null && resultArr.length > 0) {
|
||||
JSONObject jsonObject = JSONObject.fromObject(resultArr[0]);
|
||||
ReturnFilePO rfPo = (ReturnFilePO) JSONObject.toBean(jsonObject, ReturnFilePO.class);
|
||||
sendTaskReturnFile(rfPo);
|
||||
}else{
|
||||
//file.delete_bak();
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(file);
|
||||
logger.debug("TaskReturnHadle.sendAllTaskReturnFile()()任务回传临时文件(记录任务结果信息)删除:"+file.getAbsolutePath());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
}*/
|
||||
|
||||
public static void startTaskReturnFileThread(final ReturnFilePO rfPo, final List<String> filePaths) {
|
||||
Common.service.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("Backpass File Processing ID:" + rfPo.getTaskId());
|
||||
TaskReturnHandle tReturnHandle = new TaskReturnHandle();
|
||||
ReturnFilePO rfPo2 = tReturnHandle.taskReturnFileHandle(rfPo, filePaths);
|
||||
//tReturnHandle.sendTaskReturnFile(rfPo2);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 回传文件的统一处理:将回传文件移动到统一回传位置,处理后一个任务只有一个回传文件,修改回传文件名,将任务回传信息写入文件
|
||||
*/
|
||||
public ReturnFilePO taskReturnFileHandle(ReturnFilePO rfPo, List<String> filePaths){
|
||||
if(rfPo==null || filePaths==null || filePaths.size()<=0){
|
||||
logger.warn("No return file, no return");
|
||||
return null;
|
||||
}
|
||||
File tempDir = null;
|
||||
try {
|
||||
File file = new File(getTaskReturnFile(rfPo.getTaskType(),
|
||||
rfPo.getTaskId(), rfPo.getIsLoop()));
|
||||
if (file.exists() && file.length()>0) {// 如果存在,说明之前已经整理过回传文件了,本次不做操作
|
||||
return null;
|
||||
}else if(!file.getParentFile().exists()){// 创建回传文件目录
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
tempDir = new File(Contants.localTempPath + File.separator
|
||||
+ "return_" + rfPo.getTaskId());
|
||||
if(!tempDir.exists()){
|
||||
tempDir.mkdirs();
|
||||
}
|
||||
/**
|
||||
* 处理回传文件:1、过滤不存在或文件路径为空的文件 2、对文件重命名移动到临时目录
|
||||
*/
|
||||
for(String returnPath: filePaths){
|
||||
if (StringUtils.isEmpty(returnPath)) {
|
||||
// sb.append("回传“" + returnPath + "”失败,回传文件路径值为空;");
|
||||
// sb.append("Backpass“" + returnPath + "” failed, the return file path value is empty;");
|
||||
sb.append("i18n_client.TaskReturnHandle.backpass_n81i“" + returnPath + "” i18n_client.TaskReturnHandle.backFail1_n81i;");
|
||||
continue;
|
||||
}
|
||||
File returnFile = new File(returnPath);
|
||||
if (!returnFile.exists()) {
|
||||
// sb.append("Backpass“" + returnPath + "” failed, return file does not exist;");
|
||||
sb.append("i18n_client.TaskReturnHandle.backpass_n81i“" + returnPath + "” i18n_client.TaskReturnHandle.backFail2_n81i;");
|
||||
continue;
|
||||
}
|
||||
//回传文件取别名:原文件名_T任务ID_时间戳
|
||||
String aliasName = CommonSocket.addTimeTagForFileName(
|
||||
returnFile.getName(), "T" + rfPo.getTaskId(), returnFile.isFile());
|
||||
//---- 移动回传文件到临时目录,并重命名为:原文件名_T任务ID_时间戳
|
||||
File returnTmpFile = new File(tempDir.getCanonicalPath()
|
||||
+ File.separator
|
||||
+ aliasName);
|
||||
if (returnFile.isDirectory()) {
|
||||
FileUtils.copyDirectory(returnFile, returnTmpFile);
|
||||
} else {
|
||||
FileUtils.copyFile(returnFile, returnTmpFile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将回传的所有文件压缩放到统一回传位置
|
||||
*/
|
||||
String returnFileName = null;
|
||||
//---- 若回传文件的个数等于1,则把临时目录下的一个文件:按文件夹的话压缩到统一回传位置,文件的话直接移动到统一回传位置
|
||||
File[] files = tempDir.listFiles();
|
||||
if(files.length==1){
|
||||
returnFileName = files[0].getName();
|
||||
File destFile = new File(Contants.localTaskReturnPath + File.separator + files[0].getName());
|
||||
if (files[0].isDirectory()) {
|
||||
String compressFileStr = destFile.getAbsolutePath() + CompressFileMgr.getCompressSuffixByOs(false);
|
||||
returnFileName = new File(compressFileStr).getName();
|
||||
new CompressFileMgr().compressFile(files[0]
|
||||
.getAbsolutePath(), compressFileStr, null, false);
|
||||
} else {
|
||||
FileUtils.copyFile(files[0], destFile, false);// 复制并修改文件日期
|
||||
}
|
||||
}else if(files.length > 1){//---- 若回传文件的个数大于1,则把临时目录整个压缩到统一回传位置
|
||||
String compressFileStr = Contants.localTaskReturnPath + File.separator + tempDir.getName() + CompressFileMgr.getCompressSuffixByOs(false);
|
||||
returnFileName = new File(compressFileStr).getName();
|
||||
new CompressFileMgr().compressFile(tempDir
|
||||
.getAbsolutePath(), compressFileStr, null, false);
|
||||
}
|
||||
|
||||
rfPo.setReturnFileName(returnFileName);
|
||||
rfPo.setResDesc(sb.toString());
|
||||
|
||||
/**
|
||||
* 将信息写入文件
|
||||
*/
|
||||
if (!file.exists()) {
|
||||
String[] values = new String[] { JSONObject.fromObject(
|
||||
rfPo).toString() };
|
||||
FileWrUtil.cfgFilePrinter(file, Contants.charset,
|
||||
values);
|
||||
}
|
||||
|
||||
return rfPo;
|
||||
} catch (Exception e) {
|
||||
logger.error("Handling backpass file exceptions:" + Utils.printExceptionStack(e));
|
||||
return null;
|
||||
} finally {
|
||||
if(tempDir!=null && tempDir.exists()){
|
||||
try {
|
||||
logger.debug("删除临时目录--" + tempDir.getAbsolutePath());
|
||||
FileUtils.deleteDirectory(tempDir);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 回传文件:先将信息写入临时文件,再判断与Server端通信是否成功
|
||||
* 1、通信成功:回传文件,发送最终结果;删除临时文件
|
||||
* 2、通信失败:保留临时文件,直接返回
|
||||
* @param rfPo 回传文件实体类
|
||||
*/
|
||||
/*public void sendTaskReturnFile(ReturnFilePO rfPo) {
|
||||
if(rfPo==null){
|
||||
logger.debug("回传文件实体对象为空, 不用回传");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
File file = new File(getTaskReturnFile(rfPo.getTaskType(),
|
||||
rfPo.getTaskId()));
|
||||
|
||||
//--回传文件名和回传描述信息均为空时,则无回传文件
|
||||
if(StringUtil.isEmpty(rfPo.getReturnFileName()) && StringUtil.isEmpty(rfPo.getResDesc())){
|
||||
logger.warn("无回传文件, 不用回传");
|
||||
if(file.exists()){
|
||||
//file.delete_bak();
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(file);
|
||||
logger.debug("TaskReturnHandle.sendTaskReturnFile()成功发送任务回传文件后删除:"+file.getAbsolutePath());
|
||||
}
|
||||
return;
|
||||
}
|
||||
//--回传文件名为空,但回传描述信息不为空,则进行步骤4发送任务结果、删除文件
|
||||
|
||||
*//**
|
||||
* 步骤1、将信息写入文件
|
||||
*//*
|
||||
if (!file.exists()) {
|
||||
String[] values = new String[] { JSONObject.fromObject(
|
||||
rfPo).toString() };
|
||||
FileWrUtil.cfgFilePrinter(file, Contants.charset,
|
||||
values);
|
||||
}
|
||||
*//**
|
||||
* 步骤2、与Server通信
|
||||
*//*
|
||||
Future<?> serFuture = Common.service.submit(new SSLClient(
|
||||
Thread.currentThread().getName(),
|
||||
CommonSocket.REQ_HAND_SHAKE, null));
|
||||
if (!Contants.isSucessByResult((String) serFuture.get())) {
|
||||
return;
|
||||
}
|
||||
*//**
|
||||
* 步骤3、回传文件
|
||||
*//*
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if(rfPo.getResDesc()!=null){//取已有的结果描述信息
|
||||
sb.append(rfPo.getResDesc());
|
||||
}
|
||||
//准备回传文件,回传文件名不为空即有回传的文件时,再回传
|
||||
boolean success = false;
|
||||
if(rfPo.getReturnFileName()!=null && rfPo.getReturnFileName().trim().length()>0){
|
||||
for(int i=0; i<Contants.max_times; i++){//回传失败,尝试几次
|
||||
Future<?> future = Common.service.submit(new SSLClient(
|
||||
Thread.currentThread().getName(),
|
||||
CommonSocket.REQ_TASK_RETURNFILE, rfPo));
|
||||
String msg = (String) future.get();
|
||||
success = Contants.isSucessByResult(msg);
|
||||
if(success){
|
||||
sb.append("回传成功;");
|
||||
break;
|
||||
}
|
||||
|
||||
try {// 如果更新失败,让当前线程暂停几秒,再重试
|
||||
Thread.sleep(1000 * Contants.max_delay_seconds);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
success = true;
|
||||
}
|
||||
|
||||
*//**
|
||||
* 步骤4、判断文件是否回传完成
|
||||
*//*
|
||||
if(success){
|
||||
*//**
|
||||
* 步骤4-1、发送任务结果
|
||||
*//*
|
||||
TaskResultOper.sendTaskResult(rfPo.getTaskId(), rfPo.getTaskType(),
|
||||
rfPo.getState(), sb.toString(), "", rfPo.getStartTime(),
|
||||
rfPo.getEndTime(), false, rfPo.getIsLoop());
|
||||
*//**
|
||||
* 步骤4-2、删除保存回传文件信息的文件
|
||||
*//*
|
||||
if(file.exists()){
|
||||
//file.delete_bak();
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(file);
|
||||
logger.debug("TaskReturnHandle.sendTaskReturnFile()删除保存回传文件信息的文件:"+file.getAbsolutePath());
|
||||
}
|
||||
*//**
|
||||
* 步骤4-3、正常回传完成,删除文件
|
||||
*//*
|
||||
File curReturnFile = new File(Contants.localTaskReturnPath + File.separator + rfPo.getReturnFileName());
|
||||
if(curReturnFile.exists()){
|
||||
//curReturnFile.delete_bak();
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(curReturnFile);
|
||||
logger.debug("TaskReturnHandle.sendTaskReturnFile()正常回传完成,删除文件:"+curReturnFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 取任务回传临时记录任务信息文件的文件名: 若是周期任务,加时间后缀taskreturn_taskId_taskType_time.return;若未周期任务, taskreturn_taskId_taskType.return
|
||||
*/
|
||||
public static String getTaskReturnFile(long taskType, long taskId, long isLoop) {
|
||||
//isLoop 是否周期任务: 0 非周期, 1 周期
|
||||
if(isLoop==0){
|
||||
return Contants.localTaskReturnPath + File.separator + "tasktype"
|
||||
+ taskType + "_" + taskId
|
||||
+ Contants.TASK_RETURN_FILE_SUFFIX;
|
||||
}else{
|
||||
return Contants.localTaskReturnPath + File.separator + "tasktype"
|
||||
+ taskType + "_" + taskId + "_"
|
||||
+ System.currentTimeMillis()
|
||||
+ Contants.TASK_RETURN_FILE_SUFFIX;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
197
src/com/nis/nmsclient/thread/timer/DelLocalFileThread.java
Normal file
197
src/com/nis/nmsclient/thread/timer/DelLocalFileThread.java
Normal file
@@ -0,0 +1,197 @@
|
||||
package com.nis.nmsclient.thread.timer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.nis.nmsclient.common.Contants;
|
||||
import com.nis.nmsclient.util.DateUtil;
|
||||
import com.nis.nmsclient.util.FileUtil;
|
||||
import com.nis.nmsclient.util.Utils;
|
||||
import com.sun.org.apache.bcel.internal.generic.NEW;
|
||||
|
||||
public class DelLocalFileThread implements Runnable {
|
||||
Logger logger = Logger.getLogger(DelLocalFileThread.class);
|
||||
private String name;
|
||||
|
||||
public DelLocalFileThread(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName(name);
|
||||
delLogFile();
|
||||
delDataFile();
|
||||
delTempFile();
|
||||
delUpgradeFile();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除日志文件
|
||||
*/
|
||||
public void delLogFile() {
|
||||
long curDate = System.currentTimeMillis();
|
||||
// 删除指定天数之前的日志文件
|
||||
File logs = new File(Contants.localLogsPath);
|
||||
if (!logs.exists() || !logs.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
for (File f : logs.listFiles()) {
|
||||
long diff = DateUtil.getDaysFromBeginToEnd(f.lastModified(),
|
||||
curDate);
|
||||
|
||||
if (f.isFile() && diff > Contants.COMMON_DEL_LOG_DAYS) {
|
||||
//f.delete_bak();
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(f);
|
||||
logger.info("删除日志文件" + f.getAbsolutePath());
|
||||
//FileUtil.checkParentDirExist(f);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理数据文件(包括监测数据和任务文件)
|
||||
*/
|
||||
public void delDataFile(){
|
||||
long curDateMillis = System.currentTimeMillis();
|
||||
// == 1、删除指定分钟之前的所有监测文件
|
||||
File doneFile = new File(Contants.localDataDonePath);
|
||||
if (doneFile.exists()) {
|
||||
//---删除打包上传的数据
|
||||
long timeMillis = curDateMillis - Contants.COMMON_DEL_DATA_HOURS
|
||||
* 60 * 60 * 1000;
|
||||
File[] files = FileUtil.getFilesEndWithBeforeMillis(doneFile, ".zip",
|
||||
timeMillis);
|
||||
if (files != null && files.length > 0) {
|
||||
for (File f : files) {
|
||||
FileUtil.delDir(f);
|
||||
logger.debug("delDataFile 删除打包文件--" + f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
//---删除正常上传的数据文件
|
||||
File[] doneDirs = FileUtil.getDirectoryArray(doneFile);
|
||||
delDataFileCallBack(curDateMillis, doneDirs, Contants.COMMON_DEL_DATA_HOURS);
|
||||
}
|
||||
File errorFile = new File(Contants.localDataErrorPath);
|
||||
if (errorFile.exists()) {
|
||||
File[] errorDirs = FileUtil.getDirectoryArray(errorFile);
|
||||
delDataFileCallBack(curDateMillis, errorDirs, Contants.COMMON_DEL_DATA_HOURS);
|
||||
}
|
||||
|
||||
// == 2、删除指定分钟之前的所有任务相关的文件
|
||||
doneFile = new File(Contants.localTaskDonePath);
|
||||
if (doneFile.exists()) {
|
||||
//---删除打包上传的任务结果和回传文件
|
||||
long timeMillis = curDateMillis - Contants.COMMON_DEL_TASK_HOURS
|
||||
* 60 * 60 * 1000;
|
||||
File[] files = FileUtil.getFilesEndWithBeforeMillis(doneFile, ".zip",
|
||||
timeMillis);
|
||||
if (files != null && files.length > 0) {
|
||||
for (File f : files) {
|
||||
FileUtil.delDir(f);
|
||||
logger.debug("delDataFile 删除打包文件2--" + f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
//---删除正常上传的任务结果和回传文件
|
||||
File[] doneDirs = FileUtil.getDirectoryArray(doneFile);
|
||||
delDataFileCallBack(curDateMillis, doneDirs, Contants.COMMON_DEL_TASK_HOURS);
|
||||
}
|
||||
errorFile = new File(Contants.localTaskErrorPath);
|
||||
if (errorFile.exists()) {
|
||||
File[] errorDirs = FileUtil.getDirectoryArray(errorFile);
|
||||
delDataFileCallBack(curDateMillis, errorDirs, Contants.COMMON_DEL_TASK_HOURS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理数据文件的递归回调函数
|
||||
*/
|
||||
public void delDataFileCallBack(long curDateMillis, File[] dirs, int delHours) {
|
||||
if (dirs == null || dirs.length == 0) {
|
||||
return;
|
||||
}
|
||||
for (File dir : dirs) {
|
||||
long timeMillis = curDateMillis - delHours
|
||||
* 60 * 60 * 1000;
|
||||
File[] sonDirs = FileUtil.getDirsBeforeDateName(dir, DateUtil
|
||||
.getStingDate(DateUtil.YYYYMMDD, new Date(timeMillis)));
|
||||
delDataFileCallBack(curDateMillis, sonDirs, delHours);
|
||||
|
||||
File[] files = FileUtil.getFilesBeforeMillis(dir,timeMillis);
|
||||
if (files != null && files.length > 0) {
|
||||
for (File f : files) {
|
||||
FileUtil.delDir(f);
|
||||
logger.debug("删除文件--" + f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
if (files == null && files.length > 0) {
|
||||
logger.info("删除文件夹" + dir.getAbsolutePath() + "下的文件:文件"
|
||||
+ files.length + "个");
|
||||
}
|
||||
|
||||
if (dir.exists() && dir.listFiles().length <= 0) {
|
||||
FileUtil.delDir(dir);
|
||||
logger.info("删除文件夹" + dir.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理临时目录的文件
|
||||
*/
|
||||
public void delTempFile() {
|
||||
try {
|
||||
long curDate = System.currentTimeMillis();
|
||||
// 删除指定日期之前的临时文件
|
||||
File temp = new File(Contants.localTempPath);
|
||||
if (!temp.exists() || !temp.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
for (File f : temp.listFiles()) {
|
||||
long diff = DateUtil.getDaysFromBeginToEnd(f.lastModified(),
|
||||
curDate);
|
||||
if (f.exists() && diff > Contants.COMMON_DEL_TEMP_DAYS) {
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(f);
|
||||
logger.info("删除临时文件或文件夹" + f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理UPLOAD目录下的文件
|
||||
*/
|
||||
public void delUpgradeFile() {
|
||||
try {
|
||||
long curDate = System.currentTimeMillis();
|
||||
// 删除指定日期之前的升级文件
|
||||
File uploads = new File(Contants.localUploadsPath);
|
||||
if (!uploads.exists() || !uploads.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
for (File f : uploads.listFiles()) {
|
||||
long diff = DateUtil.getDaysFromBeginToEnd(f.lastModified(),
|
||||
curDate);
|
||||
if (f.exists() && diff > Contants.COMMON_DEL_UPGRADEFILE_DAYS) {
|
||||
//使用删除文件公共方法
|
||||
FileUtil.delDir(f);
|
||||
logger.info("删除升级文件或文件夹" + f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(Utils.printExceptionStack(e));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
41
src/com/nis/nmsclient/thread/upload/BusinessType.java
Normal file
41
src/com/nis/nmsclient/thread/upload/BusinessType.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.nis.nmsclient.thread.upload;
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
* @author nanfang
|
||||
*
|
||||
*/
|
||||
public enum BusinessType {
|
||||
|
||||
HeartBeat("HeartBeat",0),//心跳
|
||||
DetectZip("DetectZip",1), // 监测数据zip
|
||||
DetectData("DetectData",2),// 监测数据
|
||||
ObjTaskResult("ObjTaskResult",3),//任务结果
|
||||
FileTaskReturn("FileTaskReturn",4);//回传文件
|
||||
|
||||
|
||||
private final String name;
|
||||
private final int type;
|
||||
|
||||
private BusinessType(String name ,int type) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public static BusinessType getType(int type) {
|
||||
for (BusinessType t : BusinessType.values()) {
|
||||
if (t.getType() == type) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user