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(' ', '') 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) for hostname,obj in config["service"].items(): template = r''' object Service "{service}" {{ import "generic-service" host_name = "{hostname}" check_command = "{command}" check_interval = {check_interval}m retry_interval = {retry_interval}m }}'''.format(hostname=hostname, service=obj["service_description"], check_interval=int(float(obj["check_interval"])), retry_interval=int(float(obj["retry_interval"])),command=obj["check_command"]) print(template) for command, obj in config["command"].items(): args = obj["command_line"].split(' ') template = r''' object CheckCommand "{command_name}" {{ command = [ PluginDir + "/{command_name_arg}", # {command_name_args} ] arguments = {{ "-H" = "$ping_address$" "-w" = "$ping_wrta$,$ping_wpl$%" "-c" = "$ping_crta$,$ping_cpl$%" "-p" = "$ping_packets$" "-t" = "$ping_timeout$" }} vars.arg_address = "$address$" vars.ping_wrta = 100 vars.ping_wpl = 5 vars.ping_crta = 200 vars.ping_cpl = 15 }} '''.format(command_name=command, command_name_arg=args[0].split('/')[-1], command_name_args=' '.join(args[1:-1])) print(template)