blob: 858aad7c3b6ba1e10f9eb362f701703fda31dc49 [file] [log] [blame]
peusterm79ef6ae2016-07-08 13:53:57 +02001"""
2Copyright (c) 2015 SONATA-NFV
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
27"""
stevenvanrossem9c8a4122016-07-16 03:23:13 +020028
stevenvanrossem73efd192016-06-29 01:44:07 +020029from requests import get, put, delete
stevenvanrossem73efd192016-06-29 01:44:07 +020030import pprint
31import argparse
stevenvanrossem73efd192016-06-29 01:44:07 +020032from emuvim.cli import prometheus
33
34pp = pprint.PrettyPrinter(indent=4)
35
36class RestApiClient():
37
38 def __init__(self):
39 self.cmds = {}
40
41 def execute_command(self, args):
42 if getattr(self, args["command"]) is not None:
43 # call the local method with the same name as the command arg
44 getattr(self, args["command"])(args)
45 else:
46 print("Command not implemented.")
47
48 def setup_metric(self, args):
49 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
50 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
51
52 response = put("%s/restapi/monitor/%s/%s/%s" %
53 (args.get("endpoint"),
54 vnf_name,
55 vnf_interface,
56 args.get("metric")))
57 pp.pprint(response.json())
58
59 def stop_metric(self, args):
60 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
61 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
62
63 response = delete("%s/restapi/monitor/%s/%s/%s" %
64 (args.get("endpoint"),
65 vnf_name,
66 vnf_interface,
67 args.get("metric")))
68 pp.pprint(response.json())
69
70 def setup_flow(self, args):
71 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
72 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
73
74 response = put("%s/restapi/monitor/%s/%s/%s/%s" %
75 (args.get("endpoint"),
76 vnf_name,
77 vnf_interface,
78 args.get("metric"),
79 args.get("cookie")))
80
81 pp.pprint(response.json())
82
83 def stop_flow(self, args):
84 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
85 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
86
87 response = delete("%s/restapi/monitor/%s/%s/%s/%s" %
88 (args.get("endpoint"),
89 vnf_name,
90 vnf_interface,
91 args.get("metric"),
92 args.get("cookie")))
93
94 pp.pprint(response.json())
95
stevenvanrosseme131bf52016-07-14 11:42:09 +020096 def prometheus(self, args):
97 # This functions makes it more user-friendly to create the correct prometheus query
98 # <uuid> is replaced by the correct uuid of the deployed vnf container
99 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
100 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
101 dc_label = args.get("datacenter")
102 query = args.get("query")
103 vnf_status = get("%s/restapi/compute/%s/%s" %
104 (args.get("endpoint"),
105 args.get("datacenter"),
106 vnf_name)).json()
107 uuid = vnf_status['id']
108 query = query.replace('<uuid>', uuid)
109
110 response = prometheus.query_Prometheus(query)
111 pp.pprint(response)
112
stevenvanrossem73efd192016-06-29 01:44:07 +0200113 def _parse_vnf_name(self, vnf_name_str):
114 vnf_name = vnf_name_str.split(':')[0]
115 return vnf_name
116
117 def _parse_vnf_interface(self, vnf_name_str):
118 try:
119 vnf_interface = vnf_name_str.split(':')[1]
120 except:
121 vnf_interface = None
122
123 return vnf_interface
124
125parser = argparse.ArgumentParser(description='son-emu monitor')
126parser.add_argument(
127 "command",
128 choices=['setup_metric', 'stop_metric', 'setup_flow', 'stop_flow','prometheus'],
129 help="setup/stop a metric/flow to be monitored or query Prometheus")
130parser.add_argument(
131 "--vnf_name", "-vnf", dest="vnf_name",
132 help="vnf name:interface to be monitored")
133parser.add_argument(
134 "--metric", "-m", dest="metric",
135 help="tx_bytes, rx_bytes, tx_packets, rx_packets")
136parser.add_argument(
137 "--cookie", "-c", dest="cookie",
138 help="flow cookie to monitor")
139parser.add_argument(
140 "--query", "-q", dest="query",
141 help="prometheus query")
142parser.add_argument(
143 "--datacenter", "-d", dest="datacenter",
144 help="Data center where the vnf is deployed")
145parser.add_argument(
146 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +0200147 default="http://127.0.0.1:5001",
stevenvanrossem73efd192016-06-29 01:44:07 +0200148 help="UUID of the plugin to be manipulated.")
149
150def main(argv):
151 args = vars(parser.parse_args(argv))
152 c = RestApiClient()
153 c.execute_command(args)