Debian Codegraph @

masakarimonitors/hostmonitor/consul_check/matrix_helper.py

raw
1 # Copyright(c) 2021 Inspur
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import os
16 import yaml
17
18
19 DEFAULT_SEQUENCE = ['manage', 'tenant', 'storage']
20
21 # matrix is combined by health and actions.
22 # health: [x, x, x], repreasents status of DEFAULT_SEQUENCE.
23 # action, means which actions it will trigge if host health turns into.
24 # action choice: 'recovery'.
25 # 'recovery' means it will trigge one host recovery event.
26 DEFAULT_MATRIX = [
27 {"health": ["up", "up", "up"],
28 "action": []},
29 {"health": ["up", "up", "down"],
30 "action": ["recovery"]},
31 {"health": ["up", "down", "up"],
32 "action": []},
33 {"health": ["up", "down", "down"],
34 "action": ["recovery"]},
35 {"health": ["down", "up", "up"],
36 "action": []},
37 {"health": ["down", "up", "down"],
38 "action": ["recovery"]},
39 {"health": ["down", "down", "up"],
40 "action": []},
41 {"health": ["down", "down", "down"],
42 "action": ["recovery"]},
43 ]
44
45
46 class MatrixManager(object):
47 """Matrix Manager"""
48
49 def __init__(self, CONF):
50 cfg_file = CONF.consul.matrix_config_file
51 matrix_conf = self.load_config(cfg_file)
52 if not matrix_conf:
53 self.sequence = DEFAULT_SEQUENCE
54 self.matrix = DEFAULT_MATRIX
55 else:
56 self.sequence = matrix_conf.get("sequence")
57 self.matrix = matrix_conf.get("matrix")
58
59 self.valid_matrix(self.matrix, self.sequence)
60
61 def load_config(self, cfg_file):
62 if not cfg_file or not os.path.exists(cfg_file):
63 return None
64
65 with open(cfg_file) as f:
66 data = f.read()
67 matrix_conf = yaml.safe_load(data)
68 return matrix_conf
69
70 def get_sequence(self):
71 return self.sequence
72
73 def get_matrix(self):
74 return self.matrix
75
76 def valid_matrix(self, matrix, sequence):
77 pass