Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement basic river gathering

  • Loading branch information...
commit 02b981b84cd642918d16af5ef6270b8682624823 1 parent a16c58e
@opie4624 authored
Showing with 105 additions and 0 deletions.
  1. +105 −0 dam-remover.py
View
105 dam-remover.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*
+
+u"""CouchDB Dam Remover - Watches the CouchDB Rivers in ElasticSearch and restarts them if they stall.
+
+Example usage
+=============
+
+.. sourcecode:: bash
+
+ python dam_remover.py -vvvvv action
+
+:author: `Andrew Kraut <mailto:opie@cybyl.com>`__
+"""
+
+# Pylint checks
+# "line to long" pylint: disable-msg=C0301
+# "Used * or ** magic" pylint: disable-msg=W0142
+
+__revision__ = "$Id$"
+__docformat__ = 'restructuredtext en'
+
+import os, sys, logging, optparse
+import simplejson, urllib, string, time, datetime
+
+SERVER_URL = 'http://localhost:9200'
+POLLING_FREQ = '30' # in seconds
+TIMEOUT_FREQ = '900' # in seconds
+
+#: Glogbal logger instance
+LOG = logging.getLogger(__name__)
+#: Debug level names as a string
+LOG_HELP = ','.join(["%d=%s" % (4-x, logging.getLevelName((x+1)*10)) for x in xrange(5)])
+#: Console LOG format
+LOG_FORMAT_CONS = '%(asctime)s %(name)-12s %(levelname)8st%(message)s'
+#: File LOG format
+LOG_FORMAT_FILE = '%(asctime)s %(name)s[%(process)d] %(levelname)10s %(message)s'
+#: Levels of logging translation (count of -v, log level)
+LOGLEVEL_DICT = { 1 : 50, 2:40, 3:20, 4:10, 5:1 }
+
+DEFAULT_VERBOSITY = 0
+
+
+def default_action():
+ """ Fire the loop, then wait """
+ LOG.info("Gathering relevant rivers...")
+ rivers = gather_rivers()
+ LOG.info("Collecting _meta data...")
+ rivers = gather_meta(rivers)
+
+ seqs = dict()
+
+# for river in rivers:
+# if !check_river(river):
+# break
+# else:
+# time.sleep(POLLING_FREQ)
+
+def gather_rivers():
+ """Gather names of rivers"""
+ result = simplejson.load(urllib.urlopen("%s/_river/_mapping" % SERVER_URL))
+ rivers_good = []
+ for river in result['_river'].keys():
+ if result['_river'][river]['properties'].has_key('couchdb'):
+ rivers_good.append(river)
+ return rivers_good
+
+def gather_meta(rivers):
+ """Gather _meta documents for rivers we're interested in."""
+ rivers_meta = dict()
+ for river in rivers:
+ rivers_meta[river] = simplejson.load(urllib.urlopen('%s/_river/%s/_meta' % (SERVER_URL, river)))['_source']
+ return rivers_meta
+
+def main():
+ """ Main function - parses args and runs action """
+ parser = optparse.OptionParser(usage="%prog or type %prog -h (--help) for help", description=__doc__, version="%prog" + __revision__)
+ parser.add_option("-v", action="count", dest="verbosity", default = DEFAULT_VERBOSITY, help = "Verbosity. Add more -v to be more verbose (%s) [default: %%default]" % LOG_HELP)
+ parser.add_option("-l", "--logfile", dest="logfile", default = None, help = "Log to file instead off console [default: %default]" )
+
+ (options, args) = parser.parse_args()
+
+ verbosity = LOGLEVEL_DICT.get(int(options.verbosity), DEFAULT_VERBOSITY)
+
+ # Set up logging
+ if options.logfile is None:
+ logging.basicConfig(level=verbosity, format=LOG_FORMAT_CONS)
+ else:
+ logfilename = os.path.normpath(options.logfile)
+ logging.basicConfig(level=verbosity, format=LOG_FORMAT_FILE, filename=logfilename, filemode='a')
+ print >> sys.stderr, "Logging to %s" % logfilename
+
+ # Run actions
+ LOG.info("Starting %s, rev %s from %s using verbosity %s/%s as PID %d", __name__, __revision__, os.path.abspath(__file__), options.verbosity, verbosity, os.getpid())
+
+ for action in args:
+ if action == 'raise':
+ raise Exception("User requested exception")
+ else:
+ default_action()
+
+ LOG.info("Exited %s, rev %s from %s using verbosity %s/%s as PID %d", __name__, __revision__, os.path.abspath(__file__), options.verbosity, verbosity, os.getpid())
+
+if __name__ == "__main__":
+ main()
Please sign in to comment.
Something went wrong with that request. Please try again.