Last active
November 17, 2016 13:40
-
-
Save kayibal/4b653dce08c4abd3f2fa58b3c62973cf to your computer and use it in GitHub Desktop.
Send an SMS in case the ec2 instance is idle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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