HealthCheck.php 5.02 KB
Newer Older
voxdemonix's avatar
voxdemonix committed
1
<?php
2
/* SIMPLE HEALTH CHECK FOR LOAD BALANCER -- Version::1.0
voxdemonix's avatar
voxdemonix committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    Send http 204 if working
    Send http 501 if PHP version is bad
    Send http 502 if software (in $CONFIG["check_process_list"] list) is not running
    Send http 503 if Database fail or config file permission
    Send http 504 if Data directory fail
    Send http 505 if cache (redis) fail

Add the next line in your HaProxy Backend for accept only working webserver node
    http-check expect status 204

Infos : FR    https://wiki.0rion.netlib.re/doku.php?id=tutoriel_fr:tuto_howto_nextcloud_health_check_avec_haproxy
        USA   https://wiki.0rion.netlib.re/doku.php?id=tutorial_en:tuto_howto_nextcloud_health_check_with_haproxy
*/
/*  Edit these variables here or add them in to your nextcloud/config.php (better for can update this script without need to modify it)   */
/*
    $CONFIG["php_force_version_min"]=7.2;
    $CONFIG["php_force_version_max"]=7.2;
    $CONFIG["check_process_list"] = array(   "syncthing", "apache2"  ); //GNU/Linux Only ; uncomment and edit for check if some softwares (based on name) are running
*/

$pathConfFile='./config/config.php';
require($pathConfFile);

    /* check php server version */
if(!empty($CONFIG["php_force_version_min"])){
    if(version_compare(PHP_VERSION,$CONFIG["php_force_version_min"],'<')){
        header('HTTP/1.1 501'); exit;
    }
}
if(!empty($CONFIG["php_force_version_max"])){
    $CONFIG["php_force_version_max"] = $CONFIG["php_force_version_max"]+0.1; /* Maximal PHP version +1 */
    if(version_compare(PHP_VERSION,$CONFIG["php_force_version_max"],'>=')){
      header('HTTP/1.1 501'); exit;
  }
}

if (!empty($CONFIG["dbport"])){ $CONFIG["dbhost"]=$CONFIG["dbhost"].":".$CONFIG["dbport"]; }
try {
            /*  check database connexion    */
    $conn = new PDO($CONFIG["dbtype"].':host='.$CONFIG["dbhost"].';dbname='.$CONFIG["dbname"],$CONFIG["dbuser"],$CONFIG["dbpassword"],array(PDO::ATTR_PERSISTENT=>true));
    if(!$conn){
        header('HTTP/1.1 503'); exit;
    }
            /*  check conf file permission  */
    if(!confCheckPerm($pathConfFile, $CONFIG)){
        header('HTTP/1.1 503'); exit;
    }
                /*  check data folder  */
51
    if (!is_file($CONFIG["datadirectory"]."/index.html") || !is_writable($CONFIG["datadirectory"]."/") || !is_executable($CONFIG["datadirectory"]."/")){
voxdemonix's avatar
voxdemonix committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        header('HTTP/1.1 504'); exit;
    }
                /*  check cache    */
    if (!empty($CONFIG['redis']['host'])){
        if(!checkCacheRedis($CONFIG)){
            header('HTTP/1.1 505'); exit;
        }
    }
    if(!empty($CONFIG["memcached_servers"][0][0])){
        if(!checkCacheMemcached($CONFIG)){
            header('HTTP/1.1 505'); exit;
        }
    }
                /*  check process  */
    if (is_array($CONFIG["check_process_list"])) {
        if(!checkIfSoftwareIsRunning($CONFIG)){
            header('HTTP/1.1 502'); exit;
        }
    }
        /*  ALL GOOD & WORK, SEND HTTP 204      */
    header('HTTP/1.1 204'); exit;

}catch (PDOException $e){
        header('HTTP/1.1 503'); exit;
}

function checkIfSoftwareIsRunning($CONFIG){
    $path="/proc/";
    $subfile="cmdline";
        foreach($CONFIG["check_process_list"] as $softwareName){
            $check=false;
            $listFiles = scandir($path, 1);
            $lenght = count($listFiles);
            foreach ($listFiles as $filename) {
                if(!$check && is_dir($path.$filename)){
                    if(is_file($path.$filename.'/'.$subfile)){
                        if ($myFile = @fopen($path.$filename."/".$subfile, "r")) {
                            while (($buffer = fgets($myFile)) !== false) {
                                if(preg_match("/".$softwareName."/i", $buffer)){
                                    $check=true;
                                }
                            }
                            fclose($myFile);
                        }
                    }
                }
                $lenght--;
                if ($lenght==0 && $check==false) {  return false;  } // last tab
            }
        }
    return true;
}

function confCheckPerm($pathConfFile, $CONFIG){
    $chkPerm=false;
    if (is_writable($pathConfFile)){ $chkPerm=true; }
    if (isset($CONFIG["config_is_read_only"])){
        if (!$chkPerm && $CONFIG["config_is_read_only"]==true && is_readable($pathConfFile)){
            $chkPerm=true;
        }else{
            $chkPerm=false;
        }
    }
    return $chkPerm;  
}

function checkCacheRedis($CONFIG){
    if(empty($CONFIG['redis']['port'])){    $CONFIG['redis']['port'] = 6379;    }
    if ($cacheTest = fsockopen($CONFIG['redis']['host'], $CONFIG['redis']['port'])) {
        fclose($cacheTest);
        return true;
    }else{
        return false;
    }
}

function checkCacheMemcached($CONFIG){
    $cpt=count($CONFIG["memcached_servers"]);
    foreach ($CONFIG["memcached_servers"] as $srvInfo) {
        if(empty($srvInfo[1])){   $srvInfo[1]=11211;  }
        if(!memcache_connect($srvInfo[0], $srvInfo[1])){
            $cpt--;
        }
    }
    if($cpt){   return true;    }else{  return false;   }
}
?>