<s> import collections <EOL> import logging <EOL> import pprint <EOL> import socket <EOL> import sys <EOL> import time <EOL> from checks import AGENT_METRICS_CHECK_NAME , AgentCheck , create_service_check <EOL> from checks . check_status import ( <EOL> CheckStatus , <EOL> CollectorStatus , <EOL> EmitterStatus , <EOL> STATUS_ERROR , <EOL> STATUS_OK , <EOL> ) <EOL> from checks . ganglia import Ganglia <EOL> import checks . system . unix as u <EOL> import checks . system . win32 as w32 <EOL> from checks . server_density import plugins , yoshi <EOL> from config import get_system_stats , get_version <EOL> import modules <EOL> from resources . processes import Processes as ResProcesses <EOL> from util import ( <EOL> EC2 , <EOL> GCE , <EOL> get_hostname , <EOL> get_os , <EOL> get_uuid , <EOL> Timer , <EOL> ) <EOL> from utils . jmx import JMXFiles <EOL> from utils . subprocess_output import subprocess <EOL> log = logging . getLogger ( __name__ ) <EOL> FLUSH_LOGGING_PERIOD = <NUM_LIT:10> <EOL> FLUSH_LOGGING_INITIAL = <NUM_LIT:5> <EOL> DD_CHECK_TAG = '<STR_LIT>' <EOL> class AgentPayload ( collections . MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> METADATA_KEYS = frozenset ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> DUPLICATE_KEYS = frozenset ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> COMMON_ENDPOINT = '<STR_LIT>' <EOL> DATA_ENDPOINT = '<STR_LIT>' <EOL> METADATA_ENDPOINT = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> self . data_payload = dict ( ) <EOL> self . meta_payload = dict ( ) <EOL> @ property <EOL> def payload ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . data_payload . copy ( ) <EOL> res . update ( self . meta_payload ) <EOL> return res <EOL> def __getitem__ ( self , key ) : <EOL> if key in self . METADATA_KEYS : <EOL> return self . meta_payload [ key ] <EOL> else : <EOL> return self . data_payload [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> if key in self . DUPLICATE_KEYS : <EOL> self . data_payload [ key ] = value <EOL> self . meta_payload [ key ] = value <EOL> elif key in self . METADATA_KEYS : <EOL> self . meta_payload [ key ] = value <EOL> else : <EOL> self . data_payload [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> if key in self . DUPLICATE_KEYS : <EOL> del self . data_payload [ key ] <EOL> del self . meta_payload [ key ] <EOL> elif key in self . METADATA_KEYS : <EOL> del self . meta_payload [ key ] <EOL> else : <EOL> del self . data_payload [ key ] <EOL> def __iter__ ( self ) : <EOL> for item in self . data_payload : <EOL> yield item <EOL> for item in self . meta_payload : <EOL> yield item <EOL> def __len__ ( self ) : <EOL> return len ( self . data_payload ) + len ( self . meta_payload ) <EOL> def emit ( self , log , config , emitters , continue_running , merge_payloads = True ) : <EOL> """<STR_LIT>""" <EOL> statuses = [ ] <EOL> def _emit_payload ( payload , endpoint ) : <EOL> """<STR_LIT>""" <EOL> statuses = [ ] <EOL> for emitter in emitters : <EOL> if not continue_running : <EOL> return statuses <EOL> name = emitter . __name__ <EOL> emitter_status = EmitterStatus ( name ) <EOL> try : <EOL> emitter ( payload , log , config , endpoint ) <EOL> except Exception , e : <EOL> log . exception ( "<STR_LIT>" <EOL> % emitter . __name__ ) <EOL> emitter_status = EmitterStatus ( name , e ) <EOL> statuses . append ( emitter_status ) <EOL> return statuses <EOL> if merge_payloads : <EOL> statuses . extend ( _emit_payload ( self . payload , self . COMMON_ENDPOINT ) ) <EOL> else : <EOL> statuses . extend ( _emit_payload ( self . data_payload , self . DATA_ENDPOINT ) ) <EOL> statuses . extend ( _emit_payload ( self . meta_payload , self . METADATA_ENDPOINT ) ) <EOL> return statuses <EOL> class Collector ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , agentConfig , emitters , systemStats , hostname ) : <EOL> self . emit_duration = None <EOL> self . agentConfig = agentConfig <EOL> self . hostname = hostname <EOL> self . agentConfig [ '<STR_LIT>' ] = systemStats <EOL> self . os = get_os ( ) <EOL> self . plugins = None <EOL> self . emitters = emitters <EOL> self . check_timings = agentConfig . get ( '<STR_LIT>' ) <EOL> self . push_times = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:start>' : time . time ( ) , <EOL> '<STR_LIT>' : int ( agentConfig . get ( '<STR_LIT>' , <NUM_LIT:4> * <NUM_LIT> * <NUM_LIT> ) ) <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:start>' : time . time ( ) - <NUM_LIT:3> * <NUM_LIT> , <EOL> '<STR_LIT>' : int ( agentConfig . get ( '<STR_LIT>' , <NUM_LIT:5> * <NUM_LIT> ) ) <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:start>' : time . time ( ) , <EOL> '<STR_LIT>' : int ( agentConfig . get ( '<STR_LIT>' , <NUM_LIT:10> * <NUM_LIT> ) ) <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:start>' : time . time ( ) , <EOL> '<STR_LIT>' : int ( agentConfig . get ( '<STR_LIT>' , <NUM_LIT:10> * <NUM_LIT> ) ) <EOL> } , <EOL> } <EOL> socket . setdefaulttimeout ( <NUM_LIT:15> ) <EOL> self . run_count = <NUM_LIT:0> <EOL> self . continue_running = True <EOL> self . hostname_metadata_cache = None <EOL> self . initialized_checks_d = [ ] <EOL> self . init_failed_checks_d = { } <EOL> self . _unix_system_checks = { <EOL> '<STR_LIT>' : u . IO ( log ) , <EOL> '<STR_LIT>' : u . Load ( log ) , <EOL> '<STR_LIT>' : u . Memory ( log ) , <EOL> '<STR_LIT>' : u . Processes ( log ) , <EOL> '<STR_LIT>' : u . Cpu ( log ) , <EOL> '<STR_LIT>' : u . System ( log ) <EOL> } <EOL> self . _server_density_checks = { <EOL> '<STR_LIT>' : yoshi . Identifier ( log ) , <EOL> '<STR_LIT>' : plugins . Plugins ( log ) <EOL> } <EOL> self . _win32_system_checks = { <EOL> '<STR_LIT>' : w32 . IO ( log ) , <EOL> '<STR_LIT>' : w32 . Processes ( log ) , <EOL> '<STR_LIT>' : w32 . Memory ( log ) , <EOL> '<STR_LIT>' : w32 . Network ( log ) , <EOL> '<STR_LIT>' : w32 . Cpu ( log ) <EOL> } <EOL> self . _ganglia = Ganglia ( log ) <EOL> self . _agent_metrics = None <EOL> self . _metrics_checks = [ ] <EOL> for module_spec in [ s . strip ( ) for s in self . agentConfig . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ] : <EOL> if len ( module_spec ) == <NUM_LIT:0> : <EOL> continue <EOL> try : <EOL> self . _metrics_checks . append ( modules . load ( module_spec , '<STR_LIT>' ) ( log ) ) <EOL> log . info ( "<STR_LIT>" % module_spec ) <EOL> log . warning ( "<STR_LIT>" ) <EOL> except Exception , e : <EOL> log . exception ( '<STR_LIT>' % module_spec ) <EOL> self . _resources_checks = [ <EOL> ResProcesses ( log , self . agentConfig ) <EOL> ] <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . continue_running = False <EOL> for check in self . initialized_checks_d : <EOL> check . stop ( ) <EOL> @ staticmethod <EOL> def _stats_for_display ( raw_stats ) : <EOL> return pprint . pformat ( raw_stats , indent = <NUM_LIT:4> ) <EOL> def run ( self , checksd = None , start_event = True ) : <EOL> """<STR_LIT>""" <EOL> timer = Timer ( ) <EOL> if self . os != '<STR_LIT>' : <EOL> cpu_clock = time . clock ( ) <EOL> self . run_count += <NUM_LIT:1> <EOL> log . debug ( "<STR_LIT>" % self . run_count ) <EOL> if checksd : <EOL> self . initialized_checks_d = checksd [ '<STR_LIT>' ] <EOL> self . init_failed_checks_d = checksd [ '<STR_LIT>' ] <EOL> payload = AgentPayload ( ) <EOL> if not self . _agent_metrics : <EOL> for check in self . initialized_checks_d : <EOL> if check . name == AGENT_METRICS_CHECK_NAME : <EOL> self . _agent_metrics = check <EOL> self . initialized_checks_d . remove ( check ) <EOL> break <EOL> self . _build_payload ( payload ) <EOL> metrics = payload [ '<STR_LIT>' ] <EOL> events = payload [ '<STR_LIT>' ] <EOL> service_checks = payload [ '<STR_LIT>' ] <EOL> if self . os == '<STR_LIT>' : <EOL> try : <EOL> metrics . extend ( self . _win32_system_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) ) <EOL> metrics . extend ( self . _win32_system_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) ) <EOL> metrics . extend ( self . _win32_system_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) ) <EOL> metrics . extend ( self . _win32_system_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) ) <EOL> metrics . extend ( self . _win32_system_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) ) <EOL> except Exception : <EOL> log . exception ( '<STR_LIT>' ) <EOL> else : <EOL> sd_checks = self . _server_density_checks <EOL> identifier = sd_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) <EOL> payload . update ( identifier ) <EOL> pluginsData = sd_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) <EOL> if pluginsData : <EOL> payload [ '<STR_LIT>' ] = pluginsData <EOL> sys_checks = self . _unix_system_checks <EOL> load = sys_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) <EOL> payload . update ( load ) <EOL> system = sys_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) <EOL> payload . update ( system ) <EOL> memory = sys_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) <EOL> if memory : <EOL> payload . update ( { <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : memory . get ( '<STR_LIT>' ) <EOL> } ) <EOL> ioStats = sys_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) <EOL> if ioStats : <EOL> payload [ '<STR_LIT>' ] = ioStats <EOL> processes = sys_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) <EOL> payload . update ( { '<STR_LIT>' : processes } ) <EOL> cpuStats = sys_checks [ '<STR_LIT>' ] . check ( self . agentConfig ) <EOL> if cpuStats : <EOL> payload . update ( cpuStats ) <EOL> gangliaData = self . _ganglia . check ( self . agentConfig ) <EOL> if gangliaData is not False and gangliaData is not None : <EOL> payload [ '<STR_LIT>' ] = gangliaData <EOL> if self . os != '<STR_LIT>' : <EOL> has_resource = False <EOL> for resources_check in self . _resources_checks : <EOL> resources_check . check ( ) <EOL> snaps = resources_check . pop_snapshots ( ) <EOL> if snaps : <EOL> has_resource = True <EOL> res_value = { <EOL> '<STR_LIT>' : snaps , <EOL> '<STR_LIT>' : resources_check . get_format_version ( ) <EOL> } <EOL> res_format = resources_check . describe_format_if_needed ( ) <EOL> if res_format is not None : <EOL> res_value [ '<STR_LIT>' ] = res_format <EOL> payload [ '<STR_LIT>' ] [ resources_check . RESOURCE_KEY ] = res_value <EOL> if has_resource : <EOL> payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : self . agentConfig [ '<STR_LIT>' ] , <EOL> '<STR_LIT:host>' : payload [ '<STR_LIT>' ] , <EOL> } <EOL> for metrics_check in self . _metrics_checks : <EOL> res = metrics_check . check ( self . agentConfig ) <EOL> if res : <EOL> metrics . extend ( res ) <EOL> check_statuses = [ ] <EOL> for check in self . initialized_checks_d : <EOL> if not self . continue_running : <EOL> return <EOL> log . info ( "<STR_LIT>" % check . name ) <EOL> instance_statuses = [ ] <EOL> metric_count = <NUM_LIT:0> <EOL> event_count = <NUM_LIT:0> <EOL> service_check_count = <NUM_LIT:0> <EOL> check_start_time = time . time ( ) <EOL> check_stats = None <EOL> try : <EOL> instance_statuses = check . run ( ) <EOL> current_check_metrics = check . get_metrics ( ) <EOL> current_check_events = check . get_events ( ) <EOL> check_stats = check . _get_internal_profiling_stats ( ) <EOL> current_check_metadata = check . get_service_metadata ( ) <EOL> metrics . extend ( current_check_metrics ) <EOL> if current_check_events : <EOL> if check . name not in events : <EOL> events [ check . name ] = current_check_events <EOL> else : <EOL> events [ check . name ] += current_check_events <EOL> metric_count = len ( current_check_metrics ) <EOL> event_count = len ( current_check_events ) <EOL> except Exception : <EOL> log . exception ( "<STR_LIT>" % check . name ) <EOL> check_status = CheckStatus ( <EOL> check . name , instance_statuses , metric_count , <EOL> event_count , service_check_count , service_metadata = current_check_metadata , <EOL> library_versions = check . get_library_info ( ) , <EOL> source_type_name = check . SOURCE_TYPE_NAME or check . name , <EOL> check_stats = check_stats <EOL> ) <EOL> service_check_tags = [ "<STR_LIT>" % check . name ] <EOL> if check_status . status == STATUS_OK : <EOL> status = AgentCheck . OK <EOL> elif check_status . status == STATUS_ERROR : <EOL> status = AgentCheck . CRITICAL <EOL> check . service_check ( '<STR_LIT>' , status , tags = service_check_tags ) <EOL> current_check_service_checks = check . get_service_checks ( ) <EOL> if current_check_service_checks : <EOL> service_checks . extend ( current_check_service_checks ) <EOL> service_check_count = len ( current_check_service_checks ) <EOL> check_status . service_check_count = service_check_count <EOL> check_statuses . append ( check_status ) <EOL> check_run_time = time . time ( ) - check_start_time <EOL> log . debug ( "<STR_LIT>" % ( check . name , check_run_time ) ) <EOL> if self . check_timings : <EOL> metric = '<STR_LIT>' <EOL> meta = { '<STR_LIT>' : [ "<STR_LIT>" % check . name ] } <EOL> metrics . append ( ( metric , time . time ( ) , check_run_time , meta ) ) <EOL> for check_name , info in self . init_failed_checks_d . iteritems ( ) : <EOL> if not self . continue_running : <EOL> return <EOL> check_status = CheckStatus ( check_name , None , None , None , None , <EOL> init_failed_error = info [ '<STR_LIT:error>' ] , <EOL> init_failed_traceback = info [ '<STR_LIT>' ] ) <EOL> check_statuses . append ( check_status ) <EOL> service_checks . append ( create_service_check ( '<STR_LIT>' , AgentCheck . OK , <EOL> hostname = self . hostname ) ) <EOL> payload [ '<STR_LIT>' ] = metrics <EOL> payload [ '<STR_LIT>' ] = events <EOL> payload [ '<STR_LIT>' ] = service_checks <EOL> self . _populate_payload_metadata ( payload , check_statuses , start_event ) <EOL> collect_duration = timer . step ( ) <EOL> if self . os != '<STR_LIT>' : <EOL> if self . _agent_metrics is not None : <EOL> self . _agent_metrics . set_metric_context ( payload , <EOL> { <EOL> '<STR_LIT>' : collect_duration , <EOL> '<STR_LIT>' : self . emit_duration , <EOL> '<STR_LIT>' : time . clock ( ) - cpu_clock <EOL> } ) <EOL> self . _agent_metrics . run ( ) <EOL> agent_stats = self . _agent_metrics . get_metrics ( ) <EOL> payload [ '<STR_LIT>' ] . extend ( agent_stats ) <EOL> if self . agentConfig . get ( '<STR_LIT>' , False ) : <EOL> log . info ( "<STR_LIT>" . format ( Collector . _stats_for_display ( agent_stats ) ) ) <EOL> else : <EOL> if self . _agent_metrics is not None : <EOL> self . _agent_metrics . set_metric_context ( payload , <EOL> { <EOL> '<STR_LIT>' : collect_duration , <EOL> '<STR_LIT>' : self . emit_duration , <EOL> } ) <EOL> self . _agent_metrics . run ( ) <EOL> agent_stats = self . _agent_metrics . get_metrics ( ) <EOL> payload [ '<STR_LIT>' ] . extend ( agent_stats ) <EOL> if self . agentConfig . get ( '<STR_LIT>' , False ) : <EOL> log . info ( "<STR_LIT>" . format ( Collector . _stats_for_display ( agent_stats ) ) ) <EOL> emitter_statuses = payload . emit ( log , self . agentConfig , self . emitters , <EOL> self . continue_running ) <EOL> self . emit_duration = timer . step ( ) <EOL> try : <EOL> CollectorStatus ( check_statuses , emitter_statuses , <EOL> self . hostname_metadata_cache ) . persist ( ) <EOL> except Exception : <EOL> log . exception ( "<STR_LIT>" ) <EOL> if self . run_count <= FLUSH_LOGGING_INITIAL or self . run_count % FLUSH_LOGGING_PERIOD == <NUM_LIT:0> : <EOL> log . info ( "<STR_LIT>" % <EOL> ( self . run_count , round ( collect_duration , <NUM_LIT:2> ) , round ( self . emit_duration , <NUM_LIT:2> ) ) ) <EOL> if self . run_count == FLUSH_LOGGING_INITIAL : <EOL> log . info ( "<STR_LIT>" % <EOL> FLUSH_LOGGING_PERIOD ) <EOL> else : <EOL> log . debug ( "<STR_LIT>" % <EOL> ( self . run_count , round ( collect_duration , <NUM_LIT:2> ) , round ( self . emit_duration , <NUM_LIT:2> ) ) ) <EOL> return payload <EOL> @ staticmethod <EOL> def run_single_check ( check , verbose = True ) : <EOL> log . info ( "<STR_LIT>" % check . name ) <EOL> instance_statuses = [ ] <EOL> metric_count = <NUM_LIT:0> <EOL> event_count = <NUM_LIT:0> <EOL> service_check_count = <NUM_LIT:0> <EOL> check_start_time = time . time ( ) <EOL> check_stats = None <EOL> try : <EOL> instance_statuses = check . run ( ) <EOL> current_check_metrics = check . get_metrics ( ) <EOL> current_check_events = check . get_events ( ) <EOL> current_service_checks = check . get_service_checks ( ) <EOL> current_service_metadata = check . get_service_metadata ( ) <EOL> check_stats = check . _get_internal_profiling_stats ( ) <EOL> metric_count = len ( current_check_metrics ) <EOL> event_count = len ( current_check_events ) <EOL> service_check_count = len ( current_service_checks ) <EOL> print "<STR_LIT>" . format ( pprint . pformat ( current_check_metrics ) ) <EOL> print "<STR_LIT>" . format ( pprint . pformat ( current_check_events ) ) <EOL> print "<STR_LIT>" . format ( pprint . pformat ( current_service_checks ) ) <EOL> print "<STR_LIT>" . format ( pprint . pformat ( current_service_metadata ) ) <EOL> except Exception : <EOL> log . exception ( "<STR_LIT>" % check . name ) <EOL> check_status = CheckStatus ( <EOL> check . name , instance_statuses , metric_count , <EOL> event_count , service_check_count , <EOL> library_versions = check . get_library_info ( ) , <EOL> source_type_name = check . SOURCE_TYPE_NAME or check . name , <EOL> check_stats = check_stats <EOL> ) <EOL> return check_status <EOL> def _emit ( self , payload ) : <EOL> """<STR_LIT>""" <EOL> statuses = [ ] <EOL> for emitter in self . emitters : <EOL> if not self . continue_running : <EOL> return statuses <EOL> name = emitter . __name__ <EOL> emitter_status = EmitterStatus ( name ) <EOL> try : <EOL> emitter ( payload , log , self . agentConfig ) <EOL> except Exception , e : <EOL> log . exception ( "<STR_LIT>" % emitter . __name__ ) <EOL> emitter_status = EmitterStatus ( name , e ) <EOL> statuses . append ( emitter_status ) <EOL> return statuses <EOL> def _is_first_run ( self ) : <EOL> return self . run_count <= <NUM_LIT:1> <EOL> def _build_payload ( self , payload ) : <EOL> """<STR_LIT>""" <EOL> now = time . time ( ) <EOL> payload [ '<STR_LIT>' ] = now <EOL> payload [ '<STR_LIT>' ] = self . os <EOL> payload [ '<STR_LIT>' ] = sys . version <EOL> payload [ '<STR_LIT>' ] = self . agentConfig [ '<STR_LIT:version>' ] <EOL> payload [ '<STR_LIT>' ] = self . agentConfig [ '<STR_LIT>' ] <EOL> payload [ '<STR_LIT>' ] = { } <EOL> payload [ '<STR_LIT>' ] = [ ] <EOL> payload [ '<STR_LIT>' ] = [ ] <EOL> payload [ '<STR_LIT>' ] = { } <EOL> payload [ '<STR_LIT>' ] = self . hostname <EOL> payload [ '<STR_LIT>' ] = get_uuid ( ) <EOL> payload [ '<STR_LIT>' ] = { } <EOL> payload [ '<STR_LIT>' ] = { } <EOL> def _populate_payload_metadata ( self , payload , check_statuses , start_event = True ) : <EOL> """<STR_LIT>""" <EOL> now = time . time ( ) <EOL> if start_event and self . _is_first_run ( ) : <EOL> payload [ '<STR_LIT>' ] = self . agentConfig . get ( '<STR_LIT>' , { } ) <EOL> payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ { <EOL> '<STR_LIT>' : self . agentConfig [ '<STR_LIT>' ] , <EOL> '<STR_LIT:host>' : payload [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : now , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % get_version ( ) <EOL> } ] <EOL> if self . _should_send_additional_data ( '<STR_LIT>' ) : <EOL> try : <EOL> if get_os ( ) != '<STR_LIT>' : <EOL> command = "<STR_LIT>" <EOL> else : <EOL> command = "<STR_LIT>" <EOL> gohai_metadata , gohai_log = subprocess . Popen ( <EOL> [ command ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE <EOL> ) . communicate ( ) <EOL> payload [ '<STR_LIT>' ] = gohai_metadata <EOL> if gohai_log : <EOL> log . warning ( "<STR_LIT>" . format ( gohai_log ) ) <EOL> except OSError as e : <EOL> if e . errno == <NUM_LIT:2> : <EOL> log . info ( "<STR_LIT>" ) <EOL> else : <EOL> raise e <EOL> except Exception as e : <EOL> log . warning ( "<STR_LIT>" % str ( e ) ) <EOL> payload [ '<STR_LIT>' ] = get_system_stats ( ) <EOL> payload [ '<STR_LIT>' ] = self . _get_hostname_metadata ( ) <EOL> self . hostname_metadata_cache = payload [ '<STR_LIT>' ] <EOL> host_tags = [ ] <EOL> if self . agentConfig [ '<STR_LIT>' ] is not None : <EOL> host_tags . extend ( [ unicode ( tag . strip ( ) ) <EOL> for tag in self . agentConfig [ '<STR_LIT>' ] . split ( "<STR_LIT:U+002C>" ) ] ) <EOL> if self . agentConfig [ '<STR_LIT>' ] : <EOL> host_tags . extend ( EC2 . get_tags ( self . agentConfig ) ) <EOL> if host_tags : <EOL> payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] = host_tags <EOL> GCE_tags = GCE . get_tags ( self . agentConfig ) <EOL> if GCE_tags is not None : <EOL> payload [ '<STR_LIT>' ] [ GCE . SOURCE_TYPE_NAME ] = GCE_tags <EOL> if self . _is_first_run ( ) : <EOL> log . info ( "<STR_LIT>" % <EOL> ( repr ( self . hostname_metadata_cache ) , payload [ '<STR_LIT>' ] ) ) <EOL> external_host_tags = [ ] <EOL> if self . _should_send_additional_data ( '<STR_LIT>' ) : <EOL> for check in self . initialized_checks_d : <EOL> try : <EOL> getter = getattr ( check , '<STR_LIT>' ) <EOL> check_tags = getter ( ) <EOL> external_host_tags . extend ( check_tags ) <EOL> except AttributeError : <EOL> pass <EOL> if external_host_tags : <EOL> payload [ '<STR_LIT>' ] = external_host_tags <EOL> if self . _should_send_additional_data ( '<STR_LIT>' ) : <EOL> agent_checks = [ ] <EOL> for check in check_statuses : <EOL> if check . instance_statuses is not None : <EOL> for i , instance_status in enumerate ( check . instance_statuses ) : <EOL> agent_checks . append ( <EOL> ( <EOL> check . name , check . source_type_name , <EOL> instance_status . instance_id , <EOL> instance_status . status , <EOL> instance_status . error or instance_status . warnings or "<STR_LIT>" , <EOL> check . service_metadata [ i ] <EOL> ) <EOL> ) <EOL> else : <EOL> agent_checks . append ( <EOL> ( <EOL> check . name , check . source_type_name , <EOL> "<STR_LIT>" , <EOL> check . status , repr ( check . init_failed_error ) <EOL> ) <EOL> ) <EOL> payload [ '<STR_LIT>' ] = agent_checks <EOL> payload [ '<STR_LIT>' ] = self . hostname_metadata_cache <EOL> if self . agentConfig [ '<STR_LIT>' ] and self . _should_send_additional_data ( '<STR_LIT>' ) : <EOL> app_tags_list = [ DD_CHECK_TAG . format ( c . name ) for c in self . initialized_checks_d ] <EOL> app_tags_list . extend ( [ DD_CHECK_TAG . format ( cname ) for cname <EOL> in JMXFiles . get_jmx_appnames ( ) ] ) <EOL> if '<STR_LIT>' not in payload [ '<STR_LIT>' ] : <EOL> payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ ] <EOL> payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] . extend ( app_tags_list ) <EOL> def _get_hostname_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> metadata = EC2 . get_metadata ( self . agentConfig ) <EOL> if metadata . get ( '<STR_LIT>' ) : <EOL> metadata [ '<STR_LIT>' ] = metadata . get ( '<STR_LIT>' ) <EOL> del metadata [ '<STR_LIT>' ] <EOL> if self . agentConfig . get ( '<STR_LIT>' ) : <EOL> metadata [ '<STR_LIT>' ] = self . agentConfig . get ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> metadata [ "<STR_LIT>" ] = socket . gethostname ( ) <EOL> except Exception : <EOL> pass <EOL> try : <EOL> metadata [ "<STR_LIT>" ] = socket . getfqdn ( ) <EOL> except Exception : <EOL> pass <EOL> metadata [ "<STR_LIT>" ] = get_hostname ( ) <EOL> host_aliases = GCE . get_host_aliases ( self . agentConfig ) <EOL> if host_aliases : <EOL> metadata [ '<STR_LIT>' ] = host_aliases <EOL> return metadata <EOL> def _should_send_additional_data ( self , data_name ) : <EOL> if self . _is_first_run ( ) : <EOL> return True <EOL> now = time . time ( ) <EOL> if now - self . push_times [ data_name ] [ '<STR_LIT:start>' ] >= self . push_times [ data_name ] [ '<STR_LIT>' ] : <EOL> log . debug ( '<STR_LIT>' % data_name ) <EOL> self . push_times [ data_name ] [ '<STR_LIT:start>' ] = now <EOL> return True <EOL> return False </s>
