From 2fdfbf4a18ef349aba8eacd5df17b4a1aeb68b74 Mon Sep 17 00:00:00 2001 From: stevenvanrossem Date: Fri, 13 May 2016 15:08:47 +0200 Subject: [PATCH] add files for son-monitor test --- misc/prometheus_jenkins.yml | 26 +++++++++++++++++ setup_cli_only.py | 25 +++++++++++++++++ src/emuvim/cli/compute.py | 1 - src/emuvim/cli/monitor.py | 26 +++++++++++++---- src/emuvim/cli/prometheus.py | 28 +++++++++++++++++++ src/emuvim/dcemulator/monitoring.py | 6 ++-- .../test/integrationtests/test_sdk_monitor.sh | 15 ++++++++++ 7 files changed, 119 insertions(+), 8 deletions(-) create mode 100755 misc/prometheus_jenkins.yml create mode 100755 setup_cli_only.py create mode 100755 src/emuvim/cli/prometheus.py create mode 100755 src/emuvim/test/integrationtests/test_sdk_monitor.sh diff --git a/misc/prometheus_jenkins.yml b/misc/prometheus_jenkins.yml new file mode 100755 index 0000000..b7770d1 --- /dev/null +++ b/misc/prometheus_jenkins.yml @@ -0,0 +1,26 @@ +global: + scrape_interval: 15s # By default, scrape targets every 15 seconds. + evaluation_interval: 5s + + +# A scrape configuration containing endpoints to scrape: + +scrape_configs: + +# cAdvsior started in son-emu + - job_name: 'cAdvisor' + + scrape_interval: 1s + + target_groups: + - targets: ['172.17.0.1:8090'] + +# Pushgateway started in SP + - job_name: 'PushGateway' + + scrape_interval: 1s + + target_groups: + - targets: ['172.17.0.1:9091'] + + diff --git a/setup_cli_only.py b/setup_cli_only.py new file mode 100755 index 0000000..119cf8c --- /dev/null +++ b/setup_cli_only.py @@ -0,0 +1,25 @@ +from setuptools import setup, find_packages + +setup(name='emuvim', + version='0.0.1', + license='Apache 2.0', + description='emuvim is a VIM for the SONATA platform', + url='http://github.com/sonata-emu', + author_email='sonata-dev@sonata-nfv.eu', + package_dir={'': 'src'}, + # packages=find_packages('emuvim', exclude=['*.test', '*.test.*', 'test.*', 'test']), + packages=find_packages('src'), + install_requires=[ + 'zerorpc', + 'tabulate', + 'argparse', + ], + zip_safe=False, + entry_points={ + 'console_scripts': [ + 'son-emu-cli=emuvim.cli.son_emu_cli:main', + ], + }, + setup_requires=['pytest-runner'], + tests_require=['pytest'], +) \ No newline at end of file diff --git a/src/emuvim/cli/compute.py b/src/emuvim/cli/compute.py index dcb499d..d01dfed 100755 --- a/src/emuvim/cli/compute.py +++ b/src/emuvim/cli/compute.py @@ -30,7 +30,6 @@ class ZeroRpcClient(object): nw_list = list() if args.get("network") is not None: nw_list = self._parse_network(args.get("network")) - r = self.c.compute_action_start( args.get("datacenter"), args.get("name"), diff --git a/src/emuvim/cli/monitor.py b/src/emuvim/cli/monitor.py index 34853a6..79ff25b 100755 --- a/src/emuvim/cli/monitor.py +++ b/src/emuvim/cli/monitor.py @@ -5,20 +5,24 @@ son-emu monitor CLI import argparse import pprint -from tabulate import tabulate import zerorpc -import time - +import prometheus pp = pprint.PrettyPrinter(indent=4) class ZeroRpcClient(object): def __init__(self): + # network zerorpc self.c = zerorpc.Client() # TODO connect to DCNetwork API #self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later self.c.connect("tcp://127.0.0.1:5151") + + # compute zerorpc + self.compute_api = zerorpc.Client(heartbeat=None, timeout=120) # heartbeat=None, timeout=120 + self.compute_api.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later + self.cmds = {} def execute_command(self, args): @@ -66,7 +70,7 @@ class ZeroRpcClient(object): args.get("cookie")) pp.pprint(r) - def prometheus(self, args): + def prometheus_zrpc(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.prometheus( @@ -76,6 +80,18 @@ class ZeroRpcClient(object): args.get("query")) pp.pprint(r) + def prometheus(self, args): + vnf_name = self._parse_vnf_name(args.get("vnf_name")) + vnf_interface = self._parse_vnf_interface(args.get("vnf_name")) + dc_label = args.get("datacenter") + query = args.get("query") + vnf_status = self.compute_api.compute_status(dc_label, vnf_name) + uuid = vnf_status['id'] + query = query.replace('', uuid) + + r = prometheus.query_Prometheus(query) + pp.pprint(r) + def _parse_vnf_name(self, vnf_name_str): vnf_name = vnf_name_str.split(':')[0] @@ -93,7 +109,7 @@ parser = argparse.ArgumentParser(description='son-emu monitor') parser.add_argument( "command", choices=['setup_metric', 'stop_metric', 'setup_flow', 'stop_flow','prometheus'], - help="setup/stop a metric/flow to be monitored or Prometheus query") + help="setup/stop a metric/flow to be monitored or query Prometheus") parser.add_argument( "--vnf_name", "-vnf", dest="vnf_name", help="vnf name:interface to be monitored") diff --git a/src/emuvim/cli/prometheus.py b/src/emuvim/cli/prometheus.py new file mode 100755 index 0000000..8d87d89 --- /dev/null +++ b/src/emuvim/cli/prometheus.py @@ -0,0 +1,28 @@ +""" +Prometheus API helper functions +(c) 2016 by Steven Van Rossem +""" + +import urllib2 +import ast + +prometheus_ip = '0.0.0.0' +prometheus_port = '9090' +prometheus_REST_api = 'http://{0}:{1}'.format(prometheus_ip, prometheus_port) + + +def query_Prometheus(query): + url = prometheus_REST_api + '/' + 'api/v1/query?query=' + query + # logging.info('query:{0}'.format(url)) + req = urllib2.Request(url) + ret = urllib2.urlopen(req).read() + ret = ast.literal_eval(ret) + if ret['status'] == 'success': + # logging.info('return:{0}'.format(ret)) + try: + ret = ret['data']['result'][0]['value'] + except: + ret = None + else: + ret = None + return ret \ No newline at end of file diff --git a/src/emuvim/dcemulator/monitoring.py b/src/emuvim/dcemulator/monitoring.py index e663cae..8db10da 100755 --- a/src/emuvim/dcemulator/monitoring.py +++ b/src/emuvim/dcemulator/monitoring.py @@ -75,8 +75,8 @@ class DCNetworkMonitor(): self.monitor_flow_thread.start() # helper tools - self.pushgateway_process = self.start_PushGateway() - self.prometheus_process = self.start_Prometheus() + #self.pushgateway_process = self.start_PushGateway() + #self.prometheus_process = self.start_Prometheus() self.cadvisor_process = self.start_cadvisor() # first set some parameters, before measurement can start @@ -496,6 +496,7 @@ class DCNetworkMonitor(): self.monitor_thread.join() self.monitor_flow_thread.join() + ''' if self.prometheus_process is not None: logging.info('stopping prometheus container') self.prometheus_process.terminate() @@ -507,6 +508,7 @@ class DCNetworkMonitor(): self.pushgateway_process.terminate() self.pushgateway_process.kill() self._stop_container('pushgateway') + ''' if self.cadvisor_process is not None: logging.info('stopping cadvisor container') diff --git a/src/emuvim/test/integrationtests/test_sdk_monitor.sh b/src/emuvim/test/integrationtests/test_sdk_monitor.sh new file mode 100755 index 0000000..26f939f --- /dev/null +++ b/src/emuvim/test/integrationtests/test_sdk_monitor.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# test if a vnf can be monitored and deployed + +cpu_load=$(son-emu-cli monitor prometheus -d datacenter1 -vnf vnf1 -q 'sum(rate(container_cpu_usage_seconds_total{id="/docker/"}[10s]))') + +# test if prometheus query worked +regex="[0-9.]+, [0-9.']+" +if [[ $cpu_load =~ $regex ]] ; then + echo "OK" + exit 0 +else + echo $cpu_load + echo "not OK" + exit 1 +fi -- 2.25.1