Trigger a script when there is an alarm in 1.25

Forum for questions and support relating to the 1.25.x releases only.
Stef
Posts: 6
Joined: Fri Jun 01, 2012 4:57 am

Trigger a script when there is an alarm in 1.25

Post by Stef »

The original under "How can I use ZoneMinder to trigger something else when there is an alarm?" from the FAQ seems to be applicable to the previous version of ZoneMinder.

This line:

Code: Select all

zmDbgInit( "myscript", level=>0, to_log=>0, to_syslog=>0, to_term=>1 );
refers to a function that does not exist. As far as I can see, it has to be replaced with:

Code: Select all

logInit();
perldoc ZoneMinder::Memory shows example code which I added to the script however zmMemVerify( $monitor ) always return false.

I would very much appreciate if someone could point out how I should do this.

Regards,
Stefan

Code: Select all

#!/usr/bin/perl -w
use strict;
use ZoneMinder;
$| = 1;

#zmDbgInit( "myscript", level=>0, to_log=>0, to_syslog=>0, to_term=>1 );
logInit();

my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS );

my $sql = "select M.*, max(E.Id) as LastEventId from Monitors as M left join Events as E on M.Id = E.MonitorId where M.Function != 'None' group by (M.Id)";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );

my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
my @monitors;
while ( my $monitor = $sth->fetchrow_hashref() )
{
    push( @monitors, $monitor );
}

while( 1 )
{
    foreach my $monitor ( @monitors )
    {
         if ( zmMemVerify( $monitor ) )
         {
           my $state = zmGetMonitorState( $monitor );
           if ( $state == STATE_ALARM )
           {
           ..... alarm script ....
           }
         }
    }
    sleep( 1 );
}
Alan87i
Posts: 116
Joined: Sun Jul 27, 2008 9:58 am

Re: Trigger a script when there is an alarm in 1.25

Post by Alan87i »

What are you trying to trigger?
On older ubuntu systems with older ZM versions I have a bash script that tails the message log for the monitor name and the phrase "gone into alarm. "
Stef
Posts: 6
Joined: Fri Jun 01, 2012 4:57 am

Re: Trigger a script when there is an alarm in 1.25

Post by Stef »

Thanks for the reply. I want to trigger audio on another computer on the network. The idea is to play the sound of a barking dog. I will try the message log idea.

It does seem that there is a good system built into ZM to trigger external events. I thought it would be a streamlined way of doing this.

Regards,
Stefan
Alan87i
Posts: 116
Joined: Sun Jul 27, 2008 9:58 am

Re: Trigger a script when there is an alarm in 1.25

Post by Alan87i »

Code: Select all

#!/bin/bash
tail -f /var/log/messages | grep --line-buffered 'frontdoor.*Gone into alarm state' | while read line; do aplay /dog1.wav; done
this is one of my scripts.
I set cron to start it and stop as I need. frontdoor is a camera name dog1.wav is the wave file saved in /
Stef
Posts: 6
Joined: Fri Jun 01, 2012 4:57 am

Re: Trigger a script when there is an alarm in 1.25

Post by Stef »

Thanks!

This is the setup I have currently:

Code: Select all

#!/bin/bash
tail -f /var/log/messages | grep --line-buffered 'Cam2.*Gone into alarm state' | while read line; do curl -o --url xxx/sound.php >/dev/null 2>&1; echo $line; done
On the computer which will play the sound: /var/www/sound.php

Code: Select all

<?php
shell_exec("/usr/bin/super play");
echo "Bark, bark!";
?>
I have had to use 'super' to bypass permission issues with www-data trying to access the audio device. My super.tab line is:

Code: Select all

play            '/usr/bin/mpg123 -q --loop 1 /var/www/Growl.mp3' uid=validuser
Regards,
Stefan

PS Because of "Your post looks too spamy for a new user, please remove off-site URLs." I have replaced anything looking like a url with xxx
User avatar
hk_niko
Posts: 36
Joined: Mon Feb 20, 2012 4:29 pm

Re: Trigger a script when there is an alarm in 1.25

Post by hk_niko »

I created a script that opens a page firefox when the state is alarmed, but the window opens it only where is located ZoneMinder .... if open ZoneMinder in another PC, it does not open a new window when it is in alarm... Can you help me??
Stef
Posts: 6
Joined: Fri Jun 01, 2012 4:57 am

Re: Trigger a script when there is an alarm in 1.25

Post by Stef »

I am not sure I understand what you want. Do you want a firefox window to open on another computer when zoneminder alarms? I

Regards,
Stefan
User avatar
hk_niko
Posts: 36
Joined: Mon Feb 20, 2012 4:29 pm

Re: Trigger a script when there is an alarm in 1.25

Post by hk_niko »

Excuse me for my bad english...yes i want open a window when a monitor is alarmed....for example I open ZM in windows xp but the script.pl not work because it work on zm server... . I want to create a script that let me open a window in windows xp when there is an alarm...can help me? :)
Stef
Posts: 6
Joined: Fri Jun 01, 2012 4:57 am

Re: Trigger a script when there is an alarm in 1.25

Post by Stef »

I am afraid that I only use Windows for games & netflix. I am not able to help you with this - at least on the Windows side.

I imagine you may have to look for some sort of program that listen for commands directed at a specific port on the IP address of the Windows XP PC. It should be easy do do this with VisualBasic. Alternatively how about a script that checks a webpage on your ZM server for say "Alarm"?

Under Linux there are a variety of options such as ssh with passwordless login, inetd / xinetd service, telnet, netcat etc.

Regards,
Stefan
User avatar
hk_niko
Posts: 36
Joined: Mon Feb 20, 2012 4:29 pm

Re: Trigger a script when there is an alarm in 1.25

Post by hk_niko »

It is in fact with linux I have no problem, I created the script that generates an alarm when it generates a query on a table created by me. As for Visual Basic I scrapped, I prefer to use Firefox with php and perl to find a solution that is usable on the network quickly ... (visual basic is only for windows and this thing is hideous!)..
however I can share what I create in this post, but I also accept suggestions and ideas ;)
BobS0327
Posts: 86
Joined: Mon Oct 16, 2006 6:22 pm

Re: Trigger a script when there is an alarm in 1.25

Post by BobS0327 »

for example I open ZM in windows xp but the script.pl not work because it work on zm server... . I want to create a script that let me open a window in windows xp when there is an alarm...can help me?
Well, one option is to develop a client/server app where the server would reside on your Windows box and the client would reside on your Linux box along with ZM. The client would communicate with the server whenever an alarm is generated in ZM. When the server receives the communication, it would open a window. Keep in mind that this is not a trivial task. I know it can be written in c and I believe it can be written in php although I have no real php experience.
User avatar
hk_niko
Posts: 36
Joined: Mon Feb 20, 2012 4:29 pm

Re: Trigger a script when there is an alarm in 1.25

Post by hk_niko »

you have a script in C that does something like that?
BobS0327
Posts: 86
Joined: Mon Oct 16, 2006 6:22 pm

Re: Trigger a script when there is an alarm in 1.25

Post by BobS0327 »

you have a script in C that does something like that?
No, I personally don't have that specific type of C code. But I will provide additional info that will get you about 95% of the way to accomplishing your task.

First, I've listed a link below to a Windows server example. It's an excellent example of executing remote commands on a windows box written by a individual called Napalm.

http://www.rohitab.com/discuss/topic/27 ... ote-shell/

Next is the source code for a Linux client. This is the code that you will have to modify to access ZM in order to check for alarms. In it's current state, you can only send commands to the remote Windows box. For example,you can run this Linux client script and enter DIR c:\temp as input to it and it will display the contacts of the remote Windows box C:\temp folder on your Linux console.

Code: Select all

// execute: client xxx.xxx.xxx.xxx port

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <strings.h>
#include <arpa/inet.h>


#define BUFFER    1024


main(int argc, char **argv)
{
    struct sockaddr_in serv;
    int sock;
    char in[BUFFER];
    char out[BUFFER];
    int len;
    if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        exit(-1);
    }
    serv.sin_family = AF_INET;
    serv.sin_port = htons(atoi(argv[2]));
    serv.sin_addr.s_addr = inet_addr(argv[1]);
    bzero(&serv.sin_zero, 8);

    printf("\nThe TCPclient %d\n",ntohs(serv.sin_port));
        fflush(stdout);
    if((connect(sock, (struct sockaddr *)&serv, sizeof(struct sockaddr_in))) == -1)
    {
        perror("connect");
        exit(-1);
    }
    while(1)
    {
        printf("\nInput: ");
        fgets(in, BUFFER, stdin);
        send(sock, in, strlen(in), 0);
        len = recv(sock, out, BUFFER, 0);
        out[len] = '\0';
        printf("Output: %s\n", out);
    }
    close(sock);
}

To reiterate, in your situation, you'll have to modify the Linux client to check for ZM alarms. When a ZM alarm occurs, the client will send a command to the remote Windows box (which is executing the server code written by Napalm) to open a window.
coinageboy
Posts: 17
Joined: Sun May 27, 2012 1:06 am

Re: Trigger a script when there is an alarm in 1.25

Post by coinageboy »

I like the "Barking Dog Alarm" idea and would like to incorporate into my rather large and complex system.
Is it fully operational and does it work reliably on a machine that is not necessarily ZM server - just displaying the streams??
If there are any issues, what are they??
BobS0327
Posts: 86
Joined: Mon Oct 16, 2006 6:22 pm

Re: Trigger a script when there is an alarm in 1.25

Post by BobS0327 »

I like the "Barking Dog Alarm" idea and would like to incorporate into my rather large and complex system.
Is it fully operational and does it work reliably on a machine that is not necessarily ZM server - just displaying the streams??
If there are any issues, what are they??
I'm not sure who these questions are directed to. But the client/server scenario I previously presented can play a "barking dog" wav file on a remote windows box without any modification. First, start the server code on the windows box and then start the client code on your Linux box. At the Linux client input prompt just type in the following WITHOUT the quotes

start "c:\windows\media\chimes.wav"

The above command will use the Windows Media Player on the remote Windows box to play the chimes.wav as an example. All you have to do is create a barking dog wav any your task is complete
Locked