Make the index format string configurable in the conf file.

This allows query to work against indices not named the
typical "logstash-%Y.%m.%d".

Change-Id: I89a5b293b8e23cc81a8ed33d86c173adca927d28
This commit is contained in:
Nate Marsella 2016-04-15 06:45:27 -04:00
parent 15eb9df230
commit 9308535283
3 changed files with 14 additions and 6 deletions

View File

@ -16,3 +16,4 @@ key=/home/mtreinish/.ssh/id_rsa
es_url=http://logstash.openstack.org:80/elasticsearch
ls_url=http://logstash.openstack.org
db_uri=mysql+pymysql://query:query@logstash.openstack.org/subunit2sql
index_format=logstash-%Y.%m.%d

View File

@ -26,6 +26,7 @@ import elastic_recheck.results as er_results
LOG = logging.getLogger('erquery')
DEFAULT_INDEX_FORMAT = 'logstash-%Y.%m.%d'
DEFAULT_NUMBER_OF_DAYS = 10
DEFAULT_MAX_QUANTITY = 5
IGNORED_ATTRIBUTES = [
@ -64,9 +65,10 @@ def analyze_attributes(attributes):
def query(query_file_name, days=DEFAULT_NUMBER_OF_DAYS, es_url=er.ES_URL,
quantity=DEFAULT_MAX_QUANTITY, verbose=False):
quantity=DEFAULT_MAX_QUANTITY, verbose=False,
indexfmt=DEFAULT_INDEX_FORMAT):
es = er_results.SearchEngine(es_url)
es = er_results.SearchEngine(url=es_url, indexfmt=indexfmt)
with open(query_file_name) as f:
query_file = yaml.load(f.read())
@ -119,15 +121,19 @@ def main():
# Start with defaults
es_url = er.ES_URL
es_index_format = DEFAULT_INDEX_FORMAT
if args.conf:
config = ConfigParser.ConfigParser({'es_url': er.ES_URL})
config = ConfigParser.ConfigParser({
'es_url': er.ES_URL,
'index_format': DEFAULT_INDEX_FORMAT})
config.read(args.conf)
if config.has_section('data_source'):
es_url = config.get('data_source', 'es_url')
es_index_format = config.get('data_source', 'index_format')
query(args.query_file.name, days=args.days, quantity=args.quantity,
verbose=args.verbose, es_url=es_url)
verbose=args.verbose, es_url=es_url, indexfmt=es_index_format)
if __name__ == "__main__":

View File

@ -29,8 +29,9 @@ pp = pprint.PrettyPrinter()
class SearchEngine(object):
"""Wrapper for pyelasticsearch so that it returns result sets."""
def __init__(self, url):
def __init__(self, url, indexfmt='logstash-%Y.%m.%d'):
self._url = url
self._indexfmt = indexfmt
def search(self, query, size=1000, recent=False, days=0):
"""Search an elasticsearch server.
@ -56,7 +57,7 @@ class SearchEngine(object):
args = {'size': size}
if recent or days:
# today's index
datefmt = 'logstash-%Y.%m.%d'
datefmt = self._indexfmt
now = datetime.datetime.utcnow()
indexes = [now.strftime(datefmt)]
if recent: