<s> __author__ = '<STR_LIT>' <EOL> import errno <EOL> import os <EOL> import sys <EOL> import time <EOL> from __scalyr__ import SCALYR_VERSION , scalyr_init <EOL> scalyr_init ( ) <EOL> import scalyr_agent . scalyr_logging as scalyr_logging <EOL> import scalyr_agent . util as scalyr_util <EOL> import scalyr_agent . remote_shell as remote_shell <EOL> from scalyr_agent . monitors_manager import MonitorsManager <EOL> from scalyr_agent . scalyr_monitor import UnsupportedSystem <EOL> log = scalyr_logging . getLogger ( '<STR_LIT>' ) <EOL> scalyr_logging . set_log_destination ( use_stdout = True ) <EOL> from optparse import OptionParser <EOL> from scalyr_agent . scalyr_client import ScalyrClientSession <EOL> from scalyr_agent . copying_manager import CopyingManager <EOL> from scalyr_agent . configuration import Configuration <EOL> from scalyr_agent . util import RunState , ScriptEscalator <EOL> from scalyr_agent . agent_status import AgentStatus <EOL> from scalyr_agent . agent_status import ConfigStatus <EOL> from scalyr_agent . agent_status import OverallStats <EOL> from scalyr_agent . agent_status import report_status <EOL> from scalyr_agent . platform_controller import PlatformController , AgentAlreadyRunning , CannotExecuteAsUser <EOL> from scalyr_agent . platform_controller import AgentNotRunning <EOL> STATUS_FILE = '<STR_LIT>' <EOL> class ScalyrAgent ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , platform_controller ) : <EOL> """<STR_LIT>""" <EOL> self . __config = None <EOL> self . __controller = platform_controller <EOL> self . __escalator = None <EOL> self . __default_paths = platform_controller . default_paths <EOL> self . __config_file_path = None <EOL> self . __current_bad_config = None <EOL> self . __last_config_check_time = None <EOL> self . __start_time = None <EOL> self . __log_file_path = None <EOL> self . __copying_manager = None <EOL> self . __monitors_manager = None <EOL> self . __scalyr_client = None <EOL> self . __run_state = None <EOL> self . __unsafe_debugging_running = False <EOL> self . __debug_server = None <EOL> self . __last_verify_config = None <EOL> @ staticmethod <EOL> def agent_run_method ( controller , config_file_path , perform_config_check = False ) : <EOL> """<STR_LIT>""" <EOL> class Options ( object ) : <EOL> pass <EOL> my_options = Options ( ) <EOL> my_options . quiet = True <EOL> my_options . verbose = False <EOL> my_options . no_fork = True <EOL> my_options . no_change_user = True <EOL> if perform_config_check : <EOL> command = '<STR_LIT>' <EOL> else : <EOL> command = '<STR_LIT>' <EOL> return ScalyrAgent ( controller ) . main ( config_file_path , command , my_options ) <EOL> def main ( self , config_file_path , command , command_options ) : <EOL> """<STR_LIT>""" <EOL> quiet = command_options . quiet <EOL> verbose = command_options . verbose <EOL> no_fork = command_options . no_fork <EOL> if command == '<STR_LIT:version>' : <EOL> print '<STR_LIT>' % SCALYR_VERSION <EOL> return <NUM_LIT:0> <EOL> if config_file_path is None : <EOL> config_file_path = self . __default_paths . config_file_path <EOL> self . __config_file_path = config_file_path <EOL> try : <EOL> self . __config = self . __read_and_verify_config ( config_file_path ) <EOL> except Exception , e : <EOL> if command != '<STR_LIT>' and command != '<STR_LIT:status>' : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' % str ( e ) ) <EOL> else : <EOL> self . __config = None <EOL> print >> sys . stderr , '<STR_LIT>' % config_file_path <EOL> self . __controller . consume_config ( self . __config , config_file_path ) <EOL> self . __escalator = ScriptEscalator ( self . __controller , config_file_path , os . getcwd ( ) , command_options . no_change_user ) <EOL> try : <EOL> if command == '<STR_LIT:start>' : <EOL> return self . __start ( quiet , no_fork ) <EOL> elif command == '<STR_LIT>' : <EOL> self . __perform_config_checks ( ) <EOL> return self . __run ( self . __controller ) <EOL> elif command == '<STR_LIT>' : <EOL> return self . __run ( self . __controller ) <EOL> elif command == '<STR_LIT>' : <EOL> return self . __stop ( quiet ) <EOL> elif command == '<STR_LIT:status>' and not verbose : <EOL> return self . __status ( ) <EOL> elif command == '<STR_LIT:status>' and verbose : <EOL> if self . __config is not None : <EOL> agent_data_path = self . __config . agent_data_path <EOL> else : <EOL> agent_data_path = self . __default_paths . agent_data_path <EOL> print >> sys . stderr , '<STR_LIT>' % agent_data_path <EOL> return self . __detailed_status ( agent_data_path ) <EOL> elif command == '<STR_LIT>' : <EOL> return self . __restart ( quiet , no_fork ) <EOL> elif command == '<STR_LIT>' : <EOL> return self . __condrestart ( quiet , no_fork ) <EOL> else : <EOL> print >> sys . stderr , '<STR_LIT>' % command <EOL> return <NUM_LIT:1> <EOL> except SystemExit : <EOL> return <NUM_LIT:0> <EOL> except Exception , e : <EOL> if command == '<STR_LIT>' : <EOL> raise e <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % <EOL> ( command , str ( e ) ) ) <EOL> def __read_and_verify_config ( self , config_file_path ) : <EOL> """<STR_LIT>""" <EOL> config = self . __read_config ( config_file_path ) <EOL> self . __verify_config ( config ) <EOL> return config <EOL> def __read_config ( self , config_file_path ) : <EOL> """<STR_LIT>""" <EOL> return Configuration ( config_file_path , self . __default_paths ) <EOL> def __verify_config ( self , config ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> config . parse ( ) <EOL> monitors_manager = MonitorsManager ( config , self . __controller ) <EOL> copying_manager = CopyingManager ( config , monitors_manager . monitors ) <EOL> self . __last_verify_config = { <EOL> '<STR_LIT>' : config , <EOL> '<STR_LIT>' : monitors_manager , <EOL> '<STR_LIT>' : copying_manager <EOL> } <EOL> except UnsupportedSystem , e : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( e . monitor_name , str ( e ) ) ) <EOL> def __create_worker_thread ( self , config ) : <EOL> """<STR_LIT>""" <EOL> if self . __last_verify_config is None or self . __last_verify_config [ '<STR_LIT>' ] is not config : <EOL> self . __verify_config ( config ) <EOL> return WorkerThread ( self . __last_verify_config [ '<STR_LIT>' ] , self . __last_verify_config [ '<STR_LIT>' ] , <EOL> self . __last_verify_config [ '<STR_LIT>' ] ) <EOL> def __perform_config_checks ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __config . api_key == '<STR_LIT>' or self . __config . api_key == '<STR_LIT>' : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> self . __config . file_path ) <EOL> self . __verify_can_write_to_logs_and_data ( self . __config ) <EOL> client = self . __create_client ( quiet = True ) <EOL> try : <EOL> ping_result = client . ping ( ) <EOL> if ping_result != '<STR_LIT:success>' : <EOL> if '<STR_LIT>' in ping_result : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in ping_result : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % self . __config . file_path ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> finally : <EOL> client . close ( ) <EOL> def __start ( self , quiet , no_fork ) : <EOL> """<STR_LIT>""" <EOL> if self . __escalator . is_user_change_required ( ) : <EOL> return self . __escalator . change_user_and_rerun_script ( '<STR_LIT>' ) <EOL> self . __fail_if_already_running ( ) <EOL> try : <EOL> self . __perform_config_checks ( ) <EOL> except Exception , e : <EOL> print >> sys . stderr <EOL> print >> sys . stderr , '<STR_LIT:%s>' % str ( e ) <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:1> <EOL> if not no_fork : <EOL> self . __fail_if_already_running ( ) <EOL> if not quiet : <EOL> print "<STR_LIT>" <EOL> self . __controller . start_agent_service ( self . __run , quiet , fork = True ) <EOL> else : <EOL> self . __controller . start_agent_service ( self . __run , quiet , fork = False ) <EOL> return <NUM_LIT:0> <EOL> def __handle_terminate ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __run_state . is_running ( ) : <EOL> log . info ( '<STR_LIT>' ) <EOL> self . __run_state . stop ( ) <EOL> def __detailed_status ( self , data_directory ) : <EOL> """<STR_LIT>""" <EOL> if self . __escalator . is_user_change_required ( ) : <EOL> try : <EOL> return self . __escalator . change_user_and_rerun_script ( '<STR_LIT>' , handle_error = False ) <EOL> except CannotExecuteAsUser : <EOL> pass <EOL> try : <EOL> self . __controller . is_agent_running ( fail_if_not_running = True ) <EOL> except AgentNotRunning , e : <EOL> print '<STR_LIT>' <EOL> print "<STR_LIT:%s>" % str ( e ) <EOL> return <NUM_LIT:1> <EOL> if not os . path . isdir ( data_directory ) : <EOL> print >> sys . stderr , ( '<STR_LIT>' % <EOL> data_directory ) <EOL> return <NUM_LIT:1> <EOL> status_file = os . path . join ( data_directory , STATUS_FILE ) <EOL> if os . path . isfile ( status_file ) and not os . access ( status_file , os . W_OK ) : <EOL> print >> sys . stderr , ( '<STR_LIT>' <EOL> '<STR_LIT>' % status_file ) <EOL> return <NUM_LIT:1> <EOL> if os . path . isfile ( status_file ) : <EOL> f = file ( status_file , '<STR_LIT:w>' ) <EOL> f . truncate ( <NUM_LIT:0> ) <EOL> f . close ( ) <EOL> result = self . __controller . request_agent_status ( ) <EOL> if result is not None : <EOL> if result == errno . ESRCH : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:1> <EOL> elif result == errno . EPERM : <EOL> print >> sys . stderr , ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return <NUM_LIT:2> <EOL> deadline = time . time ( ) + <NUM_LIT:5> <EOL> while True : <EOL> if os . path . isfile ( status_file ) and os . path . getsize ( status_file ) > <NUM_LIT:0> : <EOL> break <EOL> if time . time ( ) > deadline : <EOL> if self . __config is not None : <EOL> agent_log = os . path . join ( self . __config . agent_log_path , '<STR_LIT>' ) <EOL> else : <EOL> agent_log = os . path . join ( self . __default_paths . agent_log_path , '<STR_LIT>' ) <EOL> print >> sys . stderr , ( '<STR_LIT>' <EOL> '<STR_LIT>' % agent_log ) <EOL> return <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT> ) <EOL> if not os . access ( status_file , os . R_OK ) : <EOL> print >> sys . stderr , ( '<STR_LIT>' <EOL> '<STR_LIT>' % status_file ) <EOL> return <NUM_LIT:1> <EOL> fp = open ( status_file ) <EOL> for line in fp : <EOL> print line . rstrip ( ) <EOL> fp . close ( ) <EOL> return <NUM_LIT:0> <EOL> def __stop ( self , quiet ) : <EOL> """<STR_LIT>""" <EOL> if self . __escalator . is_user_change_required ( ) : <EOL> return self . __escalator . change_user_and_rerun_script ( '<STR_LIT>' ) <EOL> try : <EOL> self . __controller . is_agent_running ( fail_if_not_running = True ) <EOL> status = self . __controller . stop_agent_service ( quiet ) <EOL> return status <EOL> except AgentNotRunning , e : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> print >> sys . stderr , "<STR_LIT:%s>" % str ( e ) <EOL> return <NUM_LIT:0> <EOL> def __status ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __controller . is_agent_running ( ) : <EOL> print '<STR_LIT>' <EOL> return <NUM_LIT:0> <EOL> else : <EOL> print '<STR_LIT>' <EOL> return <NUM_LIT:4> <EOL> def __condrestart ( self , quiet , no_fork ) : <EOL> """<STR_LIT>""" <EOL> if self . __escalator . is_user_change_required ( ) : <EOL> return self . __escalator . change_user_and_rerun_script ( '<STR_LIT>' ) <EOL> if self . __controller . is_agent_running ( ) : <EOL> if not quiet : <EOL> print '<STR_LIT>' <EOL> if self . __stop ( quiet ) != <NUM_LIT:0> : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:1> <EOL> return self . __start ( quiet , no_fork ) <EOL> elif not quiet : <EOL> print '<STR_LIT>' <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def __restart ( self , quiet , no_fork ) : <EOL> """<STR_LIT>""" <EOL> if self . __escalator . is_user_change_required ( ) : <EOL> return self . __escalator . change_user_and_rerun_script ( '<STR_LIT>' ) <EOL> if self . __controller . is_agent_running ( ) : <EOL> if not quiet : <EOL> print '<STR_LIT>' <EOL> if self . __stop ( quiet ) != <NUM_LIT:0> : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:1> <EOL> return self . __start ( quiet , no_fork ) <EOL> def __run ( self , controller ) : <EOL> """<STR_LIT>""" <EOL> self . __start_time = time . time ( ) <EOL> controller . register_for_termination ( self . __handle_terminate ) <EOL> self . __controller . register_for_status_requests ( self . __report_status_to_file ) <EOL> base_overall_stats = OverallStats ( ) <EOL> last_overall_stats_report_time = time . time ( ) <EOL> last_bw_stats_report_time = last_overall_stats_report_time <EOL> worker_thread = None <EOL> try : <EOL> try : <EOL> self . __run_state = RunState ( ) <EOL> self . __log_file_path = os . path . join ( self . __config . agent_log_path , '<STR_LIT>' ) <EOL> scalyr_logging . set_log_destination ( use_disk = True , logs_directory = self . __config . agent_log_path , <EOL> agent_log_file_path = '<STR_LIT>' ) <EOL> self . __update_debug_log_level ( self . __config . debug_level ) <EOL> agent_log_position = self . __get_file_initial_position ( self . __log_file_path ) <EOL> if agent_log_position is not None : <EOL> logs_initial_positions = { self . __log_file_path : agent_log_position } <EOL> else : <EOL> logs_initial_positions = None <EOL> log . info ( '<STR_LIT>' % ( SCALYR_VERSION , scalyr_util . get_pid_tid ( ) ) ) <EOL> log . log ( scalyr_logging . DEBUG_LEVEL_1 , '<STR_LIT>' % ( <EOL> SCALYR_VERSION , scalyr_util . get_pid_tid ( ) ) ) <EOL> self . __controller . emit_init_log ( log , self . __config . debug_init ) <EOL> self . __start_or_stop_unsafe_debugging ( ) <EOL> self . __scalyr_client = self . __create_client ( ) <EOL> worker_thread = self . __create_worker_thread ( self . __config ) <EOL> worker_thread . start ( self . __scalyr_client , logs_initial_positions ) <EOL> self . __copying_manager = worker_thread . copying_manager <EOL> self . __monitors_manager = worker_thread . monitors_manager <EOL> while not self . __run_state . sleep_but_awaken_if_stopped ( <NUM_LIT:30> ) : <EOL> current_time = time . time ( ) <EOL> self . __last_config_check_time = current_time <EOL> if current_time > last_overall_stats_report_time + <NUM_LIT> : <EOL> self . __log_overall_stats ( self . __calculate_overall_stats ( base_overall_stats ) ) <EOL> last_overall_stats_report_time = current_time <EOL> if current_time > last_bw_stats_report_time + <NUM_LIT> : <EOL> self . __log_bandwidth_stats ( self . __calculate_overall_stats ( base_overall_stats ) ) <EOL> last_bw_stats_report_time = current_time <EOL> log . log ( scalyr_logging . DEBUG_LEVEL_1 , '<STR_LIT>' ) <EOL> new_config = None <EOL> try : <EOL> new_config = self . __read_config ( self . __config_file_path ) <EOL> self . __verify_config ( new_config ) <EOL> self . __update_debug_log_level ( new_config . debug_level ) <EOL> if self . __current_bad_config is None and new_config . equivalent ( self . __config , <EOL> exclude_debug_level = True ) : <EOL> log . log ( scalyr_logging . DEBUG_LEVEL_1 , '<STR_LIT>' ) <EOL> continue <EOL> self . __verify_can_write_to_logs_and_data ( new_config ) <EOL> except Exception , e : <EOL> if self . __current_bad_config is None : <EOL> log . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , str ( e ) , error_code = '<STR_LIT>' ) <EOL> self . __current_bad_config = new_config <EOL> log . log ( scalyr_logging . DEBUG_LEVEL_1 , '<STR_LIT>' ) <EOL> continue <EOL> log . log ( scalyr_logging . DEBUG_LEVEL_1 , '<STR_LIT>' ) <EOL> base_overall_stats = self . __calculate_overall_stats ( base_overall_stats ) <EOL> log . info ( '<STR_LIT>' ) <EOL> log . info ( '<STR_LIT>' ) <EOL> worker_thread . stop ( ) <EOL> worker_thread = None <EOL> self . __config = new_config <EOL> self . __controller . consume_config ( new_config , new_config . file_path ) <EOL> self . __start_or_stop_unsafe_debugging ( ) <EOL> self . __scalyr_client = self . __create_client ( ) <EOL> log . info ( '<STR_LIT>' ) <EOL> worker_thread = self . __create_worker_thread ( new_config ) <EOL> worker_thread . start ( self . __scalyr_client ) <EOL> self . __copying_manager = worker_thread . copying_manager <EOL> self . __monitors_manager = worker_thread . monitors_manager <EOL> self . __current_bad_config = None <EOL> self . __log_overall_stats ( self . __calculate_overall_stats ( base_overall_stats ) ) <EOL> except Exception : <EOL> log . exception ( '<STR_LIT>' , error_code = '<STR_LIT>' ) <EOL> finally : <EOL> if worker_thread is not None : <EOL> worker_thread . stop ( ) <EOL> def __fail_if_already_running ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . __controller . is_agent_running ( fail_if_running = True ) <EOL> except AgentAlreadyRunning , e : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> print >> sys . stderr , "<STR_LIT:%s>" % str ( e ) <EOL> sys . exit ( <NUM_LIT:4> ) <EOL> def __update_debug_log_level ( self , debug_level ) : <EOL> """<STR_LIT>""" <EOL> levels = [ scalyr_logging . DEBUG_LEVEL_0 , scalyr_logging . DEBUG_LEVEL_1 , scalyr_logging . DEBUG_LEVEL_2 , <EOL> scalyr_logging . DEBUG_LEVEL_3 , scalyr_logging . DEBUG_LEVEL_4 , scalyr_logging . DEBUG_LEVEL_5 ] <EOL> scalyr_logging . set_log_level ( levels [ debug_level ] ) <EOL> def __create_client ( self , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> if self . __config . verify_server_certificate : <EOL> ca_file = self . __config . ca_cert_path <EOL> else : <EOL> ca_file = None <EOL> use_requests_lib = self . __config . use_requests_lib <EOL> return ScalyrClientSession ( self . __config . scalyr_server , self . __config . api_key , SCALYR_VERSION , quiet = quiet , <EOL> request_deadline = self . __config . request_deadline , ca_file = ca_file , use_requests_lib = use_requests_lib ) <EOL> def __get_file_initial_position ( self , path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return os . path . getsize ( path ) <EOL> except OSError , e : <EOL> if e . errno == errno . EPERM : <EOL> log . warn ( '<STR_LIT>' ) <EOL> return None <EOL> elif e . errno == errno . ENOENT : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> log . exception ( '<STR_LIT>' ) <EOL> return None <EOL> def __verify_can_write_to_logs_and_data ( self , config ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . isdir ( config . agent_log_path ) : <EOL> raise Exception ( '<STR_LIT>' % config . agent_log_path ) <EOL> if not os . access ( config . agent_log_path , os . W_OK ) : <EOL> raise Exception ( '<STR_LIT>' % config . agent_log_path ) <EOL> if not os . path . isdir ( config . agent_data_path ) : <EOL> raise Exception ( '<STR_LIT>' % config . agent_data_path ) <EOL> if not os . access ( config . agent_data_path , os . W_OK ) : <EOL> raise Exception ( '<STR_LIT>' % config . agent_data_path ) <EOL> def __start_or_stop_unsafe_debugging ( self ) : <EOL> """<STR_LIT>""" <EOL> should_be_running = self . __config . use_unsafe_debugging <EOL> if should_be_running and not self . __unsafe_debugging_running : <EOL> self . __unsafe_debugging_running = True <EOL> self . __debug_server = remote_shell . DebugServer ( ) <EOL> self . __debug_server . start ( ) <EOL> elif not should_be_running and self . __unsafe_debugging_running : <EOL> if self . __debug_server is not None : <EOL> self . __debug_server . stop ( ) <EOL> self . __debug_server = None <EOL> self . __unsafe_debugging_running = False <EOL> def __generate_status ( self ) : <EOL> """<STR_LIT>""" <EOL> result = AgentStatus ( ) <EOL> result . launch_time = self . __start_time <EOL> result . user = self . __controller . get_current_user ( ) <EOL> result . version = SCALYR_VERSION <EOL> result . server_host = self . __config . server_attributes [ '<STR_LIT>' ] <EOL> result . scalyr_server = self . __config . scalyr_server <EOL> result . log_path = self . __log_file_path <EOL> config_result = ConfigStatus ( ) <EOL> result . config_status = config_result <EOL> config_result . last_check_time = self . __last_config_check_time <EOL> if self . __current_bad_config is not None : <EOL> config_result . path = self . __current_bad_config . file_path <EOL> config_result . additional_paths = list ( self . __current_bad_config . additional_file_paths ) <EOL> config_result . last_read_time = self . __current_bad_config . read_time <EOL> config_result . status = '<STR_LIT>' <EOL> config_result . last_error = self . __current_bad_config . last_error <EOL> config_result . last_good_read = self . __config . read_time <EOL> config_result . last_check_time = self . __last_config_check_time <EOL> else : <EOL> config_result . path = self . __config . file_path <EOL> config_result . additional_paths = list ( self . __config . additional_file_paths ) <EOL> config_result . last_read_time = self . __config . read_time <EOL> config_result . status = '<STR_LIT>' <EOL> config_result . last_error = None <EOL> config_result . last_good_read = self . __config . read_time <EOL> if self . __copying_manager is not None : <EOL> result . copying_manager_status = self . __copying_manager . generate_status ( ) <EOL> if self . __monitors_manager is not None : <EOL> result . monitor_manager_status = self . __monitors_manager . generate_status ( ) <EOL> return result <EOL> def __log_overall_stats ( self , overall_stats ) : <EOL> """<STR_LIT>""" <EOL> stats = overall_stats <EOL> log . info ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( scalyr_util . format_time ( stats . launch_time ) , <EOL> stats . version , stats . num_watched_paths , <EOL> stats . num_copying_paths , stats . total_bytes_copied , <EOL> stats . total_bytes_skipped , stats . total_bytes_subsampled , <EOL> stats . total_redactions , stats . total_bytes_failed , <EOL> stats . total_copy_requests_errors , <EOL> stats . total_monitor_reported_lines , <EOL> stats . num_running_monitors , <EOL> stats . num_dead_monitor , stats . user_cpu , <EOL> stats . system_cpu , stats . rss_size ) ) <EOL> def __log_bandwidth_stats ( self , overall_stats ) : <EOL> """<STR_LIT>""" <EOL> stats = overall_stats <EOL> if self . __config . debug_init : <EOL> extra = '<STR_LIT>' % self . __controller . is_agent ( ) <EOL> else : <EOL> extra = '<STR_LIT>' <EOL> log . info ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( stats . total_requests_sent , <EOL> stats . total_requests_failed , <EOL> stats . total_request_bytes_sent , <EOL> stats . total_response_bytes_received , <EOL> stats . total_request_latency_secs , <EOL> stats . total_connections_created , extra ) ) <EOL> def __calculate_overall_stats ( self , base_overall_stats ) : <EOL> """<STR_LIT>""" <EOL> current_status = self . __generate_status ( ) <EOL> delta_stats = OverallStats ( ) <EOL> watched_paths = <NUM_LIT:0> <EOL> copying_paths = <NUM_LIT:0> <EOL> if current_status . copying_manager_status is not None : <EOL> delta_stats . total_copy_requests_errors = current_status . copying_manager_status . total_errors <EOL> watched_paths = len ( current_status . copying_manager_status . log_matchers ) <EOL> for matcher in current_status . copying_manager_status . log_matchers : <EOL> copying_paths += len ( matcher . log_processors_status ) <EOL> for processor_status in matcher . log_processors_status : <EOL> delta_stats . total_bytes_copied += processor_status . total_bytes_copied <EOL> delta_stats . total_bytes_skipped += processor_status . total_bytes_skipped <EOL> delta_stats . total_bytes_subsampled += processor_status . total_bytes_dropped_by_sampling <EOL> delta_stats . total_bytes_failed += processor_status . total_bytes_failed <EOL> delta_stats . total_redactions += processor_status . total_redactions <EOL> running_monitors = <NUM_LIT:0> <EOL> dead_monitors = <NUM_LIT:0> <EOL> if current_status . monitor_manager_status is not None : <EOL> running_monitors = current_status . monitor_manager_status . total_alive_monitors <EOL> dead_monitors = len ( current_status . monitor_manager_status . monitors_status ) - running_monitors <EOL> for monitor_status in current_status . monitor_manager_status . monitors_status : <EOL> delta_stats . total_monitor_reported_lines += monitor_status . reported_lines <EOL> delta_stats . total_monitor_errors += monitor_status . errors <EOL> delta_stats . total_requests_sent = self . __scalyr_client . total_requests_sent <EOL> delta_stats . total_requests_failed = self . __scalyr_client . total_requests_failed <EOL> delta_stats . total_request_bytes_sent = self . __scalyr_client . total_request_bytes_sent <EOL> delta_stats . total_response_bytes_received = self . __scalyr_client . total_response_bytes_received <EOL> delta_stats . total_request_latency_secs = self . __scalyr_client . total_request_latency_secs <EOL> delta_stats . total_connections_created = self . __scalyr_client . total_connections_created <EOL> result = delta_stats + base_overall_stats <EOL> result . launch_time = current_status . launch_time <EOL> result . version = current_status . version <EOL> result . num_watched_paths = watched_paths <EOL> result . num_copying_paths = copying_paths <EOL> result . num_running_monitors = running_monitors <EOL> result . num_dead_monitors = dead_monitors <EOL> ( result . user_cpu , result . system_cpu , result . rss_size ) = self . __controller . get_usage_info ( ) <EOL> return result <EOL> def __report_status_to_file ( self ) : <EOL> """<STR_LIT>""" <EOL> tmp_file = None <EOL> try : <EOL> tmp_file_path = os . path . join ( self . __config . agent_data_path , '<STR_LIT>' ) <EOL> final_file_path = os . path . join ( self . __config . agent_data_path , '<STR_LIT>' ) <EOL> if os . path . isfile ( final_file_path ) : <EOL> os . remove ( final_file_path ) <EOL> tmp_file = open ( tmp_file_path , '<STR_LIT:w>' ) <EOL> report_status ( tmp_file , self . __generate_status ( ) , time . time ( ) ) <EOL> tmp_file . close ( ) <EOL> tmp_file = None <EOL> os . rename ( tmp_file_path , final_file_path ) <EOL> except ( OSError , IOError ) : <EOL> log . exception ( '<STR_LIT>' , error_code = '<STR_LIT>' ) <EOL> if tmp_file is not None : <EOL> tmp_file . close ( ) <EOL> class WorkerThread ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , configuration , copying_manager , monitors ) : <EOL> self . __scalyr_client = None <EOL> self . config = configuration <EOL> self . copying_manager = copying_manager <EOL> self . monitors_manager = monitors <EOL> def start ( self , scalyr_client , log_initial_positions = None ) : <EOL> if self . __scalyr_client is not None : <EOL> self . __scalyr_client . close ( ) <EOL> self . __scalyr_client = scalyr_client <EOL> self . copying_manager . start_manager ( scalyr_client , log_initial_positions ) <EOL> self . copying_manager . wait_for_copying_to_begin ( ) <EOL> self . monitors_manager . start_manager ( ) <EOL> def stop ( self ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . monitors_manager . stop_manager ( ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> self . copying_manager . stop_manager ( ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> if self . __scalyr_client is not None : <EOL> self . __scalyr_client . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> my_controller = PlatformController . new_platform ( ) <EOL> parser = OptionParser ( usage = '<STR_LIT>' , <EOL> version = '<STR_LIT>' + SCALYR_VERSION ) <EOL> parser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , metavar = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> my_controller . add_options ( parser ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> my_controller . consume_options ( options ) <EOL> if len ( args ) < <NUM_LIT:1> : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> parser . print_help ( sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> elif len ( args ) > <NUM_LIT:1> : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> parser . print_help ( sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> elif args [ <NUM_LIT:0> ] not in ( '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:version>' ) : <EOL> print >> sys . stderr , '<STR_LIT>' % args [ <NUM_LIT:0> ] <EOL> parser . print_help ( sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if options . config_filename is not None and not os . path . isabs ( options . config_filename ) : <EOL> options . config_filename = os . path . abspath ( options . config_filename ) <EOL> main_rc = <NUM_LIT:1> <EOL> try : <EOL> main_rc = ScalyrAgent ( my_controller ) . main ( options . config_filename , args [ <NUM_LIT:0> ] , options ) <EOL> except Exception , mainExcept : <EOL> print >> sys . stderr , str ( mainExcept ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> sys . exit ( main_rc ) </s>
