+++ /dev/null
-"""\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
-import zerorpc\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
- if getattr(self, args["command"]) is not None:\r
- # 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
-\r
- def setup_metric(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.setup_metric(\r
- vnf_name,\r
- vnf_interface,\r
- args.get("metric"))\r
- pp.pprint(r)\r
-\r
- def stop_metric(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_metric(\r
- vnf_name,\r
- vnf_interface,\r
- args.get("metric"))\r
- pp.pprint(r)\r
-\r
- def setup_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.setup_flow(\r
- vnf_name,\r
- vnf_interface,\r
- args.get("metric"),\r
- args.get("cookie"))\r
- pp.pprint(r)\r
-\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(self, args):\r
- # This functions makes it more user-friendly to create the correct prometheus query\r
- # <uuid> is replaced by the correct uuid of the deployed vnf container\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
- return vnf_name\r
-\r
- def _parse_vnf_interface(self, vnf_name_str):\r
- try:\r
- vnf_interface = vnf_name_str.split(':')[1]\r
- except:\r
- vnf_interface = None\r
-\r
- return vnf_interface\r
-\r
-parser = argparse.ArgumentParser(description='son-emu monitor')\r
-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 query Prometheus")\r
-parser.add_argument(\r
- "--vnf_name", "-vnf", dest="vnf_name",\r
- help="vnf name:interface to be monitored")\r
-parser.add_argument(\r
- "--metric", "-m", dest="metric",\r
- help="tx_bytes, rx_bytes, tx_packets, rx_packets")\r
-parser.add_argument(\r
- "--cookie", "-c", dest="cookie",\r
- help="flow cookie to monitor")\r
-parser.add_argument(\r
- "--query", "-q", dest="query",\r
- help="prometheus query")\r
-parser.add_argument(\r
- "--datacenter", "-d", dest="datacenter",\r
- help="Data center where the vnf is deployed")\r
-\r
-def main(argv):\r
- #print "This is the son-emu monitor CLI."\r
- #print "Arguments: %s" % str(argv)\r
- args = vars(parser.parse_args(argv))\r
- c = ZeroRpcClient()\r
- c.execute_command(args)\r