tbrehm
2008-11-22 6f67c78cf8668e570c5ef7fd4430e3d0e4a66aef
server/mods-available/monitor_core_module.inc.php
@@ -100,6 +100,16 @@
      /* Calls the single Monitoring steps */
      $this->monitorServer();
      $this->monitorDiskUsage();
      $this->monitorMemUsage();
      $this->monitorCpu();
      $this->monitorServices();
      $this->monitorMailLog();
      $this->monitorMailWarnLog();
      $this->monitorMailErrLog();
      $this->monitorMessagesLog();
      $this->monitorFreshClamLog();
      $this->monitorClamAvLog();
      $this->monitorIspConfigLog();
   }
   
   function monitorServer(){
@@ -143,8 +153,8 @@
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $conf["server_id"] . ", " .
         "'" . $app->db->quote(serialize($type)) . "', " .
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" . 
@@ -168,14 +178,10 @@
      /*
      Fetch the data into a array
      */
      $fd = popen ("df", "r");
      $buffer = '';
      while (!feof($fd)) {
         $buffer .= fgets($fd, 4096);
      }
      $dfData = shell_exec("df");
      
      // split into array
      $df = split("\n", $buffer);
      $df = explode("\n", $dfData);
      // ignore the first line make a array of the rest
      for($i=1; $i <= sizeof($df); $i++){
         if ($df[$i] != '')
@@ -198,206 +204,519 @@
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $conf["server_id"] . ", " .
         "'" . $app->db->quote(serialize($type)) . "', " .
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" . 
         ")";
      $app->db->query($sql);
   }
   //
   //
   //   function show_memusage ()
   //   {
   //      global $app;
   //
   //      $html_out .= '<table id="system_memusage">';
   //
   //      $fd = fopen ("/proc/meminfo", "r");
   //      while (!feof($fd)) {
   //         $buffer .= fgets($fd, 4096);
   //      }
   //      fclose($fd);
   //
   //      $meminfo = split("\n",$buffer);
   //
   //      foreach($meminfo as $mline){
   //         if($x > 2 and trim($mline) != "") {
   //
   //            $mpart = split(":",$mline);
   //
   //            $html_out .= '<tr>
   //                  <td>'.$mpart[0].':</td>
   //                  <td>'.$mpart[1].'</td>
   //                  </tr>';
   //         }
   //
   //         $x++;
   //      }
   //      $html_out .= '</table>';
   //      return $html_out;
   //   }
   //
   //   function show_cpu ()
   //   {
   //      global $app;
   //
   //      $html_out .= '<table id="system_cpu">';
   //
   //      $n = 0;
   //      if(is_readable("/proc/cpuinfo")) {
   //         if($fd = fopen ("/proc/cpuinfo", "r")) {
   //            while (!feof($fd)) {
   //               $buffer .= fgets($fd, 4096);
   //               $n++;
   //               if($n > 100) break;
   //            }
   //            fclose($fd);
   //         }
   //      }
   //
   //      $meminfo = split("\n",$buffer);
   //
   //      if(is_array($meminfo)) {
   //         foreach($meminfo as $mline){
   //            if(trim($mline) != "") {
   //
   //               $mpart = split(":",$mline);
   //
   //               $html_out .= '<tr>
   //                     <td>'.$mpart[0].':</td>
   //                     <td>'.$mpart[1].'</td>
   //                     </tr>';
   //            }
   //         }
   //
   //         $x++;
   //      }
   //      $html_out .= '</table></div>';
   //
   //
   //      return $html_out;
   //   }
   //
   //   function show_services ()
   //   {
   //      global $app;
   //
   //      $html_out .= '<table id="system_services">';
   //
   //      // Checke Webserver
   //      if(_check_tcp('localhost',80)) {
   //         $status = '<span class="online">Online</span>';
   //      } else {
   //         $status = '<span class="offline">Offline</span>';
   //      }
   //      $html_out .= '<tr>
   //            <td>Web-Server:</td>
   //            <td>'.$status.'</td>
   //            </tr>';
   //
   //
   //      // Checke FTP-Server
   //      if(_check_ftp('localhost',21)) {
   //         $status = '<span class="online">Online</span>';
   //      } else {
   //         $status = '<span class="offline">Offline</span>';
   //      }
   //      $html_out .= '<tr>
   //            <td>FTP-Server:</td>
   //            <td>'.$status.'</td>
   //            </tr>';
   //
   //      // Checke SMTP-Server
   //      if(_check_tcp('localhost',25)) {
   //         $status = '<span class="online">Online</span>';
   //      } else {
   //         $status = '<span class="offline">Offline</span>';
   //      }
   //      $html_out .= '<tr>
   //            <td>SMTP-Server:</td>
   //            <td>'.$status.'</td>
   //            </tr>';
   //
   //      // Checke POP3-Server
   //      if(_check_tcp('localhost',110)) {
   //         $status = '<span class="online">Online</span>';
   //      } else {
   //         $status = '<span class="offline">Offline</span>';
   //      }
   //      $html_out .= '<tr>
   //            <td>POP3-Server:</td>
   //            <td>'.$status.'</td>
   //            </tr>';
   //
   //      // Checke BIND-Server
   //      if(_check_tcp('localhost',53)) {
   //         $status = '<span class="online">Online</span>';
   //      } else {
   //         $status = '<span class="offline">Offline</span>';
   //      }
   //      $html_out .= '<tr>
   //            <td>DNS-Server:</td>
   //            <td>'.$status.'</td>
   //            </tr>';
   //
   //      // Checke MYSQL-Server
   //      //if($this->_check_tcp('localhost',3306)) {
   //      $status = '<span class="online">Online</span>';
   //      //} else {
   //      //$status = '<span class="offline">Offline</span>';
   //      //}
   //      $html_out .= '<tr>
   //            <td>mySQL-Server:</td>
   //            <td>'.$status.'</td>
   //            </tr>';
   //
   //
   //      $html_out .= '</table></div>';
   //
   //
   //      return $html_out;
   //   }
   //
   //   function _check_tcp ($host,$port) {
   //
   //      $fp = @fsockopen ($host, $port, &$errno, &$errstr, 2);
   //
   //      if ($fp) {
   //         return true;
   //         fclose($fp);
   //      } else {
   //         return false;
   //         fclose($fp);
   //      }
   //   }
   //
   //   function _check_udp ($host,$port) {
   //
   //      $fp = @fsockopen ('udp://'.$host, $port, &$errno, &$errstr, 2);
   //
   //      if ($fp) {
   //         return true;
   //         fclose($fp);
   //      } else {
   //         return false;
   //         fclose($fp);
   //      }
   //   }
   //
   //   function _check_ftp ($host,$port){
   //
   //      $conn_id = @ftp_connect($host, $port);
   //
   //      if($conn_id){
   //         @ftp_close($conn_id);
   //         return true;
   //      } else {
   //         @ftp_close($conn_id);
   //         return false;
   //      }
   //   }
   function monitorMemUsage()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'mem_usage';
      /* Delete Data older than 10 minutes */
      $this->_delOldRecords($type, 10);
      /*
      Fetch the data into a array
      */
      $miData = shell_exec("cat /proc/meminfo");
      $memInfo = explode("\n", $miData);
      foreach($memInfo as $line){
         $part = split(":", $line);
         $key = trim($part[0]);
         $tmp = explode(" ", trim($part[1]));
         $value = 0;
         if ($tmp[1] == 'kB') $value = $tmp[0] * 1024;
         $data[$key] = $value;
      }
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorCpu()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'cpu_info';
      /* There is only ONE CPU-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /*
      Fetch the data into a array
      */
      $cpuData = shell_exec("cat /proc/cpuinfo");
      $cpuInfo = explode("\n", $cpuData);
      foreach($cpuInfo as $line){
         $part = split(":", $line);
         $key = trim($part[0]);
         $value = trim($part[1]);
         $data[$key] = $value;
      }
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorServices()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'services';
      /* There is only ONE Service-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /* Monitor Webserver */
      if($this->_checkTcp('localhost',80)) {
         $data['webserver'] = true;
      } else {
         $data['webserver'] = false;
      }
      /* Monitor FTP-Server */
      if($this->_checkFtp('localhost',21)) {
         $data['ftpserver'] = true;
      } else {
         $data['ftpserver'] = false;
      }
      /* Monitor SMTP-Server */
      if($this->_checkTcp('localhost',25)) {
         $data['smtpserver'] = true;
      } else {
         $data['smtpserver'] = false;
      }
      /* Monitor POP3-Server */
      if($this->_checkTcp('localhost',110)) {
         $data['pop3server'] = true;
      } else {
         $data['pop3server'] = false;
      }
      /* Monitor BIND-Server */
      if($this->_checkTcp('localhost',53)) {
         $data['bindserver'] = true;
      } else {
         $data['bindserver'] = false;
      }
      /* Monitor MYSQL-Server */
      if($this->_checkTcp('localhost',3306)) {
         $data['mysqlserver'] = true;
      } else {
         $data['mysqlserver'] = false;
      }
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorMailLog()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'log_mail';
      /* There is only ONE Log-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /* Get the data of the log */
      $data = $this->_getLogData($type);
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorMailWarnLog()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'log_mail_warn';
      /* There is only ONE Log-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /* Get the data of the log */
      $data = $this->_getLogData($type);
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorMailErrLog()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'log_mail_err';
      /* There is only ONE Log-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /* Get the data of the log */
      $data = $this->_getLogData($type);
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorMessagesLog()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'log_messages';
      /* There is only ONE Log-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /* Get the data of the log */
      $data = $this->_getLogData($type);
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorFreshClamLog()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'log_freshclam';
      /* There is only ONE Log-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /* Get the data of the log */
      $data = $this->_getLogData($type);
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorClamAvLog()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'log_clamav';
      /* There is only ONE Log-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /* Get the data of the log */
      $data = $this->_getLogData($type);
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function monitorIspConfigLog()
   {
      global $app;
      global $conf;
      /* the id of the server as int */
      $server_id = intval($conf["server_id"]);
      /** The type of the data */
      $type = 'log_ispconfig';
      /* There is only ONE Log-Data, so delete the old one */
      $this->_delOldRecords($type, 0);
      /* Get the data of the log */
      $data = $this->_getLogData($type);
      // Todo: the state should be calculated. For example if the load is to heavy, the state is warning...
      $state = 'ok';
      /*
      Insert the data into the database
      */
      $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
         "VALUES (".
         $server_id . ", " .
         "'" . $app->db->quote($type) . "', " .
         time() . ", " .
         "'" . $app->db->quote(serialize($data)) . "', " .
         "'" . $state . "'" .
         ")";
      $app->db->query($sql);
   }
   function _getLogData($log){
      switch($log) {
         case 'log_mail':
            $logfile = '/var/log/mail.log';
            break;
         case 'log_mail_warn':
            $logfile = '/var/log/mail.warn';
            break;
         case 'log_mail_err':
            $logfile = '/var/log/mail.err';
            break;
         case 'log_messages':
            $logfile = '/var/log/messages';
            break;
         case 'log_freshclam':
            $logfile = '/var/log/clamav/freshclam.log';
            break;
         case 'log_clamav':
            $logfile = '/var/log/clamav/clamav.log';
            break;
         case 'log_ispconfig':
            $logfile = '/var/log/ispconfig/ispconfig.log';
            break;
         default:
            $logfile = '';
            break;
      }
      // Getting the logfile content
      if($logfile != '') {
         $logfile = escapeshellcmd($logfile);
         if(stristr($logfile,';')) die('Logfile path error.');
         $log = '';
         if(is_readable($logfile)) {
            if($fd = popen("tail -n 30 $logfile", 'r')) {
               while (!feof($fd)) {
                  $log .= fgets($fd, 4096);
                  $n++;
                  if($n > 1000) break;
               }
               fclose($fd);
            }
         } else {
            $log = 'Unable to read '.$logfile;
         }
      }
      return $log;
   }
   function _checkTcp ($host,$port) {
         $fp = @fsockopen ($host, $port, &$errno, &$errstr, 2);
         if ($fp) {
            return true;
            fclose($fp);
         } else {
            return false;
            fclose($fp);
         }
      }
      function _checkUdp ($host,$port) {
         $fp = @fsockopen ('udp://'.$host, $port, &$errno, &$errstr, 2);
         if ($fp) {
            return true;
            fclose($fp);
         } else {
            return false;
            fclose($fp);
         }
      }
      function _checkFtp ($host,$port){
         $conn_id = @ftp_connect($host, $port);
         if($conn_id){
            @ftp_close($conn_id);
            return true;
         } else {
            @ftp_close($conn_id);
            return false;
         }
      }
   
   /*
    Deletes Records older than n.
@@ -409,7 +728,7 @@
      $old = $now - ($min * 60) - ($hour * 60 * 60) - ($days * 24 * 60 * 60);
      $sql = "DELETE FROM monitor_data " .
         "WHERE " .
         "type =" . "'" . $app->db->quote(serialize($type)) . "' " .
         "type =" . "'" . $app->db->quote($type) . "' " .
         "AND " .   
         "created < " . $old;
      $app->db->query($sql);