批量檢查伺服器重啟是否完成
阿新 • • 發佈:2018-11-08
#!/bin/bash #Description:該指令碼使用場景,線上伺服器批量重啟,該指令碼通過呼叫http介面(為了安全起見,不讓其他主機直接ssh連線線上伺服器,只能通過唯一的一臺堡壘機連過去,這個http的exec介面就是在堡壘機上), # 介面就是基於ssh協議,我的指令碼可以在任意一臺能夠訪問堡壘機該介面的主機上執行,到遠端主機上執行uptime命令,並在本地生成對應的日誌檔案,根據uptime判斷遠端機器是否重啟以及是否重啟成功, # 並返回線上機器的IP地址以及不線上的主機數量。線上主機的具體IP地址到/tmp/host_uptime_${1}.log中檢視,並根據up時間判斷機器是否重啟了。 #Usage:/path/to/script.sh (1|2|4|5|10|all) #引數:1/2/4/5/10分別表示1:00、2:30、4:00、5:30和10:00重啟的機器;all表示所有重啟的機器 set -m ip1Array=() ip2Array=() ip4Array=() ip5Array=() ip10Array=() ipAllArray=() curlTimeout=15 case $1 in 1) : > /tmp/host_uptime_${1}.log for ip1 in ${ip1Array[@]} do curl --max-time ${curlTimeout} -X POST http://AAA.BBB.CCC.DDD:PORT/exec -H 'cache-control: no-cache' -H 'content-type: multipart/form-data; boundary=----xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -H 'postman-token: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' -F host=${ip1} zzzzzz="echo ${ip1};uptime" >> /tmp/host_uptime_${1}.log echo >> /tmp/host_uptime_${1}.log done hostsOnlineNum=$(cat /tmp/host_uptime_${1}.log | grep -Ev "(<pre></pre>|^[[:space:]]*$)" | wc -l) failedNum=$((70-${hostsOnlineNum})) hostsYes=$(grep -Ev "(<pre></pre>|^[[:space:]]*$)" /tmp/host_uptime_${1}.log | cut -d'>' -f2 | awk -F'<' '{print $1}') echo -e "\033[1;32mThere are 70 hosts to reboot at 2017-01-07 01:00\nHosts successfully: ${hostsOnlineNum}\n${hostsYes}\033[0m" echo -e "\033[1;31mHosts failed:${failedNum}\033[0m" ;; 2) : > /tmp/host_uptime_${1}.log for ip2 in ${ip2Array[@]} do curl --max-time ${curlTimeout} -X POST http://AAA.BBB.CCC.DDD:PORT/exec -H 'cache-control: no-cache' -H 'content-type: multipart/form-data; boundary=----xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -H 'postman-token: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' -F host=${ip2} -F zzzzzz="echo ${ip2};uptime" >> /tmp/host_uptime_${1}.log echo >> /tmp/host_uptime_${1}.log done hostsOnlineNum=$(cat /tmp/host_uptime_${1}.log | grep -Ev "(<pre></pre>|^[[:space:]]*$)" | wc -l) failedNum=$((4-${hostsOnlineNum})) hostsYes=$(grep -Ev "(<pre></pre>|^[[:space:]]*$)" /tmp/host_uptime_${1}.log | cut -d'>' -f2 | awk -F'<' '{print $1}') echo -e "\033[1;32mThere are 4 hosts to reboot at 2017-01-07 02:30\nHosts successfully: ${hostsOnlineNum}\n${hostsYes}\033[0m" echo -e "\033[1;31mHosts failed:${failedNum}\033[0m" ;; 4) : > /tmp/host_uptime_${1}.log for ip4 in ${ip4Array[@]} do curl --max-time ${curlTimeout} -X POST http://AAA.BBB.CCC.DDD:PORT/exec -H 'cache-control: no-cache' -H 'content-type: multipart/form-data; boundary=----xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -H 'postman-token: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' -F host=${ip4} -F zzzzzz="echo ${ip4};uptime" >> /tmp/host_uptime_${1}.log echo >> /tmp/host_uptime_${1}.log done hostsOnlineNum=$(cat /tmp/host_uptime_${1}.log | grep -Ev "(<pre></pre>|^[[:space:]]*$)" | wc -l) failedNum=$((1-${hostsOnlineNum})) hostsYes=$(grep -Ev "(<pre></pre>|^[[:space:]]*$)" /tmp/host_uptime_${1}.log | cut -d'>' -f2 | awk -F'<' '{print $1}') echo -e "\033[1;32mThere are 1 hosts to reboot at 2017-01-07 04:00\nHosts successfully: ${hostsOnlineNum}\n${hostsYes}\033[0m" echo -e "\033[1;31mHosts failed:${failedNum}\033[0m" ;; 5) : > /tmp/host_uptime_${1}.log for ip5 in ${ip5Array[@]} do curl --max-time ${curlTimeout} -X POST http://AAA.BBB.CCC.DDD:PORT/exec -H 'cache-control: no-cache' -H 'content-type: multipart/form-data; boundary=----xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -H 'postman-token: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' -F host=${ip5} -F zzzzzz="echo ${ip5};uptime" >> /tmp/host_uptime_${1}.log echo >> /tmp/host_uptime_${1}.log done hostsOnlineNum=$(cat /tmp/host_uptime_${1}.log | grep -Ev "(<pre></pre>|^[[:space:]]*$)" | wc -l) failedNum=$((3-${hostsOnlineNum})) hostsYes=$(grep -Ev "(<pre></pre>|^[[:space:]]*$)" /tmp/host_uptime_${1}.log | cut -d'>' -f2 | awk -F'<' '{print $1}') echo -e "\033[1;32mThere are 3 hosts to reboot at 2017-01-07 05:30\nHosts successfully: ${hostsOnlineNum}\n${hostsYes}\033[0m" echo -e "\033[1;31mHosts failed:${failedNum}\033[0m" ;; 10) : > /tmp/host_uptime_${1}.log for ip10 in ${ip10Array[@]} do curl --max-time ${curlTimeout} -X POST http://AAA.BBB.CCC.DDD:PORT/exec -H 'cache-control: no-cache' -H 'content-type: multipart/form-data; boundary=----xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -H 'postman-token: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' -F host=${ip10} -F zzzzzz="echo ${ip10};uptime" >> /tmp/host_uptime_${1}.log echo >> /tmp/host_uptime_${1}.log done hostsOnlineNum=$(cat /tmp/host_uptime_${1}.log | grep -Ev "(<pre></pre>|^[[:space:]]*$)" | wc -l) failedNum=$((40-${hostsOnlineNum})) hostsYesNum=$(grep -Ev "(<pre></pre>|^[[:space:]]*$|days)" /tmp/host_uptime_${1}.log | wc -l) hostsYes=$(grep -Ev "(<pre></pre>|^[[:space:]]*$|days)" /tmp/host_uptime_${1}.log | cut -d'>' -f2 | awk -F'<' '{print $1}') hostsNoReboot=$(grep "days" /tmp/host_uptime_${1}.log | cut -d'>' -f2 | awk -F'<' '{print $1}') hostsNoRebootNum=$(grep "days" /tmp/host_uptime_${1}.log | wc -l) echo -e "\033[1;32mThere are 40 hosts to reboot at 2017-01-07 10:00\nHosts successfully: ${hostsYesNum}\n${hostsYes}\033[0m" echo -e "\033[1;34mHosts noreboot: ${hostsNoRebootNum}\n${hostsNoReboot}\033[0m" echo -e "\033[1;31mHosts failed:${failedNum}\033[0m" ;; all) : > /tmp/host_uptime_${1}.log for ipAll in ${ipAllArray[@]} do curl --max-time ${curlTimeout} -X POST http://AAA.BBB.CCC.DDD:PORT/exec -H 'cache-control: no-cache' -H 'content-type: multipart/form-data; boundary=----xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -H 'postman-token: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' -F host=${ipAll} -F zzzzzz="echo ${ipAll};uptime" >> /tmp/host_uptime_${1}.log echo >> /tmp/host_uptime_${1}.log done hostsOnlineNum=$(cat /tmp/host_uptime_${1}.log | grep -Ev "(<pre></pre>|^[[:space:]]*$)" | wc -l) failedNum=$((118-${hostsOnlineNum})) hostsYes=$(grep -Ev "(<pre></pre>|^[[:space:]]*$)" /tmp/host_uptime_${1}.log | cut -d'>' -f2 | awk -F'<' '{print $1}') echo -e "\033[1;32mThere are 118 hosts to reboot all\nHosts successfully: ${hostsOnlineNum}\n${hostsYes}\033[0m" echo -e "\033[1;31mHosts failed:${failedNum}\033[0m" ;; *) echo -e "\033[1;33mUsage: time /path/to/host_uptime_v1.0.sh (1|2|4|5|10|all)\n說明:引數1/2/4/5/10分別表示1點、2:30、4:00、5:30和10:00重啟的機器;all表示所有重啟的機器。\033[0m" ;; esac