Skip to content

Commit

Permalink
Merge pull request #51 from r3nic1e/templating
Browse files Browse the repository at this point in the history
Add message template support
  • Loading branch information
epels authored Sep 25, 2018
2 parents cb8c0d1 + d73859e commit 753711e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 22 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,38 @@ receivers:
- url: 'http://localhost:9876/alert'
```
## Message templating
Sachet supports Alertmanager-like templates for message content. You can do that by simply copying Alertmanager templates to Sachet. Some templates examples can be found in [the Alertmanager documentation](https://prometheus.io/docs/alerting/notification_examples/) as well as [available variables](https://prometheus.io/docs/alerting/notifications/).
sachet.yml:
```yaml
templates:
- /etc/sachet/notifications.tmpl

receivers:
- name: 'team-telegram'
provider: telegram
text: '{{ template "telegram_message" . }}'
```
notifications.tmpl:
```
{{ define "telegram_title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} @ {{ .CommonLabels.identifier }} {{ end }}

{{ define "telegram_message" }}
{{ if gt (len .Alerts.Firing) 0 }}
*Alerts Firing:*
{{ range .Alerts.Firing }}• {{ .Labels.instance }}: {{ .Annotations.description }}
{{ end }}{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
*Alerts Resolved:*
{{ range .Alerts.Resolved }}• {{ .Labels.instance }}: {{ .Annotations.description }}
{{ end }}{{ end }}{{ end }}

{{ define "telegram_text" }}{{ template "telegram_title" .}}
{{ template "telegram_message" . }}{{ end }}
```

## License

Sachet is licensed under [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause). Copyright (c) 2016, MessageBird
7 changes: 6 additions & 1 deletion cmd/sachet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/messagebird/sachet/provider/turbosms"
"github.com/messagebird/sachet/provider/twilio"

"github.com/prometheus/alertmanager/template"
"gopkg.in/yaml.v2"
)

Expand All @@ -25,6 +26,7 @@ type ReceiverConf struct {
Provider string
To []string
From string
Text string
}

var config struct {
Expand All @@ -45,7 +47,9 @@ var config struct {
}

Receivers []ReceiverConf
Templates []string
}
var tmpl *template.Template

// LoadConfig loads the specified YAML configuration file.
func LoadConfig(filename string) error {
Expand All @@ -59,5 +63,6 @@ func LoadConfig(filename string) error {
return err
}

return nil
tmpl, err = template.FromGlobs(config.Templates...)
return err
}
50 changes: 29 additions & 21 deletions cmd/sachet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,40 @@ func main() {
}

var text string
if len(data.Alerts) > 1 {
labelAlerts := map[string]template.Alerts{
"Firing": data.Alerts.Firing(),
"Resolved": data.Alerts.Resolved(),
if receiverConf.Text != "" {
text, err = tmpl.ExecuteTextString(receiverConf.Text, data)
if err != nil {
errorHandler(w, http.StatusInternalServerError, err, receiverConf.Provider)
return
}
for label, alerts := range labelAlerts {
if len(alerts) > 0 {
text += label + ": \n"
for _, alert := range alerts {
text += alert.Labels["alertname"] + " @" + alert.Labels["instance"]
if len(alert.Labels["exported_instance"]) > 0 {
text += " (" + alert.Labels["exported_instance"] + ")"
} else {
if len(data.Alerts) > 1 {
labelAlerts := map[string]template.Alerts{
"Firing": data.Alerts.Firing(),
"Resolved": data.Alerts.Resolved(),
}
for label, alerts := range labelAlerts {
if len(alerts) > 0 {
text += label + ": \n"
for _, alert := range alerts {
text += alert.Labels["alertname"] + " @" + alert.Labels["instance"]
if len(alert.Labels["exported_instance"]) > 0 {
text += " (" + alert.Labels["exported_instance"] + ")"
}
text += "\n"
}
text += "\n"
}
}
} else if len(data.Alerts) == 1 {
alert := data.Alerts[0]
tuples := []string{}
for k, v := range alert.Labels {
tuples = append(tuples, k+"= "+v)
}
text = strings.ToUpper(data.Status) + " \n" + strings.Join(tuples, "\n")
} else {
text = "Alert \n" + strings.Join(data.CommonLabels.Values(), " | ")
}
} else if len(data.Alerts) == 1 {
alert := data.Alerts[0]
tuples := []string{}
for k, v := range alert.Labels {
tuples = append(tuples, k+"= "+v)
}
text = strings.ToUpper(data.Status) + " \n" + strings.Join(tuples, "\n")
} else {
text = "Alert \n" + strings.Join(data.CommonLabels.Values(), " | ")
}

message := sachet.Message{
Expand Down
4 changes: 4 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ providers:
username: 'username'
password: 'password'

templates:
- telegram.tmpl

receivers:
- name: 'team-sms'
provider: 'messagebird'
Expand All @@ -51,4 +54,5 @@ receivers:
provider: 'telegram'
to:
- '164451814' # the chat id of a user. Get yours at https://telegram.me/userinfobot
text: '{{ .GroupLabels.alertname }} @ {{ .Labels.instance }}: {{ .Status | toUpper }}'

14 changes: 14 additions & 0 deletions examples/telegram.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{ define "telegram_title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} @ {{ .CommonLabels.identifier }} {{ end }}

{{ define "telegram_message" }}
{{ if gt (len .Alerts.Firing) 0 }}
*Alerts Firing:*
{{ range .Alerts.Firing }}• {{ .Labels.instance }}: {{ .Annotations.description }}
{{ end }}{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
*Alerts Resolved:*
{{ range .Alerts.Resolved }}• {{ .Labels.instance }}: {{ .Annotations.description }}
{{ end }}{{ end }}{{ end }}

{{ define "telegram_text" }}{{ template "telegram_title" .}}
{{ template "telegram_message" . }}{{ end }}

0 comments on commit 753711e

Please sign in to comment.