def _finalize_host(self, host, host_status):
        results = self._ansible_facts.pop(host, {})
        self.scan_task.log_message('host scan complete for %s.  '
                                   'Status: %s. Facts %s' %
                                   (host, host_status, results),
                                   log_level=logging.DEBUG)

        # Update scan counts
        if self.scan_task is not None:
            if host_status == SystemInspectionResult.SUCCESS:
                self.scan_task.increment_stats(
                    host, increment_sys_scanned=True)
            else:
                self.scan_task.increment_stats(host, increment_sys_failed=True)

        sys_result = SystemInspectionResult(
            name=host, status=host_status)
        sys_result.save()

        self.scan_task.inspection_result.systems.add(sys_result)
        self.scan_task.inspection_result.save()

        # Generate facts for host
        for result_key, result_value in results.items():
            if result_value == process.NO_DATA:
                continue

            # Convert all values to JSON.  Noop for str, int
            final_value = json.dumps(result_value)
            stored_fact = RawFact(name=result_key,
                                  value=final_value)
            stored_fact.save()
            sys_result.facts.add(stored_fact)
        sys_result.save()
Exemple #2
0
    def test_expand_inspect_results(self):
        """Test view expand_inspect_results."""
        # pylint: disable=no-member
        scan_job, scan_task = create_scan_job(self.source,
                                              ScanTask.SCAN_TYPE_INSPECT)

        sys_result = SystemInspectionResult(name='Foo',
                                            status=SystemConnectionResult
                                            .SUCCESS)
        sys_result.save()

        fact = RawFact(name='foo', value='"value"')
        fact.save()

        sys_result.facts.add(fact)
        sys_result.save()

        inspect_result = scan_task.inspection_result
        inspect_result.systems.add(sys_result)
        inspect_result.save()

        inspect_results_json = {'task_results': [{}]}
        expand_inspect_results(
            scan_job.inspection_results, inspect_results_json)
        self.assertEqual(
            inspect_results_json['task_results'][0]['systems'][0]
            ['facts'][0]['name'],
            'foo')
Exemple #3
0
    def record_inspect_result(self,
                              name,
                              facts,
                              status=SystemInspectionResult.SUCCESS):
        """Record a new result.

        :param name: The host name
        :param facts: The dictionary of facts
        :param status: The status of the inspection
        """
        sys_result = SystemInspectionResult(name=name,
                                            source=self.source,
                                            status=status)
        sys_result.save()

        if status == SystemInspectionResult.SUCCESS:
            for key, val in facts.items():
                if val is not None:
                    final_value = json.dumps(val)
                    stored_fact = RawFact(name=key, value=final_value)
                    stored_fact.save()
                    sys_result.facts.add(stored_fact)

        self.inspect_scan_task.inspection_result.systems.add(sys_result)
        self.inspect_scan_task.inspection_result.save()

        if status == SystemInspectionResult.SUCCESS:
            self.inspect_scan_task.increment_stats(name,
                                                   increment_sys_scanned=True)
        elif status == SystemInspectionResult.UNREACHABLE:
            self.inspect_scan_task.increment_stats(
                name, increment_sys_unreachable=True)
        else:
            self.inspect_scan_task.increment_stats(name,
                                                   increment_sys_failed=True)
Exemple #4
0
    def get_vm_info(self, data_center, cluster, host, virtual_machine):
        """Get VM information.

        :param data_center: The data center name.
        :param cluster: The cluster name.
        :param host: The host server.
        :param virtual_machine: The virtual machine.
        """
        host_name = host.summary.config.name
        host_cpu_cores = host.summary.hardware.numCpuCores
        host_cpu_threads = host.summary.hardware.numCpuThreads
        host_cpu_count = host_cpu_threads // host_cpu_cores
        mac_addresses, ip_addresses = get_nics(virtual_machine.guest)
        summary = virtual_machine.summary
        config = summary.config
        vm_name = config.name
        vm_uuid = config.uuid
        vm_cpu_count = config.numCpu
        vm_os = config.guestFullName
        vm_mem = None
        if config.memorySizeMB is not None:
            vm_mem = int(config.memorySizeMB / 1024)

        # Need to obtain DNS Name
        facts = {'vm.name': vm_name,  # Name
                 'vm.state': summary.runtime.powerState,  # State
                 'vm.uuid': vm_uuid,  # UUID
                 'vm.cpu_count': vm_cpu_count,  # CPU
                 'vm.memory_size': vm_mem,  # Memory
                 'vm.os': vm_os,  # Guest OS
                 'vm.dns_name': summary.guest.hostName,  # DNS NAME
                 # Mac Addresses
                 'vm.mac_addresses': mac_addresses,
                 'vm.ip_addresses': ip_addresses,  # IP Addresses
                 'vm.host.name': host_name,  # Host Name
                 'vm.host.cpu_cores': host_cpu_cores,  # Host CPU Cores
                 'vm.host.cpu_threads': host_cpu_threads,  # Host CPU Threads
                 'vm.host.cpu_count': host_cpu_count,  # Host CPU Count
                 'vm.datacenter': data_center,  # Data Center
                 'vm.cluster': cluster}  # Cluster

        logger.debug('system %s facts=%s', vm_name, facts)

        sys_result = SystemInspectionResult(
            name=vm_name, status=SystemInspectionResult.SUCCESS)
        sys_result.save()
        for key, val in facts.items():
            if val is not None:
                final_value = json.dumps(val)
                stored_fact = RawFact(name=key, value=final_value)
                stored_fact.save()
                sys_result.facts.add(stored_fact)
        sys_result.save()

        self.inspect_result.systems.add(sys_result)
        self.inspect_result.save()

        self.scan_task.increment_stats(vm_name, increment_sys_scanned=True)
Exemple #5
0
 def test_host_details_skip(self):
     """Test host_details method for already captured data."""
     sys_result = SystemInspectionResult(
         name='sys1', status=SystemInspectionResult.SUCCESS)
     sys_result.save()
     self.inspect_result.systems.add(sys_result)
     self.inspect_result.save()
     detail = self.api.host_details(1, 'sys1')
     self.assertEqual(len(self.inspect_result.systems.all()), 1)
     self.assertEqual(detail, {})
Exemple #6
0
    def test_details(self):
        """Get ScanJob result details by primary key."""
        # pylint: disable=no-member
        scan_job, scan_task = create_scan_job(
            self.source, ScanTask.SCAN_TYPE_INSPECT)

        # Create a connection system result
        sys_result = SystemConnectionResult(name='Foo',
                                            credential=self.cred,
                                            status=SystemConnectionResult
                                            .SUCCESS)
        sys_result.save()
        conn_result = scan_task.prerequisites.first().connection_result
        conn_result.systems.add(sys_result)
        conn_result.save()

        # Create an inspection system result
        sys_result = SystemInspectionResult(name='Foo',
                                            status=SystemConnectionResult
                                            .SUCCESS)
        sys_result.save()

        fact = RawFact(name='fact_key', value='"fact_value"')
        fact.save()
        sys_result.facts.add(fact)
        sys_result.save()

        inspect_result = scan_task.inspection_result
        inspect_result.systems.add(sys_result)
        inspect_result.save()
        scan_job.save()

        url = reverse('scanjob-detail', args=(scan_job.id,)) + 'results/'
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        json_response = response.json()
        self.assertIn('connection_results', json_response)
        self.assertIn('inspection_results', json_response)

        self.assertEqual(
            json_response, {
                'connection_results': {'task_results': [
                    {'source':
                     {'id': 1, 'name': 'source1', 'source_type': 'network'},
                     'systems':
                     [{'name': 'Foo', 'credential':
                       {'id': 1, 'name': 'cred1'},
                       'status': 'success'}]}]},
                'inspection_results': {'task_results': [
                    {'source':
                     {'id': 1, 'name': 'source1', 'source_type': 'network'},
                     'systems':
                     [{'name': 'Foo', 'status': 'success',
                       'facts': [
                           {'name': 'fact_key', 'value': 'fact_value'}]}]}]}})
Exemple #7
0
 def test_host_details_skip(self):
     """Test host_details method for already captured data."""
     # pylint: disable=no-member
     sys_result = SystemInspectionResult(
         name='sys1', status=SystemInspectionResult.SUCCESS)
     sys_result.save()
     inspect_result = self.scan_task.inspection_result
     inspect_result.systems.add(sys_result)
     inspect_result.save()
     detail = self.api.host_details(1, 'sys1')
     self.assertEqual(len(inspect_result.systems.all()), 1)
     self.assertEqual(detail, {})
Exemple #8
0
    def get_vm_info(self, data_center, cluster, host, virtual_machine):
        """Get VM information.

        :param data_center: The data center name.
        :param cluster: The cluster name.
        :param host: The host server.
        :param virtual_machine: The virtual machine.
        """
        vm_name = virtual_machine.summary.config.name
        summary = vmsummary(virtual_machine.summary, virtual_machine.guest)

        # Need to obtain "DNS Name"
        facts = {
            'vm.name': vm_name,  # "Name"
            'vm.state': summary['state'],  # "State"
            'vm.uuid': summary['uuid'],  # "UUID"
            'vm.cpu_count': summary['cpu'],  # "CPU"
            'vm.memory_size': summary['mem'],  # "Memory"
            'vm.os': summary['ostype'],  # "Guest OS"
            'vm.dns_name': summary['hostname'],  # "DNS NAME"
            'vm.mac_address': summary['mac'],  # "Mac Address"
            'vm.ip_address': summary['ip_address'],  # "IP Address"
            'vm.host': host,  # "Host"
            'vm.datacenter': data_center,  # "Data Center"
            'vm.cluster': cluster
        }  # "Cluster"

        logger.debug('system %s facts=%s', vm_name, facts)

        sys_result = SystemInspectionResult(
            name=vm_name, status=SystemInspectionResult.SUCCESS)
        sys_result.save()
        for key, val in facts.items():
            if val is not None:
                stored_fact = RawFact(name=key, value=val)
                stored_fact.save()
                sys_result.facts.add(stored_fact)
        sys_result.save()

        self.inspect_results.save()
        inspect_result = self.inspect_results.results.filter(
            source__id=self.scan_task.source.id).first()
        if inspect_result is None:
            inspect_result = InspectionResult(source=self.scan_task.source,
                                              scan_task=self.scan_task)
            inspect_result.save()
        inspect_result.systems.add(sys_result)
        inspect_result.save()
        self.inspect_results.results.add(inspect_result)
        self.inspect_results.save()

        self.scan_task.systems_scanned += 1
        self.scan_task.save()
Exemple #9
0
    def v2_runner_on_ok(self, result):
        """Print a json representation of the result."""
        result_obj = _construct_result(result)
        self.results.append(result_obj)
        logger.debug('%s', result_obj)
        # pylint: disable=protected-access
        host = str(result._host)
        if (result._result is not None and isinstance(result._result, dict) and
                'ansible_facts' in result._result):
            host_facts = result._result['ansible_facts']
            facts = {}
            for key, value in host_facts.items():
                if key == 'host_done':
                    facts = self._ansible_facts[host]
                    logger.debug('host scan complete for %s with facts %s',
                                 host, facts)

                    # Update scan counts and save
                    if self.scan_task is not None:
                        self.scan_task.systems_scanned += 1
                        self.scan_task.save()

                    # Save facts for host
                    sys_result = SystemInspectionResult(
                        name=host, status=SystemInspectionResult.SUCCESS)
                    sys_result.save()
                    for result_key, result_value in facts.items():
                        stored_fact = RawFact(name=result_key,
                                              value=result_value)
                        stored_fact.save()
                        sys_result.facts.add(stored_fact)
                    sys_result.save()

                    inspect_result = self.inspect_results.results.filter(
                        source__id=self.source.id).first()
                    if inspect_result is None:
                        inspect_result = InspectionResult(
                            scan_task=self.scan_task, source=self.source)
                        inspect_result.save()
                    inspect_result.systems.add(sys_result)
                    inspect_result.save()
                    self.inspect_results.results.add(inspect_result)
                    self.inspect_results.save()

                elif not key.startswith('internal'):
                    facts[key] = value

            if host in self._ansible_facts:
                self._ansible_facts[host].update(facts)
            else:
                self._ansible_facts[host] = facts
Exemple #10
0
 def test_host_details_skip(self):
     """Test host_details method for already captured data."""
     sys_result = SystemInspectionResult(
         name='sys1',
         status=SystemInspectionResult.SUCCESS)
     sys_result.save()
     self.inspect_result.systems.add(sys_result)
     self.inspect_result.save()
     virt = {2: {'uuid': 2, 'name': 'sys2', 'num_virtual_guests': 3}}
     detail = self.api.host_details(host_id=1, host_name='sys1',
                                    last_checkin='', virtual_hosts=virt,
                                    virtual_guests={1: 2})
     self.assertEqual(len(self.inspect_result.systems.all()), 1)
     self.assertEqual(detail, {})
 def test_recurse_datacenter(self):
     """Test the recurse_datacenter method."""
     inspect_result = TaskInspectionResult(source=self.scan_task.source,
                                           scan_task=self.scan_task)
     inspect_result.save()
     sys_result = SystemInspectionResult(
         name='vm1', status=SystemInspectionResult.SUCCESS)
     sys_result.save()
     inspect_result.systems.add(sys_result)
     inspect_result.save()
     self.inspect_results.results.add(inspect_result)
     self.inspect_results.save()
     vcenter = Mock()
     content = Mock()
     root_folder = Mock()
     child_entity = []
     for k in range(0, 2):
         child = Mock()
         child.name = 'dc' + str(k)
         host_folder = Mock()
         clusters = []
         for j in range(0, 1):
             cluster = Mock()
             cluster.name = 'cluster' + str(j)
             host = Mock()
             h_summary = Mock(name='h_summary')
             h_config = Mock(name='h_config')
             h_config.name = 'host1'
             h_summary.config = h_config
             host.summary = h_summary
             virtual_machine = Mock(name='vm')
             virtual_machine.summary.config.name = 'host1'
             host.vm = [virtual_machine]
             hosts = [host]
             cluster.host = hosts
             clusters.append(cluster)
         host_folder.childEntity = clusters
         child.hostFolder = host_folder
         child_entity.append(child)
     root_folder.childEntity = child_entity
     content.rootFolder = root_folder
     vcenter.RetrieveContent = Mock(return_value=content)
     with patch.object(InspectTaskRunner,
                       'get_vm_info') as mock_get_vm_info:
         self.runner.inspect_result = inspect_result
         self.runner.recurse_datacenter(vcenter)
         mock_get_vm_info.assert_called_with(ANY, ANY, ANY, ANY)
Exemple #12
0
    def test_expand_inspect_results(self):
        """Test view expand_inspect_results."""
        scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_INSPECT)
        scan_job.save()
        scan_job.sources.add(self.source)

        # Job in created state
        self.assertEqual(scan_job.status, ScanTask.CREATED)
        tasks = scan_job.tasks.all()
        self.assertEqual(len(tasks), 0)

        # Queue job to run
        scan_job.queue()

        inspect_task = scan_job.tasks.all()[1]

        inspect_results = JobInspectionResult()
        inspect_results.save()

        scan_job.inspection_results = inspect_results
        scan_job.save()

        inspect_result = TaskInspectionResult(source=inspect_task.source,
                                              scan_task=inspect_task)
        inspect_result.save()

        inspect_results.results.add(inspect_result)
        inspect_results.save()

        sys_result = SystemInspectionResult(
            name='Foo', status=SystemConnectionResult.SUCCESS)
        sys_result.save()

        fact = RawFact(name='foo', value='"value"')
        fact.save()
        sys_result.facts.add(fact)
        sys_result.save()

        inspect_result.systems.add(sys_result)
        inspect_result.save()

        inspect_results_json = {'results': [{}]}
        expand_inspect_results(inspect_results, inspect_results_json)
        self.assertEqual(
            inspect_results_json['results'][0]['systems'][0]['facts'][0]
            ['name'], 'foo')
    def _finalize_host(self, host, host_status):
        results = self._ansible_facts.pop(host, {})

        if settings.QPC_EXCLUDE_INTERNAL_FACTS:
            # remove internal facts before saving result
            results = {
                fact_key: fact_value
                for fact_key, fact_value in results.items()
                if not fact_key.startswith(INTERNAL_)
            }
        self.scan_task.log_message('host scan complete for %s.  '
                                   'Status: %s. Facts %s' %
                                   (host, host_status, results),
                                   log_level=logging.DEBUG)

        # Update scan counts
        if self.scan_task is not None:
            if host_status == SystemInspectionResult.SUCCESS:
                self.scan_task.increment_stats(host,
                                               increment_sys_scanned=True)
            elif host_status == SystemInspectionResult.UNREACHABLE:
                self.scan_task.increment_stats(host,
                                               increment_sys_unreachable=True)
            else:
                self.scan_task.increment_stats(host, increment_sys_failed=True)

        sys_result = SystemInspectionResult(name=host,
                                            status=host_status,
                                            source=self.scan_task.source)
        sys_result.save()

        self.scan_task.inspection_result.systems.add(sys_result)
        self.scan_task.inspection_result.save()

        # Generate facts for host
        for result_key, result_value in results.items():
            if result_value == process.NO_DATA:
                continue

            # Convert all values to JSON.  Noop for str, int
            final_value = json.dumps(result_value)
            stored_fact = RawFact(name=result_key, value=final_value)
            stored_fact.save()
            sys_result.facts.add(stored_fact)
        sys_result.save()
Exemple #14
0
    def test_delete_scan_cascade(self, start_scan):
        """Delete a scan and its related data."""
        # pylint: disable=no-member
        scan_job, scan_task = create_scan_job(
            self.source, ScanTask.SCAN_TYPE_INSPECT)

        scan = scan_job.scan
        scan_id = scan.id

        self.create_job_expect_201(scan_id)

        # Create a connection system result
        sys_result = SystemConnectionResult(name='Foo',
                                            credential=self.cred,
                                            status=SystemConnectionResult
                                            .SUCCESS)
        sys_result.save()
        conn_result = scan_task.prerequisites.first().connection_result
        conn_result.systems.add(sys_result)
        conn_result.save()

        # Create an inspection system result
        sys_result = SystemInspectionResult(name='Foo',
                                            status=SystemConnectionResult
                                            .SUCCESS)
        sys_result.save()

        fact = RawFact(name='fact_key', value='"fact_value"')
        fact.save()
        sys_result.facts.add(fact)
        sys_result.save()

        inspect_result = scan_task.inspection_result
        inspect_result.systems.add(sys_result)
        inspect_result.save()
        scan_job.save()

        job_count = len(scan.jobs.all())
        self.assertNotEqual(job_count, 0)
        url = reverse('scan-detail', args=(scan_id,))
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code,
                         status.HTTP_204_NO_CONTENT)
        job_count = len(scan.jobs.all())
        self.assertEqual(job_count, 0)
Exemple #15
0
 def test_host_details_skip(self):
     """Test host_details method for already captured data."""
     # pylint: disable=no-member
     sys_result = SystemInspectionResult(
         name='sys1_1', status=SystemInspectionResult.SUCCESS)
     sys_result.save()
     inspect_result = self.scan_task.inspection_result
     inspect_result.systems.add(sys_result)
     inspect_result.save()
     virt = {2: {'uuid': 2, 'name': 'sys2', 'num_virtual_guests': 3}}
     detail = self.api.host_details(host_id=1,
                                    host_name='sys1',
                                    last_checkin='',
                                    virtual_hosts=virt,
                                    virtual_guests={1: 2},
                                    physical_hosts=[])
     self.assertEqual(len(inspect_result.systems.all()), 1)
     self.assertEqual(detail, None)
Exemple #16
0
    def record_inspect_result(self, name, facts):
        """Record a new result.

        :param name: The host name
        :param facts: The dictionary of facts
        """
        sys_result = SystemInspectionResult(
            name=name, status=SystemInspectionResult.SUCCESS)
        sys_result.save()

        for key, val in facts.items():
            if val is not None:
                final_value = json.dumps(val)
                stored_fact = RawFact(name=key, value=final_value)
                stored_fact.save()
                sys_result.facts.add(stored_fact)

        self.inspect_result.systems.add(sys_result)
        self.inspect_result.save()

        self.scan_task.increment_stats(name, increment_sys_scanned=True)
    def v2_runner_on_unreachable(self, result):
        """Print a json representation of the result."""
        result_obj = _construct_result(result)
        self.results.append(result_obj)
        logger.warning('%s', result_obj)

        # pylint: disable=protected-access
        unreachable_host = result._host.name
        result_message = result._result.get(
            'msg', 'No information given on unreachable warning.  '
            'Missing msg attribute.')
        message = 'UNREACHABLE %s. %s' % (unreachable_host, result_message)
        self.scan_task.log_message(message, log_level=logging.ERROR)

        self._get_inspect_result()
        sys_result = SystemInspectionResult(
            name=unreachable_host, status=SystemInspectionResult.UNREACHABLE)
        sys_result.save()

        self.scan_task.increment_stats(unreachable_host,
                                       increment_sys_failed=True)
Exemple #18
0
    def _add_unreachable_hosts(self, systems_list):
        """Add entry for systems that were unreachable.

        :param systems_list: Current list of system results.
        """
        connected_hosts = \
            self.connect_scan_task.connection_result.systems.filter(
                status=SystemConnectionResult.SUCCESS).values('name')
        # pylint: disable=consider-using-set-comprehension
        connected_hosts = set(
            [system.get('name') for system in connected_hosts])
        scanned_hosts = set(
            [system.get(NETWORK_SCAN_IDENTITY_KEY) for system in systems_list])
        unreachable_hosts = connected_hosts - scanned_hosts

        for host in unreachable_hosts:
            sys_result = SystemInspectionResult(
                name=host,
                status=SystemInspectionResult.UNREACHABLE,
                source=self.scan_task.source,
                task_inspection_result=self.scan_task.inspection_result)
            sys_result.save()
Exemple #19
0
    def _update_reachable_hosts(self, result_obj):
        if self.scan_task.scan_type != ScanTask.SCAN_TYPE_INSPECT:
            # Don't update for discovery scan.
            return

        unreachable_host = result_obj['host']
        logger.error(
            'Host %s is no longer reachable.  Moving host to failed results',
            unreachable_host)

        inspect_result = self.inspect_results.results.filter(
            source__id=self.source.id).first()
        if inspect_result is None:
            inspect_result = InspectionResult(
                scan_task=self.scan_task, source=self.source)
            inspect_result.save()
        sys_result = SystemInspectionResult(
            name=unreachable_host,
            status=SystemInspectionResult.UNREACHABLE)
        sys_result.save()
        inspect_result.systems.add(sys_result)
        inspect_result.save()
Exemple #20
0
    def test_merge_jobs_success(self):
        """Test merge jobs success."""
        # pylint: disable=no-member
        scan_job1, scan_task1 = create_scan_job(
            self.source, ScanTask.SCAN_TYPE_INSPECT,
            scan_name='test1')

        scan_job2, scan_task2 = create_scan_job(
            self.source, ScanTask.SCAN_TYPE_INSPECT,
            scan_name='test2')

        # Create a connection system result
        connect_sys_result = SystemConnectionResult(
            name='Foo',
            credential=self.cred,
            status=SystemConnectionResult
            .SUCCESS)
        connect_sys_result.save()

        # Create an inspection system result
        inspect_sys_result = SystemInspectionResult(
            name='Foo',
            status=SystemConnectionResult
            .SUCCESS)
        inspect_sys_result.save()

        fact = RawFact(name='fact_key', value='"fact_value"')
        fact.save()
        inspect_sys_result.facts.add(fact)
        inspect_sys_result.save()

        conn_result = scan_task1.prerequisites.first().connection_result
        conn_result.systems.add(connect_sys_result)
        conn_result.save()

        inspect_result = scan_task1.inspection_result
        inspect_result.systems.add(inspect_sys_result)
        inspect_result.save()
        scan_task1.status = ScanTask.COMPLETED
        scan_task1.save()

        conn_result = scan_task2.prerequisites.first().connection_result
        conn_result.systems.add(connect_sys_result)
        conn_result.save()

        inspect_result = scan_task2.inspection_result
        inspect_result.systems.add(inspect_sys_result)
        inspect_result.save()
        scan_task1.status = ScanTask.COMPLETED
        scan_task1.save()

        scan_job1.status = ScanTask.COMPLETED
        scan_job1.save()

        scan_job2.status = ScanTask.COMPLETED
        scan_job2.save()

        url = reverse('scanjob-merge')
        data = {'jobs': [scan_job1.id, scan_job2.id]}
        response = self.client.put(url, json.dumps(data),
                                   content_type='application/json',
                                   format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        json_response = response.json()
        expected = {'id': 1, 'sources':
                    [{'source_id': 1,
                      'source_type': 'network',
                      'facts': [{'fact_key': 'fact_value'}]},
                     {'source_id': 1,
                      'source_type': 'network',
                      'facts': [{'fact_key': 'fact_value'}]}],
                    'status': 'complete'}
        self.assertEqual(
            json_response, expected)
Exemple #21
0
    def parse_vm_props(self, props, host_dict):
        """Parse Virtual Machine properties.

        :param props: Array of Dynamic Properties
        :param host_dict: Dictionary of host properties
        """
        now = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')

        facts = raw_facts_template()
        for prop in props:
            if prop.name == 'name':
                facts[VcenterRawFacts.NAME] = prop.val
            if prop.name == 'guest.net':
                mac_addresses, ip_addresses = get_nics(prop.val)
                facts[VcenterRawFacts.MAC_ADDRESSES] = mac_addresses
                facts[VcenterRawFacts.IP_ADDRESSES] = ip_addresses
            elif prop.name == 'summary.runtime.powerState':
                facts[VcenterRawFacts.STATE] = prop.val
                if facts[VcenterRawFacts.STATE] == "poweredOn":
                    facts[VcenterRawFacts.LAST_CHECK_IN] = now
            elif prop.name == 'summary.guest.hostName':
                facts[VcenterRawFacts.DNS_NAME] = prop.val
            elif prop.name == "config.template":
                facts[VcenterRawFacts.TEMPLATE] = prop.val
            elif prop.name == 'summary.config.guestFullName':
                facts[VcenterRawFacts.OS] = prop.val
            elif prop.name == 'summary.config.memorySizeMB':
                facts[VcenterRawFacts.MEMORY_SIZE] = int(prop.val / 1024)
            elif prop.name == 'summary.config.numCpu':
                facts[VcenterRawFacts.CPU_COUNT] = prop.val
            elif prop.name == 'summary.config.uuid':
                facts[VcenterRawFacts.UUID] = prop.val
            elif prop.name == 'runtime.host':
                host_facts = host_dict.get(str(prop.val))
                if host_facts:
                    facts[VcenterRawFacts.HOST_NAME] = host_facts.get(HostRawFacts.NAME)
                    facts[VcenterRawFacts.HOST_UUID] = host_facts.get(HostRawFacts.UUID)
                    facts[VcenterRawFacts.HOST_CPU_CORES] = host_facts.get(
                        HostRawFacts.CPU_CORES
                    )
                    facts[VcenterRawFacts.HOST_CPU_COUNT] = host_facts.get(
                        HostRawFacts.CPU_COUNT
                    )
                    facts[VcenterRawFacts.HOST_CPU_THREADS] = host_facts.get(
                        HostRawFacts.CPU_THREADS
                    )
                    facts[VcenterRawFacts.CLUSTER] = host_facts.get(
                        HostRawFacts.CLUSTER
                    )
                    facts[VcenterRawFacts.DATACENTER] = host_facts.get(
                        HostRawFacts.DATACENTER
                    )

        vm_name = facts[VcenterRawFacts.NAME]

        logger.debug('system %s facts=%s', vm_name, facts)

        sys_result = SystemInspectionResult(
            name=vm_name, status=SystemInspectionResult.SUCCESS,
            source=self.scan_task.source,
            task_inspection_result=self.scan_task.inspection_result)
        sys_result.save()

        for key, val in facts.items():
            if val is not None:
                final_value = json.dumps(val)
                stored_fact = RawFact(
                    name=key,
                    value=final_value,
                    system_inspection_result=sys_result)
                stored_fact.save()

        self.scan_task.increment_stats(vm_name, increment_sys_scanned=True)
Exemple #22
0
    def test_details(self, start_scan):
        """Get ScanJob result details by primary key."""
        scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_INSPECT)
        scan_job.save()
        scan_job.sources.add(self.source)

        # Job in created state
        self.assertEqual(scan_job.status, ScanTask.CREATED)
        tasks = scan_job.tasks.all()
        self.assertEqual(len(tasks), 0)

        # Queue job to run
        scan_job.queue()

        conn_task = scan_job.tasks.first()

        conn_results = ConnectionResults(scan_job=scan_job)
        conn_results.save()

        conn_result = ConnectionResult(source=conn_task.source,
                                       scan_task=conn_task)
        conn_result.save()

        conn_results.results.add(conn_result)
        conn_results.save()

        sys_result = SystemConnectionResult(
            name='Foo',
            credential=self.cred,
            status=SystemConnectionResult.SUCCESS)
        sys_result.save()
        conn_result.systems.add(sys_result)
        conn_result.save()

        inspect_task = scan_job.tasks.all()[1]

        inspect_results = InspectionResults(scan_job=scan_job)
        inspect_results.save()

        inspect_result = InspectionResult(source=inspect_task.source,
                                          scan_task=inspect_task)
        inspect_result.save()

        inspect_results.results.add(inspect_result)
        inspect_results.save()

        sys_result = SystemInspectionResult(
            name='Foo', status=SystemConnectionResult.SUCCESS)
        sys_result.save()

        fact = RawFact(name='foo', value='value')
        fact.save()
        sys_result.facts.add(fact)
        sys_result.save()

        inspect_result.systems.add(sys_result)
        inspect_result.save()

        url = reverse('scanjob-detail', args=(scan_job.id, )) + 'results/'
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        json_response = response.json()
        self.assertIn('connection_results', json_response)
        self.assertIn('inspection_results', json_response)

        self.assertEqual(
            json_response, {
                'connection_results': {
                    'scan_job': 1,
                    'results': []
                },
                'inspection_results': {
                    'scan_job': 1,
                    'results': []
                }
            })
Exemple #23
0
    def parse_vm_props(self, props, host_dict):
        """Parse Virtual Machine properties.

        :param props: Array of Dynamic Properties
        :param host_dict: Dictionary of host properties
        """
        now = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')

        facts = {}
        for prop in props:
            if prop.name == 'name':
                facts['vm.name'] = prop.val
            if prop.name == 'guest.net':
                mac_addresses, ip_addresses = get_nics(prop.val)
                facts['vm.mac_addresses'] = mac_addresses
                facts['vm.ip_addresses'] = ip_addresses
            elif prop.name == 'summary.runtime.powerState':
                facts['vm.state'] = prop.val
                if facts['vm.state'] == 'poweredOn':
                    facts['vm.last_check_in'] = now
            elif prop.name == 'summary.guest.hostName':
                facts['vm.dns_name'] = prop.val
            elif prop.name == 'summary.config.guestFullName':
                facts['vm.os'] = prop.val
            elif prop.name == 'summary.config.memorySizeMB':
                facts['vm.memory_size'] = int(prop.val / 1024)
            elif prop.name == 'summary.config.numCpu':
                facts['vm.cpu_count'] = prop.val
            elif prop.name == 'summary.config.uuid':
                facts['vm.uuid'] = prop.val
            elif prop.name == 'runtime.host':
                host_facts = host_dict[str(prop.val)]
                facts['vm.host.name'] = host_facts['host.name']
                facts['vm.host.cpu_cores'] = host_facts['host.cpu_cores']
                facts['vm.host.cpu_count'] = host_facts['host.cpu_count']
                facts['vm.host.cpu_threads'] = host_facts['host.cpu_threads']
                facts['vm.cluster'] = host_facts['host.cluster']
                facts['vm.datacenter'] = host_facts['host.datacenter']

        vm_name = facts['vm.name']

        logger.debug('system %s facts=%s', vm_name, facts)

        sys_result = SystemInspectionResult(
            name=vm_name,
            status=SystemInspectionResult.SUCCESS,
            source=self.scan_task.source)
        sys_result.save()

        for key, val in facts.items():
            if val is not None:
                final_value = json.dumps(val)
                stored_fact = RawFact(name=key, value=final_value)
                stored_fact.save()
                sys_result.facts.add(stored_fact)

        sys_result.save()

        self.scan_task.inspection_result.systems.add(sys_result)
        self.scan_task.inspection_result.save()

        self.scan_task.increment_stats(vm_name, increment_sys_scanned=True)