#!/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目錄是否存在,則創(chuàng)建
##獲取每個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服務(wù)
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服務(wù)
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