本帖最后由 mingxingye 于 2022-3-9 13:07 编辑
- mode="cpu" #判断服务器负载方式 load负载法 cpu CPU百分比法 只能选一个
- if [ "$mode" = "cpu" ];
- then
- check=80 #5秒内CPU连续超过80 重启NG【可以根据您的服务器负荷情况调整】
- #系统空闲时间
- TIME_INTERVAL=60
- time=$(date "+%Y-%m-%d %H:%M:%S")
- LAST_CPU_INFO=$(cat /proc/stat | grep -w cpu | awk ‘{print $2,$3,$4,$5,$6,$7,$8}’)
- LAST_SYS_IDLE=$(echo $LAST_CPU_INFO | awk ‘{print $4}’)
- LAST_TOTAL_CPU_T=$(echo $LAST_CPU_INFO | awk ‘{print $1+$2+$3+$4+$5+$6+$7}’)
- sleep ${TIME_INTERVAL}
- NEXT_CPU_INFO=$(cat /proc/stat | grep -w cpu | awk ‘{print $2,$3,$4,$5,$6,$7,$8}’)
- NEXT_SYS_IDLE=$(echo $NEXT_CPU_INFO | awk ‘{print $4}’)
- NEXT_TOTAL_CPU_T=$(echo $NEXT_CPU_INFO | awk ‘{print $1+$2+$3+$4+$5+$6+$7}’)
-
- #系统空闲时间
- SYSTEM_IDLE=`echo ${NEXT_SYS_IDLE} ${LAST_SYS_IDLE} | awk ‘{print $1-$2}’`
- #CPU总时间
- TOTAL_TIME=`echo ${NEXT_TOTAL_CPU_T} ${LAST_TOTAL_CPU_T} | awk ‘{print $1-$2}’`
- load=`echo ${SYSTEM_IDLE} ${TOTAL_TIME} | awk ‘{printf "%.2f", 100-$1/$2*100}’`
- else
- load=$(cat /proc/loadavg | colrm 60)
- check=$(cat /proc/cpuinfo | grep "processor" | wc -l)
- fi
- if [[ $load >$check ]]
- #加上重启NGINX的命令
- echo -e "n$mode当前负载大于$check 重启Nginx"
- elif [[ $load <$check ]]
- then
- echo -e "n$mode当前负载低于$check不做任何操作"
- fi
复制代码
|