52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from pyparsing import (Suppress, Keyword, Word, alphas, alphanums, Combine,
|
|
OneOrMore, quotedString, removeQuotes, Group, ZeroOrMore, SkipTo, LineEnd, Regex)
|
|
import pprint
|
|
from collections import defaultdict
|
|
|
|
LBRACE, RBRACE, EQ = map(Suppress, "{}=")
|
|
|
|
DEFINE = Keyword("define")
|
|
|
|
filter_comments = Regex(r"#.*")
|
|
filter_comments = filter_comments.suppress()
|
|
|
|
ident = Word(alphas, alphanums)
|
|
dottedIdent = Combine(ident + OneOrMore("_" + ident))
|
|
|
|
value = Regex('.*')
|
|
propertyDefn = Group((dottedIdent | ident)("name") + value("value"))
|
|
|
|
objectBodyDefn = Group(ZeroOrMore(propertyDefn("properties*")))
|
|
|
|
objectDefn = Group(DEFINE + ident("type") + LBRACE + objectBodyDefn("body") + RBRACE)
|
|
|
|
parser = ZeroOrMore(objectDefn)
|
|
|
|
config = defaultdict(defaultdict)
|
|
for obj in parser.parseFile('objects.cache'):
|
|
if obj.body.properties:
|
|
o = defaultdict(defaultdict)
|
|
for prop in obj.body.properties:
|
|
o[prop.name] = prop.value
|
|
if obj.type in ('service', 'host'):
|
|
config[obj.type][o["host_name"]] = o
|
|
if obj.type == 'timeperiod':
|
|
config[obj.type][o["timeperiod_name"]] = o
|
|
if obj.type == 'command':
|
|
config[obj.type][o["command_name"]] = o
|
|
else:
|
|
print(' ', '<none>')
|
|
print()
|
|
|
|
#pprint.pp(config)
|
|
#from icinga2confgen.Servers.Server import Server
|
|
#from icinga2confgen.ConfigBuilder import ConfigBuilder
|
|
#from string import Template
|
|
|
|
for hostname,obj in config["host"].items():
|
|
template = r'''
|
|
object Host "{hostname}" {{
|
|
address = "{address}"
|
|
check_command = "hostalive"
|
|
}}'''.format(hostname=hostname,address=obj["address"])
|
|
print(template) |