add files for son-monitor test
authorstevenvanrossem <steven.vanrossem@intec.ugent.be>
Fri, 13 May 2016 13:08:47 +0000 (15:08 +0200)
committerstevenvanrossem <steven.vanrossem@intec.ugent.be>
Fri, 13 May 2016 13:08:47 +0000 (15:08 +0200)
misc/prometheus_jenkins.yml [new file with mode: 0755]
setup_cli_only.py [new file with mode: 0755]
src/emuvim/cli/compute.py
src/emuvim/cli/monitor.py
src/emuvim/cli/prometheus.py [new file with mode: 0755]
src/emuvim/dcemulator/monitoring.py
src/emuvim/test/integrationtests/test_sdk_monitor.sh [new file with mode: 0755]

diff --git a/misc/prometheus_jenkins.yml b/misc/prometheus_jenkins.yml
new file mode 100755 (executable)
index 0000000..b7770d1
--- /dev/null
@@ -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 (executable)
index 0000000..119cf8c
--- /dev/null
@@ -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
index dcb499d..d01dfed 100755 (executable)
@@ -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"),
index 34853a6..79ff25b 100755 (executable)
@@ -5,20 +5,24 @@ son-emu monitor CLI
 \r
 import argparse\r
 import pprint\r
-from tabulate import tabulate\r
 import zerorpc\r
-import time\r
-\r
+import prometheus\r
 \r
 pp = pprint.PrettyPrinter(indent=4)\r
 \r
 class ZeroRpcClient(object):\r
 \r
     def __init__(self):\r
+        # network zerorpc\r
         self.c = zerorpc.Client()\r
         # TODO connect to DCNetwork API\r
         #self.c.connect("tcp://127.0.0.1:4242")  # TODO hard coded for now. we'll change this later\r
         self.c.connect("tcp://127.0.0.1:5151")\r
+\r
+        # compute zerorpc\r
+        self.compute_api = zerorpc.Client(heartbeat=None, timeout=120)  # heartbeat=None, timeout=120\r
+        self.compute_api.connect("tcp://127.0.0.1:4242")  # TODO hard coded for now. we'll change this later\r
+\r
         self.cmds = {}\r
 \r
     def execute_command(self, args):\r
@@ -66,7 +70,7 @@ class ZeroRpcClient(object):
             args.get("cookie"))\r
         pp.pprint(r)\r
 \r
-    def prometheus(self, args):\r
+    def prometheus_zrpc(self, args):\r
         vnf_name = self._parse_vnf_name(args.get("vnf_name"))\r
         vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))\r
         r = self.c.prometheus(\r
@@ -76,6 +80,18 @@ class ZeroRpcClient(object):
             args.get("query"))\r
         pp.pprint(r)\r
 \r
+    def prometheus(self, args):\r
+        vnf_name = self._parse_vnf_name(args.get("vnf_name"))\r
+        vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))\r
+        dc_label = args.get("datacenter")\r
+        query = args.get("query")\r
+        vnf_status = self.compute_api.compute_status(dc_label, vnf_name)\r
+        uuid = vnf_status['id']\r
+        query = query.replace('<uuid>', uuid)\r
+\r
+        r = prometheus.query_Prometheus(query)\r
+        pp.pprint(r)\r
+\r
 \r
     def _parse_vnf_name(self, vnf_name_str):\r
         vnf_name = vnf_name_str.split(':')[0]\r
@@ -93,7 +109,7 @@ parser = argparse.ArgumentParser(description='son-emu monitor')
 parser.add_argument(\r
     "command",\r
     choices=['setup_metric', 'stop_metric', 'setup_flow', 'stop_flow','prometheus'],\r
-    help="setup/stop a metric/flow to be monitored or Prometheus query")\r
+    help="setup/stop a metric/flow to be monitored or query Prometheus")\r
 parser.add_argument(\r
     "--vnf_name", "-vnf", dest="vnf_name",\r
     help="vnf name:interface to be monitored")\r
diff --git a/src/emuvim/cli/prometheus.py b/src/emuvim/cli/prometheus.py
new file mode 100755 (executable)
index 0000000..8d87d89
--- /dev/null
@@ -0,0 +1,28 @@
+"""
+Prometheus API helper functions
+(c) 2016 by Steven Van Rossem <steven.vanrossem@intec.ugent.be>
+"""
+
+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
index e663cae..8db10da 100755 (executable)
@@ -75,8 +75,8 @@ class DCNetworkMonitor():
         self.monitor_flow_thread.start()\r
 \r
         # helper tools\r
-        self.pushgateway_process = self.start_PushGateway()\r
-        self.prometheus_process = self.start_Prometheus()\r
+        #self.pushgateway_process = self.start_PushGateway()\r
+        #self.prometheus_process = self.start_Prometheus()\r
         self.cadvisor_process = self.start_cadvisor()\r
 \r
     # first set some parameters, before measurement can start\r
@@ -496,6 +496,7 @@ class DCNetworkMonitor():
         self.monitor_thread.join()\r
         self.monitor_flow_thread.join()\r
 \r
+        '''\r
         if self.prometheus_process is not None:\r
             logging.info('stopping prometheus container')\r
             self.prometheus_process.terminate()\r
@@ -507,6 +508,7 @@ class DCNetworkMonitor():
             self.pushgateway_process.terminate()\r
             self.pushgateway_process.kill()\r
             self._stop_container('pushgateway')\r
+        '''\r
 \r
         if self.cadvisor_process is not None:\r
             logging.info('stopping cadvisor container')\r
diff --git a/src/emuvim/test/integrationtests/test_sdk_monitor.sh b/src/emuvim/test/integrationtests/test_sdk_monitor.sh
new file mode 100755 (executable)
index 0000000..26f939f
--- /dev/null
@@ -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/<uuid>"}[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