Skip to content

Commit 656a2c8

Browse files
author
Meik Milevczik
committed
Improve code quality of check_redis_cluster.py
1 parent 0f65f9a commit 656a2c8

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

src/check_redis_cluster.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,28 @@
3232
from argparse import ArgumentParser
3333
from sys import exit
3434

35+
3536
def main():
3637
"""Main entrypoint for script"""
3738

3839
args = get_parser().parse_args()
3940

40-
master_addr, master_state, cluster_state_master, failed_master = _get_cluster_status(
41-
args.master_port, args.password)
42-
slave_addr, slave_state, cluster_state_slave, failed_slave = _get_cluster_status(
43-
args.slave_port, args.password)
41+
master_addr, master_state, cluster_state_master, failed_master = (
42+
_get_cluster_status(str(rgs.master_port), args.password)
43+
)
44+
slave_addr, slave_state, cluster_state_slave, failed_slave = (
45+
_get_cluster_status(str(args.slave_port), args.password)
46+
)
4447
if master_state != 'unknown' and slave_state != 'unknown':
48+
failed_hosts = failed_master + failed_slave
4549
if cluster_state_master != 'ok' and cluster_state_slave != 'ok':
4650
print('CRITICAL - cluster is broken')
4751
code = 2
48-
elif len(failed_master + failed_slave) > 0:
52+
elif failed_hosts:
4953
print('WARNING - cluster status is degraded')
50-
for host in failed_master + failed_slave:
54+
code = 1
55+
for host in failed_hosts:
5156
print('{} is in a failed state'.format(host))
52-
code = 1
5357
else:
5458
print('OK - cluster status is OK')
5559
print('{} - {}'.format(master_addr, master_state))
@@ -109,14 +113,33 @@ def _get_cluster_status(port, password):
109113
The status of the local instances will be checked
110114
"""
111115

116+
"""The output from redis-cli cluser nodes has the following format:
117+
6 lines where each line is a space seperated list with the following data:
118+
<hex_string> (hash of the node)
119+
<ip>:7001@17001 (ip:port@remote port)
120+
slave,fail (myself,)?(master|slave)(,fail)?
121+
<hex_string> (remote node hash)
122+
<number> (performance data)
123+
<number> (performance data)
124+
<number> (number of connected clients)
125+
connected (fixed string)
126+
127+
The string ",fail" is optional. It will only exists on failed hosts
128+
"""
112129
try:
113130
role = subprocess.check_output(
114131
'redis-cli -p {0} -a {1} cluster nodes'.format(
115132
port, password), shell=True).decode().split()
133+
# Find keyword myself in the output
116134
state_index = [i for i, s in enumerate(role) if 'myself' in s][0]
117-
failed_hosts = [role[i-1] for i, s in enumerate(role) if 'fail' in s and str(port) in role[i-1]]
135+
# Remove "myself," from the string. It will be either master or slave
118136
role_state = role[state_index].replace('myself,', '')
137+
# Get ip:port information which is one to the left of mysql, string
119138
role_addr = role[state_index - 1]
139+
# Get ip:port of the node that uses the current port and
140+
# is in status "fail".
141+
failed_hosts = [role[i-1] for i, s in enumerate(role)
142+
if 'fail' in s and port in role[i-1]]
120143

121144
cluster_state = subprocess.check_output(
122145
'redis-cli -p {0} -a {1} cluster info'.format(

0 commit comments

Comments
 (0)