示例#1
0
def get_hash():
    """
    Function to return the rendered hash.
    Usefull when other object need values from this one.
    """
    return render(build_hash('serviceTemplate'), get_commands_hash(),
                  build_hash('contact'), False)
def actions(object_name, contact_hash):
    """
    @TODO : Add something here
    Passing the contact_hash so that every object doesn't need to generate it.
    """
    settings.outputfile = settings.outputdir + object_name + '.conf'

    # Depending on the object type different actions need to be taken
    if object_name in ['host']:
        hosts.render(build_hash(object_name), contact_hash)
    elif object_name in ['hostTemplate']:
        hostTemplates.render(build_hash(object_name), contact_hash)
    elif object_name in ['hostgroup']:
        hostGroups.render(build_hash(object_name))
    elif object_name in ['service']:
        services.render(build_hash(object_name), commands.get_hash(), serviceTemplates.get_hash(), contact_hash)
    elif object_name in ['serviceTemplate']:
        serviceTemplates.render(build_hash(object_name), commands.get_hash(), contact_hash)
    elif object_name in ['servicegroup']:
        serviceGroups.render(build_hash(object_name))
    elif object_name in ['command']:
        commands.render(build_hash(object_name))
    elif object_name in ['contact']:
        contacts.render(contact_hash, build_hash('contactgroup'))
    elif object_name in ['resource']:
        resource.render(object_name)
    # @TODO: Render lists of what contact gets what notifications?
    #elif object_name in ['notification']:
    #    build_icinga_notifications(inputdir,outputfile)
    elif object_name in ['all']:
        for object_n in settings.objects_all:
            info('Building configuration for: ' + object_n)
            actions(object_n, contact_hash)
            info('')
def get_hash():
    """
    Function to return the rendered hash.
    Usefull when other objects need values from this one.
    """
    return render(build_hash('command'), False)
    # Depending on the object type different actions need to be taken
    if object_name in ['host']:
        hosts.render(build_hash(object_name), contact_hash)
    elif object_name in ['hostTemplate']:
        hostTemplates.render(build_hash(object_name), contact_hash)
    elif object_name in ['hostgroup']:
        hostGroups.render(build_hash(object_name))
    elif object_name in ['service']:
        services.render(build_hash(object_name), commands.get_hash(), serviceTemplates.get_hash(), contact_hash)
    elif object_name in ['serviceTemplate']:
        serviceTemplates.render(build_hash(object_name), commands.get_hash(), contact_hash)
    elif object_name in ['servicegroup']:
        serviceGroups.render(build_hash(object_name))
    elif object_name in ['command']:
        commands.render(build_hash(object_name))
    elif object_name in ['contact']:
        contacts.render(contact_hash, build_hash('contactgroup'))
    elif object_name in ['resource']:
        resource.render(object_name)
    # @TODO: Render lists of what contact gets what notifications?
    #elif object_name in ['notification']:
    #    build_icinga_notifications(inputdir,outputfile)
    elif object_name in ['all']:
        for object_n in settings.objects_all:
            info('Building configuration for: ' + object_n)
            actions(object_n, contact_hash)
            info('')

actions(settings.object_name, build_hash('contact'))
示例#5
0
def build_icinga_notifications(inputdir, outputfile):
    """
    Function to build notification objects.
    object Notification "<notificationname>" {
      import "mail-host-notification"
      host_name = "<thishostname>"
      command = "<notificationcommandname>"
      states = [ OK, Warning, Critical ]
      types = [ Recovery, Problem, Custom ]
      period = "24x7"
      users = [ "<contactwithnotificationcommand>" ]
    }
    """
    # Header
    header = '# File generated by script, do not edit!\n'
    write_configfile(header, outputfile)

    #Defaults
    write_blocks = 0
    service_notifications = OrderedDict({})
    host_notifications = OrderedDict({})

    # Get the needed hashes
    host_hash = build_hash('host', inputdir)
    hosttemplate_hash = build_hash('hostTemplate', inputdir)
    service_hash = build_hash('service', inputdir)
    servicetemplate_hash = build_hash('serviceTemplate', inputdir)

    for service in service_hash:
        debug('--------------------')
        debug(service)
        debug('--------------------')
        #debug_3(service_hash[service])

        # Setup the notifications hash
        sane_service = service.replace('!', '_').replace('/',
                                                         '').replace('"', '')
        service_notifications[sane_service] = OrderedDict({})
        service_notifications[sane_service]['host'] = service_hash[service][
            'config']['host_name']

        # Get some contact information
        if 'contacts' in service_hash[service]['config']:
            service_notifications[sane_service]['contacts'] = service_hash[
                service]['config']['contacts']
        if 'contact_groups' in service_hash[service]['config']:
            service_notifications[sane_service][
                'contact_groups'] = service_hash[service]['config'][
                    'contact_groups']
        if 'notification_period' in service_hash[service]['config']:
            service_notifications[sane_service][
                'notification_period'] = service_hash[service]['config'][
                    'notification_period']
        if 'notification_interval' in service_hash[service]['config']:
            service_notifications[sane_service][
                'notification_interval'] = service_hash[service]['config'][
                    'notification_interval']
        if 'notification_options' in service_hash[service]['config']:
            state, types = convert_options(
                service_hash[service]['config']['notification_options'])
            service_notifications[sane_service]['state'] = state
            service_notifications[sane_service]['type'] = types
        if 'notifications_enabled' in service_hash[service]['config']:
            service_notifications[sane_service][
                'notifications_enabled'] = service_hash[service]['config'][
                    'notifications_enabled']

        # Print all the collected attributes
        for key in service_notifications[sane_service]:
            debug(key + ' : ' + str(service_notifications[sane_service][key]))

    for host in host_hash:
        debug('--------------------')
        debug(host)
        debug('--------------------')
        #debug_3(host_hash[host])

        # Setup the notifications hash
        sane_host = host
        host_notifications[host] = OrderedDict({})

        #Form a list of all the contacts involved
        if 'contacts' in host_hash[host]:
            host_notifications[sane_host]['contacts'] = host_hash[host][
                'contacts']
        if 'contact_groups' in host_hash[host]:
            host_notifications[sane_host]['contact_groups'] = host_hash[host][
                'contact_groups']
        if 'notification_period' in host_hash[host]:
            host_notifications[sane_host]['notification_period'] = host_hash[
                host]['notification_period']
        if 'notification_interval' in host_hash[host]:
            host_notifications[sane_host]['notification_interval'] = host_hash[
                host]['notification_interval']
        if 'notification_options' in host_hash[host]:
            state, types = convert_options(
                host_hash[host]['notification_options'])
            host_notifications[sane_host]['state'] = state
            host_notifications[sane_host]['type'] = types
        if 'notifications_enabled' in host_hash[host]:
            host_notifications[sane_host]['notifications_enabled'] = host_hash[
                host]['notifications_enabled']

        # Print all the collected attributes
        for key in host_notifications[sane_host]:
            debug(key + ' : ' + str(host_notifications[sane_host][key]))