Search code examples
pythonpython-2.7syslogpyparsing

PyParsing and multi-line syslog messages


I have copy-pasted a PyParsing syslog parser from here and there. It's all nice and fluffy, but I have some Syslog messages that look non-compliant to the "standard":

Apr  2 09:23:09 dawn Java App[537]: [main] ERROR ch.java.core.Verifier - Unknown validation error

        java.lang.NullPointerException
                at org.databin.cms.CMSSignedData.getSignedData(Unknown Source)
                at org.databin.cms.CMSSignedData.<init>(Unknown Source)
                at org.databin.cms.CMSSignedData.<init>(Unknown Source)

And so on. Now with my PyParsing grammar I go through syslog.log line by line.

def main():
    with open("system.log", "r") as myfile:
        data = myfile.readlines()
        pattern = Parser()._pattern
        pattern.runTests(data)

if __name__ == '__main__':
    main()

I somehow need to handle multi-line syslog messages. Either I need

  • to attach the many lines of these Java exceptions to the Syslog message, that has already been parsed.
  • or make the left side optional.

I don't know. Right now my implementation fails, because it assumes a new line is logged by a new app. Which would be... usual... unless Java...

> Traceback (most recent call last):   File
> "/Users/wishi/PycharmProjects/Sparky_1/syslog_to_spark.py", line 39,
> in <module>
>     main()   File "/Users/wishi/PycharmProjects/Sparky_1/syslog_to_spark.py", line 34,
> in main
>     pattern.runTests(data)   File "/Users/wishi/anaconda2/envs/sparky/lib/python2.7/site-packages/pyparsing.py",
> line 2305, in runTests
>     if comment is not None and comment.matches(t, False) or comments and not t:   File
> "/Users/wishi/anaconda2/envs/sparky/lib/python2.7/site-packages/pyparsing.py",
> line 2205, in matches
>     self.parseString(_ustr(testString), parseAll=parseAll)   File "/Users/wishi/anaconda2/envs/sparky/lib/python2.7/site-packages/pyparsing.py",
> line 1622, in parseString
>     loc, tokens = self._parse( instring, 0 )   File "/Users/wishi/anaconda2/envs/sparky/lib/python2.7/site-packages/pyparsing.py",
> line 1383, in _parseNoCache
>     loc,tokens = self.parseImpl( instring, preloc, doActions )   File "/Users/wishi/anaconda2/envs/sparky/lib/python2.7/site-packages/pyparsing.py",
> line 2410, in parseImpl
>     if (instring[loc] == self.firstMatchChar and IndexError: string index out of range

Does anyone know a simple way to avoid failure here?

    from pyparsing import Word, alphas, Suppress, Combine, nums, string, Regex, Optional, ParserElement, LineEnd, OneOrMore, \
    unicodeString, White
import sys
from datetime import datetime


class Parser(object):
    # log lines don't include the year, but if we don't provide one, datetime.strptime will assume 1900
    ASSUMED_YEAR = str(datetime.now().year)

    def __init__(self):
        ints = Word(nums)

        ParserElement.setDefaultWhitespaceChars(" \t")
        NL = Suppress(LineEnd())
        unicodePrintables = u''.join(unichr(c) for c in xrange(sys.maxunicode)
                                     if not unichr(c).isspace())

        # priority
        # priority = Suppress("<") + ints + Suppress(">")

        # timestamp
        month = Word(string.ascii_uppercase, string.ascii_lowercase, exact=3)
        day = ints
        hour = Combine(ints + ":" + ints + ":" + ints)

        timestamp = month + day + hour
        # a parse action will convert this timestamp to a datetime
        timestamp.setParseAction(
            lambda t: datetime.strptime(Parser.ASSUMED_YEAR + ' ' + ' '.join(t), '%Y %b %d %H:%M:%S'))

        # hostname
        # usually hostnames follow some convention
        hostname = Word(alphas + nums + "_-.")

        # appname
        # if you call your app "my big fat app with a very long name" go away
        appname = (Word(alphas + nums + "/-_.()") + Optional(Word(" ")) + Optional(Word(alphas + nums + "/-_.()")))(
            "appname") + (Suppress("[") + ints("pid") + Suppress("]")) | (Word(alphas + "/-_.")("appname"))
        appname.setName("appname")

        # message
        # supports messages with printed unicode
        message = Combine(OneOrMore(Word(unicodePrintables) | OneOrMore("\t") | OneOrMore(" "))) +  Suppress(OneOrMore(NL))
        messages = OneOrMore(message) # does not work

        # pattern build
        # (add results names to make it easier to access parsed fields)
        self._pattern = timestamp("timestamp") + hostname("hostname") + Optional(appname) + Optional(Suppress(':')) + messages("message")

    def parse(self, line):
        if line.strip():
            parsed = self._pattern.parseString(line)
            return parsed.asDict()

The partly parsed result is:

[datetime.datetime(2018, 4, 2, 9, 23, 9), 'dawn', 'Java', 'App', '537', '[main] ERROR ch.databin.core.Verifier - Unknown validation error']
- appname: ['Java', 'App']
- hostname: 'dawn'
- message: '[main] ERROR ch.databin.core.Verifier - Unknown validation error'
- pid: '537'
- timestamp: datetime.datetime(2018, 4, 2, 9, 23, 9)

It only contains the first line.

So for syslog messages without linebreaks this works.


Solution

  • The simplest solution is to go back to parsing a line at a time, and keep the valid log lines in a list. If you get a valid log line, just append it to the list; if you don't then append it to the 'messages' item of the last line in the list.

    def main():
        valid_log_lines = []
        with open("system.log", "r") as myfile:
            data = myfile.read()
            pattern = Parser()._pattern
            for line in data.splitlines():
                try:
                    log_dict = pattern.parse(line)
                    if log_dict is None:
                        continue
                except ParseException:
                    if valid_log_lines:
                        valid_log_lines[-1]['message'] += '\n' + line
                else:
                    valid_log_lines.append(log_dict)
    

    To speed up detection of invalid lines, try adding timestamp.leaveWhitespace(), so that any line that does not start with a timestamp in column 1 will immediately fail.

    Or you can modify your parser to handle multi-line log messages, that is a longer topic.

    I like that you were using runTests, but that is more of a development tool; in your actual code, probably use parseString or one of its ilk.