send email when peoplecounter is offline

master
pidor 2017-07-29 22:57:43 +02:00
parent f78adee9de
commit c6407b5bf3
5 changed files with 37 additions and 8 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ spaceapikey.txt
pychromecast pychromecast
beamerip.txt beamerip.txt
peoplecounterip.txt peoplecounterip.txt
mailconfig.py

View File

@ -25,6 +25,10 @@ tzselect
comment out the last 4 lines of rsyslogd.conf (for xconsole) comment out the last 4 lines of rsyslogd.conf (for xconsole)
like explained here https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=122601 like explained here https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=122601
echo "pidor" > /etc/hostname
sed -i 's/^127.0.1.1.*/127.0.1.1 pidor/' /etc/hosts
cp -p systemfiles/sudoers.d/* /etc/sudoers.d/ cp -p systemfiles/sudoers.d/* /etc/sudoers.d/
mkdir /root/var # kinda important mkdir /root/var # kinda important

11
scripts/mkmail.py Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/python
import smtplib
import sys
import mailconfig as cnf
server = smtplib.SMTP(cnf.mailserver, 25)
if (len(sys.argv) < 3):
print "usage: "+sys.argv[0]+" 'subject' 'body text'"
sys.exit()
msg = "Subject: " + sys.argv[1] + "\n\n" + sys.argv[2]
server.sendmail(cnf.mailsource, cnf.maildestination, msg)
server.quit()

View File

@ -10,7 +10,7 @@ else
fi fi
STATSFILE="/run/peoplecounter$DEV" STATSFILE="/run/peoplecounter$DEV"
SAMPLES=20 # how many records to keep in file SAMPLES=20 # how many records to keep in file
INTERVAL=0 # how long to wait between polls INTERVAL=1 # how long to wait between polls
INTERVALSKIP=20 # poll with INTERVAL but only consider every INTERVALSKIP's for SAMPLES INTERVALSKIP=20 # poll with INTERVAL but only consider every INTERVALSKIP's for SAMPLES
MAXFILE="/var/cache/peoplecountermax$DEV" MAXFILE="/var/cache/peoplecountermax$DEV"
PRESENCYRT="/run/presencyrt$DEV" PRESENCYRT="/run/presencyrt$DEV"
@ -59,7 +59,7 @@ while true
do do
# scrape new value # scrape new value
p="$( p="$(
wget -qO - "http://$PEOPLECOUNTERIP/output.cgi?t=$(date +%s)" | wget --tries=3 --timeout=2 -qO - "http://$PEOPLECOUNTERIP/output.cgi?t=$(date +%s)" |
sed 's/.*Occupancy://'| sed 's/.*Occupancy://'|
awk '{print $2}')" awk '{print $2}')"
# echo "p=$p" # debug # echo "p=$p" # debug
@ -113,12 +113,17 @@ do
then then
state="online" state="online"
logger $(basename $0) people counter online logger $(basename $0) people counter online
$(dirname "$0")"/mkmail.py 'peoplecounter level2 online' 'peoplecounter is now reachable'
#python -c 'import smtplib ; server = smtplib.SMTP("mail.syn2cat.lu", 25) ; msg = "Subject: peoplecounter level2 onfline\n\npeoplecounter is now reachable" ; server.sendmail("gkess@pt.lu", "gunstick@syn2cat.lu", msg) ; server.quit()'
fi fi
else else
if [ "$state" = "online" ] if [ "$state" = "online" ]
then then
state="offline" state="offline"
logger $(basename $0) people counter offline logger $(basename $0) people counter offline
$(dirname "$0")"/mkmail.py 'peoplecounter level2 offline' 'peoplecounter was not reachable'
#python -c 'import smtplib ; server = smtplib.SMTP("mail.syn2cat.lu", 25) ; msg = "Subject: peoplecounter level2 offline\n\npeoplecounter was not reachable" ; server.sendmail("gkess@pt.lu", "gunstick@syn2cat.lu", msg) ; server.quit()'
fi fi
fi fi
sleep "$INTERVAL" sleep "$INTERVAL"

View File

@ -3,16 +3,24 @@
if [ "$1" = "closed" ] && [ "$2" = "open" ] if [ "$1" = "closed" ] && [ "$2" = "open" ]
then then
p="$(tail -1 /run/peoplecounter)" p="$(tail -1 /run/peoplecounter)"
if [ "$p" != "0" ] pp="$(tac /run/peoplecounter | awk -v p="$p" 'p!=$1 {print;exit}')" # get previous count. this may fail if people exit and after long time close door
if [ "$pp" = "" ]
then then
logger $(basename $0) There are $p people, closed door from inside. Waiting 1 min. pp=0 # if no variation found, don not do anything
fi
if [ "$p" != "0" ] && # there are people present
[ "$p" -ge "$pp" ] # nobody left i.e. current count is greater or equal to previous count
then
logger $(basename $0) There are $p people, previusly $pp, closed door from inside. Waiting 1 min.
sleep 60 sleep 60
p="$(tail -1 /run/peoplecounter)" np="$(tail -1 /run/peoplecounter)"
if [ "$p" = "0" ] if [ "$np" -lt "$p" ] # new count is lower than previous
then then
logger $(basename $0) There are $p people, after 1 min wait. All OK. logger $(basename $0) There are now $np people, after 1 min wait. All OK.
else else
logger $(basename $0) There are $p people, after 1 min. Maybe ALARM logger $(basename $0) There are $np people, after 1 min. Maybe ALARM
python -c 'import smtplib ; server = smtplib.SMTP("mail.syn2cat.lu", 25) ; msg = "Subject: ALARM level2 intruder\n\ndoor closed with people inside" ; server.sendmail("gkess@pt.lu", "gunstick@syn2cat.lu", msg) ; server.quit()'
fi fi
else else
logger $(basename $0) There are $p people, correctly closed door from outside. All OK. logger $(basename $0) There are $p people, correctly closed door from outside. All OK.