jmontoya
2010-08-04 e8a29cf134f7df1a1e7637083f6d7adf64949b7c
server/mods-available/monitor_core_module.inc.php
@@ -51,26 +51,7 @@
      global $app;
      /*
        Annonce the actions that where provided by this module, so plugins
        can register on them.
      */
      /* none at them moment */
      //$app->plugins->announceEvents($this->module_name,$this->actions_available);
      /*
        As we want to get notified of any changes on several database tables,
        we register for them.
        The following function registers the function "functionname"
            to be executed when a record for the table "dbtable" is
            processed in the sys_datalog. "classname" is the name of the
            class that contains the function functionname.
      */
      /* none at them moment */
      //$app->modules->registerTableHook('mail_access','mail_module','process');
      /*
        Do the monitor every n minutes and write the result in the db
         * Do the monitor every n minutes and write the result to the db
      */
      $min = date('i');
      if (($min % $this->interval) == 0) {
@@ -83,15 +64,7 @@
     The function then raises the events for the plugins.
   */
   function process($tablename, $action, $data) {
      //      global $app;
      //
      //      switch ($tablename) {
      //         case 'mail_access':
      //            if($action == 'i') $app->plugins->raiseEvent('mail_access_insert',$data);
      //            if($action == 'u') $app->plugins->raiseEvent('mail_access_update',$data);
      //            if($action == 'd') $app->plugins->raiseEvent('mail_access_delete',$data);
      //            break;
      //      } // end switch
      // not needed
   } // end function
   //** Get distribution identifier
@@ -222,12 +195,16 @@
   // TODO: what monitoring is done should be a config-var
   function doMonitor() {
      /* Calls the single Monitoring steps */
      $this->monitorHDQuota();
      $this->monitorServer();
      $this->monitorOSVer();
      $this->monitorIspCVer();
      $this->monitorDiskUsage();
      $this->monitorMemUsage();
      $this->monitorCpu();
      $this->monitorServices();
      $this->monitorOpenVzHost();
      $this->monitorOpenVzUserBeancounter();
      $this->monitorMailLog();
      $this->monitorMailWarnLog();
      $this->monitorMailErrLog();
@@ -243,7 +220,84 @@
      $this->monitorFail2ban();
      $this->monitorSysLog();
   }
   function monitorHDQuota() {
      global $app;
      global $conf;
      /* Initialize data array */
      $data = array();
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'harddisk_quota';
      /** The state of the harddisk_quota. */
      $state = 'ok';
      /** Fetch the data for all users*/
      $dfData = shell_exec("repquota -asu");
      // split into array
      $df = explode("\n", $dfData);
      /*
         * ignore the first 5 lines, process the rest
      */
      for($i=5; $i <= sizeof($df); $i++) {
         if ($df[$i] != '') {
            /*
                 * Make a array of the data
            */
            $s = preg_split ("/[\s]+/", $df[$i]);
            $username = $s[0];
            $data['user'][$username]['used'] = $s[2];
            $data['user'][$username]['soft'] = $s[3];
            $data['user'][$username]['hard'] = $s[4];
         }
      }
      /** Fetch the data for all users*/
      $dfData = shell_exec("repquota -asg");
      // split into array
      $df = explode("\n", $dfData);
      /*
         * ignore the first 5 lines, process the rest
      */
      for($i=5; $i <= sizeof($df); $i++) {
         if ($df[$i] != '') {
            /*
                 * Make a array of the data
            */
            $s = preg_split ("/[\s]+/", $df[$i]);
            $groupname = $s[0];
            $data['group'][$groupname]['used'] = $s[1];
            $data['group'][$groupname]['soft'] = $s[2];
            $data['group'][$groupname]['hard'] = $s[3];
         }
      }
      /*
        Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
            $server_id . ", " .
            "'" . $app->dbmaster->quote($type) . "', " .
            time() . ", " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 4);
   }
   function monitorServer() {
      global $app;
      global $conf;
@@ -296,7 +350,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorOsVer() {
@@ -334,10 +388,46 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorIspcVer() {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'ispc_info';
      /*
        Fetch the data into a array
      */
      $data['name'] = ISPC_APP_TITLE;
      $data['version'] = ISPC_APP_VERSION;
      /* the ISPC-Version has no state. It is, what it is */
      $state = 'no_state';
      /*
        Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
            $server_id . ", " .
            "'" . $app->dbmaster->quote($type) . "', " .
            time() . ", " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 4);
   }
   function monitorDiskUsage() {
      global $app;
      global $conf;
@@ -403,7 +493,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
@@ -453,7 +543,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
@@ -500,7 +590,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
@@ -616,7 +706,116 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorOpenVzHost() {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'openvz_veinfo';
      /*
        Fetch the data into a array
      */
      $app->load(openvz_tools);
      $openVzTools = new openvz_tools();
      $data = $openVzTools->getOpenVzVeInfo();
      /* the VE-Info has no state. It is, what it is */
      $state = 'no_state';
      /*
        Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
            $server_id . ", " .
            "'" . $app->dbmaster->quote($type) . "', " .
            time() . ", " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 4);
   }
   function monitorOpenVzUserBeancounter() {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'openvz_beancounter';
      /*
        Fetch the data into a array
      */
      $app->load(openvz_tools);
      $openVzTools = new openvz_tools();
      $data = $openVzTools->getOpenVzVeBeanCounter();
      /* calculate the state of the beancounter */
      if ($data == '') {
         $state = 'no_state';
      }
      else {
         $state = 'ok';
         /* transfer this output-string into a array */
         $test = explode("\n", $data);
         /* the first list of the output is not needed */
         array_shift($test);
         /* now process all items of the rest */
         foreach ($test as $item) {
            /*
              * eliminate all doubled spaces and spaces at the beginning and end
            */
            while (strpos($item, '  ') !== false) {
               $item = str_replace('  ', ' ', $item);
            }
            $item = trim($item);
            /*
              * The failcounter is the LAST
            */
            if ($item != '') {
               $tmp = explode(' ', $item);
               $failCounter = $tmp[sizeof($tmp)-1];
               if ($failCounter > 0 ) $state = 'info';
               if ($failCounter > 50 ) $state = 'warning';
               if ($failCounter > 200 ) $state = 'critical';
               if ($failCounter > 10000 ) $state = 'error';
            }
         }
      }
      /*
        Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
            $server_id . ", " .
            "'" . $app->dbmaster->quote($type) . "', " .
            time() . ", " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 4);
   }
@@ -657,8 +856,11 @@
            $state = 'ok';
         }
         else {
            /* There is something to update! */
            $state = 'warning';
            /*
             * There is something to update! this is in most cases not critical, so we can
             * do a system-update once a month or so...
            */
            $state = 'info';
         }
         /*
@@ -738,7 +940,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 0, 2);
      $this->_delOldRecords($type, 4);
   }
   function monitorMailQueue() {
@@ -784,7 +986,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
@@ -798,8 +1000,16 @@
      /** The type of the data */
      $type = 'raid_state';
      /* This monitoring is only available if mdadm is installed */
      $location = system('which mdadm', $retval);
      /*
       * We support some RAIDS. But if we can't find any of them, we have no data
      */
      $state = 'no_state';
      $data['output']= '';
      /*
       * Check, if we have mdadm installed (software-raid)
      */
      system('which mdadm', $retval);
      if($retval === 0) {
         /*
             * Fetch the output
@@ -848,20 +1058,56 @@
               }
            }
         }
      }
      else {
      /*
       * Check, if we have mpt-status installed (LSIsoftware-raid)
      */
      system('which mpt-status', $retval);
      if($retval === 0) {
         /*
             * mdadm is not installed, so there is no data and no state
             *
             * no_state, NOT unknown, because "unknown" is shown as state
             * inside the GUI. no_state is hidden.
             *
             * We have to write NO DATA inside the DB, because the GUI
             * could not know, if there is any dat, or not...
             * Fetch the output
         */
         $state = 'no_state';
         $data['output']= '';
         $data['output'] = shell_exec('mpt-status --autoload -n');
         /*
             * Then calc the state.
         */
         $state = 'ok';
         foreach ($data['output'] as $item) {
            /*
             * The output contains information for every RAID and every HDD.
             * We only need the state of the RAID
            */
            if (strpos($item, 'raidlevel:') !== false) {
               /*
                * We found a raid, process the state of it
               */
               if (strpos($item, ' ONLINE ') !== false) {
                  $this->_setState($state, 'ok');
               }
               else if (strpos($item, ' OPTIMAL ') !== false) {
                  $this->_setState($state, 'ok');
               }
               else if (strpos($item, ' INITIAL ') !== false) {
                  $this->_setState($state, 'info');
               }
               else if (strpos($item, ' INACTIVE ') !== false) {
                  $this->_setState($state, 'critical');
               }
               else if (strpos($item, ' RESYNC ') !== false) {
                  $this->_setState($state, 'info');
               }
               else if (strpos($item, ' DEGRADED ') !== false) {
                  $this->_setState($state, 'critical');
               }
               else {
                  /* we don't know the state. so we set the state to critical, that the
                   * admin is warned, that something is wrong
                  */
                  $this->_setState($state, 'critical');
               }
            }
         }
      }
      /*
@@ -878,7 +1124,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorRkHunter() {
@@ -899,7 +1145,7 @@
      $type = 'rkhunter';
      /* This monitoring is only available if rkhunter is installed */
      $location = system('which rkhunter', $retval);
      system('which rkhunter', $retval);
      if($retval === 0) {
         /*
             * Fetch the output
@@ -939,7 +1185,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 0, 2);
      $this->_delOldRecords($type, 4);
   }
   function monitorFail2ban() {
@@ -953,8 +1199,8 @@
      $type = 'log_fail2ban';
      /* This monitoring is only available if fail2ban is installed */
      $location = system('which fail2ban-client', $retval); // Debian, Ubuntu, Fedora
      if($retval !== 0) $location = system('which fail2ban', $retval); // CentOS
      system('which fail2ban-client', $retval); // Debian, Ubuntu, Fedora
      if($retval !== 0) system('which fail2ban', $retval); // CentOS
      if($retval === 0) {
         /*  Get the data of the log */
         $data = $this->_getLogData($type);
@@ -992,7 +1238,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorSysLog() {
@@ -1034,7 +1280,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorMailLog() {
@@ -1070,7 +1316,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorMailWarnLog() {
@@ -1106,7 +1352,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorMailErrLog() {
@@ -1142,7 +1388,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
@@ -1179,7 +1425,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorISPCCronLog() {
@@ -1215,7 +1461,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorFreshClamLog() {
@@ -1265,7 +1511,12 @@
      */
      foreach($lastLog as $line) {
         if (strpos(strtolower($line), "outdated") !== false) {
            $state = $this->_setState($state, 'warning');
            /*
             * Outdatet is only info, because if we set this to warning, the server is
             * as long in state warning, as there is a new version of ClamAv which takes
             * sometimes weeks!
            */
            $state = $this->_setState($state, 'info');
         }
      }
@@ -1283,7 +1534,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorClamAvLog() {
@@ -1316,7 +1567,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }
   function monitorIspConfigLog() {
@@ -1349,7 +1600,7 @@
      $app->dbmaster->query($sql);
      /* The new data is written, now we can delete the old one */
      $this->_delOldRecords($type, 10);
      $this->_delOldRecords($type, 4);
   }