From e1ceb050e19c7574bca146a8da7047ee4ff456b5 Mon Sep 17 00:00:00 2001
From: Marius Burkard <m.burkard@pixcept.de>
Date: Sun, 10 Jul 2016 05:02:35 -0400
Subject: [PATCH] Merge branch 'stable-3.1'

---
 server/lib/classes/monitor_tools.inc.php |  751 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 444 insertions(+), 307 deletions(-)

diff --git a/server/lib/classes/monitor_tools.inc.php b/server/lib/classes/monitor_tools.inc.php
index 42ef73f..9c39fc2 100644
--- a/server/lib/classes/monitor_tools.inc.php
+++ b/server/lib/classes/monitor_tools.inc.php
@@ -35,207 +35,292 @@
 	//   This is the same code as in install/lib/install.lib.php
 	//   So if you change it here, you also have to change it in there!
 	//   Please do not forget to remove the swriteln(); - lines here at this file
-	public function get_distname() {
+	function get_distname() {
 
-		$distname = '';
-		$distver = '';
-		$distid = '';
-		$distbaseid = '';
+	$distname = '';
+	$distver = '';
+	$distid = '';
+	$distbaseid = '';
 
-		//** Debian or Ubuntu
-		if (file_exists('/etc/debian_version')) {
-			if (strstr(trim(file_get_contents('/etc/issue')), 'Ubuntu')) {
-				if (strstr(trim(file_get_contents('/etc/issue')), 'LTS')) {
+	//** Debian or Ubuntu
+	if(file_exists('/etc/debian_version')) {
+		
+		// Check if this is Ubuntu and not Debian
+		if (strstr(trim(file_get_contents('/etc/issue')), 'Ubuntu') || (is_file('/etc/os-release') && stristr(file_get_contents('/etc/os-release'), 'Ubuntu'))) {
+			
+			$issue = file_get_contents('/etc/issue');
+			
+			// Use content of /etc/issue file
+			if(strstr($issue,'Ubuntu')) {
+				if (strstr(trim($issue), 'LTS')) {
 					$lts=" LTS";
 				} else {
 					$lts="";
 				}
 
-				$issue=file_get_contents('/etc/issue');
 				$distname = 'Ubuntu';
 				$distid = 'debian40';
 				$distbaseid = 'debian';
-				$ver = explode(' ',$issue);
+				$ver = explode(' ', $issue);
 				$ver = array_filter($ver);
 				$ver = next($ver);
-				$mainver = explode('.',$ver);
+				$mainver = explode('.', $ver);
 				$mainver = array_filter($mainver);
 				$mainver = current($mainver).'.'.next($mainver);
-				switch ($mainver){
-				case "12.10":
-					$relname = "(Quantal Quetzal)";
+			// Use content of /etc/os-release file
+			} else {
+				$os_release = file_get_contents('/etc/os-release');
+				if (strstr(trim($os_release), 'LTS')) {
+					$lts = " LTS";
+				} else {
+					$lts = "";
+				}
+				
+				$distname = 'Ubuntu';
+				$distid = 'debian40';
+				$distbaseid = 'debian';
+
+				preg_match("/.*VERSION=\"(.*)\".*/ui", $os_release, $ver);
+				$ver = str_replace("LTS", "", $ver[1]);
+				$ver = explode(" ", $ver, 2);
+				$ver = reset($ver);
+				$mainver = $ver;
+			}
+			switch ($mainver){
+			case "16.04":
+				$relname = "(Xenial Xerus)";
+				$distconfid = 'ubuntu1604';
 				break;
-				case "12.04":
-					$relname = "(Precise Pangolin)";
+			case "15.10":
+				$relname = "(Wily Werewolf)";
 				break;
-				case "11.10":
-					$relname = "(Oneiric Ocelot)";
+			case "15.04":
+				$relname = "(Vivid Vervet)";
 				break;
-				case "11.14":
-					$relname = "(Natty Narwhal)";
+			case "14.10":
+				$relname = "(Utopic Unicorn)";
 				break;
-				case "10.10":
-					$relname = "(Maverick Meerkat)";
+			case "14.04":
+				$relname = "(Trusty Tahr)";
 				break;
-				case "10.04":
-					$relname = "(Lucid Lynx)";
+			case "13.10":
+				$relname = "(Saucy Salamander)";
 				break;
-				case "9.10":
-					$relname = "(Karmic Koala)";
+			case "13.04":
+				$relname = "(Raring Ringtail)";
 				break;
-				case "9.04":
-					$relname = "(Jaunty Jackpole)";
+			case "12.10":
+				$relname = "(Quantal Quetzal)";
 				break;
-				case "8.10":
+			case "12.04":
+				$relname = "(Precise Pangolin)";
+				break;
+			case "11.10":
+				$relname = "(Oneiric Ocelot)";
+				break;
+			case "11.14":
+				$relname = "(Natty Narwhal)";
+				break;
+			case "10.10":
+				$relname = "(Maverick Meerkat)";
+				break;
+			case "10.04":
+				$relname = "(Lucid Lynx)";
+				break;
+			case "9.10":
+				$relname = "(Karmic Koala)";
+				break;
+			case "9.04":
+				$relname = "(Jaunty Jackpole)";
+				break;
+			case "8.10":
 				$relname = "(Intrepid Ibex)";
 				break;
-				case "8.04":
-					$relname = "(Hardy Heron)";
+			case "8.04":
+				$relname = "(Hardy Heron)";
 				break;
-				case "7.10":
-					$relname = "(Gutsy Gibbon)";
+			case "7.10":
+				$relname = "(Gutsy Gibbon)";
 				break;
-				case "7.04":
-					$relname = "(Feisty Fawn)";
+			case "7.04":
+				$relname = "(Feisty Fawn)";
 				break;
-				case "6.10":
-					$relname = "(Edgy Eft)";
+			case "6.10":
+				$relname = "(Edgy Eft)";
 				break;
-				case "6.06":
-					$relname = "(Dapper Drake)";
+			case "6.06":
+				$relname = "(Dapper Drake)";
 				break;
-				case "5.10":
-					$relname = "(Breezy Badger)";
+			case "5.10":
+				$relname = "(Breezy Badger)";
 				break;
-				case "5.04":
-					$relname = "(Hoary Hedgehog)";
+			case "5.04":
+				$relname = "(Hoary Hedgehog)";
 				break;
-				case "4.10":
-					$relname = "(Warty Warthog)";
+			case "4.10":
+				$relname = "(Warty Warthog)";
 				break;
-				default:
-					$relname = "UNKNOWN";
-				}
-				$distver = $ver.$lts." ".$relname;
-			} elseif(trim(file_get_contents('/etc/debian_version')) == '4.0') {
-				$distname = 'Debian';
-				$distver = '4.0';
-				$distid = 'debian40';
-				$distbaseid = 'debian';
-			} elseif (strstr(trim(file_get_contents('/etc/debian_version')), '5.0')) {
-				$distname = 'Debian';
-				$distver = 'Lenny';
-				$distid = 'debian40';
-				$distbaseid = 'debian';
-			} elseif (strstr(trim(file_get_contents('/etc/debian_version')), '6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') {
-				$distname = 'Debian';
-				$distver = 'Squeeze/Sid';
-				$distid = 'debian60';
-				$distbaseid = 'debian';
-			} elseif (strstr(trim(file_get_contents('/etc/debian_version')), '7.0') || strstr(trim(file_get_contents('/etc/debian_version')), '7.1') || trim(file_get_contents('/etc/debian_version')) == 'wheezy/sid') {
-				$distname = 'Debian';
-				$distver = 'Wheezy/Sid';
-				$distid = 'debian60';
-				$distbaseid = 'debian';
-			} else {
-				$distname = 'Debian';
-				$distver = 'Unknown';
-				$distid = 'debian40';
-				$distbaseid = 'debian';
+			default:
+				$relname = "UNKNOWN";
 			}
-		}
-
-		//** OpenSuSE
-		elseif (file_exists('/etc/SuSE-release')) {
-			if (stristr(file_get_contents('/etc/SuSE-release'), '11.0')) {
-				$distname = 'openSUSE';
-				$distver = '11.0';
-				$distid = 'opensuse110';
-				$distbaseid = 'opensuse';
-			} elseif (stristr(file_get_contents('/etc/SuSE-release'), '11.1')) {
-				$distname = 'openSUSE';
-				$distver = '11.1';
-				$distid = 'opensuse110';
-				$distbaseid = 'opensuse';
-			} elseif (stristr(file_get_contents('/etc/SuSE-release'), '11.2')) {
-				$distname = 'openSUSE';
-				$distver = '11.1';
-				$distid = 'opensuse110';
-				$distbaseid = 'opensuse';
-			} else {
-				$distname = 'openSUSE';
-				$distver = 'Unknown';
-				$distid = 'opensuse110';
-				$distbaseid = 'opensuse';
-			}
-		}
-
-
-		//** Redhat
-		elseif (file_exists('/etc/redhat-release')) {
-
-			$content = file_get_contents('/etc/redhat-release');
-
-			if (stristr($content, 'Fedora release 9 (Sulphur)')) {
-				$distname = 'Fedora';
-				$distver = '9';
-				$distid = 'fedora9';
-				$distbaseid = 'fedora';
-			} elseif (stristr($content, 'Fedora release 10 (Cambridge)')) {
-				$distname = 'Fedora';
-				$distver = '10';
-				$distid = 'fedora9';
-				$distbaseid = 'fedora';
-			} elseif (stristr($content, 'Fedora release 10')) {
-				$distname = 'Fedora';
-				$distver = '11';
-				$distid = 'fedora9';
-				$distbaseid = 'fedora';
-			} elseif (stristr($content, 'CentOS release 5.2 (Final)')) {
-				$distname = 'CentOS';
-				$distver = '5.2';
-				$distid = 'centos52';
-				$distbaseid = 'fedora';
-			} elseif (stristr($content, 'CentOS release 5.3 (Final)')) {
-				$distname = 'CentOS';
-				$distver = '5.3';
-				$distid = 'centos53';
-				$distbaseid = 'fedora';
-			} else {
-				$distname = 'Redhat';
-				$distver = 'Unknown';
-				$distid = 'fedora9';
-				$distbaseid = 'fedora';
-			}
-		}
-
-		//** Gentoo
-		elseif (file_exists('/etc/gentoo-release')) {
-
-			$content = file_get_contents('/etc/gentoo-release');
-
-			preg_match_all('/([0-9]{1,2})/', $content, $version);
-			$distname = 'Gentoo';
-			$distver = $version[0][0] . $version[0][1];
-			$distid = 'gentoo';
-			$distbaseid = 'gentoo';
+			$distver = $ver.$lts." ".$relname;
+		} elseif(trim(file_get_contents('/etc/debian_version')) == '4.0') {
+			$distname = 'Debian';
+			$distver = '4.0';
+			$distid = 'debian40';
+			$distbaseid = 'debian';
+		} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '5.0')) {
+			$distname = 'Debian';
+			$distver = 'Lenny';
+			$distid = 'debian40';
+			$distbaseid = 'debian';
+		} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '6.0') || trim(file_get_contents('/etc/debian_version')) == 'squeeze/sid') {
+			$distname = 'Debian';
+			$distver = 'Squeeze/Sid';
+			$distid = 'debian60';
+			$distbaseid = 'debian';
+		} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '7.0') || substr(trim(file_get_contents('/etc/debian_version')),0,2) == '7.' || trim(file_get_contents('/etc/debian_version')) == 'wheezy/sid') {
+			$distname = 'Debian';
+			$distver = 'Wheezy/Sid';
+			$distid = 'debian60';
+			$distbaseid = 'debian';
+		} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '8') || substr(trim(file_get_contents('/etc/debian_version')),0,1) == '8') {
+			$distname = 'Debian';
+			$distver = 'Jessie';
+			$distid = 'debian60';
+			$distbaseid = 'debian';
+		} elseif(strstr(trim(file_get_contents('/etc/debian_version')), '/sid')) {
+			$distname = 'Debian';
+			$distver = 'Testing';
+			$distid = 'debian60';
+			$distconfid = 'debiantesting';
+			$distbaseid = 'debian';
 		} else {
-			die('Unrecognized GNU/Linux distribution');
+			$distname = 'Debian';
+			$distver = 'Unknown';
+			$distid = 'debian40';
+			$distbaseid = 'debian';
 		}
-
-		return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid);
 	}
 
-    // this function remains in the tools class, because it is used by cron AND rescue
+	//** OpenSuSE
+	elseif(file_exists('/etc/SuSE-release')) {
+		if(stristr(file_get_contents('/etc/SuSE-release'), '11.0')) {
+			$distname = 'openSUSE';
+			$distver = '11.0';
+			$distid = 'opensuse110';
+			$distbaseid = 'opensuse';
+		} elseif(stristr(file_get_contents('/etc/SuSE-release'), '11.1')) {
+			$distname = 'openSUSE';
+			$distver = '11.1';
+			$distid = 'opensuse110';
+			$distbaseid = 'opensuse';
+		} elseif(stristr(file_get_contents('/etc/SuSE-release'), '11.2')) {
+			$distname = 'openSUSE';
+			$distver = '11.2';
+			$distid = 'opensuse112';
+			$distbaseid = 'opensuse';
+		}  else {
+			$distname = 'openSUSE';
+			$distver = 'Unknown';
+			$distid = 'opensuse112';
+			$distbaseid = 'opensuse';
+		}
+	}
+
+
+	//** Redhat
+	elseif(file_exists('/etc/redhat-release')) {
+
+		$content = file_get_contents('/etc/redhat-release');
+
+		if(stristr($content, 'Fedora release 9 (Sulphur)')) {
+			$distname = 'Fedora';
+			$distver = '9';
+			$distid = 'fedora9';
+			$distbaseid = 'fedora';
+		} elseif(stristr($content, 'Fedora release 10 (Cambridge)')) {
+			$distname = 'Fedora';
+			$distver = '10';
+			$distid = 'fedora9';
+			$distbaseid = 'fedora';
+		} elseif(stristr($content, 'Fedora release 10')) {
+			$distname = 'Fedora';
+			$distver = '11';
+			$distid = 'fedora9';
+			$distbaseid = 'fedora';
+		} elseif(stristr($content, 'CentOS release 5.2 (Final)')) {
+			$distname = 'CentOS';
+			$distver = '5.2';
+			$distid = 'centos52';
+			$distbaseid = 'fedora';
+		} elseif(stristr($content, 'CentOS release 5.3 (Final)')) {
+			$distname = 'CentOS';
+			$distver = '5.3';
+			$distid = 'centos53';
+			$distbaseid = 'fedora';
+		} elseif(stristr($content, 'CentOS release 5')) {
+			$distname = 'CentOS';
+			$distver = 'Unknown';
+			$distid = 'centos53';
+			$distbaseid = 'fedora';
+		} elseif(stristr($content, 'CentOS Linux release 6')) {
+			$distname = 'CentOS';
+			$distver = 'Unknown';
+			$distid = 'centos53';
+			$distbaseid = 'fedora';
+		} elseif(stristr($content, 'CentOS Linux release 7.2')) {
+			$distname = 'CentOS';
+			$distver = 'Unknown';
+			$distid = 'centos70';
+			$distconfid = 'centos72';
+			$distbaseid = 'fedora';
+		} elseif(stristr($content, 'CentOS Linux release 7')) {
+			$distname = 'CentOS';
+			$distver = 'Unknown';
+			$distid = 'centos70';
+			$distbaseid = 'fedora';
+		} else {
+			$distname = 'Redhat';
+			$distver = 'Unknown';
+			$distid = 'fedora9';
+			$distbaseid = 'fedora';
+		}
+	}
+
+	//** Gentoo
+	elseif(file_exists('/etc/gentoo-release')) {
+
+		$content = file_get_contents('/etc/gentoo-release');
+
+		preg_match_all('/([0-9]{1,2})/', $content, $version);
+		$distname = 'Gentoo';
+		$distver = $version[0][0].$version[0][1];
+		$distid = 'gentoo';
+		$distbaseid = 'gentoo';
+
+	} else {
+		die('Unrecognized GNU/Linux distribution');
+	}
+	
+	// Set $distconfid to distid, if no different id for the config is defined
+	if(!isset($distconfid)) $distconfid = $distid;
+
+	return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'confid' => $distconfid, 'baseid' => $distbaseid);
+	}
+
+	// this function remains in the tools class, because it is used by cron AND rescue
 	public function monitorServices() {
 		global $app;
 		global $conf;
 
 		/** the id of the server as int */
+
+
 		$server_id = intval($conf['server_id']);
 
 		/**  get the "active" Services of the server from the DB */
-		$services = $app->db->queryOneRecord('SELECT * FROM server WHERE server_id = ' . $server_id);
+		$services = $app->db->queryOneRecord('SELECT * FROM server WHERE server_id = ?', $server_id);
 		/*
 		 * If the DB is down, we have to set the db to "yes".
 		 * If we don't do this, then the monitor will NOT monitor, that the db is down and so the
@@ -331,14 +416,16 @@
 				$state = 'error'; // because service is down
 			}
 		}
-        $data['mongodbserver'] = -1;
-        if ($this->_checkTcp('localhost', 27017)) {
-            $data['mongodbserver'] = 1;
-        } else {
-            $data['mongodbserver'] = 0;
-            //$state = 'error'; // because service is down 
-            /* TODO!!! check if this is a mongodbserver at all, otherwise it will always throw an error state!!! */
-        }
+/*
+		$data['mongodbserver'] = -1;
+		if ($this->_checkTcp('localhost', 27017)) {
+			$data['mongodbserver'] = 1;
+		} else {
+			$data['mongodbserver'] = 0;
+*/
+			//$state = 'error'; // because service is down
+			/* TODO!!! check if this is a mongodbserver at all, otherwise it will always throw an error state!!! */
+//		}
 
 		/*
 		 * Return the Result
@@ -349,7 +436,7 @@
 		$res['state'] = $state;
 		return $res;
 	}
-    
+
 	public function _getLogData($log) {
 		global $conf;
 
@@ -367,111 +454,111 @@
 		}
 
 		switch ($log) {
-			case 'log_mail':
-				if ($dist == 'debian') {
-					$logfile = '/var/log/mail.log';
-				} elseif ($dist == 'redhat') {
-					$logfile = '/var/log/maillog';
-				} elseif ($dist == 'suse') {
-					$logfile = '/var/log/mail.info';
-				} elseif ($dist == 'gentoo') {
-					$logfile = '/var/log/maillog';
-				}
-				break;
-			case 'log_mail_warn':
-				if ($dist == 'debian') {
-					$logfile = '/var/log/mail.warn';
-				} elseif ($dist == 'redhat') {
-					$logfile = '/var/log/maillog';
-				} elseif ($dist == 'suse') {
-					$logfile = '/var/log/mail.warn';
-				} elseif ($dist == 'gentoo') {
-					$logfile = '/var/log/maillog';
-				}
-				break;
-			case 'log_mail_err':
-				if ($dist == 'debian') {
-					$logfile = '/var/log/mail.err';
-				} elseif ($dist == 'redhat') {
-					$logfile = '/var/log/maillog';
-				} elseif ($dist == 'suse') {
-					$logfile = '/var/log/mail.err';
-				} elseif ($dist == 'gentoo') {
-					$logfile = '/var/log/maillog';
-				}
-				break;
-			case 'log_messages':
-				if ($dist == 'debian') {
-					$logfile = '/var/log/syslog';
-				} elseif ($dist == 'redhat') {
-					$logfile = '/var/log/messages';
-				} elseif ($dist == 'suse') {
-					$logfile = '/var/log/messages';
-				} elseif ($dist == 'gentoo') {
-					$logfile = '/var/log/messages';
-				}
-				break;
-			case 'log_ispc_cron':
-				if ($dist == 'debian') {
-					$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
-				} elseif ($dist == 'redhat') {
-					$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
-				} elseif ($dist == 'suse') {
-					$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
-				} elseif ($dist == 'gentoo') {
-					$logfile = '/var/log/cron';
-				}
-				break;
-			case 'log_freshclam':
-				if ($dist == 'debian') {
-					$logfile = '/var/log/clamav/freshclam.log';
-				} elseif ($dist == 'redhat') {
-					$logfile = (is_file('/var/log/clamav/freshclam.log') ? '/var/log/clamav/freshclam.log' : '/var/log/freshclam.log');
-				} elseif ($dist == 'suse') {
-					$logfile = '/var/log/freshclam.log';
-				} elseif ($dist == 'gentoo') {
-					$logfile = '/var/log/clamav/freshclam.log';
-				}
-				break;
-			case 'log_clamav':
-				if ($dist == 'debian') {
-					$logfile = '/var/log/clamav/clamav.log';
-				} elseif ($dist == 'redhat') {
-					$logfile = (is_file('/var/log/clamav/clamd.log') ? '/var/log/clamav/clamd.log' : '/var/log/maillog');
-				} elseif ($dist == 'suse') {
-					$logfile = '/var/log/clamd.log';
-				} elseif ($dist == 'gentoo') {
-					$logfile = '/var/log/clamav/clamd.log';
-				}
-				break;
-			case 'log_fail2ban':
-				if ($dist == 'debian') {
-					$logfile = '/var/log/fail2ban.log';
-				} elseif ($dist == 'redhat') {
-					$logfile = '/var/log/fail2ban.log';
-				} elseif ($dist == 'suse') {
-					$logfile = '/var/log/fail2ban.log';
-				} elseif ($dist == 'gentoo') {
-					$logfile = '/var/log/fail2ban.log';
-				}
-				break;
-			case 'log_mongodb':
-					$logfile = '/var/log/mongodb/mongodb.log';
-				break;
-			case 'log_ispconfig':
-				if ($dist == 'debian') {
-					$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
-				} elseif ($dist == 'redhat') {
-					$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
-				} elseif ($dist == 'suse') {
-					$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
-				} elseif ($dist == 'gentoo') {
-					$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
-				}
-				break;
-			default:
-				$logfile = '';
-				break;
+		case 'log_mail':
+			if ($dist == 'debian') {
+				$logfile = '/var/log/mail.log';
+			} elseif ($dist == 'redhat') {
+				$logfile = '/var/log/maillog';
+			} elseif ($dist == 'suse') {
+				$logfile = '/var/log/mail.info';
+			} elseif ($dist == 'gentoo') {
+				$logfile = '/var/log/maillog';
+			}
+			break;
+		case 'log_mail_warn':
+			if ($dist == 'debian') {
+				$logfile = '/var/log/mail.warn';
+			} elseif ($dist == 'redhat') {
+				$logfile = '/var/log/maillog';
+			} elseif ($dist == 'suse') {
+				$logfile = '/var/log/mail.warn';
+			} elseif ($dist == 'gentoo') {
+				$logfile = '/var/log/maillog';
+			}
+			break;
+		case 'log_mail_err':
+			if ($dist == 'debian') {
+				$logfile = '/var/log/mail.err';
+			} elseif ($dist == 'redhat') {
+				$logfile = '/var/log/maillog';
+			} elseif ($dist == 'suse') {
+				$logfile = '/var/log/mail.err';
+			} elseif ($dist == 'gentoo') {
+				$logfile = '/var/log/maillog';
+			}
+			break;
+		case 'log_messages':
+			if ($dist == 'debian') {
+				$logfile = '/var/log/syslog';
+			} elseif ($dist == 'redhat') {
+				$logfile = '/var/log/messages';
+			} elseif ($dist == 'suse') {
+				$logfile = '/var/log/messages';
+			} elseif ($dist == 'gentoo') {
+				$logfile = '/var/log/messages';
+			}
+			break;
+		case 'log_ispc_cron':
+			if ($dist == 'debian') {
+				$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
+			} elseif ($dist == 'redhat') {
+				$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
+			} elseif ($dist == 'suse') {
+				$logfile = $conf['ispconfig_log_dir'] . '/cron.log';
+			} elseif ($dist == 'gentoo') {
+				$logfile = '/var/log/cron';
+			}
+			break;
+		case 'log_freshclam':
+			if ($dist == 'debian') {
+				$logfile = '/var/log/clamav/freshclam.log';
+			} elseif ($dist == 'redhat') {
+				$logfile = (is_file('/var/log/clamav/freshclam.log') ? '/var/log/clamav/freshclam.log' : '/var/log/freshclam.log');
+			} elseif ($dist == 'suse') {
+				$logfile = '/var/log/freshclam.log';
+			} elseif ($dist == 'gentoo') {
+				$logfile = '/var/log/clamav/freshclam.log';
+			}
+			break;
+		case 'log_clamav':
+			if ($dist == 'debian') {
+				$logfile = '/var/log/clamav/clamav.log';
+			} elseif ($dist == 'redhat') {
+				$logfile = (is_file('/var/log/clamav/clamd.log') ? '/var/log/clamav/clamd.log' : '/var/log/maillog');
+			} elseif ($dist == 'suse') {
+				$logfile = '/var/log/clamd.log';
+			} elseif ($dist == 'gentoo') {
+				$logfile = '/var/log/clamav/clamd.log';
+			}
+			break;
+		case 'log_fail2ban':
+			if ($dist == 'debian') {
+				$logfile = '/var/log/fail2ban.log';
+			} elseif ($dist == 'redhat') {
+				$logfile = '/var/log/fail2ban.log';
+			} elseif ($dist == 'suse') {
+				$logfile = '/var/log/fail2ban.log';
+			} elseif ($dist == 'gentoo') {
+				$logfile = '/var/log/fail2ban.log';
+			}
+			break;
+		case 'log_mongodb':
+			$logfile = '/var/log/mongodb/mongodb.log';
+			break;
+		case 'log_ispconfig':
+			if ($dist == 'debian') {
+				$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
+			} elseif ($dist == 'redhat') {
+				$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
+			} elseif ($dist == 'suse') {
+				$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
+			} elseif ($dist == 'gentoo') {
+				$logfile = $conf['ispconfig_log_dir'] . '/ispconfig.log';
+			}
+			break;
+		default:
+			$logfile = '';
+			break;
 		}
 
 		// Getting the logfile content
@@ -578,39 +665,39 @@
 		 * Calculate the weight of the old state
 		 */
 		switch ($oldState) {
-			case 'no_state': $oldInt = 0;
-				break;
-			case 'ok': $oldInt = 1;
-				break;
-			case 'unknown': $oldInt = 2;
-				break;
-			case 'info': $oldInt = 3;
-				break;
-			case 'warning': $oldInt = 4;
-				break;
-			case 'critical': $oldInt = 5;
-				break;
-			case 'error': $oldInt = 6;
-				break;
+		case 'no_state': $oldInt = 0;
+			break;
+		case 'ok': $oldInt = 1;
+			break;
+		case 'unknown': $oldInt = 2;
+			break;
+		case 'info': $oldInt = 3;
+			break;
+		case 'warning': $oldInt = 4;
+			break;
+		case 'critical': $oldInt = 5;
+			break;
+		case 'error': $oldInt = 6;
+			break;
 		}
 		/*
 		 * Calculate the weight of the new state
 		 */
 		switch ($newState) {
-			case 'no_state': $newInt = 0;
-				break;
-			case 'ok': $newInt = 1;
-				break;
-			case 'unknown': $newInt = 2;
-				break;
-			case 'info': $newInt = 3;
-				break;
-			case 'warning': $newInt = 4;
-				break;
-			case 'critical': $newInt = 5;
-				break;
-			case 'error': $newInt = 6;
-				break;
+		case 'no_state': $newInt = 0;
+			break;
+		case 'ok': $newInt = 1;
+			break;
+		case 'unknown': $newInt = 2;
+			break;
+		case 'info': $newInt = 3;
+			break;
+		case 'warning': $newInt = 4;
+			break;
+		case 'critical': $newInt = 5;
+			break;
+		case 'error': $newInt = 6;
+			break;
 		}
 
 		/*
@@ -634,7 +721,7 @@
 
 		// $now = time();
 		// $old = $now - (4 * 60); // 4 minutes
-		$old = 'UNIX_TIMESTAMP() - 240';
+		$old = 240; //seconds
 
 		/*
 		 * ATTENTION if i do NOT pay attention of the server id, i delete all data (of the type)
@@ -644,16 +731,66 @@
 		 * 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 . ' ' .
-				'AND ' .
-				'  server_id = ' . $serverId;
-		$app->dbmaster->query($sql);
+		$sql = 'DELETE FROM `monitor_data` WHERE `type` = ? AND `created` < UNIX_TIMESTAMP() - ? AND `server_id` = ?';
+		$app->dbmaster->query($sql, $type, $old, $serverId);
 	}
 
+	public function send_notification_email($template, $placeholders, $recipients) {
+		global $conf;
+
+		if(!is_array($recipients) || count($recipients) < 1) return false;
+		if(!is_array($placeholders)) $placeholders = array();
+
+		if(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt')) {
+			$lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt');
+		} elseif(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt')) {
+			$lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt');
+		} elseif(file_exists($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt')) {
+			$lines = file($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt');
+		} else {
+			$lines = file($conf['rootpath'].'/conf/mail/' . $template . '_en.txt');
+		}
+
+		//* get mail headers, subject and body
+		$mailHeaders = '';
+		$mailBody = '';
+		$mailSubject = '';
+		$inHeader = true;
+		for($l = 0; $l < count($lines); $l++) {
+			if(trim($lines[$l]) == '') {
+				$inHeader = false;
+				continue;
+			}
+			if($inHeader == true) {
+				$parts = explode(':', $lines[$l], 2);
+				if(strtolower($parts[0]) == 'subject') {
+					$mailSubject = trim($parts[1]);
+					continue;
+				}
+				unset($parts);
+				$mailHeaders .= trim($lines[$l]) . "\n";
+			} else {
+				$mailBody .= trim($lines[$l]) . "\n";
+			}
+		}
+		$mailBody = trim($mailBody);
+
+		//* Replace placeholders
+		$mailHeaders = strtr($mailHeaders, $placeholders);
+		$mailSubject = strtr($mailSubject, $placeholders);
+		$mailBody = strtr($mailBody, $placeholders);
+
+		for($r = 0; $r < count($recipients); $r++) {
+			mail($recipients[$r], $mailSubject, $mailBody, $mailHeaders);
+		}
+
+		unset($mailSubject);
+		unset($mailHeaders);
+		unset($mailBody);
+		unset($lines);
+
+		return true;
+	}
 
 }
 

--
Gitblit v1.9.1