X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fvim-emu.git;a=blobdiff_plain;f=src%2Femuvim%2Fdcemulator%2Fmonitoring.py;h=21985cba695edd456d9847c16626ae49517bb220;hp=d0e45daabc0fe426cccf25f510eadc76d1b3e921;hb=fe68c220a6d415a7a8980a290ecb68b17c00b02f;hpb=a6ce6f3c00d8d4ae7d378da7ddc6feccf9a38477 diff --git a/src/emuvim/dcemulator/monitoring.py b/src/emuvim/dcemulator/monitoring.py index d0e45da..21985cb 100755 --- a/src/emuvim/dcemulator/monitoring.py +++ b/src/emuvim/dcemulator/monitoring.py @@ -38,8 +38,9 @@ from subprocess import Popen import os import docker import json +from copy import deepcopy -logging.basicConfig(level=logging.INFO) +logging.basicConfig() """ class to read openflow stats from the Ryu controller of the DCNetwork @@ -202,7 +203,7 @@ class DCNetworkMonitor(): network_metric = {} # check if port is specified (vnf:port) - if vnf_interface is None: + if vnf_interface is None or vnf_interface == '': # 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] @@ -272,7 +273,7 @@ class DCNetworkMonitor(): 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: + 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: @@ -302,20 +303,17 @@ class DCNetworkMonitor(): 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') + 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() - logging.info('Stopped monitoring vnf: {0}'.format(vnf_name)) - return 'Stopped monitoring: {0}'.format(vnf_name) + continue - return 'Error stopping monitoring metric: {0} on {1}:{2}'.format(metric, vnf_name, vnf_interface) + 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) # get all metrics defined in the list and export it to Prometheus @@ -404,8 +402,10 @@ class DCNetworkMonitor(): previous_measurement = metric_dict['previous_measurement'] previous_monitor_time = metric_dict['previous_monitor_time'] mon_port = metric_dict['mon_port'] - for port_stat in port_stat_dict[str(switch_dpid)]: + # ovs output also gives back 'LOCAL' port + if port_stat['port_no'] == 'LOCAL': + continue if int(port_stat['port_no']) == int(mon_port): port_uptime = port_stat['duration_sec'] + port_stat['duration_nsec'] * 10 ** (-9) this_measurement = int(port_stat[metric_key]) @@ -428,7 +428,7 @@ class DCNetworkMonitor(): else: time_delta = (port_uptime - metric_dict['previous_monitor_time']) - metric_rate = (this_measurement - metric_dict['previous_measurement']) / float(time_delta) + #metric_rate = (this_measurement - metric_dict['previous_measurement']) / float(time_delta) metric_dict['previous_measurement'] = this_measurement metric_dict['previous_monitor_time'] = port_uptime @@ -436,6 +436,7 @@ class DCNetworkMonitor(): logging.exception('metric {0} not found on {1}:{2}'.format(metric_key, vnf_name, vnf_interface)) logging.exception('monport:{0}, dpid:{1}'.format(mon_port, switch_dpid)) + logging.exception('monitored network_metrics:{0}'.format(self.network_metrics)) logging.exception('port dict:{0}'.format(port_stat_dict)) return 'metric {0} not found on {1}:{2}'.format(metric_key, vnf_name, vnf_interface) @@ -502,7 +503,11 @@ class DCNetworkMonitor(): "--publish={0}:8080".format(port), "--name=cadvisor", "--label",'com.containernet=""', - "google/cadvisor:latest" + "--detach=true", + "google/cadvisor:latest", + #"--storage_duration=1m0s", + #"--allow_dynamic_housekeeping=true", + #"--housekeeping_interval=1s", ] logging.info('Start cAdvisor container {0}'.format(cmd)) return Popen(cmd) @@ -535,8 +540,15 @@ class DCNetworkMonitor(): def _stop_container(self, name): - container = self.dockercli.containers.get(name) - container.remove(force=True) + #container = self.dockercli.containers.get(name) + #container.stop() + #container.remove(force=True) + + # the only robust way to stop these containers is via Popen, it seems + time.sleep(1) + cmd = ['docker', 'rm', '-f', name] + Popen(cmd) + def update_skewmon(self, vnf_name, resource_name, action): @@ -600,9 +612,61 @@ class DCNetworkMonitor(): labels=['com.containernet'], name='skewmon' ) + # Wait a while for containers to be completely started + started = False + wait_time = 0 + while not started: + list1 = self.dockercli.containers.list(filters={'status': 'running', 'name': 'prometheus'}) + if len(list1) >= 1: + time.sleep(1) + started = True + if wait_time > 5: + return 'skewmon not started' + time.sleep(1) + wait_time += 1 + return ret + + def term(self, vnf_list=[]): + """ + Start a terminal window for the specified VNFs + (start a terminal for all VNFs if vnf_list is empty) + :param vnf_list: + :return: + """ + + + if vnf_list is None: + vnf_list = [] + if not isinstance(vnf_list, list): + vnf_list = str(vnf_list).split(',') + vnf_list = map(str.strip, vnf_list) + logging.info('vnf_list: {}'.format(vnf_list)) + + return self.start_xterm(vnf_list) + + + # start an xterm for the specfified vnfs + def start_xterm(self, vnf_names): + # start xterm for all vnfs + for vnf_name in vnf_names: + terminal_cmd = "docker exec -it mn.{0} /bin/bash".format(vnf_name) + + cmd = ['xterm', '-xrm', 'XTerm*selectToClipboard: true', '-xrm', 'XTerm.vt100.allowTitleOps: false', + '-T', vnf_name, + '-e', terminal_cmd] + Popen(cmd) + + ret = 'xterms started for {0}'.format(vnf_names) + if len(vnf_names) == 0: + ret = 'vnf list is empty, no xterms started' return ret + + + + +