def _buildCache(self, orgtype=None, instancetype=None, relname=None, treePrefix=None, orderby=None): if orgtype and not getattr(self._root, "_cache", None): cat = ICatalogTool(self._object.unrestrictedTraverse(self.uid)) results = cat.search(orgtype, orderby=orderby) if instancetype: instanceresults = cat.search(instancetype, orderby=None) self._root._cache = PathIndexCache(results, instanceresults, relname, treePrefix) else: self._root._cache = PathIndexCache(results) return self._root._cache
def remove(self, app, leaveObjects=False): if not leaveObjects: from Products.ZenModel.DeviceHW import DeviceHW NEW_COMPONENT_TYPES = tuple([x for x in productNames]) log.info('Removing ZenPacks.community.CiscoEnvmonE components') cat = ICatalogTool(app.zport.dmd) # Search the catalog for components of this zenpacks type. if NEW_COMPONENT_TYPES: for brain in cat.search(types=NEW_COMPONENT_TYPES): component = brain.getObject() component.getPrimaryParent()._delObject(component.id) hw_relations = ("ciscoenvvoltagesensors", "ciscoenvtempsensors" "ciscoenvfans" "ciscoenvpowersupplies") # remote HW component relations DeviceHW._relations = tuple( [x for x in DeviceHW._relations if x[0] not in hw_relations]) log.info('Removing ZenPacks.community.CiscoEnvMonE' 'relationships from existing devices') self._buildHWRelations() super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def moveProcess(self, uid, targetUid): obj = self._getObject(uid) target = self._getObject(targetUid) brainsCollection = [] # reindex all the devices and processes underneath this guy and the target for org in (obj.getPrimaryParent().getPrimaryParent(), target): catalog = ICatalogTool(org) brainsCollection.append(catalog.search(OSProcess)) if isinstance(obj, OSProcessClass): source = obj.osProcessOrganizer() source.moveOSProcessClasses(targetUid, obj.id) newObj = getattr(target.osProcessClasses, obj.id) elif isinstance(obj, OSProcessOrganizer): source = aq_parent(obj) source.moveOrganizer(targetUid, (obj.id,)) newObj = getattr(target, obj.id) else: raise Exception('Illegal type %s' % obj.__class__.__name__) # fire the object moved event for the process instances (will update catalog) for brains in brainsCollection: objs = imap(unbrain, brains) for item in objs: notify(ObjectMovedEvent(item, item.os(), item.id, item.os(), item.id)) return newObj.getPrimaryPath()
def getDeviceBrains(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None): cat = ICatalogTool(self._getObject(uid)) reverse = dir=='DESC' qs = [] query = None globFilters = {} if params is None: params = {} for key, value in params.iteritems(): if key == 'ipAddress': ip = ensureIp(value) try: checkip(ip) except IpAddressError: pass else: if numbip(ip): minip, maxip = getSubnetBounds(ip) qs.append(Between('ipAddress', str(minip), str(maxip))) elif key == 'deviceClass': qs.append(MatchRegexp('uid', '(?i).*%s.*' % value)) elif key == 'productionState': qs.append(Or(*[Eq('productionState', str(state)) for state in value])) else: globFilters[key] = value if qs: query = And(*qs) brains = cat.search('Products.ZenModel.Device.Device', start=start, limit=limit, orderby=sort, reverse=reverse, query=query, globFilters=globFilters, hashcheck=hashcheck) return brains
def _serviceSearch(self, limit=None, start=None, sort='name', dir='ASC', params=None, uid=None, criteria=()): cat = ICatalogTool(self._getObject(uid)) reverse = dir == 'DESC' qs = [] query = None if params: if 'name' in params: qs.append(MatchRegexp('name', '(?i).*%s.*' % params['name'])) if 'port' in params: qs.append(MatchRegexp('port', '(?i).*%s.*' % params['port'])) if qs: query = And(*qs) return cat.search("Products.ZenModel.ServiceClass.ServiceClass", start=start, limit=limit, orderby=sort, reverse=reverse, query=query)
def _buildCache(self, orgtype=None, instancetype=None, relname=None, treePrefix=None, orderby=None): if orgtype and not getattr(self._root, '_cache', None): cat = ICatalogTool(self._object.unrestrictedTraverse(self.uid)) results = cat.search(orgtype, orderby=orderby) if instancetype: instanceresults = cat.search(instancetype, orderby=None) self._root._cache = PathIndexCache(results, instanceresults, relname, treePrefix) else: self._root._cache = PathIndexCache(results) return self._root._cache
def getSubComponents(self, dmd): i = 0 catalog = ICatalogTool(dmd.Devices) COMPONENT = 'Products.ZenModel.DeviceComponent.DeviceComponent' query = Eq('monitored', '1') for brain in catalog.search(COMPONENT, query=query): i += 1 obj = None try: obj = brain.getObject() except KeyError: continue dev = obj.device() status = obj.getStatus() row = (dict(getParentDeviceTitle=obj.getParentDeviceTitle(), hostname=obj.getParentDeviceTitle(), name=obj.name(), meta_type=obj.meta_type, getInstDescription=obj.getInstDescription(), getStatusString=obj.convertStatus(status), getDeviceLink=obj.getDeviceLink(), getPrimaryUrlPath=obj.getPrimaryUrlPath(), cssclass=obj.getStatusCssClass(status), status=status)) obj._p_invalidate() dev._p_invalidate() if i % 100 == 0: transaction.abort() yield Utils.Record(**row)
def remove(self, app, leaveObjects=False): if not leaveObjects: log.info('Removing Hadoop components') cat = ICatalogTool(app.zport.dmd) for brain in cat.search(types=NEW_COMPONENT_TYPES): component = brain.getObject() component.getPrimaryParent()._delObject(component.id) # Remove our Device relations additions. Device._relations = tuple( [x for x in Device._relations if x[0] not in NEW_DEVICE_RELATIONS]) # Remove zHBaseMasterPort if HBase ZenPack is not installed try: self.dmd.ZenPackManager.packs._getOb( 'ZenPacks.zenoss.HBase' ) except AttributeError: if self.dmd.Devices.hasProperty('zHBaseMasterPort'): self.dmd.Devices.deleteZenProperty('zHBaseMasterPort') log.debug('Removing zHBaseMasterPort property') log.info('Removing Hadoop device relationships') self._buildDeviceRelations() super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def getSubComponents(self, dmd): i = 0 catalog = ICatalogTool(dmd.Devices) COMPONENT = 'Products.ZenModel.DeviceComponent.DeviceComponent' query = Eq('monitored', '1') for brain in catalog.search(COMPONENT, query=query): i += 1 obj = None try: obj = brain.getObject() except KeyError: continue dev = obj.device() status = obj.getStatus() row = (dict( getParentDeviceTitle=obj.getParentDeviceTitle(), hostname=obj.getParentDeviceTitle(), name=obj.name(), meta_type=obj.meta_type, getInstDescription=obj.getInstDescription(), getStatusString=obj.convertStatus(status), getDeviceLink=obj.getDeviceLink(), getPrimaryUrlPath=obj.getPrimaryUrlPath(), cssclass=obj.getStatusCssClass(status), status=status )) obj._p_invalidate() dev._p_invalidate() if i % 100 == 0: transaction.abort() yield Utils.Record(**row)
def _processSearch(self, limit=None, start=None, sort='name', dir='ASC', params=None, uid=None, criteria=()): ob = self._getObject(uid) if isinstance(uid, basestring) else uid cat = ICatalogTool(ob) reverse = dir == 'DESC' qs = [] query = None if params: if 'name' in params: qs.append(MatchRegexp('name', '(?i).*%s.*' % params['name'])) if qs: query = And(*qs) return cat.search("Products.ZenModel.OSProcessClass.OSProcessClass", start=start, limit=limit, orderby=sort, reverse=reverse, query=query)
def remove(self, app, leaveObjects=False): if not leaveObjects: self.remove_plugin_symlinks() log.info( 'Removing ZenPacks.zenoss.OrderedComponentLists components') cat = ICatalogTool(app.zport.dmd) # Search the catalog for components of this zenpacks type. if NEW_COMPONENT_TYPES: for brain in cat.search(types=NEW_COMPONENT_TYPES): component = brain.getObject() component.getPrimaryParent()._delObject(component.id) # Remove our Device relations additions. Device._relations = tuple( [x for x in Device._relations \ if x[0] not in NEW_DEVICE_RELATIONS]) log.info( 'Removing ZenPacks.zenoss.OrderedComponentLists relationships from existing devices' ) self._buildDeviceRelations() super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def getIpAddresses(self, limit=0, start=0, sort='ipAddressAsInt', dir='DESC', params=None, uid=None, criteria=()): infos = [] cat = ICatalogTool(self._getObject(uid)) reverse = dir=='DESC' brains = cat.search("Products.ZenModel.IpAddress.IpAddress", start=start, limit=limit, orderby=sort, reverse=reverse) for brain in brains: infos.append(IInfo(unbrain(brain))) devuuids = set(info.device.uuid for info in infos if info.device) # get ping severities zep = getFacade('zep') pingSeverities = zep.getEventSeverities(devuuids, severities=(), status=(), eventClass=Status_Ping) self._assignPingStatuses(infos, pingSeverities) # get snmp severities snmpSeverities = zep.getEventSeverities(devuuids, severities=(), status=(), eventClass=Status_Snmp) self._assignSnmpStatuses(infos, snmpSeverities) return SearchResults(infos, brains.total, brains.hash_)
def getAddTemplateTargets(self, query=None): """ @returns list of targets for our new template """ cat = ICatalogTool(self._dmd) results = [] # it can be any device class that we add the template too brains = cat.search(types=[DeviceClass]) for brain in brains: # HACK: use the path to get the organizer name so we don't have to wake up the object label = brain.getPath() if label == "/zport/dmd/Devices": label = "/" else: label = label.replace("/zport/dmd/Devices/", "/") results.append(dict(uid=brain.getPath(), label=label)) # the display is organizer name and we do not index it # so filter after the query if query is not None: results = [ result for result in results if query.lower() in result['label'].lower() ] return sorted(results, key=lambda org: org['label'])
def _findDevices(self, identifier, ipAddress, limit=None): """ Returns a tuple ([device brains], [devices]) searching manage IP and interface IPs. limit is the maximum total number in both lists. """ dev_cat = ICatalogTool(self._devices) try: ip_address = next(i for i in (ipAddress, identifier) if isip(i)) ip_decimal = ipToDecimal(ip_address) except Exception: ip_address = None ip_decimal = None query_set = Or(Eq('id', identifier), Eq('name', identifier)) if ip_decimal is not None: query_set.addSubquery(Eq('ipAddress', str(ip_decimal))) device_brains = list( dev_cat.search(types=Device, query=query_set, limit=limit, filterPermissions=False)) limit = None if limit is None else limit - len(device_brains) if not limit: return device_brains, [] if ip_decimal is not None: # don't search interfaces for 127.x.x.x IPv4 addresses if ipToDecimal('126.255.255.255') < ip_decimal < ipToDecimal( '128.0.0.0'): ip_decimal = None # don't search interfaces for the ::1 IPv6 address elif ipToDecimal('::1') == ip_decimal: ip_decimal = None if ip_decimal is None: return [], [] net_cat = ICatalogTool(self._networks) results = net_cat.search(types=IpAddress, query=(Eq('name', ip_address)), limit=limit, filterPermissions=False) devices = [brain.getObject().device() for brain in results] return device_brains, devices
def interfaces_by_type(device, type_): """Yield Interface objects matching the given type.""" catalog = ICatalogTool(device.primaryAq()) search_results = catalog.search( types=('ZenPacks.Rackspace.BTI7200.%s.%s' % (type_, type_))) for result in search_results.results: yield result.getObject()
def removeSoftwareAndOperatingSystems(self, dmd): """ Find everything that is an OperationSystem Or Software and unindex it """ cat = ICatalogTool(dmd) brains = cat.search(types=(OperatingSystem, Software)) for brain in brains: dmd.global_catalog.uncatalog_object(brain.getPath())
def removeSoftwareAndOperatingSystems(self, dmd): """ Find everything that is an OperationSystem Or Software and unindex it """ cat = ICatalogTool(dmd) brains = cat.search(types=(OperatingSystem,Software)) for brain in brains: dmd.global_catalog.uncatalog_object(brain.getPath())
def _findDevices(self, identifier, ipAddress, limit=None): """ Returns a tuple ([device brains], [devices]) searching manage IP and interface IPs. limit is the maximum total number in both lists. """ dev_cat = ICatalogTool(self._devices) try: ip_address = next(i for i in (ipAddress, identifier) if isip(i)) ip_decimal = ipToDecimal(ip_address) except Exception: ip_address = None ip_decimal = None query_set = Or(Eq('id', identifier), Eq('name', identifier)) if ip_decimal is not None: query_set.addSubquery(Eq('ipAddress', str(ip_decimal))) device_brains = list(dev_cat.search(types=Device, query=query_set, limit=limit, filterPermissions=False)) limit = None if limit is None else limit - len(device_brains) if not limit: return device_brains, [] if ip_decimal is not None: # don't search interfaces for 127.x.x.x IPv4 addresses if ipToDecimal('126.255.255.255') < ip_decimal < ipToDecimal('128.0.0.0'): ip_decimal = None # don't search interfaces for the ::1 IPv6 address elif ipToDecimal('::1') == ip_decimal: ip_decimal = None if ip_decimal is None and not device_brains: return [], [] net_cat = ICatalogTool(self._networks) results = net_cat.search(types=IpAddress, query=(Eq('name', ip_address)), limit = limit, filterPermissions = False) devices = [brain.getObject().device() for brain in results] return device_brains, devices
def getInstances(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None): # do the catalog search cat = ICatalogTool(self._getObject(uid)) reverse = bool(dir == 'DESC') brains = cat.search(self._instanceClass, start=start, limit=limit, orderby=sort, reverse=reverse) objs = imap(unbrain, brains) # convert to info objects return SearchResults(imap(IInfo, objs), brains.total, brains.hash_)
def remote_createAllUsers(self): cat = ICatalogTool(self.dmd) brains = cat.search(("Products.ZenModel.Device.Device", "Products.ZenModel.DeviceClass.DeviceClass")) users = [] for brain in brains: device = brain.getObject() user = self._create_user(device) if user is not None: users.append(user) fmt = 'SnmpTrapConfig.remote_createAllUsers {0} users' log.debug(fmt.format(len(users))) return users
def interfaces_by_names(device, interface_names, type_='Interface'): """Yield Interface objects matching the given names.""" catalog = ICatalogTool(device.primaryAq()) name_equals = (Eq('name', x) for x in interface_names) search_results = catalog.search( types=('ZenPacks.Rackspace.BTI7200.%s.%s' % (type_, type_,)), query=Or(*name_equals)) for result in search_results.results: yield result.getObject()
def getSubComponents(self): cat = ICatalogTool(self) query = And(Not(Eq('objectImplements', 'Products.ZenModel.ComponentGroup.ComponentGroup')), Not(Eq('objectImplements', 'Products.ZenModel.Device.Device'))) brains = cat.search(query=query) children = [] for brain in brains: try: children.append(brain.getObject()) except: pass return children
def initialize(self, context): LOG.debug("initializing invalidation checksums") catalog = ICatalogTool(context.dmd.Devices.primaryAq()) self.checksums = {} for b in catalog.search(CHECKSUMMERS.keys()): try: self.checksums[b.getPath()] = checksum(b.getObject()) except Exception: # Catalog or database inconsistencies must not prevent # zenhub from starting. pass LOG.debug("initialized %d invalidation checksums", len(self.checksums))
def cleanDevicePath(self, dmd): """ Make sure only groups systems and locations and device classes are the only thing indexed by the path """ cat = ICatalogTool(dmd) brains = cat.search(types=(Device,)) idx = dmd.global_catalog._catalog.indexes['path'] for brain in brains: badPaths = [] for path in idx._unindex[brain.getRID()]: if not self.keepDevicePath(path): badPaths.append(path) if badPaths: dmd.global_catalog.unindex_object_from_paths(DummyDevice(brain), badPaths)
def keyword_search(root, keywords): """Generate objects that match one or more of given keywords.""" if isinstance(keywords, basestring): keywords = [keywords] elif isinstance(keywords, set): keywords = list(keywords) if keywords: catalog = ICatalogTool(root) query = In('searchKeywords', keywords) for result in catalog.search(query=query): try: yield result.getObject() except Exception: pass
def getSubComponents(self): cat = ICatalogTool(self) query = And( Not( Eq('objectImplements', 'Products.ZenModel.ComponentGroup.ComponentGroup')), Not(Eq('objectImplements', 'Products.ZenModel.Device.Device'))) brains = cat.search(query=query) children = [] for brain in brains: try: children.append(brain.getObject()) except: pass return children
def remove(self, app, leaveObjects=False): if not leaveObjects: log.info("Removing MySqlMonitor components") cat = ICatalogTool(app.zport.dmd) for brain in cat.search(types=NEW_COMPONENT_TYPES): component = brain.getObject() component.getPrimaryParent()._delObject(component.id) # Remove our Device relations additions. Device._relations = tuple([x for x in Device._relations if x[0] not in NEW_DEVICE_RELATIONS]) log.info("Removing MySqlMonitor device relationships") self._buildDeviceRelations() super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def cleanDevicePath(self, dmd): """ Make sure only groups systems and locations and device classes are the only thing indexed by the path """ cat = ICatalogTool(dmd) brains = cat.search(types=(Device, )) idx = dmd.global_catalog._catalog.indexes['path'] for brain in brains: badPaths = [] for path in idx._unindex[brain.getRID()]: if not self.keepDevicePath(path): badPaths.append(path) if badPaths: dmd.global_catalog.unindex_object_from_paths( DummyDevice(brain), badPaths)
def remove(self, app, leaveObjects=False): if not leaveObjects: log.info('Removing all ElementPool components') cat = ICatalogTool(app.zport.dmd) for brain in cat.search(types=NEW_COMPONENT_TYPES): component = brain.getObject() component.getPrimaryParent()._delObject(component.id) # Remove our relations additions. for relname, _, _, _, _, containingClass in NEW_DEVICE_RELATIONS: containingClass._relations = tuple( [x for x in containingClass._relations if x[0] != relname]) log.info('Removing ElementPool device/component relationships') self._buildDeviceRelations() super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def _processSearch(self, limit=None, start=None, sort='name', dir='ASC', params=None, uid=None, criteria=()): ob = self._getObject(uid) if isinstance(uid, basestring) else uid cat = ICatalogTool(ob) reverse = dir=='DESC' qs = [] query = None if params: if 'name' in params: qs.append(MatchRegexp('name', '(?i).*%s.*' % params['name'])) if qs: query = And(*qs) return cat.search("Products.ZenModel.OSProcessClass.OSProcessClass", start=start, limit=limit, orderby=sort, reverse=reverse, query=query)
def remove(self, app, leaveObjects=False): if not leaveObjects: log.info('Removing RabbitMQ components') cat = ICatalogTool(app.zport.dmd) for brain in cat.search(types=NEW_COMPONENT_TYPES): component = brain.getObject() component.getPrimaryParent()._delObject(component.id) # Remove our Device relations additions. Device._relations = tuple( [x for x in Device._relations \ if x[0] not in NEW_DEVICE_RELATIONS]) log.info('Removing RabbitMQ device relationships') self._buildDeviceRelations() super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def processOrganizerUpdated(self, object, event): catalog = ICatalogTool(object.primaryAq()) results = catalog.search(OSProcessClass) if not results.total: return devices = set() for organizer in results: if results.areBrains: organizer = organizer.getObject() for process in organizer.instances(): device = process.device() if not device: continue device = device.primaryAq() device_path = device.getPrimaryUrlPath() if not device_path in devices: self._notifyAll(device) devices.add(device_path)
def _serviceSearch(self, limit=None, start=None, sort='name', dir='ASC', params=None, uid=None, criteria=()): cat = ICatalogTool(self._getObject(uid)) reverse = dir=='DESC' qs = [] query = None if params: if 'name' in params: qs.append(MatchRegexp('name', '(?i).*%s.*' % params['name'])) if 'port' in params: qs.append(MatchRegexp('port', '(?i).*%s.*' % params['port'])) if qs: query = And(*qs) return cat.search("Products.ZenModel.ServiceClass.ServiceClass", start=start, limit=limit, orderby=sort, reverse=reverse, query=query)
def getCopyTargets(self, uid, query=''): catalog = ICatalogTool(self._dmd) template = self._getObject(uid) types = ['Products.ZenModel.DeviceClass.DeviceClass'] brains = catalog.search(types=types) objs = imap(unbrain, brains) def genTargets(): for obj in objs: container = obj.rrdTemplates organizer = '/' + '/'.join(obj.getPrimaryPath()[4:]) label = organizer if template.id in container.objectIds(): label += " (%s)" % _t('Create Copy') if label.lower().startswith(query.lower()): uid = '/'.join(obj.getPrimaryPath()) yield dict(uid=uid, label=label) def byLabel(left, right): return cmp(left['label'].lower(), right['label'].lower()) return sorted(genTargets(), byLabel)
def getDeviceBrains(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None): cat = ICatalogTool(self._getObject(uid)) reverse = bool(dir == 'DESC') qs = [] query = None globFilters = {} if params is None: params = {} for key, value in params.iteritems(): if key == 'ipAddress': ip = ensureIp(value) try: checkip(ip) except IpAddressError: pass else: if numbip(ip): minip, maxip = getSubnetBounds(ip) qs.append(Between('ipAddress', str(minip), str(maxip))) elif key == 'deviceClass': qs.append(MatchRegexp('uid', '(?i).*%s.*' % value)) # ZEN-10057 - move filtering on indexed groups/systems/location from post-filter to query elif key in organizersToClass: organizerQuery = self.findMatchingOrganizers(organizersToClass[key], organizersToPath[key], value) if not organizerQuery: return [] qs.append(organizerQuery) elif key == 'productionState': qs.append(Or(*[Eq('productionState', str(state)) for state in value])) else: globFilters[key] = value if qs: query = And(*qs) brains = cat.search( 'Products.ZenModel.Device.Device', start=start, limit=limit, orderby=sort, reverse=reverse, query=query, globFilters=globFilters, hashcheck=hashcheck ) return brains
def getSubComponents(self, meta_type="", monitored=True): """ Return generator of components, by meta_type if specified """ catalog = ICatalogTool(self) COMPONENT = 'Products.ZenModel.DeviceComponent.DeviceComponent' monitorq, typeq = None, None if monitored: monitorq = Eq('monitored', '1') if meta_type: typeq = Eq('meta_type', meta_type) queries = filter(None, (monitorq, typeq)) if queries: query = And(*queries) if len(queries) > 1 else queries[0] else: query = None for brain in catalog.search(COMPONENT, query=query): try: yield brain.getObject() except KeyError: log.warn("bad path '%s' in global catalog", brain.getPath())
def getAddTemplateTargets(self, query=None): """ @returns list of targets for our new template """ cat = ICatalogTool(self._dmd) results = [] # it can be any device class that we add the template too brains = cat.search(types=[DeviceClass]) for brain in brains: # HACK: use the path to get the organizer name so we don't have to wake up the object label = brain.getPath() if label == "/zport/dmd/Devices": label = "/" else: label = label.replace("/zport/dmd/Devices/", "/") results.append(dict(uid=brain.getPath(), label=label)) # the display is organizer name and we do not index it # so filter after the query if query is not None: results = [result for result in results if query.lower() in result['label'].lower()] return sorted(results, key=lambda org: org['label'])
def remove(self, app, leaveObjects=False): if not leaveObjects: self.remove_plugin_symlinks() LOG.info('Removing ZenPacks.EE.HelloWorld components') cat = ICatalogTool(app.zport.dmd) # Search the catalog for components of this zenpacks type. if NEW_COMPONENT_TYPES: for brain in cat.search(types=NEW_COMPONENT_TYPES): component = brain.getObject() component.getPrimaryParent()._delObject(component.id) # Remove our Device relations additions. Device._relations = tuple( [x for x in Device._relations if x[0] not in NEW_DEVICE_RELATIONS]) LOG.info('Removing ZenPacks.EE.HelloWorld relationships from existing devices') self._buildDeviceRelations() super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def getDeviceBrains(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None): cat = ICatalogTool(self._getObject(uid)) reverse = bool(dir == 'DESC') qs = [] query = None globFilters = {} if params is None: params = {} for key, value in params.iteritems(): if key == 'ipAddress': ip = ensureIp(value) try: checkip(ip) except IpAddressError: pass else: if numbip(ip): minip, maxip = getSubnetBounds(ip) qs.append(Between('ipAddress', str(minip), str(maxip))) # ZEN-10057 - move filtering on indexed groups/systems/location from post-filter to query elif key in organizersToClass: organizerQuery = self.findMatchingOrganizers(organizersToClass[key], organizersToPath[key], value) if not organizerQuery: return [] qs.append(organizerQuery) elif key == 'productionState': qs.append(Or(*[Eq('productionState', str(state)) for state in value])) else: globFilters[key] = value if qs: query = And(*qs) brains = cat.search( 'Products.ZenModel.Device.Device', start=start, limit=limit, orderby=sort, reverse=reverse, query=query, globFilters=globFilters, hashcheck=hashcheck ) return brains
def getOpenStackServer(self): device_ips = set() if self.manageIp: device_ips.add(self.manageIp) for iface in self.os.interfaces(): for ip in iface.getIpAddresses(): device_ips.add(ip.split('/')[0]) catalog = ICatalogTool(self.dmd) for record in catalog.search('ZenPacks.zenoss.OpenStack.Server.Server'): server = record.getObject() server_ips = set() if server.publicIps: server_ips.update(server.publicIps) if server.privateIps: server_ips.update(server.privateIps) if server_ips.intersection(device_ips): return server
def getOpenVZComponentOnHost(self): # returns the component on the OpenVZ host, so we can link from an OpenVZ # container that is managed (SNMP/SSH) to the container component on the # host. catalog = ICatalogTool(self.dmd) # for each Container .... for record in catalog.search('ZenPacks.zenoss.OpenVZ.Container.Container'): c = record.getObject() # for each IP address in the Container... for c_ip in c.ipaddrs: if c_ip == self.manageIp: return c # for each interface on our device: for iface in self.os.interfaces(): # for each IP address on our interface: for our_ip in iface.getIpAddresses(): if c_ip == our_ip.split("/")[0]: return c for c_mac in c.macaddrs: for iface in self.os.interfaces(): our_mac = iface.getInterfaceMacaddress().lower() if our_mac == c_mac: return c return
def remove(self, app, leaveObjects=False): if not leaveObjects: NEW_COMPONENT_TYPES = tuple([x for x in productNames]) log.info('Removing ZenPacks.community.CiscoEnvmonE components') cat = ICatalogTool(app.zport.dmd) # Search the catalog for components of this zenpacks type. if NEW_COMPONENT_TYPES: for brain in cat.search(types=NEW_COMPONENT_TYPES): component = brain.getObject() component.getPrimaryParent()._delObject(component.id) # remote HW component relations DeviceHW._relations = tuple( [x for x in DeviceHW._relations if x[0] != 'voltagesensors']) log.info('Removing ZenPacks.community.CiscoEnvMonE \ relationships from existing devices') self._buildHWRelations() super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def getDevelopmentZenPacks(self, uid='/zport/dmd/ZenPackManager'): catalog = ICatalogTool(self._dmd.unrestrictedTraverse(uid)) brains = catalog.search(types=ZenPack) zenpacks = imap(unbrain, brains) return ifilter(lambda zp: zp.isDevelopment(), zenpacks)
def getDeviceUids(self, uid): cat = ICatalogTool(self._getObject(uid)) return [ b.getPath() for b in cat.search('Products.ZenModel.Device.Device') ]
def test_standard_catalogs(self): datamaps = [ RelationshipMap( modname="ZenPacks.test.ClassProxies.MyComponent", relname="myComponents", objmaps=[ObjectMap({"id": "myComponent-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyCPU", compname="hw", relname="cpus", objmaps=[ObjectMap({"id": "myCPU-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyExpansionCard", compname="hw", relname="cards", objmaps=[ObjectMap({"id": "myExpansionCard-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyFan", compname="hw", relname="fans", objmaps=[ObjectMap({"id": "myFan-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyFileSystem", compname="os", relname="filesystems", objmaps=[ObjectMap({"id": "myFileSystem-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyHardDisk", compname="hw", relname="harddisks", objmaps=[ObjectMap({"id": "myHardDisk-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyHardwareComponent", relname="myHardwareComponents", objmaps=[ObjectMap({"id": "myHardwareComponent-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyHWComponent", relname="myHWComponents", objmaps=[ObjectMap({"id": "myHWComponent-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyIpInterface", compname="os", relname="interfaces", objmaps=[ObjectMap({"id": "myIpInterface-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyIpRouteEntry", compname="os", relname="routes", objmaps=[ObjectMap({"id": "myIpRouteEntry-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyIpService", compname="os", relname="ipservices", objmaps=[ObjectMap({"id": "myIpService-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyOSComponent", relname="myOSComponents", objmaps=[ObjectMap({"id": "myOSComponent-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyOSProcess", compname="os", relname="processes", objmaps=[ObjectMap({"id": "myOSProcess-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyPowerSupply", compname="hw", relname="powersupplies", objmaps=[ObjectMap({"id": "myPowerSupply-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyService", relname="myServices", objmaps=[ObjectMap({"id": "myService-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyTemperatureSensor", compname="hw", relname="temperaturesensors", objmaps=[ObjectMap({"id": "myTemperatureSensor-1"})]), RelationshipMap( modname="ZenPacks.test.ClassProxies.MyWinService", compname="os", relname="winservices", objmaps=[ObjectMap({"id": "myWinService-1"})]), ] device = create_device( self.dmd, zPythonClass="ZenPacks.test.ClassProxies.MyDevice", device_id="test-device", datamaps=datamaps) all_classnames = { 'MyDevice', 'MyComponent', 'MyCPU', 'MyDevice', 'MyExpansionCard', 'MyFan', 'MyFileSystem', 'MyHardDisk', 'MyHardwareComponent', 'MyHWComponent', 'MyIpInterface', 'MyIpRouteEntry', 'MyIpService', 'MyOSComponent', 'MyOSProcess', 'MyPowerSupply', 'MyService', 'MyTemperatureSensor', 'MyWinService', } # global_catalog: Should be one instance of each class. global_catalog = ICatalogTool(device) fq_classnames = { "ZenPacks.test.ClassProxies.{0}.{0}".format(x) for x in all_classnames} for fq_classname in fq_classnames: results = global_catalog.search(types=[fq_classname]) self.assertTrue( results.total == 1, "{} not found in global catalog".format(fq_classname)) # componentSearch: Should be one instance of each component class. meta_types = all_classnames - {"MyDevice"} for meta_type in meta_types: results = device.componentSearch(meta_type=meta_type) self.assertTrue( len(results) == 1, "{} not found in componentSearch catalog".format(meta_type)) # ComponentBaseSearch: Should be one instance of each component class. ids = { "{}{}-1".format(x[:1].lower(), x[1:]) for x in all_classnames - {"MyDevice"}} for id_ in ids: results = device.ComponentBaseSearch(id=id_) self.assertTrue( len(results) == 1, "{} not found in ComponentBaseSearch catalog".format(id_))
def remove(self, app, leaveObjects=False): if self._v_specparams is None: return from Products.Zuul.interfaces import ICatalogTool if leaveObjects: # Check whether the ZPL-managed monitoring templates have # been modified by the user. If so, those changes will # be lost during the upgrade. # # Ideally, I would inspect self.packables() to find these # objects, but we do not have access to that relationship # at this point in the process. for dcname, dcspec in self._v_specparams.device_classes.iteritems( ): deviceclass = dcspec.get_organizer(app.zport.dmd) if not deviceclass: self.LOG.warning( "DeviceClass {} has been removed at some point " "after the {} ZenPack was installed. It will be " "reinstated if this ZenPack is upgraded or reinstalled" .format(dcname, self.id)) continue for orig_mtname, orig_mtspec in dcspec.templates.iteritems(): # attempt to find an existing template template = self.get_object(deviceclass, 'rrdTemplates', orig_mtname) # back it up if it exists if template: self.get_or_create_backup(deviceclass, 'rrdTemplates', orig_mtname) else: self.LOG.warning( "Monitoring template {}/{} has been removed at some point " "after the {} ZenPack was installed. It will be " "reinstated if this ZenPack is upgraded or reinstalled" .format(dcname, orig_mtname, self.id)) else: dc = app.Devices for catalog in self.GLOBAL_CATALOGS: catObj = getattr(dc, catalog, None) if catObj: self.LOG.info('Removing Catalog {}'.format(catalog)) dc._delObject(catalog) if self.NEW_COMPONENT_TYPES: self.LOG.info('Removing {} components'.format(self.id)) cat = ICatalogTool(app.zport.dmd) for brain in cat.search(types=self.NEW_COMPONENT_TYPES): try: component = brain.getObject() except Exception as e: self.LOG.error( "Trying to remove non-existent object {}".format( e)) continue else: component.getPrimaryParent()._delObject(component.id) # Remove our Device relations additions. from Products.ZenUtils.Utils import importClass for device_module_id in self.NEW_RELATIONS: Device = importClass(device_module_id) Device._relations = tuple([ x for x in Device._relations if x[0] not in self.NEW_RELATIONS[device_module_id] ]) self.LOG.info( 'Removing {} relationships from existing devices.'.format( self.id)) self._buildDeviceRelations(app) for dcname, dcspec in self.device_classes.iteritems(): if dcspec.remove: self.remove_device_class(app, dcspec) # Remove EventClasses with remove flag set self.remove_organizer_or_subs(app.zport.dmd.Events, self.event_classes, 'mappings', 'removeInstances') # Remove Process Classes/Organizers with remove flag set self.remove_organizer_or_subs(app.zport.dmd.Processes, self.process_class_organizers, 'process_classes', 'removeOSProcessClasses') super(ZenPack, self).remove(app, leaveObjects=leaveObjects)
def getDeviceUids(self, uid): cat = ICatalogTool(self._getObject(uid)) return [b.getPath() for b in cat.search('Products.ZenModel.Device.Device')]