'Main loop of the collector'

def run(self, config=None):

    signal.signal(signal.SIGTERM, self._handle_sigterm)
    if (not Platform.is_windows()):
        signal.signal(signal.SIGUSR1, self._handle_sigusr1)
        signal.signal(signal.SIGINT, self._handle_sigterm)
        signal.signal(signal.SIGHUP, self._handle_sighup)
    else:
        sdk_integrations = get_sdk_integration_paths()
        for (name, path) in sdk_integrations.iteritems():
            lib_path = os.path.join(path, 'lib')
            if os.path.exists(lib_path):
                sys.path.append(lib_path)
    CollectorStatus().persist()
    if (not config):
        config = get_config(parse_args=True)
    self._agentConfig = self._set_agent_config_hostname(config)
    hostname = get_hostname(self._agentConfig)
    systemStats = get_system_stats(proc_path=self._agentConfig.get('procfs_path', '/proc').rstrip('/'))
    emitters = self._get_emitters()
    if self._agentConfig.get('service_discovery'):
        self.sd_backend = get_sd_backend(self._agentConfig)
    if (self.sd_backend and _is_affirmative(self._agentConfig.get('sd_jmx_enable', False))):
        pipe_path = get_jmx_pipe_path()
        if Platform.is_windows():
            pipe_name = pipe_path.format(pipename=SD_PIPE_NAME)
        else:
            pipe_name = os.path.join(pipe_path, SD_PIPE_NAME)
        if os.access(pipe_path, os.W_OK):
            if (not os.path.exists(pipe_name)):
                os.mkfifo(pipe_name)
            self.sd_pipe = os.open(pipe_name, os.O_RDWR)
            self.supervisor_proxy = self._get_supervisor_socket(self._agentConfig)
        else:
            log.debug('Unable    to    create    pipe    in    temporary    directory.    JMX    service    discovery    disabled.')
    self._checksd = load_check_directory(self._agentConfig, hostname)
    if self._jmx_service_discovery_enabled:
        self.sd_pipe_jmx_configs(hostname)
    self.collector = Collector(self._agentConfig, emitters, systemStats, hostname)
    try:
        self.collector_profile_interval = int(self._agentConfig.get('collector_profile_interval', DEFAULT_COLLECTOR_PROFILE_INTERVAL))
    except ValueError:
        log.warn(('collector_profile_interval    is    invalid.    Using    default    value    instead    (%s).' % DEFAULT_COLLECTOR_PROFILE_INTERVAL))
        self.collector_profile_interval = DEFAULT_COLLECTOR_PROFILE_INTERVAL
    self.check_frequency = int(self._agentConfig['check_freq'])
    watchdog = self._get_watchdog(self.check_frequency)
    self.restart_interval = int(self._agentConfig.get('restart_interval', RESTART_INTERVAL))
    self.agent_start = time.time()
    self.allow_profiling = self._agentConfig.get('allow_profiling', True)
    profiled = False
    collector_profiled_runs = 0
    while self.run_forever:
        if (self.allow_profiling and self.in_developer_mode and (not profiled)):
            try:
                profiler = AgentProfiler()
                profiler.enable_profiling()
                profiled = True
            except Exception as e:
                log.warn(('Cannot    enable    profiler:    %s' % str(e)))
        if self.reload_configs_flag:
            if isinstance(self.reload_configs_flag, set):
                self.reload_configs(checks_to_reload=self.reload_configs_flag)
            else:
                self.reload_configs()
        if (self._jmx_service_discovery_enabled and ((not self.reload_configs_flag) or isinstance(self.reload_configs_flag, set))):
            try:
                jmx_launch = JMXFetch._get_jmx_launchtime()
                if (self.last_jmx_piped and (self.last_jmx_piped < jmx_launch)):
                    self.sd_pipe_jmx_configs(hostname)
            except Exception as e:
                log.debug('could    not    stat    JMX    lunch    file:    %s', e)
        self.collector.run(checksd=self._checksd, start_event=self.start_event, configs_reloaded=(True if self.reload_configs_flag else False))
        self.reload_configs_flag = False
        if (self._agentConfig.get('service_discovery') and self.sd_backend and (not self.sd_backend.reload_check_configs)):
            try:
                self.sd_backend.reload_check_configs = get_config_store(self._agentConfig).crawl_config_template()
            except Exception as e:
                log.warn(('Something    went    wrong    while    looking    for    config    template    changes:    %s' % str(e)))
        if (self._agentConfig.get('service_discovery') and self.sd_backend and self.sd_backend.reload_check_configs):
            self.reload_configs_flag = self.sd_backend.reload_check_configs
            self.sd_backend.reload_check_configs = False
        if profiled:
            if (collector_profiled_runs >= self.collector_profile_interval):
                try:
                    profiler.disable_profiling()
                    profiled = False
                    collector_profiled_runs = 0
                except Exception as e:
                    log.warn(('Cannot    disable    profiler:    %s' % str(e)))
        if (self.autorestart and self._should_restart()):
            self._do_restart()
        if self.run_forever:
            if watchdog:
                watchdog.reset()
            if profiled:
                collector_profiled_runs += 1
            log.debug('Sleeping    for    {0}    seconds'.format(self.check_frequency))
            time.sleep(self.check_frequency)
    try:
        CollectorStatus.remove_latest_status()
    except Exception:
        pass
    log.info('Exiting.    Bye    bye.')
    sys.exit(0)
