def test_metrics_group_hash(self): now = round(time.time(), METRICS_TIMESTAMP_PRECISION) metrics_group = PanoptesMetricsGroup(self.__panoptes_resource, 'test', 120) metrics_group_two = PanoptesMetricsGroup(self.__panoptes_resource, 'test', 120) dimension = PanoptesMetricDimension('if_alias', 'bar') metric = PanoptesMetric('test_metric', 0, PanoptesMetricType.GAUGE, metric_creation_timestamp=now) metric_diff_timestamp = PanoptesMetric('test_metric', 0, PanoptesMetricType.GAUGE, metric_creation_timestamp=now + 0.01) metrics_group.add_dimension(dimension) metrics_group_two.add_dimension(dimension) self.assertEqual(metrics_group.__hash__(), metrics_group_two.__hash__()) metrics_group.add_metric(metric) metrics_group_two.add_metric(metric_diff_timestamp) self.assertEqual(metrics_group.__hash__(), metrics_group_two.__hash__()) metrics_group_set = PanoptesMetricsGroupSet() metrics_group_set.add(metrics_group) metrics_group_set.add(metrics_group_two) assert len(metrics_group_set) == 1
def __init__(self): super(PluginPollingDeviceInterfaceMetrics, self).__init__() self._device_interface_metrics = PanoptesMetricsGroupSet() self._polling_status = None self._interface_metrics_group = None self._dot3stats_map = None self._if_table_stats_map = None self._ifx_table_stats_map = None self._DIMENSION_MAP = { u'alias': self.get_alias, u'media_type': self.get_media_type, u'description': self.get_description, u'configured_speed': self.get_configured_speed, u'port_speed': self.get_port_speed, u'interface_name': self.get_interface_name, u'parent_interface_name': self.get_parent_interface_name, u'parent_interface_media_type': self.get_parent_interface_media_type, u'parent_interface_configured_speed': self.get_parent_interface_configured_speed, u'parent_interface_port_speed': self.get_parent_interface_port_speed }
def run(self, context): metric_group_set = PanoptesMetricsGroupSet() metric1 = PanoptesMetric("test", 0.0, PanoptesMetricType.GAUGE) metric_group = PanoptesMetricsGroup(self.panoptes_resource, "Test", _TEST_INTERVAL) metric_group.add_metric(metric1) metric_group_set.add(metric_group) return metric_group_set
def __init__(self, plugin_context, device_resource, execute_frequency): self._plugin_context = plugin_context self._logger = plugin_context.logger self._enrichment = plugin_context.enrichment self._device_resource = device_resource self._device_fqdn = device_resource.resource_endpoint self._execute_frequency = execute_frequency self._device_heartbeat_metrics = PanoptesMetricsGroupSet()
def run(self, context): logger = context.logger resource = context.data host = resource.resource_endpoint config = context.config[u'main'] execute_frequency = int(config[u'execute_frequency']) start_time = time() try: count = int(config[u'count']) except KeyError: count = DEFAULT_PING_COUNT logger.info(u'For device {}, count not set - setting it to {}'.format(host, DEFAULT_PING_COUNT)) except ValueError: raise PanoptesPollingPluginConfigurationError( u'For device {}, configured count is not an integer: {}'.format(host, config[u'count'])) try: timeout = int(config[u'timeout']) except KeyError: timeout = DEFAULT_PING_TIMEOUT logger.info(u'For device {}, timeout not set - setting it to {}s'.format(host, DEFAULT_PING_TIMEOUT)) except ValueError: raise PanoptesPollingPluginConfigurationError( u'For device {}, configured timeout is not an integer: {}'.format(host, config[u'timeout'])) ping_metrics_group = PanoptesMetricsGroup(resource, u'ping', execute_frequency) try: panoptes_ping = PanoptesPing(hostname=host, count=count, timeout=timeout) for metric, object_property in list(PING_METRICS.items()): ping_metrics_group.add_metric(PanoptesMetric(metric, getattr(panoptes_ping, object_property), PanoptesMetricType.GAUGE)) if panoptes_ping.packet_loss_pct == 100.0: ping_status = DEVICE_METRICS_STATES.PING_FAILURE else: ping_status = DEVICE_METRICS_STATES.SUCCESS except Exception as e: logger.warn(u'For device {}, ping failed: {}'.format(host, repr(e))) ping_status = DEVICE_METRICS_STATES.PING_FAILURE ping_metrics_group.add_metric(PanoptesMetric(u'ping_status', ping_status, PanoptesMetricType.GAUGE)) logger.debug(u'For device {}, ping results are: {}'.format(host, str(ping_metrics_group.json))) ping_metrics_group_set = PanoptesMetricsGroupSet() ping_metrics_group_set.add(ping_metrics_group) end_time = time() logger.info(u'Done pinging device "{}" in {} seconds, {} metric groups'.format(host, round(end_time - start_time, 2), len(ping_metrics_group_set))) return ping_metrics_group_set
def __init__(self): self._plugin_context: PanoptesPluginContext = None self._config: Dict[str, Any] = {} self._panoptes_metrics_group_set: PanoptesMetricsGroupSet = PanoptesMetricsGroupSet( ) self._device: PanoptesResource = None self._execute_frequency: int = 60 self._logger = None self.napalm_device_connection = None super(NapalmPollingPlugin, self).__init__()
def run(self, context): self._plugin_context = context self._logger = context.logger self._device = context.data self._device_host = self._device.resource_endpoint self._device_model = self._device.resource_metadata.get( u'model', u'unknown') self._execute_frequency = int( context.config[u'main'][u'execute_frequency']) self._snmp_connection = None self._asr_device_metrics = PanoptesMetricsGroupSet() try: polling_status_metric_name = context.config[u'main'][ u'polling_status_metric_name'] except: self._logger.error( u'Polling status metric name not defined for %s' % self._device_host) raise PanoptesPollingPluginConfigurationError( u'Polling status metric name not defined for %s' % self._device_host) self._polling_status = PanoptesPollingStatus( resource=self._device, execute_frequency=self._execute_frequency, logger=self._logger, metric_name=polling_status_metric_name) self._max_repetitions = _MAX_REPETITIONS # Todo self._logger.info( u'Going to poll ASR Device "%s" (model "%s") for device metrics' % (self._device_host, self._device_model)) start_time = time.time() device_results = self.get_device_metrics() end_time = time.time() if device_results: self._logger.info( u'Done polling ASR Device metrics for device "%s" in %.2f seconds, %s metrics' % (self._device_host, end_time - start_time, len(device_results))) else: self._logger.warn( u'Error polling device metrics for ASR Device "%s" (model "%s")' % (self._device_host, self._device_model)) return device_results
def prepare_panoptes_metrics_group_set(self, file_path=None): panoptes_metric_group_set = PanoptesMetricsGroupSet() path_to_metrics_file = plugin_results_file if file_path is None else file_path with open(path_to_metrics_file) as results_file: panoptes_json_data = json.load(results_file) for panoptes_data_object in panoptes_json_data: resource = panoptes_data_object[u'resource'] panoptes_resource = PanoptesResource( resource_site=resource[u'resource_site'], resource_class=resource[u'resource_class'], resource_subclass=resource[u'resource_subclass'], resource_type=resource[u'resource_type'], resource_id=resource[u'resource_id'], resource_endpoint=resource[u'resource_endpoint'], resource_plugin=resource[u'resource_plugin'], resource_creation_timestamp=0) panoptes_metric_group = PanoptesMetricsGroup( resource=panoptes_resource, group_type=panoptes_data_object[u'metrics_group_type'], interval=panoptes_data_object[u'metrics_group_interval'] ) for dimension in panoptes_data_object[u'dimensions']: panoptes_metric_group.add_dimension( PanoptesMetricDimension( name=dimension[u'dimension_name'], value=dimension[u'dimension_value'] ) ) for metric in panoptes_data_object[u'metrics']: panoptes_metric_group.add_metric( PanoptesMetric( metric_name=metric[u'metric_name'], metric_value=metric[u'metric_value'], metric_type=PanoptesMetricType().GAUGE if metric[u'metric_type'] == u'gauge' else PanoptesMetricType().COUNTER, metric_creation_timestamp=metric[u'metric_creation_timestamp'] ) ) panoptes_metric_group_set.add(panoptes_metric_group) return panoptes_metric_group_set
def __init__(self): self._plugin_context = None self._logger = None self._device = None self._device_host = None self._device_model = None self._execute_frequency = None self._snmp_connection = None self._asr_device_metrics = PanoptesMetricsGroupSet() self._polling_status = None self._max_repetitions = None self._cpu_metrics = None self._memory_metrics = None self._temp_metrics = None self._power_metrics = None self._crypto_metrics = None self._load_metrics = None super(PluginPollingASRDeviceMetrics, self).__init__()
def run(self, context): self._plugin_context = context self._logger = context.logger self._device = context.data self._device_host = self._device.resource_endpoint self._device_model = self._device.resource_metadata.get( u'model', u'unknown') self._execute_frequency = int( context.config[u'main'][u'execute_frequency']) self._snmp_connection = None self._arista_device_metrics = PanoptesMetricsGroupSet() self._polling_status = PanoptesPollingStatus( resource=self._device, execute_frequency=self._execute_frequency, logger=self._logger) self._max_repetitions = _MAX_REPETITIONS self._logger.info( u'Going to poll Arista device "%s" (model "%s") for device metrics' % (self._device_host, self._device_model)) start_time = time.time() device_results = self.get_device_metrics() end_time = time.time() if device_results: self._logger.info( u'Done polling Arista Device metrics for device "%s" in %.2f seconds, %s metrics' % (self._device_host, end_time - start_time, len(device_results))) else: self._logger.warn( u'Error polling device metrics for Arista device %s' % self._device_host) return device_results
def test_panoptes_metrics_group_set(self): """Tests basic PanoptesMetricsGroupSet operations""" metrics_group_set = PanoptesMetricsGroupSet() metrics_group = PanoptesMetricsGroup(self.__panoptes_resource, u'test', 120) metrics_group_two = PanoptesMetricsGroup(self.__panoptes_resource, u'test', 120) metrics_group_set.add(metrics_group) metrics_group_set.add(metrics_group_two) assert len(metrics_group_set) == 1 self.assertIn(metrics_group, metrics_group_set.metrics_groups) metrics_group_set.remove(metrics_group_two) assert len(metrics_group_set) == 0 metrics_group_set.add(metrics_group) metrics_group_three = PanoptesMetricsGroup(self.__panoptes_resource, u'test3', 120) metrics_group_three.add_metric( PanoptesMetric(u"test3", 0.0, PanoptesMetricType.GAUGE)) metrics_group_set.add(metrics_group_three) assert len(metrics_group_set) == 2 metrics_group_set_two = PanoptesMetricsGroupSet() metrics_group_four = PanoptesMetricsGroup(self.__panoptes_resource, u'test', 120) metrics_group_four.add_metric( PanoptesMetric(u"test4", 0.0, PanoptesMetricType.GAUGE)) metrics_group_set_two.add(metrics_group_four) assert len(metrics_group_set_two) == 1 # Test PanoptesMetricsGroupSet.__add__ metrics_group_set_union = metrics_group_set + metrics_group_set_two assert len(metrics_group_set_union) == 3 with self.assertRaises(AssertionError): metrics_group_set.remove(self.__panoptes_resource) with self.assertRaises(TypeError): metrics_group_set + metrics_group # Test PanoptesMetricsGroupSet.__iter__ & 'next' metrics_group_count = 0 metrics_group_set_union_interator = iter(metrics_group_set_union) for _ in metrics_group_set_union: self.assertIn(next(metrics_group_set_union_interator), metrics_group_set_union.metrics_groups) metrics_group_count += 1 assert len(metrics_group_set_union) == metrics_group_count with self.assertRaises(Exception): next(metrics_group_set_union_interator) # Test PanoptesMetricsGroupSet.__repr__ _METRICS_GROUP_SET_REPR = u"PanoptesMetricsGroupSet[PanoptesMetricsGroup[resource:" \ u"plugin|test|site|test|class|test|subclass|test|type|test|id|test|endpoint|test," \ u"interval:120,schema_version:0.2,group_type:test,creation_timestamp:{}," \ u"dimensions:[],metrics:[]],PanoptesMetricsGroup[resource:" \ u"plugin|test|site|test|class|test|subclass|test|type|test|id|test|endpoint|test," \ u"interval:120,schema_version:0.2,group_type:test3,creation_timestamp:{}," \ u"dimensions:[],metrics:[" \ u"PanoptesMetric[test3|0.0|GAUGE|{}]]]]".format(mock_time.return_value, mock_time.return_value, mock_time.return_value) self.assertEqual(repr(metrics_group_set), _METRICS_GROUP_SET_REPR)
def test_panoptes_metrics_group_set(self): """Tests basic PanoptesMetricsGroupSet operations""" metrics_group_set = PanoptesMetricsGroupSet() metrics_group = PanoptesMetricsGroup(self.__panoptes_resource, 'test', 120) metrics_group_two = PanoptesMetricsGroup(self.__panoptes_resource, 'test', 120) metrics_group_set.add(metrics_group) metrics_group_set.add(metrics_group_two) assert len(metrics_group_set) == 1 self.assertIn(metrics_group, metrics_group_set.metrics_groups) metrics_group_set.remove(metrics_group_two) assert len(metrics_group_set) == 0 metrics_group_set.add(metrics_group) metrics_group_three = PanoptesMetricsGroup(self.__panoptes_resource, 'test3', 120) metrics_group_three.add_metric( PanoptesMetric("test3", 0.0, PanoptesMetricType.GAUGE)) metrics_group_set.add(metrics_group_three) assert len(metrics_group_set) == 2 metrics_group_set_two = PanoptesMetricsGroupSet() metrics_group_four = PanoptesMetricsGroup(self.__panoptes_resource, 'test', 120) metrics_group_four.add_metric( PanoptesMetric("test4", 0.0, PanoptesMetricType.GAUGE)) metrics_group_set_two.add(metrics_group_four) assert len(metrics_group_set_two) == 1 # Test PanoptesMetricsGroupSet.__add__ metrics_group_set_union = metrics_group_set + metrics_group_set_two assert len(metrics_group_set_union) == 3 with self.assertRaises(AssertionError): metrics_group_set.remove(self.__panoptes_resource) with self.assertRaises(TypeError): metrics_group_set + metrics_group # Test PanoptesMetricsGroupSet.__iter__ & 'next' metrics_group_count = 0 metrics_group_set_union_interator = iter(metrics_group_set_union) for _ in metrics_group_set_union: self.assertIn(metrics_group_set_union_interator.next(), metrics_group_set_union.metrics_groups) metrics_group_count += 1 assert len(metrics_group_set_union) == metrics_group_count with self.assertRaises(Exception): metrics_group_set_union_interator.next() # Test PanoptesMetricsGroupSet.__repr__ _METRICS_GROUP_SET_REPR = "set([{{'metrics_group_interval': 120, " \ "'resource': plugin|test|site|test|class|test|subclass|test|type|test|id|test|" \ "endpoint|test, 'dimensions': set([]), 'metrics_group_type': 'test', " \ "'metrics': set([]), 'metrics_group_creation_timestamp': {}, " \ "'metrics_group_schema_version': '0.2'}}, {{'metrics_group_interval': 120, " \ "'resource': plugin|test|site|test|class|test|subclass|test|type|test|id|test|" \ "endpoint|test, 'dimensions': set([]), 'metrics_group_type': 'test3', " \ "'metrics': set([{{'metric_creation_timestamp': {}, " \ "'metric_type': 'gauge', " \ "'metric_name': 'test3', 'metric_value': 0.0}}]), " \ "'metrics_group_creation_timestamp': {}, " \ "'metrics_group_schema_version': '0.2'}}])".format(mock_time.return_value, mock_time.return_value, mock_time.return_value) self.assertEqual(repr(metrics_group_set), _METRICS_GROUP_SET_REPR)
def _process_transforms(context, transforms, metrics_group_set): """ { "member": [{"rate": ["real_bytes_in", "real_bytes_out", "real_packets_in", "real_packets_out", "real_total_connections"]}] } Args: context (PanoptesContext): The PanoptesContext being used by the Plugin Agent transforms (dict): The transformations to apply metrics_group_set (PanoptesMetricsGroupSet): The metrics group set on which to apply the transformations Returns: PanoptesMetricsGroupSet: The processed/transformed metrics group set """ callbacks = {u'rate': _transformation_rate} logger = context.logger lookup = dict() output_metrics_group_set = PanoptesMetricsGroupSet() logger.debug(u'Going to process transforms: %s' % transforms) for key in transforms: transform_type, transform_metrics_group_type, transform_inputs = transforms[ key].split(':') transform_inputs = _split_and_strip(transform_inputs) if transform_metrics_group_type not in lookup: lookup[transform_metrics_group_type] = list() lookup[transform_metrics_group_type].append( (transform_type, transform_inputs)) logger.debug(u'Transform lookups: %s' % lookup) for metrics_group in metrics_group_set: if metrics_group.group_type not in lookup: output_metrics_group_set.add(metrics_group) continue resource_serialization_key = metrics_group.resource.serialization_key for transform in lookup[metrics_group.group_type]: logger.debug(u'For resource %s, trying to process transform %s' % (resource_serialization_key, transform)) transform_type, transform_inputs = transform if transform_type not in callbacks: logger.warn( u'For resource %s, no implementation for transform type "%s" found, skipping' % (resource_serialization_key, transform)) continue try: output_metrics_group = callbacks[transform_type]( context, metrics_group, transform_inputs) if output_metrics_group is not None: output_metrics_group_set.add(output_metrics_group) except Exception as e: logger.error( u'For resource %s, error while trying to transform metrics group "%s": %s, skipping' % (resource_serialization_key, metrics_group.group_type, repr(e))) return output_metrics_group_set