Skip to content
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

Add a MongoDB collector #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
81 changes: 81 additions & 0 deletions collectors/0/mongo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/python
#
# mongo.py -- a MongoDB collector for tcollector/OpenTSDB
# Copyright 2013 Tim Douglas, [email protected]
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version. This program is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
# General Public License for more details. You should have received a copy
# of the GNU Lesser General Public License along with this program. If not,
# see <http://www.gnu.org/licenses/>.

import sys
import time
import pymongo

HOST = 'localhost'
PORT = 27017
INTERVAL = 15
METRICS = (
'backgroundFlushing.average_ms',
'backgroundFlushing.flushes',
'backgroundFlushing.total_ms',
'connections.available',
'connections.current',
'cursors.totalOpen',
'cursors.timedOut',
'dur.commits',
'dur.commitsInWriteLock',
'dur.compression',
'dur.earlyCommits',
'dur.journaledMB',
'dur.writeToDataFilesMB',
'extra_info.heap_usage_bytes',
'extra_info.page_faults',
'globalLock.lockTime',
'globalLock.totalTime',
'indexCounters.btree.accesses',
'indexCounters.btree.hits',
'indexCounters.btree.missRatio',
'indexCounters.btree.misses',
'indexCounters.btree.resets',
'mem.resident',
'mem.virtual',
'mem.mapped',
'network.bytesIn',
'network.bytesOut',
'network.numRequests',
)
TAG_METRICS = (
('asserts', ('msg', 'regular', 'user', 'warning')),
('opcounters', ('command', 'delete', 'getmore', 'insert', 'query', 'update')),
)

def main():
c = pymongo.Connection(host=HOST, port=PORT)

while True:
res = c.admin.command('serverStatus')
ts = int(time.time())

for base_metric, tags in TAG_METRICS:
for tag in tags:
print 'mongo.'+base_metric, ts, res[base_metric][tag], 'type=' + tag
for metric in METRICS:
cur = res
try:
for m in metric.split('.'):
cur = cur[m]
except KeyError:
continue
print 'mongo.' + metric, ts, cur

sys.stdout.flush()
time.sleep(INTERVAL)

if __name__ == '__main__':
main()