Alehandro Oh, OK.
I proxied all of my site (mostly Laravel) through Cloudflare and set bash script to monitor my CPU usage, When it reaches, let's say, 80%, it will send a request to Cloudflare's REST API to activate my Cloudflare challenge.
Something like:
Activate WAF and other necessary Coudflare rules when CPU or RAM usage is >= 80% (cronjob every minute)
CPU_PERCENTAGE=`top -b -n2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,' | awk '{print 100-$8}'`
CPU_PERCENTAGE=`printf "%.0f" "$CPU_PERCENTAGE"`
RAM_PERCENTAGE=`free -t | awk 'FNR == 2 {printf("%d"), $3/$2*100}'`
RAM_PERCENTAGE=`printf "%.0f" "$RAM_PERCENTAGE"`
if [[ $CPU_PERCENTAGE -ge 80 || $RAM_PERCENTAGE -ge 80 ]]
then
echo "ACTIVATING CF WAF - RAM: $RAM_PERCENTAGE% - CPU: $CPU_PERCENTAGE%"
cd /www/wwwroot/mysite.com/ && php artisan app:cloudflare --status=on
fi
De-activate WAF and other necessary Coudflare rules when CPU or RAM usage is <= 30% (cronjob every hour or as you wish)
CPU_PERCENTAGE=`top -b -n2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,' | awk '{print 100-$8}'`
CPU_PERCENTAGE=`printf "%.0f" "$CPU_PERCENTAGE"`
RAM_PERCENTAGE=`free -t | awk 'FNR == 2 {printf("%d"), $3/$2*100}'`
RAM_PERCENTAGE=`printf "%.0f" "$RAM_PERCENTAGE"`
if [[ $CPU_PERCENTAGE -le 30 || $RAM_PERCENTAGE -le 30 ]]
then
echo "DEACTIVATING CF WAF - RAM: $RAM_PERCENTAGE% - CPU: $CPU_PERCENTAGE%"
cd /www/wwwroot/mysite.com/ && php artisan app:cloudflare --status=off
fi
Cloudflare's REST API docs are available here:
https://developers.cloudflare.com/api/