A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 1 | import os |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 2 | import re |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 3 | from SSHTestAgent import SSHTestAgent |
| 4 | |
| 5 | class 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 Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 32 | def get_log(self, search_terms = None, exception = True): |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 33 | """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 Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 38 | exception_map = {'Exception' : [] } |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 39 | 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 Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 45 | terms = list(search_terms) |
| 46 | if exception is True and 'Exception' not in terms: |
| 47 | terms.append('Exception') |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 48 | 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 Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 57 | if t == 'Exception': |
| 58 | exception_map[t] = lines[i+1:i+1+10] |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 59 | output = '\n'.join(match_lines) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 60 | output += '\n'.join(exception_map['Exception']) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 61 | |
| 62 | #update the last snapshot |
| 63 | self.update_last_snapshot(self.ssh_agent.host, lines) |
| 64 | return st, output |
| 65 | |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 66 | 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 Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 74 | if __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 Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 87 | pat = onos_log.search_log_pattern('ApplicationManager .* Started') |
| 88 | if pat: |
| 89 | print(pat) |
| 90 | else: |
| 91 | print('Onos did not start') |