shell图形化界面脚本实现

image.png
image.png
语法:
whiptail --title "" --yesno ""  

whiptail --title "" --radiolist ""    [    ] . . .

进度条:
whiptail --gauge ""   

1.基于架构/角色进行检测2.检测网络端口映射是否正常3.检测其进程/启动。

shell字符串拼接截取,shell数组如何引入函数,shell图像化界面,以及一些检测和启动服务的命令

#!/bin/bash
. /etc/rc.d/init.d/functions

#此脚本用于---服务运维
#time:2018/10/29
#@一个热爱生活的好少年_lzy

echo_red() {
    stty erase '^H'
    echo -n -e "\033[31m$1\033[0m"
}

echo_red_enter() {
    echo -e "\033[31m$1\033[0m"
}

echo_green() {
    stty erase '^H'
    echo -n -e "\033[32m$1\033[0m"
}

echo_green_enter() {
    echo -e "\033[32m$1\033[0m"
}

function clean_buffer(){

    echo 3 > /proc/sys/vm/drop_caches
}

function warning
{
    if [[ $# = 2 ]];then
        [[ "$1" = 0 ]] && action "$2" /bin/true || action "$2" /bin/false
    elif [[ $# = 1 ]];then
        icontent=$(cat $ERROR_LOG)
        if [[ -n "$icontent" ]];then
            action "$1" /bin/false && cat $ERROR_LOG >> $LOG && rm -rf $ERROR_LOG && exit
        else
            action "$1" /bin/true
        fi
    fi
}

#进程
#定义全局变量
hybird_select_animo=
#界面2
function radiolist() {
    DISTROS=$(whiptail --title "请选择所需要的模块" --radiolist \
    "请选择所需要的脚本?" 15 60 4 \
    "hybird0" "/与网络检测" ON \
    "hybird1" "中架构" OFF \
    "hybird2" "架构" OFF \
    "hybird3" "他()" OFF 3>&1 1>&2 2>&3)
 
    exitstatus=$?
    if [ $exitstatus = 0 ]; then
        hybird_select_animo=$DISTROS
        echo_green_enter "您所选择的模块 $hybird_select_animo" 
    else
        echo "You chose Cancel."
    fi
}

#界面3
function hybird0 {
    OPTION=$(whiptail --title "请选择需要检测的模块" --menu "Choose your option" 15 60 4 \
    "1" "网络联通性)" \
    "2" "t服务检测" \
    "3" "s服务器检测" \
    "4" "all(融合)"  3>&1 1>&2 2>&3)
 
    exitstatus=$?
    #echo > $hytmp
    #shell数组如何传递到函数中
    if [ $exitstatus = 0 ]; then
        #echo "Your chosen option:" $OPTION
        if [ $OPTION == 1 ];then
            echo_green_enter "网络联通性检测:"
            #network_test
            network_test0
        elif [ $OPTION == 2 ];then
            echo "开始检服务:"
           # Hardware 变量融t
          #Process_test $Process_all_rh  
            echo_green "本次共检测nginx rsync memcached 服务"
            Process_all_rh=("nginx" "rsync" "memcached")
            Process_test "${Process_all_rh[*]}"
            Process_all_rh1=("telt" "tele" "ant")
            Process_test_java "${Process_all_rh1[*]}"
            #swift_service_test    
        elif [ $OPTION == 3 ];then
            swift_service_test   
        elif [ $OPTION == 4 ];then
            echo_green_enter "----性能测试:"
            all
            
        fi
    else
        echo "You chose Cancel."
    fi  
}






#swift进程检测

function swift_service_test(){
    echo_red "swift进程检测:"
    ps -ef | grep swift
    sleep 5
    if (whiptail --title "是否继续启动swift服务" --yesno "建议:相关存储服务服务 是否继续执行:请选择" 15 80) then
    echo "You chose Yes. 开始执行环境检测!"  

    swift_proxy restart




else
    echo "You chose No. Exit status was $?."
fi

}



function swift_proxy() {
    systemctl $1 openstack-swift-proxy.service memcached.service rsyncd.service openstack-keystone.service
    systemctl $1 openstack-swift-account.service openstack-swift-account-auditor.service openstack-swift-account-reaper.service openstack-swift-account-replicator.service
    systemctl $1 openstack-swift-container.service openstack-swift-container-auditor.service openstack-swift-container-replicator.service openstack-swift-container-updater.service
    systemctl $1 openstack-swift-object.service openstack-swift-object-auditor.service openstack-swift-object-replicator.service openstack-swift-object-updater.service
}

function swift_object() {
    systemctl $1 rsyncd.service
    systemctl $1 openstack-swift-account.service openstack-swift-account-auditor.service openstack-swift-account-reaper.service openstack-swift-account-replicator.service
    systemctl $1 openstack-swift-container.service openstack-swift-container-auditor.service openstack-swift-container-replicator.service openstack-swift-container-updater.service
    systemctl $1 openstack-swift-object.service openstack-swift-object-auditor.service openstack-swift-object-replicator.service openstack-swift-object-updater.service
}















# #查看telnet命令是否存在
function network_test0 {
    #查看telnet命令是否存在
    which "telnet" > /dev/null
    if [ $? -eq 0 ]
    then
        echo_green_enter "telnet is exist"
        network_telnet
    else
        echo_red "telnet not exist"
        echo "请先安装telnet服务:yum install  -y telnet 即可。"
    fi  

}

#网络层面检测
function network_telnet {
    #如果telnet安装成功

    #shell数组
    array=(www.baidu.com)
    eco_print1="检测结果:该服务器连接"
    eco_print3="主站服务器正常ok"
    eco_print4="主站服务器异常:请尽快开通。"
    #遍历数组
    for(( i=0;i<${#array[@]};i++)) 
        do 
        #echo ${array[i]}; 
        NPORT1=`echo ""|telnet  ${array[i]} 443 2>/dev/null|grep "\^]"|wc -l`
        if [[ $NPORT1 == 1  ]];then
            eco_print=${eco_print1}${array[i]}${eco_print3}
            echo_green ${eco_print}
        else
            eco_print=${eco_print1}${array[i]}${eco_print4}
            echo_red ${eco_print}
            whiptail --title "警告" --msgbox ${eco_print} 10 60
        fi
        sleep 1
        echo " "

    done;


}


#服务进程检测



#变量
eco_Pro_print1="检测结果:该服务"
eco_Pro_print3="已启动正常ok"
eco_Pro_print4="异常未启动(并检测服务器是否部署该服务)"

#function Arrayassignment(){

#}



#将所传的服务进行进程检测
function Process_test() {
    arr=$1
    #轮询上述所传的数组;
    for i in ${arr[*]}; do
        echo $i
        #echo ${array[i]}; 
        A=`ps -C $i --no-header |wc -l`
        echo_green "正在查看服务进程是否存在:"
        ps -ef | grep $i
        sleep 3
        if [ $A -eq 0 ];then                               ## 如果没有进程值得为 零 服务未启动 
            #弹出是否重启服务界面
            #if nginx如何下  teleport(java服务)需要/etc/ini.d/teleport start
            #
            #if 是rsync的话 需要变更为rsyncd
            echo_red "目前无该服务进程"
            if [[ $i == rsync ]]; then

                #statements
                value=${i}"d"

            else
                value=$i
            fi
            
            echo $value
            restart_Process_Interface $value
        
            
        else
            echo_green "该服务正常:进程已启动----------------"

        fi
    done

}

function Process_test_java() {
    arr=$1
    #轮询上述所传的数组;
    for i in ${arr[*]}; do
        echo $i
        #echo ${array[i]}; 
        #A=`ps -C $i --no-header |wc -l`
        echo_green "正在查看服务进程是否存在:"
        ps -ef | grep $i

        if [[ $i == teleport ]]; then

            #statements
            Pvalue=${i}"/"
        elif [[ $i == holmes ]]; then
            #statements
            Pvalue=${i}"/"
        else
            Pvalue=$i
        fi



        A=`ps -ef | grep $Pvalue | wc -l`
        sleep 3
        if [ $A -eq 1 ];then                               ## 如果没有进程值得为 零 服务未启动 
            #弹出是否重启服务界面
            #if nginx如何下  (java服务)需要/etc/ini.d/
            #
            #if 是rsync的话 需要变更为rsyncd
            echo_red "目前无该服务进程:"
            if [[ $i == rsync ]]; then

                #statements
                value=${i}"d"

            else
                value=$i
            fi
            
            echo $value
            restart_Process_Interface_java $value
        
            
        else
            echo_green "该服务正常:进程已启动----------------"

        fi
    done

}


#是否重启服务
function restart_Process(){

    if (whiptail --title "欢迎使用维护脚本" --yesno "提示:是否启动该服务:请选择" 15 80) then
    echo "You chose Yes. 正在启动服务!"  

    #radiolist
    if [[ $hybird_select_animo == hybird0 ]]; then
        hybird0
        #statements:
    elif [[ $hybird_select_animo == hybird1 ]]; then
        #statements
        hybird1
    elif [[ $hybird_select_animo == hybird2 ]]; then
     #statements
        hybird2
    elif [[ $hybird_select_animo == hybird3 ]]; then
        #statements
        hybird3
    fi



else
    echo "You chose No. Exit status was $?."
fi
}

#变量 shell 字符串拼接/截取:
eco_Pro_Interfac_print1="是否重启该服务:"
eco_Pro_Interfac_print3="已启动正常ok"
eco_Pro_Interfac_print4=":服务器异常启动失败(并检测服务器是否部署该服务)"

#界面显示并抉择是否重启/并检测(nginx/rabbit/rsyslog/rsyncd/)
function restart_Process_Interface(){

    restart_Process_name=$1
    eco_Pro_Interfac_print=${eco_Pro_Interfac_print1}${restart_Process_name}
    if (whiptail --title $eco_Pro_Interfac_print --yesno "提示:        是否继续执行:请选择" 15 80) then
        echo "You chose Yes. 开始启动服务!"  
        systemctl  start  $restart_Process_name

        if [[ $restart_Process_name == rsyncd ]]; then
            #statements
            restart_Process_name=rsync
        fi

                #如何判断进程是否存在
        A=`ps -C $restart_Process_name --no-header |wc -l`
        if [ $A -eq 0 ];then                               ## 如果没有进程值得为 零 服务未启动 
            #
            eco_Pro_Interfac_print_error=${restart_Process_name}${eco_Pro_Interfac_print4}
            echo_red $eco_Pro_Interfac_print_error
                    
        else
            eco_Pro_Interfac_print_suc=${restart_Process_name}${eco_Pro_Interfac_print3}
            echo_green $eco_Pro_Interfac_print_suc

         fi

    else
        echo "You chose No. Exit status was $?."
    fi
}

function restart_Process_Interface_java(){

    restart_Process_name=$1
    eco_Pro_Interfac_print=${eco_Pro_Interfac_print1}${restart_Process_name}
    if (whiptail --title $eco_Pro_Interfac_print --yesno "提示:        是否继续执行:请选择" 15 80) then
        echo "You chose Yes. 开始启动服务!"  
        cd /etc/init.d
        ./$restart_Process_name start

        if [[ $restart_Process_name == rsyncd ]]; then
            #statements
            restart_Process_name=rsync
        fi


        A=`ps -ef | grep $Pvalue | wc -l`
        if [ $A -eq 1 ];then                               ## 如果没有进程值得为 零 服务未启动 
            #如何进行字符串拼接
            eco_Pro_Interfac_print_error=${restart_Process_name}${eco_Pro_Interfac_print4}
            echo_red $eco_Pro_Interfac_print_error
                    
        else
            eco_Pro_Interfac_print_suc=${restart_Process_name}${eco_Pro_Interfac_print3}
            echo_green $eco_Pro_Interfac_print_suc

         fi

    else
        echo "You chose No. Exit status was $?."
    fi
}














#执行入口图形化界面
if (whiptail --title "欢迎使用---维护脚本" --yesno "提示:是否继续执行:请选择" 15 80) then
    echo "You chose Yes. 开始执行环境检测!"  

    radiolist
    if [[ $hybird_select_animo == hybird0 ]]; then
        hybird0
        #statements
    elif [[ $hybird_select_animo == hybird1 ]]; then
        #statements
        hybird1
    elif [[ $hybird_select_animo == hybird2 ]]; then
     #statements
        hybird2
    elif [[ $hybird_select_animo == hybird3 ]]; then
        #statements
        hybird3
    fi



else
    echo "You chose No. Exit status was $?."
fi

你可能感兴趣的:(shell图形化界面脚本实现)