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. |
| 15 | import os |
| 16 | import subprocess |
| 17 | |
| 18 | |
| 19 | def __virtual__(): |
| 20 | ''' |
| 21 | Only load this module if contrail-status or doctrail utility |
| 22 | (in case of containerized contrail version) is available. |
| 23 | ''' |
| 24 | if _is_cmd_available('contrail-status') or _is_cmd_available('doctrail'): |
| 25 | return 'contrail_health' |
| 26 | return False |
| 27 | |
| 28 | |
| 29 | def _is_cmd_available(cmd_name): |
| 30 | try: |
| 31 | with open(os.devnull) as devnull: |
| 32 | subprocess.Popen( |
| 33 | [cmd_name], stdout=devnull, stderr=devnull |
| 34 | ).communicate() |
| 35 | except OSError as e: |
| 36 | if e.errno == os.errno.ENOENT: |
| 37 | return False |
| 38 | return True |
| 39 | |
| 40 | |
| 41 | def get_services_status(): |
| 42 | |
| 43 | if _is_cmd_available('contrail-status'): |
| 44 | status_cmd_list = ['contrail-status'] |
| 45 | else: |
| 46 | status_cmd_list = ['doctrail', 'all', 'contrail-status'] |
| 47 | |
| 48 | cs_out = str(subprocess.check_output(status_cmd_list)) |
| 49 | status_map = {} |
| 50 | |
| 51 | for line in cs_out.split('\n'): |
| 52 | line_list = line.split() |
| 53 | if (not line.startswith("==") and "FOR NODE" not in line and |
| 54 | len(line_list) >= 2): |
| 55 | status_map[line_list[0].split(":")[0]] = line_list[1] |
| 56 | |
| 57 | return status_map |