Wake Display on Alarm (and launch montage view)
Posted: Sun Jan 24, 2016 5:17 pm
Goal: Have a Display turn on when there is an alarm. Also save power and display backlight life by allowing the display to go to sleep when there are no alarms. I spent the last month working through this and think I have a fairly stable but more complex than expected solution.
System is Ubuntu 14.04 server w/GUI and ZM 1.28.1
1) Setting up a filter to run a command when there is a new alarm
There are many suggestions I found but the only one I could get to work was to use event archiving. I wish there was another way as this method eliminates the use of archiving to save important events (unless you have unlimited storage).
- Create a new filter. I named mine TurnDisplayOn
- Add triggers
-- Alarm Frames, Greater than or equal to, 5 (adjust as needed for your system)
-- and, Archive Status equal to Unarchived Only
- Sort by, Date/Time, Desc, Limit to first 10 (adjust as needed) results only
- Archive all matches, check
- Execute command on all matches:
If you are running the PurgeWhenFull filter, you need to modify the filter and remove the 'Unarchived Only' trigger.
2) Getting zmDOA-ssh.sh to execute
This was a challenge. I could never get the www-data to have enough permission to get the display to turn on (lots of visudo experiments). In the end I found leaving the browser running for extended times on the montage view resulted in browser freezes, so needed more permission to close/restart the browsers anyway. Solution was creating a script which will kick off commands (via ssh) as another Ubuntu user.
I am sure there is a much more elegant solution. I also believe there is a significant security exposure with this method, however in my case the system use is local only with no internet access. So intrusion requires physical access at which point the hacker owns the system anyway. No data of value on this system anyway. They'd just take the hardware for the hardware value.
The ZoneMinder user on Ubuntu is www-data. This is not a full user, so getting commands to execute is challenging.
a) Create the www-data user directory, open a terminal
b) Create the zmDOA-ssh.sh script
Paste this into and save. Replace yourUbuntuUser with your default Ubuntu user account. Note www-data is not your default user account. Also this could be used to wake the display on a remote system if so desired by replacing localhost with another system's address and altering a couple of the other instructions below.
c) Change ownership of everything to www-data
d) Allow www-data to ssh
This is where the security exposure comes in. There must be a secure way to do this, but I really did not need to find one.
- As your ubuntu default user ssh into your system.
- Accept the ECDSA key fingerprint and exit
- Copy the key to a location where the www-data user can find it. I had to put it in the /var/www-data directory.
- Change ownership of the .ssh directory and key
- Allow login with no password
### START-Keychain ###
# Let re-use ssh-agent and/or gpg-agent between logins
/usr/bin/keychain $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOSTNAME-sh
### End-Keychain ###
[/code]
3) Install some tools which will be needed.
4) Create the script to wake the display. Note I use firefox as I have found it requires less CPU in the montage view. Chromium can be used as well but may require more adjustment than just replacing 'firefox' with 'chromium-browser'.
- Paste this in and save
5) Create the firefox timeout script to gracefully close firefox when the display is off. I found this was required as running the browser for extended time in montage mode would result in a browser freeze (same for chromium). Aslo by closing the browser when the monitor is off a little more power is saved as the CPU is not working as much.
-Paste this in and save
6) Have the ff_to.sh script run every 5 minutes. I left the Ubuntu defaults for Display turn off. This way if there is no alarm for 5 minutes the Display will turn off and within 5 minutes after the display turns off the browser will be closed.
-Paste this in at the last line, replace yourUbuntuUser as appropriate, save
7) Configure firefox to launch the montage page when started.
- In preferences set show my homepage when firefox starts
- Use the following as your homepage replacing zmuser and zmpassw0rd as appropriate
Good luck, hope it works as well for you as it does for me.
System is Ubuntu 14.04 server w/GUI and ZM 1.28.1
1) Setting up a filter to run a command when there is a new alarm
There are many suggestions I found but the only one I could get to work was to use event archiving. I wish there was another way as this method eliminates the use of archiving to save important events (unless you have unlimited storage).
- Create a new filter. I named mine TurnDisplayOn
- Add triggers
-- Alarm Frames, Greater than or equal to, 5 (adjust as needed for your system)
-- and, Archive Status equal to Unarchived Only
- Sort by, Date/Time, Desc, Limit to first 10 (adjust as needed) results only
- Archive all matches, check
- Execute command on all matches:
Code: Select all
/bin/sh /home/www-data/zmDOA-ssh.sh
2) Getting zmDOA-ssh.sh to execute
This was a challenge. I could never get the www-data to have enough permission to get the display to turn on (lots of visudo experiments). In the end I found leaving the browser running for extended times on the montage view resulted in browser freezes, so needed more permission to close/restart the browsers anyway. Solution was creating a script which will kick off commands (via ssh) as another Ubuntu user.
I am sure there is a much more elegant solution. I also believe there is a significant security exposure with this method, however in my case the system use is local only with no internet access. So intrusion requires physical access at which point the hacker owns the system anyway. No data of value on this system anyway. They'd just take the hardware for the hardware value.
The ZoneMinder user on Ubuntu is www-data. This is not a full user, so getting commands to execute is challenging.
a) Create the www-data user directory, open a terminal
Code: Select all
$ sudo mkdir /home/www-data
Code: Select all
sudo nano /home/www-data/zmDOA-ssh.sh
Code: Select all
#!/bin/sh
/usr/bin/ssh yourUbuntuUser@localhost /bin/sh /home/yourUbuntuUser/zmDispOnAlarm.sh
Code: Select all
$ sudo chown -R www-data:www-data /home/www-data
This is where the security exposure comes in. There must be a secure way to do this, but I really did not need to find one.
- As your ubuntu default user ssh into your system.
Code: Select all
$ ssh localhost
Code: Select all
$ exit
Code: Select all
$ sudo cp /home/yourUbuntuUser/.ssh /var/www
Code: Select all
$ sudo chown -R www-data:www-data /var/www/.ssh
Code: Select all
$ nano /home/yourUbuntuUser/.bash_profile
- Paste in and save
# Let re-use ssh-agent and/or gpg-agent between logins
/usr/bin/keychain $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOSTNAME-sh
### End-Keychain ###
[/code]
3) Install some tools which will be needed.
Code: Select all
$ sudo apt-get install xdotool
$ sudo apt-get install wmctrl
Code: Select all
$ cd ~
$ nano zmDispOnAlarm.sh
Code: Select all
#!/bin/sh
# Check if firefox is already running (could be due to a recent alarm) if not start firefox
if ps ax |grep -v grep | grep firefox > /dev/null
then
echo firefox already running > /dev/null
else
env DISPLAY=:0 firefox &
fi
# Wake the display. I have found that the export command will wake the display all by itself in some cases.
export DISPLAY=:0
# To make sure the display is awake, move the mouse a pixel and back
/usr/bin/xdotool mousemove_relative 1 1
sleep 1
#/usr/bin/xdotool mousemove 0 480
/usr/bin/xdotool mousemove_relative -1 -1
# Uncomment items below if you wish to log when this sript is called
#TIMESTAMP=`/bin/date "+%Y-%m-%d %H:%M:%S"`
#MONITOR=`/usr/bin/xset -display :0 -q|grep "Monitor is"`
#echo "$TIMESTAMP: $MONITOR" >> /~/zmDispOnAlarm.log
Code: Select all
$ cd ~
$ nano ff_to.sh
Code: Select all
#!/bin/sh
#grab the current display status
MONITOR=`/usr/bin/xset -display :0 -q|grep "Monitor is"`
#If display is off close firefox
case $MONITOR in *Off*)
if ps ax |grep -v grep | grep firefox > /dev/null
then
env DISPLAY=:0 wmctrl -c firefox
fi
esac
Code: Select all
$ crontab -e
Code: Select all
0,5,10,15,20,25,30,35,40,45,50,55 * * * * sh /home/yourUbuntuUser/ff_to.sh
- In preferences set show my homepage when firefox starts
- Use the following as your homepage replacing zmuser and zmpassw0rd as appropriate
Code: Select all
http://localhost/zm/?action=login&view=montage&group=0&username=zmuser&password=zmpassw0rd