예제 #1
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
예제 #2
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)
예제 #3
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')
예제 #4
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)
예제 #5
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
예제 #6
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'
예제 #7
0
파일: metrics.py 프로젝트: rec0dex/panoptes
    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()
예제 #8
0
파일: context.py 프로젝트: rec0dex/panoptes
 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
예제 #9
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()
예제 #10
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))
예제 #11
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)
예제 #12
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'
예제 #13
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)))
예제 #14
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
예제 #15
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')
            )
예제 #16
0
파일: runner.py 프로젝트: rec0dex/panoptes
 def __init__(self, plugin_name, plugin_type, plugin_class,
              plugin_info_class, plugin_data, panoptes_context,
              plugin_agent_kv_store_class, plugin_kv_store_class,
              plugin_secrets_store_class, plugin_logger_name,
              plugin_result_class, results_callback):
     assert PanoptesValidators.valid_nonempty_string(
         plugin_name), u'plugin_name must be a non-empty string'
     assert PanoptesValidators.valid_nonempty_string(
         plugin_type), u'plugin_type must be a non-empty string'
     assert PanoptesBasePluginValidators.valid_plugin_class(
         plugin_class
     ), u'plugin_class must be instance of PanoptesBasePlugin'
     assert PanoptesPluginInfoValidators.valid_plugin_info_class(
         plugin_info_class
     ), u'plugin_info_class must be instance of PanoptesPluginInfo'
     assert PanoptesValidators.valid_hashable_object(
         plugin_data), u'plugin_data must be a valid hashable object'
     assert PanoptesKeyValueStoreValidators.valid_kv_store_class(
         plugin_kv_store_class
     ), u'plugin_kv_store_class must be a subclass of PanoptesKeyValueStore'
     assert PanoptesKeyValueStoreValidators.valid_kv_store_class(
         plugin_secrets_store_class
     ), u'plugin_secrets_store_class must be a subclass of PanoptesKeyValueStore'
     assert PanoptesValidators.valid_nonempty_string(plugin_logger_name), u'plugin_logger_name must be a non-empty '\
                                                                          u'string'
     assert PanoptesValidators.valid_callback(
         results_callback), u'plugin_callback must be a callable'
     self._plugin_name = plugin_name
     self._plugin_type = plugin_type
     self._plugin_class = plugin_class
     self._plugin_info_class = plugin_info_class
     self._plugin_data = plugin_data
     self._panoptes_context = panoptes_context
     self._plugin_agent_kv_store_class = plugin_agent_kv_store_class
     self._plugin_secrets_store_class = plugin_secrets_store_class
     self._plugin_kv_store_class = plugin_kv_store_class
     self._plugin_logger_name = plugin_logger_name
     self._logger = self._panoptes_context.logger
     self._plugin_result_class = plugin_result_class
     self._results_callback = weakref.proxy(results_callback)
예제 #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)
예제 #18
0
    def set_add(self, set_name, member):
        """
        Add a member to the set associated in the the key/value store

        This does an 'upsert' - inserts the set (and member) if it does not exist and updates the member if the set \
        exists

        Args:
            set_name (str): The key whose value should be set
            member (str): The value of the member to add to the set

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

        """
        assert PanoptesValidators.valid_nonempty_string(
            set_name), u'set_name must be a non-empty str'
        assert PanoptesValidators.valid_nonempty_string(
            member), u'member must be a non-empty str'

        return self._get_redis_shard(set_name).sadd(
            self._normalized_key(set_name), member.encode('utf-8'))
예제 #19
0
    def get_by_site(self, secret_name, site, fallback_to_default=True):
        assert PanoptesValidators.valid_nonempty_string(
            secret_name), u'secret_name must be a non-empty str or unicode'
        assert PanoptesValidators.valid_nonempty_string(
            site), u'site must be a non-empty str or unicode'
        assert type(fallback_to_default
                    ) == bool, u'fallback_to_default must be a boolean'

        secret_key = secret_name + u':' + site
        try:
            secret = super(PanoptesSecretsStore, self).get(key=secret_key)
            return secret
        except Exception as e:
            if not fallback_to_default:
                raise e

        # If we didn't find a site based key AND fallback_to_default is set to true
        secret_key = secret_name + u':default'
        try:
            secret = super(PanoptesSecretsStore, self).get(key=secret_key)
            return secret
        except Exception as e:
            raise e
예제 #20
0
    def config_filename(self, filename):
        """
        Sets name of configuration file associated with the plugin - note that setting this does not change/reload the
        configuration

        Args:
            filename (str): The name of configuration file associated with the plugin

        Returns:
            None
        """
        assert PanoptesValidators.valid_nonempty_string(
            filename), u'filename must be a non empty string'
        self._config_filename = filename
예제 #21
0
    def ttl(self, key):
        """
        Return the Time to Live (TTL) in seconds, of the given key.

        Args:
            key (str): The key whose ttl should be obtained

        Returns:
            ttl (int): TTL of the key, in seconds
        """
        assert PanoptesValidators.valid_nonempty_string(
            key), u'key must be a non-empty str'

        return self._get_redis_shard(key).ttl(self._normalized_key(key))
예제 #22
0
    def __init__(self, context, path, timeout, retries=1, identifier=None):
        """
        Creates and maintains state for a lock

        Args:
            path (str): A '/' separated path for the lock
            timeout (int): in seconds. Must be a positive integer
            retries (int): how many times to try before giving up. Zero implies try forever
            identifier (str): Name to use for this lock contender. This can be useful for querying \
            to see who the current lock contenders are

        Returns:
            PanoptesLock: lock
        """
        assert PanoptesContextValidators.valid_panoptes_context(
            context), u'context must be a valid PanoptesContext'
        assert PanoptesValidators.valid_nonempty_string(path) and re.search(r"^/\S+", path), \
            u'path must be a non-empty string that begins with /'
        assert PanoptesValidators.valid_nonzero_integer(
            timeout), u'timeout must be a positive integer'
        assert PanoptesValidators.valid_positive_integer(
            retries), u'retries must be a non-negative integer'
        assert PanoptesValidators.valid_nonempty_string(
            identifier), u'identifier must be a non-empty string'

        self._context = context
        self._logger = self._context.logger
        self._path = path
        self._timeout = timeout
        self._retries = retries
        self._identifier = identifier
        self._lock = None
        self._locked = False
        self._calling_module = get_calling_module_name(3)

        self._get_lock()
예제 #23
0
    def get_resource(self, resource_key):
        assert PanoptesValidators.valid_nonempty_string(resource_key), u'resource_key must be a non-empty str or' \
                                                                       u' unicode'

        logger = self.__panoptes_context.logger

        try:
            value = self.__kv.get(resource_key)
        except Exception as e:
            logger.exception(u'Error trying to get value from key-value store for key "%s"' % resource_key)
            raise PanoptesResourceError(u'Error trying to fetch value for key "%s": %s ' % (resource_key, repr(e)))

        if not value:
            logger.exception(u'Error -- No resource found for key "%s"' % resource_key)
            raise PanoptesResourceError(u'No resource found for key "%s"' % resource_key)

        return self._deserialize_resource(resource_key, value)
예제 #24
0
    def __init__(self, query, panoptes_context):
        assert PanoptesValidators.valid_nonempty_string(query), u'query must be an instance of str'
        assert panoptes_context and isinstance(panoptes_context,
                                               PanoptesContext), u'panoptes_context must be an instance of ' \
                                                                 u'PanoptesContext'
        self._query = query
        logger = panoptes_context.logger

        logger.info(u'Parsing query expression: %s' % query)
        try:
            tokens = self._parse_query()
        except ParseException as e:
            logger.error(u'Error in parsing expression "%s": %s' % (query, str(e)))
            raise e
        else:
            logger.debug(u'Tokens = %s' % tokens)
            self._tokens = tokens
예제 #25
0
    def get(self, key):
        """
        Get the value associated with the key from the key/value store

        Args:
            key (str): The key whose value should be returned

        Returns:
            str: The value associated with the key. None if the key is not found in the key/value store. Passes \
            through exceptions in case of failure
        """
        assert PanoptesValidators.valid_nonempty_string(
            key), u'key must be a non-empty str'

        key = self._normalized_key(key)
        value = self._get_redis_shard(key).get(key)
        if value is not None:
            return value.decode('utf-8')
예제 #26
0
    def set_members(self, set_name):
        """
        Get the members associated with the set from the key/value store

        Args:
            set_name (str): The set whose members should be returned

        Returns:
            list: The members associated with the set. None if the set is not found in the key/value store. Passes \
            through exceptions in case of failure
        """
        assert PanoptesValidators.valid_nonempty_string(
            set_name), u'set_name must be a non-empty str'

        result = {
            member.decode('utf-8')
            for member in self._get_redis_shard(set_name).smembers(
                self._normalized_key(set_name))
        }

        return result
예제 #27
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))
예제 #28
0
    def __init__(self, count=10, timeout=10, hostname=u'localhost'):
        assert PanoptesValidators.valid_nonzero_integer(
            count), u'count must be integer > 0'
        assert PanoptesValidators.valid_nonempty_string(
            hostname), u'hostname must be nonempty string'
        assert PanoptesValidators.valid_nonzero_integer(
            timeout), u'timeout must be integer > 0'

        self._response = dict()  # dictionary containing ping statistics
        self._response[u'packets_transmitted'] = None
        self._response[u'packets_received'] = None
        self._response[u'packet_loss_pct'] = None
        self._response[u'execution_time'] = None
        self._response[u'round_trip_min'] = None
        self._response[u'round_trip_avg'] = None
        self._response[u'round_trip_max'] = None
        self._response[u'round_trip_stddev'] = None

        try:
            resp = subprocess.check_output(
                [
                    u'/bin/ping', u'-c',
                    str(count), u'-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[u'packets_transmitted'] is not None:
                raise PanoptesPingTimeoutException(
                    u"Ping timed out with response: " + str(self._response))
            raise PanoptesPingException(e.output)
        except Exception as e:
            raise PanoptesPingException(e.message)
예제 #29
0
 def __init__(self, app_name):
     assert PanoptesValidators.valid_nonempty_string(
         app_name), u'app_name must be a non-empty string'
     self._celery_app_name = app_name
예제 #30
0
 def test_valid_nonempty_string(self):
     self.assertFalse(PanoptesValidators.valid_nonempty_string(u""))
     self.assertTrue(
         PanoptesValidators.valid_nonempty_string(u"hello world"))
     self.assertFalse(PanoptesValidators.valid_nonempty_string(0))