Created
October 27, 2012 19:20
-
-
Save tony-landis/3965743 to your computer and use it in GitHub Desktop.
Revisions
-
Tony Landis created this gist
Oct 27, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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)