Skip to content

Instantly share code, notes, and snippets.

@tony-landis
Created October 27, 2012 19:20
Show Gist options
  • Select an option

  • Save tony-landis/3965743 to your computer and use it in GitHub Desktop.

Select an option

Save tony-landis/3965743 to your computer and use it in GitHub Desktop.

Revisions

  1. Tony Landis created this gist Oct 27, 2012.
    46 changes: 46 additions & 0 deletions couch-replicate-monitor.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #!/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)