blob: 5ce683863e692d098e545e3f1374d0f1950ddd34 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
A R Karthick9313b762016-11-07 13:14:35 -080017import os
A R Karthick19aaf5c2016-11-09 17:47:57 -080018import re
A R Karthick9313b762016-11-07 13:14:35 -080019from SSHTestAgent import SSHTestAgent
20
21class OnosLog(object):
22 CLI_USER = 'karaf'
23 CLI_PASSWD = 'karaf'
24 CLI_PORT = 8101
A.R Karthickdda22062017-02-09 14:39:20 -080025 KARAF_VERSION = os.getenv('KARAF_VERSION', '3.0.8')
A R Karthick1f908202016-11-16 17:32:20 -080026 HOST = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2').split(',')[0]
A R Karthick9313b762016-11-07 13:14:35 -080027 last_snapshot_map = {}
28
A R Karthick973010f2017-02-06 16:41:51 -080029 def __init__(self, host = HOST, log_file = None):
30 if log_file is None:
31 log_file = '/root/onos/apache-karaf-{}/data/log/karaf.log'.format(self.KARAF_VERSION)
32 self.log_file = log_file
A R Karthick9313b762016-11-07 13:14:35 -080033 self.ssh_agent = SSHTestAgent(host = host, user = self.CLI_USER,
34 password = self.CLI_PASSWD, port = self.CLI_PORT)
35 if not OnosLog.last_snapshot_map.has_key(host):
36 OnosLog.last_snapshot_map[host] = []
37
A R Karthick9313b762016-11-07 13:14:35 -080038 @classmethod
39 def get_last_snapshot(cls, host):
40 if cls.last_snapshot_map.has_key(host):
41 return cls.last_snapshot_map[host]
42 return []
43
44 @classmethod
45 def update_last_snapshot(cls, host, res):
46 cls.last_snapshot_map[host] = res
47
A R Karthicke8935c62016-12-08 18:17:17 -080048 def get_log(self, search_terms = None, exception = True, cache_result = True):
A R Karthick9313b762016-11-07 13:14:35 -080049 """Run the command on the test host"""
A R Karthick973010f2017-02-06 16:41:51 -080050 cmd = 'cat {}'.format(self.log_file)
A R Karthick9313b762016-11-07 13:14:35 -080051 st, output = self.ssh_agent.run_cmd(cmd)
52 if st is False:
A R Karthick1f908202016-11-16 17:32:20 -080053 return st, output
A R Karthick19aaf5c2016-11-09 17:47:57 -080054 exception_map = {'Exception' : [] }
A R Karthick9313b762016-11-07 13:14:35 -080055 last_snapshot = self.get_last_snapshot(self.ssh_agent.host)
56 lines = output.splitlines()
57 if search_terms:
58 if type(search_terms) is str:
59 terms = [ search_terms ]
60 else:
A R Karthick19aaf5c2016-11-09 17:47:57 -080061 terms = list(search_terms)
62 if exception is True and 'Exception' not in terms:
63 terms.append('Exception')
A R Karthick9313b762016-11-07 13:14:35 -080064 match_lines = []
65 last_len = len(last_snapshot)
66 for i in xrange(0, len(lines)):
67 if i < last_len and lines[i] in last_snapshot:
68 ##skip lines matching the last snapshot
69 continue
70 for t in terms:
71 if lines[i].find(t) >= 0:
72 match_lines.append(lines[i])
A R Karthick19aaf5c2016-11-09 17:47:57 -080073 if t == 'Exception':
74 exception_map[t] = lines[i+1:i+1+10]
A R Karthick9313b762016-11-07 13:14:35 -080075 output = '\n'.join(match_lines)
A R Karthick928ad622017-01-30 12:18:32 -080076 if len(exception_map['Exception']) > 0:
77 output += '\nException:\n'
78 output += '\n'.join(exception_map['Exception'])
A R Karthick9313b762016-11-07 13:14:35 -080079
80 #update the last snapshot
A R Karthicke8935c62016-12-08 18:17:17 -080081 if cache_result is True:
82 self.update_last_snapshot(self.ssh_agent.host, lines)
A R Karthick9313b762016-11-07 13:14:35 -080083 return st, output
84
A R Karthick19aaf5c2016-11-09 17:47:57 -080085 def search_log_pattern(self, pattern):
86 r_pat = re.compile(pattern)
A R Karthick973010f2017-02-06 16:41:51 -080087 cmd = 'cat {}'.format(self.log_file)
A R Karthick19aaf5c2016-11-09 17:47:57 -080088 st, output = self.ssh_agent.run_cmd(cmd)
89 if st is False:
90 return None
91 return r_pat.findall(output)
92
A R Karthick9313b762016-11-07 13:14:35 -080093if __name__ == '__main__':
94 onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2')
95 onos_log = OnosLog(host = onos)
96 print('Checking for INFO')
97 st, output = onos_log.get_log('INFO')
98 print(st, output)
99 print('\n\nChecking for ERROR\n\n')
100 st, output = onos_log.get_log('ERROR')
101 print(st, output)
102 print('Checking for ERROR and INFO')
103 st, output = onos_log.get_log(('ERROR', 'INFO'))
104 print(st, output)
A R Karthick19aaf5c2016-11-09 17:47:57 -0800105 pat = onos_log.search_log_pattern('ApplicationManager .* Started')
106 if pat:
107 print(pat)
108 else:
109 print('Onos did not start')