def scan_address_job( ip_address=None, plugins=None, results=None, automerge=AUTOMERGE_MODE, called_from_ui=False, **kwargs ): """The function that is actually running on the worker.""" job = rq.get_current_job() available_plugins = getattr(settings, 'SCAN_PLUGINS', {}).keys() if not plugins: plugins = available_plugins run_postprocessing = not (set(available_plugins) - set(plugins)) if ip_address and plugins: if not kwargs: ip, created = IPAddress.concurrent_get_or_create( address=ip_address, ) if not (ip.snmp_name and ip.snmp_community): message = "SNMP name/community is missing. Forcing autoscan." job.meta['messages'] = [ (ip_address, 'ralph.scan', 'info', message) ] job.save() autoscan_address(ip_address) kwargs = { 'snmp_community': ip.snmp_community, 'snmp_version': ip.snmp_version, 'http_family': ip.http_family, 'snmp_name': ip.snmp_name, } results = _run_plugins(ip_address, plugins, job, **kwargs) if run_postprocessing: _scan_postprocessing(results, job, ip_address) if automerge and job.meta.get('changed', True): # Run only when automerge mode is enabled and some change was # detected. When `change` state is not available just run it... save_job_results(job.id) elif not called_from_ui and job.args and job.meta.get('changed', True): # Run only when some change was detected. When `change` state is # not available just run it... try: ip_obj = IPAddress.objects.select_related().get( address=job.args[0] # job.args[0] == ip_address ) except IPAddress.DoesNotExist: pass else: for plugin_name in getattr( settings, 'SCAN_POSTPROCESS_ENABLED_JOBS', [] ): try: module = import_module(plugin_name) except ImportError as e: logger.error(unicode(e)) else: module.run_job(ip_obj) return results
def scan_address_job( ip_address=None, plugins=None, results=None, automerge=AUTOMERGE_MODE, **kwargs ): """ The function that is actually running on the worker. """ job = rq.get_current_job() available_plugins = getattr(settings, 'SCAN_PLUGINS', {}).keys() if not plugins: plugins = available_plugins run_postprocessing = not (set(available_plugins) - set(plugins)) if ip_address and plugins: if not kwargs: ip, created = IPAddress.concurrent_get_or_create( address=ip_address, ) kwargs = { 'snmp_community': ip.snmp_community, 'snmp_version': ip.snmp_version, 'http_family': ip.http_family, 'snmp_name': ip.snmp_name, } results = _run_plugins(ip_address, plugins, job, **kwargs) if run_postprocessing: _scan_postprocessing(results, job, ip_address) # Run only when automerge mode is enabled and some change was detected. # When `change` state is not available just run it... if automerge and job.meta.get('changed', True): save_job_results(job.id) return results
def scan_address_job(ip_address=None, plugins=None, results=None, automerge=AUTOMERGE_MODE, called_from_ui=False, **kwargs): """The function that is actually running on the worker.""" job = rq.get_current_job() available_plugins = getattr(settings, 'SCAN_PLUGINS', {}).keys() if not plugins: plugins = available_plugins run_postprocessing = not (set(available_plugins) - set(plugins)) if ip_address and plugins: if not kwargs: ip, created = IPAddress.concurrent_get_or_create( address=ip_address, ) if not (ip.snmp_name and ip.snmp_community): message = ("SNMP name and community is missing. Forcing " " autoscan.") job.meta['messages'] = [(ip_address, 'ralph.scan', 'info', message)] job.save() autoscan_address(ip_address) # since autoscan_address can update some fields on IPAddress, # we need to refresh it here ip = IPAddress.objects.get(address=ip_address) kwargs = { 'snmp_community': ip.snmp_community, 'snmp_version': ip.snmp_version, 'http_family': ip.http_family, 'snmp_name': ip.snmp_name, } results = _run_plugins(ip_address, plugins, job, **kwargs) if run_postprocessing: _scan_postprocessing(results, job, ip_address) if automerge and job.meta.get('changed', True): # Run only when automerge mode is enabled and some change was # detected. When `change` state is not available just run it... save_job_results(job.id) elif not called_from_ui and job.args: try: ip_obj = IPAddress.objects.select_related().get( address=job.args[0] # job.args[0] == ip_address ) except IPAddress.DoesNotExist: pass else: for plugin_name in getattr(settings, 'SCAN_POSTPROCESS_ENABLED_JOBS', []): try: module = import_module(plugin_name) except ImportError as e: logger.error(unicode(e)) else: module.run_job(ip_obj, plugins_results=results) return results