Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
 def handle(self, *args, **kwargs):
     if sum([
         kwargs['network'],
         kwargs['data_center'],
         kwargs['environment'],
         kwargs['queue']
     ]) > 1:
         raise SystemExit(
             "You can't mix networks, environments, data centers and "
             "queues.",
         )
     if not args:
         raise SystemExit("Please specify the addresses to scan.")
     if kwargs['network']:
         try:
             for network in [
                 find_network(network_spec) for network_spec in args
             ]:
                 autoscan_network(network)
         except (Error, Network.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['environment']:
         try:
             for environment in [
                 Environment.objects.get(name=name) for name in args
             ]:
                 autoscan_environment(environment)
         except (Error, Environment.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['data_center']:
         try:
             for data_center in [
                 DataCenter.objects.get(name=name) for name in args
             ]:
                 for environment in data_center.environment_set.filter(
                     queue__isnull=False,
                 ):
                     autoscan_environment(environment)
         except (Error, DataCenter.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['queue']:
         try:
             for queue in [
                 DiscoveryQueue.objects.get(name=name) for name in args
             ]:
                 for environment in queue.environment_set.all():
                     autoscan_environment(environment)
         except (Error, DiscoveryQueue.DoesNotExist) as e:
             raise SystemExit(e)
     else:
         try:
             addresses = [str(ipaddr.IPAddress(ip)) for ip in args]
             for address in addresses:
                 autoscan_address(address)
         except (Error, ValueError) as e:
             raise SystemExit(e)
Beispiel #4
0
 def handle(self, *args, **kwargs):
     if sum([
         kwargs['network'],
         kwargs['data_center'],
         kwargs['queue']
     ]) > 1:
         raise SystemExit("You can't mix networks, data centers and queues.")
     if not args:
         raise SystemExit("Please specify the addresses to scan.")
     if kwargs['network']:
         try:
             networks = [
                 find_network(network_spec) for network_spec in args
             ]
             for network in networks:
                 autoscan_network(network)
         except (Error, Network.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['data_center']:
         try:
             data_centers = [
                 DataCenter.objects.get(name=name) for name in args
             ]
             for data_center in data_centers:
                 autoscan_data_center(data_center)
         except (Error, DataCenter.DoesNotExist) as e:
             raise SystemExit(e)
     elif kwargs['queue']:
         try:
             queues = [
                 DiscoveryQueue.objects.get(name=name) for name in args
             ]
             for queue in queues:
                 for network in queue.network_set.all():
                     autoscan_network(network)
         except (Error, DiscoveryQueue.DoesNotExist) as e:
             raise SystemExit(e)
     else:
         try:
             addresses = [str(ipaddr.IPAddress(ip)) for ip in args]
             for address in addresses:
                 autoscan_address(address)
         except (Error, ValueError) as e:
             raise SystemExit(e)