<s> from hashlib import md5 <EOL> import time <EOL> import requests <EOL> from checks import AgentCheck <EOL> class Mesos ( AgentCheck ) : <EOL> SERVICE_CHECK_NAME = "<STR_LIT>" <EOL> def check ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> self . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if '<STR_LIT:url>' not in instance : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> url = instance [ '<STR_LIT:url>' ] <EOL> instance_tags = instance . get ( '<STR_LIT>' , [ ] ) <EOL> default_timeout = self . init_config . get ( '<STR_LIT>' , <NUM_LIT:5> ) <EOL> timeout = float ( instance . get ( '<STR_LIT>' , default_timeout ) ) <EOL> response = self . get_master_roles ( url , timeout ) <EOL> if response is not None : <EOL> for role in response [ '<STR_LIT>' ] : <EOL> tags = [ '<STR_LIT>' + role [ '<STR_LIT:name>' ] ] + instance_tags <EOL> self . gauge ( '<STR_LIT>' , len ( role [ '<STR_LIT>' ] ) , tags = tags ) <EOL> self . gauge ( '<STR_LIT>' , role [ '<STR_LIT>' ] , tags = tags ) <EOL> resources = role [ '<STR_LIT>' ] <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if attr in resources : <EOL> self . gauge ( '<STR_LIT>' + attr , resources [ attr ] , tags = tags ) <EOL> response = self . get_master_stats ( url , timeout ) <EOL> if response is not None : <EOL> tags = instance_tags <EOL> for key in iter ( response ) : <EOL> self . gauge ( '<STR_LIT>' + key , response [ key ] , tags = tags ) <EOL> response = self . get_master_state ( url , timeout ) <EOL> if response is not None : <EOL> tags = instance_tags <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . gauge ( '<STR_LIT>' + attr , response [ attr ] , tags = tags ) <EOL> for framework in response [ '<STR_LIT>' ] : <EOL> tags = [ '<STR_LIT>' + framework [ '<STR_LIT:id>' ] ] + instance_tags <EOL> resources = framework [ '<STR_LIT>' ] <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if attr in resources : <EOL> self . gauge ( '<STR_LIT>' + attr , resources [ attr ] , tags = tags ) <EOL> for slave in response [ '<STR_LIT>' ] : <EOL> tags = [ '<STR_LIT>' , '<STR_LIT>' + slave [ '<STR_LIT:id>' ] ] + instance_tags <EOL> resources = slave [ '<STR_LIT>' ] <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if attr in resources : <EOL> self . gauge ( '<STR_LIT>' + attr , resources [ attr ] , tags = tags ) <EOL> def get_master_roles ( self , url , timeout ) : <EOL> return self . get_json ( url + "<STR_LIT>" , timeout ) <EOL> def get_master_stats ( self , url , timeout ) : <EOL> return self . get_json ( url + "<STR_LIT>" , timeout ) <EOL> def get_master_state ( self , url , timeout ) : <EOL> return self . get_json ( url + "<STR_LIT>" , timeout ) <EOL> def get_json ( self , url , timeout ) : <EOL> aggregation_key = md5 ( url ) . hexdigest ( ) <EOL> tags = [ "<STR_LIT>" % url ] <EOL> msg = None <EOL> status = None <EOL> try : <EOL> r = requests . get ( url , timeout = timeout ) <EOL> if r . status_code != <NUM_LIT:200> : <EOL> self . status_code_event ( url , r , aggregation_key ) <EOL> status = AgentCheck . CRITICAL <EOL> msg = "<STR_LIT>" % ( r . status_code , url ) <EOL> else : <EOL> status = AgentCheck . OK <EOL> msg = "<STR_LIT>" % url <EOL> except requests . exceptions . Timeout as e : <EOL> self . timeout_event ( url , timeout , aggregation_key ) <EOL> msg = "<STR_LIT>" % ( timeout , url ) <EOL> status = AgentCheck . CRITICAL <EOL> except Exception as e : <EOL> msg = str ( e ) <EOL> status = AgentCheck . CRITICAL <EOL> finally : <EOL> self . service_check ( self . SERVICE_CHECK_NAME , status , tags = tags , message = msg ) <EOL> if status is AgentCheck . CRITICAL : <EOL> self . warning ( msg ) <EOL> return None <EOL> return r . json ( ) <EOL> def timeout_event ( self , url , timeout , aggregation_key ) : <EOL> self . event ( { <EOL> '<STR_LIT>' : int ( time . time ( ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( url , timeout ) , <EOL> '<STR_LIT>' : aggregation_key <EOL> } ) <EOL> def status_code_event ( self , url , r , aggregation_key ) : <EOL> self . event ( { <EOL> '<STR_LIT>' : int ( time . time ( ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % url , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( url , r . status_code ) , <EOL> '<STR_LIT>' : aggregation_key <EOL> } ) </s>
