forked from NiceRath/zabbix-linuxha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinuxha.py
125 lines (91 loc) · 3.21 KB
/
linuxha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python3
# {{ ansible_managed }}
from subprocess import Popen as subprocess_popen
from subprocess import PIPE as subprocess_pipe
from sys import argv as sys_argv
from re import search as regex_search
from json import dumps as json_dumps
from socket import gethostname
RES_PREFIX = 'res'
LINUXHA_BIN = 'sudo /usr/sbin/crm'
COROSYNC_CMD = 'sudo /usr/sbin/corosync-cmapctl runtime.members | grep status'
QUORUM_CMD = 'sudo /usr/sbin/corosync-quorumtool -s'
ZBX_KEY = '{#LINHA_RES}'
try:
CHECK = sys_argv[1]
try:
RESOURCE = sys_argv[2]
except IndexError:
RESOURCE = None
except IndexError:
raise SystemExit('You must specify the following arguments: 1 => check-type')
if CHECK == 'discover':
cmd = f'{LINUXHA_BIN} resource status'
elif CHECK in ['members', 'members_active']:
cmd = COROSYNC_CMD
elif CHECK in ['votes', 'quorum']:
cmd = QUORUM_CMD
else:
cmd = f"{LINUXHA_BIN} status bynode"
with subprocess_popen([cmd], shell=True, stdout=subprocess_pipe, stderr=subprocess_pipe) as ps:
_stdout, _stderr = ps.communicate()
STDOUT = _stdout.decode('utf-8')
STDERR = _stderr.decode('utf-8')
def output(msg: (str, int)):
print(msg)
raise SystemExit
if CHECK == 'discover':
result = {'data': []}
for line in STDOUT.split('\n'):
res = regex_search(r'\s((%s).*?)(\s|\t)' % RES_PREFIX, line)
if res is not None:
result['data'].append({ZBX_KEY: res.group(1)})
output(json_dumps(result))
elif CHECK == 'resource':
# if one of the hosts is the active node for the given resource
result = 0
for line in STDOUT.split('\n'):
if line.find(RESOURCE) != -1 and (line.find('Started') != -1 or line.find('Master') != -1):
result = 1
break
output(result)
elif CHECK == 'resource_active':
# if the current host is the active node for the given resource
node_start = False
result = 0
for line in STDOUT.split('\n'):
if not node_start and line.find(f'Node {gethostname()}') != -1:
# if own node block begins
node_start = True
continue
if node_start and line.find('Node ') != -1:
# if own node block ended
break
if node_start:
if line.find(RESOURCE) != -1 and (line.find('Started') != -1 or line.find('Master') != -1):
result = 1
break
output(result)
elif CHECK == 'quorum':
for line in STDOUT.split('\n'):
if line.find('Quorate') != -1:
output(1) if line.find('Yes') != -1 else output(0)
break
elif CHECK == 'votes':
votes_expected = 0
votes_now = 0
for line in STDOUT.split('\n'):
if line.find('Expected votes') != -1:
votes_expected = int(line.split(':', 1)[1].strip())
elif line.find('Total votes') != -1:
votes_now = int(line.split(':', 1)[1].strip())
break
output(1) if votes_expected == votes_now else output(0)
elif CHECK == 'members':
# all available members
output(STDOUT.count('runtime.members'))
elif CHECK == 'members_active':
# currently 'online' members
output(STDOUT.count('joined'))
else:
output('No supported check found')