2020-04-06 11:44:03 +00:00
|
|
|
package main
|
2020-02-16 18:59:02 +00:00
|
|
|
|
2020-03-28 23:48:30 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-06 11:44:03 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2020-03-28 23:48:30 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-02-16 18:59:02 +00:00
|
|
|
|
2020-04-06 11:44:03 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2020-03-28 23:48:30 +00:00
|
|
|
)
|
2020-02-16 18:59:02 +00:00
|
|
|
|
2020-03-28 23:48:30 +00:00
|
|
|
// Parse parses rule configs from given file patterns
|
2020-04-06 11:44:03 +00:00
|
|
|
func Parse(pathPatterns []string, validateAnnotations bool) ([]Group, error) {
|
2020-03-28 23:48:30 +00:00
|
|
|
var fp []string
|
|
|
|
for _, pattern := range pathPatterns {
|
|
|
|
matches, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading file patther %s:%v", pattern, err)
|
|
|
|
}
|
|
|
|
fp = append(fp, matches...)
|
|
|
|
}
|
2020-04-06 11:44:03 +00:00
|
|
|
var groups []Group
|
2020-03-28 23:48:30 +00:00
|
|
|
for _, file := range fp {
|
|
|
|
groupsNames := map[string]struct{}{}
|
|
|
|
gr, err := parseFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("file %s: %w", file, err)
|
|
|
|
}
|
|
|
|
for _, group := range gr {
|
|
|
|
if _, ok := groupsNames[group.Name]; ok {
|
|
|
|
return nil, fmt.Errorf("one file can not contain groups with the same name %s, filepath:%s", file, group.Name)
|
|
|
|
}
|
|
|
|
groupsNames[group.Name] = struct{}{}
|
|
|
|
for _, rule := range group.Rules {
|
|
|
|
if err = rule.Validate(); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid rule filepath:%s, group %s:%w", file, group.Name, err)
|
|
|
|
}
|
2020-04-06 11:44:03 +00:00
|
|
|
// TODO: this init looks weird here
|
|
|
|
rule.alerts = make(map[uint64]*notifier.Alert)
|
2020-03-28 23:48:30 +00:00
|
|
|
if validateAnnotations {
|
2020-04-26 11:15:04 +00:00
|
|
|
if err = notifier.ValidateTemplates(rule.Annotations); err != nil {
|
2020-04-26 10:53:57 +00:00
|
|
|
return nil, fmt.Errorf("invalid annotations filepath:%s, group %s:%w", file, group.Name, err)
|
|
|
|
}
|
2020-04-26 11:15:04 +00:00
|
|
|
if err = notifier.ValidateTemplates(rule.Labels); err != nil {
|
2020-04-26 10:53:57 +00:00
|
|
|
return nil, fmt.Errorf("invalid labels filepath:%s, group %s:%w", file, group.Name, err)
|
2020-03-28 23:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 21:51:22 +00:00
|
|
|
rule.group = group
|
2020-03-28 23:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
groups = append(groups, gr...)
|
|
|
|
}
|
|
|
|
if len(groups) < 1 {
|
|
|
|
return nil, fmt.Errorf("no groups found in %s", strings.Join(pathPatterns, ";"))
|
|
|
|
}
|
|
|
|
return groups, nil
|
2020-02-16 18:59:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 11:44:03 +00:00
|
|
|
func parseFile(path string) ([]Group, error) {
|
2020-03-28 23:48:30 +00:00
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading alert rule file: %w", err)
|
|
|
|
}
|
|
|
|
g := struct {
|
2020-04-06 11:44:03 +00:00
|
|
|
Groups []Group `yaml:"groups"`
|
2020-03-28 23:48:30 +00:00
|
|
|
}{}
|
|
|
|
err = yaml.Unmarshal(data, &g)
|
|
|
|
return g.Groups, err
|
2020-02-16 18:59:02 +00:00
|
|
|
}
|