#!/bin/bash infile=WATERLOG.TXT tmp1=/tmp/tmp1.$$ tmp2=/tmp/tmp2.$$ tmp3=/tmp/tmp3.$$ file=/tmp/tmp4.$$ tmps="$tmp1 $tmp2 $tmp3" trap "rm -f $tmps; exit" 0 1 2 3 15 APIKEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" FEED="99999" DATASTREAM="xxxxxxxxxx" IP="192.168.999.999" EMAIL="me@email.com" cd $HOME echo "start: $(date)" wget http://$IP/WATERLOG.TXT if [ "$(find $infile -mmin -10)" != "$infile" ]; then echo "could not retrieve $infile from $IP" msg="FAILED: wget http://$IP/$infile" $HOME/bin/sendEmail.sh -t $EMAIL -u "$msg" -m "$msg" exit 0 fi cat $infile | while read line; do time=$(echo $line | awk '{ print $4 }'); ll=$(echo $line | awk '{ print $1 }'); hh=$(date --date="1970-01-01 $time sec GMT" +'%Y-%m-%dT%H:%M:%SZ') ; echo "$hh,$ll" done > $file today=$(date +'%Y-%m-%dT') liters=$(grep $today $file | wc -l) if [ $liters -gt 100 ]; then msg="Alert : abnormal water consumption ($liters liters)" elif [ $liters -gt 50 ]; then msg="Warning : strange water consumption ($liters liters)" else msg="OK: normal water consumption ($liters liters)" fi $HOME/bin/sendEmail.sh -t $EMAIL -u "$msg" -m "$msg" # # send to www.cosm.com # #---------------------------------- # add 0 points at the beginning of each day time1=$(head -1 $infile | awk '{ print $4 }'| sed -e 's;[^0-9];;g' ) time2=$(tail -1 $infile | awk '{ print $4 }'| sed -e 's;[^0-9];;g' ) if [ "$time1" == "" -o "$time2" == "" ]; then exit fi while [ $time1 -lt $time2 ] do ll=0; hh=$(date --date="1970-01-01 $time1 sec GMT" +'%Y-%m-%dT%00:%00:%01Z') ; time1=$(($time1 + 86400)) echo "$tt,$l" >> $tmp1 done for date in $(cat $file | awk -FT '{ print $1 }' | sort -u) do l=0 rm -f $tmp1 cat $file | grep $date | sort | while read line do ll=$(echo $line | awk -F, '{ print $2 }'); tt=$(echo $line | awk -F, '{ print $1 }'); l=$(($ll + $l)) echo "$tt,$l" >> $tmp1 done echo "$date, $(cat $tmp1 | wc -l) records" split $tmp1 -l 500 $tmp2 for upload in $(ls -1 ${tmp2}* ) do curl --request POST --data-binary @$upload --header "X-ApiKey: $APIKEY" http://api.cosm.com/v2/feeds/$FEED/datastreams/$DATASTREAM/datapoints.csv done echo " " done echo "finish: $(date)" rm -rf $tmps