Search code examples
pythonparsingdictionaryloggingbgp

Parse Logs and store Dict


BGP LOGs File: bgplog.log

Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of BGP UPDATE messages received changed from '110376' to '110393'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of BGP UPDATE messages received changed from '133736' to '134146'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of BGP UPDATE messages sent changed from '108252' to '108348'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of BGP UPDATE messages sent changed from '2094' to '2132'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.103: the number of BGP UPDATE messages sent changed from '91440' to '91462'
Host local.domain.net [11.130.55.2] with interface to BGP peer eth1-local.domain.net [11.8.44.10]: the number of BGP UPDATE messages sent changed from '1411' to '1413'
Host local.domain.net [11.130.55.2] with interface to BGP peer 10.81.244.18: the number of BGP UPDATE messages sent changed from '112347' to '112506'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of messages received from the remote peer changed from '538672' to '538691'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of messages received from the remote peer changed from '547397' to '547814'

Objective:

  1. Find specific IP, let say: 11.130.44.93
  2. Any line matches the IP above, split the lines and store certain values to the each key.
  3. Sorted the key

Here is the code I tried: Seems I'm stuck

import re
import os


def find(line):
    findThis = ""
    found = re.match(r'.*?11.130.44.103.*', line)
    # Find is true:
    if found:
        # found a item and adds in to findThis
        findThis = found.group()
    else:
        findThis = "NONE"
    return findThis


def generateDicts(log):
    currentDict = {}
    for line in log:
        if line.startswith(find(line)):
            currentDict = {
                "host": line.split(" ")[1][:24],
                "ip": line.split(" ", 9)[2],
                "peer": line.split(" ")[8],
                "info": line.split(" ", 9)[-1]}
        else:
            # currentDict = {"info":line.split("-",6)[-1]}
            currentDict = line


with open("bgplog.txt") as f:
    print list(generateDicts(f))

I'm only getting the last value and somehow it is not appending. What would be the best approach?


Solution

  • I'm guessing you want a generator, in which case you need to use the yield keyword. Try this:

    def generateDicts(log):
        for line in log:
            if line.startswith(find(line)):
                yield {
                    "host": line.split(" ")[1][:24],
                    "ip": line.split(" ", 9)[2],
                    "peer": line.split(" ")[8],
                    "info": line.split(" ", 9)[-1]}