2017-07-25 14:17:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/log"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the Go representation of the yaml config file.
|
|
|
|
type Config struct {
|
2018-09-21 19:50:30 +00:00
|
|
|
Modules map[string]IPMIConfig `yaml:"modules"`
|
2017-07-25 14:17:57 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:50:30 +00:00
|
|
|
// IPMIConfig is the Go representation of a module configuration in the yaml
|
2017-07-25 14:17:57 +00:00
|
|
|
// config file.
|
2018-09-21 19:50:30 +00:00
|
|
|
type IPMIConfig struct {
|
|
|
|
User string `yaml:"user"`
|
|
|
|
Password string `yaml:"pass"`
|
|
|
|
Privilege string `yaml:"privilege"`
|
|
|
|
Driver string `yaml:"driver"`
|
2019-03-22 11:24:47 +00:00
|
|
|
Timeout uint32 `yaml:"timeout"`
|
2018-09-21 19:50:30 +00:00
|
|
|
Collectors []string `yaml:"collectors"`
|
|
|
|
ExcludeSensorIDs []int64 `yaml:"exclude_sensor_ids"`
|
2017-07-25 14:17:57 +00:00
|
|
|
|
|
|
|
// Catches all undefined fields and must be empty after parsing.
|
|
|
|
XXX map[string]interface{} `yaml:",inline"`
|
|
|
|
}
|
|
|
|
|
2019-10-14 19:43:30 +00:00
|
|
|
var emptyConfig = IPMIConfig{Collectors: []string{"ipmi", "dcmi", "bmc", "chassis"}}
|
2018-09-21 19:50:30 +00:00
|
|
|
|
|
|
|
// CollectorName is used for unmarshaling the list of collectors in the yaml config file
|
|
|
|
type CollectorName string
|
|
|
|
|
2017-07-25 14:17:57 +00:00
|
|
|
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.
|
2018-09-21 19:50:30 +00:00
|
|
|
func (s *IPMIConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*s = emptyConfig
|
|
|
|
type plain IPMIConfig
|
2017-07-25 14:17:57 +00:00
|
|
|
if err := unmarshal((*plain)(s)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-21 19:50:30 +00:00
|
|
|
if err := checkOverflow(s.XXX, "modules"); err != nil {
|
2017-07-25 14:17:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-09-21 19:50:30 +00:00
|
|
|
for _, c := range s.Collectors {
|
2019-10-14 19:43:30 +00:00
|
|
|
if !(c == "ipmi" || c == "dcmi" || c == "bmc" || c == "chassis") {
|
2018-09-21 19:50:30 +00:00
|
|
|
return fmt.Errorf("unknown collector name: %s", c)
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 14:17:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{}
|
2018-09-21 19:50:30 +00:00
|
|
|
var config []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if configFile != "" {
|
|
|
|
config, err = ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error reading config file: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config = []byte("# use empty file as default")
|
2017-07-25 14:17:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:50:30 +00:00
|
|
|
if err = yaml.Unmarshal(config, c); err != nil {
|
2017-07-25 14:17:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sc.Lock()
|
|
|
|
sc.C = c
|
|
|
|
sc.Unlock()
|
|
|
|
|
2018-09-21 19:50:30 +00:00
|
|
|
if configFile != "" {
|
|
|
|
log.Infoln("Loaded config file", configFile)
|
|
|
|
}
|
2017-07-25 14:17:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:50:30 +00:00
|
|
|
// HasModule returns true if a given module is configured. It is concurrency-safe.
|
|
|
|
func (sc *SafeConfig) HasModule(module string) bool {
|
2017-07-25 14:17:57 +00:00
|
|
|
sc.Lock()
|
|
|
|
defer sc.Unlock()
|
2018-09-21 19:50:30 +00:00
|
|
|
|
|
|
|
_, ok := sc.C.Modules[module]
|
|
|
|
return ok
|
2017-07-25 14:17:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:50:30 +00:00
|
|
|
// ConfigForTarget returns the config for a given target/module, or the
|
|
|
|
// default. It is concurrency-safe.
|
|
|
|
func (sc *SafeConfig) ConfigForTarget(target, module string) IPMIConfig {
|
2017-07-25 14:17:57 +00:00
|
|
|
sc.Lock()
|
|
|
|
defer sc.Unlock()
|
2018-09-21 19:50:30 +00:00
|
|
|
|
|
|
|
var config IPMIConfig
|
|
|
|
var ok = false
|
|
|
|
|
|
|
|
if module != "default" {
|
|
|
|
config, ok = sc.C.Modules[module]
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("Requested module %s for target %s not found, using default", module, 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
|
|
|
|
log.Debugf("Needed default config for target %s, but none configured, using FreeIPMI defaults", targetName(target))
|
|
|
|
config = emptyConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
2017-07-25 14:17:57 +00:00
|
|
|
}
|