|
32 | 32 | from argparse import ArgumentParser |
33 | 33 | from sys import exit |
34 | 34 |
|
| 35 | + |
35 | 36 | def main(): |
36 | 37 | """Main entrypoint for script""" |
37 | 38 |
|
38 | 39 | args = get_parser().parse_args() |
39 | 40 |
|
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 | + ) |
44 | 47 | if master_state != 'unknown' and slave_state != 'unknown': |
| 48 | + failed_hosts = failed_master + failed_slave |
45 | 49 | if cluster_state_master != 'ok' and cluster_state_slave != 'ok': |
46 | 50 | print('CRITICAL - cluster is broken') |
47 | 51 | code = 2 |
48 | | - elif len(failed_master + failed_slave) > 0: |
| 52 | + elif failed_hosts: |
49 | 53 | print('WARNING - cluster status is degraded') |
50 | | - for host in failed_master + failed_slave: |
| 54 | + code = 1 |
| 55 | + for host in failed_hosts: |
51 | 56 | print('{} is in a failed state'.format(host)) |
52 | | - code = 1 |
53 | 57 | else: |
54 | 58 | print('OK - cluster status is OK') |
55 | 59 | print('{} - {}'.format(master_addr, master_state)) |
@@ -109,14 +113,33 @@ def _get_cluster_status(port, password): |
109 | 113 | The status of the local instances will be checked |
110 | 114 | """ |
111 | 115 |
|
| 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 | + """ |
112 | 129 | try: |
113 | 130 | role = subprocess.check_output( |
114 | 131 | 'redis-cli -p {0} -a {1} cluster nodes'.format( |
115 | 132 | port, password), shell=True).decode().split() |
| 133 | + # Find keyword myself in the output |
116 | 134 | 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 |
118 | 136 | role_state = role[state_index].replace('myself,', '') |
| 137 | + # Get ip:port information which is one to the left of mysql, string |
119 | 138 | 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]] |
120 | 143 |
|
121 | 144 | cluster_state = subprocess.check_output( |
122 | 145 | 'redis-cli -p {0} -a {1} cluster info'.format( |
|
0 commit comments