-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.go
103 lines (85 loc) · 2.36 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
package main
import (
"flag"
"io/ioutil"
"log"
"regexp"
"time"
yaml "gopkg.in/yaml.v2"
)
var configFile = flag.String("config", "xapi_exporter.yml", "config filename")
var config *configClass
type configClass struct {
BindAddress string
NameSpace string
EnabledMetrics []string
Pools map[string][]string
Auth struct {
Username string
Password string
}
TimeoutLogin time.Duration
}
func initConfig() {
config = &configClass{}
data, err := ioutil.ReadFile(*configFile)
if err != nil {
log.Fatalf("Unable to read config: %s", err.Error())
}
err = yaml.Unmarshal(data, config)
if err != nil {
log.Fatalf("Unable to parse config: %s", err.Error())
}
validateConfig()
}
func validateConfig() {
hasErrors := false
// ensure that bindaddress is a valid pattern
if match, err := regexp.MatchString("(i?)^[0-9a-z\\-\\_\\.]*:[0-9]+$",
config.BindAddress); err != nil || !match {
log.Println("config: bindaddress must be in \"host:port\" or \":port\" " +
"format")
hasErrors = true
}
// ensure that prometheus metric namespace is reasonable
if match, err := regexp.MatchString("(i?)^[a-z0-9]+$",
config.NameSpace); err != nil || !match {
log.Println("config: namespace must be reasonable metrics namespace " +
"(try \"xen\")")
hasErrors = true
}
// ensure that username has been specified
if config.Auth.Username == "" {
log.Println("config: auth/username must be specified")
hasErrors = true
}
// ensure that password has been specified
if config.Auth.Password == "" {
log.Println("config: auth/password must be specified")
hasErrors = true
}
// ensure that a timeout value isn't missing/zero
if config.TimeoutLogin == 0 {
log.Println("config: timeoutlogin should be non-zero")
hasErrors = true
}
// ensure at least one pool is configured from which to gather data
if len(config.Pools) == 0 {
log.Println("config: no pools configured")
hasErrors = true
}
// if enabledmetrics is specified, ensure only valid metrics are listed
if len(config.EnabledMetrics) > 0 {
for _, mName := range config.EnabledMetrics {
if _, ok := metricHelp[mName]; !ok {
log.Printf("config: enabledmetrics contains invalid metric name: %s\n",
mName)
hasErrors = true
}
}
}
// if any errors occurred, abort the program
if hasErrors {
log.Fatalln("Aborting due to configuration errors.")
}
}