mirror of
https://github.com/ansible/awx.git
synced 2026-05-16 22:07:36 -02:30
Add grafana notification type
This commit is contained in:
25
awx/main/migrations/0055_v340_add_grafana_notification.py
Normal file
25
awx/main/migrations/0055_v340_add_grafana_notification.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.16 on 2019-01-20 12:00
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('main', '0054_v340_workflow_convergence'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='notification',
|
||||
name='notification_type',
|
||||
field=models.CharField(choices=[('email', 'Email'), ('slack', 'Slack'), ('twilio', 'Twilio'), ('pagerduty', 'Pagerduty'), ('grafana', 'Grafana'), ('hipchat', 'HipChat'), ('webhook', 'Webhook'), ('mattermost', 'Mattermost'), ('rocketchat', 'Rocket.Chat'), ('irc', 'IRC')], max_length=32),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='notificationtemplate',
|
||||
name='notification_type',
|
||||
field=models.CharField(choices=[('email', 'Email'), ('slack', 'Slack'), ('twilio', 'Twilio'), ('pagerduty', 'Pagerduty'), ('grafana', 'Grafana'), ('hipchat', 'HipChat'), ('webhook', 'Webhook'), ('mattermost', 'Mattermost'), ('rocketchat', 'Rocket.Chat'), ('irc', 'IRC')], max_length=32),
|
||||
),
|
||||
]
|
||||
@@ -20,6 +20,7 @@ from awx.main.notifications.pagerduty_backend import PagerDutyBackend
|
||||
from awx.main.notifications.hipchat_backend import HipChatBackend
|
||||
from awx.main.notifications.webhook_backend import WebhookBackend
|
||||
from awx.main.notifications.mattermost_backend import MattermostBackend
|
||||
from awx.main.notifications.grafana_backend import GrafanaBackend
|
||||
from awx.main.notifications.rocketchat_backend import RocketChatBackend
|
||||
from awx.main.notifications.irc_backend import IrcBackend
|
||||
from awx.main.fields import JSONField
|
||||
@@ -36,6 +37,7 @@ class NotificationTemplate(CommonModelNameNotUnique):
|
||||
('slack', _('Slack'), SlackBackend),
|
||||
('twilio', _('Twilio'), TwilioBackend),
|
||||
('pagerduty', _('Pagerduty'), PagerDutyBackend),
|
||||
('grafana', _('Grafana'), GrafanaBackend),
|
||||
('hipchat', _('HipChat'), HipChatBackend),
|
||||
('webhook', _('Webhook'), WebhookBackend),
|
||||
('mattermost', _('Mattermost'), MattermostBackend),
|
||||
|
||||
66
awx/main/notifications/grafana_backend.py
Normal file
66
awx/main/notifications/grafana_backend.py
Normal file
@@ -0,0 +1,66 @@
|
||||
# Copyright (c) 2016 Ansible, Inc.
|
||||
# All Rights Reserved.
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
import requests
|
||||
import dateutil.parser as dp
|
||||
|
||||
from django.utils.encoding import smart_text
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from awx.main.notifications.base import AWXBaseEmailBackend
|
||||
|
||||
|
||||
logger = logging.getLogger('awx.main.notifications.grafana_backend')
|
||||
|
||||
|
||||
class GrafanaBackend(AWXBaseEmailBackend):
|
||||
|
||||
init_parameters = {"grafana_url": {"label": "Grafana URL", "type": "string"},
|
||||
"grafana_key": {"label": "Grafana API Key", "type": "password"}}
|
||||
recipient_parameter = "grafana_url"
|
||||
sender_parameter = None
|
||||
|
||||
def __init__(self, grafana_key,dashboardId=None, panelId=None, annotation_tags=None, grafana_no_verify_ssl=False, isRegion=True,
|
||||
fail_silently=False, **kwargs):
|
||||
super(GrafanaBackend, self).__init__(fail_silently=fail_silently)
|
||||
self.grafana_key = grafana_key
|
||||
self.dashboardId = dashboardId
|
||||
self.panelId = panelId
|
||||
self.annotation_tags = annotation_tags if annotation_tags is not None else []
|
||||
self.grafana_no_verify_ssl = grafana_no_verify_ssl
|
||||
self.isRegion = isRegion
|
||||
|
||||
def format_body(self, body):
|
||||
return body
|
||||
|
||||
def send_messages(self, messages):
|
||||
sent_messages = 0
|
||||
for m in messages:
|
||||
grafana_data = {}
|
||||
grafana_headers = {}
|
||||
try:
|
||||
epoch=datetime.datetime.utcfromtimestamp(0)
|
||||
grafana_data['time'] = int((dp.parse(m.body['started']).replace(tzinfo=None) - epoch).total_seconds() * 1000)
|
||||
grafana_data['timeEnd'] = int((dp.parse(m.body['finished']).replace(tzinfo=None) - epoch).total_seconds() * 1000)
|
||||
except ValueError:
|
||||
logger.error(smart_text(_("Error converting time {} or timeEnd {} to int.").format(m.body['started'],m.body['finished'])))
|
||||
if not self.fail_silently:
|
||||
raise Exception(smart_text(_("Error converting time {} and/or timeEnd {} to int.").format(m.body['started'],m.body['finished'])))
|
||||
grafana_data['isRegion'] = self.isRegion
|
||||
grafana_data['dashboardId'] = self.dashboardId
|
||||
grafana_data['panelId'] = self.panelId
|
||||
grafana_data['tags'] = self.annotation_tags
|
||||
grafana_data['text'] = m.subject
|
||||
grafana_headers['Authorization'] = "Bearer {}".format(self.grafana_key)
|
||||
grafana_headers['Content-Type'] = "application/json"
|
||||
r = requests.post("{}/api/annotations".format(m.recipients()[0]),
|
||||
json=grafana_data,
|
||||
headers=grafana_headers,
|
||||
verify=(not self.grafana_no_verify_ssl))
|
||||
if r.status_code >= 400:
|
||||
logger.error(smart_text(_("Error sending notification grafana: {}").format(r.text)))
|
||||
if not self.fail_silently:
|
||||
raise Exception(smart_text(_("Error sending notification grafana: {}").format(r.text)))
|
||||
sent_messages += 1
|
||||
return sent_messages
|
||||
@@ -186,7 +186,11 @@ export default ['Rest', 'Wait', 'NotificationsFormObject',
|
||||
if (field.type === 'textarea') {
|
||||
if (field.name === 'headers') {
|
||||
$scope[i] = JSON.parse($scope[i]);
|
||||
} else {
|
||||
}
|
||||
else if (field.name === 'annotation_tags' && $scope.notification_type.value === "grafana" && value === null) {
|
||||
$scope[i] = null;
|
||||
}
|
||||
else {
|
||||
$scope[i] = $scope[i].toString().split('\n');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,7 +256,11 @@ export default ['Rest', 'Wait',
|
||||
if (field.type === 'textarea') {
|
||||
if (field.name === 'headers') {
|
||||
$scope[i] = JSON.parse($scope[i]);
|
||||
} else {
|
||||
}
|
||||
else if (field.name === 'annotation_tags' && $scope.notification_type.value === "grafana" && value === null) {
|
||||
$scope[i] = null;
|
||||
}
|
||||
else {
|
||||
$scope[i] = $scope[i].toString().split('\n');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,6 +261,69 @@ export default ['i18n', function(i18n) {
|
||||
subForm: 'typeSubForm',
|
||||
ngDisabled: '!(notification_template.summary_fields.user_capabilities.edit || canAdd)'
|
||||
},
|
||||
grafana_url: {
|
||||
label: i18n._('Grafana URL'),
|
||||
type: 'text',
|
||||
awPopOver: i18n._('The base URL of the Grafana server - the /api/annotations endpoint will be added automatically to the base Grafana URL.'),
|
||||
placeholder: 'https://grafana.com',
|
||||
dataPlacement: 'right',
|
||||
dataContainer: "body",
|
||||
awRequiredWhen: {
|
||||
reqExpression: "grafana_required",
|
||||
init: "false"
|
||||
},
|
||||
ngShow: "notification_type.value == 'grafana' ",
|
||||
subForm: 'typeSubForm',
|
||||
ngDisabled: '!(notification_template.summary_fields.user_capabilities.edit || canAdd)'
|
||||
},
|
||||
grafana_key: {
|
||||
label: i18n._('Grafana API Key'),
|
||||
type: 'sensitive',
|
||||
hasShowInputButton: true,
|
||||
name: 'grafana_key',
|
||||
awRequiredWhen: {
|
||||
reqExpression: "grafana_required",
|
||||
init: "false"
|
||||
},
|
||||
ngShow: "notification_type.value == 'grafana' ",
|
||||
subForm: 'typeSubForm',
|
||||
ngDisabled: '!(notification_template.summary_fields.user_capabilities.edit || canAdd)'
|
||||
},
|
||||
dashboardId: {
|
||||
label: i18n._('ID of the Dashboard (optional)'),
|
||||
type: 'number',
|
||||
integer: true,
|
||||
ngShow: "notification_type.value == 'grafana' ",
|
||||
subForm: 'typeSubForm',
|
||||
ngDisabled: '!(notification_template.summary_fields.user_capabilities.edit || canAdd)'
|
||||
},
|
||||
panelId: {
|
||||
label: i18n._('ID of the Panel (optional)'),
|
||||
type: 'number',
|
||||
integer: true,
|
||||
ngShow: "notification_type.value == 'grafana' ",
|
||||
subForm: 'typeSubForm',
|
||||
ngDisabled: '!(notification_template.summary_fields.user_capabilities.edit || canAdd)'
|
||||
},
|
||||
annotation_tags: {
|
||||
label: i18n._('Tags for the Annotation (optional)'),
|
||||
dataTitle: i18n._('Tags for the Annotation'),
|
||||
type: 'textarea',
|
||||
name: 'annotation_tags',
|
||||
rows: 3,
|
||||
placeholder: 'ansible',
|
||||
awPopOver: i18n._('Enter one Annotation Tag per line, without commas.'),
|
||||
ngShow: "notification_type.value == 'grafana' ",
|
||||
subForm: 'typeSubForm',
|
||||
ngDisabled: '!(notification_template.summary_fields.user_capabilities.edit || canAdd)'
|
||||
},
|
||||
grafana_no_verify_ssl: {
|
||||
label: i18n._('Disable SSL Verification'),
|
||||
type: 'checkbox',
|
||||
ngShow: "notification_type.value == 'grafana' ",
|
||||
subForm: 'typeSubForm',
|
||||
ngDisabled: '!(notification_template.summary_fields.user_capabilities.edit || canAdd)'
|
||||
},
|
||||
api_url: {
|
||||
label: 'API URL',
|
||||
type: 'text',
|
||||
|
||||
@@ -12,6 +12,7 @@ function (i18n) {
|
||||
|
||||
obj.email_required = false;
|
||||
obj.slack_required = false;
|
||||
obj.grafana_required = false;
|
||||
obj.hipchat_required = false;
|
||||
obj.pagerduty_required = false;
|
||||
obj.irc_required = false;
|
||||
@@ -38,6 +39,9 @@ function (i18n) {
|
||||
obj.token_required = true;
|
||||
obj.channel_required = true;
|
||||
break;
|
||||
case 'grafana':
|
||||
obj.grafana_required = true;
|
||||
break;
|
||||
case 'hipchat':
|
||||
obj.tokenLabel = ' ' + i18n._('Token');
|
||||
obj.hipchat_required = true;
|
||||
|
||||
Reference in New Issue
Block a user