Skip to content

Instantly share code, notes, and snippets.

@kayibal
Last active November 17, 2016 13:40
Show Gist options
  • Save kayibal/4b653dce08c4abd3f2fa58b3c62973cf to your computer and use it in GitHub Desktop.
Save kayibal/4b653dce08c4abd3f2fa58b3c62973cf to your computer and use it in GitHub Desktop.
Send an SMS in case the ec2 instance is idle
#!/usr/bin/env bash
MIN_LOAD=1
ADMIN_NUMBERS=( "+49NUMBER1" "+49NUMBER2" )
if ! who | grep -q *; then
echo "users logged in"
exit 0
fi
LOAD=`uptime | awk '{print $NF}'`
WORKING=`echo "$LOAD>$MIN_LOAD" | bc `
if [[ $WORKING == 1 ]]; then
echo "server is doing work"
exit 0
fi
TWILIO_SID="YOUR TWILIO SID"
TWILIO_TOKEN="YOUR TWILIO TOKEN"
IP=`wget -qO- http://instance-data/latest/meta-data/public-ipv4`
MESSAGE="Hi, I'm idleing and no one is logged in. Cheers\n$IP"
for NUMBER in "${ADMIN_NUMBERS[@]}"
do
RES=`curl -sfX POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_SID/Messages.json" \
--data-urlencode "To=$NUMBER" \
--data-urlencode 'From=TWILIO' \
--data-urlencode "Body=$MESSAGE" \
-u $TWILIO_SID:$TWILIO_TOKEN`
if [ $? -gt 0 ]; then
echo "Failed to send SMS: $RES"
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment