vogelor
2010-04-30 220bb93ba4c0f5142b22905c94add1012cd8caf0
server/mods-available/monitor_core_module.inc.php
@@ -1,6 +1,6 @@
<?php
/*
Copyright (c) 2007-2008, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
Copyright (c) 2007-2010, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
@@ -28,7 +28,6 @@
*/
class monitor_core_module {
    /* TODO: this should be a config - var instead of a "constant" */
    var $interval = 5; // do the monitoring every 5 minutes
    var $module_name = 'monitor_core_module';
@@ -74,8 +73,7 @@
        Do the monitor every n minutes and write the result in the db
        */
        $min = date('i');
        if (($min % $this->interval) == 0)
        {
      if (($min % $this->interval) == 0) {
            $this->doMonitor();
        }
    }
@@ -96,15 +94,136 @@
        //      } // end switch
    } // end function
   //** Get distribution identifier
   //** IMPORTANT!
   //   This is the same code as in /install/install.php
   //   So if you change it here, you also have to change it in /install/install.php!
   //   Please do not forget to remove the swriteln(); - lines here at this file
   function get_distname() {
      $distname = '';
      $distver = '';
      $distid = '';
      $distbaseid = '';
      //** Debian or Ubuntu
      if(file_exists('/etc/debian_version')) {
         if(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 = 'debian40';
            $distbaseid = 'debian';
         }  else {
            $distname = 'Debian';
            $distver = 'Unknown';
            $distid = 'debian40';
            $distbaseid = 'debian';
         }
      }
      //** 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';
      } else {
         die('unrecognized linux distribution');
      }
      return array('name' => $distname, 'version' => $distver, 'id' => $distid, 'baseid' => $distbaseid);
   }
    /*
    This method is called every n minutes, when the module ist loaded.
    The method then does a system-monitoring
    */
    // TODO: what monitoring is done should be a config-var
    function doMonitor()
    {
   function doMonitor() {
        /* Calls the single Monitoring steps */
        $this->monitorServer();
      $this->monitorOSVer();
        $this->monitorDiskUsage();
        $this->monitorMemUsage();
        $this->monitorCpu();
@@ -149,13 +268,6 @@
        $tmpUser = explode(" ", trim($tmp[2]));
        $data['user_online'] = intval($tmpUser[0]);
      
      /* Old Load Average Code
        $loadTmp = explode(":" , trim($tmp[3]));
        $load = explode(",",  $loadTmp[1]);
        $data['load_1'] = floatval(trim($load[0]));
        $data['load_5'] = floatval(trim($load[1]));
        $data['load_15'] = floatval(trim($load[2])); */
      //* New Load Average code to fix "always zero" bug in non-english distros. NEEDS TESTING
      $loadTmp = shell_exec("cat /proc/loadavg | cut -f1-3 -d' '");
      $load = explode(" ", $loadTmp);
@@ -187,6 +299,45 @@
        $this->_delOldRecords($type, 10);
    }
   function monitorOsVer() {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'os_info';
      /*
        Fetch the data into a array
      */
      $dist = $this->get_distname();
      $data['name'] = $dist['name'];
      $data['version'] = $dist['version'];
      /* the OS 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, 10);
   }
    function monitorDiskUsage() {
        global $app;
        global $conf;
@@ -210,8 +361,7 @@
         * ignore the first line, process the rest
         */
        for($i=1; $i <= sizeof($df); $i++){
            if ($df[$i] != '')
            {
         if ($df[$i] != '') {
                /*
                 * Make a array of the data
                 */
@@ -257,8 +407,7 @@
    }
    function monitorMemUsage()
    {
   function monitorMemUsage() {
        global $app;
        global $conf;
@@ -308,8 +457,7 @@
    }
    function monitorCpu()
    {
   function monitorCpu() {
        global $app;
        global $conf;
@@ -356,8 +504,7 @@
    }
    function monitorServices()
    {
   function monitorServices() {
        global $app;
        global $conf;
@@ -379,8 +526,7 @@
        /* Monitor Webserver */
        $data['webserver'] = -1; // unknown - not needed
        if ($services['web_server'] == 1)
        {
      if ($services['web_server'] == 1) {
            if($this->_checkTcp('localhost', 80)) {
                $data['webserver'] = 1;
            } else {
@@ -391,8 +537,7 @@
        /* Monitor FTP-Server */
        $data['ftpserver'] = -1; // unknown - not needed
        if ($services['file_server'] == 1)
        {
      if ($services['file_server'] == 1) {
            if($this->_checkFtp('localhost', 21)) {
                $data['ftpserver'] = 1;
            } else {
@@ -403,8 +548,7 @@
        /* Monitor SMTP-Server */
        $data['smtpserver'] = -1; // unknown - not needed
        if ($services['mail_server'] == 1)
        {
      if ($services['mail_server'] == 1) {
            if($this->_checkTcp('localhost', 25)) {
                $data['smtpserver'] = 1;
            } else {
@@ -415,8 +559,7 @@
        /* Monitor POP3-Server */
        $data['pop3server'] = -1; // unknown - not needed
        if ($services['mail_server'] == 1)
        {
      if ($services['mail_server'] == 1) {
            if($this->_checkTcp('localhost', 110)) {
                $data['pop3server'] = 1;
            } else {
@@ -427,8 +570,7 @@
        /* Monitor IMAP-Server */
        $data['imapserver'] = -1; // unknown - not needed
        if ($services['mail_server'] == 1)
        {
      if ($services['mail_server'] == 1) {
            if($this->_checkTcp('localhost', 143)) {
                $data['imapserver'] = 1;
            } else {
@@ -439,8 +581,7 @@
        /* Monitor BIND-Server */
        $data['bindserver'] = -1; // unknown - not needed
        if ($services['dns_server'] == 1)
        {
      if ($services['dns_server'] == 1) {
            if($this->_checkTcp('localhost', 53)) {
                $data['bindserver'] = 1;
            } else {
@@ -451,8 +592,7 @@
        /* Monitor MYSQL-Server */
        $data['mysqlserver'] = -1; // unknown - not needed
        if ($services['db_server'] == 1)
        {
      if ($services['db_server'] == 1) {
            if($this->_checkTcp('localhost', 3306)) {
                $data['mysqlserver'] = 1;
            } else {
@@ -512,13 +652,11 @@
             * if there is any output, then there is a needed update
             */
            $aptData = shell_exec('apt-get -s -qq dist-upgrade');
            if ($aptData == '')
            {
         if ($aptData == '') {
                /* There is nothing to update! */
                $state = 'ok';
            }
            else
            {
         else {
                /* There is something to update! */
                $state = 'warning';
            }
@@ -536,14 +674,12 @@
           
           // In keeping with gentoo's rsync policy, don't update to frequently (every four hours - taken from http://www.gentoo.org/doc/en/source_mirrors.xml)
           $do_update = true;
           if (file_exists('/usr/portage/metadata/timestamp.chk'))
           {
         if (file_exists('/usr/portage/metadata/timestamp.chk')) {
              $datetime = file_get_contents('/usr/portage/metadata/timestamp.chk');
              $datetime = trim($datetime);
              
              $dstamp = strtotime($datetime);
              if ($dstamp)
              {
            if ($dstamp) {
                 $checkat = $dstamp + 14400; // + 4hours
                 if (mktime() < $checkat) {
                    $do_update = false;
@@ -560,13 +696,11 @@
             * if there is any output, then there is a needed update
             */
            $emergeData = shell_exec('emerge -puDNt --color n --nospinner --quiet world');
           if ($emergeData == '')
            {
         if ($emergeData == '') {
                /* There is nothing to update! */
                $state = 'ok';
            }
            else
            {
         else {
                /* There is something to update! */
                $state = 'warning';
            }
@@ -681,8 +815,7 @@
                /* fetch the next line */
                $line = $tmp[$i];
                if ((strpos($line, '[U_]') !== false) || (strpos($line, '[_U]') !== false))
                {
            if ((strpos($line, '[U_]') !== false) || (strpos($line, '[_U]') !== false)) {
                    /* One Disk is not working.
                     * if the next line starts with "[>" or "[=" then
                     * recovery (resync) is in state and the state is
@@ -692,18 +825,15 @@
                    if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) {
                        $state = $this->_setState($state, 'critical');
                    }
                    else
                    {
               else {
                        $state = $this->_setState($state, 'info');
                    }
                }
                if (strpos($line, '[__]') !== false)
                {
            if (strpos($line, '[__]') !== false) {
                    /* both Disk are not working */
                    $state = $this->_setState($state, 'error');
                }
                if (strpos($line, '[UU]') !== false)
                {
            if (strpos($line, '[UU]') !== false) {
                    /* The disks are OK.
                     * if the next line starts with "[>" or "[=" then
                     * recovery (resync) is in state and the state is
@@ -713,8 +843,7 @@
                    if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) {
                        $state = $this->_setState($state, 'ok');
                    }
                    else
                    {
               else {
                        $state = $this->_setState($state, 'info');
                    }
                }
@@ -908,8 +1037,7 @@
        $this->_delOldRecords($type, 10);
    }
   function monitorMailLog()
    {
   function monitorMailLog() {
        global $app;
        global $conf;
@@ -945,8 +1073,7 @@
        $this->_delOldRecords($type, 10);
    }
    function monitorMailWarnLog()
    {
   function monitorMailWarnLog() {
        global $app;
        global $conf;
@@ -982,8 +1109,7 @@
        $this->_delOldRecords($type, 10);
    }
    function monitorMailErrLog()
    {
   function monitorMailErrLog() {
        global $app;
        global $conf;
@@ -1020,8 +1146,7 @@
    }
    function monitorMessagesLog()
    {
   function monitorMessagesLog() {
        global $app;
        global $conf;
@@ -1057,8 +1182,7 @@
        $this->_delOldRecords($type, 10);
    }
    function monitorISPCCronLog()
    {
   function monitorISPCCronLog() {
        global $app;
        global $conf;
@@ -1094,8 +1218,7 @@
        $this->_delOldRecords($type, 10);
    }
    
    function monitorFreshClamLog()
    {
   function monitorFreshClamLog() {
        global $app;
        global $conf;
@@ -1117,13 +1240,11 @@
        $tmp = explode("\n", $data);
        $lastLog = array();
        if ($tmp[sizeof($tmp)-1] == "")
        {
      if ($tmp[sizeof($tmp)-1] == "") {
            /* the log ends with an empty line remove this */
            array_pop($tmp);
        }
        if (strpos($tmp[sizeof($tmp)-1], "-------------") !== false)
        {
      if (strpos($tmp[sizeof($tmp)-1], "-------------") !== false) {
            /* the log ends with "-----..." remove this */
            array_pop($tmp);
        }
@@ -1132,8 +1253,7 @@
                /* no delimiter found, so add this to the last-log */
                $lastLog[] = $tmp[$i];
            }
            else
            {
         else {
                /* delimiter found, so there is no more line left! */
                break;
            }
@@ -1166,8 +1286,7 @@
        $this->_delOldRecords($type, 10);
    }
    function monitorClamAvLog()
    {
   function monitorClamAvLog() {
        global $app;
        global $conf;
@@ -1200,8 +1319,7 @@
        $this->_delOldRecords($type, 10);
    }
    function monitorIspConfigLog()
    {
   function monitorIspConfigLog() {
        global $app;
        global $conf;
@@ -1311,8 +1429,7 @@
            if(stristr($logfile, ';') or substr($logfile,0,9) != '/var/log/' or stristr($logfile, '..')) {
                $log = 'Logfile path error.';
            }
            else
            {
         else {
                $log = '';
                if(is_readable($logfile)) {
                    if($fd = popen("tail -n 100 $logfile", 'r')) {
@@ -1392,8 +1509,7 @@
     * * If the actual state is critical and you call the method with error,
     *   then the state is error.
     */
    function _setState($oldState, $newState)
    {
   function _setState($oldState, $newState) {
        /*
         * Calculate the weight of the old state
         */
@@ -1439,8 +1555,7 @@
        if ($newInt > $oldInt){
            return $newState;
        }
        else
        {
      else {
            return $oldState;
        }
    }
@@ -1458,8 +1573,5 @@
        }
        return $res;
    }
} // end class
}
?>