Saturday, June 21, 2008

ESX LUN Load balance

After having fighting my way through the Virtual Client setting the active and preferred path for all my currently 22 Lun's on my 24 ESX servers to load balance the load over as many HBA's as possible i looked at the script i could find on the net and saw that the scripts all set the paths to the same for all hosts.
 
I was looking at different scripts out there and found the one below. I need an idea on how to randomize some of this because otherwise it will always be the same om all hosts and if there are 4 hba' as in my case the fourth one will newer be used. I need to set the counter to some random number between 1 and the PATHS value for the hba assignment to start somwhere..   :
#! /bin/bash
#
# by Allan Christiansen/2008 on esx 3.5.0 Update 1
#
for PATHS in 2 4 6 8 10 12 14 16
do
   STPATHS=${PATHS}
   COUNTER=${RANDOM}
   while [[ ${COUNTER} -gt ${PATHS} ]];
   do
      COUNT=`expr ${COUNTER} / ${PATHS}`
      COUNTER=${COUNT}
   done
   for LUN in $(esxcfg-mpath -l | grep "Disk" | grep "has ${STPATHS} paths" | awk '{print $2}')
   do
      esxcfg-mpath --lun=${LUN} --path=$(esxcfg-mpath -q --lun=${LUN} | grep FC | awk '{print $4}' | awk '{print NR "S\t " $0}' | grep ${COUNTER}S | awk '{print $2}') --preferred
      COUNT=`expr ${COUNTER} + 1`
      COUNTER=${COUNT}
      if [[ ${COUNTER} -gt ${STPATHS} ]]
      then
         COUNTER="1"
      fi
   done
done