2017-07-04 08:16:32 -05:00
package notifiers
import (
2018-10-25 05:17:05 -05:00
"fmt"
2017-07-04 08:16:32 -05:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
)
2018-10-25 05:29:47 -05:00
const DefaultDingdingMsgType = "link"
2018-10-25 05:24:04 -05:00
const DingdingOptionsTemplate = `
2017-07-04 08:16:32 -05:00
< h3 class = "page-heading" > DingDing settings < / h3 >
< div class = "gf-form" >
< span class = "gf-form-label width-10" > Url < / span >
2017-07-10 09:51:03 -05:00
< input type = "text" required class = "gf-form-input max-width-26" ng - model = "ctrl.model.settings.url" placeholder = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx" > < / input >
2017-07-04 08:16:32 -05:00
< / div >
2018-10-25 05:29:47 -05:00
< div class = "gf-form" >
< span class = "gf-form-label width-10" > MessageType < / span >
< select class = "gf-form-input max-width-14" ng - model = "ctrl.model.settings.msgType" ng - options = "s for s in ['link','actionCard']" ng - init = "ctrl.model.settings.msgType=ctrl.model.settings.msgType || '` + DefaultDingdingMsgType + `'" > < / select >
< / div >
2018-10-25 05:24:04 -05:00
`
func init ( ) {
alerting . RegisterNotifier ( & alerting . NotifierPlugin {
Type : "dingding" ,
Name : "DingDing" ,
Description : "Sends HTTP POST request to DingDing" ,
Factory : NewDingDingNotifier ,
OptionsTemplate : DingdingOptionsTemplate ,
2017-07-04 08:16:32 -05:00
} )
}
func NewDingDingNotifier ( model * m . AlertNotification ) ( alerting . Notifier , error ) {
url := model . Settings . Get ( "url" ) . MustString ( )
if url == "" {
return nil , alerting . ValidationError { Reason : "Could not find url property in settings" }
}
2018-10-25 05:29:47 -05:00
msgType := model . Settings . Get ( "msgType" ) . MustString ( DefaultDingdingMsgType )
2017-07-04 08:16:32 -05:00
return & DingDingNotifier {
2018-06-05 03:27:29 -05:00
NotifierBase : NewNotifierBase ( model ) ,
2018-10-25 05:29:47 -05:00
MsgType : msgType ,
2017-07-04 08:16:32 -05:00
Url : url ,
log : log . New ( "alerting.notifier.dingding" ) ,
} , nil
}
type DingDingNotifier struct {
NotifierBase
2018-10-25 05:29:47 -05:00
MsgType string
Url string
log log . Logger
2017-07-04 08:16:32 -05:00
}
func ( this * DingDingNotifier ) Notify ( evalContext * alerting . EvalContext ) error {
this . log . Info ( "Sending dingding" )
messageUrl , err := evalContext . GetRuleUrl ( )
if err != nil {
this . log . Error ( "Failed to get messageUrl" , "error" , err , "dingding" , this . Name )
messageUrl = ""
}
this . log . Info ( "messageUrl:" + messageUrl )
message := evalContext . Rule . Message
picUrl := evalContext . ImagePublicUrl
title := evalContext . GetNotificationTitle ( )
2018-10-19 04:17:38 -05:00
if message == "" {
message = title
}
2017-07-04 08:16:32 -05:00
2018-10-25 05:17:05 -05:00
for i , match := range evalContext . EvalMatches {
2018-10-25 05:53:45 -05:00
message += fmt . Sprintf ( "\\n%2d. %s: %s" , i + 1 , match . Metric , match . Value )
2018-10-25 05:17:05 -05:00
}
2018-10-25 05:29:47 -05:00
var bodyStr string
if this . MsgType == "actionCard" {
bodyStr = ` {
"msgtype" : "actionCard" ,
"actionCard" : {
"text" : "` + message + `" ,
"title" : "` + title + `" ,
"singleTitle" : "More" ,
"singleURL" : "` + messageUrl + `"
}
} `
} else {
bodyStr = ` {
"msgtype" : "link" ,
"link" : {
"text" : "` + message + `" ,
"title" : "` + title + `" ,
"picUrl" : "` + picUrl + `" ,
"messageUrl" : "` + messageUrl + `"
}
} `
}
2018-10-25 05:24:04 -05:00
bodyJSON , err := simplejson . NewJson ( [ ] byte ( bodyStr ) )
2017-07-04 08:16:32 -05:00
if err != nil {
this . log . Error ( "Failed to create Json data" , "error" , err , "dingding" , this . Name )
}
2018-04-10 15:22:12 -05:00
body , err := bodyJSON . MarshalJSON ( )
if err != nil {
return err
}
2017-07-04 08:16:32 -05:00
cmd := & m . SendWebhookSync {
Url : this . Url ,
Body : string ( body ) ,
}
if err := bus . DispatchCtx ( evalContext . Ctx , cmd ) ; err != nil {
this . log . Error ( "Failed to send DingDing" , "error" , err , "dingding" , this . Name )
return err
}
return nil
}