-
Notifications
You must be signed in to change notification settings - Fork 3
feat: force inventory refresh #149
Changes from 2 commits
d09e65b
db1a90a
bed9a60
0b7a0fa
764ce07
8726135
3cc0021
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,10 @@ def __init__(self, args, server_config): | |
self._local_snmp_engine = SnmpEngine() | ||
self._unmatched_devices = {} | ||
self._lock = threading.Lock() | ||
self._force_refresh = False | ||
|
||
def force_inventory_refresh(self): | ||
self._force_refresh = True | ||
|
||
def __get_splunk_indexes(self): | ||
return { | ||
|
@@ -88,7 +92,9 @@ def __check_inventory(self): | |
) | ||
|
||
# update job when either inventory changes or config changes | ||
if server_config_modified or inventory_config_modified: | ||
if server_config_modified or inventory_config_modified or self._force_refresh: | ||
logger.info("Refreshing inventory and config") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add force_refresh state to that log to have more information |
||
self._force_refresh = False | ||
inventory_hosts = set() | ||
profiles = get_profiles(self._server_config) | ||
for ir in parse_inventory_file(self._args.inventory, profiles): | ||
|
@@ -195,6 +201,8 @@ def __start_realtime_scheduler_task(self): | |
self.__get_splunk_indexes(), | ||
self._server_config, | ||
self._local_snmp_engine, | ||
self.force_inventory_refresh, | ||
False, | ||
) | ||
|
||
schedule.every(self._args.matching_task_frequency).seconds.do( | ||
|
@@ -208,6 +216,8 @@ def __start_realtime_scheduler_task(self): | |
self.__get_splunk_indexes(), | ||
self._server_config, | ||
self._local_snmp_engine, | ||
self.force_inventory_refresh, | ||
True, | ||
) | ||
|
||
def add_device_for_profile_matching(self, device: InventoryRecord): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
import csv | ||
import logging.config | ||
import threading | ||
from pathlib import Path | ||
|
||
import schedule | ||
from pysnmp.hlapi import ObjectIdentity, ObjectType, UdpTransportTarget, getCmd | ||
|
@@ -62,9 +61,8 @@ def onetime_task(inventory_record: InventoryRecord, server_config, splunk_indexe | |
return schedule.CancelJob | ||
|
||
|
||
def refresh_inventory(inventory_file_path): | ||
Path(inventory_file_path).touch() | ||
|
||
def refresh_inventory(force_inventory_refresh): | ||
force_inventory_refresh() | ||
return schedule.CancelJob | ||
|
||
|
||
|
@@ -163,6 +161,8 @@ def automatic_realtime_job( | |
splunk_indexes, | ||
server_config, | ||
local_snmp_engine, | ||
force_inventory_refresh, | ||
initial_walk, | ||
): | ||
job_thread = threading.Thread( | ||
target=automatic_realtime_task, | ||
|
@@ -172,6 +172,8 @@ def automatic_realtime_job( | |
splunk_indexes, | ||
server_config, | ||
local_snmp_engine, | ||
force_inventory_refresh, | ||
initial_walk, | ||
], | ||
) | ||
job_thread.start() | ||
|
@@ -183,6 +185,8 @@ def automatic_realtime_task( | |
splunk_indexes, | ||
server_config, | ||
local_snmp_engine, | ||
force_inventory_refresh, | ||
initial_walk, | ||
): | ||
try: | ||
for inventory_record in parse_inventory_file( | ||
|
@@ -208,11 +212,11 @@ def automatic_realtime_task( | |
server_config, | ||
splunk_indexes, | ||
) | ||
# touch inventory file after 2 min to trigger reloading of inventory with new walk data | ||
schedule.every(2).minutes.do( | ||
refresh_inventory, | ||
inventory_file_path, | ||
) | ||
if not initial_walk: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we schedule automatic_realtime_task with False in __start_realtime_scheduler_task There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The feature of forcing inventory refreshing is supposed to be called when new walk for device was conducted and in case sysDescr has changed we would like run the matching again. When we start the application automatic_realtime_task is called to schedule first walk for device and there is no need to force inventory reloading. |
||
# force inventory reloading after 2 min with new walk data | ||
schedule.every(2).minutes.do( | ||
refresh_inventory, force_inventory_refresh | ||
) | ||
_update_mongo( | ||
all_walked_hosts_collection, | ||
db_host_id, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we change it and request in a few different scheduled functions, can't we run into any race condition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boolean assignment is considered threadsafe in Python.
self._force_refresh = False - is the first statement in refreshing fragment of code so if other thread sets the flag to true it will be picked up during next iteration