2015-10-11 15:24:49 +00:00
|
|
|
// Copyright 2015 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-10-11 11:32:24 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-07-04 13:31:09 +00:00
|
|
|
tmplhtml "html/template"
|
2018-09-24 13:12:40 +00:00
|
|
|
"io/ioutil"
|
2015-11-26 17:19:46 +00:00
|
|
|
"net/url"
|
2015-12-09 13:51:18 +00:00
|
|
|
"path/filepath"
|
2017-03-04 12:07:31 +00:00
|
|
|
"regexp"
|
2015-11-25 14:49:26 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-11-20 14:10:38 +00:00
|
|
|
tmpltext "text/template"
|
2019-07-04 13:31:09 +00:00
|
|
|
"time"
|
2015-11-25 14:49:26 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
2018-09-24 13:12:40 +00:00
|
|
|
"github.com/prometheus/alertmanager/asset"
|
2015-11-25 14:49:26 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2015-10-11 11:32:24 +00:00
|
|
|
)
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// Template bundles a text and a html template instance.
|
2015-10-11 11:32:24 +00:00
|
|
|
type Template struct {
|
2015-11-20 14:10:38 +00:00
|
|
|
text *tmpltext.Template
|
|
|
|
html *tmplhtml.Template
|
2015-11-26 17:19:46 +00:00
|
|
|
|
|
|
|
ExternalURL *url.URL
|
2015-10-11 11:32:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// FromGlobs calls ParseGlob on all path globs provided and returns the
|
|
|
|
// resulting Template.
|
2015-10-11 14:54:39 +00:00
|
|
|
func FromGlobs(paths ...string) (*Template, error) {
|
|
|
|
t := &Template{
|
2015-11-20 14:10:38 +00:00
|
|
|
text: tmpltext.New("").Option("missingkey=zero"),
|
|
|
|
html: tmplhtml.New("").Option("missingkey=zero"),
|
2015-10-11 14:54:39 +00:00
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
2015-11-25 14:49:26 +00:00
|
|
|
t.text = t.text.Funcs(tmpltext.FuncMap(DefaultFuncs))
|
|
|
|
t.html = t.html.Funcs(tmplhtml.FuncMap(DefaultFuncs))
|
|
|
|
|
2018-09-24 13:12:40 +00:00
|
|
|
f, err := asset.Assets.Open("/templates/default.tmpl")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b, err := ioutil.ReadAll(f)
|
2015-12-01 17:39:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if t.text, err = t.text.Parse(string(b)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if t.html, err = t.html.Parse(string(b)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-11 14:54:39 +00:00
|
|
|
for _, tp := range paths {
|
2015-12-09 13:51:18 +00:00
|
|
|
// ParseGlob in the template packages errors if not at least one file is
|
|
|
|
// matched. We want to allow empty matches that may be populated later on.
|
|
|
|
p, err := filepath.Glob(tp)
|
|
|
|
if err != nil {
|
2015-10-11 14:54:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-09 13:51:18 +00:00
|
|
|
if len(p) > 0 {
|
|
|
|
if t.text, err = t.text.ParseGlob(tp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if t.html, err = t.html.ParseGlob(tp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-11 14:54:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// ExecuteTextString needs a meaningful doc comment (TODO(fabxc)).
|
2015-10-29 13:34:24 +00:00
|
|
|
func (t *Template) ExecuteTextString(text string, data interface{}) (string, error) {
|
2016-04-01 08:23:34 +00:00
|
|
|
if text == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2015-10-29 13:34:24 +00:00
|
|
|
tmpl, err := t.text.Clone()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-02-05 14:41:15 +00:00
|
|
|
tmpl, err = tmpl.New("").Option("missingkey=zero").Parse(text)
|
2015-10-29 13:34:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-10-11 11:32:24 +00:00
|
|
|
var buf bytes.Buffer
|
2015-10-29 13:34:24 +00:00
|
|
|
err = tmpl.Execute(&buf, data)
|
2015-10-11 11:32:24 +00:00
|
|
|
return buf.String(), err
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// ExecuteHTMLString needs a meaningful doc comment (TODO(fabxc)).
|
2015-10-29 13:34:24 +00:00
|
|
|
func (t *Template) ExecuteHTMLString(html string, data interface{}) (string, error) {
|
2016-04-01 08:23:34 +00:00
|
|
|
if html == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2015-10-29 13:34:24 +00:00
|
|
|
tmpl, err := t.html.Clone()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-02-05 14:41:15 +00:00
|
|
|
tmpl, err = tmpl.New("").Option("missingkey=zero").Parse(html)
|
2015-10-29 13:34:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-10-11 11:32:24 +00:00
|
|
|
var buf bytes.Buffer
|
2015-10-29 13:34:24 +00:00
|
|
|
err = tmpl.Execute(&buf, data)
|
2015-10-11 11:32:24 +00:00
|
|
|
return buf.String(), err
|
|
|
|
}
|
2015-11-25 14:49:26 +00:00
|
|
|
|
|
|
|
type FuncMap map[string]interface{}
|
|
|
|
|
|
|
|
var DefaultFuncs = FuncMap{
|
|
|
|
"toUpper": strings.ToUpper,
|
|
|
|
"toLower": strings.ToLower,
|
2015-11-28 10:16:13 +00:00
|
|
|
"title": strings.Title,
|
2015-11-30 17:00:38 +00:00
|
|
|
// join is equal to strings.Join but inverts the argument order
|
|
|
|
// for easier pipelining in templates.
|
|
|
|
"join": func(sep string, s []string) string {
|
|
|
|
return strings.Join(s, sep)
|
2015-11-25 14:49:26 +00:00
|
|
|
},
|
2018-07-03 09:55:47 +00:00
|
|
|
"match": regexp.MatchString,
|
2016-04-20 14:14:40 +00:00
|
|
|
"safeHtml": func(text string) tmplhtml.HTML {
|
|
|
|
return tmplhtml.HTML(text)
|
|
|
|
},
|
2017-03-04 12:07:31 +00:00
|
|
|
"reReplaceAll": func(pattern, repl, text string) string {
|
|
|
|
re := regexp.MustCompile(pattern)
|
|
|
|
return re.ReplaceAllString(text, repl)
|
|
|
|
},
|
2019-11-13 09:32:51 +00:00
|
|
|
"stringSlice": func(s ...string) []string {
|
|
|
|
return s
|
|
|
|
},
|
2015-11-25 14:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pair is a key/value string pair.
|
|
|
|
type Pair struct {
|
|
|
|
Name, Value string
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:00:38 +00:00
|
|
|
// Pairs is a list of key/value string pairs.
|
|
|
|
type Pairs []Pair
|
|
|
|
|
|
|
|
// Names returns a list of names of the pairs.
|
|
|
|
func (ps Pairs) Names() []string {
|
|
|
|
ns := make([]string, 0, len(ps))
|
|
|
|
for _, p := range ps {
|
|
|
|
ns = append(ns, p.Name)
|
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values returns a list of values of the pairs.
|
|
|
|
func (ps Pairs) Values() []string {
|
|
|
|
vs := make([]string, 0, len(ps))
|
|
|
|
for _, p := range ps {
|
|
|
|
vs = append(vs, p.Value)
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
|
|
|
|
|
|
|
// KV is a set of key/value string pairs.
|
|
|
|
type KV map[string]string
|
|
|
|
|
|
|
|
// SortedPairs returns a sorted list of key/value pairs.
|
|
|
|
func (kv KV) SortedPairs() Pairs {
|
|
|
|
var (
|
|
|
|
pairs = make([]Pair, 0, len(kv))
|
|
|
|
keys = make([]string, 0, len(kv))
|
|
|
|
sortStart = 0
|
|
|
|
)
|
|
|
|
for k := range kv {
|
|
|
|
if k == string(model.AlertNameLabel) {
|
|
|
|
keys = append([]string{k}, keys...)
|
|
|
|
sortStart = 1
|
|
|
|
} else {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(keys[sortStart:])
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
pairs = append(pairs, Pair{k, kv[k]})
|
|
|
|
}
|
|
|
|
return pairs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove returns a copy of the key/value set without the given keys.
|
|
|
|
func (kv KV) Remove(keys []string) KV {
|
|
|
|
keySet := make(map[string]struct{}, len(keys))
|
|
|
|
for _, k := range keys {
|
|
|
|
keySet[k] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := KV{}
|
|
|
|
for k, v := range kv {
|
|
|
|
if _, ok := keySet[k]; !ok {
|
|
|
|
res[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Names returns the names of the label names in the LabelSet.
|
|
|
|
func (kv KV) Names() []string {
|
|
|
|
return kv.SortedPairs().Names()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values returns a list of the values in the LabelSet.
|
|
|
|
func (kv KV) Values() []string {
|
|
|
|
return kv.SortedPairs().Values()
|
|
|
|
}
|
|
|
|
|
2016-02-10 16:28:36 +00:00
|
|
|
// Data is the data passed to notification templates and webhook pushes.
|
|
|
|
//
|
|
|
|
// End-users should not be exposed to Go's type system, as this will confuse them and prevent
|
|
|
|
// simple things like simple equality checks to fail. Map everything to float64/string.
|
2015-11-25 14:49:26 +00:00
|
|
|
type Data struct {
|
2016-02-10 16:28:36 +00:00
|
|
|
Receiver string `json:"receiver"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Alerts Alerts `json:"alerts"`
|
2015-11-25 14:49:26 +00:00
|
|
|
|
2016-02-10 16:28:36 +00:00
|
|
|
GroupLabels KV `json:"groupLabels"`
|
|
|
|
CommonLabels KV `json:"commonLabels"`
|
|
|
|
CommonAnnotations KV `json:"commonAnnotations"`
|
2015-11-25 14:49:26 +00:00
|
|
|
|
2016-02-10 16:28:36 +00:00
|
|
|
ExternalURL string `json:"externalURL"`
|
2015-11-25 14:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Alert holds one alert for notification templates.
|
|
|
|
type Alert struct {
|
2016-02-12 10:00:33 +00:00
|
|
|
Status string `json:"status"`
|
|
|
|
Labels KV `json:"labels"`
|
|
|
|
Annotations KV `json:"annotations"`
|
|
|
|
StartsAt time.Time `json:"startsAt"`
|
|
|
|
EndsAt time.Time `json:"endsAt"`
|
|
|
|
GeneratorURL string `json:"generatorURL"`
|
2019-06-28 08:32:03 +00:00
|
|
|
Fingerprint string `json:"fingerprint"`
|
2015-11-30 17:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Alerts is a list of Alert objects.
|
|
|
|
type Alerts []Alert
|
|
|
|
|
|
|
|
// Firing returns the subset of alerts that are firing.
|
|
|
|
func (as Alerts) Firing() []Alert {
|
|
|
|
res := []Alert{}
|
|
|
|
for _, a := range as {
|
|
|
|
if a.Status == string(model.AlertFiring) {
|
|
|
|
res = append(res, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolved returns the subset of alerts that are resolved.
|
|
|
|
func (as Alerts) Resolved() []Alert {
|
|
|
|
res := []Alert{}
|
|
|
|
for _, a := range as {
|
|
|
|
if a.Status == string(model.AlertResolved) {
|
|
|
|
res = append(res, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
2015-11-25 14:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 17:19:46 +00:00
|
|
|
// Data assembles data for template expansion.
|
2015-12-03 16:27:36 +00:00
|
|
|
func (t *Template) Data(recv string, groupLabels model.LabelSet, alerts ...*types.Alert) *Data {
|
2015-11-25 14:49:26 +00:00
|
|
|
data := &Data{
|
2019-08-28 08:01:47 +00:00
|
|
|
Receiver: regexp.QuoteMeta(recv),
|
2015-12-03 16:27:36 +00:00
|
|
|
Status: string(types.Alerts(alerts...).Status()),
|
2015-11-30 17:00:38 +00:00
|
|
|
Alerts: make(Alerts, 0, len(alerts)),
|
|
|
|
GroupLabels: KV{},
|
|
|
|
CommonLabels: KV{},
|
|
|
|
CommonAnnotations: KV{},
|
2015-11-26 17:19:46 +00:00
|
|
|
ExternalURL: t.ExternalURL.String(),
|
2015-11-25 14:49:26 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 10:00:33 +00:00
|
|
|
// The call to types.Alert is necessary to correctly resolve the internal
|
|
|
|
// representation to the user representation.
|
|
|
|
for _, a := range types.Alerts(alerts...) {
|
2015-11-25 14:49:26 +00:00
|
|
|
alert := Alert{
|
2015-12-03 16:27:36 +00:00
|
|
|
Status: string(a.Status()),
|
|
|
|
Labels: make(KV, len(a.Labels)),
|
|
|
|
Annotations: make(KV, len(a.Annotations)),
|
2016-02-12 10:00:33 +00:00
|
|
|
StartsAt: a.StartsAt,
|
|
|
|
EndsAt: a.EndsAt,
|
2016-02-10 16:28:36 +00:00
|
|
|
GeneratorURL: a.GeneratorURL,
|
2019-06-28 08:32:03 +00:00
|
|
|
Fingerprint: a.Fingerprint().String(),
|
2015-11-25 14:49:26 +00:00
|
|
|
}
|
|
|
|
for k, v := range a.Labels {
|
|
|
|
alert.Labels[string(k)] = string(v)
|
|
|
|
}
|
|
|
|
for k, v := range a.Annotations {
|
|
|
|
alert.Annotations[string(k)] = string(v)
|
|
|
|
}
|
|
|
|
data.Alerts = append(data.Alerts, alert)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range groupLabels {
|
|
|
|
data.GroupLabels[string(k)] = string(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(alerts) >= 1 {
|
|
|
|
var (
|
|
|
|
commonLabels = alerts[0].Labels.Clone()
|
|
|
|
commonAnnotations = alerts[0].Annotations.Clone()
|
|
|
|
)
|
|
|
|
for _, a := range alerts[1:] {
|
2019-07-04 13:31:09 +00:00
|
|
|
if len(commonLabels) == 0 && len(commonAnnotations) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2015-11-25 14:49:26 +00:00
|
|
|
for ln, lv := range commonLabels {
|
|
|
|
if a.Labels[ln] != lv {
|
|
|
|
delete(commonLabels, ln)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for an, av := range commonAnnotations {
|
|
|
|
if a.Annotations[an] != av {
|
|
|
|
delete(commonAnnotations, an)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range commonLabels {
|
|
|
|
data.CommonLabels[string(k)] = string(v)
|
|
|
|
}
|
|
|
|
for k, v := range commonAnnotations {
|
|
|
|
data.CommonAnnotations[string(k)] = string(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|