Last active
December 21, 2015 06:39
-
-
Save idooo/6265988 to your computer and use it in GitHub Desktop.
AWS Instance-side script to push custom metrics UsedSpacePercent and UsedMemoryPercent 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/python | |
import boto.ec2.cloudwatch | |
import boto.utils | |
import commands | |
if not boto.config.has_section('Boto'): | |
boto.config.add_section('Boto') | |
boto.config.set('Boto', 'proxy', 'proxy.com.au') | |
boto.config.set('Boto', 'proxy_port', '8080') | |
cw = boto.ec2.cloudwatch.connect_to_region('ap-southeast-2') | |
instance_id = boto.utils.get_instance_metadata()['instance-id'] | |
def e(command): | |
return commands.getstatusoutput(command)[1] | |
def postMetric(metric_name, value, dimensions = {}): | |
dimensions.update({'InstanceId': instance_id}) | |
cw.put_metric_data('System/Linux', metric_name, value, unit='Percent', dimensions=dimensions) | |
def postUsedSpace(path): | |
usedpercent = e('df --local '+path+' | grep '+path+' | tr -s \' \' | cut -d \' \' -f 5 | grep -o "[0-9]*"') | |
postMetric('UsedSpacePercent', float(usedpercent), {'Path': path}) | |
def postMem(): | |
memtotal = e('free -m | grep \'Mem\' | tr -s \' \' | cut -d \' \' -f 2') | |
memfree = e('free -m | grep \'buffers/cache\' | tr -s \' \' | cut -d \' \' -f 4') | |
memused = 100-(float(memfree)*100)/float(memtotal) | |
postMetric('UsedMemoryPercent', memused) | |
postUsedSpace('/') | |
postUsedSpace('/data') | |
postMem() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment