Esempio n. 1
0
    def test_dict2yaml(self):
        """Testing method / function dict2yaml."""
        # Initializing key variables
        data_dict = {"1": "test 1", "two": "test 2"}
        data_yaml = """'1': test 1
two: test 2
"""
        # Do test with good dict
        yaml_result = testimport.dict2yaml(data_dict)
        self.assertEqual(yaml_result, data_yaml)
Esempio n. 2
0
    def test_dict2yaml(self):
        """Testing method / function dict2yaml."""
        # Initializing key variables
        data_dict = {'1': 'test 1', 'two': 'test 2'}
        data_yaml = """'1': test 1
two: test 2
"""
        # Do test with good dict
        yaml_result = testimport.dict2yaml(data_dict)
        self.assertEqual(yaml_result, data_yaml)
Esempio n. 3
0
    def run(self):
        """Update the database using threads."""
        while True:
            # Get the data_dict
            data_dict = self.queue.get()
            host = data_dict['host']
            config = data_dict['config']
            verbose = data_dict['verbose']
            temp_dir = data_dict['temp_dir']

            # Show host information
            validate = snmp_manager.Validate(host, config.snmp_auth())
            snmp_params = validate.credentials()
            snmp_object = snmp_manager.Interact(snmp_params)

            # Verbose output
            if verbose is True:
                output = ('Processing on: host %s') % (host)
                print(output)

            # Skip invalid, and uncontactable hosts
            if bool(snmp_params) is False:
                if verbose is True:
                    log_message = (
                        'Uncontactable host %s or no valid SNMP '
                        'credentials found for it.') % (host)
                    log.log2quiet(1019, log_message)
                continue

            # Process if valid
            if bool(snmp_params) is True:
                # Get data
                status = snmp_info.Query(snmp_object)
                data = status.everything()
                yaml_string = jm_general.dict2yaml(data)

                # Dump data
                temp_file = ('%s/%s.yaml') % (temp_dir, host)
                with open(temp_file, 'w') as file_handle:
                    file_handle.write(yaml_string)

                # Verbose output
                if verbose is True:
                    output = ('Completed run: host %s') % (host)
                    print(output)

            # Signals to queue job is done
            self.queue.task_done()
Esempio n. 4
0
    def run(self):
        """Update the database using threads."""
        while True:
            # Get the data_dict
            data_dict = self.queue.get()
            host = data_dict['host']
            config = data_dict['config']
            verbose = data_dict['verbose']
            temp_dir = data_dict['temp_dir']

            # Show host information
            validate = snmp_manager.Validate(host, config.snmp_auth())
            snmp_params = validate.credentials()
            snmp_object = snmp_manager.Interact(snmp_params)

            # Verbose output
            if verbose is True:
                output = ('Processing on: host %s') % (host)
                print(output)

            # Skip invalid, and uncontactable hosts
            if bool(snmp_params) is False:
                if verbose is True:
                    log_message = (
                        'Uncontactable host %s or no valid SNMP '
                        'credentials found for it.') % (host)
                    jm_general.logit(1019, log_message, False)
                continue

            # Process if valid
            if bool(snmp_params) is True:
                # Get data
                status = snmp_info.Query(snmp_object)
                data = status.everything()
                yaml_string = jm_general.dict2yaml(data)

                # Dump data
                temp_file = ('%s/%s.yaml') % (temp_dir, host)
                with open(temp_file, 'w') as file_handle:
                    file_handle.write(yaml_string)

                # Verbose output
                if verbose is True:
                    output = ('Completed run: host %s') % (host)
                    print(output)

            # Signals to queue job is done
            self.queue.task_done()
Esempio n. 5
0
    def _create_yaml(self):
        """Create the master dictionary for the host.

        Args:
            None
        Returns:
            value: Index value

        """
        # Initialize key variables
        temp_dir = tempfile.mkdtemp()
        temp_file = ('%s/%s.yaml') % (temp_dir, self.hostname)
        perm_file = self.server_config.topology_device_file(self.hostname)

        # Get data
        log_message = (
            'Querying topology data from host %s.'
            '') % (self.hostname)
        log.log2quiet(1125, log_message)

        # Create YAML file by polling device
        status = snmp_info.Query(self.snmp_object)
        data = status.everything()
        yaml_string = jm_general.dict2yaml(data)

        # Dump data
        with open(temp_file, 'w') as file_handle:
            file_handle.write(yaml_string)

        # Get data
        log_message = (
            'Completed topology query from host %s.'
            '') % (self.hostname)
        log.log2quiet(1019, log_message)

        # Clean up files
        os.rename(temp_file, perm_file)
        os.rmdir(temp_dir)