tbrehm
2010-05-11 2e961201be67cc348e7da22ad7c32c87c9f3d30d
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
@@ -224,10 +197,13 @@
      /* Calls the single Monitoring steps */
      $this->monitorServer();
      $this->monitorOSVer();
      $this->monitorIspCVer();
      $this->monitorDiskUsage();
      $this->monitorMemUsage();
      $this->monitorCpu();
      $this->monitorServices();
      $this->monitorOpenVzHost();
      $this->monitorOpenVzUserBeancounter();
      $this->monitorMailLog();
      $this->monitorMailWarnLog();
      $this->monitorMailErrLog();
@@ -296,7 +272,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 +310,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 +415,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 +465,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 +512,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 +628,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 +778,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 +862,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 +908,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 +922,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 +980,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 +1046,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 +1067,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 +1107,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 +1121,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 +1160,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 +1202,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 +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 monitorMailWarnLog() {
@@ -1106,7 +1274,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 +1310,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 +1347,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 +1383,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 +1433,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 +1456,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 +1489,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 +1522,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);
   }