+ # cAdvisor, Prometheus pushgateway are started as external container,\r
+ # to gather monitoring metric in son-emu\r
+ self.pushgateway_process = self.start_PushGateway()\r
+ self.cadvisor_process = self.start_cAdvisor()\r
+\r
+ # first set some parameters, before measurement can start\r
+\r
+ def setup_flow(self, vnf_name, vnf_interface=None,\r
+ metric='tx_packets', cookie=0):\r
+\r
+ flow_metric = {}\r
+\r
+ # check if port is specified (vnf:port)\r
+ if vnf_interface is None:\r
+ # take first interface by default\r
+ connected_sw = self.net.DCNetwork_graph.neighbors(vnf_name)[0]\r
+ link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]\r
+ vnf_interface = link_dict[0]['src_port_id']\r
+\r
+ flow_metric['vnf_name'] = vnf_name\r
+ flow_metric['vnf_interface'] = vnf_interface\r
+\r
+ vnf_switch = None\r
+ for connected_sw in self.net.DCNetwork_graph.neighbors(vnf_name):\r
+ link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]\r
+ for link in link_dict:\r
+ if link_dict[link]['src_port_id'] == vnf_interface:\r
+ # found the right link and connected switch\r
+ vnf_switch = connected_sw\r
+ flow_metric['mon_port'] = link_dict[link]['dst_port_nr']\r
+ break\r
+\r
+ if not vnf_switch:\r
+ logging.exception("vnf switch of {0}:{1} not found!".format(\r
+ vnf_name, vnf_interface))\r
+ return "vnf switch of {0}:{1} not found!".format(\r
+ vnf_name, vnf_interface)\r
+\r
+ try:\r
+ # default port direction to monitor\r
+ if metric is None:\r
+ metric = 'tx_packets'\r
+\r
+ next_node = self.net.getNodeByName(vnf_switch)\r
+\r
+ if not isinstance(next_node, OVSSwitch):\r
+ logging.info(\r
+ "vnf: {0} is not connected to switch".format(vnf_name))\r
+ return\r
+\r
+ flow_metric['previous_measurement'] = 0\r
+ flow_metric['previous_monitor_time'] = 0\r
+\r
+ flow_metric['switch_dpid'] = int(str(next_node.dpid), 16)\r
+ flow_metric['metric_key'] = metric\r
+ flow_metric['cookie'] = cookie\r
+\r
+ self.monitor_flow_lock.acquire()\r
+ self.flow_metrics.append(flow_metric)\r
+ self.monitor_flow_lock.release()\r
+\r
+ logging.info('Started monitoring flow:{3} {2} on {0}:{1}'.format(\r
+ vnf_name, vnf_interface, metric, cookie))\r
+ return 'Started monitoring flow:{3} {2} on {0}:{1}'.format(\r
+ vnf_name, vnf_interface, metric, cookie)\r
+\r
+ except Exception as ex:\r
+ logging.exception("setup_metric error.")\r
+ return ex.message\r
+\r
+ def stop_flow(self, vnf_name, vnf_interface=None, metric=None, cookie=0,):\r
+\r
+ # check if port is specified (vnf:port)\r
+ if vnf_interface is None and metric is not None:\r
+ # take first interface by default\r
+ connected_sw = self.net.DCNetwork_graph.neighbors(vnf_name)[0]\r
+ link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw]\r
+ vnf_interface = link_dict[0]['src_port_id']\r
+\r
+ for flow_dict in self.flow_metrics:\r
+ if flow_dict['vnf_name'] == vnf_name and flow_dict['vnf_interface'] == vnf_interface \\r
+ and flow_dict['metric_key'] == metric and flow_dict['cookie'] == cookie:\r
+\r
+ self.monitor_flow_lock.acquire()\r
+\r
+ self.flow_metrics.remove(flow_dict)\r
+\r
+ # set metric to NaN\r
+ self.prom_metrics[flow_dict['metric_key']]. \\r
+ labels(vnf_name=vnf_name, vnf_interface=vnf_interface, flow_id=cookie). \\r
+ set(float('nan'))\r
+\r
+ delete_from_gateway(\r
+ self.pushgateway, job='sonemu-SDNcontroller')\r
+\r
+ self.monitor_flow_lock.release()\r
+\r
+ logging.info('Stopped monitoring flow {3}: {2} on {0}:{1}'.format(\r
+ vnf_name, vnf_interface, metric, cookie))\r
+ return 'Stopped monitoring flow {3}: {2} on {0}:{1}'.format(\r
+ vnf_name, vnf_interface, metric, cookie)\r
+\r
+ return 'Error stopping monitoring flow: {0} on {1}:{2}'.format(\r
+ metric, vnf_name, vnf_interface)\r