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