Skip to content
monitor_core_module.inc.php 36.4 KiB
Newer Older
        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, ';')) {
                $log = 'Logfile path error.';
            }
            else
            {
                $log = '';
                if(is_readable($logfile)) {
                    if($fd = popen("tail -n 100 $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) {
            fclose($fp);
            return true;
        } else {
            return false;
        }
    }

    function _checkUdp ($host,$port) {

        $fp = @fsockopen ('udp://'.$host, $port, &$errno, &$errstr, 2);

        if ($fp) {
            fclose($fp);
            return true;
        } else {
            return false;
        }
    }

    function _checkFtp ($host,$port){

        $conn_id = @ftp_connect($host, $port);

        if($conn_id){
            @ftp_close($conn_id);
            return true;
        } else {
            return false;
        }
    }

    /*
     Deletes Records older than n.
    */
    function _delOldRecords($type, $min, $hour=0, $days=0) {
        global $app;

        $now = time();
        $old = $now - ($min * 60) - ($hour * 60 * 60) - ($days * 24 * 60 * 60);
        $sql = "DELETE FROM monitor_data " .
            "WHERE " .
            "type =" . "'" . $app->db->quote($type) . "' " .
            "AND " .
            "created < " . $old;
        $app->db->query($sql);
    }

    /*
     * Set the state to the given level (or higher, but not lesser).
     * * If the actual state is critical and you call the method with ok,
     *   then the state is critical.
     *
     * * If the actual state is critical and you call the method with error,
     *   then the state is error.
     */
    function _setState($oldState, $newState)
    {
        /*
         * Calculate the weight of the old state
         */
        switch ($oldState) {
            case 'no_state': $oldInt = 0;
                break;
            case 'ok': $oldInt = 1;
                break;
            case 'unknown': $oldInt = 2;
                break;
            case 'info': $oldInt = 3;
                break;
            case 'warning': $oldInt = 4;
                break;
            case 'critical': $oldInt = 5;
                break;
            case 'error': $oldInt = 6;
                break;
        }
        /*
         * Calculate the weight of the new state
         */
        switch ($newState) {
            case 'no_state': $newInt = 0 ;
                break;
            case 'ok': $newInt = 1 ;
            case 'unknown': $newInt = 2 ;
                break;
            case 'info': $newInt = 3 ;
                break;
            case 'warning': $newInt = 4 ;
                break;
            case 'critical': $newInt = 5 ;
                break;
            case 'error': $newInt = 6 ;
                break;
        }

        /*
         * Set to the higher level
         */
        if ($newInt > $oldInt){
            return $newState;
        }
        else
        {
            return $oldState;
        }
    }

    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;
    }