9620a98aca368e7521edf077f87e9bcba8729c30
[osm/vim-emu.git] / src / emuvim / cli / 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 import argparse
30 import pprint
31 import zerorpc
32 from emuvim.cli import prometheus
33
34 pp = pprint.PrettyPrinter(indent=4)
35
36 class ZeroRpcClient(object):
37
38 def __init__(self):
39 # network zerorpc
40 self.c = zerorpc.Client()
41 # TODO connect to DCNetwork API
42 #self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
43 self.c.connect("tcp://127.0.0.1:5151")
44
45 # compute zerorpc
46 self.compute_api = zerorpc.Client(heartbeat=None, timeout=120) # heartbeat=None, timeout=120
47 self.compute_api.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
48
49 self.cmds = {}
50
51 def execute_command(self, args):
52 if getattr(self, args["command"]) is not None:
53 # call the local method with the same name as the command arg
54 getattr(self, args["command"])(args)
55 else:
56 print("Command not implemented.")
57
58 def setup_metric(self, args):
59 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
60 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
61 r = self.c.setup_metric(
62 vnf_name,
63 vnf_interface,
64 args.get("metric"))
65 pp.pprint(r)
66
67 def stop_metric(self, args):
68 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
69 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
70 r = self.c.stop_metric(
71 vnf_name,
72 vnf_interface,
73 args.get("metric"))
74 pp.pprint(r)
75
76 def setup_flow(self, args):
77 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
78 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
79 r = self.c.setup_flow(
80 vnf_name,
81 vnf_interface,
82 args.get("metric"),
83 args.get("cookie"))
84 pp.pprint(r)
85
86 def stop_flow(self, args):
87 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
88 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
89 r = self.c.stop_flow(
90 vnf_name,
91 vnf_interface,
92 args.get("metric"),
93 args.get("cookie"))
94 pp.pprint(r)
95
96 def prometheus_zrpc(self, args):
97 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
98 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
99 r = self.c.prometheus(
100 args.get("datacenter"),
101 vnf_name,
102 vnf_interface,
103 args.get("query"))
104 pp.pprint(r)
105
106 def prometheus(self, args):
107 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
108 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
109 dc_label = args.get("datacenter")
110 query = args.get("query")
111 vnf_status = self.compute_api.compute_status(dc_label, vnf_name)
112 uuid = vnf_status['id']
113 query = query.replace('<uuid>', uuid)
114
115 r = prometheus.query_Prometheus(query)
116 pp.pprint(r)
117
118
119 def _parse_vnf_name(self, vnf_name_str):
120 vnf_name = vnf_name_str.split(':')[0]
121 return vnf_name
122
123 def _parse_vnf_interface(self, vnf_name_str):
124 try:
125 vnf_interface = vnf_name_str.split(':')[1]
126 except:
127 vnf_interface = None
128
129 return vnf_interface
130
131 parser = argparse.ArgumentParser(description='son-emu monitor')
132 parser.add_argument(
133 "command",
134 choices=['setup_metric', 'stop_metric', 'setup_flow', 'stop_flow','prometheus'],
135 help="setup/stop a metric/flow to be monitored or query Prometheus")
136 parser.add_argument(
137 "--vnf_name", "-vnf", dest="vnf_name",
138 help="vnf name:interface to be monitored")
139 parser.add_argument(
140 "--metric", "-m", dest="metric",
141 help="tx_bytes, rx_bytes, tx_packets, rx_packets")
142 parser.add_argument(
143 "--cookie", "-c", dest="cookie",
144 help="flow cookie to monitor")
145 parser.add_argument(
146 "--query", "-q", dest="query",
147 help="prometheus query")
148 parser.add_argument(
149 "--datacenter", "-d", dest="datacenter",
150 help="Data center where the vnf is deployed")
151
152 def main(argv):
153 #print "This is the son-emu monitor CLI."
154 #print "Arguments: %s" % str(argv)
155 args = vars(parser.parse_args(argv))
156 c = ZeroRpcClient()
157 c.execute_command(args)