Fix: Crashbug that was caused by an import of an
[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
29 from requests import get, put, delete
30 import pprint
31 import argparse
32 from emuvim.cli import prometheus
33
34 pp = pprint.PrettyPrinter(indent=4)
35
36 class 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
50 params = self._create_dict(
51 vnf_name=self._parse_vnf_name(args.get("vnf_name")),
52 vnf_interface = self._parse_vnf_interface(args.get("vnf_name")),
53 metric = args.get("metric"))
54
55 url = "{0}/restapi/monitor/interface".format(args.get("endpoint"))
56 response = put(url, params=params)
57 pp.pprint(response.text)
58
59 def stop_metric(self, args):
60 params = self._create_dict(
61 vnf_name=self._parse_vnf_name(args.get("vnf_name")),
62 vnf_interface=self._parse_vnf_interface(args.get("vnf_name")),
63 metric=args.get("metric"))
64
65 url = "{0}/restapi/monitor/interface".format(args.get("endpoint"))
66 response = put(url, params=params)
67 pp.pprint(response.text)
68
69 def setup_flow(self, args):
70 params = self._create_dict(
71 vnf_name=self._parse_vnf_name(args.get("vnf_name")),
72 vnf_interface=self._parse_vnf_interface(args.get("vnf_name")),
73 metric=args.get("metric"),
74 cookie=args.get("cookie"))
75
76 url = "{0}/restapi/monitor/flow".format(args.get("endpoint"))
77 response = put(url, params=params)
78 pp.pprint(response.text)
79
80 def stop_flow(self, args):
81 params = self._create_dict(
82 vnf_name=self._parse_vnf_name(args.get("vnf_name")),
83 vnf_interface=self._parse_vnf_interface(args.get("vnf_name")),
84 metric=args.get("metric"),
85 cookie=args.get("cookie"))
86
87 url = "{0}/restapi/monitor/flow".format(args.get("endpoint"))
88 response = put(url, params=params)
89 pp.pprint(response.text)
90
91 def prometheus(self, args):
92 # This functions makes it more user-friendly to create the correct prometheus query
93 # <uuid> is replaced by the correct uuid of the deployed vnf container
94 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
95 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
96 dc_label = args.get("datacenter")
97 query = args.get("query")
98
99 vnf_status = get("%s/restapi/compute/%s/%s" %
100 (args.get("endpoint"),
101 args.get("datacenter"),
102 vnf_name)).json()
103 uuid = vnf_status['id']
104 query = query.replace('<uuid>', uuid)
105
106 response = prometheus.query_Prometheus(query)
107 pp.pprint(response)
108
109 def _parse_vnf_name(self, vnf_name_str):
110 vnf_name = vnf_name_str.split(':')[0]
111 return vnf_name
112
113 def _parse_vnf_interface(self, vnf_name_str):
114 try:
115 vnf_interface = vnf_name_str.split(':')[1]
116 except:
117 vnf_interface = None
118
119 return vnf_interface
120
121 def _create_dict(self, **kwargs):
122 return kwargs
123
124 parser = argparse.ArgumentParser(description='son-emu-cli monitor')
125 parser.add_argument(
126 "command",
127 choices=['setup_metric', 'stop_metric', 'setup_flow', 'stop_flow','prometheus'],
128 help="setup/stop a metric/flow to be monitored or query Prometheus")
129 parser.add_argument(
130 "--vnf_name", "-vnf", dest="vnf_name",
131 help="vnf name:interface to be monitored")
132 parser.add_argument(
133 "--metric", "-m", dest="metric",
134 help="tx_bytes, rx_bytes, tx_packets, rx_packets")
135 parser.add_argument(
136 "--cookie", "-c", dest="cookie",
137 help="flow cookie to monitor")
138 parser.add_argument(
139 "--query", "-q", dest="query",
140 help="prometheus query")
141 parser.add_argument(
142 "--datacenter", "-d", dest="datacenter",
143 help="Data center where the vnf is deployed")
144 parser.add_argument(
145 "--endpoint", "-e", dest="endpoint",
146 default="http://127.0.0.1:5001",
147 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
148
149 def main(argv):
150 args = vars(parser.parse_args(argv))
151 c = RestApiClient()
152 c.execute_command(args)