Last active
January 22, 2017 09:34
-
-
Save cabrinha/035a22a63c1de3b6c3704401d4c95273 to your computer and use it in GitHub Desktop.
Retrieve a key from redis and parse it's values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import redis | |
import json | |
import argparse | |
parser = argparse.ArgumentParser( | |
description='', add_help=True) | |
parser.add_argument('-H', '--host', default='localhost', metavar='HOST', | |
help='Hostname to run the check on.', dest='host') | |
parser.add_argument('-M', '--metric', metavar='METRIC', | |
help='Ostrich [gauges] or [counters] to fetch the value of.', | |
dest='metric') | |
parser.add_argument('-K', '--key', metavar='KEY', | |
help='Key to return the value of.', dest='key') | |
parser.add_argument('-V', '--value', metavar='VALUE', | |
help='Value of key argument if key is "metrics".', dest='val') | |
parser.add_argument('-x', '--compare', metavar='COMPARE', type=str, | |
help='Method of camparison.', dest='compare') | |
parser.add_argument('-w', '--warn', metavar='WARN', type=int, | |
help='Warning threshold.', dest='warn') | |
parser.add_argument('-c', '--crit', metavar='CRIT', type=int, | |
help='Critical threshold.', dest='crit') | |
parser.add_argument('-m', '--max-value', dest='max_value', metavar='MAXVALUE', | |
help='Ostrich value to compare warn and crit percentage against.') | |
args = parser.parse_args() | |
if not len(sys.argv) > 1: | |
parser.print_help() | |
sys.exit() | |
# Set redis connection to POOL and specify the master | |
POOL = redis.ConnectionPool(host='redis.my_redis.com', port=6379, db=0) | |
def get_redis(key): | |
redis_server = redis.Redis(connection_pool=POOL) | |
response = redis_server.get(key) | |
return response | |
json_data = json.loads(get_redis(args.host)) | |
if 'metrics' in args.metric: | |
try: | |
value = float( | |
json_data['metrics']['{}'.format(args.key)]['{}'.format(args.val)]) | |
except: | |
pass | |
elif 'value' in args.metric and 'status' in args.key: | |
value = float( | |
json_data['status']) | |
elif 'value' in args.metric: | |
value = float( | |
json_data['value']['{}'.format(args.key)]['{}'.format(args.val)]) | |
else: | |
value = float( | |
json_data['{}'.format(args.metric)]['{}'.format(args.key)]) | |
if args.max_value: | |
max_val = float(json_data[str(args.metric)][str(args.max_value)]) | |
perc = value / max_val * 100 | |
if perc > args.crit: | |
print 'CRITICAL - {} = {}: {}% of {} ({})'.format( | |
args.key, int(value), int(perc), args.max_value, int(max_val) | |
) | |
sys.exit(2) | |
if perc > args.warn: | |
print 'WARNING - {} = {}: {}% of {} ({})'.format( | |
args.key, int(value), int(perc), args.max_value, int(max_val) | |
) | |
sys.exit(1) | |
if perc < args.warn: | |
print 'OK - {} = {}: {}% of {} = {}'.format( | |
args.key, int(value), int(perc), args.max_value, int(max_val) | |
) | |
if args.compare == 'lt': | |
if value < args.crit: | |
print 'CRITICAL - {} = {}'.format(args.key, value) | |
sys.exit(2) | |
if value < args.warn: | |
print 'WARNING - {} = {}'.format(args.key, value) | |
sys.exit(1) | |
if value > args.warn: | |
print 'OK - {} = {}'.format(args.key, value) | |
sys.exit(0) | |
if args.compare == 'gt': | |
if value > args.crit: | |
print 'CRITICAL - {} = {}'.format(args.key, value) | |
sys.exit(2) | |
if value > args.warn: | |
print 'WARNING - {} = {}'.format(args.key, value) | |
sys.exit(1) | |
if value < args.warn: | |
print 'OK - {} = {}'.format(args.key, value) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment