def show_agents_with_group(group_id): agents_data = agent.get_agents_in_group(group_list=[group_id], limit=None).to_dict() if agents_data['total_affected_items'] == 0: print("No agents found in group '{0}'.".format(group_id)) else: print("{0} agent(s) in group '{1}':".format(agents_data['total_affected_items'], group_id)) for a in agents_data['affected_items']: print(" ID: {0} Name: {1}.".format(a['id'], a['name']))
async def get_solver_node(self) -> Dict: """Get the node(s) that can solve a request. Get the node(s) that have all the necessary information to answer the request. Only called when the request type is 'master_distributed' and the node_type is master. Returns ------- dict Dict with node names with agents. """ select_node = ['node_name'] if 'agent_id' in self.f_kwargs or 'agent_list' in self.f_kwargs: # Group requested agents by node_name requested_agents = self.f_kwargs.get( 'agent_list', None) or [self.f_kwargs['agent_id']] # Filter by node_name if we receive a node_id if 'node_id' in self.f_kwargs: requested_nodes = self.f_kwargs.get( 'node_list', None) or [self.f_kwargs['node_id']] filters = {'node_name': requested_nodes} elif requested_agents != '*': filters = {'id': requested_agents} else: filters = None system_agents = agent.Agent.get_agents_overview( select=select_node, limit=None, filters=filters)['items'] node_name = defaultdict(list) for element in system_agents: node_name[element['node_name']].append(element['id']) # Update node_name in case it is empty or a node has no agents if 'node_id' in self.f_kwargs: if self.f_kwargs['node_id'] not in node_name: node_name.update({self.f_kwargs['node_id']: []}) if requested_agents != '*': # When all agents are requested cannot be non existent ids # Add non existing ids in the master's dictionary entry non_existent_ids = list( set(requested_agents) - set(map(operator.itemgetter('id'), system_agents))) if non_existent_ids: if self.node_info['node'] in node_name: node_name[self.node_info['node']].extend( non_existent_ids) else: node_name[self.node_info['node']] = non_existent_ids return node_name elif 'node_id' in self.f_kwargs or ('node_list' in self.f_kwargs and self.f_kwargs['node_list'] != '*'): requested_nodes = self.f_kwargs.get( 'node_list', None) or [self.f_kwargs['node_id']] return {node_id: [] for node_id in requested_nodes} elif 'group_id' in self.f_kwargs: common.rbac.set(self.rbac_permissions) agents = agent.get_agents_in_group( group_list=[self.f_kwargs['group_id']], select=select_node, sort={ 'fields': ['node_name'], 'order': 'desc' }).affected_items if len(agents) == 0: raise WazuhError(1755) del self.f_kwargs['group_id'] node_name = { k: list(map(operator.itemgetter('id'), g)) for k, g in itertools.groupby( agents, key=operator.itemgetter('node_name')) } return node_name else: if self.broadcasting: node_name = {} else: # agents, syscheck and syscollector # API calls that affect all agents. For example, PUT/agents/restart, etc... agents = agent.Agent.get_agents_overview(select=select_node, limit=None, sort={ 'fields': ['node_name'], 'order': 'desc' })['items'] node_name = { k: [] for k, _ in itertools.groupby( agents, key=operator.itemgetter('node_name')) } return node_name
async def get_solver_node(self) -> Dict: """Get the node(s) that can solve a request. Get the node(s) that have all the necessary information to answer the request. Only called when the request type is 'master_distributed' and the node_type is master. Returns ------- dict Dict with node names with agents. """ select_node = ['node_name'] if 'agent_id' in self.f_kwargs or 'agent_list' in self.f_kwargs: # Group requested agents by node_name requested_agents = self.f_kwargs.get( 'agent_list', None) or [self.f_kwargs['agent_id']] filters = { 'id': requested_agents } if requested_agents != '*' else None system_agents = agent.Agent.get_agents_overview(select=select_node, limit=None, filters=filters, sort={ 'fields': ['node_name'], 'order': 'desc' })['items'] node_name = { k: list(map(operator.itemgetter('id'), g)) for k, g in itertools.groupby( system_agents, key=operator.itemgetter('node_name')) } if requested_agents != '*': # When all agents are requested cannot be non existent ids # Add non existing ids in the master's dictionary entry non_existent_ids = list( set(requested_agents) - set(map(operator.itemgetter('id'), system_agents))) if non_existent_ids: if self.node_info['node'] in node_name: node_name[self.node_info['node']].extend( non_existent_ids) else: node_name[self.node_info['node']] = non_existent_ids return node_name elif 'node_id' in self.f_kwargs or ('node_list' in self.f_kwargs and self.f_kwargs['node_list'] != '*'): requested_nodes = self.f_kwargs.get( 'node_list', None) or [self.f_kwargs['node_id']] del self.f_kwargs['node_id' if 'node_id' in self.f_kwargs else 'node_list'] return {node_id: [] for node_id in requested_nodes} elif 'group_id' in self.f_kwargs: common.rbac.set(self.rbac_permissions) agents = agent.get_agents_in_group( group_list=[self.f_kwargs['group_id']], select=select_node, sort={ 'fields': ['node_name'], 'order': 'desc' }).affected_items if len(agents) == 0: raise WazuhError(1755) del self.f_kwargs['group_id'] node_name = { k: list(map(operator.itemgetter('id'), g)) for k, g in itertools.groupby( agents, key=operator.itemgetter('node_name')) } return node_name else: if self.broadcasting: if 'node_list' in self.f_kwargs: del self.f_kwargs['node_list'] client = self.get_client() nodes = json.loads(await client.execute(command=b'get_nodes', data=json.dumps({}).encode(), wait_for_complete=False), object_hook=c_common.as_wazuh_object) node_name = {item['name']: [] for item in nodes['items']} else: # agents, syscheck and syscollector # API calls that affect all agents. For example, PUT/agents/restart, etc... agents = agent.Agent.get_agents_overview(select=select_node, limit=None, sort={ 'fields': ['node_name'], 'order': 'desc' })['items'] node_name = { k: [] for k, _ in itertools.groupby( agents, key=operator.itemgetter('node_name')) } return node_name