blob: 750cd8c96e5a4008be2799ff4bd3a600337d4040 [file] [log] [blame]
Anton Samoylov28ad4fa2018-10-02 14:45:41 +04001#!/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 Samoylov7a8d83c2018-10-11 12:33:36 +040015import logging
Anton Samoylov28ad4fa2018-10-02 14:45:41 +040016import os
17import subprocess
18
19
Anton Samoylov7a8d83c2018-10-11 12:33:36 +040020MODULE_NAME = 'contrail_health'
21LOG = logging.getLogger(__name__)
22
23
Anton Samoylov28ad4fa2018-10-02 14:45:41 +040024def __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 Samoylov7a8d83c2018-10-11 12:33:36 +040030 return MODULE_NAME
Anton Samoylov28ad4fa2018-10-02 14:45:41 +040031 return False
32
33
34def _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
46def get_services_status():
Anton Samoylov7a8d83c2018-10-11 12:33:36 +040047 cs_out = None
Anton Samoylov28ad4fa2018-10-02 14:45:41 +040048
49 if _is_cmd_available('contrail-status'):
Anton Samoylov7a8d83c2018-10-11 12:33:36 +040050 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 Samoylov28ad4fa2018-10-02 14:45:41 +040067
Anton Samoylov28ad4fa2018-10-02 14:45:41 +040068 status_map = {}
69
Anton Samoylov7a8d83c2018-10-11 12:33:36 +040070 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 Samoylov28ad4fa2018-10-02 14:45:41 +040079
80 return status_map