/
mongodb.py
290 lines (252 loc) · 10.3 KB
/
mongodb.py
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
#
# Plugin to collectd statistics from MongoDB
#
import collectd
import types
from pymongo import Connection
from distutils.version import StrictVersion as V
from copy import deepcopy
# maps that duplicate the key/nested-key structure for returned data from mongodb commands but mapping
# to values that are a tuple containing the value's collectd type (types.db)
# and optionally the collectd value name (defaults to nested key path to the leaf value with key strings joined with ".")
# NOTE: some metrics are more complicated and thus are handled specially (ex. connPoolStats hosts data)
CONNECTION_POOL_STATUS_METRICS = {
# db.runCommand({connPoolStats:1})
# hosts and replicaSets handled specially
"createdByType": {
"master": "total_connections",
"set": "total_connections",
"sync": "total_connections"
},
"totalAvailable": "gauge",
"totalCreated": "total_connections",
"numDBClientConnection": "gauge",
"numAScopedConnection": "gauge"
}
DBSTATS_METRICS = {
# db.runCommand({dbStats:1})
"collections": "gauge",
"objects" : "gauge",
"avgObjSize" : "bytes",
"dataSize" : "bytes",
"storageSize" : "bytes",
"numExtents" : "gauge",
"indexes" : "gauge",
"indexSize" : "bytes",
"fileSize" : "bytes",
"nsSizeMB": "gauge"
}
SERVER_STATUS_METRICS = {
# db.runCommand({serverStatus:1})
"backgroundFlushing": {
"last_ms": "gauge"
},
"connections": {
"current": "gauge",
"available": "gauge"
},
"cursors": {
"totalOpen": "gauge",
"timedout": "derive"
},
"globalLock": {
"currentQueue" : {
"total" : "gauge",
"readers" : "gauge",
"writers" : "gauge"
},
"activeClients" : {
"total" : "gauge",
"readers" : "gauge",
"writers" : "gauge"
}
},
"indexCounters" : {
"accesses" : "derive",
"hits" : "derive",
"misses" : "derive"
},
"locks": {
"." : {
"timeLockedMicros" : {
"R" : "derive",
"W" : "derive"
},
"timeAcquiringMicros" : {
"R" : "derive",
"W" : "derive"
}
}
# special handling for other configured DBs
},
"opcounters": {
"insert" : "derive",
"query" : "derive",
"update" : "derive",
"delete" : "derive",
"getmore" : "derive",
"command" : "derive"
},
"recordStats": {
"accessesNotInMemory" : "derive",
"pageFaultExceptionsThrown" : "derive"
# do we want db specificity?
},
"mem": {
"bits" : "gauge",
"resident" : "gauge",
"virtual" : "gauge",
"mapped" : "gauge",
"mappedWithJournal" : "gauge"
},
"metrics" : {
"document" : {
"deleted" : "derive",
"inserted" : "derive",
"returned" : "derive",
"updated" : "derive"
}
}
}
class MongoDB(object):
def __init__(self):
self.plugin_name = "mongo"
self.mongo_host = "127.0.0.1"
self.mongo_port = 27017
self.mongo_dbs = ["admin", ]
self.mongo_user = None
self.mongo_password = None
self.includeConnPoolMetrics = None
self.includeServerStatsMetrics = None
self.includeDbstatsMetrics = None
def connect(self):
global SERVER_STATUS_METRICS
self.mongo_client = Connection(host=self.mongo_host, port=self.mongo_port, slave_okay=True)
if not self.mongo_client.alive():
collectd.error("mongodb plugin failed to connect to %s:%d" % (self.mongo_host, self.mongo_port))
server_status = self.mongo_client[self.mongo_dbs[0]].command("serverStatus")
version = server_status["version"]
at_least_2_4 = V(version) >= V("2.4.0")
if not at_least_2_4:
indexCountersMap = SERVER_STATUS_METRICS.pop("indexCounters")
SERVER_STATUS_METRICS["indexCounters"] = {"btree": indexCountersMap}
def disconnect(self):
if self.mongo_client and self.mongo_client.alive():
self.mongo_client.disconnect()
def config(self, obj):
for node in obj.children:
if node.key == "Port":
self.mongo_port = int(node.values[0])
collectd.info("mongodb plugin: Port " + self.mongo_port)
elif node.key == "Host":
self.mongo_host = node.values[0]
collectd.info("mongodb plugin: Host " + self.Host)
elif node.key == "User":
self.mongo_user = node.values[0]
elif node.key == "Password":
self.mongo_password = node.values[0]
elif node.key == "Databases":
self.mongo_dbs = node.values
collectd.info("mongodb plugin: Databases " + self.mongo_dbs)
elif node.key == "ConnectionPoolStatus":
self.includeConnPoolMetrics = node.values
collectd.info("mongodb plugin: ConnectionPoolStatus " + self.ConnectionPoolStatus)
elif node.key == "ServerStats":
self.includeServerStatsMetrics = node.values
collectd.info("mongodb plugin: ServerStats " + self.ServerStats)
elif node.key == "DBStats":
self.includeDbstatsMetrics = node.values
collectd.info("mongodb plugin: DBStats " + self.DBStats)
else:
collectd.warning("mongodb plugin: Unkown configuration key %s" % node.key)
def submit(self, instance, type, value, db=None):
# actually a recursive submit call to dive deeper into nested dict data
# since the leaf value in the nested dicts is the type, we check on the type type :-)
if db:
plugin_instance = "%s-%s" % (self.mongo_port, db)
else:
plugin_instance = str(self.mongo_port)
v = collectd.Values()
v.plugin = self.plugin_name
v.plugin_instance = plugin_instance
v.type = type
v.type_instance = instance
v.values = [value, ]
v.dispatch()
def recursive_submit(self, type_tree, data_tree, instance_name=None, db=None):
# if we are still in the middle of the type and data tree
if isinstance(type_tree, types.DictType) and isinstance(data_tree, types.DictType):
for type_name, type_value in type_tree.iteritems():
next_instance_name = None
if instance_name:
next_instance_name = instance_name + "." + type_name
else:
next_instance_name = type_name
if data_tree.has_key(type_name):
self.recursive_submit(type_value, data_tree[type_name], next_instance_name, db=db)
else:
# may want to log this but some mongodb setups may not have anything to report
pass
elif isinstance(type_tree, types.DictType) or isinstance(data_tree, types.DictType):
print("type tree and data tree structure differ for data instance: " + instance_name)
else:
self.submit(instance_name, type_tree, data_tree, db)
def publish_connection_pool_metrics(self):
# connPoolStats produces the same results regardless of db used
db = self.mongo_client[self.mongo_dbs[0]]
if self.mongo_user and self.mongo_password:
db.authenticate(self.mongo_user, self.mongo_password)
conn_pool_stats = db.command("connPoolStats")
metrics_to_collect = {}
if self.includeConnPoolMetrics:
for root_metric_key in self.includeConnPoolMetrics.iterkeys():
if conn_pool_stats.has_key(root_metric_key):
metrics_to_collect[root_metric_key] = deepcopy(CONNECTION_POOL_STATUS_METRICS[root_metric_key])
else:
metrics_to_collect = CONNECTION_POOL_STATUS_METRICS
self.recursive_submit(metrics_to_collect, conn_pool_stats)
def publish_dbstats(self):
for db_name in self.mongo_dbs:
db = self.mongo_client[db_name]
if self.mongo_user and self.mongo_password:
db.authenticate(self.mongo_user, self.mongo_password)
dbstats = db.command("dbStats")
metrics_to_collect = {}
if self.includeDbstatsMetrics:
for root_metric_key in self.includeDbstatsMetrics.iterkeys():
if dbstats.has_key(root_metric_key):
metrics_to_collect[root_metric_key] = deepcopy(DBSTATS_METRICS[root_metric_key])
else:
metrics_to_collect = DBSTATS_METRICS
self.recursive_submit(metrics_to_collect, dbstats, db=db_name)
def publish_server_status(self):
# serverStatus produces the same results regardless of db used
db = self.mongo_client[self.mongo_dbs[0]]
if self.mongo_user and self.mongo_password:
db.authenticate(self.mongo_user, self.mongo_password)
server_status = db.command("serverStatus")
metrics_to_collect = {}
if self.includeServerStatsMetrics:
for root_metric_key in self.includeServerStatsMetrics.iterkeys():
if server_status.has_key(root_metric_key):
metrics_to_collect[root_metric_key] = deepcopy(SERVER_STATUS_METRICS[root_metric_key])
else:
metrics_to_collect = deepcopy(SERVER_STATUS_METRICS)
# rename "." lock to be "GLOBAL"
if metrics_to_collect["locks"].has_key("."):
print(SERVER_STATUS_METRICS["locks"])
global_lock_data = metrics_to_collect["locks"].pop(".")
metrics_to_collect["locks"]["GLOBAL"] = global_lock_data
print(SERVER_STATUS_METRICS["locks"])
for db_name in self.mongo_dbs:
metrics_to_collect["locks"][db_name] = deepcopy(SERVER_STATUS_METRICS["locks"]["."])
self.recursive_submit(metrics_to_collect, server_status)
def publish_data(self):
self.publish_server_status()
self.publish_connection_pool_metrics()
self.publish_dbstats()
mongodb = MongoDB()
collectd.register_read(mongodb.publish_data)
collectd.register_config(mongodb.config)
collectd.register_init(mongodb.connect)
collectd.register_shutdown(mongodb.disconnect)