Esempio n. 1
0
    def test_init(self):
        """Testing method init."""
        # Testing with non-existant directory
        directory = 'bogus'
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        with self.assertRaises(SystemExit):
            configuration.Config()

        # Testing with an empty directory
        empty_directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = empty_directory
        with self.assertRaises(SystemExit):
            configuration.Config()

        # Write bad_config to file
        empty_config_file = ('%s/test_config.yaml') % (empty_directory)
        with open(empty_config_file, 'w') as f_handle:
            f_handle.write('')

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.log_file()

        # Cleanup files in temp directories
        _delete_files(directory)
Esempio n. 2
0
    def test_bind_port(self):
        """Testing method bind_port."""
        # Testing bind_port with good_dictionary
        # good key and key_value
        result = self.config.bind_port()
        self.assertEqual(result, 3000)
        self.assertEqual(result, self.good_dict['main']['bind_port'])

        # Set the environmental variable for the configuration directory
        directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        config_file = ('%s/test_config.yaml') % (directory)

        # Testing bind_port with blank key and blank key_value
        key = ''
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        # Write bad_config to file
        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.bind_port()

        # Testing bind_port with good key and blank key_value
        key = 'bind_port:'
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        # Write bad_config to file
        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        result = config.bind_port()
        self.assertEqual(result, 7000)

        # Cleanup files in temp directories
        _delete_files(directory)
Esempio n. 3
0
    def test_log_directory(self):
        """Testing method log_directory."""
        # Testing log_directory with temp directory
        # Set the environmental variable for the configuration directory
        directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        config_file = ('%s/test_config.yaml') % (directory)

        # Testing log_directory with blank key_value(filepath)
        key = ''
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.log_directory()

        # Cleanup files in temp directories
        _delete_files(directory)
Esempio n. 4
0
def poller(args):
    """Process 'show poller' commands.

    Args:
        args: Argparse arguments

    Returns:
        None

    """
    if args.subqualifier == 'logs':
        # Process logs
        config = configuration.Config()
        filename = config.log_file()
        tail = input_output.File(filename)
        tail.tail()

        # Done
        sys.exit(0)

    elif args.subqualifier == 'status':
        # Create agent objects
        agent_poller = Agent(POLLER_EXECUTABLE)

        # Get agent status
        daemon_poller = AgentDaemon(agent_poller)
        daemon_poller.status()

        # Done
        sys.exit(0)

    # Show help if there are no matches
    general.cli_help()
Esempio n. 5
0
def check_user():
    """Check to make sure the user environment is correct.

    Args:
        None

    Returns:
        None

    """
    # Initialize key variables
    username = getpass.getuser()
    config = configuration.Config()
    configured_username = config.username()

    # Prevent running as sudo user
    check_sudo()

    # Prevent others from running the script
    if username != configured_username:
        log_message = (
            'You can only run this script as user \'{}\' '
            'in the configuration file. Try running the command like this:\n'
            '').format(configured_username)
        print(log_message)
        fixed_command = ('$ su -c \'{}\' {}\n'.format(' '.join(sys.argv[:]),
                                                      configured_username))
        print(fixed_command)
        sys.exit(2)
Esempio n. 6
0
    def __init__(self, searchstring):
        """Method initializing the class.

        Args:
            searchstring: search string to look for

        Returns:
            None

        """
        # Initialize key variables
        self._search = []
        self.config = configuration.Config()

        # Generate various possible strings that could be used for searches
        self._search = _search_list(searchstring)

        # Read ifindex file
        self.ifindex_data = general.read_yaml_file(self.config.ifindex_file())

        # Read host file
        self.arp_data = general.read_yaml_file(self.config.arp_file())

        # Read ifalias file
        self.ifalias_data = general.read_yaml_file(self.config.ifalias_file())
Esempio n. 7
0
    def __init__(self, parent, child):
        """Method initializing the class.

        Args:
            parent: Name of parent daemon
            child: Name of child daemon

        Returns:
            None

        """
        # Initialize key variables
        Agent.__init__(self, parent, child)
        self.config = configuration.Config()
Esempio n. 8
0
def main():
    """Initialize the class.

    Args:
        config: Configuration object
        host: Hostname to process

    Returns:
        None

    """
    # Initialize key variables
    input_filename = sys.argv[1]
    config = configuration.Config()
    mac_addresses = {}
    mac_hex_regex = (
        '[0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F]')
    regex = re.compile(r'^{}\s+.*?\(hex\)\s+.*?$'.format(mac_hex_regex))

    # Ingest file
    if os.path.isfile(input_filename) is False:
        log_message = 'Filename {} not found.'.format(input_filename)
        log.log2die(1025, log_message)
    if input_filename.endswith('oui.txt') is False:
        log_message = ('Filename {} is invalid. It must end with '
                       '\'oui.txt\'.'.format(input_filename))
        log.log2die(1000, log_message)

    with open(input_filename) as f_handle:
        lines = f_handle.readlines()

    # Read data
    for line in lines:
        # Create dictionary of mac addresses
        if bool(regex.match(line)) is True:
            (field1, field2) = line.split('(hex)')
            mac_address = field1.strip().lower().replace('-', '')
            company = field2.strip().replace(':', ' ')
            mac_addresses[mac_address] = company

    # Output the file to the metadata directory
    output_file = config.mac_address_file()
    if bool(mac_addresses) is True:
        with open(output_file, 'w') as csvfile:
            spamwriter = csv.writer(csvfile,
                                    delimiter=':',
                                    quoting=csv.QUOTE_NONE)
            for key, value in sorted(mac_addresses.items()):
                spamwriter.writerow([key, value])
Esempio n. 9
0
    def __init__(self):
        """Method initializing the class."""
        # Define key variables
        app_name = 'switchmap'
        levels = {
            'debug': logging.DEBUG,
            'info': logging.INFO,
            'warning': logging.WARNING,
            'error': logging.ERROR,
            'critical': logging.CRITICAL
        }

        # Get the logging directory
        config = configuration.Config()
        log_file = config.log_file()
        config_log_level = config.log_level()

        # Set logging level
        if config_log_level in levels:
            log_level = levels[config_log_level]
        else:
            log_level = levels['debug']

        # create logger with app_name
        self.logger_file = logging.getLogger(('%s_file') % (app_name))
        self.logger_stdout = logging.getLogger(('%s_console') % (app_name))

        # Set logging levels to file and stdout
        self.logger_stdout.setLevel(log_level)
        self.logger_file.setLevel(log_level)

        # create file handler which logs even debug messages
        file_handler = logging.FileHandler(log_file)
        file_handler.setLevel(log_level)

        # create console handler with a higher log level
        stdout_handler = logging.StreamHandler()
        stdout_handler.setLevel(log_level)

        # create formatter and add it to the handlers
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        file_handler.setFormatter(formatter)
        stdout_handler.setFormatter(formatter)

        # add the handlers to the logger
        self.logger_file.addHandler(file_handler)
        self.logger_stdout.addHandler(stdout_handler)
Esempio n. 10
0
def _hostnames():
    """Process 'show hostnames' commands.

    Args:
        None

    Returns:
        None

    """
    config = configuration.Config()
    _hostnames = config.hostnames()
    for _hostname in _hostnames:
        print(_hostname)

    # Done
    sys.exit(0)
Esempio n. 11
0
def run(args):
    """Process 'test' command.

    Args:
        parser: Argparse parser
        args: Argparse arguments

    Returns:
        None

    """
    # Process the config
    snmp_config = configuration.ConfigSNMP()
    config = configuration.Config()

    # Show help if no arguments provided
    if args.qualifier is None:
        general.cli_help()

    # Test a single host
    if bool(args.hostname) is True:
        hostname = args.hostname
        test_hostname(hostname, snmp_config)
        sys.exit(0)

    # Test all hosts
    elif bool(args.all) is True:
        hosts = config.hostnames()
        if isinstance(hosts, list) is True:
            if len(hosts) > 0:
                for host in hosts:
                    test_hostname(host, snmp_config)
                sys.exit(0)
            else:
                # No hosts found
                log_message = 'No hosts found in configuration'
                log.log2see(1038, log_message)

        else:
            # No hosts found
            log_message = 'No hosts found in configuration'
            log.log2see(1039, log_message)

    # Show help if there are no matches
    general.cli_help()
Esempio n. 12
0
def _configuration():
    """Process 'show hostnames' commands.

    Args:
        None

    Returns:
        None

    """
    config = configuration.Config()
    contents = config.configuration()
    print('')
    pprint(contents, indent=2)
    print('\n# Configuration read from directory: {}\n'
          ''.format(config.configuration_directory()))

    # Done
    sys.exit(0)
Esempio n. 13
0
def get_hosts():
    """Get hosts listed in toplogy YAML files.

    Args:
        None

    Returns:
        hosts: List of hostnames

    """
    # Read configuration
    hosts = []
    topology_directory = configuration.Config().topology_directory()

    # Cycle through list of files in directory
    for filename in os.listdir(topology_directory):
        if filename.endswith('.yaml'):
            hostname = filename[:-5]
            hosts.append(hostname)
    return sorted(hosts)
Esempio n. 14
0
    def __init__(self):
        """Function for intializing the class.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        running_username = getpass.getuser()
        daemon_username = configuration.Config().username()
        self.root_directory = general.root_directory()
        switchmap_user_exists = True
        self.switchmap_user = None
        self.running_as_root = False

        # Set the username we need to be running as
        if running_username == 'root':
            try:
                # Get GID and UID for user
                self.switchmap_user = daemon_username
                self.gid = getpwnam(self.switchmap_user).pw_gid
                self.uid = getpwnam(self.switchmap_user).pw_uid
            except KeyError:
                switchmap_user_exists = False

            # Die if user doesn't exist
            if switchmap_user_exists is False:
                log_message = (
                    'User {} not found. Please try again.'
                    ''.format(self.switchmap_user))
                log.log2die_safe(1129, log_message)
        else:
            self.switchmap_user = daemon_username

        # If running as the root user, then the switchmap user needs to exist
        if running_username == 'root':
            self.running_as_root = True
            return
Esempio n. 15
0
def api(args):
    """Process 'show api' commands.

    Args:
        args: Argparse arguments

    Returns:
        None

    """
    if args.subqualifier == 'logs':
        # Process logs
        config = configuration.Config()
        filename = config.web_log_file()
        tail = input_output.File(filename)
        tail.tail()

        # Done
        sys.exit(0)

    elif args.subqualifier == 'status':
        # Create agent objects
        agent_gunicorn = Agent(API_GUNICORN_AGENT)
        agent_api = AgentAPI(API_EXECUTABLE, API_GUNICORN_AGENT)

        # Get agent status
        daemon_gunicorn = AgentDaemon(agent_gunicorn)
        daemon_gunicorn.status()
        daemon_api = AgentDaemon(agent_api)
        daemon_api.status()

        # Done
        sys.exit(0)

    # Show help if there are no matches
    general.cli_help()
Esempio n. 16
0
# Do library imports
from switchmap.utils import configuration

# Create global variables used by all applications
CONFIG = configuration.Config()
CONFIG_SNMP = configuration.ConfigSNMP()

# Create global variables for the API
SITE_PREFIX = '/switchmap-ng'
API_PREFIX = '{}/api/v1'.format(SITE_PREFIX)
API_STATIC_FOLDER = 'static/default'
API_TEMPLATE_FOLDER = 'templates/default'
API_EXECUTABLE = 'switchmap-ng-api'
API_GUNICORN_AGENT = 'switchmap-ng-gunicorn'
POLLER_EXECUTABLE = 'switchmap-ng-poller'
Esempio n. 17
0
class TestConfig(unittest.TestCase):
    """Checks all functions and methods."""

    #########################################################################
    # General object setup
    #########################################################################

    random_string = ''.join([
        random.choice(string.ascii_letters + string.digits) for n in range(9)
    ])

    log_directory = tempfile.mkdtemp()
    cache_directory = tempfile.mkdtemp()
    good_config = ("""\
main:
    log_directory: %s
    cache_directory: %s
    agent_threads: 25
    bind_port: 3000
    hostnames:
    - 192.168.1.1
    - 192.168.1.2
    - 192.168.1.3
    - 192.168.1.4
    listen_address: 0.0.0.0
    log_level: debug
    polling_interval: 20
""") % (log_directory, cache_directory)

    # Convert good_config to dictionary
    good_dict = yaml.load(bytes(good_config, 'utf-8'))

    # Set the environmental variable for the configuration directory
    directory = tempfile.mkdtemp()
    os.environ['SWITCHMAP_CONFIGDIR'] = directory
    config_file = ('%s/test_config.yaml') % (directory)

    # Write good_config to file
    with open(config_file, 'w') as f_handle:
        yaml.dump(good_dict, f_handle, default_flow_style=True)

    # Create configuration object
    config = configuration.Config()

    @classmethod
    def tearDownClass(cls):
        """Post test cleanup."""
        os.rmdir(cls.log_directory)
        os.rmdir(cls.config.topology_directory())
        os.rmdir(cls.config.idle_directory())
        os.rmdir(cls.cache_directory)
        os.remove(cls.config_file)
        os.rmdir(cls.directory)

    def test_init(self):
        """Testing method init."""
        # Testing with non-existant directory
        directory = 'bogus'
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        with self.assertRaises(SystemExit):
            configuration.Config()

        # Testing with an empty directory
        empty_directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = empty_directory
        with self.assertRaises(SystemExit):
            configuration.Config()

        # Write bad_config to file
        empty_config_file = ('%s/test_config.yaml') % (empty_directory)
        with open(empty_config_file, 'w') as f_handle:
            f_handle.write('')

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.log_file()

        # Cleanup files in temp directories
        _delete_files(directory)

    def test_log_file(self):
        """Testing method log_file."""
        # Test the log_file with a good_dict
        # good key and key_value
        result = self.config.log_file()
        self.assertEqual(result,
                         ('%s/switchmap-ng.log') % (self.log_directory))

    def test_web_log_file(self):
        """Testing method web_log_file ."""
        # Testing web_log_file with a good dictionary.
        result = self.config.web_log_file()
        self.assertEqual(result,
                         ('%s/switchmap-ng-api.log') % (self.log_directory))

    def test_log_level(self):
        """Testing method log_level."""
        # Tesing with a good_dictionary
        # good key and good key_value
        result = self.config.log_level()
        self.assertEqual(result, 'debug')
        self.assertEqual(result, self.good_dict['main']['log_level'])

        # Set the environmental variable for the configuration directory
        directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        config_file = ('%s/test_config.yaml') % (directory)

        # Testing log_level with blank key and blank key_value
        key = ''
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        # Write bad_config to file
        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.log_level()

        # Testing log_level with good key and blank key_value
        key = 'log_level:'
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        # Write bad_config to file
        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.log_level()

        # Cleanup files in temp directories
        _delete_files(directory)

    def test_cache_directory(self):
        """Testing method cache_directory."""
        # Testing cache_directory with temp directory
        # Set the environmental variable for the configuration directory
        directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        config_file = ('%s/test_config.yaml') % (directory)

        # Testing cache_directory with blank key_value(filepath)
        key = ''
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.cache_directory()

        # Cleanup files in temp directories
        _delete_files(directory)

    def test_agent_threads(self):
        """Testing method agent_threads."""
        # Testing agent_threads with good_dict
        # good key and key_value
        result = self.config.agent_threads()

        # Get CPU cores
        cores = multiprocessing.cpu_count()
        desired_max_threads = max(1, cores - 1)

        # We don't want a value that's too big that the CPU cannot cope
        expected = min(result, desired_max_threads)

        self.assertEqual(result, expected)

    def test_polling_interval(self):
        """Testing method polling_interval."""
        # Testing polling_interval with good_dictionary
        # good key and key_value
        result = self.config.polling_interval()
        self.assertEqual(result, 20)
        self.assertEqual(result, self.good_dict['main']['polling_interval'])

        # Set the environmental variable for the configuration directory
        directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        config_file = ('%s/test_config.yaml') % (directory)

        # Testing polling_interval with blank key and blank key_value
        key = ''
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        # Write bad_config to file
        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.polling_interval()

        # Testing polling_interval with good key and blank key_value
        key = 'polling_interval:'
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        # Write bad_config to file
        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        result = config.polling_interval()
        self.assertEqual(result, 86400)

        # Cleanup files in temp directories
        _delete_files(directory)

    def test_bind_port(self):
        """Testing method bind_port."""
        # Testing bind_port with good_dictionary
        # good key and key_value
        result = self.config.bind_port()
        self.assertEqual(result, 3000)
        self.assertEqual(result, self.good_dict['main']['bind_port'])

        # Set the environmental variable for the configuration directory
        directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        config_file = ('%s/test_config.yaml') % (directory)

        # Testing bind_port with blank key and blank key_value
        key = ''
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        # Write bad_config to file
        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.bind_port()

        # Testing bind_port with good key and blank key_value
        key = 'bind_port:'
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        # Write bad_config to file
        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        result = config.bind_port()
        self.assertEqual(result, 7000)

        # Cleanup files in temp directories
        _delete_files(directory)

    def test_idle_directory(self):
        """Testing function idle_directory."""
        # Verify that directory exists
        result = self.config.idle_directory()
        self.assertEqual(os.path.exists(result), True)
        self.assertEqual(os.path.isdir(result), True)

        # Doesn't fail because directory now exists
        result = self.config.idle_directory()
        expected = ('%s/idle') % (self.good_dict['main']['cache_directory'])
        self.assertEqual(result, expected)

    def test_topology_directory(self):
        """Testing function topology_directory."""
        # Verify that directory exists
        result = self.config.topology_directory()
        self.assertEqual(os.path.exists(result), True)
        self.assertEqual(os.path.isdir(result), True)

        # Doesn't fail because directory now exists
        result = self.config.topology_directory()
        expected = ('%s/topology') % (
            self.good_dict['main']['cache_directory'])
        self.assertEqual(result, expected)

    def test_topology_device_file(self):
        """Testing function topology_device_file."""
        # Recreate the path to the device file
        result = self.config.topology_device_file(self.random_string)
        expected = ('%s/%s.yaml') % (self.config.topology_directory(),
                                     self.random_string)
        self.assertEqual(result, expected)

    def test_hostnames(self):
        """Testing function hostnames."""
        # Test expected versus returned values
        result = self.config.hostnames()
        expected = sorted(self.good_dict['main']['hostnames'])
        self.assertEqual(result, expected)

    def test_log_directory(self):
        """Testing method log_directory."""
        # Testing log_directory with temp directory
        # Set the environmental variable for the configuration directory
        directory = tempfile.mkdtemp()
        os.environ['SWITCHMAP_CONFIGDIR'] = directory
        config_file = ('%s/test_config.yaml') % (directory)

        # Testing log_directory with blank key_value(filepath)
        key = ''
        key_value = ''
        bad_config = ("""\
main:
    %s %s
""") % (key, key_value)
        bad_dict = yaml.load(bytes(bad_config, 'utf-8'))

        with open(config_file, 'w') as f_handle:
            yaml.dump(bad_dict, f_handle, default_flow_style=True)

        # Create configuration object
        config = configuration.Config()
        with self.assertRaises(SystemExit):
            config.log_directory()

        # Cleanup files in temp directories
        _delete_files(directory)