Esempio n. 1
0
 def load_log_message(self, message: Message):
     """ Adds a log message to messages
     :param message: an object of type Message
     :return: the attribute messages with an additional element
     """
     key = message.get_length()
     if key in self.messages:
         self.messages[key].append(message)
     else:
         self.messages[key] = []
         self.messages[key].append(message)
Esempio n. 2
0
def adapt_log_message(log_message: str, regex=None):
    # first detect time (12:34:56 or 12:54 )
    # date 21-03-2005 03/04/12
    # ip address
    # memory address
    # file path
    # mac address
    # before adding space around ':'

    is_time = re.findall('(^|\s+)(\d){1,2}:(\d){1,2}(|:(\d){2,4})(\s+|$)', log_message)
    if is_time:
        log_message = re.sub('(^|\s+)(\d){1,2}:(\d){1,2}(|:(\d){2,4})(\s+|$)', "   #spec#   ", log_message)

    is_date = re.findall('(^|\s)(\d{1,2}(-|/)\d{1,2}(-|/)\d{2,4})(\s|$)', log_message)
    if is_date:
        log_message = re.sub('(^|\s)(\d{1,2}(-|/)\d{1,2}(-|/)\d{2,4})(\s|$)', "   #spec#   ", log_message)

    is_ip_address1 = re.findall('(/|)([0-9]+\.){3}[0-9]+(:[0-9]+|)', log_message)
    if is_ip_address1:
        log_message = re.sub('(/|)([0-9]+\.){3}[0-9]+(:[0-9]+|)', "   #spec#   ", log_message)

    is_ip_address = re.findall('(|^)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(|$)', log_message)
    if is_ip_address:
        log_message = re.sub('(|^)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(|$)', "   #spec#   ", log_message)

    is_file_path = re.findall('(/[\d+\w+\-_\.\#\$]*[/\.][\d+\w+\-_\.\#\$/*]*)+', log_message)
    if is_file_path:
        log_message = re.sub('(|\w+)(/[\d+\w+\-_\.\#\$]*[/\.][\d+\w+\-_\.\#\$/*]*)+', "   #spec#   ", log_message)

    is_memory_address = re.findall('0x([a-zA-Z]|[0-9])+', log_message)
    if is_memory_address:
        log_message = re.sub('0x([a-zA-Z]|[0-9])+', "   #spec#   ", log_message)

    is_hex = re.findall('(^|\s)([0-9A-F]){9,}(\s|$)', log_message)
    if is_hex:
        log_message = re.sub('(^|\s)([0-9A-F]){9,}(\s|$)', "   #spec#   ", log_message)

    is_hex1 = re.findall('(^|\s)([0-9a-f]){8,}(\s|$)', log_message)
    if is_hex1:
        log_message = re.sub('(^|\s)([0-9a-f]){8,}(\s|$)', "   #spec#   ", log_message)

    is_mac_address = re.findall('([0-9A-F]{2}[:-]){5,}([0-9A-F]{2})', log_message)
    if is_mac_address:
        log_message = re.sub('([0-9A-F]{2}[:-]){5,}([0-9A-F]{2})', "   #spec#   ", log_message)

    is_number = re.findall('(^| )\d+( |$)', log_message)
    if is_number:
        log_message = re.sub('(^| )\d+( |$)', "   #spec#   ", log_message)

    # add space around '-->' [=:,] <> () [ ] { }
    log_message = re.sub("([<>=:,;'\(\)\{\}\[\]])", r' \1 ', log_message)
    # regex is empty if not given as parameter
    if regex is None:
        regex = []
    for i in range(len(regex)):
        match = re.findall(regex[i], log_message)
        if match:
            log_message = re.sub(regex[i], "   #spec#   ", log_message)

    # let's identify the prefixes that we don't have to change
    prefix_regex = "^[ ]*\[[A-Z *]+\]"
    match = re.findall(prefix_regex, log_message)
    if len(match) > 0:
        log_message = log_message.replace(match[0], match[0].replace(' ', ''))

    log_message = log_message.replace("-- >", "-->")
    message = Message(log_message.split())

    for index in range(0, message.get_length()):
        match = False
        match = match or re.findall(".xml", message.words[index]) # xml files
        if match:
            message.words[index] = '#spec#'
    return message