forked from nathanielc/morgoth
/
config.go
140 lines (122 loc) · 3.14 KB
/
config.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package morgoth
import (
"github.com/nathanielc/morgoth/Godeps/_workspace/src/github.com/golang/glog"
"github.com/nathanielc/morgoth/Godeps/_workspace/src/gopkg.in/validator.v2"
"github.com/nathanielc/morgoth/Godeps/_workspace/src/gopkg.in/yaml.v2"
"github.com/nathanielc/morgoth/config"
"io/ioutil"
"os"
"time"
)
// Base config struct for the entire morgoth config
type Config struct {
APIPort uint `yaml:"api_port" validate:"min=1,max=65535" default:"7000"`
EngineConf EngineConf `yaml:"engine"`
Schedules []*ScheduleConf `yaml:"schedules"`
Mappings []*MappingConf `yaml:"mappings"`
Alerts []*AlertConf `yaml:"alerts"`
}
type ScheduleConf struct {
Query string
Period time.Duration
GroupByInterval time.Duration `yaml:"group_by_interval"`
Delay time.Duration
Tags map[string]string
}
type MappingConf struct {
Name string
Tags map[string]string
Detector *DetectorConf
}
type DetectorConf struct {
NormalCount int `yaml:"normal_count" validate:"nonzero" default:"3"`
Consensus float64 `yaml:"consensus" validate:"nonzero" default:"0.5"`
MinSupport float64 `yaml:"min_support" validate:"nonzero,max=1" default:"0.05"`
ErrorTolerance float64 `yaml:"error_tolerance" validate:"nonzero,max=1" default:"0.1"`
Fingerprinters []FingerprinterConf `yaml:"fingerprints"`
}
type AlertConf struct {
Message string
Threshold float64
Query string
Period time.Duration
GroupByInterval time.Duration `yaml:"group_by_interval"`
Delay time.Duration
Notifiers []*NotifierConf
}
func (self *Config) Default() {
config.PerformDefault(self)
self.EngineConf.Default()
for _, mp := range self.Mappings {
mp.Default()
}
}
func (self Config) Validate() (err error) {
glog.V(2).Info("Validating Config")
err = validator.Validate(self)
if err != nil {
return
}
err = self.EngineConf.Validate()
if err != nil {
return
}
for _, mp := range self.Mappings {
err = mp.Validate()
if err != nil {
return
}
}
return
}
func (self *MappingConf) Validate() error {
return validator.Validate(self)
}
func (self *MappingConf) Default() {
config.PerformDefault(self)
self.Detector.Default()
}
func (self *DetectorConf) Validate() error {
glog.Info("Validating DetectorConf ", self)
err := validator.Validate(self)
if err != nil {
return err
}
for _, f := range self.Fingerprinters {
err = f.Validate()
if err != nil {
return err
}
}
return nil
}
func (self *DetectorConf) Default() {
config.PerformDefault(self)
for _, f := range self.Fingerprinters {
f.Default()
}
}
func LoadFromFile(path string) (*Config, error) {
config_file, err := os.Open(path)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(config_file)
if err != nil {
return nil, err
}
return Load(data)
}
func Load(data []byte) (*Config, error) {
config := new(Config)
err := yaml.Unmarshal(data, config)
if err != nil {
return nil, err
}
config.Default()
err = config.Validate()
if err != nil {
return nil, err
}
return config, nil
}