Skip to content

Instantly share code, notes, and snippets.

@addomafi
Last active June 3, 2020 17:16
Show Gist options
  • Save addomafi/87f8659249de0e754770fe19f3d55ba7 to your computer and use it in GitHub Desktop.
Save addomafi/87f8659249de0e754770fe19f3d55ba7 to your computer and use it in GitHub Desktop.
Script to get metrics from EBS
import sys
import boto3, json
from datetime import datetime, timedelta
import dateutil.parser
from time import sleep
# pbpaste | | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' | sed s/\"//g
# based on http://www.quora.com/Amazon-S3/What-is-the-fastest-way-to-measure-the-total-size-of-an-S3-bucket
# assumes you've already configured your access id & secret key
session = boto3.Session(profile_name='bkp-default', region_name='us-east-1')
ec2 = session.client('ec2')
def getValue(key, list):
for item in list:
return item[key]
return ""
def getItem(key, field, list):
if field in list:
for item in list[field]:
if item["Key"] == key:
return item["Value"]
return ""
def get_ebs_volumes():
'''Given a bucket name, retrieve the size of each key in the bucket
and sum them together. Returns the size in gigabytes and
the number of objects.'''
response = ec2.describe_volumes()
ebsVolumes = []
# For each metric get data
for volume in response['Volumes']:
volId = volume['VolumeId']
print("Getting data for ebs volume: {}".format(volId))
dateIso8601 = ""
timestamp = volume['CreateTime']
if timestamp:
dateIso8601 = timestamp.isoformat()
ebsVolumes.append({
"VolumeId": volId,
"Size": volume['Size'],
"State": volume['State'],
"VolumeType": volume['VolumeType'],
"CreateTime": dateIso8601,
"SnapshotId": volume['SnapshotId'],
"Name": getItem('Name', 'Tags', volume),
"Onwer": getItem('Owner', 'Tags', volume),
"Application": getItem('Application', 'Tags', volume),
"EmrClusterId": getItem('aws:elasticmapreduce:job-flow-id', 'Tags', volume),
"EmrNodeType": getItem('aws:elasticmapreduce:instance-group-role', 'Tags', volume),
"InstanceId": getValue('InstanceId', volume['Attachments'])
})
return ebsVolumes
if __name__ == '__main__':
bucket_sizes = get_ebs_volumes()
text_file = open("/Users/admartins/LocalDocuments/notes/ebs_volumes.json", "w")
text_file.write(json.dumps(bucket_sizes, ensure_ascii=False))
text_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment