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 dispatch(self, event): log.debug("TrapEventDispatcher: Dispatching TrapEvent: %r" % (event)) 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
def dispatch(self, event): log.debug("TrapEventDispatcher: Dispatching TrapEvent: %r" % (event)) 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
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