def __init__(self, agent_config, emitter, checksd): super(Collector, self).__init__(agent_config) self.agent_config = agent_config self.os = util.get_os() self.plugins = None self.emitter = emitter socket.setdefaulttimeout(15) self.run_count = 0 self.continue_running = True self.collection_metrics = {} # is of type {check_name: check} initialized_checks_d = checksd['initialized_checks'] self.pool_size = int(self.agent_config.get('num_collector_threads', 1)) log.info('Using %d Threads for Collector' % self.pool_size) self.pool = Pool(self.pool_size) self.pool_full_count = 0 self.collection_times = {} self.collection_results = {} for check in initialized_checks_d: self.collection_times[check.name] = {'check': check, 'last_collect_time': 99999999} self.pool_full_max_retries = int(self.agent_config.get('pool_full_max_retries', 4))
def __init__(self, agent_config, emitter, checksd=None): super(Collector, self).__init__(agent_config) self.agent_config = agent_config self.os = util.get_os() self.plugins = None self.emitter = emitter socket.setdefaulttimeout(15) self.run_count = 0 self.continue_running = True self.initialized_checks_d = [] self.init_failed_checks_d = [] # add windows system checks if self.os == 'windows': self._checks = [w32.Disk(log), w32.IO(log), w32.Processes(log), w32.Memory(log), w32.Network(log), w32.Cpu(log)] else: self._checks = [] if checksd: # is of type {check_name: check} self.initialized_checks_d = checksd['initialized_checks'] # is of type {check_name: {error, traceback}} self.init_failed_checks_d = checksd['init_failed_checks']
def kill_subprocess(process_obj): try: process_obj.terminate() except AttributeError: # py < 2.6 doesn't support process.terminate() if get_os() == "windows": import ctypes PROCESS_TERMINATE = 1 handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process_obj.pid) ctypes.windll.kernel32.TerminateProcess(handle, -1) ctypes.windll.kernel32.CloseHandle(handle) else: os.kill(process_obj.pid, signal.SIGKILL)
def kill_subprocess(process_obj): try: process_obj.terminate() except AttributeError: # py < 2.6 doesn't support process.terminate() if get_os() == 'windows': import ctypes PROCESS_TERMINATE = 1 handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process_obj.pid) ctypes.windll.kernel32.TerminateProcess(handle, -1) ctypes.windll.kernel32.CloseHandle(handle) else: os.kill(process_obj.pid, signal.SIGKILL)
def __init__(self, agent_config, emitter, checksd=None): super(Collector, self).__init__(agent_config) self.agent_config = agent_config self.os = util.get_os() self.plugins = None self.emitter = emitter socket.setdefaulttimeout(15) self.run_count = 0 self.continue_running = True self.initialized_checks_d = [] self.init_failed_checks_d = [] if checksd: # is of type {check_name: check} self.initialized_checks_d = checksd['initialized_checks'] # is of type {check_name: {error, traceback}} self.init_failed_checks_d = checksd['init_failed_checks']
def __init__(self, agent_config, emitter, checksd): super(Collector, self).__init__(agent_config) self.agent_config = agent_config self.os = util.get_os() self.plugins = None self.emitter = emitter socket.setdefaulttimeout(15) self.run_count = 0 self.continue_running = True self.collection_metrics = {} # is of type {check_name: check} initialized_checks_d = checksd['initialized_checks'] self.pool_size = int(self.agent_config.get('num_collector_threads', 1)) log.info('Using %d Threads for Collector' % self.pool_size) self.pool = Pool(self.pool_size) self.pool_full_count = 0 self.collection_times = {} self.collection_results = {} self.collect_runs = 0 for check in initialized_checks_d: derived_collect_periods = 1 if 'collect_period' in check.init_config: if check.init_config['collect_period'] < 0: log.warn('Invalid negative time parameter. ' 'collect_period for %s will be reset ' 'to default' % check.name) else: # This equation calculates on which nth run the plugin # gets called. It converts the collect_period from seconds # to an integer which holds the collection round the # plugin should get called on. derived_collect_periods = ( ((check.init_config['collect_period'] - 1) / agent_config['check_freq']) + 1) self.collection_times[check.name] = { 'check': check, 'last_collect_time': 99999999, 'derived_collect_periods': derived_collect_periods } self.pool_full_max_retries = int( self.agent_config.get('pool_full_max_retries', 4))
def __init__(self, agent_config, emitter, checksd): super(Collector, self).__init__(agent_config) self.agent_config = agent_config self.os = util.get_os() self.plugins = None self.emitter = emitter socket.setdefaulttimeout(15) self.run_count = 0 self.continue_running = True self.collection_metrics = {} # is of type {check_name: check} initialized_checks_d = checksd['initialized_checks'] self.pool_size = int(self.agent_config.get('num_collector_threads', 1)) log.info('Using %d Threads for Collector' % self.pool_size) self.pool = Pool(self.pool_size) self.pool_full_count = 0 self.collection_times = {} self.collection_results = {} self.collect_runs = 0 for check in initialized_checks_d: derived_collect_periods = 1 if 'collect_period' in check.init_config: if check.init_config['collect_period'] < 0: log.warn('Invalid negative time parameter. ' 'collect_period for %s will be reset ' 'to default' % check.name) else: # This equation calculates on which nth run the plugin # gets called. It converts the collect_period from seconds # to an integer which holds the collection round the # plugin should get called on. derived_collect_periods = ( ((check.init_config['collect_period'] - 1) / agent_config['check_freq']) + 1) self.collection_times[check.name] = { 'check': check, 'last_collect_time': 99999999, 'derived_collect_periods': derived_collect_periods} self.pool_full_max_retries = int(self.agent_config.get('pool_full_max_retries', 4))
def main(): options, args = util.get_parsed_args() config = cfg.Config() collector_config = config.get_config(['Main', 'Api', 'Logging']) # todo autorestart isn't used remove autorestart = collector_config.get('autorestart', False) COMMANDS = [ 'start', 'stop', 'restart', 'foreground', 'status', 'info', 'check', 'check_all', 'configcheck', 'jmx', ] if len(args) < 1: sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS))) return 2 command = args[0] if command not in COMMANDS: sys.stderr.write("Unknown command: %s\n" % command) return 3 pid_file = util.PidFile('monasca-agent') if options.clean: pid_file.clean() agent = CollectorDaemon(pid_file.get_path(), autorestart) if command in START_COMMANDS: log.info('Agent version %s' % config.get_version()) if 'start' == command: log.info('Start daemon') agent.start() elif 'stop' == command: log.info('Stop daemon') agent.stop() elif 'restart' == command: log.info('Restart daemon') agent.restart() elif 'status' == command: agent.status() elif 'info' == command: return agent.info(verbose=options.verbose) elif 'foreground' == command: logging.info('Running in foreground') if autorestart: # Set-up the supervisor callbacks and fork it. logging.info('Running Agent with auto-restart ON') def child_func(): agent.run() def parent_func(): agent.start_event = False monasca_agent.common.daemon.AgentSupervisor.start(parent_func, child_func) else: # Run in the standard foreground. agent.run(collector_config) elif 'check' == command: check_name = args[1] checks = util.load_check_directory() for check in checks['initialized_checks']: if check.name == check_name: run_check(check) elif 'check_all' == command: print("Loading check directory...") checks = util.load_check_directory() print("...directory loaded.\n") for check in checks['initialized_checks']: run_check(check) elif 'configcheck' == command or 'configtest' == command: osname = util.get_os() all_valid = True paths = util.Paths() for conf_path in glob.glob(os.path.join(paths.get_confd_path(), "*.yaml")): basename = os.path.basename(conf_path) try: config.check_yaml(conf_path) except Exception as e: all_valid = False print("%s contains errors:\n %s" % (basename, e)) else: print("%s is valid" % basename) if all_valid: print("All yaml files passed. You can now run the Monitoring agent.") return 0 else: print("Fix the invalid yaml files above in order to start the Monitoring agent. " "A useful external tool for yaml parsing can be found at " "http://yaml-online-parser.appspot.com/") return 1 elif 'jmx' == command: if len(args) < 2 or args[1] not in jmxfetch.JMX_LIST_COMMANDS.keys(): print("#" * 80) print("JMX tool to be used to help configure your JMX checks.") print("See http://docs.datadoghq.com/integrations/java/ for more information") print("#" * 80) print("\n") print("You have to specify one of the following commands:") for command, desc in jmxfetch.JMX_LIST_COMMANDS.iteritems(): print(" - %s [OPTIONAL: LIST OF CHECKS]: %s" % (command, desc)) print("Example: sudo /etc/init.d/monasca-agent jmx list_matching_attributes tomcat jmx solr") print("\n") else: jmx_command = args[1] checks_list = args[2:] paths = util.Paths() confd_path = paths.get_confd_path() # Start JMXFetch if needed should_run = jmxfetch.JMXFetch.init(confd_path, config, 15, jmx_command, checks_list, reporter="console") if not should_run: print("Couldn't find any valid JMX configuration in your conf.d directory: %s" % confd_path) print("Have you enabled any JMX checks ?") return 0