+ def set_flow_metric(self, metric_dict, flow_stat_dict):\r
+ # vnf tx is the datacenter switch rx and vice-versa\r
+ metric_key = metric_dict['metric_key']\r
+ switch_dpid = metric_dict['switch_dpid']\r
+ vnf_name = metric_dict['vnf_name']\r
+ vnf_interface = metric_dict['vnf_interface']\r
+ previous_measurement = metric_dict['previous_measurement']\r
+ previous_monitor_time = metric_dict['previous_monitor_time']\r
+ cookie = metric_dict['cookie']\r
+\r
+ # TODO aggregate all found flow stats\r
+ #flow_stat = flow_stat_dict[str(switch_dpid)][0]\r
+ #if 'bytes' in metric_key:\r
+ # counter = flow_stat['byte_count']\r
+ #elif 'packet' in metric_key:\r
+ # counter = flow_stat['packet_count']\r
+\r
+ counter = 0\r
+ for flow_stat in flow_stat_dict[str(switch_dpid)]:\r
+ if 'bytes' in metric_key:\r
+ counter += flow_stat['byte_count']\r
+ elif 'packet' in metric_key:\r
+ counter += flow_stat['packet_count']\r
+\r
+ flow_stat = flow_stat_dict[str(switch_dpid)][0]\r
+ flow_uptime = flow_stat['duration_sec'] + flow_stat['duration_nsec'] * 10 ** (-9)\r
+\r
+ self.prom_metrics[metric_dict['metric_key']]. \\r
+ labels({'vnf_name': vnf_name, 'vnf_interface': vnf_interface, 'flow_id': cookie}). \\r
+ set(counter)\r
+ pushadd_to_gateway(self.pushgateway, job='sonemu-SDNcontroller', registry=self.registry)\r
+\r
+\r
+ def start_Prometheus(self, port=9090):\r
+ # prometheus.yml configuration file is located in the same directory as this file\r
+ cmd = ["docker",\r
+ "run",\r
+ "--rm",\r
+ "-p", "{0}:9090".format(port),\r
+ "-v", "{0}/prometheus.yml:/etc/prometheus/prometheus.yml".format(os.path.dirname(os.path.abspath(__file__))),\r
+ "-v", "{0}/profile.rules:/etc/prometheus/profile.rules".format(os.path.dirname(os.path.abspath(__file__))),\r
+ "--name", "prometheus",\r
+ "prom/prometheus"\r
+ ]\r
+ logging.info('Start Prometheus container {0}'.format(cmd))\r
+ return Popen(cmd)\r
+\r
+ def start_PushGateway(self, port=9091):\r
+ cmd = ["docker",\r
+ "run",\r
+ "-d",\r
+ "-p", "{0}:9091".format(port),\r
+ "--name", "pushgateway",\r
+ "prom/pushgateway"\r
+ ]\r
+\r
+ logging.info('Start Prometheus Push Gateway container {0}'.format(cmd))\r
+ return Popen(cmd)\r
+\r
+ def start_cadvisor(self, port=8090):\r
+ cmd = ["docker",\r
+ "run",\r
+ "--rm",\r
+ "--volume=/:/rootfs:ro",\r
+ "--volume=/var/run:/var/run:rw",\r
+ "--volume=/sys:/sys:ro",\r
+ "--volume=/var/lib/docker/:/var/lib/docker:ro",\r
+ "--publish={0}:8080".format(port),\r
+ "--name=cadvisor",\r
+ "google/cadvisor:latest"\r
+ ]\r
+ logging.info('Start cAdvisor container {0}'.format(cmd))\r
+ return Popen(cmd)\r
+\r
+ def stop(self):\r
+ # stop the monitoring thread\r
+ self.start_monitoring = False\r
+ self.monitor_thread.join()\r
+ self.monitor_flow_thread.join()\r
+\r
+ # these containers are used for monitoring but are started now outside of son-emu\r
+ '''\r
+ if self.prometheus_process is not None:\r
+ logging.info('stopping prometheus container')\r
+ self.prometheus_process.terminate()\r
+ self.prometheus_process.kill()\r
+ self._stop_container('prometheus')\r
+\r
+ if self.pushgateway_process is not None:\r
+ logging.info('stopping pushgateway container')\r
+ self.pushgateway_process.terminate()\r
+ self.pushgateway_process.kill()\r
+ self._stop_container('pushgateway')\r
+\r
+ if self.cadvisor_process is not None:\r
+ logging.info('stopping cadvisor container')\r
+ self.cadvisor_process.terminate()\r
+ self.cadvisor_process.kill()\r
+ self._stop_container('cadvisor')\r
+ '''\r
+\r
+ def switch_tx_rx(self,metric=''):\r
+ # when monitoring vnfs, the tx of the datacenter switch is actually the rx of the vnf\r
+ # so we need to change the metric name to be consistent with the vnf rx or tx\r
+ if 'tx' in metric:\r
+ metric = metric.replace('tx','rx')\r
+ elif 'rx' in metric:\r
+ metric = metric.replace('rx','tx')\r
+\r
+ return metric\r
+\r
+ def _stop_container(self, name):\r
+ cmd = ["docker",\r
+ "stop",\r
+ name]\r
+ Popen(cmd).wait()\r
+\r
+ cmd = ["docker",\r
+ "rm",\r
+ name]\r
+ Popen(cmd).wait()\r