Skip to content

Code authorized to be public. No idea what this does. #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 53 additions & 41 deletions mongodb.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
#
# Plugin to collectd statistics from MongoDB
#
# From https://github.com/sebest/collectd-mongodb
#
# Copyright (c) Sebastien Estienne
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


import collectd
from pymongo import MongoClient
from pymongo.read_preferences import ReadPreference
from pymongo import Connection
from distutils.version import StrictVersion as V


Expand All @@ -25,9 +47,10 @@ def __init__(self):

def submit(self, type, instance, value, db=None):
if db:
plugin_instance = '%s-%s' % (self.mongo_port, db)
plugin_instance = '%s' % (db)
else:
plugin_instance = str(self.mongo_port)
plugin_instance = 'global'

v = collectd.Values()
v.plugin = self.plugin_name
v.plugin_instance = plugin_instance
Expand All @@ -37,7 +60,7 @@ def submit(self, type, instance, value, db=None):
v.dispatch()

def do_server_status(self):
con = MongoClient(host=self.mongo_host, port=self.mongo_port, read_preference=ReadPreference.SECONDARY)
con = Connection(host=self.mongo_host, port=self.mongo_port, slave_okay=True)
db = con[self.mongo_db[0]]
if self.mongo_user and self.mongo_password:
db.authenticate(self.mongo_user, self.mongo_password)
Expand All @@ -55,48 +78,37 @@ def do_server_status(self):
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])
self.submit('connections', 'connections', server_status['connections']['current'])

# 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']
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
if 'indexCounters' in server_status:
accesses = None
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
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']
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]
Expand Down