Example #1
0
    def __init__(self,
                 metric_name,
                 metric_value,
                 metric_type,
                 metric_creation_timestamp=None):
        assert PanoptesValidators.valid_nonempty_string(
            metric_name), 'metric_name must be a non-empty str'
        assert PanoptesValidators.valid_number(
            metric_value), 'metric_value must be number'
        assert PanoptesMetricValidators.valid_panoptes_metric_type(
            metric_type
        ), u'metric_type must be an attribute of PanoptesMetricType'
        assert (metric_creation_timestamp is None) or PanoptesValidators.valid_number(metric_creation_timestamp), \
            u'metric_creation_timestamp should be None or a number'

        if not _VALID_KEY.match(metric_name):
            raise ValueError(
                u'metric name "%s" has to match pattern: (letter|"_") (letter | digit | "_")*'
                % metric_name)

        self.__data = dict()
        self.__data[u'metric_creation_timestamp'] = round(metric_creation_timestamp, METRICS_TIMESTAMP_PRECISION) if \
            metric_creation_timestamp is not None else round(time(), METRICS_TIMESTAMP_PRECISION)
        self.__data[u'metric_name'] = metric_name
        self.__data[u'metric_value'] = metric_value
        self.__metric_type_raw = metric_type
        self.__data[u'metric_type'] = METRIC_TYPE_NAMES[metric_type].lower()
Example #2
0
    def _parse_x509_configuration(self):
        """
        Parses the config spec to provide some details on the x509 communication.  This is effectively requests over
        x509 based on the value of _x509_secure_connection.

        0 - x509 communications
        1 - x509 where available (optional)
        2 - x509 mandatory (no connection made without x509)

        cert and key must be available for 1 & 2.
        """
        self._x509_secure_connection = int(self._plugin_x509_configuration.get(u'x509_secured_requests',
                                                                               self._default_x509_configuration[
                                                                                   u'x509_secured_requests']))
        assert PanoptesValidators.valid_positive_integer(self._x509_secure_connection),\
            u'x509 secure connection must be a positive integer'
        assert self._x509_secure_connection < 3, u'x509 secure connection cannot be greater than 2'

        cert_location = self._plugin_x509_configuration.get(u'x509_cert_location',
                                                            self._default_x509_configuration[u'x509_cert_location'])

        assert PanoptesValidators.valid_nonempty_string(cert_location), u'cert location must be a valid string'
        cert_filename = self._plugin_x509_configuration.get(u'x509_cert_filename',
                                                            self._default_x509_configuration[u'x509_cert_filename'])
        assert PanoptesValidators.valid_nonempty_string(cert_filename), u'cert filename must be a valid string'
        self._x509_certificate_file = u'{}/{}'.format(cert_location, cert_filename)

        key_location = self._plugin_x509_configuration.get(u'x509_key_location',
                                                           self._default_x509_configuration[u'x509_key_location'])
        assert PanoptesValidators.valid_nonempty_string(key_location), u'key location must be a valid string'
        key_filename = self._plugin_x509_configuration.get(u'x509_key_filename',
                                                           self._default_x509_configuration[u'x509_key_filename'])
        assert PanoptesValidators.valid_nonempty_string(key_filename), u'key filename must be a valid string'
        self._x509_key_file = u'{}/{}'.format(key_location, key_filename)
Example #3
0
    def __init__(self,
                 resource,
                 group_type,
                 interval,
                 creation_timestamp=None):
        assert PanoptesMetricValidators.valid_panoptes_resource(
            resource), u'resource must be an instance of PanoptesResource'
        assert PanoptesValidators.valid_nonempty_string(
            group_type), u'group_type must be a non-empty string'
        assert PanoptesValidators.valid_nonzero_integer(
            interval), u'interval must a integer greater than zero'

        self.__data = dict()
        self.__metrics_index = {
            metric_type: list()
            for metric_type in METRIC_TYPE_NAMES
        }
        self.__data[u'metrics_group_type'] = group_type
        self.__data[u'metrics_group_interval'] = interval
        self.__data[u'metrics_group_creation_timestamp'] = round(time(), METRICS_TIMESTAMP_PRECISION) \
            if creation_timestamp is None else creation_timestamp
        self.__data[
            u'metrics_group_schema_version'] = METRICS_GROUP_SCHEMA_VERSION
        self.__data[u'resource'] = resource
        self.__data[u'metrics'] = set()
        self.__data[u'dimensions'] = set()
        self._data_lock = threading.Lock()
Example #4
0
 def test_valid_numeric_snmp_oid(self):
     self.assertTrue(
         PanoptesValidators.valid_numeric_snmp_oid(
             u".1.3.6.1.4.1.2636.3.1.13.1.6.2.1.0.0"))
     self.assertFalse(
         PanoptesValidators.valid_numeric_snmp_oid(
             u"1.3.6.1.4.1.2636.3.1.13.1.6.2.1.0.0"))
Example #5
0
    def bulk_walk(self, oid, non_repeaters=0, max_repetitions=10):
        """
        Takes a single numeric oid and fetches oids under it's subtree using GETBULKs

        Args:
            oid (str): A numeric oid
            non_repeaters (int): A non-negative integer
            max_repetitions (int):  A non-negative integer

        Returns:
            list(PanoptesSNMPVariable): A list of PanoptesSNMPVariables within the subtree of the provided oid
        """
        assert PanoptesValidators.valid_numeric_snmp_oid(oid), u'oid must be numeric string with a leading period'
        assert PanoptesValidators.valid_positive_integer(non_repeaters), u'non_repeaters must a positive integer'
        assert PanoptesValidators.valid_positive_integer(max_repetitions), u'max_repetitions must a positive integer'
        assert self._easy_snmp_session is not None, u'PanoptesSNMPSession not initialized'

        base_oid = oid
        varbinds = list()
        running = 1

        while running:
            results = self.get_bulk(oid=oid, non_repeaters=non_repeaters,
                                    max_repetitions=max_repetitions, base_oid=base_oid)
            varbinds.extend(results)
            if len(results) < max_repetitions:
                break
            oid = base_oid + u'.' + results[-1].index

        return varbinds
Example #6
0
    def get_bulk(self, oid, non_repeaters=0, max_repetitions=10, base_oid=None):
        """
         Takes a single numeric oid and returns a list of PanoptesSNMPVariables which start with the oid

        Args:
            oid (str): A numeric oid
            non_repeaters (int): A non-negative integer
            max_repetitions (int):  A non-negative integer
            base_oid (str): An optional numeric oid which is used to set the oid in the returned PanoptesSNMPVariables

        Returns:
            list(): A list of PanoptesSNMPVariables starting with the provided oid
        """
        assert PanoptesValidators.valid_numeric_snmp_oid(oid), u'oid must be numeric string with a leading period'
        assert PanoptesValidators.valid_positive_integer(non_repeaters), u'non_repeaters must a positive integer'
        assert PanoptesValidators.valid_positive_integer(max_repetitions), u'max_repetitions must a positive integer'
        assert (base_oid is None) or PanoptesValidators.valid_numeric_snmp_oid(
                base_oid), u'oid must be numeric string with a leading period'
        assert self._easy_snmp_session is not None, u'PanoptesSNMPSession not initialized'
        try:
            varbinds = self._easy_snmp_session.get_bulk(oids=oid, non_repeaters=non_repeaters,
                                                        max_repetitions=max_repetitions)

            result = list()
            for varbind in varbinds:
                if varbind.oid.startswith(base_oid):
                    result.append(PanoptesSNMPVariable(queried_oid=base_oid if base_oid else oid, oid=varbind.oid,
                                                       index=varbind.oid_index, value=varbind.value,
                                                       snmp_type=varbind.snmp_type))
            return result
        except Exception as e:
            raise self._wrap_panoptes_snmp_exception(e)
Example #7
0
    def __init__(self, panoptes_context, plugin_type, plugin_type_display_name, celery_config, lock_timeout,
                 plugin_scheduler_task, plugin_subtype=None):
        assert PanoptesContextValidators.valid_panoptes_context(
                panoptes_context), u'panoptes_context must be an instance of PanoptesContext'
        assert PanoptesValidators.valid_nonempty_string(plugin_type), u'plugin_type must be a non-empty str'
        assert PanoptesValidators.valid_nonempty_string(plugin_type_display_name), \
            u'plugin_type_display_name must be a non-empty str'
        assert PanoptesCeleryValidators.valid_celery_config(
                celery_config), u'celery_config must be an instance of PanoptesCeleryConfig'
        assert PanoptesValidators.valid_nonzero_integer(lock_timeout), u'lock_timeout must be an int greater than zero'
        assert PanoptesValidators.valid_callback(plugin_scheduler_task), u'plugin_scheduler_task must be a callable'
        assert plugin_type is None or PanoptesValidators.valid_nonempty_string(plugin_type), u'plugin_type must be a ' \
                                                                                             u'None or a non-empty str'

        self._panoptes_context = panoptes_context
        self._config = self._panoptes_context.config_dict
        self._logger = self._panoptes_context.logger
        self._shutdown_plugin_scheduler = threading.Event()
        self._plugin_scheduler_celery_beat_service = None
        self._celery_config = celery_config
        self._celery = None
        self._t = None
        self._lock = None
        self._plugin_type = plugin_type
        self._plugin_subtype = plugin_subtype
        self._plugin_type_display_name = plugin_type_display_name
        self._lock_timeout = lock_timeout
        self._plugin_scheduler_task = plugin_scheduler_task
        self._tour_of_duty = PanoptesTourOfDuty(splay_percent=50)
        self._cycles_without_lock = 0
Example #8
0
    def add_metadata(self, key, value):
        """
        Add a metadata key/value pair

        Args:
            key(str): The syntax for a key is the same as for valid Python identifier: (letter|"_") (letter | digit "_")
            value(str): A pipe (|) is a special reserved character and cannot be present in a value

        Returns:
            None

        Raises:
            ValueError: If the key or value don't match the acceptable string patterns, a ValueError is raised

        Examples:
            add_metadata('os_name', 'Advanced Core OS')
            add_metadata('os_version', '2.6.1-GR1-P16')
        """
        assert PanoptesValidators.valid_nonempty_string(key), u'key must be a non-empty str or unicode'
        assert PanoptesValidators.valid_nonempty_string(value), u'value must be a non-empty str or unicode'

        if not self.__class__._metadata_key.match(key):
            raise ValueError(u'metadata key "%s" has to match pattern: (letter|"_") (letter | digit | "_")*' % key)

        if u'|' in value:
            raise ValueError(u'metadata value "%s" cannot contain |' % value)

        self.__data[u'resource_metadata'][key] = value
Example #9
0
    def __init__(self, host, port, timeout, retries, context, our_identity, their_identity):
        """
        Starts a SNMP V3 connection with the given parameters - works only with TLS

        Args:
            host (str): The host to interact with SNMP
            port (int): The port on the host
            timeout (int): Non-zero seconds to wait for connection and replies
            retries (int): The number of times to retry a failed query
            context (str): A non-empty SNMP v3 context
            our_identity (str): The fingerprint or filename of the certificate to present to the host
            their_identity (str): The fingerprint or filename of the certificate to expect from the host

        Returns:
            None: The session is initiated and stored locally
        """
        super(PanoptesSNMPV3TLSConnection, self).__init__(host, port, timeout, retries)
        assert PanoptesValidators.valid_nonempty_string(context), u'context must be a non-empty string'
        assert PanoptesValidators.valid_nonempty_string(our_identity), u'our_identity must be a non-empty string'
        assert PanoptesValidators.valid_nonempty_string(their_identity), u'their_identity must be a non-empty string'

        self._context = context
        self._our_identity = our_identity
        self._their_identity = their_identity

        try:
            self._easy_snmp_session = easysnmp.Session(hostname=self._host, remote_port=self._port,
                                                       timeout=self._timeout, retries=self._retries,
                                                       version=3, use_numeric=True,
                                                       transport=u'tlstcp',
                                                       context=self._context,
                                                       our_identity=self._our_identity,
                                                       their_identity=self._their_identity)
        except Exception as e:
            raise self._wrap_panoptes_snmp_exception(e)
Example #10
0
    def getset(self, key, value, expire=604800):
        """
        Set the value associated with the key in the key/value store and returns the previously set value

        This does an 'upsert' - inserts the key/value if it does not exist and updates the value if the key exists

        Args:
            key (str): The key whose value should be set
            value (str): The value to set
            expire (int): A positive integer that, if sets, would expire the key in the number of seconds specified

        Returns:
            None: Nothing. Passes through exceptions in case of failure

        """
        assert PanoptesValidators.valid_nonempty_string(
            key), u'key must be a non-empty str'
        assert PanoptesValidators.valid_nonempty_string(
            value), u'key value be a non-empty str'
        assert PanoptesValidators.valid_nonzero_integer(
            expire), u'expire must be an integer greater than zero'

        value = self._get_redis_shard(key).getset(self._normalized_key(key),
                                                  value.encode('utf-8'))
        self._get_redis_shard(key).expire(self._normalized_key(key), expire)

        if value is not None:
            return value.decode('utf-8')
Example #11
0
    def get_enrichment_keys(self, resource_id, namespace):
        """
        Returns enrichment keys for matching resource_id and namespace

        Args:
            resource_id (str): Panoptes resource id
            namespace (str): Enrichment namespace
        Returns:
              enrichment_keys (list): enrichment keys(label)
        """
        assert PanoptesValidators().valid_nonempty_string(resource_id), u'resource_id must be a string'
        assert PanoptesValidators().valid_nonempty_string(namespace), u'enrichment namespace must be a string'

        if resource_id == u'self':
            resource_id = self._resource_id

        try:
            return list(self._enrichment_data[resource_id][namespace].keys())
        except KeyError:
            try:
                self._preload_data(resource_id, namespace)
                return list(self._enrichment_data[resource_id][namespace].keys())
            except Exception as e:
                raise PanoptesEnrichmentCacheError(u'Failed to get data for resource {} namespace {} from enrichment '
                                                   u'cache object: {} plugin_name'
                                                   .format(resource_id, namespace, repr(e), self._plugin_name))
Example #12
0
    def get_enrichment_value(self, resource_id, namespace, enrichment_key):
        """
        Returns enrichment value for matching resource_id, namespace and
        enrichment_key

        Args:
            resource_id (str): Panoptes resource id
            namespace (str): Enrichment namespace
            enrichment_key (str): Enrichment set key
        Returns:
              enrichment_set (dict): key, value pairs of enrichment
        """
        assert PanoptesValidators().valid_nonempty_string(resource_id), u'resource_id must be a string'
        assert PanoptesValidators().valid_nonempty_string(namespace), u'enrichment namespace must be a string'
        assert PanoptesValidators().valid_nonempty_string(enrichment_key), u'enrichment_key must be a string'

        if resource_id == u'self':
            resource_id = self._resource_id

        try:
            return self._enrichment_data[resource_id][namespace][enrichment_key]
        except KeyError:
            try:
                self._preload_data(resource_id, namespace)
                return self._enrichment_data[resource_id][namespace][enrichment_key]
            except Exception as e:
                raise PanoptesEnrichmentCacheError(
                    u'Failed to get data for resource {} namespace {} enrichment_key {} '
                    u'from enrichment cache object: {} plugin_name: {}'.format(
                        resource_id, namespace, enrichment_key, repr(e), self._plugin_name))
Example #13
0
    def __init__(self, host, port, timeout, retries):
        assert PanoptesValidators.valid_nonempty_string(host), u'host must a non-empty string'
        assert PanoptesValidators.valid_port(port), u'port must be an integer between 1 and 65535'
        assert PanoptesValidators.valid_nonzero_integer(timeout), u'timeout must be a integer greater than zero'
        assert PanoptesValidators.valid_positive_integer(retries), u'retries must a non-negative integer'

        self._host = host
        self._port = port
        self._timeout = timeout
        self._retries = retries
        self._easy_snmp_session = None
Example #14
0
    def get_resources(self, site=None, plugin_name=None):
        """
        Get all resources from the Redis store

        The serialization that this expects in Redis is:

        key: 'resource:resource_site:<sitename>:resource_class:<class>:resource_subclass:<subclass>:resouce_type
        :<type>:resource_id:<id>:resource_endpoint:endpoint'
        value: 'timestamp:<unix epoch>{:meta:<key name>:<value>}*'

        Args:
            site (str, None): An optional string which filters resources by site
            plugin_name (str, None): An optional string which filters resources by the plugin they were discovered
                                     through

        Returns:
            PanoptesResourceSet: All resources fetched from the Redis store
        """
        assert PanoptesValidators.valid_none_or_string(site), u'site should be None or a str'
        assert PanoptesValidators.valid_none_or_string(plugin_name), u'plugin_name should be None or a str'

        logger = self.__panoptes_context.logger
        resources = PanoptesResourceSet()

        key_namespace = u''

        if plugin_name:
            key_namespace += u'plugin|' + plugin_name + u'|'

        if site:
            key_namespace += u'site|' + site + u'|'

        key_namespace += u'*'

        logger.info(u'Trying to get all resources under key namespace "%s"' % key_namespace)

        start = time()
        for key in self.__kv.find_keys(pattern=key_namespace):
            logger.debug(u'Attempting to get resource under key "%s"' % key)

            try:
                resource = self.get_resource(key)
                logger.debug(u'Found resource "%s"' % resource)
            except:
                logger.exception(u'Error trying to get "%s", skipping resource' % key)
                continue

            resources.add(resource)

        end = time()
        logger.info(u'Fetched %d resource(s) in %.2f seconds' % (len(resources), end - start))

        return resources
Example #15
0
 def test_valid_port(self):
     self.assertTrue(PanoptesValidators.valid_port(1))
     self.assertFalse(PanoptesValidators.valid_port(1.0))
     self.assertFalse(PanoptesValidators.valid_port(65540))
     self.assertFalse(PanoptesValidators.valid_port(0))
     self.assertFalse(PanoptesValidators.valid_port(-1))
     self.assertFalse(PanoptesValidators.valid_port(u"0"))
     self.assertFalse(PanoptesValidators.valid_port(u"1"))
     self.assertFalse(PanoptesValidators.valid_port(True))
     self.assertFalse(PanoptesValidators.valid_port(False))
Example #16
0
    def test_valid_nonempty_iterable_of_strings(self):
        class SampleIterator(object):
            def __init__(self, item):
                self.test_list = list()
                self.test_list.append(item)

            def __iter__(self):
                return iter(self.test_list)

            def __len__(self):
                return len(self.test_list)

        self.assertTrue(
            PanoptesValidators.valid_nonempty_iterable_of_strings(list(u'x')))
        self.assertTrue(
            PanoptesValidators.valid_nonempty_iterable_of_strings(set(u'x')))
        self.assertTrue(
            PanoptesValidators.valid_nonempty_iterable_of_strings(
                SampleIterator(u'x')))
        self.assertFalse(
            PanoptesValidators.valid_nonempty_iterable_of_strings(u'x'))
        self.assertFalse(
            PanoptesValidators.valid_nonempty_iterable_of_strings([]))
        self.assertFalse(
            PanoptesValidators.valid_nonempty_iterable_of_strings([u'']))
        self.assertFalse(
            PanoptesValidators.valid_nonempty_iterable_of_strings([u'x', u'']))
        self.assertFalse(
            PanoptesValidators.valid_nonempty_iterable_of_strings(
                [u'x', set(u'x')]))
Example #17
0
    def __init__(self, resource_site, resource_class, resource_subclass, resource_type,
                 resource_id, resource_endpoint, resource_creation_timestamp=None,
                 resource_plugin=None, resource_ttl=const.RESOURCE_MANAGER_RESOURCE_EXPIRE):
        assert PanoptesValidators.valid_nonempty_string(resource_site), u'resource_site must be a non-empty str'
        assert PanoptesValidators.valid_nonempty_string(resource_class), u'resource_class must be a non-empty str'
        assert PanoptesValidators.valid_nonempty_string(resource_subclass), u'resource_subclass must be a non-empty'
        assert PanoptesValidators.valid_nonempty_string(resource_type), u'resource_type must be a non-empty str'
        assert PanoptesValidators.valid_nonempty_string(resource_id), u'resource_id must be a non-empty str'
        assert PanoptesValidators.valid_nonempty_string(resource_endpoint), u'resource_endpoint must be a non-empty str'
        assert PanoptesValidators.valid_none_or_nonempty_string(
            resource_plugin), u'resource_plugin must be None or a non-empty str'
        assert PanoptesValidators.valid_nonzero_integer(resource_ttl), u'resource_ttl must be an integer greater than 0'

        self.__data = OrderedDict()
        self.__data[u'resource_site'] = str(resource_site)
        self.__data[u'resource_class'] = str(resource_class)
        self.__data[u'resource_subclass'] = str(resource_subclass)
        self.__data[u'resource_type'] = str(resource_type)
        self.__data[u'resource_id'] = str(resource_id)
        self.__data[u'resource_endpoint'] = str(resource_endpoint)
        self.__data[u'resource_metadata'] = OrderedDict()
        if not resource_creation_timestamp:
            self.__data[u'resource_creation_timestamp'] = time()
        else:
            self.__data[u'resource_creation_timestamp'] = resource_creation_timestamp
        self.__data[u'resource_plugin'] = resource_plugin
        self.__data[u'resource_metadata'][u'_resource_ttl'] = str(resource_ttl)
Example #18
0
 def __init__(self,
              panoptes_context,
              logger_name,
              config,
              key_value_store,
              secrets_store,
              data=None):
     assert PanoptesContextValidators.valid_panoptes_context(
         panoptes_context
     ), u'panoptes_context must be an instance of PanoptesContext'
     assert PanoptesValidators.valid_nonempty_string(
         logger_name), u'logger_name must be a non-empty str'
     assert config is None or isinstance(config,
                                         dict), u'config must be a dict'
     assert isinstance(
         secrets_store, PanoptesKeyValueStore
     ), u'secrets_store must be an instance of PanoptesKeyValueStore'
     assert isinstance(
         key_value_store, PanoptesKeyValueStore
     ), u'key_value_store must be an instance of PanoptesKeyValueStore'
     self._panoptes_context = panoptes_context
     self._logger = panoptes_context.logger.getChild(logger_name)
     self._config = config
     self._kv_store = key_value_store
     self._secrets_store = secrets_store
     self._data = data
     self._sites = panoptes_context.config_object.sites
     self._snmp_defaults = panoptes_context.config_object.snmp_defaults
     self._x509_defaults = panoptes_context.config_object.x509_defaults
Example #19
0
 def __init__(self, namespace, schema_validator, enrichment_ttl, execute_frequency):
     assert PanoptesValidators().valid_nonempty_string(namespace), u'enrichment namespace must be a string'
     assert isinstance(schema_validator, PanoptesEnrichmentSchemaValidator), \
         u'schema_validator must be an instance of PanoptesEnrichmentSchemaValidator'
     assert PanoptesValidators().valid_nonzero_integer(enrichment_ttl), \
         u'enrichment_ttl must be a valid nonzero integer'
     assert PanoptesValidators().valid_nonzero_integer(execute_frequency), \
         u'execute_frequency must be a valid nonzero integer'
     self.__data = dict()
     self.__data[u'metadata'] = dict()
     self.__data[u'namespace'] = namespace
     self.__data[u'data'] = set()
     self.__schema_validator = schema_validator
     self.__data[u'metadata'][u'_enrichment_group_creation_timestamp'] = time.time()
     self.__data[u'metadata'][u'_enrichment_ttl'] = enrichment_ttl
     self.__data[u'metadata'][u'_execute_frequency'] = execute_frequency
Example #20
0
 def _smart_add_dimension(self, method, dimension_name, index):
     dimension = method(index)
     if dimension is not None and PanoptesValidators.valid_nonempty_string(str(dimension)):
         self._interface_metrics_group.add_dimension(PanoptesMetricDimension(dimension_name, str(dimension)))
     else:
         self._interface_metrics_group.add_dimension(PanoptesMetricDimension(dimension_name,
                                                                             _DEFAULT_DIMENSION_VALUE))
Example #21
0
    def __init__(self, host, port, timeout, retries, community):
        """
        Starts a SNMP V2 connection with the given parameters

        Args:
            host (str): The host to interact with SNMP
            port (int): The port on the host
            timeout (int): Non-zero seconds to wait for connection and replies
            retries (int): The number of times to retry a failed query
            community (str): The community string to use

        Returns:
            None: The session is initiated and stored locally
        """

        super(PanoptesSNMPV2Connection, self).__init__(host, port, timeout, retries)
        assert PanoptesValidators.valid_nonempty_string(community), u'community_string must a non-empty string'

        self._community = community

        try:
            self._easy_snmp_session = easysnmp.Session(hostname=self._host, remote_port=self._port,
                                                       timeout=self._timeout, retries=self._retries,
                                                       version=2, use_numeric=True,
                                                       community=self._community)

        except Exception as e:
            raise self._wrap_panoptes_snmp_exception(e)
Example #22
0
 def __init__(self, key, value={None: None}):
     assert PanoptesValidators().valid_nonempty_string(key), u'enrichment key must be a string'
     assert isinstance(value, dict), u'enrichment value must be a dict'
     if value == {None: None}:
         value = {}
     self.__data = dict()
     self._key = key
     self.__data[self._key] = value
Example #23
0
    def _get_snmp_community_string(self):
        """
        This method sets the community string that should be used. It looks up the community string in the following
        order of preference

        1. A community string specified in the plugin configuration file
        2. A community string related to the site in which the resource resides from the 'secrets' store
        3. The default community string specified in the Panoptes configuration file

        Returns:
            None
        """
        logger = self._plugin_context.logger

        # Try and get community string from the plugin configuration
        self._community = self._plugin_snmp_configuration.get(u'community')

        if self._community:
            assert PanoptesValidators.valid_nonempty_string(
                self._community), u'SNMP community must be a non-empty string'
            return

        # Else lookup the site-specific community string
        site = self._plugin_context.data.resource_site

        try:
            logger.debug(
                u'Going to get fetch SNMP community string using key "{}" for site "{}"'
                .format(self.community_string_key, site))
            self._community = self._plugin_context.secrets.get_by_site(
                self.community_string_key, site)
        except Exception as e:
            raise PanoptesSNMPException(
                u'Could not fetch SNMP community string for site "{}" using key "{}": {}'
                .format(site, self.community_string_key, repr(e)))

        if self._community:
            assert PanoptesValidators.valid_nonempty_string(
                self._community), u'SNMP community must be a non-empty string'
            return

        # Else return default community string
        self._community = self._default_snmp_configuration.get(u'community')

        assert PanoptesValidators.valid_nonempty_string(
            self._community), u'SNMP community must be a non-empty string'
Example #24
0
 def test_valid_positive_integer(self):
     self.assertTrue(PanoptesValidators.valid_positive_integer(0))
     self.assertTrue(
         PanoptesValidators.valid_positive_integer(2**(2**(2**(2**2)))))
     self.assertFalse(PanoptesValidators.valid_positive_integer(False))
     self.assertFalse(PanoptesValidators.valid_positive_integer(True))
     self.assertFalse(PanoptesValidators.valid_positive_integer(u""))
     self.assertFalse(PanoptesValidators.valid_positive_integer(1.0))
     self.assertFalse(PanoptesValidators.valid_positive_integer(-5))
     self.assertTrue(PanoptesValidators.valid_positive_integer(-0))
Example #25
0
 def test_valid_nonzero_integer(self):
     self.assertFalse(PanoptesValidators.valid_nonzero_integer(-1))
     self.assertFalse(PanoptesValidators.valid_nonzero_integer(0))
     self.assertFalse(PanoptesValidators.valid_nonzero_integer(-0))
     self.assertFalse(PanoptesValidators.valid_nonzero_integer(1.0))
     self.assertFalse(PanoptesValidators.valid_nonzero_integer(u""))
     self.assertFalse(PanoptesValidators.valid_nonzero_integer(False))
     self.assertFalse(PanoptesValidators.valid_nonzero_integer(True))
     self.assertTrue(
         PanoptesValidators.valid_nonzero_integer(2**(2**(2**(2**2)))))
Example #26
0
    def add_resource(self, plugin_signature, resource):
        assert PanoptesValidators.valid_nonempty_string(plugin_signature), u'plugin_signature must be a non-empty str'
        assert PanoptesResourceValidators.valid_panoptes_resource(resource), u'resource must be a non-empty instance ' \
                                                                             u'of PanoptesResource'
        key, value = self._serialize_resource(resource)

        try:
            self.__kv.set(key, value, expire=int(resource.resource_ttl))
        except Exception as e:
            raise PanoptesResourceError(u'Error trying to add resource "%s": %s' % (resource, str(e)))
Example #27
0
    def _parse_snmp_configuration(self):
        self._connection_factory_module = self._plugin_snmp_configuration.get(u'connection_factory_module',
                                                                              self._default_snmp_configuration[
                                                                                  u'connection_factory_module'])
        assert PanoptesValidators.valid_nonempty_string(
            self._connection_factory_module), u'SNMP connection factory module must be a non-empty string'

        self._connection_factory_class = self._plugin_snmp_configuration.get(u'connection_factory_class',
                                                                             self._default_snmp_configuration[
                                                                                 u'connection_factory_class'])
        assert PanoptesValidators.valid_nonempty_string(
            self._connection_factory_class), u'SNMP connection factory class must be a non-empty string'

        self._community_string_key = self._plugin_snmp_configuration.get(u'community_string_key',
                                                                         self._default_snmp_configuration.get(
                                                                             u'community_string_key'))
        assert PanoptesValidators.valid_nonempty_string(
            self._community_string_key), u'SNMP community string key must be a non-empty string'

        self._port = int(
            self._plugin_snmp_configuration.get(u'port', self._default_snmp_configuration[u'port']))
        assert PanoptesValidators.valid_port(self._port), u'SNMP port must be a valid TCP/UDP port number'

        self._proxy_port = int(
            self._plugin_snmp_configuration.get(u'proxy_port', self._default_snmp_configuration[u'proxy_port']))
        assert PanoptesValidators.valid_port(self._proxy_port), u'SNMP proxy port must be a valid TCP/UDP port number'

        self._timeout = int(
            self._plugin_snmp_configuration.get(u'timeout', self._default_snmp_configuration[u'timeout']))
        assert PanoptesValidators.valid_nonzero_integer(
            self._timeout), u'SNMP timeout must be a positive integer'

        self._retries = int(
            self._plugin_snmp_configuration.get(u'retries', self._default_snmp_configuration[u'retries']))
        assert PanoptesValidators.valid_nonzero_integer(
            self._retries), u'SNMP retries must be a positive integer'

        self._non_repeaters = int(self._plugin_snmp_configuration.get(u'non_repeaters',
                                                                      self._default_snmp_configuration[
                                                                          u'non_repeaters']))
        assert PanoptesValidators.valid_positive_integer(
            self._non_repeaters), u'SNMP non-repeaters must be a positive integer'

        self._max_repetitions = int(
            self._plugin_snmp_configuration.get(u'max_repetitions',
                                                self._default_snmp_configuration[u'max_repetitions']))
        assert PanoptesValidators.valid_nonzero_integer(
            self._max_repetitions), u'SNMP max-repetitions must be a positive integer'
Example #28
0
    def send_messages(self, topic, key, messages, partitioning_key=None):
        """
        Send messages to the specified topic

        This method tries to ensures that the topic exists before trying to send a message to it. If auto-creation of
        topics is enabled, then this should always succeed (barring Kafka/Zookeeper failures)

        Args:
            topic (str): The topic to which the message should be sent
            key (str): The key for the message
            messages (str): The message to send
            partitioning_key (str): If provided, then it would be used to select the partition for the message instead \
            of the message key

        Returns:
            None: Nothing. Passes through exceptions in case of failure

        """
        assert PanoptesValidators.valid_nonempty_string(topic), u'topic must be a non-empty string'
        assert PanoptesValidators.valid_nonempty_string(key), u'key must be a non-empty string'
        assert PanoptesValidators.valid_nonempty_string(messages), u'messages must be a non-empty string'

        self._kafka_client.ensure_topic_exists(topic)

        if partitioning_key:
            # We do this hack so that the partitioning key can be different from the message key
            partition = self._kafka_producer._next_partition(topic, partitioning_key.encode('utf-8'))
            self._kafka_producer._send_messages(
                topic,
                partition,
                messages.encode('utf-8'),
                key=key.encode('utf-8')
            )

        else:
            # In this case, the message key is used as the partitioning key
            self._kafka_producer.send_messages(
                topic,
                key.encode('utf-8'),
                messages.encode('utf-8')
            )
Example #29
0
    def __init__(self, count=10, timeout=10, hostname='localhost'):
        assert PanoptesValidators.valid_nonzero_integer(count), 'count must be integer > 0'
        assert PanoptesValidators.valid_nonempty_string(hostname), 'hostname must be nonempty string'
        assert PanoptesValidators.valid_nonzero_integer(timeout), 'timeout must be integer > 0'

        super(PanoptesPingDirect, self).__init__()

        try:
            resp = subprocess.check_output(
                ['/bin/ping', '-c', str(count), '-w', str(timeout), hostname],
                stderr=subprocess.STDOUT,
                universal_newlines=True  # return string not bytes
            )
            self._get_ping_stats(resp)
        except subprocess.CalledProcessError as e:
            self._get_ping_stats(e.output)
            if self._response['packets_transmitted'] is not None:
                raise PanoptesPingTimeoutException("Ping timed out with response: " + str(self._response))
            raise PanoptesPingException(e.output)
        except Exception as e:
            raise PanoptesPingException(str(e))
Example #30
0
    def upsert_metadata(self, metadata_key, metadata_value):
        """
        Adds metadata key / value pairs to PanoptesEnrichmentGroup

        Args:
            metadata_key (str): metadata key
            metadata_value (str or int or float):  metadata value

        Returns:
              None
        """
        assert PanoptesValidators().valid_nonempty_string(metadata_key), u'metadata_key must be a string'

        if metadata_key.startswith(u'_'):
            raise ValueError(u'Failed to update reserved metadata')

        assert \
            PanoptesValidators().valid_nonempty_string(metadata_value) or \
            PanoptesValidators().valid_number(metadata_value), u'metadata_value must be any of string / float / integer'

        self.__data[u'metadata'][metadata_key] = metadata_value