blob: 0a31b80039da73ebf6c1c2e43a2bd1f668aa55af [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 Karthick1f908202016-11-16 17:32:20 -08009 HOST = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2').split(',')[0]
A R Karthick9313b762016-11-07 13:14:35 -080010 last_snapshot_map = {}
11
12 def __init__(self, host = HOST):
13 self.ssh_agent = SSHTestAgent(host = host, user = self.CLI_USER,
14 password = self.CLI_PASSWD, port = self.CLI_PORT)
15 if not OnosLog.last_snapshot_map.has_key(host):
16 OnosLog.last_snapshot_map[host] = []
17
A R Karthick9313b762016-11-07 13:14:35 -080018 @classmethod
19 def get_last_snapshot(cls, host):
20 if cls.last_snapshot_map.has_key(host):
21 return cls.last_snapshot_map[host]
22 return []
23
24 @classmethod
25 def update_last_snapshot(cls, host, res):
26 cls.last_snapshot_map[host] = res
27
A R Karthicke8935c62016-12-08 18:17:17 -080028 def get_log(self, search_terms = None, exception = True, cache_result = True):
A R Karthick9313b762016-11-07 13:14:35 -080029 """Run the command on the test host"""
30 cmd = 'cat /root/onos/apache-karaf-3.0.5/data/log/karaf.log'
31 st, output = self.ssh_agent.run_cmd(cmd)
32 if st is False:
A R Karthick1f908202016-11-16 17:32:20 -080033 return st, output
A R Karthick19aaf5c2016-11-09 17:47:57 -080034 exception_map = {'Exception' : [] }
A R Karthick9313b762016-11-07 13:14:35 -080035 last_snapshot = self.get_last_snapshot(self.ssh_agent.host)
36 lines = output.splitlines()
37 if search_terms:
38 if type(search_terms) is str:
39 terms = [ search_terms ]
40 else:
A R Karthick19aaf5c2016-11-09 17:47:57 -080041 terms = list(search_terms)
42 if exception is True and 'Exception' not in terms:
43 terms.append('Exception')
A R Karthick9313b762016-11-07 13:14:35 -080044 match_lines = []
45 last_len = len(last_snapshot)
46 for i in xrange(0, len(lines)):
47 if i < last_len and lines[i] in last_snapshot:
48 ##skip lines matching the last snapshot
49 continue
50 for t in terms:
51 if lines[i].find(t) >= 0:
52 match_lines.append(lines[i])
A R Karthick19aaf5c2016-11-09 17:47:57 -080053 if t == 'Exception':
54 exception_map[t] = lines[i+1:i+1+10]
A R Karthick9313b762016-11-07 13:14:35 -080055 output = '\n'.join(match_lines)
A R Karthick928ad622017-01-30 12:18:32 -080056 if len(exception_map['Exception']) > 0:
57 output += '\nException:\n'
58 output += '\n'.join(exception_map['Exception'])
A R Karthick9313b762016-11-07 13:14:35 -080059
60 #update the last snapshot
A R Karthicke8935c62016-12-08 18:17:17 -080061 if cache_result is True:
62 self.update_last_snapshot(self.ssh_agent.host, lines)
A R Karthick9313b762016-11-07 13:14:35 -080063 return st, output
64
A R Karthick19aaf5c2016-11-09 17:47:57 -080065 def search_log_pattern(self, pattern):
66 r_pat = re.compile(pattern)
67 cmd = 'cat /root/onos/apache-karaf-3.0.5/data/log/karaf.log'
68 st, output = self.ssh_agent.run_cmd(cmd)
69 if st is False:
70 return None
71 return r_pat.findall(output)
72
A R Karthick9313b762016-11-07 13:14:35 -080073if __name__ == '__main__':
74 onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2')
75 onos_log = OnosLog(host = onos)
76 print('Checking for INFO')
77 st, output = onos_log.get_log('INFO')
78 print(st, output)
79 print('\n\nChecking for ERROR\n\n')
80 st, output = onos_log.get_log('ERROR')
81 print(st, output)
82 print('Checking for ERROR and INFO')
83 st, output = onos_log.get_log(('ERROR', 'INFO'))
84 print(st, output)
A R Karthick19aaf5c2016-11-09 17:47:57 -080085 pat = onos_log.search_log_pattern('ApplicationManager .* Started')
86 if pat:
87 print(pat)
88 else:
89 print('Onos did not start')