Skip to content

Instantly share code, notes, and snippets.

@tony-landis
Created October 27, 2012 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tony-landis/3965743 to your computer and use it in GitHub Desktop.
Save tony-landis/3965743 to your computer and use it in GitHub Desktop.
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