How to use SSMTP (MTA) within a Docker-Zoneminder container on Ubuntu 19.10

In this tutorial we are going to use “Docker: Enterprise Container Platform”  (docker.io) to install “quantumobject/docker-zoneminder” on Ubuntu 18.10 operating system.
Then SSMTP (MTA) is installed within the Zoneminder container created using quantumobject/docker-zoneminder .
The “quantumobject/docker-zoneminder  which  is based on  zoneminder master packages has been maintained and uploaded to the Docker Hub by Angel Rodriguez-Rodiguez-Riccrdo.
Installation procedure is given below.
Installation of Docker on Ubuntu 19.10
Open the Ubuntu terminal and run
sudo apt install docker.io

On the Ubuntu terminal
sudo docker network create net
( I have used my timezone as Asia/Colombo . Your time zone can be get from here  )The above commands will pull mysql/mysql-server:5.7 and create a container named “db” on the Docker Platform
Then on the Ubuntu terminal
sudo docker run -d -e TZ=Asia/Colombo -e MYSQL_USER=zmuser -e MYSQL_PASSWORD=zmpass -e MYSQL_DATABASE=zm -e MYSQL_ROOT_PASSWORD=mysqlpsswd -e MYSQL_ROOT_HOST=% –net net –name db mysql/mysql-server:5.7

The above commands will pull mysql/mysql-server:5.7 from docker hub and create a container named “db”

sudo docker run -d –shm-size=4096m -e TZ=Asia/Colombo -e ZM_DB_HOST=db –net net –name zm -p 8085:80 –privileged=true quantumobject/docker-zoneminder

The above commands will pull quantumobject/docker-zoneminder from the Docker Hub and create a container named “zm”
(Here ,I have used port 8085 for the ZM panal )
( You can  notice  “- -device=/dev/video0:/dev/video0” in above command which facilitate adding USB cameras on Zoneminder/Docker . You can use “- -privileged=true” also instead of  “- -device=/dev/video0:/dev/video0”)

Note:-It is observed that there are syntax  errors in above commends after  posting the blog due to some shortcomings in the wordpress editor . Therefore please refer the following link to get the correct commands

https://bkjaya.weebly.com/blog/how-to-use-ssmtp-mail-transfer-agent-within-a-docker-zoneminder-container-on-uibuntu-1910


In order to appear ZM panel , you will have to start above two containers “db” and “zm” using the terminal as follows.
sudo docker start db zm

Then open the web browser http://localhost:8085/zm/

screenshot-from-2019-11-26-19-17-28_orig

Figure:- 1 zm console with USB camera

Open the Ubuntu terminal and run
sudo docker exec -t -i zm /bin/bash
Then the following prompt will apear
root@79ae35734aea:/#
Now you are inside the ZM container
Then enter  following commands to install SSMTP within the ZM container
apt update
apt install ssmtp
Now the SSMTP is installed wihin the ZM container.
If you go to /etc/ssmtp/ you can see two ssmtp configuration files namely ssmtp.conf and
revaliases
You will have to edit above files to suit your email requirements . As the editing the files within the container is difficult, ssmtp.conf and revaliases are created wihin the host and copy in to the container .
Let us create ssmtp.conf and revaliases files at the home folder of host
Go to the home directory of the host and open the Ubuntu terminal
sudo gedit ssmtp.conf
Then enter the following scripts in the opened file and save
root=youer email@gmail.com
mailhub=smtp.gmail.com:587
hostname=localhost
RewriteDomain=gmail.com
UseSTARTTLS=YES
UseTLS=YES
AuthUser=youer email@gmail.com
AuthPass=Your gmail password

———————————————————————————————————————
sudo gedit revaliases
Then enter the following scripts in the opened file and save
root:youer email@gmail.com:smtp.gmail.com:587
www-data:youer email@gmail.com:smtp.gmail.com:587
———————————————————————————————————————-

Now you will have to copy two files ssmtp.conf and revaliases in to the folder /etc/ssmtp/  of the ZM container using following commands on the Ubuntu terminal
sudo docker cp ssmtp.conf zm:/etc/ssmtp/ssmtp.conf
sudo docker cp revaliases zm:/etc/ssmtp/revaliases
Now ZM container is ready for sending emails from localhost/zm
Then the Email details have to be entered and save at ZM-Options/Email as shown in the following  figure.

screenshot-from-2019-11-26-19-22-09_orig

 

screenshot-from-2019-11-26-19-23-05_orig

Figure:- 2 Scripts in ssmtp.conf and revaliases files

 

screenshot-from-2019-11-26-19-18-17_orig

screenshot-from-2019-11-26-19-19-07_orig

Figure:- 3 Entering  email detalis

 

The a ZM filter has to be created as shown in the following figure and execute to be able to send emails alerts to your email address.

screenshot-from-2019-11-26-22-29-28_orig

Figure:- 4 Filter created for sending email alerts

screenshot-from-2019-11-26-19-15-46_orig

screenshot-from-2019-11-26-19-16-04_orig

Figure:- 5 Email alerts on motion detection on Aol mail

How to make docker-zoneminder auto start at boot

To start you will have to start docker containers db and zm

First I have tried to use a single script file to start db and zm , but it failed to start at boot.

Therefore I had to do small trick to achieve the target by creating two inter linked script files namely zmdockerstart.sh and bkj.sh  in the Documents directory as follows

Screenshot from 2019-11-27 18-19-55

Figure:- 6 zmdockerstart.sh

Screenshot from 2019-11-27 18-20-28

Figure:- 7 bkj.sh

To make this scripts executable

On the Ubuntu terminal run

sudo chmod +x zmdockerstart.sh

sudo chmod +x bkj.sh

To start zmdockerstart.sh without root password visudo command is used.

On the Ubuntu terminal

sudo visudo

When the /etc/sudoers.tmp file is opened , enter the following command at the bottom of the file . Then press Ctrl o and Ctrl x to save and exit

bkjaya1952 ALL=(ALL:ALL) NOPASSWD:/home/bkjaya1952/Documents/zmdockerstart.sh

Screenshot from 2019-11-27 21-10-40

Figure:- 8 Editing /etc/sudoers.tmp to start zmdockerstart.sh without root password

 

When you double click on bkj.sh file  , it connects with the zmdockerstart.sh file and then start db and zm docker containers .

Then I have  included  bkj.sh in the startup programmes  and but failed to start at boot.

When finding the reason for not starting , I have found the important  file in this connection at   /etc/security/pam_env.conf

In the pam_env.conf the specified  default paths have  been defined as

PATH DEFAULT=${HOME}/bin:/usr/local/bin:/bin\
:/usr/bin:/usr/local/bin/X11:/usr/bin/X11

Then I have copied bkj.sh to /usr/bin and the path  included to the startup progarmmes .

sudo cp ~/Documents/bkj.sh /usr/bin

(note :- copying script files to /usr/bin is not a good practice. Instead of copying to /usr/bin you also can include the file path in  pam_env.conf file as an alternative)

Then It was found that the db and zm started successfully at boot

 

Screenshot from 2019-11-27 18-21-57

Figure:- 9 Adding bkj.sh to the startup programmes

 

 

 

 

 

 

 

 

A Solution to the installation problem of ZONEMINDER,v1.33.14.ON UBUNTU 19.10 (Eoan Ermine) using Mriadb instead of mysql-server-8.0

There is a problem in installing Zoneminder on Ubuntu 19.10 , as  there are no official zoneminder eoan ermine versions  issued up to now.

The other problem is the presently available Zoneminder  versions are based on mysql 5.7 , libmysqlclient20  while The Ubuntu 19.10 uses mysql 8 , libmysqlclient21 by default. Therefore dependency issues crop up when you try to install Zoneminder.

As mysql-server-8.0  makes some problems, I have decided to use  mariadb-server-10.3 .

To overcome dependency issues in installing Zoneminder,   libmysqlclient20 has to be downloaded and installed manually before installing Zoneminder using GDebi Package Installer

Please refer the following link to download libmysqlclient20 for Ubuntu  Disco

https://www.ubuntuupdates.org/package/core/disco/main/updates/libmysqlclient20

Screenshot from 2019-11-03 11-29-30

Figure:- 1 installed libmysqlclient20 is shown in the synaptic

After installing the deb file libmysqlclient20, Install  maridb-server-10.3 using synaptic package manager.

Then the installed packages are shown as the following figure

Screenshot from 2019-11-03 11-27-30

Figure:- 2 installed mairadb packages

Mariadb installation

Once mariadb-sever installed using the synaptc package manager , the mariadb-sever automatically starts.

To check the states you can use one of the following 2 commands on the Ubuntu terminal

systemtcl status mysql

or

systemtcl status mariadb

If it is not running

try

sudo systemtcl start mariadb

To enable auto start at boot time , run

sudo systemtcl enable mariadb

To secure mariadb  sever run

sudo_mysql_sequre_installation

When the script ask you to enter password for root , you can enter your password for the server

Screenshot from 2019-11-03 11-24-56

Figure:- 3

As the   mariadb sever and the dependency software  for zoneminder ,libmysqlclient20 are installed , now it is ready to install zoneminder .

I have selected Zonminder master  for installation.

To install Zoneminder Please refer Connor’s web site

Adding PPA to Ubuntu 19.10 (Eoan Ermine) is not possible as there is no official zoneminder version . Therefore the zoneminder deb file of ubuntu disco is downloaded and installed using GDebi Package Installer.

 

Note:- As we have installed libmysqlclient20 , there won’t be dependency issue

sudo su

rm /etc/mysql/my.cnf

cp /etc/mysql/mariadb.conf.d/50-server.cnf  /etc/mysql/my.cnf

gedit /etc/mysql/my.cnf

add the following line below [mysqld]

sql_mode = NO_ENGINE_SUBSTITUTION

and save the file

Screenshot from 2019-11-03 16-09-34

Figure:- 4

 

systemctl restart mysql

mysql -uroot -p < /usr/share/zoneminder/db/zm_create.sql

mysql -uroot -p -e "grant all on zm.* to 
'zmuser'@localhost identified by 'zmpass';"

(In above steps , when ask to enter the pass ,you will have to enter the root password of mysql)

mysqladmin -uroot -p reload
chmod 740 /etc/zm/zm.conf
chown root:www-data /etc/zm/zm.conf
adduser www-data video
a2enmod cgi
a2enconf zoneminder
a2enmod rewrite
chown -R www-data:www-data /usr/share/zoneminder/
systemctl enable zoneminder
service zoneminder start

To add  timezone to PHP

gedit /etc/php/7.3/apache2/php.ini

edit line 956 with your timezone

Screenshot from 2019-11-03 16-25-52

Figure:- 5 php.ini after adding timezone

 

Screenshot from 2019-11-03 11-19-23

Figure:-6

 

Then save the file php.ini
Your time zone can be get from here

Then run on the Ubuntu terminal

service apache2 reload
Optional …..You can run zonemider without cambozola.   Adding cambozola.jar to the directory /usr/share/zoneminder/www/ Download the latest cambozola and extract to the Downloads directory of your PC Then copy the cambozola.jar to the directory /usr/share/zoneminder/www/ On the ubuntu terminal run sudo cp /home/bkjaya1952/Downloads/cambozola-latest/cambozola-0.936/dist/cambozola.jar /usr/share/zoneminder/www/

Open zoneminder web console (http://localhost/zm/)

Now you can add your camera to using the ZM-Console

Following figures shows the status after adding the USB camera

Screenshot from 2019-11-03 11-16-44

Figure:- 7 Adding usb camera to the monitor

Screenshot from 2019-11-03 11-15-34

 

 

Figure:-8 ZM-Console & ZM-Monitor

To avoid errors pertain to  Time , you will have to re enter the TIMEZONE in ZM as follows.

In the ZM-Console , go to Options/System/TIMEZONE and select your date.timezone entered in the  php.ini file at /etc/php/7.3/apache2/  previously . Then save the setting.

 

 

 

 

 

 

 

 

 

How to use MSMTP as email software in Zoneminder (v1.33.14.) on Ubuntu 19.04

The  software MSMTP is a latest  SMTP client runs on a wide variety of platforms. It seems msmtp is overtaking ssmtp which is a software of similar nature.

At present the Zoneminder has been  programmed  to use ssmtp as an email software under NEW_MAIL_MODULES.

To send email alerts in zmfilter.pl , a programme has been written to use MIME::Lite module  which uses “sendmail” command.

The worked example in this blog post shows how to use MSMTP  insted of SSMPT  in Zoneminder to send email alerts of motion detection events.

 

First you will have to install MSMTP  ( If  ssmtp , sendmail  available , please uninstall completely )

Open the Ubuntu terminal and run

sudo apt install msmtp

After installing , you can find the main executable file msmtp at /usr/binScreenshot from 2019-10-21 11-17-51

Figure:- 1 msmtp file created at /usr/bin/

Then create   sendmail file which a symlink of  msmtp ( at /usr/bin)  in /usr/sbin/ .

Run on the Ubuntu terminal

sudo ln -s /usr/bin/msmtp /usr/sbin/sendmail

Note: before making above symlink , make sure that there is no sendmail file in /usr/bin

When zmfilter.pl searchs  for /usr/sbin/sendmail , it redirect to msmtp which is the main executable file at /usr/bin/.

Screenshot from 2019-10-20 22-51-42

Figure:-2 sendmail file at /usr/sbin

Now you will have to create a configuration file called msmtprc which contains email hosts ,ports ,certificates ,email details etc at the directory /etc/

Creating a ~/.msmtprc file in home directory is not essential as we are making a file at /etc/ . Then msmtp   by default , connects with    msmtprc at /etc/

On the Ubuntu terminal

sudo gedit /etc/msmtprc

Screenshot from 2019-10-21 11-13-49

Figure:- 3 msmtprc at /etc/

When the empty file is opened, enter the following scripts

# Set default values for all following accounts.
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log

# Gmail
account gmail
host smtp.gmail.com
port 587
from your gmail address
user your gmail address
password your gmail password

# Set a default account
account default : gmail

 

Screenshot from 2019-10-21 22-16-22

Figure:-4 Scrips in /etc/msmtprc

To add group www-data to the file

Then on the Ubuntu terminal

sudo chown root:www-data /etc/msmtprc

Now you can try a test mail on the terminal

echo “Subject: sendmail test” | sendmail -v bkjaya1952@aol.com

Note : replace bkjaya1952@aol.com with your receiving email

How to make changes in Zoneminder for MSMTP

It is assumed that the Zoneminder software is already installed in your Ubuntu PC

Please refer the following link for installation guide for Zoneminder

HOW TO INSTALL ZONEMINDER, v1.33.0. ON UBUNTU 18.10 /19.04

Screenshot from 2019-10-21 11-07-57

Figure:- 5 ZM-console

Go to Options/Email on the ZM-Console

And  tick boxes and make changes in the cages as sown in following two figures

 

Screenshot from 2019-10-21 22-43-28

Figure:- 6 Upper part of the page at Options/Email

Please note the following additional entries under EMAIL_BODY cage

Attach (first) event image with the highest score : %EIM%

Attach event mpeg video : %EV%

 

Screenshot from 2019-10-21 11-10-46

Figure:-7 Lower part of the page at Options/Email

Making a filter to initiate email process

Make a filter as shown in the figure below and execute

Screenshot from 2019-10-21 09-47-52

Figure:-8 filter to select events of maximum score is > or= to 5 and send email alerts

Screenshot from 2019-10-21 11-26-15

Figure:- 9 Zm-System Log showing email sending process

Following 2 figures show the received emails

Screenshot from 2019-10-21 11-11-31

Figure:- 10 received email alerts

Screenshot from 2019-10-21 12-44-34

 

Figure:- 11 received email alerts

 

 

 

 

 

 

How to make a script program to Auto-upload Motion Detection videos of Motion Linux to free ftp server DriveHQ , using Rclone and get email alerts on Ubuntu 19.04

It is assumed that the Motion Linux is installed on the Linux PC

Please refer my blog on “How to use Motion Linux 4.2.2-1 to create a CCTV system on Ubuntu 18.10 , 64 bit with a Web Cam and Hikvision IP Cam” to get the details of installing Motion Linux

A brief introduction of main  activities in this tutorial is given below.

To  upload motion detection events , immediately after the occurrence , to the DriveHQ cloud , the Rclone software is used . For emailing the alert Mutt on Ubuntu is used

The DriveHQ FileManager free  for Android is used to view uploaded video on the android phone

The email messages  are viewed using  Aol app on the phone

The following main software have to installed for above purpose

Motion Linux

Mutt

Rclone

Note :- The installation procedure of Motion Linux  and Mutt  has already being explained in detail  in the following blog post

How to Email Motion Detection videos of Motion Linux( 3.2.2) using Mutt on Ubuntu 19.04

 

We will have to signup and get DriveHQ and Aol mail free accounts first.

The DriveHQ is the best free ftp cloud server with 5GB free storage, which is more than enough to save our motion detection videos generated by Motion Linux .

 

following details are required for the configuration of rclone

ftp host address of DriveHQ is  ftp.drivehq.com

ftp port is 21

 

Screenshot from 2019-07-27 18-30-38

Figure:-1.  web login view of DriveHQ cloud

 


Installation and configuring Rclone

On the Ubuntu terminal run

sudo apt install rclone

Screenshot from 2019-07-28 22-28-12

Figure:-2. installing rclone

Screenshot from 2019-07-28 22-28-33

Figure:- 3. installing rclone

rclone config

 

Screenshot from 2019-07-29 10-19-15

Figure:- 4. making ftp drive in rclone to upload files ( you can assign any name)

Figure:- 5. selecting  the cloud connection or connection method

 

 

 

Screenshot from 2019-07-28 22-33-29

 

Figure:- 6. selecting ftp connection on ftp.drivehq.com host,  port 21 , and entering user name and password of  free DriveHQ cloud account

 

Screenshot from 2019-07-28 22-38-19

Figure:- 7.  finishing configuration of rclone


Figure:- 7b rclone configuration file  at ~/.config/rclone

Screenshot from 2019-07-28 22-46-02

Figure:- 8. Testing the ftp upload using rclone to see whether a file can be uploaded to DriveHQ cloud account using rclone copy command

Screenshot from 2019-07-28 21-58-54

Figure:- 9. scripts in camera4 (USB camera)

Making a script file MotionTest.sh mainly to

1. sort latest motion detection video stored in ~/Videos/motion and to copy to ~/Videos/motion/sub/

2 .  upload to DriveHQ using rclone with ftp facility

3. send email alerts of the latest motion detection events to Aol account using Mutt

 

Screenshot from 2019-07-28 22-02-19

Figure:- 10. script file of MotionTest.sh

The scripts in MotionTest.sh are appended below


#!/bin/bash
rm ~/Videos/motion/sub/*.*
cp -p “`ls -dtr1 ~/Videos/motion/*.mkv | tail -1`” ~/Videos/motion/sub/
rclone copy ~/Videos/motion/sub/*.mkv ftpdrv:camera

echo “web link of the folder where the videos are stored in drivehq.com” | mutt -s “Motion is detected see the link” — bkjaya1952@aol.com

 

The script file MotionTest.sh is saved in /home/bkjaya1952/Documents/  folder

Then go to /home/bkjaya1952/Documents/ and run on the terminal

chmod +x MotionTest.sh


 

Note:-web link of the folder where the videos are stored in drivehq.com has to be replaced accordingly .Please see the link underlined in red in the figure below

 

Screenshot from 2019-07-29 00-11-17

 

Figure:- 11.  Getting web link of the folder where the videos are stored in drivehq.com

Email alerts on Aol mail

When a motion is detected,  MotionTest.sh script  in camrea4.conf is activated.

The scripts in MotionTest.sh do the following activities

1. delete all files in the sub directory /Videos/motion/sub/  before coping the latest video

script command    rm ~/Videos/motion/sub/*.*

2. select the latest motion detection video saved by Motion Linux in the  directory  ~/Videos/motion/ and copy  it to the sub directory ~/Videos/motion/sub/

script command   cp -p “`ls -dtr1 ~/Videos/motion/*.mkv | tail -1`” ~/Videos/motion/sub/

3. upload the latest motion detection video copied in to ~/Videos/motion/sub/  to the camera directory of DriveHQ cloud using ftp facility in rclone

script command  rclone copy ~/Videos/motion/sub/*.mkv ftpdrv:camera

4. email the alert of the latest motion detection event to Aol account using Mutt  (with the access  link of video in  DriveHQ cloud )

script command   echo “web link of the folder where the videos are stored in drivehq.com” | mutt -s “Motion is detected see the link” — bkjaya1952@aol.com

 

Screenshot from 2019-07-29 09-37-18

Figure:- 12. received email alerts on Aol mail

 

Screenshot from 2019-07-29 09-37-36

 

Figure:- 13. email the alert of the latest motion detection event with the access  link of video in  DriveHQ cloud

Viewing motion detection videos on mobile phone

Install DriveHQ FileManager on the phone and login to the DriveHQ account to see the automatically uploaded motion detection videos

Screenshot_2019-07-27-21-45-18

Figure:- 14. viewing files on DiveHQ cloud using  DriveHQ FileManager   on a mobile phone

Screenshot_2019-07-27-21-46-07

Figure:- 15.viewing motion detection events pushed to DriveHQ cloud using DriveHQ FileManager on a mobile phone

Screenshot_2019-07-27-19-41-08.png

 

Figure:-  17. viewing videos  in DriveHQ cloud using mobile phone

 The DriveHQ  which is having ftp facility in free plan seems to be the best cloud to save motion detection events of CCTV open source software like Motion Linux and Zoneminder .

 

 

 

 

 

 

 

 

 

 

 

 

 

How to Email Motion Detection videos of Motion Linux( 3.2.2) using Mutt on Ubuntu 19.04

Install Motion Linux

The Motion Linux available in the Ubuntu software center is not the latest. Therefore it is advisable to get the software from the Motion website .

First install Motion Linux latest release  using the GDebi  Package Installer.

The Motion release compatible with Ubuntu 18.10 , 64 bit is

cosmic_motion_4.2.2-1_amd64.deb

Now if you see the folder motion is being created at /etc/motion/. You can see 5 files in this folder as i the following figure

Screenshot from 2018-02-02 18-28-44

 Figure:- 1 Configuration files of Motion Linux at /etc/motion

 

Editing motion.conf file to suit our cameras

 

The main file is “motion.conf”. Other files give sample script files for different cameras.

If you open the motion.conf using a text editor like gedit , you can see the commands related  to  linking cameras .Please see the following figure for the details

Screenshot from 2019-07-22 09-35-27

 

Figure:- 2 Initiation of USB Web camera (camera4.conf) in motion.conf

You will have to delete semicolon ” ; ” placed at the beginning of the commands in order to activate the corresponding usb web camera ( camera4.conf)

On the Ubuntu terminal

sudo gedit /etc/motion/motion.conf

Then the motion.conf file is opened

Then delete semicolon ” ; ” in front of

camera /etc/motion/camera4.conf ( line no 166)

Then save the file motion.conf

Creating configuration files for USB web camera

For the web camera

On the Ubuntu terminal

To copy the scripts in camera4-dist.conf ( guide conf file)  to /etc/motion/camera4.conf ( which links with motion.conf )

sudo cp /etc/motion/camera4-dist.conf  /etc/motion/camera4.conf

Open camera4.conf file for edit

sudo gedit /etc/motion/camera4.conf

Then edit  file as shown in the figure below to add emailing script and streaming port

enter the scripts shown in the following figure and save by adding following lines on line number 25 , 32 and 34 respectively

target_dir /home/bkjaya1952/Videos/motion ( change “bkjaya1952” to suit to your case)

on_movie_end /home/bkjaya1952/Documents/MotionTest.sh

stream_port 8081

Screenshot from 2019-07-22 09-34-45

 

Figure:- 3 Configuraton of USB Web camera

Here you can see a script file namely MotionTest.sh  is included in line number 32 of above camera4.conf file which consists of emailing script . For emailing motion detection Mutt software has to be installed .The details of creating MotionTest.sh is explained below.

Installation of Mutt

Open the Ubuntu terminal and run

sudo apt install mutt

Configure Mutt

mkdir -p ~/.mutt/cache/headers

mkdir ~/.mutt/cache/bodies

touch ~/.mutt/certificates

Create Mutt configuration file muttric

touch ~/.mutt/muttrc

Open muttric

sudo gedit ~/.mutt/muttrc

Add following configurations to opened muttrc empty file and save. Please see the figure below . fill “your gmail address” , , “your name”, and “your gmail password”  appropriately

# vi ~/.muttrc
# About Me
set from = “your gmail address”
set realname = “your name”

# My credentials
set smtp_url = “smtp://your gmail address@smtp.gmail.com:587/”

set imap_user = “Your gmail address”

set smtp_pass = “your gmail password”
# Where to put the stuff
set header_cache = “~/.mutt/cache/headers”
set message_cachedir = “~/.mutt/cache/bodies”
set certificate_file = “~/.mutt/certificates”

# Etc
set mail_check = 30
set move = no
set imap_keepalive = 900

Screenshot from 2019-07-22 09-38-57

Figure:- 4 Configuration of muttrc

Creating MotionTest.sh emailing script file

(Please see the foot note of the blog for an alternative script file)

Create a script file namely MotionTest.sh using gedit as follows

Open Ubuntu terminal

cd ~/Documents

sudo gedit MotionTest.sh

The paste following script lines on the opened empty file

 

#!/bin/bash
rm ~/Videos/motion/sub/*.*
i=0
j=$(stat ~/Videos/motion/*.mkv –printf “%i\n” | wc -l )
for k in ~/Videos/motion/*.mkv; do
if (( (j – ++i) < 1 )); then
cp -v — “$k” ~/Videos/motion/sub
fi
done
echo “See this vieo” | mutt -s “Motion is Detected” -a /home/bkjaya1952/Videos/motion/sub/*.mkv — your aol email

 

Then save the file

(Replace “bkjaya1952” apropreatly)

To make MotionTest.sh executable

chmod +x MotionTest.sh

 

Screenshot from 2019-07-22 11-07-44

Figure:- 5 The scripts in MotionTest.sh

On above scripts all the motion detection videos are stored in /home/bkjaya1952/Videos/motion

Before email the latest motion detection event , the latest video is selected from above directory and saved in the sub directory /home/bkjaya1952/Videos/motion/sub  to avoid emailing unnecessary old videos .

Line number 2

rm ~/Videos/motion/sub/*.*

The above script cleans the directory by deleting  all files before copying  the latest event from /home/bkjaya1952/Videos/motion to /home/bkjaya1952/Videos/motion/sub/

line number 3 to 9

Selects the latest video event from /home/bkjaya1952/Videos/motion  and copy to /home/bkjaya1952/Videos/motion/sub/

Line number 10

echo “See this vieo” | mutt -s “Motion is Detected” -a /home/bkjaya1952/Videos/motion/sub/*.mkv — your aol email

Email the latest event at /home/bkjaya1952/Videos/motion/sub/ to your aol email using Mutt

I have selected aol email service as it is not blocking mail or sending our emails to spam folder.

Note:- I have created  “motion”  and “sub” folders under the ~/Videos directory of the desktop as follows

Open Ubuntu terminal

 cd ~/Videos

mkdir motion

cd motion

mkdir sub

 

Screenshot from 2019-07-22 11-31-46

Figure:- 6  Motion Linux panel

Screenshot from 2019-07-22 11-33-04

 

 

 

 

 

 

Figure:- 7 Stored motion detection videos

Screenshot from 2019-07-22 11-33-15

Figure:- 8 Copied  latest motion detection video

 

Screenshot from 2019-07-22 11-34-23

 

Figure:- 9 Received  email on motion detection events

Screenshot from 2019-07-22 11-34-45

 

Figure:- 10 Attached video of motion detection

 

Foot Note:- An alternative script for MotionTest.sh  which seems to be much simpler is given below ( with compressed video files)

#!/bin/bash
rm ~/Videos/motion/sub/*.*
cp -p “`ls -dtr1 ~/Videos/motion/*.mkv | tail -1`” ~/Videos/motion/sub/
tar c ~/Videos/motion/sub/*.mkv | gzip –best > ~/Videos/motion/sub/motion.tar.gz

echo “See this vieo” | mutt -s “Motion is Detected” -a ~/Videos/motion/sub/*.tar.gz — bkjaya1952@aol.com

 

 

Screenshot from 2019-07-24 18-26-12

 

Figure:- 11 An alternative script for MotionTest.sh

 

-END-

How to use Motion Linux 4.2.2-1 to create a CCTV system on Ubuntu 18.10 , 64 bit with a Web Cam and Hikvision IP Cam

The CCTV system that is explained here is based on 2 cheap cameras.

One normal USB web Camera and a Hikvision Q1 network PT camera (wifi)

IMG_20190615_162612

Figure:-1

The Motion Linux available in the Ubuntu software center is not the latest. Therefore it is advisable to get the software from the Motion website .

First install Motion Linux latest release  using the GDebi  Package Installer.

The Motion release compatible with Ubuntu 18.10 , 64 bit is

cosmic_motion_4.2.2-1_amd64.deb

Now if you see the folder motion is being created at /etc/motion/. You can see 5 files in this folder as i the following figure

Screenshot from 2018-02-02 18-28-44

 Figure:- 2

Editing motion.conf file to suit our cameras

 

The main file is “motion.conf”. Other files give sample script files for different cameras.

If you open the motion.conf using a text editor like gedit , you can see the commands related  to  linking cameras .Please see the following figure for the details.

Screenshot from 2019-06-15 16-12-42

Figure:- 3

You will have to delete semicolon ” ; ” placed at the beginning of the commands in order to activate the corresponding camera

Here ,I have used camera1.conf as the configuration file for the Hikvision camera

and camera4.conf as the configuration file for the web cam

The above configuration files have been prepared using the guide scripts in camera1-dist.conf and camera4-dist.conf files available in /etc/motion/

On the Ubuntu terminal

sudo gedit /etc/motion/motion.conf

Then the motion.conf file is opened

Then delete semicolon ” ; ” in front of

camera /etc/motion/camera1.conf  ( line no 163)

camera /etc/motion/camera4.conf

Then save the file motion.conf

Since we are using a separate configuration  file ( camera4.conf) for the web cam we don’t need some commands in main “motion.conf” file as those commands are included in camera4.conf .Therefore we introduce semicolon “;” in front of those commands ( for videodevice at line no 34 and stream_port at line no 155 )as follows in the motion.conf file as shown in the following figures

Screenshot from 2019-06-15 16-13-46

Figure:-4

Screenshot from 2019-06-15 16-13-08

Figure:-5

Creating configuration files for cameras

For the web camera

On the Ubuntu terminal

sudo gedit /etc/motion/camera4.conf

Then a blank text file is opened

enter the scripts shown in the following figure and save

target_dir at line no 29 has to be selected in your computer to store motion detection video events .

Screenshot from 2019-06-15 17-11-38

Figure:-6

 

For the Hikvision camera

sudo gedit /etc/motion/camera1.conf

 

On the Ubuntu terminal

sudo gedit /etc/motion/camera1.conf

Then a blank text file is opened

enter the scripts shown in the following figure and save

Screenshot from 2019-06-15 17-12-08

Figure:-7

The netcam_url at line no 15 should be as follows for the Hikvision Q1 Network PT camera

rtsp://username:password@ip address:554/Streaming/Channels/102?transportmode=mcast&profile=Profile_2

Now all the configuration files for cameras are ready

Initiation of Motion

On the Ubuntu terminal

sudo /usr/bin/motion

Then open the web browser and enter the url

http://localhost:8080/

You can see all the cameras are working  as shown in the figure

Screenshot from 2019-06-15 15-39-44

 

Figure:-8

 

 

How to use an Executable Perl script in zoneminder filter to send email alerts of motion detection events on Ubuntu 18.10/19.94

In this example SSMTP is being used as the sendmail emulator .

Therefor SSMTP has to be installed as follows.

I it is assumed that the Zoneminder is installed .

Installation of SSMTP

sudo apt update

sudo apt install ssmtp

sudo apt install mailutils

Note:  ssmtp has to be installed before installing mailutils . If you install mailutils first , installation of postfix also will be taken place ,which may hinder installing some dependent software needed for ssmpt.

Configuration of SSMTP

To open ssmtp.conf

sudo gedit /etc/ssmtp/ssmtp.conf.

Then edit to include following  lines as shown in the figure:-1  below  and save the file

In line number 23 your gmail password has to be entered in front of AuthPass=

Screenshot from 2019-05-23 20-08-11

Figure:1

 

Checking to see mail works

On the terminal enter

sudo echo “This is the body of the email” | mail -s “This is the subject line” receiver’s email address

Note : The receiver’s email address has to be replaced with your receiver’s actual email address in above command.

Make a dropbox account and install dropbox on computer .Then sync zoneminder events at /var/cache/zoneminder/events with the dropbox cloud and get the share link for attaching it to email .For details on making sync please refer this link

Perl Script file

A Perl script file “perlscript.pl”  is prepared in  /home/bkjaya1952/zm/

(Note:- the script file can be placed in any directory)

Open Ubuntu terminal run

sudo gedit /home/bkjaya1952/zm/perlscript.pl

Then copy and paste following code in the the file and make changes to suit your requirements and save. I have used an Aol mail account to receive emails

 

#!/usr/bin/perl -w
use strict;
use MIME::Lite;
# Recipient
my $email = ‘bkjaya1952@aol.com’;
my $msg = MIME::Lite->new (
Subject => “Motion Detection”,
From => ‘bkjaya1952@gmail.com’,
To => $email,
Type => ‘text/html’,
Data => ‘<H1>Motion Detection</H1>Please see the link
<a href=”https://www.dropbox.com/sh/xxxxxxxxxx”>Zoneminder motion events in dropbox</a><hr>
B.K.Jayasundera’
);
$msg->send();

 

Note:- Please replace https://www.dropbox.com/sh/xxxxxxxxxx with your share link of “events” folder in the dropbox cloud.

(Note: “MIME::Lite” is installed during the zoneminder installation)

Please refer MIME::Lite

 

Screenshot from 2019-05-24 18-45-51.png

Figure:-2

Making a zm filter using ” Perl script” to send email alerts on matching  motion detection events

Please see the following figure:-2 for the details.

Then start zoneminder on “Modect” mode and execute the filter

Screenshot from 2019-05-24 19-11-51

 

Figure:-3

The Command entered on the box at “Execute command on all matches” should be

perl /home/bkjaya1952/zm/perlscript.pl

(Note: Replace “/home/bkjaya1952/zm/” with your directory path where your script file is stored )

Now you can see the received email alerts in my Aol mail as follows

Screenshot_2019-05-25-16-19-13

Figure:-4 email alert received on mobile phone

Figure:-5 Details on the event video clips at the link (Zoneminder motion events in dropbox) in emil