3232from argparse import ArgumentParser
3333from sys import exit
3434
35+
3536def 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 (args .master_port , args .password )
43+ )
44+ slave_addr , slave_state , cluster_state_slave , failed_slave = (
45+ _get_cluster_status (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 ))
@@ -80,15 +84,15 @@ def get_parser():
8084 '--master-port' ,
8185 action = 'store' ,
8286 dest = 'master_port' ,
83- type = int ,
87+ type = str ,
8488 default = 7000 ,
8589 help = 'redis port of the master instance' ,
8690 )
8791 parser .add_argument (
8892 '--slave-port' ,
8993 action = 'store' ,
9094 dest = 'slave_port' ,
91- type = int ,
95+ type = str ,
9296 default = 7001 ,
9397 help = 'redis port of the slave instance' ,
9498 )
@@ -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