From b098cb54745c7d2d391d3ff13b501c7c8891cdfd Mon Sep 17 00:00:00 2001 From: stevenvanrossem Date: Fri, 15 Apr 2016 13:28:23 +0200 Subject: [PATCH] start cadvisor and prometheus docker container at startup --- src/emuvim/api/zerorpc/network.py | 10 +++ src/emuvim/cli/monitor.py | 16 ++-- src/emuvim/dcemulator/monitoring.py | 133 +++++++++++++--------------- 3 files changed, 77 insertions(+), 82 deletions(-) diff --git a/src/emuvim/api/zerorpc/network.py b/src/emuvim/api/zerorpc/network.py index 1b7e1e0..f21ce99 100644 --- a/src/emuvim/api/zerorpc/network.py +++ b/src/emuvim/api/zerorpc/network.py @@ -100,6 +100,16 @@ class DCNetworkApi(object): logging.exception("RPC error.") return ex.message + # remove the rate measurement for a vnf interface + def remove_metric(self, vnf_name, vnf_interface, metric): + logging.debug("RPC CALL: setup metric") + try: + c = self.net.monitor_agent.remove_metric(vnf_name, vnf_interface, metric) + return c + except Exception as ex: + logging.exception("RPC error.") + return ex.message + # setup the rate measurement for a vnf interface def monitor_setup_rate_measurement(self, vnf_name, vnf_interface, metric): logging.debug("RPC CALL: get rate") diff --git a/src/emuvim/cli/monitor.py b/src/emuvim/cli/monitor.py index 1d4abfb..bae0c07 100755 --- a/src/emuvim/cli/monitor.py +++ b/src/emuvim/cli/monitor.py @@ -36,19 +36,15 @@ class ZeroRpcClient(object): vnf_interface, args.get("metric")) pp.pprint(r) - ''' - self.c.monitor_setup_rate_measurement( + + def remove_metric(self, args): + vnf_name = self._parse_vnf_name(args.get("vnf_name")) + vnf_interface = self._parse_vnf_interface(args.get("vnf_name")) + r = self.c.remove_metric( vnf_name, vnf_interface, args.get("metric")) - while True: - r = self.c.monitor_get_rate( - vnf_name, - vnf_interface, - args.get("metric")) - pp.pprint(r) - time.sleep(1) - ''' + pp.pprint(r) def _parse_vnf_name(self, vnf_name_str): vnf_name = vnf_name_str.split(':')[0] diff --git a/src/emuvim/dcemulator/monitoring.py b/src/emuvim/dcemulator/monitoring.py index d745069..7fa5e18 100755 --- a/src/emuvim/dcemulator/monitoring.py +++ b/src/emuvim/dcemulator/monitoring.py @@ -7,8 +7,8 @@ import ast import time from prometheus_client import start_http_server, Summary, Histogram, Gauge, Counter import threading -from subprocess import Popen -from os import getcwd +from subprocess import Popen, PIPE +import os logging.basicConfig(level=logging.INFO) @@ -25,16 +25,6 @@ class DCNetworkMonitor(): self.REST_api = 'http://{0}:{1}'.format(self.ip,self.port) # helper variables to calculate the metrics - # TODO put these in a list to support multiple metrics simultaneously - self.switch_dpid = 0 - self.vnf_name = None - self.vnf_interface = None - self.previous_measurement = 0 - self.previous_monitor_time = 0 - self.metric_key = None - self.mon_port = None - - # Start up the server to expose the metrics to Prometheus. start_http_server(8000) # supported Prometheus metrics @@ -66,13 +56,13 @@ class DCNetworkMonitor(): self.network_metrics = [] # start monitoring thread + self.start_monitoring = True self.monitor_thread = threading.Thread(target=self.get_network_metrics) self.monitor_thread.start() # helper tools - self.prometheus_process = None - self.cAdvisor_process = None - + self.prometheus_process = self.start_Prometheus() + self.cadvisor_process = self.start_cadvisor() # first set some parameters, before measurement can start def setup_metric(self, vnf_name, vnf_interface=None, metric='tx_packets'): @@ -88,8 +78,6 @@ class DCNetworkMonitor(): network_metric['vnf_name'] = vnf_name network_metric['vnf_interface'] = vnf_interface - #self.vnf_name = vnf_name - #self.vnf_interface = vnf_interface for connected_sw in self.net.DCNetwork_graph.neighbors(vnf_name): link_dict = self.net.DCNetwork_graph[vnf_name][connected_sw] @@ -99,7 +87,6 @@ class DCNetworkMonitor(): # found the right link and connected switch # logging.info("{0},{1}".format(link_dict[link]['src_port_id'], vnf_source_interface)) network_metric['mon_port'] = link_dict[link]['dst_port'] - # self.mon_port = link_dict[link]['dst_port'] break if 'mon_port' not in network_metric: @@ -129,13 +116,11 @@ class DCNetworkMonitor(): network_metric['previous_measurement'] = 0 network_metric['previous_monitor_time'] = 0 - #self.previous_measurement = 0 - #self.previous_monitor_time = 0 + network_metric['switch_dpid'] = int(str(next_node.dpid), 16) network_metric['metric_key'] = metric - #self.switch_dpid = int(str(next_node.dpid), 16) - #self.metric_key = '{0}_{1}'.format(direction, metric) + self.network_metrics.append(network_metric) @@ -146,10 +131,18 @@ class DCNetworkMonitor(): logging.exception("get_rate error.") return ex.message + def remove_metric(self, vnf_name, vnf_interface, metric): + 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'] == metric: + self.network_metrics.remove(metric_dict) + 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) - # get all metrics defined in the list + + # get all metrics defined in the list and export it to Prometheus def get_network_metrics(self): - while True: + while self.start_monitoring: # group metrics by dpid to optimize the rest api calls dpid_list = [metric_dict['switch_dpid'] for metric_dict in self.network_metrics] dpid_set = set(dpid_list) @@ -168,10 +161,10 @@ class DCNetworkMonitor(): time.sleep(1) - # call this function repeatedly for streaming measurements + # add metric to the list to export to Prometheus, parse the Ryu port-stats reply def set_network_metric(self, metric_dict, port_stat_dict): - - metric_key = metric_dict['metric_key'] + # vnf tx is the datacenter switch rx and vice-versa + metric_key = self.switch_tx_rx(metric_dict['metric_key']) switch_dpid = metric_dict['switch_dpid'] vnf_name = metric_dict['vnf_name'] vnf_interface = metric_dict['vnf_interface'] @@ -209,43 +202,6 @@ class DCNetworkMonitor(): return 'metric {0} not found on {1}:{2}'.format(metric_key, vnf_name, vnf_interface) - # call this function repeatedly for streaming measurements - def get_rate(self, vnf_name, vnf_interface=None, direction='tx', metric='packets'): - - key = self.metric_key - - ret = self.REST_cmd('stats/port', self.switch_dpid) - port_stat_dict = ast.literal_eval(ret) - for port_stat in port_stat_dict[str(self.switch_dpid)]: - if port_stat['port_no'] == self.mon_port: - port_uptime = port_stat['duration_sec'] + port_stat['duration_nsec'] * 10 ** (-9) - this_measurement = int(port_stat[key]) - #logging.info('packets:{0}'.format(this_measurement)) - - # set prometheus metrics - if metric == 'packets': - self.prom_tx_packet_count.labels(self.vnf_name, self.vnf_interface).set(this_measurement) - elif metric == 'bytes': - self.prom_tx_byte_count.labels(self.vnf_name, self.vnf_interface).set(this_measurement) - - if self.previous_monitor_time <= 0 or self.previous_monitor_time >= port_uptime: - self.previous_measurement = int(port_stat[key]) - self.previous_monitor_time = port_uptime - # do first measurement - time.sleep(1) - byte_rate = self.get_rate(vnf_name, vnf_interface, direction, metric) - return byte_rate - else: - time_delta = (port_uptime - self.previous_monitor_time) - byte_rate = (this_measurement - self.previous_measurement) / float(time_delta) - #logging.info('uptime:{2} delta:{0} rate:{1}'.format(time_delta,byte_rate,port_uptime)) - - self.previous_measurement = this_measurement - self.previous_monitor_time = port_uptime - return byte_rate - - return ret - def REST_cmd(self, prefix, dpid): url = self.REST_api + '/' + str(prefix) + '/' + str(dpid) req = urllib2.Request(url) @@ -253,18 +209,19 @@ class DCNetworkMonitor(): return ret def start_Prometheus(self, port=9090): + # prometheus.yml configuration file is located in the same directory as this file cmd = ["docker", "run", "--rm", "-p", "{0}:9090".format(port), - "-v", "{0}/prometheus.yml:/etc/prometheus/prometheus.yml".format(getcwd()), + "-v", "{0}/prometheus.yml:/etc/prometheus/prometheus.yml".format(os.path.dirname(os.path.abspath(__file__))), "--name", "prometheus", "prom/prometheus" ] + logging.info('Start Prometheus container {0}'.format(cmd)) + return Popen(cmd) - self.prometheus_process = Popen(cmd) - - def start_cAdvisor(self, port=8090): + def start_cadvisor(self, port=8090): cmd = ["docker", "run", "--rm", @@ -276,13 +233,45 @@ class DCNetworkMonitor(): "--name=cadvisor", "google/cadvisor:latest" ] - self.cAdvisor_process = Popen(cmd) + logging.info('Start cAdvisor container {0}'.format(cmd)) + return Popen(cmd) def stop(self): + # stop the monitoring thread + self.start_monitoring = False + self.monitor_thread.join() + if self.prometheus_process is not None: + logging.info('stopping prometheus container') self.prometheus_process.terminate() self.prometheus_process.kill() + self._stop_container('prometheus') + + 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 + # so we need to change the metric name to be consistent with the vnf rx or tx + if 'tx' in metric: + metric = metric.replace('tx','rx') + elif 'rx' in metric: + metric = metric.replace('rx','tx') + + return metric + + def _stop_container(self, name): + cmd = ["docker", + "stop", + name] + Popen(cmd).wait() + + cmd = ["docker", + "rm", + name] + Popen(cmd).wait() + - if self.cAdvisor_process is not None: - self.cAdvisor_process.terminate() - self.cAdvisor_process.kill() -- 2.25.1