2018-03-28 04:56:54 -05:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-12-07 07:17:09 -06:00
|
|
|
"time"
|
|
|
|
|
2018-03-28 04:56:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-05-14 01:15:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-03-28 04:56:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
alerting.RegisterNotifier(&alerting.NotifierPlugin{
|
|
|
|
Type: "googlechat",
|
|
|
|
Name: "Google Hangouts Chat",
|
|
|
|
Description: "Sends notifications to Google Hangouts Chat via webhooks based on the official JSON message " +
|
|
|
|
"format (https://developers.google.com/hangouts/chat/reference/message-formats/).",
|
2019-05-20 08:23:06 -05:00
|
|
|
Factory: newGoogleChatNotifier,
|
2018-03-28 04:56:54 -05:00
|
|
|
OptionsTemplate: `
|
|
|
|
<h3 class="page-heading">Google Hangouts Chat settings</h3>
|
|
|
|
<div class="gf-form max-width-30">
|
|
|
|
<span class="gf-form-label width-6">Url</span>
|
|
|
|
<input type="text" required class="gf-form-input max-width-30" ng-model="ctrl.model.settings.url" placeholder="Google Hangouts Chat incoming webhook url"></input>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
func newGoogleChatNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
|
2018-03-28 04:56:54 -05:00
|
|
|
url := model.Settings.Get("url").MustString()
|
|
|
|
if url == "" {
|
|
|
|
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &GoogleChatNotifier{
|
2018-12-07 07:17:09 -06:00
|
|
|
NotifierBase: NewNotifierBase(model),
|
2019-05-20 08:23:06 -05:00
|
|
|
URL: url,
|
2018-03-28 04:56:54 -05:00
|
|
|
log: log.New("alerting.notifier.googlechat"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// GoogleChatNotifier is responsible for sending
|
|
|
|
// alert notifications to Google chat.
|
2018-03-28 04:56:54 -05:00
|
|
|
type GoogleChatNotifier struct {
|
|
|
|
NotifierBase
|
2019-05-20 08:23:06 -05:00
|
|
|
URL string
|
2018-12-07 07:39:44 -06:00
|
|
|
log log.Logger
|
2018-03-28 04:56:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Structs used to build a custom Google Hangouts Chat message card.
|
|
|
|
See: https://developers.google.com/hangouts/chat/reference/message-formats/cards
|
|
|
|
*/
|
|
|
|
type outerStruct struct {
|
2020-01-13 13:42:52 -06:00
|
|
|
FallbackText string `json:"fallbackText"`
|
|
|
|
Cards []card `json:"cards"`
|
2018-03-28 04:56:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type card struct {
|
|
|
|
Header header `json:"header"`
|
|
|
|
Sections []section `json:"sections"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type header struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type section struct {
|
|
|
|
Widgets []widget `json:"widgets"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// "generic" widget used to add different types of widgets (buttonWidget, textParagraphWidget, imageWidget)
|
|
|
|
type widget interface {
|
|
|
|
}
|
|
|
|
|
|
|
|
type buttonWidget struct {
|
|
|
|
Buttons []button `json:"buttons"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type textParagraphWidget struct {
|
|
|
|
Text text `json:"textParagraph"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type text struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageWidget struct {
|
|
|
|
Image image `json:"image"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type image struct {
|
2019-05-20 08:23:06 -05:00
|
|
|
ImageURL string `json:"imageUrl"`
|
2018-03-28 04:56:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type button struct {
|
|
|
|
TextButton textButton `json:"textButton"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type textButton struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
OnClick onClick `json:"onClick"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type onClick struct {
|
|
|
|
OpenLink openLink `json:"openLink"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type openLink struct {
|
2019-05-20 08:23:06 -05:00
|
|
|
URL string `json:"url"`
|
2018-03-28 04:56:54 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// Notify send an alert notification to Google Chat.
|
|
|
|
func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
|
|
|
|
gcn.log.Info("Executing Google Chat notification")
|
2018-03-28 04:56:54 -05:00
|
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
"Content-Type": "application/json; charset=UTF-8",
|
|
|
|
}
|
|
|
|
|
2019-06-03 03:25:58 -05:00
|
|
|
ruleURL, err := evalContext.GetRuleURL()
|
2018-03-28 04:56:54 -05:00
|
|
|
if err != nil {
|
2019-05-20 08:23:06 -05:00
|
|
|
gcn.log.Error("evalContext returned an invalid rule URL")
|
2018-03-28 04:56:54 -05:00
|
|
|
}
|
|
|
|
|
2020-04-14 08:02:58 -05:00
|
|
|
widgets := []widget{}
|
|
|
|
if len(evalContext.Rule.Message) > 0 {
|
|
|
|
// add a text paragraph widget for the message if there is a message
|
|
|
|
// Google Chat API doesn't accept an empty text property
|
|
|
|
widgets = append(widgets, textParagraphWidget{
|
2018-03-28 04:56:54 -05:00
|
|
|
Text: text{
|
|
|
|
Text: evalContext.Rule.Message,
|
|
|
|
},
|
2020-04-14 08:02:58 -05:00
|
|
|
})
|
2018-03-28 04:56:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// add a text paragraph widget for the fields
|
|
|
|
var fields []textParagraphWidget
|
|
|
|
fieldLimitCount := 4
|
|
|
|
for index, evt := range evalContext.EvalMatches {
|
|
|
|
fields = append(fields,
|
|
|
|
textParagraphWidget{
|
|
|
|
Text: text{
|
|
|
|
Text: "<i>" + evt.Metric + ": " + fmt.Sprint(evt.Value) + "</i>",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if index > fieldLimitCount {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
widgets = append(widgets, fields)
|
|
|
|
|
2020-03-30 17:46:01 -05:00
|
|
|
if gcn.NeedsImage() {
|
|
|
|
// if an image exists, add it as an image widget
|
|
|
|
if evalContext.ImagePublicURL != "" {
|
|
|
|
widgets = append(widgets, imageWidget{
|
|
|
|
Image: image{
|
|
|
|
ImageURL: evalContext.ImagePublicURL,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
gcn.log.Info("Could not retrieve a public image URL.")
|
|
|
|
}
|
2018-03-28 04:56:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// add a button widget (link to Grafana)
|
|
|
|
widgets = append(widgets, buttonWidget{
|
|
|
|
Buttons: []button{
|
|
|
|
{
|
|
|
|
TextButton: textButton{
|
|
|
|
Text: "OPEN IN GRAFANA",
|
|
|
|
OnClick: onClick{
|
|
|
|
OpenLink: openLink{
|
2019-05-20 08:23:06 -05:00
|
|
|
URL: ruleURL,
|
2018-03-28 04:56:54 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// add text paragraph widget for the build version and timestamp
|
|
|
|
widgets = append(widgets, textParagraphWidget{
|
|
|
|
Text: text{
|
|
|
|
Text: "Grafana v" + setting.BuildVersion + " | " + (time.Now()).Format(time.RFC822),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// nest the required structs
|
|
|
|
res1D := &outerStruct{
|
2020-01-13 13:42:52 -06:00
|
|
|
FallbackText: evalContext.GetNotificationTitle(),
|
2018-03-28 04:56:54 -05:00
|
|
|
Cards: []card{
|
|
|
|
{
|
|
|
|
Header: header{
|
|
|
|
Title: evalContext.GetNotificationTitle(),
|
|
|
|
},
|
|
|
|
Sections: []section{
|
|
|
|
{
|
|
|
|
Widgets: widgets,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(res1D)
|
|
|
|
|
2019-05-14 01:15:05 -05:00
|
|
|
cmd := &models.SendWebhookSync{
|
2019-05-20 08:23:06 -05:00
|
|
|
Url: gcn.URL,
|
2018-03-28 04:56:54 -05:00
|
|
|
HttpMethod: "POST",
|
|
|
|
HttpHeader: headers,
|
|
|
|
Body: string(body),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
2019-05-20 08:23:06 -05:00
|
|
|
gcn.log.Error("Failed to send Google Hangouts Chat alert", "error", err, "webhook", gcn.Name)
|
2018-03-28 04:56:54 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|