Exemple #1
0
    def test_snmp_defaults(self):
        """Test that SNMP defaults from the Panoptes configuration file are used if no plugin specific SNMP config is
        provided and no site specific community secret is avalilable in the secrets store"""
        global secret_store

        secret_store.get_by_site.return_value = None

        plugin_context = create_autospec(
            PanoptesPluginWithEnrichmentContext,
            instance=True,
            spec_set=True,
            data=panoptes_resource,
            config=plugin_conf,
            snmp=panoptes_context.config_object.snmp_defaults,
            secrets=secret_store,
            logger=logging.getLogger(__name__))

        snmp_configuration = PanoptesSNMPPluginConfiguration(plugin_context)

        self.assertEqual(snmp_configuration.port, 10161)
        self.assertEqual(snmp_configuration.proxy_port, 10161)
        self.assertEqual(snmp_configuration.connection_factory_module,
                         'yahoo_panoptes.framework.utilities.snmp.connection')
        self.assertEqual(snmp_configuration.connection_factory_class,
                         'PanoptesSNMPConnectionFactory')
        self.assertEqual(snmp_configuration.timeout, 5)
        self.assertEqual(snmp_configuration.retries, 1)
        self.assertEqual(snmp_configuration.non_repeaters, 0)
        self.assertEqual(snmp_configuration.max_repetitions, 25)
        self.assertEqual(snmp_configuration.community, 'public')
        self.assertEqual(snmp_configuration.community_string_key,
                         'snmp_community_string')
Exemple #2
0
    def test_snmp_plugin_community(self):
        """
        Test that the plugin specific community string is used if present
        """
        global secret_store

        secret_store.get_by_site.return_value = 'test_site_community'

        plugin_conf_with_community = {
            'Core': {
                'name': 'Test Plugin',
                'module': 'test_plugin'
            },
            'main': {
                'execute_frequency': '60',
                'resource_filter': 'resource_class = "network"'
            },
            'snmp': {
                'community': 'test_plugin_community'
            }
        }

        plugin_context = create_autospec(
            PanoptesPluginWithEnrichmentContext,
            instance=True,
            spec_set=True,
            data=panoptes_resource,
            config=plugin_conf_with_community,
            snmp=panoptes_context.config_object.snmp_defaults,
            secrets=secret_store,
            logger=logging.getLogger(__name__))

        snmp_configuration = PanoptesSNMPPluginConfiguration(plugin_context)
        self.assertEqual(snmp_configuration.community, 'test_plugin_community')
    def test_x509_defaults(self):
        """
        Test that x509 defaults from the Panoptes configuration file are used if no plugin specific SNMP config is
        provided
        """
        global secret_store

        secret_store.get_by_site.return_value = None

        plugin_context = create_autospec(
            PanoptesPluginWithEnrichmentContext,
            instance=True,
            spec_set=True,
            data=panoptes_resource,
            config=plugin_conf,
            snmp=panoptes_context.config_object.snmp_defaults,
            x509=panoptes_context.config_object.x509_defaults,
            secrets=secret_store,
            logger=logging.getLogger(__name__))

        x509_configuration = PanoptesSNMPPluginConfiguration(plugin_context)

        self.assertEqual(x509_configuration.x509_secure_connection, 0)
        self.assertEqual(x509_configuration.x509_cert_file,
                         u'/home/panoptes/x509/certs/panoptes.pem')
        self.assertEqual(x509_configuration.x509_key_file,
                         u'/home/panoptes/x509/keys/panoptes.key')
    def test_x509_bad_configuration_values(self):
        # x509_secure_connection
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_secured_requests=5))

        # x509_cert_location
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_cert_location=u''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_cert_location=0))
        # x509_cert_filename
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_cert_filename=u''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_cert_filename=0))

        # x509_key_location
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_key_location=u''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_key_location=0))
        # x509_key_filename
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_key_filename=u''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_x509_configuration(
                    x509_key_filename=0))
    def test_snmp_plugin_overrides(self):
        """
        Test that the plugin specific SNMP configuration is used if present
        """
        plugin_conf_with_overrides = {
            u'Core': {
                u'name': u'Test Plugin',
                u'module': u'test_plugin'
            },
            u'main': {
                u'execute_frequency': u'60',
                u'resource_filter': u'resource_class = "network"'
            },
            u'snmp': {
                u'connection_factory_module': u'test_module',
                u'connection_factory_class': u'test_class',
                u'port': 162,
                u'proxy_port': 10162,
                u'timeout': 10,
                u'retries': 2,
                u'non_repeaters': 1,
                u'max_repetitions': 10,
                u'community_string_key': u'test_community_string_key'
            }
        }

        plugin_context = create_autospec(
            PanoptesPluginWithEnrichmentContext,
            instance=True,
            spec_set=True,
            data=panoptes_resource,
            config=plugin_conf_with_overrides,
            snmp=panoptes_context.config_object.snmp_defaults,
            x509=panoptes_context.config_object.x509_defaults,
            secrets=secret_store,
            logger=logging.getLogger(__name__))

        snmp_configuration = PanoptesSNMPPluginConfiguration(plugin_context)
        self.assertEqual(snmp_configuration.port, 162)
        self.assertEqual(snmp_configuration.proxy_port, 10162)
        self.assertEqual(snmp_configuration.connection_factory_module,
                         u'test_module')
        self.assertEqual(snmp_configuration.connection_factory_class,
                         u'test_class')
        self.assertEqual(snmp_configuration.timeout, 10)
        self.assertEqual(snmp_configuration.retries, 2)
        self.assertEqual(snmp_configuration.non_repeaters, 1)
        self.assertEqual(snmp_configuration.max_repetitions, 10)
        self.assertEqual(snmp_configuration.community_string_key,
                         u'test_community_string_key')
Exemple #6
0
    def run(self, context):
        self._plugin_context = context
        self._plugin_config = context.config
        self._logger = context.logger
        self._execute_frequency = int(
            self._plugin_config[u'main'][u'execute_frequency'])
        self._resource = context.data
        self._enrichment = context.enrichment
        self._host = self._resource.resource_endpoint

        try:
            # Max Repetitions && Tests
            self._snmp_configuration = PanoptesSNMPPluginConfiguration(
                self._plugin_context)
        except Exception as e:
            raise PanoptesPluginConfigurationError(
                u'Error parsing SNMP configuration: {}'.format(repr(e)))

        try:
            self._get_snmp_connection()
        except Exception as e:
            raise PanoptesPluginRuntimeError(
                u'Error creating SNMP connection: {}'.format(repr(e)))

        start_time = time.time()

        self.logger.info(u'Going to poll device "{}:{}" for metrics'.format(
            self._host, self.snmp_configuration.port))

        results = self.get_results()

        end_time = time.time()

        if results:
            self.logger.info(
                u'Done polling metrics for device "{}" in {:.2f} seconds, {} metrics groups'
                .format(self._host, end_time - start_time, len(results)))
        else:
            self.logger.warn(u'Error polling metrics for device {}'.format(
                self._host))

        return results
Exemple #7
0
    def test_snmp_site_community(self):
        """
        Test that the site specific community string is used if present
        """
        global secret_store

        secret_store.get_by_site.return_value = 'test_site_community'

        plugin_context = create_autospec(
            PanoptesPluginWithEnrichmentContext,
            instance=True,
            spec_set=True,
            data=panoptes_resource,
            config=plugin_conf,
            snmp=panoptes_context.config_object.snmp_defaults,
            secrets=secret_store,
            logger=logging.getLogger(__name__))

        snmp_configuration = PanoptesSNMPPluginConfiguration(plugin_context)
        self.assertEqual(snmp_configuration.community, 'test_site_community')
Exemple #8
0
    def test_snmp_bad_configuration_values(self):
        """
        Test that 'bad' configuration values raise expected exceptions
        """
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    connection_factory_module=0))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    connection_factory_module=''))

        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    connection_factory_class=0))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    connection_factory_class=''))

        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(community=0))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(community=''))

        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    community_string_key=0))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    community_string_key=''))

        with self.assertRaises(ValueError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(port=''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(port=-1))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(port=65536))

        with self.assertRaises(ValueError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    proxy_port=''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    proxy_port=-1))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    proxy_port=65536))

        with self.assertRaises(ValueError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(timeout=''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(timeout=0))

        with self.assertRaises(ValueError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(retries=''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(retries=0))

        with self.assertRaises(ValueError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    non_repeaters=''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    non_repeaters=-1))

        with self.assertRaises(ValueError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    max_repetitions=''))
        with self.assertRaises(AssertionError):
            PanoptesSNMPPluginConfiguration(
                self._plugin_context_with_bad_snmp_configuration(
                    max_repetitions=0))