Marius Cramer
2014-01-22 ad955792a8ade5c0015aa3d68f86b1035c1acc31
server/mods-available/rescue_core_module.inc.php
@@ -38,9 +38,13 @@
    * The monitoring-Data of this module.
    * [0] are the actual data, [1] are the data 1 minnute ago [2] are teh data 2 minuntes...
    */
   private $_monitoringData = array();
   /** The rescue-Data of this module. */
   private $_rescueData = array();
   /**
    *  This function is called during ispconfig installation to determine
@@ -50,12 +54,14 @@
      return true;
   }
   /**
    * This function is called when the module is loaded
    */
   function onLoad() {
      $this->_doRescue();
   }
   /**
    * This function is called when a change in one of the registered tables is detected.
@@ -74,7 +80,7 @@
       */
      global $conf;
      if ((!isset($conf['serverconfig']['rescue']['try_rescue'])) ||
            ((isset($conf['serverconfig']['rescue']['try_rescue'])) && ($conf['serverconfig']['rescue']['try_rescue'] !='y'))){
         ((isset($conf['serverconfig']['rescue']['try_rescue'])) && ($conf['serverconfig']['rescue']['try_rescue'] !='y'))){
         return;
      }
@@ -87,11 +93,6 @@
       * Next we get the rescue data needed for rescuing the system
       */
      $this->_rescueData = $this->_getRescueData();
      /*
       * rescue MongoDB if needed
       */
      $this->_rescueMongoDB();
      /*
       * rescue mysql if needed (maybe httpd depends on mysql, so try this first!)
@@ -175,6 +176,8 @@
      return $data;
   }
   /**
    * This gets the rescue-Data, needed for rescuing the system.
    * Because we can not be 100% sure, that the mysql-DB is up and running, so we use the
@@ -250,7 +253,7 @@
       * This means, we check the data 1 minute ago
       */
      if ((!isset($this->_monitoringData[1][0]['data']['webserver'])) ||
            ((isset($this->_monitoringData[1][0]['data']['webserver'])) && ($this->_monitoringData[1][0]['data']['webserver'] != 0))){
         ((isset($this->_monitoringData[1][0]['data']['webserver'])) && ($this->_monitoringData[1][0]['data']['webserver'] != 0))){
         /*
          * We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
          * This means, it could be, that the admin is restarting the server.
@@ -294,71 +297,7 @@
      $this->_rescueDaemon($daemon);
   }
   /**
    * restarts MongoDB, if needed
    */
   private function _rescueMongoDB(){
      global $app, $conf;
      /*
       * do nothing, if it is not allowed to rescue mysql
       */
      if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_mongodb']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_mongodb']) == 'y')){
         return;
      }
      /*
       * if the service is up and running, or the service is not installed there is nothing to do...
       */
      if ($this->_monitoringData[0][0]['data']['mongodbserver'] != 0){
         /* Clear the try counter, because we do not have to try to rescue the service */
         $this->_rescueData['mongodbserver']['try_counter'] = 0;
         return;
      }
      /*
       * OK, the service is installed and down.
       * Maybe this is because of a restart of the service by the admin.
       * This means, we check the data 1 minute ago
       */
      if ((!isset($this->_monitoringData[1][0]['data']['mongodbserver'])) ||
            ((isset($this->_monitoringData[1][0]['data']['mongodbserver'])) && ($this->_monitoringData[1][0]['data']['mongodbserver'] != 0))){
         /*
          * We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
          * This means, it could be, that the admin is restarting the server.
          * We wait one more minute...
          */
         return;
      }
      /*#####
       * The service is down and it was down 1 minute ago.
       * We try to rescue it
       *#####*/
      /* Get the try counter */
      $tryCount = (!isset($this->_rescueData['mongodbserver']['try_counter']))? 1 : $this->_rescueData['mongodbserver']['try_counter'] + 1;
      /* Set the new try counter */
      $this->_rescueData['mongodbserver']['try_counter'] = $tryCount;
      /* if 5 times will not work, we have to give up... */
      if ($tryCount > 5){
         $app->log('MongoDB is down! Rescue will not help!', LOGLEVEL_ERROR);
         return;
      }
      $app->log('MongoDB is down! Try rescue MongoDB (try:' . $tryCount . ')...', LOGLEVEL_WARN);
      if(is_file($conf['init_scripts'] . '/' . 'mongodb')) {
         $daemon = 'mongodb';
      } else {
         $daemon = 'mongodb';
      }
      $this->_rescueDaemon($daemon);
   }
   /**
    * restarts mysql, if needed
@@ -388,7 +327,7 @@
       * This means, we check the data 1 minute ago
       */
      if ((!isset($this->_monitoringData[1][0]['data']['mysqlserver'])) ||
            ((isset($this->_monitoringData[1][0]['data']['mysqlserver'])) && ($this->_monitoringData[1][0]['data']['mysqlserver'] != 0))){
         ((isset($this->_monitoringData[1][0]['data']['mysqlserver'])) && ($this->_monitoringData[1][0]['data']['mysqlserver'] != 0))){
         /*
          * We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
          * This means, it could be, that the admin is restarting the server.
@@ -434,6 +373,7 @@
   private function _rescueDaemon($daemon){
      global $conf;
      $app->uses('system');
      // if you need to find all restarts search for "['init_scripts']"
      /*
       * First we stop the running service "normally"
@@ -445,7 +385,7 @@
       * So we have to try to stop but if this will not work, we have to kill the stopping
       * of the service
       */
      exec($conf['init_scripts'] . '/' . $daemon . ' stop && (sleep 3; kill $!; sleep 2; kill -9 $!) &> /dev/null');
      exec($app->system->getinitcommand($daemon, 'stop').' && (sleep 3; kill $!; sleep 2; kill -9 $!) &> /dev/null');
      /*
       * OK, we tryed to stop it normally, maybe this worked maybe not. So we have to look
@@ -456,7 +396,9 @@
      /*
       * There are no more zombies left. Lets start the service..
       */
      exec($conf['init_scripts'] . '/' . $daemon . ' start');
      exec($app->system->getinitcommand($daemon, 'start'));
   }
}
?>