Receive a single Pushover notification when Swizzin bandwidth quota is about to expire.
| #!/bin/bash | |
| # INSTRUCTIONS | |
| # Create a new APP at pushover.net, note the APP API token and your USER token | |
| # Save this script anywhere in your Swizzin.net box, say $HOME/bin/swizzin-quota-alarm.sh | |
| # Give it exec permissions with chmod +x $HOME/bin/swizzin-quota-alarm.sh | |
| # Make it run each 15 minutes by running crontab -e and write: | |
| # */15 * * * * /home/YOURUSERNAME/bin/swizzin-quota-alarm.sh | |
| PU_USER_KEY="" | |
| PU_API_TOKEN="" | |
| ALARM_AT_GB=500 # At this or lower GB quota, you get one notification | |
| LOCKFILE="$HOME/lock/`basename $0`" # Lockfile to get only one notification per quota expiration. | |
| # Path will be created if not present | |
| QUOTA=$(/usr/bin/sudo box bw |awk 'FNR == 3 { print $2 }') | |
| if [ "${QUOTA: -1}" = "T" ]; then | |
| QUOTA_GB=$(/usr/bin/sudo box bw |awk 'FNR == 3 { print $2 }' | sed 's/.$//' | sed 's/\.//') | |
| else | |
| QUOTA_GB=$(/usr/bin/sudo box bw |awk 'FNR == 3 { print $2 }' | sed 's/.$//' | sed 's/\([0-9]*\)\.\([0-9]*\)/\1/') | |
| fi | |
| PU_NOTIF_TITLE="Swizzin" | |
| PU_NOTIF_MESSAGE="Swizzin bandwidth left: $QUOTA_GB GB" | |
| if [ "$QUOTA_GB" -le "$ALARM_AT_GB" ]; then | |
| if [ ! -f $LOCKFILE ]; then | |
| /usr/bin/wget https://api.pushover.net/1/messages.json --post-data="token=$PU_API_TOKEN&user=$PU_USER_KEY&message=$PU_NOTIF_MESSAGE&title=$PU_NOTIF_TITLE" -qO- > /dev/null 2>&1 & | |
| /bin/mkdir -p "$(/usr/bin/dirname "$LOCKFILE")" && /usr/bin/touch $LOCKFILE | |
| fi | |
| else | |
| /bin/rm -f $LOCKFILE | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment