Last active
November 26, 2021 11:33
-
-
Save akshendra/5994ea8a53ee3b18efbeaa7e9c4c7860 to your computer and use it in GitHub Desktop.
Send redis info to cloudwatch
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 python3 | |
''' | |
Send Redis usage metrics to CloudWatch | |
''' | |
import redis | |
import json | |
import boto3 | |
import os | |
machine = os.environ['MACHINE_NAME'] | |
region = os.environ['AWS_REGION'] | |
cloudwatch = boto3.client('cloudwatch') | |
host = 'localhost' | |
passw = None | |
if 'REDIS_HOST' in os.environ: | |
host = os.environ['REDIS_HOST'] | |
if 'REDIS_PASSWORD' in os.environ: | |
passw = os.environ['REDIS_PASSWORD'] | |
def collect_redis_info(): | |
r = redis.StrictRedis(host=host, port=6379, db=0, password=passw) | |
info = r.info() | |
return info | |
def make_matrix(name, value, unit): | |
return { | |
'MetricName': name, | |
'Dimensions': [ | |
{ | |
'Name': 'Machine', | |
'Value': machine | |
}, | |
], | |
'Value': value, | |
'Unit': unit, | |
} | |
def send_multi_metrics(metrics, namespace='EC2/RedisV2'): | |
data = [] | |
for key, value in metrics.items(): | |
data.append(make_matrix(key, value[0], value[1])) | |
if len(data) == 19: | |
cloudwatch.put_metric_data( | |
Namespace=namespace, | |
MetricData=data | |
) | |
data = [] | |
cloudwatch.put_metric_data( | |
Namespace=namespace, | |
MetricData=data | |
) | |
if __name__ == '__main__': | |
redis_data = collect_redis_info() | |
count_metrics = { | |
'CurrConnections': [redis_data['connected_clients'], 'Count'], | |
'UsedMemory': [redis_data['used_memory'], 'Bytes'], | |
'IOPS': [redis_data['instantaneous_ops_per_sec'], 'Count'], | |
'InputKbps': [redis_data['instantaneous_input_kbps'], 'Kilobytes/Second'], | |
'OutputKbps': [redis_data['instantaneous_output_kbps'], 'Kilobytes/Second'], | |
'PubsubChannels': [redis_data['pubsub_channels'], 'Count'], | |
'PubsubPatterns': [redis_data['pubsub_patterns'], 'Count'], | |
} | |
hits = redis_data['keyspace_misses'] | |
misses = redis_data['keyspace_misses'] | |
permiss = 0; | |
if (misses is not 0): | |
permiss = (misses) / (hits + misses) | |
count_metrics['Misses'] = [permiss, 'Percent'] | |
for i in range(12): | |
dbname = 'db{}'.format(i) | |
if (dbname in redis_data): | |
count_metrics['keys_{}'.format(i)] = [redis_data[dbname]['keys'], 'Count'] | |
count_metrics['expires_{}'.format(i)] = [redis_data[dbname]['expires'], 'Count'] | |
ttl_hours = 0 | |
if 'avg_ttl' in redis_data[dbname]: | |
ttl_hours = redis_data[dbname]['avg_ttl'] / (1000 * 3600) | |
count_metrics['ttl_{}'.format(i)] = [ttl_hours, 'Count'] | |
print(json.dumps(count_metrics)) | |
if redis_data['role'] == 'master': | |
count_metrics['Slaves'] = [redis_data['connected_slaves'], 'Count'] | |
else: | |
count_metrics['Slaves'] = [0, 'Count'] | |
send_multi_metrics(count_metrics) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment