Last active
February 11, 2020 02:29
-
-
Save johnroyer/05a84ae6576aeb4f675764e071e146d4 to your computer and use it in GitHub Desktop.
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 | |
function alert() { | |
# mail notification | |
curl -v -s --user 'api:MAILGUN_API_KEY' \ | |
https://api.mailgun.net/v3/YOUR_DOMAIN/messages \ | |
-F from='Monitor <you@gmail.com>' \ | |
-F to=your@gmail.com \ | |
-F subject="low momey allert - monitor" \ | |
--form-string html="<span style=\"font-size: 1.5em;\">memory left: $1 MB <br> SWAP left: $2 MB</span>" | |
# PushOver notification | |
curl -s \ | |
--form-string "token=APP_TOKEN" \ | |
--form-string "user=USER_TOKEN" \ | |
--form-string "html=1" \ | |
--form-string "message=memory of box is too low. <span style=\"color: red;\">Memory left $1 MB, SWAP left $2 MB." \ | |
https://api.pushover.net/1/messages.json | |
} | |
MEMFREE=`free -m | grep "Mem:" | awk -F' ' '{print $4}' ` | |
SWAPLEFT=`free -m | grep "Swap:" | awk -F' ' '{print $4}' ` | |
if [ $MEMFREE -gt 1000 ]; then | |
# memory is enough | |
exit 0 | |
elif [ $MEMFREE -lt 1000 ]; then | |
# memory is almost full | |
if [ $SWAPLEFT -gt 1000 ]; then | |
# SWAP can be used | |
exit 0 | |
elif [ $SWAPLEFT -lt 1000 ]; then | |
allert $MEMFREE $SWAPLEFT | |
elif [ $SWAPLEFT -lt 100]; then | |
# out of memory | |
alert $MEMFREE $SWAPLEFT | |
fi | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
檢查記憶體是否爆炸的 sciprt