-
Notifications
You must be signed in to change notification settings - Fork 426
/
main.go
288 lines (250 loc) · 11.7 KB
/
main.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
// mongodb_exporter
// Copyright (C) 2017 Percona LLC
//
// 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"
"net"
"net/url"
"regexp"
"strings"
"github.com/alecthomas/kong"
"github.com/sirupsen/logrus"
"github.com/percona/mongodb_exporter/exporter"
)
//nolint:gochecknoglobals
var (
version string
commit string
buildDate string
)
// GlobalFlags has command line flags to configure the exporter.
type GlobalFlags struct {
User string `name:"mongodb.user" help:"monitor user, need clusterMonitor role in admin db and read role in local db" env:"MONGODB_USER" placeholder:"monitorUser"`
Password string `name:"mongodb.password" help:"monitor user password" env:"MONGODB_PASSWORD" placeholder:"monitorPassword"`
CollStatsNamespaces string `name:"mongodb.collstats-colls" help:"List of comma separared databases.collections to get $collStats" placeholder:"db1,db2.col2"`
IndexStatsCollections string `name:"mongodb.indexstats-colls" help:"List of comma separared databases.collections to get $indexStats" placeholder:"db1.col1,db2.col2"`
URI []string `name:"mongodb.uri" help:"MongoDB connection URI" env:"MONGODB_URI" placeholder:"mongodb://user:[email protected]:27017/admin?ssl=true"`
GlobalConnPool bool `name:"mongodb.global-conn-pool" help:"Use global connection pool instead of creating new pool for each http request." negatable:""`
DirectConnect bool `name:"mongodb.direct-connect" help:"Whether or not a direct connect should be made. Direct connections are not valid if multiple hosts are specified or an SRV URI is used." default:"true" negatable:""`
WebListenAddress string `name:"web.listen-address" help:"Address to listen on for web interface and telemetry" default:":9216"`
WebTelemetryPath string `name:"web.telemetry-path" help:"Metrics expose path" default:"/metrics"`
TLSConfigPath string `name:"web.config" help:"Path to the file having Prometheus TLS config for basic auth"`
TimeoutOffset int `name:"web.timeout-offset" help:"Offset to subtract from the request timeout in seconds" default:"1"`
LogLevel string `name:"log.level" help:"Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]" enum:"debug,info,warn,error,fatal" default:"error"`
ConnectTimeoutMS int `name:"mongodb.connect-timeout-ms" help:"Connection timeout in milliseconds" default:"5000"`
EnableExporterMetrics bool `name:"collector.exporter-metrics" help:"Enable collecting metrics about the exporter itself (process_*, go_*)" negatable:"" default:"True"`
EnableDiagnosticData bool `name:"collector.diagnosticdata" help:"Enable collecting metrics from getDiagnosticData"`
EnableReplicasetStatus bool `name:"collector.replicasetstatus" help:"Enable collecting metrics from replSetGetStatus"`
EnableDBStats bool `name:"collector.dbstats" help:"Enable collecting metrics from dbStats"`
EnableDBStatsFreeStorage bool `name:"collector.dbstatsfreestorage" help:"Enable collecting free space metrics from dbStats"`
EnableTopMetrics bool `name:"collector.topmetrics" help:"Enable collecting metrics from top admin command"`
EnableCurrentopMetrics bool `name:"collector.currentopmetrics" help:"Enable collecting metrics currentop admin command"`
EnableIndexStats bool `name:"collector.indexstats" help:"Enable collecting metrics from $indexStats"`
EnableCollStats bool `name:"collector.collstats" help:"Enable collecting metrics from $collStats"`
EnableProfile bool `name:"collector.profile" help:"Enable collecting metrics from profile"`
EnableFCV bool `name:"collector.fcv" help:"Enable Feature Compatibility Version collector"`
EnableShards bool `help:"Enable collecting metrics from sharded Mongo clusters about chunks" name:"collector.shards"`
EnablePBM bool `help:"Enable collecting metrics from Percona Backup for MongoDB" name:"collector.pbm"`
EnableOverrideDescendingIndex bool `name:"metrics.overridedescendingindex" help:"Enable descending index name override to replace -1 with _DESC"`
CollectAll bool `name:"collect-all" help:"Enable all collectors. Same as specifying all --collector.<name>"`
CollStatsLimit int `name:"collector.collstats-limit" help:"Disable collstats, dbstats, topmetrics and indexstats collector if there are more than <n> collections. 0=No limit" default:"0"`
ProfileTimeTS int `name:"collector.profile-time-ts" help:"Set time for scrape slow queries." default:"30"`
CurrentOpSlowTime string `name:"collector.currentopmetrics-slow-time" help:"Set minimum time for registration queries." default:"1m"`
DiscoveringMode bool `name:"discovering-mode" help:"Enable autodiscover collections" negatable:""`
CompatibleMode bool `name:"compatible-mode" help:"Enable old mongodb-exporter compatible metrics" negatable:""`
Version bool `name:"version" help:"Show version and exit"`
SplitCluster bool `name:"split-cluster" help:"Treat each node in cluster as a separate target" negatable:"" default:"false"`
}
func main() {
var opts GlobalFlags
ctx := kong.Parse(&opts,
kong.Name("mongodb_exporter"),
kong.Description("MongoDB Prometheus exporter"),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
kong.Vars{
"version": version,
})
if opts.Version {
fmt.Println("mongodb_exporter - MongoDB Prometheus exporter")
fmt.Printf("Version: %s\n", version)
fmt.Printf("Commit: %s\n", commit)
fmt.Printf("Build date: %s\n", buildDate)
return
}
log := logrus.New()
levels := map[string]logrus.Level{
"debug": logrus.DebugLevel,
"error": logrus.ErrorLevel,
"fatal": logrus.FatalLevel,
"info": logrus.InfoLevel,
"warn": logrus.WarnLevel,
}
log.SetLevel(levels[opts.LogLevel])
log.Debugf("Compatible mode: %v", opts.CompatibleMode)
if opts.WebTelemetryPath == "" {
log.Warn("Web telemetry path \"\" invalid, falling back to \"/\" instead")
opts.WebTelemetryPath = "/"
}
if len(opts.URI) == 0 {
ctx.Fatalf("No MongoDB hosts were specified. You must specify the host(s) with the --mongodb.uri command argument or the MONGODB_URI environment variable")
}
if opts.TimeoutOffset <= 0 {
log.Warn("Timeout offset needs to be greater than \"0\", falling back to \"1\". You can specify the timout offset with --web.timeout-offset command argument")
opts.TimeoutOffset = 1
}
serverOpts := &exporter.ServerOpts{
Path: opts.WebTelemetryPath,
MultiTargetPath: "/scrape",
OverallTargetPath: "/scrapeall",
WebListenAddress: opts.WebListenAddress,
TLSConfigPath: opts.TLSConfigPath,
}
exporter.RunWebServer(serverOpts, buildServers(opts, log), log)
}
func buildExporter(opts GlobalFlags, uri string, log *logrus.Logger) *exporter.Exporter {
uri = buildURI(uri, opts.User, opts.Password, log)
log.Debugf("Connection URI: %s", uri)
uriParsed, _ := url.Parse(uri)
var nodeName string
if uriParsed.Port() != "" {
nodeName = net.JoinHostPort(uriParsed.Hostname(), uriParsed.Port())
} else {
nodeName = uriParsed.Host
}
exporterOpts := &exporter.Opts{
CollStatsNamespaces: strings.Split(opts.CollStatsNamespaces, ","),
CompatibleMode: opts.CompatibleMode,
DiscoveringMode: opts.DiscoveringMode,
IndexStatsCollections: strings.Split(opts.IndexStatsCollections, ","),
Logger: log,
URI: uri,
NodeName: nodeName,
GlobalConnPool: opts.GlobalConnPool,
DirectConnect: opts.DirectConnect,
ConnectTimeoutMS: opts.ConnectTimeoutMS,
TimeoutOffset: opts.TimeoutOffset,
DisableDefaultRegistry: !opts.EnableExporterMetrics,
EnableDiagnosticData: opts.EnableDiagnosticData,
EnableReplicasetStatus: opts.EnableReplicasetStatus,
EnableCurrentopMetrics: opts.EnableCurrentopMetrics,
EnableTopMetrics: opts.EnableTopMetrics,
EnableDBStats: opts.EnableDBStats,
EnableDBStatsFreeStorage: opts.EnableDBStatsFreeStorage,
EnableIndexStats: opts.EnableIndexStats,
EnableCollStats: opts.EnableCollStats,
EnableProfile: opts.EnableProfile,
EnableShards: opts.EnableShards,
EnableFCV: opts.EnableFCV,
EnablePBMMetrics: opts.EnablePBM,
EnableOverrideDescendingIndex: opts.EnableOverrideDescendingIndex,
CollStatsLimit: opts.CollStatsLimit,
CollectAll: opts.CollectAll,
ProfileTimeTS: opts.ProfileTimeTS,
CurrentOpSlowTime: opts.CurrentOpSlowTime,
}
e := exporter.New(exporterOpts)
return e
}
func buildServers(opts GlobalFlags, logger *logrus.Logger) []*exporter.Exporter {
URIs := parseURIList(opts.URI, logger, opts.SplitCluster)
servers := make([]*exporter.Exporter, len(URIs))
for serverIdx := range URIs {
servers[serverIdx] = buildExporter(opts, URIs[serverIdx], logger)
}
return servers
}
func parseURIList(uriList []string, logger *logrus.Logger, splitCluster bool) []string {
var URIs []string
// If server URI is prefixed with mongodb scheme string, then every next URI in
// line not prefixed with mongodb scheme string is a part of cluster. Otherwise
// treat it as a standalone server
realURI := ""
matchRegexp := regexp.MustCompile(`^mongodb(\+srv)?://`)
for _, URI := range uriList {
matches := matchRegexp.FindStringSubmatch(URI)
if matches != nil {
if realURI != "" {
// Add the previous host buffer to the url list as we met the scheme part
URIs = append(URIs, realURI)
realURI = ""
}
if matches[1] == "" {
realURI = URI
} else {
// There can be only one host in SRV connection string
if splitCluster {
// In splitCluster mode we get srv connection string from SRV recors
URI = exporter.GetSeedListFromSRV(URI, logger)
}
URIs = append(URIs, URI)
}
} else {
if realURI == "" {
URIs = append(URIs, "mongodb://"+URI)
} else {
realURI += "," + URI
}
}
}
if realURI != "" {
URIs = append(URIs, realURI)
}
if splitCluster {
// In this mode we split cluster strings into separate targets
separateURIs := []string{}
for _, hosturl := range URIs {
urlParsed, err := url.Parse(hosturl)
if err != nil {
logger.Fatal(fmt.Sprintf("Failed to parse URI %s: %v", hosturl, err))
}
for _, host := range strings.Split(urlParsed.Host, ",") {
targetURI := "mongodb://"
if urlParsed.User != nil {
targetURI += urlParsed.User.String() + "@"
}
targetURI += host
if urlParsed.Path != "" {
targetURI += urlParsed.Path
}
if urlParsed.RawQuery != "" {
targetURI += "?" + urlParsed.RawQuery
}
separateURIs = append(separateURIs, targetURI)
}
}
return separateURIs
}
return URIs
}
func buildURI(uri string, user string, password string, log *logrus.Logger) string {
defaultPrefix := "mongodb://" // default prefix
matchRegexp := regexp.MustCompile(`^mongodb(\+srv)?://`)
// Split the uri defaultPrefix if there is any
if !matchRegexp.MatchString(uri) {
uri = defaultPrefix + uri
}
parsedURI, err := url.Parse(uri)
if err != nil {
log.Fatalf("Failed to parse URI %s: %v", uri, err)
return uri
}
if parsedURI.User == nil && user != "" && password != "" {
parsedURI.User = url.UserPassword(user, password)
}
return parsedURI.String()
}