def _connect(self): log.debug("TrapEventDispatcher: Connecting to %s:%d" % (self._remote_host, self._remote_port)) # create socket self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if self._socket is None: # log Error log.error("TrapEventDispatcher: Unable to create socket") # close Socket self._close() return # set socket timeout self._socket.settimeout(self._socket_timeout) # connect to graphite server try: self._socket.connect((self._remote_host, self._remote_port)) # Log log.debug("TrapEventDispatcher: Established connection to %s:%d" % (self._remote_host, self._remote_port)) except: # Log Error log.exception("TrapEventDispatcher: Failed to connect to %s:%d" % (self._remote_host, self._remote_port)) # Close Socket self._close() return
def _notification_callback(self, snmp_engine, stateReference, contextEngineId, contextName, varBinds, cbCtx): """ Callback function for receiving notifications """ trap_oid = None trap_name = None trap_args = dict() try: # get the source address for this notification transportDomain, trap_source = snmp_engine.msgAndPduDsp.getTransportInfo( stateReference) log.debug("TrapReceiver: Notification received from %s, %s" % (trap_source[0], trap_source[1])) # read all the varBinds for oid, val in varBinds: # translate OID to mib symbol/modname (module, symbol) = self._mibs.lookup_oid(oid) if module == "SNMPv2-MIB" and symbol == "snmpTrapOID": # the SNMPv2-MIB::snmpTrapOID value is the trap oid trap_oid = val # load the mib symbol/modname for the trap oid (trap_symbol_name, trap_mod_name) = self._mibs.lookup_oid(trap_oid) else: # all other values should be converted to mib symbol/modname # and put in the trap_data dict trap_arg_oid = oid # convert value trap_arg_value = self._mibs.lookup_value( module, symbol, val) trap_args[trap_arg_oid] = trap_arg_value log.debug("TrapReceiver: Trap argument: %s, %s = %s" % (module, symbol, val)) # get trap source info trap_source_address, trap_source_port = trap_source trap_source_hostname, trap_source_domain = get_hostname_from_address( trap_source_address) # set trap propreties trap_properties = dict() trap_properties['hostname'] = trap_source_hostname trap_properties['ipaddress'] = trap_source_address trap_properties['domain'] = trap_source_domain # create trap trap = self._create_trap(trap_oid, trap_args, trap_properties) # now that everything has been parsed, trigger the callback self._callback(trap) except Exception, ex: log.exception("Error handling SNMP notification")
def _notification_callback(self, snmp_engine, stateReference, contextEngineId, contextName, varBinds, cbCtx): """ Callback function for receiving notifications """ trap_oid = None trap_name = None trap_args = dict() try: # get the source address for this notification transportDomain, trap_source = snmp_engine.msgAndPduDsp.getTransportInfo(stateReference) log.debug("TrapReceiver: Notification received from %s, %s" % (trap_source[0], trap_source[1])) # read all the varBinds for oid, val in varBinds: # translate OID to mib symbol/modname (module, symbol) = self._mibs.lookup_oid(oid) if module == "SNMPv2-MIB" and symbol == "snmpTrapOID": # the SNMPv2-MIB::snmpTrapOID value is the trap oid trap_oid = val # load the mib symbol/modname for the trap oid (trap_symbol_name, trap_mod_name) = self._mibs.lookup_oid(trap_oid) else: # all other values should be converted to mib symbol/modname # and put in the trap_data dict #trap_arg_oid = oid # For the case the faultIndex was added into the OID, we have to lookup # the original OID from MIB instead of using the OID in the received packet directly. trap_arg_oid = self._mibs.lookup(module, symbol) # convert value trap_arg_value = self._mibs.lookup_value(module, symbol, val) trap_args[trap_arg_oid] = trap_arg_value log.debug("TrapReceiver: Trap argument: %s, %s = %s" % (module, symbol, val)) # get trap source info trap_source_address, trap_source_port = trap_source trap_source_hostname, trap_source_domain = get_hostname_from_address(trap_source_address) # set trap propreties trap_properties = dict() trap_properties['hostname'] = trap_source_hostname trap_properties['ipaddress'] = trap_source_address trap_properties['domain'] = trap_source_domain # create trap trap = self._create_trap(trap_oid, trap_args, trap_properties) # now that everything has been parsed, trigger the callback self._callback(trap) except Exception, ex: log.exception("Error handling SNMP notification")
def dispatch(self, event): # TODO: send event! log.debug("TrapEventDispatcher: Dispatching TrapEvent: %r" % (event)) try: # try to (re)connect if self._socket is None: log.debug( "TrapEventDispatcher: Socket is not connected. Reconnecting" ) self._connect() if self._socket is not None: # Send event self._socket.sendall(event.to_json()) if self._config['dispatcher']['check_response']: # Receive event confirmation self._socket.setblocking(0) timer = int(time.time()) data = "" while (int(time.time()) - timer) < self._socket_timeout: try: data = self._socket.recv(512) break except socket.error, e: pass self._socket.setblocking(1) if len(data) <= 0 or data.strip() != "ok": log.error( "TrapEventDispatcher: Error dispatching event. Response was: %s" % (data)) return False # TODO: send event! log.info("TrapEventDispatcher: Dispatched TrapEvent: %r" % (event)) return True except: self._close() log.exception("TrapEventDispatcher: Error dispatching event") return False
def dispatch(self, event): # TODO: send event! log.debug("TrapEventDispatcher: Dispatching TrapEvent: %r" % (event)) try: # try to (re)connect if self._socket is None: log.debug("TrapEventDispatcher: Socket is not connected. Reconnecting") self._connect() if self._socket is not None: # Send event self._socket.sendall(event.to_json()) if self._config["dispatcher"]["check_response"]: # Receive event confirmation self._socket.setblocking(0) timer = int(time.time()) data = "" while (int(time.time()) - timer) < self._socket_timeout: try: data = self._socket.recv(512) break except socket.error, e: pass self._socket.setblocking(1) if len(data) <= 0 or data.strip() != "ok": log.error("TrapEventDispatcher: Error dispatching event. Response was: %s" % (data)) return False # TODO: send event! log.info("TrapEventDispatcher: Dispatched TrapEvent: %r" % (event)) return True except: self._close() log.exception("TrapEventDispatcher: Error dispatching event") return False
try: self._connect() # Send event self._socket.sendall(event.to_json()) if self._config['dispatcher']['check_response']: # Receive event confirmation self._socket.setblocking(0) timer = int(time.time()) data = "" while (int(time.time()) - timer) < self._socket_timeout: try: data = self._socket.recv(512) break except socket.error, e: pass self._socket.setblocking(1) if len(data) <= 0 or data.strip() != "ok": log.error("TrapEventDispatcher: Error dispatching event. Response was: %s" % (data)) return False log.info("TrapEventDispatcher: Dispatched TrapEvent: %r" % (event)) return True except: log.exception("TrapEventDispatcher: Error dispatching event") finally: self._close() return False
# Send event self._socket.sendall(event.to_json()) if self._config['dispatcher']['check_response']: # Receive event confirmation self._socket.setblocking(0) timer = int(time.time()) data = "" while (int(time.time()) - timer) < self._socket_timeout: try: data = self._socket.recv(512) break except socket.error, e: pass self._socket.setblocking(1) if len(data) <= 0 or data.strip() != "ok": log.error( "TrapEventDispatcher: Error dispatching event. Response was: %s" % (data)) return False log.info("TrapEventDispatcher: Dispatched TrapEvent: %r" % (event)) return True except: log.exception("TrapEventDispatcher: Error dispatching event") finally: self._close() return False