Page 1 of 1

Execute script immediately after alarm state is triggered

Posted: Wed Apr 27, 2011 9:34 am
by haseldow
I'm looking for a convenient way to execute a script immediately after a monitor has gone into alarm state.

Currently I have a very simple filter (if cause is Motion and Duration greater than 0) to upload matches, send email and run custom script. When a motion alarm is triggered the following will happen in order:
  • Monitor goes into alarm state
  • When there's no more motion the monitor leaves alarm state
  • Notification email gets sent
  • Files will be uploaded to FTP server
  • Script gets executed
The problem is that the monitor can be in alarm state for a long time, also uploading events might take a long time and after all this the script gets executed. Even if I remove upload matches and send email, it still waits for the monitor to leave alarm state after which there is some delay before the script gets executed.

One solution I found on this forum is to actively tail syslog for "Gone into alarm state" and trigger the script that way. Although this solution would work, it seems a bit too ugly hack for me.

Does anyone have any suggestions how I could run a custom script as soon as a monitor goes into alarm state? Is there a zm?_* script where I could add an exec (or similar) line (I couldn't find this myself)?

Re: Execute script immediately after alarm state is triggere

Posted: Wed Apr 27, 2011 10:53 am
by haseldow
I found a method of doing this which is just a bit less of a hack by using a simple log watcher called swatch.

Simply install swatch using your preferred method: package manager or source (I was not allowed to post link, but you can search sourceforge).

Create configuration file for swatch (e.g. /etc/swatch.conf).

Code: Select all

watchfor /Gone into alarm state/
  threshold track_by="Gone into alarm state", type=limit, count=1, seconds=3600
  exec /path/to/my/script.sh
(the threshold line is optional, in this case run the script only once per match, and then do nothing for repeating matches for 3600 seconds)

Create script dir (path where swatch creates temporary watcher script).
mkdir /tmp/swatch
chmod 700 /tmp/swatch

According to the man page, this folder should not be world-writable.

Run swatch in daemon mode
swatch -c /etc/swatch.conf --daemon --pid-file /var/run/swatch.pid --script-dir /tmp/swatch --tail-file /var/log/syslog

Add to rc.local, or create a init script. Also remember to write your /path/to/my/script.sh script. :wink:


I'm not completely satisfied with this solution but it will serve me fine until I find a better solution. New ideas still greatly appreciated.