Site icon David Ramsden

loadbalancer.org Linux Feedback Agent

I’ve been working with some loadbalancer.org appliances recently, load balancing traffic over MySQL and Apache servers running Linux. The load balancer supports a feedback agent where it can query the real server to gauge how utilised it is based on, for example, CPU load and then distribute the request to the real server that should perform the best.

Over on the loadbalancer.org blog is an article about the feedback agent and how to implement it for Linux servers. The shell script suggested is:

#!/bin/bash
LOAD=`/usr/bin/vmstat 1 2| /usr/bin/tail -1| /usr/bin/awk '{print $15;}' | /usr/bin/tee`
echo "$LOAD%"
#This outputs a 1 second average CPU idle

Although this does give you a 1 second average CPU, it’s a bit too accurate and doesn’t give much headroom. If the CPU suddenly spiked very quickly and then returned to normal the load balancer wouldn’t know this. And indeed watching the real server weighting change on the load balancer v’s what top is reporting confirms this. The weighting on a real server can drastically jump up and down.

A better feedback agent script is:

#!/bin/bash
#

# Check CPU idle every 1 second, NUM_CHECKS times.
# Calculate average idle time from this.
NUM_CHECKS=3
cpu_idle=($( vmstat 1 $NUM_CHECKS | tail -$NUM_CHECKS | awk '{print $15};' ))

idle=0
for i in "${cpu_idle[@]}"
do
        idle=$(( $idle+$i ))
done
idle=$(( $idle / $NUM_CHECKS ))

echo $idle
exit 0

This will take a 3 second average reading and then report back the overall average. This prevents the real server weighting on the load balancer from fluctuating so much. Change the NUM_CHECKS variable to take more or less readings as required.