Anton Samoylov | 28ad4fa | 2018-10-02 14:45:41 +0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2018 Mirantis, Inc. |
| 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. |
Anton Samoylov | 7a8d83c | 2018-10-11 12:33:36 +0400 | [diff] [blame] | 15 | import logging |
Anton Samoylov | 28ad4fa | 2018-10-02 14:45:41 +0400 | [diff] [blame] | 16 | import os |
| 17 | import subprocess |
| 18 | |
| 19 | |
Anton Samoylov | 7a8d83c | 2018-10-11 12:33:36 +0400 | [diff] [blame] | 20 | MODULE_NAME = 'contrail_health' |
| 21 | LOG = logging.getLogger(__name__) |
| 22 | |
| 23 | |
Anton Samoylov | 28ad4fa | 2018-10-02 14:45:41 +0400 | [diff] [blame] | 24 | def __virtual__(): |
| 25 | ''' |
| 26 | Only load this module if contrail-status or doctrail utility |
| 27 | (in case of containerized contrail version) is available. |
| 28 | ''' |
| 29 | if _is_cmd_available('contrail-status') or _is_cmd_available('doctrail'): |
Anton Samoylov | 7a8d83c | 2018-10-11 12:33:36 +0400 | [diff] [blame] | 30 | return MODULE_NAME |
Anton Samoylov | 28ad4fa | 2018-10-02 14:45:41 +0400 | [diff] [blame] | 31 | return False |
| 32 | |
| 33 | |
| 34 | def _is_cmd_available(cmd_name): |
| 35 | try: |
| 36 | with open(os.devnull) as devnull: |
| 37 | subprocess.Popen( |
| 38 | [cmd_name], stdout=devnull, stderr=devnull |
| 39 | ).communicate() |
| 40 | except OSError as e: |
| 41 | if e.errno == os.errno.ENOENT: |
| 42 | return False |
| 43 | return True |
| 44 | |
| 45 | |
| 46 | def get_services_status(): |
Anton Samoylov | 7a8d83c | 2018-10-11 12:33:36 +0400 | [diff] [blame] | 47 | cs_out = None |
Anton Samoylov | 28ad4fa | 2018-10-02 14:45:41 +0400 | [diff] [blame] | 48 | |
| 49 | if _is_cmd_available('contrail-status'): |
Anton Samoylov | 7a8d83c | 2018-10-11 12:33:36 +0400 | [diff] [blame] | 50 | LOG.info('Trying to get status of contrail services ' |
| 51 | 'using contrail-status utility on host ...') |
| 52 | try: |
| 53 | cs_out = str(subprocess.check_output(['contrail-status'])) |
| 54 | except subprocess.CalledProcessError as e: |
| 55 | LOG.warn('Status of contrail services cannot be checked ' |
| 56 | 'by contrail-status utility from host') |
| 57 | if cs_out is None and _is_cmd_available('doctrail'): |
| 58 | LOG.info('Trying to get status of contrail services inside containers ' |
| 59 | 'using doctrail utility ...') |
| 60 | try: |
| 61 | cs_out = str(subprocess.check_output( |
| 62 | ['doctrail', 'all', 'contrail-status']) |
| 63 | ) |
| 64 | except subprocess.CalledProcessError as e: |
| 65 | LOG.warn('Status of contrail services inside containers cannot ' |
| 66 | 'be checked by contrail-status utility via doctrail cmd') |
Anton Samoylov | 28ad4fa | 2018-10-02 14:45:41 +0400 | [diff] [blame] | 67 | |
Anton Samoylov | 28ad4fa | 2018-10-02 14:45:41 +0400 | [diff] [blame] | 68 | status_map = {} |
| 69 | |
Anton Samoylov | 7a8d83c | 2018-10-11 12:33:36 +0400 | [diff] [blame] | 70 | if cs_out: |
| 71 | for line in cs_out.split('\n'): |
| 72 | line_list = line.split() |
| 73 | if (not line.startswith("==") and "FOR NODE" not in line and |
| 74 | len(line_list) >= 2): |
| 75 | status_map[line_list[0].split(":")[0]] = line_list[1] |
| 76 | else: |
| 77 | LOG.error('Status of contrail services cannot be checked ' |
| 78 | 'by {0} module.'.format(MODULE_NAME)) |
Anton Samoylov | 28ad4fa | 2018-10-02 14:45:41 +0400 | [diff] [blame] | 79 | |
| 80 | return status_map |