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.opnfv_logger 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):
32 _, stdout, _ = ssh_conn.exec_command('hostname')
33 found_host = stdout.readline()
35 except Exception as e:
38 def __dump_to_file(self, operation, host, text, timestamp=None):
39 ts = (timestamp if timestamp is not None
40 else time.strftime("%Y%m%d-%H%M%S"))
41 dumpdir = os.path.join(self.ovs_dir, ts)
42 self.__mkdir_p(dumpdir)
43 fname = '{0}_{1}'.format(operation, host)
44 with open(os.path.join(dumpdir, fname), 'w') as f:
47 def __remote_cmd(self, ssh_conn, cmd):
49 _, stdout, stderr = ssh_conn.exec_command(cmd)
50 errors = stderr.readlines()
52 host = self.__ssh_host(ssh_conn)
53 logger.error(''.join(errors))
54 raise Exception('Could not execute {0} in {1}'
56 output = ''.join(stdout.readlines())
58 except Exception as e:
59 logger.error('[__remote_command(ssh_client, {0})]: {1}'
63 def create_artifact_archive(self):
64 shutil.make_archive(self.ovs_dir,
66 root_dir=os.path.dirname(self.ovs_dir),
67 base_dir=self.ovs_dir)
68 shutil.copy2('{0}.zip'.format(self.ovs_dir), self.ft_resdir)
70 def ofctl_dump_flows(self, ssh_conn, br='br-int',
71 choose_table=None, timestamp=None):
73 cmd = 'ovs-ofctl -OOpenFlow13 dump-flows {0}'.format(br)
74 if choose_table is not None:
75 cmd = '{0} table={1}'.format(cmd, choose_table)
76 output = self.__remote_cmd(ssh_conn, cmd)
77 operation = 'ofctl_dump_flows'
78 host = self.__ssh_host(ssh_conn)
79 self.__dump_to_file(operation, host, output, timestamp=timestamp)
81 except Exception as e:
82 logger.error('[ofctl_dump_flows(ssh_client, {0}, {1})]: {2}'
83 .format(br, choose_table, e))
86 def vsctl_show(self, ssh_conn, timestamp=None):
88 cmd = 'ovs-vsctl show'
89 output = self.__remote_cmd(ssh_conn, cmd)
90 operation = 'vsctl_show'
91 host = self.__ssh_host(ssh_conn)
92 self.__dump_to_file(operation, host, output, timestamp=timestamp)
94 except Exception as e:
95 logger.error('[vsctl_show(ssh_client)]: {0}'.format(e))
98 def dump_ovs_logs(self, controller_clients, compute_clients,
99 related_error=None, timestamp=None):
101 delete controller_clients argument because
102 that was producing an error in XCI installer.
104 For more information see:
105 TODO: https://jira.opnfv.org/browse/RELENG-314
107 del controller_clients
108 if timestamp is None:
109 timestamp = time.strftime("%Y%m%d-%H%M%S")
110 # clients = controller_clients + compute_clients
111 clients = compute_clients
112 for client in clients:
113 self.ofctl_dump_flows(client, timestamp=timestamp)
114 self.vsctl_show(client, timestamp=timestamp)
116 if related_error is not None:
117 dumpdir = os.path.join(self.ovs_dir, timestamp)
118 self.__mkdir_p(dumpdir)
119 with open(os.path.join(dumpdir, 'error'), 'w') as f:
120 f.write(related_error)