過載保護腳本

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容