Created
March 16, 2018 14:23
-
-
Save jalada/362c486ec799dbe2adc654dbbf318bf9 to your computer and use it in GitHub Desktop.
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 | |
''' | |
Send Redis connection metrics to Amazon CloudWatch | |
''' | |
import sys | |
import re | |
from subprocess import check_output | |
from boto.ec2 import cloudwatch | |
from boto.utils import get_instance_metadata | |
def collect_redis_connections(): | |
conninfo = {} | |
connections = check_output('redis-cli client list | wc -l', shell=True).rstrip() | |
return connections | |
def send_metric(instance_id, region, key, value, namespace='Redis', unit='Count'): | |
cw = cloudwatch.connect_to_region(region) | |
cw.put_metric_data(namespace, key, value, unit=unit, | |
dimensions={"InstanceId": instance_id}) | |
if __name__ == '__main__': | |
metadata = get_instance_metadata() | |
instance_id = metadata['instance-id'] | |
region = metadata['placement']['availability-zone'][0:-1] | |
connections = collect_redis_connections() | |
send_metric(instance_id, region, 'Connections', connections) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment