-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmongodb.py
146 lines (123 loc) · 5.83 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
#
# Plugin to collectd statistics from MongoDB
#
import collectd
from pymongo import MongoClient
from pymongo.read_preferences import ReadPreference
from distutils.version import LooseVersion as V
class MongoDB(object):
def __init__(self):
self.plugin_name = "mongo"
self.mongo_host = "127.0.0.1"
self.mongo_port = 27017
self.mongo_db = ["admin", ]
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 get_db_and_collection_stats(self):
con = MongoClient(host=self.mongo_host, port=self.mongo_port, read_preference=ReadPreference.SECONDARY)
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', 'current', server_status['connections']['current'])
if 'available' in server_status['connections']:
self.submit('connections', 'available', server_status['connections']['available'])
if 'totalCreated' in server_status['connections']:
self.submit('connections', 'totalCreated', server_status['connections']['totalCreated'])
# network
if 'network' in server_status:
for t in ['bytesIn', 'bytesOut', 'numRequests']:
self.submit('bytes', t, server_status['network'][t])
# locks
if 'lockTime' in server_status['globalLock']:
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.lockTime = server_status['globalLock']['lockTime']
self.lockTotalTime = server_status['globalLock']['totalTime']
# indexes
if 'indexCounters' in server_status:
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:
con[self.mongo_db[0]].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)
# collection stats
collections = db.collection_names()
for collection in collections:
collection_stats = db.command('collstats', collection)
if 'wiredTiger' in collection_stats:
if 'cursor' in collection_stats['wiredTiger']:
for k, v in collection_stats['wiredTiger']['cursor'].items():
self.submit('collection_stats', (collection + '-' + k), v, mongo_db)
con.close()
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.get_db_and_collection_stats)
collectd.register_config(mongodb.config)