From bbf2733c9516f343eecf1f4f3018466fb734e440 Mon Sep 17 00:00:00 2001 From: omrozowicz-splunk <86965961+omrozowicz-splunk@users.noreply.github.com> Date: Tue, 24 Aug 2021 12:06:44 +0200 Subject: [PATCH] fix: change the way of creating host name which is an id in the database (#104) --- splunk_connect_for_snmp_poller/manager/poller_utilities.py | 6 +++++- splunk_connect_for_snmp_poller/mongo.py | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/splunk_connect_for_snmp_poller/manager/poller_utilities.py b/splunk_connect_for_snmp_poller/manager/poller_utilities.py index c2d8aa1..074ae9e 100644 --- a/splunk_connect_for_snmp_poller/manager/poller_utilities.py +++ b/splunk_connect_for_snmp_poller/manager/poller_utilities.py @@ -24,6 +24,7 @@ from splunk_connect_for_snmp_poller.manager.realtime.real_time_data import ( should_redo_walk, ) +from splunk_connect_for_snmp_poller.manager.task_utilities import parse_port from splunk_connect_for_snmp_poller.manager.tasks import snmp_polling from splunk_connect_for_snmp_poller.manager.validator.inventory_validator import ( is_valid_inventory_line_from_dict, @@ -132,7 +133,10 @@ def _update_mongo( all_walked_hosts_collection, host, host_already_walked, current_sys_up_time ): if not host_already_walked: - all_walked_hosts_collection.add_host(host) + _host, _port = parse_port(host) + host_to_add = f"{_host}:{_port}" + logger.info(f"Adding host: {host_to_add} into Mongo database") + all_walked_hosts_collection.add_host(host_to_add) all_walked_hosts_collection.update_real_time_data_for(host, current_sys_up_time) diff --git a/splunk_connect_for_snmp_poller/mongo.py b/splunk_connect_for_snmp_poller/mongo.py index bffb234..efce33b 100644 --- a/splunk_connect_for_snmp_poller/mongo.py +++ b/splunk_connect_for_snmp_poller/mongo.py @@ -30,6 +30,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +import logging import os from pymongo import MongoClient, ReturnDocument @@ -37,6 +38,8 @@ from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import InterfaceMib +logger = logging.getLogger(__name__) + """ In order to store some general data into Mongo we use the following structure. Each WalkedHostsRepository can contain the following fields: @@ -117,6 +120,9 @@ def real_time_data_for(self, host): def static_data_for(self, host): full_collection = self._walked_hosts.find_one({"_id": host}) + if not full_collection: + logger.info(f"No id {host} in walked_host collection") + return None if WalkedHostsRepository.MIB_STATIC_DATA in full_collection: mib_static_data = full_collection[WalkedHostsRepository.MIB_STATIC_DATA] if InterfaceMib.IF_MIB_DATA_MONGO_IDENTIFIER in mib_static_data: @@ -147,6 +153,5 @@ def update_mib_static_data_for(self, host, if_mib_data): self._walked_hosts.find_one_and_update( {"_id": host}, {"$set": real_time_data_dictionary}, - upsert=True, return_document=ReturnDocument.AFTER, )