Skip to content

Instantly share code, notes, and snippets.

@ramn
Created September 21, 2012 12:21
Show Gist options
  • Save ramn/3761184 to your computer and use it in GitHub Desktop.
Save ramn/3761184 to your computer and use it in GitHub Desktop.
Monitor Amazon EMR and send XMPP message on completion
#! /bin/bash
# A script that watches the running status of Amazon EMR jobs, sending an XMPP
# message upon completion.
#
# A destination jabber account, where the messages are sent, should be supplied
# either as the first argument at invocation or in the environment variable
# JABBER_ACCOUNT.
#
# Dependencies:
# - https://github.com/hapyrus/elastic-mapreduce
# - sendxmpp (available as debian package)
CHECK_INTERVAL_SECONDS=10 # subject to throttling on Amazon, at least 3 secs?
JABBER_ACCOUNT=${1:-$JABBER_ACCOUNT}
function validate_args() {
if [ -z $JABBER_ACCOUNT ]
then
echo "Usage: supply a destination jabber account either as the first"
echo "argument or in the environment variable JABBER_ACCOUNT"
exit 1
fi
}
function list_emr() {
./emr --list --active --no-steps
}
function filter_done() {
grep 'COMPLETED\|FAILED\|WAITING'
}
function send_xmpp() {
sendxmpp $JABBER_ACCOUNT 2>/dev/null
}
function check_status() {
read OUT
if [ ! -z "$OUT" ]
then
echo -e "Job is finished:\n$OUT" | send_xmpp
exit 1
else
echo "$(date +'%Y-%m-%d %H:%M:%S'), waiting for job.."
fi
}
function main() {
while true
do
list_emr | filter_done | check_status
if [ $? -ne 0 ]
then
echo 'done, exiting..'
exit 1
fi
sleep $CHECK_INTERVAL_SECONDS
done
}
validate_args
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment