Skip to content

Instantly share code, notes, and snippets.

@philpownall
Created January 4, 2017 16:56
Show Gist options
  • Save philpownall/6326b75d1106cbcc9922ed3c7bc0e5a8 to your computer and use it in GitHub Desktop.
Save philpownall/6326b75d1106cbcc9922ed3c7bc0e5a8 to your computer and use it in GitHub Desktop.
#!/bin/bash
#set -x #echo on
echo "Rogersmogrify run at $(date)"
#
# Mogrify script for MeterMaid
# Crop the interesting bits out of the image files
# downloaded from the Rogers website by myRogers.py
# and copy them to the server directory for MeterMaid
# Sorry for the magic numbers..
#
MYEMAIL="<your email here>"
USAGELIMIT="<your monthly limit here>"
ROLLOVERDAY="<your rollover day here>"
METERMAID="$HOME/Metermaid"
ABYSS="$HOME/abyssws/htdocs/MeterMaid"
MYLOGFILE="$METERMAID/RogersData.txt"
MSGFILE="$METERMAID/MeterMaidMessage.txt"
#
# These magic numbers crop the interesting bits out of the usage images
# the format for mogrify is Xsize x Ysize + Xorigin + Yorigin (in pixels)
#
mogrify -crop 250x250+82+350 $METERMAID/RogersUsage.png
cp $METERMAID/RogersUsage.png $ABYSS/RogersUsage.png
mogrify -crop 564x400+190+515 $METERMAID/RogersDailyUsage.png
cp $METERMAID/RogersDailyUsage.png $ABYSS/RogersDailyUsage.png
# copy the txt file (containing usage numbers) to the server MeterMaid folder
cp $METERMAID/RogersData.txt $ABYSS/RogersData.txt
#
# Alerts and Notifications section
#
# read the last line of the file
# line format is "Fri Dec 23 07:10:31 2016","$85.87","53697"
# i.e. Date, Balance, Usage
#
line=$(tail -n 1 $MYLOGFILE)
# split into fields
IFS=',' read -r -a array <<< "$line"
# strip the trailing quote and leading quote from array[2]
usage="${array[2]%\"}"
usage="${usage#\"}"
today=$(date +%d)
if [ $today -le $ROLLOVERDAY ]; then today=$((today+30)); fi
let average=$(($usage/($today-$ROLLOVERDAY)))
let prediction=$average*30
echo "Usage $usage"
echo "Limit $USAGELIMIT"
echo "Today $today"
echo "ROLLOVERDAY day $ROLLOVERDAY"
echo "Number of days into cycle $(($today - $ROLLOVERDAY))"
echo "Average usage per day $average"
echo "Usage per month at current rate $prediction"
if [ $prediction -gt $USAGELIMIT ]; then
# predicted usage will exceed limit: send out a warning
echo "WARNING: Predicted Rogers Data usage of $prediction MB will exceed the limit of $USAGELIMIT MB"
mail -s "WARNING: Predicted Rogers Data usage of $prediction MB will exceed the limit of $USAGELIMIT MB" $MYEMAIL < $MSGFILE ;
fi
if [ $usage -gt $USAGELIMIT ]; then
# usage limit exceeded: send out an alert
echo "ALERT: Rogers Data Usage of $usage MB has exceeded the limit of $USAGELIMIT MB"
mail -s "ALERT: Rogers Data Usage of $usage MB has exceeded the limit of $USAGELIMIT MB" $MYEMAIL < $MSGFILE ;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment