-
Notifications
You must be signed in to change notification settings - Fork 133
/
config.go
298 lines (261 loc) · 8 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2021 The Prometheus Authors
// 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.
package main
import (
"fmt"
"os"
"regexp"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus-community/ipmi_exporter/freeipmi"
yaml "gopkg.in/yaml.v2"
)
// CollectorName is used for unmarshaling the list of collectors in the yaml config file
type CollectorName string
// ConfiguredCollector wraps an existing collector implementation,
// potentially altering its default settings.
type ConfiguredCollector struct {
collector collector
command string
default_args []string
custom_args []string
}
func (c ConfiguredCollector) Name() CollectorName {
return c.collector.Name()
}
func (c ConfiguredCollector) Cmd() string {
if c.command != "" {
return c.command
}
return c.collector.Cmd()
}
func (c ConfiguredCollector) Args() []string {
args := []string{}
if c.custom_args != nil {
// custom args come first, this way it is quite easy to
// override a collector to use e.g. sudo
args = append(args, c.custom_args...)
}
if c.default_args != nil {
args = append(args, c.default_args...)
} else {
args = append(args, c.collector.Args()...)
}
return args
}
func (c ConfiguredCollector) Collect(output freeipmi.Result, ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
return c.collector.Collect(output, ch, target)
}
func (c CollectorName) GetInstance() (collector, error) {
// This is where a new collector would have to be "registered"
switch c {
case IPMICollectorName:
return IPMICollector{}, nil
case BMCCollectorName:
return BMCCollector{}, nil
case BMCWatchdogCollectorName:
return BMCWatchdogCollector{}, nil
case SELCollectorName:
return SELCollector{}, nil
case SELEventsCollectorName:
return SELEventsCollector{}, nil
case DCMICollectorName:
return DCMICollector{}, nil
case ChassisCollectorName:
return ChassisCollector{}, nil
case SMLANModeCollectorName:
return SMLANModeCollector{}, nil
}
return nil, fmt.Errorf("invalid collector: %s", string(c))
}
func (c CollectorName) IsValid() error {
_, err := c.GetInstance()
return err
}
// Config is the Go representation of the yaml config file.
type Config struct {
Modules map[string]IPMIConfig `yaml:"modules"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}
// SafeConfig wraps Config for concurrency-safe operations.
type SafeConfig struct {
sync.RWMutex
C *Config
}
// IPMIConfig is the Go representation of a module configuration in the yaml
// config file.
type IPMIConfig struct {
User string `yaml:"user"`
Password string `yaml:"pass"`
Privilege string `yaml:"privilege"`
Driver string `yaml:"driver"`
Timeout uint32 `yaml:"timeout"`
Collectors []CollectorName `yaml:"collectors"`
ExcludeSensorIDs []int64 `yaml:"exclude_sensor_ids"`
WorkaroundFlags []string `yaml:"workaround_flags"`
CollectorCmd map[CollectorName]string `yaml:"collector_cmd"`
CollectorArgs map[CollectorName][]string `yaml:"default_args"`
CustomArgs map[CollectorName][]string `yaml:"custom_args"`
SELEvents []*IpmiSELEvent `yaml:"sel_events,omitempty"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}
type IpmiSELEvent struct {
Name string `yaml:"name"`
RegexRaw string `yaml:"regex"`
Regex *regexp.Regexp `yaml:"-"`
}
var defaultConfig = IPMIConfig{
Collectors: []CollectorName{IPMICollectorName, DCMICollectorName, BMCCollectorName, ChassisCollectorName},
}
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 *IPMIConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*s = defaultConfig
type plain IPMIConfig
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if err := checkOverflow(s.XXX, "modules"); err != nil {
return err
}
for _, c := range s.Collectors {
if err := c.IsValid(); err != nil {
return err
}
}
for _, selEvent := range s.SELEvents {
selEvent.Regex = regexp.MustCompile(selEvent.RegexRaw)
}
return nil
}
func (c IPMIConfig) GetCollectors() []collector {
result := []collector{}
for _, co := range c.Collectors {
// At this point validity has already been checked
i, _ := co.GetInstance()
cc := ConfiguredCollector{
collector: i,
command: c.CollectorCmd[i.Name()],
default_args: c.CollectorArgs[i.Name()],
custom_args: c.CustomArgs[i.Name()],
}
result = append(result, cc)
}
return result
}
func (c IPMIConfig) GetFreeipmiConfig() string {
var b strings.Builder
if c.Driver != "" {
fmt.Fprintf(&b, "driver-type %s\n", c.Driver)
}
if c.Privilege != "" {
fmt.Fprintf(&b, "privilege-level %s\n", c.Privilege)
}
if c.User != "" {
fmt.Fprintf(&b, "username %s\n", c.User)
}
if c.Password != "" {
fmt.Fprintf(&b, "password %s\n", freeipmi.EscapePassword(c.Password))
}
if c.Timeout != 0 {
fmt.Fprintf(&b, "session-timeout %d\n", c.Timeout)
}
if len(c.WorkaroundFlags) > 0 {
fmt.Fprintf(&b, "workaround-flags")
for _, flag := range c.WorkaroundFlags {
fmt.Fprintf(&b, " %s", flag)
}
fmt.Fprintln(&b)
}
return b.String()
}
// ReloadConfig reloads the config in a concurrency-safe way. If the configFile
// is unreadable or unparsable, an error is returned and the old config is kept.
func (sc *SafeConfig) ReloadConfig(configFile string) error {
var c = &Config{}
var config []byte
var err error
if configFile != "" {
config, err = os.ReadFile(configFile)
if err != nil {
logger.Error("Error reading config file", "error", err)
return err
}
} else {
config = []byte("# use empty file as default")
}
if err = yaml.Unmarshal(config, c); err != nil {
return err
}
sc.Lock()
sc.C = c
sc.Unlock()
if configFile != "" {
logger.Info("Loaded config file", "path", configFile)
}
return nil
}
// HasModule returns true if a given module is configured. It is concurrency-safe.
func (sc *SafeConfig) HasModule(module string) bool {
sc.Lock()
defer sc.Unlock()
_, ok := sc.C.Modules[module]
return ok
}
// ConfigForTarget returns the config for a given target/module, or the
// default. It is concurrency-safe.
func (sc *SafeConfig) ConfigForTarget(target, module string) IPMIConfig {
sc.Lock()
defer sc.Unlock()
var config IPMIConfig
var ok = false
if module != "default" {
config, ok = sc.C.Modules[module]
if !ok {
logger.Error("Requested module not found, using default", "module", module, "target", targetName(target))
}
}
// If nothing found, fall back to defaults
if !ok {
config, ok = sc.C.Modules["default"]
if !ok {
// This is probably fine for running locally, so not making this a warning
logger.Debug("Needed default config for, but none configured, using FreeIPMI defaults", "target", targetName(target))
config = defaultConfig
}
}
return config
}