tbrehm
2013-08-02 2332b2279d8a8599b4f041370315edc9544b1560
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();
      }
@@ -84,8 +89,9 @@
      $this->_tools = new monitor_tools();
      /*
       * Calls the single Monitoring steps
       * Calls the single Monitoring steps
       */
      $this->_monitorEmailQuota();
      $this->_monitorHDQuota();
      $this->_monitorServer();
      $this->_monitorOsVer();
@@ -111,8 +117,46 @@
      $this->_monitorRaid();
      $this->_monitorRkHunter();
      $this->_monitorFail2ban();
      $this->_monitorMongoDB();
      $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() {
      global $app;
@@ -125,11 +169,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -150,11 +194,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -175,11 +219,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -200,11 +244,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -225,11 +269,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -249,11 +293,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -273,11 +317,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -298,11 +342,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -323,11 +367,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -348,11 +392,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -366,7 +410,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;
@@ -380,14 +424,18 @@
       */
      $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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -408,11 +456,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -433,11 +481,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -451,8 +499,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;
      /*
@@ -468,11 +516,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -483,21 +531,72 @@
   }
   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 _monitorMongoDB() {
   global $app;
        /*
         * First we get the Monitoring-data from the tools
         */
        $res = $this->_tools->monitorMongoDB();
        /*
         * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -518,11 +617,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -543,11 +642,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -568,11 +667,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -593,11 +692,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -618,11 +717,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -640,14 +739,17 @@
       */
      $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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -668,11 +770,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -693,11 +795,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -718,11 +820,11 @@
      /*
       * 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']) . "', " .
            time() . ', ' .
            'UNIX_TIMESTAMP(), ' .
            "'" . $app->dbmaster->quote(serialize($res['data'])) . "', " .
            "'" . $res['state'] . "'" .
            ')';
@@ -741,11 +843,13 @@
   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