Skip to content

Instantly share code, notes, and snippets.

Last active April 3, 2020 12:45
Show Gist options
  • Save amalgjose/3b99fd42bd14bb1d41d0 to your computer and use it in GitHub Desktop.
Save amalgjose/3b99fd42bd14bb1d41d0 to your computer and use it in GitHub Desktop.
Program to find get the summary of EMR clusters and EC2 instances running across all regions in an AWS account. This program can be executed via cron to get summary alerts in periodic intervals. An HTML email will be generated and send to the list of recipients mentioned in program. In this program, EMR and EC2 service summary are only considere…
__author__ = 'Amal G Jose'
import sys
import smtplib
import time
import boto
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from ConfigParser import SafeConfigParser
from boto.regioninfo import RegionInfo
from boto.emr.connection import EmrConnection
BASE_HTML = """<!DOCTYPE html>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
th, td {
padding: 5px;
text-align: left;
END_HTML = """
<table style="width:100%">
<caption><b>EMR Cluster Details</b></caption>
<th>Cluster Name</th>
<th>Number of Nodes</th>
<th>Instance Type</th>
<th>Cluster Creation Date</th>
EC2_HEADER = """
<table style="width:100%">
<caption><b>EC2 Instance Details</b></caption>
<th>EC2 Name</th>
<th>Instance Id</th>
<th>Instance Type</th>
TABLE_END = """</table>"""
LINE_BREAK = "<br> <br> <br>"
class GetSummary(object):
def __init__(self):
self.from_email_id = ""
self.email_password = "xxxxxxxxxxxx"
self.aws_access_key = 'XXXXXXXXXXXXXXXXXX'
self.aws_secret_key = 'XXXXXXXXXXXXXXXXXX'
to_address = ",,"
self.receiver_email = to_address.split(',')
##Method for getting the details of EMR
def get_all_emr(self, regions):
html_data = EMR_HEADER
for region in regions:
emr_conn = EmrConnection(self.aws_access_key, self.aws_secret_key,
endpoint=region + ''))
cluster_list = emr_conn.describe_jobflows()
for cluster in cluster_list:
if cluster.state in ['RUNNING', 'WAITING', 'STARTING', 'BOOTSTRAPPING']:
row_html = """<tr>
</tr>""" % (, str(cluster.instancecount), cluster.masterinstancetype, cluster.creationdatetime, region)
html_data += row_html + "\n"
return html_data + TABLE_END
except Exception, e:
print "Exception occurred while getting the details of EMR cluster : " + str(e)
##Method for getting the details of EC2
def get_all_running_instances(self, regions):
emr_groups = ['ElasticMapReduce-slave', 'ElasticMapReduce-master']
html_data = EC2_HEADER
for region in regions:
conn = boto.ec2.connect_to_region(region,
##List all running EC2 instances
reservations = conn.get_all_reservations()
for reservation in reservations:
for instance in reservation.instances:
if instance.state in ['running'] and (instance.groups[0].name not in emr_groups):
row_html = """<tr>
</tr>""" % (instance.tags['Name'],, instance.instance_type,region )
row_html = """<tr>
</tr>""" % (, instance.instance_type, region)
html_data += row_html + '\n'
return html_data + TABLE_END
except Exception, e:
print "Error occurred while getting EC2 instance details : " + str(e)
##Method to send email alerts
def send_email(self, email_body):
SUBJECT = "AWS Account Summary"
localtime = time.asctime( time.localtime(time.time()))
recepients = ','.join(self.receiver_email)
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT + " " + str(localtime)
msg['From'] = self.from_email_id
msg['To'] = recepients
msg_part = MIMEText(email_body, 'html')
server = smtplib.SMTP("", 587)
server.login(self.from_email_id, self.email_password)
server.sendmail(self.from_email_id, self.receiver_email, msg.as_string())
print 'Email Sent Successfully'
except Exception, e:
print "Failed to send email : " + str(e)
if __name__== '__main__':
get_summary = GetSummary()
regions = ['ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1',
'us-east-1','us-west-1', 'us-west-2','eu-west-1', 'sa-east-1']
ec2_details = get_summary.get_all_running_instances(regions)
emr_details = get_summary.get_all_emr(regions)
complete_details = BASE_HTML + emr_details + LINE_BREAK + ec2_details + END_HTML
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment