<s> import argparse <EOL> from maas_common import get_auth_ref <EOL> from maas_common import get_keystone_client <EOL> from maas_common import metric_bool <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> from requests import exceptions as exc <EOL> def check ( auth_ref , args ) : <EOL> keystone = get_keystone_client ( auth_ref ) <EOL> auth_token = keystone . auth_token <EOL> VOLUME_ENDPOINT = ( <EOL> '<STR_LIT>' . format ( hostname = args . hostname , <EOL> tenant = keystone . tenant_id ) <EOL> ) <EOL> s = requests . Session ( ) <EOL> s . headers . update ( <EOL> { '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : auth_token } ) <EOL> try : <EOL> r = s . get ( '<STR_LIT>' % VOLUME_ENDPOINT , verify = False , timeout = <NUM_LIT:10> ) <EOL> except ( exc . ConnectionError , <EOL> exc . HTTPError , <EOL> exc . Timeout ) as e : <EOL> status_err ( str ( e ) ) <EOL> if not r . ok : <EOL> status_err ( '<STR_LIT>' ) <EOL> services = r . json ( ) [ '<STR_LIT>' ] <EOL> if args . host : <EOL> backend = '<STR_LIT>' . join ( ( args . host , '<STR_LIT:@>' ) ) <EOL> services = [ service for service in services <EOL> if ( service [ '<STR_LIT:host>' ] . startswith ( backend ) or <EOL> service [ '<STR_LIT:host>' ] == args . host ) ] <EOL> if len ( services ) == <NUM_LIT:0> : <EOL> status_err ( '<STR_LIT>' ) <EOL> status_ok ( ) <EOL> if args . host : <EOL> all_services_are_up = True <EOL> for service in services : <EOL> service_is_up = True <EOL> if service [ '<STR_LIT:status>' ] == '<STR_LIT>' and service [ '<STR_LIT:state>' ] != '<STR_LIT>' : <EOL> service_is_up = False <EOL> all_services_are_up = False <EOL> if '<STR_LIT:@>' in service [ '<STR_LIT:host>' ] : <EOL> [ host , backend ] = service [ '<STR_LIT:host>' ] . split ( '<STR_LIT:@>' ) <EOL> name = '<STR_LIT>' % ( service [ '<STR_LIT>' ] , backend ) <EOL> metric_bool ( name , service_is_up ) <EOL> name = '<STR_LIT>' % service [ '<STR_LIT>' ] <EOL> metric_bool ( name , all_services_are_up ) <EOL> else : <EOL> for service in services : <EOL> service_is_up = True <EOL> if service [ '<STR_LIT:status>' ] == '<STR_LIT>' and service [ '<STR_LIT:state>' ] != '<STR_LIT>' : <EOL> service_is_up = False <EOL> name = '<STR_LIT>' % ( service [ '<STR_LIT>' ] , service [ '<STR_LIT:host>' ] ) <EOL> metric_bool ( name , service_is_up ) <EOL> def main ( args ) : <EOL> auth_ref = get_auth_ref ( ) <EOL> check ( auth_ref , args ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> with print_output ( ) : <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> type = str , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> type = str , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> main ( args ) </s>
