Merge remote-tracking branch 'upstream/master'
[osm/vim-emu.git] / src / emuvim / cli / monitor.py
1 """
2 son-emu network CLI
3 (c) 2016 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5
6 import argparse
7 import pprint
8 from tabulate import tabulate
9 import zerorpc
10 import time
11
12
13 pp = pprint.PrettyPrinter(indent=4)
14
15 class ZeroRpcClient(object):
16
17 def __init__(self):
18 self.c = zerorpc.Client()
19 # TODO connect to DCNetwork API
20 #self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
21 self.c.connect("tcp://127.0.0.1:5151")
22 self.cmds = {}
23
24 def execute_command(self, args):
25 if getattr(self, args["command"]) is not None:
26 # call the local method with the same name as the command arg
27 getattr(self, args["command"])(args)
28 else:
29 print "Command not implemented."
30
31 def setup_metric(self, args):
32 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
33 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
34 r = self.c.setup_metric(
35 vnf_name,
36 vnf_interface,
37 args.get("metric"))
38 pp.pprint(r)
39 '''
40 self.c.monitor_setup_rate_measurement(
41 vnf_name,
42 vnf_interface,
43 args.get("metric"))
44 while True:
45 r = self.c.monitor_get_rate(
46 vnf_name,
47 vnf_interface,
48 args.get("metric"))
49 pp.pprint(r)
50 time.sleep(1)
51 '''
52
53 def _parse_vnf_name(self, vnf_name_str):
54 vnf_name = vnf_name_str.split(':')[0]
55 return vnf_name
56
57 def _parse_vnf_interface(self, vnf_name_str):
58 try:
59 vnf_interface = vnf_name_str.split(':')[1]
60 except:
61 vnf_interface = None
62
63 return vnf_interface
64
65 parser = argparse.ArgumentParser(description='son-emu network')
66 parser.add_argument(
67 "command",
68 help="Action to be executed: get_rate")
69 parser.add_argument(
70 "--vnf_name", "-vnf", dest="vnf_name",
71 help="vnf name to be monitored")
72 parser.add_argument(
73 "--metric", "-m", dest="metric",
74 help="tx_bytes, rx_bytes, tx_packets, rx_packets")
75
76 def main(argv):
77 #print "This is the son-emu monitor CLI."
78 #print "Arguments: %s" % str(argv)
79 args = vars(parser.parse_args(argv))
80 c = ZeroRpcClient()
81 c.execute_command(args)