Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6.25 check.sh汇总 #49

Closed
vieyahn2017 opened this issue Jun 25, 2019 · 6 comments
Closed

6.25 check.sh汇总 #49

vieyahn2017 opened this issue Jun 25, 2019 · 6 comments

Comments

@vieyahn2017
Copy link
Owner

check.sh使用的bash知识不少

@vieyahn2017
Copy link
Owner Author

第一版 5.26

vieyahn2017/iBlog#288 (comment)

@vieyahn2017
Copy link
Owner Author

第二版 稳定版 6.10

#!/usr/bin/env bash

SRC_PATH=`cd "$(dirname "$0")"; pwd`

####全部公共配置项
source ./install.sh declare > /dev/null

root_pwd=""

if [ -z "$ssh_port" ];then
    sshpass_port=22
else
    sshpass_port=$ssh_port
fi


read_pwd() {
    #输入待部署虚拟机的root账号的密码
    echo -e "\033[34mPlease enter the password of root account of the virtual machine to be deployed:\033[0m"
    read root_pwd
}

check_read_pwd_by_connection() {
    sshpass -p $root_pwd ssh -p $sshpass_port root@$1 -y "uname -a "  > /dev/null
    if [ $? -ne 0 ]; then
        echo "connect failed, please check the pwd."
        exit 1
    fi
}

## using sshpass
check_sshpass() {
    sshpass -V > /dev/null
    result=$?
    if [ -n "$1" ];then
        return $result
    fi
    if [ $result -ne 0 ]; then
        echo "sshpass not installed."
        sh "${SRC_PATH}/tools/deploy/deploy_machine/prepare/install_sshpass.sh"
    fi
}

check_nginx_by_sshpass () {
    local nginx_server_ip=$1
    local pwd=$2
    local port=$3
    local sshout
    local service_ip
    local service_ip_num_begin=`echo $nginx_server_ip | grep -o ','|wc -l`
    local service_ip_num=$(($service_ip_num_begin+1))
    for((i=0;i<$service_ip_num;i++)); do
        count=$(($i+1))
        service_ip=`echo $nginx_server_ip | awk -F ',' '{print $'$count'}'`
        sshout=`sshpass -p $pwd ssh -p $port root@$service_ip -y  "ps -ef | grep nginx | grep viid | grep -v grep"`
        echo -e "nginx at ${service_ip}:"  >> $4
        echo "$sshout"  >> $4
        echo -e "----------------------------------\n"  >> $4
        if [ `echo "$sshout" | wc -l` -gt 3 ]; then
            echo -e "\033[34mnginx\033[0m at \033[35m${service_ip}\033[0m \033[32msuccess\033[0m"
        else
            echo -e "\033[34mnginx\033[0m at \033[35m${service_ip}\033[0m \033[31mfailed\033[0m"
        fi
    done
}

check_keepalived_by_sshpass () {
    local keepalived_server_ip=$1
    local pwd=$2
    local port=$3
    local sshout
    local service_ip
    local service_ip_num_begin=`echo $keepalived_server_ip | grep -o ','|wc -l`
    local service_ip_num=$(($service_ip_num_begin+1))
    for((i=0;i<$service_ip_num;i++)); do
        count=$(($i+1))
        service_ip=`echo $keepalived_server_ip | awk -F ',' '{print $'$count'}'`
        sshout=`sshpass -p $pwd ssh -p $port root@$service_ip -y  "systemctl status keepalived"`

        echo -e "keepalived at ${service_ip}:\n"  >> $4
        echo "$sshout"  >> $4
        echo -e "----------------------------------\n"  >> $4

        keepalived_ps_count=`sshpass -p $pwd ssh -p $port root@$service_ip -y  "ps -ef | grep keepalived | grep -v grep | wc -l"`
        if [ "$keepalived_ps_count" == "2" -a `echo "$sshout" | grep -c ailed` -eq 0 ]; then
            echo -e "\033[34mkeepalived\033[0m at \033[35m${service_ip}\033[0m \033[32msuccess\033[0m"
        else
            echo -e "\033[34mkeepalived\033[0m at \033[35m${service_ip}\033[0m \033[31mfailed\033[0m"
        fi
    done
}


check_microservices_by_sshpass () {
    local java_server_ip=$1
    local pwd=$2
    local port=$3
    local sshout
    local service_ip
    local service_ip_num_begin=`echo $java_server_ip | grep -o ','|wc -l`
    local service_ip_num=$(($service_ip_num_begin+1))
    for((i=0;i<$service_ip_num;i++)); do
        count=$(($i+1))
        service_ip=`echo $java_server_ip | awk -F ',' '{print $'$count'}'`
        sshout=`sshpass -p $pwd ssh -p $port root@$service_ip -y  "ps -ef | grep java | grep viid | grep -v grep"`
        echo -e "\java microservice at ${service_ip}:\n"  >> $4
        echo "$sshout"  >> $4
        echo -e "\----------------------------------\n"  >> $4
        # echo "$sshout" | awk '{print $10}' | cut -d '/' -f 4
        for item in `echo "$sshout" | awk '{print $10}' | cut -d '/' -f 4`; do
            echo -e "\033[32msuccess ${service_ip} ${item}\033[0m"
            echo "${item} ${service_ip}" >> $5
        done
    done
}

check_restgateway() {
    local restgateway_server_ip=$1
    echo -e "RestGateWay http calling:\n"  >> $2
    curl -s "http://${restgateway_server_ip}:8088/"  >> $2
    echo -e "=======================================\n"  >> $2
}

check_floatip() {
    local float_ip=$1
    echo -e "Https proxy calling:\n"  >> $2
    curl -s -k "https://${float_ip}:8090/"  >> $2
    echo -e "=======================================\n"  >> $2
}


current_time=`date +%y%m%d%k%M%S`
check_log_file="check${current_time}.log"
touch $check_log_file



check_sshpass
check_sshpass 2
if [ $? -ne 0 ]; then
    echo "sshpass not installed success. this check.sh will exit."
    exit 1
fi


if [ -z "$root_pwd" ]; then
    read_pwd
fi

# 以任意ip检查read_pwd用户输入的密码,这边以网关为例。密码错误则退出
check_read_pwd_by_connection $RestGateWay_ip


## 检查nginx
check_nginx_by_sshpass  $Nginx_sotfware_ip $root_pwd $sshpass_port $check_log_file

## 检查keepalived
check_keepalived_by_sshpass $Keepalived_software_ip $root_pwd $sshpass_port $check_log_file

Java_microservices_ip=`declare | grep Service_ip | cut -d "=" -f 2 | sort | uniq | xargs -n 2 | tr ' ' ','`
if [ `echo $Java_microservices_ip | grep -c $RestGateWay_ip` -eq 0 ];then
    Java_microservices_ip="${Java_microservices_ip},${RestGateWay_ip}"
fi

touch check_java_names_xsafe.log
## 检查java微服务
check_microservices_by_sshpass $Java_microservices_ip $root_pwd $sshpass_port $check_log_file "check_java_names_xsafe.log"


check_restgateway $RestGateWay_ip $check_log_file

#check_floatip $Keepalived_float_ip  $check_log_file

check_floatip `echo $Nginx_sotfware_ip | awk -F "," '{print $1}' `  $check_log_file
# 目前的安装环境,浮动ip不可用,改用nginx的ip访问


@vieyahn2017
Copy link
Owner Author

vieyahn2017 commented Jun 25, 2019

最初的 ps -ef | grep java
查询结果里面:

ps -ef | grep java
viid       377     1  0 19:05 ?        00:00:08 java -jar /opt/fudd/XxManagementService/XxManagementService-*-SNAPSHOT.jar

直接使用

| awk '{print $10}' 

再固定位的cut就可以获得结果

| cut -d '/' -f 4

就可以获得结果

@vieyahn2017
Copy link
Owner Author

但是后续java进程的改变:

ps -ef | grep java
viid     32590     1  0 19:05 ?        00:00:10 java -jar /opt/fudd/PpService/PpService-*-SNAPSHOT.jar
viid     32757     1  0 19:05 ?        00:00:11 java -cp .:/opt/fudd/DdService/lib/*:/opt/fusion_viid/DdService/DdService-*-SNAPSHOT.jar com.my.service.ServiceProvider
viid     23880     1  1 19:07 ?        00:00:36 java -Xmx1G -Xms1G -Xss256K -XX:+UseG1GC -cp .:/opt/fudd/DiService/lib/*:/opt/fusion_viid/DiService/libv5/*:/opt/fusion_viid/DiService/DiService-*-SNAPSHOT.jar com.my.provider.ServiceProvider

给获取服务名带来了极大的麻烦

@vieyahn2017
Copy link
Owner Author

目前的查进程获取服务名的代码:

ps -ef | grep java | grep viid | grep -v grep 
| awk '{for(i=8;i<=NF;i++){if($i~/Service|Rest/){print $i;break;}}}' 
| awk -F '/' '{for(i=1;i<=NF;i++){if($i~/Service|Rest/){print $i;break;}}}'

第一个awk判断服务所在位置;
第二个awk按/切割,获取服务名字。

@vieyahn2017
Copy link
Owner Author

最新版 6.26

#!/usr/bin/env bash

SRC_PATH=`cd "$(dirname "$0")"; pwd`

####全部公共配置项
source ./install.sh declare > /dev/null

root_pwd="$1"

if [ -z "$ssh_port" ];then
    sshpass_port=22
else
    sshpass_port=$ssh_port
fi


read_pwd() {
    #输入待部署虚拟机的root账号的密码
    echo -e "\033[34mPlease enter the password of root account of the virtual machine to be deployed:\033[0m"
    read root_pwd
}

check_read_pwd_by_connection() {
    sshpass -p $root_pwd ssh -p $sshpass_port root@$1 -y "uname -a "  > /dev/null
    if [ $? -ne 0 ]; then
        echo "connect failed, please check the pwd."
        exit 1
    fi
}

## using sshpass
check_sshpass() {
    sshpass -V > /dev/null
    result=$?
    if [ -n "$1" ];then
        return $result
    fi
    if [ $result -ne 0 ]; then
        echo "sshpass not installed."
        sh "${SRC_PATH}/tools/deploy/deploy_machine/prepare/install_sshpass.sh"
    fi
}

check_nginx_by_sshpass () {
    local nginx_server_ip=$1
    local pwd=$2
    local port=$3
    local sshout
    local service_ip
    local service_ip_num_begin=`echo $nginx_server_ip | grep -o ','|wc -l`
    local service_ip_num=$(($service_ip_num_begin+1))
    for((i=0;i<$service_ip_num;i++)); do
        count=$(($i+1))
        service_ip=`echo $nginx_server_ip | awk -F ',' '{print $'$count'}'`
        sshout=`sshpass -p $pwd ssh -p $port root@$service_ip -y  "ps -ef | grep nginx | grep viid | grep -v grep"`
        echo -e "nginx at ${service_ip}:"  >> $4
        echo "$sshout"  >> $4
        echo -e "----------------------------------\n"  >> $4
        if [ `echo "$sshout" | wc -l` -gt 3 ]; then
            # echo -e "\033[34mnginx\033[0m at \033[35m${service_ip}\033[0m \033[32msuccess\033[0m"
            printf "%-30s%-15s%20s\n" "nginx" ${service_ip} "success"
        else
            # echo -e "\033[34mnginx\033[0m at \033[35m${service_ip}\033[0m \033[31mfailed\033[0m"
            printf "%-30s%-15s%20s\n" "nginx" ${service_ip} "failed"
        fi
    done
}

check_keepalived_by_sshpass () {
    local keepalived_server_ip=$1
    local pwd=$2
    local port=$3
    local sshout
    local service_ip
    local service_ip_num_begin=`echo $keepalived_server_ip | grep -o ','|wc -l`
    local service_ip_num=$(($service_ip_num_begin+1))
    for((i=0;i<$service_ip_num;i++)); do
        count=$(($i+1))
        service_ip=`echo $keepalived_server_ip | awk -F ',' '{print $'$count'}'`
        sshout=`sshpass -p $pwd ssh -p $port root@$service_ip -y  "systemctl status keepalived"`

        echo -e "keepalived at ${service_ip}:\n"  >> $4
        echo "$sshout"  >> $4
        echo -e "----------------------------------\n"  >> $4

        keepalived_ps_count=`sshpass -p $pwd ssh -p $port root@$service_ip -y  "ps -ef | grep keepalived | grep -v grep | wc -l"`
        if [ "$keepalived_ps_count" == "2" -a `echo "$sshout" | grep -c ailed` -eq 0 ]; then
            # echo -e "\033[34mkeepalived\033[0m at \033[35m${service_ip}\033[0m \033[32msuccess\033[0m"
            printf "%-30s%-15s%20s\n" "keepalived" ${service_ip} "success"
        else
            # echo -e "\033[34mkeepalived\033[0m at \033[35m${service_ip}\033[0m \033[31mfailed\033[0m"
            printf "%-30s%-15s%20s\n" "keepalived"  ${service_ip} "failed"
        fi
    done
}


check_microservices_by_sshpass () {
    local java_server_ip=$1
    local pwd=$2
    local port=$3
    local sshout
    local service_ip
    local service_ip_num_begin=`echo $java_server_ip | grep -o ','|wc -l`
    local service_ip_num=$(($service_ip_num_begin+1))
    for((i=0;i<$service_ip_num;i++)); do
        count=$(($i+1))
        service_ip=`echo $java_server_ip | awk -F ',' '{print $'$count'}'`
        sshout=`sshpass -p $pwd ssh -p $port root@$service_ip -y  "ps -ef | grep java | grep viid | grep -v grep"`
        echo -e "\java microservice at ${service_ip}:\n"  >> $4
        echo "$sshout"  >> $4
        echo -e "\----------------------------------\n"  >> $4
        for item in `echo "$sshout" | awk '{for(i=8;i<=NF;i++){if($i~/Service|RestGateWay/){print $i;break;}}}' | awk -F '/' '{for(i=1;i<=NF;i++){if($i~/Service|RestGateWay/){print $i;break;}}}' `; do
            # echo -e "\033[35m${service_ip}\033[0m \033[32msuccess\033[0m \033[34m${item}\033[0m"
            printf "%-30s%-15s%20s\n" ${item} ${service_ip} "success"
        done
    done
}

check_restgateway() {
    local restgateway_server_ip=$1
    echo -e "RestGateWay http calling:\n"  >> $2
    curl -s "http://${restgateway_server_ip}:8088/"  >> $2
    echo -e "=======================================\n"  >> $2
}

check_floatip() {
    local float_ip=$1
    echo -e "Https proxy calling:\n"  >> $2
    curl -s -k "https://${float_ip}:8090/"  >> $2
    echo -e "=======================================\n"  >> $2
}


#current_time=`date +%y%m%d%k%M%S`
#check_log_file="check${current_time}.log"
check_log_file=check_touch.log
touch $check_log_file



check_sshpass
check_sshpass 2
if [ $? -ne 0 ]; then
    echo "sshpass not installed success. this check.sh will exit."
    exit 1
fi


if [ -z "$root_pwd" ]; then
    read_pwd
fi

# 以任意ip检查read_pwd用户输入的密码,这边以网关为例。密码错误则退出
check_read_pwd_by_connection $RestGateWay_ip


## 检查nginx
check_nginx_by_sshpass  $Nginx_sotfware_ip $root_pwd $sshpass_port $check_log_file

## 检查keepalived
check_keepalived_by_sshpass $Keepalived_software_ip $root_pwd $sshpass_port $check_log_file

Java_microservices_ip=`declare | grep Service_ip | cut -d "=" -f 2 | sort | uniq | xargs -n 2 | tr ' ' ','`
if [ `echo $Java_microservices_ip | grep -c $RestGateWay_ip` -eq 0 ];then
    Java_microservices_ip="${Java_microservices_ip},${RestGateWay_ip}"
fi


## 检查java微服务
check_microservices_by_sshpass $Java_microservices_ip $root_pwd $sshpass_port $check_log_file


check_restgateway $RestGateWay_ip $check_log_file

#check_floatip $Keepalived_float_ip  $check_log_file

check_floatip `echo $Nginx_sotfware_ip | awk -F "," '{print $1}' `  $check_log_file
# 目前的安装环境,浮动ip不可用,改用nginx的ip访问

echo "... more information at $check_log_file"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant