Beispiel #1
0
    def stop_flow(self, vnf_name, vnf_interface=None, metric=None, cookie=0,):

        # check if port is specified (vnf:port)
        if vnf_interface is None and metric is not None:
            # take first interface by default
            connected_sw = self.net.DCNetwork_graph.neighbors(vnf_name)[0]
            link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]
            vnf_interface = link_dict[0]['src_port_id']

        for flow_dict in self.flow_metrics:
            if flow_dict['vnf_name'] == vnf_name and flow_dict['vnf_interface'] == vnf_interface \
                    and flow_dict['metric_key'] == metric and flow_dict['cookie'] == cookie:

                self.monitor_flow_lock.acquire()

                self.flow_metrics.remove(flow_dict)

                # set metric to NaN
                self.prom_metrics[flow_dict['metric_key']]. \
                    labels(vnf_name=vnf_name, vnf_interface=vnf_interface, flow_id=cookie). \
                    set(float('nan'))

                delete_from_gateway(
                    self.pushgateway, job='sonemu-SDNcontroller')

                self.monitor_flow_lock.release()

                logging.info('Stopped monitoring flow {3}: {2} on {0}:{1}'.format(
                    vnf_name, vnf_interface, metric, cookie))
                return 'Stopped monitoring flow {3}: {2} on {0}:{1}'.format(
                    vnf_name, vnf_interface, metric, cookie)

        return 'Error stopping monitoring flow: {0} on {1}:{2}'.format(
            metric, vnf_name, vnf_interface)
Beispiel #2
0
def test_add_metrics_keeps_last_step():
    metrics_logger_config = MetricsLogger(step=0)
    delete_from_gateway(metrics_logger_config._push_gateway,
                        metrics_logger_config.id,
                        grouping_key=metrics_logger_config._grouping_key)

    add_metrics({'cat': 1, 'dog': 2, 'catdog': 1.2}, step=0)
    add_metrics({'cat': 1, 'dragon': 23.5}, step=0)
    add_metrics({
        'cat': 7,
    }, step=0)

    # Get metrics
    r = requests.get(f'{LOCAL_PUSH_GATEWAY}/api/v1/metrics')
    # One row of metrics are aggregrated by the pushgateway

    gateway_metrics = r.json()['data']
    assert 4 == len(gateway_metrics)
    for metric in gateway_metrics:
        if "cat" in metric:
            assert '7' == metric['cat']['metrics'][0]['value']
        if "dog" in metric:
            assert '2' == metric['dog']['metrics'][0]['value']
        if "catdog" in metric:
            assert '1.2' == metric['catdog']['metrics'][0]['value']
        if "dragon" in metric:
            assert '23.5' == metric['dragon']['metrics'][0]['value']

    # Clean up
    clean_up_gateway(['cat', 'dog', 'catdog', 'dragon'], [0])
Beispiel #3
0
    def stop_metric(self, vnf_name, vnf_interface=None, metric=None):

        # check if port is specified (vnf:port)
        if vnf_interface is None and metric is not None:
            # take first interface by default
            connected_sw = self.net.DCNetwork_graph.neighbors(vnf_name)[0]
            link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]
            vnf_interface = link_dict[0]['src_port_id']

        for metric_dict in deepcopy(self.network_metrics):
            if metric_dict['vnf_name'] == vnf_name and metric_dict['vnf_interface'] == vnf_interface \
                    and metric_dict['metric_key'] == metric:

                self.monitor_lock.acquire()

                self.network_metrics.remove(metric_dict)

                # set values to NaN, prometheus api currently does not support removal of metrics
                # self.prom_metrics[metric_dict['metric_key']].labels(vnf_name, vnf_interface).set(float('nan'))
                self.prom_metrics[metric_dict['metric_key']]. \
                    labels(vnf_name=vnf_name, vnf_interface=vnf_interface, flow_id=None). \
                    set(float('nan'))

                # this removes the complete metric, all labels...
                # 1 single monitor job for all metrics of the SDN controller
                # we can only  remove from the pushgateway grouping keys(labels) which we have defined for the add_to_pushgateway
                # we can not specify labels from the metrics to be removed
                # if we need to remove the metrics seperatelty, we need to give
                # them a separate grouping key, and probably a diffferent
                # registry also
                delete_from_gateway(
                    self.pushgateway, job='sonemu-SDNcontroller')

                self.monitor_lock.release()

                logging.info('Stopped monitoring: {2} on {0}:{1}'.format(
                    vnf_name, vnf_interface, metric))
                return 'Stopped monitoring: {2} on {0}:{1}'.format(
                    vnf_name, vnf_interface, metric)

            # delete everything from this vnf
            elif metric_dict['vnf_name'] == vnf_name and vnf_interface is None and metric is None:
                self.monitor_lock.acquire()
                self.network_metrics.remove(metric_dict)
                logging.info('remove metric from monitor: vnf_name:{0} vnf_interface:{1} mon_port:{2}'.format(
                    metric_dict['vnf_name'], metric_dict['vnf_interface'], metric_dict['mon_port']))

                delete_from_gateway(
                    self.pushgateway, job='sonemu-SDNcontroller')
                self.monitor_lock.release()
                continue

        if vnf_interface is None and metric is None:
            logging.info('Stopped monitoring vnf: {0}'.format(vnf_name))
            return 'Stopped monitoring: {0}'.format(vnf_name)
        else:
            return 'Error stopping monitoring metric: {0} on {1}:{2}'.format(
                metric, vnf_name, vnf_interface)
Beispiel #4
0
def clean_up_gateway(metric_names=None, steps=None):
    if metric_names is None:
        return
    if steps is None:
        steps = ["None"]
    for step in steps:
        for metric_name in metric_names:
            grouping_key = (_get_default_grouping_key())
            grouping_key.update({
                "name": metric_name,
                "step": step,
            })
            delete_from_gateway(get_metric_pushgateway(),
                                get_workload_id(),
                                grouping_key=grouping_key)
Beispiel #5
0
    def stop_flow(self, vnf_name, vnf_interface=None, metric=None, cookie=0):
        for flow_dict in self.flow_metrics:
            if flow_dict['vnf_name'] == vnf_name and flow_dict['vnf_interface'] == vnf_interface \
                    and flow_dict['metric_key'] == metric and flow_dict['cookie'] == cookie:

                self.monitor_flow_lock.acquire()

                self.flow_metrics.remove(flow_dict)

                for collector in self.registry._collectors:
                    if (vnf_name, vnf_interface, cookie) in collector._metrics:
                        #logging.info('2 name:{0} labels:{1} metrics:{2}'.format(collector._name, collector._labelnames,
                        #                                                        collector._metrics))
                        collector.remove(vnf_name, vnf_interface, cookie)

                delete_from_gateway(self.pushgateway, job='sonemu-SDNcontroller')

                self.monitor_flow_lock.release()

                logging.info('Stopped monitoring flow {3}: {2} on {0}:{1}'.format(vnf_name, vnf_interface, metric, cookie))
                return 'Stopped monitoring flow {3}: {2} on {0}:{1}'.format(vnf_name, vnf_interface, metric, cookie)
Beispiel #6
0
 def test_pushadd_with_groupingkey(self):
     delete_from_gateway(self.address, "my_job", {'a': 9})
     self.assertEqual(self.requests[0][0].command, 'DELETE')
     self.assertEqual(self.requests[0][0].path, '/job/my_job/a/9')
     self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_LATEST)
     self.assertEqual(self.requests[0][1], b'')
Beispiel #7
0
 def test_delete(self):
     delete_from_gateway(self.address, "my_job")
     self.assertEqual(self.requests[0][0].command, 'DELETE')
     self.assertEqual(self.requests[0][0].path, '/job/my_job')
     self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_LATEST)
     self.assertEqual(self.requests[0][1], b'')
Beispiel #8
0
    def stop_metric(self, vnf_name, vnf_interface=None, metric=None):

        # check if port is specified (vnf:port)
        if vnf_interface is None and metric is not None:
            # take first interface by default
            connected_sw = self.net.DCNetwork_graph.neighbors(vnf_name)[0]
            link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]
            vnf_interface = link_dict[0]['src_port_id']

        for metric_dict in self.network_metrics:
            if metric_dict['vnf_name'] == vnf_name and metric_dict['vnf_interface'] == vnf_interface \
                    and metric_dict['metric_key'] == metric:

                self.monitor_lock.acquire()

                self.network_metrics.remove(metric_dict)

                #this removes the complete metric, all labels...
                #REGISTRY.unregister(self.prom_metrics[metric_dict['metric_key']])
                #self.registry.unregister(self.prom_metrics[metric_dict['metric_key']])

                for collector in self.registry._collectors :

                    """
                    INFO:root:name:sonemu_rx_count_packets
                    labels:('vnf_name', 'vnf_interface')
                    metrics:{(u'tsrc', u'output'): < prometheus_client.core.Gauge
                    object
                    at
                    0x7f353447fd10 >}
                    """
                    logging.info('{0}'.format(collector._metrics.values()))

                    if (vnf_name, vnf_interface, 'None') in collector._metrics:
                        logging.info('2 name:{0} labels:{1} metrics:{2}'.format(collector._name, collector._labelnames,
                                                                              collector._metrics))
                        collector.remove(vnf_name, vnf_interface, 'None')

                # set values to NaN, prometheus api currently does not support removal of metrics
                #self.prom_metrics[metric_dict['metric_key']].labels(vnf_name, vnf_interface).set(float('nan'))

                # this removes the complete metric, all labels...
                # 1 single monitor job for all metrics of the SDN controller
                # we can only  remove from the pushgateway grouping keys(labels) which we have defined for the add_to_pushgateway
                # we can not specify labels from the metrics to be removed
                # if we need to remove the metrics seperatelty, we need to give them a separate grouping key, and probably a diffferent registry also
                delete_from_gateway(self.pushgateway, job='sonemu-SDNcontroller')

                self.monitor_lock.release()

                logging.info('Stopped monitoring: {2} on {0}:{1}'.format(vnf_name, vnf_interface, metric))
                return 'Stopped monitoring: {2} on {0}:{1}'.format(vnf_name, vnf_interface, metric)

            # delete everything from this vnf
            elif metric_dict['vnf_name'] == vnf_name and vnf_interface is None and metric is None:
                self.monitor_lock.acquire()
                self.network_metrics.remove(metric_dict)
                for collector in self.registry._collectors:
                    collector_dict = collector._metrics.copy()
                    for name, interface, id in collector_dict:
                        if name == vnf_name:
                            logging.info('3 name:{0} labels:{1} metrics:{2}'.format(collector._name, collector._labelnames,
                                                                           collector._metrics))
                            collector.remove(name, interface, 'None')

                delete_from_gateway(self.pushgateway, job='sonemu-SDNcontroller')
                self.monitor_lock.release()
                logging.info('Stopped monitoring vnf: {0}'.format(vnf_name))
                return 'Stopped monitoring: {0}'.format(vnf_name)

        return 'Error stopping monitoring metric: {0} on {1}:{2}'.format(metric, vnf_name, vnf_interface)
Beispiel #9
0
 def test_delete_with_groupingkey(self):
     delete_from_gateway(self.address, "my_job", {"a": 9})
     self.assertEqual(self.requests[0][0].command, "DELETE")
     self.assertEqual(self.requests[0][0].path, "/metrics/job/my_job/a/9")
     self.assertEqual(self.requests[0][0].headers.get("content-type"), CONTENT_TYPE_LATEST)
     self.assertEqual(self.requests[0][1], b"")