Skip to content

Commit 753711e

Browse files
authored
Merge pull request #51 from r3nic1e/templating
Add message template support
2 parents cb8c0d1 + d73859e commit 753711e

5 files changed

Lines changed: 85 additions & 22 deletions

File tree

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ receivers:
5757
- url: 'http://localhost:9876/alert'
5858
```
5959
60+
## Message templating
61+
62+
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/).
63+
64+
sachet.yml:
65+
```yaml
66+
templates:
67+
- /etc/sachet/notifications.tmpl
68+
69+
receivers:
70+
- name: 'team-telegram'
71+
provider: telegram
72+
text: '{{ template "telegram_message" . }}'
73+
```
74+
notifications.tmpl:
75+
```
76+
{{ define "telegram_title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} @ {{ .CommonLabels.identifier }} {{ end }}
77+
78+
{{ define "telegram_message" }}
79+
{{ if gt (len .Alerts.Firing) 0 }}
80+
*Alerts Firing:*
81+
{{ range .Alerts.Firing }}• {{ .Labels.instance }}: {{ .Annotations.description }}
82+
{{ end }}{{ end }}
83+
{{ if gt (len .Alerts.Resolved) 0 }}
84+
*Alerts Resolved:*
85+
{{ range .Alerts.Resolved }}• {{ .Labels.instance }}: {{ .Annotations.description }}
86+
{{ end }}{{ end }}{{ end }}
87+
88+
{{ define "telegram_text" }}{{ template "telegram_title" .}}
89+
{{ template "telegram_message" . }}{{ end }}
90+
```
91+
6092
## License
6193

6294
Sachet is licensed under [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause). Copyright (c) 2016, MessageBird

cmd/sachet/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/messagebird/sachet/provider/turbosms"
1818
"github.com/messagebird/sachet/provider/twilio"
1919

20+
"github.com/prometheus/alertmanager/template"
2021
"gopkg.in/yaml.v2"
2122
)
2223

@@ -25,6 +26,7 @@ type ReceiverConf struct {
2526
Provider string
2627
To []string
2728
From string
29+
Text string
2830
}
2931

3032
var config struct {
@@ -45,7 +47,9 @@ var config struct {
4547
}
4648

4749
Receivers []ReceiverConf
50+
Templates []string
4851
}
52+
var tmpl *template.Template
4953

5054
// LoadConfig loads the specified YAML configuration file.
5155
func LoadConfig(filename string) error {
@@ -59,5 +63,6 @@ func LoadConfig(filename string) error {
5963
return err
6064
}
6165

62-
return nil
66+
tmpl, err = template.FromGlobs(config.Templates...)
67+
return err
6368
}

cmd/sachet/main.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,40 @@ func main() {
6464
}
6565

6666
var text string
67-
if len(data.Alerts) > 1 {
68-
labelAlerts := map[string]template.Alerts{
69-
"Firing": data.Alerts.Firing(),
70-
"Resolved": data.Alerts.Resolved(),
67+
if receiverConf.Text != "" {
68+
text, err = tmpl.ExecuteTextString(receiverConf.Text, data)
69+
if err != nil {
70+
errorHandler(w, http.StatusInternalServerError, err, receiverConf.Provider)
71+
return
7172
}
72-
for label, alerts := range labelAlerts {
73-
if len(alerts) > 0 {
74-
text += label + ": \n"
75-
for _, alert := range alerts {
76-
text += alert.Labels["alertname"] + " @" + alert.Labels["instance"]
77-
if len(alert.Labels["exported_instance"]) > 0 {
78-
text += " (" + alert.Labels["exported_instance"] + ")"
73+
} else {
74+
if len(data.Alerts) > 1 {
75+
labelAlerts := map[string]template.Alerts{
76+
"Firing": data.Alerts.Firing(),
77+
"Resolved": data.Alerts.Resolved(),
78+
}
79+
for label, alerts := range labelAlerts {
80+
if len(alerts) > 0 {
81+
text += label + ": \n"
82+
for _, alert := range alerts {
83+
text += alert.Labels["alertname"] + " @" + alert.Labels["instance"]
84+
if len(alert.Labels["exported_instance"]) > 0 {
85+
text += " (" + alert.Labels["exported_instance"] + ")"
86+
}
87+
text += "\n"
7988
}
80-
text += "\n"
8189
}
8290
}
91+
} else if len(data.Alerts) == 1 {
92+
alert := data.Alerts[0]
93+
tuples := []string{}
94+
for k, v := range alert.Labels {
95+
tuples = append(tuples, k+"= "+v)
96+
}
97+
text = strings.ToUpper(data.Status) + " \n" + strings.Join(tuples, "\n")
98+
} else {
99+
text = "Alert \n" + strings.Join(data.CommonLabels.Values(), " | ")
83100
}
84-
} else if len(data.Alerts) == 1 {
85-
alert := data.Alerts[0]
86-
tuples := []string{}
87-
for k, v := range alert.Labels {
88-
tuples = append(tuples, k+"= "+v)
89-
}
90-
text = strings.ToUpper(data.Status) + " \n" + strings.Join(tuples, "\n")
91-
} else {
92-
text = "Alert \n" + strings.Join(data.CommonLabels.Values(), " | ")
93101
}
94102

95103
message := sachet.Message{

examples/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ providers:
4141
username: 'username'
4242
password: 'password'
4343

44+
templates:
45+
- telegram.tmpl
46+
4447
receivers:
4548
- name: 'team-sms'
4649
provider: 'messagebird'
@@ -51,4 +54,5 @@ receivers:
5154
provider: 'telegram'
5255
to:
5356
- '164451814' # the chat id of a user. Get yours at https://telegram.me/userinfobot
57+
text: '{{ .GroupLabels.alertname }} @ {{ .Labels.instance }}: {{ .Status | toUpper }}'
5458

examples/telegram.tmpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{ define "telegram_title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} @ {{ .CommonLabels.identifier }} {{ end }}
2+
3+
{{ define "telegram_message" }}
4+
{{ if gt (len .Alerts.Firing) 0 }}
5+
*Alerts Firing:*
6+
{{ range .Alerts.Firing }}• {{ .Labels.instance }}: {{ .Annotations.description }}
7+
{{ end }}{{ end }}
8+
{{ if gt (len .Alerts.Resolved) 0 }}
9+
*Alerts Resolved:*
10+
{{ range .Alerts.Resolved }}• {{ .Labels.instance }}: {{ .Annotations.description }}
11+
{{ end }}{{ end }}{{ end }}
12+
13+
{{ define "telegram_text" }}{{ template "telegram_title" .}}
14+
{{ template "telegram_message" . }}{{ end }}

0 commit comments

Comments
 (0)