#!/bin/bash

# dependencies: awscli, bc, jq

clusters=$(aws emr list-clusters --active)

cluster_arr=$(echo $clusters | jq '[.Clusters[] | {id: .Id, timestamp: .Status.Timeline.CreationDateTime, status: .Status.State}]')

current_time=$(date +%s)

echo $cluster_arr | jq -r '.[]|[.id, .timestamp, .status] | @tsv' |
while IFS=$'\t' read -r id timestamp status; do
  minutes_running=$(echo \($current_time-$timestamp\)/60 | bc)
  if [ $minutes_running > 30 ] && [ $status == 'STARTING' ]
  then
   # insert desired contingency action here; example on following line
   echo "Looks like cluster $id has been starting for $minutes_running minutes!"
  fi
done
