/
condition.go
112 lines (100 loc) · 2.83 KB
/
condition.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package alerter
import (
"errors"
"fmt"
"strings"
"github.com/spf13/viper"
"github.com/vogtp/som/pkg/core"
"github.com/vogtp/som/pkg/core/msg"
"github.com/vogtp/som/pkg/core/status"
)
// Conditon is a check if a alerting rule is triggered
type Conditon interface {
Kind() string
DoAlert(*msg.AlertMsg, *viper.Viper) error
CheckConfig(*viper.Viper) error
}
// AddConditon add an alerting condition
func (a *Alerter) AddConditon(c Conditon) error {
if c == nil {
return errors.New("condition must not be nil")
}
if len(c.Kind()) < 1 {
return errors.New("condition must have a kind")
}
a.conditions[c.Kind()] = c
return nil
}
// StatusCond is a filter condtion based on the status
type StatusCond struct{}
// Kind is the name
func (StatusCond) Kind() string { return "status" }
// CheckConfig checks if the config is valid
func (StatusCond) CheckConfig(cfg *viper.Viper) error {
lvl := status.Unknown.FromString(cfg.GetString("level"))
if lvl == status.Unknown {
return fmt.Errorf("reqested unknown status level: %s", cfg.GetString("level"))
}
return nil
}
// DoAlert checks if an alert should be send and returns an error if not
func (StatusCond) DoAlert(msg *msg.AlertMsg, cfg *viper.Viper) error {
lvl := status.Unknown.FromString(cfg.GetString("level"))
if lvl == status.Unknown {
return fmt.Errorf("not alerting, reqested unknown status level: %s", cfg.GetString("level"))
}
if status.Unknown.FromString(msg.Level) < lvl {
return fmt.Errorf("%s < %s", status.Unknown.FromString(msg.Level), lvl)
}
return nil
}
// SzenarioCond is a filter condtion based on the szenario name
type SzenarioCond struct{}
// Kind is the name
func (SzenarioCond) Kind() string { return "szenario" }
// CheckConfig checks if the config is valid
func (SzenarioCond) CheckConfig(cfg *viper.Viper) error {
var err error
szCfg := core.Get().SzenaioConfig()
if szCfg == nil {
return nil
}
szList := cfg.GetStringSlice(cfgInclude)
szList = append(szList, cfg.GetStringSlice(cfgExclude)...)
for _, n := range szList {
if sz := szCfg.ByName(n); sz == nil {
if err == nil {
err = errors.New(n)
} else {
err = fmt.Errorf("%w, %q", err, n)
}
}
}
if err != nil {
err = fmt.Errorf("no such szenario %w", err)
}
return err
}
// DoAlert checks if an alert should be send and returns an error if not
func (SzenarioCond) DoAlert(msg *msg.AlertMsg, cfg *viper.Viper) error {
name := strings.ToLower(msg.Name)
include := cfg.GetStringSlice(cfgInclude)
var err error
if len(include) > 0 {
err = fmt.Errorf("szenario %s is not included", msg.Name)
}
for _, n := range include {
if strings.ToLower(n) == name {
return nil // is included
}
}
if err != nil {
return err
}
for _, n := range cfg.GetStringSlice(cfgExclude) {
if strings.ToLower(n) == name {
return fmt.Errorf("szenario %s is excluded", msg.Name)
}
}
return nil
}