From 646d53f8140ee776b9e90141ae9afbeb6fa46570 Mon Sep 17 00:00:00 2001 From: peusterm Date: Wed, 17 Apr 2019 12:27:56 +0200 Subject: [PATCH] Added auto start/stop for prometheus #80 Signed-off-by: peusterm --- src/tngsdk/benchmark/__init__.py | 34 +++++++++++++++++++++++++++++++- src/tngsdk/benchmark/helper.py | 17 ++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/tngsdk/benchmark/__init__.py b/src/tngsdk/benchmark/__init__.py index ed24981..2ee0ede 100755 --- a/src/tngsdk/benchmark/__init__.py +++ b/src/tngsdk/benchmark/__init__.py @@ -37,13 +37,14 @@ import coloredlogs import time import shutil +import subprocess from tngsdk.benchmark.experiment import ServiceExperiment, FunctionExperiment from tngsdk.benchmark.generator.sonata \ import SonataServiceConfigurationGenerator from tngsdk.benchmark.generator.tango \ import TangoServiceConfigurationGenerator from tngsdk.benchmark.executor import Executor -from tngsdk.benchmark.helper import read_yaml +from tngsdk.benchmark.helper import read_yaml, get_prometheus_path from tngsdk.benchmark.resultprocessor.ietfbmwg import IetfBmwgResultProcessor from tngsdk.benchmark.resultprocessor.vimemu import VimemuResultProcessor from tngsdk.benchmark.logger import TangoLogger @@ -114,9 +115,11 @@ def run(self): if self.cgen is None: return self.generate_experiments() + self.start_prometheus_monitoring() self.execute_experiments() self.process_results() self.copy_ped() + self.stop_prometheus_monitoring() def check_rd_existence(self): if os.path.exists(self.args.result_dir): @@ -133,6 +136,35 @@ def check_rd_existence(self): self.logger.info("Overwriting old results: {}" .format(self.args.result_dir)) shutil.rmtree(self.args.result_dir) + # also clean prometheus data (if present, rquires sudo) + try: + pm_path = get_prometheus_path() + self.logger.info("Removing Prometheus data: {}" + .format(pm_path)) + subprocess.call(["./clean.sh"], cwd=pm_path) + except BaseException as ex: + self.logger.warning("Couldn't remove Prometheus data: {}" + .format(ex)) + + def start_prometheus_monitoring(self): + try: + pm_path = get_prometheus_path() + self.logger.info("Starting Prometheus 'docker-compose up in:' {}" + .format(pm_path)) + subprocess.call(["docker-compose", "up", "-d"], cwd=pm_path) + except BaseException as ex: + self.logger.warning("Couldn't start Prometheus. Skipping it. ({})" + .format(ex)) + + def stop_prometheus_monitoring(self): + try: + pm_path = get_prometheus_path() + self.logger.info("Stopping Prometheus 'docker-compose down in:' {}" + .format(pm_path)) + subprocess.call(["docker-compose", "down"], cwd=pm_path) + except BaseException as ex: + self.logger.warning("Couldn't stop Prometheus. Skipping it. ({})" + .format(ex)) def populate_experiments(self): if self.args.no_population: diff --git a/src/tngsdk/benchmark/helper.py b/src/tngsdk/benchmark/helper.py index ea5a84b..aee6e75 100755 --- a/src/tngsdk/benchmark/helper.py +++ b/src/tngsdk/benchmark/helper.py @@ -171,3 +171,20 @@ def dubunderscore_reducer(k1, k2): return k2 else: return k1 + "__" + k2 + + +def get_prometheus_path(): + """ + Returns the absolute path to the + Prometheus fodler of the used tng-bench + installation. + """ + try: + p = os.path.dirname(os.path.abspath(__file__)) + p = os.path.join(p, "../../../prometheus") + if os.path.exists(p): + return os.path.abspath(p) + raise BaseException("Prometheus path {} not found".format(p)) + except BaseException as ex: + LOG.warning("Couldn't find prometheus path: {}".format(ex)) + return None