-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_redis_sentinel_master
61 lines (54 loc) · 2.28 KB
/
check_redis_sentinel_master
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
#! /usr/bin/env python3
""" Script to check Redis-Sentinel, Redis-master and Redis-slave health """
import sys
import argparse
import redis
from redis.sentinel import Sentinel
STATES = {'OK':0, 'WARNING': 1, 'CRITICAL': 2, 'UNKNOWN':3}
STATE = 'OK'
parser = argparse.ArgumentParser(description='Redis-Sentinel checker')
parser.add_argument('--host', metavar='host', help='Address of Redis-Sentinel host')
parser.add_argument('--port', metavar='port', help='Port of Redis-Sentinel host')
parser.add_argument('--master', metavar='master', help="Master's name that Redis-Sentinel monitors")
parser.add_argument('--warning', metavar='warning', help="Comma separated list of threshold for min slaves and sentinels running")
parser.add_argument('--critical', metavar='critical', help="Comma separated list of threshold to trigger critical state")
ARGS = vars(parser.parse_args())
try:
redisConnection = redis.Redis(ARGS['host'], ARGS['port'])
redisInfo = redisConnection.info()
except (redis.exceptions.ConnectionError, redis.exceptions.ResponseError):
STATE = 'CRITICAL'
print("CRITICAL: Problem establishing connection to Redis-Sentinel")
sys.exit(STATE)
sentinel = Sentinel([(ARGS['host'], ARGS['port'])])
try:
masterInfo = sentinel.discover_master(ARGS['master'])
except redis.sentinel.MasterNotFoundError:
STATE = 'CRITICAL'
print("CRITICAL : no master")
sys.exit(STATE)
warningThreshold = ARGS['warning'].split(',')
criticalThreshold = ARGS['warning'].split(',')
# Getting current slaves and sentinels info
sentinelsInfo = redisConnection.info('sentinel')
activeSlaves = sentinelsInfo['master0']['slaves']
activeSentinels = sentinelsInfo['master0']['sentinels']
activeMasterName = sentinelsInfo['master0']['address']
if activeSlaves < int(criticalThreshold[0]):
STATE = 'CRITICAL'
print"Error, no slaves available"
sys.exit(STATE)
if activeSentinels < int(criticalThreshold[1]):
STATE = 'CRITICAL'
print"Error, no sentinels available"
sys.exit(STATE)
if activeSlaves < int(warningThreshold[0]):
STATE = 'WARNING'
print"Warning, missing slaves"
sys.exit(STATE)
if activeSentinels < int(warningThreshold[1]):
STATE = 'WARNING'
print"Warning, missing sentinels"
sys.exit(STATE)
print "Everything ok, master is %s" % (activeMasterName)
sys.exit(STATE)