This repository was archived by the owner on Oct 25, 2024. It is now read-only.
forked from sebest/collectd-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.py
156 lines (134 loc) · 6.32 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
#
# Plugin to collectd statistics from MongoDB
#
import collectd
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
from pymongo.read_preferences import ReadPreference
from distutils.version import StrictVersion 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
collectd.info("%s: plugin started" % self.plugin_name)
def submit(self, type, instance, value, db=None):
try:
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()
except TypeError as e:
collectd.error("%s: %s" % (self.plugin_name, e))
except:
collectd.error("%s: Unexpected error in submit()" % self.plugin_name)
def do_server_status(self):
try:
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)
except ConnectionFailure as e:
collectd.error("%s: %s" % (self.plugin_name, e))
return
except:
collectd.error("%s: Unexpected error in do_server_status() while connecting to server" % self.plugin_name)
return
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', 'bits']:
if t in server_status['mem']:
self.submit('memory', t, server_status['mem'][t])
# connections
self.submit('mongo_connections', 'current', server_status['connections']['current'])
if 'available' in server_status['connections']:
self.submit('mongo_connections', 'available', server_status['connections']['available'])
if 'totalCreated' in server_status['connections']:
self.submit('mongo_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 'globalLock' in server_status and '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']
if 'globalLock' in server_status and 'totalTime' in server_status['globalLock']:
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')
if "raw" in db_stats:
collectd.warning("%s: This plugin is not compatible with mongos" % self.plugin_name)
continue
# 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)
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("%s: Unkown configuration key %s" % (self.plugin_name, node.key))
mongodb = MongoDB()
collectd.register_read(mongodb.do_server_status)
collectd.register_config(mongodb.config)