def all_done_linking(self, inst_id):
        """In addition to the original all_done_linking our items will get sorted"""
        super(self.__class__, self).all_done_linking(inst_id)

        # now sort the item collections by name
        safe_print("SORTING HOSTS AND SERVICES")
        # First install a new attribute _id_heap, which holds the
        # item ids in sorted order
        setattr(self.services, '_id_heap', [(get_obj_full_name(v), k) for (k, v) in self.services.items.iteritems()])
        self.services._id_heap.sort(key=lambda x: x[0])
        setattr(self.hosts, '_id_heap', [(get_obj_full_name(v), k) for (k, v) in self.hosts.items.iteritems()])
        self.hosts._id_heap.sort(key=lambda x: x[0])
        setattr(self.contacts, '_id_heap', [(get_obj_full_name(v), k) for (k, v) in self.contacts.items.iteritems()])
        self.contacts._id_heap.sort(key=lambda x: x[0])
        setattr(self.servicegroups, '_id_heap', [(get_obj_full_name(v), k) for (k, v) in self.servicegroups.items.iteritems()])
        self.servicegroups._id_heap.sort(key=lambda x: x[0])
        setattr(self.hostgroups, '_id_heap', [(get_obj_full_name(v), k) for (k, v) in self.hostgroups.items.iteritems()])
        self.hostgroups._id_heap.sort(key=lambda x: x[0])
        setattr(self.contactgroups, '_id_heap', [(get_obj_full_name(v), k) for (k, v) in self.contactgroups.items.iteritems()])
        self.contactgroups._id_heap.sort(key=lambda x: x[0])
        # Then install a method for accessing the lists' elements in sorted order
        setattr(self.services, '__itersorted__', types.MethodType(itersorted, self.services))
        setattr(self.hosts, '__itersorted__', types.MethodType(itersorted, self.hosts))
        setattr(self.contacts, '__itersorted__', types.MethodType(itersorted, self.contacts))
        setattr(self.servicegroups, '__itersorted__', types.MethodType(itersorted, self.servicegroups))
        setattr(self.hostgroups, '__itersorted__', types.MethodType(itersorted, self.hostgroups))
        setattr(self.contactgroups, '__itersorted__', types.MethodType(itersorted, self.contactgroups))

        # Everything is new now. We should clean the cache
        self.cache.wipeout()
Example #2
0
    def all_done_linking(self, inst_id):
        """In addition to the original all_done_linking our items will get sorted"""

        # We will relink all objects if need. If we are in a scheduler, this function will just bailout
        # because it's not need :)
        super(self.__class__, self).all_done_linking(inst_id)

        # now sort the item collections by name
        safe_print("SORTING HOSTS AND SERVICES")
        # First install a new attribute _id_heap, which holds the
        # item ids in sorted order
        setattr(self.services, '_id_heap', self.services.items.keys())
        self.services._id_heap.sort(key=lambda x: get_obj_full_name(self.services.items[x]))
        setattr(self.hosts, '_id_heap', self.hosts.items.keys())
        self.hosts._id_heap.sort(key=lambda x: get_obj_full_name(self.hosts.items[x]))
        setattr(self.contacts, '_id_heap', self.contacts.items.keys())
        self.contacts._id_heap.sort(key=lambda x: get_obj_full_name(self.contacts.items[x]))
        setattr(self.servicegroups, '_id_heap', self.servicegroups.items.keys())
        self.servicegroups._id_heap.sort(key=lambda x: get_obj_full_name(self.servicegroups.items[x]))
        setattr(self.hostgroups, '_id_heap', self.hostgroups.items.keys())
        self.hostgroups._id_heap.sort(key=lambda x: get_obj_full_name(self.hostgroups.items[x]))
        setattr(self.contactgroups, '_id_heap', self.contactgroups.items.keys())
        self.contactgroups._id_heap.sort(key=lambda x: get_obj_full_name(self.contactgroups.items[x]))
        setattr(self.commands, '_id_heap', self.commands.items.keys())
        self.commands._id_heap.sort(key=lambda x: get_obj_full_name(self.commands.items[x]))
        setattr(self.timeperiods, '_id_heap', self.timeperiods.items.keys())
        self.timeperiods._id_heap.sort(key=lambda x: get_obj_full_name(self.timeperiods.items[x]))
        # Then install a method for accessing the lists' elements in sorted order
        setattr(self.services, '__itersorted__', types.MethodType(itersorted, self.services))
        setattr(self.hosts, '__itersorted__', types.MethodType(itersorted, self.hosts))
        setattr(self.contacts, '__itersorted__', types.MethodType(itersorted, self.contacts))
        setattr(self.servicegroups, '__itersorted__', types.MethodType(itersorted, self.servicegroups))
        setattr(self.hostgroups, '__itersorted__', types.MethodType(itersorted, self.hostgroups))
        setattr(self.contactgroups, '__itersorted__', types.MethodType(itersorted, self.contactgroups))
        setattr(self.commands, '__itersorted__', types.MethodType(itersorted, self.commands))
        setattr(self.timeperiods, '__itersorted__', types.MethodType(itersorted, self.timeperiods))

        # Speedup authUser requests by populating _id_contact_heap with contact-names as key and
        # an array with the associated host and service ids
        setattr(self.hosts, '_id_contact_heap', dict())
        setattr(self.services, '_id_contact_heap', dict())
        setattr(self.hostgroups, '_id_contact_heap', dict())
        setattr(self.servicegroups, '_id_contact_heap', dict())

        [self.hosts._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k) for (k, v) in self.hosts.items.iteritems() for c in v.contacts]
        for c in self.hosts._id_contact_heap.keys():
            self.hosts._id_contact_heap[c].sort(key=lambda x: get_obj_full_name(self.hosts.items[x]))

        # strict: one must be an explicity contact of a service in order to see it.
        if self.service_authorization_strict:
            [self.services._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k) for (k, v) in self.services.items.iteritems() for c in v.contacts]
        else:
            # 1. every host contact automatically becomes a service contact
            [self.services._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k) for (k, v) in self.services.items.iteritems() for c in v.host.contacts]
            # 2. explicit service contacts
            [self.services._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k) for (k, v) in self.services.items.iteritems() for c in v.contacts]
        # services without contacts inherit the host's contacts (no matter of strict or loose)
        [self.services._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k) for (k, v) in self.services.items.iteritems() if not v.contacts for c in v.host.contacts]
        for c in self.services._id_contact_heap.keys():
            # remove duplicates
            self.services._id_contact_heap[c] = list(set(self.services._id_contact_heap[c]))
            self.services._id_contact_heap[c].sort(key=lambda x: get_obj_full_name(self.services.items[x]))


        if self.group_authorization_strict:
            for c in self.hosts._id_contact_heap.keys():
                # only host contacts can be hostgroup-contacts at all
                # now, which hosts does the contact know?
                contact_host_ids = set([h for h in self.hosts._id_contact_heap[c]])
                for (k, v) in self.hostgroups.items.iteritems():
                    # now look if c is contact of all v.members
                    # we already know the hosts for which c is a contact
                    # self.hosts._id_contact_heap[c] is [(hostname, id), (hostname, id)
                    hostgroup_host_ids = set([h.id for h in v.members])
                    # if all of the hostgroup_host_ids are in contact_host_ids
                    # then the hostgroup belongs to the contact
                    if hostgroup_host_ids <= contact_host_ids:
                        self.hostgroups._id_contact_heap.setdefault(c, []).append(v.id)
            for c in self.services._id_contact_heap.keys():
                # only service contacts can be servicegroup-contacts at all
                # now, which service does the contact know?
                contact_service_ids = set([h for h in self.services._id_contact_heap[c]])
                for (k, v) in self.servicegroups.items.iteritems():
                    # now look if c is contact of all v.members
                    # we already know the services for which c is a contact
                    # self.services._id_contact_heap[c] is [(svcdesc, id), (svcdesc, id)
                    servicegroup_service_ids = set([h.id for h in v.members])
                    # if all of the servicegroup_service_ids are in contact_service_ids
                    # then the servicegroup belongs to the contact
                    # print "%-10s %-15s %s <= %s" % (c, v.get_name(), servicegroup_service_ids, contact_service_ids)
                    if servicegroup_service_ids <= contact_service_ids:
                        self.servicegroups._id_contact_heap.setdefault(c, []).append(v.id)
        else:
            # loose: a contact of a member becomes contact of the whole group
            [self.hostgroups._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k) for (k, v) in self.hostgroups.items.iteritems() for h in v.members for c in h.contacts]
            [self.servicegroups._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k) for (k, v) in self.servicegroups.items.iteritems() for s in v.members for c in s.contacts] # todo: look at mk-livestatus. what about service's host contacts?
        for c in self.hostgroups._id_contact_heap.keys():
            # remove duplicates
            self.hostgroups._id_contact_heap[c] = list(set(self.hostgroups._id_contact_heap[c]))
            self.hostgroups._id_contact_heap[c].sort(key=lambda x: get_obj_full_name(self.hostgroups.items[x]))
        for c in self.servicegroups._id_contact_heap.keys():
            # remove duplicates
            self.servicegroups._id_contact_heap[c] = list(set(self.servicegroups._id_contact_heap[c]))
            self.servicegroups._id_contact_heap[c].sort(key=lambda x: get_obj_full_name(self.servicegroups.items[x]))

        # Add another helper structure which allows direct lookup by name
        # For hosts: _id_by_host_name_heap = {'name1':id1, 'name2': id2,...}
        # For services: _id_by_host_name_heap = {'name1':[id1, id2,...], 'name2': [id6, id7,...],...} = hostname maps to list of service_ids
        # For services: _id_by_service_name_heap = {'name1':id1, 'name2': id6,...} = full_service_description maps to service_id
        setattr(self.hosts, '_id_by_host_name_heap', dict([(get_obj_full_name(v), k) for (k, v) in self.hosts.items.iteritems()]))
        setattr(self.services, '_id_by_service_name_heap', dict([(get_obj_full_name(v), k) for (k, v) in self.services.items.iteritems()]))
        setattr(self.services, '_id_by_host_name_heap', dict())
        [self.services._id_by_host_name_heap.setdefault(get_obj_full_name(v.host), []).append(k) for (k, v) in self.services.items.iteritems()]
        # print self.services._id_by_host_name_heap
        for hn in self.services._id_by_host_name_heap.keys():
            self.services._id_by_host_name_heap[hn].sort(key=lambda x: get_obj_full_name(self.services[x]))

        # Add another helper structure which allows direct lookup by name
        # For hostgroups: _id_by_hostgroup_name_heap = {'name1':id1, 'name2': id2,...}
        # For servicegroups: _id_by_servicegroup_name_heap = {'name1':id1, 'name2': id2,...}
        setattr(self.hostgroups, '_id_by_hostgroup_name_heap', dict([(get_obj_full_name(v), k) for (k, v) in self.hostgroups.items.iteritems()]))
        setattr(self.servicegroups, '_id_by_servicegroup_name_heap', dict([(get_obj_full_name(v), k) for (k, v) in self.servicegroups.items.iteritems()]))

        # For hosts: key is a hostgroup_name, value is an array with all host_ids of the hosts in this group
        setattr(self.hosts, '_id_by_hostgroup_name_heap', dict())
        [self.hosts._id_by_hostgroup_name_heap.setdefault(get_obj_full_name(hg), []).append(k) for (k, v) in self.hosts.items.iteritems() for hg in v.hostgroups]
        # For services: key is a servicegroup_name, value is an array with all service_ids of the services in this group
        setattr(self.services, '_id_by_servicegroup_name_heap', dict())
        [self.services._id_by_servicegroup_name_heap.setdefault(get_obj_full_name(sg), []).append(k) for (k, v) in self.services.items.iteritems() for sg in v.servicegroups]
        # For services: key is a hostgroup_name, value is an array with all service_ids of the hosts in this group
        setattr(self.services, '_id_by_hostgroup_name_heap', dict())
        [self.services._id_by_hostgroup_name_heap.setdefault(get_obj_full_name(hg), []).append(k) for (k, v) in self.services.items.iteritems() for hg in v.host.hostgroups]



        # print self.services._id_by_host_name_heap
        for hn in self.services._id_by_host_name_heap.keys():
            self.services._id_by_host_name_heap[hn].sort(key=lambda x: get_obj_full_name(self.services[x]))


        # Everything is new now. We should clean the cache
        self.cache.wipeout()
Example #3
0
    def all_done_linking(self, inst_id):
        """In addition to the original all_done_linking our items will get sorted"""

        # We will relink all objects if need. If we are in a scheduler, this function will just bailout
        # because it's not need :)
        super(LiveStatusRegenerator, self).all_done_linking(inst_id)

        # now sort the item collections by name
        safe_print("SORTING HOSTS AND SERVICES")
        # First install a new attribute _id_heap, which holds the
        # item ids in sorted order
        setattr(self.services, '_id_heap', self.services.items.keys())
        self.services._id_heap.sort(
            key=lambda x: get_obj_full_name(self.services.items[x])
        )
        setattr(self.hosts, '_id_heap', self.hosts.items.keys())
        self.hosts._id_heap.sort(
            key=lambda x: get_obj_full_name(self.hosts.items[x])
        )
        setattr(self.contacts, '_id_heap', self.contacts.items.keys())
        self.contacts._id_heap.sort(
            key=lambda x: get_obj_full_name(self.contacts.items[x])
        )
        setattr(self.servicegroups, '_id_heap', self.servicegroups.items.keys())
        self.servicegroups._id_heap.sort(
            key=lambda x: get_obj_full_name(self.servicegroups.items[x])
        )
        setattr(self.hostgroups, '_id_heap', self.hostgroups.items.keys())
        self.hostgroups._id_heap.sort(
            key=lambda x: get_obj_full_name(self.hostgroups.items[x])
        )
        setattr(self.contactgroups, '_id_heap', self.contactgroups.items.keys())
        self.contactgroups._id_heap.sort(
            key=lambda x: get_obj_full_name(self.contactgroups.items[x])
        )
        setattr(self.commands, '_id_heap', self.commands.items.keys())
        self.commands._id_heap.sort(
            key=lambda x: get_obj_full_name(self.commands.items[x])
        )
        setattr(self.timeperiods, '_id_heap', self.timeperiods.items.keys())
        self.timeperiods._id_heap.sort(
            key=lambda x: get_obj_full_name(self.timeperiods.items[x])
        )
        # Then install a method for accessing the lists' elements in sorted order
        setattr(self.services, '__itersorted__',
                types.MethodType(itersorted, self.services))
        setattr(self.hosts, '__itersorted__',
                types.MethodType(itersorted, self.hosts))
        setattr(self.contacts, '__itersorted__',
                types.MethodType(itersorted, self.contacts))
        setattr(self.servicegroups, '__itersorted__',
                types.MethodType(itersorted, self.servicegroups))
        setattr(self.hostgroups, '__itersorted__',
                types.MethodType(itersorted, self.hostgroups))
        setattr(self.contactgroups, '__itersorted__',
                types.MethodType(itersorted, self.contactgroups))
        setattr(self.commands, '__itersorted__',
                types.MethodType(itersorted, self.commands))
        setattr(self.timeperiods, '__itersorted__',
                types.MethodType(itersorted, self.timeperiods))

        # Speedup authUser requests by populating _id_contact_heap with contact-names as key and
        # an array with the associated host and service ids
        setattr(self.hosts, '_id_contact_heap', dict())
        setattr(self.services, '_id_contact_heap', dict())
        setattr(self.hostgroups, '_id_contact_heap', dict())
        setattr(self.servicegroups, '_id_contact_heap', dict())

        [
            self.hosts._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k)
            for (k, v) in self.hosts.items.iteritems()
            for c in v.contacts
        ]
        for c in self.hosts._id_contact_heap.keys():
            self.hosts._id_contact_heap[c].sort(
                key=lambda x: get_obj_full_name(self.hosts.items[x])
            )

        # strict: one must be an explicitly contact of a service in order to see it.
        if self.service_authorization_strict:
            [
                self.services._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k)
                for (k, v) in self.services.items.iteritems()
                for c in v.contacts
            ]
        else:
            # 1. every host contact automatically becomes a service contact
            [
                self.services._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k)
                for (k, v) in self.services.items.iteritems()
                for c in v.host.contacts
            ]
            # 2. explicit service contacts
            [
                self.services._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k)
                for (k, v) in self.services.items.iteritems()
                for c in v.contacts
            ]
        # services without contacts inherit the host's contacts (no matter of strict or loose)
        [
            self.services._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k)
            for (k, v) in self.services.items.iteritems() if not v.contacts
            for c in v.host.contacts
        ]
        for c in self.services._id_contact_heap.keys():
            # remove duplicates
            self.services._id_contact_heap[c] = list(set(self.services._id_contact_heap[c]))
            self.services._id_contact_heap[c].sort(
                key=lambda x: get_obj_full_name(self.services.items[x])
            )

        if self.group_authorization_strict:
            for c in self.hosts._id_contact_heap.keys():
                # only host contacts can be hostgroup-contacts at all
                # now, which hosts does the contact know?
                contact_host_ids = set([h for h in self.hosts._id_contact_heap[c]])
                for (k, v) in self.hostgroups.items.iteritems():
                    # now look if c is contact of all v.members
                    # we already know the hosts for which c is a contact
                    # self.hosts._id_contact_heap[c] is [(hostname, id), (hostname, id)
                    hostgroup_host_ids = set([h.id for h in v.members])
                    # if all of the hostgroup_host_ids are in contact_host_ids
                    # then the hostgroup belongs to the contact
                    if hostgroup_host_ids <= contact_host_ids:
                        self.hostgroups._id_contact_heap.setdefault(c, []).append(v.id)
            for c in self.services._id_contact_heap.keys():
                # only service contacts can be servicegroup-contacts at all
                # now, which service does the contact know?
                contact_service_ids = set([h for h in self.services._id_contact_heap[c]])
                for (k, v) in self.servicegroups.items.iteritems():
                    # now look if c is contact of all v.members
                    # we already know the services for which c is a contact
                    # self.services._id_contact_heap[c] is [(svcdesc, id), (svcdesc, id)
                    servicegroup_service_ids = set([h.id for h in v.members])
                    # if all of the servicegroup_service_ids are in contact_service_ids
                    # then the servicegroup belongs to the contact
                    # print "%-10s %-15s %s <= %s" % (c, v.get_name(), servicegroup_service_ids, contact_service_ids)
                    if servicegroup_service_ids <= contact_service_ids:
                        self.servicegroups._id_contact_heap.setdefault(c, []).append(v.id)
        else:
            # loose: a contact of a member becomes contact of the whole group
            [
                self.hostgroups._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k)
                for (k, v) in self.hostgroups.items.iteritems()
                for h in v.members
                for c in h.contacts
            ]
            [
                self.servicegroups._id_contact_heap.setdefault(get_obj_full_name(c), []).append(k)
                for (k, v) in self.servicegroups.items.iteritems()
                for s in v.members
                for c in s.contacts
            ] # todo: look at mk-livestatus. what about service's host contacts?
        for c in self.hostgroups._id_contact_heap.keys():
            # remove duplicates
            self.hostgroups._id_contact_heap[c] = list(set(self.hostgroups._id_contact_heap[c]))
            self.hostgroups._id_contact_heap[c].sort(
                key=lambda x: get_obj_full_name(self.hostgroups.items[x])
            )
        for c in self.servicegroups._id_contact_heap.keys():
            # remove duplicates
            self.servicegroups._id_contact_heap[c] = list(set(self.servicegroups._id_contact_heap[c]))
            self.servicegroups._id_contact_heap[c].sort(
                key=lambda x: get_obj_full_name(self.servicegroups.items[x])
            )

        # Add another helper structure which allows direct lookup by name
        # For hosts: _id_by_host_name_heap = {'name1':id1, 'name2': id2,...}
        # For services: _id_by_host_name_heap = {'name1':[id1, id2,...], 'name2': [id6, id7,...],...} = hostname maps to list of service_ids
        # For services: _id_by_service_name_heap = {'name1':id1, 'name2': id6,...} = full_service_description maps to service_id
        setattr(self.hosts, '_id_by_host_name_heap', dict([(get_obj_full_name(v), k) for (k, v) in self.hosts.items.iteritems()]))
        setattr(self.services, '_id_by_service_name_heap', dict([(get_obj_full_name(v), k) for (k, v) in self.services.items.iteritems()]))
        setattr(self.services, '_id_by_host_name_heap', dict())
        [
            self.services._id_by_host_name_heap.setdefault(get_obj_full_name(v.host), []).append(k)
            for (k, v) in self.services.items.iteritems()
        ]
        logger.debug("[Livestatus Regenerator] Id by Hostname heap: %s" % str(self.services._id_by_host_name_heap))
        for hn in self.services._id_by_host_name_heap.keys():
            self.services._id_by_host_name_heap[hn].sort(
                key=lambda x: get_obj_full_name(self.services[x])
            )

        # Add another helper structure which allows direct lookup by name
        # For hostgroups: _id_by_hostgroup_name_heap = {'name1':id1, 'name2': id2,...}
        # For servicegroups: _id_by_servicegroup_name_heap = {'name1':id1, 'name2': id2,...}
        setattr(self.hostgroups, '_id_by_hostgroup_name_heap',
                dict([
                    (get_obj_full_name(v), k) for (k, v) in self.hostgroups.items.iteritems()
                ]))
        setattr(self.servicegroups, '_id_by_servicegroup_name_heap',
                dict([
                    (get_obj_full_name(v), k) for (k, v) in self.servicegroups.items.iteritems()
                ]))

        # For hosts: key is a hostgroup_name, value is an array with all host_ids of the hosts in this group
        setattr(self.hosts, '_id_by_hostgroup_name_heap', dict())
        [
            self.hosts._id_by_hostgroup_name_heap.setdefault(get_obj_full_name(hg), []).append(k)
            for (k, v) in self.hosts.items.iteritems()
            for hg in v.hostgroups
        ]
        for hg in self.hosts._id_by_hostgroup_name_heap.keys():
            self.hosts._id_by_hostgroup_name_heap[hg].sort(
                key=lambda x: get_obj_full_name(self.hosts.items[x])
            )
        # For services: key is a servicegroup_name, value is an array with all service_ids of the services in this group
        setattr(self.services, '_id_by_servicegroup_name_heap', dict())
        [
            self.services._id_by_servicegroup_name_heap.setdefault(get_obj_full_name(sg), []).append(k)
            for (k, v) in self.services.items.iteritems()
            for sg in v.servicegroups
        ]
        for sg in self.services._id_by_servicegroup_name_heap.keys():
            self.services._id_by_servicegroup_name_heap[sg].sort(
                key=lambda x: get_obj_full_name(self.services[x])
            )
        # For services: key is a hostgroup_name, value is an array with all service_ids of the hosts in this group
        setattr(self.services, '_id_by_hostgroup_name_heap', dict())
        [
            self.services._id_by_hostgroup_name_heap.setdefault(get_obj_full_name(hg), []).append(k)
            for (k, v) in self.services.items.iteritems()
            for hg in v.host.hostgroups
        ]
        for hg in self.services._id_by_hostgroup_name_heap.keys():
            self.services._id_by_hostgroup_name_heap[hg].sort(
                key=lambda x: get_obj_full_name(self.services[x])
            )

        # print self.services._id_by_host_name_heap
        for hn in self.services._id_by_host_name_heap.keys():
            self.services._id_by_host_name_heap[hn].sort(
                key=lambda x: get_obj_full_name(self.services[x])
            )

        # Everything is new now. We should clean the cache
        self.cache.wipeout()
        if self.transparent_update:
            self.clear_previous_state()
        self.initialized = True