def receive_alert(): recv_started = receive_timer.start_timer() try: incomingAlert = Alert.parse_alert(request.data) except ValueError as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 400 if request.headers.getlist("X-Forwarded-For"): incomingAlert.attributes.update(ip=request.headers.getlist("X-Forwarded-For")[0]) else: incomingAlert.attributes.update(ip=request.remote_addr) try: alert = process_alert(incomingAlert) except RejectException as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 403 except RuntimeWarning as e: receive_timer.stop_timer(recv_started) return jsonify(status="ok", id=incomingAlert.id, message=str(e)), 202 except Exception as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 500 receive_timer.stop_timer(recv_started) if alert: body = alert.get_body() body['href'] = "%s/%s" % (request.base_url, alert.id) return jsonify(status="ok", id=alert.id, alert=body), 201, {'Location': '%s/%s' % (request.base_url, alert.id)} else: return jsonify(status="error", message="insert or update of received alert failed"), 500
def receive_alert(): recv_started = receive_timer.start_timer() try: incomingAlert = Alert.parse_alert(request.data) except ValueError, e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 400
def on_message(self, headers, body): LOG.info("Received %s %s", headers['type'], headers['correlation-id']) LOG.debug("Received body : %s", body) if headers['type'] == 'Heartbeat': # TODO(nsatterl): Heartbeat.parse_heartbeat(body) etc. pass elif headers['type'].endswith('Alert'): alert = Alert.parse_alert(body) if alert: alert.receive_now() LOG.debug('Queueing alert %s', alert.get_body()) self.queue.put(alert)
def receive_alert(): if not Switch.get('sender-api-allow').is_on(): return jsonify( status="error", message="API not accepting alerts. Try again later."), 503 recv_started = receive_timer.start_timer() try: incomingAlert = Alert.parse_alert(request.data) except ValueError as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 400 if g.get('customer', None): incomingAlert.customer = g.get('customer') if request.headers.getlist("X-Forwarded-For"): incomingAlert.attributes.update( ip=request.headers.getlist("X-Forwarded-For")[0]) else: incomingAlert.attributes.update(ip=request.remote_addr) try: alert = process_alert(incomingAlert) except RejectException as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 403 except RuntimeWarning as e: receive_timer.stop_timer(recv_started) return jsonify(status="ok", id=incomingAlert.id, message=str(e)), 202 except Exception as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 500 receive_timer.stop_timer(recv_started) if alert: body = alert.get_body() body['href'] = absolute_url('/alert/' + alert.id) return jsonify(status="ok", id=alert.id, alert=body), 201, { 'Location': body['href'] } else: return jsonify( status="error", message="insert or update of received alert failed"), 500
def receive_alert(): recv_started = receive_timer.start_timer() try: incomingAlert = Alert.parse_alert(request.data) except ValueError as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 400 if g.get('customer', None): incomingAlert.customer = g.get('customer') if request.headers.getlist("X-Forwarded-For"): incomingAlert.attributes.update( ip=request.headers.getlist("X-Forwarded-For")[0]) else: incomingAlert.attributes.update(ip=request.remote_addr) try: alert = process_alert(incomingAlert) except RejectException as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 403 except RuntimeWarning as e: receive_timer.stop_timer(recv_started) return jsonify(status="ok", id=incomingAlert.id, message=str(e)), 202 except Exception as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 500 receive_timer.stop_timer(recv_started) if alert: body = alert.get_body() body['href'] = "%s/%s" % (request.base_url, alert.id) return jsonify(status="ok", id=alert.id, alert=body), 201, { 'Location': '%s/%s' % (request.base_url, alert.id) } else: return jsonify( status="error", message="insert or update of received alert failed"), 500
def on_message(self, headers, body): LOG.debug("Received: %s", body) alert = Alert.parse_alert(body).get_body() if alert: LOG.info('%s : [%s] %s', alert['lastReceiveId'], alert['status'], alert['summary']) # TODO(nsatterl): is this still required? if 'tags' not in alert or not alert['tags']: # Kibana GUI borks if tags are null alert['tags'] = 'none' LOG.debug('alert last receivetime %s', alert['lastReceiveTime']) logstash = { '@message': alert['summary'], '@source': alert['resource'], '@source_host': 'not_used', '@source_path': alert['origin'], '@tags': alert['tags'], '@timestamp': json.dumps(alert['lastReceiveTime'], cls=DateEncoder), '@type': alert['type'], '@fields': str(alert) } LOG.debug('Index payload %s', logstash) try: index_url = "http://%s:%s/alerta/%s" % (CONF.es_host, CONF.es_port, 'alerta-' + datetime.datetime.utcnow().strftime('%Y.%M.%d')) LOG.debug('Index URL: %s', index_url) response = urllib2.urlopen(index_url, json.dumps(logstash)).read() except Exception, e: LOG.error('%s : Alert indexing to %s failed - %s', alert['lastReceiveId'], url, e) return try: es_id = json.loads(response)['_id'] LOG.info('%s : Alert indexed at %s/%s', alert['lastReceiveId'], index_url, es_id) except Exception: pass
def receive_alert(): if not Switch.get('sender-api-allow').is_on(): return jsonify(status="error", message="API not accepting alerts. Try again later."), 503 recv_started = receive_timer.start_timer() try: incomingAlert = Alert.parse_alert(request.data) except ValueError as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 400 if g.get('customer', None): incomingAlert.customer = g.get('customer') if request.headers.getlist("X-Forwarded-For"): incomingAlert.attributes.update(ip=request.headers.getlist("X-Forwarded-For")[0]) else: incomingAlert.attributes.update(ip=request.remote_addr) try: alert = process_alert(incomingAlert) except RejectException as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 403 except RuntimeWarning as e: receive_timer.stop_timer(recv_started) return jsonify(status="ok", id=incomingAlert.id, message=str(e)), 202 except Exception as e: receive_timer.stop_timer(recv_started) return jsonify(status="error", message=str(e)), 500 receive_timer.stop_timer(recv_started) if alert: body = alert.get_body() body['href'] = absolute_url('/alert/' + alert.id) return jsonify(status="ok", id=alert.id, alert=body), 201, {'Location': body['href']} else: return jsonify(status="error", message="insert or update of received alert failed"), 500
def on_message(self, headers, body): if tokens: _Lock.acquire() tokens -= 1 _Lock.release() LOG.debug('Taken a token, there are only %d left', tokens) else: LOG.warning('%s : No tokens left, rate limiting this alert', 'FIXME') #TODO(nsatterl): alert['lastReceiveId']) return LOG.debug("Received alert : %s", body) if headers['type'].endswith('Alert'): alert = Alert.parse_alert(body) if alert: LOG.info('%s : Send IRC message to %s', alert['lastReceiveId'], IRC_CHANNEL) shortid = alert['id'].split('-')[0] try: self.irc.send( 'PRIVMSG %s :%s [%s] %s\r\n' % (IRC_CHANNEL, shortid, alert['status'], alert['summary'])) except Exception, e: LOG.error('%s : IRC send failed - %s', alert['lastReceiveId'], e)