def query(self, change_id=None, filters=None, block=False, limit=None, io_loop=None): """ Query datastream :param filters: :return: """ # Basic URL and query base_url = "http://datastream/api/datastream/%s" % self.name base_qs = [] if filters: base_qs += ["filter=%s" % x for x in filters] if block: base_qs += ["block=1"] if limit: base_qs += ["limit=%d" % limit] req_headers = {"X-NOC-API-Access": "datastream:%s" % self.name} # Continue until finish while True: # Build URL # *datastream* host name will be resolved with *resolve* method qs = base_qs[:] if change_id: qs += ["from=%s" % change_id] if qs: url = "%s?%s" % (base_url, "&".join(qs)) else: url = base_url # Get data logger.debug("Request: %s", url) code, headers, data = yield fetch( url, io_loop=io_loop, resolver=self.resolve, headers=req_headers ) logger.debug("Response: %s %s", code, headers) if code == ERR_TIMEOUT or code == ERR_READ_TIMEOUT: continue # Retry on timeout elif code != 200: logger.info("Invalid response code: %s", code) raise NOCError(code=ERR_DS_BAD_CODE, msg="Invalid response code %s" % code) # Parse response try: data = ujson.loads(data) except ValueError as e: logger.info("Cannot parse response: %s", e) raise NOCError(code=ERR_DS_PARSE_ERROR, msg="Cannot parse response: %s" % e) # Process result for item in data: if "$deleted" in item: self.on_delete(item) else: self.on_change(item) # Continue from last change if "X-NOC-DataStream-Last-Change" in headers: change_id = headers["X-NOC-DataStream-Last-Change"] elif not block: break # Empty batch, stop if non-blocking mode
def check_snmp_v2c_get(self, param): """ Perform SNMP v2c GET. Param is OID or symbolic name """ try: param = mib[param] except KeyError: self.logger.error("Cannot resolve OID '%s'. Ignoring", param) return None for v in self.snmp_version: if v == SNMP_v1: r = self.snmp_v1_get(param) elif v == SNMP_v2c: r = self.snmp_v2c_get(param) else: raise NOCError(msg="Unsupported SNMP version") if r: return r
def check_snmp_v2c_get(self, param): """ Perform SNMP v2c GET. Param is OID or symbolic name """ if hasattr(self.object, "_suggest_snmp") and self.object._suggest_snmp: # Use guessed community # as defined one may be invalid snmp_ro = self.object._suggest_snmp[0] if not self.snmp_version_def: self.snmp_version_def = self.object._suggest_snmp[2] else: # @todo caps for version snmp_ro = self.object.credentials.snmp_ro if not self.snmp_version_def: self.snmp_version_def = "snmp_v2c_get" caps = self.object.get_caps() if caps.get("SNMP | v2c") is False: self.snmp_version_def = "snmp_v1_get" if not snmp_ro: self.logger.error("No SNMP credentials. Ignoring") raise NOCError(msg="No SNMP credentials") try: param = mib[param] except KeyError: self.logger.error("Cannot resolve OID '%s'. Ignoring", param) return None self.logger.info("Use %s for request", self.snmp_version_def) try: return open_sync_rpc("activator", pool=self.object.pool.name, calling_service="discovery").__getattr__( self.snmp_version_def)( self.object.address, snmp_ro, param) except RPCError as e: self.logger.error("RPC Error: %s", e) return None