running a program

Support and queries relating to all previous versions of ZoneMinder
Locked
User avatar
Sir Veillance
Posts: 11
Joined: Tue Nov 13, 2007 3:44 am
Location: Germany

running a program

Post by Sir Veillance »

Hi,

I'd like ZM to run a program whenever an event is triggered.
The example script in the FAQ seems to be suitable for my purpose.
But I am not a perl programmer, so the simple task of placing a call to my own (bash) script is too high for me :cry:

Could somebody please show me, what I have to write instead of

Code: Select all

            print( "Monitor ".$monitor->{Name}." has alarmed\n" );
            #
            # Do your stuff here
            #
in order to run MYSCRIPT.SH?

And - if possible - I want to pass parameters like Event ID, time, cause and so on..

Thanks a lot!
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

In it's simplest form you probably want to have something like this

Code: Select all

$sql = "select * from Events where Id = ?";
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute( $last_event_id ) or die( "Can't execute '$sql': ".$sth->errstr() );
my $event = $sth->fetchrow_hashref();

my $command = "/<pathto>/myscript.sh --event=".$event->{Id};
my $output = qx( $command );
my $status = $? >> 8;

# Print output if required
# Check status if required
Phil
User avatar
Sir Veillance
Posts: 11
Joined: Tue Nov 13, 2007 3:44 am
Location: Germany

[solved] running a program

Post by Sir Veillance »

it works :D

Here is my script alarm_loop.pl

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 );

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 );
}

$sql = "select * from Events where Id = ?";
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );

while( 1 )
{
    foreach my $monitor ( @monitors )
    {
        next if ( !zmShmVerify( $monitor ) );
 
        if ( my $last_event_id = zmHasAlarmed( $monitor, $monitor->{LastEventId} ) )
        {
            $monitor->{LastEventId} = $last_event_id;
            # print( "Monitor ".$monitor->{Name}." has alarmed\n" );
            $res = $sth->execute( $last_event_id ) or die( "Can't execute '$sql': ".$sth->errstr() );
            my $event = $sth->fetchrow_hashref();
            
            if ( $event->{Id} ) {
              # print( "EventID= ".$event->{Id} );
            
              my $command = "./alarm1.sh" 
               # ." --monitor=" .$monitor->{Id}
               ." --event=" .$event->{Id} 
               ." --starttime=\"" .$event->{StartTime} ."\"";
              my $output = qx( $command );
              my $status = $? >> 8;
            
              # print ("Output(" .$status ."):" .$output);


              # Print output if required
              # Check status if required            
              #
              # Do your stuff here
              #
            }
        }
    }
    sleep( 1 );
}
This is my alarm1.sh (playing an mp3)

Code: Select all

#!/bin/bash
echo "1=$1 2=$2 3=$3 4=$4"
/usr/local/bin/mplayer /home/Martin/cam/web/sounds/alarm1.mp3>/dev/nul
Thanks a lot, again
Martin from Germany
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

No problem, glad I could help.
Phil
achix
Posts: 146
Joined: Tue Oct 02, 2007 9:38 am
Location: Greece

Post by achix »

A dog bark would be great!
Or smth like playing from the external speakers:

YOUR ACTIONS ARE CURRENTLY MONITORED BY THE POLICE!!!

Thats cool!
User avatar
Sir Veillance
Posts: 11
Joined: Tue Nov 13, 2007 3:44 am
Location: Germany

Post by Sir Veillance »

achix wrote:A dog bark would be great!
that's exactly what intruders get to hear in my garden :wink:

BTW: I haven't yet found any good (scary) dog bark sound files on the net. :(
I guess I have to go to a scrapyard and find a mastiff :wink:
Martin from Germany
danielareas
Posts: 33
Joined: Thu Oct 26, 2006 6:00 pm

How do I load the script?

Post by danielareas »

Sir Veillance,

At what time do you run the alarm_loop.pl file? When loading zoneminder?
How do I load it automatically? Is it a service?

Thank you,

Daniel
User avatar
Sir Veillance
Posts: 11
Joined: Tue Nov 13, 2007 3:44 am
Location: Germany

Re: How do I load the script?

Post by Sir Veillance »

danielareas wrote:At what time do you run the alarm_loop.pl file?
at the moment (still testing), I run it manually (from a console):
./alarm_loop.pl &
("&" means: in the background)

Later, I am going to start it from a console script together with ZM. This script can be placed in the /etc/init.d folder (may differ in your distribution)
Martin from Germany
Locked