-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathreplset_conf.go
144 lines (129 loc) · 3.89 KB
/
replset_conf.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
package collector
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)
var (
memberHidden = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: subsystem,
Name: "member_hidden",
Help: "This field conveys if the member is hidden (1) or not-hidden (0).",
}, []string{"id", "host"})
memberArbiter = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: subsystem,
Name: "member_arbiter",
Help: "This field conveys if the member is an arbiter (1) or not (0).",
}, []string{"id", "host"})
memberBuildIndexes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: subsystem,
Name: "member_build_indexes",
Help: "This field conveys if the member is builds indexes (1) or not (0).",
}, []string{"id", "host"})
memberPriority = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: subsystem,
Name: "member_priority",
Help: "This field conveys the priority of a given member",
}, []string{"id", "host"})
memberVotes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: subsystem,
Name: "member_votes",
Help: "This field conveys the number of votes of a given member",
}, []string{"id", "host"})
)
// Although the docs say that it returns a map with id etc. it *actually* returns
// that wrapped in a map
type OuterReplSetConf struct {
Config ReplSetConf `bson:"config"`
}
// ReplSetConf keeps the data returned by the GetReplSetConf method
type ReplSetConf struct {
Id string `bson:"_id"`
Version int `bson:"version"`
Members []MemberConf `bson:"members"`
}
/*
Example:
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 5000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
*/
type ReplSetConfSettings struct {
}
// Member represents an array element of ReplSetConf.Members
type MemberConf struct {
Id int32 `bson:"_id"`
Host string `bson:"host"`
ArbiterOnly bool `bson:"arbiterOnly"`
BuildIndexes bool `bson:"buildIndexes"`
Hidden bool `bson:"hidden"`
Priority int32 `bson:"priority"`
Tags map[string]string `bson:"tags"`
SlaveDelay float64 `bson:"saveDelay"`
Votes int32 `bson:"votes"`
}
// Export exports the replSetGetStatus stati to be consumed by prometheus
func (replConf *ReplSetConf) Export(ch chan<- prometheus.Metric) {
for _, member := range replConf.Members {
ls := prometheus.Labels{
"id": replConf.Id,
"host": member.Host,
}
if member.Hidden {
memberHidden.With(ls).Set(1)
} else {
memberHidden.With(ls).Set(0)
}
if member.ArbiterOnly {
memberArbiter.With(ls).Set(1)
} else {
memberArbiter.With(ls).Set(0)
}
if member.BuildIndexes {
memberBuildIndexes.With(ls).Set(1)
} else {
memberBuildIndexes.With(ls).Set(0)
}
memberPriority.With(ls).Set(float64(member.Priority))
memberVotes.With(ls).Set(float64(member.Votes))
}
// collect metrics
memberHidden.Collect(ch)
memberArbiter.Collect(ch)
memberBuildIndexes.Collect(ch)
memberPriority.Collect(ch)
memberVotes.Collect(ch)
}
// Describe describes the replSetGetStatus metrics for prometheus
func (replConf *ReplSetConf) Describe(ch chan<- *prometheus.Desc) {
memberHidden.Describe(ch)
memberArbiter.Describe(ch)
memberBuildIndexes.Describe(ch)
memberPriority.Describe(ch)
memberVotes.Describe(ch)
}
// GetReplSetConf returns the replica status info
func GetReplSetConf(session *mgo.Session) *ReplSetConf {
result := &OuterReplSetConf{}
err := session.DB("admin").Run(bson.D{{"replSetGetConfig", 1}}, result)
if err != nil {
glog.Error("Failed to get replSet config.")
return nil
}
return &result.Config
}