Skip to content

Instantly share code, notes, and snippets.

@axltxl
Forked from shevron/LICENSE
Last active August 29, 2015 14:24
Show Gist options
  • Save axltxl/4cb6e2c46971fa6975c0 to your computer and use it in GitHub Desktop.
Save axltxl/4cb6e2c46971fa6975c0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
Send memory usage metrics to Amazon CloudWatch
This is intended to run on an Amazon EC2 instance and requires an IAM
role allowing to write CloudWatch metrics. Alternatively, you can create
a boto credentials file and rely on it instead.
Original idea based on https://github.com/colinbjohnson/aws-missing-tools
'''
import sys
import re
import boto.ec2
from boto.ec2 import cloudwatch
from boto.utils import get_instance_metadata
import boto.ec2.autoscale
def collect_memory_usage():
meminfo = {}
pattern = re.compile('([\w\(\)]+):\s*(\d+)(:?\s*(\w+))?')
with open('/proc/meminfo') as f:
for line in f:
match = pattern.match(line)
if match:
# For now we don't care about units (match.group(3))
meminfo[match.group(1)] = float(match.group(2))
return meminfo
def send_multi_metrics( instance_id, region, metrics,
autoscaling_group_name=None,
namespace='System/Linux',
unit='Percent'):
'''
Send multiple metrics to CloudWatch
metrics is expected to be a map of key -> value pairs of metrics
'''
# Connect to CloudWatch
cw = cloudwatch.connect_to_region(region)
# Send metrics on behalf of the instance
cw.put_metric_data(namespace, metrics.keys(), metrics.values(),
unit=unit,
dimensions={"InstanceId": instance_id})
# Send metrics on behalf of the autoscaling group where
# this instance belongs
if autoscaling_group_name is not None:
cw.put_metric_data(namespace, metrics.keys(), metrics.values(), unit=unit,
dimensions={"AutoScalingGroupName": autoscaling_group_name})
def get_autoscaling_group_name(region, instance_id):
'''
According to AWS documentation:
Note that when you launch an instance in an Auto Scaling group,
Auto Scaling adds a tag to the instance with a key
of aws:autoscaling:groupName and a value of the name of the Auto Scaling group.
'''
conn = boto.ec2.connect_to_region(region)
# Find a specific instance, returns a list of Reservation objects
reservations = conn.get_all_instances(instance_ids=[instance_id])
# Find the Instance object inside the reservation
instance = reservations[0].instances[0]
# Return the value set by tag
if 'aws:autoscaling:groupName' in instance.tags:
return instance.tags['aws:autoscaling:groupName']
return None
if __name__ == '__main__':
metadata = get_instance_metadata()
instance_id = metadata['instance-id']
region = metadata['placement']['availability-zone'][0:-1]
mem_usage = collect_memory_usage()
# Get the value set by tag aws:autoscaling:groupName
tag_autoscaling_group_name = get_autoscaling_group_name(region, instance_id)
mem_free = mem_usage['MemFree'] + mem_usage['Buffers'] + mem_usage['Cached']
mem_used = mem_usage['MemTotal'] - mem_free
if mem_usage['SwapTotal'] != 0 :
swap_used = mem_usage['SwapTotal'] - mem_usage['SwapFree'] - mem_usage['SwapCached']
swap_percent = swap_used / mem_usage['SwapTotal'] * 100
else:
swap_percent = 0
metrics = {'MemUsage': mem_used / mem_usage['MemTotal'] * 100,
'SwapUsage': swap_percent }
send_multi_metrics(instance_id, region, metrics, tag_autoscaling_group_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment