image.png
image.png
語法:
whiptail --title "<dialog box title>" --yesno "<text to show>" <height> <width>
whiptail --title "<radiolist title>" --radiolist "<text to show>" <height> <width> <list height> [ <tag> <item> <status> ] . . .
進度條:
whiptail --gauge "<test to show>" <height> <width> <inital percent>
1.基于架構(gòu)/角色進行檢測2.檢測網(wǎng)絡(luò)端口映射是否正常3.檢測其進程/啟動。
shell字符串拼接截取,shell數(shù)組如何引入函數(shù)想帅,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" "/與網(wǎng)絡(luò)檢測" ON \
"hybird1" "中架構(gòu)" OFF \
"hybird2" "架構(gòu)" 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" "網(wǎng)絡(luò)聯(lián)通性)" \
"2" "t服務檢測" \
"3" "s服務器檢測" \
"4" "all(融合)" 3>&1 1>&2 2>&3)
exitstatus=$?
#echo > $hytmp
#shell數(shù)組如何傳遞到函數(shù)中
if [ $exitstatus = 0 ]; then
#echo "Your chosen option:" $OPTION
if [ $OPTION == 1 ];then
echo_green_enter "網(wǎng)絡(luò)聯(lián)通性檢測:"
#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 "是否繼續(xù)啟動swift服務" --yesno "建議:相關(guān)存儲服務服務 是否繼續(xù)執(zhí)行:請選擇" 15 80) then
echo "You chose Yes. 開始執(zhí)行環(huán)境檢測啡莉!"
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
}
#網(wǎng)絡(luò)層面檢測
function network_telnet {
#如果telnet安裝成功
#shell數(shù)組
array=(www.baidu.com)
eco_print1="檢測結(jié)果:該服務器連接"
eco_print3="主站服務器正常ok"
eco_print4="主站服務器異常:請盡快開通。"
#遍歷數(shù)組
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="檢測結(jié)果:該服務"
eco_Pro_print3="已啟動正常ok"
eco_Pro_print4="異常未啟動(并檢測服務器是否部署該服務)"
#function Arrayassignment(){
#}
#將所傳的服務進行進程檢測
function Process_test() {
arr=$1
#輪詢上述所傳的數(shù)組咧欣;
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
#輪詢上述所傳的數(shù)組浅缸;
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 "提示: 是否繼續(xù)執(zhí)行:請選擇" 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 "提示: 是否繼續(xù)執(zhí)行:請選擇" 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
}
#執(zhí)行入口圖形化界面
if (whiptail --title "歡迎使用---維護腳本" --yesno "提示:是否繼續(xù)執(zhí)行:請選擇" 15 80) then
echo "You chose Yes. 開始執(zhí)行環(huán)境檢測烟具!"
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