From 7162553354e297f7c152144eed24aaecd28e7b43 Mon Sep 17 00:00:00 2001
From: vogelor <vogelor@ispconfig3>
Date: Mon, 24 Nov 2008 21:27:37 +0000
Subject: [PATCH] The monitor now monitors the mailq and (on debian/ubuntu) the
 update-status

---
 interface/web/monitor/lib/module.conf.php     |  41 +++---
 interface/web/monitor/show_data.php           |  12 ++
 interface/web/monitor/show_sys_state.php      |  60 +++++++-
 interface/web/monitor/tools.inc.php           |  45 +++++-
 .../monitor_core_module.inc.php               | 129 ++++++++++++++++++
 5 files changed, 256 insertions(+), 31 deletions(-)

diff --git a/interface/web/monitor/lib/module.conf.php b/interface/web/monitor/lib/module.conf.php
index 2a443e3a1c..93200466b2 100644
--- a/interface/web/monitor/lib/module.conf.php
+++ b/interface/web/monitor/lib/module.conf.php
@@ -14,7 +14,7 @@ $items[] = array( 'title' 	=> "Show System State",
                   'target' 	=> 'content',
                   'link'	=> 'monitor/show_sys_state.php?state=system');
 
-$module["nav"][] = array(	'title'	=> 'System State',
+$module["nav"][] = array(	'title'	=> 'System State (All Servers)',
                             'open' 	=> 1,
                             'items'	=> $items);
 
@@ -51,8 +51,17 @@ $_SESSION['monitor']['server_id']   = $servers[0]['server_id'];
 $_SESSION['monitor']['server_name'] = $servers[0]['server_name'];
 
 /*
- * Logmonitoring module
+ * Clear and set the Navigation-Items
  */
+unset($items);
+
+$items[] = array( 'title' 	=> "Show CPU info",
+        'target' 	=> 'content',
+        'link'	=> 'monitor/show_data.php?type=cpu_info');
+
+$module["nav"][] = array(	'title'	=> 'System-Information',
+        'open' 	=> 1,
+        'items'	=> $items);
 
 /*
  * Clear and set the Navigation-Items
@@ -61,6 +70,14 @@ unset($items);
 $items[] = array( 'title' 	=> "Show Server State",
                   'target' 	=> 'content',
                   'link'	=> 'monitor/show_sys_state.php?state=server');
+/*
+ * The next menu is only available at debian or Ubuntu
+ */
+if(file_exists('/etc/debian_version')){
+$items[] = array( 'title' 	=> "Show Update State",
+                  'target' 	=> 'content',
+                  'link'	=> 'monitor/show_data.php?type=system_update');
+}
 
 $items[] = array( 'title' 	=> "Show Server Load",
                   'target' 	=> 'content',
@@ -78,6 +95,9 @@ $items[] = array( 'title' 	=> "Show Services",
                   'target' 	=> 'content',
                   'link'	=> 'monitor/show_data.php?type=services');
 
+$items[] = array( 'title' 	=> "Show Mailq",
+                  'target' 	=> 'content',
+                  'link'	=> 'monitor/show_data.php?type=mailq');
 
 $module["nav"][] = array(	'title'	=> 'Monitoring',
                             'open' 	=> 1,
@@ -88,23 +108,6 @@ $module["nav"][] = array(	'title'	=> 'Monitoring',
  */
 unset($items);
 
-$items[] = array( 'title' 	=> "Show CPU info",
-        'target' 	=> 'content',
-        'link'	=> 'monitor/show_data.php?type=cpu_info');
-
-$module["nav"][] = array(	'title'	=> 'System-Information',
-        'open' 	=> 1,
-        'items'	=> $items);
-
-/*
- *   Logmonitoring module
- */
-
-/*
- * Clear and set the Navigation-Items
- */
-unset($items);
-
 $items[] = array( 'title' 	=> "Show Mail-Log",
                   'target' 	=> 'content',
                   'link'	=> 'monitor/show_log.php?log=log_mail');
diff --git a/interface/web/monitor/show_data.php b/interface/web/monitor/show_data.php
index 37c77dc6c8..043a20231c 100644
--- a/interface/web/monitor/show_data.php
+++ b/interface/web/monitor/show_data.php
@@ -72,6 +72,18 @@ switch($dataType) {
         $title = $app->lng("Status of services").' (Server: ' . $_SESSION['monitor']['server_name'] . ')';
         $description = '';
         break;
+    case 'system_update':
+        $template = 'templates/show_data.htm';
+        $output .= showSystemUpdate();
+        $title = "Update State" . ' (Server: ' . $_SESSION['monitor']['server_name'] . ')';
+        $description = '';
+        break;
+    case 'mailq':
+        $template = 'templates/show_data.htm';
+        $output .= showMailq();
+        $title = "Mailq" . ' (Server: ' . $_SESSION['monitor']['server_name'] . ')';
+        $description = '';
+        break;
     default:
         $template = '';
         break;
diff --git a/interface/web/monitor/show_sys_state.php b/interface/web/monitor/show_sys_state.php
index 7d4cab4998..83e6068fba 100644
--- a/interface/web/monitor/show_sys_state.php
+++ b/interface/web/monitor/show_sys_state.php
@@ -142,7 +142,7 @@ function _getServerState($serverId, $serverName, $showAll)
             /*
              * There is no need, to show the "ok" - messages
              */
-            if ($key != 'ok')
+//            if ($key != 'ok')
             {
                 $res .= $key . ':<br />';
                 foreach ($state as $msg)
@@ -278,6 +278,52 @@ function _processDbState($type, $serverId, &$serverState, &$messages)
                 break;
         }
     }
+    if ($type == 'system_update'){
+        switch ($record['state']) {
+            case 'ok':
+                $messages['ok'][] = 'Your System is up to date. ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=system_update\");'>[more...]</a>";
+
+                break;
+            case 'warning':
+                $messages['warning'][] = 'One or more Components needs a update ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=system_update\");'>[more...]</a>";
+                break;
+            default:
+                $messages['unknown'][] = 'System-Updatese:??? ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=system_update\");'>[more...]</a>";
+                break;
+        }
+    }
+
+    if ($type == 'mailq'){
+        switch ($record['state']) {
+            case 'ok':
+                $messages['ok'][] = 'Your Mailq load is ok ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
+                break;
+            case 'info':
+                $messages['info'][] = 'Your Mailq in under heavy load ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
+                break;
+            case 'warning':
+                $messages['warning'][] = 'Your Mailq in under high load ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
+                break;
+            case 'critical':
+                $messages['critical'][] = 'Your Mailq in under higher load ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
+                break;
+            case 'error':
+                $messages['error'][] = 'Your Mailq in under highest load ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
+                break;
+            default:
+                $messages['unknown'][] = 'Mailq: ??? ' .
+                                    "<a href='#' onclick='loadContent(\"monitor/show_data.php?type=mailq\");'>[more...]</a>";
+                break;
+        }
+    }
     if ($type == 'log_clamav'){
         /* this type has no state */
     }
@@ -311,9 +357,9 @@ function _processDbState($type, $serverId, &$serverState, &$messages)
   */
 function _setState($oldState, $newState)
 {
-        /*
-         * Calculate the weight of the old state
-         */
+   /*
+    * Calculate the weight of the old state
+    */
     switch ($oldState) {
         case 'no_state': $oldInt = 0;
             break;
@@ -350,9 +396,9 @@ function _setState($oldState, $newState)
             break;
     }
 
-        /*
-         * Set to the higher level
-         */
+   /*
+    * Set to the higher level
+    */
     if ($newInt > $oldInt){
         return $newState;
     }
diff --git a/interface/web/monitor/tools.inc.php b/interface/web/monitor/tools.inc.php
index a23867195e..6f56f5600b 100644
--- a/interface/web/monitor/tools.inc.php
+++ b/interface/web/monitor/tools.inc.php
@@ -11,7 +11,7 @@ function showServerLoad(){
         /*
         Format the data
         */
-        $html .=
+        $html =
         '<table id="system_load">
             <tr>
             <td>' . $app->lng("Server online since").':</td>
@@ -53,7 +53,7 @@ function showDiskUsage () {
         /*
         Format the data
         */
-        $html .= '<table id="system_disk">';
+        $html = '<table id="system_disk">';
         foreach($data as $line) {
             $html .= '<tr>';
             foreach ($line as $item) {
@@ -84,7 +84,7 @@ function showMemUsage ()
         /*
         Format the data
         */
-        $html .= '<table id="system_memusage">';
+        $html = '<table id="system_memusage">';
 
         foreach($data as $key => $value){
             if ($key != '') {
@@ -115,7 +115,7 @@ function showCpuInfo ()
         /*
         Format the data
         */
-        $html .= '<table id="system_cpu">';
+        $html = '<table id="system_cpu">';
         foreach($data as $key => $value){
             if ($key != '') {
                 $html .= '<tr>
@@ -145,7 +145,7 @@ function showServices ()
         /*
         Format the data
         */
-        $html .= '<table id="system_services">';
+        $html = '<table id="system_services">';
 
         if($data['webserver'] != -1) {
             if($data['webserver'] == 1) {
@@ -239,6 +239,41 @@ function showServices ()
     }
 
 
+    return $html;
+}
+
+function showSystemUpdate()
+{
+    global $app;
+
+    /* fetch the Data from the DB */
+    $record = $app->db->queryOneRecord("SELECT data, state FROM monitor_data WHERE type = 'system_update' and server_id = " . $_SESSION['monitor']['server_id'] . " order by created desc");
+
+    if(isset($record['data'])) {
+        $data = unserialize($record['data']);
+        $html = nl2br($data['output']);
+    } else {
+        $html = '<p>' . "No Update-Data available" . '</p>';
+    }
+
+    return $html;
+}
+
+
+function showMailq()
+{
+    global $app;
+
+    /* fetch the Data from the DB */
+    $record = $app->db->queryOneRecord("SELECT data, state FROM monitor_data WHERE type = 'mailq' and server_id = " . $_SESSION['monitor']['server_id'] . " order by created desc");
+
+    if(isset($record['data'])) {
+        $data = unserialize($record['data']);
+        $html = nl2br($data['output']);
+    } else {
+        $html = '<p>' . "No Mailq-Data available" . '</p>';
+    }
+
     return $html;
 }
 ?>
diff --git a/server/mods-available/monitor_core_module.inc.php b/server/mods-available/monitor_core_module.inc.php
index f89625be73..aff1c38240 100644
--- a/server/mods-available/monitor_core_module.inc.php
+++ b/server/mods-available/monitor_core_module.inc.php
@@ -107,6 +107,8 @@ class monitor_core_module {
         $this->monitorFreshClamLog();
         $this->monitorClamAvLog();
         $this->monitorIspConfigLog();
+        $this->monitorSystemUpdate();
+        $this->monitorMailQueue();
     }
 
     function monitorServer(){
@@ -448,6 +450,119 @@ class monitor_core_module {
 
     }
 
+
+    function monitorSystemUpdate(){
+        /* This monitoring is only available at debian or Ubuntu */
+        if(!file_exists('/etc/debian_version')) return;
+
+        /*
+         *  This monitoring is expensive, so do it only once a hour!
+         */
+        $min = date('i');
+        if ($min != 0) return;
+
+        /*
+         * OK - here we go...
+         */
+        global $app;
+        global $conf;
+
+        /* the id of the server as int */
+        $server_id = intval($conf["server_id"]);
+
+        /** The type of the data */
+        $type = 'system_update';
+
+        /* There is only ONE Update-Data, so delete the old one */
+        $this->_delOldRecords($type, 0);
+
+        /*
+         * first update the "update-database"
+         */
+        shell_exec('apt-get update');
+
+        /*
+         * Then test the upgrade.
+         * if there is any output, then there is a needed update
+         */
+        $aptData = shell_exec('apt-get -s -qq dist-upgrade');
+        if ($aptData == '')
+        {
+            /* There is nothing to update! */
+            $state = 'ok';
+        }
+        else
+        {
+            /* There is something to update! */
+            $state = 'warning';
+        }
+
+        /*
+         * Fetch the output
+         */
+        $data['output'] = shell_exec('apt-get -s -q dist-upgrade');
+
+        /*
+         * 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 monitorMailQueue(){
+        global $app;
+        global $conf;
+
+        /* the id of the server as int */
+        $server_id = intval($conf["server_id"]);
+
+        /** The type of the data */
+        $type = 'mailq';
+
+        /* There is only ONE Update-Data, so delete the old one */
+        $this->_delOldRecords($type, 0);
+
+        /* Get the data from the mailq */
+        $data['output'] = shell_exec('mailq');
+
+        /*
+         *  The last line has more informations
+         */
+        $tmp = explode("\n", $data['output']);
+        $more = $tmp[sizeof($tmp) - 1];
+        $this->_getIntArray($more);
+        $data['bytes'] = $res[0];
+        $data['requests'] = $res[1];
+
+        /** The state of the mailq. */
+        $state = 'ok';
+        if ($data['requests'] > 2000 ) $state = 'info';
+        if ($data['requests'] > 5000 ) $state = 'warning';
+        if ($data['requests'] > 8000 ) $state = 'critical';
+        if ($data['requests'] > 10000 ) $state = 'error';
+
+        /*
+         * 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;
@@ -872,6 +987,20 @@ class monitor_core_module {
         }
     }
 
+    function _getIntArray($line){
+        /** The array of float found */
+        $res = array();
+        /* First build a array from the line */
+        $data = explode(' ', $line);
+        /* then check if any item is a float */
+        foreach ($data as $item) {
+            if ($item . '' == (int)$item . ''){
+                $res[] = $item;
+            }
+        }
+        return $res;
+    }
+
 
 } // end class
 
-- 
GitLab