Merge pull request #133 from mpeuster/master
[osm/vim-emu.git] / src / emuvim / cli / monitor.py
index 123abe5..9620a98 100755 (executable)
@@ -1,24 +1,51 @@
 """\r
-son-emu monitor CLI\r
-(c) 2016 by Manuel Peuster <manuel.peuster@upb.de>\r
+Copyright (c) 2015 SONATA-NFV\r
+ALL RIGHTS RESERVED.\r
+\r
+Licensed under the Apache License, Version 2.0 (the "License");\r
+you may not use this file except in compliance with the License.\r
+You may obtain a copy of the License at\r
+\r
+    http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+Unless required by applicable law or agreed to in writing, software\r
+distributed under the License is distributed on an "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+See the License for the specific language governing permissions and\r
+limitations under the License.\r
+\r
+Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]\r
+nor the names of its contributors may be used to endorse or promote\r
+products derived from this software without specific prior written\r
+permission.\r
+\r
+This work has been performed in the framework of the SONATA project,\r
+funded by the European Commission under Grant number 671517 through\r
+the Horizon 2020 and 5G-PPP programmes. The authors would like to\r
+acknowledge the contributions of their colleagues of the SONATA\r
+partner consortium (www.sonata-nfv.eu).\r
 """\r
 \r
 import argparse\r
 import pprint\r
-from tabulate import tabulate\r
 import zerorpc\r
-import time\r
-\r
+from emuvim.cli 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
@@ -26,7 +53,7 @@ class ZeroRpcClient(object):
             # call the local method with the same name as the command arg\r
             getattr(self, args["command"])(args)\r
         else:\r
-            print "Command not implemented."\r
+            print("Command not implemented.")\r
 \r
     def setup_metric(self, args):\r
         vnf_name = self._parse_vnf_name(args.get("vnf_name"))\r
@@ -56,7 +83,17 @@ class ZeroRpcClient(object):
             args.get("cookie"))\r
         pp.pprint(r)\r
 \r
-    def prometheus(self, args):\r
+    def stop_flow(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.stop_flow(\r
+            vnf_name,\r
+            vnf_interface,\r
+            args.get("metric"),\r
+            args.get("cookie"))\r
+        pp.pprint(r)\r
+\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
@@ -66,6 +103,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
@@ -82,7 +131,8 @@ class ZeroRpcClient(object):
 parser = argparse.ArgumentParser(description='son-emu monitor')\r
 parser.add_argument(\r
     "command",\r
-    help="Action to be executed")\r
+    choices=['setup_metric', 'stop_metric', 'setup_flow', 'stop_flow','prometheus'],\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