2012-07-10 16:49:18 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
MongoDB Replication Lag
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Connects to a single mongo instance and retrieve
|
|
|
|
replication lag for all connected members.
|
|
|
|
|
|
|
|
munin-node.conf:
|
|
|
|
[mongodb_lag]
|
|
|
|
env.host 127.0.0.1
|
|
|
|
env.port 27017
|
|
|
|
|
|
|
|
:author: Stefan Andersen <stefan@stefanandersen.dk>
|
|
|
|
:license: The Beer Ware License (Revision 42)
|
2018-08-02 02:03:42 +02:00
|
|
|
<stefan@stefanandersen.dk> wrote this file. As long
|
|
|
|
as you retain this notice you can do whatever you want
|
|
|
|
with this stuff. If we meet some day, and you think
|
|
|
|
this stuff is worth it, you can buy me a beer in return.
|
2012-07-10 16:49:18 +02:00
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import pymongo
|
|
|
|
|
|
|
|
def _get_members():
|
|
|
|
host = os.environ.get('host', '127.0.0.1')
|
|
|
|
port = os.environ.get('port', 27017)
|
2016-06-14 20:23:56 +02:00
|
|
|
conn = pymongo.MongoClient(host,port)
|
2012-07-10 16:49:18 +02:00
|
|
|
repl_status = conn.admin.command("replSetGetStatus")
|
|
|
|
|
|
|
|
members = {}
|
|
|
|
for member in repl_status['members']:
|
2012-09-25 10:51:21 +02:00
|
|
|
if member['stateStr'] != 'ARBITER' :
|
|
|
|
name = member['name'].split('.')[0]
|
|
|
|
members[name] = {'state': member['state'], 'optimeDate': member['optimeDate']}
|
2012-07-10 16:49:18 +02:00
|
|
|
return members
|
|
|
|
|
|
|
|
def run():
|
|
|
|
members = _get_members();
|
|
|
|
for member in members:
|
|
|
|
if members[member]['state'] == 1:
|
|
|
|
primary_optime = members[member]['optimeDate']
|
|
|
|
|
|
|
|
for member in members:
|
2012-07-11 10:21:33 +02:00
|
|
|
lag = (primary_optime - members[member]['optimeDate']).seconds
|
2012-09-25 10:51:21 +02:00
|
|
|
print "{0}.value {1}".format(member, lag)
|
2012-07-10 16:49:18 +02:00
|
|
|
|
|
|
|
def config():
|
|
|
|
print """graph_title MongoDB replication lag
|
|
|
|
graph_args --base 1000
|
|
|
|
graph_vlabel Replication lag (seconds)
|
2017-02-23 20:45:01 +01:00
|
|
|
graph_category db
|
2012-07-10 16:49:18 +02:00
|
|
|
"""
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2012-07-10 16:49:18 +02:00
|
|
|
for member in _get_members():
|
2012-07-11 10:21:33 +02:00
|
|
|
print "{0}.label {0}".format(member)
|
2012-07-10 16:49:18 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == "config":
|
|
|
|
config()
|
|
|
|
else:
|
|
|
|
run()
|