-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (111 loc) · 4.95 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
// mongodb_exporter
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"log"
"strings"
"github.com/alecthomas/kong"
"github.com/sirupsen/logrus"
"github.com/johnteoKheng/mongodb_exporter-0.20.7/exporter"
)
//nolint:gochecknoglobals
var (
version string
commit string
buildDate string
)
// GlobalFlags has command line flags to configure the exporter.
type GlobalFlags struct {
CollStatsCollections string `name:"mongodb.collstats-colls" help:"List of comma separared databases.collections to get $collStats" placeholder:"db1.col1,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:pass@127.0.0.1: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."`
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"`
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"`
LogLevel string `name:"log.level" help:"Only log messages with the given severuty or above. Valid levels: [debug, info, warn, error, fatal]" enum:"debug,info,warn,error,fatal" default:"error"`
DisableDiagnosticData bool `name:"disable.diagnosticdata" help:"Disable collecting metrics from getDiagnosticData"`
DisableReplicasetStatus bool `name:"disable.replicasetstatus" help:"Disable collecting metrics from replSetGetStatus"`
DiscoveringMode bool `name:"discovering-mode" help:"Enable autodiscover collections"`
CompatibleMode bool `name:"compatible-mode" help:"Enable old mongodb-exporter compatible metrics"`
Version bool `name:"version" help:"Show version and exit"`
WebSslCertFile string `name:"web.ssl-cert-file" help:"Path to PEM file that conains the certificate"`
WebSslKeyFile string `name:"web.ssl-key-file" help:"Path to PEM file that conains the key"`
}
func main() {
var opts GlobalFlags
_ = 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
}
e, err := buildExporter(opts)
if err != nil {
log.Fatal(err)
}
e.Run()
}
func buildExporter(opts GlobalFlags) (*exporter.Exporter, error) {
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 !strings.HasPrefix(opts.URI, "mongodb") {
log.Debugf("Prepending mongodb:// to the URI")
opts.URI = "mongodb://" + opts.URI
}
log.Debugf("Connection URI: %s", opts.URI)
exporterOpts := &exporter.Opts{
CollStatsCollections: strings.Split(opts.CollStatsCollections, ","),
CompatibleMode: opts.CompatibleMode,
DiscoveringMode: opts.DiscoveringMode,
IndexStatsCollections: strings.Split(opts.IndexStatsCollections, ","),
Logger: log,
Path: opts.WebTelemetryPath,
URI: opts.URI,
GlobalConnPool: opts.GlobalConnPool,
WebListenAddress: opts.WebListenAddress,
DisableDiagnosticData: opts.DisableDiagnosticData,
DisableReplicasetStatus: opts.DisableReplicasetStatus,
DirectConnect: opts.DirectConnect,
WebSslCertFile: opts.WebSslCertFile,
WebSslKeyFile: opts.WebSslKeyFile,
}
e, err := exporter.New(exporterOpts)
if err != nil {
return nil, err
}
return e, nil
}