tbrehm
2008-12-12 ec09b18c9c44f85ceb6d9e7588a03a221cd1193f
server/mods-available/monitor_core_module.inc.php
@@ -1,7 +1,6 @@
<?php
/*
Copyright (c) 2008, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
Copyright (c) 2007-2008, 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,
@@ -27,6 +26,7 @@
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class monitor_core_module {
    /* TODO: this should be a config - var instead of a "constant" */
    var $interval = 5; // do the monitoring every 5 minutes
@@ -36,7 +36,16 @@
    /* No actions at this time. maybe later... */
    var $actions_available = array();
    /*
    //* This function is called during ispconfig installation to determine
   //  if a symlink shall be created for this plugin.
   function onInstall() {
      global $conf;
      return true;
   }
   /*
        This function is called when the module is loaded
    */
    function onLoad() {
@@ -107,6 +116,10 @@
        $this->monitorFreshClamLog();
        $this->monitorClamAvLog();
        $this->monitorIspConfigLog();
        $this->monitorSystemUpdate();
        $this->monitorMailQueue();
        $this->monitorRaid();
        $this->monitorRkHunter();
    }
    function monitorServer(){
@@ -119,9 +132,6 @@
        /** The type of the data */
        $type = 'server_load';
        /* Delete Data older than 1 day */
        $this->_delOldRecords($type, 0, 0, 1);
        /*
        Fetch the data into a array
        */
@@ -132,11 +142,11 @@
        $data['uptime'] = shell_exec("uptime");
        $tmp = explode(",", $data['uptime'], 3);
        $tmpUser = explode(" ", trim($tmp[1]));
        $tmp = explode(",", $data['uptime'], 4);
        $tmpUser = explode(" ", trim($tmp[2]));
        $data['user_online'] = intval($tmpUser[0]);
        $loadTmp = explode(":" , trim($tmp[2]));
        $loadTmp = explode(":" , trim($tmp[3]));
        $load = explode(",",  $loadTmp[1]);
        $data['load_1'] = floatval(trim($load[0]));
        $data['load_5'] = floatval(trim($load[1]));
@@ -155,12 +165,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
    function monitorDiskUsage() {
@@ -172,9 +185,6 @@
        /** The type of the data */
        $type = 'disk_usage';
        /* Delete Data older than 10 minutes */
        $this->_delOldRecords($type, 10);
        /** The state of the disk-usage */
        $state = 'ok';
@@ -219,12 +229,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
@@ -238,9 +251,6 @@
        /** The type of the data */
        $type = 'mem_usage';
        /* Delete Data older than 10 minutes */
        $this->_delOldRecords($type, 10);
        /*
        Fetch the data into a array
@@ -270,12 +280,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
@@ -289,9 +302,6 @@
        /** 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
@@ -315,12 +325,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
@@ -333,13 +346,10 @@
        $server_id = intval($conf["server_id"]);
        /** get the "active" Services of the server from the DB */
        $services = $app->db->queryOneRecord("SELECT * FROM server WHERE server_id = " . $server_id);
        $services = $app->dbmaster->queryOneRecord("SELECT * FROM server WHERE server_id = " . $server_id);
        /* The type of the Monitor-data */
        $type = 'services';
        /* There is only ONE Service-Data, so delete the old one */
        $this->_delOldRecords($type, 0);
        /** the State of the monitoring */
        /* ok, if ALL aktive services are running,
@@ -439,13 +449,284 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
    function monitorSystemUpdate(){
        /*
         *  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';
        /* This monitoring is only available at debian or Ubuntu */
        if(file_exists('/etc/debian_version')){
            /*
             * 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');
        }
        else {
            /*
             * It is not debian/Ubuntu, 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...
             */
            $state = 'no_state';
            $data['output']= '';
        }
        /*
         * 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, 0, 2);
    }
    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';
        /* 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->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 monitorRaid(){
        global $app;
        global $conf;
        /* the id of the server as int */
        $server_id = intval($conf["server_id"]);
        /** The type of the data */
        $type = 'raid_state';
        /* This monitoring is only available if mdadm is installed */
        $location = shell_exec('which mdadm');
        if($location != ''){
            /*
             * Fetch the output
             */
            $data['output'] = shell_exec('cat /proc/mdstat');
            /*
             * Then calc the state.
             */
            $tmp = explode("\n", $data['output']);
            $state = 'ok';
            for ($i = 0; $i < sizeof($tmp); $i++){
                /* fetch the next line */
                $line = $tmp[$i];
                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
                     * information instead of critical
                     */
                    $nextLine = $tmp[$i+1];
                    if ((strpos($nextLine, '[>') === false) && (strpos($nextLine, '[=') === false)) {
                        $state = $this->_setState($state, 'critical');
                    }
                    else
                    {
                        $state = $this->_setState($state, 'info');
                    }
                }
                if (strpos($line, '[__]') !== false)
                {
                    /* both Disk are not working */
                    $state = $this->_setState($state, 'error');
                }
            }
        }
        else {
            /*
             * 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...
             */
            $state = 'no_state';
            $data['output']= '';
        }
        /*
         * 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 monitorRkHunter(){
        /*
         *  This monitoring is expensive, so do it only once a hour
         */
        $min = date('i');
        if ($min != 0) return;
        global $app;
        global $conf;
        /* the id of the server as int */
        $server_id = intval($conf["server_id"]);
        /** The type of the data */
        $type = 'rkhunter';
        /* This monitoring is only available if rkhunter is installed */
        $location = shell_exec('which rkhunter');
        if($location != ''){
            /*
             * Fetch the output
             */
            $data['output'] = shell_exec('rkhunter --update --checkall --nocolors --skip-keypress');
            /*
             * At this moment, there is no state (maybe later)
             */
            $state = 'no_state';
        }
        else {
            /*
             * rkhunter 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...
             */
            $state = 'no_state';
            $data['output']= '';
        }
        /*
         * 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, 0, 2);
    }
    function monitorMailLog()
@@ -459,9 +740,6 @@
        /** 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);
@@ -477,12 +755,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
    function monitorMailWarnLog()
@@ -496,10 +777,6 @@
        /** 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);
@@ -515,12 +792,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
    function monitorMailErrLog()
@@ -534,10 +814,6 @@
        /** 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);
@@ -553,12 +829,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
@@ -573,9 +852,6 @@
        /** 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);
@@ -591,12 +867,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
    function monitorFreshClamLog()
@@ -610,15 +889,49 @@
        /** 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.
        /* Get the data from the LAST log-Entry.
         * if there can be found:
         * WARNING: Your ClamAV installation is OUTDATED!
         * then the clamav is outdated. This is a warning!
         */
        $state = 'ok';
        $tmp = explode("\n", $data);
        $lastLog = array();
        if ($tmp[sizeof($tmp)-1] == "")
        {
            /* the log ends with an empty line remove this */
            array_pop($tmp);
        }
        if (strpos($tmp[sizeof($tmp)-1], "-------------") !== false)
        {
            /* the log ends with "-----..." remove this */
            array_pop($tmp);
        }
        for ($i = sizeof($tmp) -1; $i > 0; $i--){
            if (strpos($tmp[$i], "---------") === false){
                /* no delimiter found, so add this to the last-log */
                $lastLog[] = $tmp[$i];
            }
            else
            {
                /* delimiter found, so there is no more line left! */
                break;
            }
        }
        /*
         * Now we have the last log in the array.
         * Check if the outdated-string is found...
         */
        foreach($lastLog as $line){
            if (strpos(strtolower($line), "outdated") !== false) {
                 $state = $this->_setState($state, 'warning');
            }
        }
        /*
        Insert the data into the database
@@ -626,12 +939,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
    function monitorClamAvLog()
@@ -645,9 +961,6 @@
        /** 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);
@@ -660,12 +973,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
    function monitorIspConfigLog()
@@ -679,10 +995,6 @@
        /** 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);
@@ -695,12 +1007,15 @@
        $sql = "INSERT INTO monitor_data (server_id, type, created, data, state) " .
            "VALUES (".
        $server_id . ", " .
            "'" . $app->db->quote($type) . "', " .
            "'" . $app->dbmaster->quote($type) . "', " .
        time() . ", " .
            "'" . $app->db->quote(serialize($data)) . "', " .
            "'" . $app->dbmaster->quote(serialize($data)) . "', " .
            "'" . $state . "'" .
            ")";
        $app->db->query($sql);
        $app->dbmaster->query($sql);
        /* The new data is written, now we can delete the old one */
        $this->_delOldRecords($type, 10);
    }
@@ -805,10 +1120,10 @@
        $old = $now - ($min * 60) - ($hour * 60 * 60) - ($days * 24 * 60 * 60);
        $sql = "DELETE FROM monitor_data " .
            "WHERE " .
            "type =" . "'" . $app->db->quote($type) . "' " .
            "type =" . "'" . $app->dbmaster->quote($type) . "' " .
            "AND " .
            "created < " . $old;
        $app->db->query($sql);
        $app->dbmaster->query($sql);
    }
    /*
@@ -872,6 +1187,20 @@
        }
    }
    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