blob: f5dcb77b06837e7193c69ec32ec5a97418c1d61b [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 Karthick19aaf5c2016-11-09 17:47:57 -080028 def get_log(self, search_terms = None, exception = 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 Karthick19aaf5c2016-11-09 17:47:57 -080056 output += '\n'.join(exception_map['Exception'])
A R Karthick9313b762016-11-07 13:14:35 -080057
58 #update the last snapshot
59 self.update_last_snapshot(self.ssh_agent.host, lines)
60 return st, output
61
A R Karthick19aaf5c2016-11-09 17:47:57 -080062 def search_log_pattern(self, pattern):
63 r_pat = re.compile(pattern)
64 cmd = 'cat /root/onos/apache-karaf-3.0.5/data/log/karaf.log'
65 st, output = self.ssh_agent.run_cmd(cmd)
66 if st is False:
67 return None
68 return r_pat.findall(output)
69
A R Karthick9313b762016-11-07 13:14:35 -080070if __name__ == '__main__':
71 onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2')
72 onos_log = OnosLog(host = onos)
73 print('Checking for INFO')
74 st, output = onos_log.get_log('INFO')
75 print(st, output)
76 print('\n\nChecking for ERROR\n\n')
77 st, output = onos_log.get_log('ERROR')
78 print(st, output)
79 print('Checking for ERROR and INFO')
80 st, output = onos_log.get_log(('ERROR', 'INFO'))
81 print(st, output)
A R Karthick19aaf5c2016-11-09 17:47:57 -080082 pat = onos_log.search_log_pattern('ApplicationManager .* Started')
83 if pat:
84 print(pat)
85 else:
86 print('Onos did not start')