def clear(agent_id=None, all_agents=False): """ Clears the database. :param agent_id: For an agent. :param all_agents: For all agents. :return: Message. """ # Clear DB if int(all_agents): db_agents = glob('{0}/*-*.db'.format(common.database_path_agents)) else: Agent(agent_id).get_basic_information() # check if the agent exists db_agents = glob('{0}/{1}-*.db'.format(common.database_path_agents, agent_id)) if not db_agents: raise WazuhException(1600) for db_agent in db_agents: conn = Connection(db_agent) conn.begin() try: conn.execute('DELETE FROM pm_event') except WazuhException as e: raise e except Exception as exception: conn.commit() conn.vacuum() raise WazuhException(1654, exception) else: conn.commit() conn.vacuum() # Clear OSSEC info if int(all_agents): rootcheck_files = glob('{0}/queue/rootcheck/*'.format( common.ossec_path)) else: if agent_id == "000": rootcheck_files = [ '{0}/queue/rootcheck/rootcheck'.format(common.ossec_path) ] else: agent_info = Agent(agent_id).get_basic_information() rootcheck_files = glob( '{0}/queue/rootcheck/({1}) {2}->rootcheck'.format( common.ossec_path, agent_info['name'], agent_info['ip'])) for rootcheck_file in rootcheck_files: if path.exists(rootcheck_file): remove(rootcheck_file) return "Rootcheck database deleted"
def clear(agent_id=None, all_agents=False): """ Clears the database. :param agent_id: For an agent. :param all_agents: For all agents. :return: Message. """ # Clear DB if int(all_agents): db_agents = glob('{0}/*-*.db'.format(common.database_path_agents)) else: db_agents = glob('{0}/{1}-*.db'.format(common.database_path_agents, agent_id)) if not db_agents: raise WazuhException(1600) for db_agent in db_agents: conn = Connection(db_agent) conn.begin() try: conn.execute('DELETE FROM fim_event') conn.execute('DELETE FROM fim_file') except Exception as exception: raise exception finally: conn.commit() conn.vacuum() # Clear OSSEC info if int(all_agents): syscheck_files = glob('{0}/queue/syscheck/*'.format(common.ossec_path)) else: if agent_id == "000": syscheck_files = ['{0}/queue/syscheck/syscheck'.format(common.ossec_path)] else: agent_info = Agent(agent_id).get_basic_information() syscheck_files = glob('{0}/queue/syscheck/({1}) {2}->syscheck'.format(common.ossec_path, agent_info['name'], agent_info['ip'])) for syscheck_file in syscheck_files: if path.exists(syscheck_file): remove(syscheck_file) return "Syscheck database deleted"
def clear_local(agent_id=None, all_agents=False): # Clear DB if int(all_agents): db_agents = glob('{0}/*-*.db'.format(common.database_path_agents)) else: db_agents = glob('{0}/{1}-*.db'.format(common.database_path_agents, agent_id)) if not db_agents: raise WazuhException(1600) for db_agent in db_agents: conn = Connection(db_agent) conn.begin() try: conn.execute('DELETE FROM fim_event') conn.execute('DELETE FROM fim_file') except Exception as exception: raise exception finally: conn.commit() conn.vacuum() # Clear OSSEC info if int(all_agents): syscheck_files = glob('{0}/queue/syscheck/*'.format(common.ossec_path)) else: if agent_id == "000": syscheck_files = [ '{0}/queue/syscheck/syscheck'.format(common.ossec_path) ] else: agent_info = Agent(agent_id).get_basic_information() syscheck_files = glob( '{0}/queue/syscheck/({1}) {2}->syscheck'.format( common.ossec_path, agent_info['name'], agent_info['ip'])) for syscheck_file in syscheck_files: if path.exists(syscheck_file): remove(syscheck_file) return "Syscheck database deleted"