Skip to content

Commit 7f2c15b

Browse files
committed
add backup status collector
reference: percona/mongodb_exporter#557
1 parent 2bfbb14 commit 7f2c15b

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

exporter/backupstatus_collector.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

exporter/exporter.go

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type Opts struct {
6868
EnableTopMetrics bool
6969
EnableIndexStats bool
7070
EnableCollStats bool
71+
EnableBackupStatus bool
7172

7273
EnableOverrideDescendingIndex bool
7374

@@ -169,6 +170,8 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
169170
e.opts.EnableTopMetrics = true
170171
e.opts.EnableReplicasetStatus = true
171172
e.opts.EnableIndexStats = true
173+
e.opts.EnableBackupStatus = true
174+
172175
}
173176

174177
// arbiter only have isMaster privileges
@@ -290,6 +293,8 @@ func (e *Exporter) Handler() http.Handler {
290293
requestOpts.EnableIndexStats = true
291294
case "collstats":
292295
requestOpts.EnableCollStats = true
296+
case "backupstatus":
297+
requestOpts.EnableBackupStatus = true
293298
}
294299
}
295300

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type GlobalFlags struct {
5151
EnableTopMetrics bool `name:"collector.topmetrics" help:"Enable collecting metrics from top admin command"`
5252
EnableIndexStats bool `name:"collector.indexstats" help:"Enable collecting metrics from $indexStats"`
5353
EnableCollStats bool `name:"collector.collstats" help:"Enable collecting metrics from $collStats"`
54+
EnableBackupStatus bool `name:"collector.backupstatus" help:"Enable collecting metrics from backupStatus"`
5455

5556
EnableOverrideDescendingIndex bool `name:"metrics.overridedescendingindex" help:"Enable descending index name override to replace -1 with _DESC"`
5657

@@ -129,6 +130,7 @@ func buildExporter(opts GlobalFlags) *exporter.Exporter {
129130
EnableDBStats: opts.EnableDBStats,
130131
EnableIndexStats: opts.EnableIndexStats,
131132
EnableCollStats: opts.EnableCollStats,
133+
EnableBackupStatus: opts.EnableBackupStatus,
132134

133135
EnableOverrideDescendingIndex: opts.EnableOverrideDescendingIndex,
134136

0 commit comments

Comments
 (0)