1
+ // mongodb_exporter
2
+ // Copyright (C) 2017 Percona LLC
3
+ //
4
+ // This program is free software: you can redistribute it and/or modify
5
+ // it under the terms of the GNU Affero General Public License as published by
6
+ // the Free Software Foundation, either version 3 of the License, or
7
+ // (at your option) any later version.
8
+ //
9
+ // This program is distributed in the hope that it will be useful,
10
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ // GNU Affero General Public License for more details.
13
+ //
14
+ // You should have received a copy of the GNU Affero General Public License
15
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+
17
+ package exporter
18
+
19
+ import (
20
+ "context"
21
+ "strconv"
22
+ "strings"
23
+ "os"
24
+ "github.com/prometheus/client_golang/prometheus"
25
+ "github.com/sirupsen/logrus"
26
+ "go.mongodb.org/mongo-driver/bson"
27
+ "go.mongodb.org/mongo-driver/mongo"
28
+ )
29
+
30
+ type backupStatusCollector struct {
31
+ ctx context.Context
32
+ base * baseCollector
33
+
34
+ compatibleMode bool
35
+ topologyInfo labelsGetter
36
+ }
37
+
38
+ // newBackupStatusCollector creates a collector for statistics on backup status.
39
+ func newBackupStatusCollector (ctx context.Context , client * mongo.Client , logger * logrus.Logger , compatible bool , topology labelsGetter ) * backupStatusCollector {
40
+ return & backupStatusCollector {
41
+ ctx : ctx ,
42
+ base : newBaseCollector (client , logger ),
43
+ compatibleMode : compatible ,
44
+ topologyInfo : topology ,
45
+ }
46
+ }
47
+
48
+ func (d * backupStatusCollector ) Describe (ch chan <- * prometheus.Desc ) {
49
+ d .base .Describe (d .ctx , ch , d .collect )
50
+ }
51
+
52
+ func (d * backupStatusCollector ) Collect (ch chan <- prometheus.Metric ) {
53
+ d .base .Collect (ch )
54
+ }
55
+
56
+ func (d * backupStatusCollector ) collect (ch chan <- prometheus.Metric ) {
57
+ defer measureCollectTime (ch , "mongodb" , "backupstatus" )()
58
+
59
+ logger := d .base .logger
60
+
61
+ var m bson.M
62
+ var delblank string
63
+ filename := "/tmp/mongodbBakStatus.txt"
64
+ content , err := os .ReadFile (filename )
65
+ if err != nil {
66
+ m = bson.M {"10000" }
67
+ ch <- prometheus .NewInvalidMetric (prometheus .NewInvalidDesc (err ), err )
68
+ return
69
+ }
70
+
71
+ delblank = strings .Replace (string (content ), " " , "" , - 1 )
72
+ delnewline , _ := strconv .ParseFloat (strings .Replace (delblank ,"\n " , "" , - 1 ), 64 )
73
+ if delnewline == 1 {
74
+ m = bson.M {"1" }
75
+ } else {
76
+ m = bson.M {"0" }
77
+ }
78
+
79
+ logger .Debug ("backupStatus result:" )
80
+ debugResult (logger , m )
81
+
82
+ for _ , metric := range makeMetrics ("" , bson.M {"backupStatus" : m }, d .topologyInfo .baseLabels (), d .compatibleMode ) {
83
+ ch <- metric
84
+ }
85
+ }
0 commit comments