"15", # new zoozle mirror # "85.214.66.15" => "5.94" , # fishwarez.com server "85.214.59.105" => "5", # old zoozle server # "85.214.77.110" => "5.95", # alleonlineshops server # mirror => maxload ); $MirrorFile = "/home/wwwroot/mirror"; $MirrorOld = "/home/wwwroot/mirror.old"; $FileSizeMirror = filesize($MirrorFile); $FileSizeMirrorOld = filesize($MirrorOld); $IsAllowedToUseHard = IsCurrentLoadHigher( LOADWHENHARDLOADBALANCINGISUSED ); $IsAllowedToUseSoft = IsCurrentLoadHigher( LOADWHENSOFTLOADBALANCINGISUSED ); # wenn statische mirror datei gültig, dann nehme diese datei if ( $FileSizeMirror > 0 && $FileSizeMirror >= $FileSizeMirrorOld ){ ########### ### benutze die werte aus der originalen datei mit den loadwerten der mirrors ### sortiere diese werte und benutze anschließend den mirror mit dem geringsten load ### anschließend entscheide, ob hard oder soft load balancing durchgeführt wird ########### # echo "DEBUG: Using org static mirror file\n"; $FileContent = file($MirrorFile); # softiere aufsteigend von klein nach groß sort($FileContent); # nehme immer den ersten treffer, denn er wurde sortiert list ($load, $MirrorIP) = explode(' ', $FileContent[0] ); $MirrorIP = rtrim($MirrorIP); $CurrentMirrorURI = "http://$MirrorIP" . $_SERVER["REQUEST_URI"]; # wenn der aktuelle mirror load kleiner als der max erlaubte mirror load ist, dann führe loadbalancing durch if ( $load < $ZoozleMirrors[$MirrorIP] && strlen($ZoozleMirrors[$MirrorIP]) > 0 ) { # echo "DEBUG: CurrentMirrorLoad: '$load' und MaxMirrorLoad: $ZoozleMirrors[$MirrorIP] '$MirrorIP'\n"; if ( $IsAllowedToUseHard == 1) { _doHardLoadBalancing($CurrentMirrorURI); exit(0); } elseif ( $IsAllowedToUseSoft == 1) { _doSoftLoadBalancing($CurrentMirrorURI); exit(0); }; # if ( $IsAllowedToUseHard == 1) { }; # if ( $load < $ZoozleMirrors[$MirrorIP] ) { } elseif ( $FileSizeMirrorOld > 0 && $FileSizeMirrorOld >= $FileSizeMirror ){ ########### ### benutze die werte aus der backup datei mit den loadwerten der mirrors ### sortiere diese werte und benutze anschließend den mirror mit dem geringsten load ### anschließend entscheide, ob hard oder soft load balancing durchgeführt wird ########### # echo "DEBUG: Using bak static mirror file\n"; $FileContent = file($MirrorOld); # softiere aufsteigend von klein nach groß sort($FileContent); # nehme immer den ersten treffer, denn er wurde sortiert list ($load, $MirrorIP) = explode(' ', $FileContent[0] ); $MirrorIP = rtrim($MirrorIP); $CurrentMirrorURI = "http://$MirrorIP" . $_SERVER["REQUEST_URI"]; # wenn der aktuelle mirror load kleiner als der max erlaubte mirror load ist, dann führe loadbalancing durch if ( $load < $ZoozleMirrors[$MirrorIP] && strlen($ZoozleMirrors[$MirrorIP]) > 0 ) { # echo "DEBUG: CurrentMirrorLoad: '$load' und MaxMirrorLoad: $ZoozleMirrors[$MirrorIP] '$MirrorIP'\n"; if ( $IsAllowedToUseHard == 1) { _doHardLoadBalancing($CurrentMirrorURI); exit(0); } elseif ( $IsAllowedToUseSoft == 1) { _doSoftLoadBalancing($CurrentMirrorURI); exit(0); }; # if ( $IsAllowedToUseHard == 1) { }; # if ( $load < $ZoozleMirrors[$MirrorIP] ) { } else { ############# ### wenn es einer der mirror dateien nicht gelesen worden konnte, dann benutze die alte version, um den load abzufragen etc ### diese version hat sich als extrem langsam herrausgestellt ############ ############################ ### hartes loadblancing: leite via header 307(temporary) redirect auf den mirror weiter ############################ if ( $IsAllowedToUseHard == 1) { while (list($MirrorIP,$MaxMirrorLoad) = each ($ZoozleMirrors)) { $obj = new HTTPRequest("http://$MirrorIP/load.txt"); $MirrorLoad = $obj->DownloadToString(); # echo "[$MirrorIP] MirrorLoad $MirrorLoad und MaxMirrorLoad: $MaxMirrorLoad \n"; if ( $MirrorLoad < $MaxMirrorLoad ) { $CurrentMirrorURI = "http://$MirrorIP" . $_SERVER["REQUEST_URI"]; _doHardLoadBalancing($CurrentMirrorURI); exit(0); }; # if ( $MirrorLoad < $MaxMirrorLoad ) { }; # while (list($MaxMirrorLoad, $Mirror) = each ($ZoozleMirrors)) { }; # if ( $IsAllowed == 1) { ############################ ### softes loadblancing: hole seite vom mirror und zeige sie an - dies scheint mir sehr arbeitsintensiv, darum diese art erstmal nicht nutzen ############################ if ( $IsAllowedToUseSoft == 1) { while (list($MirrorIP,$MaxMirrorLoad) = each ($ZoozleMirrors)) { $obj = new HTTPRequest("http://$MirrorIP/load.txt"); $MirrorLoad = $obj->DownloadToString(); # echo "[$MirrorIP] MirrorLoad $MirrorLoad und MaxMirrorLoad: $MaxMirrorLoad \n"; if ( $MirrorLoad < $MaxMirrorLoad ) { $CurrentMirrorURI = "http://$MirrorIP" . $_SERVER["REQUEST_URI"]; _doSoftLoadBalancing($CurrentMirrorURI); exit(0); }; # if ( $MirrorLoad < $MaxMirrorLoad ) { }; # while (list($MaxMirrorLoad, $Mirror) = each ($ZoozleMirrors)) { }; # if ( $IsAllowedToUseSoft == 1) { }; # if ( $FileSizeMirror > 0 ){ return 1; }; # function SoftLoadBalancing(){ function _doSoftLoadBalancing( $MirrorUrl ){ /* # debug: echo "_doSoftLoadBalancing( $MirrorUrl )\n"; exit; */ $rh = fopen($MirrorUrl, 'r'); socket_set_timeout($rh, 4); while (!feof($rh)) { $ResultPage .= fread($rh, 1024); }; # while (!feof($rh)) { fclose($rh); if ( strlen($ResultPage) >= 5000 ) { header("Content-type: text/html"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: public"); // HTTP/1.1 header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: use-cache"); echo $ResultPage; exit(0); }; # if ( strlen($ResultPage) >= 5000 ) { }; # function _doSoftLoadBalancing( $MirrorUrl ){ function _doHardLoadBalancing( $MirrorUrl ){ /* # debug: echo "_doHardLoadBalancing( $MirrorUrl )\n"; exit; */ header("HTTP/1.1 307 Temporary Redirect"); header ("Location: $MirrorUrl"); exit(0); }; # function _doHardLoadBalancing( $MirrorUrl ){ function IsCurrentLoadHigher( $GivenLoad ){ $LOAD_HANDLE = fopen("/proc/loadavg","r"); $get_load_info = fgets($LOAD_HANDLE, 100); fclose($LOAD_HANDLE); $LOAD = explode(" ", $get_load_info); $current_load = $LOAD[0]; if ( $current_load >= $GivenLoad ) { return 1; } else { return 0; }; }; # function GetCurrentLoad(){ function GetCurrentLoad(){ $LOAD_HANDLE = fopen("/proc/loadavg","r"); $get_load_info = fgets($LOAD_HANDLE, 100); fclose($LOAD_HANDLE); $LOAD = explode(" ", $get_load_info); $current_load = $LOAD[0]; return $current_load; }; # function GetCurrentLoad(){ /* $rh = fopen($CurrentMirrorURI, 'r'); while (!feof($rh)) { echo fread($rh, 4024); }; # while (!feof($rh)) { fclose($rh); */ ?>