批量检查服务器重启是否完成

#!/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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值