def publish_metrics(self, data, event_name, onu_device_id):
        """

        :param data:  actual test result dict
        :param event_name: Test_result
        :param onu_device_id:  Onu device id
        :return: None
        """
        metric_data = MetricInformation(metadata=MetricMetaData(
            title=OmciTestRequest.OPTICAL_GROUP_NAME,
            ts=arrow.utcnow().float_timestamp,
            logical_device_id=self.logical_device_id,
            serial_no=self.serial_number,
            device_id=onu_device_id,
            uuid=self.uuid,
            context={'events': event_name}),
                                        metrics=data)
        self.log.info('Publish-Test-Result')
        raised_ts = Timestamp()
        raised_ts.GetCurrentTime()
        event_header = self.get_event_header(EventType.KPI_EVENT2,
                                             EventCategory.EQUIPMENT,
                                             EventSubCategory.ONU, "KPI_EVENT",
                                             raised_ts)
        kpi_event = KpiEvent2(type=KpiEventType.slice,
                              ts=arrow.utcnow().float_timestamp,
                              slice_data=[metric_data])
        event = Event(header=event_header, kpi_event2=kpi_event)
        self.core_proxy.submit_event(event)
Esempio n. 2
0
    def collect_optical_metrics(self):
        """
        Collect the metrics for optical information from all ANI/PONs

        :return: (list) collected metrics (MetricInformation)
        """
        now = self._omci_onu_device.timestamp

        group_name = OnuOmciPmMetrics.OPTICAL_GROUP_NAME
        if now is None or not self.pm_group_metrics[group_name].enabled:
            return []

        # Because several different metrics are collected by this class,
        # and the collection interval may be more often than the group interval,
        # check and make sure these metrics are actually due at this time.
        if self.last_collect_optical_metrics:
            elapsed_tenths = now - self.last_collect_optical_metrics
            if elapsed_tenths < self.pm_group_metrics[group_name].group_freq:
                self.log.info("collect-optical-metrics-not-time-yet",
                              elapsed_tenths=elapsed_tenths)
                return []

        # Scan all ANI-G ports
        ani_g_entities = self._omci_onu_device.configuration.ani_g_entities
        ani_g_entities_ids = list(
            ani_g_entities.keys()) if ani_g_entities is not None else None
        metrics_info = []

        if ani_g_entities_ids is not None and len(ani_g_entities_ids):
            from pyvoltha.adapters.extensions.omci.omci_entities import AniG
            ani_g_items = ['optical_signal_level', 'transmit_optical_level']

            for entity_id in ani_g_entities_ids:
                metrics = dict()
                data = self._omci_onu_device.query_mib(class_id=AniG.class_id,
                                                       instance_id=entity_id,
                                                       attributes=ani_g_items)
                if len(data):
                    if 'optical_signal_level' in data:
                        metrics['receive_power'] = data['optical_signal_level']

                    if 'transmit_optical_level' in data:
                        metrics['transmit_power'] = data[
                            'transmit_optical_level']

                if len(metrics):
                    metric_data = MetricInformation(metadata=MetricMetaData(
                        title=group_name,
                        ts=now,
                        logical_device_id=self.logical_device_id,
                        serial_no=self.serial_number,
                        device_id=self.device_id,
                        context={'intf_id': str(entity_id)}),
                                                    metrics=metrics)
                    metrics_info.append(metric_data)

        self.last_collect_optical_metrics = now

        return metrics_info
    def publish_metrics(self, interval_data):
        """
        Collect the metrics for this ONU PM Interval

        :param interval_data: (dict) PM interval dictionary with structure of
                    {
                        'class_id': self._class_id,
                        'entity_id': self._entity_id,
                        'me_name': self._entity.__name__,   # Mostly for debugging...
                        'interval_utc_time': None,
                        # Counters added here as they are retrieved
                    }

        :return: (dict) Key/Value of metric data
        """
        self.log.debug('publish-metrics')

        try:
            # Locate config
            now = arrow.utcnow()
            class_id = interval_data['class_id']
            config = self._configs.get(class_id)
            group = self.pm_group_metrics.get(OnuPmIntervalMetrics.ME_ID_INFO.get(class_id, ''))

            if config is not None and group is not None and group.enabled:
                # Extract only the metrics we need to publish
                metrics = dict()
                context = {
                    'interval_start_time': str(now.replace(minute=int(now.minute / 15) * 15,
                                                           second=0,
                                                           microsecond=0).timestamp)
                }
                for metric, config_item in config.items():
                    if config_item.type == PmConfig.CONTEXT and metric in interval_data:
                        context[metric] = str(interval_data[metric])

                    elif (config_item.type in (PmConfig.COUNTER, PmConfig.GAUGE, PmConfig.STATE) and
                          metric in interval_data and
                          config_item.enabled):
                        metrics[metric] = interval_data[metric]

                if len(metrics):
                    metadata = MetricMetaData(title=group.group_name,
                                              ts=now.float_timestamp,
                                              logical_device_id=self.logical_device_id,
                                              serial_no=self.serial_number,
                                              device_id=self.device_id,
                                              context=context)
                    slice_data = [MetricInformation(metadata=metadata, metrics=metrics)]

                    kpi_event = KpiEvent2(type=KpiEventType.slice,
                                          ts=now.float_timestamp,
                                          slice_data=slice_data)
                    yield self.core_proxy.submit_kpis(kpi_event)

        except Exception as e:
            self.log.exception('failed-to-submit-kpis', e=e)
    def collect_group_metrics(self, group_name, group, names, config):
        """
        Collect the metrics for a specific PM group.

        This common collection method expects that the 'group object' provide as the second
        parameter supports an attribute or property with the name of the value to
        retrieve.

        :param group_name: (str) The unique collection name. The name should not contain spaces.
        :param group: (object) The object to query for the value of various attributes (PM names)
        :param names: (set) A collection of PM names that, if implemented as a property in the object,
                            will return a value to store in the returned PM dictionary
        :param config: (PMConfig) PM Configuration settings. The enabled flag is examined to determine
                                  if the data associated with a PM Name will be collected.

        :return: (MetricInformation) collected metrics
        """
        assert ' ' not in group_name, 'Spaces are not allowed in metric titles, use an underscore'

        if group is None:
            return None

        metrics = dict()
        context = dict()

        now = getattr(group, AdapterPmMetrics.TIMESTAMP_ATTRIBUTE) \
            if hasattr(group, AdapterPmMetrics.TIMESTAMP_ATTRIBUTE) \
            else arrow.utcnow().float_timestamp

        if now is None:
            return None  # No metrics available at this time for collection

        for (metric, t) in names:
            if config[metric].type == PmConfig.CONTEXT and hasattr(
                    group, metric):
                context[metric] = str(getattr(group, metric))

            elif config[metric].type in (PmConfig.COUNTER, PmConfig.GAUGE,
                                         PmConfig.STATE):
                if config[metric].enabled and hasattr(group, metric):
                    metrics[metric] = getattr(group, metric)

        # Check length of metric data. Will be zero if if/when individual group
        # metrics can be disabled and all are (or or not supported by the
        # underlying adapter)
        if len(metrics) == 0:
            return None

        return MetricInformation(metadata=MetricMetaData(
            title=group_name,
            ts=now,
            logical_device_id=self.logical_device_id,
            serial_no=self.serial_number,
            device_id=self.device_id,
            context=context),
                                 metrics=metrics)
Esempio n. 5
0
    def collect_optical_metrics(self):
        """
        Collect the metrics for optical information from all ANI/PONs

        :return: (list) collected metrics (MetricInformation)
        """
        now = self._omci_onu_device.timestamp

        group_name = OnuOmciPmMetrics.OPTICAL_GROUP_NAME
        if now is None or not self.pm_group_metrics[group_name].enabled:
            return []

        # Scan all ANI-G ports
        ani_g_entities = self._omci_onu_device.configuration.ani_g_entities
        ani_g_entities_ids = ani_g_entities.keys() if ani_g_entities is not None else None
        metrics_info = []

        if ani_g_entities_ids is not None and len(ani_g_entities_ids):
            from pyvoltha.adapters.extensions.omci.omci_entities import AniG
            ani_g_items = ['optical_signal_level', 'transmit_optical_level']

            for entity_id in ani_g_entities_ids:
                metrics = dict()
                data = self._omci_onu_device.query_mib(class_id=AniG.class_id,
                                                       instance_id=entity_id,
                                                       attributes=ani_g_items)
                if len(data):
                    if 'optical_signal_level' in data:
                        metrics['receive_power'] = data['optical_signal_level']

                    if 'transmit_optical_level' in data:
                        metrics['transmit_power'] = data['transmit_optical_level']

                if len(metrics):
                    metric_data = MetricInformation(metadata=MetricMetaData(title=group_name,
                                                                            ts=now,
                                                                            logical_device_id=self.logical_device_id,
                                                                            serial_no=self.serial_number,
                                                                            device_id=self.device_id,
                                                                            context={
                                                                                'intf_id': str(entity_id)
                                                                            }),
                                                    metrics=metrics)
                    metrics_info.append(metric_data)

        return metrics_info
Esempio n. 6
0
    def collect_uni_status_metrics(self):
        """
        Collect the metrics for optical information from all ANI/PONs

        :return: (list) collected metrics (MetricInformation)
        """
        now = self._omci_onu_device.timestamp

        group_name = OnuOmciPmMetrics.UNI_STATUS_GROUP_NAME
        if now is None or not self.pm_group_metrics[group_name].enabled:
            return []

        # Scan all UNI-G and PPTP ports
        uni_g_entities = self._omci_onu_device.configuration.uni_g_entities
        uni_g_entities_ids = list(uni_g_entities.keys()) if uni_g_entities is not None else None
        pptp_entities = self._omci_onu_device.configuration.pptp_entities
        pptp_entities_ids = list(pptp_entities.keys()) if pptp_entities is not None else None

        metrics_info = []

        if uni_g_entities_ids and pptp_entities_ids and len(uni_g_entities_ids) and \
                len(uni_g_entities_ids) <= len(pptp_entities_ids):

            uni_g_items = ['administrative_state']
            pptp_items = ['administrative_state', 'operational_state', 'sensed_type']

            for entity_id in pptp_entities_ids:
                metrics = dict()
                data = self._omci_onu_device.query_mib(class_id=UniG.class_id,
                                                       instance_id=entity_id,
                                                       attributes=uni_g_items)
                if len(data):
                    if 'administrative_state' in data:
                        metrics['uni_admin_state'] = data['administrative_state']

                data = self._omci_onu_device.query_mib(class_id=PptpEthernetUni.class_id,
                                                       instance_id=entity_id,
                                                       attributes=pptp_items)
                if len(data):
                    if 'administrative_state' in data:
                        metrics['pptp_admin_state'] = data['administrative_state']

                    if 'operational_state' in data:
                        metrics['oper_status'] = data['operational_state']

                    if 'sensed_type' in data:
                        metrics['ethernet_type'] = data['sensed_type']

                if len(metrics):
                    metric_data = MetricInformation(metadata=MetricMetaData(title=group_name,
                                                                            ts=now,
                                                                            logical_device_id=self.logical_device_id,
                                                                            serial_no=self.serial_number,
                                                                            device_id=self.device_id,
                                                                            context={
                                                                                'intf_id': str(entity_id & 0xFF)
                                                                            }),
                                                    metrics=metrics)
                    metrics_info.append(metric_data)

        return metrics_info