1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 from requests
import get
, put
29 from emuvim
.cli
import prometheus
31 pp
= pprint
.PrettyPrinter(indent
=4)
34 class RestApiClient():
39 def execute_command(self
, args
):
40 if getattr(self
, args
["command"]) is not None:
41 # call the local method with the same name as the command arg
42 getattr(self
, args
["command"])(args
)
44 print("Command not implemented.")
46 def setup_metric(self
, args
):
48 params
= self
._create
_dict
(
49 vnf_name
=self
._parse
_vnf
_name
(args
.get("vnf_name")),
50 vnf_interface
=self
._parse
_vnf
_interface
(args
.get("vnf_name")),
51 metric
=args
.get("metric"))
53 url
= "{0}/restapi/monitor/interface".format(args
.get("endpoint"))
54 response
= put(url
, params
=params
)
55 pp
.pprint(response
.text
)
57 def stop_metric(self
, args
):
58 params
= self
._create
_dict
(
59 vnf_name
=self
._parse
_vnf
_name
(args
.get("vnf_name")),
60 vnf_interface
=self
._parse
_vnf
_interface
(args
.get("vnf_name")),
61 metric
=args
.get("metric"))
63 url
= "{0}/restapi/monitor/interface".format(args
.get("endpoint"))
64 response
= put(url
, params
=params
)
65 pp
.pprint(response
.text
)
67 def setup_flow(self
, args
):
68 params
= self
._create
_dict
(
69 vnf_name
=self
._parse
_vnf
_name
(args
.get("vnf_name")),
70 vnf_interface
=self
._parse
_vnf
_interface
(args
.get("vnf_name")),
71 metric
=args
.get("metric"),
72 cookie
=args
.get("cookie"))
74 url
= "{0}/restapi/monitor/flow".format(args
.get("endpoint"))
75 response
= put(url
, params
=params
)
76 pp
.pprint(response
.text
)
78 def stop_flow(self
, args
):
79 params
= self
._create
_dict
(
80 vnf_name
=self
._parse
_vnf
_name
(args
.get("vnf_name")),
81 vnf_interface
=self
._parse
_vnf
_interface
(args
.get("vnf_name")),
82 metric
=args
.get("metric"),
83 cookie
=args
.get("cookie"))
85 url
= "{0}/restapi/monitor/flow".format(args
.get("endpoint"))
86 response
= put(url
, params
=params
)
87 pp
.pprint(response
.text
)
89 def prometheus(self
, args
):
90 # This functions makes it more user-friendly to create the correct prometheus query
91 # <uuid> is replaced by the correct uuid of the deployed vnf container
92 vnf_name
= self
._parse
_vnf
_name
(args
.get("vnf_name"))
93 query
= args
.get("query")
95 vnf_status
= get("%s/restapi/compute/%s/%s" %
96 (args
.get("endpoint"),
97 args
.get("datacenter"),
99 uuid
= vnf_status
['id']
100 query
= query
.replace('<uuid>', uuid
)
102 response
= prometheus
.query_Prometheus(query
)
105 def _parse_vnf_name(self
, vnf_name_str
):
106 vnf_name
= vnf_name_str
.split(':')[0]
109 def _parse_vnf_interface(self
, vnf_name_str
):
111 vnf_interface
= vnf_name_str
.split(':')[1]
112 except BaseException
:
117 def _create_dict(self
, **kwargs
):
121 parser
= argparse
.ArgumentParser(description
='son-emu-cli monitor')
124 choices
=['setup_metric', 'stop_metric',
125 'setup_flow', 'stop_flow', 'prometheus'],
126 help="setup/stop a metric/flow to be monitored or query Prometheus")
128 "--vnf_name", "-vnf", dest
="vnf_name",
129 help="vnf name:interface to be monitored")
131 "--metric", "-m", dest
="metric",
132 help="tx_bytes, rx_bytes, tx_packets, rx_packets")
134 "--cookie", "-c", dest
="cookie",
135 help="flow cookie to monitor")
137 "--query", "-q", dest
="query",
138 help="prometheus query")
140 "--datacenter", "-d", dest
="datacenter",
141 help="Data center where the vnf is deployed")
143 "--endpoint", "-e", dest
="endpoint",
144 default
="http://127.0.0.1:5001",
145 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
149 args
= vars(parser
.parse_args(argv
))
151 c
.execute_command(args
)