def _check_when_enabled(config): """Stop agent. Args: config: Agent configuration object Returns: None """ # Initialize key variables agent_name = config.agent_name() agent_filepath = _agent_filepath(config) # Get agent status variables pidfile = daemon.pid_file(agent_name) lockfile = daemon.lock_file(agent_name) # Ignore agents that cannot be found if os.path.isfile(agent_filepath) is False: log_message = ('Agent executable file %s listed in the ' 'configuration file ' 'of agent "%s" does not exist. Please fix.' '') % (agent_filepath, agent_name) log.log2info(1075, log_message) return # Check for pid file if os.path.isfile(pidfile) is True: with open(pidfile, 'r') as f_handle: pidvalue = int(f_handle.readline().strip()) # Check if service died catastrophically. No PID file if psutil.pid_exists(pidvalue) is False: log_message = ('Agent "%s" is dead. Attempting to restart.' '') % (agent_name) log.log2info(1041, log_message) # Remove PID file and restart os.remove(pidfile) _restart(config) else: # Check if agent hung without updating the PID if config.monitor_agent_pid() is True: try: mtime = os.path.getmtime(pidfile) except OSError: mtime = 0 if mtime < int(time.time()) - (60 * 10): log_message = ('Agent "%s" is hung. Attempting to restart.' '') % (agent_name) log.log2info(1076, log_message) _restart(config) else: if os.path.isfile(lockfile) is True: _restart(config) else: _start(config)
def __init__(self, poller): """Method initializing the class. Args: poller: PollingAgent object Returns: None """ # Instantiate poller self.poller = poller # Get PID filename agent_name = self.poller.name() pidfile = daemon.pid_file(agent_name) lockfile = daemon.lock_file(agent_name) # Call up the base daemon Daemon.__init__(self, pidfile, lockfile=lockfile)
def __init__(self, parent, child=None): """Method initializing the class. Args: parent: Name of parent daemon child: Name of child daemon Returns: None """ # Initialize key variables (Parent) self.parent = parent self.pidfile_parent = daemon.pid_file(parent) self.lockfile_parent = daemon.lock_file(parent) # Initialize key variables (Child) if bool(child) is None: self.pidfile_child = None else: self.pidfile_child = daemon.pid_file(child)
def process(ingester_agent_name): """Process cache data by adding it to the database using subprocesses. Args: ingester_agent_name: Ingester agent name Returns: None """ # Initialize key variables argument_list = [] id_agent_metadata = defaultdict(lambda: defaultdict(dict)) # Configuration setup config = configuration.Config() configured_pool_size = config.ingest_pool_size() # Make sure we have database connectivity if db.connectivity() is False: log_message = ('No connectivity to database. Check if running. ' 'Check database authentication parameters.' '') log.log2warning(1053, log_message) return # Get meta data on files id_agent_metadata = validate_cache_files() # Spawn processes only if we have files to process if bool(id_agent_metadata.keys()) is True: # Process lock file lockfile = daemon.lock_file(ingester_agent_name) if os.path.exists(lockfile) is True: # Return if lock file is present log_message = ( 'Ingest lock file %s exists. Multiple ingest daemons running ' 'or lots of cache files to ingest. Ingester may have died ' 'catastrophically in the past, in which case the lockfile ' 'should be deleted. Exiting ingest process. ' 'Will try again later.' '') % (lockfile) log.log2warning(1069, log_message) return else: # Create lockfile open(lockfile, 'a').close() # Read each cache file for devicehash in id_agent_metadata.keys(): for id_agent in id_agent_metadata[devicehash].keys(): # Create a list of arguments to process argument_list.append( (config, id_agent_metadata[devicehash][id_agent], ingester_agent_name)) # Create a pool of sub process resources pool_size = int(min(configured_pool_size, len(id_agent_metadata))) with Pool(processes=pool_size) as pool: # Create sub processes from the pool pool.map(_wrapper_process, argument_list) # Wait for all the processes to end # pool.join() # Return if lock file is present if os.path.exists(lockfile) is True: os.remove(lockfile)