From 7fe908c50c8dbc5cc05f571dbe11d66141caacd4 Mon Sep 17 00:00:00 2001
From: Marius Cramer <m.cramer@pixcept.de>
Date: Thu, 14 Nov 2013 09:01:22 -0500
Subject: [PATCH] Cleaning up code to match coding guidelines

---
 server/mods-available/rescue_core_module.inc.php |  122 ++++++++++++++++++++++------------------
 1 files changed, 67 insertions(+), 55 deletions(-)

diff --git a/server/mods-available/rescue_core_module.inc.php b/server/mods-available/rescue_core_module.inc.php
index 15abd54..a3a5ad7 100644
--- a/server/mods-available/rescue_core_module.inc.php
+++ b/server/mods-available/rescue_core_module.inc.php
@@ -35,12 +35,16 @@
 	/* No actions at this time. maybe later... */
 	var $actions_available = array();
 	/**
-	 * The monitoring-Data of this module. 
+	 * 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.
@@ -73,8 +79,8 @@
 		 * do nothing, if the rescue-system is not enabled
 		 */
 		global $conf;
-		if ((!isset($conf['serverconfig']['rescue']['try_rescue'])) || 
-				((isset($conf['serverconfig']['rescue']['try_rescue'])) && ($conf['serverconfig']['rescue']['try_rescue'] !='y'))){
+		if ((!isset($conf['serverconfig']['rescue']['try_rescue'])) ||
+			((isset($conf['serverconfig']['rescue']['try_rescue'])) && ($conf['serverconfig']['rescue']['try_rescue'] !='y'))){
 			return;
 		}
 
@@ -87,17 +93,17 @@
 		 * Next we get the rescue data needed for rescuing the system
 		 */
 		$this->_rescueData = $this->_getRescueData();
-		
+
 		/*
 		 * rescue mysql if needed (maybe httpd depends on mysql, so try this first!)
 		 */
 		$this->_rescueMySql();
-		
+
 		/*
 		 * rescue httpd if needed
 		 */
 		$this->_rescueHttpd();
-		
+
 		/*
 		 * The last step is to save the rescue-data
 		 */
@@ -112,12 +118,12 @@
 	 */
 	private function _getMonitoringData() {
 		global $app;
-		
+
 		$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
-		 * delete it, because it could be, that the server was down for some times and so the data 
+		 * delete it, because it could be, that the server was down for some times and so the data
 		 * are outdated
 		 */
 		if (file_exists($dataFilename) && (filemtime($dataFilename) < (time() - 5 * 60))) {
@@ -132,7 +138,7 @@
 		} else {
 			$data = array();
 		}
-		
+
 		/*
 		 * $temp[0] was the data of the last monitoring (means 1 minute ago), $temp[1] is the data
 		 * 2 minutes ago and so on. Now we have make place for the newest data...
@@ -144,32 +150,34 @@
 		for ($i = $max; $i > 0; $i--){
 			$data[$i] = $data[$i -1];
 		}
-		
+
 		/*
 		 * we need the monitoring tools
 		 */
 		$app->load('monitor_tools');
 		$tools = new monitor_tools();
-		
+
 		/*
 		 * Call the needed Monitoring-step and get the data
 		 */
 		$tmp[0] = $tools->monitorServices();
-		
+
 		/* Add the data at the FIRST position of the history */
 		$data[0] = $tmp;
-		
+
 		/*
 		 * 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;
 	}
-	
+
+
+
 	/**
 	 * 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
@@ -181,7 +189,7 @@
 
 		/*
 		 * If the file containing the data is too old (older than 5 minutes) it is better to
-		 * delete it, because it could be, that the server was down for some times and so the data 
+		 * delete it, because it could be, that the server was down for some times and so the data
 		 * are outdated
 		 */
 		if (file_exists($dataFilename) && (filemtime($dataFilename) < (time() - 5 * 60))) {
@@ -196,7 +204,7 @@
 		} else {
 			$data = array();
 		}
-		
+
 		/* Thats it */
 		return $data;
 	}
@@ -222,14 +230,14 @@
 	 */
 	private function _rescueHttpd(){
 		global $app, $conf;
-		
+
 		/*
 		 * do nothing, if it is not allowed to rescue httpd
 		 */
 		if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_httpd']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_httpd']) == 'y')){
 			return;
 		}
-		
+
 		/*
 		 * if the service is up and running, or the service is not installed there is nothing to do...
 		 */
@@ -238,42 +246,42 @@
 			$this->_rescueData['webserver']['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']['webserver'])) || 
-				((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. 
+		if ((!isset($this->_monitoringData[1][0]['data']['webserver'])) ||
+			((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.
 			 * 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['webserver']['try_counter']))? 1 : $this->_rescueData['webserver']['try_counter'] + 1;
-		
+
 		/* Set the new try counter */
 		$this->_rescueData['webserver']['try_counter'] = $tryCount;
-		
+
 		/* if 5 times will not work, we have to give up... */
 		if ($tryCount > 5){
 			$app->log('httpd is down! Rescue will not help!', LOGLEVEL_ERROR);
 			return;
 		}
-		
-		
+
+
 		$app->log('httpd is down! Try rescue httpd (try:' . $tryCount . ')...', LOGLEVEL_WARN);
-		
+
 		if($conf['serverconfig']['web']['server_type'] == 'nginx'){
 			$daemon = 'nginx';
 		} else {
@@ -285,23 +293,25 @@
 				$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...
 		 */
@@ -310,40 +320,40 @@
 			$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. 
+		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')) {
@@ -351,24 +361,24 @@
 		} 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;
-		
+
 		$app->uses('system');
 		// if you need to find all restarts search for "['init_scripts']"
 		/*
 		 * First we stop the running service "normally"
 		 */
-		
+
 		/*
 		 * ATTENTION!
 		 * The service hangs. this means it could be, that "stop" will hang also.
@@ -376,17 +386,19 @@
 		 * of the service
 		 */
 		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
 		 * if the service is already running or not. If so, we have to kill them hard
 		 */
 		exec("kill -9 `ps -A | grep " . $daemon . "| grep -v grep | awk '{print $1}'` &> /dev/null");
-		
+
 		/*
 		 * There are no more zombies left. Lets start the service..
 		 */
 		exec($app->system->getinitcommand($daemon, 'start'));
 	}
+
 }
+
 ?>

--
Gitblit v1.9.1