diff --git a/splunk_connect_for_snmp/common/inventory_processor.py b/splunk_connect_for_snmp/common/inventory_processor.py index e50c72aeb..6cea73211 100644 --- a/splunk_connect_for_snmp/common/inventory_processor.py +++ b/splunk_connect_for_snmp/common/inventory_processor.py @@ -57,22 +57,29 @@ def gen_walk_task(ir: InventoryRecord, profile=None, group=None): return task_config -def return_hosts_from_deleted_groups(previous_groups, new_groups): +def return_hosts_from_deleted_groups( + previous_groups, new_groups, inventory_group_port_mapping +): inventory_lines_to_delete = [] for group_name in previous_groups.keys(): - previous_groups_keys = get_groups_keys(previous_groups[group_name]) + previous_groups_keys = get_groups_keys( + previous_groups[group_name], group_name, inventory_group_port_mapping + ) if group_name not in new_groups: inventory_lines_to_delete += previous_groups_keys else: - new_groups_keys = get_groups_keys(new_groups[group_name]) + new_groups_keys = get_groups_keys( + new_groups[group_name], group_name, inventory_group_port_mapping + ) deleted_hosts = set(previous_groups_keys) - set(new_groups_keys) inventory_lines_to_delete += deleted_hosts return inventory_lines_to_delete -def get_groups_keys(list_of_groups): +def get_groups_keys(list_of_groups, group_name, inventory_group_port_mapping): + group_port = inventory_group_port_mapping.get(group_name, 161) groups_keys = [ - f"{transform_address_to_key(element.get('address'), element.get('port', 161))}" + f"{transform_address_to_key(element.get('address'), element.get('port', group_port))}" for element in list_of_groups ] return groups_keys @@ -84,6 +91,7 @@ def __init__(self, group_manager: GroupsManager, logger): self.group_manager = group_manager self.logger = logger self.hosts_from_groups: dict = {} + self.inventory_group_port_mapping: dict = {} self.single_hosts: List[dict] = [] def get_all_hosts(self): @@ -103,7 +111,7 @@ def get_all_hosts(self): self.logger.warning( f"Record: {host} has been already configured in group. Skipping..." ) - return self.inventory_records + return self.inventory_records, self.inventory_group_port_mapping def process_line(self, source_record): address = source_record["address"] @@ -122,6 +130,9 @@ def get_group_hosts(self, source_object, group_name): group_list = list(groups) if group_list: groups_object_list = list(group_list[0].values()) + self.inventory_group_port_mapping[group_name] = ( + source_object["port"] if source_object["port"] else 161 + ) for group_object in groups_object_list[0]: host_group_object = copy.copy(source_object) for key in group_object.keys(): diff --git a/splunk_connect_for_snmp/inventory/loader.py b/splunk_connect_for_snmp/inventory/loader.py index bb8f3b5da..dc33ed345 100644 --- a/splunk_connect_for_snmp/inventory/loader.py +++ b/splunk_connect_for_snmp/inventory/loader.py @@ -89,11 +89,11 @@ def load(): mongo_client, periodic_obj, logger ) logger.info(f"Loading inventory from {INVENTORY_PATH}") - inventory_lines = inventory_processor.get_all_hosts() + inventory_lines, inventory_group_port_mapping = inventory_processor.get_all_hosts() # Function to delete inventory records that are hosts_from_groups_to_delete = return_hosts_from_deleted_groups( - previous_groups, new_groups + previous_groups, new_groups, inventory_group_port_mapping ) for host in hosts_from_groups_to_delete: inventory_record_manager.delete(host) diff --git a/test/common/test_inventory_processor.py b/test/common/test_inventory_processor.py index 9b499e3ab..4a4c0374c 100644 --- a/test/common/test_inventory_processor.py +++ b/test/common/test_inventory_processor.py @@ -72,7 +72,11 @@ def test_return_hosts_from_deleted_groups_one_host(self): } self.assertEqual( - return_hosts_from_deleted_groups(previous_groups, new_groups), + return_hosts_from_deleted_groups( + previous_groups, + new_groups, + {"group1": {"port": 161}, "switches": {"port": 161}}, + ), ["1.1.1.1:162"], ) @@ -95,7 +99,11 @@ def test_return_hosts_from_deleted_groups_whole_group(self): } self.assertEqual( - return_hosts_from_deleted_groups(previous_groups, new_groups), + return_hosts_from_deleted_groups( + previous_groups, + new_groups, + {"group1": 161, "switches": 161}, + ), ["12.22.23.33", "1.1.1.1:162"], ) @@ -115,7 +123,11 @@ def test_return_hosts_from_deleted_groups_one_host_and_group(self): } self.assertEqual( - return_hosts_from_deleted_groups(previous_groups, new_groups), + return_hosts_from_deleted_groups( + previous_groups, + new_groups, + {"group1": 161, "switches": 161}, + ), ["123.0.0.1", "178.8.8.1:999", "1.1.1.1:162"], ) @@ -123,14 +135,37 @@ def test_return_hosts_empty(self): previous_groups = {} new_groups = {} self.assertEqual( - return_hosts_from_deleted_groups(previous_groups, new_groups), [] + return_hosts_from_deleted_groups(previous_groups, new_groups, {}), [] ) def test_return_hosts_new_ones(self): previous_groups = {} new_groups = {"switches": [{"address": "12.22.23.33", "port": 161}]} self.assertEqual( - return_hosts_from_deleted_groups(previous_groups, new_groups), [] + return_hosts_from_deleted_groups( + previous_groups, new_groups, {"switches": {"port": 161}} + ), + [], + ) + + def test_return_deleted_host_without_port_config(self): + previous_groups = { + "group1": [ + {"address": "123.0.0.1", "port": 161}, + {"address": "178.8.8.1"}, + ] + } + new_groups = { + "group1": [ + {"address": "123.0.0.1", "port": 161}, + {"address": "178.8.8.1", "port": 162}, + ] + } + self.assertEqual( + ["178.8.8.1:1161"], + return_hosts_from_deleted_groups( + previous_groups, new_groups, {"group1": 1161} + ), ) def test_get_group_hosts(self):