Example #1
0
def overwriteAllNagiosConfigFiles():
    #Todo: Have this actualy write nagios config file out.
    files = glob.glob('/nagiosetc/conf.d/*.cfg')
    for f in files:
        os.remove(f)

    hosts = Host.get_all()

    for host in hosts:
        writeNagiosConfigFile(host)
    return True
Example #2
0
def getHostList(tag=None, display_name=None):
    print(f"getHostList(tag={tag}, display_name={display_name})")

    if tag:
        host_list = findHostsByTag(tag)
    elif display_name:
        host_list = findHostsByDisplayName(display_name)
    else:
        host_list = Host.get_all()

    json_host_list = [host.to_json() for host in host_list]

    # FIXME: pagination
    return {'count': 0, 'results': json_host_list}, 200
Example #3
0
    def get(self, jwt):
        host_data = []
        hosts = Host.get_all()
        status_data = get_nagios_status()
        for host in hosts:
            status_data_hosts = status_data['hosts']
            host_status = ""
            host_name = host.host_name
            if status_data is not None and 'hosts' in status_data.keys(
            ) and len(status_data_hosts) > 0:
                h_name = host_name.strip()
                if h_name in status_data['hosts']:
                    current_state = status_data['hosts'][h_name][
                        'current_state']
                    if current_state == "0":
                        host_status = "UP"
                    elif current_state == "1":
                        host_status = "DOWN"
                    elif current_state == "2":
                        host_status = "DOWN"
                    else:
                        host_status = "DOWN"
                else:
                    host_status = "DOWN"

            host_data.append({
                'id': host.id,
                'ip_address': host.address,
                'street_address': host.street_address,
                'host_name': host_name,
                'host_status': host_status,
            })

        return jsonify({
            "data": host_data,
        })
Example #4
0
def syncNagiosAllConfigWithDb():
    # Delete all nagios config files
    deleteNagiosAllConfigFile()

    # Delete all relation tables
    Contact2Group.delete_all()
    ContactService.delete_all()
    ContactgroupService.delete_all()
    HostContact.delete_all()
    HostContactGroup.delete_all()
    HostService.delete_all()
    HostgroupHost.delete_all()
    Service2Group.delete_all()

    # Sync all settings
    # sync timerperiods table
    timeperiods = Timeperiod.get_all()
    if timeperiods is not None:
        for timeperiod in timeperiods:
            timeperioditems = Timeperioditem.get_by_timeperiodid(timeperiod.id)
            writeNagiosTimeperiodsConfigFile(timeperiod, timeperioditems)

    # sync servicetemplate tables
    servicetemplates = ServiceTemplate.get_all()
    if servicetemplates is not None:
        for servicetemplate in servicetemplates:
            writeNagiosServiceTemplatesConfigFile(servicetemplate)

    # sync commands table
    commands = Command.get_all()
    if commands is not None:
        for command in commands:
            writeNagiosCommandsConfigFile(command)

    # sync servicegroups table
    servicegroups = ServiceGroup.get_all()
    if servicegroups is not None:
        for servicegroup in servicegroups:
            writeNagiosServiceGroupsConfigFile(servicegroup)

    # sync contactgroups table
    contactgroups = ContactGroup.get_all()
    if contactgroups is not None:
        for contactgroup in contactgroups:
            writeNagiosContactGroupsConfigFile(contactgroup)

    # sync contacts table
    contacts = Contact.get_all()
    contact_to_group_relations = []
    if contacts is not None:
        for contact in contacts:
            writeNagiosContactsConfigFile(contact)
            contactgroup_str = contact.contactgroups

            if contactgroup_str is not None and len(
                    contactgroup_str) > 0 and contactgroup_str != "NULL":
                contactgroups = contactgroup_str.split(',')

                for contactgroup_name in contactgroups:
                    contactgroup = ContactGroup.get_by_contactgroupname(
                        contactgroup_name)
                    if contactgroup is None:
                        continue

                    # insert contact_contactgroup table
                    newrelation = Contact2Group(
                        contact_id=contact.id, contactgroup_id=contactgroup.id)
                    contact_to_group_relations.append(newrelation)
        Contact2Group.save_all(contact_to_group_relations)

    # sync hostgroups table
    hostgroups = Hostgroup.get_all()
    hots_to_group_relations = []
    if hostgroups is not None:
        for hostgroup in hostgroups:
            writeNagiosHostgroupsConfigFile(hostgroup)
            hostgroup_str = hostgroup.members

            if hostgroup_str is not None and len(
                    hostgroup_str) > 0 and hostgroup_str != "NULL":
                members = hostgroup_str.split(',')

                for member in members:
                    host = Host.get_by_hostname(member)
                    if host is None:
                        continue

                    # insert hostgroup_host table
                    newrelation = HostgroupHost(hostgroup_id=hostgroup.id,
                                                host_id=host.id)
                    hots_to_group_relations.append(newrelation)
        HostgroupHost.save_all(hots_to_group_relations)

    # sync services table
    services = Service.get_all()
    hots_to_service_relations = []
    contact_to_service_relations = []
    contactgroup_to_service_relations = []
    service_to_group_relations = []
    if services is not None:
        for service in services:
            tmp_checkInterval = service.check_interval
            service.check_interval = round(int(service.check_interval) / 60, 1)
            writeNagiosServicesConfigFile(service)
            service.check_interval = tmp_checkInterval

            # Create relation table between hosts and services
            host_str = service.host_name
            if host_str is not None and len(
                    host_str) > 0 and host_str != "NULL":
                hosts = host_str.split(',')

                for hname in hosts:
                    hostname = Host.get_by_hostname(hname)
                    if hostname is None:
                        continue

                    # insert host_service table
                    newhostrelation = HostService(service_id=service.id,
                                                  host_id=hostname.id)
                    hots_to_service_relations.append(newhostrelation)

            # Create relation table between contacts and services
            contact_str = service.contacts
            if contact_str is not None and len(
                    contact_str) > 0 and contact_str != "NULL":
                contacts = contact_str.split(',')

                for contact in contacts:
                    contactname = Contact.get_by_contactname(contact)
                    if contactname is None:
                        continue

                    # insert contact_service table
                    newcontactrelation = ContactService(
                        service_id=service.id, contact_id=contactname.id)
                    contact_to_service_relations.append(newcontactrelation)

            # Create relation table between contactgroups and services
            contactgroup_str = service.contact_groups
            if contactgroup_str is not None and len(
                    contactgroup_str) > 0 and contactgroup_str != "NULL":
                contact_groups = contactgroup_str.split(',')

                for contactgroup in contact_groups:
                    contactgroupname = ContactGroup.get_by_contactgroupname(
                        contactgroup)
                    if contactgroupname is None:
                        continue

                    # insert contactgroup_service table
                    newgrouprelation = ContactgroupService(
                        service_id=service.id,
                        contactgroup_id=contactgroupname.id)
                    contactgroup_to_service_relations.append(newgrouprelation)

            # Create relation table between services and servicegroups
            servicegroup_str = service.servicegroups
            if servicegroup_str is not None and len(
                    servicegroup_str) > 0 and servicegroup_str != "NULL":
                servicegroups = servicegroup_str.split(',')

                for servicegroup_name in servicegroups:
                    servicegroup = ServiceGroup.get_by_servicegroupname(
                        servicegroup_name)
                    if servicegroup is None:
                        continue

                    # insert service_servicegroup table
                    newservicerelation = Service2Group(
                        service_id=service.id, servicegroup_id=servicegroup.id)
                    service_to_group_relations.append(newservicerelation)
        HostService.save_all(hots_to_service_relations)
        ContactService.save_all(contact_to_service_relations)
        ContactgroupService.save_all(contactgroup_to_service_relations)
        Service2Group.save_all(service_to_group_relations)

    # sync hosts table
    hosts = Host.get_all()
    host_to_contact_relations = []
    host_to_contactgroup_relations = []
    if hosts is not None:
        for host in hosts:
            writeNagiosConfigFile(host)

            contact_str = host.contacts
            if contact_str is not None and len(
                    contact_str) > 0 and contact_str != "NULL":
                contacts = contact_str.split(',')

                for contact_name in contacts:
                    contact = Contact.get_by_contactname(contact_name)
                    if contact is None:
                        continue
                    newhostcontact = HostContact(host_id=host.id,
                                                 contact_id=contact.id)
                    host_to_contact_relations.append(newhostcontact)

            contactgroup_str = host.contact_groups
            if contactgroup_str is not None and len(
                    contactgroup_str) > 0 and contactgroup_str != "NULL":
                contactgroups = contactgroup_str.split(',')

                for contactgroup_name in contactgroups:
                    contactgroup = ContactGroup.get_by_contactgroupname(
                        contactgroup_name)
                    if contactgroup is None:
                        continue
                    newhostcontactgroup = HostContactGroup(
                        host_id=host.id, contactgroup_id=contactgroup.id)
                    host_to_contactgroup_relations.append(newhostcontactgroup)
        HostContact.save_all(contactgroup_to_service_relations)
        HostContactGroup.save_all(service_to_group_relations)

    restartNagios()