From c6abf136c6fe1830e93a50a5c9c552a7c37f1d9f Mon Sep 17 00:00:00 2001 From: stevenvanrossem Date: Thu, 14 Apr 2016 11:15:58 +0200 Subject: [PATCH] start prometheus and cadvisor with Dockernet --- ansible/install.yml | 6 ++++ setup.py | 3 +- src/emuvim/dcemulator/monitoring.py | 45 ++++++++++++++++++++++++++-- src/emuvim/dcemulator/net.py | 2 ++ src/emuvim/dcemulator/prometheus.yml | 36 ++++++++++++++++++++++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/emuvim/dcemulator/prometheus.yml diff --git a/ansible/install.yml b/ansible/install.yml index 19c1ef2..b31615e 100755 --- a/ansible/install.yml +++ b/ansible/install.yml @@ -53,3 +53,9 @@ - name: install docker-py pip: name=docker-py state=latest + + - name: install prometheus_client + pip: name=prometheus_client state=latest + + + diff --git a/setup.py b/setup.py index 5bcad50..367c4fb 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,8 @@ setup(name='emuvim', 'Flask', 'flask_restful', 'docker-py', - 'requests' + 'requests', + 'prometheus_client' ], zip_safe=False, entry_points={ diff --git a/src/emuvim/dcemulator/monitoring.py b/src/emuvim/dcemulator/monitoring.py index 1f7ff2e..d745069 100755 --- a/src/emuvim/dcemulator/monitoring.py +++ b/src/emuvim/dcemulator/monitoring.py @@ -7,6 +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 logging.basicConfig(level=logging.INFO) @@ -61,12 +63,16 @@ class DCNetworkMonitor(): mon_port = None } ''' - self.network_metrics=[] + self.network_metrics = [] # start monitoring thread self.monitor_thread = threading.Thread(target=self.get_network_metrics) self.monitor_thread.start() + # helper tools + self.prometheus_process = None + self.cAdvisor_process = None + # first set some parameters, before measurement can start def setup_metric(self, vnf_name, vnf_interface=None, metric='tx_packets'): @@ -244,4 +250,39 @@ class DCNetworkMonitor(): url = self.REST_api + '/' + str(prefix) + '/' + str(dpid) req = urllib2.Request(url) ret = urllib2.urlopen(req).read() - return ret \ No newline at end of file + return ret + + def start_Prometheus(self, port=9090): + cmd = ["docker", + "run", + "--rm", + "-p", "{0}:9090".format(port), + "-v", "{0}/prometheus.yml:/etc/prometheus/prometheus.yml".format(getcwd()), + "--name", "prometheus", + "prom/prometheus" + ] + + self.prometheus_process = Popen(cmd) + + def start_cAdvisor(self, port=8090): + cmd = ["docker", + "run", + "--rm", + "--volume=/:/rootfs:ro", + "--volume=/var/run:/var/run:rw", + "--volume=/sys:/sys:ro", + "--volume=/var/lib/docker/:/var/lib/docker:ro", + "--publish={0}:8080".format(port), + "--name=cadvisor", + "google/cadvisor:latest" + ] + self.cAdvisor_process = Popen(cmd) + + def stop(self): + if self.prometheus_process is not None: + self.prometheus_process.terminate() + self.prometheus_process.kill() + + if self.cAdvisor_process is not None: + self.cAdvisor_process.terminate() + self.cAdvisor_process.kill() diff --git a/src/emuvim/dcemulator/net.py b/src/emuvim/dcemulator/net.py index 8130f84..c3c3c03 100755 --- a/src/emuvim/dcemulator/net.py +++ b/src/emuvim/dcemulator/net.py @@ -178,6 +178,8 @@ class DCNetwork(Dockernet): # stop Ryu controller Dockernet.stop(self) self.stopRyu() + # stop the monitor agent + self.monitor_agent.stop() def CLI(self): CLI(self) diff --git a/src/emuvim/dcemulator/prometheus.yml b/src/emuvim/dcemulator/prometheus.yml new file mode 100644 index 0000000..2915578 --- /dev/null +++ b/src/emuvim/dcemulator/prometheus.yml @@ -0,0 +1,36 @@ +global: + scrape_interval: 15s # By default, scrape targets every 15 seconds. + + # Attach these labels to any time series or alerts when communicating with + # external systems (federation, remote storage, Alertmanager). + external_labels: + monitor: 'codelab-monitor' + +# A scrape configuration containing exactly one endpoint to scrape: +# Here it's Prometheus itself. +scrape_configs: + # The job name is added as a label `job=` to any timeseries scraped from this config. + - job_name: 'prometheus' + + # Override the global default and scrape targets from this job every 5 seconds. + scrape_interval: 5s + + target_groups: + #- targets: ['localhost:9090'] + + - job_name: 'son-emu' + + # Override the global default and scrape targets from this job every 5 seconds. + scrape_interval: 5s + + target_groups: + - targets: ['172.17.0.1:8000'] + + - job_name: 'cAdvisor' + + # Override the global default and scrape targets from this job every 5 seconds. + scrape_interval: 5s + + target_groups: + - targets: ['172.17.0.1:8090'] + -- 2.25.1