forked from kbudde/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
187 lines (161 loc) · 4.81 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
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(".*"),
SkipVHost: regexp.MustCompile("^$"),
IncludeVHost: regexp.MustCompile(".*"),
RabbitCapabilities: make(rabbitCapabilitySet),
EnabledExporters: []string{"exchange", "node", "overview", "queue"},
Timeout: 30,
MaxQueues: 0,
}
)
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
SkipVHost *regexp.Regexp
IncludeVHost *regexp.Regexp
RabbitCapabilities rabbitCapabilitySet
EnabledExporters []string
Timeout int
MaxQueues 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 SkipVHost := os.Getenv("SKIP_VHOST"); SkipVHost != "" {
config.SkipVHost = regexp.MustCompile(SkipVHost)
}
if IncludeVHost := os.Getenv("INCLUDE_VHOST"); IncludeVHost != "" {
config.IncludeVHost = regexp.MustCompile(IncludeVHost)
}
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
}
if maxQueues := os.Getenv("MAX_QUEUES"); maxQueues != "" {
m, err := strconv.Atoi(maxQueues)
if err != nil {
panic(fmt.Errorf("maxQueues is not a number: %v", err))
}
config.MaxQueues = m
}
}
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
}