2016-07-27 05:09:55 -05:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
2017-09-30 18:26:19 -05:00
|
|
|
"bytes"
|
2016-07-28 10:03:53 -05:00
|
|
|
"encoding/json"
|
2020-02-11 14:43:28 -06:00
|
|
|
"fmt"
|
2017-09-30 18:26:19 -05:00
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
2020-06-26 13:47:06 -05:00
|
|
|
"net/http"
|
2017-09-30 18:26:19 -05:00
|
|
|
"os"
|
2017-11-29 04:16:45 -06:00
|
|
|
"path/filepath"
|
2020-02-14 10:11:23 -06:00
|
|
|
"regexp"
|
2020-02-11 14:43:28 -06:00
|
|
|
"strings"
|
2016-07-28 10:03:53 -05:00
|
|
|
"time"
|
2016-07-27 05:09:55 -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"
|
2016-07-27 05:09:55 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2016-08-12 03:12:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-07-27 05:09:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-01-06 05:04:25 -06:00
|
|
|
alerting.RegisterNotifier(&alerting.NotifierPlugin{
|
|
|
|
Type: "slack",
|
|
|
|
Name: "Slack",
|
2017-09-30 18:26:19 -05:00
|
|
|
Description: "Sends notifications to Slack via Slack Webhooks",
|
2020-06-29 06:39:12 -05:00
|
|
|
Heading: "Slack settings",
|
2017-01-06 05:04:25 -06:00
|
|
|
Factory: NewSlackNotifier,
|
|
|
|
OptionsTemplate: `
|
|
|
|
<h3 class="page-heading">Slack settings</h3>
|
|
|
|
<div class="gf-form max-width-30">
|
2020-02-11 14:43:28 -06:00
|
|
|
<span class="gf-form-label width-8">Url</span>
|
2020-07-08 03:17:05 -05:00
|
|
|
<div class="gf-form gf-form--grow" ng-if="!ctrl.model.secureFields.url">
|
|
|
|
<input type="text"
|
|
|
|
required
|
|
|
|
class="gf-form-input max-width-30"
|
|
|
|
ng-init="ctrl.model.secureSettings.url = ctrl.model.settings.url || null; ctrl.model.settings.url = null;"
|
|
|
|
ng-model="ctrl.model.secureSettings.url"
|
|
|
|
placeholder="Slack incoming webhook url">
|
|
|
|
</input>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form" ng-if="ctrl.model.secureFields.url">
|
|
|
|
<input type="text" class="gf-form-input max-width-18" disabled="disabled" value="configured" />
|
|
|
|
<a class="btn btn-secondary gf-form-btn" href="#" ng-click="ctrl.model.secureFields.url = false">reset</a>
|
|
|
|
</div>
|
2017-01-06 05:04:25 -06:00
|
|
|
</div>
|
|
|
|
<div class="gf-form max-width-30">
|
2020-02-11 14:43:28 -06:00
|
|
|
<span class="gf-form-label width-8">Recipient</span>
|
2017-01-06 05:04:25 -06:00
|
|
|
<input type="text"
|
|
|
|
class="gf-form-input max-width-30"
|
|
|
|
ng-model="ctrl.model.settings.recipient"
|
|
|
|
data-placement="right">
|
|
|
|
</input>
|
|
|
|
<info-popover mode="right-absolute">
|
2020-02-14 10:11:23 -06:00
|
|
|
Override default channel or user, use #channel-name, @username (has to be all lowercase, no whitespace), or user/channel Slack ID
|
2017-01-06 05:04:25 -06:00
|
|
|
</info-popover>
|
|
|
|
</div>
|
2018-11-07 20:57:58 -06:00
|
|
|
<div class="gf-form max-width-30">
|
2020-02-11 14:43:28 -06:00
|
|
|
<span class="gf-form-label width-8">Username</span>
|
2018-11-07 20:57:58 -06:00
|
|
|
<input type="text"
|
|
|
|
class="gf-form-input max-width-30"
|
|
|
|
ng-model="ctrl.model.settings.username"
|
|
|
|
data-placement="right">
|
|
|
|
</input>
|
|
|
|
<info-popover mode="right-absolute">
|
|
|
|
Set the username for the bot's message
|
|
|
|
</info-popover>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form max-width-30">
|
2020-02-11 14:43:28 -06:00
|
|
|
<span class="gf-form-label width-8">Icon emoji</span>
|
2018-11-07 20:57:58 -06:00
|
|
|
<input type="text"
|
|
|
|
class="gf-form-input max-width-30"
|
|
|
|
ng-model="ctrl.model.settings.icon_emoji"
|
|
|
|
data-placement="right">
|
|
|
|
</input>
|
|
|
|
<info-popover mode="right-absolute">
|
|
|
|
Provide an emoji to use as the icon for the bot's message. Overrides the icon URL
|
|
|
|
</info-popover>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form max-width-30">
|
2020-02-11 14:43:28 -06:00
|
|
|
<span class="gf-form-label width-8">Icon URL</span>
|
2018-11-07 20:57:58 -06:00
|
|
|
<input type="text"
|
|
|
|
class="gf-form-input max-width-30"
|
|
|
|
ng-model="ctrl.model.settings.icon_url"
|
|
|
|
data-placement="right">
|
|
|
|
</input>
|
|
|
|
<info-popover mode="right-absolute">
|
|
|
|
Provide a URL to an image to use as the icon for the bot's message
|
|
|
|
</info-popover>
|
|
|
|
</div>
|
2017-01-06 05:04:25 -06:00
|
|
|
<div class="gf-form max-width-30">
|
2020-02-11 14:43:28 -06:00
|
|
|
<span class="gf-form-label width-8">Mention Users</span>
|
2017-01-06 05:04:25 -06:00
|
|
|
<input type="text"
|
|
|
|
class="gf-form-input max-width-30"
|
2020-02-11 14:43:28 -06:00
|
|
|
ng-model="ctrl.model.settings.mentionUsers"
|
2017-01-06 05:04:25 -06:00
|
|
|
data-placement="right">
|
|
|
|
</input>
|
|
|
|
<info-popover mode="right-absolute">
|
2020-02-11 14:43:28 -06:00
|
|
|
Mention one or more users (comma separated) when notifying in a channel, by ID (you can copy this from the user's Slack profile)
|
2017-01-06 05:04:25 -06:00
|
|
|
</info-popover>
|
|
|
|
</div>
|
2017-09-30 18:26:19 -05:00
|
|
|
<div class="gf-form max-width-30">
|
2020-02-11 14:43:28 -06:00
|
|
|
<span class="gf-form-label width-8">Mention Groups</span>
|
|
|
|
<input type="text"
|
|
|
|
class="gf-form-input max-width-30"
|
|
|
|
ng-model="ctrl.model.settings.mentionGroups"
|
|
|
|
data-placement="right">
|
|
|
|
</input>
|
|
|
|
<info-popover mode="right-absolute">
|
|
|
|
Mention one or more groups (comma separated) when notifying in a channel (you can copy this from the group's Slack profile URL)
|
|
|
|
</info-popover>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form max-width-30">
|
|
|
|
<span class="gf-form-label width-8">Mention Channel</span>
|
|
|
|
<select
|
|
|
|
class="gf-form-input max-width-30"
|
|
|
|
ng-model="ctrl.model.settings.mentionChannel"
|
|
|
|
data-placement="right">
|
|
|
|
<option value="">Disabled</option>
|
|
|
|
<option value="here">Every active channel member</option>
|
|
|
|
<option value="channel">Every channel member</option>
|
|
|
|
</select>
|
|
|
|
<info-popover mode="right-absolute">
|
|
|
|
Mention whole channel or just active members when notifying
|
|
|
|
</info-popover>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form max-width-30">
|
2020-07-08 03:17:05 -05:00
|
|
|
<div class="gf-form gf-form--v-stretch"><label class="gf-form-label width-8">Token</label></div>
|
|
|
|
<div class="gf-form gf-form--grow" ng-if="!ctrl.model.secureFields.token">
|
|
|
|
<input type="text"
|
|
|
|
class="gf-form-input max-width-30"
|
|
|
|
ng-init="ctrl.model.secureSettings.token = ctrl.model.settings.token || null; ctrl.model.settings.token = null;"
|
|
|
|
ng-model="ctrl.model.secureSettings.token"
|
|
|
|
data-placement="right">
|
|
|
|
</input>
|
|
|
|
<info-popover mode="right-absolute">
|
|
|
|
Provide a bot token to use the Slack file.upload API (starts with "xoxb"). Specify Recipient for this to work
|
|
|
|
</info-popover>
|
|
|
|
</div>
|
|
|
|
<div class="gf-form" ng-if="ctrl.model.secureFields.token">
|
|
|
|
<input type="text" class="gf-form-input max-width-18" disabled="disabled" value="configured" />
|
|
|
|
<a class="btn btn-secondary gf-form-btn" href="#" ng-click="ctrl.model.secureFields.token = false">reset</a>
|
|
|
|
</div>
|
2017-09-30 18:26:19 -05:00
|
|
|
</div>
|
2017-01-06 05:04:25 -06:00
|
|
|
`,
|
2020-06-29 06:39:12 -05:00
|
|
|
Options: []alerting.NotifierOption{
|
|
|
|
{
|
|
|
|
Label: "Url",
|
|
|
|
Element: alerting.ElementTypeInput,
|
|
|
|
InputType: alerting.InputTypeText,
|
|
|
|
Placeholder: "Slack incoming webhook url",
|
|
|
|
PropertyName: "url",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Label: "Recipient",
|
|
|
|
Element: alerting.ElementTypeInput,
|
|
|
|
InputType: alerting.InputTypeText,
|
|
|
|
Description: "Override default channel or user, use #channel-name, @username (has to be all lowercase, no whitespace), or user/channel Slack ID",
|
|
|
|
PropertyName: "recipient",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Label: "Username",
|
|
|
|
Element: alerting.ElementTypeInput,
|
|
|
|
InputType: alerting.InputTypeText,
|
|
|
|
Description: "Set the username for the bot's message",
|
|
|
|
PropertyName: "username",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Label: "Icon emoji",
|
|
|
|
Element: alerting.ElementTypeInput,
|
|
|
|
InputType: alerting.InputTypeText,
|
|
|
|
Description: "Provide an emoji to use as the icon for the bot's message. Overrides the icon URL.",
|
|
|
|
PropertyName: "icon_emoji",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Label: "Icon URL",
|
|
|
|
Element: alerting.ElementTypeInput,
|
|
|
|
InputType: alerting.InputTypeText,
|
|
|
|
Description: "Provide a URL to an image to use as the icon for the bot's message",
|
|
|
|
PropertyName: "icon_url",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Label: "Mention Users",
|
|
|
|
Element: alerting.ElementTypeInput,
|
|
|
|
InputType: alerting.InputTypeText,
|
|
|
|
Description: "Mention one or more users (comma separated) when notifying in a channel, by ID (you can copy this from the user's Slack profile)",
|
|
|
|
PropertyName: "mentionUsers",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Label: "Mention Groups",
|
|
|
|
Element: alerting.ElementTypeInput,
|
|
|
|
InputType: alerting.InputTypeText,
|
|
|
|
Description: "Mention one or more groups (comma separated) when notifying in a channel (you can copy this from the group's Slack profile URL)",
|
|
|
|
PropertyName: "mentionGroups",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Label: "Mention Channel",
|
|
|
|
Element: alerting.ElementTypeSelect,
|
|
|
|
SelectOptions: []alerting.SelectOption{
|
|
|
|
{
|
|
|
|
Value: "",
|
|
|
|
Label: "Disabled",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "here",
|
|
|
|
Label: "Every active channel member",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "channel",
|
|
|
|
Label: "Every channel member",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Description: "Mention whole channel or just active members when notifying",
|
|
|
|
PropertyName: "mentionChannel",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Label: "Token",
|
|
|
|
Element: alerting.ElementTypeInput,
|
|
|
|
InputType: alerting.InputTypeText,
|
|
|
|
Description: "Provide a bot token to use the Slack file.upload API (starts with \"xoxb\"). Specify Recipient for this to work",
|
|
|
|
PropertyName: "token",
|
|
|
|
},
|
|
|
|
},
|
2017-01-06 05:04:25 -06:00
|
|
|
})
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2020-02-14 10:11:23 -06:00
|
|
|
var reRecipient *regexp.Regexp = regexp.MustCompile("^((@[a-z0-9][a-zA-Z0-9._-]*)|(#[^ .A-Z]{1,79})|([a-zA-Z0-9]+))$")
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// NewSlackNotifier is the constructor for the Slack notifier
|
2019-05-14 01:15:05 -05:00
|
|
|
func NewSlackNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
|
2020-07-08 03:17:05 -05:00
|
|
|
url := model.DecryptedValue("url", model.Settings.Get("url").MustString())
|
2016-07-27 05:09:55 -05:00
|
|
|
if url == "" {
|
2016-07-27 09:29:28 -05:00
|
|
|
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2020-02-14 10:11:23 -06:00
|
|
|
recipient := strings.TrimSpace(model.Settings.Get("recipient").MustString())
|
|
|
|
if recipient != "" && !reRecipient.MatchString(recipient) {
|
|
|
|
return nil, alerting.ValidationError{Reason: fmt.Sprintf("Recipient on invalid format: %q", recipient)}
|
|
|
|
}
|
2018-11-07 20:57:58 -06:00
|
|
|
username := model.Settings.Get("username").MustString()
|
|
|
|
iconEmoji := model.Settings.Get("icon_emoji").MustString()
|
2019-05-20 08:23:06 -05:00
|
|
|
iconURL := model.Settings.Get("icon_url").MustString()
|
2020-02-11 14:43:28 -06:00
|
|
|
mentionUsersStr := model.Settings.Get("mentionUsers").MustString()
|
|
|
|
mentionGroupsStr := model.Settings.Get("mentionGroups").MustString()
|
|
|
|
mentionChannel := model.Settings.Get("mentionChannel").MustString()
|
2020-07-08 03:17:05 -05:00
|
|
|
token := model.DecryptedValue("token", model.Settings.Get("token").MustString())
|
|
|
|
|
2017-10-02 22:18:48 -05:00
|
|
|
uploadImage := model.Settings.Get("uploadImage").MustBool(true)
|
2016-10-31 16:46:51 -05:00
|
|
|
|
2020-02-11 14:43:28 -06:00
|
|
|
if mentionChannel != "" && mentionChannel != "here" && mentionChannel != "channel" {
|
2020-02-14 10:11:23 -06:00
|
|
|
return nil, alerting.ValidationError{
|
|
|
|
Reason: fmt.Sprintf("Invalid value for mentionChannel: %q", mentionChannel),
|
|
|
|
}
|
2020-02-11 14:43:28 -06:00
|
|
|
}
|
|
|
|
mentionUsers := []string{}
|
|
|
|
for _, u := range strings.Split(mentionUsersStr, ",") {
|
|
|
|
u = strings.TrimSpace(u)
|
|
|
|
if u != "" {
|
|
|
|
mentionUsers = append(mentionUsers, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mentionGroups := []string{}
|
|
|
|
for _, g := range strings.Split(mentionGroupsStr, ",") {
|
|
|
|
g = strings.TrimSpace(g)
|
|
|
|
if g != "" {
|
|
|
|
mentionGroups = append(mentionGroups, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
return &SlackNotifier{
|
2020-02-11 14:43:28 -06:00
|
|
|
NotifierBase: NewNotifierBase(model),
|
|
|
|
URL: url,
|
|
|
|
Recipient: recipient,
|
|
|
|
Username: username,
|
|
|
|
IconEmoji: iconEmoji,
|
|
|
|
IconURL: iconURL,
|
|
|
|
MentionUsers: mentionUsers,
|
|
|
|
MentionGroups: mentionGroups,
|
|
|
|
MentionChannel: mentionChannel,
|
|
|
|
Token: token,
|
|
|
|
Upload: uploadImage,
|
|
|
|
log: log.New("alerting.notifier.slack"),
|
2016-07-27 05:09:55 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// SlackNotifier is responsible for sending
|
|
|
|
// alert notification to Slack.
|
2016-07-27 05:09:55 -05:00
|
|
|
type SlackNotifier struct {
|
|
|
|
NotifierBase
|
2020-02-11 14:43:28 -06:00
|
|
|
URL string
|
|
|
|
Recipient string
|
|
|
|
Username string
|
|
|
|
IconEmoji string
|
|
|
|
IconURL string
|
|
|
|
MentionUsers []string
|
|
|
|
MentionGroups []string
|
|
|
|
MentionChannel string
|
|
|
|
Token string
|
|
|
|
Upload bool
|
|
|
|
log log.Logger
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// Notify send alert notification to Slack.
|
|
|
|
func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
|
2019-06-03 03:25:58 -05:00
|
|
|
sn.log.Info("Executing slack notification", "ruleId", evalContext.Rule.ID, "notification", sn.Name)
|
2016-07-27 05:09:55 -05:00
|
|
|
|
2019-06-03 03:25:58 -05:00
|
|
|
ruleURL, err := evalContext.GetRuleURL()
|
2016-07-27 05:09:55 -05:00
|
|
|
if err != nil {
|
2019-05-20 08:23:06 -05:00
|
|
|
sn.log.Error("Failed get rule link", "error", err)
|
2016-10-03 02:38:03 -05:00
|
|
|
return err
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2016-07-28 10:03:53 -05:00
|
|
|
fields := make([]map[string]interface{}, 0)
|
2016-07-29 06:41:42 -05:00
|
|
|
fieldLimitCount := 4
|
2016-10-03 02:38:03 -05:00
|
|
|
for index, evt := range evalContext.EvalMatches {
|
2016-07-28 10:03:53 -05:00
|
|
|
fields = append(fields, map[string]interface{}{
|
|
|
|
"title": evt.Metric,
|
|
|
|
"value": evt.Value,
|
2016-07-29 06:41:42 -05:00
|
|
|
"short": true,
|
2016-07-28 10:03:53 -05:00
|
|
|
})
|
2016-07-29 06:41:42 -05:00
|
|
|
if index > fieldLimitCount {
|
|
|
|
break
|
|
|
|
}
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
if evalContext.Error != nil {
|
2016-08-31 08:55:01 -05:00
|
|
|
fields = append(fields, map[string]interface{}{
|
|
|
|
"title": "Error message",
|
2016-10-03 02:38:03 -05:00
|
|
|
"value": evalContext.Error.Error(),
|
2016-08-31 08:55:01 -05:00
|
|
|
"short": false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:43:28 -06:00
|
|
|
mentionsBuilder := strings.Builder{}
|
|
|
|
appendSpace := func() {
|
|
|
|
if mentionsBuilder.Len() > 0 {
|
|
|
|
mentionsBuilder.WriteString(" ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mentionChannel := strings.TrimSpace(sn.MentionChannel)
|
|
|
|
if mentionChannel != "" {
|
|
|
|
mentionsBuilder.WriteString(fmt.Sprintf("<!%s|%s>", mentionChannel, mentionChannel))
|
|
|
|
}
|
|
|
|
if len(sn.MentionGroups) > 0 {
|
|
|
|
appendSpace()
|
|
|
|
for _, g := range sn.MentionGroups {
|
|
|
|
mentionsBuilder.WriteString(fmt.Sprintf("<!subteam^%s>", g))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(sn.MentionUsers) > 0 {
|
|
|
|
appendSpace()
|
|
|
|
for _, u := range sn.MentionUsers {
|
|
|
|
mentionsBuilder.WriteString(fmt.Sprintf("<@%s>", u))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg := ""
|
2019-05-14 01:15:05 -05:00
|
|
|
if evalContext.Rule.State != models.AlertStateOK { //don't add message when going back to alert state ok.
|
2020-02-11 14:43:28 -06:00
|
|
|
msg = evalContext.Rule.Message
|
2016-08-31 08:55:01 -05:00
|
|
|
}
|
2019-05-20 08:23:06 -05:00
|
|
|
imageURL := ""
|
2017-09-30 18:26:19 -05:00
|
|
|
// default to file.upload API method if a token is provided
|
2019-05-20 08:23:06 -05:00
|
|
|
if sn.Token == "" {
|
2019-06-03 03:25:58 -05:00
|
|
|
imageURL = evalContext.ImagePublicURL
|
2017-09-30 18:26:19 -05:00
|
|
|
}
|
2016-08-31 08:55:01 -05:00
|
|
|
|
2020-02-11 14:43:28 -06:00
|
|
|
var blocks []map[string]interface{}
|
|
|
|
if mentionsBuilder.Len() > 0 {
|
|
|
|
blocks = []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"type": "section",
|
|
|
|
"text": map[string]interface{}{
|
|
|
|
"type": "mrkdwn",
|
|
|
|
"text": mentionsBuilder.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 08:43:54 -06:00
|
|
|
attachment := map[string]interface{}{
|
|
|
|
"color": evalContext.GetStateModel().Color,
|
|
|
|
"title": evalContext.GetNotificationTitle(),
|
|
|
|
"title_link": ruleURL,
|
|
|
|
"text": msg,
|
|
|
|
"fallback": evalContext.GetNotificationTitle(),
|
|
|
|
"fields": fields,
|
|
|
|
"footer": "Grafana v" + setting.BuildVersion,
|
|
|
|
"footer_icon": "https://grafana.com/assets/img/fav32.png",
|
|
|
|
"ts": time.Now().Unix(),
|
|
|
|
}
|
2020-03-30 17:46:01 -05:00
|
|
|
if sn.NeedsImage() && imageURL != "" {
|
2020-02-21 08:43:54 -06:00
|
|
|
attachment["image_url"] = imageURL
|
|
|
|
}
|
2016-07-28 10:03:53 -05:00
|
|
|
body := map[string]interface{}{
|
2020-06-26 13:47:06 -05:00
|
|
|
"text": evalContext.GetNotificationTitle(),
|
2016-07-28 10:03:53 -05:00
|
|
|
"attachments": []map[string]interface{}{
|
2020-02-21 08:43:54 -06:00
|
|
|
attachment,
|
2016-07-28 10:03:53 -05:00
|
|
|
},
|
2016-10-31 16:55:21 -05:00
|
|
|
"parse": "full", // to linkify urls, users and channels in alert message.
|
2016-07-28 10:03:53 -05:00
|
|
|
}
|
2020-06-26 13:47:06 -05:00
|
|
|
if len(blocks) > 0 {
|
|
|
|
body["blocks"] = blocks
|
|
|
|
}
|
2016-07-27 05:09:55 -05:00
|
|
|
|
2016-10-31 16:46:51 -05:00
|
|
|
//recipient override
|
2019-05-20 08:23:06 -05:00
|
|
|
if sn.Recipient != "" {
|
|
|
|
body["channel"] = sn.Recipient
|
2016-10-31 16:46:51 -05:00
|
|
|
}
|
2019-05-20 08:23:06 -05:00
|
|
|
if sn.Username != "" {
|
|
|
|
body["username"] = sn.Username
|
2018-11-07 20:57:58 -06:00
|
|
|
}
|
2019-05-20 08:23:06 -05:00
|
|
|
if sn.IconEmoji != "" {
|
|
|
|
body["icon_emoji"] = sn.IconEmoji
|
2018-11-07 20:57:58 -06:00
|
|
|
}
|
2019-05-20 08:23:06 -05:00
|
|
|
if sn.IconURL != "" {
|
|
|
|
body["icon_url"] = sn.IconURL
|
2018-11-07 20:57:58 -06:00
|
|
|
}
|
2020-02-11 14:43:28 -06:00
|
|
|
data, err := json.Marshal(&body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-21 08:43:54 -06:00
|
|
|
|
2020-06-26 13:47:06 -05:00
|
|
|
cmd := &models.SendWebhookSync{
|
|
|
|
Url: sn.URL,
|
|
|
|
Body: string(data),
|
|
|
|
HttpMethod: http.MethodPost,
|
|
|
|
}
|
|
|
|
if sn.Token != "" {
|
|
|
|
sn.log.Debug("Adding authorization header to HTTP request")
|
|
|
|
cmd.HttpHeader = map[string]string{
|
|
|
|
"Authorization": fmt.Sprintf("Bearer %s", sn.Token),
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 10:03:21 -05:00
|
|
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
2019-05-20 08:23:06 -05:00
|
|
|
sn.log.Error("Failed to send slack notification", "error", err, "webhook", sn.Name)
|
2016-12-12 03:20:50 -06:00
|
|
|
return err
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
2019-05-20 08:23:06 -05:00
|
|
|
if sn.Token != "" && sn.UploadImage {
|
|
|
|
err = slackFileUpload(evalContext, sn.log, "https://slack.com/api/files.upload", sn.Recipient, sn.Token)
|
2017-09-30 18:26:19 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 19:45:22 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
func slackFileUpload(evalContext *alerting.EvalContext, log log.Logger, url string, recipient string, token string) error {
|
2017-10-02 19:45:22 -05:00
|
|
|
if evalContext.ImageOnDiskPath == "" {
|
2017-11-29 04:16:45 -06:00
|
|
|
evalContext.ImageOnDiskPath = filepath.Join(setting.HomePath, "public/img/mixed_styles.png")
|
2017-10-02 19:45:22 -05:00
|
|
|
}
|
|
|
|
log.Info("Uploading to slack via file.upload API")
|
2019-05-20 08:23:06 -05:00
|
|
|
headers, uploadBody, err := generateSlackBody(evalContext.ImageOnDiskPath, token, recipient)
|
2017-10-02 19:45:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-14 01:15:05 -05:00
|
|
|
cmd := &models.SendWebhookSync{Url: url, Body: uploadBody.String(), HttpHeader: headers, HttpMethod: "POST"}
|
2017-10-02 19:45:22 -05:00
|
|
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
|
|
|
log.Error("Failed to upload slack image", "error", err, "webhook", "file.upload")
|
|
|
|
return err
|
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
return nil
|
2016-07-27 05:09:55 -05:00
|
|
|
}
|
2017-09-30 18:26:19 -05:00
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
func generateSlackBody(file string, token string, recipient string) (map[string]string, bytes.Buffer, error) {
|
2017-09-30 18:26:19 -05:00
|
|
|
// Slack requires all POSTs to files.upload to present
|
|
|
|
// an "application/x-www-form-urlencoded" encoded querystring
|
|
|
|
// See https://api.slack.com/methods/files.upload
|
|
|
|
var b bytes.Buffer
|
|
|
|
w := multipart.NewWriter(&b)
|
|
|
|
// Add the generated image file
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, b, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
fw, err := w.CreateFormFile("file", file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, b, err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(fw, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, b, err
|
|
|
|
}
|
|
|
|
// Add the authorization token
|
|
|
|
err = w.WriteField("token", token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, b, err
|
|
|
|
}
|
|
|
|
// Add the channel(s) to POST to
|
|
|
|
err = w.WriteField("channels", recipient)
|
|
|
|
if err != nil {
|
|
|
|
return nil, b, err
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
headers := map[string]string{
|
|
|
|
"Content-Type": w.FormDataContentType(),
|
|
|
|
"Authorization": "auth_token=\"" + token + "\"",
|
|
|
|
}
|
|
|
|
return headers, b, nil
|
|
|
|
}
|