def getDevices(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None): params = params if params else {} # Passing a key that's not an index in the catalog will # cause a serious performance penalty. Remove 'status' from # the 'params' parameter before passing it on. statuses = params.pop('status', None) brains = self.getDeviceBrains(uid, start, limit, sort, dir, params, hashcheck) # ZEN-10057 - Handle the case of empty results for a filter with no matches if not brains: return SearchResults(iter([]), 0, '0') devices = [IInfo(obj) for obj in imap(unbrain, brains) if obj] if statuses is not None and len(statuses) < 3: devices = [d for d in devices if d.status in statuses] uuids = set(dev.uuid for dev in devices) if uuids: zep = getFacade('zep', self._dmd) severities = zep.getEventSeverities(uuids) for device in devices: device.setEventSeverities(severities[device.uuid]) return SearchResults(iter(devices), brains.total, brains.hash_)
def getDevices(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None): # We have to query for the process(es) and then use unrestrictedTraverse on the deviceIds to get the devices brains = self.getObjectBrains(uid, 0, None, sort, dir, params, None, 'Products.ZenModel.OSProcess.OSProcess', ['deviceId']) # ZEN-10057 - Handle the case of empty results for a filter with no matches if not brains: return SearchResults([], 0, []) def getDevice(devId): return self.context.dmd.unrestrictedTraverse(str(devId)) devices = list(getDevice(brain.deviceId) for brain in brains) devices = list(device for device in devices if device) # we may have changed the number of results, so check the hash here totalCount = len(devices) hash_ = str(totalCount) if hashcheck is not None: if hash_ != hashcheck: raise StaleResultsException("Search results do not match") # Pick out the correct range start = max(start, 0) if limit is None: stop = None else: stop = start + limit results = islice(devices, start, stop) deviceInfos = list(imap(IInfo, results)) # This part is copy-paste from ZuulFacade.getDevices: uuids = set(dev.uuid for dev in deviceInfos) if uuids: zep = getFacade('zep', self._dmd) severities = zep.getEventSeverities(uuids) for device in deviceInfos: device.setEventSeverities(severities[device.uuid]) return SearchResults(iter(deviceInfos), totalCount, hash_)
def getUsers(self, start=0, limit=50, sort='name', dir='ASC', name=None): users = map(IInfo, self._dmd.ZenUsers.getAllUserSettings()) sortedUsers = sorted(users, key=lambda u: getattr(u, sort)) if dir == "DESC": sortedUsers.reverse() total = len(sortedUsers) return SearchResults(iter(sortedUsers[start:limit]), total, total, areBrains=False)
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 getDevices(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None): brains = self.getDeviceBrains(uid, start, limit, sort, dir, params, hashcheck) # ZEN-10057 - Handle the case of empty results for a filter with no matches if not brains: return SearchResults([], 0, []) devices = list(imap(IInfo, imap(unbrain, brains))) uuids = set(dev.uuid for dev in devices) if uuids: zep = getFacade('zep', self._dmd) severities = zep.getEventSeverities(uuids) for device in devices: device.setEventSeverities(severities[device.uuid]) return SearchResults(iter(devices), brains.total, brains.hash_)
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 _componentSearch(self, uid=None, types=(), meta_type=(), start=0, limit=None, sort='name', dir='ASC', name=None, keys=()): reverse = dir=='DESC' if isinstance(types, basestring): types = (types,) if isinstance(meta_type, basestring): meta_type = (meta_type,) querySet = [] if meta_type: querySet.append(Or(*(Eq('meta_type', t) for t in meta_type))) querySet.append(Generic('getAllPaths', uid)) query = And(*querySet) obj = self._getObject(uid) cat = obj.device().componentSearch if 'getAllPaths' not in cat.indexes(): obj.device()._createComponentSearchPathIndex() brains = cat.evalAdvancedQuery(query) # unbrain the results comps=map(IInfo, map(unbrain, brains)) # filter the components if name is not None: comps = self._filterComponents(comps, keys, name) total = len(comps) hash_ = str(total) def componentSortKey(parent): val = getattr(parent, sort) if val: if isinstance(val, list): val = val[0] if callable(val): val = val() if IInfo.providedBy(val): val = val.name return val # sort the components sortedResults = list(sorted(comps, key=componentSortKey, reverse=reverse)) # limit the search results to the specified range if limit is None: pagedResult = sortedResults[start:] else: pagedResult = sortedResults[start:start + limit] # fetch any rrd data necessary self.bulkLoadMetricData(pagedResult) return SearchResults(iter(pagedResult), total, hash_, False)
def getList(self, limit=None, start=None, sort='name', dir='DESC', params=None, uid=None, criteria=()): brains = self._processSearch(limit, start, sort, dir, params, uid, criteria) wrapped = imap(IInfo, imap(unbrain, brains)) return SearchResults(wrapped, brains.total, brains.hash_)
def _componentSearch(self, uid=None, types=(), meta_type=(), start=0, limit=None, sort='name', dir='ASC', name=None, keys=()): reverse = dir == 'DESC' if isinstance(types, basestring): types = (types, ) if isinstance(meta_type, basestring): meta_type = (meta_type, ) querySet = [] if meta_type: querySet.append(Or(*(Eq('meta_type', t) for t in meta_type))) querySet.append(Generic('getAllPaths', uid)) query = And(*querySet) obj = self._getObject(uid) if getattr(aq_base(obj.device()), 'componentSearch', None) is None: obj.device()._create_componentSearch() cat = obj.device().componentSearch if 'getAllPaths' not in cat.indexes(): obj.device()._createComponentSearchPathIndex() brains = cat.evalAdvancedQuery(query) # unbrain the results comps = map(IInfo, map(unbrain, brains)) total = len(comps) hash_ = str(total) # filter the components if name is not None: wrapped = map(IInfo, map(unbrain, brains)) comps = self._filterComponents(wrapped, keys, name) total = len(comps) hash_ = str(total) # sort the components sortedResults = list( sorted(comps, key=lambda x: getattr(x, sort), reverse=reverse)) # limit the search results to the specified range if limit is None: pagedResult = sortedResults[start:] else: pagedResult = sortedResults[start:start + limit] return SearchResults(iter(pagedResult), total, hash_, False)
def _typecatComponentPostProcess(self, brains, total, sort='name', reverse=False): hash_ = str(total) comps = map(IInfo, map(unbrain, brains)) # fetch any rrd data necessary self.bulkLoadMetricData(comps) # Do one big lookup of component events and add to the result objects showSeverityIcon = self.context.dmd.UserInterfaceSettings.getInterfaceSettings().get('showEventSeverityIcons') if showSeverityIcon: uuids = [r.uuid for r in comps] zep = getFacade('zep') severities = zep.getWorstSeverity(uuids) for r in comps: r.setWorstEventSeverity(severities[r.uuid]) sortedComps = sorted(comps, key=lambda x: getattr(x, sort), reverse=reverse) return SearchResults(iter(sortedComps), total, hash_, False)
def getDevices(self, uid=None, start=0, limit=50, sort='name', dir='ASC', params=None, hashcheck=None): brains = self.getDeviceBrains(uid, start, limit, sort, dir, params, hashcheck) devices = list(imap(IInfo, imap(unbrain, brains))) uuids = set(dev.uuid for dev in devices) if uuids: zep = getFacade('zep', self._dmd) severities = zep.getEventSeverities(uuids) for device in devices: device.setEventSeverities(severities[device.uuid]) return SearchResults(iter(devices), brains.total, brains.hash_)
def _componentSearch(self, uid=None, types=(), meta_type=(), start=0, limit=None, sort='name', dir='ASC', name=None, keys=()): reverse = dir == 'DESC' if isinstance(meta_type, basestring) and get_component_field_spec( meta_type) is not None: brains, total = self._typecatComponentBrains( uid, types, meta_type, start, limit, sort, dir, name, keys) if brains is not None: return self._typecatComponentPostProcess( brains, total, sort, reverse) if isinstance(meta_type, basestring): meta_type = (meta_type, ) if isinstance(types, basestring): types = (types, ) querySet = [] if meta_type: querySet.append(Or(*(Eq('meta_type', t) for t in meta_type))) querySet.append(Generic('getAllPaths', uid)) query = And(*querySet) obj = self._getObject(uid) cat = obj.device().componentSearch if 'getAllPaths' not in cat.indexes(): obj.device()._createComponentSearchPathIndex() brains = cat.evalAdvancedQuery(query) # unbrain the results comps = [] for brain in brains: try: comps.append(IInfo(unbrain(brain))) except: log.warn( 'There is broken component "{}" in componentSearch catalog on {} device.' .format(brain.id, obj.device().id)) # filter the components if name is not None: comps = self._filterComponents(comps, keys, name) total = len(comps) hash_ = str(total) def componentSortKey(parent): val = getattr(parent, sort) if val: if isinstance(val, list): val = val[0] if callable(val): val = val() if IInfo.providedBy(val): val = val.name return pad_numeric_values_for_indexing(val) # sort the components sortedResults = list( sorted(comps, key=componentSortKey, reverse=reverse)) # limit the search results to the specified range if limit is None: pagedResult = sortedResults[start:] else: pagedResult = sortedResults[start:start + limit] # fetch any rrd data necessary self.bulkLoadMetricData(pagedResult) # Do one big lookup of component events and add to the result objects showSeverityIcon = self.context.dmd.UserInterfaceSettings.getInterfaceSettings( ).get('showEventSeverityIcons') if showSeverityIcon: uuids = [r.uuid for r in pagedResult] zep = getFacade('zep') severities = zep.getWorstSeverity(uuids) for r in pagedResult: r.setWorstEventSeverity(severities[r.uuid]) return SearchResults(iter(pagedResult), total, hash_, False)