root / plugins / mongodb / mongo_lag @ 8fc3d29d
Historique | Voir | Annoter | Télécharger (2,09 ko)
| 1 |
#!/usr/bin/env python |
|---|---|
| 2 |
""" |
| 3 |
MongoDB Replication Lag |
| 4 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 |
|
| 6 |
Connects to a single mongo instance and retrieve |
| 7 |
replication lag for all connected members. |
| 8 |
|
| 9 |
munin-node.conf: |
| 10 |
[mongo_lag] |
| 11 |
env.host 127.0.0.1 |
| 12 |
env.port 27017 |
| 13 |
env.username user |
| 14 |
env.password P@55w0rd |
| 15 |
|
| 16 |
:author: Stefan Andersen <stefan@stefanandersen.dk> |
| 17 |
:license: The Beer Ware License (Revision 42) |
| 18 |
<stefan@stefanandersen.dk> wrote this file. As long |
| 19 |
as you retain this notice you can do whatever you want |
| 20 |
with this stuff. If we meet some day, and you think |
| 21 |
this stuff is worth it, you can buy me a beer in return. |
| 22 |
""" |
| 23 |
import os |
| 24 |
import sys |
| 25 |
import pymongo |
| 26 |
|
| 27 |
def _get_members(): |
| 28 |
host = os.environ.get('host', '127.0.0.1')
|
| 29 |
port = os.environ.get('port', 27017)
|
| 30 |
username = os.environ.get('username', '')
|
| 31 |
password = os.environ.get('password', '')
|
| 32 |
conn = pymongo.MongoClient(host,int(port)) |
| 33 |
if username: |
| 34 |
connAuth = conn['admin'] |
| 35 |
connAuth.authenticate(username, password) |
| 36 |
repl_status = conn.admin.command("replSetGetStatus")
|
| 37 |
else: |
| 38 |
repl_status = conn.admin.command("replSetGetStatus")
|
| 39 |
|
| 40 |
members = {}
|
| 41 |
for member in repl_status['members']: |
| 42 |
if member['stateStr'] != 'ARBITER' : |
| 43 |
name = member['name'].split('.')[0]
|
| 44 |
members[name] = {'state': member['state'], 'optimeDate': member['optimeDate']}
|
| 45 |
return members |
| 46 |
|
| 47 |
def run(): |
| 48 |
members = _get_members(); |
| 49 |
for member in members: |
| 50 |
if members[member]['state'] == 1: |
| 51 |
primary_optime = members[member]['optimeDate'] |
| 52 |
|
| 53 |
for member in members: |
| 54 |
lag = (primary_optime - members[member]['optimeDate']).seconds |
| 55 |
print "{0}.value {1}".format(member, lag)
|
| 56 |
|
| 57 |
def config(): |
| 58 |
print """graph_title MongoDB replication lag |
| 59 |
graph_args --base 1000 |
| 60 |
graph_vlabel Replication lag (seconds) |
| 61 |
graph_category db |
| 62 |
""" |
| 63 |
|
| 64 |
for member in _get_members(): |
| 65 |
print "{0}.label {0}".format(member)
|
| 66 |
|
| 67 |
if __name__ == "__main__": |
| 68 |
if len(sys.argv) > 1 and sys.argv[1] == "config": |
| 69 |
config() |
| 70 |
else: |
| 71 |
run() |
