-
Notifications
You must be signed in to change notification settings - Fork 69
/
config.go
258 lines (217 loc) · 6.76 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package config
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
"sync"
yaml "gopkg.in/yaml.v2"
)
// Config - Azure exporter configuration
type Config struct {
ActiveDirectoryAuthorityURL string `yaml:"active_directory_authority_url"`
ResourceManagerURL string `yaml:"resource_manager_url"`
Credentials Credentials `yaml:"credentials"`
Targets []Target `yaml:"targets"`
ResourceGroups []ResourceGroup `yaml:"resource_groups"`
ResourceTags []ResourceTag `yaml:"resource_tags"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}
// SafeConfig - mutex protected config for live reloads.
type SafeConfig struct {
sync.RWMutex
C *Config
}
// ReloadConfig - allows for live reloads of the configuration file.
func (sc *SafeConfig) ReloadConfig(confFile string) (err error) {
var c = &Config{
ActiveDirectoryAuthorityURL: "https://login.microsoftonline.com/",
ResourceManagerURL: "https://management.azure.com/",
}
yamlFile, err := ioutil.ReadFile(confFile)
if err != nil {
return fmt.Errorf("Error reading config file: %s", err)
}
if err := yaml.Unmarshal(yamlFile, c); err != nil {
return fmt.Errorf("Error parsing config file: %s", err)
}
if err := c.Validate(); err != nil {
return fmt.Errorf("Error validating config file: %s", err)
}
sc.Lock()
sc.C = c
sc.Unlock()
return nil
}
var validAggregations = []string{"Total", "Average", "Minimum", "Maximum"}
func (c *Config) Validate() (err error) {
for _, t := range c.Targets {
if err := c.validateAggregations(t.Aggregations); err != nil {
return err
}
if len(t.Resource) == 0 {
return fmt.Errorf("name needs to be specified in each resource")
}
if !strings.HasPrefix(t.Resource, "/") {
return fmt.Errorf("Resource path %q must start with a /", t.Resource)
}
if len(t.Metrics) == 0 {
return fmt.Errorf("At least one metric needs to be specified in each resource")
}
}
for _, t := range c.ResourceGroups {
if err := c.validateAggregations(t.Aggregations); err != nil {
return err
}
if len(t.ResourceGroup) == 0 {
return fmt.Errorf("resource_group needs to be specified in each resource group")
}
if len(t.ResourceTypes) == 0 {
return fmt.Errorf("At lease one resource type needs to be specified in each resource group")
}
if len(t.Metrics) == 0 {
return fmt.Errorf("At least one metric needs to be specified in each resource group")
}
}
return nil
}
func (c *Config) validateAggregations(aggregations []string) error {
for _, a := range aggregations {
ok := false
for _, valid := range validAggregations {
if a == valid {
ok = true
break
}
}
if !ok {
return fmt.Errorf("%s is not one of the valid aggregations (%v)", a, validAggregations)
}
}
return nil
}
// Credentials - Azure credentials
type Credentials struct {
SubscriptionID string `yaml:"subscription_id"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
TenantID string `yaml:"tenant_id"`
XXX map[string]interface{} `yaml:",inline"`
}
// Target represents Azure target resource and its associated metric definitions
type Target struct {
Resource string `yaml:"resource"`
MetricNamespace string `yaml:"metric_namespace"`
Metrics []Metric `yaml:"metrics"`
Aggregations []string `yaml:"aggregations"`
XXX map[string]interface{} `yaml:",inline"`
}
// ResourceGroup represents Azure target resource group and its associated metric definitions
type ResourceGroup struct {
ResourceGroup string `yaml:"resource_group"`
MetricNamespace string `yaml:"metric_namespace"`
ResourceTypes []string `yaml:"resource_types"`
ResourceNameIncludeRe []Regexp `yaml:"resource_name_include_re"`
ResourceNameExcludeRe []Regexp `yaml:"resource_name_exclude_re"`
Metrics []Metric `yaml:"metrics"`
Aggregations []string `yaml:"aggregations"`
XXX map[string]interface{} `yaml:",inline"`
}
// ResourceTag selects resources with tag name and tag value
type ResourceTag struct {
ResourceTagName string `yaml:"resource_tag_name"`
ResourceTagValue string `yaml:"resource_tag_value"`
MetricNamespace string `yaml:"metric_namespace"`
ResourceTypes []string `yaml:"resource_types"`
Metrics []Metric `yaml:"metrics"`
Aggregations []string `yaml:"aggregations"`
XXX map[string]interface{} `yaml:",inline"`
}
// Metric defines metric name
type Metric struct {
Name string `yaml:"name"`
XXX map[string]interface{} `yaml:",inline"`
}
// Regexp encapsulates a regexp.Regexp and makes it YAML marshalable.
type Regexp struct {
*regexp.Regexp
}
func checkOverflow(m map[string]interface{}, ctx string) error {
if len(m) > 0 {
var keys []string
for k := range m {
keys = append(keys, k)
}
return fmt.Errorf("unknown fields in %s: %s", ctx, strings.Join(keys, ", "))
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Config
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if err := checkOverflow(s.XXX, "config"); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *Credentials) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Credentials
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if err := checkOverflow(s.XXX, "config"); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *Metric) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Metric
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if err := checkOverflow(s.XXX, "config"); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *Target) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Target
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if err := checkOverflow(s.XXX, "config"); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (s *ResourceGroup) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain ResourceGroup
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if err := checkOverflow(s.XXX, "config"); err != nil {
return err
}
return nil
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
regex, err := regexp.Compile("^(?:" + s + ")$")
if err != nil {
return err
}
re.Regexp = regex
return nil
}