@@ -26,6 +26,12 @@ import (
2626 "go.mongodb.org/mongo-driver/mongo"
2727)
2828
29+ const (
30+ kmipEncryption = "kmip"
31+ vaultEncryption = "vault"
32+ localKeyFileEncryption = "localKeyFile"
33+ )
34+
2935type diagnosticDataCollector struct {
3036 ctx context.Context
3137 base * baseCollector
@@ -89,6 +95,13 @@ func (d *diagnosticDataCollector) collect(ch chan<- prometheus.Metric) {
8995 metrics := makeMetrics ("" , m , d .topologyInfo .baseLabels (), d .compatibleMode )
9096 metrics = append (metrics , locksMetrics (logger , m )... )
9197
98+ securityMetric , err := d .getSecurityMetricFromLineOptions (client )
99+ if err != nil {
100+ logger .Errorf ("cannot decode getCmdLineOtpions: %s" , err )
101+ } else if securityMetric != nil {
102+ metrics = append (metrics , securityMetric )
103+ }
104+
92105 if d .compatibleMode {
93106 metrics = append (metrics , specialMetrics (d .ctx , client , m , logger )... )
94107
@@ -111,5 +124,67 @@ func (d *diagnosticDataCollector) collect(ch chan<- prometheus.Metric) {
111124 }
112125}
113126
127+ func (d * diagnosticDataCollector ) getSecurityMetricFromLineOptions (client * mongo.Client ) (prometheus.Metric , error ) {
128+ var cmdLineOpionsBson bson.M
129+ cmdLineOptions := bson.D {{Key : "getCmdLineOpts" , Value : "1" }}
130+ resCmdLineOptions := client .Database ("admin" ).RunCommand (d .ctx , cmdLineOptions )
131+ if resCmdLineOptions .Err () != nil {
132+ return nil , errors .Wrap (resCmdLineOptions .Err (), "cannot execute getCmdLineOpts command" )
133+ }
134+ if err := resCmdLineOptions .Decode (& cmdLineOpionsBson ); err != nil {
135+ return nil , errors .Wrap (err , "cannot parse response of the getCmdLineOpts command" )
136+ }
137+
138+ if cmdLineOpionsBson == nil || cmdLineOpionsBson ["parsed" ] == nil {
139+ return nil , errors .New ("cmdlined options is empty" )
140+ }
141+ parsedOptions , ok := cmdLineOpionsBson ["parsed" ].(bson.M )
142+ if ! ok {
143+ return nil , errors .New ("cannot cast parsed options to BSON" )
144+ }
145+ securityOptions , ok := parsedOptions ["security" ].(bson.M )
146+ if ! ok {
147+ return nil , nil
148+ }
149+
150+ metric , err := d .retrieveSecurityEncryptionMetric (securityOptions )
151+ if err != nil {
152+ return nil , err
153+ }
154+
155+ return metric , nil
156+ }
157+
158+ func (d * diagnosticDataCollector ) retrieveSecurityEncryptionMetric (securityOptions bson.M ) (prometheus.Metric , error ) {
159+ _ , ok := securityOptions ["enableEncryption" ]
160+ if ! ok {
161+ return nil , nil
162+ }
163+
164+ var encryptionType string
165+ _ , ok = securityOptions ["kmip" ]
166+ if ok {
167+ encryptionType = kmipEncryption
168+ }
169+ _ , ok = securityOptions ["vault" ]
170+ if ok {
171+ encryptionType = vaultEncryption
172+ }
173+ _ , ok = securityOptions ["encryptionKeyFile" ]
174+ if ok {
175+ encryptionType = localKeyFileEncryption
176+ }
177+
178+ labels := map [string ]string {"type" : encryptionType }
179+ desc := prometheus .NewDesc ("mongodb_security_encryption_enabled" , "Shows that encryption is enabled" ,
180+ nil , labels )
181+ metric , err := prometheus .NewConstMetric (desc , prometheus .GaugeValue , float64 (1 ))
182+ if err != nil {
183+ return nil , errors .Wrap (err , "cannot create metric mongodb_security_encryption_enabled" )
184+ }
185+
186+ return metric , nil
187+ }
188+
114189// check interface.
115190var _ prometheus.Collector = (* diagnosticDataCollector )(nil )
0 commit comments