-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmongodb.py
304 lines (263 loc) · 12.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
#
# Plugin to collectd statistics from MongoDB
#
import collectd
import pymongo
from pymongo import Connection
from distutils.version import StrictVersion as V
def set_read_preference(db):
if pymongo.version >= "2.1":
db.read_preference = pymongo.ReadPreference.SECONDARY
def mongo_connect(host=None, port=None, ssl=False, user=None, passwd=None, replica=None):
try:
# ssl connection for pymongo > 2.3
if pymongo.version >= "2.3":
if replica is None:
con = pymongo.MongoClient(host, port)
else:
con = pymongo.Connection(host, port, read_preference=pymongo.ReadPreference.SECONDARY, ssl=ssl, replicaSet=replica, network_timeout=10)
else:
if replica is None:
con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
else:
con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
#con = pymongo.Connection(host, port, slave_okay=True, replicaSet=replica, network_timeout=10)
if user and passwd:
db = con["admin"]
if not db.authenticate(user, passwd):
sys.exit("Username/Password incorrect")
except Exception, e:
if isinstance(e, pymongo.errors.AutoReconnect) and str(e).find(" is an arbiter") != -1:
# We got a pymongo AutoReconnect exception that tells us we connected to an Arbiter Server
# This means: Arbiter is reachable and can answer requests/votes - this is all we need to know from an arbiter
print "OK - State: 7 (Arbiter)"
sys.exit(0)
return exit_with_general_critical(e), None
return 0, con
def replication_get_time_diff(con):
col = 'oplog.rs'
local = con.local
ol = local.system.namespaces.find_one({"name": "local.oplog.$main"})
if ol:
col = 'oplog.$main'
firstc = local[col].find().sort("$natural", 1).limit(1)
lastc = local[col].find().sort("$natural", -1).limit(1)
first = firstc.next()
last = lastc.next()
tfirst = first["ts"]
tlast = last["ts"]
delta = tlast.time - tfirst.time
return delta
class MongoDB(object):
def __init__(self):
self.plugin_name = "mongo"
self.mongo_host = "127.0.0.1"
self.mongo_port = 27017
self.mongo_db = ""
self.mongo_user = None
self.mongo_password = None
self.lockTotalTime = None
self.lockTime = None
self.accesses = None
self.misses = None
def submit(self, type, instance, value, db=None):
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 do_server_status(self):
host = self.mongo_host
port = self.mongo_port
user = self.mongo_user
passwd = self.mongo_password
perf_data = False
con = Connection(host=self.mongo_host, port=self.mongo_port, slave_okay=True)
if not self.mongo_db:
self.mongo_db = con.database_names()
db = con[self.mongo_db[0]]
if self.mongo_user and self.mongo_password:
db.authenticate(self.mongo_user, self.mongo_password)
server_status = db.command('serverStatus')
version = server_status['version']
at_least_2_4 = V(version) >= V('2.4.0')
# operations
for k, v in server_status['opcounters'].items():
self.submit('total_operations', k, v)
# memory
for t in ['resident', 'virtual', 'mapped']:
self.submit('memory', t, server_status['mem'][t])
# connections
self.submit('connections', 'connections', server_status['connections']['current'])
# locks
if self.lockTotalTime is not None and self.lockTime is not None:
if self.lockTime == server_status['globalLock']['lockTime']:
value = 0.0
else:
value = float(server_status['globalLock']['lockTime'] - self.lockTime) * 100.0 / float(server_status['globalLock']['totalTime'] - self.lockTotalTime)
self.submit('percent', 'lock_ratio', value)
self.lockTotalTime = server_status['globalLock']['totalTime']
self.lockTime = server_status['globalLock']['lockTime']
# indexes
accesses = None
misses = None
index_counters = server_status['indexCounters'] if at_least_2_4 else server_status['indexCounters']['btree']
if self.accesses is not None:
accesses = index_counters['accesses'] - self.accesses
if accesses < 0:
accesses = None
misses = (index_counters['misses'] or 0) - (self.misses or 0)
if misses < 0:
misses = None
if accesses and misses is not None:
self.submit('cache_ratio', 'cache_misses', int(misses * 100 / float(accesses)))
else:
self.submit('cache_ratio', 'cache_misses', 0)
self.accesses = index_counters['accesses']
self.misses = index_counters['misses']
for mongo_db in self.mongo_db:
db = con[mongo_db]
if self.mongo_user and self.mongo_password:
db.authenticate(self.mongo_user, self.mongo_password)
db_stats = db.command('dbstats')
# stats counts
self.submit('counter', 'object_count', db_stats['objects'], mongo_db)
self.submit('counter', 'collections', db_stats['collections'], mongo_db)
self.submit('counter', 'num_extents', db_stats['numExtents'], mongo_db)
self.submit('counter', 'indexes', db_stats['indexes'], mongo_db)
# stats sizes
self.submit('file_size', 'storage', db_stats['storageSize'], mongo_db)
self.submit('file_size', 'index', db_stats['indexSize'], mongo_db)
self.submit('file_size', 'data', db_stats['dataSize'], mongo_db)
# Replica check
rs_status = {}
slaveDelays = {}
try:
# Get replica set status
try:
rs_status = con.admin.command("replSetGetStatus")
except pymongo.errors.OperationFailure, e:
if e.code == None and str(e).find('failed: not running with --replSet"'):
print "OK - Not running with replSet"
con.disconnect()
return 0
rs_conf = con.local.system.replset.find_one()
for member in rs_conf['members']:
if member.get('slaveDelay') is not None:
slaveDelays[member['host']] = member.get('slaveDelay')
else:
slaveDelays[member['host']] = 0
# Find the primary and/or the current node
primary_node = None
host_node = None
for member in rs_status["members"]:
if member["stateStr"] == "PRIMARY":
primary_node = member
if member["name"].split(':')[0] == host and int(member["name"].split(':')[1]) == port:
host_node = member
# Check if we're in the middle of an election and don't have a primary
if primary_node is None:
print "WARNING - No primary defined. In an election?"
con.disconnect()
return 1
# Check if we failed to find the current host
# below should never happen
if host_node is None:
print "CRITICAL - Unable to find host '" + host + "' in replica set."
con.disconnect()
return 2
# Is the specified host the primary?
if host_node["stateStr"] == "PRIMARY":
if max_lag == False:
print "OK - This is the primary."
con.disconnect()
return 0
else:
#get the maximal replication lag
data = ""
maximal_lag = 0
for member in rs_status['members']:
if not member['stateStr'] == "ARBITER":
lastSlaveOpTime = member['optimeDate']
replicationLag = abs(primary_node["optimeDate"] - lastSlaveOpTime).seconds - slaveDelays[member['name']]
data = data + member['name'] + " lag=%d;" % replicationLag
maximal_lag = max(maximal_lag, replicationLag)
# send message with maximal lag
message = "Maximal lag is " + str(maximal_lag) + " seconds"
print message
self.submit('replication', 'maximal-lag-seconds', str(maximal_lag))
# send message with maximal lag in percentage
err, con = mongo_connect(primary_node['name'].split(':')[0], int(primary_node['name'].split(':')[1]), False, user, passwd)
if err != 0:
con.disconnect()
return err
primary_timediff = replication_get_time_diff(con)
maximal_lag = int(float(maximal_lag) / float(primary_timediff) * 100)
message = "Maximal lag is " + str(maximal_lag) + " percents"
print message
self.submit('replication', 'maximal-lag-percentage', str(maximal_lag))
con.disconnect()
return str(maximal_lag)
elif host_node["stateStr"] == "ARBITER":
print "OK - This is an arbiter"
con.disconnect()
return 0
# Find the difference in optime between current node and PRIMARY
optime_lag = abs(primary_node["optimeDate"] - host_node["optimeDate"])
if host_node['name'] in slaveDelays:
slave_delay = slaveDelays[host_node['name']]
elif host_node['name'].endswith(':27017') and host_node['name'][:-len(":27017")] in slaveDelays:
slave_delay = slaveDelays[host_node['name'][:-len(":27017")]]
else:
raise Exception("Unable to determine slave delay for {0}".format(host_node['name']))
try: # work starting from python2.7
lag = optime_lag.total_seconds()
except:
lag = float(optime_lag.seconds + optime_lag.days * 24 * 3600)
# send message with lag
message = "Lag is " + str(lag) + " seconds"
print message
self.submit('replication', 'lag-seconds', str(lag))
# send message with lag in percentage
err, con = mongo_connect(primary_node['name'].split(':')[0], int(primary_node['name'].split(':')[1]), False, user, passwd)
if err != 0:
con.disconnect()
return err
primary_timediff = replication_get_time_diff(con)
if primary_timediff != 0:
lag = int(float(lag) / float(primary_timediff) * 100)
else:
lag = 0
message = "Lag is " + str(lag) + " percents"
print message
self.submit('replication', 'lag-percentage', str(lag))
con.disconnect()
return str(lag)
#return check_levels(lag, warning + slaveDelays[host_node['name']], critical + slaveDelays[host_node['name']], message)
except Exception, e:
con.disconnect()
return e
def config(self, obj):
for node in obj.children:
if node.key == 'Port':
self.mongo_port = int(node.values[0])
elif node.key == 'Host':
self.mongo_host = node.values[0]
elif node.key == 'User':
self.mongo_user = node.values[0]
elif node.key == 'Password':
self.mongo_password = node.values[0]
elif node.key == 'Database':
self.mongo_db = node.values
else:
collectd.warning("mongodb plugin: Unkown configuration key %s" % node.key)
mongodb = MongoDB()
collectd.register_read(mongodb.do_server_status)
collectd.register_config(mongodb.config)