/**
  * Execute checks for a single database server
  * @param string $databaseName Database name to use for connection
  * @param LoadBalancer $loadBalancer Load Balancer instance for the given cluster
  * @param int $index Server index to test
  * @return bool Is server healthy?
  * @throws MWException
  */
 private function testHost($databaseName, LoadBalancer $loadBalancer, $index)
 {
     $serverInfo = $loadBalancer->getServerInfo($index);
     $master = $index == 0;
     // connection check
     try {
         $db = wfGetDB($index, array(), $databaseName);
     } catch (DBError $e) {
         $this->addError("could not connect to server: " . $e->getMessage());
         return false;
     }
     // lag check
     if (!$master && isset($serverInfo['max lag'])) {
         try {
             $maxLag = $serverInfo['max lag'];
             $lag = $db->getLag();
             if ($lag > $maxLag) {
                 $this->addError("lag (%d) is greater than configured \"max lag\" (%d)", $lag, $maxLag);
                 $db->close();
                 return false;
             }
         } catch (DBError $e) {
             $this->addError("could not fetch lag time");
             $db->close();
             return false;
         }
     }
     // read_only check on master
     try {
         $res = $db->query("SHOW VARIABLES LIKE 'read_only';");
         $row = $res->fetchRow();
         $res->free();
         $readWrite = $row['Value'] != 'ON';
         if ($master && !$readWrite) {
             $this->addMessage("read_only is set on master host");
         } else {
             if (!$master && $readWrite) {
                 $this->addMessage("read_only is unset on slave host");
             }
         }
     } catch (DBError $e) {
         $this->addError("could not check read_only flag");
         $db->close();
         return false;
     }
     $db->close();
     return true;
 }