<?php

###############################################################################
###############################################################################
# PHP-Loadbalancing-Script
########################
# Requires:
# - PHP 4.x +
# - Apache with /server-status and "Extended Status on"
########################
# Copyleft by Bernd Moessinger (bernd@welcome2inter.net)

###############################################################################
###############################################################################

#Nur hier Aenderungen durchfuehren!!!

#Servername ohne http:// und abschliessenden / !!!
$wwwServerName[0] = "www01";
$wwwServerName[1] = "www02";

#This ServerIPs
$serverIp[0] = "192.168.1.2";
$serverIp[1] = "192.168.1.3";

#Wenn die Server down sind, dann soll die folgende Seite angezeigt werden.
$alternativ "down.php";
###############################################################################
###############################################################################

#Webserverport
$wwwServerPort 80;

#Nur die Domain und Tld
$domainTld explode(".",$_SERVER['SERVER_NAME']); 

if ((
$domainTld[0] == "gw01" || "www") && count($domainTld) > 2) {
    
$domainTld array_reverse($domainTld);
    
array_pop($domainTld);
    
$domainTld array_reverse($domainTld);
}


$numberOfDomainTld count($domainTld);

for (
$i=0$i<$numberOfDomainTld$i++) {
    
$point "";
    if (
$i 0) {
        
$point ".";
    }
    
    
$serverName $serverName $point $domainTld[$i] ;
}
                

$numerOfWwwServer count($wwwServerName);

$serverLoad "100";
$bestServer "";

$numberOfIp count($serverIp);

for (
$i=0$i<$numberOfIp$i++) {
    if (
$serverIp[$i] == $_SERVER['SERVER_NAME']) {
        exit;
    }
}

for (
$i=0$i<$numerOfWwwServer$i++) {
    
        
$wwwServer $wwwServerName[$i] . "." $serverName;
    
        
#Check if Apache ist working
    
$socket = @fsockopen($wwwServer$wwwServerPort);
        if(
$socket) {
        
fclose($socket);

            
#Read /server-status
        
$handle fopen ("http://$wwwServer/server-status?auto""r");
        while (!
feof($handle)) {
            
$buffer=fread($handle,4096);
        }
            
fclose($handle);

        
#Data from /server-status
            
list($th,$tb,$cpu,$uptime,$rs,$bs,$br,$busy,$idle,$scoreboard)= split ("\n"$buffer);
        
        
#Is CPU-Load existing?
        
if (preg_match("/CPULoad:/"$cpu)) {
     
            
#We only need the Value
            
$cpu ereg_replace(".*CPULoad: """$cpu);
        }
    
        
#If no value is given, it will be assumed 0
        
$cpu $cpu;

#Debugcode
#        echo $wwwServer . ": " . $cpu . "<br><br>";
    
            #Find the Server with lowest Load
            
if ($serverLoad $cpu) {
                
$serverLoad $cpu;
            
$bestServer $wwwServer;
        }
    }
}

#Debugcode
#echo "<br><br>Best Performer: " . $bestServer;

$url $_SERVER['REQUEST_URI'];

#Debugcode
#echo $url;

if ($bestServer != "") {
    
#Und ab geht es >>>
    
header("Location: http://$bestServer$url");
    exit;
}
else {
    
header("Location: http://$serverName/$alternativ");
}

?>