项目总体调度脚本

#!/bin/bash
#判断web配置的数据库信息是否可用
function MYSQL_CONNECT {
   WEB_INIT=/home/orca/orca_web/webapps/ROOT/WEB-INF/classes/
   cd $WEB_INIT
   sed -i 's/\r//' init.properties
   WEB_DB=`cat init.properties | grep '^jdbc.url' | awk -F '/' '{print $4}' |awk -F '?' '{print $1}'`
   WEB_IP=`cat init.properties | grep '^jdbc.url' |awk -F '//' '{print $2}' |awk -F ':' '{print $1}'`
   WEB_PORT=`cat init.properties | grep '^jdbc.url' |awk -F '/' '{print $3}' |awk -F ':' '{print $2}'`
   WEB_NAME=`cat init.properties |grep '^jdbc.user' |awk -F '=' '{print $2}'`
   WEB_PASS=`cat init.properties |grep '^jdbc.password' |awk -F '=' '{print $2}'` 
   DB_DATE=$(mysql -h$WEB_IP -u$WEB_NAME -p$WEB_PASS -e "show databases;" 2>/dev/null)
   for i in $DB_DATE
   do
     if [ "$i" != "Database" ] && [ "$i" == "$WEB_DB" ]
     then
       echo "ok"
     fi
   done
}
#判断数据分析配置的数据库信息是否可用
function DATA_MYSQL_CONNECT {
   DATA_INIT=/home/orca/orca_collector/da/conf/
   cd $DATA_INIT
   sed -i 's/\r//' init.properties
   MYSQL_DB=`cat init.properties | grep '^jdbc.url' | awk -F '/' '{print $4}' |awk -F '?' '{print $1}'`
   MYSQL_IP=`cat init.properties | grep '^jdbc.url' |awk -F '//' '{print $2}' |awk -F ':' '{print $1}'`
   MYSQL_PORT=`cat init.properties | grep '^jdbc.url' |awk -F '/' '{print $3}' |awk -F ':' '{print $2}'`
   MYSQL_NAME=`cat init.properties |grep '^jdbc.user' |awk -F '=' '{print $2}'`
   MYSQL_PASS=`cat init.properties |grep '^jdbc.password' |awk -F '=' '{print $2}'` 
   DB_DATE=$(mysql -h$MYSQL_IP -u$MYSQL_NAME -p$MYSQL_PASS -e "show databases;" 2>/dev/null)
   for i in $DB_DATE
   do
     if [ "$i" != "Database" ] && [ "$i" == "$MYSQL_DB" ]
     then
       echo "ok"
     fi
   done 
}
#校验WEB配置文件中IP地址是否可用
function VERIFY_WEB_IP {
   MYSQL_CONNECT >/dev/null
   HOST_IP=`hostname -i`
   LOCALHOST=127.0.0.1
   if [ "$WEB_IP" == "$HOST_IP" ] || [ "$WEB_IP" == "$LOCALHOST" ]
   then
     echo "OK"
   fi
}
#校验DATE配置文件中IP地址是否可用
function VERIFY_DB_IP {
   DATA_MYSQL_CONNECT >/dev/null
   HOST_IP=`hostname -i`
   LOCALHOST=127.0.0.1
   if [ "$MYSQL_IP" == "$HOST_IP" ] || [ "$WEB_IP" == "$LOCALHOST" ]
   then
     echo "OK"
   fi
}
#提取所有配置文件中的IP地址
function EXTRACT_OTHER_IP {
    DA_CONF=/home/orca/orca_collector/da/conf
    cd $DA_CONF
    sed -i 's/\r//' init.properties
    ALARM_WEBSOCKET_IP=`cat init.properties |grep '^alarm.websocket.ip' |awk -F = '{print $2}'`
    echo $ALARM_WEBSOCKET_IP
    COLLECTMSG_WEBSOCKET_IP=`cat init.properties |grep '^collectMsg.webSocket.ip' |awk -F = '{print $2}'`
    echo $COLLECTMSG_WEBSOCKET_IP
    HELPME_RMISERVICE_IP=`cat init.properties |grep '^helpMe.RMIService.ip' |awk -F = '{print $2}'`
    echo $HELPME_RMISERVICE_IP
    COLLECTDATA_SAVE_RMISERVER_IP=`cat init.properties |grep '^collectData.save.RMIServer.ip' |awk -F = '{print $2}'`
    echo $COLLECTDATA_SAVE_RMISERVER_IP
    sed -i 's/\r//' jms.properties
    JMSIPPORT=`cat jms.properties |grep '^jmsIpPort' |awk -F // '{print $2}' |awk -F '\' '{print $1}'`
    echo $JMSIPPORT
    QUEUENAME=`cat jms.properties |grep '^queueName' |awk -F = '{print $2}' |awk -F '.watch6' '{print $1}'`
    echo $QUEUENAME
    SAVE_CFG_RMISERVER_IP=`cat jms.properties |grep '^save.cfg.rmiserver.ip' |awk -F = '{print $2}'`
    echo $SAVE_CFG_RMISERVER_IP


    DC_CONF=/home/orca/orca_collector/dc/conf
    cd $DC_CONF
    sed -i 's/\r//' init.properties
    JMSIP=`cat init.properties |grep '^jmsip' |awk -F // '{print $2}' |awk -F : '{print $1}'`
    echo $JMSIP
    LOCALHOSTIP=`cat init.properties |grep '^localhostip' |awk -F = '{print $2}'`
    echo $LOCALHOSTIP
    JMSCONIP=`cat init.properties |grep '^jmsconip' |awk -F = '{print $2}'`
    echo $JMSCONIP
    SCMDB_WEB_SERVER_IP=`cat init.properties |grep '^scmdb_web_server_ip' |awk -F = '{print $2}'`
    echo $SCMDB_WEB_SERVER_IP
    DERBY_IP=`cat init.properties |grep '^derby_ip' |awk -F = '{print $2}'`
    echo $DERBY_IP


    WEB_CONF=/home/orca/orca_web/webapps/ROOT/WEB-INF/classes
    cd $WEB_CONF
    sed -i 's/\r//' init.properties
    SERVERIP=`cat init.properties |grep '^serverIp' |awk -F = '{print $2}'`
    echo $SERVERIP
    SOCKETIP=`cat init.properties |grep '^socketIp' |awk -F = '{print $2}'`
    echo $SOCKETIP
    MSGSOCKETIP=`cat init.properties |grep '^msgSocketIp' |awk -F = '{print $2}'`
    echo $MSGSOCKETIP
    ANALYSISSERVERIP=`cat init.properties |grep '^analysisServerIp' |awk -F = '{print $2}'`
    echo $ANALYSISSERVERIP
    FLOWCOLLECTRMIIP=`cat init.properties |grep '^flowCollectRmiIp' |awk -F = '{print $2}'`
    echo $FLOWCOLLECTRMIIP
}
#验证录入的配置文件是否准确
function VERIFY_OTHER_IP {
    OTHER_CONF_IP=$(EXTRACT_OTHER_IP)
    HOST_IP=`hostname -i`
    LOCALHOST=127.0.0.1
    for i in $OTHER_CONF_IP
    do
      if [ "$i" == "$HOST_IP" ] || [ "$i" == "$LOCALHOST" ]
      then
         echo "ok" 
      else
         echo "no"
      fi
    done  
}
#所有配置文件IP结果
function VERIFY_CONF {
    CONF_RESULT=$(VERIFY_OTHER_IP)
    CONF_VERIFY_RESULT=`echo ${CONF_RESULT[*]} |wc -l`
    if [ "$CONF_VERIFY_RESULT" -eq "1" ]
    then
      echo "ok"
    else
      echo "no"
    fi
}
#校验整体配置文件是否正确
function VERIFY_WEB {
   VERIFY_DB=$(DATA_MYSQL_CONNECT)
   VERIFY_WEB=$(MYSQL_CONNECT)
   VERIFY_DB_IP=$(VERIFY_DB_IP)
   VERIFY_WEB_IP=$(VERIFY_WEB_IP)
   VERIFY_OTHER_CONF_IP=$(VERIFY_CONF)
   if [ "$VERIFY_WEB" == "$VERIFY_DB" ] && [ "$VERIFY_DB_IP" == "$VERIFY_WEB_IP" ] && [ "$VERIFY_OTHER_CONF_IP" == "ok" ]
   then
     echo "ok"
   else 
     echo "no"   
   fi


   
}
#MQ启动命令
function MQ_START {
  if [ -z "$MQ_START_PID" ]
  then
    nohup java -jar $MQ_JAR start &
  else
    echo "MQ ALREADY START...."
  fi
}
#MQ停止命令
function MQ_STOP {
  if [ -n "$MQ_START_PID" ]
  then
    kill -9 $MQ_START_PID
  else
    echo "MQ ALREADY STOP...."
  fi  
}
#MQ重启命令
function MQ_RESTART {
  if [ -n "$MQ_START_PID" ]
  then
    kill -9 $MQ_START_PID
    nohup java -jar $MQ_JAR start &
  else
    echo "MQ ALREADY STOP....."
    nohup java -jar $MQ_JAR start &
  fi
}
#DERBY启动命令
function DERBY_START {
  if [ -z "$DERBY_START_PID" ]
  then
    cd $DERBY_DIR
    nohup ./startNetworkServer -h $HOST_IP &
  else
    echo "DERBY ALREADY START...."
  fi
}
#DERBY停止命令
function DERBY_STOP {
  if [ -n "$DERBY_START_PID" ]
  then
    kill -9 $DERBY_START_PID
  else
    echo "DERBY ALREADY STOP....."
  fi  
}
#DERBY重启命令
function DERBY_RESTART {
  if [ -n "$DERBY_START_PID" ]
  then
    kill -9 $DERBY_START_PID
    cd $DERBY_DIR
    nohup ./startNetworkServer -h $HOST_IP &
  else
    echo "DERBY ALREADY STOP....."
    cd $DERBY_DIR
    nohup ./startNetworkServer -h $HOST_IP &
  fi
}
#DATA启动命令
function DATA_START {
  cd $DATA_DIR
  DATA_JAR=`ls -t | grep 'Data_analysis' | awk "NR==1"`
  if [ -z "$DATA_START_PID" ]
  then
    nohup java -jar $DATA_JAR -h$HOST_IP start &
  else
    echo "DATA ALREADY START...."
  fi

#DATA停止命令
function DATA_STOP {
  if [ -n "$DATA_START_PID" ]
  then
    kill -9 $DATA_START_PID
  else
    echo "DATA ALREADY STOP...."
  fi  
}
#DATA重启命令
function DATA_RESTART {
  cd $DATA_DIR
  DATA_JAR=`ls -t | grep 'Data_analysis' | awk "NR==1"`
  if [ -n "$DATA_START_PID" ]
  then
    kill -9 $DATA_START_PID
    nohup java -jar $DATA_JAR -h$HOST_IP start &
  else
    echo "DATA ALREADY STOP...."
    nohup java -jar $DATA_JAR -h$HOST_IP start &
  fi
}
#COLLECT启动命令
function COLLECT_START {
  cd $COLLECT_DIR
  COLLECT_JAR=`ls -t | grep 'ORCA_COLLECTOR' | awk "NR==1"`
  if [ -z "$COLLECT_START_PID" ]
  then
    nohup java -jar $COLLECT_JAR -h$HOST_IP start &
  else
    echo "COLLECT ALREADY START...."
  fi

#COLLECT停止命令
function COLLECT_STOP {
  if [ -n "$COLLECT_START_PID" ]
  then
    kill -9 $COLLECT_START_PID
  else
    echo "COLLECT ALREADY STOP...."
  fi  
}
#COLLECT重启命令
function COLLECT_RESTART {
  cd $COLLECT_DIR
  COLLECT_JAR=`ls -t | grep 'ORCA_COLLECTOR' | awk "NR==1"`
  if [ -n "$COLLECT_START_PID" ]
  then
    kill -9 $COLLECT_START_PID
    nohup java -jar $COLLECT_JAR -h$HOST_IP start & 
  else
    echo "COLLECT ALREADY STOP...."
    nohup java -jar $COLLECT_JAR -h$HOST_IP start & 
  fi
}
#WEB启动命令
function WEB_START {
    cd $WEB_DIR
    if [ -z "$WEB_START_PID" ]
then
  rm -rf logs/*
  rm -rf work/*
  rm -rf temp/*
  cd bin/
  sh ./startup.sh >/dev/null
else
  echo "WEB ALREADY START...."
fi

#WEB停止命令
function WEB_STOP {
    cd $WEB_DIR
if [ -n "$WEB_START_PID" ]
then
  kill -9 $WEB_START_PID
else
  echo "WEB ALREADY STOP....."
fi
}
#WEB重启命令
function WEB_RESTART {
    cd $WEB_DIR
if [ -n "$WEB_START_PID" ]
then
   kill -9 $WEB_START_PID
       rm -rf logs/*
   rm -rf work/*
   rm -rf temp/*
   cd bin/
   sh ./startup.sh >/dev/null
else
  echo "WEB ALREADY STOP......"
  rm -rf logs/*
  rm -rf work/*
  rm -rf temp/*
  cd bin/
  sh ./startup.sh >/dev/null
fi  
}
#MQ调度方法
function HANDLE_MQ {
   if [ "$2" == "start" ]
   then
     MQ_START
   elif [ "$2" == "stop" ]
   then
     MQ_STOP
   elif [ "$2" == "restart" ]
   then
     MQ_RESTART
   else
     echo "please input start / stop / restart"
   fi
}
#DERBY调度方法
function HANDLE_DERBY {
   if [ "$2" == "start" ]
   then
     DERBY_START
   elif [ "$2" == "stop" ]
   then
     DERBY_STOP
   elif [ "$2" == "restart" ]
   then
     DERBY_RESTART
   else
     echo "please input start / stop / restart"
   fi
}
#DATA调度方法
function HANDLE_DATA {
   if [ "$2" == "start" ]
   then
     DATA_START
   elif [ "$2" == "stop" ]
   then
     DATA_STOP
   elif [ "$2" == "restart" ]
   then
     DATA_RESTART
   else
     echo "please input start / stop / restart"
   fi
}
#COLLECT调度方法
function HANDLE_COLLECT {
   if [ "$2" == "start" ]
   then
     COLLECT_START
   elif [ "$2" == "stop" ]
   then
     COLLECT_STOP
   elif [ "$2" == "restart" ]
   then
     COLLECT_RESTART
   else
     echo "please input start / stop / restart"
   fi
}
#WEB调度方法
function HANDLE_VERIFY_WEB {
   if [ "$2" == "start" ]
   then
     WEB_START
   elif [ "$2" == "stop" ]
   then
     WEB_STOP
   elif [ "$2" == "restart" ]
   then
     WEB_RESTART
   else
     echo "please input start / stop / restart"
   fi
}
#WEB验证后调度方法
function HANDLE_WEB {
    CONF_VERIFY_WEB=$(VERIFY_WEB)
if [ "$CONF_VERIFY_WEB" == "ok" ]
then
  HANDLE_VERIFY_WEB "$@"
else
  echo "CONFIG FILE ERROR..."
fi
}
#总体启动调度方法
function ALL_START {
   CONF_VERIFY_WEB=$(VERIFY_WEB)
   MQ_START
   DERBY_START
   COLLECT_START
   if [ "$CONF_VERIFY_WEB" == "ok" ]
   then
     WEB_START
   else
     echo "CONFIG FILE ERROR...."
   fi  
   DATA_START
}
#总体停止调度方法
function ALL_STOP {
   CONF_VERIFY_WEB=$(VERIFY_WEB)
   MQ_STOP
   DERBY_STOP
   COLLECT_STOP
   if [ "$CONF_VERIFY_WEB" == "ok" ]
   then
     WEB_STOP
   else
     echo "CONFIG FILE ERROR...."
   fi  
   DATA_STOP
}
#总体重启调度方法
function ALL_RESTART {
   CONF_VERIFY_WEB=$(VERIFY_WEB)
   MQ_RESTART
   DERBY_RESTART
   COLLECT_RESTART
   if [ "$CONF_VERIFY_WEB" == "ok" ]
   then
      WEB_RESTART
   else
      echo "CONFIG FILE ERROR...."
   fi   
   DATA_RESTART
}
#总体调度方法
function HANDLE_ALL {
   if [ "$2" == "start" ]
   then
     ALL_START
   elif [ "$2" == "stop" ]
   then
     ALL_STOP
   elif [ "$2" == "restart" ]
   then
     ALL_RESTART
   else
     echo "please input start / stop / restart"
   fi
}
#实现逻辑
if [ "$1" == "-M" ]
then
  HANDLE_MQ "$@"
elif [ "$1" == "-D" ]
then  
  HANDLE_DERBY "$@"
elif [ "$1" == "-A" ]
then 
  HANDLE_DATA "$@"
elif [ "$1" == "-C" ]
then
  HANDLE_COLLECT "$@"
elif [ "$1" == "-O" ]
then 
  HANDLE_ALL "$@"
elif [ "$1" == "-W" ]
then
  HANDLE_WEB "$@"
else
  echo "please input -(M/D/A/C/O/W)  start/stop/restart"
  echo "-M   apache-activemq "
  echo "-D   db-derby"
  echo "-A   Data_analysis"
  echo "-C   ORCA_COLLECTOR"
  echo "-W   WEB"
  echo "-O   ORCA_COLLECTOR:db-derby:Data_analysis:apache-activemq:web together " 
fi

你可能感兴趣的:(项目总体调度脚本)