def getConnection(self): """Return a wrapped app connection from the connection pool. """ if not self.db: raise ZentinelException( "running inside zope can't open connections.") with self.poollock: connection = self.db.open() root = connection.root() app = root['Application'] app = set_context(app) app._p_jar.sync() return app
def test_resolveException_with_exception_value(self): failure = Failure() failure.value = ZentinelException() self.assertIs(failure.value, resolveException(failure))
def discoverDevice(self, ip, devicepath=None, prodState=None): """ Discover the device at the given IP address. @param ip: IP address @type ip: string @param devicepath: where in the DMD to put any discovered devices @type devicepath: string @param prodState: production state (see Admin Guide for a description) @type prodState: integer @return: Twisted/Zenoss Python iterable @rtype: Python iterable """ if devicepath is None: devicepath = self.options.deviceclass if prodState is None: prodState = self.options.productionState self.scanned += 1 if self.options.maxdevices: if self.scanned >= self.options.maxdevices: self.log.info("Limit of %d devices reached", self.options.maxdevices) defer.returnValue(None) try: kw = dict(deviceName=ip, discoverProto=None, devicePath=devicepath, performanceMonitor=self.options.monitor, productionState=prodState) # If zProperties are set via a job, get them and pass them in if self.options.job: job_props = yield self.config().callRemote( 'getJobProperties', self.options.job) if job_props is not None: # grab zProperties from Job kw['zProperties'] = getattr(job_props, 'zProperties', {}) # grab other Device properties from jobs # deviceProps = job_props.get('deviceProps', {}) # kw.update(deviceProps) # @FIXME we are not getting deviceProps, check calling # chain for clues. twisted upgrade heartburn perhaps? # if we are using SNMP, lookup the device SNMP info and use the # name defined there for deviceName if not self.options.nosnmp: self.log.debug("Scanning device with address %s", ip) snmpCommunities = \ kw.get('zProperties', {}).get('zSnmpCommunities', None) snmp_config = yield self.findRemoteDeviceInfo( ip, devicepath, snmpCommunities) if snmp_config: if snmp_config.sysName: kw['deviceName'] = snmp_config.sysName if snmp_config.version: kw['zSnmpVer'] = snmp_config.version if snmp_config.port: kw['zSnmpPort'] = snmp_config.port if snmp_config.community: kw['zSnmpCommunity'] = snmp_config.community # Since we did not find any snmp info and we are in # strict discovery mode, do not create a device elif self.options.zSnmpStrictDiscovery: self.log.info( "zSnmpStrictDiscovery is True. " "Not creating device for %s.", ip) defer.returnValue(None) # RULES FOR DEVICE NAMING: # 1. If zPreferSnmpNaming is true: # If snmp name is returned, use snmp name. Otherwise, # use the passed device name. If no device name was passed, # do a dns lookup on the ip. # 2. If zPreferSnmpNaming is false: # If we are discovering a single device and a name is # passed in instead of an IP, use the passed-in name. # Otherwise, do a dns lookup on the ip. if self.options.zPreferSnmpNaming and not isip(kw['deviceName']): # In this case, we want to keep kw['deviceName'] as-is, # because it is what we got from snmp pass elif self.options.device and not isip(self.options.device): kw['deviceName'] = self.options.device else: # An IP was passed in so we do a reverse lookup on it to get # deviceName try: kw["deviceName"] = yield asyncNameLookup(ip) except Exception as ex: self.log.debug("Failed to lookup %s (%s)", ip, ex) # If it's discovering a particular device, # ignore zAutoDiscover limitations forceDiscovery = bool(self.options.device) # now create the device by calling zenhub result = None try: result = yield self.config().callRemote('createDevice', ipunwrap(ip), force=forceDiscovery, **kw) except Exception as ex: raise ZentinelException(ex) self.log.debug("Got result from remote_createDevice: %s", result) dev, created = result # if no device came back from createDevice we assume that it # was told to not auto-discover the device. This seems very # dubious to me! -EAD if not dev: self.log.info("IP '%s' on no auto-discover, skipping", ip) defer.returnValue(None) # A device came back and it already existed. if not created and not dev.temp_device: # if we shouldn't remodel skip the device by returning # at the end of this block if not self.options.remodel: self.log.info( "Found IP '%s' on device '%s';" " skipping discovery", ip, dev.id) if self.options.device: self.setExitCode(3) defer.returnValue(dev) else: # we continue on to model the device. self.log.info("IP '%s' on device '%s' remodel", ip, dev.id) self.sendDiscoveredEvent(ip, dev) # the device that we found/created or that should be remodeled # is added to the list of devices to be modeled later if not self.options.nosnmp: self.discovered.append(dev.id) yield self.config().callRemote('succeedDiscovery', dev.id) defer.returnValue(dev) except ZentinelException as e: self.log.exception(e) if self.options.snmpMissing: self.sendEvent( dict(device=ip, component=ip, ipAddress=ip, eventKey=ip, eventClass=Status_Snmp, summary=str(e), severity=Info, agent="Discover")) except Exception as e: self.log.exception("Failed device discovery for '%s'", ip) finally: self.log.info("Finished scanning device with address %s", ip)
def inner(driver): """ Twisted driver class to iterate through devices @param driver: Zenoss driver @type driver: Zenoss driver @return: successful result is a list of IPs that were added @rtype: Twisted deferred @todo: modularize this function (130+ lines is ridiculous) """ try: kw = dict(deviceName=ip, discoverProto=None, devicePath=devicepath, performanceMonitor=self.options.monitor, productionState=prodState) # If zProperties are set via a job, get them and pass them in if self.options.job: yield self.config().callRemote('getJobProperties', self.options.job) job_props = driver.next() if job_props is not None: # grab zProperties from Job kw['zProperties'] = getattr(job_props, 'zProperties', {}) # grab other Device properties from jobs #deviceProps = job_props.get('deviceProps', {}) #kw.update(deviceProps) #@FIXME we are not getting deviceProps, check calling # chain for clues. twisted upgrade heartburn perhaps? # if we are using SNMP, lookup the device SNMP info and use the # name defined there for deviceName if not self.options.nosnmp: self.log.debug("Scanning device with address %s", ip) snmpCommunities = kw.get('zProperties', {}).get('zSnmpCommunities', None) yield self.findRemoteDeviceInfo(ip, devicepath, snmpCommunities) snmp_config = driver.next() if snmp_config: if snmp_config.sysName: kw['deviceName'] = snmp_config.sysName if snmp_config.version: kw['zSnmpVer'] = snmp_config.version if snmp_config.port: kw['zSnmpPort'] = snmp_config.port if snmp_config.community: kw['zSnmpCommunity'] = snmp_config.community # if we are using SNMP, did not find any snmp info, # and we are in strict discovery mode, do not # create a device elif self.options.zSnmpStrictDiscovery: self.log.info('zSnmpStrictDiscovery is True. ' + 'Not creating device for %s.' % ip) return # RULES FOR DEVICE NAMING: # 1. If zPreferSnmpNaming is true: # If snmp name is returned, use snmp name. Otherwise, # use the passed device name. If no device name was passed, # do a dns lookup on the ip. # 2. If zPreferSnmpNaming is false: # If we are discovering a single device and a name is # passed in instead of an IP, use the passed-in name. # Otherwise, do a dns lookup on the ip. if self.options.zPreferSnmpNaming and \ not isip( kw['deviceName'] ): # In this case, we want to keep kw['deviceName'] as-is, # because it is what we got from snmp pass elif self.options.device and not isip(self.options.device): kw['deviceName'] = self.options.device else: # An IP was passed in so we do a reverse lookup on it to get # deviceName yield asyncNameLookup(ip) try: kw.update(dict(deviceName=driver.next())) except Exception, ex: self.log.debug("Failed to lookup %s (%s)" % (ip, ex)) # If it's discovering a particular device, # ignore zAutoDiscover limitations forceDiscovery = bool(self.options.device) # now create the device by calling zenhub yield self.config().callRemote('createDevice', ipunwrap(ip), force=forceDiscovery, **kw) result = driver.next() self.log.debug( "ZenDisc.discoverDevice.inner: got result from remote_createDevice" ) if isinstance(result, Failure): raise ZentinelException(result.value) dev, created = result # if no device came back from createDevice we assume that it # was told to not auto-discover the device. This seems very # dubious to me! -EAD if not dev: self.log.info("IP '%s' on no auto-discover, skipping", ip) return else: # A device came back and it already existed. if not created and not dev.temp_device: # if we shouldn't remodel skip the device by returning # at the end of this block if not self.options.remodel: self.log.info( "Found IP '%s' on device '%s';" " skipping discovery", ip, dev.id) if self.options.device: self.setExitCode(3) yield succeed(dev) driver.next() return else: # we continue on to model the device. self.log.info("IP '%s' on device '%s' remodel", ip, dev.id) self.sendDiscoveredEvent(ip, dev) # the device that we found/created or that should be remodeled # is added to the list of devices to be modeled later if not self.options.nosnmp: self.discovered.append(dev.id) yield succeed(dev) driver.next()