cfoe
2012-05-22 0ae667e6cfdd1bcc85bacf85cd93a84f99fa88f8
server/mods-available/rescue_core_module.inc.php
@@ -89,9 +89,14 @@
      $this->_rescueData = $this->_getRescueData();
      
      /*
       * rescue apache if needed
       * rescue mysql if needed (maybe httpd depends on mysql, so try this first!)
       */
      $this->_rescueApache();
      $this->_rescueMySql();
      /*
       * rescue httpd if needed
       */
      $this->_rescueHttpd();
      
      /*
       * The last step is to save the rescue-data
@@ -108,7 +113,7 @@
   private function _getMonitoringData() {
      global $app;
      
      $dataFilename = dirname(__FILE__) . "/../lib/rescue_module_monitoringdata.ser.txt";
      $dataFilename = dirname(__FILE__) . "/../temp/rescue_module_monitoringdata.ser.txt";
      
      /*
       * If the file containing the data is too old (older than 5 minutes) it is better to
@@ -156,8 +161,10 @@
      
      /*
       * We have the newest monitoring data. Save it!
       * (and protect it, because there may be sensible data in it)
       */
      file_put_contents($dataFilename, serialize($data));
      chmod($dataFilename, 0600);
                  
      /* Thats it */
      return $data;
@@ -170,7 +177,7 @@
    * so we do not have parallel access.
    */
   private function _getRescueData() {
      $dataFilename = dirname(__FILE__) . "/../lib/rescue_module_rescuedata.ser.txt";
      $dataFilename = dirname(__FILE__) . "/../temp/rescue_module_rescuedata.ser.txt";
      /*
       * If the file containing the data is too old (older than 5 minutes) it is better to
@@ -201,20 +208,25 @@
    * so we do not have parallel access.
    */
   private function _saveRescueData() {
      $dataFilename = dirname(__FILE__) . "/../lib/rescue_module_rescuedata.ser.txt";
      $dataFilename = dirname(__FILE__) . "/../temp/rescue_module_rescuedata.ser.txt";
      /*
       * We have the newest data. Save it!
       * (and protect it, because there may be sensible data in it)
       */
      file_put_contents($dataFilename, serialize($this->_rescueData));
      chmod($dataFilename, 0600);
   }
   /**
    * restarts apache, if needed
    * restarts httpd, if needed
    */
   private function _rescueApache(){
   private function _rescueHttpd(){
      global $app, $conf;
      
      /*
       * do nothing, if it is not allowed to rescue apache
       * do nothing, if it is not allowed to rescue httpd
       */
      if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_apache']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_apache']) == 'y')){
      if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_httpd']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_httpd']) == 'y')){
         return;
      }
      
@@ -255,23 +267,106 @@
      
      /* if 5 times will not work, we have to give up... */
      if ($tryCount > 5){
         $app->log('Apache is down! Rescue will not help!', LOGLEVEL_ERROR);
         $app->log('httpd is down! Rescue will not help!', LOGLEVEL_ERROR);
         return;
      }
      
      
      $app->log('Apache is down! Try rescue apache (try:' . $tryCount . ')...', LOGLEVEL_WARN);
//      echo 'Apache is down! Try rescue apache (try:' . $tryCount . ')...';
      $app->log('httpd is down! Try rescue httpd (try:' . $tryCount . ')...', LOGLEVEL_WARN);
      if($conf['serverconfig']['web']['server_type'] == 'nginx'){
         $daemon = 'nginx';
      } else {
         if(is_file($conf['init_scripts'] . '/' . 'httpd')) {
            $daemon = 'httpd';
         } elseif(is_file($conf['init_scripts'] . '/' . 'httpd2')){
            $daemon = 'httpd2';
         } else {
            $daemon = 'apache2';
         }
      }
      $this->_rescueDaemon($daemon);
   }
   /**
    * restarts mysql, if needed
    */
   private function _rescueMySql(){
      global $app, $conf;
      /*
       * do nothing, if it is not allowed to rescue mysql
       */
      if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_mysql']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_mysql']) == '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']['mysqlserver'] != 0){
         /* Clear the try counter, because we do not have to try to rescue the service */
         $this->_rescueData['mysqlserver']['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']['mysqlserver'])) ||
            ((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.
          * 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['mysqlserver']['try_counter']))? 1 : $this->_rescueData['mysqlserver']['try_counter'] + 1;
      /* Set the new try counter */
      $this->_rescueData['mysqlserver']['try_counter'] = $tryCount;
      /* if 5 times will not work, we have to give up... */
      if ($tryCount > 5){
         $app->log('MySQL is down! Rescue will not help!', LOGLEVEL_ERROR);
         return;
      }
      $app->log('MySQL is down! Try rescue mysql (try:' . $tryCount . ')...', LOGLEVEL_WARN);
      if(is_file($conf['init_scripts'] . '/' . 'mysqld')) {
         $daemon = 'mysqld';
      } else {
         $daemon = 'mysql';
      }
      $this->_rescueDaemon($daemon);
   }
   /**
    * Tries to stop and then restart the daemon
    *
    * @param type $daemon the name of the daemon
    */
   private function _rescueDaemon($daemon){
      global $conf;
      // if you need to find all restarts search for "['init_scripts']"
      /*
       * First we stop the running service "normally"
       */
      $daemon = '';
      if(is_file($conf['init_scripts'] . '/' . 'httpd')) {
         $daemon = 'httpd';
      } else {
         $daemon = 'apache2';
      }
      
      /*
       * ATTENTION!
@@ -292,7 +387,5 @@
       */
      exec($conf['init_scripts'] . '/' . $daemon . ' start');
   }
// if you need to find all restarts search for "['init_scripts']"
}
?>