Search in sources :

Example 1 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class LogNetworkStreamMerger method readLogBatch.

/**
 * Read a batch of logs that share the same timestamp and return the first subsequent
 * log that has a different timestamp.
 *
 * @param startLog the starting log of the current log batch
 * @param logBatch the current log batch to fill in
 * @return the starting log of the next log batch
 * @throws IOException
 * @throws CompressorException
 */
public LogMessage readLogBatch(LogMessage startLog, List<LogMessage> logBatch) throws IOException, CompressorException {
    long batchTime = startLog.getTime();
    logBatch.add(startLog);
    LogMessage msg;
    while ((msg = readNextMergedLogMessage()) != null) {
        if (msg.getTime() == batchTime) {
            logBatch.add(msg);
        } else {
            return msg;
        }
    }
    // msg == null, i.e., no more logs to read
    return null;
}
Also used : LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage)

Example 2 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class LogNetworkStreamMerger method streamLogs.

public void streamLogs(OutputStream outputStream) {
    logger.trace("Entering into LogNetworkStreamMerger.streamLogs()");
    CountingOutputStream cos = new CountingOutputStream(new BufferedOutputStream(outputStream, LogConstants.BUFFER_SIZE));
    Marshaller marshaller = MarshallerFactory.getLogMarshaller(mediaType, cos);
    int finalCount = 0;
    try {
        marshaller.head();
        LogMessage msg = readNextMergedLogMessage();
        if (msg != null) {
            do {
                // Maximum bytes to stream check
                streamedBytes = cos.getByteCount();
                if (request.getMaxBytes() > 0 && streamedBytes >= request.getMaxBytes()) {
                    logger.info("Streamed log size {}bytes reached maximum allowed limit {}bytes. So quitting.", streamedBytes, request.getMaxBytes());
                    break;
                }
                List<LogMessage> currentLogBatch = new ArrayList<>();
                LogMessage startLogOfNextBatch = readLogBatch(msg, currentLogBatch);
                if (!LogUtil.permitNextLogBatch(request.getMaxCount(), finalCount, currentLogBatch.size())) {
                    // discard this batch
                    break;
                }
                // current log batch has been accepted
                for (String st : status.getStatus()) {
                    // use previous log message's timeStamp as status's timeStamp
                    marshaller.marshall(st, msg);
                }
                status.clear();
                for (LogMessage logMessage : currentLogBatch) {
                    marshaller.marshall(logMessage);
                    finalCount++;
                }
                msg = startLogOfNextBatch;
            } while (msg != null);
        }
        logger.info("final count={}", finalCount);
        marshaller.tail();
        marshaller.flush();
    } catch (Exception e) {
        logger.error("Exception in streamLogs:", e);
    }
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) Marshaller(com.emc.storageos.systemservices.impl.logsvc.marshaller.Marshaller) LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) ArrayList(java.util.ArrayList) BufferedOutputStream(java.io.BufferedOutputStream) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) SocketTimeoutException(java.net.SocketTimeoutException) CompressorException(org.apache.commons.compress.compressors.CompressorException) IOException(java.io.IOException) InternalServerErrorException(com.emc.storageos.svcs.errorhandling.resources.InternalServerErrorException)

Example 3 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class LogNginxAccessParser method parseLine.

@Override
public LogMessage parseLine(String line, LogRequest info) {
    Date date = null;
    String msg = null;
    String[] splitLine = line.split(" ");
    if (splitLine.length < 10) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    // splitLine[3] is like "[15/May/2014:06:16:21"
    String timeStr = splitLine[3].substring(1);
    if (timeStr.length() != TIME_LENGTH) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    DateFormat format = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
    try {
        date = format.parse(timeStr);
    } catch (Exception e) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    int inTime = inTimeRange(date, info);
    if (inTime < 0) {
        // too early
        return LogMessage.REJECTED_LOGMESSAGE;
    } else if (inTime > 0) {
        // too late
        return LogMessage.REJECTED_LAST_LOGMESSAGE;
    }
    int logOffset = line.indexOf(splitLine[5]);
    int timeBytesStartIndex = line.indexOf(splitLine[3]);
    if (logOffset > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    if (timeBytesStartIndex + 1 > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    LogMessage log = new LogMessage(date.getTime(), line.getBytes());
    log.setLogOffset(logOffset);
    log.setTimeBytes(timeBytesStartIndex + 1, TIME_LENGTH);
    // put the initial message (IP and user) as a following line.
    log.appendMessage(line.substring(0, timeBytesStartIndex).getBytes());
    return log;
}
Also used : LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class LogServiceParser method parseLine.

/**
 * Parse line from file to LogMessage
 * If line does not match log format(it
 * is the message part for multiple lines log), return
 * LogMessage.CONTINUATION_LOGMESSAGE; if line matches log formant, time is
 * too late for time filter, return LogMessage.REJECTED_LAST_LOGMESSAGE if
 * line matches log formant, but does not match all filters, return
 * LogMessage.REJECTED_LOGMESSAGE; if line matches log formant and all
 * filters, return LogMessage object
 */
@Override
public LogMessage parseLine(String line, LogRequest info) {
    int lineLength = line.length();
    if (lineLength == ViPRHeaderPatternLayout.HEADER_START_LENGTH + HEADER_TIMESTAMP_LENGTH) {
        boolean isHeaderStart = true;
        for (int i = 0; i < ViPRHeaderPatternLayout.HEADER_START_LENGTH; i++) {
            if (line.charAt(i) != ViPRHeaderPatternLayout.HEADER_START_INDICATOR) {
                isHeaderStart = false;
                break;
            }
        }
        if (isHeaderStart) {
            // find the timestamp at the end of the header start line
            String timestampStr = line.substring(ViPRHeaderPatternLayout.HEADER_START_LENGTH);
            long timestamp = Long.parseLong(timestampStr);
            int inTime = LogUtil.timeInRange(new Date(timestamp), info.getStartTime(), info.getEndTime());
            if (inTime < 0) {
                // too early
                return LogMessage.REJECTED_LOGMESSAGE;
            } else if (inTime > 0) {
                // too late
                return LogMessage.REJECTED_LAST_LOGMESSAGE;
            }
            LogMessage header = LogMessage.makeHeaderLog(timestamp);
            return header;
        }
    }
    if (lineLength <= TIME_LENGTH || line.charAt(4) != '-' || line.charAt(7) != '-' || line.charAt(10) != ' ' || line.charAt(13) != ':' || line.charAt(16) != ':' || line.charAt(19) != ',' || line.charAt(23) != ' ' || line.charAt(24) != '[') {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    /*
         * final String[] parts = line.substring(0,
         * timeLength).split("[\\s-:,]"); if (parts.length < 7) { return
         * LogMessage.CONTINUATION_LOGMESSAGE; }
         * 
         * int[] partsInt = new int[7]; for (int i = 0; i < 7; i++) {
         * partsInt[i] = toNumber(parts[i]); if (partsInt[i] < 0) { return
         * LogMessage.CONTINUATION_LOGMESSAGE; } }
         */
    String yearStr = line.substring(0, 4);
    int year = toNumber(yearStr);
    if (year < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String monthStr = line.substring(5, 7);
    int month = toNumber(monthStr);
    if (month < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String dayStr = line.substring(8, 10);
    int day = toNumber(dayStr);
    if (day < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String hourStr = line.substring(11, 13);
    int hour = toNumber(hourStr);
    if (hour < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String minStr = line.substring(14, 16);
    int min = toNumber(minStr);
    if (min < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String secStr = line.substring(17, 19);
    int sec = toNumber(secStr);
    if (sec < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String msStr = line.substring(20, 23);
    int ms = toNumber(msStr);
    if (ms < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String timeStr = line.substring(0, TIME_LENGTH - 2);
    final int endBracket = line.indexOf("]", TIME_LENGTH);
    if (endBracket < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    if (endBracket - TIME_LENGTH > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    // Skip white spaces before level
    int levelStartIndex = endBracket + 1;
    while (levelStartIndex < lineLength && line.charAt(levelStartIndex) == ' ') {
        levelStartIndex++;
    }
    if (levelStartIndex >= lineLength || levelStartIndex > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    // Find the next white space after level
    int levelEndIndex = line.indexOf(' ', levelStartIndex);
    if (levelEndIndex < 0 || levelEndIndex - levelStartIndex > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    final String levelString = line.substring(levelStartIndex, levelEndIndex);
    final int level = LogSeverity.toLevel(levelString);
    if (level < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    int fileNameStartIndex = levelEndIndex + 1;
    // Skip white spaces before file name
    while (fileNameStartIndex < lineLength && line.charAt(fileNameStartIndex) == ' ') {
        fileNameStartIndex++;
    }
    if (fileNameStartIndex >= lineLength || fileNameStartIndex > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    // Find the next white space after file name
    final int fileNameEndIndex = line.indexOf(' ', fileNameStartIndex);
    if (fileNameEndIndex < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    if (fileNameEndIndex + 1 >= lineLength || fileNameEndIndex - fileNameStartIndex > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    if (line.charAt(fileNameEndIndex + 1) != '(') {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    int classNameStartIndex = fileNameStartIndex;
    int classNameEndIndex = fileNameEndIndex;
    if (line.charAt(fileNameEndIndex - 5) == '.') {
        // remove the trailing .java
        classNameEndIndex -= 5;
    }
    int rightParentheseIndex = line.indexOf(')', fileNameEndIndex + 1);
    if (rightParentheseIndex <= 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String[] lineNumber = line.substring(fileNameEndIndex + 1, rightParentheseIndex).split(" ");
    if (lineNumber.length != 2) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    int lineNo = toNumber(lineNumber[1]);
    if (lineNo < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    // rightParentheseIndex+1 is a white space
    int messageStartIndex = rightParentheseIndex + 2;
    if (rightParentheseIndex + 2 > lineLength) {
        // it could be true, like a stacktrace of an exception
        // in which case the first line is ""
        messageStartIndex = lineLength;
    }
    if (messageStartIndex > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    // test time filter
    // int inTime = inTimeRange(partsInt[0], partsInt[1], partsInt[2],
    // partsInt[3], partsInt[4], partsInt[5], partsInt[6],info);
    int inTime = inTimeRange(year, month, day, hour, min, sec, ms, info);
    if (inTime < 0) {
        // too early
        return LogMessage.REJECTED_LOGMESSAGE;
    } else if (inTime > 0) {
        // too late
        return LogMessage.REJECTED_LAST_LOGMESSAGE;
    }
    int matchLevel = matchLevelFilter(level, info);
    if (matchLevel > 0) {
        // level value bigger than request
        return LogMessage.REJECTED_LOGMESSAGE;
    }
    final int lineNumberStartIndex = line.indexOf(' ', fileNameEndIndex + 1) + 1;
    if (lineNumberStartIndex > Short.MAX_VALUE || rightParentheseIndex - lineNumberStartIndex > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    LogMessage log = new LogMessage(getTime(year, month, day, hour, min, sec, ms), line.getBytes());
    log.setLogOffset(messageStartIndex);
    log.setTimeBytes(0, TIME_LENGTH - 2);
    log.setThreadName(TIME_LENGTH, endBracket - TIME_LENGTH);
    log.setLevel(level);
    log.setFileName(classNameStartIndex, classNameEndIndex - classNameStartIndex);
    log.setLineNumber(lineNumberStartIndex, rightParentheseIndex - lineNumberStartIndex);
    return log;
}
Also used : LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) Date(java.util.Date)

Example 5 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class LogSyslogParser method parseLine.

/**
 * Parse line from file to LogMessage If line does not match log format(it
 * is the message part for multiple lines log), return
 * LogMessage.CONTINUATION_LOGMESSAGE; if line matches log formant, time is
 * too late for time filter, return LogMessage.REJECTED_LAST_LOGMESSAGE if
 * line matches log formant, but does not match all filters, return
 * LogMessage.REJECTED_LOGMESSAGE; if line matches log formant and all
 * filters, return LogMessage object
 */
@Override
public LogMessage parseLine(String line, LogRequest info) {
    // length of the time 2013-11-20 13:56:48 [
    int lineLength = line.length();
    if (lineLength <= TIME_LENGTH || line.charAt(4) != '-' || line.charAt(7) != '-' || line.charAt(10) != ' ' || line.charAt(13) != ':' || line.charAt(16) != ':' || line.charAt(19) != ' ' || line.charAt(20) != '[') {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String yearStr = line.substring(0, 4);
    int year = toNumber(yearStr);
    if (year < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String monthStr = line.substring(5, 7);
    int month = toNumber(monthStr);
    if (month < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String dayStr = line.substring(8, 10);
    int day = toNumber(dayStr);
    if (day < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String hourStr = line.substring(11, 13);
    int hour = toNumber(hourStr);
    if (hour < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String minStr = line.substring(14, 16);
    int min = toNumber(minStr);
    if (min < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String secStr = line.substring(17, 19);
    int sec = toNumber(secStr);
    if (sec < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    String timeStr = line.substring(0, TIME_LENGTH - 2);
    final int endBracket = line.indexOf("]", TIME_LENGTH);
    if (endBracket < 0 || endBracket - TIME_LENGTH > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    // endBracket + 1 is
    final int priorityStartIndex = endBracket + 2;
    // white space
    if (priorityStartIndex > lineLength || priorityStartIndex > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    final int priorityEndIndex = line.indexOf(" ", priorityStartIndex);
    if (priorityEndIndex + 1 > lineLength || priorityEndIndex + 1 > Short.MAX_VALUE || priorityEndIndex - priorityStartIndex > Short.MAX_VALUE) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    // test time filter
    int inTime = inTimeRange(year, month, day, hour, min, sec, 0, info);
    if (inTime < 0) {
        // too early
        return LogMessage.REJECTED_LOGMESSAGE;
    } else if (inTime > 0) {
        // too late
        return LogMessage.REJECTED_LAST_LOGMESSAGE;
    }
    final String priority = line.substring(priorityStartIndex, priorityEndIndex);
    final int level = LogSeverity.toLevel(priority);
    if (level < 0) {
        return LogMessage.CONTINUATION_LOGMESSAGE;
    }
    int matchLevel = matchLevelFilter(level, info);
    if (matchLevel > 0) {
        // level value bigger then request
        return LogMessage.REJECTED_LOGMESSAGE;
    }
    LogMessage log = new LogMessage(getTime(year, month, day, hour, min, sec, 0), line.getBytes());
    log.setLogOffset(priorityEndIndex + 1);
    log.setTimeBytes(0, TIME_LENGTH - 2);
    log.setThreadName(TIME_LENGTH, endBracket - TIME_LENGTH);
    log.setLevel(level);
    return log;
}
Also used : LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage)

Aggregations

LogMessage (com.emc.storageos.systemservices.impl.logsvc.LogMessage)25 LogRequest (com.emc.vipr.model.sys.logging.LogRequest)15 Test (org.junit.Test)15 LogStatusInfo (com.emc.storageos.systemservices.impl.logsvc.LogStatusInfo)12 Date (java.util.Date)9 LogReader (com.emc.storageos.systemservices.impl.logsvc.stream.LogReader)7 ArrayList (java.util.ArrayList)7 Calendar (java.util.Calendar)6 File (java.io.File)5 Ignore (org.junit.Ignore)5 LogFileStream (com.emc.storageos.systemservices.impl.logsvc.stream.LogFileStream)4 LogStreamMerger (com.emc.storageos.systemservices.impl.logsvc.merger.LogStreamMerger)3 IOException (java.io.IOException)3 BufferedOutputStream (java.io.BufferedOutputStream)2 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)1 InternalServerErrorException (com.emc.storageos.svcs.errorhandling.resources.InternalServerErrorException)1