#!/bin/bash

echo "=========================="
echo "====  check service  ====="
echo "=========================="


servicetatus="/tmp/servicetatus.sh"


function check(){
	
	_type=$1
	_name=$2
	_port=$3
	_desc=$4
	_bleo=$5
	_targ=$6
	
	

	#echo "$result" "$key"
	#verify "$result" "$key"
    #isstart=$?
	
	_name_c=''
	_status=''
	_desc_c=''
	#echo $_name "curl -s ${host}:${_port}"
	result=`curl -m 2 -s ${host}:${_port}`
	
	if [[ $? == 0 ]]
    then
    	_status='\033[32m start \033[0m'
    	
	else
		_status='\033[31m stop  \033[0m'
	fi

	if [[ ${#_name} -lt 8 ]]
	then
		_name_c="${_name}\t\t\t"
	fi

	if [[ ${#_name} -ge 8 && ${#_name} -lt 16 ]]
	then
		_name_c="${_name}\t\t"
	fi

	if [[ ${#_name} -ge 16 ]]
	then
		_name_c="${_name}\t"
	fi

	if [[ $_bleo =~ "${_targ}"  ]]
    then
    	_desc_c="\033[32m ${_desc} \033[0m"
    else
    	_desc_c=" ${_desc} "
	
	fi




	echo "echo -e '${_status}\t|\t${_type}\t|\t${_name_c}\t${_desc_c}' "  >>$servicetatus
	echo "echo '----------------------------------------------------------------------------------------------'" >>$servicetatus
}



function info(){
    process=`ps -ef |grep $1|grep -v grep |wc -l`
    if [ $process -eq 0 ];then
        _status='\033[31m stop  \033[0m'
    else
        _status='\033[32m start \033[0m'
    fi
    echo "echo -e '${_status}\t\t\t|\t$1'   "  >>$servicetatus
}

rm -rf $servicetatus

echo "#!/bin/bash" >>$servicetatus
echo "echo '----------------------------------------------------------------------------------------------'" >>$servicetatus

host="127.0.0.1"

projectStr=`node /home/qa-deploy-utils/qa_shell_script/check/project_item.js`

for line in ${projectStr[@]}; do

    #echo "check ${line//|@|/ } $1"
    check ${line//|@|/ } $1

done


info "mysql"
info "zookeeper"
info "redis"

chmod 755 $servicetatus

sh $servicetatus
