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
 
No, it won't (agree on that)! I noticed the bots (the nasty things) would spin up a new ip once one was blocked pretty quick. ...I also noticed something I didn't quite understand where it seemed like one ip would make an initial request and "keep the connection live" while some other ip's would act as a spider from that level down (like they were working in conjunction). ...I really don't know the web stuff all that well, so I hope my description doesn't sound too much like pure gibberish.
 
I could kinda cope with the load but they were exhausting other resources like db connections which was annoying. my luck was that they for some reason where scrapping some long urls with lots of query args that were not really necessary for humans so i could block them from the url string via htaccess
 
I tried the htaccess route too (didn't work for me because I was always behind them in time). ...I created a token bucket kind of thing in C that PHP called (sat in between Apache and php sort of) but I made it too "strict" so to speak.
 
Back
Top