blob: 6bdef9fe230f7ab483b54ae49e085b42d97eded9 [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
9 HOST = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2')
10 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
18 #should be the last function to call for tearing down the log session
19 def close(self):
20 self.ssh_agent.client.close()
21
22 @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 Karthick19aaf5c2016-11-09 17:47:57 -080032 def get_log(self, search_terms = None, exception = True):
A R Karthick9313b762016-11-07 13:14:35 -080033 """Run the command on the test host"""
34 cmd = 'cat /root/onos/apache-karaf-3.0.5/data/log/karaf.log'
35 st, output = self.ssh_agent.run_cmd(cmd)
36 if st is False:
37 return 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 Karthick19aaf5c2016-11-09 17:47:57 -080060 output += '\n'.join(exception_map['Exception'])
A R Karthick9313b762016-11-07 13:14:35 -080061
62 #update the last snapshot
63 self.update_last_snapshot(self.ssh_agent.host, lines)
64 return st, output
65
A R Karthick19aaf5c2016-11-09 17:47:57 -080066 def search_log_pattern(self, pattern):
67 r_pat = re.compile(pattern)
68 cmd = 'cat /root/onos/apache-karaf-3.0.5/data/log/karaf.log'
69 st, output = self.ssh_agent.run_cmd(cmd)
70 if st is False:
71 return None
72 return r_pat.findall(output)
73
A R Karthick9313b762016-11-07 13:14:35 -080074if __name__ == '__main__':
75 onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2')
76 onos_log = OnosLog(host = onos)
77 print('Checking for INFO')
78 st, output = onos_log.get_log('INFO')
79 print(st, output)
80 print('\n\nChecking for ERROR\n\n')
81 st, output = onos_log.get_log('ERROR')
82 print(st, output)
83 print('Checking for ERROR and INFO')
84 st, output = onos_log.get_log(('ERROR', 'INFO'))
85 print(st, output)
86 onos_log.close()
A R Karthick19aaf5c2016-11-09 17:47:57 -080087 pat = onos_log.search_log_pattern('ApplicationManager .* Started')
88 if pat:
89 print(pat)
90 else:
91 print('Onos did not start')