Git browser: sbin-tm/
This page presents code associated with the module/unit named above.
sbin-tm/load-trigger.sh
#!/bin/sh
# 2021-04-11
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
interval=120
counter=0
while true;
do
load=$(uptime | awk '{a=$(NF-2);sub(/,$/,"",a); print int(a+.5)}')
echo -n $load
echo -n ' - '
if [ 10 -le $load ]
then
tm_http_categories_throttle.sh high
else
tm_http_categories_throttle.sh low
fi
if [ 30 -le $load ]
then
service httpd stop
service mysqld stop
service mysqld start
service httpd start
echo -n 'restarted DB at '
echo $(date)
counter=$(($counter + 1))
# long interval
sleep $interval
else
counter=0
# tm_http_categories_throttle.sh low
fi
# if the load has been heavy for a while
if test $counter -ge 3
then
tm_http_categories_throttle.sh high
sleep $(($interval * 5))
fi
sleep 60
done
sbin-tm/tm_http_categories_throttle.sh
#!/bin/sh
# 2020-04-07
# updated 2021-04-23
# monitor CPU load and throttle via .htaccess if too high
# hysteresis is managed by not running the script frequently
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:
# implement alternative .htaccess settings at this load or higher
threshold=10
mode=${1}
# settings files: target, archive for normal, archive for high load
htaccess=/var/www/html/.htaccess
normal="${htaccess}.normal"
highload="${htaccess}.highload"
# verify that the source files exist
test -f ${normal} || test -r ${normal} || exit 1
test -f ${highload} || test -r ${highload} || exit 1
# exit on error
set -e
if [ ${mode} == 'high' ]
then
cp ${highload} ${htaccess}
echo -n 'Triggered: protected mode enabled at '
echo $(date)
# service httpd restart
exit
elif [ ${mode} == 'low' ]
then
cp ${normal} ${htaccess}
# echo -n 'Triggered: normal mode enabled at '
# echo $(date)
# service httpd restart
exit
fi
# scrape the current load average from top
load=$(top -b -n 1 | awk -F '[ ,]+' '{print int($12+.5);exit}')
# deal with it
if [ ${load} -ge ${threshold} ]; then
cp ${highload} ${htaccess}
echo -n 'Dealt with: protected mode enabled at '
echo $(date)
else
cp ${normal} ${htaccess}
echo -n 'Dealt with: normal mode enabled at '
echo $(date)
fi
# service httpd restart
exit 0