From ad955792a8ade5c0015aa3d68f86b1035c1acc31 Mon Sep 17 00:00:00 2001
From: Marius Cramer <m.cramer@pixcept.de>
Date: Wed, 22 Jan 2014 11:30:52 -0500
Subject: [PATCH] Fixed bug from previous commit

---
 server/mods-available/monitor_core_module.inc.php |  651 +++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 372 insertions(+), 279 deletions(-)

diff --git a/server/mods-available/monitor_core_module.inc.php b/server/mods-available/monitor_core_module.inc.php
index a336786..e83ccf7 100644
--- a/server/mods-available/monitor_core_module.inc.php
+++ b/server/mods-available/monitor_core_module.inc.php
@@ -35,8 +35,12 @@
 	var $class_name = 'monitor_core_module';
 	/* No actions at this time. maybe later... */
 	var $actions_available = array();
-	/** The Tools*/
+	/** The Tools */
+
+
 	private $_tools = null;
+	//** time the script was called
+	private $_run_time = null;
 
 	/**
 	 * This function is called during ispconfig installation to determine
@@ -53,10 +57,13 @@
 	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();
 		}
@@ -76,15 +83,17 @@
 	 */
 	// TODO: what monitoring is done should be a config-var
 	private function _doMonitor() {
+		global $app;
 		/*
 		 * We need the tools in almost every method, so initialize them once...
 		 */
 		$app->load('monitor_tools');
 		$this->_tools = new monitor_tools();
-		
+
 		/*
-		 * Calls the single Monitoring steps 
+		 * Calls the single Monitoring steps
 		 */
+		$this->_monitorEmailQuota();
 		$this->_monitorHDQuota();
 		$this->_monitorServer();
 		$this->_monitorOsVer();
@@ -110,7 +119,44 @@
 		$this->_monitorRaid();
 		$this->_monitorRkHunter();
 		$this->_monitorFail2ban();
+		$this->_monitorIPTables();
 		$this->_monitorSysLog();
+	}
+
+	private function _monitorEmailQuota() {
+		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();
+
+		/*
+         * 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 _monitorHDQuota() {
@@ -120,22 +166,22 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorHDQuota();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorServer() {
@@ -145,22 +191,22 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorServer();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorOsVer() {
@@ -170,22 +216,22 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorOsVer();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorIspcVer() {
@@ -195,22 +241,22 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorIspcVer();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorDiskUsage() {
@@ -220,22 +266,22 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorDiskUsage();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorMemUsage() {
@@ -244,22 +290,22 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorMemUsage();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorCpu() {
@@ -268,104 +314,104 @@
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorCpu();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorServices() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorServices();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorOpenVzHost() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorOpenVzHost();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorOpenVzUserBeancounter() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorOpenVzUserBeancounter();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorSystemUpdate() {
 		/*
 		 *  This monitoring is expensive, so do it only once an hour
 		 */
-		$min = date('i');
+		$min = @date('i', $this->_run_time);
 		if ($min != 0)
 			return;
 
@@ -373,378 +419,425 @@
 		 * OK - here we go...
 		 */
 		global $app;
-		
+
 		/*
 		 * 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) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorMailQueue() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorMailQueue();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorRaid() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorRaid();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorRkHunter() {
 		/*
 		 *  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;
 		/*
 		 * OK . here we go...
 		 */
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorRkHunter();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorFail2ban() {
 		global $app;
-		
+
 		/*
-		 * First we get the Monitoring-data from the tools
-		 */
+         * First we get the Monitoring-data from the tools
+         */
 		$res = $this->_tools->monitorFail2ban();
-		
+
 		/*
-		 * Insert the data into the database
-		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+         * 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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
+	}
+
+
+	private function _monitorIPTables() {
+		global $app;
+
+		/*
+		 * First we get the Monitoring-data from the tools
+		 */
+		$res = $this->_tools->monitorIPTables();
+
+		/*
+		 * 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 _monitorSysLog() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorSysLog();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorMailLog() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorMailLog();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorMailWarnLog() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorMailWarnLog();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorMailErrLog() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorMailErrLog();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorMessagesLog() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorMessagesLog();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorISPCCronLog() {
 		global $app;
-		
+
 		/*
 		 * 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) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorFreshClamLog() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorFreshClamLog();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorClamAvLog() {
-			global $app;
-		
+		global $app;
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorClamAvLog();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
 	private function _monitorIspConfigLog() {
 		global $app;
-		
+
 		/*
 		 * First we get the Monitoring-data from the tools
 		 */
 		$res = $this->_tools->monitorIspConfigLog();
-		
+
 		/*
 		 * Insert the data into the database
 		 */
-		$sql = 'INSERT INTO monitor_data (server_id, type, created, data, state) ' .
-				'VALUES (' .
-				$res['server_id'] . ', ' .
-				"'" . $app->dbmaster->quote($res['type']) . "', " .
-				time() . ', ' .
-				"'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
-				"'" . $res['state'] . "'" .
-				')';
+		$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($type, 4);
+		$this->_delOldRecords($res['type'], $res['server_id']);
 	}
 
-	/*
-	  Deletes Records older than n.
+	/**
+	 * Deletes Records older than 4 minutes.
+	 * The monitor writes new data every 5 minutes or longer (4 hour, 1 day).
+	 * So if i delete all Date older than 4 minutes i can be sure, that all old data
+	 * are deleted...
 	 */
-
-	private function _delOldRecords($type, $min, $hour=0, $days=0) {
+	private function _delOldRecords($type, $serverId) {
 		global $app;
 
-		$now = time();
-		$old = $now - ($min * 60) - ($hour * 60 * 60) - ($days * 24 * 60 * 60);
+		// $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
+		 * time not synced with the others (for example, all server has 11:00 and ONE server has
+		 * 10:45) then the actual data of this server (with the time-stamp 10:45) get lost
+		 * even though it is the NEWEST data of this server. To avoid this i HAVE to include
+		 * the server-id!
+		 */
 		$sql = 'DELETE FROM monitor_data ' .
-				'WHERE ' .
-				'type =' . "'" . $app->dbmaster->quote($type) . "' " .
-				'AND ' .
-				'created < ' . $old;
+			'WHERE ' .
+			'  type =' . "'" . $app->dbmaster->quote($type) . "' " .
+			'AND ' .
+			'  created < ' . $old . ' ' .
+			'AND ' .
+			'  server_id = ' . $serverId;
 		$app->dbmaster->query($sql);
 	}
 

--
Gitblit v1.9.1