Difference between revisions of "SMS Notifications"

From ZoneMinder Wiki
Jump to navigationJump to search
(25 intermediate revisions by 2 users not shown)
Line 13: Line 13:


There are two ways to send emails in options - email. One is sending a short email, and one is designed for longer messages. In the code (as of 1.30 lines 818 and 918), in zmfilter.pl these are correspondingly sub SendEmail and sub SendMessage. They are distinct functions. SSMTP/MSMTP support is in SendEmail but not in SendMessage.
There are two ways to send emails in options - email. One is sending a short email, and one is designed for longer messages. In the code (as of 1.30 lines 818 and 918), in zmfilter.pl these are correspondingly sub SendEmail and sub SendMessage. They are distinct functions. SSMTP/MSMTP support is in SendEmail but not in SendMessage.
=== MSMTP Specific Install ===
Debian 10 should use MSMTP.
apt-get install msmtp msmtp-mta mutt
Add a working config to /etc/msmtprc (see below for resources on this)
echo "hello" | msmtp -a default user@email.com


=== Testing the email ===
=== Testing the email ===
Line 19: Line 25:


Then:
Then:
* run a filter in the background (see tips below) or execute.  
# run a filter in the background (see tips below) or execute.  
* Enable logging on /var/log/zm folder.  
# Enable logging on /var/log/zm folder.  
* tail the ZMFilter log and syslog.  
# tail the ZMFilter log and syslog.  
* to initiate an alarm (if alarms are used to test emails), use 'force alarm' on the monitor screen. That is, view a camera, and click force alarm.
# to initiate an alarm (if alarms are used to test emails), use 'force alarm' on the monitor screen. That is, view a camera, and click force alarm.


You should see SSMTP send an email on the syslog (or try) and the zmfilter log will report the trigger.
You should see SSMTP send an email on the syslog (or try) and the zmfilter log will report the trigger.
Line 71: Line 77:


====SSMTP Specific Tips====
====SSMTP Specific Tips====
Deleted. See archive if you need these tips for Debian < 10.


* ssmtp.conf has issues with special characters in AuthPass. Do not use special characters for your password.
====MSMTP Specific Tips====


* Add DEBUG=yes to ssmtp.conf to see full email logs in syslog
* MSMTP has been tested to work as an alternative to SSMTP (but configured identically) in Debian Buster / Devuan 3.
 
* Apparmor can impede msmtp logging on Buster (07/2020).
* If SSMTP is unable to connect, there may be a two minute timeout before it reports unable to connect. If it seems like ssmtp has frozen, wait before closing out of it, if you want its logs.
* Cron may require setting an environment variable of EMAIL=sendingaddress@email.com in the crontab
 
* msmtp can use mutt to send attachments. (ssmtp above was setup with mailutils). See below:
* Don't forget to change revaliases as needed. it should match ssmtp.conf
 
* Expert: Manually test email server connections with something like
<pre>
openssl s_client -debug -starttls smtp -crlf -connect smtp.emailprovider.com:465
then continue with
EHLO hostname
AUTH
etc...
</pre>


* ssmtp supports only auth login, and cram-md5. If you have digest-md5 or auth plain at your email server it will likely not work. Test manually using the commands above to verify this.
Compare these cronjobs


* Test ssmtp manually with $ssmtp -vvvvv email@address.com < msg.txt ([https://wiki.archlinux.org/index.php/SSMTP#Sending_email example message]) (though DEBUG=yes has more information)
'''ssmtp/mailutils:'''
 
<code>0 1 1 * * echo hello  | mail -A /cameras/report.log -s "ZM Report Log"  user@email.com</code>
* SSMTP is lacking some authentication methods and is abandoned as of 07/2020, therefore MSMTP is recommended.
 
====MSMTP Specific Tips====
 
* MSMTP has been tested to work as an alternative to SSMTP (but configured identically) in Debian Buster / Devuan 3.


'''msmtp/mutt    :'''
<code>  0 1 1 * * echo hello  | mutt -a /cameras/report.log -s "ZM Report Log" -- user@email.com</code>
<br>
<br>
<br>


=== Example Filter ===
=== Example Filter ===
Line 122: Line 119:


Note that in the author's opinion, run states can become unwieldy for large installations. It's best used with smaller setups only.
Note that in the author's opinion, run states can become unwieldy for large installations. It's best used with smaller setups only.
== Send email if a part of ZM goes offline ==
For any serious deployment you will want to monitor ZM in case it goes offline, or a hdd fails. While it's possible to use a separate system for this (e.g. https://en.wikipedia.org/wiki/Comparison_of_network_monitoring_systems), you can also simply have email alerts in cron. Here is an example script that can watch two hdds (adjust as needed) and the status of ZM and Mysql.
<pre>
#!/bin/bash
/etc/init.d/zoneminder status > /root/zm_up_log
echo "" >> /root/zm_up_log
/etc/init.d/mysql status >> /root/zm_up_log
ps -Ao user,fname,pmem | grep -e zmc -e zma | grep -v zmaudit >> /root/zm_up_log
echo "" >> /root/zm_up_log
echo "" >> /root/zm_up_log
lsblk >> /root/zm_up_log
echo "" >> /root/zm_up_log
echo "" >> /root/zm_up_log
dmesg | grep -e sda -e sdb >> /root/zm_up_log
echo "ZM_Status" | mutt -s "ZM Status" destination@email.com -a /root/zm_up_log
</pre>
This script can be run in crontab as often as you wish. It should also be easy to filter the script to email only if something is out of the ordinary. In fact, let's do that next.
== Send email if HDD Fails ==
<pre>
#!/bin/bash
# 20 * * * *  root /root/hdderror.sh (this script)
# must run as root for access to dmesg in debian
# chmod +x
LOGFILE=/root/file.log
SUBJECT="e.g. Home Camera System HDD Error"
echo "" > $LOGFILE
# any text of "error" with regards to sda,sdb, etc that isn't a remount
dmesg | grep -e sda -e sdb -e sdc -e sdd -e sde | grep -i error | grep -v remount >> $LOGFILE
#returns 1 if nothing
if [ $? -eq 0 ]; then
#send email
  echo "HDD Error..." | mutt -s $SUBJECT destination@email.com -a $LOGFILE
else
  echo "do nothing"
fi
</pre>


==Resources==
==Resources==


 
* [[Email]]
* [http://zoneminder.readthedocs.io/en/latest/faq.html#how-can-i-get-zm-to-do-different-things-at-different-times-of-day-or-week ZM FAQ: How can I get ZM to do different things at different times of day or week]
* [http://zoneminder.readthedocs.io/en/latest/faq.html#how-can-i-get-zm-to-do-different-things-at-different-times-of-day-or-week ZM FAQ: How can I get ZM to do different things at different times of day or week]
* [http://wiki.zoneminder.com/Instant_Notification Instant Notification]
* [http://wiki.zoneminder.com/Instant_Notification Instant Notification]
Line 134: Line 176:
* [https://forums.zoneminder.com/viewtopic.php?f=9&t=12646 Receive Email on Runstate change]
* [https://forums.zoneminder.com/viewtopic.php?f=9&t=12646 Receive Email on Runstate change]
* [https://forums.zoneminder.com/viewtopic.php?f=40&t=29755&p=116634#p116634]
* [https://forums.zoneminder.com/viewtopic.php?f=40&t=29755&p=116634#p116634]
[[Category:Dummies_Guide]]

Revision as of 14:40, 9 October 2022

These instructions are to assist with email or text message setup.

For SMS, You setup email to point to a text gateway. Setup a filter that monitors the cameras for alarms, and when an alarm is set off you have it send an email to the text gateway (or email address). Simple.

You can get images from the alarm embedded in the text (covered in install steps).

The following is tested for 1.30, and 1.34.

Installation

Follow How to get ssmtp working with Zoneminder.

There are two ways to send emails in options - email. One is sending a short email, and one is designed for longer messages. In the code (as of 1.30 lines 818 and 918), in zmfilter.pl these are correspondingly sub SendEmail and sub SendMessage. They are distinct functions. SSMTP/MSMTP support is in SendEmail but not in SendMessage.

MSMTP Specific Install

Debian 10 should use MSMTP.

apt-get install msmtp msmtp-mta mutt

Add a working config to /etc/msmtprc (see below for resources on this)

echo "hello" | msmtp -a default user@email.com

Testing the email

First test via terminal outside of ZM. Confirm it is working there. Example configurations and calls can be found at: https://wiki.debian.org/msmtp and https://wiki.archlinux.org/index.php/MSMTP (for msmtp).

Then:

  1. run a filter in the background (see tips below) or execute.
  2. Enable logging on /var/log/zm folder.
  3. tail the ZMFilter log and syslog.
  4. to initiate an alarm (if alarms are used to test emails), use 'force alarm' on the monitor screen. That is, view a camera, and click force alarm.

You should see SSMTP send an email on the syslog (or try) and the zmfilter log will report the trigger.

Tips

  • Free email services limit how many emails you are allowed to send through SMTP. It helps to disable cron emails.
  • Always make filters for 1 (or low number) results. If you fail to do this, you could try to send dozens of emails.
  • When you make the filter you want the 'current' alarms. You don't want old events. This is accomplished with a Date/Time greater than or equal to -1 minute.
  • If when testing the filter execute, the 'execute' button becomes unclickable, uncheck then recheck the email option.
#watch "tail -n 30000 /var/log/syslog | grep sSMTP | tail"
  • When testing use a background filter, and watch the logs. Filters run every 60 seconds by default.
  • If you send email as someone other than root, make sure he is in /etc/ssmtp/revaliases
  • test emails with
cat file | mail -s "subject" destination@email.com
OR
echo "some text" | mail -s "subject destination@email.com
FOR ATTACHMENTS:
echo hello | mail -A /dir/to/attachment.log -s "email with file" destination@email.com
OR
Also try mutt (slightly different)
mutt -a /dir/to/attachment.log -s "email with file" -- destination@email.com
  • make sure email is checked off in filters. It gets unchecked easily.
  • service zoneminder restart after changes (NOTE: this wasn't required for just setting email settings in ZM options 07/2020).
  • test setting off alarms by clicking 'force alarm' in the monitor view, then unclick to remove alarm.
  • make one dedicated email alert account if you want multiple users to receive alerts. There is a patch in the forums to add multiple recipients.
  • It's tough to use Motion detection for alerts with IR active.. You might be best off using a device with ZMTrigger, or the cameras built in motion detection if night IR is used... False alerts appear when a camera switches from Day lens to IR.
  • Always double check the filters are right, AFTER you save them.
  • Once an email has been sent for an event, that event is marked and an email for it won't be sent again[1].

SSMTP Specific Tips

Deleted. See archive if you need these tips for Debian < 10.

MSMTP Specific Tips

  • MSMTP has been tested to work as an alternative to SSMTP (but configured identically) in Debian Buster / Devuan 3.
  • Apparmor can impede msmtp logging on Buster (07/2020).
  • Cron may require setting an environment variable of EMAIL=sendingaddress@email.com in the crontab
  • msmtp can use mutt to send attachments. (ssmtp above was setup with mailutils). See below:

Compare these cronjobs

ssmtp/mailutils: 0 1 1 * * echo hello | mail -A /cameras/report.log -s "ZM Report Log" user@email.com

msmtp/mutt  : 0 1 1 * * echo hello | mutt -a /cameras/report.log -s "ZM Report Log" -- user@email.com


Example Filter

For monitoring on the weekend you might have

date/time greater than or equal to -1 minute AND
alarm frames greather than or equal to 1 AND
Weekday equal to Saturday OR
Weekday equal to Sunday AND
alarm frames greater than or equal to 1 AND
date/time greater than or equal to -1 minute

Notice that you must include all parameters for each group/set individually when there is an OR.

Send email if only on a certain runstate (Filter on Run States)

Notifications are easy if you want alarms emailed between a static time, for example between 12AM and 6AM. If you have a situation where you may not want the alarms to send you text messages on occasion, you can filter on run states.

Starting from (ZM > 1.30.4) you can set filters based on run states, which means you can change the run state at any desired time (using Cron, or manually), and only if a runstate matches the filter will SMS be sent.

Note that in the author's opinion, run states can become unwieldy for large installations. It's best used with smaller setups only.

Send email if a part of ZM goes offline

For any serious deployment you will want to monitor ZM in case it goes offline, or a hdd fails. While it's possible to use a separate system for this (e.g. https://en.wikipedia.org/wiki/Comparison_of_network_monitoring_systems), you can also simply have email alerts in cron. Here is an example script that can watch two hdds (adjust as needed) and the status of ZM and Mysql.

#!/bin/bash
/etc/init.d/zoneminder status > /root/zm_up_log
echo "" >> /root/zm_up_log
/etc/init.d/mysql status >> /root/zm_up_log
ps -Ao user,fname,pmem | grep -e zmc -e zma | grep -v zmaudit >> /root/zm_up_log
echo "" >> /root/zm_up_log
echo "" >> /root/zm_up_log
lsblk >> /root/zm_up_log
echo "" >> /root/zm_up_log
echo "" >> /root/zm_up_log
dmesg | grep -e sda -e sdb >> /root/zm_up_log

echo "ZM_Status" | mutt -s "ZM Status" destination@email.com -a /root/zm_up_log

This script can be run in crontab as often as you wish. It should also be easy to filter the script to email only if something is out of the ordinary. In fact, let's do that next.

Send email if HDD Fails

#!/bin/bash
# 20 * * * *   root /root/hdderror.sh (this script)
# must run as root for access to dmesg in debian
# chmod +x 

LOGFILE=/root/file.log
SUBJECT="e.g. Home Camera System HDD Error"

echo "" > $LOGFILE

# any text of "error" with regards to sda,sdb, etc that isn't a remount
dmesg | grep -e sda -e sdb -e sdc -e sdd -e sde | grep -i error | grep -v remount >> $LOGFILE
#returns 1 if nothing

if [ $? -eq 0 ]; then
#send email
  echo "HDD Error..." | mutt -s $SUBJECT destination@email.com -a $LOGFILE
else
  echo "do nothing"
fi

Resources