From 2d2fd172e1548dd24e1719accd0b856cff6a31a0 Mon Sep 17 00:00:00 2001
From: Falko Timme <ft@falkotimme.com>
Date: Fri, 18 Oct 2013 12:20:13 -0400
Subject: [PATCH] - Added funtion to convert currency formatted numbers back to floating numbers. - Improved getSearchSQL() function so that users can use their native date format so search for dates in lists.

---
 server/mods-available/monitor_core_module.inc.php |  120 +++++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 86 insertions(+), 34 deletions(-)

diff --git a/server/mods-available/monitor_core_module.inc.php b/server/mods-available/monitor_core_module.inc.php
index 4294e54..2e66d64 100644
--- a/server/mods-available/monitor_core_module.inc.php
+++ b/server/mods-available/monitor_core_module.inc.php
@@ -37,6 +37,8 @@
 	var $actions_available = array();
 	/** The Tools */
 	private $_tools = null;
+    //** time the script was called
+    private $_run_time = null;
 
 	/**
 	 * This function is called during ispconfig installation to determine
@@ -52,11 +54,14 @@
 	 */
 	public function onLoad() {
 		global $app;
-
+        
+        //* store the running time
+        $this->_run_time = time();
+        
 		/*
 		 * Do the monitor every n minutes and write the result to the db
 		 */
-		$min = @date('i');
+		$min = @date('i', $this->_run_time);
 		if (($min % $this->interval) == 0) {
 			$this->_doMonitor();
 		}
@@ -112,13 +117,25 @@
 		$this->_monitorRaid();
 		$this->_monitorRkHunter();
 		$this->_monitorFail2ban();
+		$this->_monitorIPTables();
 		$this->_monitorSysLog();
 	}
 
     private function _monitorEmailQuota() {
-        global $app;
+        global $app, $conf;
 
         /*
+		 *  This monitoring is expensive, so do it only every 15 minutes
+		 */
+		$min = @date('i', $this->_run_time);
+		if ($min % 15 != 0) return;
+		
+		$app->uses('getconf');
+		$mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
+		if($mail_config['mailbox_quota_stats'] == 'n') return;
+		
+		
+		/*
          * First we get the Monitoring-data from the tools
          */
         $res = $this->_tools->monitorEmailQuota();
@@ -126,7 +143,7 @@
         /*
          * Insert the data into the database
          */
-        $sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+        $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
                 'VALUES (' .
                 $res['server_id'] . ', ' .
                 "'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -151,7 +168,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -176,7 +193,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -201,7 +218,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -226,7 +243,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -251,7 +268,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -275,7 +292,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -299,7 +316,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -324,7 +341,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -349,7 +366,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -374,7 +391,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -392,7 +409,7 @@
 		/*
 		 *  This monitoring is expensive, so do it only once an hour
 		 */
-		$min = @date('i');
+		$min = @date('i', $this->_run_time);
 		if ($min != 0)
 			return;
 
@@ -405,11 +422,15 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorSystemUpdate();
+		
+		//* Ensure that output is encoded so that it does not break the serialize
+		//$res['data']['output'] = htmlentities($res['data']['output']);
+		$res['data']['output'] = htmlentities($res['data']['output'],ENT_QUOTES,'UTF-8');
 
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -434,7 +455,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -459,7 +480,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -477,8 +498,8 @@
 		/*
 		 *  This monitoring is expensive, so do it only once a day
 		 */
-		$min = @date('i');
-		$hour = @date('H');
+		$min = @date('i', $this->_run_time);
+		$hour = @date('H', $this->_run_time);
 		if (!($min == 0 && $hour == 23))
 			return;
 		/*
@@ -494,7 +515,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -509,17 +530,43 @@
 	}
 
 	private function _monitorFail2ban() {
+        global $app;
+
+        /*
+         * First we get the Monitoring-data from the tools
+         */
+        $res = $this->_tools->monitorFail2ban();
+
+        /*
+         * Insert the data into the database
+         */
+        $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
+                'VALUES (' .
+                $res['server_id'] . ', ' .
+                "'" . $app->dbmaster->quote($res['type']) . "', " .
+                'UNIX_TIMESTAMP(), ' .
+                "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
+                "'" . $res['state'] . "'" .
+                ')';
+        $app->dbmaster->query($sql);
+
+        /* The new data is written, now we can delete the old one */
+        $this->_delOldRecords($res['type'], $res['server_id']);
+    }
+
+
+	private function _monitorIPTables() {
 		global $app;
 
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
-		$res = $this->_tools->monitorFail2ban();
+		$res = $this->_tools->monitorIPTables();
 
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -544,7 +591,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -569,7 +616,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -594,7 +641,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -619,7 +666,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -644,7 +691,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -665,11 +712,14 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorISPCCronLog();
+		
+		//* Ensure that output is encoded so that it does not break the serialize
+		if(is_array($res) && isset($res['data'])) $res['data'] = htmlentities($res['data']);
 
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -694,7 +744,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -719,7 +769,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -744,7 +794,7 @@
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
+		$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
 				'VALUES (' .
 				$res['server_id'] . ', ' .
 				"'" . $app->dbmaster->quote($res['type']) . "', " .
@@ -767,8 +817,10 @@
 	private function _delOldRecords($type, $serverId) {
 		global $app;
 
-		$now = time();
-		$old = $now - (4 * 60); // 4 minutes
+		// $now = time();
+		// $old = $now - (4 * 60); // 4 minutes
+		$old = 'UNIX_TIMESTAMP() - 240';
+		
 		/*
 		 * ATTENTION if i do NOT pay attention of the server id, i delete all data (of the type)
 		 * of ALL servers. This means, if i have a multiserver-environment and a server has a 

--
Gitblit v1.9.1