Difference between revisions of "SMS Notifications"
(→Tips) |
|||
(61 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
These instructions | These are general email instructions. Eventually I will merge them to the Email page. | ||
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 | 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 | ||
Line 6: | Line 6: | ||
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 | The following is used in 1.36 / Debian 10 and newer. | ||
== Installation == | == Installation == | ||
Line 12: | Line 12: | ||
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 support is in SendEmail | 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 === | === Testing the email === | ||
First, run a filter in the background (see tips below) | 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. | You should see SSMTP send an email on the syslog (or try) and the zmfilter log will report the trigger. | ||
Line 22: | Line 39: | ||
=== Tips === | === Tips === | ||
* Free email services limit how many emails you are allowed to send | * 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. | * Always make filters for 1 (or low number) results. If you fail to do this, you could try to send dozens of emails. | ||
Line 30: | Line 47: | ||
* 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> | ||
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> | ||
* | * make sure email is checked off in filters. | ||
* | * service zoneminder restart may be needed after changes (depends upon ZM version) | ||
* test | * 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'''[https://forums.zoneminder.com/viewtopic.php?f=8&t=7774&p=27386&]. | ||
* | * Cron may require setting an environment variable of EMAIL=sendingaddress@email.com in the crontab. When testing this from the shell, you must do the same but with export, e.g. "export EMAIL=sendingaddress@email.com" | ||
=== 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 [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. | |||
== Examples of Emails for Monitoring == | |||
=== Send Email if ZM is Not Running === | |||
Here is a minimal script to check that Zoneminder is running. Note that this does not check for mysql errors or hard drive errors (which are possible). Do not put any spaces in the subject or email. Also, you may want to monitor that the server is actually powered on, from another computer (as if the server shuts down for any reason, this will obviously not run). | |||
<pre> | <pre> | ||
#!/bin/bash | |||
then | |||
SUBJECT="Name_of_server_ZM" | |||
EMAIL="emailaddress" | |||
/etc/init.d/zoneminder status 2> /dev/null | |||
if test $? -ne 0 ; then | |||
echo "Detected ZM not running" | |||
echo "ZM is not running" | mutt -s $SUBJECT $EMAIL | |||
else | |||
echo "ZM is running, do nothing" | |||
fi | |||
</pre> | </pre> | ||
* | === Send Email if HDD Fails === | ||
This script will give you 1 email an hour, if a hdd has an error. This is an alternative way of checking hdd status instead of S.M.A.R.T. . | |||
<pre> | |||
#!/bin/bash | |||
# 20 * * * * root /root/hdderror.sh (this script, and the root assumes you are using /etc/crontab) | |||
# 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 | |||
echo "email sent" | |||
else | |||
echo "do nothing" | |||
fi | |||
</pre> | |||
=== Send Email if Camera is Offline === | |||
This script will ping an ip address 10 times, then if there is an error, it will try again 10 more times. If it still cannot connect, an email will be sent. There is a lock file, so that only one email is sent every couple hours. | |||
<pre> | <pre> | ||
#!/bin/bash | |||
SERVERIP=$1 | |||
LOGFILE=$1_$(date +%A)_LOG | |||
HISTORYFILE=$1_$(date +%A)_LOCKFILE | |||
date/ | NOTIFYEMAIL=email_address | ||
#setup this script in cron once an hour (or as frequent as you want), and also | |||
#crontab requires historyfile / lockfile to be blanked (echo "" > file) each day or each hour, whatever you prefer. | |||
#make the log directory: | |||
#mkdir /var/log/networkalerts | |||
#this script will run like this: | |||
#e.g. $ test_up.sh <ipaddress> (test_up is this script) | |||
# in /etc/crontab | |||
#0 * * * * root /root/test_up.sh 192.168.1.1 | |||
#0 */5 * * * root rm /var/log/networkalerts/*LOCKFILE | |||
#0 0 * * * root rm /var/log/networkalerts/*$(date +%A)*LOG | |||
#check for lock file | |||
if test -s /var/log/networkalerts/$HISTORYFILE | |||
then | |||
exit | |||
fi | |||
#keep track of time | |||
date >> /var/log/networkalerts/$LOGFILE | |||
ping -c 10 $SERVERIP >> /var/log/networkalerts/$LOGFILE | |||
#if return val is error 1 (but not 2) (see man on ping regarding count and deadline)(success return is zero) | |||
if test $? == 1 | |||
then | |||
sleep 60 | |||
else | |||
exit | |||
fi | |||
== | #reduce false alerts, run it once again to be certain. | ||
ping -c 10 $SERVERIP >> /var/log/networkalerts/$LOGFILE | |||
if test $? == 1 | |||
then | |||
echo "$SERVERIP is down" | msmtp -a default $NOTIFYEMAIL | |||
#lock file / history file | |||
echo "alertsent" > /var/log/networkalerts/$HISTORYFILE | |||
else | |||
exit | |||
fi | |||
</pre> | |||
</pre> | |||
=== Basic Server Monitor === | |||
These would go in /etc/crontab. You might run this once a week, to monitor a server. | |||
<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> | |||
==Troubleshooting== | |||
===Error sending message, child exited 127 (Exec error.).=== | |||
If when sending something like the following you get the above error: | |||
echo "message" | mutt -s "cameras_email" user@email -a /tmp/attachment.txt | |||
Error sending message, child exited 127 (Exec error.). | |||
You need to install msmtp-mta. | |||
==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 115: | Line 206: | ||
* [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=9&t=12646 Receive Email on Runstate change] | ||
* [https://forums.zoneminder.com/viewtopic.php?f=40&t=29755&p=116634#p116634] | |||
* https://linux.die.net/man/8/logcheck - logcheck / logfile scanner. | |||
[[Category:Dummies_Guide]] |
Latest revision as of 12:17, 4 October 2024
These are general email instructions. Eventually I will merge them to the Email page.
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:
- 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
- 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. When testing this from the shell, you must do the same but with export, e.g. "export EMAIL=sendingaddress@email.com"
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.
Examples of Emails for Monitoring
Send Email if ZM is Not Running
Here is a minimal script to check that Zoneminder is running. Note that this does not check for mysql errors or hard drive errors (which are possible). Do not put any spaces in the subject or email. Also, you may want to monitor that the server is actually powered on, from another computer (as if the server shuts down for any reason, this will obviously not run).
#!/bin/bash SUBJECT="Name_of_server_ZM" EMAIL="emailaddress" /etc/init.d/zoneminder status 2> /dev/null if test $? -ne 0 ; then echo "Detected ZM not running" echo "ZM is not running" | mutt -s $SUBJECT $EMAIL else echo "ZM is running, do nothing" fi
Send Email if HDD Fails
This script will give you 1 email an hour, if a hdd has an error. This is an alternative way of checking hdd status instead of S.M.A.R.T. .
#!/bin/bash # 20 * * * * root /root/hdderror.sh (this script, and the root assumes you are using /etc/crontab) # 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 echo "email sent" else echo "do nothing" fi
Send Email if Camera is Offline
This script will ping an ip address 10 times, then if there is an error, it will try again 10 more times. If it still cannot connect, an email will be sent. There is a lock file, so that only one email is sent every couple hours.
#!/bin/bash SERVERIP=$1 LOGFILE=$1_$(date +%A)_LOG HISTORYFILE=$1_$(date +%A)_LOCKFILE NOTIFYEMAIL=email_address #setup this script in cron once an hour (or as frequent as you want), and also #crontab requires historyfile / lockfile to be blanked (echo "" > file) each day or each hour, whatever you prefer. #make the log directory: #mkdir /var/log/networkalerts #this script will run like this: #e.g. $ test_up.sh <ipaddress> (test_up is this script) # in /etc/crontab #0 * * * * root /root/test_up.sh 192.168.1.1 #0 */5 * * * root rm /var/log/networkalerts/*LOCKFILE #0 0 * * * root rm /var/log/networkalerts/*$(date +%A)*LOG #check for lock file if test -s /var/log/networkalerts/$HISTORYFILE then exit fi #keep track of time date >> /var/log/networkalerts/$LOGFILE ping -c 10 $SERVERIP >> /var/log/networkalerts/$LOGFILE #if return val is error 1 (but not 2) (see man on ping regarding count and deadline)(success return is zero) if test $? == 1 then sleep 60 else exit fi #reduce false alerts, run it once again to be certain. ping -c 10 $SERVERIP >> /var/log/networkalerts/$LOGFILE if test $? == 1 then echo "$SERVERIP is down" | msmtp -a default $NOTIFYEMAIL #lock file / history file echo "alertsent" > /var/log/networkalerts/$HISTORYFILE else exit fi
Basic Server Monitor
These would go in /etc/crontab. You might run this once a week, to monitor a server.
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
Troubleshooting
Error sending message, child exited 127 (Exec error.).
If when sending something like the following you get the above error:
echo "message" | mutt -s "cameras_email" user@email -a /tmp/attachment.txt Error sending message, child exited 127 (Exec error.).
You need to install msmtp-mta.
Resources
- ZM FAQ: How can I get ZM to do different things at different times of day or week
- Instant Notification
- Email Events When Zone Triggered, SMS When Different Zone Triggered
- Event notifications do not work
- How to get ssmtp working with Zoneminder
- [2]
- Receive Email on Runstate change
- [3]
- https://linux.die.net/man/8/logcheck - logcheck / logfile scanner.