Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Commit

Permalink
fix: fix issue with onetime walk not running
Browse files Browse the repository at this point in the history
  • Loading branch information
omrozowicz-splunk committed Oct 25, 2021
1 parent 58587cd commit b479693
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
24 changes: 12 additions & 12 deletions splunk_connect_for_snmp_poller/manager/poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ def __check_inventory(self):
continue
inventory_entry_keys.add(entry_key)
ir_host = return_database_id(ir.host)
if ir_host not in inventory_hosts:
self.add_enricher_to_a_host(
new_enricher, profiles, copy.deepcopy(ir), True
)
inventory_hosts.add(return_database_id(ir_host))
inventory_hosts.add(ir_host)
inventory_hosts_with_snmp_data[ir_host] = copy.deepcopy(ir)
if ir.profile == DYNAMIC_PROFILE:
self.delete_all_entries_per_host(ir.host)
Expand All @@ -138,20 +134,24 @@ def __check_inventory(self):
self._server_config["profiles"],
)
if entry_key not in self._jobs_map:
ir_host = return_database_id(entry_key)
if self._old_enricher != {}:
logger.info(f"New host: {ir_host}")
self.add_enricher_to_a_host(
new_enricher, copy.deepcopy(ir), True
)
self.process_new_job(entry_key, ir, profiles)
else:
self.update_schedule_for_changed_conf(entry_key, ir, profiles)

if server_config_modified:
if new_enricher != self._old_enricher:
self.run_enricher_check(
new_enricher, profiles, inventory_hosts_with_snmp_data
new_enricher, inventory_hosts_with_snmp_data
)
self.clean_job_inventory(inventory_entry_keys, inventory_hosts)

def run_enricher_check(
self, new_enricher, profiles, inventory_hosts_with_snmp_data
):
def run_enricher_check(self, new_enricher, inventory_hosts_with_snmp_data):
logger.info(
f"Previous enricher: {self._old_enricher} \n New enricher: {new_enricher}"
)
Expand All @@ -162,18 +162,18 @@ def run_enricher_check(
return
for inventory_host in inventory_hosts_with_snmp_data.keys():
self.add_enricher_to_a_host(
new_enricher, profiles, inventory_hosts_with_snmp_data[inventory_host]
new_enricher, inventory_hosts_with_snmp_data[inventory_host]
)
self._old_enricher = new_enricher

def add_enricher_to_a_host(self, current_enricher, profiles, ir, new_host=False):
def add_enricher_to_a_host(self, current_enricher, ir, new_host=False):
logger.info("Add enricher to a host")
old_enricher = {} if new_host else self._old_enricher
if current_enricher != {}:
update_enricher_config(
old_enricher,
current_enricher,
self._mongo,
profiles,
ir,
self._server_config,
self.__get_splunk_indexes(),
Expand Down
18 changes: 11 additions & 7 deletions splunk_connect_for_snmp_poller/manager/poller_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ def _extract_sys_uptime_instance(


def _walk_info(mongo_collection, host, current_sys_up_time):
host_already_walked = mongo_collection.contains_host(host) != 0
host_already_walked = mongo_collection.first_time_walked_was_initiated(host) != 0
logger.info(f"host_already_walked: {host_already_walked}")
should_do_walk = not host_already_walked
if host_already_walked:
previous_sys_up_time = mongo_collection.real_time_data_for(host)
Expand Down Expand Up @@ -277,6 +278,10 @@ def automatic_realtime_task(
host_already_walked,
sys_up_time,
)
if should_do_walk:
mongo_collection.update_walked_host(
db_host_id, {"walked_first_time": True}
)
except Exception:
logger.exception("Error during automatic_realtime_task")

Expand All @@ -285,15 +290,14 @@ def update_enricher_config(
old_enricher,
new_enricher,
mongo,
profiles,
inventory_host,
server_config,
splunk_indexes,
):
run_ifmib_walk = is_ifmib_different(old_enricher, new_enricher)
if run_ifmib_walk:
_update_enricher_config_with_ifmib(
profiles, inventory_host, server_config, splunk_indexes
inventory_host, server_config, splunk_indexes
)
else:
_update_enricher_config_for_additional_varbinds(
Expand All @@ -306,17 +310,17 @@ def update_enricher_config(


def _update_enricher_config_with_ifmib(
profiles,
inventory_host,
server_config,
splunk_indexes,
):
inventory_host.profile = OidConstant.IF_MIB
snmp_polling.delay(
inventory_host.to_json(),
schedule.every().second.do(
onetime_task,
inventory_host,
server_config,
splunk_indexes,
profiles,
None,
)


Expand Down
16 changes: 15 additions & 1 deletion splunk_connect_for_snmp_poller/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,14 @@ def is_connected(self):
def contains_host(self, host):
return self._walked_hosts.find({"_id": host}).count()

def first_time_walked_was_initiated(self, host):
return self._walked_hosts.find({"_id": host, "walked_first_time": True}).count()

def add_host(self, host):
self._walked_hosts.insert_one({"_id": host})
try:
self._walked_hosts.insert_one({"_id": host})
except: # noqa: E722
logger.info(f"Id {host} already exists in MongoDB")

def get_all_unwalked_hosts(self):
return list(self._unwalked_hosts.find({}))
Expand All @@ -136,6 +142,14 @@ def delete_host(self, host):
def clear(self):
self._walked_hosts.remove()

def update_walked_host(self, host, element):
self._walked_hosts.find_one_and_update(
{"_id": host},
{"$set": element},
return_document=ReturnDocument.AFTER,
upsert=True,
)

def real_time_data_for(self, host):
full_collection = self._walked_hosts.find_one({"_id": host})
if (
Expand Down

0 comments on commit b479693

Please sign in to comment.