Difference between revisions of "SMS Notifications"

From ZoneMinder Wiki
Jump to navigationJump to search
 
(67 intermediate revisions by 2 users not shown)
Line 1: Line 1:
You setup email to point to a [https://en.wikipedia.org/wiki/List_of_SMS_gateways text gateway]. Setup a filter that monitors the cameras for alarms, and when an alarm
These instructions are to assist with email or text message setup.
is set off you have it send an email to the text gateway. Simple.
 
For SMS, You setup email to point to a [https://en.wikipedia.org/wiki/List_of_SMS_gateways 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).
You can get images from the alarm embedded in the text (covered in install steps).
The following is used in 1.36 / Debian 10 and newer.


== Installation ==
== Installation ==


Follow [https://wiki.zoneminder.com/How_to_get_ssmtp_working_with_Zoneminder How to get ssmtp working with Zoneminder].  
Follow [https://wiki.zoneminder.com/How_to_get_ssmtp_working_with_Zoneminder 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 and newer should use MSMTP.
apt-get install msmtp msmtp-mta mutt
=== 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).
Once you get a working /etc/msmtprc configuration, try:
echo "hello" | msmtp -a default user@email.com
Watch out for the following:
* Make sure app-armor is disabled for msmtp. The following commands will help: apt install apparmor-utils and then aa-disable /etc/apparmor.d/usr.bin.msmtp
=== Testing the email in Zoneminder ===
Then:
# run a filter in the background (see tips below) or execute.
# Enable logging on /var/log/zm folder.
# 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.
You should see SSMTP send an email on the syslog (or try) and the zmfilter log will report the trigger.


=== Tips ===
=== Tips ===


When testing be aware of the following:
* Free email services may limit how many emails you are allowed to send.
* Free email services limit how many emails you are allowed to send through SMTP. It helps to disable cron emails.
 
* Always test by emailing 1 (or similar low number) results.  
* 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.  
* 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.
* If when testing the filter execute, the 'execute' button becomes unclickable, uncheck then recheck the email option.


* If you send email as someone other than root you may need to include the user in revaliases.
* test email attachments with one of these:
<pre>
<pre>
#watch "tail -n 30000 /var/log/syslog | grep sSMTP | tail"
echo hello | mail -A /dir/to/attachment.log -s "email with file" destination@email.com
mutt -a /dir/to/attachment.log -s "email with file" -- destination@email.com
echo "test" | mutt -d 5 -s "Test Email" user@email.com -a /tmp/file_attachment
</pre>
</pre>


* When testing use submit at first, not execute. Submit will show you results but not email.
* make sure email is checked off in filters.  


* If you send email as someone other than root, make sure he is in /etc/ssmtp/revaliases
* service zoneminder restart may be needed after changes (depends upon ZM version)


* test emails with
* test setting off alarms by clicking 'force alarm' in the camera monitor view, then unclick to remove alarm.
<pre>
 
cat file | mail -s "subject" destination@email.com
* If you find false alerts with motion detection, consider using either ZMES, or passing the event to an object detection based script.
OR
echo "some text" | mail -s "subject destination@email.com
</pre>


* make sure email is checked off in filters. It gets unchecked easily.
* Always double check the filters are right, AFTER you save them.


* service zoneminder restart in between changes
* Once an email has been sent for an event, that event is marked and an email for it '''won't be sent again'''[https://forums.zoneminder.com/viewtopic.php?f=8&t=7774&p=27386&].


* test setting off alarms by clicking 'force alarm' in the monitor view
* Cron may require setting an environment variable of EMAIL=sendingaddress@email.com in the crontab


=== Example Filter ===
=== Example Filter ===


For monitoring on the weekend you might have
See [[Filters]]
 
== 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 [http://www.github.com/zoneminder/zoneminder/issues/1750 (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.
<pre>
<pre>
date/time greater than or equal to -1 minute AND
#!/bin/bash
alarm frames greather than or equal to 1 AND
/etc/init.d/zoneminder status > /root/zm_up_log
Weekday equal to Saturday OR
echo "" >> /root/zm_up_log
Weekday equal to Sunday AND
/etc/init.d/mysql status >> /root/zm_up_log
alarm frames greater than or equal to 1 AND
ps -Ao user,fname,pmem | grep -e zmc -e zma | grep -v zmaudit >> /root/zm_up_log
date/time greater than or equal to -1 minute
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>
</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.


Notice that you must include all parameters for each group/set individually when there is an OR.
== 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"


== Notes ==
echo "" > $LOGFILE


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, this requires work.
# 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


A [http://www.github.com/zoneminder/zoneminder/issues/1750 feature in the works] would allow you to set filters based on [https://wiki.zoneminder.com/filter_on_run_states run states], which would mean, you could change the run state at any desired time (using [[Cron]]), and only then would SMS be sent. Until then, you must have duplicate monitors and have the filters activated on only one of them. If you like, you can compile zoneminder from a [https://wiki.debian.org/BuildingTutorial package source] such as the debian package and add the commits manually at your own risk (it is not difficult, but beware updating).
if [ $? -eq 0 ]; then
#send email
  echo "HDD Error..." | mutt -s $SUBJECT destination@email.com -a $LOGFILE
else
  echo "do nothing"
fi
 
</pre>
 
== Basic ZM Server Monitor ==
These would go in /etc/crontab
<pre>
1 0 * * 1 root dmesg | tail -10 > /tmp/hdd_report
2 0 * * 1 root lsblk  >> /tmp/hdd_report
3 0 * * 1 root ls -lt /videos/ >> /tmp/hdd_report
4 0 * * 1 root df -h >> /tmp/hdd_report
5 0 * * 1 root echo "zm server" | mutt -s "zm server" user@email.com -a /tmp/hdd_report
</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 71: Line 146:
* [https://wiki.zoneminder.com/How_to_get_ssmtp_working_with_Zoneminder How to get ssmtp working with Zoneminder]
* [https://wiki.zoneminder.com/How_to_get_ssmtp_working_with_Zoneminder How to get ssmtp working with Zoneminder]
* [https://forums.zoneminder.com/viewtopic.php?f=36&t=25678&p=98158]
* [https://forums.zoneminder.com/viewtopic.php?f=36&t=25678&p=98158]
* [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]
[[Category:Dummies_Guide]]

Latest revision as of 04:41, 9 September 2023

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 used in 1.36 / Debian 10 and newer.

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 and newer should use MSMTP.

apt-get install msmtp msmtp-mta mutt

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).

Once you get a working /etc/msmtprc configuration, try:

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

Watch out for the following:

  • Make sure app-armor is disabled for msmtp. The following commands will help: apt install apparmor-utils and then aa-disable /etc/apparmor.d/usr.bin.msmtp

Testing the email in Zoneminder

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 may limit how many emails you are allowed to send.
  • 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.
  • If you send email as someone other than root you may need to include the user in revaliases.
  • test email attachments with one of these:
echo hello | mail -A /dir/to/attachment.log -s "email with file" destination@email.com
mutt -a /dir/to/attachment.log -s "email with file" -- destination@email.com
echo "test" | mutt -d 5 -s "Test Email" user@email.com -a /tmp/file_attachment
  • make sure email is checked off in filters.
  • service zoneminder restart may be needed after changes (depends upon ZM version)
  • test setting off alarms by clicking 'force alarm' in the camera monitor view, then unclick to remove alarm.
  • If you find false alerts with motion detection, consider using either ZMES, or passing the event to an object detection based script.
  • 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].
  • Cron may require setting an environment variable of EMAIL=sendingaddress@email.com in the crontab

Example Filter

See Filters

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

Basic ZM Server Monitor

These would go in /etc/crontab

1 0 * * 1 root dmesg | tail -10 > /tmp/hdd_report
2 0 * * 1 root lsblk  >> /tmp/hdd_report
3 0 * * 1 root ls -lt /videos/ >> /tmp/hdd_report
4 0 * * 1 root df -h >> /tmp/hdd_report
5 0 * * 1 root echo "zm server" | mutt -s "zm server" user@email.com -a /tmp/hdd_report

Resources