./pc_downgrade/ 0000755 0156533 0000144 00000000000 12535011447 014015 5 ustar hailong.xhl users ./pc_downgrade/pc_get_status.lua 0000644 0156533 0000144 00000000204 12535011337 017356 0 ustar hailong.xhl users local status = ngx.shared.pc_status key = "pc:g:fault"; local status_value = status:get(key) ngx.say(status_value); ngx.say("OK"); ./pc_downgrade/varnishctl 0000755 0156533 0000144 00000004625 12535010263 016122 0 ustar hailong.xhl users #!/bin/bash if [ -z $1 ];then echo "please input version" exit fi check_ver() { echo $1 | grep -Eq '[0-9]{14}$' if [ $? -ne 0 ];then echo "version is wrong" exit fi } varnish_id=$(date +%Y%m%d%H%M%S) varnish="/home/varnish/sbin/varnishd" varnishadm="/home/varnish/bin/varnishadm" prog=$(basename $varnish) VARNISH_CONF_FILE="/home/varnish/etc/varnish/default.vcl" PID=$(pgrep varnish) myPid=$$ adm_port=2000 listen_port=8080 start_jobs() { $varnish \ -a 0.0.0.0:$listen_port \ -f $VARNISH_CONF_FILE \ -T 127.0.0.1:$adm_port \ -s file,/home/varnish/vcache/varnish_cache.data,10G \ -p thread_pool_min=200 \ -p thread_pool_max=4000 \ -p thread_pools=6 \ -p thread_pool_timeout=60 \ -p thread_pool_add_delay=2 \ -p listen_depth=4096 \ -p lru_interval=86400 } start_check() { [ -x $varnish ] || exit 5 [ -f $VARNISH_CONF_FILE ] || exit 6 if [ "$PID" == "$myPid" ] || [ -z "$PID" ];then echo $"Starting $prog: OK" start_jobs else echo "already running : $PID" fi } stop_check() { [ -x $varnish ] || exit 5 [ -f $VARNISH_CONF_FILE ] || exit 6 if [ -z "$PID" ];then echo $"$prog not running" else ret=$(pkill varnishd) echo $ret fi } purgehost_check() { [ -x $varnishadm ] || exit 5 if [ $# > 0 ];then $varnishadm -T localhost:$adm_port ban req.http.host == $1 echo "purged $1" fi } purge_check() { [ -x $varnishadm ] || exit 5 if [ $# > 0 ];then $varnishadm -T localhost:$adm_port ban.url $1 echo "purged $1" fi } check_reload(){ [ -x $varnishadm ] || exit 5 [ -f $VARNISH_CONF_FILE ] || exit 6 if [ -z "$PID" ];then echo $"$prog not running" else $varnishadm -T localhost:$adm_port vcl.load $varnish_id $VARNISH_CONF_FILE if [ $? -eq 0 ];then $varnishadm -T localhost:$adm_port vcl.use $varnish_id echo $"Check $prog : OK " else echo $"Check $prog Fail: " fi fi } check_list() { if [ -z "$PID" ];then echo $"$prog not running" else $varnishadm -T localhost:$adm_port vcl.list fi } check_reback() { check_ver $1 if [ $? -eq 0 ];then if [ -z "$PID" ];then echo $"$prog not running" else $varnishadm -T localhost:$adm_port vcl.use $1 fi echo $"$prog Version Back $1" fi } case "$1" in start) start_check && exit 0 $1 ;; stop) stop_check && exit 0 $1 ;; purgehost) purgehost_check $2 && exit 0 $1 ;; purge) purge_check $2 && exit 0 $1 ;; reload) check_reload && exit 7 $1 ;; list) check_list && exit 0 $1 ;; reback) check_reback $2 && exit 0 $1 ;; *) echo $"Usage: $0 {start|stop|reload|purge|purgehost|list|reback}" exit 2 esac ./pc_downgrade/pc_status_stat.lua 0000644 0156533 0000144 00000000211 12535011303 017541 0 ustar hailong.xhl users --ngx.log(ngx.ERR,"log it"..ngx.status) if ngx.status>=500 and ngx.status ~= 501 and ngx.status ~= 503 then pc_incr_fault_num() end ./pc_downgrade/varnish_crond.php 0000644 0156533 0000144 00000004244 12535010511 017360 0 ustar hailong.xhl users 1000000 ? 1000000: $size; fseek($file, 0-$offset, SEEK_END); $content = fread($file, $offset); $pattern = '/"GET (.*?)[" ]/'; preg_match_all($pattern, $content, $matchs); if(isset($matchs[1])){ return $matchs[1]; } return $ret; } $urls = parseNginxLog(); foreach($urls as $url){ get($url); } get("http://demo.bo56.com/index.php"); ?> ./pc_downgrade/init.lua 0000644 0156533 0000144 00000001314 12535011263 015456 0 ustar hailong.xhl users pc_get_global_fault_status = function() local status = ngx.shared.pc_status key = "pc:g:fault"; return tonumber(status:get(key)) or 0 end pc_get_fault_key = function(timestamp) if not timestamp then timestamp = ngx.time() end return os.date("fault:minute:%M", timestamp) end pc_get_fault_num = function(timestamp) local status = ngx.shared.pc_status local key = pc_get_fault_key(timestamp) return tonumber(status:get(key)) or 0 end pc_incr_fault_num = function(timestamp) local status = ngx.shared.pc_status local key = pc_get_fault_key(timestamp) if not status:incr(key, 1) then status:add(key, 0, 600) status:incr(key, 1) end end ./pc_downgrade/pc_set_status.lua 0000644 0156533 0000144 00000000210 12535011425 017365 0 ustar hailong.xhl users local status = ngx.shared.pc_status key = "pc:g:fault"; status:set(key,ngx.var.arg_status) ngx.say(ngx.var.arg_status); ngx.say("OK"); ./pc_downgrade/pc_get_downgrade_data.lua 0000644 0156533 0000144 00000001760 12535010626 021006 0 ustar hailong.xhl users -- threshold, error times in one minute local err_num = 50 -- how long happening error, minute local err_minute = 1 local ok = true local g_ok = true local g_status = pc_get_global_fault_status() if g_status >= 1 then ngx.log(ngx.ERR,g_status); g_ok = false end if g_ok then for i = 0, err_minute do --fail num > $err_num in recent $err_minute mins local num = pc_get_fault_num(ngx.time() - i * 60) --ngx.log(ngx.ERR,num); local status = ngx.shared.pc_status key = "pc:a:fault"; if num >= err_num then ok = false status:set(key,1) break else ok = true status:set(key,0) end end end if g_ok == false or ok == false then local url = ngx.var.uri; if ngx.var.args then url = url .. "?" .. ngx.var.args end if ngx.var.uri == "/check.php" then return ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE) end ngx.var.str_params = url ngx.exec("@var") else ngx.exec("@php") end ./pc_downgrade/default.vcl 0000644 0156533 0000144 00000007320 12535010067 016146 0 ustar hailong.xhl users # This is a basic VCL configuration file for varnish. See the vcl(7) # man page for details on VCL syntax and semantics. # # Default backend definition. Set this to point to your content # server. # probe dycheck { .url = "/tools/monitor.php"; .interval = 2s; .timeout = 3s; .window = 4; .threshold = 2; .initial = 3; .expected_response = 200; } backend default { .host = "wanke.etao.com"; .port = "80"; .probe = dycheck; } # # Below is a commented-out copy of the default VCL logic. If you # redefine any of these subroutines, the built-in logic will be # appended to your code. sub vcl_recv { if (req.http.User-Agent ~ "varnish") { return(error); }elseif(req.http.User-Agent ~ "crond"){ set req.http.User-Agent = "varnish_crond"; }else{ set req.http.User-Agent = "varnish_nginx"; set req.grace = 1d; } set req.url = regsub(req.url, "\?spm=[\.a-zA-Z0-9]*", ""); set req.url = regsub(req.url, "\&spm=[\.a-zA-Z0-9]*", ""); if (req.restarts == 0) { if (req.http.x-forwarded-for) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip; } else { set req.http.X-Forwarded-For = client.ip; } } if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") { /* Non-RFC2616 or CONNECT which is weird. */ return (pipe); } if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } /* if (req.http.Authorization || req.http.Cookie) { return (pass); } */ return (lookup); } sub vcl_pipe { # Note that only the first request to the backend will have # X-Forwarded-For set. If you use X-Forwarded-For and want to # have it set for all requests, make sure to have: # set bereq.http.connection = "close"; # here. It is not set by default as it might break some broken web # applications, like IIS with NTLM authentication. return (pipe); } sub vcl_pass { return (pass); } sub vcl_hash { hash_data(req.url); /* if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } */ return (hash); } sub vcl_hit { return (deliver); } sub vcl_miss { if(req.http.User-Agent ~ "varnish_nginx"){ return(error); }else{ return (fetch); } } sub vcl_fetch { if (beresp.status == 500 || beresp.status == 501 || beresp.status == 502 || beresp.status == 503) { set beresp.saintmode = 10s; } set beresp.grace = 7d; set beresp.ttl = 1d; return (deliver); } sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; }else{ set resp.http.X-Cache = "MISS"; } return (deliver); } sub vcl_error { set obj.http.Content-Type = "text/html; charset=utf-8"; set obj.http.Retry-After = "5"; synthetic {"
"} + obj.response + {"
XID: "} + req.xid + {"
Varnish cache server
"}; return (deliver); } sub vcl_init { return (ok); } sub vcl_fini { return (ok); }