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 | dda2206 | 2017-02-09 14:39:20 -0800 | [diff] [blame] | 9 | KARAF_VERSION = os.getenv('KARAF_VERSION', '3.0.8') |
A R Karthick | 1f90820 | 2016-11-16 17:32:20 -0800 | [diff] [blame] | 10 | 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] | 11 | last_snapshot_map = {} |
| 12 | |
A R Karthick | 973010f | 2017-02-06 16:41:51 -0800 | [diff] [blame] | 13 | def __init__(self, host = HOST, log_file = None): |
| 14 | if log_file is None: |
| 15 | log_file = '/root/onos/apache-karaf-{}/data/log/karaf.log'.format(self.KARAF_VERSION) |
| 16 | self.log_file = log_file |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 17 | self.ssh_agent = SSHTestAgent(host = host, user = self.CLI_USER, |
| 18 | password = self.CLI_PASSWD, port = self.CLI_PORT) |
| 19 | if not OnosLog.last_snapshot_map.has_key(host): |
| 20 | OnosLog.last_snapshot_map[host] = [] |
| 21 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 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 | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 32 | 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] | 33 | """Run the command on the test host""" |
A R Karthick | 973010f | 2017-02-06 16:41:51 -0800 | [diff] [blame] | 34 | cmd = 'cat {}'.format(self.log_file) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 35 | st, output = self.ssh_agent.run_cmd(cmd) |
| 36 | if st is False: |
A R Karthick | 1f90820 | 2016-11-16 17:32:20 -0800 | [diff] [blame] | 37 | return st, 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 | 928ad62 | 2017-01-30 12:18:32 -0800 | [diff] [blame] | 60 | if len(exception_map['Exception']) > 0: |
| 61 | output += '\nException:\n' |
| 62 | output += '\n'.join(exception_map['Exception']) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 63 | |
| 64 | #update the last snapshot |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 65 | if cache_result is True: |
| 66 | self.update_last_snapshot(self.ssh_agent.host, lines) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 67 | return st, output |
| 68 | |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 69 | def search_log_pattern(self, pattern): |
| 70 | r_pat = re.compile(pattern) |
A R Karthick | 973010f | 2017-02-06 16:41:51 -0800 | [diff] [blame] | 71 | cmd = 'cat {}'.format(self.log_file) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 72 | st, output = self.ssh_agent.run_cmd(cmd) |
| 73 | if st is False: |
| 74 | return None |
| 75 | return r_pat.findall(output) |
| 76 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 77 | if __name__ == '__main__': |
| 78 | onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2') |
| 79 | onos_log = OnosLog(host = onos) |
| 80 | print('Checking for INFO') |
| 81 | st, output = onos_log.get_log('INFO') |
| 82 | print(st, output) |
| 83 | print('\n\nChecking for ERROR\n\n') |
| 84 | st, output = onos_log.get_log('ERROR') |
| 85 | print(st, output) |
| 86 | print('Checking for ERROR and INFO') |
| 87 | st, output = onos_log.get_log(('ERROR', 'INFO')) |
| 88 | print(st, output) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 89 | pat = onos_log.search_log_pattern('ApplicationManager .* Started') |
| 90 | if pat: |
| 91 | print(pat) |
| 92 | else: |
| 93 | print('Onos did not start') |