Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 17 | import os |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 18 | import re |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 19 | from SSHTestAgent import SSHTestAgent |
| 20 | |
| 21 | class OnosLog(object): |
| 22 | CLI_USER = 'karaf' |
| 23 | CLI_PASSWD = 'karaf' |
| 24 | CLI_PORT = 8101 |
A.R Karthick | dda2206 | 2017-02-09 14:39:20 -0800 | [diff] [blame] | 25 | KARAF_VERSION = os.getenv('KARAF_VERSION', '3.0.8') |
A R Karthick | 1f90820 | 2016-11-16 17:32:20 -0800 | [diff] [blame] | 26 | 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] | 27 | last_snapshot_map = {} |
| 28 | |
A R Karthick | 973010f | 2017-02-06 16:41:51 -0800 | [diff] [blame] | 29 | def __init__(self, host = HOST, log_file = None): |
| 30 | if log_file is None: |
| 31 | log_file = '/root/onos/apache-karaf-{}/data/log/karaf.log'.format(self.KARAF_VERSION) |
| 32 | self.log_file = log_file |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 33 | self.ssh_agent = SSHTestAgent(host = host, user = self.CLI_USER, |
| 34 | password = self.CLI_PASSWD, port = self.CLI_PORT) |
| 35 | if not OnosLog.last_snapshot_map.has_key(host): |
| 36 | OnosLog.last_snapshot_map[host] = [] |
| 37 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 38 | @classmethod |
| 39 | def get_last_snapshot(cls, host): |
| 40 | if cls.last_snapshot_map.has_key(host): |
| 41 | return cls.last_snapshot_map[host] |
| 42 | return [] |
| 43 | |
| 44 | @classmethod |
| 45 | def update_last_snapshot(cls, host, res): |
| 46 | cls.last_snapshot_map[host] = res |
| 47 | |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 48 | 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] | 49 | """Run the command on the test host""" |
A R Karthick | 973010f | 2017-02-06 16:41:51 -0800 | [diff] [blame] | 50 | cmd = 'cat {}'.format(self.log_file) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 51 | st, output = self.ssh_agent.run_cmd(cmd) |
| 52 | if st is False: |
A R Karthick | 1f90820 | 2016-11-16 17:32:20 -0800 | [diff] [blame] | 53 | return st, output |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 54 | exception_map = {'Exception' : [] } |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 55 | last_snapshot = self.get_last_snapshot(self.ssh_agent.host) |
| 56 | lines = output.splitlines() |
| 57 | if search_terms: |
| 58 | if type(search_terms) is str: |
| 59 | terms = [ search_terms ] |
| 60 | else: |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 61 | terms = list(search_terms) |
| 62 | if exception is True and 'Exception' not in terms: |
| 63 | terms.append('Exception') |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 64 | match_lines = [] |
| 65 | last_len = len(last_snapshot) |
| 66 | for i in xrange(0, len(lines)): |
| 67 | if i < last_len and lines[i] in last_snapshot: |
| 68 | ##skip lines matching the last snapshot |
| 69 | continue |
| 70 | for t in terms: |
| 71 | if lines[i].find(t) >= 0: |
| 72 | match_lines.append(lines[i]) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 73 | if t == 'Exception': |
| 74 | exception_map[t] = lines[i+1:i+1+10] |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 75 | output = '\n'.join(match_lines) |
A R Karthick | 928ad62 | 2017-01-30 12:18:32 -0800 | [diff] [blame] | 76 | if len(exception_map['Exception']) > 0: |
| 77 | output += '\nException:\n' |
| 78 | output += '\n'.join(exception_map['Exception']) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 79 | |
| 80 | #update the last snapshot |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 81 | if cache_result is True: |
| 82 | self.update_last_snapshot(self.ssh_agent.host, lines) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 83 | return st, output |
| 84 | |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 85 | def search_log_pattern(self, pattern): |
| 86 | r_pat = re.compile(pattern) |
A R Karthick | 973010f | 2017-02-06 16:41:51 -0800 | [diff] [blame] | 87 | cmd = 'cat {}'.format(self.log_file) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 88 | st, output = self.ssh_agent.run_cmd(cmd) |
| 89 | if st is False: |
| 90 | return None |
| 91 | return r_pat.findall(output) |
| 92 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 93 | if __name__ == '__main__': |
| 94 | onos = os.getenv('ONOS_CONTROLLER_IP', '172.17.0.2') |
| 95 | onos_log = OnosLog(host = onos) |
| 96 | print('Checking for INFO') |
| 97 | st, output = onos_log.get_log('INFO') |
| 98 | print(st, output) |
| 99 | print('\n\nChecking for ERROR\n\n') |
| 100 | st, output = onos_log.get_log('ERROR') |
| 101 | print(st, output) |
| 102 | print('Checking for ERROR and INFO') |
| 103 | st, output = onos_log.get_log(('ERROR', 'INFO')) |
| 104 | print(st, output) |
A R Karthick | 19aaf5c | 2016-11-09 17:47:57 -0800 | [diff] [blame] | 105 | pat = onos_log.search_log_pattern('ApplicationManager .* Started') |
| 106 | if pat: |
| 107 | print(pat) |
| 108 | else: |
| 109 | print('Onos did not start') |