forked from kbudde/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
165 lines (142 loc) · 4.16 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
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
)
var (
config rabbitExporterConfig
defaultConfig = rabbitExporterConfig{
RabbitURL: "http://localhost:15672",
RabbitUsername: "guest",
RabbitPassword: "guest",
PublishPort: "9090",
PublishAddr: "",
OutputFormat: "TTY", //JSON
CAFile: "ca.pem",
InsecureSkipVerify: false,
SkipQueues: regexp.MustCompile("^$"),
IncludeQueues: regexp.MustCompile(".*"),
RabbitCapabilities: make(rabbitCapabilitySet),
EnabledExporters: []string{"exchange", "node", "overview", "queue"},
Timeout: 30,
}
)
type rabbitExporterConfig struct {
RabbitURL string
RabbitUsername string
RabbitPassword string
PublishPort string
PublishAddr string
OutputFormat string
CAFile string
InsecureSkipVerify bool
SkipQueues *regexp.Regexp
IncludeQueues *regexp.Regexp
RabbitCapabilities rabbitCapabilitySet
EnabledExporters []string
Timeout int
}
type rabbitCapability string
type rabbitCapabilitySet map[rabbitCapability]bool
const (
rabbitCapNoSort rabbitCapability = "no_sort"
rabbitCapBert rabbitCapability = "bert"
)
var allRabbitCapabilities = rabbitCapabilitySet{
rabbitCapNoSort: true,
rabbitCapBert: true,
}
func initConfig() {
config = defaultConfig
if url := os.Getenv("RABBIT_URL"); url != "" {
if valid, _ := regexp.MatchString("https?://[a-zA-Z.0-9]+", strings.ToLower(url)); valid {
config.RabbitURL = url
} else {
panic(fmt.Errorf("Rabbit URL must start with http:// or https://"))
}
}
var user string
var pass string
if len(os.Getenv("RABBIT_USER_FILE")) != 0 {
fileContents, err := ioutil.ReadFile(os.Getenv("RABBIT_USER_FILE"))
if err != nil {
panic(err)
}
user = strings.TrimSpace(string(fileContents))
} else {
user = os.Getenv("RABBIT_USER")
}
if user != "" {
config.RabbitUsername = user
}
if len(os.Getenv("RABBIT_PASSWORD_FILE")) != 0 {
fileContents, err := ioutil.ReadFile(os.Getenv("RABBIT_PASSWORD_FILE"))
if err != nil {
panic(err)
}
pass = strings.TrimSpace(string(fileContents))
} else {
pass = os.Getenv("RABBIT_PASSWORD")
}
if pass != "" {
config.RabbitPassword = pass
}
if port := os.Getenv("PUBLISH_PORT"); port != "" {
if _, err := strconv.Atoi(port); err == nil {
config.PublishPort = port
} else {
panic(fmt.Errorf("The configured port is not a valid number: %v", port))
}
}
if addr := os.Getenv("PUBLISH_ADDR"); addr != "" {
config.PublishAddr = addr
}
if output := os.Getenv("OUTPUT_FORMAT"); output != "" {
config.OutputFormat = output
}
if cafile := os.Getenv("CAFILE"); cafile != "" {
config.CAFile = cafile
}
if insecureSkipVerify := os.Getenv("SKIPVERIFY"); insecureSkipVerify == "true" || insecureSkipVerify == "1" || insecureSkipVerify == "TRUE" {
config.InsecureSkipVerify = true
}
if SkipQueues := os.Getenv("SKIP_QUEUES"); SkipQueues != "" {
config.SkipQueues = regexp.MustCompile(SkipQueues)
}
if IncludeQueues := os.Getenv("INCLUDE_QUEUES"); IncludeQueues != "" {
config.IncludeQueues = regexp.MustCompile(IncludeQueues)
}
if rawCapabilities := os.Getenv("RABBIT_CAPABILITIES"); rawCapabilities != "" {
config.RabbitCapabilities = parseCapabilities(rawCapabilities)
}
if enabledExporters := os.Getenv("RABBIT_EXPORTERS"); enabledExporters != "" {
config.EnabledExporters = strings.Split(enabledExporters, ",")
}
if timeout := os.Getenv("RABBIT_TIMEOUT"); timeout != "" {
t, err := strconv.Atoi(timeout)
if err != nil {
panic(fmt.Errorf("timeout is not a number: %v", err))
}
config.Timeout = t
}
}
func parseCapabilities(raw string) rabbitCapabilitySet {
result := make(rabbitCapabilitySet)
candidates := strings.Split(raw, ",")
for _, maybeCapStr := range candidates {
maybeCap := rabbitCapability(strings.TrimSpace(maybeCapStr))
enabled, present := allRabbitCapabilities[maybeCap]
if enabled && present {
result[maybeCap] = true
}
}
return result
}
func isCapEnabled(config rabbitExporterConfig, cap rabbitCapability) bool {
exists, enabled := config.RabbitCapabilities[cap]
return exists && enabled
}