#!/usr/bin/env bash
# 获取服务目录
xxx_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
# 端口检测间隔
w_interval=3
# 启动后端口检测次数
max_retried_times=50
REDIS_INSTALL_DIR=/bin/
REDIS_CONFIG_FILE=/etc/redis/redis.conf
MONGO_INSTALL_DIR=/usr/
MONGO_CONFIG_FILE=/etc/mongodb.conf
NGINX_INSTALL_DIR=/usr/sbin/
NGINX_CONFIG_DIR=/etc/nginx/nginx.conf
CONSUL_INSTALL_DIR=/opt/consul
HYBOX=/opt/xxx/hybox/bin
ES=/opt/elasticsearch/bin
declare -A monitor_third_service
monitor_third_service=(
[redis]=6379 # 数据结构服务器
[mongo]=27017 # mongo数据库
[mysql]=3306 # mysql数据库
[web]=80 # 平台前端
[consul]=8500 # 分析控制服务
[hybox]=29200 # yyy
[elasticsearch]=9200 # es
#[mysql_]=3307 # yyymysql
[solr]=8983 # solr
#[soffice]=8100
)
declare -a control_service
control_service=(
lts
jobtracker
yarn_tasktracker
comparison
tldw
graph
analyzer
case_management
chart_management
#tactic
data_gov
gateway
#dictionary
)
# 控制服务的列表
declare -A service_install_dir
service_install_dir=(
[lts]=$xxx_dir/lts-admin
[jobtracker]=$xxx_dir/jobtracker
[yarn_tasktracker]=$xxx_dir/yarn-tasktracker
[comparison]=$xxx_dir/comparison
[tldw]=$xxx_dir/service
[analyzer]=$xxx_dir/analyzer
[case_management]=$xxx_dir/case-management
[tactic]=$xxx_dir/tactic
[graph]=$xxx_dir/graphdb
[chart_management]=$xxx_dir/chart-management
[data_gov]=$xxx_dir/dg_back
[gateway]=$xxx_dir/gateway
[dictionary]=$xxx_dir/dictionary
)
declare -A service_control_script
service_control_script=(
[lts]=./bin/lts-admin.sh
[jobtracker]=./bin/tldw-jobtracker.sh
[yarn_tasktracker]=./bin/tldw-yarn-tasktracker.sh
[comparison]=./bin/tldw-comparison.sh
[tldw]=./bin/tldw-platform-server.sh
[analyzer]=./bin/tldw-pc.sh
[case_management]=./bin/tldw-casemanager.sh
[tactic]=./bin/tactic-server.sh
[graph]=./bin/tldw-graph-server.sh
[chart_management]=./bin/tldw-chartmanager.sh
[data_gov]=./bin/tldw-data-gov.sh
[gateway]=./bin/tldw-gateway.sh
[dictionary]=./bin/dictionary.sh
)
#服务监控列表,格式为[服务]=端口
declare -A service_port
service_port=(
[lts]=8720 # 分布式任务调度框架,管理服务
[jobtracker]=8721 # 分布式任务调度框架,作业管理
[comparison]=8731 # 比对服务
[yarn_tasktracker]=17030 # 分布式任务调度框架,任务执行
[graph]=8999 # 图服务
[tldw]=8080 # 平台后台
[analyzer]=8051 # 分析服务
[case_management]=8060 # 案件管理
[tactic]=8099 # 战法服务
[chart_management]=8810 # 图表管理
[data_gov]=18080 # 案件数据治理
[gateway]=8070
[dictionary]=8820 # 查询工具
)
# 输出格式化
my_echo()
{
local RED="\033[31m"
local GREEN="\033[32m"
local YELLOW="\033[33m"
local END="\033[0m"
if [ $# -eq 1 ];then
echo -e "${GREEN}$1${END}"
elif [ $# -eq 2 ];then
if [[ ! $1 =~ ^(1|2)$ ]];then
echo -e "${RED}$1 is not a insufficent argument, the argument must be 1 or 2, other argument must not permitted.$END"
return 1
fi
case $1 in
1)
echo -e "${YELLOW}${2}${END}"
;;
2)
echo -e "${RED}${2}${END}"
;;
*)
echo -e "${RED}Insufficent argument${END}"
;;
esac
else
echo -e "${RED}Input arguments are not 2, please make sure of it${END}"
return 1
fi
}
#帮助信息输出
help()
{
echo ""
echo "Usage:"
echo "1 Check service status"
my_echo "./start_all.sh status"
echo "2 Control tldw service"
my_echo "./start_all.sh service <operation> <service> "
echo "operation variable is one of start|stop|restart"
echo "service variable is service in list ( ${control_service[@]} ),you can edit zhe control_service array to decision which service has been controlled"
my_echo "Example:./start_all.sh service restart graph restart graphdb service"
my_echo "Otherwise:You can use ./start_all.sh service start|stop|restart all to control all service"
echo "3 Control thirdpart service"
my_echo "./start_all.sh start_mongo|start_redis|start_nginx|start_mysql|start_consul "
echo "You can use command upward to start thirdpart service"
my_echo "./start_all.sh stop_mongo|stop_redis|stop_nginx|stop_mysql|stop_consul "
echo "You can use command upward to stop thirdpart service"
echo "You can also use command './start_all.sh start_thirdpart|stop_thirdpart' to control all thirdpart service"
echo ""
echo "使用方法:"
echo "1 检查服务状态"
my_echo "./start_all.sh status"
echo "2 天网服务控制"
my_echo "./start_all.sh service <operation> <service>"
echo "operation 变量填写start|stop|restart"
echo "service 变量填写列表(${control_service[@]} )中的服务名 ,你可以通过修改脚本中的control_service数组来觉得控制哪些服务"
my_echo "例如:./start_all.sh service restart graph 重启图库服务"
my_echo "此外:你可以使用./start_all.sh service start|stop|restart all 来控制所有服务"
echo "3 第三方服务控制"
my_echo "./start_all.sh start_mongo|start_redis|start_nginx|start_mysql|start_consul "
echo "你可以使用上面的命令启动第三方服务"
my_echo "./start_all.sh stop_mongo|stop_redis|stop_nginx|stop_mysql|stop_consul "
echo "你可以使用上面的命令停止第三方服务"
echo "你也可以使用'./start_all.sh start_thirdpart|stop_thirdpart' 命令同时控制多个第三方服务"
}
exception()
{
my_echo 2 "Error Function!"
help
exit 1
}
# 检测服务状态并返回状态码
service_exists()
{
service_name=$1
port=$2
pid=$(netstat -nptl | awk '{print $0, $4}' \
| grep -E :"$port"$ | awk '{print $7}' | awk -F "/" '{print $1}')
if [ "$pid" != "" ];then
my_echo "[OK]$(hostname) - $service_name (port: $port) started ok, pid is $pid"
return 0
fi
my_echo 1 "[STARTING]$(hostname) - $service_name (port: $port) ... ...\r\c"
return 2
}
#检测服务状态
check_service()
{
service_name=$1
port=$2
pid=$(netstat -nptl | awk '{print $0, $4}' \
| grep -E :"$port"$ | awk '{print $7}' | awk -F "/" '{print $1}')
if [ "$pid" != "" ];then
printf "\033[32m%-8s %-20s %-10s %-10s\n\033[0m" "[OK]" "$service_name" "$port" "$pid"
else
printf "\033[31m%-8s %-20s %-10s %-10s\n\033[0m" "[NOT OK]" "$service_name" "$port" "not exist"
fi
}
# 检查所有服务状态
check_services()
{
echo "==================thirdpart service=================="
printf "%-8s %-20s %-10s %-10s\n" "Status" "Service Name" "Port" "PID"
for s_ in $(echo ${!monitor_third_service[*]});do
check_service ${s_} ${monitor_third_service[${s_}]}
done
echo "====================tldw service====================="
printf "%-8s %-20s %-10s %-10s\n" "Status" "Service Name" "Port" "PID"
for s_ in $(echo ${control_service[*]});do
check_service ${s_} ${service_port[${s_}]}
done
}
# 多次检测服务状态,直到确认服务启动
check_long_time_service()
{
local count=0
s_name=$1
s_port=$2
if [ "x$(service_exists ${s_name} ${s_port} > /dev/null 2>&1;echo $?)" == "x0" ];then
if service_exists ${s_name} ${s_port}
then
return 0
fi
fi
echo ""
while [ "x$count" != "x${max_retried_times}" ]
do
sleep 5
count=$((count + 1))
my_echo "retried times $count/$max_retried_times \c"
if service_exists ${s_name} ${s_port}
then
return 0
fi
done
# when failed
my_echo ""
my_echo 2 "[STARTED FAILED] $s_name $s_port."
return 1
}
# 获取所有服务pid
get_all_services_pids()
{
pids=""
for s_ in ${!service_port[*]};do
port=${service_port[${s_}]}
pid=$(netstat -nptl | awk '{print $0, $4}' \
| grep -E :"$port"$ | awk '{print $7}' | awk -F "/" '{print $1}')
pids=${pids}" "${pid}
done
echo "[INFO]all active pids are "$pids
}
# 获取所有服务状态
status()
{
check_services
}
service(){
local operation=$1
local service=$2
if [[ ! ${control_service[@]} =~ $service && $service != "all" ]];then
my_echo 2 "$service is not valid service"
return 1
fi
if [ ! -d ${service_install_dir[$service]} ]; then
my_echo 2 "$service dir ${service_install_dir[$service]} not exist,please check!"
return 1
fi
if [[ ! $operation =~ ^(start|stop|restart)$ ]];then
my_echo 2 "$operation is not valid operation"
return 1
fi
if [[ $service == "all" ]];then
for service in $(echo ${control_service[*]})
do
service $operation $service
done
else
cd ${service_install_dir[$service]} && ${service_control_script[$service]} $operation
if [[ $operation =~ ^(start|restart)$ ]];then
check_long_time_service $service ${service_port[$service]}
fi
fi
}
##***修改配置文件模块***
edit_properties(){
config_file=$1
config_key=$2
config_new_value=$3
if [ ! -f $config_file ];then
echo "can not find file $config_file"
return 1
fi
grep -i $config_key $config_file >/dev/null
if [ $? -ne 0 ] ;then
echo "can not find propertied $config_key in file $config_file"
return 1
fi
sed -i "s/^$config_key.*/$config_key=$config_new_value/g" $config_file
echo "change $config_key in $config_file to $config_new_value"
return 0
}
# ***服务依赖组件控制模块***
# Start redis server
start_redis()
{
if service_exists redis ${monitor_third_service[redis]}
then
return 0
fi
$REDIS_INSTALL_DIR/redis-server $REDIS_CONFIG_FILE
check_long_time_service redis ${monitor_third_service[redis]}
}
stop_redis()
{
ps aux|grep redis-server|grep -v grep|awk '{print $2}'|xargs -i kill {}
}
# Start mongodb
start_mongo()
{
if service_exists mongo ${monitor_third_service[mongo]}
then
return 0
fi
$MONGO_INSTALL_DIR/bin/mongod -config $MONGO_CONFIG_FILE
check_long_time_service mongo ${monitor_third_service[mongo]}
}
stop_mongo()
{
$MONGO_INSTALL_DIR/bin/mongod -f $MONGO_CONFIG_FILE --shutdown
}
# mysql control
start_mysql()
{
systemctl start mysqld
}
stop_mysql()
{
systemctl stop mysqld
}
# Start tldw web server
start_web()
{
if service_exists web ${monitor_third_service[web]}
then
return 0
fi
$NGINX_INSTALL_DIR/nginx -c $NGINX_CONFIG_DIR
check_long_time_service web ${monitor_third_service[web]}
}
stop_web()
{
$NGINX_INSTALL_DIR/nginx -s stop
}
start_consul()
{
if service_exists consul ${monitor_third_service[consul]}
then
return 0
fi
cd $CONSUL_INSTALL_DIR&& nohup ./consul agent -dev -client 0.0.0.0 >/dev/null &
sleep 3
check_long_time_service consul ${monitor_third_service[consul]}
}
stop_consul()
{
$CONSUL_INSTALL_DIR/consul leave
}
start_hybox()
{
(cd $HYBOX;./proc_manager.sh start)
}
stop_hybox()
{
(cd $HYBOX;./proc_manager.sh stop)
}
start_hybox_mysql()
{
systemctl start mysqld
sleep 1
}
stop_hybox_mysql()
{
systemctl stop mysqld
sleep 1
}
start_es()
{
#(su elastic;$ES/elasticsearch -d;exit)
su es -c "$ES/elasticsearch -d"
}
stop_es()
{
kill -9 $(ps -ef |grep "elasticsearch"|grep -v grep| awk '{print $2}')
}
start_solr()
{
systemctl start solr
check_long_time_service solr ${monitor_third_service[solr]}
}
stop_solr()
{
systemctl stop solr
}
start_soffice()
{
cd /opt/openoffice4/program && ./soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
sleep 3
check_long_time_service soffice ${monitor_third_service[soffice]}
}
stop_soffice()
{
ps -ef |grep "[^]]soffice"| awk '{print $2}'|xargs kill -9
}
start_thirdpart()
{
start_redis
start_mongo
start_mysql
start_web
start_consul
#start_hybox_mysql
start_hybox
start_es
start_solr
#start_soffice
}
stop_thirdpart()
{
stop_redis
stop_mongo
stop_mysql
stop_web
stop_consul
#stop_hybox_mysql
stop_hybox
stop_es
stop_solr
#stop_soffice
}
if [ $# -eq 0 ];then
help
exit 1
fi
command -v $1 2>&1 1>/dev/null || exception
##脚本主方法,通过eval动态调用脚本中的函数
eval $* 2>/dev/null