This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlusterPeerStatus.py
94 lines (76 loc) · 2.73 KB
/
GlusterPeerStatus.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
import json
import logging
import platform
import sys
import subprocess
import time
import re
class GlusterPeerStatus(object):
"""
Check the status of Gluster.
gluster peer status - output:
Number of Peers: 2
Hostname: gfs2.easybill.io
Uuid: 8521f8a1-5610-4856-8ef3-ffacd648ea03
State: Peer in Cluster (Connected)
Hostname: gfs3.easybill.io
Uuid: ffafd30f-b0a3-47ad-a221-01829862419e
State: Peer in Cluster (Connected)
Desired output:
{
"peer-count": 2
"peer-connected-count": 2,
"peer-disconnected-count": 0,
"peer-gfs2.easybill.io-state": "Peer in Cluster (Connected)",
"peer-gfs3.easybill.io-state": "Peer in Cluster (Connected)"
}
"""
def __init__(self, agent_config, checks_logger, raw_config):
self.agent_config = agent_config
self.checks_logger = checks_logger
self.raw_config = raw_config
self.version = platform.python_version_tuple()
def run(self):
data = {}
try:
proc = subprocess.Popen(
['sudo', '/usr/sbin/gluster', 'peer', 'status'],
stdout=subprocess.PIPE,
close_fds=True)
output = proc.communicate()[0]
except OSError as exception:
self.checks_logger.error(
' Error: {0}'.format(exception.message))
return data
pcc = output.count('Connected')
# print ('Total connected are: ', pcc)
pdc = output.count('Disconnected')
# print ('Total disconnected are: ', pdc)
lines = output.split("\n\n")
firstline = lines.pop(0)
data['peer-count'] = firstline.replace('Number of Peers:', '').replace(' ', '')
data['peer-connected-count'] = pcc
data['peer-disconnected-count'] = pdc
for line in lines:
match = re.search("Hostname: (.*)", line)
hostname = match.group(1)
match = re.search("State: (.*)", line)
state = match.group(1)
data['peer-' + hostname + '-state'] = state
return data
if __name__ == '__main__':
"""
Standalone test
"""
raw_agent_config = {}
main_checks_logger = logging.getLogger('gluster-peer')
main_checks_logger.setLevel(logging.DEBUG)
main_checks_logger.addHandler(logging.StreamHandler(sys.stdout))
megaraid_check = GlusterPeerStatus({}, main_checks_logger, raw_agent_config)
while True:
try:
print json.dumps(megaraid_check.run(), indent=4, sort_keys=True)
except:
main_checks_logger.exception("Unhandled exception")
finally:
time.sleep(60)