X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=src%2Femuvim%2Fdcemulator%2Fmonitoring.py;h=ba04771c5107d092700c570460dd2791c204b7e4;hb=36afe6369a9b6178525057a524a27fde59398b00;hp=89d82ba52fc6e2091972f0b0944512543d1036b8;hpb=e131bf5fefb90aadfe698dd72126cd2fdfe2fb7a;p=osm%2Fvim-emu.git diff --git a/src/emuvim/dcemulator/monitoring.py b/src/emuvim/dcemulator/monitoring.py index 89d82ba..ba04771 100755 --- a/src/emuvim/dcemulator/monitoring.py +++ b/src/emuvim/dcemulator/monitoring.py @@ -27,6 +27,7 @@ partner consortium (www.sonata-nfv.eu). """ import logging +import sys from mininet.node import OVSSwitch import ast import time @@ -60,8 +61,6 @@ class DCNetworkMonitor(): self.pushgateway = 'localhost:9091' # when sdk is started with docker-compose, we could use # self.pushgateway = 'pushgateway:9091' - # Start up the server to expose the metrics to Prometheus - #start_http_server(8000) # supported Prometheus metrics self.registry = CollectorRegistry() @@ -104,10 +103,8 @@ class DCNetworkMonitor(): self.monitor_flow_thread.start() # helper tools - # Prometheus pushgateway and DB are started as external contianer, outside of son-emu - #self.pushgateway_process = self.start_PushGateway() - #self.prometheus_process = self.start_Prometheus() - self.cadvisor_process = self.start_cadvisor() + # cAdvisor, Prometheus pushgateway and DB are started as external container, outside of son-emu + # first set some parameters, before measurement can start def setup_flow(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0): @@ -167,7 +164,15 @@ class DCNetworkMonitor(): logging.exception("setup_metric error.") return ex.message - def stop_flow(self, vnf_name, vnf_interface=None, metric=None, cookie=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: @@ -187,6 +192,8 @@ class DCNetworkMonitor(): 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) + # first set some parameters, before measurement can start def setup_metric(self, vnf_name, vnf_interface=None, metric='tx_packets'): @@ -258,6 +265,13 @@ class DCNetworkMonitor(): 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: @@ -319,8 +333,13 @@ class DCNetworkMonitor(): 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) + + - # get all metrics defined in the list and export it to Prometheus + + +# get all metrics defined in the list and export it to Prometheus def get_flow_metrics(self): while self.start_monitoring: @@ -330,6 +349,7 @@ class DCNetworkMonitor(): data = {} data['cookie'] = flow_dict['cookie'] + data['cookie_mask'] = flow_dict['cookie'] if 'tx' in flow_dict['metric_key']: data['match'] = {'in_port':flow_dict['mon_port']} @@ -339,9 +359,15 @@ class DCNetworkMonitor(): # query Ryu ret = self.net.ryu_REST('stats/flow', dpid=flow_dict['switch_dpid'], data=data) - flow_stat_dict = ast.literal_eval(ret) + if isinstance(ret, dict): + flow_stat_dict = ret + elif isinstance(ret, basestring): + flow_stat_dict = ast.literal_eval(ret.rstrip()) + else: + flow_stat_dict = None logging.debug('received flow stat:{0} '.format(flow_stat_dict)) + self.set_flow_metric(flow_dict, flow_stat_dict) self.monitor_flow_lock.release() @@ -428,19 +454,23 @@ class DCNetworkMonitor(): previous_monitor_time = metric_dict['previous_monitor_time'] cookie = metric_dict['cookie'] - # TODO aggregate all found flow stats - flow_stat = flow_stat_dict[str(switch_dpid)][0] - if 'bytes' in metric_key: - counter = flow_stat['byte_count'] - elif 'packet' in metric_key: - counter = flow_stat['packet_count'] + counter = 0 + for flow_stat in flow_stat_dict[str(switch_dpid)]: + if 'bytes' in metric_key: + counter += flow_stat['byte_count'] + elif 'packet' in metric_key: + counter += flow_stat['packet_count'] + flow_stat = flow_stat_dict[str(switch_dpid)][0] flow_uptime = flow_stat['duration_sec'] + flow_stat['duration_nsec'] * 10 ** (-9) self.prom_metrics[metric_dict['metric_key']]. \ labels({'vnf_name': vnf_name, 'vnf_interface': vnf_interface, 'flow_id': cookie}). \ set(counter) - pushadd_to_gateway(self.pushgateway, job='sonemu-SDNcontroller', registry=self.registry) + try: + pushadd_to_gateway(self.pushgateway, job='sonemu-SDNcontroller', registry=self.registry) + except Exception, e: + logging.warning("Pushgateway not reachable: {0} {1}".format(Exception, e)) def start_Prometheus(self, port=9090): @@ -490,6 +520,7 @@ class DCNetworkMonitor(): self.monitor_thread.join() self.monitor_flow_thread.join() + # these containers are used for monitoring but are started now outside of son-emu ''' if self.prometheus_process is not None: logging.info('stopping prometheus container') @@ -502,13 +533,13 @@ class DCNetworkMonitor(): self.pushgateway_process.terminate() self.pushgateway_process.kill() self._stop_container('pushgateway') - ''' if self.cadvisor_process is not None: logging.info('stopping cadvisor container') self.cadvisor_process.terminate() self.cadvisor_process.kill() self._stop_container('cadvisor') + ''' def switch_tx_rx(self,metric=''): # when monitoring vnfs, the tx of the datacenter switch is actually the rx of the vnf