##// END OF EJS Templates
setup: change url to github
setup: change url to github

File last commit:

r153:32f4b641
r196:472d1df0 master
Show More
slack.py
270 lines | 9.6 KiB | text/x-python | PythonLexer
project: initial commit
r0 # -*- coding: utf-8 -*-
license: change the license to Apache 2.0
r112 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # http://www.apache.org/licenses/LICENSE-2.0
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project: initial commit
r0
import logging
from appenlight.models.alert_channel import AlertChannel
from appenlight.models.integrations.slack import SlackIntegration
from webhelpers2.text import truncate
log = logging.getLogger(__name__)
class SlackAlertChannel(AlertChannel):
black: reformat source
r153 __mapper_args__ = {"polymorphic_identity": "slack"}
project: initial commit
r0
def notify_reports(self, **kwargs):
"""
Notify user of individual reports
kwargs:
application: application that the event applies for,
user: user that should be notified
request: request object
since_when: reports are newer than this time value,
reports: list of reports to render
"""
template_vars = self.report_alert_notification_vars(kwargs)
black: reformat source
r153 template_vars["title"] = template_vars["resource_name"]
project: initial commit
r0
black: reformat source
r153 if template_vars["confirmed_total"] > 1:
template_vars["subtext"] = "%s reports" % template_vars["confirmed_total"]
project: initial commit
r0 else:
black: reformat source
r153 error_title = truncate(
template_vars["reports"][0][1].error or "slow report", 90
)
template_vars["subtext"] = error_title
project: initial commit
r0
black: reformat source
r153 log_msg = "NOTIFY : %s via %s :: %s reports" % (
kwargs["user"].user_name,
project: initial commit
r0 self.channel_visible_value,
black: reformat source
r153 template_vars["confirmed_total"],
)
project: initial commit
r0 log.warning(log_msg)
black: reformat source
r153 client = SlackIntegration.create_client(self.integration.config["webhook_url"])
project: initial commit
r0 report_data = {
refactor: fix inconsistent naming
r28 "username": "AppEnlight",
project: initial commit
r0 "icon_emoji": ":fire:",
"attachments": [
{
"mrkdwn_in": ["text", "pretext", "title", "fallback"],
black: reformat source
r153 "fallback": "*%s* - <%s| Browse>"
% (template_vars["title"], template_vars["destination_url"]),
"pretext": "*%s* - <%s| Browse>"
% (template_vars["title"], template_vars["destination_url"]),
project: initial commit
r0 "color": "warning",
"fields": [
black: reformat source
r153 {"value": "Info: %s" % template_vars["subtext"], "short": False}
],
project: initial commit
r0 }
black: reformat source
r153 ],
project: initial commit
r0 }
client.make_request(data=report_data)
def notify_report_alert(self, **kwargs):
"""
Build and send report alert notification
Kwargs:
application: application that the event applies for,
event: event that is notified,
user: user that should be notified
request: request object
"""
template_vars = self.report_alert_notification_vars(kwargs)
black: reformat source
r153 if kwargs["event"].unified_alert_action() == "OPEN":
title = "*ALERT %s*: %s" % (
template_vars["alert_action"],
template_vars["resource_name"],
project: initial commit
r0 )
black: reformat source
r153 template_vars["subtext"] = "Got at least %s %s" % (
kwargs["event"].values["reports"],
template_vars["report_type"],
project: initial commit
r0 )
else:
black: reformat source
r153 title = "*ALERT %s*: %s" % (
template_vars["alert_action"],
template_vars["resource_name"],
project: initial commit
r0 )
black: reformat source
r153 template_vars["subtext"] = ""
project: initial commit
r0
black: reformat source
r153 alert_type = template_vars["alert_type"].replace("_", " ")
alert_type = alert_type.replace("alert", "").capitalize()
project: initial commit
r0
black: reformat source
r153 template_vars["type"] = "Type: %s" % alert_type
project: initial commit
r0
black: reformat source
r153 client = SlackIntegration.create_client(self.integration.config["webhook_url"])
project: initial commit
r0 report_data = {
refactor: fix inconsistent naming
r28 "username": "AppEnlight",
project: initial commit
r0 "icon_emoji": ":rage:",
"attachments": [
{
"mrkdwn_in": ["text", "pretext", "title", "fallback"],
black: reformat source
r153 "fallback": "%s - <%s| Browse>"
% (title, template_vars["destination_url"]),
"pretext": "%s - <%s| Browse>"
% (title, template_vars["destination_url"]),
project: initial commit
r0 "color": "danger",
"fields": [
{
black: reformat source
r153 "title": template_vars["type"],
"value": template_vars["subtext"],
"short": False,
project: initial commit
r0 }
black: reformat source
r153 ],
project: initial commit
r0 }
black: reformat source
r153 ],
project: initial commit
r0 }
client.make_request(data=report_data)
def notify_uptime_alert(self, **kwargs):
"""
Build and send uptime alert notification
Kwargs:
application: application that the event applies for,
event: event that is notified,
user: user that should be notified
request: request object
"""
template_vars = self.uptime_alert_notification_vars(kwargs)
black: reformat source
r153 title = "*ALERT %s*: %s" % (
template_vars["alert_action"],
template_vars["resource_name"],
project: initial commit
r0 )
black: reformat source
r153 client = SlackIntegration.create_client(self.integration.config["webhook_url"])
project: initial commit
r0 report_data = {
refactor: fix inconsistent naming
r28 "username": "AppEnlight",
project: initial commit
r0 "icon_emoji": ":rage:",
"attachments": [
{
"mrkdwn_in": ["text", "pretext", "title", "fallback"],
"fallback": "{} - <{}| Browse>".format(
black: reformat source
r153 title, template_vars["destination_url"]
),
project: initial commit
r0 "pretext": "{} - <{}| Browse>".format(
black: reformat source
r153 title, template_vars["destination_url"]
),
project: initial commit
r0 "color": "danger",
"fields": [
{
"title": "Application has uptime issues",
black: reformat source
r153 "value": template_vars["reason"],
"short": False,
project: initial commit
r0 }
black: reformat source
r153 ],
project: initial commit
r0 }
black: reformat source
r153 ],
project: initial commit
r0 }
client.make_request(data=report_data)
def notify_chart_alert(self, **kwargs):
"""
Build and send chart alert notification
Kwargs:
application: application that the event applies for,
event: event that is notified,
user: user that should be notified
request: request object
"""
template_vars = self.chart_alert_notification_vars(kwargs)
black: reformat source
r153 title = '*ALERT {}*: value in *"{}"* chart ' 'met alert *"{}"* criteria'.format(
template_vars["alert_action"],
template_vars["chart_name"],
template_vars["action_name"],
project: initial commit
r0 )
black: reformat source
r153 subtext = ""
for item in template_vars["readable_values"]:
subtext += "{} - {}\n".format(item["label"], item["value"])
project: initial commit
r0
black: reformat source
r153 client = SlackIntegration.create_client(self.integration.config["webhook_url"])
project: initial commit
r0 report_data = {
refactor: fix inconsistent naming
r28 "username": "AppEnlight",
project: initial commit
r0 "icon_emoji": ":rage:",
"attachments": [
black: reformat source
r153 {
"mrkdwn_in": ["text", "pretext", "title", "fallback"],
"fallback": "{} - <{}| Browse>".format(
title, template_vars["destination_url"]
),
"pretext": "{} - <{}| Browse>".format(
title, template_vars["destination_url"]
),
"color": "danger",
"fields": [
{
"title": "Following criteria were met:",
"value": subtext,
"short": False,
}
],
}
],
project: initial commit
r0 }
client.make_request(data=report_data)
def send_digest(self, **kwargs):
"""
Build and send daily digest notification
kwargs:
application: application that the event applies for,
user: user that should be notified
request: request object
since_when: reports are newer than this time value,
reports: list of reports to render
"""
template_vars = self.report_alert_notification_vars(kwargs)
black: reformat source
r153 title = "*Daily report digest*: %s" % template_vars["resource_name"]
project: initial commit
r0
black: reformat source
r153 subtext = "%s reports" % template_vars["confirmed_total"]
project: initial commit
r0
black: reformat source
r153 client = SlackIntegration.create_client(self.integration.config["webhook_url"])
project: initial commit
r0 report_data = {
refactor: fix inconsistent naming
r28 "username": "AppEnlight",
project: initial commit
r0 "attachments": [
{
"mrkdwn_in": ["text", "pretext", "title", "fallback"],
black: reformat source
r153 "fallback": "%s : <%s| Browse>"
% (title, template_vars["destination_url"]),
"pretext": "%s: <%s| Browse>"
% (title, template_vars["destination_url"]),
project: initial commit
r0 "color": "good",
black: reformat source
r153 "fields": [{"title": "Got at least: %s" % subtext, "short": False}],
project: initial commit
r0 }
black: reformat source
r153 ],
project: initial commit
r0 }
client.make_request(data=report_data)
black: reformat source
r153 log_msg = "DIGEST : %s via %s :: %s reports" % (
kwargs["user"].user_name,
project: initial commit
r0 self.channel_visible_value,
black: reformat source
r153 template_vars["confirmed_total"],
)
project: initial commit
r0 log.warning(log_msg)