webserver automated bot defense?

I run Anubis in front of my webserver, but the crawler bots manage to be an annoyance from time to time still.
the problem is that when the load on the server goes high (15.xx) and stays there (for more than five - ten minutes) the web server is unable to server pages.
Manual workaroun is to take down (stop) the web server for ten minutes, then start it again.
Has anyone made a script or something (configurable preferred) that does in effect
if server load is > 15 for more than 10 minutes
then
stop web server
wait 10 minutes
start web server
endif
I can write it myself, but if such a tool already exists...
 
made with ai but seems legit
sh:
#!/bin/sh
# /usr/local/bin/load-guard.sh: restart the web server after sustained high load
SERVICE="nginx"        # or apache24 / lighttpd
THRESHOLD=15
SAMPLE=30              # seconds between checks
WINDOW=600             # sustained high load required (10 min)
COOLDOWN=600           # time the server stays stopped (10 min)

over=0
while true; do
  # vm.loadavg prints "{ 1min 5min 15min }", so field 2 is the 1-minute average
  load=$(sysctl -n vm.loadavg | awk '{print $2}')
  if awk -v l="$load" -v t="$THRESHOLD" 'BEGIN{exit !(l>t)}'; then
    over=$((over + SAMPLE))
  else
    over=0
  fi

  if [ "$over" -ge "$WINDOW" ]; then
    logger -t load-guard "load $load > $THRESHOLD for ${WINDOW}s: restarting $SERVICE"
    service "$SERVICE" stop
    sleep "$COOLDOWN"
    service "$SERVICE" start
    over=0
  fi
  sleep "$SAMPLE"
done
Code:
#!/bin/sh
# /usr/local/etc/rc.d/loadguard

# PROVIDE: loadguard
# REQUIRE: DAEMON LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="loadguard"
rcvar="loadguard_enable"
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-P ${pidfile} -r -f /usr/local/bin/load-guard.sh"

load_rc_config $name
: ${loadguard_enable:="NO"}

run_rc_command "$1"

chmod +x /usr/local/bin/load-guard.sh /usr/local/etc/rc.d/loadguard
sysrc loadguard_enable=YES
service loadguard start
 
Back
Top