<s> import json <EOL> import optparse <EOL> import os <EOL> import re <EOL> from maas_common import metric <EOL> from maas_common import print_output <EOL> from maas_common import status_err <EOL> from maas_common import status_ok <EOL> import requests <EOL> def get_elasticsearch_bind_host ( ) : <EOL> """<STR_LIT>""" <EOL> config = '<STR_LIT>' <EOL> if not os . path . exists ( config ) : <EOL> return '<STR_LIT:localhost>' <EOL> with open ( config ) as fd : <EOL> contents = fd . readlines ( ) <EOL> bind_host_re = re . compile ( '<STR_LIT>' ) <EOL> hosts = filter ( bind_host_re . match , contents ) <EOL> if not hosts : <EOL> raise SystemExit ( False ) <EOL> match = bind_host_re . match ( hosts [ - <NUM_LIT:1> ] ) <EOL> return match . groups ( ) [ <NUM_LIT:0> ] <EOL> ES_PORT = '<STR_LIT>' <EOL> ELASTICSEARCH = None <EOL> def json_querystring ( query_string , sort = None ) : <EOL> """<STR_LIT>""" <EOL> query_dict = { '<STR_LIT>' : { '<STR_LIT>' : query_string } } <EOL> if sort : <EOL> query_dict [ '<STR_LIT>' ] = sort <EOL> return json . dumps ( query_dict ) <EOL> def json_filter ( filtered_query ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( { '<STR_LIT>' : { '<STR_LIT>' : filtered_query } } ) <EOL> def find_indices ( ) : <EOL> """<STR_LIT>""" <EOL> url = ELASTICSEARCH + '<STR_LIT>' <EOL> query = json_querystring ( { '<STR_LIT>' : '<STR_LIT>' } , <EOL> [ { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } ] ) <EOL> json = get_json ( url , query ) <EOL> return sorted ( res [ '<STR_LIT>' ] for res in json [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def most_recent_index ( ) : <EOL> """<STR_LIT>""" <EOL> indices = find_indices ( ) <EOL> if not indices : <EOL> status_err ( '<STR_LIT>' ) <EOL> return indices [ - <NUM_LIT:1> ] <EOL> def search_url_for ( index ) : <EOL> """<STR_LIT>""" <EOL> return ELASTICSEARCH + '<STR_LIT:/>' + index + '<STR_LIT>' <EOL> def get_json ( url , data ) : <EOL> """<STR_LIT>""" <EOL> exceptions = ( requests . exceptions . HTTPError , <EOL> requests . exceptions . ConnectionError ) <EOL> try : <EOL> r = requests . get ( url , data = data ) <EOL> except exceptions as e : <EOL> status_err ( str ( e ) ) <EOL> return r . json ( ) <EOL> def get_number_of ( loglevel , index ) : <EOL> """<STR_LIT>""" <EOL> url = search_url_for ( index ) <EOL> data = json_querystring ( { '<STR_LIT>' : loglevel , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:message>' ] } ) <EOL> json = get_json ( url , data ) <EOL> return json [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def parse_args ( ) : <EOL> parser = optparse . OptionParser ( usage = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT:host>' , <EOL> default = None , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT:port>' , <EOL> default = ES_PORT , <EOL> help = '<STR_LIT>' ) <EOL> return parser . parse_args ( ) <EOL> def configure ( options ) : <EOL> global ELASTICSEARCH <EOL> host = options . host or get_elasticsearch_bind_host ( ) <EOL> port = options . port <EOL> ELASTICSEARCH = '<STR_LIT>' . format ( host , port ) <EOL> def main ( ) : <EOL> options , _ = parse_args ( ) <EOL> configure ( options ) <EOL> latest = most_recent_index ( ) <EOL> num_errors = get_number_of ( '<STR_LIT>' , latest ) <EOL> num_warnings = get_number_of ( '<STR_LIT>' , latest ) <EOL> status_ok ( ) <EOL> metric ( '<STR_LIT>' , '<STR_LIT>' , num_errors ) <EOL> metric ( '<STR_LIT>' , '<STR_LIT>' , num_warnings ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> with print_output ( ) : <EOL> main ( ) </s>
