Create a gist now

Instantly share code, notes, and snippets.

Simple script you can put in your crontab to monitor replication state of all docs in the _replicator couchdb database. Will send an email with the host name, doc name and state if state is not 'triggered'.
#!/usr/bin/python
"""
Simple script you can put in your
crontab to monitor replication state
of all docs in the _replicator
couchdb database.
Will send an email with the host name,
doc name and state if state is not 'triggered'.
"""
EMAIL = 'you@you.com'
COUCH_PW = 'XXXXXX'
uri = 'http://127.0.0.1:5984/_replicator/_all_docs?include_docs=true'
import requests
import json
from pprint import pprint
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
import socket
HOST = socket.gethostname()
def mail(doc):
""
msg = """
STATE: %(state)s
TIME: %(time)s
""" % dict(state=doc.get('_replication_state'), time=doc.get('_replication_state_time'))
msg = MIMEText(msg)
msg["From"] = "couchdb@" + HOST
msg["To"] = EMAIL
msg["Subject"] = "%s %s %s" % (HOST, doc.get('_replication_state'), doc.get('_id'))
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(msg.as_string())
rs = requests.get(uri, auth=('admin', COUCH_PW))
for doc in json.loads(rs.text).get('rows'):
doc = doc.get('doc')
if not doc.get('_replication_state'): continue
state = doc.get('_replication_state')
if state != 'triggered':
# email alert
mail(doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment