#!/usr/bin/python
 
import boto3
 
client = boto3.client('emr')
 
clusters = client.list_clusters(
  ClusterStates=[
        'STARTING','BOOTSTRAPPING','RUNNING','WAITING',
    ],
  )['Clusters']
 
results = []
 
for i in range(0,len(clusters)):
      clusterId = clusters[i]['Id'].encode('utf-8')
      clusterName = clusters[i]['Name'].encode('utf-8')
      if len(clusterName) > 24:
            clusterName = clusterName[:24][:-3]+"..."
      clusterState = clusters[i]['Status']['State'].encode('utf-8')
      instanceGroups = client.list_instance_groups(ClusterId=clusterId)['InstanceGroups']
      tRun = 0
      tReq = 0
      tId = ""
      cRun = 0
      cReq = 0
      cId = ""
      for j in instanceGroups:
            if j['InstanceGroupType'] == "TASK":
                  tRun = j['RunningInstanceCount']
                  tReq = j['RequestedInstanceCount']
                  tId = j['Id'].encode('utf-8')
            if j['InstanceGroupType'] == "CORE":
                  cRun = j['RunningInstanceCount']
                  cReq = j['RequestedInstanceCount']
                  cId = j['Id'].encode('utf-8')
      if tReq == 0:
            tId = "N/A"
      if cReq ==0:
            cId = "N/A"
      data = dict([('ClusterId',clusterId),('Name',clusterName),('State',clusterState),('CoreGroup',cId),('CoreRequested',cReq),('CoreRunning',cRun),('TaskGroup',tId),('TaskRequested',tReq),('TaskRunning',tRun)])
      results.append(data)
print ""
print ""
print "{:<16} {:<24} {:<13} {:<16} {:<7} {:<7} {:<16} {:<7} {:<7}".format('Cluster','Name','State','Core','CoreReq','CoreRun','TaskGroup','TaskReq','TaskRun')
print '---------------- ------------------------ ------------- ---------------- ------- ------- ---------------- ------- -------'
for i in results:
        print "{:<16} {:<24} {:<13} {:<16} {:<7} {:<7} {:<16} {:<7} {:<7}".format(i['ClusterId'], i['Name'], i['State'], i['CoreGroup'], i['CoreRequested'], i['CoreRunning'], i['TaskGroup'], i['TaskRequested'], i['TaskRunning'])
 
print ""
print ""
