blob: e876c67207ae0813551c5a21a2524f1a1af3ffad [file] [log] [blame]
A R Karthick9313b762016-11-07 13:14:35 -08001import os
2from SSHTestAgent import SSHTestAgent
3
4class OnosLog(object):
5 CLI_USER = 'karaf'
6 CLI_PASSWD = 'karaf'
7 CLI_PORT = 8101
8 HOST = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2')
9 last_snapshot_map = {}
10
11 def __init__(self, host = HOST):
12 self.ssh_agent = SSHTestAgent(host = host, user = self.CLI_USER,
13 password = self.CLI_PASSWD, port = self.CLI_PORT)
14 if not OnosLog.last_snapshot_map.has_key(host):
15 OnosLog.last_snapshot_map[host] = []
16
17 #should be the last function to call for tearing down the log session
18 def close(self):
19 self.ssh_agent.client.close()
20
21 @classmethod
22 def get_last_snapshot(cls, host):
23 if cls.last_snapshot_map.has_key(host):
24 return cls.last_snapshot_map[host]
25 return []
26
27 @classmethod
28 def update_last_snapshot(cls, host, res):
29 cls.last_snapshot_map[host] = res
30
31 def get_log(self, search_terms = None):
32 """Run the command on the test host"""
33 cmd = 'cat /root/onos/apache-karaf-3.0.5/data/log/karaf.log'
34 st, output = self.ssh_agent.run_cmd(cmd)
35 if st is False:
36 return output
37 last_snapshot = self.get_last_snapshot(self.ssh_agent.host)
38 lines = output.splitlines()
39 if search_terms:
40 if type(search_terms) is str:
41 terms = [ search_terms ]
42 else:
43 terms = search_terms
44 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])
53
54 output = '\n'.join(match_lines)
55
56 #update the last snapshot
57 self.update_last_snapshot(self.ssh_agent.host, lines)
58 return st, output
59
60if __name__ == '__main__':
61 onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2')
62 onos_log = OnosLog(host = onos)
63 print('Checking for INFO')
64 st, output = onos_log.get_log('INFO')
65 print(st, output)
66 print('\n\nChecking for ERROR\n\n')
67 st, output = onos_log.get_log('ERROR')
68 print(st, output)
69 print('Checking for ERROR and INFO')
70 st, output = onos_log.get_log(('ERROR', 'INFO'))
71 print(st, output)
72 onos_log.close()