def telegram(): data = request.json if 'callback_query' in data: author = data['callback_query']['from'] user = "******".format(author.get('first_name'), author.get('last_name')) command, alert_id = data['callback_query']['data'].split(' ', 1) alert = Alert.get(alert_id) if not alert: jsonify(status="error", message="alert not found for Telegram message") action = command.lstrip('/') if action in ['open', 'ack', 'close']: alert.set_status(status=action, text='status change via Telegram') elif action in ['watch', 'unwatch']: alert.untag(tags=["{}:{}".format(action, user)]) elif action == 'blackout': environment, resource, event = alert.split('|', 2) blackout = Blackout(environment, resource=resource, event=event) blackout.create() send_message_reply(alert, action, user, data) return jsonify(status="ok") else: return jsonify(status="error", message="no callback_query in Telegram message"), 400
def pagerduty(): data = request.json updated = False if data and 'messages' in data: for message in data['messages']: try: incident_key, status, text = parse_pagerduty(message) except ValueError as e: raise ApiError(str(e), 400) customer = g.get('customer', None) try: alert = Alert.get(id=incident_key, customer=customer) except Exception as e: raise ApiError(str(e), 500) if not alert: raise ApiError("not found", 404) try: updated = alert.set_status(status, text) except Exception as e: raise ApiError(str(e), 500) else: raise ApiError("no messages in PagerDuty data payload", 400) if updated: return jsonify(status="ok"), 200 else: raise ApiError("update PagerDuty incident status failed", 500)
def get_alert(alert_id): customer = g.get('customer', None) alert = Alert.get(alert_id, customer) if alert: return jsonify(status="ok", total=1, alert=alert.serialize) else: raise ApiError("not found", 404)
def delete_alert(alert_id): customer = g.get('customer', None) alert = Alert.get(alert_id, customer) if not alert: raise ApiError("not found", 404) if alert.delete(): return jsonify(status="ok") else: raise ApiError("failed to delete alert", 500)
def update_attributes(alert_id): if not request.json.get('attributes', None): raise ApiError("must supply 'attributes' as json data", 400) customer = g.get('customer', None) alert = Alert.get(alert_id, customer) if not alert: raise ApiError("not found", 404) if alert.update_attributes(request.json['attributes']): return jsonify(status="ok") else: raise ApiError("failed to update attributes", 500)
def untag_alert(alert_id): if not request.json.get('tags', None): raise ApiError("must supply 'tags' as json list") customer = g.get('customer', None) alert = Alert.get(alert_id, customer) if not alert: raise ApiError("not found", 404) if alert.untag(tags=request.json['tags']): return jsonify(status="ok") else: raise ApiError("failed to untag alert", 500)
def slack(): alert_id, user, action = parse_slack(request.form) alert = Alert.get(alert_id) if not alert: jsonify(status="error", message="alert not found for #slack message") if action in ['open', 'ack', 'close']: alert.set_status(status=action, text="status change via #slack by {}".format(user)) elif action in ['watch', 'unwatch']: alert.untag(alert.id, ["{}:{}".format(action, user)]) else: raise ApiError('Unsupported #slack action', 400) response = build_slack_response(alert, action, user, request.form) return jsonify(**response), 201
def set_status(alert_id): status = request.json.get('status', None) text = request.json.get('text', '') if not status: raise ApiError("must supply 'status' as json data") customer = g.get('customer', None) alert = Alert.get(alert_id, customer) if not alert: raise ApiError("not found", 404) try: alert, status, text = process_status(alert, status, text) except RejectException as e: raise ApiError(str(e), 403) except Exception as e: raise ApiError(str(e), 500) if alert.set_status(status, text): return jsonify(status="ok") else: raise ApiError("failed to set alert status", 500)