forked from mackerelio/mackerel-agent-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.go
294 lines (268 loc) · 9.42 KB
/
mongodb.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"reflect"
"strconv"
"strings"
mp "github.com/mackerelio/go-mackerel-plugin-helper"
"github.com/mackerelio/mackerel-agent/logging"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var logger = logging.GetLogger("metrics.plugin.mongodb")
var graphdef = map[string](mp.Graphs){
"mongodb.background_flushing": mp.Graphs{
Label: "MongoDB Command",
Unit: "float",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "duration_ms", Label: "Duration in ms", Diff: true, Type: "uint64"},
},
},
"mongodb.connections": mp.Graphs{
Label: "MongoDB Connections",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "connections_current", Label: "current"},
},
},
"mongodb.index_counters.btree": mp.Graphs{
Label: "MongoDB Index Counters Btree",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "btree_hits", Label: "hits", Diff: true, Type: "uint64"},
},
},
"mongodb.opcounters": mp.Graphs{
Label: "MongoDB opcounters",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "opcounters_insert", Label: "Insert", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_query", Label: "Query", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_update", Label: "Update", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_delete", Label: "Delete", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_getmore", Label: "Getmore", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_command", Label: "Command", Diff: true, Type: "uint64"},
},
},
}
var graphdef30 = map[string](mp.Graphs){
"mongodb.background_flushing": mp.Graphs{
Label: "MongoDB Command",
Unit: "float",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "duration_ms", Label: "Duration in ms", Diff: true, Type: "uint64"},
},
},
"mongodb.connections": mp.Graphs{
Label: "MongoDB Connections",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "connections_current", Label: "current"},
},
},
"mongodb.opcounters": mp.Graphs{
Label: "MongoDB opcounters",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "opcounters_insert", Label: "Insert", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_query", Label: "Query", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_update", Label: "Update", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_delete", Label: "Delete", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_getmore", Label: "Getmore", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_command", Label: "Command", Diff: true, Type: "uint64"},
},
},
}
var graphdef32 = map[string](mp.Graphs){
"mongodb.connections": mp.Graphs{
Label: "MongoDB Connections",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "connections_current", Label: "current"},
},
},
"mongodb.opcounters": mp.Graphs{
Label: "MongoDB opcounters",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "opcounters_insert", Label: "Insert", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_query", Label: "Query", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_update", Label: "Update", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_delete", Label: "Delete", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_getmore", Label: "Getmore", Diff: true, Type: "uint64"},
mp.Metrics{Name: "opcounters_command", Label: "Command", Diff: true, Type: "uint64"},
},
},
}
var metricPlace22 = map[string][]string{
"duration_ms": []string{"backgroundFlushing", "total_ms"},
"connections_current": []string{"connections", "current"},
"btree_hits": []string{"indexCounters", "btree", "hits"},
"opcounters_insert": []string{"opcounters", "insert"},
"opcounters_query": []string{"opcounters", "query"},
"opcounters_update": []string{"opcounters", "update"},
"opcounters_delete": []string{"opcounters", "delete"},
"opcounters_getmore": []string{"opcounters", "getmore"},
"opcounters_command": []string{"opcounters", "command"},
}
var metricPlace24 = map[string][]string{
"duration_ms": []string{"backgroundFlushing", "total_ms"},
"connections_current": []string{"connections", "current"},
"btree_hits": []string{"indexCounters", "hits"},
"opcounters_insert": []string{"opcounters", "insert"},
"opcounters_query": []string{"opcounters", "query"},
"opcounters_update": []string{"opcounters", "update"},
"opcounters_delete": []string{"opcounters", "delete"},
"opcounters_getmore": []string{"opcounters", "getmore"},
"opcounters_command": []string{"opcounters", "command"},
}
// indexCounters is removed from mongodb 3.0.
// ref. http://stackoverflow.com/questions/29428793/where-is-the-indexcounter-in-db-serverstatus-on-mongodb-3-0
var metricPlace30 = map[string][]string{
"duration_ms": []string{"backgroundFlushing", "total_ms"},
"connections_current": []string{"connections", "current"},
"opcounters_insert": []string{"opcounters", "insert"},
"opcounters_query": []string{"opcounters", "query"},
"opcounters_update": []string{"opcounters", "update"},
"opcounters_delete": []string{"opcounters", "delete"},
"opcounters_getmore": []string{"opcounters", "getmore"},
"opcounters_command": []string{"opcounters", "command"},
}
// backgroundFlushing information only appears for instances that use the MMAPv1 storage engine.
// and the MMAPv1 is no longer the default storage engine in MongoDB 3.2
// ref. https://docs.mongodb.org/manual/reference/command/serverStatus/#server-status-backgroundflushing
var metricPlace32 = map[string][]string{
"connections_current": []string{"connections", "current"},
"opcounters_insert": []string{"opcounters", "insert"},
"opcounters_query": []string{"opcounters", "query"},
"opcounters_update": []string{"opcounters", "update"},
"opcounters_delete": []string{"opcounters", "delete"},
"opcounters_getmore": []string{"opcounters", "getmore"},
"opcounters_command": []string{"opcounters", "command"},
}
func getFloatValue(s map[string]interface{}, keys []string) (float64, error) {
var val float64
sm := s
var err error
for i, k := range keys {
if i+1 < len(keys) {
switch sm[k].(type) {
case bson.M:
sm = sm[k].(bson.M)
default:
return 0, fmt.Errorf("Cannot handle as a hash for %s", k)
}
} else {
val, err = strconv.ParseFloat(fmt.Sprint(sm[k]), 64)
if err != nil {
return 0, err
}
}
}
return val, nil
}
// MongoDBPlugin mackerel plugin for mongo
type MongoDBPlugin struct {
URL string
Verbose bool
}
func (m MongoDBPlugin) fetchStatus() (bson.M, error) {
session, err := mgo.Dial(m.URL)
if err != nil {
return nil, err
}
defer session.Close()
serverStatus := bson.M{}
if err := session.Run("serverStatus", &serverStatus); err != nil {
return nil, err
}
if m.Verbose {
str, err := json.Marshal(serverStatus)
if err != nil {
fmt.Println(fmt.Errorf("Marshaling error: %s", err.Error()))
}
fmt.Println(string(str))
}
return serverStatus, nil
}
// FetchMetrics interface for mackerelplugin
func (m MongoDBPlugin) FetchMetrics() (map[string]interface{}, error) {
serverStatus, err := m.fetchStatus()
if err != nil {
return nil, err
}
return m.parseStatus(serverStatus)
}
func (m MongoDBPlugin) getVersion(serverStatus bson.M) string {
if reflect.TypeOf(serverStatus["version"]).String() == "string" {
version := serverStatus["version"].(string)
return version
}
return ""
}
func (m MongoDBPlugin) parseStatus(serverStatus bson.M) (map[string]interface{}, error) {
stat := make(map[string]interface{})
metricPlace := &metricPlace22
version := m.getVersion(serverStatus)
if strings.HasPrefix(version, "2.4") {
metricPlace = &metricPlace24
} else if strings.HasPrefix(version, "2.6") {
metricPlace = &metricPlace24
} else if strings.HasPrefix(version, "3.0") {
metricPlace = &metricPlace30
} else if strings.HasPrefix(version, "3.2") {
metricPlace = &metricPlace32
}
for k, v := range *metricPlace {
val, err := getFloatValue(serverStatus, v)
if err != nil {
logger.Warningf("Cannot fetch metric %s: %s", v, err)
}
stat[k] = val
}
return stat, nil
}
// GraphDefinition interface for mackerelplugin
func (m MongoDBPlugin) GraphDefinition() map[string](mp.Graphs) {
serverStatus, err := m.fetchStatus()
if err != nil {
return graphdef
}
version := m.getVersion(serverStatus)
if strings.HasPrefix(version, "3.0") {
return graphdef30
} else if strings.HasPrefix(version, "3.2") {
return graphdef32
}
return graphdef
}
func main() {
optHost := flag.String("host", "localhost", "Hostname")
optPort := flag.String("port", "27017", "Port")
optUser := flag.String("username", "", "Username")
optPass := flag.String("password", "", "Password")
optVerbose := flag.Bool("v", false, "Verbose mode")
optTempfile := flag.String("tempfile", "", "Temp file name")
flag.Parse()
var mongodb MongoDBPlugin
mongodb.Verbose = *optVerbose
if *optUser == "" && *optPass == "" {
mongodb.URL = fmt.Sprintf("mongodb://%s:%s", *optHost, *optPort)
} else {
mongodb.URL = fmt.Sprintf("mongodb://%s:%s@%s:%s", *optUser, *optPass, *optHost, *optPort)
}
helper := mp.NewMackerelPlugin(mongodb)
if *optTempfile != "" {
helper.Tempfile = *optTempfile
} else {
helper.Tempfile = fmt.Sprintf("/tmp/mackerel-plugin-mongodb-%s-%s", *optHost, *optPort)
}
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
helper.OutputDefinitions()
} else {
helper.OutputValues()
}
}