1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # Author: George Paraskevopoulos (geopar@intracom-telecom.com)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 import opnfv.utils.OPNFVLogger as OPNFVLogger
15 logger = OPNFVLogger.Logger('ovs_logger').getLogger()
18 class OVSLogger(object):
20 def __init__(self, basedir, ft_resdir):
21 self.ovs_dir = basedir
22 self.ft_resdir = ft_resdir
23 self.__mkdir_p(self.ovs_dir)
24 self.__mkdir_p(self.ft_resdir)
26 def __mkdir_p(self, dirpath):
27 if not os.path.exists(dirpath):
30 def __ssh_host(self, ssh_conn, host_prefix='10.20.0'):
32 _, stdout, _ = ssh_conn.exec_command('hostname -I')
33 hosts = stdout.readline().strip().split(' ')
34 found_host = [h for h in hosts if h.startswith(host_prefix)][0]
36 except Exception as e:
39 def __dump_to_file(self, operation, host, text, timestamp=None):
40 ts = (timestamp if timestamp is not None
41 else time.strftime("%Y%m%d-%H%M%S"))
42 dumpdir = os.path.join(self.ovs_dir, ts)
43 self.__mkdir_p(dumpdir)
44 fname = '{0}_{1}'.format(operation, host)
45 with open(os.path.join(dumpdir, fname), 'w') as f:
48 def __remote_cmd(self, ssh_conn, cmd):
50 _, stdout, stderr = ssh_conn.exec_command(cmd)
51 errors = stderr.readlines()
53 host = self.__ssh_host(ssh_conn)
54 logger.error(''.join(errors))
55 raise Exception('Could not execute {0} in {1}'
57 output = ''.join(stdout.readlines())
59 except Exception as e:
60 logger.error('[__remote_command(ssh_client, {0})]: {1}'
64 def create_artifact_archive(self):
65 shutil.make_archive(self.ovs_dir,
67 root_dir=os.path.dirname(self.ovs_dir),
68 base_dir=self.ovs_dir)
69 shutil.copy2('{0}.zip'.format(self.ovs_dir), self.ft_resdir)
71 def ofctl_dump_flows(self, ssh_conn, br='br-int',
72 choose_table=None, timestamp=None):
74 cmd = 'ovs-ofctl -OOpenFlow13 dump-flows {0}'.format(br)
75 if choose_table is not None:
76 cmd = '{0} table={1}'.format(cmd, choose_table)
77 output = self.__remote_cmd(ssh_conn, cmd)
78 operation = 'ofctl_dump_flows'
79 host = self.__ssh_host(ssh_conn)
80 self.__dump_to_file(operation, host, output, timestamp=timestamp)
82 except Exception as e:
83 logger.error('[ofctl_dump_flows(ssh_client, {0}, {1})]: {2}'
84 .format(br, choose_table, e))
87 def vsctl_show(self, ssh_conn, timestamp=None):
89 cmd = 'ovs-vsctl show'
90 output = self.__remote_cmd(ssh_conn, cmd)
91 operation = 'vsctl_show'
92 host = self.__ssh_host(ssh_conn)
93 self.__dump_to_file(operation, host, output, timestamp=timestamp)
95 except Exception as e:
96 logger.error('[vsctl_show(ssh_client)]: {0}'.format(e))
99 def dump_ovs_logs(self, controller_clients, compute_clients,
100 related_error=None, timestamp=None):
101 if timestamp is None:
102 timestamp = time.strftime("%Y%m%d-%H%M%S")
104 for controller_client in controller_clients:
105 self.ofctl_dump_flows(controller_client,
107 self.vsctl_show(controller_client,
110 for compute_client in compute_clients:
111 self.ofctl_dump_flows(compute_client,
113 self.vsctl_show(compute_client,
116 if related_error is not None:
117 dumpdir = os.path.join(self.ovs_dir, timestamp)
118 with open(os.path.join(dumpdir, 'error'), 'w') as f:
119 f.write(related_error)