2017-02-26 09:10:14 +01:00
|
|
|
from datetime import datetime
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, List, Optional, Callable, Tuple, Text
|
2016-08-31 21:23:20 +02:00
|
|
|
|
2017-04-15 03:29:56 +02:00
|
|
|
from django.utils.timezone import utc as timezone_utc
|
2016-08-31 21:23:20 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
2016-08-31 21:23:20 +02:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-09-30 04:18:16 +02:00
|
|
|
from zerver.lib.actions import check_send_stream_message
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-08-31 21:23:20 +02:00
|
|
|
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
ALERT_CLEAR = 'clear'
|
|
|
|
ALERT_VIOLATION = 'violations'
|
|
|
|
SNAPSHOT = 'image_url'
|
|
|
|
|
2017-11-05 11:53:59 +01:00
|
|
|
class LibratoWebhookParser:
|
2016-08-31 21:23:20 +02:00
|
|
|
ALERT_URL_TEMPLATE = "https://metrics.librato.com/alerts#/{alert_id}"
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def __init__(self, payload: Dict[str, Any], attachments: List[Dict[str, Any]]) -> None:
|
2016-08-31 21:23:20 +02:00
|
|
|
self.payload = payload
|
|
|
|
self.attachments = attachments
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def generate_alert_url(self, alert_id: int) -> Text:
|
2016-08-31 21:23:20 +02:00
|
|
|
return self.ALERT_URL_TEMPLATE.format(alert_id=alert_id)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def parse_alert(self) -> Tuple[int, Text, Text, Text]:
|
2016-08-31 21:23:20 +02:00
|
|
|
alert = self.payload['alert']
|
|
|
|
alert_id = alert['id']
|
|
|
|
return alert_id, alert['name'], self.generate_alert_url(alert_id), alert['runbook_url']
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def parse_condition(self, condition: Dict[str, Any]) -> Tuple[Text, Text, Text, Text]:
|
2016-08-31 21:23:20 +02:00
|
|
|
summary_function = condition['summary_function']
|
|
|
|
threshold = condition.get('threshold', '')
|
|
|
|
condition_type = condition['type']
|
|
|
|
duration = condition.get('duration', '')
|
|
|
|
return summary_function, threshold, condition_type, duration
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def parse_violation(self, violation: Dict[str, Any]) -> Tuple[Text, Text]:
|
2016-08-31 21:23:20 +02:00
|
|
|
metric_name = violation['metric']
|
2017-02-26 09:10:14 +01:00
|
|
|
recorded_at = datetime.fromtimestamp((violation['recorded_at']),
|
2017-04-15 03:29:56 +02:00
|
|
|
tz=timezone_utc).strftime('%Y-%m-%d %H:%M:%S')
|
2016-08-31 21:23:20 +02:00
|
|
|
return metric_name, recorded_at
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def parse_conditions(self) -> List[Dict[str, Any]]:
|
2016-08-31 21:23:20 +02:00
|
|
|
conditions = self.payload['conditions']
|
|
|
|
return conditions
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def parse_violations(self) -> List[Dict[str, Any]]:
|
2016-08-31 21:23:20 +02:00
|
|
|
violations = self.payload['violations']['test-source']
|
|
|
|
return violations
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def parse_snapshot(self, snapshot: Dict[str, Any]) -> Tuple[Text, Text, Text]:
|
2016-08-31 21:23:20 +02:00
|
|
|
author_name, image_url, title = snapshot['author_name'], snapshot['image_url'], snapshot['title']
|
|
|
|
return author_name, image_url, title
|
|
|
|
|
|
|
|
class LibratoWebhookHandler(LibratoWebhookParser):
|
2017-11-04 07:47:46 +01:00
|
|
|
def __init__(self, payload: Dict[str, Any], attachments: List[Dict[str, Any]]) -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().__init__(payload, attachments)
|
2016-08-31 21:23:20 +02:00
|
|
|
self.payload_available_types = {
|
|
|
|
ALERT_CLEAR: self.handle_alert_clear_message,
|
|
|
|
ALERT_VIOLATION: self.handle_alert_violation_message
|
|
|
|
}
|
|
|
|
|
|
|
|
self.attachments_available_types = {
|
|
|
|
SNAPSHOT: self.handle_snapshots
|
|
|
|
}
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def find_handle_method(self) -> Callable[[], Text]:
|
2016-08-31 21:23:20 +02:00
|
|
|
for available_type in self.payload_available_types:
|
|
|
|
if self.payload.get(available_type):
|
|
|
|
return self.payload_available_types[available_type]
|
|
|
|
for available_type in self.attachments_available_types:
|
|
|
|
if self.attachments[0].get(available_type):
|
|
|
|
return self.attachments_available_types[available_type]
|
|
|
|
raise Exception("Unexcepted message type")
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def handle(self) -> Text:
|
2016-08-31 21:23:20 +02:00
|
|
|
return self.find_handle_method()()
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def generate_topic(self) -> Text:
|
2016-08-31 21:23:20 +02:00
|
|
|
if self.attachments:
|
|
|
|
return "Snapshots"
|
|
|
|
topic_template = "Alert {alert_name}"
|
|
|
|
alert_id, alert_name, alert_url, alert_runbook_url = self.parse_alert()
|
|
|
|
return topic_template.format(alert_name=alert_name)
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def handle_alert_clear_message(self) -> Text:
|
2017-02-26 09:10:14 +01:00
|
|
|
alert_clear_template = "Alert [alert_name]({alert_url}) has cleared at {trigger_time} UTC!"
|
|
|
|
trigger_time = datetime.fromtimestamp((self.payload['trigger_time']),
|
2017-04-15 03:29:56 +02:00
|
|
|
tz=timezone_utc).strftime('%Y-%m-%d %H:%M:%S')
|
2016-08-31 21:23:20 +02:00
|
|
|
alert_id, alert_name, alert_url, alert_runbook_url = self.parse_alert()
|
2017-11-05 02:48:25 +01:00
|
|
|
content = alert_clear_template.format(alert_name=alert_name,
|
|
|
|
alert_url=alert_url,
|
|
|
|
trigger_time=trigger_time)
|
2016-08-31 21:23:20 +02:00
|
|
|
return content
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def handle_snapshots(self) -> Text:
|
2016-08-31 21:23:20 +02:00
|
|
|
content = u''
|
|
|
|
for attachment in self.attachments:
|
|
|
|
content += self.handle_snapshot(attachment)
|
|
|
|
return content
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def handle_snapshot(self, snapshot: Dict[str, Any]) -> Text:
|
2016-08-31 21:23:20 +02:00
|
|
|
snapshot_template = u"**{author_name}** sent a [snapshot]({image_url}) of [metric]({title})"
|
|
|
|
author_name, image_url, title = self.parse_snapshot(snapshot)
|
|
|
|
content = snapshot_template.format(author_name=author_name, image_url=image_url, title=title)
|
|
|
|
return content
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def handle_alert_violation_message(self) -> Text:
|
2016-08-31 21:23:20 +02:00
|
|
|
alert_violation_template = u"Alert [alert_name]({alert_url}) has triggered! "
|
|
|
|
alert_id, alert_name, alert_url, alert_runbook_url = self.parse_alert()
|
|
|
|
content = alert_violation_template.format(alert_name=alert_name, alert_url=alert_url)
|
|
|
|
if alert_runbook_url:
|
|
|
|
alert_runbook_template = u"[Reaction steps]({alert_runbook_url})"
|
|
|
|
content += alert_runbook_template.format(alert_runbook_url=alert_runbook_url)
|
|
|
|
content += self.generate_conditions_and_violations()
|
|
|
|
return content
|
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def generate_conditions_and_violations(self) -> Text:
|
2016-08-31 21:23:20 +02:00
|
|
|
conditions = self.parse_conditions()
|
|
|
|
violations = self.parse_violations()
|
|
|
|
content = u""
|
|
|
|
for condition, violation in zip(conditions, violations):
|
|
|
|
content += self.generate_violated_metric_condition(violation, condition)
|
|
|
|
return content
|
|
|
|
|
2017-11-10 04:33:28 +01:00
|
|
|
def generate_violated_metric_condition(self, violation: Dict[str, Any],
|
|
|
|
condition: Dict[str, Any]) -> Text:
|
2016-08-31 21:23:20 +02:00
|
|
|
summary_function, threshold, condition_type, duration = self.parse_condition(condition)
|
|
|
|
metric_name, recorded_at = self.parse_violation(violation)
|
2017-11-10 04:33:28 +01:00
|
|
|
metric_condition_template = (u"\n>Metric `{metric_name}`, {summary_function} "
|
|
|
|
"was {condition_type} {threshold}")
|
2016-08-31 21:23:20 +02:00
|
|
|
content = metric_condition_template.format(
|
2017-01-24 07:06:13 +01:00
|
|
|
metric_name=metric_name, summary_function=summary_function, condition_type=condition_type,
|
|
|
|
threshold=threshold)
|
2016-08-31 21:23:20 +02:00
|
|
|
if duration:
|
|
|
|
content += u" by {duration}s".format(duration=duration)
|
2017-02-26 09:10:14 +01:00
|
|
|
content += u", recorded at {recorded_at} UTC".format(recorded_at=recorded_at)
|
2016-08-31 21:23:20 +02:00
|
|
|
return content
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Librato')
|
|
|
|
@has_request_variables
|
2017-05-02 01:00:50 +02:00
|
|
|
def api_librato_webhook(request, user_profile, payload=REQ(converter=ujson.loads, default={}),
|
2016-08-31 21:23:20 +02:00
|
|
|
stream=REQ(default='librato'), topic=REQ(default=None)):
|
2017-05-02 01:00:50 +02:00
|
|
|
# type: (HttpRequest, UserProfile, Dict[str, Any], Text, Text) -> HttpResponse
|
2016-08-31 21:23:20 +02:00
|
|
|
try:
|
|
|
|
attachments = ujson.loads(request.body).get('attachments', [])
|
|
|
|
except ValueError:
|
|
|
|
attachments = []
|
|
|
|
|
|
|
|
if not attachments and not payload:
|
|
|
|
return json_error(_("Malformed JSON input"))
|
|
|
|
|
|
|
|
message_handler = LibratoWebhookHandler(payload, attachments)
|
|
|
|
|
|
|
|
if not topic:
|
|
|
|
topic = message_handler.generate_topic()
|
|
|
|
|
|
|
|
try:
|
|
|
|
content = message_handler.handle()
|
|
|
|
except Exception as e:
|
|
|
|
return json_error(_(str(e)))
|
|
|
|
|
2017-09-30 04:18:16 +02:00
|
|
|
check_send_stream_message(user_profile, request.client, stream, topic, content)
|
2016-08-31 21:23:20 +02:00
|
|
|
return json_success()
|