过载保护脚本

#!/bin/bash

##create date 20170518

##write by swx420528

##version 1.0

##overload_protection.sh

##use for GenexCloud Riak-CS proxy(nginx)

##Nginx 部署路径

nginx_home_path=/opt/nginx

##限制阀值和解除限制阀值

limit_threshold=95

open_threshold=85

##riak-cs node 存储挂载目录

riak_mount_dir='/mnt'

##check account

if [ "`whoami`" != "root" ];then

echo "Please  execuse this script with paas account..."

exit 0

fi

##get riak-cs cluster node IP address

riak_node_iplist=`cat $nginx_home_path/conf/nginx.conf|grep 8080|awk -F: '{print $1}'|awk '{print $2}'`

##检查是否已安装ansible,未安装则退出,并记录日志

##检查tmp目录是否存在,则创建

##获取每个node的磁盘使用率,并记录在临时文件

for i in $riak_node_iplist

do

ansible $i -m command -a 'df'|grep $riak_mount_dir|awk '{print $5}' | sed -ne 2p | cut -d"%" -f1 >> /tmp/riak_node_disk_useage

done

##get disk usage,取最大值为

used_rate=`cat /tmp/riak_node_disk_useage|sort -rn|head -1`

##获取 PUT_flag 值,0表示已限制PUT请求,1表示未限制PUT请求

cat $nginx_home_path/conf/nginx.conf|grep "$request_method = PUT"|grep -v "#"

PUT_flag="$?"

##判断如果磁盘使用率超过阈值,且nginx未做PUT限制,在nginx配置文件增加PUT限制的配置并reload nginx服务

if [ "$used_rate" -ge "$limit_threshold" ] && [ "$PUT_flag" = "1" ];then

sed -i '/location \//a\            if ( $request_method = PUT ) { return 403; }' $nginx_home_path/conf/nginx.conf

$nginx_home_path/sbin/nginx -s reload

fi

##判断如果磁盘使用率低于解除限制阈值,且nginx已做PUT限制,在nginx配置文件删除PUT限制的配置并reload nginx服务

if [ "$used_rate" -le "$open_threshold" ] && [ "$PUT_flag" = "0" ];then

sed -i '/$request_method = PUT/d' $nginx_home_path/conf/nginx.conf

$nginx_home_path/sbin/nginx -s reload

fi

你可能感兴趣的:(过载保护脚本)