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 | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 56 | output += '\n'.join(exception_map['Exception']) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 57 | |
| 58 | #update the last snapshot |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 59 | if cache_result is True: |
| 60 | self.update_last_snapshot(self.ssh_agent.host, lines) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 61 | return st, output |
| 62 | |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 63 | def search_log_pattern(self, pattern): |
| 64 | r_pat = re.compile(pattern) |
| 65 | cmd = 'cat /root/onos/apache-karaf-3.0.5/data/log/karaf.log' |
| 66 | st, output = self.ssh_agent.run_cmd(cmd) |
| 67 | if st is False: |
| 68 | return None |
| 69 | return r_pat.findall(output) |
| 70 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 71 | if __name__ == '__main__': |
| 72 | onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2') |
| 73 | onos_log = OnosLog(host = onos) |
| 74 | print('Checking for INFO') |
| 75 | st, output = onos_log.get_log('INFO') |
| 76 | print(st, output) |
| 77 | print('\n\nChecking for ERROR\n\n') |
| 78 | st, output = onos_log.get_log('ERROR') |
| 79 | print(st, output) |
| 80 | print('Checking for ERROR and INFO') |
| 81 | st, output = onos_log.get_log(('ERROR', 'INFO')) |
| 82 | print(st, output) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 83 | pat = onos_log.search_log_pattern('ApplicationManager .* Started') |
| 84 | if pat: |
| 85 | print(pat) |
| 86 | else: |
| 87 | print('Onos did not start') |