This repository has been archived by the owner on Jun 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
slack.go
71 lines (58 loc) · 2.03 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package slack
import (
"bytes"
"fmt"
"strconv"
"time"
"github.com/moira-alert/notifier"
"github.com/nlopes/slack"
)
var log notifier.Logger
// Sender implements moira sender interface via slack
type Sender struct {
APIToken string
FrontURI string
}
//Init read yaml config
func (sender *Sender) Init(senderSettings map[string]string, logger notifier.Logger) error {
sender.APIToken = senderSettings["api_token"]
if sender.APIToken == "" {
return fmt.Errorf("Can not read slack api_token from config")
}
log = logger
sender.FrontURI = senderSettings["front_uri"]
return nil
}
//SendEvents implements Sender interface Send
func (sender *Sender) SendEvents(events notifier.EventsData, contact notifier.ContactData, trigger notifier.TriggerData, throttled bool) error {
api := slack.New(sender.APIToken)
var message bytes.Buffer
state := events.GetSubjectState()
tags := trigger.GetTags()
message.WriteString(fmt.Sprintf("*%s* %s <%s/#/events/%s|%s>\n %s \n```", state, tags, sender.FrontURI, events[0].TriggerID, trigger.Name, trigger.Desc))
icon := fmt.Sprintf("%s/public/fav72_ok.png", sender.FrontURI)
for _, event := range events {
if event.State != "OK" {
icon = fmt.Sprintf("%s/public/fav72_error.png", sender.FrontURI)
}
value := strconv.FormatFloat(event.Value, 'f', -1, 64)
message.WriteString(fmt.Sprintf("\n%s: %s = %s (%s to %s)", time.Unix(event.Timestamp, 0).Format("15:04"), event.Metric, value, event.OldState, event.State))
if len(event.Message) > 0 {
message.WriteString(fmt.Sprintf(". %s", event.Message))
}
}
message.WriteString("```")
if throttled {
message.WriteString("\nPlease, *fix your system or tune this trigger* to generate less events.")
}
log.Debugf("Calling slack with message body %s", message.String())
params := slack.PostMessageParameters{
Username: "Moira",
IconURL: icon,
}
_, _, err := api.PostMessage(contact.Value, message.String(), params)
if err != nil {
return fmt.Errorf("Failed to send message to slack [%s]: %s", contact.Value, err.Error())
}
return nil
}