Modify the timed trigger on zte-pod1
[releng.git] / modules / opnfv / utils / ovs_logger.py
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 ##############################################################################
9
10 import opnfv.utils.opnfv_logger as OPNFVLogger
11 import os
12 import time
13 import shutil
14
15 logger = OPNFVLogger.Logger('ovs_logger').getLogger()
16
17
18 class OVSLogger(object):
19
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)
25
26     def __mkdir_p(self, dirpath):
27         if not os.path.exists(dirpath):
28             os.makedirs(dirpath)
29
30     def __ssh_host(self, ssh_conn, host_prefix='10.20.0'):
31         try:
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]
35             return found_host
36         except Exception as e:
37             logger.error(e)
38
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:
46             f.write(text)
47
48     def __remote_cmd(self, ssh_conn, cmd):
49         try:
50             _, stdout, stderr = ssh_conn.exec_command(cmd)
51             errors = stderr.readlines()
52             if len(errors) > 0:
53                 host = self.__ssh_host(ssh_conn)
54                 logger.error(''.join(errors))
55                 raise Exception('Could not execute {0} in {1}'
56                                 .format(cmd, host))
57             output = ''.join(stdout.readlines())
58             return output
59         except Exception as e:
60             logger.error('[__remote_command(ssh_client, {0})]: {1}'
61                          .format(cmd, e))
62             return None
63
64     def create_artifact_archive(self):
65         shutil.make_archive(self.ovs_dir,
66                             'zip',
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)
70
71     def ofctl_dump_flows(self, ssh_conn, br='br-int',
72                          choose_table=None, timestamp=None):
73         try:
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)
81             return output
82         except Exception as e:
83             logger.error('[ofctl_dump_flows(ssh_client, {0}, {1})]: {2}'
84                          .format(br, choose_table, e))
85             return None
86
87     def vsctl_show(self, ssh_conn, timestamp=None):
88         try:
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)
94             return output
95         except Exception as e:
96             logger.error('[vsctl_show(ssh_client)]: {0}'.format(e))
97             return None
98
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")
103
104         clients = controller_clients + compute_clients
105         for client in clients:
106             self.ofctl_dump_flows(client, timestamp=timestamp)
107             self.vsctl_show(client, timestamp=timestamp)
108
109         if related_error is not None:
110             dumpdir = os.path.join(self.ovs_dir, timestamp)
111             self.__mkdir_p(dumpdir)
112             with open(os.path.join(dumpdir, 'error'), 'w') as f:
113                 f.write(related_error)