Till Brehm
2014-08-01 2c49c56ebcb55e4dec88f22bf2977d0e6db9bcc7
server/mods-available/monitor_core_module.inc.php
@@ -36,7 +36,11 @@
   /* No actions at this time. maybe later... */
   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
@@ -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,6 +83,7 @@
    */
   // 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...
       */
@@ -83,8 +91,9 @@
      $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() {
@@ -124,18 +170,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorServer() {
@@ -149,18 +195,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorOsVer() {
@@ -174,18 +220,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorIspcVer() {
@@ -199,18 +245,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorDiskUsage() {
@@ -224,18 +270,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorMemUsage() {
@@ -248,18 +294,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorCpu() {
@@ -272,18 +318,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorServices() {
@@ -297,18 +343,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorOpenVzHost() {
@@ -322,18 +368,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorOpenVzUserBeancounter() {
@@ -347,25 +393,25 @@
      /*
       * 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, $res['server_id']);
      $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,27 +419,35 @@
       * OK - here we go...
       */
      global $app;
      $app->uses('getconf');
      $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
      if($server_config['monitor_system_updates'] == 'n') return;
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorMailQueue() {
@@ -407,18 +461,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorRaid() {
@@ -432,26 +486,26 @@
      /*
       * 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, $res['server_id']);
      $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;
      /*
@@ -467,43 +521,69 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   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) ' .
            '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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorSysLog() {
@@ -517,18 +597,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorMailLog() {
@@ -542,18 +622,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorMailWarnLog() {
@@ -567,18 +647,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorMailErrLog() {
@@ -592,18 +672,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorMessagesLog() {
@@ -617,18 +697,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorISPCCronLog() {
@@ -639,21 +719,24 @@
       */
      $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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorFreshClamLog() {
@@ -667,18 +750,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorClamAvLog() {
@@ -692,18 +775,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   private function _monitorIspConfigLog() {
@@ -717,18 +800,18 @@
      /*
       * 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, $res['server_id']);
      $this->_delOldRecords($res['type'], $res['server_id']);
   }
   /**
@@ -740,23 +823,25 @@
   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
       * 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 . ' ' .
            'AND ' .
            '  server_id = ' . $serverId;
         'WHERE ' .
         '  type =' . "'" . $app->dbmaster->quote($type) . "' " .
         'AND ' .
         '  created < ' . $old . ' ' .
         'AND ' .
         '  server_id = ' . $serverId;
      $app->dbmaster->query($sql);
   }