How to parse nagios status.dat file?

前端 未结 7 1133
一生所求
一生所求 2020-12-21 02:06

I\'d like to parse status.dat file for nagios3 and output as xml with a python script. The xml part is the easy one but how do I go about parsing the file? Use multi line re

7条回答
  •  一向
    一向 (楼主)
    2020-12-21 02:37

    If you slightly tweak Andrea's solution you can use that code to parse both the status.dat as well as the objects.cache

    def parseConf(source):
    conf = []
    for line in source.splitlines():
        line=line.strip()
        matchID = re.match(r"(?:\s*define)?\s*(\w+)\s+{", line)
        matchAttr = re.match(r"\s*(\w+)(?:=|\s+)(.*)", line)
        matchEndID = re.match(r"\s*}", line)
        if len(line) == 0 or line[0]=='#':
            pass
        elif matchID:
            identifier = matchID.group(1)
            cur = [identifier, {}]
        elif matchAttr:
            attribute = matchAttr.group(1)
            value = matchAttr.group(2).strip()
            cur[1][attribute] = value
        elif matchEndID and cur:
            conf.append(cur)
            del cur
    return conf
    

    It is a little puzzling why nagios chose to use two different formats for these files, but once you've parsed them both into some usable python objects you can do quite a bit of magic through the external command file.

    If anybody has a solution for getting this into a a real xml dom that'd be awesome.

提交回复
热议问题