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 |
A R Karthick | 1f90820 | 2016-11-16 17:32:20 -0800 | [diff] [blame] | 9 | HOST = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2').split(',')[0] |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 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 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 18 | @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 Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 28 | def get_log(self, search_terms = None, exception = True, cache_result = True): |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 29 | """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 Karthick | 1f90820 | 2016-11-16 17:32:20 -0800 | [diff] [blame] | 33 | return st, output |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 34 | exception_map = {'Exception' : [] } |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 35 | 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 Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 41 | terms = list(search_terms) |
| 42 | if exception is True and 'Exception' not in terms: |
| 43 | terms.append('Exception') |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 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]) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 53 | if t == 'Exception': |
| 54 | exception_map[t] = lines[i+1:i+1+10] |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 55 | output = '\n'.join(match_lines) |
A R Karthick | 928ad62 | 2017-01-30 12:18:32 -0800 | [diff] [blame] | 56 | if len(exception_map['Exception']) > 0: |
| 57 | output += '\nException:\n' |
| 58 | output += '\n'.join(exception_map['Exception']) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 59 | |
| 60 | #update the last snapshot |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 61 | if cache_result is True: |
| 62 | self.update_last_snapshot(self.ssh_agent.host, lines) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 63 | return st, output |
| 64 | |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 65 | 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 Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 73 | if __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 Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 85 | pat = onos_log.search_log_pattern('ApplicationManager .* Started') |
| 86 | if pat: |
| 87 | print(pat) |
| 88 | else: |
| 89 | print('Onos did not start') |