Created
October 27, 2012 19:20
-
-
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'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 = '[email protected]' | |
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