blob: bc25434f8496e5f63d64ea86c4aff14f47403b37 [file] [log] [blame]
A R Karthick9313b762016-11-07 13:14:35 -08001import os
A R Karthick19aaf5c2016-11-09 17:47:57 -08002import re
A R Karthick9313b762016-11-07 13:14:35 -08003from SSHTestAgent import SSHTestAgent
4
5class OnosLog(object):
6 CLI_USER = 'karaf'
7 CLI_PASSWD = 'karaf'
8 CLI_PORT = 8101
A.R Karthickdda22062017-02-09 14:39:20 -08009 KARAF_VERSION = os.getenv('KARAF_VERSION', '3.0.8')
A R Karthick1f908202016-11-16 17:32:20 -080010 HOST = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2').split(',')[0]
A R Karthick9313b762016-11-07 13:14:35 -080011 last_snapshot_map = {}
12
A R Karthick973010f2017-02-06 16:41:51 -080013 def __init__(self, host = HOST, log_file = None):
14 if log_file is None:
15 log_file = '/root/onos/apache-karaf-{}/data/log/karaf.log'.format(self.KARAF_VERSION)
16 self.log_file = log_file
A R Karthick9313b762016-11-07 13:14:35 -080017 self.ssh_agent = SSHTestAgent(host = host, user = self.CLI_USER,
18 password = self.CLI_PASSWD, port = self.CLI_PORT)
19 if not OnosLog.last_snapshot_map.has_key(host):
20 OnosLog.last_snapshot_map[host] = []
21
A R Karthick9313b762016-11-07 13:14:35 -080022 @classmethod
23 def get_last_snapshot(cls, host):
24 if cls.last_snapshot_map.has_key(host):
25 return cls.last_snapshot_map[host]
26 return []
27
28 @classmethod
29 def update_last_snapshot(cls, host, res):
30 cls.last_snapshot_map[host] = res
31
A R Karthicke8935c62016-12-08 18:17:17 -080032 def get_log(self, search_terms = None, exception = True, cache_result = True):
A R Karthick9313b762016-11-07 13:14:35 -080033 """Run the command on the test host"""
A R Karthick973010f2017-02-06 16:41:51 -080034 cmd = 'cat {}'.format(self.log_file)
A R Karthick9313b762016-11-07 13:14:35 -080035 st, output = self.ssh_agent.run_cmd(cmd)
36 if st is False:
A R Karthick1f908202016-11-16 17:32:20 -080037 return st, output
A R Karthick19aaf5c2016-11-09 17:47:57 -080038 exception_map = {'Exception' : [] }
A R Karthick9313b762016-11-07 13:14:35 -080039 last_snapshot = self.get_last_snapshot(self.ssh_agent.host)
40 lines = output.splitlines()
41 if search_terms:
42 if type(search_terms) is str:
43 terms = [ search_terms ]
44 else:
A R Karthick19aaf5c2016-11-09 17:47:57 -080045 terms = list(search_terms)
46 if exception is True and 'Exception' not in terms:
47 terms.append('Exception')
A R Karthick9313b762016-11-07 13:14:35 -080048 match_lines = []
49 last_len = len(last_snapshot)
50 for i in xrange(0, len(lines)):
51 if i < last_len and lines[i] in last_snapshot:
52 ##skip lines matching the last snapshot
53 continue
54 for t in terms:
55 if lines[i].find(t) >= 0:
56 match_lines.append(lines[i])
A R Karthick19aaf5c2016-11-09 17:47:57 -080057 if t == 'Exception':
58 exception_map[t] = lines[i+1:i+1+10]
A R Karthick9313b762016-11-07 13:14:35 -080059 output = '\n'.join(match_lines)
A R Karthick928ad622017-01-30 12:18:32 -080060 if len(exception_map['Exception']) > 0:
61 output += '\nException:\n'
62 output += '\n'.join(exception_map['Exception'])
A R Karthick9313b762016-11-07 13:14:35 -080063
64 #update the last snapshot
A R Karthicke8935c62016-12-08 18:17:17 -080065 if cache_result is True:
66 self.update_last_snapshot(self.ssh_agent.host, lines)
A R Karthick9313b762016-11-07 13:14:35 -080067 return st, output
68
A R Karthick19aaf5c2016-11-09 17:47:57 -080069 def search_log_pattern(self, pattern):
70 r_pat = re.compile(pattern)
A R Karthick973010f2017-02-06 16:41:51 -080071 cmd = 'cat {}'.format(self.log_file)
A R Karthick19aaf5c2016-11-09 17:47:57 -080072 st, output = self.ssh_agent.run_cmd(cmd)
73 if st is False:
74 return None
75 return r_pat.findall(output)
76
A R Karthick9313b762016-11-07 13:14:35 -080077if __name__ == '__main__':
78 onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2')
79 onos_log = OnosLog(host = onos)
80 print('Checking for INFO')
81 st, output = onos_log.get_log('INFO')
82 print(st, output)
83 print('\n\nChecking for ERROR\n\n')
84 st, output = onos_log.get_log('ERROR')
85 print(st, output)
86 print('Checking for ERROR and INFO')
87 st, output = onos_log.get_log(('ERROR', 'INFO'))
88 print(st, output)
A R Karthick19aaf5c2016-11-09 17:47:57 -080089 pat = onos_log.search_log_pattern('ApplicationManager .* Started')
90 if pat:
91 print(pat)
92 else:
93 print('Onos did not start')